1: /* DO NOT EDIT!
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.
6: **
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.
11: **
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.
14: **
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.
18: */
19: /*
20: ** 2001 September 15
21: **
22: ** The author disclaims copyright to this source code. In place of
23: ** a legal notice, here is a blessing:
24: **
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.
28: **
29: *************************************************************************
30: ** This file contains code to implement the "sqlite" command line
31: ** utility for accessing SQLite databases.
32: */
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
36: #endif
37:
38: /*
39: ** Determine if we are dealing with WinRT, which provides only a subset of
40: ** the full Win32 API.
41: */
42: #if !defined(SQLITE_OS_WINRT)
43: # define SQLITE_OS_WINRT 0
44: #endif
45:
46: /*
47: ** Warning pragmas copied from msvc.h in the core.
48: */
49: #if defined(_MSC_VER)
50: #pragma warning(disable : 4054)
51: #pragma warning(disable : 4055)
52: #pragma warning(disable : 4100)
53: #pragma warning(disable : 4127)
54: #pragma warning(disable : 4130)
55: #pragma warning(disable : 4152)
56: #pragma warning(disable : 4189)
57: #pragma warning(disable : 4206)
58: #pragma warning(disable : 4210)
59: #pragma warning(disable : 4232)
60: #pragma warning(disable : 4244)
61: #pragma warning(disable : 4305)
62: #pragma warning(disable : 4306)
63: #pragma warning(disable : 4702)
64: #pragma warning(disable : 4706)
65: #endif /* defined(_MSC_VER) */
66:
67: /*
68: ** No support for loadable extensions in VxWorks.
69: */
70: #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
71: # define SQLITE_OMIT_LOAD_EXTENSION 1
72: #endif
73:
74: /*
75: ** Enable large-file support for fopen() and friends on unix.
76: */
77: #ifndef SQLITE_DISABLE_LFS
78: # define _LARGE_FILE 1
79: # ifndef _FILE_OFFSET_BITS
80: # define _FILE_OFFSET_BITS 64
81: # endif
82: # define _LARGEFILE_SOURCE 1
83: #endif
84:
85: #include <stdlib.h>
86: #include <string.h>
87: #include <stdio.h>
88: #include <assert.h>
89: #include "sqlite3.h"
90: typedef sqlite3_int64 i64;
91: typedef sqlite3_uint64 u64;
92: typedef unsigned char u8;
93: #if SQLITE_USER_AUTHENTICATION
94: # include "sqlite3userauth.h"
95: #endif
96: #include <ctype.h>
97: #include <stdarg.h>
98:
99: #if !defined(_WIN32) && !defined(WIN32)
100: # include <signal.h>
101: # if !defined(__RTP__) && !defined(_WRS_KERNEL)
102: # include <pwd.h>
103: # endif
104: #endif
105: #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
106: # include <unistd.h>
107: # include <dirent.h>
108: # define GETPID getpid
109: # if defined(__MINGW32__)
110: # define DIRENT dirent
111: # ifndef S_ISLNK
112: # define S_ISLNK(mode) (0)
113: # endif
114: # endif
115: #else
116: # define GETPID (int)GetCurrentProcessId
117: #endif
118: #include <sys/types.h>
119: #include <sys/stat.h>
120:
121: #if HAVE_READLINE
122: # include <readline/readline.h>
123: # include <readline/history.h>
124: #endif
125:
126: #if HAVE_EDITLINE
127: # include <editline/readline.h>
128: #endif
129:
130: #if HAVE_EDITLINE || HAVE_READLINE
131:
132: # define shell_add_history(X) add_history(X)
133: # define shell_read_history(X) read_history(X)
134: # define shell_write_history(X) write_history(X)
135: # define shell_stifle_history(X) stifle_history(X)
136: # define shell_readline(X) readline(X)
137:
138: #elif HAVE_LINENOISE
139:
140: # include "linenoise.h"
141: # define shell_add_history(X) linenoiseHistoryAdd(X)
142: # define shell_read_history(X) linenoiseHistoryLoad(X)
143: # define shell_write_history(X) linenoiseHistorySave(X)
144: # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
145: # define shell_readline(X) linenoise(X)
146:
147: #else
148:
149: # define shell_read_history(X)
150: # define shell_write_history(X)
151: # define shell_stifle_history(X)
152:
153: # define SHELL_USE_LOCAL_GETLINE 1
154: #endif
155:
156:
157: #if defined(_WIN32) || defined(WIN32)
158: # if SQLITE_OS_WINRT
159: # define SQLITE_OMIT_POPEN 1
160: # else
161: # include <io.h>
162: # include <fcntl.h>
163: # define isatty(h) _isatty(h)
164: # ifndef access
165: # define access(f,m) _access((f),(m))
166: # endif
167: # ifndef unlink
168: # define unlink _unlink
169: # endif
170: # ifndef strdup
171: # define strdup _strdup
172: # endif
173: # undef popen
174: # define popen _popen
175: # undef pclose
176: # define pclose _pclose
177: # endif
178: #else
179: /* Make sure isatty() has a prototype. */
180: extern int isatty(int);
181:
182: # if !defined(__RTP__) && !defined(_WRS_KERNEL)
183: /* popen and pclose are not C89 functions and so are
184: ** sometimes omitted from the <stdio.h> header */
185: extern FILE *popen(const char*,const char*);
186: extern int pclose(FILE*);
187: # else
188: # define SQLITE_OMIT_POPEN 1
189: # endif
190: #endif
191:
192: #if defined(_WIN32_WCE)
193: /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
194: * thus we always assume that we have a console. That can be
195: * overridden with the -batch command line option.
196: */
197: #define isatty(x) 1
198: #endif
199:
200: /* ctype macros that work with signed characters */
201: #define IsSpace(X) isspace((unsigned char)X)
202: #define IsDigit(X) isdigit((unsigned char)X)
203: #define ToLower(X) (char)tolower((unsigned char)X)
204:
205: #if defined(_WIN32) || defined(WIN32)
206: #if SQLITE_OS_WINRT
207: #include <intrin.h>
208: #endif
209: #include <windows.h>
210:
211: /* string conversion routines only needed on Win32 */
212: extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
213: extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
214: extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
215: extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
216: #endif
217:
218: /* On Windows, we normally run with output mode of TEXT so that \n characters
219: ** are automatically translated into \r\n. However, this behavior needs
220: ** to be disabled in some cases (ex: when generating CSV output and when
221: ** rendering quoted strings that contain \n characters). The following
222: ** routines take care of that.
223: */
224: #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
225: static void setBinaryMode(FILE *file, int isOutput){
226: if( isOutput ) fflush(file);
227: _setmode(_fileno(file), _O_BINARY);
228: }
229: static void setTextMode(FILE *file, int isOutput){
230: if( isOutput ) fflush(file);
231: _setmode(_fileno(file), _O_TEXT);
232: }
233: #else
234: # define setBinaryMode(X,Y)
235: # define setTextMode(X,Y)
236: #endif
237:
238:
239: /* True if the timer is enabled */
240: static int enableTimer = 0;
241:
242: /* Return the current wall-clock time */
243: static sqlite3_int64 timeOfDay(void){
244: static sqlite3_vfs *clockVfs = 0;
245: sqlite3_int64 t;
246: if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
247: if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
248: clockVfs->xCurrentTimeInt64(clockVfs, &t);
249: }else{
250: double r;
251: clockVfs->xCurrentTime(clockVfs, &r);
252: t = (sqlite3_int64)(r*86400000.0);
253: }
254: return t;
255: }
256:
257: #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
258: #include <sys/time.h>
259: #include <sys/resource.h>
260:
261: /* VxWorks does not support getrusage() as far as we can determine */
262: #if defined(_WRS_KERNEL) || defined(__RTP__)
263: struct rusage {
264: struct timeval ru_utime; /* user CPU time used */
265: struct timeval ru_stime; /* system CPU time used */
266: };
267: #define getrusage(A,B) memset(B,0,sizeof(*B))
268: #endif
269:
270: /* Saved resource information for the beginning of an operation */
271: static struct rusage sBegin; /* CPU time at start */
272: static sqlite3_int64 iBegin; /* Wall-clock time at start */
273:
274: /*
275: ** Begin timing an operation
276: */
277: static void beginTimer(void){
278: if( enableTimer ){
279: getrusage(RUSAGE_SELF, &sBegin);
280: iBegin = timeOfDay();
281: }
282: }
283:
284: /* Return the difference of two time_structs in seconds */
285: static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
286: return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
287: (double)(pEnd->tv_sec - pStart->tv_sec);
288: }
289:
290: /*
291: ** Print the timing results.
292: */
293: static void endTimer(void){
294: if( enableTimer ){
295: sqlite3_int64 iEnd = timeOfDay();
296: struct rusage sEnd;
297: getrusage(RUSAGE_SELF, &sEnd);
298: printf("Run Time: real %.3f user %f sys %f\n",
299: (iEnd - iBegin)*0.001,
300: timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
301: timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
302: }
303: }
304:
305: #define BEGIN_TIMER beginTimer()
306: #define END_TIMER endTimer()
307: #define HAS_TIMER 1
308:
309: #elif (defined(_WIN32) || defined(WIN32))
310:
311: /* Saved resource information for the beginning of an operation */
312: static HANDLE hProcess;
313: static FILETIME ftKernelBegin;
314: static FILETIME ftUserBegin;
315: static sqlite3_int64 ftWallBegin;
316: typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
317: LPFILETIME, LPFILETIME);
318: static GETPROCTIMES getProcessTimesAddr = NULL;
319:
320: /*
321: ** Check to see if we have timer support. Return 1 if necessary
322: ** support found (or found previously).
323: */
324: static int hasTimer(void){
325: if( getProcessTimesAddr ){
326: return 1;
327: } else {
328: #if !SQLITE_OS_WINRT
329: /* GetProcessTimes() isn't supported in WIN95 and some other Windows
330: ** versions. See if the version we are running on has it, and if it
331: ** does, save off a pointer to it and the current process handle.
332: */
333: hProcess = GetCurrentProcess();
334: if( hProcess ){
335: HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
336: if( NULL != hinstLib ){
337: getProcessTimesAddr =
338: (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
339: if( NULL != getProcessTimesAddr ){
340: return 1;
341: }
342: FreeLibrary(hinstLib);
343: }
344: }
345: #endif
346: }
347: return 0;
348: }
349:
350: /*
351: ** Begin timing an operation
352: */
353: static void beginTimer(void){
354: if( enableTimer && getProcessTimesAddr ){
355: FILETIME ftCreation, ftExit;
356: getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
357: &ftKernelBegin,&ftUserBegin);
358: ftWallBegin = timeOfDay();
359: }
360: }
361:
362: /* Return the difference of two FILETIME structs in seconds */
363: static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
364: sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
365: sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
366: return (double) ((i64End - i64Start) / 10000000.0);
367: }
368:
369: /*
370: ** Print the timing results.
371: */
372: static void endTimer(void){
373: if( enableTimer && getProcessTimesAddr){
374: FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
375: sqlite3_int64 ftWallEnd = timeOfDay();
376: getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
377: printf("Run Time: real %.3f user %f sys %f\n",
378: (ftWallEnd - ftWallBegin)*0.001,
379: timeDiff(&ftUserBegin, &ftUserEnd),
380: timeDiff(&ftKernelBegin, &ftKernelEnd));
381: }
382: }
383:
384: #define BEGIN_TIMER beginTimer()
385: #define END_TIMER endTimer()
386: #define HAS_TIMER hasTimer()
387:
388: #else
389: #define BEGIN_TIMER
390: #define END_TIMER
391: #define HAS_TIMER 0
392: #endif
393:
394: /*
395: ** Used to prevent warnings about unused parameters
396: */
397: #define UNUSED_PARAMETER(x) (void)(x)
398:
399: /*
400: ** Number of elements in an array
401: */
402: #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
403:
404: /*
405: ** If the following flag is set, then command execution stops
406: ** at an error if we are not interactive.
407: */
408: static int bail_on_error = 0;
409:
410: /*
411: ** Threat stdin as an interactive input if the following variable
412: ** is true. Otherwise, assume stdin is connected to a file or pipe.
413: */
414: static int stdin_is_interactive = 1;
415:
416: /*
417: ** On Windows systems we have to know if standard output is a console
418: ** in order to translate UTF-8 into MBCS. The following variable is
419: ** true if translation is required.
420: */
421: static int stdout_is_console = 1;
422:
423: /*
424: ** The following is the open SQLite database. We make a pointer
425: ** to this database a static variable so that it can be accessed
426: ** by the SIGINT handler to interrupt database processing.
427: */
428: static sqlite3 *globalDb = 0;
429:
430: /*
431: ** True if an interrupt (Control-C) has been received.
432: */
433: static volatile int seenInterrupt = 0;
434:
435: #ifdef SQLITE_DEBUG
436: /*
437: ** Out-of-memory simulator variables
438: */
439: static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
440: static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
441: static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
442: #endif /* SQLITE_DEBUG */
443:
444: /*
445: ** This is the name of our program. It is set in main(), used
446: ** in a number of other places, mostly for error messages.
447: */
448: static char *Argv0;
449:
450: /*
451: ** Prompt strings. Initialized in main. Settable with
452: ** .prompt main continue
453: */
454: static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
455: static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
456:
457: /*
458: ** Render output like fprintf(). Except, if the output is going to the
459: ** console and if this is running on a Windows machine, translate the
460: ** output from UTF-8 into MBCS.
461: */
462: #if defined(_WIN32) || defined(WIN32)
463: void utf8_printf(FILE *out, const char *zFormat, ...){
464: va_list ap;
465: va_start(ap, zFormat);
466: if( stdout_is_console && (out==stdout || out==stderr) ){
467: char *z1 = sqlite3_vmprintf(zFormat, ap);
468: char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
469: sqlite3_free(z1);
470: fputs(z2, out);
471: sqlite3_free(z2);
472: }else{
473: vfprintf(out, zFormat, ap);
474: }
475: va_end(ap);
476: }
477: #elif !defined(utf8_printf)
478: # define utf8_printf fprintf
479: #endif
480:
481: /*
482: ** Render output like fprintf(). This should not be used on anything that
483: ** includes string formatting (e.g. "%s").
484: */
485: #if !defined(raw_printf)
486: # define raw_printf fprintf
487: #endif
488:
489: /* Indicate out-of-memory and exit. */
490: static void shell_out_of_memory(void){
491: raw_printf(stderr,"Error: out of memory\n");
492: exit(1);
493: }
494:
495: #ifdef SQLITE_DEBUG
496: /* This routine is called when a simulated OOM occurs. It is broken
497: ** out as a separate routine to make it easy to set a breakpoint on
498: ** the OOM
499: */
500: void shellOomFault(void){
501: if( oomRepeat>0 ){
502: oomRepeat--;
503: }else{
504: oomCounter--;
505: }
506: }
507: #endif /* SQLITE_DEBUG */
508:
509: #ifdef SQLITE_DEBUG
510: /* This routine is a replacement malloc() that is used to simulate
511: ** Out-Of-Memory (OOM) errors for testing purposes.
512: */
513: static void *oomMalloc(int nByte){
514: if( oomCounter ){
515: if( oomCounter==1 ){
516: shellOomFault();
517: return 0;
518: }else{
519: oomCounter--;
520: }
521: }
522: return defaultMalloc(nByte);
523: }
524: #endif /* SQLITE_DEBUG */
525:
526: #ifdef SQLITE_DEBUG
527: /* Register the OOM simulator. This must occur before any memory
528: ** allocations */
529: static void registerOomSimulator(void){
530: sqlite3_mem_methods mem;
531: sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
532: defaultMalloc = mem.xMalloc;
533: mem.xMalloc = oomMalloc;
534: sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
535: }
536: #endif
537:
538: /*
539: ** Write I/O traces to the following stream.
540: */
541: #ifdef SQLITE_ENABLE_IOTRACE
542: static FILE *iotrace = 0;
543: #endif
544:
545: /*
546: ** This routine works like printf in that its first argument is a
547: ** format string and subsequent arguments are values to be substituted
548: ** in place of % fields. The result of formatting this string
549: ** is written to iotrace.
550: */
551: #ifdef SQLITE_ENABLE_IOTRACE
552: static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
553: va_list ap;
554: char *z;
555: if( iotrace==0 ) return;
556: va_start(ap, zFormat);
557: z = sqlite3_vmprintf(zFormat, ap);
558: va_end(ap);
559: utf8_printf(iotrace, "%s", z);
560: sqlite3_free(z);
561: }
562: #endif
563:
564: /*
565: ** Output string zUtf to stream pOut as w characters. If w is negative,
566: ** then right-justify the text. W is the width in UTF-8 characters, not
567: ** in bytes. This is different from the %*.*s specification in printf
568: ** since with %*.*s the width is measured in bytes, not characters.
569: */
570: static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
571: int i;
572: int n;
573: int aw = w<0 ? -w : w;
574: for(i=n=0; zUtf[i]; i++){
575: if( (zUtf[i]&0xc0)!=0x80 ){
576: n++;
577: if( n==aw ){
578: do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
579: break;
580: }
581: }
582: }
583: if( n>=aw ){
584: utf8_printf(pOut, "%.*s", i, zUtf);
585: }else if( w<0 ){
586: utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
587: }else{
588: utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
589: }
590: }
591:
592:
593: /*
594: ** Determines if a string is a number of not.
595: */
596: static int isNumber(const char *z, int *realnum){
597: if( *z=='-' || *z=='+' ) z++;
598: if( !IsDigit(*z) ){
599: return 0;
600: }
601: z++;
602: if( realnum ) *realnum = 0;
603: while( IsDigit(*z) ){ z++; }
604: if( *z=='.' ){
605: z++;
606: if( !IsDigit(*z) ) return 0;
607: while( IsDigit(*z) ){ z++; }
608: if( realnum ) *realnum = 1;
609: }
610: if( *z=='e' || *z=='E' ){
611: z++;
612: if( *z=='+' || *z=='-' ) z++;
613: if( !IsDigit(*z) ) return 0;
614: while( IsDigit(*z) ){ z++; }
615: if( realnum ) *realnum = 1;
616: }
617: return *z==0;
618: }
619:
620: /*
621: ** Compute a string length that is limited to what can be stored in
622: ** lower 30 bits of a 32-bit signed integer.
623: */
624: static int strlen30(const char *z){
625: const char *z2 = z;
626: while( *z2 ){ z2++; }
627: return 0x3fffffff & (int)(z2 - z);
628: }
629:
630: /*
631: ** Return the length of a string in characters. Multibyte UTF8 characters
632: ** count as a single character.
633: */
634: static int strlenChar(const char *z){
635: int n = 0;
636: while( *z ){
637: if( (0xc0&*(z++))!=0x80 ) n++;
638: }
639: return n;
640: }
641:
642: /*
643: ** Return true if zFile does not exist or if it is not an ordinary file.
644: */
645: #ifdef _WIN32
646: # define notNormalFile(X) 0
647: #else
648: static int notNormalFile(const char *zFile){
649: struct stat x;
650: int rc;
651: memset(&x, 0, sizeof(x));
652: rc = stat(zFile, &x);
653: return rc || !S_ISREG(x.st_mode);
654: }
655: #endif
656:
657: /*
658: ** This routine reads a line of text from FILE in, stores
659: ** the text in memory obtained from malloc() and returns a pointer
660: ** to the text. NULL is returned at end of file, or if malloc()
661: ** fails.
662: **
663: ** If zLine is not NULL then it is a malloced buffer returned from
664: ** a previous call to this routine that may be reused.
665: */
666: static char *local_getline(char *zLine, FILE *in){
667: int nLine = zLine==0 ? 0 : 100;
668: int n = 0;
669:
670: while( 1 ){
671: if( n+100>nLine ){
672: nLine = nLine*2 + 100;
673: zLine = realloc(zLine, nLine);
674: if( zLine==0 ) shell_out_of_memory();
675: }
676: if( fgets(&zLine[n], nLine - n, in)==0 ){
677: if( n==0 ){
678: free(zLine);
679: return 0;
680: }
681: zLine[n] = 0;
682: break;
683: }
684: while( zLine[n] ) n++;
685: if( n>0 && zLine[n-1]=='\n' ){
686: n--;
687: if( n>0 && zLine[n-1]=='\r' ) n--;
688: zLine[n] = 0;
689: break;
690: }
691: }
692: #if defined(_WIN32) || defined(WIN32)
693: /* For interactive input on Windows systems, translate the
694: ** multi-byte characterset characters into UTF-8. */
695: if( stdin_is_interactive && in==stdin ){
696: char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
697: if( zTrans ){
698: int nTrans = strlen30(zTrans)+1;
699: if( nTrans>nLine ){
700: zLine = realloc(zLine, nTrans);
701: if( zLine==0 ) shell_out_of_memory();
702: }
703: memcpy(zLine, zTrans, nTrans);
704: sqlite3_free(zTrans);
705: }
706: }
707: #endif /* defined(_WIN32) || defined(WIN32) */
708: return zLine;
709: }
710:
711: /*
712: ** Retrieve a single line of input text.
713: **
714: ** If in==0 then read from standard input and prompt before each line.
715: ** If isContinuation is true, then a continuation prompt is appropriate.
716: ** If isContinuation is zero, then the main prompt should be used.
717: **
718: ** If zPrior is not NULL then it is a buffer from a prior call to this
719: ** routine that can be reused.
720: **
721: ** The result is stored in space obtained from malloc() and must either
722: ** be freed by the caller or else passed back into this routine via the
723: ** zPrior argument for reuse.
724: */
725: static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
726: char *zPrompt;
727: char *zResult;
728: if( in!=0 ){
729: zResult = local_getline(zPrior, in);
730: }else{
731: zPrompt = isContinuation ? continuePrompt : mainPrompt;
732: #if SHELL_USE_LOCAL_GETLINE
733: printf("%s", zPrompt);
734: fflush(stdout);
735: zResult = local_getline(zPrior, stdin);
736: #else
737: free(zPrior);
738: zResult = shell_readline(zPrompt);
739: if( zResult && *zResult ) shell_add_history(zResult);
740: #endif
741: }
742: return zResult;
743: }
744:
745:
746: /*
747: ** Return the value of a hexadecimal digit. Return -1 if the input
748: ** is not a hex digit.
749: */
750: static int hexDigitValue(char c){
751: if( c>='0' && c<='9' ) return c - '0';
752: if( c>='a' && c<='f' ) return c - 'a' + 10;
753: if( c>='A' && c<='F' ) return c - 'A' + 10;
754: return -1;
755: }
756:
757: /*
758: ** Interpret zArg as an integer value, possibly with suffixes.
759: */
760: static sqlite3_int64 integerValue(const char *zArg){
761: sqlite3_int64 v = 0;
762: static const struct { char *zSuffix; int iMult; } aMult[] = {
763: { "KiB", 1024 },
764: { "MiB", 1024*1024 },
765: { "GiB", 1024*1024*1024 },
766: { "KB", 1000 },
767: { "MB", 1000000 },
768: { "GB", 1000000000 },
769: { "K", 1000 },
770: { "M", 1000000 },
771: { "G", 1000000000 },
772: };
773: int i;
774: int isNeg = 0;
775: if( zArg[0]=='-' ){
776: isNeg = 1;
777: zArg++;
778: }else if( zArg[0]=='+' ){
779: zArg++;
780: }
781: if( zArg[0]=='0' && zArg[1]=='x' ){
782: int x;
783: zArg += 2;
784: while( (x = hexDigitValue(zArg[0]))>=0 ){
785: v = (v<<4) + x;
786: zArg++;
787: }
788: }else{
789: while( IsDigit(zArg[0]) ){
790: v = v*10 + zArg[0] - '0';
791: zArg++;
792: }
793: }
794: for(i=0; i<ArraySize(aMult); i++){
795: if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
796: v *= aMult[i].iMult;
797: break;
798: }
799: }
800: return isNeg? -v : v;
801: }
802:
803: /*
804: ** A variable length string to which one can append text.
805: */
806: typedef struct ShellText ShellText;
807: struct ShellText {
808: char *z;
809: int n;
810: int nAlloc;
811: };
812:
813: /*
814: ** Initialize and destroy a ShellText object
815: */
816: static void initText(ShellText *p){
817: memset(p, 0, sizeof(*p));
818: }
819: static void freeText(ShellText *p){
820: free(p->z);
821: initText(p);
822: }
823:
824: /* zIn is either a pointer to a NULL-terminated string in memory obtained
825: ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
826: ** added to zIn, and the result returned in memory obtained from malloc().
827: ** zIn, if it was not NULL, is freed.
828: **
829: ** If the third argument, quote, is not '\0', then it is used as a
830: ** quote character for zAppend.
831: */
832: static void appendText(ShellText *p, char const *zAppend, char quote){
833: int len;
834: int i;
835: int nAppend = strlen30(zAppend);
836:
837: len = nAppend+p->n+1;
838: if( quote ){
839: len += 2;
840: for(i=0; i<nAppend; i++){
841: if( zAppend[i]==quote ) len++;
842: }
843: }
844:
845: if( p->n+len>=p->nAlloc ){
846: p->nAlloc = p->nAlloc*2 + len + 20;
847: p->z = realloc(p->z, p->nAlloc);
848: if( p->z==0 ) shell_out_of_memory();
849: }
850:
851: if( quote ){
852: char *zCsr = p->z+p->n;
853: *zCsr++ = quote;
854: for(i=0; i<nAppend; i++){
855: *zCsr++ = zAppend[i];
856: if( zAppend[i]==quote ) *zCsr++ = quote;
857: }
858: *zCsr++ = quote;
859: p->n = (int)(zCsr - p->z);
860: *zCsr = '\0';
861: }else{
862: memcpy(p->z+p->n, zAppend, nAppend);
863: p->n += nAppend;
864: p->z[p->n] = '\0';
865: }
866: }
867:
868: /*
869: ** Attempt to determine if identifier zName needs to be quoted, either
870: ** because it contains non-alphanumeric characters, or because it is an
871: ** SQLite keyword. Be conservative in this estimate: When in doubt assume
872: ** that quoting is required.
873: **
874: ** Return '"' if quoting is required. Return 0 if no quoting is required.
875: */
876: static char quoteChar(const char *zName){
877: int i;
878: if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
879: for(i=0; zName[i]; i++){
880: if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
881: }
882: return sqlite3_keyword_check(zName, i) ? '"' : 0;
883: }
884:
885: /*
886: ** Construct a fake object name and column list to describe the structure
887: ** of the view, virtual table, or table valued function zSchema.zName.
888: */
889: static char *shellFakeSchema(
890: sqlite3 *db, /* The database connection containing the vtab */
891: const char *zSchema, /* Schema of the database holding the vtab */
892: const char *zName /* The name of the virtual table */
893: ){
894: sqlite3_stmt *pStmt = 0;
895: char *zSql;
896: ShellText s;
897: char cQuote;
898: char *zDiv = "(";
899: int nRow = 0;
900:
901: zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
902: zSchema ? zSchema : "main", zName);
903: sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
904: sqlite3_free(zSql);
905: initText(&s);
906: if( zSchema ){
907: cQuote = quoteChar(zSchema);
908: if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
909: appendText(&s, zSchema, cQuote);
910: appendText(&s, ".", 0);
911: }
912: cQuote = quoteChar(zName);
913: appendText(&s, zName, cQuote);
914: while( sqlite3_step(pStmt)==SQLITE_ROW ){
915: const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
916: nRow++;
917: appendText(&s, zDiv, 0);
918: zDiv = ",";
919: cQuote = quoteChar(zCol);
920: appendText(&s, zCol, cQuote);
921: }
922: appendText(&s, ")", 0);
923: sqlite3_finalize(pStmt);
924: if( nRow==0 ){
925: freeText(&s);
926: s.z = 0;
927: }
928: return s.z;
929: }
930:
931: /*
932: ** SQL function: shell_module_schema(X)
933: **
934: ** Return a fake schema for the table-valued function or eponymous virtual
935: ** table X.
936: */
937: static void shellModuleSchema(
938: sqlite3_context *pCtx,
939: int nVal,
940: sqlite3_value **apVal
941: ){
942: const char *zName = (const char*)sqlite3_value_text(apVal[0]);
943: char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
944: UNUSED_PARAMETER(nVal);
945: if( zFake ){
946: sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
947: -1, sqlite3_free);
948: free(zFake);
949: }
950: }
951:
952: /*
953: ** SQL function: shell_add_schema(S,X)
954: **
955: ** Add the schema name X to the CREATE statement in S and return the result.
956: ** Examples:
957: **
958: ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
959: **
960: ** Also works on
961: **
962: ** CREATE INDEX
963: ** CREATE UNIQUE INDEX
964: ** CREATE VIEW
965: ** CREATE TRIGGER
966: ** CREATE VIRTUAL TABLE
967: **
968: ** This UDF is used by the .schema command to insert the schema name of
969: ** attached databases into the middle of the sqlite_schema.sql field.
970: */
971: static void shellAddSchemaName(
972: sqlite3_context *pCtx,
973: int nVal,
974: sqlite3_value **apVal
975: ){
976: static const char *aPrefix[] = {
977: "TABLE",
978: "INDEX",
979: "UNIQUE INDEX",
980: "VIEW",
981: "TRIGGER",
982: "VIRTUAL TABLE"
983: };
984: int i = 0;
985: const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
986: const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
987: const char *zName = (const char*)sqlite3_value_text(apVal[2]);
988: sqlite3 *db = sqlite3_context_db_handle(pCtx);
989: UNUSED_PARAMETER(nVal);
990: if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
991: for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
992: int n = strlen30(aPrefix[i]);
993: if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
994: char *z = 0;
995: char *zFake = 0;
996: if( zSchema ){
997: char cQuote = quoteChar(zSchema);
998: if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
999: z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1000: }else{
1001: z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1002: }
1003: }
1004: if( zName
1005: && aPrefix[i][0]=='V'
1006: && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1007: ){
1008: if( z==0 ){
1009: z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1010: }else{
1011: z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1012: }
1013: free(zFake);
1014: }
1015: if( z ){
1016: sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1017: return;
1018: }
1019: }
1020: }
1021: }
1022: sqlite3_result_value(pCtx, apVal[0]);
1023: }
1024:
1025: /*
1026: ** The source code for several run-time loadable extensions is inserted
1027: ** below by the ../tool/mkshellc.tcl script. Before processing that included
1028: ** code, we need to override some macros to make the included program code
1029: ** work here in the middle of this regular program.
1030: */
1031: #define SQLITE_EXTENSION_INIT1
1032: #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1033:
1034: #if defined(_WIN32) && defined(_MSC_VER)
1035: /************************* Begin test_windirent.h ******************/
1036: /*
1037: ** 2015 November 30
1038: **
1039: ** The author disclaims copyright to this source code. In place of
1040: ** a legal notice, here is a blessing:
1041: **
1042: ** May you do good and not evil.
1043: ** May you find forgiveness for yourself and forgive others.
1044: ** May you share freely, never taking more than you give.
1045: **
1046: *************************************************************************
1047: ** This file contains declarations for most of the opendir() family of
1048: ** POSIX functions on Win32 using the MSVCRT.
1049: */
1050:
1051: #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1052: #define SQLITE_WINDIRENT_H
1053:
1054: /*
1055: ** We need several data types from the Windows SDK header.
1056: */
1057:
1058: #ifndef WIN32_LEAN_AND_MEAN
1059: #define WIN32_LEAN_AND_MEAN
1060: #endif
1061:
1062: #include "windows.h"
1063:
1064: /*
1065: ** We need several support functions from the SQLite core.
1066: */
1067:
1068: /* #include "sqlite3.h" */
1069:
1070: /*
1071: ** We need several things from the ANSI and MSVCRT headers.
1072: */
1073:
1074: #include <stdio.h>
1075: #include <stdlib.h>
1076: #include <errno.h>
1077: #include <io.h>
1078: #include <limits.h>
1079: #include <sys/types.h>
1080: #include <sys/stat.h>
1081:
1082: /*
1083: ** We may need several defines that should have been in "sys/stat.h".
1084: */
1085:
1086: #ifndef S_ISREG
1087: #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1088: #endif
1089:
1090: #ifndef S_ISDIR
1091: #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1092: #endif
1093:
1094: #ifndef S_ISLNK
1095: #define S_ISLNK(mode) (0)
1096: #endif
1097:
1098: /*
1099: ** We may need to provide the "mode_t" type.
1100: */
1101:
1102: #ifndef MODE_T_DEFINED
1103: #define MODE_T_DEFINED
1104: typedef unsigned short mode_t;
1105: #endif
1106:
1107: /*
1108: ** We may need to provide the "ino_t" type.
1109: */
1110:
1111: #ifndef INO_T_DEFINED
1112: #define INO_T_DEFINED
1113: typedef unsigned short ino_t;
1114: #endif
1115:
1116: /*
1117: ** We need to define "NAME_MAX" if it was not present in "limits.h".
1118: */
1119:
1120: #ifndef NAME_MAX
1121: # ifdef FILENAME_MAX
1122: # define NAME_MAX (FILENAME_MAX)
1123: # else
1124: # define NAME_MAX (260)
1125: # endif
1126: #endif
1127:
1128: /*
1129: ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1130: */
1131:
1132: #ifndef NULL_INTPTR_T
1133: # define NULL_INTPTR_T ((intptr_t)(0))
1134: #endif
1135:
1136: #ifndef BAD_INTPTR_T
1137: # define BAD_INTPTR_T ((intptr_t)(-1))
1138: #endif
1139:
1140: /*
1141: ** We need to provide the necessary structures and related types.
1142: */
1143:
1144: #ifndef DIRENT_DEFINED
1145: #define DIRENT_DEFINED
1146: typedef struct DIRENT DIRENT;
1147: typedef DIRENT *LPDIRENT;
1148: struct DIRENT {
1149: ino_t d_ino; /* Sequence number, do not use. */
1150: unsigned d_attributes; /* Win32 file attributes. */
1151: char d_name[NAME_MAX + 1]; /* Name within the directory. */
1152: };
1153: #endif
1154:
1155: #ifndef DIR_DEFINED
1156: #define DIR_DEFINED
1157: typedef struct DIR DIR;
1158: typedef DIR *LPDIR;
1159: struct DIR {
1160: intptr_t d_handle; /* Value returned by "_findfirst". */
1161: DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1162: DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1163: };
1164: #endif
1165:
1166: /*
1167: ** Provide a macro, for use by the implementation, to determine if a
1168: ** particular directory entry should be skipped over when searching for
1169: ** the next directory entry that should be returned by the readdir() or
1170: ** readdir_r() functions.
1171: */
1172:
1173: #ifndef is_filtered
1174: # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1175: #endif
1176:
1177: /*
1178: ** Provide the function prototype for the POSIX compatiable getenv()
1179: ** function. This function is not thread-safe.
1180: */
1181:
1182: extern const char *windirent_getenv(const char *name);
1183:
1184: /*
1185: ** Finally, we can provide the function prototypes for the opendir(),
1186: ** readdir(), readdir_r(), and closedir() POSIX functions.
1187: */
1188:
1189: extern LPDIR opendir(const char *dirname);
1190: extern LPDIRENT readdir(LPDIR dirp);
1191: extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1192: extern INT closedir(LPDIR dirp);
1193:
1194: #endif /* defined(WIN32) && defined(_MSC_VER) */
1195:
1196: /************************* End test_windirent.h ********************/
1197: /************************* Begin test_windirent.c ******************/
1198: /*
1199: ** 2015 November 30
1200: **
1201: ** The author disclaims copyright to this source code. In place of
1202: ** a legal notice, here is a blessing:
1203: **
1204: ** May you do good and not evil.
1205: ** May you find forgiveness for yourself and forgive others.
1206: ** May you share freely, never taking more than you give.
1207: **
1208: *************************************************************************
1209: ** This file contains code to implement most of the opendir() family of
1210: ** POSIX functions on Win32 using the MSVCRT.
1211: */
1212:
1213: #if defined(_WIN32) && defined(_MSC_VER)
1214: /* #include "test_windirent.h" */
1215:
1216: /*
1217: ** Implementation of the POSIX getenv() function using the Win32 API.
1218: ** This function is not thread-safe.
1219: */
1220: const char *windirent_getenv(
1221: const char *name
1222: ){
1223: static char value[32768]; /* Maximum length, per MSDN */
1224: DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1225: DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1226:
1227: memset(value, 0, sizeof(value));
1228: dwRet = GetEnvironmentVariableA(name, value, dwSize);
1229: if( dwRet==0 || dwRet>dwSize ){
1230: /*
1231: ** The function call to GetEnvironmentVariableA() failed -OR-
1232: ** the buffer is not large enough. Either way, return NULL.
1233: */
1234: return 0;
1235: }else{
1236: /*
1237: ** The function call to GetEnvironmentVariableA() succeeded
1238: ** -AND- the buffer contains the entire value.
1239: */
1240: return value;
1241: }
1242: }
1243:
1244: /*
1245: ** Implementation of the POSIX opendir() function using the MSVCRT.
1246: */
1247: LPDIR opendir(
1248: const char *dirname
1249: ){
1250: struct _finddata_t data;
1251: LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1252: SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1253:
1254: if( dirp==NULL ) return NULL;
1255: memset(dirp, 0, sizeof(DIR));
1256:
1257: /* TODO: Remove this if Unix-style root paths are not used. */
1258: if( sqlite3_stricmp(dirname, "/")==0 ){
1259: dirname = windirent_getenv("SystemDrive");
1260: }
1261:
1262: memset(&data, 0, sizeof(struct _finddata_t));
1263: _snprintf(data.name, namesize, "%s\\*", dirname);
1264: dirp->d_handle = _findfirst(data.name, &data);
1265:
1266: if( dirp->d_handle==BAD_INTPTR_T ){
1267: closedir(dirp);
1268: return NULL;
1269: }
1270:
1271: /* TODO: Remove this block to allow hidden and/or system files. */
1272: if( is_filtered(data) ){
1273: next:
1274:
1275: memset(&data, 0, sizeof(struct _finddata_t));
1276: if( _findnext(dirp->d_handle, &data)==-1 ){
1277: closedir(dirp);
1278: return NULL;
1279: }
1280:
1281: /* TODO: Remove this block to allow hidden and/or system files. */
1282: if( is_filtered(data) ) goto next;
1283: }
1284:
1285: dirp->d_first.d_attributes = data.attrib;
1286: strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1287: dirp->d_first.d_name[NAME_MAX] = '\0';
1288:
1289: return dirp;
1290: }
1291:
1292: /*
1293: ** Implementation of the POSIX readdir() function using the MSVCRT.
1294: */
1295: LPDIRENT readdir(
1296: LPDIR dirp
1297: ){
1298: struct _finddata_t data;
1299:
1300: if( dirp==NULL ) return NULL;
1301:
1302: if( dirp->d_first.d_ino==0 ){
1303: dirp->d_first.d_ino++;
1304: dirp->d_next.d_ino++;
1305:
1306: return &dirp->d_first;
1307: }
1308:
1309: next:
1310:
1311: memset(&data, 0, sizeof(struct _finddata_t));
1312: if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1313:
1314: /* TODO: Remove this block to allow hidden and/or system files. */
1315: if( is_filtered(data) ) goto next;
1316:
1317: dirp->d_next.d_ino++;
1318: dirp->d_next.d_attributes = data.attrib;
1319: strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1320: dirp->d_next.d_name[NAME_MAX] = '\0';
1321:
1322: return &dirp->d_next;
1323: }
1324:
1325: /*
1326: ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1327: */
1328: INT readdir_r(
1329: LPDIR dirp,
1330: LPDIRENT entry,
1331: LPDIRENT *result
1332: ){
1333: struct _finddata_t data;
1334:
1335: if( dirp==NULL ) return EBADF;
1336:
1337: if( dirp->d_first.d_ino==0 ){
1338: dirp->d_first.d_ino++;
1339: dirp->d_next.d_ino++;
1340:
1341: entry->d_ino = dirp->d_first.d_ino;
1342: entry->d_attributes = dirp->d_first.d_attributes;
1343: strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1344: entry->d_name[NAME_MAX] = '\0';
1345:
1346: *result = entry;
1347: return 0;
1348: }
1349:
1350: next:
1351:
1352: memset(&data, 0, sizeof(struct _finddata_t));
1353: if( _findnext(dirp->d_handle, &data)==-1 ){
1354: *result = NULL;
1355: return ENOENT;
1356: }
1357:
1358: /* TODO: Remove this block to allow hidden and/or system files. */
1359: if( is_filtered(data) ) goto next;
1360:
1361: entry->d_ino = (ino_t)-1; /* not available */
1362: entry->d_attributes = data.attrib;
1363: strncpy(entry->d_name, data.name, NAME_MAX);
1364: entry->d_name[NAME_MAX] = '\0';
1365:
1366: *result = entry;
1367: return 0;
1368: }
1369:
1370: /*
1371: ** Implementation of the POSIX closedir() function using the MSVCRT.
1372: */
1373: INT closedir(
1374: LPDIR dirp
1375: ){
1376: INT result = 0;
1377:
1378: if( dirp==NULL ) return EINVAL;
1379:
1380: if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1381: result = _findclose(dirp->d_handle);
1382: }
1383:
1384: sqlite3_free(dirp);
1385: return result;
1386: }
1387:
1388: #endif /* defined(WIN32) && defined(_MSC_VER) */
1389:
1390: /************************* End test_windirent.c ********************/
1391: #define dirent DIRENT
1392: #endif
1393: /************************* Begin ../ext/misc/shathree.c ******************/
1394: /*
1395: ** 2017-03-08
1396: **
1397: ** The author disclaims copyright to this source code. In place of
1398: ** a legal notice, here is a blessing:
1399: **
1400: ** May you do good and not evil.
1401: ** May you find forgiveness for yourself and forgive others.
1402: ** May you share freely, never taking more than you give.
1403: **
1404: ******************************************************************************
1405: **
1406: ** This SQLite extension implements functions that compute SHA3 hashes.
1407: ** Two SQL functions are implemented:
1408: **
1409: ** sha3(X,SIZE)
1410: ** sha3_query(Y,SIZE)
1411: **
1412: ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1413: ** X is NULL.
1414: **
1415: ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1416: ** and returns a hash of their results.
1417: **
1418: ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1419: ** is used. If SIZE is included it must be one of the integers 224, 256,
1420: ** 384, or 512, to determine SHA3 hash variant that is computed.
1421: */
1422: /* #include "sqlite3ext.h" */
1423: SQLITE_EXTENSION_INIT1
1424: #include <assert.h>
1425: #include <string.h>
1426: #include <stdarg.h>
1427:
1428: #ifndef SQLITE_AMALGAMATION
1429: /* typedef sqlite3_uint64 u64; */
1430: #endif /* SQLITE_AMALGAMATION */
1431:
1432: /******************************************************************************
1433: ** The Hash Engine
1434: */
1435: /*
1436: ** Macros to determine whether the machine is big or little endian,
1437: ** and whether or not that determination is run-time or compile-time.
1438: **
1439: ** For best performance, an attempt is made to guess at the byte-order
1440: ** using C-preprocessor macros. If that is unsuccessful, or if
1441: ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1442: ** at run-time.
1443: */
1444: #ifndef SHA3_BYTEORDER
1445: # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1446: defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1447: defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1448: defined(__arm__)
1449: # define SHA3_BYTEORDER 1234
1450: # elif defined(sparc) || defined(__ppc__)
1451: # define SHA3_BYTEORDER 4321
1452: # else
1453: # define SHA3_BYTEORDER 0
1454: # endif
1455: #endif
1456:
1457:
1458: /*
1459: ** State structure for a SHA3 hash in progress
1460: */
1461: typedef struct SHA3Context SHA3Context;
1462: struct SHA3Context {
1463: union {
1464: u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1465: unsigned char x[1600]; /* ... or 1600 bytes */
1466: } u;
1467: unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1468: unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1469: unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1470: };
1471:
1472: /*
1473: ** A single step of the Keccak mixing function for a 1600-bit state
1474: */
1475: static void KeccakF1600Step(SHA3Context *p){
1476: int i;
1477: u64 b0, b1, b2, b3, b4;
1478: u64 c0, c1, c2, c3, c4;
1479: u64 d0, d1, d2, d3, d4;
1480: static const u64 RC[] = {
1481: 0x0000000000000001ULL, 0x0000000000008082ULL,
1482: 0x800000000000808aULL, 0x8000000080008000ULL,
1483: 0x000000000000808bULL, 0x0000000080000001ULL,
1484: 0x8000000080008081ULL, 0x8000000000008009ULL,
1485: 0x000000000000008aULL, 0x0000000000000088ULL,
1486: 0x0000000080008009ULL, 0x000000008000000aULL,
1487: 0x000000008000808bULL, 0x800000000000008bULL,
1488: 0x8000000000008089ULL, 0x8000000000008003ULL,
1489: 0x8000000000008002ULL, 0x8000000000000080ULL,
1490: 0x000000000000800aULL, 0x800000008000000aULL,
1491: 0x8000000080008081ULL, 0x8000000000008080ULL,
1492: 0x0000000080000001ULL, 0x8000000080008008ULL
1493: };
1494: # define a00 (p->u.s[0])
1495: # define a01 (p->u.s[1])
1496: # define a02 (p->u.s[2])
1497: # define a03 (p->u.s[3])
1498: # define a04 (p->u.s[4])
1499: # define a10 (p->u.s[5])
1500: # define a11 (p->u.s[6])
1501: # define a12 (p->u.s[7])
1502: # define a13 (p->u.s[8])
1503: # define a14 (p->u.s[9])
1504: # define a20 (p->u.s[10])
1505: # define a21 (p->u.s[11])
1506: # define a22 (p->u.s[12])
1507: # define a23 (p->u.s[13])
1508: # define a24 (p->u.s[14])
1509: # define a30 (p->u.s[15])
1510: # define a31 (p->u.s[16])
1511: # define a32 (p->u.s[17])
1512: # define a33 (p->u.s[18])
1513: # define a34 (p->u.s[19])
1514: # define a40 (p->u.s[20])
1515: # define a41 (p->u.s[21])
1516: # define a42 (p->u.s[22])
1517: # define a43 (p->u.s[23])
1518: # define a44 (p->u.s[24])
1519: # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1520:
1521: for(i=0; i<24; i+=4){
1522: c0 = a00^a10^a20^a30^a40;
1523: c1 = a01^a11^a21^a31^a41;
1524: c2 = a02^a12^a22^a32^a42;
1525: c3 = a03^a13^a23^a33^a43;
1526: c4 = a04^a14^a24^a34^a44;
1527: d0 = c4^ROL64(c1, 1);
1528: d1 = c0^ROL64(c2, 1);
1529: d2 = c1^ROL64(c3, 1);
1530: d3 = c2^ROL64(c4, 1);
1531: d4 = c3^ROL64(c0, 1);
1532:
1533: b0 = (a00^d0);
1534: b1 = ROL64((a11^d1), 44);
1535: b2 = ROL64((a22^d2), 43);
1536: b3 = ROL64((a33^d3), 21);
1537: b4 = ROL64((a44^d4), 14);
1538: a00 = b0 ^((~b1)& b2 );
1539: a00 ^= RC[i];
1540: a11 = b1 ^((~b2)& b3 );
1541: a22 = b2 ^((~b3)& b4 );
1542: a33 = b3 ^((~b4)& b0 );
1543: a44 = b4 ^((~b0)& b1 );
1544:
1545: b2 = ROL64((a20^d0), 3);
1546: b3 = ROL64((a31^d1), 45);
1547: b4 = ROL64((a42^d2), 61);
1548: b0 = ROL64((a03^d3), 28);
1549: b1 = ROL64((a14^d4), 20);
1550: a20 = b0 ^((~b1)& b2 );
1551: a31 = b1 ^((~b2)& b3 );
1552: a42 = b2 ^((~b3)& b4 );
1553: a03 = b3 ^((~b4)& b0 );
1554: a14 = b4 ^((~b0)& b1 );
1555:
1556: b4 = ROL64((a40^d0), 18);
1557: b0 = ROL64((a01^d1), 1);
1558: b1 = ROL64((a12^d2), 6);
1559: b2 = ROL64((a23^d3), 25);
1560: b3 = ROL64((a34^d4), 8);
1561: a40 = b0 ^((~b1)& b2 );
1562: a01 = b1 ^((~b2)& b3 );
1563: a12 = b2 ^((~b3)& b4 );
1564: a23 = b3 ^((~b4)& b0 );
1565: a34 = b4 ^((~b0)& b1 );
1566:
1567: b1 = ROL64((a10^d0), 36);
1568: b2 = ROL64((a21^d1), 10);
1569: b3 = ROL64((a32^d2), 15);
1570: b4 = ROL64((a43^d3), 56);
1571: b0 = ROL64((a04^d4), 27);
1572: a10 = b0 ^((~b1)& b2 );
1573: a21 = b1 ^((~b2)& b3 );
1574: a32 = b2 ^((~b3)& b4 );
1575: a43 = b3 ^((~b4)& b0 );
1576: a04 = b4 ^((~b0)& b1 );
1577:
1578: b3 = ROL64((a30^d0), 41);
1579: b4 = ROL64((a41^d1), 2);
1580: b0 = ROL64((a02^d2), 62);
1581: b1 = ROL64((a13^d3), 55);
1582: b2 = ROL64((a24^d4), 39);
1583: a30 = b0 ^((~b1)& b2 );
1584: a41 = b1 ^((~b2)& b3 );
1585: a02 = b2 ^((~b3)& b4 );
1586: a13 = b3 ^((~b4)& b0 );
1587: a24 = b4 ^((~b0)& b1 );
1588:
1589: c0 = a00^a20^a40^a10^a30;
1590: c1 = a11^a31^a01^a21^a41;
1591: c2 = a22^a42^a12^a32^a02;
1592: c3 = a33^a03^a23^a43^a13;
1593: c4 = a44^a14^a34^a04^a24;
1594: d0 = c4^ROL64(c1, 1);
1595: d1 = c0^ROL64(c2, 1);
1596: d2 = c1^ROL64(c3, 1);
1597: d3 = c2^ROL64(c4, 1);
1598: d4 = c3^ROL64(c0, 1);
1599:
1600: b0 = (a00^d0);
1601: b1 = ROL64((a31^d1), 44);
1602: b2 = ROL64((a12^d2), 43);
1603: b3 = ROL64((a43^d3), 21);
1604: b4 = ROL64((a24^d4), 14);
1605: a00 = b0 ^((~b1)& b2 );
1606: a00 ^= RC[i+1];
1607: a31 = b1 ^((~b2)& b3 );
1608: a12 = b2 ^((~b3)& b4 );
1609: a43 = b3 ^((~b4)& b0 );
1610: a24 = b4 ^((~b0)& b1 );
1611:
1612: b2 = ROL64((a40^d0), 3);
1613: b3 = ROL64((a21^d1), 45);
1614: b4 = ROL64((a02^d2), 61);
1615: b0 = ROL64((a33^d3), 28);
1616: b1 = ROL64((a14^d4), 20);
1617: a40 = b0 ^((~b1)& b2 );
1618: a21 = b1 ^((~b2)& b3 );
1619: a02 = b2 ^((~b3)& b4 );
1620: a33 = b3 ^((~b4)& b0 );
1621: a14 = b4 ^((~b0)& b1 );
1622:
1623: b4 = ROL64((a30^d0), 18);
1624: b0 = ROL64((a11^d1), 1);
1625: b1 = ROL64((a42^d2), 6);
1626: b2 = ROL64((a23^d3), 25);
1627: b3 = ROL64((a04^d4), 8);
1628: a30 = b0 ^((~b1)& b2 );
1629: a11 = b1 ^((~b2)& b3 );
1630: a42 = b2 ^((~b3)& b4 );
1631: a23 = b3 ^((~b4)& b0 );
1632: a04 = b4 ^((~b0)& b1 );
1633:
1634: b1 = ROL64((a20^d0), 36);
1635: b2 = ROL64((a01^d1), 10);
1636: b3 = ROL64((a32^d2), 15);
1637: b4 = ROL64((a13^d3), 56);
1638: b0 = ROL64((a44^d4), 27);
1639: a20 = b0 ^((~b1)& b2 );
1640: a01 = b1 ^((~b2)& b3 );
1641: a32 = b2 ^((~b3)& b4 );
1642: a13 = b3 ^((~b4)& b0 );
1643: a44 = b4 ^((~b0)& b1 );
1644:
1645: b3 = ROL64((a10^d0), 41);
1646: b4 = ROL64((a41^d1), 2);
1647: b0 = ROL64((a22^d2), 62);
1648: b1 = ROL64((a03^d3), 55);
1649: b2 = ROL64((a34^d4), 39);
1650: a10 = b0 ^((~b1)& b2 );
1651: a41 = b1 ^((~b2)& b3 );
1652: a22 = b2 ^((~b3)& b4 );
1653: a03 = b3 ^((~b4)& b0 );
1654: a34 = b4 ^((~b0)& b1 );
1655:
1656: c0 = a00^a40^a30^a20^a10;
1657: c1 = a31^a21^a11^a01^a41;
1658: c2 = a12^a02^a42^a32^a22;
1659: c3 = a43^a33^a23^a13^a03;
1660: c4 = a24^a14^a04^a44^a34;
1661: d0 = c4^ROL64(c1, 1);
1662: d1 = c0^ROL64(c2, 1);
1663: d2 = c1^ROL64(c3, 1);
1664: d3 = c2^ROL64(c4, 1);
1665: d4 = c3^ROL64(c0, 1);
1666:
1667: b0 = (a00^d0);
1668: b1 = ROL64((a21^d1), 44);
1669: b2 = ROL64((a42^d2), 43);
1670: b3 = ROL64((a13^d3), 21);
1671: b4 = ROL64((a34^d4), 14);
1672: a00 = b0 ^((~b1)& b2 );
1673: a00 ^= RC[i+2];
1674: a21 = b1 ^((~b2)& b3 );
1675: a42 = b2 ^((~b3)& b4 );
1676: a13 = b3 ^((~b4)& b0 );
1677: a34 = b4 ^((~b0)& b1 );
1678:
1679: b2 = ROL64((a30^d0), 3);
1680: b3 = ROL64((a01^d1), 45);
1681: b4 = ROL64((a22^d2), 61);
1682: b0 = ROL64((a43^d3), 28);
1683: b1 = ROL64((a14^d4), 20);
1684: a30 = b0 ^((~b1)& b2 );
1685: a01 = b1 ^((~b2)& b3 );
1686: a22 = b2 ^((~b3)& b4 );
1687: a43 = b3 ^((~b4)& b0 );
1688: a14 = b4 ^((~b0)& b1 );
1689:
1690: b4 = ROL64((a10^d0), 18);
1691: b0 = ROL64((a31^d1), 1);
1692: b1 = ROL64((a02^d2), 6);
1693: b2 = ROL64((a23^d3), 25);
1694: b3 = ROL64((a44^d4), 8);
1695: a10 = b0 ^((~b1)& b2 );
1696: a31 = b1 ^((~b2)& b3 );
1697: a02 = b2 ^((~b3)& b4 );
1698: a23 = b3 ^((~b4)& b0 );
1699: a44 = b4 ^((~b0)& b1 );
1700:
1701: b1 = ROL64((a40^d0), 36);
1702: b2 = ROL64((a11^d1), 10);
1703: b3 = ROL64((a32^d2), 15);
1704: b4 = ROL64((a03^d3), 56);
1705: b0 = ROL64((a24^d4), 27);
1706: a40 = b0 ^((~b1)& b2 );
1707: a11 = b1 ^((~b2)& b3 );
1708: a32 = b2 ^((~b3)& b4 );
1709: a03 = b3 ^((~b4)& b0 );
1710: a24 = b4 ^((~b0)& b1 );
1711:
1712: b3 = ROL64((a20^d0), 41);
1713: b4 = ROL64((a41^d1), 2);
1714: b0 = ROL64((a12^d2), 62);
1715: b1 = ROL64((a33^d3), 55);
1716: b2 = ROL64((a04^d4), 39);
1717: a20 = b0 ^((~b1)& b2 );
1718: a41 = b1 ^((~b2)& b3 );
1719: a12 = b2 ^((~b3)& b4 );
1720: a33 = b3 ^((~b4)& b0 );
1721: a04 = b4 ^((~b0)& b1 );
1722:
1723: c0 = a00^a30^a10^a40^a20;
1724: c1 = a21^a01^a31^a11^a41;
1725: c2 = a42^a22^a02^a32^a12;
1726: c3 = a13^a43^a23^a03^a33;
1727: c4 = a34^a14^a44^a24^a04;
1728: d0 = c4^ROL64(c1, 1);
1729: d1 = c0^ROL64(c2, 1);
1730: d2 = c1^ROL64(c3, 1);
1731: d3 = c2^ROL64(c4, 1);
1732: d4 = c3^ROL64(c0, 1);
1733:
1734: b0 = (a00^d0);
1735: b1 = ROL64((a01^d1), 44);
1736: b2 = ROL64((a02^d2), 43);
1737: b3 = ROL64((a03^d3), 21);
1738: b4 = ROL64((a04^d4), 14);
1739: a00 = b0 ^((~b1)& b2 );
1740: a00 ^= RC[i+3];
1741: a01 = b1 ^((~b2)& b3 );
1742: a02 = b2 ^((~b3)& b4 );
1743: a03 = b3 ^((~b4)& b0 );
1744: a04 = b4 ^((~b0)& b1 );
1745:
1746: b2 = ROL64((a10^d0), 3);
1747: b3 = ROL64((a11^d1), 45);
1748: b4 = ROL64((a12^d2), 61);
1749: b0 = ROL64((a13^d3), 28);
1750: b1 = ROL64((a14^d4), 20);
1751: a10 = b0 ^((~b1)& b2 );
1752: a11 = b1 ^((~b2)& b3 );
1753: a12 = b2 ^((~b3)& b4 );
1754: a13 = b3 ^((~b4)& b0 );
1755: a14 = b4 ^((~b0)& b1 );
1756:
1757: b4 = ROL64((a20^d0), 18);
1758: b0 = ROL64((a21^d1), 1);
1759: b1 = ROL64((a22^d2), 6);
1760: b2 = ROL64((a23^d3), 25);
1761: b3 = ROL64((a24^d4), 8);
1762: a20 = b0 ^((~b1)& b2 );
1763: a21 = b1 ^((~b2)& b3 );
1764: a22 = b2 ^((~b3)& b4 );
1765: a23 = b3 ^((~b4)& b0 );
1766: a24 = b4 ^((~b0)& b1 );
1767:
1768: b1 = ROL64((a30^d0), 36);
1769: b2 = ROL64((a31^d1), 10);
1770: b3 = ROL64((a32^d2), 15);
1771: b4 = ROL64((a33^d3), 56);
1772: b0 = ROL64((a34^d4), 27);
1773: a30 = b0 ^((~b1)& b2 );
1774: a31 = b1 ^((~b2)& b3 );
1775: a32 = b2 ^((~b3)& b4 );
1776: a33 = b3 ^((~b4)& b0 );
1777: a34 = b4 ^((~b0)& b1 );
1778:
1779: b3 = ROL64((a40^d0), 41);
1780: b4 = ROL64((a41^d1), 2);
1781: b0 = ROL64((a42^d2), 62);
1782: b1 = ROL64((a43^d3), 55);
1783: b2 = ROL64((a44^d4), 39);
1784: a40 = b0 ^((~b1)& b2 );
1785: a41 = b1 ^((~b2)& b3 );
1786: a42 = b2 ^((~b3)& b4 );
1787: a43 = b3 ^((~b4)& b0 );
1788: a44 = b4 ^((~b0)& b1 );
1789: }
1790: }
1791:
1792: /*
1793: ** Initialize a new hash. iSize determines the size of the hash
1794: ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1795: ** can be zero to use the default hash size of 256 bits.
1796: */
1797: static void SHA3Init(SHA3Context *p, int iSize){
1798: memset(p, 0, sizeof(*p));
1799: if( iSize>=128 && iSize<=512 ){
1800: p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1801: }else{
1802: p->nRate = (1600 - 2*256)/8;
1803: }
1804: #if SHA3_BYTEORDER==1234
1805: /* Known to be little-endian at compile-time. No-op */
1806: #elif SHA3_BYTEORDER==4321
1807: p->ixMask = 7; /* Big-endian */
1808: #else
1809: {
1810: static unsigned int one = 1;
1811: if( 1==*(unsigned char*)&one ){
1812: /* Little endian. No byte swapping. */
1813: p->ixMask = 0;
1814: }else{
1815: /* Big endian. Byte swap. */
1816: p->ixMask = 7;
1817: }
1818: }
1819: #endif
1820: }
1821:
1822: /*
1823: ** Make consecutive calls to the SHA3Update function to add new content
1824: ** to the hash
1825: */
1826: static void SHA3Update(
1827: SHA3Context *p,
1828: const unsigned char *aData,
1829: unsigned int nData
1830: ){
1831: unsigned int i = 0;
1832: #if SHA3_BYTEORDER==1234
1833: if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1834: for(; i+7<nData; i+=8){
1835: p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1836: p->nLoaded += 8;
1837: if( p->nLoaded>=p->nRate ){
1838: KeccakF1600Step(p);
1839: p->nLoaded = 0;
1840: }
1841: }
1842: }
1843: #endif
1844: for(; i<nData; i++){
1845: #if SHA3_BYTEORDER==1234
1846: p->u.x[p->nLoaded] ^= aData[i];
1847: #elif SHA3_BYTEORDER==4321
1848: p->u.x[p->nLoaded^0x07] ^= aData[i];
1849: #else
1850: p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1851: #endif
1852: p->nLoaded++;
1853: if( p->nLoaded==p->nRate ){
1854: KeccakF1600Step(p);
1855: p->nLoaded = 0;
1856: }
1857: }
1858: }
1859:
1860: /*
1861: ** After all content has been added, invoke SHA3Final() to compute
1862: ** the final hash. The function returns a pointer to the binary
1863: ** hash value.
1864: */
1865: static unsigned char *SHA3Final(SHA3Context *p){
1866: unsigned int i;
1867: if( p->nLoaded==p->nRate-1 ){
1868: const unsigned char c1 = 0x86;
1869: SHA3Update(p, &c1, 1);
1870: }else{
1871: const unsigned char c2 = 0x06;
1872: const unsigned char c3 = 0x80;
1873: SHA3Update(p, &c2, 1);
1874: p->nLoaded = p->nRate - 1;
1875: SHA3Update(p, &c3, 1);
1876: }
1877: for(i=0; i<p->nRate; i++){
1878: p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1879: }
1880: return &p->u.x[p->nRate];
1881: }
1882: /* End of the hashing logic
1883: *****************************************************************************/
1884:
1885: /*
1886: ** Implementation of the sha3(X,SIZE) function.
1887: **
1888: ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1889: ** size is 256. If X is a BLOB, it is hashed as is.
1890: ** For all other non-NULL types of input, X is converted into a UTF-8 string
1891: ** and the string is hashed without the trailing 0x00 terminator. The hash
1892: ** of a NULL value is NULL.
1893: */
1894: static void sha3Func(
1895: sqlite3_context *context,
1896: int argc,
1897: sqlite3_value **argv
1898: ){
1899: SHA3Context cx;
1900: int eType = sqlite3_value_type(argv[0]);
1901: int nByte = sqlite3_value_bytes(argv[0]);
1902: int iSize;
1903: if( argc==1 ){
1904: iSize = 256;
1905: }else{
1906: iSize = sqlite3_value_int(argv[1]);
1907: if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1908: sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1909: "384 512", -1);
1910: return;
1911: }
1912: }
1913: if( eType==SQLITE_NULL ) return;
1914: SHA3Init(&cx, iSize);
1915: if( eType==SQLITE_BLOB ){
1916: SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1917: }else{
1918: SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1919: }
1920: sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1921: }
1922:
1923: /* Compute a string using sqlite3_vsnprintf() with a maximum length
1924: ** of 50 bytes and add it to the hash.
1925: */
1926: static void hash_step_vformat(
1927: SHA3Context *p, /* Add content to this context */
1928: const char *zFormat,
1929: ...
1930: ){
1931: va_list ap;
1932: int n;
1933: char zBuf[50];
1934: va_start(ap, zFormat);
1935: sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1936: va_end(ap);
1937: n = (int)strlen(zBuf);
1938: SHA3Update(p, (unsigned char*)zBuf, n);
1939: }
1940:
1941: /*
1942: ** Implementation of the sha3_query(SQL,SIZE) function.
1943: **
1944: ** This function compiles and runs the SQL statement(s) given in the
1945: ** argument. The results are hashed using a SIZE-bit SHA3. The default
1946: ** size is 256.
1947: **
1948: ** The format of the byte stream that is hashed is summarized as follows:
1949: **
1950: ** S<n>:<sql>
1951: ** R
1952: ** N
1953: ** I<int>
1954: ** F<ieee-float>
1955: ** B<size>:<bytes>
1956: ** T<size>:<text>
1957: **
1958: ** <sql> is the original SQL text for each statement run and <n> is
1959: ** the size of that text. The SQL text is UTF-8. A single R character
1960: ** occurs before the start of each row. N means a NULL value.
1961: ** I mean an 8-byte little-endian integer <int>. F is a floating point
1962: ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1963: ** B means blobs of <size> bytes. T means text rendered as <size>
1964: ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1965: ** text integers.
1966: **
1967: ** For each SQL statement in the X input, there is one S segment. Each
1968: ** S segment is followed by zero or more R segments, one for each row in the
1969: ** result set. After each R, there are one or more N, I, F, B, or T segments,
1970: ** one for each column in the result set. Segments are concatentated directly
1971: ** with no delimiters of any kind.
1972: */
1973: static void sha3QueryFunc(
1974: sqlite3_context *context,
1975: int argc,
1976: sqlite3_value **argv
1977: ){
1978: sqlite3 *db = sqlite3_context_db_handle(context);
1979: const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1980: sqlite3_stmt *pStmt = 0;
1981: int nCol; /* Number of columns in the result set */
1982: int i; /* Loop counter */
1983: int rc;
1984: int n;
1985: const char *z;
1986: SHA3Context cx;
1987: int iSize;
1988:
1989: if( argc==1 ){
1990: iSize = 256;
1991: }else{
1992: iSize = sqlite3_value_int(argv[1]);
1993: if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1994: sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1995: "384 512", -1);
1996: return;
1997: }
1998: }
1999: if( zSql==0 ) return;
2000: SHA3Init(&cx, iSize);
2001: while( zSql[0] ){
2002: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2003: if( rc ){
2004: char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2005: zSql, sqlite3_errmsg(db));
2006: sqlite3_finalize(pStmt);
2007: sqlite3_result_error(context, zMsg, -1);
2008: sqlite3_free(zMsg);
2009: return;
2010: }
2011: if( !sqlite3_stmt_readonly(pStmt) ){
2012: char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2013: sqlite3_finalize(pStmt);
2014: sqlite3_result_error(context, zMsg, -1);
2015: sqlite3_free(zMsg);
2016: return;
2017: }
2018: nCol = sqlite3_column_count(pStmt);
2019: z = sqlite3_sql(pStmt);
2020: if( z ){
2021: n = (int)strlen(z);
2022: hash_step_vformat(&cx,"S%d:",n);
2023: SHA3Update(&cx,(unsigned char*)z,n);
2024: }
2025:
2026: /* Compute a hash over the result of the query */
2027: while( SQLITE_ROW==sqlite3_step(pStmt) ){
2028: SHA3Update(&cx,(const unsigned char*)"R",1);
2029: for(i=0; i<nCol; i++){
2030: switch( sqlite3_column_type(pStmt,i) ){
2031: case SQLITE_NULL: {
2032: SHA3Update(&cx, (const unsigned char*)"N",1);
2033: break;
2034: }
2035: case SQLITE_INTEGER: {
2036: sqlite3_uint64 u;
2037: int j;
2038: unsigned char x[9];
2039: sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2040: memcpy(&u, &v, 8);
2041: for(j=8; j>=1; j--){
2042: x[j] = u & 0xff;
2043: u >>= 8;
2044: }
2045: x[0] = 'I';
2046: SHA3Update(&cx, x, 9);
2047: break;
2048: }
2049: case SQLITE_FLOAT: {
2050: sqlite3_uint64 u;
2051: int j;
2052: unsigned char x[9];
2053: double r = sqlite3_column_double(pStmt,i);
2054: memcpy(&u, &r, 8);
2055: for(j=8; j>=1; j--){
2056: x[j] = u & 0xff;
2057: u >>= 8;
2058: }
2059: x[0] = 'F';
2060: SHA3Update(&cx,x,9);
2061: break;
2062: }
2063: case SQLITE_TEXT: {
2064: int n2 = sqlite3_column_bytes(pStmt, i);
2065: const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2066: hash_step_vformat(&cx,"T%d:",n2);
2067: SHA3Update(&cx, z2, n2);
2068: break;
2069: }
2070: case SQLITE_BLOB: {
2071: int n2 = sqlite3_column_bytes(pStmt, i);
2072: const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2073: hash_step_vformat(&cx,"B%d:",n2);
2074: SHA3Update(&cx, z2, n2);
2075: break;
2076: }
2077: }
2078: }
2079: }
2080: sqlite3_finalize(pStmt);
2081: }
2082: sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2083: }
2084:
2085:
2086: #ifdef _WIN32
2087:
2088: #endif
2089: int sqlite3_shathree_init(
2090: sqlite3 *db,
2091: char **pzErrMsg,
2092: const sqlite3_api_routines *pApi
2093: ){
2094: int rc = SQLITE_OK;
2095: SQLITE_EXTENSION_INIT2(pApi);
2096: (void)pzErrMsg; /* Unused parameter */
2097: rc = sqlite3_create_function(db, "sha3", 1,
2098: SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2099: 0, sha3Func, 0, 0);
2100: if( rc==SQLITE_OK ){
2101: rc = sqlite3_create_function(db, "sha3", 2,
2102: SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2103: 0, sha3Func, 0, 0);
2104: }
2105: if( rc==SQLITE_OK ){
2106: rc = sqlite3_create_function(db, "sha3_query", 1,
2107: SQLITE_UTF8 | SQLITE_DIRECTONLY,
2108: 0, sha3QueryFunc, 0, 0);
2109: }
2110: if( rc==SQLITE_OK ){
2111: rc = sqlite3_create_function(db, "sha3_query", 2,
2112: SQLITE_UTF8 | SQLITE_DIRECTONLY,
2113: 0, sha3QueryFunc, 0, 0);
2114: }
2115: return rc;
2116: }
2117:
2118: /************************* End ../ext/misc/shathree.c ********************/
2119: /************************* Begin ../ext/misc/fileio.c ******************/
2120: /*
2121: ** 2014-06-13
2122: **
2123: ** The author disclaims copyright to this source code. In place of
2124: ** a legal notice, here is a blessing:
2125: **
2126: ** May you do good and not evil.
2127: ** May you find forgiveness for yourself and forgive others.
2128: ** May you share freely, never taking more than you give.
2129: **
2130: ******************************************************************************
2131: **
2132: ** This SQLite extension implements SQL functions readfile() and
2133: ** writefile(), and eponymous virtual type "fsdir".
2134: **
2135: ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2136: **
2137: ** If neither of the optional arguments is present, then this UDF
2138: ** function writes blob DATA to file FILE. If successful, the number
2139: ** of bytes written is returned. If an error occurs, NULL is returned.
2140: **
2141: ** If the first option argument - MODE - is present, then it must
2142: ** be passed an integer value that corresponds to a POSIX mode
2143: ** value (file type + permissions, as returned in the stat.st_mode
2144: ** field by the stat() system call). Three types of files may
2145: ** be written/created:
2146: **
2147: ** regular files: (mode & 0170000)==0100000
2148: ** symbolic links: (mode & 0170000)==0120000
2149: ** directories: (mode & 0170000)==0040000
2150: **
2151: ** For a directory, the DATA is ignored. For a symbolic link, it is
2152: ** interpreted as text and used as the target of the link. For a
2153: ** regular file, it is interpreted as a blob and written into the
2154: ** named file. Regardless of the type of file, its permissions are
2155: ** set to (mode & 0777) before returning.
2156: **
2157: ** If the optional MTIME argument is present, then it is interpreted
2158: ** as an integer - the number of seconds since the unix epoch. The
2159: ** modification-time of the target file is set to this value before
2160: ** returning.
2161: **
2162: ** If three or more arguments are passed to this function and an
2163: ** error is encountered, an exception is raised.
2164: **
2165: ** READFILE(FILE):
2166: **
2167: ** Read and return the contents of file FILE (type blob) from disk.
2168: **
2169: ** FSDIR:
2170: **
2171: ** Used as follows:
2172: **
2173: ** SELECT * FROM fsdir($path [, $dir]);
2174: **
2175: ** Parameter $path is an absolute or relative pathname. If the file that it
2176: ** refers to does not exist, it is an error. If the path refers to a regular
2177: ** file or symbolic link, it returns a single row. Or, if the path refers
2178: ** to a directory, it returns one row for the directory, and one row for each
2179: ** file within the hierarchy rooted at $path.
2180: **
2181: ** Each row has the following columns:
2182: **
2183: ** name: Path to file or directory (text value).
2184: ** mode: Value of stat.st_mode for directory entry (an integer).
2185: ** mtime: Value of stat.st_mtime for directory entry (an integer).
2186: ** data: For a regular file, a blob containing the file data. For a
2187: ** symlink, a text value containing the text of the link. For a
2188: ** directory, NULL.
2189: **
2190: ** If a non-NULL value is specified for the optional $dir parameter and
2191: ** $path is a relative path, then $path is interpreted relative to $dir.
2192: ** And the paths returned in the "name" column of the table are also
2193: ** relative to directory $dir.
2194: */
2195: /* #include "sqlite3ext.h" */
2196: SQLITE_EXTENSION_INIT1
2197: #include <stdio.h>
2198: #include <string.h>
2199: #include <assert.h>
2200:
2201: #include <sys/types.h>
2202: #include <sys/stat.h>
2203: #include <fcntl.h>
2204: #if !defined(_WIN32) && !defined(WIN32)
2205: # include <unistd.h>
2206: # include <dirent.h>
2207: # include <utime.h>
2208: # include <sys/time.h>
2209: #else
2210: # include "windows.h"
2211: # include <io.h>
2212: # include <direct.h>
2213: /* # include "test_windirent.h" */
2214: # define dirent DIRENT
2215: # ifndef chmod
2216: # define chmod _chmod
2217: # endif
2218: # ifndef stat
2219: # define stat _stat
2220: # endif
2221: # define mkdir(path,mode) _mkdir(path)
2222: # define lstat(path,buf) stat(path,buf)
2223: #endif
2224: #include <time.h>
2225: #include <errno.h>
2226:
2227:
2228: /*
2229: ** Structure of the fsdir() table-valued function
2230: */
2231: /* 0 1 2 3 4 5 */
2232: #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2233: #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2234: #define FSDIR_COLUMN_MODE 1 /* Access mode */
2235: #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2236: #define FSDIR_COLUMN_DATA 3 /* File content */
2237: #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2238: #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2239:
2240:
2241: /*
2242: ** Set the result stored by context ctx to a blob containing the
2243: ** contents of file zName. Or, leave the result unchanged (NULL)
2244: ** if the file does not exist or is unreadable.
2245: **
2246: ** If the file exceeds the SQLite blob size limit, through an
2247: ** SQLITE_TOOBIG error.
2248: **
2249: ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2250: ** off of disk.
2251: */
2252: static void readFileContents(sqlite3_context *ctx, const char *zName){
2253: FILE *in;
2254: sqlite3_int64 nIn;
2255: void *pBuf;
2256: sqlite3 *db;
2257: int mxBlob;
2258:
2259: in = fopen(zName, "rb");
2260: if( in==0 ){
2261: /* File does not exist or is unreadable. Leave the result set to NULL. */
2262: return;
2263: }
2264: fseek(in, 0, SEEK_END);
2265: nIn = ftell(in);
2266: rewind(in);
2267: db = sqlite3_context_db_handle(ctx);
2268: mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2269: if( nIn>mxBlob ){
2270: sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2271: fclose(in);
2272: return;
2273: }
2274: pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2275: if( pBuf==0 ){
2276: sqlite3_result_error_nomem(ctx);
2277: fclose(in);
2278: return;
2279: }
2280: if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2281: sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2282: }else{
2283: sqlite3_result_error_code(ctx, SQLITE_IOERR);
2284: sqlite3_free(pBuf);
2285: }
2286: fclose(in);
2287: }
2288:
2289: /*
2290: ** Implementation of the "readfile(X)" SQL function. The entire content
2291: ** of the file named X is read and returned as a BLOB. NULL is returned
2292: ** if the file does not exist or is unreadable.
2293: */
2294: static void readfileFunc(
2295: sqlite3_context *context,
2296: int argc,
2297: sqlite3_value **argv
2298: ){
2299: const char *zName;
2300: (void)(argc); /* Unused parameter */
2301: zName = (const char*)sqlite3_value_text(argv[0]);
2302: if( zName==0 ) return;
2303: readFileContents(context, zName);
2304: }
2305:
2306: /*
2307: ** Set the error message contained in context ctx to the results of
2308: ** vprintf(zFmt, ...).
2309: */
2310: static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2311: char *zMsg = 0;
2312: va_list ap;
2313: va_start(ap, zFmt);
2314: zMsg = sqlite3_vmprintf(zFmt, ap);
2315: sqlite3_result_error(ctx, zMsg, -1);
2316: sqlite3_free(zMsg);
2317: va_end(ap);
2318: }
2319:
2320: #if defined(_WIN32)
2321: /*
2322: ** This function is designed to convert a Win32 FILETIME structure into the
2323: ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2324: */
2325: static sqlite3_uint64 fileTimeToUnixTime(
2326: LPFILETIME pFileTime
2327: ){
2328: SYSTEMTIME epochSystemTime;
2329: ULARGE_INTEGER epochIntervals;
2330: FILETIME epochFileTime;
2331: ULARGE_INTEGER fileIntervals;
2332:
2333: memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2334: epochSystemTime.wYear = 1970;
2335: epochSystemTime.wMonth = 1;
2336: epochSystemTime.wDay = 1;
2337: SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2338: epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2339: epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2340:
2341: fileIntervals.LowPart = pFileTime->dwLowDateTime;
2342: fileIntervals.HighPart = pFileTime->dwHighDateTime;
2343:
2344: return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2345: }
2346:
2347: /*
2348: ** This function attempts to normalize the time values found in the stat()
2349: ** buffer to UTC. This is necessary on Win32, where the runtime library
2350: ** appears to return these values as local times.
2351: */
2352: static void statTimesToUtc(
2353: const char *zPath,
2354: struct stat *pStatBuf
2355: ){
2356: HANDLE hFindFile;
2357: WIN32_FIND_DATAW fd;
2358: LPWSTR zUnicodeName;
2359: extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2360: zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2361: if( zUnicodeName ){
2362: memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2363: hFindFile = FindFirstFileW(zUnicodeName, &fd);
2364: if( hFindFile!=NULL ){
2365: pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2366: pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2367: pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2368: FindClose(hFindFile);
2369: }
2370: sqlite3_free(zUnicodeName);
2371: }
2372: }
2373: #endif
2374:
2375: /*
2376: ** This function is used in place of stat(). On Windows, special handling
2377: ** is required in order for the included time to be returned as UTC. On all
2378: ** other systems, this function simply calls stat().
2379: */
2380: static int fileStat(
2381: const char *zPath,
2382: struct stat *pStatBuf
2383: ){
2384: #if defined(_WIN32)
2385: int rc = stat(zPath, pStatBuf);
2386: if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2387: return rc;
2388: #else
2389: return stat(zPath, pStatBuf);
2390: #endif
2391: }
2392:
2393: /*
2394: ** This function is used in place of lstat(). On Windows, special handling
2395: ** is required in order for the included time to be returned as UTC. On all
2396: ** other systems, this function simply calls lstat().
2397: */
2398: static int fileLinkStat(
2399: const char *zPath,
2400: struct stat *pStatBuf
2401: ){
2402: #if defined(_WIN32)
2403: int rc = lstat(zPath, pStatBuf);
2404: if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2405: return rc;
2406: #else
2407: return lstat(zPath, pStatBuf);
2408: #endif
2409: }
2410:
2411: /*
2412: ** Argument zFile is the name of a file that will be created and/or written
2413: ** by SQL function writefile(). This function ensures that the directory
2414: ** zFile will be written to exists, creating it if required. The permissions
2415: ** for any path components created by this function are set in accordance
2416: ** with the current umask.
2417: **
2418: ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2419: ** SQLITE_OK is returned if the directory is successfully created, or
2420: ** SQLITE_ERROR otherwise.
2421: */
2422: static int makeDirectory(
2423: const char *zFile
2424: ){
2425: char *zCopy = sqlite3_mprintf("%s", zFile);
2426: int rc = SQLITE_OK;
2427:
2428: if( zCopy==0 ){
2429: rc = SQLITE_NOMEM;
2430: }else{
2431: int nCopy = (int)strlen(zCopy);
2432: int i = 1;
2433:
2434: while( rc==SQLITE_OK ){
2435: struct stat sStat;
2436: int rc2;
2437:
2438: for(; zCopy[i]!='/' && i<nCopy; i++);
2439: if( i==nCopy ) break;
2440: zCopy[i] = '\0';
2441:
2442: rc2 = fileStat(zCopy, &sStat);
2443: if( rc2!=0 ){
2444: if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2445: }else{
2446: if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2447: }
2448: zCopy[i] = '/';
2449: i++;
2450: }
2451:
2452: sqlite3_free(zCopy);
2453: }
2454:
2455: return rc;
2456: }
2457:
2458: /*
2459: ** This function does the work for the writefile() UDF. Refer to
2460: ** header comments at the top of this file for details.
2461: */
2462: static int writeFile(
2463: sqlite3_context *pCtx, /* Context to return bytes written in */
2464: const char *zFile, /* File to write */
2465: sqlite3_value *pData, /* Data to write */
2466: mode_t mode, /* MODE parameter passed to writefile() */
2467: sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2468: ){
2469: #if !defined(_WIN32) && !defined(WIN32)
2470: if( S_ISLNK(mode) ){
2471: const char *zTo = (const char*)sqlite3_value_text(pData);
2472: if( symlink(zTo, zFile)<0 ) return 1;
2473: }else
2474: #endif
2475: {
2476: if( S_ISDIR(mode) ){
2477: if( mkdir(zFile, mode) ){
2478: /* The mkdir() call to create the directory failed. This might not
2479: ** be an error though - if there is already a directory at the same
2480: ** path and either the permissions already match or can be changed
2481: ** to do so using chmod(), it is not an error. */
2482: struct stat sStat;
2483: if( errno!=EEXIST
2484: || 0!=fileStat(zFile, &sStat)
2485: || !S_ISDIR(sStat.st_mode)
2486: || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2487: ){
2488: return 1;
2489: }
2490: }
2491: }else{
2492: sqlite3_int64 nWrite = 0;
2493: const char *z;
2494: int rc = 0;
2495: FILE *out = fopen(zFile, "wb");
2496: if( out==0 ) return 1;
2497: z = (const char*)sqlite3_value_blob(pData);
2498: if( z ){
2499: sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2500: nWrite = sqlite3_value_bytes(pData);
2501: if( nWrite!=n ){
2502: rc = 1;
2503: }
2504: }
2505: fclose(out);
2506: if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2507: rc = 1;
2508: }
2509: if( rc ) return 2;
2510: sqlite3_result_int64(pCtx, nWrite);
2511: }
2512: }
2513:
2514: if( mtime>=0 ){
2515: #if defined(_WIN32)
2516: #if !SQLITE_OS_WINRT
2517: /* Windows */
2518: FILETIME lastAccess;
2519: FILETIME lastWrite;
2520: SYSTEMTIME currentTime;
2521: LONGLONG intervals;
2522: HANDLE hFile;
2523: LPWSTR zUnicodeName;
2524: extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2525:
2526: GetSystemTime(¤tTime);
2527: SystemTimeToFileTime(¤tTime, &lastAccess);
2528: intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2529: lastWrite.dwLowDateTime = (DWORD)intervals;
2530: lastWrite.dwHighDateTime = intervals >> 32;
2531: zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2532: if( zUnicodeName==0 ){
2533: return 1;
2534: }
2535: hFile = CreateFileW(
2536: zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2537: FILE_FLAG_BACKUP_SEMANTICS, NULL
2538: );
2539: sqlite3_free(zUnicodeName);
2540: if( hFile!=INVALID_HANDLE_VALUE ){
2541: BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2542: CloseHandle(hFile);
2543: return !bResult;
2544: }else{
2545: return 1;
2546: }
2547: #endif
2548: #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2549: /* Recent unix */
2550: struct timespec times[2];
2551: times[0].tv_nsec = times[1].tv_nsec = 0;
2552: times[0].tv_sec = time(0);
2553: times[1].tv_sec = mtime;
2554: if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2555: return 1;
2556: }
2557: #else
2558: /* Legacy unix */
2559: struct timeval times[2];
2560: times[0].tv_usec = times[1].tv_usec = 0;
2561: times[0].tv_sec = time(0);
2562: times[1].tv_sec = mtime;
2563: if( utimes(zFile, times) ){
2564: return 1;
2565: }
2566: #endif
2567: }
2568:
2569: return 0;
2570: }
2571:
2572: /*
2573: ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2574: ** Refer to header comments at the top of this file for details.
2575: */
2576: static void writefileFunc(
2577: sqlite3_context *context,
2578: int argc,
2579: sqlite3_value **argv
2580: ){
2581: const char *zFile;
2582: mode_t mode = 0;
2583: int res;
2584: sqlite3_int64 mtime = -1;
2585:
2586: if( argc<2 || argc>4 ){
2587: sqlite3_result_error(context,
2588: "wrong number of arguments to function writefile()", -1
2589: );
2590: return;
2591: }
2592:
2593: zFile = (const char*)sqlite3_value_text(argv[0]);
2594: if( zFile==0 ) return;
2595: if( argc>=3 ){
2596: mode = (mode_t)sqlite3_value_int(argv[2]);
2597: }
2598: if( argc==4 ){
2599: mtime = sqlite3_value_int64(argv[3]);
2600: }
2601:
2602: res = writeFile(context, zFile, argv[1], mode, mtime);
2603: if( res==1 && errno==ENOENT ){
2604: if( makeDirectory(zFile)==SQLITE_OK ){
2605: res = writeFile(context, zFile, argv[1], mode, mtime);
2606: }
2607: }
2608:
2609: if( argc>2 && res!=0 ){
2610: if( S_ISLNK(mode) ){
2611: ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2612: }else if( S_ISDIR(mode) ){
2613: ctxErrorMsg(context, "failed to create directory: %s", zFile);
2614: }else{
2615: ctxErrorMsg(context, "failed to write file: %s", zFile);
2616: }
2617: }
2618: }
2619:
2620: /*
2621: ** SQL function: lsmode(MODE)
2622: **
2623: ** Given a numberic st_mode from stat(), convert it into a human-readable
2624: ** text string in the style of "ls -l".
2625: */
2626: static void lsModeFunc(
2627: sqlite3_context *context,
2628: int argc,
2629: sqlite3_value **argv
2630: ){
2631: int i;
2632: int iMode = sqlite3_value_int(argv[0]);
2633: char z[16];
2634: (void)argc;
2635: if( S_ISLNK(iMode) ){
2636: z[0] = 'l';
2637: }else if( S_ISREG(iMode) ){
2638: z[0] = '-';
2639: }else if( S_ISDIR(iMode) ){
2640: z[0] = 'd';
2641: }else{
2642: z[0] = '?';
2643: }
2644: for(i=0; i<3; i++){
2645: int m = (iMode >> ((2-i)*3));
2646: char *a = &z[1 + i*3];
2647: a[0] = (m & 0x4) ? 'r' : '-';
2648: a[1] = (m & 0x2) ? 'w' : '-';
2649: a[2] = (m & 0x1) ? 'x' : '-';
2650: }
2651: z[10] = '\0';
2652: sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2653: }
2654:
2655: #ifndef SQLITE_OMIT_VIRTUALTABLE
2656:
2657: /*
2658: ** Cursor type for recursively iterating through a directory structure.
2659: */
2660: typedef struct fsdir_cursor fsdir_cursor;
2661: typedef struct FsdirLevel FsdirLevel;
2662:
2663: struct FsdirLevel {
2664: DIR *pDir; /* From opendir() */
2665: char *zDir; /* Name of directory (nul-terminated) */
2666: };
2667:
2668: struct fsdir_cursor {
2669: sqlite3_vtab_cursor base; /* Base class - must be first */
2670:
2671: int nLvl; /* Number of entries in aLvl[] array */
2672: int iLvl; /* Index of current entry */
2673: FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2674:
2675: const char *zBase;
2676: int nBase;
2677:
2678: struct stat sStat; /* Current lstat() results */
2679: char *zPath; /* Path to current entry */
2680: sqlite3_int64 iRowid; /* Current rowid */
2681: };
2682:
2683: typedef struct fsdir_tab fsdir_tab;
2684: struct fsdir_tab {
2685: sqlite3_vtab base; /* Base class - must be first */
2686: };
2687:
2688: /*
2689: ** Construct a new fsdir virtual table object.
2690: */
2691: static int fsdirConnect(
2692: sqlite3 *db,
2693: void *pAux,
2694: int argc, const char *const*argv,
2695: sqlite3_vtab **ppVtab,
2696: char **pzErr
2697: ){
2698: fsdir_tab *pNew = 0;
2699: int rc;
2700: (void)pAux;
2701: (void)argc;
2702: (void)argv;
2703: (void)pzErr;
2704: rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2705: if( rc==SQLITE_OK ){
2706: pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2707: if( pNew==0 ) return SQLITE_NOMEM;
2708: memset(pNew, 0, sizeof(*pNew));
2709: sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2710: }
2711: *ppVtab = (sqlite3_vtab*)pNew;
2712: return rc;
2713: }
2714:
2715: /*
2716: ** This method is the destructor for fsdir vtab objects.
2717: */
2718: static int fsdirDisconnect(sqlite3_vtab *pVtab){
2719: sqlite3_free(pVtab);
2720: return SQLITE_OK;
2721: }
2722:
2723: /*
2724: ** Constructor for a new fsdir_cursor object.
2725: */
2726: static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2727: fsdir_cursor *pCur;
2728: (void)p;
2729: pCur = sqlite3_malloc( sizeof(*pCur) );
2730: if( pCur==0 ) return SQLITE_NOMEM;
2731: memset(pCur, 0, sizeof(*pCur));
2732: pCur->iLvl = -1;
2733: *ppCursor = &pCur->base;
2734: return SQLITE_OK;
2735: }
2736:
2737: /*
2738: ** Reset a cursor back to the state it was in when first returned
2739: ** by fsdirOpen().
2740: */
2741: static void fsdirResetCursor(fsdir_cursor *pCur){
2742: int i;
2743: for(i=0; i<=pCur->iLvl; i++){
2744: FsdirLevel *pLvl = &pCur->aLvl[i];
2745: if( pLvl->pDir ) closedir(pLvl->pDir);
2746: sqlite3_free(pLvl->zDir);
2747: }
2748: sqlite3_free(pCur->zPath);
2749: sqlite3_free(pCur->aLvl);
2750: pCur->aLvl = 0;
2751: pCur->zPath = 0;
2752: pCur->zBase = 0;
2753: pCur->nBase = 0;
2754: pCur->nLvl = 0;
2755: pCur->iLvl = -1;
2756: pCur->iRowid = 1;
2757: }
2758:
2759: /*
2760: ** Destructor for an fsdir_cursor.
2761: */
2762: static int fsdirClose(sqlite3_vtab_cursor *cur){
2763: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2764:
2765: fsdirResetCursor(pCur);
2766: sqlite3_free(pCur);
2767: return SQLITE_OK;
2768: }
2769:
2770: /*
2771: ** Set the error message for the virtual table associated with cursor
2772: ** pCur to the results of vprintf(zFmt, ...).
2773: */
2774: static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2775: va_list ap;
2776: va_start(ap, zFmt);
2777: pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2778: va_end(ap);
2779: }
2780:
2781:
2782: /*
2783: ** Advance an fsdir_cursor to its next row of output.
2784: */
2785: static int fsdirNext(sqlite3_vtab_cursor *cur){
2786: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2787: mode_t m = pCur->sStat.st_mode;
2788:
2789: pCur->iRowid++;
2790: if( S_ISDIR(m) ){
2791: /* Descend into this directory */
2792: int iNew = pCur->iLvl + 1;
2793: FsdirLevel *pLvl;
2794: if( iNew>=pCur->nLvl ){
2795: int nNew = iNew+1;
2796: sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2797: FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2798: if( aNew==0 ) return SQLITE_NOMEM;
2799: memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2800: pCur->aLvl = aNew;
2801: pCur->nLvl = nNew;
2802: }
2803: pCur->iLvl = iNew;
2804: pLvl = &pCur->aLvl[iNew];
2805:
2806: pLvl->zDir = pCur->zPath;
2807: pCur->zPath = 0;
2808: pLvl->pDir = opendir(pLvl->zDir);
2809: if( pLvl->pDir==0 ){
2810: fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2811: return SQLITE_ERROR;
2812: }
2813: }
2814:
2815: while( pCur->iLvl>=0 ){
2816: FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2817: struct dirent *pEntry = readdir(pLvl->pDir);
2818: if( pEntry ){
2819: if( pEntry->d_name[0]=='.' ){
2820: if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2821: if( pEntry->d_name[1]=='\0' ) continue;
2822: }
2823: sqlite3_free(pCur->zPath);
2824: pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2825: if( pCur->zPath==0 ) return SQLITE_NOMEM;
2826: if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2827: fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2828: return SQLITE_ERROR;
2829: }
2830: return SQLITE_OK;
2831: }
2832: closedir(pLvl->pDir);
2833: sqlite3_free(pLvl->zDir);
2834: pLvl->pDir = 0;
2835: pLvl->zDir = 0;
2836: pCur->iLvl--;
2837: }
2838:
2839: /* EOF */
2840: sqlite3_free(pCur->zPath);
2841: pCur->zPath = 0;
2842: return SQLITE_OK;
2843: }
2844:
2845: /*
2846: ** Return values of columns for the row at which the series_cursor
2847: ** is currently pointing.
2848: */
2849: static int fsdirColumn(
2850: sqlite3_vtab_cursor *cur, /* The cursor */
2851: sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2852: int i /* Which column to return */
2853: ){
2854: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2855: switch( i ){
2856: case FSDIR_COLUMN_NAME: {
2857: sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2858: break;
2859: }
2860:
2861: case FSDIR_COLUMN_MODE:
2862: sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2863: break;
2864:
2865: case FSDIR_COLUMN_MTIME:
2866: sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2867: break;
2868:
2869: case FSDIR_COLUMN_DATA: {
2870: mode_t m = pCur->sStat.st_mode;
2871: if( S_ISDIR(m) ){
2872: sqlite3_result_null(ctx);
2873: #if !defined(_WIN32) && !defined(WIN32)
2874: }else if( S_ISLNK(m) ){
2875: char aStatic[64];
2876: char *aBuf = aStatic;
2877: sqlite3_int64 nBuf = 64;
2878: int n;
2879:
2880: while( 1 ){
2881: n = readlink(pCur->zPath, aBuf, nBuf);
2882: if( n<nBuf ) break;
2883: if( aBuf!=aStatic ) sqlite3_free(aBuf);
2884: nBuf = nBuf*2;
2885: aBuf = sqlite3_malloc64(nBuf);
2886: if( aBuf==0 ){
2887: sqlite3_result_error_nomem(ctx);
2888: return SQLITE_NOMEM;
2889: }
2890: }
2891:
2892: sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2893: if( aBuf!=aStatic ) sqlite3_free(aBuf);
2894: #endif
2895: }else{
2896: readFileContents(ctx, pCur->zPath);
2897: }
2898: }
2899: case FSDIR_COLUMN_PATH:
2900: default: {
2901: /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2902: ** always return their values as NULL */
2903: break;
2904: }
2905: }
2906: return SQLITE_OK;
2907: }
2908:
2909: /*
2910: ** Return the rowid for the current row. In this implementation, the
2911: ** first row returned is assigned rowid value 1, and each subsequent
2912: ** row a value 1 more than that of the previous.
2913: */
2914: static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2915: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2916: *pRowid = pCur->iRowid;
2917: return SQLITE_OK;
2918: }
2919:
2920: /*
2921: ** Return TRUE if the cursor has been moved off of the last
2922: ** row of output.
2923: */
2924: static int fsdirEof(sqlite3_vtab_cursor *cur){
2925: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2926: return (pCur->zPath==0);
2927: }
2928:
2929: /*
2930: ** xFilter callback.
2931: **
2932: ** idxNum==1 PATH parameter only
2933: ** idxNum==2 Both PATH and DIR supplied
2934: */
2935: static int fsdirFilter(
2936: sqlite3_vtab_cursor *cur,
2937: int idxNum, const char *idxStr,
2938: int argc, sqlite3_value **argv
2939: ){
2940: const char *zDir = 0;
2941: fsdir_cursor *pCur = (fsdir_cursor*)cur;
2942: (void)idxStr;
2943: fsdirResetCursor(pCur);
2944:
2945: if( idxNum==0 ){
2946: fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2947: return SQLITE_ERROR;
2948: }
2949:
2950: assert( argc==idxNum && (argc==1 || argc==2) );
2951: zDir = (const char*)sqlite3_value_text(argv[0]);
2952: if( zDir==0 ){
2953: fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2954: return SQLITE_ERROR;
2955: }
2956: if( argc==2 ){
2957: pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2958: }
2959: if( pCur->zBase ){
2960: pCur->nBase = (int)strlen(pCur->zBase)+1;
2961: pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2962: }else{
2963: pCur->zPath = sqlite3_mprintf("%s", zDir);
2964: }
2965:
2966: if( pCur->zPath==0 ){
2967: return SQLITE_NOMEM;
2968: }
2969: if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2970: fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2971: return SQLITE_ERROR;
2972: }
2973:
2974: return SQLITE_OK;
2975: }
2976:
2977: /*
2978: ** SQLite will invoke this method one or more times while planning a query
2979: ** that uses the generate_series virtual table. This routine needs to create
2980: ** a query plan for each invocation and compute an estimated cost for that
2981: ** plan.
2982: **
2983: ** In this implementation idxNum is used to represent the
2984: ** query plan. idxStr is unused.
2985: **
2986: ** The query plan is represented by values of idxNum:
2987: **
2988: ** (1) The path value is supplied by argv[0]
2989: ** (2) Path is in argv[0] and dir is in argv[1]
2990: */
2991: static int fsdirBestIndex(
2992: sqlite3_vtab *tab,
2993: sqlite3_index_info *pIdxInfo
2994: ){
2995: int i; /* Loop over constraints */
2996: int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2997: int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2998: int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2999: int seenDir = 0; /* True if an unusable DIR= constraint is seen */
3000: const struct sqlite3_index_constraint *pConstraint;
3001:
3002: (void)tab;
3003: pConstraint = pIdxInfo->aConstraint;
3004: for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3005: if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3006: switch( pConstraint->iColumn ){
3007: case FSDIR_COLUMN_PATH: {
3008: if( pConstraint->usable ){
3009: idxPath = i;
3010: seenPath = 0;
3011: }else if( idxPath<0 ){
3012: seenPath = 1;
3013: }
3014: break;
3015: }
3016: case FSDIR_COLUMN_DIR: {
3017: if( pConstraint->usable ){
3018: idxDir = i;
3019: seenDir = 0;
3020: }else if( idxDir<0 ){
3021: seenDir = 1;
3022: }
3023: break;
3024: }
3025: }
3026: }
3027: if( seenPath || seenDir ){
3028: /* If input parameters are unusable, disallow this plan */
3029: return SQLITE_CONSTRAINT;
3030: }
3031:
3032: if( idxPath<0 ){
3033: pIdxInfo->idxNum = 0;
3034: /* The pIdxInfo->estimatedCost should have been initialized to a huge
3035: ** number. Leave it unchanged. */
3036: pIdxInfo->estimatedRows = 0x7fffffff;
3037: }else{
3038: pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3039: pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3040: if( idxDir>=0 ){
3041: pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3042: pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3043: pIdxInfo->idxNum = 2;
3044: pIdxInfo->estimatedCost = 10.0;
3045: }else{
3046: pIdxInfo->idxNum = 1;
3047: pIdxInfo->estimatedCost = 100.0;
3048: }
3049: }
3050:
3051: return SQLITE_OK;
3052: }
3053:
3054: /*
3055: ** Register the "fsdir" virtual table.
3056: */
3057: static int fsdirRegister(sqlite3 *db){
3058: static sqlite3_module fsdirModule = {
3059: 0, /* iVersion */
3060: 0, /* xCreate */
3061: fsdirConnect, /* xConnect */
3062: fsdirBestIndex, /* xBestIndex */
3063: fsdirDisconnect, /* xDisconnect */
3064: 0, /* xDestroy */
3065: fsdirOpen, /* xOpen - open a cursor */
3066: fsdirClose, /* xClose - close a cursor */
3067: fsdirFilter, /* xFilter - configure scan constraints */
3068: fsdirNext, /* xNext - advance a cursor */
3069: fsdirEof, /* xEof - check for end of scan */
3070: fsdirColumn, /* xColumn - read data */
3071: fsdirRowid, /* xRowid - read data */
3072: 0, /* xUpdate */
3073: 0, /* xBegin */
3074: 0, /* xSync */
3075: 0, /* xCommit */
3076: 0, /* xRollback */
3077: 0, /* xFindMethod */
3078: 0, /* xRename */
3079: 0, /* xSavepoint */
3080: 0, /* xRelease */
3081: 0, /* xRollbackTo */
3082: 0, /* xShadowName */
3083: };
3084:
3085: int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3086: return rc;
3087: }
3088: #else /* SQLITE_OMIT_VIRTUALTABLE */
3089: # define fsdirRegister(x) SQLITE_OK
3090: #endif
3091:
3092: #ifdef _WIN32
3093:
3094: #endif
3095: int sqlite3_fileio_init(
3096: sqlite3 *db,
3097: char **pzErrMsg,
3098: const sqlite3_api_routines *pApi
3099: ){
3100: int rc = SQLITE_OK;
3101: SQLITE_EXTENSION_INIT2(pApi);
3102: (void)pzErrMsg; /* Unused parameter */
3103: rc = sqlite3_create_function(db, "readfile", 1,
3104: SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3105: readfileFunc, 0, 0);
3106: if( rc==SQLITE_OK ){
3107: rc = sqlite3_create_function(db, "writefile", -1,
3108: SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3109: writefileFunc, 0, 0);
3110: }
3111: if( rc==SQLITE_OK ){
3112: rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3113: lsModeFunc, 0, 0);
3114: }
3115: if( rc==SQLITE_OK ){
3116: rc = fsdirRegister(db);
3117: }
3118: return rc;
3119: }
3120:
3121: /************************* End ../ext/misc/fileio.c ********************/
3122: /************************* Begin ../ext/misc/completion.c ******************/
3123: /*
3124: ** 2017-07-10
3125: **
3126: ** The author disclaims copyright to this source code. In place of
3127: ** a legal notice, here is a blessing:
3128: **
3129: ** May you do good and not evil.
3130: ** May you find forgiveness for yourself and forgive others.
3131: ** May you share freely, never taking more than you give.
3132: **
3133: *************************************************************************
3134: **
3135: ** This file implements an eponymous virtual table that returns suggested
3136: ** completions for a partial SQL input.
3137: **
3138: ** Suggested usage:
3139: **
3140: ** SELECT DISTINCT candidate COLLATE nocase
3141: ** FROM completion($prefix,$wholeline)
3142: ** ORDER BY 1;
3143: **
3144: ** The two query parameters are optional. $prefix is the text of the
3145: ** current word being typed and that is to be completed. $wholeline is
3146: ** the complete input line, used for context.
3147: **
3148: ** The raw completion() table might return the same candidate multiple
3149: ** times, for example if the same column name is used to two or more
3150: ** tables. And the candidates are returned in an arbitrary order. Hence,
3151: ** the DISTINCT and ORDER BY are recommended.
3152: **
3153: ** This virtual table operates at the speed of human typing, and so there
3154: ** is no attempt to make it fast. Even a slow implementation will be much
3155: ** faster than any human can type.
3156: **
3157: */
3158: /* #include "sqlite3ext.h" */
3159: SQLITE_EXTENSION_INIT1
3160: #include <assert.h>
3161: #include <string.h>
3162: #include <ctype.h>
3163:
3164: #ifndef SQLITE_OMIT_VIRTUALTABLE
3165:
3166: /* completion_vtab is a subclass of sqlite3_vtab which will
3167: ** serve as the underlying representation of a completion virtual table
3168: */
3169: typedef struct completion_vtab completion_vtab;
3170: struct completion_vtab {
3171: sqlite3_vtab base; /* Base class - must be first */
3172: sqlite3 *db; /* Database connection for this completion vtab */
3173: };
3174:
3175: /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3176: ** serve as the underlying representation of a cursor that scans
3177: ** over rows of the result
3178: */
3179: typedef struct completion_cursor completion_cursor;
3180: struct completion_cursor {
3181: sqlite3_vtab_cursor base; /* Base class - must be first */
3182: sqlite3 *db; /* Database connection for this cursor */
3183: int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3184: char *zPrefix; /* The prefix for the word we want to complete */
3185: char *zLine; /* The whole that we want to complete */
3186: const char *zCurrentRow; /* Current output row */
3187: int szRow; /* Length of the zCurrentRow string */
3188: sqlite3_stmt *pStmt; /* Current statement */
3189: sqlite3_int64 iRowid; /* The rowid */
3190: int ePhase; /* Current phase */
3191: int j; /* inter-phase counter */
3192: };
3193:
3194: /* Values for ePhase:
3195: */
3196: #define COMPLETION_FIRST_PHASE 1
3197: #define COMPLETION_KEYWORDS 1
3198: #define COMPLETION_PRAGMAS 2
3199: #define COMPLETION_FUNCTIONS 3
3200: #define COMPLETION_COLLATIONS 4
3201: #define COMPLETION_INDEXES 5
3202: #define COMPLETION_TRIGGERS 6
3203: #define COMPLETION_DATABASES 7
3204: #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3205: #define COMPLETION_COLUMNS 9
3206: #define COMPLETION_MODULES 10
3207: #define COMPLETION_EOF 11
3208:
3209: /*
3210: ** The completionConnect() method is invoked to create a new
3211: ** completion_vtab that describes the completion virtual table.
3212: **
3213: ** Think of this routine as the constructor for completion_vtab objects.
3214: **
3215: ** All this routine needs to do is:
3216: **
3217: ** (1) Allocate the completion_vtab object and initialize all fields.
3218: **
3219: ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3220: ** result set of queries against completion will look like.
3221: */
3222: static int completionConnect(
3223: sqlite3 *db,
3224: void *pAux,
3225: int argc, const char *const*argv,
3226: sqlite3_vtab **ppVtab,
3227: char **pzErr
3228: ){
3229: completion_vtab *pNew;
3230: int rc;
3231:
3232: (void)(pAux); /* Unused parameter */
3233: (void)(argc); /* Unused parameter */
3234: (void)(argv); /* Unused parameter */
3235: (void)(pzErr); /* Unused parameter */
3236:
3237: /* Column numbers */
3238: #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3239: #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3240: #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3241: #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3242:
3243: sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3244: rc = sqlite3_declare_vtab(db,
3245: "CREATE TABLE x("
3246: " candidate TEXT,"
3247: " prefix TEXT HIDDEN,"
3248: " wholeline TEXT HIDDEN,"
3249: " phase INT HIDDEN" /* Used for debugging only */
3250: ")");
3251: if( rc==SQLITE_OK ){
3252: pNew = sqlite3_malloc( sizeof(*pNew) );
3253: *ppVtab = (sqlite3_vtab*)pNew;
3254: if( pNew==0 ) return SQLITE_NOMEM;
3255: memset(pNew, 0, sizeof(*pNew));
3256: pNew->db = db;
3257: }
3258: return rc;
3259: }
3260:
3261: /*
3262: ** This method is the destructor for completion_cursor objects.
3263: */
3264: static int completionDisconnect(sqlite3_vtab *pVtab){
3265: sqlite3_free(pVtab);
3266: return SQLITE_OK;
3267: }
3268:
3269: /*
3270: ** Constructor for a new completion_cursor object.
3271: */
3272: static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3273: completion_cursor *pCur;
3274: pCur = sqlite3_malloc( sizeof(*pCur) );
3275: if( pCur==0 ) return SQLITE_NOMEM;
3276: memset(pCur, 0, sizeof(*pCur));
3277: pCur->db = ((completion_vtab*)p)->db;
3278: *ppCursor = &pCur->base;
3279: return SQLITE_OK;
3280: }
3281:
3282: /*
3283: ** Reset the completion_cursor.
3284: */
3285: static void completionCursorReset(completion_cursor *pCur){
3286: sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3287: sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3288: sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3289: pCur->j = 0;
3290: }
3291:
3292: /*
3293: ** Destructor for a completion_cursor.
3294: */
3295: static int completionClose(sqlite3_vtab_cursor *cur){
3296: completionCursorReset((completion_cursor*)cur);
3297: sqlite3_free(cur);
3298: return SQLITE_OK;
3299: }
3300:
3301: /*
3302: ** Advance a completion_cursor to its next row of output.
3303: **
3304: ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3305: ** record the current state of the scan. This routine sets ->zCurrentRow
3306: ** to the current row of output and then returns. If no more rows remain,
3307: ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3308: ** table that has reached the end of its scan.
3309: **
3310: ** The current implementation just lists potential identifiers and
3311: ** keywords and filters them by zPrefix. Future enhancements should
3312: ** take zLine into account to try to restrict the set of identifiers and
3313: ** keywords based on what would be legal at the current point of input.
3314: */
3315: static int completionNext(sqlite3_vtab_cursor *cur){
3316: completion_cursor *pCur = (completion_cursor*)cur;
3317: int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3318: int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3319: pCur->iRowid++;
3320: while( pCur->ePhase!=COMPLETION_EOF ){
3321: switch( pCur->ePhase ){
3322: case COMPLETION_KEYWORDS: {
3323: if( pCur->j >= sqlite3_keyword_count() ){
3324: pCur->zCurrentRow = 0;
3325: pCur->ePhase = COMPLETION_DATABASES;
3326: }else{
3327: sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3328: }
3329: iCol = -1;
3330: break;
3331: }
3332: case COMPLETION_DATABASES: {
3333: if( pCur->pStmt==0 ){
3334: sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3335: &pCur->pStmt, 0);
3336: }
3337: iCol = 1;
3338: eNextPhase = COMPLETION_TABLES;
3339: break;
3340: }
3341: case COMPLETION_TABLES: {
3342: if( pCur->pStmt==0 ){
3343: sqlite3_stmt *pS2;
3344: char *zSql = 0;
3345: const char *zSep = "";
3346: sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3347: while( sqlite3_step(pS2)==SQLITE_ROW ){
3348: const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3349: zSql = sqlite3_mprintf(
3350: "%z%s"
3351: "SELECT name FROM \"%w\".sqlite_schema",
3352: zSql, zSep, zDb
3353: );
3354: if( zSql==0 ) return SQLITE_NOMEM;
3355: zSep = " UNION ";
3356: }
3357: sqlite3_finalize(pS2);
3358: sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3359: sqlite3_free(zSql);
3360: }
3361: iCol = 0;
3362: eNextPhase = COMPLETION_COLUMNS;
3363: break;
3364: }
3365: case COMPLETION_COLUMNS: {
3366: if( pCur->pStmt==0 ){
3367: sqlite3_stmt *pS2;
3368: char *zSql = 0;
3369: const char *zSep = "";
3370: sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3371: while( sqlite3_step(pS2)==SQLITE_ROW ){
3372: const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3373: zSql = sqlite3_mprintf(
3374: "%z%s"
3375: "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3376: " JOIN pragma_table_info(sm.name,%Q) AS pti"
3377: " WHERE sm.type='table'",
3378: zSql, zSep, zDb, zDb
3379: );
3380: if( zSql==0 ) return SQLITE_NOMEM;
3381: zSep = " UNION ";
3382: }
3383: sqlite3_finalize(pS2);
3384: sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3385: sqlite3_free(zSql);
3386: }
3387: iCol = 0;
3388: eNextPhase = COMPLETION_EOF;
3389: break;
3390: }
3391: }
3392: if( iCol<0 ){
3393: /* This case is when the phase presets zCurrentRow */
3394: if( pCur->zCurrentRow==0 ) continue;
3395: }else{
3396: if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3397: /* Extract the next row of content */
3398: pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3399: pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3400: }else{
3401: /* When all rows are finished, advance to the next phase */
3402: sqlite3_finalize(pCur->pStmt);
3403: pCur->pStmt = 0;
3404: pCur->ePhase = eNextPhase;
3405: continue;
3406: }
3407: }
3408: if( pCur->nPrefix==0 ) break;
3409: if( pCur->nPrefix<=pCur->szRow
3410: && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3411: ){
3412: break;
3413: }
3414: }
3415:
3416: return SQLITE_OK;
3417: }
3418:
3419: /*
3420: ** Return values of columns for the row at which the completion_cursor
3421: ** is currently pointing.
3422: */
3423: static int completionColumn(
3424: sqlite3_vtab_cursor *cur, /* The cursor */
3425: sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3426: int i /* Which column to return */
3427: ){
3428: completion_cursor *pCur = (completion_cursor*)cur;
3429: switch( i ){
3430: case COMPLETION_COLUMN_CANDIDATE: {
3431: sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3432: break;
3433: }
3434: case COMPLETION_COLUMN_PREFIX: {
3435: sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3436: break;
3437: }
3438: case COMPLETION_COLUMN_WHOLELINE: {
3439: sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3440: break;
3441: }
3442: case COMPLETION_COLUMN_PHASE: {
3443: sqlite3_result_int(ctx, pCur->ePhase);
3444: break;
3445: }
3446: }
3447: return SQLITE_OK;
3448: }
3449:
3450: /*
3451: ** Return the rowid for the current row. In this implementation, the
3452: ** rowid is the same as the output value.
3453: */
3454: static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3455: completion_cursor *pCur = (completion_cursor*)cur;
3456: *pRowid = pCur->iRowid;
3457: return SQLITE_OK;
3458: }
3459:
3460: /*
3461: ** Return TRUE if the cursor has been moved off of the last
3462: ** row of output.
3463: */
3464: static int completionEof(sqlite3_vtab_cursor *cur){
3465: completion_cursor *pCur = (completion_cursor*)cur;
3466: return pCur->ePhase >= COMPLETION_EOF;
3467: }
3468:
3469: /*
3470: ** This method is called to "rewind" the completion_cursor object back
3471: ** to the first row of output. This method is always called at least
3472: ** once prior to any call to completionColumn() or completionRowid() or
3473: ** completionEof().
3474: */
3475: static int completionFilter(
3476: sqlite3_vtab_cursor *pVtabCursor,
3477: int idxNum, const char *idxStr,
3478: int argc, sqlite3_value **argv
3479: ){
3480: completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3481: int iArg = 0;
3482: (void)(idxStr); /* Unused parameter */
3483: (void)(argc); /* Unused parameter */
3484: completionCursorReset(pCur);
3485: if( idxNum & 1 ){
3486: pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3487: if( pCur->nPrefix>0 ){
3488: pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3489: if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3490: }
3491: iArg = 1;
3492: }
3493: if( idxNum & 2 ){
3494: pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3495: if( pCur->nLine>0 ){
3496: pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3497: if( pCur->zLine==0 ) return SQLITE_NOMEM;
3498: }
3499: }
3500: if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3501: int i = pCur->nLine;
3502: while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3503: i--;
3504: }
3505: pCur->nPrefix = pCur->nLine - i;
3506: if( pCur->nPrefix>0 ){
3507: pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3508: if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3509: }
3510: }
3511: pCur->iRowid = 0;
3512: pCur->ePhase = COMPLETION_FIRST_PHASE;
3513: return completionNext(pVtabCursor);
3514: }
3515:
3516: /*
3517: ** SQLite will invoke this method one or more times while planning a query
3518: ** that uses the completion virtual table. This routine needs to create
3519: ** a query plan for each invocation and compute an estimated cost for that
3520: ** plan.
3521: **
3522: ** There are two hidden parameters that act as arguments to the table-valued
3523: ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3524: ** is available and bit 1 is set if "wholeline" is available.
3525: */
3526: static int completionBestIndex(
3527: sqlite3_vtab *tab,
3528: sqlite3_index_info *pIdxInfo
3529: ){
3530: int i; /* Loop over constraints */
3531: int idxNum = 0; /* The query plan bitmask */
3532: int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3533: int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3534: int nArg = 0; /* Number of arguments that completeFilter() expects */
3535: const struct sqlite3_index_constraint *pConstraint;
3536:
3537: (void)(tab); /* Unused parameter */
3538: pConstraint = pIdxInfo->aConstraint;
3539: for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3540: if( pConstraint->usable==0 ) continue;
3541: if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3542: switch( pConstraint->iColumn ){
3543: case COMPLETION_COLUMN_PREFIX:
3544: prefixIdx = i;
3545: idxNum |= 1;
3546: break;
3547: case COMPLETION_COLUMN_WHOLELINE:
3548: wholelineIdx = i;
3549: idxNum |= 2;
3550: break;
3551: }
3552: }
3553: if( prefixIdx>=0 ){
3554: pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3555: pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3556: }
3557: if( wholelineIdx>=0 ){
3558: pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3559: pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3560: }
3561: pIdxInfo->idxNum = idxNum;
3562: pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3563: pIdxInfo->estimatedRows = 500 - 100*nArg;
3564: return SQLITE_OK;
3565: }
3566:
3567: /*
3568: ** This following structure defines all the methods for the
3569: ** completion virtual table.
3570: */
3571: static sqlite3_module completionModule = {
3572: 0, /* iVersion */
3573: 0, /* xCreate */
3574: completionConnect, /* xConnect */
3575: completionBestIndex, /* xBestIndex */
3576: completionDisconnect, /* xDisconnect */
3577: 0, /* xDestroy */
3578: completionOpen, /* xOpen - open a cursor */
3579: completionClose, /* xClose - close a cursor */
3580: completionFilter, /* xFilter - configure scan constraints */
3581: completionNext, /* xNext - advance a cursor */
3582: completionEof, /* xEof - check for end of scan */
3583: completionColumn, /* xColumn - read data */
3584: completionRowid, /* xRowid - read data */
3585: 0, /* xUpdate */
3586: 0, /* xBegin */
3587: 0, /* xSync */
3588: 0, /* xCommit */
3589: 0, /* xRollback */
3590: 0, /* xFindMethod */
3591: 0, /* xRename */
3592: 0, /* xSavepoint */
3593: 0, /* xRelease */
3594: 0, /* xRollbackTo */
3595: 0 /* xShadowName */
3596: };
3597:
3598: #endif /* SQLITE_OMIT_VIRTUALTABLE */
3599:
3600: int sqlite3CompletionVtabInit(sqlite3 *db){
3601: int rc = SQLITE_OK;
3602: #ifndef SQLITE_OMIT_VIRTUALTABLE
3603: rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3604: #endif
3605: return rc;
3606: }
3607:
3608: #ifdef _WIN32
3609:
3610: #endif
3611: int sqlite3_completion_init(
3612: sqlite3 *db,
3613: char **pzErrMsg,
3614: const sqlite3_api_routines *pApi
3615: ){
3616: int rc = SQLITE_OK;
3617: SQLITE_EXTENSION_INIT2(pApi);
3618: (void)(pzErrMsg); /* Unused parameter */
3619: #ifndef SQLITE_OMIT_VIRTUALTABLE
3620: rc = sqlite3CompletionVtabInit(db);
3621: #endif
3622: return rc;
3623: }
3624:
3625: /************************* End ../ext/misc/completion.c ********************/
3626: /************************* Begin ../ext/misc/appendvfs.c ******************/
3627: /*
3628: ** 2017-10-20
3629: **
3630: ** The author disclaims copyright to this source code. In place of
3631: ** a legal notice, here is a blessing:
3632: **
3633: ** May you do good and not evil.
3634: ** May you find forgiveness for yourself and forgive others.
3635: ** May you share freely, never taking more than you give.
3636: **
3637: ******************************************************************************
3638: **
3639: ** This file implements a VFS shim that allows an SQLite database to be
3640: ** appended onto the end of some other file, such as an executable.
3641: **
3642: ** A special record must appear at the end of the file that identifies the
3643: ** file as an appended database and provides the offset to the first page
3644: ** of the exposed content. (Or, it is the length of the content prefix.)
3645: ** For best performance page 1 should be located at a disk page boundary,
3646: ** though that is not required.
3647: **
3648: ** When opening a database using this VFS, the connection might treat
3649: ** the file as an ordinary SQLite database, or it might treat it as a
3650: ** database appended onto some other file. The decision is made by
3651: ** applying the following rules in order:
3652: **
3653: ** (1) An empty file is an ordinary database.
3654: **
3655: ** (2) If the file ends with the appendvfs trailer string
3656: ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3657: **
3658: ** (3) If the file begins with the standard SQLite prefix string
3659: ** "SQLite format 3", that file is an ordinary database.
3660: **
3661: ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3662: ** set, then a new database is appended to the already existing file.
3663: **
3664: ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3665: **
3666: ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3667: ** the file containing the database is limited to 1GB. (1000013824 bytes)
3668: ** This VFS will not read or write past the 1GB mark. This restriction
3669: ** might be lifted in future versions. For now, if you need a larger
3670: ** database, then keep it in a separate file.
3671: **
3672: ** If the file being opened is a plain database (not an appended one), then
3673: ** this shim is a pass-through into the default underlying VFS. (rule 3)
3674: **/
3675: /* #include "sqlite3ext.h" */
3676: SQLITE_EXTENSION_INIT1
3677: #include <string.h>
3678: #include <assert.h>
3679:
3680: /* The append mark at the end of the database is:
3681: **
3682: ** Start-Of-SQLite3-NNNNNNNN
3683: ** 123456789 123456789 12345
3684: **
3685: ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3686: ** the offset to page 1, and also the length of the prefix content.
3687: */
3688: #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3689: #define APND_MARK_PREFIX_SZ 17
3690: #define APND_MARK_FOS_SZ 8
3691: #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3692:
3693: /*
3694: ** Maximum size of the combined prefix + database + append-mark. This
3695: ** must be less than 0x40000000 to avoid locking issues on Windows.
3696: */
3697: #define APND_MAX_SIZE (65536*15259)
3698:
3699: /*
3700: ** Size of storage page upon which to align appendvfs portion.
3701: */
3702: #ifndef APND_ROUNDUP_BITS
3703: #define APND_ROUNDUP_BITS 12
3704: #endif
3705:
3706: /*
3707: ** Forward declaration of objects used by this utility
3708: */
3709: typedef struct sqlite3_vfs ApndVfs;
3710: typedef struct ApndFile ApndFile;
3711:
3712: /* Access to a lower-level VFS that (might) implement dynamic loading,
3713: ** access to randomness, etc.
3714: */
3715: #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3716: #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3717:
3718: /* Invariants for an open appendvfs file:
3719: * Once an appendvfs file is opened, it will be in one of three states:
3720: * State 0: Never written. Underlying file (if any) is unaltered.
3721: * State 1: Append mark is persisted, content write is in progress.
3722: * State 2: Append mark is persisted, content writes are complete.
3723: *
3724: * State 0 is persistent in the sense that nothing will have been done
3725: * to the underlying file, including any attempt to convert it to an
3726: * appendvfs file.
3727: *
3728: * State 1 is normally transitory. However, if a write operation ends
3729: * abnormally (disk full, power loss, process kill, etc.), then State 1
3730: * may be persistent on disk with an incomplete content write-out. This
3731: * is logically equivalent to an interrupted write to an ordinary file,
3732: * where some unknown portion of to-be-written data is persisted while
3733: * the remainder is not. Database integrity in such cases is maintained
3734: * (or not) by the same measures available for ordinary file access.
3735: *
3736: * State 2 is persistent under normal circumstances (when there is no
3737: * abnormal termination of a write operation such that data provided
3738: * to the underlying VFS write method has not yet reached storage.)
3739: *
3740: * In order to maintain the state invariant, the append mark is written
3741: * in advance of content writes where any part of such content would
3742: * overwrite an existing (or yet to be written) append mark.
3743: */
3744: struct ApndFile {
3745: /* Access to IO methods of the underlying file */
3746: sqlite3_file base;
3747: /* File offset to beginning of appended content (unchanging) */
3748: sqlite3_int64 iPgOne;
3749: /* File offset of written append-mark, or -1 if unwritten */
3750: sqlite3_int64 iMark;
3751: };
3752:
3753: /*
3754: ** Methods for ApndFile
3755: */
3756: static int apndClose(sqlite3_file*);
3757: static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3758: static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3759: static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3760: static int apndSync(sqlite3_file*, int flags);
3761: static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3762: static int apndLock(sqlite3_file*, int);
3763: static int apndUnlock(sqlite3_file*, int);
3764: static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3765: static int apndFileControl(sqlite3_file*, int op, void *pArg);
3766: static int apndSectorSize(sqlite3_file*);
3767: static int apndDeviceCharacteristics(sqlite3_file*);
3768: static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3769: static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3770: static void apndShmBarrier(sqlite3_file*);
3771: static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3772: static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3773: static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3774:
3775: /*
3776: ** Methods for ApndVfs
3777: */
3778: static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3779: static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3780: static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3781: static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3782: static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3783: static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3784: static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3785: static void apndDlClose(sqlite3_vfs*, void*);
3786: static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3787: static int apndSleep(sqlite3_vfs*, int microseconds);
3788: static int apndCurrentTime(sqlite3_vfs*, double*);
3789: static int apndGetLastError(sqlite3_vfs*, int, char *);
3790: static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3791: static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3792: static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3793: static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3794:
3795: static sqlite3_vfs apnd_vfs = {
3796: 3, /* iVersion (set when registered) */
3797: 0, /* szOsFile (set when registered) */
3798: 1024, /* mxPathname */
3799: 0, /* pNext */
3800: "apndvfs", /* zName */
3801: 0, /* pAppData (set when registered) */
3802: apndOpen, /* xOpen */
3803: apndDelete, /* xDelete */
3804: apndAccess, /* xAccess */
3805: apndFullPathname, /* xFullPathname */
3806: apndDlOpen, /* xDlOpen */
3807: apndDlError, /* xDlError */
3808: apndDlSym, /* xDlSym */
3809: apndDlClose, /* xDlClose */
3810: apndRandomness, /* xRandomness */
3811: apndSleep, /* xSleep */
3812: apndCurrentTime, /* xCurrentTime */
3813: apndGetLastError, /* xGetLastError */
3814: apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3815: apndSetSystemCall, /* xSetSystemCall */
3816: apndGetSystemCall, /* xGetSystemCall */
3817: apndNextSystemCall /* xNextSystemCall */
3818: };
3819:
3820: static const sqlite3_io_methods apnd_io_methods = {
3821: 3, /* iVersion */
3822: apndClose, /* xClose */
3823: apndRead, /* xRead */
3824: apndWrite, /* xWrite */
3825: apndTruncate, /* xTruncate */
3826: apndSync, /* xSync */
3827: apndFileSize, /* xFileSize */
3828: apndLock, /* xLock */
3829: apndUnlock, /* xUnlock */
3830: apndCheckReservedLock, /* xCheckReservedLock */
3831: apndFileControl, /* xFileControl */
3832: apndSectorSize, /* xSectorSize */
3833: apndDeviceCharacteristics, /* xDeviceCharacteristics */
3834: apndShmMap, /* xShmMap */
3835: apndShmLock, /* xShmLock */
3836: apndShmBarrier, /* xShmBarrier */
3837: apndShmUnmap, /* xShmUnmap */
3838: apndFetch, /* xFetch */
3839: apndUnfetch /* xUnfetch */
3840: };
3841:
3842: /*
3843: ** Close an apnd-file.
3844: */
3845: static int apndClose(sqlite3_file *pFile){
3846: pFile = ORIGFILE(pFile);
3847: return pFile->pMethods->xClose(pFile);
3848: }
3849:
3850: /*
3851: ** Read data from an apnd-file.
3852: */
3853: static int apndRead(
3854: sqlite3_file *pFile,
3855: void *zBuf,
3856: int iAmt,
3857: sqlite_int64 iOfst
3858: ){
3859: ApndFile *paf = (ApndFile *)pFile;
3860: pFile = ORIGFILE(pFile);
3861: return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3862: }
3863:
3864: /*
3865: ** Add the append-mark onto what should become the end of the file.
3866: * If and only if this succeeds, internal ApndFile.iMark is updated.
3867: * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3868: */
3869: static int apndWriteMark(
3870: ApndFile *paf,
3871: sqlite3_file *pFile,
3872: sqlite_int64 iWriteEnd
3873: ){
3874: sqlite_int64 iPgOne = paf->iPgOne;
3875: unsigned char a[APND_MARK_SIZE];
3876: int i = APND_MARK_FOS_SZ;
3877: int rc;
3878: assert(pFile == ORIGFILE(paf));
3879: memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3880: while (--i >= 0) {
3881: a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3882: iPgOne >>= 8;
3883: }
3884: iWriteEnd += paf->iPgOne;
3885: if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3886: (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3887: paf->iMark = iWriteEnd;
3888: }
3889: return rc;
3890: }
3891:
3892: /*
3893: ** Write data to an apnd-file.
3894: */
3895: static int apndWrite(
3896: sqlite3_file *pFile,
3897: const void *zBuf,
3898: int iAmt,
3899: sqlite_int64 iOfst
3900: ){
3901: ApndFile *paf = (ApndFile *)pFile;
3902: sqlite_int64 iWriteEnd = iOfst + iAmt;
3903: if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3904: pFile = ORIGFILE(pFile);
3905: /* If append-mark is absent or will be overwritten, write it. */
3906: if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3907: int rc = apndWriteMark(paf, pFile, iWriteEnd);
3908: if( SQLITE_OK!=rc )
3909: return rc;
3910: }
3911: return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3912: }
3913:
3914: /*
3915: ** Truncate an apnd-file.
3916: */
3917: static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3918: ApndFile *paf = (ApndFile *)pFile;
3919: pFile = ORIGFILE(pFile);
3920: /* The append mark goes out first so truncate failure does not lose it. */
3921: if( SQLITE_OK!=apndWriteMark(paf, pFile, size) )
3922: return SQLITE_IOERR;
3923: /* Truncate underlying file just past append mark */
3924: return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3925: }
3926:
3927: /*
3928: ** Sync an apnd-file.
3929: */
3930: static int apndSync(sqlite3_file *pFile, int flags){
3931: pFile = ORIGFILE(pFile);
3932: return pFile->pMethods->xSync(pFile, flags);
3933: }
3934:
3935: /*
3936: ** Return the current file-size of an apnd-file.
3937: ** If the append mark is not yet there, the file-size is 0.
3938: */
3939: static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3940: ApndFile *paf = (ApndFile *)pFile;
3941: *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3942: return SQLITE_OK;
3943: }
3944:
3945: /*
3946: ** Lock an apnd-file.
3947: */
3948: static int apndLock(sqlite3_file *pFile, int eLock){
3949: pFile = ORIGFILE(pFile);
3950: return pFile->pMethods->xLock(pFile, eLock);
3951: }
3952:
3953: /*
3954: ** Unlock an apnd-file.
3955: */
3956: static int apndUnlock(sqlite3_file *pFile, int eLock){
3957: pFile = ORIGFILE(pFile);
3958: return pFile->pMethods->xUnlock(pFile, eLock);
3959: }
3960:
3961: /*
3962: ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3963: */
3964: static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3965: pFile = ORIGFILE(pFile);
3966: return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3967: }
3968:
3969: /*
3970: ** File control method. For custom operations on an apnd-file.
3971: */
3972: static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3973: ApndFile *paf = (ApndFile *)pFile;
3974: int rc;
3975: pFile = ORIGFILE(pFile);
3976: if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
3977: rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3978: if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3979: *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
3980: }
3981: return rc;
3982: }
3983:
3984: /*
3985: ** Return the sector-size in bytes for an apnd-file.
3986: */
3987: static int apndSectorSize(sqlite3_file *pFile){
3988: pFile = ORIGFILE(pFile);
3989: return pFile->pMethods->xSectorSize(pFile);
3990: }
3991:
3992: /*
3993: ** Return the device characteristic flags supported by an apnd-file.
3994: */
3995: static int apndDeviceCharacteristics(sqlite3_file *pFile){
3996: pFile = ORIGFILE(pFile);
3997: return pFile->pMethods->xDeviceCharacteristics(pFile);
3998: }
3999:
4000: /* Create a shared memory file mapping */
4001: static int apndShmMap(
4002: sqlite3_file *pFile,
4003: int iPg,
4004: int pgsz,
4005: int bExtend,
4006: void volatile **pp
4007: ){
4008: pFile = ORIGFILE(pFile);
4009: return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4010: }
4011:
4012: /* Perform locking on a shared-memory segment */
4013: static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4014: pFile = ORIGFILE(pFile);
4015: return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4016: }
4017:
4018: /* Memory barrier operation on shared memory */
4019: static void apndShmBarrier(sqlite3_file *pFile){
4020: pFile = ORIGFILE(pFile);
4021: pFile->pMethods->xShmBarrier(pFile);
4022: }
4023:
4024: /* Unmap a shared memory segment */
4025: static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4026: pFile = ORIGFILE(pFile);
4027: return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4028: }
4029:
4030: /* Fetch a page of a memory-mapped file */
4031: static int apndFetch(
4032: sqlite3_file *pFile,
4033: sqlite3_int64 iOfst,
4034: int iAmt,
4035: void **pp
4036: ){
4037: ApndFile *p = (ApndFile *)pFile;
4038: if( p->iMark < 0 || iOfst+iAmt > p->iMark)
4039: return SQLITE_IOERR; /* Cannot read what is not yet there. */
4040: pFile = ORIGFILE(pFile);
4041: return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4042: }
4043:
4044: /* Release a memory-mapped page */
4045: static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4046: ApndFile *p = (ApndFile *)pFile;
4047: pFile = ORIGFILE(pFile);
4048: return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4049: }
4050:
4051: /*
4052: ** Try to read the append-mark off the end of a file. Return the
4053: ** start of the appended database if the append-mark is present.
4054: ** If there is no valid append-mark, return -1;
4055: **
4056: ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4057: ** indicates that the appended database contains at least one page. The
4058: ** start-of-database value must be a multiple of 512.
4059: */
4060: static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4061: int rc, i;
4062: sqlite3_int64 iMark;
4063: int msbs = 8 * (APND_MARK_FOS_SZ-1);
4064: unsigned char a[APND_MARK_SIZE];
4065:
4066: if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4067: rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4068: if( rc ) return -1;
4069: if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4070: iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4071: for(i=1; i<8; i++){
4072: msbs -= 8;
4073: iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4074: }
4075: if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4076: if( iMark & 0x1ff ) return -1;
4077: return iMark;
4078: }
4079:
4080: static const char apvfsSqliteHdr[] = "SQLite format 3";
4081: /*
4082: ** Check to see if the file is an appendvfs SQLite database file.
4083: ** Return true iff it is such. Parameter sz is the file's size.
4084: */
4085: static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4086: int rc;
4087: char zHdr[16];
4088: sqlite3_int64 iMark = apndReadMark(sz, pFile);
4089: if( iMark>=0 ){
4090: /* If file has right end-marker, the expected odd size, and the
4091: * SQLite DB type marker where the end-marker puts it, then it
4092: * is an appendvfs database (to be treated as such.)
4093: */
4094: rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4095: if( SQLITE_OK==rc && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4096: && (sz & 0x1ff)== APND_MARK_SIZE && sz>=512+APND_MARK_SIZE )
4097: return 1; /* It's an appendvfs database */
4098: }
4099: return 0;
4100: }
4101:
4102: /*
4103: ** Check to see if the file is an ordinary SQLite database file.
4104: ** Return true iff so. Parameter sz is the file's size.
4105: */
4106: static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4107: char zHdr[16];
4108: if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4109: || (sz & 0x1ff) != 0
4110: || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4111: || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4112: ){
4113: return 0;
4114: }else{
4115: return 1;
4116: }
4117: }
4118:
4119: /* Round-up used to get appendvfs portion to begin at a page boundary. */
4120: #define APND_ALIGN_MASK(nbits) ((1<<nbits)-1)
4121: #define APND_START_ROUNDUP(fsz, nbits) \
4122: ( ((fsz)+APND_ALIGN_MASK(nbits)) & ~(sqlite3_int64)APND_ALIGN_MASK(nbits) )
4123:
4124: /*
4125: ** Open an apnd file handle.
4126: */
4127: static int apndOpen(
4128: sqlite3_vfs *pVfs,
4129: const char *zName,
4130: sqlite3_file *pFile,
4131: int flags,
4132: int *pOutFlags
4133: ){
4134: ApndFile *p;
4135: sqlite3_file *pSubFile;
4136: sqlite3_vfs *pSubVfs;
4137: int rc;
4138: sqlite3_int64 sz;
4139: pSubVfs = ORIGVFS(pVfs);
4140: if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4141: /* The appendvfs is not to be used for transient or temporary databases. */
4142: return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4143: }
4144: p = (ApndFile*)pFile;
4145: memset(p, 0, sizeof(*p));
4146: pSubFile = ORIGFILE(pFile);
4147: pFile->pMethods = &apnd_io_methods;
4148: rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4149: if( rc ) goto apnd_open_done;
4150: rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4151: if( rc ){
4152: pSubFile->pMethods->xClose(pSubFile);
4153: goto apnd_open_done;
4154: }
4155: if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4156: memmove(pFile, pSubFile, pSubVfs->szOsFile);
4157: return SQLITE_OK;
4158: }
4159: /* Record that append mark has not been written until seen otherwise. */
4160: p->iMark = -1;
4161: p->iPgOne = apndReadMark(sz, pFile);
4162: if( p->iPgOne>=0 ){
4163: /* Append mark was found, infer its offset */
4164: p->iMark = sz - p->iPgOne - APND_MARK_SIZE;
4165: return SQLITE_OK;
4166: }
4167: if( (flags & SQLITE_OPEN_CREATE)==0 ){
4168: pSubFile->pMethods->xClose(pSubFile);
4169: rc = SQLITE_CANTOPEN;
4170: }
4171: /* Round newly added appendvfs location to #define'd page boundary.
4172: * Note that nothing has yet been written to the underlying file.
4173: * The append mark will be written along with first content write.
4174: * Until then, the p->iMark value indicates it is not yet written.
4175: */
4176: p->iPgOne = APND_START_ROUNDUP(sz, APND_ROUNDUP_BITS);
4177: apnd_open_done:
4178: if( rc ) pFile->pMethods = 0;
4179: return rc;
4180: }
4181:
4182: /*
4183: ** Delete an apnd file.
4184: ** For an appendvfs, this could mean delete the appendvfs portion,
4185: ** leaving the appendee as it was before it gained an appendvfs.
4186: ** For now, this code deletes the underlying file too.
4187: */
4188: static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4189: return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4190: }
4191:
4192: /*
4193: ** All other VFS methods are pass-thrus.
4194: */
4195: static int apndAccess(
4196: sqlite3_vfs *pVfs,
4197: const char *zPath,
4198: int flags,
4199: int *pResOut
4200: ){
4201: return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4202: }
4203: static int apndFullPathname(
4204: sqlite3_vfs *pVfs,
4205: const char *zPath,
4206: int nOut,
4207: char *zOut
4208: ){
4209: return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4210: }
4211: static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4212: return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4213: }
4214: static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4215: ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4216: }
4217: static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4218: return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4219: }
4220: static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4221: ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4222: }
4223: static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4224: return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4225: }
4226: static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4227: return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4228: }
4229: static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4230: return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4231: }
4232: static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4233: return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4234: }
4235: static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4236: return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4237: }
4238: static int apndSetSystemCall(
4239: sqlite3_vfs *pVfs,
4240: const char *zName,
4241: sqlite3_syscall_ptr pCall
4242: ){
4243: return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4244: }
4245: static sqlite3_syscall_ptr apndGetSystemCall(
4246: sqlite3_vfs *pVfs,
4247: const char *zName
4248: ){
4249: return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4250: }
4251: static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4252: return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4253: }
4254:
4255:
4256: #ifdef _WIN32
4257:
4258: #endif
4259: /*
4260: ** This routine is called when the extension is loaded.
4261: ** Register the new VFS.
4262: */
4263: int sqlite3_appendvfs_init(
4264: sqlite3 *db,
4265: char **pzErrMsg,
4266: const sqlite3_api_routines *pApi
4267: ){
4268: int rc = SQLITE_OK;
4269: sqlite3_vfs *pOrig;
4270: SQLITE_EXTENSION_INIT2(pApi);
4271: (void)pzErrMsg;
4272: (void)db;
4273: pOrig = sqlite3_vfs_find(0);
4274: apnd_vfs.iVersion = pOrig->iVersion;
4275: apnd_vfs.pAppData = pOrig;
4276: apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4277: rc = sqlite3_vfs_register(&apnd_vfs, 0);
4278: #ifdef APPENDVFS_TEST
4279: if( rc==SQLITE_OK ){
4280: rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4281: }
4282: #endif
4283: if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4284: return rc;
4285: }
4286:
4287: /************************* End ../ext/misc/appendvfs.c ********************/
4288: /************************* Begin ../ext/misc/memtrace.c ******************/
4289: /*
4290: ** 2019-01-21
4291: **
4292: ** The author disclaims copyright to this source code. In place of
4293: ** a legal notice, here is a blessing:
4294: **
4295: ** May you do good and not evil.
4296: ** May you find forgiveness for yourself and forgive others.
4297: ** May you share freely, never taking more than you give.
4298: **
4299: *************************************************************************
4300: **
4301: ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4302: ** mechanism to add a tracing layer on top of SQLite. If this extension
4303: ** is registered prior to sqlite3_initialize(), it will cause all memory
4304: ** allocation activities to be logged on standard output, or to some other
4305: ** FILE specified by the initializer.
4306: **
4307: ** This file needs to be compiled into the application that uses it.
4308: **
4309: ** This extension is used to implement the --memtrace option of the
4310: ** command-line shell.
4311: */
4312: #include <assert.h>
4313: #include <string.h>
4314: #include <stdio.h>
4315:
4316: /* The original memory allocation routines */
4317: static sqlite3_mem_methods memtraceBase;
4318: static FILE *memtraceOut;
4319:
4320: /* Methods that trace memory allocations */
4321: static void *memtraceMalloc(int n){
4322: if( memtraceOut ){
4323: fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4324: memtraceBase.xRoundup(n));
4325: }
4326: return memtraceBase.xMalloc(n);
4327: }
4328: static void memtraceFree(void *p){
4329: if( p==0 ) return;
4330: if( memtraceOut ){
4331: fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4332: }
4333: memtraceBase.xFree(p);
4334: }
4335: static void *memtraceRealloc(void *p, int n){
4336: if( p==0 ) return memtraceMalloc(n);
4337: if( n==0 ){
4338: memtraceFree(p);
4339: return 0;
4340: }
4341: if( memtraceOut ){
4342: fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4343: memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4344: }
4345: return memtraceBase.xRealloc(p, n);
4346: }
4347: static int memtraceSize(void *p){
4348: return memtraceBase.xSize(p);
4349: }
4350: static int memtraceRoundup(int n){
4351: return memtraceBase.xRoundup(n);
4352: }
4353: static int memtraceInit(void *p){
4354: return memtraceBase.xInit(p);
4355: }
4356: static void memtraceShutdown(void *p){
4357: memtraceBase.xShutdown(p);
4358: }
4359:
4360: /* The substitute memory allocator */
4361: static sqlite3_mem_methods ersaztMethods = {
4362: memtraceMalloc,
4363: memtraceFree,
4364: memtraceRealloc,
4365: memtraceSize,
4366: memtraceRoundup,
4367: memtraceInit,
4368: memtraceShutdown,
4369: 0
4370: };
4371:
4372: /* Begin tracing memory allocations to out. */
4373: int sqlite3MemTraceActivate(FILE *out){
4374: int rc = SQLITE_OK;
4375: if( memtraceBase.xMalloc==0 ){
4376: rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4377: if( rc==SQLITE_OK ){
4378: rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4379: }
4380: }
4381: memtraceOut = out;
4382: return rc;
4383: }
4384:
4385: /* Deactivate memory tracing */
4386: int sqlite3MemTraceDeactivate(void){
4387: int rc = SQLITE_OK;
4388: if( memtraceBase.xMalloc!=0 ){
4389: rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4390: if( rc==SQLITE_OK ){
4391: memset(&memtraceBase, 0, sizeof(memtraceBase));
4392: }
4393: }
4394: memtraceOut = 0;
4395: return rc;
4396: }
4397:
4398: /************************* End ../ext/misc/memtrace.c ********************/
4399: /************************* Begin ../ext/misc/uint.c ******************/
4400: /*
4401: ** 2020-04-14
4402: **
4403: ** The author disclaims copyright to this source code. In place of
4404: ** a legal notice, here is a blessing:
4405: **
4406: ** May you do good and not evil.
4407: ** May you find forgiveness for yourself and forgive others.
4408: ** May you share freely, never taking more than you give.
4409: **
4410: ******************************************************************************
4411: **
4412: ** This SQLite extension implements the UINT collating sequence.
4413: **
4414: ** UINT works like BINARY for text, except that embedded strings
4415: ** of digits compare in numeric order.
4416: **
4417: ** * Leading zeros are handled properly, in the sense that
4418: ** they do not mess of the maginitude comparison of embedded
4419: ** strings of digits. "x00123y" is equal to "x123y".
4420: **
4421: ** * Only unsigned integers are recognized. Plus and minus
4422: ** signs are ignored. Decimal points and exponential notation
4423: ** are ignored.
4424: **
4425: ** * Embedded integers can be of arbitrary length. Comparison
4426: ** is *not* limited integers that can be expressed as a
4427: ** 64-bit machine integer.
4428: */
4429: /* #include "sqlite3ext.h" */
4430: SQLITE_EXTENSION_INIT1
4431: #include <assert.h>
4432: #include <string.h>
4433: #include <ctype.h>
4434:
4435: /*
4436: ** Compare text in lexicographic order, except strings of digits
4437: ** compare in numeric order.
4438: */
4439: static int uintCollFunc(
4440: void *notUsed,
4441: int nKey1, const void *pKey1,
4442: int nKey2, const void *pKey2
4443: ){
4444: const unsigned char *zA = (const unsigned char*)pKey1;
4445: const unsigned char *zB = (const unsigned char*)pKey2;
4446: int i=0, j=0, x;
4447: (void)notUsed;
4448: while( i<nKey1 && j<nKey2 ){
4449: x = zA[i] - zB[j];
4450: if( isdigit(zA[i]) ){
4451: int k;
4452: if( !isdigit(zB[j]) ) return x;
4453: while( i<nKey1 && zA[i]=='0' ){ i++; }
4454: while( j<nKey2 && zB[j]=='0' ){ j++; }
4455: k = 0;
4456: while( i+k<nKey1 && isdigit(zA[i+k])
4457: && j+k<nKey2 && isdigit(zB[j+k]) ){
4458: k++;
4459: }
4460: if( i+k<nKey1 && isdigit(zA[i+k]) ){
4461: return +1;
4462: }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4463: return -1;
4464: }else{
4465: x = memcmp(zA+i, zB+j, k);
4466: if( x ) return x;
4467: i += k;
4468: j += k;
4469: }
4470: }else if( x ){
4471: return x;
4472: }else{
4473: i++;
4474: j++;
4475: }
4476: }
4477: return (nKey1 - i) - (nKey2 - j);
4478: }
4479:
4480: #ifdef _WIN32
4481:
4482: #endif
4483: int sqlite3_uint_init(
4484: sqlite3 *db,
4485: char **pzErrMsg,
4486: const sqlite3_api_routines *pApi
4487: ){
4488: SQLITE_EXTENSION_INIT2(pApi);
4489: (void)pzErrMsg; /* Unused parameter */
4490: return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4491: }
4492:
4493: /************************* End ../ext/misc/uint.c ********************/
4494: /************************* Begin ../ext/misc/decimal.c ******************/
4495: /*
4496: ** 2020-06-22
4497: **
4498: ** The author disclaims copyright to this source code. In place of
4499: ** a legal notice, here is a blessing:
4500: **
4501: ** May you do good and not evil.
4502: ** May you find forgiveness for yourself and forgive others.
4503: ** May you share freely, never taking more than you give.
4504: **
4505: ******************************************************************************
4506: **
4507: ** Routines to implement arbitrary-precision decimal math.
4508: **
4509: ** The focus here is on simplicity and correctness, not performance.
4510: */
4511: /* #include "sqlite3ext.h" */
4512: SQLITE_EXTENSION_INIT1
4513: #include <assert.h>
4514: #include <string.h>
4515: #include <ctype.h>
4516: #include <stdlib.h>
4517:
4518: /* Mark a function parameter as unused, to suppress nuisance compiler
4519: ** warnings. */
4520: #ifndef UNUSED_PARAMETER
4521: # define UNUSED_PARAMETER(X) (void)(X)
4522: #endif
4523:
4524:
4525: /* A decimal object */
4526: typedef struct Decimal Decimal;
4527: struct Decimal {
4528: char sign; /* 0 for positive, 1 for negative */
4529: char oom; /* True if an OOM is encountered */
4530: char isNull; /* True if holds a NULL rather than a number */
4531: char isInit; /* True upon initialization */
4532: int nDigit; /* Total number of digits */
4533: int nFrac; /* Number of digits to the right of the decimal point */
4534: signed char *a; /* Array of digits. Most significant first. */
4535: };
4536:
4537: /*
4538: ** Release memory held by a Decimal, but do not free the object itself.
4539: */
4540: static void decimal_clear(Decimal *p){
4541: sqlite3_free(p->a);
4542: }
4543:
4544: /*
4545: ** Destroy a Decimal object
4546: */
4547: static void decimal_free(Decimal *p){
4548: if( p ){
4549: decimal_clear(p);
4550: sqlite3_free(p);
4551: }
4552: }
4553:
4554: /*
4555: ** Allocate a new Decimal object. Initialize it to the number given
4556: ** by the input string.
4557: */
4558: static Decimal *decimal_new(
4559: sqlite3_context *pCtx,
4560: sqlite3_value *pIn,
4561: int nAlt,
4562: const unsigned char *zAlt
4563: ){
4564: Decimal *p;
4565: int n, i;
4566: const unsigned char *zIn;
4567: int iExp = 0;
4568: p = sqlite3_malloc( sizeof(*p) );
4569: if( p==0 ) goto new_no_mem;
4570: p->sign = 0;
4571: p->oom = 0;
4572: p->isInit = 1;
4573: p->isNull = 0;
4574: p->nDigit = 0;
4575: p->nFrac = 0;
4576: if( zAlt ){
4577: n = nAlt,
4578: zIn = zAlt;
4579: }else{
4580: if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4581: p->a = 0;
4582: p->isNull = 1;
4583: return p;
4584: }
4585: n = sqlite3_value_bytes(pIn);
4586: zIn = sqlite3_value_text(pIn);
4587: }
4588: p->a = sqlite3_malloc64( n+1 );
4589: if( p->a==0 ) goto new_no_mem;
4590: for(i=0; isspace(zIn[i]); i++){}
4591: if( zIn[i]=='-' ){
4592: p->sign = 1;
4593: i++;
4594: }else if( zIn[i]=='+' ){
4595: i++;
4596: }
4597: while( i<n && zIn[i]=='0' ) i++;
4598: while( i<n ){
4599: char c = zIn[i];
4600: if( c>='0' && c<='9' ){
4601: p->a[p->nDigit++] = c - '0';
4602: }else if( c=='.' ){
4603: p->nFrac = p->nDigit + 1;
4604: }else if( c=='e' || c=='E' ){
4605: int j = i+1;
4606: int neg = 0;
4607: if( j>=n ) break;
4608: if( zIn[j]=='-' ){
4609: neg = 1;
4610: j++;
4611: }else if( zIn[j]=='+' ){
4612: j++;
4613: }
4614: while( j<n && iExp<1000000 ){
4615: if( zIn[j]>='0' && zIn[j]<='9' ){
4616: iExp = iExp*10 + zIn[j] - '0';
4617: }
4618: j++;
4619: }
4620: if( neg ) iExp = -iExp;
4621: break;
4622: }
4623: i++;
4624: }
4625: if( p->nFrac ){
4626: p->nFrac = p->nDigit - (p->nFrac - 1);
4627: }
4628: if( iExp>0 ){
4629: if( p->nFrac>0 ){
4630: if( iExp<=p->nFrac ){
4631: p->nFrac -= iExp;
4632: iExp = 0;
4633: }else{
4634: iExp -= p->nFrac;
4635: p->nFrac = 0;
4636: }
4637: }
4638: if( iExp>0 ){
4639: p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4640: if( p->a==0 ) goto new_no_mem;
4641: memset(p->a+p->nDigit, 0, iExp);
4642: p->nDigit += iExp;
4643: }
4644: }else if( iExp<0 ){
4645: int nExtra;
4646: iExp = -iExp;
4647: nExtra = p->nDigit - p->nFrac - 1;
4648: if( nExtra ){
4649: if( nExtra>=iExp ){
4650: p->nFrac += iExp;
4651: iExp = 0;
4652: }else{
4653: iExp -= nExtra;
4654: p->nFrac = p->nDigit - 1;
4655: }
4656: }
4657: if( iExp>0 ){
4658: p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4659: if( p->a==0 ) goto new_no_mem;
4660: memmove(p->a+iExp, p->a, p->nDigit);
4661: memset(p->a, 0, iExp);
4662: p->nDigit += iExp;
4663: p->nFrac += iExp;
4664: }
4665: }
4666: return p;
4667:
4668: new_no_mem:
4669: if( pCtx ) sqlite3_result_error_nomem(pCtx);
4670: sqlite3_free(p);
4671: return 0;
4672: }
4673:
4674: /*
4675: ** Make the given Decimal the result.
4676: */
4677: static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4678: char *z;
4679: int i, j;
4680: int n;
4681: if( p==0 || p->oom ){
4682: sqlite3_result_error_nomem(pCtx);
4683: return;
4684: }
4685: if( p->isNull ){
4686: sqlite3_result_null(pCtx);
4687: return;
4688: }
4689: z = sqlite3_malloc( p->nDigit+4 );
4690: if( z==0 ){
4691: sqlite3_result_error_nomem(pCtx);
4692: return;
4693: }
4694: i = 0;
4695: if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4696: p->sign = 0;
4697: }
4698: if( p->sign ){
4699: z[0] = '-';
4700: i = 1;
4701: }
4702: n = p->nDigit - p->nFrac;
4703: if( n<=0 ){
4704: z[i++] = '0';
4705: }
4706: j = 0;
4707: while( n>1 && p->a[j]==0 ){
4708: j++;
4709: n--;
4710: }
4711: while( n>0 ){
4712: z[i++] = p->a[j] + '0';
4713: j++;
4714: n--;
4715: }
4716: if( p->nFrac ){
4717: z[i++] = '.';
4718: do{
4719: z[i++] = p->a[j] + '0';
4720: j++;
4721: }while( j<p->nDigit );
4722: }
4723: z[i] = 0;
4724: sqlite3_result_text(pCtx, z, i, sqlite3_free);
4725: }
4726:
4727: /*
4728: ** SQL Function: decimal(X)
4729: **
4730: ** Convert input X into decimal and then back into text
4731: */
4732: static void decimalFunc(
4733: sqlite3_context *context,
4734: int argc,
4735: sqlite3_value **argv
4736: ){
4737: Decimal *p = decimal_new(context, argv[0], 0, 0);
4738: UNUSED_PARAMETER(argc);
4739: decimal_result(context, p);
4740: decimal_free(p);
4741: }
4742:
4743: /*
4744: ** Compare to Decimal objects. Return negative, 0, or positive if the
4745: ** first object is less than, equal to, or greater than the second.
4746: **
4747: ** Preconditions for this routine:
4748: **
4749: ** pA!=0
4750: ** pA->isNull==0
4751: ** pB!=0
4752: ** pB->isNull==0
4753: */
4754: static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4755: int nASig, nBSig, rc, n;
4756: if( pA->sign!=pB->sign ){
4757: return pA->sign ? -1 : +1;
4758: }
4759: if( pA->sign ){
4760: const Decimal *pTemp = pA;
4761: pA = pB;
4762: pB = pTemp;
4763: }
4764: nASig = pA->nDigit - pA->nFrac;
4765: nBSig = pB->nDigit - pB->nFrac;
4766: if( nASig!=nBSig ){
4767: return nASig - nBSig;
4768: }
4769: n = pA->nDigit;
4770: if( n>pB->nDigit ) n = pB->nDigit;
4771: rc = memcmp(pA->a, pB->a, n);
4772: if( rc==0 ){
4773: rc = pA->nDigit - pB->nDigit;
4774: }
4775: return rc;
4776: }
4777:
4778: /*
4779: ** SQL Function: decimal_cmp(X, Y)
4780: **
4781: ** Return negative, zero, or positive if X is less then, equal to, or
4782: ** greater than Y.
4783: */
4784: static void decimalCmpFunc(
4785: sqlite3_context *context,
4786: int argc,
4787: sqlite3_value **argv
4788: ){
4789: Decimal *pA = 0, *pB = 0;
4790: int rc;
4791:
4792: UNUSED_PARAMETER(argc);
4793: pA = decimal_new(context, argv[0], 0, 0);
4794: if( pA==0 || pA->isNull ) goto cmp_done;
4795: pB = decimal_new(context, argv[1], 0, 0);
4796: if( pB==0 || pB->isNull ) goto cmp_done;
4797: rc = decimal_cmp(pA, pB);
4798: if( rc<0 ) rc = -1;
4799: else if( rc>0 ) rc = +1;
4800: sqlite3_result_int(context, rc);
4801: cmp_done:
4802: decimal_free(pA);
4803: decimal_free(pB);
4804: }
4805:
4806: /*
4807: ** Expand the Decimal so that it has a least nDigit digits and nFrac
4808: ** digits to the right of the decimal point.
4809: */
4810: static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4811: int nAddSig;
4812: int nAddFrac;
4813: if( p==0 ) return;
4814: nAddFrac = nFrac - p->nFrac;
4815: nAddSig = (nDigit - p->nDigit) - nAddFrac;
4816: if( nAddFrac==0 && nAddSig==0 ) return;
4817: p->a = sqlite3_realloc64(p->a, nDigit+1);
4818: if( p->a==0 ){
4819: p->oom = 1;
4820: return;
4821: }
4822: if( nAddSig ){
4823: memmove(p->a+nAddSig, p->a, p->nDigit);
4824: memset(p->a, 0, nAddSig);
4825: p->nDigit += nAddSig;
4826: }
4827: if( nAddFrac ){
4828: memset(p->a+p->nDigit, 0, nAddFrac);
4829: p->nDigit += nAddFrac;
4830: p->nFrac += nAddFrac;
4831: }
4832: }
4833:
4834: /*
4835: ** Add the value pB into pA.
4836: **
4837: ** Both pA and pB might become denormalized by this routine.
4838: */
4839: static void decimal_add(Decimal *pA, Decimal *pB){
4840: int nSig, nFrac, nDigit;
4841: int i, rc;
4842: if( pA==0 ){
4843: return;
4844: }
4845: if( pA->oom || pB==0 || pB->oom ){
4846: pA->oom = 1;
4847: return;
4848: }
4849: if( pA->isNull || pB->isNull ){
4850: pA->isNull = 1;
4851: return;
4852: }
4853: nSig = pA->nDigit - pA->nFrac;
4854: if( nSig && pA->a[0]==0 ) nSig--;
4855: if( nSig<pB->nDigit-pB->nFrac ){
4856: nSig = pB->nDigit - pB->nFrac;
4857: }
4858: nFrac = pA->nFrac;
4859: if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4860: nDigit = nSig + nFrac + 1;
4861: decimal_expand(pA, nDigit, nFrac);
4862: decimal_expand(pB, nDigit, nFrac);
4863: if( pA->oom || pB->oom ){
4864: pA->oom = 1;
4865: }else{
4866: if( pA->sign==pB->sign ){
4867: int carry = 0;
4868: for(i=nDigit-1; i>=0; i--){
4869: int x = pA->a[i] + pB->a[i] + carry;
4870: if( x>=10 ){
4871: carry = 1;
4872: pA->a[i] = x - 10;
4873: }else{
4874: carry = 0;
4875: pA->a[i] = x;
4876: }
4877: }
4878: }else{
4879: signed char *aA, *aB;
4880: int borrow = 0;
4881: rc = memcmp(pA->a, pB->a, nDigit);
4882: if( rc<0 ){
4883: aA = pB->a;
4884: aB = pA->a;
4885: pA->sign = !pA->sign;
4886: }else{
4887: aA = pA->a;
4888: aB = pB->a;
4889: }
4890: for(i=nDigit-1; i>=0; i--){
4891: int x = aA[i] - aB[i] - borrow;
4892: if( x<0 ){
4893: pA->a[i] = x+10;
4894: borrow = 1;
4895: }else{
4896: pA->a[i] = x;
4897: borrow = 0;
4898: }
4899: }
4900: }
4901: }
4902: }
4903:
4904: /*
4905: ** Compare text in decimal order.
4906: */
4907: static int decimalCollFunc(
4908: void *notUsed,
4909: int nKey1, const void *pKey1,
4910: int nKey2, const void *pKey2
4911: ){
4912: const unsigned char *zA = (const unsigned char*)pKey1;
4913: const unsigned char *zB = (const unsigned char*)pKey2;
4914: Decimal *pA = decimal_new(0, 0, nKey1, zA);
4915: Decimal *pB = decimal_new(0, 0, nKey2, zB);
4916: int rc;
4917: UNUSED_PARAMETER(notUsed);
4918: if( pA==0 || pB==0 ){
4919: rc = 0;
4920: }else{
4921: rc = decimal_cmp(pA, pB);
4922: }
4923: decimal_free(pA);
4924: decimal_free(pB);
4925: return rc;
4926: }
4927:
4928:
4929: /*
4930: ** SQL Function: decimal_add(X, Y)
4931: ** decimal_sub(X, Y)
4932: **
4933: ** Return the sum or difference of X and Y.
4934: */
4935: static void decimalAddFunc(
4936: sqlite3_context *context,
4937: int argc,
4938: sqlite3_value **argv
4939: ){
4940: Decimal *pA = decimal_new(context, argv[0], 0, 0);
4941: Decimal *pB = decimal_new(context, argv[1], 0, 0);
4942: UNUSED_PARAMETER(argc);
4943: decimal_add(pA, pB);
4944: decimal_result(context, pA);
4945: decimal_free(pA);
4946: decimal_free(pB);
4947: }
4948: static void decimalSubFunc(
4949: sqlite3_context *context,
4950: int argc,
4951: sqlite3_value **argv
4952: ){
4953: Decimal *pA = decimal_new(context, argv[0], 0, 0);
4954: Decimal *pB = decimal_new(context, argv[1], 0, 0);
4955: UNUSED_PARAMETER(argc);
4956: if( pB==0 ) return;
4957: pB->sign = !pB->sign;
4958: decimal_add(pA, pB);
4959: decimal_result(context, pA);
4960: decimal_free(pA);
4961: decimal_free(pB);
4962: }
4963:
4964: /* Aggregate funcion: decimal_sum(X)
4965: **
4966: ** Works like sum() except that it uses decimal arithmetic for unlimited
4967: ** precision.
4968: */
4969: static void decimalSumStep(
4970: sqlite3_context *context,
4971: int argc,
4972: sqlite3_value **argv
4973: ){
4974: Decimal *p;
4975: Decimal *pArg;
4976: UNUSED_PARAMETER(argc);
4977: p = sqlite3_aggregate_context(context, sizeof(*p));
4978: if( p==0 ) return;
4979: if( !p->isInit ){
4980: p->isInit = 1;
4981: p->a = sqlite3_malloc(2);
4982: if( p->a==0 ){
4983: p->oom = 1;
4984: }else{
4985: p->a[0] = 0;
4986: }
4987: p->nDigit = 1;
4988: p->nFrac = 0;
4989: }
4990: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4991: pArg = decimal_new(context, argv[0], 0, 0);
4992: decimal_add(p, pArg);
4993: decimal_free(pArg);
4994: }
4995: static void decimalSumInverse(
4996: sqlite3_context *context,
4997: int argc,
4998: sqlite3_value **argv
4999: ){
5000: Decimal *p;
5001: Decimal *pArg;
5002: UNUSED_PARAMETER(argc);
5003: p = sqlite3_aggregate_context(context, sizeof(*p));
5004: if( p==0 ) return;
5005: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5006: pArg = decimal_new(context, argv[0], 0, 0);
5007: if( pArg ) pArg->sign = !pArg->sign;
5008: decimal_add(p, pArg);
5009: decimal_free(pArg);
5010: }
5011: static void decimalSumValue(sqlite3_context *context){
5012: Decimal *p = sqlite3_aggregate_context(context, 0);
5013: if( p==0 ) return;
5014: decimal_result(context, p);
5015: }
5016: static void decimalSumFinalize(sqlite3_context *context){
5017: Decimal *p = sqlite3_aggregate_context(context, 0);
5018: if( p==0 ) return;
5019: decimal_result(context, p);
5020: decimal_clear(p);
5021: }
5022:
5023: /*
5024: ** SQL Function: decimal_mul(X, Y)
5025: **
5026: ** Return the product of X and Y.
5027: **
5028: ** All significant digits after the decimal point are retained.
5029: ** Trailing zeros after the decimal point are omitted as long as
5030: ** the number of digits after the decimal point is no less than
5031: ** either the number of digits in either input.
5032: */
5033: static void decimalMulFunc(
5034: sqlite3_context *context,
5035: int argc,
5036: sqlite3_value **argv
5037: ){
5038: Decimal *pA = decimal_new(context, argv[0], 0, 0);
5039: Decimal *pB = decimal_new(context, argv[1], 0, 0);
5040: signed char *acc = 0;
5041: int i, j, k;
5042: int minFrac;
5043: UNUSED_PARAMETER(argc);
5044: if( pA==0 || pA->oom || pA->isNull
5045: || pB==0 || pB->oom || pB->isNull
5046: ){
5047: goto mul_end;
5048: }
5049: acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5050: if( acc==0 ){
5051: sqlite3_result_error_nomem(context);
5052: goto mul_end;
5053: }
5054: memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5055: minFrac = pA->nFrac;
5056: if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5057: for(i=pA->nDigit-1; i>=0; i--){
5058: signed char f = pA->a[i];
5059: int carry = 0, x;
5060: for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5061: x = acc[k] + f*pB->a[j] + carry;
5062: acc[k] = x%10;
5063: carry = x/10;
5064: }
5065: x = acc[k] + carry;
5066: acc[k] = x%10;
5067: acc[k-1] += x/10;
5068: }
5069: sqlite3_free(pA->a);
5070: pA->a = acc;
5071: acc = 0;
5072: pA->nDigit += pB->nDigit + 2;
5073: pA->nFrac += pB->nFrac;
5074: pA->sign ^= pB->sign;
5075: while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5076: pA->nFrac--;
5077: pA->nDigit--;
5078: }
5079: decimal_result(context, pA);
5080:
5081: mul_end:
5082: sqlite3_free(acc);
5083: decimal_free(pA);
5084: decimal_free(pB);
5085: }
5086:
5087: #ifdef _WIN32
5088:
5089: #endif
5090: int sqlite3_decimal_init(
5091: sqlite3 *db,
5092: char **pzErrMsg,
5093: const sqlite3_api_routines *pApi
5094: ){
5095: int rc = SQLITE_OK;
5096: static const struct {
5097: const char *zFuncName;
5098: int nArg;
5099: void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5100: } aFunc[] = {
5101: { "decimal", 1, decimalFunc },
5102: { "decimal_cmp", 2, decimalCmpFunc },
5103: { "decimal_add", 2, decimalAddFunc },
5104: { "decimal_sub", 2, decimalSubFunc },
5105: { "decimal_mul", 2, decimalMulFunc },
5106: };
5107: unsigned int i;
5108: (void)pzErrMsg; /* Unused parameter */
5109:
5110: SQLITE_EXTENSION_INIT2(pApi);
5111:
5112: for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5113: rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5114: SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5115: 0, aFunc[i].xFunc, 0, 0);
5116: }
5117: if( rc==SQLITE_OK ){
5118: rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5119: SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5120: decimalSumStep, decimalSumFinalize,
5121: decimalSumValue, decimalSumInverse, 0);
5122: }
5123: if( rc==SQLITE_OK ){
5124: rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5125: 0, decimalCollFunc);
5126: }
5127: return rc;
5128: }
5129:
5130: /************************* End ../ext/misc/decimal.c ********************/
5131: /************************* Begin ../ext/misc/ieee754.c ******************/
5132: /*
5133: ** 2013-04-17
5134: **
5135: ** The author disclaims copyright to this source code. In place of
5136: ** a legal notice, here is a blessing:
5137: **
5138: ** May you do good and not evil.
5139: ** May you find forgiveness for yourself and forgive others.
5140: ** May you share freely, never taking more than you give.
5141: **
5142: ******************************************************************************
5143: **
5144: ** This SQLite extension implements functions for the exact display
5145: ** and input of IEEE754 Binary64 floating-point numbers.
5146: **
5147: ** ieee754(X)
5148: ** ieee754(Y,Z)
5149: **
5150: ** In the first form, the value X should be a floating-point number.
5151: ** The function will return a string of the form 'ieee754(Y,Z)' where
5152: ** Y and Z are integers such that X==Y*pow(2,Z).
5153: **
5154: ** In the second form, Y and Z are integers which are the mantissa and
5155: ** base-2 exponent of a new floating point number. The function returns
5156: ** a floating-point value equal to Y*pow(2,Z).
5157: **
5158: ** Examples:
5159: **
5160: ** ieee754(2.0) -> 'ieee754(2,0)'
5161: ** ieee754(45.25) -> 'ieee754(181,-2)'
5162: ** ieee754(2, 0) -> 2.0
5163: ** ieee754(181, -2) -> 45.25
5164: **
5165: ** Two additional functions break apart the one-argument ieee754()
5166: ** result into separate integer values:
5167: **
5168: ** ieee754_mantissa(45.25) -> 181
5169: ** ieee754_exponent(45.25) -> -2
5170: **
5171: ** These functions convert binary64 numbers into blobs and back again.
5172: **
5173: ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5174: ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5175: **
5176: ** In all single-argument functions, if the argument is an 8-byte blob
5177: ** then that blob is interpreted as a big-endian binary64 value.
5178: **
5179: **
5180: ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5181: ** -----------------------------------------------
5182: **
5183: ** This extension in combination with the separate 'decimal' extension
5184: ** can be used to compute the exact decimal representation of binary64
5185: ** values. To begin, first compute a table of exponent values:
5186: **
5187: ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5188: ** WITH RECURSIVE c(x,v) AS (
5189: ** VALUES(0,'1')
5190: ** UNION ALL
5191: ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5192: ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5193: ** WITH RECURSIVE c(x,v) AS (
5194: ** VALUES(-1,'0.5')
5195: ** UNION ALL
5196: ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5197: ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5198: **
5199: ** Then, to compute the exact decimal representation of a floating
5200: ** point value (the value 47.49 is used in the example) do:
5201: **
5202: ** WITH c(n) AS (VALUES(47.49))
5203: ** ---------------^^^^^---- Replace with whatever you want
5204: ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5205: ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5206: **
5207: ** Here is a query to show various boundry values for the binary64
5208: ** number format:
5209: **
5210: ** WITH c(name,bin) AS (VALUES
5211: ** ('minimum positive value', x'0000000000000001'),
5212: ** ('maximum subnormal value', x'000fffffffffffff'),
5213: ** ('mininum positive nornal value', x'0010000000000000'),
5214: ** ('maximum value', x'7fefffffffffffff'))
5215: ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5216: ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5217: **
5218: */
5219: /* #include "sqlite3ext.h" */
5220: SQLITE_EXTENSION_INIT1
5221: #include <assert.h>
5222: #include <string.h>
5223:
5224: /* Mark a function parameter as unused, to suppress nuisance compiler
5225: ** warnings. */
5226: #ifndef UNUSED_PARAMETER
5227: # define UNUSED_PARAMETER(X) (void)(X)
5228: #endif
5229:
5230: /*
5231: ** Implementation of the ieee754() function
5232: */
5233: static void ieee754func(
5234: sqlite3_context *context,
5235: int argc,
5236: sqlite3_value **argv
5237: ){
5238: if( argc==1 ){
5239: sqlite3_int64 m, a;
5240: double r;
5241: int e;
5242: int isNeg;
5243: char zResult[100];
5244: assert( sizeof(m)==sizeof(r) );
5245: if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5246: && sqlite3_value_bytes(argv[0])==sizeof(r)
5247: ){
5248: const unsigned char *x = sqlite3_value_blob(argv[0]);
5249: unsigned int i;
5250: sqlite3_uint64 v = 0;
5251: for(i=0; i<sizeof(r); i++){
5252: v = (v<<8) | x[i];
5253: }
5254: memcpy(&r, &v, sizeof(r));
5255: }else{
5256: r = sqlite3_value_double(argv[0]);
5257: }
5258: if( r<0.0 ){
5259: isNeg = 1;
5260: r = -r;
5261: }else{
5262: isNeg = 0;
5263: }
5264: memcpy(&a,&r,sizeof(a));
5265: if( a==0 ){
5266: e = 0;
5267: m = 0;
5268: }else{
5269: e = a>>52;
5270: m = a & ((((sqlite3_int64)1)<<52)-1);
5271: if( e==0 ){
5272: m <<= 1;
5273: }else{
5274: m |= ((sqlite3_int64)1)<<52;
5275: }
5276: while( e<1075 && m>0 && (m&1)==0 ){
5277: m >>= 1;
5278: e++;
5279: }
5280: if( isNeg ) m = -m;
5281: }
5282: switch( *(int*)sqlite3_user_data(context) ){
5283: case 0:
5284: sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5285: m, e-1075);
5286: sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5287: break;
5288: case 1:
5289: sqlite3_result_int64(context, m);
5290: break;
5291: case 2:
5292: sqlite3_result_int(context, e-1075);
5293: break;
5294: }
5295: }else{
5296: sqlite3_int64 m, e, a;
5297: double r;
5298: int isNeg = 0;
5299: m = sqlite3_value_int64(argv[0]);
5300: e = sqlite3_value_int64(argv[1]);
5301:
5302: /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */
5303: if( e>10000 ){
5304: e = 10000;
5305: }else if( e<-10000 ){
5306: e = -10000;
5307: }
5308:
5309: if( m<0 ){
5310: isNeg = 1;
5311: m = -m;
5312: if( m<0 ) return;
5313: }else if( m==0 && e>-1000 && e<1000 ){
5314: sqlite3_result_double(context, 0.0);
5315: return;
5316: }
5317: while( (m>>32)&0xffe00000 ){
5318: m >>= 1;
5319: e++;
5320: }
5321: while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5322: m <<= 1;
5323: e--;
5324: }
5325: e += 1075;
5326: if( e<=0 ){
5327: /* Subnormal */
5328: m >>= 1-e;
5329: e = 0;
5330: }else if( e>0x7ff ){
5331: e = 0x7ff;
5332: }
5333: a = m & ((((sqlite3_int64)1)<<52)-1);
5334: a |= e<<52;
5335: if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5336: memcpy(&r, &a, sizeof(r));
5337: sqlite3_result_double(context, r);
5338: }
5339: }
5340:
5341: /*
5342: ** Functions to convert between blobs and floats.
5343: */
5344: static void ieee754func_from_blob(
5345: sqlite3_context *context,
5346: int argc,
5347: sqlite3_value **argv
5348: ){
5349: UNUSED_PARAMETER(argc);
5350: if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5351: && sqlite3_value_bytes(argv[0])==sizeof(double)
5352: ){
5353: double r;
5354: const unsigned char *x = sqlite3_value_blob(argv[0]);
5355: unsigned int i;
5356: sqlite3_uint64 v = 0;
5357: for(i=0; i<sizeof(r); i++){
5358: v = (v<<8) | x[i];
5359: }
5360: memcpy(&r, &v, sizeof(r));
5361: sqlite3_result_double(context, r);
5362: }
5363: }
5364: static void ieee754func_to_blob(
5365: sqlite3_context *context,
5366: int argc,
5367: sqlite3_value **argv
5368: ){
5369: UNUSED_PARAMETER(argc);
5370: if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5371: || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5372: ){
5373: double r = sqlite3_value_double(argv[0]);
5374: sqlite3_uint64 v;
5375: unsigned char a[sizeof(r)];
5376: unsigned int i;
5377: memcpy(&v, &r, sizeof(r));
5378: for(i=1; i<=sizeof(r); i++){
5379: a[sizeof(r)-i] = v&0xff;
5380: v >>= 8;
5381: }
5382: sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5383: }
5384: }
5385:
5386:
5387: #ifdef _WIN32
5388:
5389: #endif
5390: int sqlite3_ieee_init(
5391: sqlite3 *db,
5392: char **pzErrMsg,
5393: const sqlite3_api_routines *pApi
5394: ){
5395: static const struct {
5396: char *zFName;
5397: int nArg;
5398: int iAux;
5399: void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5400: } aFunc[] = {
5401: { "ieee754", 1, 0, ieee754func },
5402: { "ieee754", 2, 0, ieee754func },
5403: { "ieee754_mantissa", 1, 1, ieee754func },
5404: { "ieee754_exponent", 1, 2, ieee754func },
5405: { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5406: { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5407:
5408: };
5409: unsigned int i;
5410: int rc = SQLITE_OK;
5411: SQLITE_EXTENSION_INIT2(pApi);
5412: (void)pzErrMsg; /* Unused parameter */
5413: for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5414: rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5415: SQLITE_UTF8|SQLITE_INNOCUOUS,
5416: (void*)&aFunc[i].iAux,
5417: aFunc[i].xFunc, 0, 0);
5418: }
5419: return rc;
5420: }
5421:
5422: /************************* End ../ext/misc/ieee754.c ********************/
5423: /************************* Begin ../ext/misc/series.c ******************/
5424: /*
5425: ** 2015-08-18
5426: **
5427: ** The author disclaims copyright to this source code. In place of
5428: ** a legal notice, here is a blessing:
5429: **
5430: ** May you do good and not evil.
5431: ** May you find forgiveness for yourself and forgive others.
5432: ** May you share freely, never taking more than you give.
5433: **
5434: *************************************************************************
5435: **
5436: ** This file demonstrates how to create a table-valued-function using
5437: ** a virtual table. This demo implements the generate_series() function
5438: ** which gives similar results to the eponymous function in PostgreSQL.
5439: ** Examples:
5440: **
5441: ** SELECT * FROM generate_series(0,100,5);
5442: **
5443: ** The query above returns integers from 0 through 100 counting by steps
5444: ** of 5.
5445: **
5446: ** SELECT * FROM generate_series(0,100);
5447: **
5448: ** Integers from 0 through 100 with a step size of 1.
5449: **
5450: ** SELECT * FROM generate_series(20) LIMIT 10;
5451: **
5452: ** Integers 20 through 29.
5453: **
5454: ** HOW IT WORKS
5455: **
5456: ** The generate_series "function" is really a virtual table with the
5457: ** following schema:
5458: **
5459: ** CREATE TABLE generate_series(
5460: ** value,
5461: ** start HIDDEN,
5462: ** stop HIDDEN,
5463: ** step HIDDEN
5464: ** );
5465: **
5466: ** Function arguments in queries against this virtual table are translated
5467: ** into equality constraints against successive hidden columns. In other
5468: ** words, the following pairs of queries are equivalent to each other:
5469: **
5470: ** SELECT * FROM generate_series(0,100,5);
5471: ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5472: **
5473: ** SELECT * FROM generate_series(0,100);
5474: ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5475: **
5476: ** SELECT * FROM generate_series(20) LIMIT 10;
5477: ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5478: **
5479: ** The generate_series virtual table implementation leaves the xCreate method
5480: ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5481: ** TABLE command with "generate_series" as the USING argument. Instead, there
5482: ** is a single generate_series virtual table that is always available without
5483: ** having to be created first.
5484: **
5485: ** The xBestIndex method looks for equality constraints against the hidden
5486: ** start, stop, and step columns, and if present, it uses those constraints
5487: ** to bound the sequence of generated values. If the equality constraints
5488: ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5489: ** xBestIndex returns a small cost when both start and stop are available,
5490: ** and a very large cost if either start or stop are unavailable. This
5491: ** encourages the query planner to order joins such that the bounds of the
5492: ** series are well-defined.
5493: */
5494: /* #include "sqlite3ext.h" */
5495: SQLITE_EXTENSION_INIT1
5496: #include <assert.h>
5497: #include <string.h>
5498:
5499: #ifndef SQLITE_OMIT_VIRTUALTABLE
5500:
5501:
5502: /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5503: ** serve as the underlying representation of a cursor that scans
5504: ** over rows of the result
5505: */
5506: typedef struct series_cursor series_cursor;
5507: struct series_cursor {
5508: sqlite3_vtab_cursor base; /* Base class - must be first */
5509: int isDesc; /* True to count down rather than up */
5510: sqlite3_int64 iRowid; /* The rowid */
5511: sqlite3_int64 iValue; /* Current value ("value") */
5512: sqlite3_int64 mnValue; /* Mimimum value ("start") */
5513: sqlite3_int64 mxValue; /* Maximum value ("stop") */
5514: sqlite3_int64 iStep; /* Increment ("step") */
5515: };
5516:
5517: /*
5518: ** The seriesConnect() method is invoked to create a new
5519: ** series_vtab that describes the generate_series virtual table.
5520: **
5521: ** Think of this routine as the constructor for series_vtab objects.
5522: **
5523: ** All this routine needs to do is:
5524: **
5525: ** (1) Allocate the series_vtab object and initialize all fields.
5526: **
5527: ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5528: ** result set of queries against generate_series will look like.
5529: */
5530: static int seriesConnect(
5531: sqlite3 *db,
5532: void *pUnused,
5533: int argcUnused, const char *const*argvUnused,
5534: sqlite3_vtab **ppVtab,
5535: char **pzErrUnused
5536: ){
5537: sqlite3_vtab *pNew;
5538: int rc;
5539:
5540: /* Column numbers */
5541: #define SERIES_COLUMN_VALUE 0
5542: #define SERIES_COLUMN_START 1
5543: #define SERIES_COLUMN_STOP 2
5544: #define SERIES_COLUMN_STEP 3
5545:
5546: (void)pUnused;
5547: (void)argcUnused;
5548: (void)argvUnused;
5549: (void)pzErrUnused;
5550: rc = sqlite3_declare_vtab(db,
5551: "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5552: if( rc==SQLITE_OK ){
5553: pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5554: if( pNew==0 ) return SQLITE_NOMEM;
5555: memset(pNew, 0, sizeof(*pNew));
5556: sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5557: }
5558: return rc;
5559: }
5560:
5561: /*
5562: ** This method is the destructor for series_cursor objects.
5563: */
5564: static int seriesDisconnect(sqlite3_vtab *pVtab){
5565: sqlite3_free(pVtab);
5566: return SQLITE_OK;
5567: }
5568:
5569: /*
5570: ** Constructor for a new series_cursor object.
5571: */
5572: static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5573: series_cursor *pCur;
5574: (void)pUnused;
5575: pCur = sqlite3_malloc( sizeof(*pCur) );
5576: if( pCur==0 ) return SQLITE_NOMEM;
5577: memset(pCur, 0, sizeof(*pCur));
5578: *ppCursor = &pCur->base;
5579: return SQLITE_OK;
5580: }
5581:
5582: /*
5583: ** Destructor for a series_cursor.
5584: */
5585: static int seriesClose(sqlite3_vtab_cursor *cur){
5586: sqlite3_free(cur);
5587: return SQLITE_OK;
5588: }
5589:
5590:
5591: /*
5592: ** Advance a series_cursor to its next row of output.
5593: */
5594: static int seriesNext(sqlite3_vtab_cursor *cur){
5595: series_cursor *pCur = (series_cursor*)cur;
5596: if( pCur->isDesc ){
5597: pCur->iValue -= pCur->iStep;
5598: }else{
5599: pCur->iValue += pCur->iStep;
5600: }
5601: pCur->iRowid++;
5602: return SQLITE_OK;
5603: }
5604:
5605: /*
5606: ** Return values of columns for the row at which the series_cursor
5607: ** is currently pointing.
5608: */
5609: static int seriesColumn(
5610: sqlite3_vtab_cursor *cur, /* The cursor */
5611: sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5612: int i /* Which column to return */
5613: ){
5614: series_cursor *pCur = (series_cursor*)cur;
5615: sqlite3_int64 x = 0;
5616: switch( i ){
5617: case SERIES_COLUMN_START: x = pCur->mnValue; break;
5618: case SERIES_COLUMN_STOP: x = pCur->mxValue; break;
5619: case SERIES_COLUMN_STEP: x = pCur->iStep; break;
5620: default: x = pCur->iValue; break;
5621: }
5622: sqlite3_result_int64(ctx, x);
5623: return SQLITE_OK;
5624: }
5625:
5626: /*
5627: ** Return the rowid for the current row. In this implementation, the
5628: ** first row returned is assigned rowid value 1, and each subsequent
5629: ** row a value 1 more than that of the previous.
5630: */
5631: static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5632: series_cursor *pCur = (series_cursor*)cur;
5633: *pRowid = pCur->iRowid;
5634: return SQLITE_OK;
5635: }
5636:
5637: /*
5638: ** Return TRUE if the cursor has been moved off of the last
5639: ** row of output.
5640: */
5641: static int seriesEof(sqlite3_vtab_cursor *cur){
5642: series_cursor *pCur = (series_cursor*)cur;
5643: if( pCur->isDesc ){
5644: return pCur->iValue < pCur->mnValue;
5645: }else{
5646: return pCur->iValue > pCur->mxValue;
5647: }
5648: }
5649:
5650: /* True to cause run-time checking of the start=, stop=, and/or step=
5651: ** parameters. The only reason to do this is for testing the
5652: ** constraint checking logic for virtual tables in the SQLite core.
5653: */
5654: #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5655: # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5656: #endif
5657:
5658: /*
5659: ** This method is called to "rewind" the series_cursor object back
5660: ** to the first row of output. This method is always called at least
5661: ** once prior to any call to seriesColumn() or seriesRowid() or
5662: ** seriesEof().
5663: **
5664: ** The query plan selected by seriesBestIndex is passed in the idxNum
5665: ** parameter. (idxStr is not used in this implementation.) idxNum
5666: ** is a bitmask showing which constraints are available:
5667: **
5668: ** 1: start=VALUE
5669: ** 2: stop=VALUE
5670: ** 4: step=VALUE
5671: **
5672: ** Also, if bit 8 is set, that means that the series should be output
5673: ** in descending order rather than in ascending order. If bit 16 is
5674: ** set, then output must appear in ascending order.
5675: **
5676: ** This routine should initialize the cursor and position it so that it
5677: ** is pointing at the first row, or pointing off the end of the table
5678: ** (so that seriesEof() will return true) if the table is empty.
5679: */
5680: static int seriesFilter(
5681: sqlite3_vtab_cursor *pVtabCursor,
5682: int idxNum, const char *idxStrUnused,
5683: int argc, sqlite3_value **argv
5684: ){
5685: series_cursor *pCur = (series_cursor *)pVtabCursor;
5686: int i = 0;
5687: (void)idxStrUnused;
5688: if( idxNum & 1 ){
5689: pCur->mnValue = sqlite3_value_int64(argv[i++]);
5690: }else{
5691: pCur->mnValue = 0;
5692: }
5693: if( idxNum & 2 ){
5694: pCur->mxValue = sqlite3_value_int64(argv[i++]);
5695: }else{
5696: pCur->mxValue = 0xffffffff;
5697: }
5698: if( idxNum & 4 ){
5699: pCur->iStep = sqlite3_value_int64(argv[i++]);
5700: if( pCur->iStep==0 ){
5701: pCur->iStep = 1;
5702: }else if( pCur->iStep<0 ){
5703: pCur->iStep = -pCur->iStep;
5704: if( (idxNum & 16)==0 ) idxNum |= 8;
5705: }
5706: }else{
5707: pCur->iStep = 1;
5708: }
5709: for(i=0; i<argc; i++){
5710: if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5711: /* If any of the constraints have a NULL value, then return no rows.
5712: ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5713: pCur->mnValue = 1;
5714: pCur->mxValue = 0;
5715: break;
5716: }
5717: }
5718: if( idxNum & 8 ){
5719: pCur->isDesc = 1;
5720: pCur->iValue = pCur->mxValue;
5721: if( pCur->iStep>0 ){
5722: pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5723: }
5724: }else{
5725: pCur->isDesc = 0;
5726: pCur->iValue = pCur->mnValue;
5727: }
5728: pCur->iRowid = 1;
5729: return SQLITE_OK;
5730: }
5731:
5732: /*
5733: ** SQLite will invoke this method one or more times while planning a query
5734: ** that uses the generate_series virtual table. This routine needs to create
5735: ** a query plan for each invocation and compute an estimated cost for that
5736: ** plan.
5737: **
5738: ** In this implementation idxNum is used to represent the
5739: ** query plan. idxStr is unused.
5740: **
5741: ** The query plan is represented by bits in idxNum:
5742: **
5743: ** (1) start = $value -- constraint exists
5744: ** (2) stop = $value -- constraint exists
5745: ** (4) step = $value -- constraint exists
5746: ** (8) output in descending order
5747: */
5748: static int seriesBestIndex(
5749: sqlite3_vtab *tabUnused,
5750: sqlite3_index_info *pIdxInfo
5751: ){
5752: int i, j; /* Loop over constraints */
5753: int idxNum = 0; /* The query plan bitmask */
5754: int unusableMask = 0; /* Mask of unusable constraints */
5755: int nArg = 0; /* Number of arguments that seriesFilter() expects */
5756: int aIdx[3]; /* Constraints on start, stop, and step */
5757: const struct sqlite3_index_constraint *pConstraint;
5758:
5759: /* This implementation assumes that the start, stop, and step columns
5760: ** are the last three columns in the virtual table. */
5761: assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5762: assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5763: (void)tabUnused;
5764: aIdx[0] = aIdx[1] = aIdx[2] = -1;
5765: pConstraint = pIdxInfo->aConstraint;
5766: for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5767: int iCol; /* 0 for start, 1 for stop, 2 for step */
5768: int iMask; /* bitmask for those column */
5769: if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5770: iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5771: assert( iCol>=0 && iCol<=2 );
5772: iMask = 1 << iCol;
5773: if( pConstraint->usable==0 ){
5774: unusableMask |= iMask;
5775: continue;
5776: }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5777: idxNum |= iMask;
5778: aIdx[iCol] = i;
5779: }
5780: }
5781: for(i=0; i<3; i++){
5782: if( (j = aIdx[i])>=0 ){
5783: pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5784: pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5785: }
5786: }
5787: if( (unusableMask & ~idxNum)!=0 ){
5788: /* The start, stop, and step columns are inputs. Therefore if there
5789: ** are unusable constraints on any of start, stop, or step then
5790: ** this plan is unusable */
5791: return SQLITE_CONSTRAINT;
5792: }
5793: if( (idxNum & 3)==3 ){
5794: /* Both start= and stop= boundaries are available. This is the
5795: ** the preferred case */
5796: pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5797: pIdxInfo->estimatedRows = 1000;
5798: if( pIdxInfo->nOrderBy==1 ){
5799: if( pIdxInfo->aOrderBy[0].desc ){
5800: idxNum |= 8;
5801: }else{
5802: idxNum |= 16;
5803: }
5804: pIdxInfo->orderByConsumed = 1;
5805: }
5806: }else{
5807: /* If either boundary is missing, we have to generate a huge span
5808: ** of numbers. Make this case very expensive so that the query
5809: ** planner will work hard to avoid it. */
5810: pIdxInfo->estimatedRows = 2147483647;
5811: }
5812: pIdxInfo->idxNum = idxNum;
5813: return SQLITE_OK;
5814: }
5815:
5816: /*
5817: ** This following structure defines all the methods for the
5818: ** generate_series virtual table.
5819: */
5820: static sqlite3_module seriesModule = {
5821: 0, /* iVersion */
5822: 0, /* xCreate */
5823: seriesConnect, /* xConnect */
5824: seriesBestIndex, /* xBestIndex */
5825: seriesDisconnect, /* xDisconnect */
5826: 0, /* xDestroy */
5827: seriesOpen, /* xOpen - open a cursor */
5828: seriesClose, /* xClose - close a cursor */
5829: seriesFilter, /* xFilter - configure scan constraints */
5830: seriesNext, /* xNext - advance a cursor */
5831: seriesEof, /* xEof - check for end of scan */
5832: seriesColumn, /* xColumn - read data */
5833: seriesRowid, /* xRowid - read data */
5834: 0, /* xUpdate */
5835: 0, /* xBegin */
5836: 0, /* xSync */
5837: 0, /* xCommit */
5838: 0, /* xRollback */
5839: 0, /* xFindMethod */
5840: 0, /* xRename */
5841: 0, /* xSavepoint */
5842: 0, /* xRelease */
5843: 0, /* xRollbackTo */
5844: 0 /* xShadowName */
5845: };
5846:
5847: #endif /* SQLITE_OMIT_VIRTUALTABLE */
5848:
5849: #ifdef _WIN32
5850:
5851: #endif
5852: int sqlite3_series_init(
5853: sqlite3 *db,
5854: char **pzErrMsg,
5855: const sqlite3_api_routines *pApi
5856: ){
5857: int rc = SQLITE_OK;
5858: SQLITE_EXTENSION_INIT2(pApi);
5859: #ifndef SQLITE_OMIT_VIRTUALTABLE
5860: if( sqlite3_libversion_number()<3008012 ){
5861: *pzErrMsg = sqlite3_mprintf(
5862: "generate_series() requires SQLite 3.8.12 or later");
5863: return SQLITE_ERROR;
5864: }
5865: rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5866: #endif
5867: return rc;
5868: }
5869:
5870: /************************* End ../ext/misc/series.c ********************/
5871: #ifdef SQLITE_HAVE_ZLIB
5872: /************************* Begin ../ext/misc/zipfile.c ******************/
5873: /*
5874: ** 2017-12-26
5875: **
5876: ** The author disclaims copyright to this source code. In place of
5877: ** a legal notice, here is a blessing:
5878: **
5879: ** May you do good and not evil.
5880: ** May you find forgiveness for yourself and forgive others.
5881: ** May you share freely, never taking more than you give.
5882: **
5883: ******************************************************************************
5884: **
5885: ** This file implements a virtual table for reading and writing ZIP archive
5886: ** files.
5887: **
5888: ** Usage example:
5889: **
5890: ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
5891: **
5892: ** Current limitations:
5893: **
5894: ** * No support for encryption
5895: ** * No support for ZIP archives spanning multiple files
5896: ** * No support for zip64 extensions
5897: ** * Only the "inflate/deflate" (zlib) compression method is supported
5898: */
5899: /* #include "sqlite3ext.h" */
5900: SQLITE_EXTENSION_INIT1
5901: #include <stdio.h>
5902: #include <string.h>
5903: #include <assert.h>
5904:
5905: #include <zlib.h>
5906:
5907: #ifndef SQLITE_OMIT_VIRTUALTABLE
5908:
5909: #ifndef SQLITE_AMALGAMATION
5910:
5911: /* typedef sqlite3_int64 i64; */
5912: /* typedef unsigned char u8; */
5913: typedef unsigned short u16;
5914: typedef unsigned long u32;
5915: #define MIN(a,b) ((a)<(b) ? (a) : (b))
5916:
5917: #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
5918: # define ALWAYS(X) (1)
5919: # define NEVER(X) (0)
5920: #elif !defined(NDEBUG)
5921: # define ALWAYS(X) ((X)?1:(assert(0),0))
5922: # define NEVER(X) ((X)?(assert(0),1):0)
5923: #else
5924: # define ALWAYS(X) (X)
5925: # define NEVER(X) (X)
5926: #endif
5927:
5928: #endif /* SQLITE_AMALGAMATION */
5929:
5930: /*
5931: ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
5932: **
5933: ** In some ways it would be better to obtain these values from system
5934: ** header files. But, the dependency is undesirable and (a) these
5935: ** have been stable for decades, (b) the values are part of POSIX and
5936: ** are also made explicit in [man stat], and (c) are part of the
5937: ** file format for zip archives.
5938: */
5939: #ifndef S_IFDIR
5940: # define S_IFDIR 0040000
5941: #endif
5942: #ifndef S_IFREG
5943: # define S_IFREG 0100000
5944: #endif
5945: #ifndef S_IFLNK
5946: # define S_IFLNK 0120000
5947: #endif
5948:
5949: static const char ZIPFILE_SCHEMA[] =
5950: "CREATE TABLE y("
5951: "name PRIMARY KEY," /* 0: Name of file in zip archive */
5952: "mode," /* 1: POSIX mode for file */
5953: "mtime," /* 2: Last modification time (secs since 1970)*/
5954: "sz," /* 3: Size of object */
5955: "rawdata," /* 4: Raw data */
5956: "data," /* 5: Uncompressed data */
5957: "method," /* 6: Compression method (integer) */
5958: "z HIDDEN" /* 7: Name of zip file */
5959: ") WITHOUT ROWID;";
5960:
5961: #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
5962: #define ZIPFILE_BUFFER_SIZE (64*1024)
5963:
5964:
5965: /*
5966: ** Magic numbers used to read and write zip files.
5967: **
5968: ** ZIPFILE_NEWENTRY_MADEBY:
5969: ** Use this value for the "version-made-by" field in new zip file
5970: ** entries. The upper byte indicates "unix", and the lower byte
5971: ** indicates that the zip file matches pkzip specification 3.0.
5972: ** This is what info-zip seems to do.
5973: **
5974: ** ZIPFILE_NEWENTRY_REQUIRED:
5975: ** Value for "version-required-to-extract" field of new entries.
5976: ** Version 2.0 is required to support folders and deflate compression.
5977: **
5978: ** ZIPFILE_NEWENTRY_FLAGS:
5979: ** Value for "general-purpose-bit-flags" field of new entries. Bit
5980: ** 11 means "utf-8 filename and comment".
5981: **
5982: ** ZIPFILE_SIGNATURE_CDS:
5983: ** First 4 bytes of a valid CDS record.
5984: **
5985: ** ZIPFILE_SIGNATURE_LFH:
5986: ** First 4 bytes of a valid LFH record.
5987: **
5988: ** ZIPFILE_SIGNATURE_EOCD
5989: ** First 4 bytes of a valid EOCD record.
5990: */
5991: #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
5992: #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
5993: #define ZIPFILE_NEWENTRY_REQUIRED 20
5994: #define ZIPFILE_NEWENTRY_FLAGS 0x800
5995: #define ZIPFILE_SIGNATURE_CDS 0x02014b50
5996: #define ZIPFILE_SIGNATURE_LFH 0x04034b50
5997: #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
5998:
5999: /*
6000: ** The sizes of the fixed-size part of each of the three main data
6001: ** structures in a zip archive.
6002: */
6003: #define ZIPFILE_LFH_FIXED_SZ 30
6004: #define ZIPFILE_EOCD_FIXED_SZ 22
6005: #define ZIPFILE_CDS_FIXED_SZ 46
6006:
6007: /*
6008: *** 4.3.16 End of central directory record:
6009: ***
6010: *** end of central dir signature 4 bytes (0x06054b50)
6011: *** number of this disk 2 bytes
6012: *** number of the disk with the
6013: *** start of the central directory 2 bytes
6014: *** total number of entries in the
6015: *** central directory on this disk 2 bytes
6016: *** total number of entries in
6017: *** the central directory 2 bytes
6018: *** size of the central directory 4 bytes
6019: *** offset of start of central
6020: *** directory with respect to
6021: *** the starting disk number 4 bytes
6022: *** .ZIP file comment length 2 bytes
6023: *** .ZIP file comment (variable size)
6024: */
6025: typedef struct ZipfileEOCD ZipfileEOCD;
6026: struct ZipfileEOCD {
6027: u16 iDisk;
6028: u16 iFirstDisk;
6029: u16 nEntry;
6030: u16 nEntryTotal;
6031: u32 nSize;
6032: u32 iOffset;
6033: };
6034:
6035: /*
6036: *** 4.3.12 Central directory structure:
6037: ***
6038: *** ...
6039: ***
6040: *** central file header signature 4 bytes (0x02014b50)
6041: *** version made by 2 bytes
6042: *** version needed to extract 2 bytes
6043: *** general purpose bit flag 2 bytes
6044: *** compression method 2 bytes
6045: *** last mod file time 2 bytes
6046: *** last mod file date 2 bytes
6047: *** crc-32 4 bytes
6048: *** compressed size 4 bytes
6049: *** uncompressed size 4 bytes
6050: *** file name length 2 bytes
6051: *** extra field length 2 bytes
6052: *** file comment length 2 bytes
6053: *** disk number start 2 bytes
6054: *** internal file attributes 2 bytes
6055: *** external file attributes 4 bytes
6056: *** relative offset of local header 4 bytes
6057: */
6058: typedef struct ZipfileCDS ZipfileCDS;
6059: struct ZipfileCDS {
6060: u16 iVersionMadeBy;
6061: u16 iVersionExtract;
6062: u16 flags;
6063: u16 iCompression;
6064: u16 mTime;
6065: u16 mDate;
6066: u32 crc32;
6067: u32 szCompressed;
6068: u32 szUncompressed;
6069: u16 nFile;
6070: u16 nExtra;
6071: u16 nComment;
6072: u16 iDiskStart;
6073: u16 iInternalAttr;
6074: u32 iExternalAttr;
6075: u32 iOffset;
6076: char *zFile; /* Filename (sqlite3_malloc()) */
6077: };
6078:
6079: /*
6080: *** 4.3.7 Local file header:
6081: ***
6082: *** local file header signature 4 bytes (0x04034b50)
6083: *** version needed to extract 2 bytes
6084: *** general purpose bit flag 2 bytes
6085: *** compression method 2 bytes
6086: *** last mod file time 2 bytes
6087: *** last mod file date 2 bytes
6088: *** crc-32 4 bytes
6089: *** compressed size 4 bytes
6090: *** uncompressed size 4 bytes
6091: *** file name length 2 bytes
6092: *** extra field length 2 bytes
6093: ***
6094: */
6095: typedef struct ZipfileLFH ZipfileLFH;
6096: struct ZipfileLFH {
6097: u16 iVersionExtract;
6098: u16 flags;
6099: u16 iCompression;
6100: u16 mTime;
6101: u16 mDate;
6102: u32 crc32;
6103: u32 szCompressed;
6104: u32 szUncompressed;
6105: u16 nFile;
6106: u16 nExtra;
6107: };
6108:
6109: typedef struct ZipfileEntry ZipfileEntry;
6110: struct ZipfileEntry {
6111: ZipfileCDS cds; /* Parsed CDS record */
6112: u32 mUnixTime; /* Modification time, in UNIX format */
6113: u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
6114: i64 iDataOff; /* Offset to data in file (if aData==0) */
6115: u8 *aData; /* cds.szCompressed bytes of compressed data */
6116: ZipfileEntry *pNext; /* Next element in in-memory CDS */
6117: };
6118:
6119: /*
6120: ** Cursor type for zipfile tables.
6121: */
6122: typedef struct ZipfileCsr ZipfileCsr;
6123: struct ZipfileCsr {
6124: sqlite3_vtab_cursor base; /* Base class - must be first */
6125: i64 iId; /* Cursor ID */
6126: u8 bEof; /* True when at EOF */
6127: u8 bNoop; /* If next xNext() call is no-op */
6128:
6129: /* Used outside of write transactions */
6130: FILE *pFile; /* Zip file */
6131: i64 iNextOff; /* Offset of next record in central directory */
6132: ZipfileEOCD eocd; /* Parse of central directory record */
6133:
6134: ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
6135: ZipfileEntry *pCurrent; /* Current entry */
6136: ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
6137: };
6138:
6139: typedef struct ZipfileTab ZipfileTab;
6140: struct ZipfileTab {
6141: sqlite3_vtab base; /* Base class - must be first */
6142: char *zFile; /* Zip file this table accesses (may be NULL) */
6143: sqlite3 *db; /* Host database connection */
6144: u8 *aBuffer; /* Temporary buffer used for various tasks */
6145:
6146: ZipfileCsr *pCsrList; /* List of cursors */
6147: i64 iNextCsrid;
6148:
6149: /* The following are used by write transactions only */
6150: ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6151: ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
6152: FILE *pWriteFd; /* File handle open on zip archive */
6153: i64 szCurrent; /* Current size of zip archive */
6154: i64 szOrig; /* Size of archive at start of transaction */
6155: };
6156:
6157: /*
6158: ** Set the error message contained in context ctx to the results of
6159: ** vprintf(zFmt, ...).
6160: */
6161: static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6162: char *zMsg = 0;
6163: va_list ap;
6164: va_start(ap, zFmt);
6165: zMsg = sqlite3_vmprintf(zFmt, ap);
6166: sqlite3_result_error(ctx, zMsg, -1);
6167: sqlite3_free(zMsg);
6168: va_end(ap);
6169: }
6170:
6171: /*
6172: ** If string zIn is quoted, dequote it in place. Otherwise, if the string
6173: ** is not quoted, do nothing.
6174: */
6175: static void zipfileDequote(char *zIn){
6176: char q = zIn[0];
6177: if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6178: int iIn = 1;
6179: int iOut = 0;
6180: if( q=='[' ) q = ']';
6181: while( ALWAYS(zIn[iIn]) ){
6182: char c = zIn[iIn++];
6183: if( c==q && zIn[iIn++]!=q ) break;
6184: zIn[iOut++] = c;
6185: }
6186: zIn[iOut] = '\0';
6187: }
6188: }
6189:
6190: /*
6191: ** Construct a new ZipfileTab virtual table object.
6192: **
6193: ** argv[0] -> module name ("zipfile")
6194: ** argv[1] -> database name
6195: ** argv[2] -> table name
6196: ** argv[...] -> "column name" and other module argument fields.
6197: */
6198: static int zipfileConnect(
6199: sqlite3 *db,
6200: void *pAux,
6201: int argc, const char *const*argv,
6202: sqlite3_vtab **ppVtab,
6203: char **pzErr
6204: ){
6205: int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
6206: int nFile = 0;
6207: const char *zFile = 0;
6208: ZipfileTab *pNew = 0;
6209: int rc;
6210:
6211: /* If the table name is not "zipfile", require that the argument be
6212: ** specified. This stops zipfile tables from being created as:
6213: **
6214: ** CREATE VIRTUAL TABLE zzz USING zipfile();
6215: **
6216: ** It does not prevent:
6217: **
6218: ** CREATE VIRTUAL TABLE zipfile USING zipfile();
6219: */
6220: assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
6221: if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
6222: *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
6223: return SQLITE_ERROR;
6224: }
6225:
6226: if( argc>3 ){
6227: zFile = argv[3];
6228: nFile = (int)strlen(zFile)+1;
6229: }
6230:
6231: rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
6232: if( rc==SQLITE_OK ){
6233: pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
6234: if( pNew==0 ) return SQLITE_NOMEM;
6235: memset(pNew, 0, nByte+nFile);
6236: pNew->db = db;
6237: pNew->aBuffer = (u8*)&pNew[1];
6238: if( zFile ){
6239: pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
6240: memcpy(pNew->zFile, zFile, nFile);
6241: zipfileDequote(pNew->zFile);
6242: }
6243: }
6244: sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
6245: *ppVtab = (sqlite3_vtab*)pNew;
6246: return rc;
6247: }
6248:
6249: /*
6250: ** Free the ZipfileEntry structure indicated by the only argument.
6251: */
6252: static void zipfileEntryFree(ZipfileEntry *p){
6253: if( p ){
6254: sqlite3_free(p->cds.zFile);
6255: sqlite3_free(p);
6256: }
6257: }
6258:
6259: /*
6260: ** Release resources that should be freed at the end of a write
6261: ** transaction.
6262: */
6263: static void zipfileCleanupTransaction(ZipfileTab *pTab){
6264: ZipfileEntry *pEntry;
6265: ZipfileEntry *pNext;
6266:
6267: if( pTab->pWriteFd ){
6268: fclose(pTab->pWriteFd);
6269: pTab->pWriteFd = 0;
6270: }
6271: for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
6272: pNext = pEntry->pNext;
6273: zipfileEntryFree(pEntry);
6274: }
6275: pTab->pFirstEntry = 0;
6276: pTab->pLastEntry = 0;
6277: pTab->szCurrent = 0;
6278: pTab->szOrig = 0;
6279: }
6280:
6281: /*
6282: ** This method is the destructor for zipfile vtab objects.
6283: */
6284: static int zipfileDisconnect(sqlite3_vtab *pVtab){
6285: zipfileCleanupTransaction((ZipfileTab*)pVtab);
6286: sqlite3_free(pVtab);
6287: return SQLITE_OK;
6288: }
6289:
6290: /*
6291: ** Constructor for a new ZipfileCsr object.
6292: */
6293: static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
6294: ZipfileTab *pTab = (ZipfileTab*)p;
6295: ZipfileCsr *pCsr;
6296: pCsr = sqlite3_malloc(sizeof(*pCsr));
6297: *ppCsr = (sqlite3_vtab_cursor*)pCsr;
6298: if( pCsr==0 ){
6299: return SQLITE_NOMEM;
6300: }
6301: memset(pCsr, 0, sizeof(*pCsr));
6302: pCsr->iId = ++pTab->iNextCsrid;
6303: pCsr->pCsrNext = pTab->pCsrList;
6304: pTab->pCsrList = pCsr;
6305: return SQLITE_OK;
6306: }
6307:
6308: /*
6309: ** Reset a cursor back to the state it was in when first returned
6310: ** by zipfileOpen().
6311: */
6312: static void zipfileResetCursor(ZipfileCsr *pCsr){
6313: ZipfileEntry *p;
6314: ZipfileEntry *pNext;
6315:
6316: pCsr->bEof = 0;
6317: if( pCsr->pFile ){
6318: fclose(pCsr->pFile);
6319: pCsr->pFile = 0;
6320: zipfileEntryFree(pCsr->pCurrent);
6321: pCsr->pCurrent = 0;
6322: }
6323:
6324: for(p=pCsr->pFreeEntry; p; p=pNext){
6325: pNext = p->pNext;
6326: zipfileEntryFree(p);
6327: }
6328: }
6329:
6330: /*
6331: ** Destructor for an ZipfileCsr.
6332: */
6333: static int zipfileClose(sqlite3_vtab_cursor *cur){
6334: ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6335: ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
6336: ZipfileCsr **pp;
6337: zipfileResetCursor(pCsr);
6338:
6339: /* Remove this cursor from the ZipfileTab.pCsrList list. */
6340: for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
6341: *pp = pCsr->pCsrNext;
6342:
6343: sqlite3_free(pCsr);
6344: return SQLITE_OK;
6345: }
6346:
6347: /*
6348: ** Set the error message for the virtual table associated with cursor
6349: ** pCsr to the results of vprintf(zFmt, ...).
6350: */
6351: static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
6352: va_list ap;
6353: va_start(ap, zFmt);
6354: sqlite3_free(pTab->base.zErrMsg);
6355: pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
6356: va_end(ap);
6357: }
6358: static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
6359: va_list ap;
6360: va_start(ap, zFmt);
6361: sqlite3_free(pCsr->base.pVtab->zErrMsg);
6362: pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
6363: va_end(ap);
6364: }
6365:
6366: /*
6367: ** Read nRead bytes of data from offset iOff of file pFile into buffer
6368: ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
6369: ** otherwise.
6370: **
6371: ** If an error does occur, output variable (*pzErrmsg) may be set to point
6372: ** to an English language error message. It is the responsibility of the
6373: ** caller to eventually free this buffer using
6374: ** sqlite3_free().
6375: */
6376: static int zipfileReadData(
6377: FILE *pFile, /* Read from this file */
6378: u8 *aRead, /* Read into this buffer */
6379: int nRead, /* Number of bytes to read */
6380: i64 iOff, /* Offset to read from */
6381: char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
6382: ){
6383: size_t n;
6384: fseek(pFile, (long)iOff, SEEK_SET);
6385: n = fread(aRead, 1, nRead, pFile);
6386: if( (int)n!=nRead ){
6387: *pzErrmsg = sqlite3_mprintf("error in fread()");
6388: return SQLITE_ERROR;
6389: }
6390: return SQLITE_OK;
6391: }
6392:
6393: static int zipfileAppendData(
6394: ZipfileTab *pTab,
6395: const u8 *aWrite,
6396: int nWrite
6397: ){
6398: if( nWrite>0 ){
6399: size_t n = nWrite;
6400: fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
6401: n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
6402: if( (int)n!=nWrite ){
6403: pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
6404: return SQLITE_ERROR;
6405: }
6406: pTab->szCurrent += nWrite;
6407: }
6408: return SQLITE_OK;
6409: }
6410:
6411: /*
6412: ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
6413: */
6414: static u16 zipfileGetU16(const u8 *aBuf){
6415: return (aBuf[1] << 8) + aBuf[0];
6416: }
6417:
6418: /*
6419: ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
6420: */
6421: static u32 zipfileGetU32(const u8 *aBuf){
6422: return ((u32)(aBuf[3]) << 24)
6423: + ((u32)(aBuf[2]) << 16)
6424: + ((u32)(aBuf[1]) << 8)
6425: + ((u32)(aBuf[0]) << 0);
6426: }
6427:
6428: /*
6429: ** Write a 16-bit little endiate integer into buffer aBuf.
6430: */
6431: static void zipfilePutU16(u8 *aBuf, u16 val){
6432: aBuf[0] = val & 0xFF;
6433: aBuf[1] = (val>>8) & 0xFF;
6434: }
6435:
6436: /*
6437: ** Write a 32-bit little endiate integer into buffer aBuf.
6438: */
6439: static void zipfilePutU32(u8 *aBuf, u32 val){
6440: aBuf[0] = val & 0xFF;
6441: aBuf[1] = (val>>8) & 0xFF;
6442: aBuf[2] = (val>>16) & 0xFF;
6443: aBuf[3] = (val>>24) & 0xFF;
6444: }
6445:
6446: #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
6447: #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
6448:
6449: #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
6450: #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
6451:
6452: /*
6453: ** Magic numbers used to read CDS records.
6454: */
6455: #define ZIPFILE_CDS_NFILE_OFF 28
6456: #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
6457:
6458: /*
6459: ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
6460: ** if the record is not well-formed, or SQLITE_OK otherwise.
6461: */
6462: static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
6463: u8 *aRead = aBuf;
6464: u32 sig = zipfileRead32(aRead);
6465: int rc = SQLITE_OK;
6466: if( sig!=ZIPFILE_SIGNATURE_CDS ){
6467: rc = SQLITE_ERROR;
6468: }else{
6469: pCDS->iVersionMadeBy = zipfileRead16(aRead);
6470: pCDS->iVersionExtract = zipfileRead16(aRead);
6471: pCDS->flags = zipfileRead16(aRead);
6472: pCDS->iCompression = zipfileRead16(aRead);
6473: pCDS->mTime = zipfileRead16(aRead);
6474: pCDS->mDate = zipfileRead16(aRead);
6475: pCDS->crc32 = zipfileRead32(aRead);
6476: pCDS->szCompressed = zipfileRead32(aRead);
6477: pCDS->szUncompressed = zipfileRead32(aRead);
6478: assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6479: pCDS->nFile = zipfileRead16(aRead);
6480: pCDS->nExtra = zipfileRead16(aRead);
6481: pCDS->nComment = zipfileRead16(aRead);
6482: pCDS->iDiskStart = zipfileRead16(aRead);
6483: pCDS->iInternalAttr = zipfileRead16(aRead);
6484: pCDS->iExternalAttr = zipfileRead32(aRead);
6485: pCDS->iOffset = zipfileRead32(aRead);
6486: assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
6487: }
6488:
6489: return rc;
6490: }
6491:
6492: /*
6493: ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
6494: ** if the record is not well-formed, or SQLITE_OK otherwise.
6495: */
6496: static int zipfileReadLFH(
6497: u8 *aBuffer,
6498: ZipfileLFH *pLFH
6499: ){
6500: u8 *aRead = aBuffer;
6501: int rc = SQLITE_OK;
6502:
6503: u32 sig = zipfileRead32(aRead);
6504: if( sig!=ZIPFILE_SIGNATURE_LFH ){
6505: rc = SQLITE_ERROR;
6506: }else{
6507: pLFH->iVersionExtract = zipfileRead16(aRead);
6508: pLFH->flags = zipfileRead16(aRead);
6509: pLFH->iCompression = zipfileRead16(aRead);
6510: pLFH->mTime = zipfileRead16(aRead);
6511: pLFH->mDate = zipfileRead16(aRead);
6512: pLFH->crc32 = zipfileRead32(aRead);
6513: pLFH->szCompressed = zipfileRead32(aRead);
6514: pLFH->szUncompressed = zipfileRead32(aRead);
6515: pLFH->nFile = zipfileRead16(aRead);
6516: pLFH->nExtra = zipfileRead16(aRead);
6517: }
6518: return rc;
6519: }
6520:
6521:
6522: /*
6523: ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
6524: ** Scan through this buffer to find an "extra-timestamp" field. If one
6525: ** exists, extract the 32-bit modification-timestamp from it and store
6526: ** the value in output parameter *pmTime.
6527: **
6528: ** Zero is returned if no extra-timestamp record could be found (and so
6529: ** *pmTime is left unchanged), or non-zero otherwise.
6530: **
6531: ** The general format of an extra field is:
6532: **
6533: ** Header ID 2 bytes
6534: ** Data Size 2 bytes
6535: ** Data N bytes
6536: */
6537: static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
6538: int ret = 0;
6539: u8 *p = aExtra;
6540: u8 *pEnd = &aExtra[nExtra];
6541:
6542: while( p<pEnd ){
6543: u16 id = zipfileRead16(p);
6544: u16 nByte = zipfileRead16(p);
6545:
6546: switch( id ){
6547: case ZIPFILE_EXTRA_TIMESTAMP: {
6548: u8 b = p[0];
6549: if( b & 0x01 ){ /* 0x01 -> modtime is present */
6550: *pmTime = zipfileGetU32(&p[1]);
6551: ret = 1;
6552: }
6553: break;
6554: }
6555: }
6556:
6557: p += nByte;
6558: }
6559: return ret;
6560: }
6561:
6562: /*
6563: ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
6564: ** fields of the CDS structure passed as the only argument to a 32-bit
6565: ** UNIX seconds-since-the-epoch timestamp. Return the result.
6566: **
6567: ** "Standard" MS-DOS time format:
6568: **
6569: ** File modification time:
6570: ** Bits 00-04: seconds divided by 2
6571: ** Bits 05-10: minute
6572: ** Bits 11-15: hour
6573: ** File modification date:
6574: ** Bits 00-04: day
6575: ** Bits 05-08: month (1-12)
6576: ** Bits 09-15: years from 1980
6577: **
6578: ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
6579: */
6580: static u32 zipfileMtime(ZipfileCDS *pCDS){
6581: int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
6582: int M = ((pCDS->mDate >> 5) & 0x0F);
6583: int D = (pCDS->mDate & 0x1F);
6584: int B = -13;
6585:
6586: int sec = (pCDS->mTime & 0x1F)*2;
6587: int min = (pCDS->mTime >> 5) & 0x3F;
6588: int hr = (pCDS->mTime >> 11) & 0x1F;
6589: i64 JD;
6590:
6591: /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
6592:
6593: /* Calculate the JD in seconds for noon on the day in question */
6594: if( M<3 ){
6595: Y = Y-1;
6596: M = M+12;
6597: }
6598: JD = (i64)(24*60*60) * (
6599: (int)(365.25 * (Y + 4716))
6600: + (int)(30.6001 * (M + 1))
6601: + D + B - 1524
6602: );
6603:
6604: /* Correct the JD for the time within the day */
6605: JD += (hr-12) * 3600 + min * 60 + sec;
6606:
6607: /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
6608: return (u32)(JD - (i64)(24405875) * 24*60*6);
6609: }
6610:
6611: /*
6612: ** The opposite of zipfileMtime(). This function populates the mTime and
6613: ** mDate fields of the CDS structure passed as the first argument according
6614: ** to the UNIX timestamp value passed as the second.
6615: */
6616: static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
6617: /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
6618: i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
6619:
6620: int A, B, C, D, E;
6621: int yr, mon, day;
6622: int hr, min, sec;
6623:
6624: A = (int)((JD - 1867216.25)/36524.25);
6625: A = (int)(JD + 1 + A - (A/4));
6626: B = A + 1524;
6627: C = (int)((B - 122.1)/365.25);
6628: D = (36525*(C&32767))/100;
6629: E = (int)((B-D)/30.6001);
6630:
6631: day = B - D - (int)(30.6001*E);
6632: mon = (E<14 ? E-1 : E-13);
6633: yr = mon>2 ? C-4716 : C-4715;
6634:
6635: hr = (mUnixTime % (24*60*60)) / (60*60);
6636: min = (mUnixTime % (60*60)) / 60;
6637: sec = (mUnixTime % 60);
6638:
6639: if( yr>=1980 ){
6640: pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
6641: pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
6642: }else{
6643: pCds->mDate = pCds->mTime = 0;
6644: }
6645:
6646: assert( mUnixTime<315507600
6647: || mUnixTime==zipfileMtime(pCds)
6648: || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
6649: /* || (mUnixTime % 2) */
6650: );
6651: }
6652:
6653: /*
6654: ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
6655: ** size) containing an entire zip archive image. Or, if aBlob is NULL,
6656: ** then pFile is a file-handle open on a zip file. In either case, this
6657: ** function creates a ZipfileEntry object based on the zip archive entry
6658: ** for which the CDS record is at offset iOff.
6659: **
6660: ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
6661: ** the new object. Otherwise, an SQLite error code is returned and the
6662: ** final value of (*ppEntry) undefined.
6663: */
6664: static int zipfileGetEntry(
6665: ZipfileTab *pTab, /* Store any error message here */
6666: const u8 *aBlob, /* Pointer to in-memory file image */
6667: int nBlob, /* Size of aBlob[] in bytes */
6668: FILE *pFile, /* If aBlob==0, read from this file */
6669: i64 iOff, /* Offset of CDS record */
6670: ZipfileEntry **ppEntry /* OUT: Pointer to new object */
6671: ){
6672: u8 *aRead;
6673: char **pzErr = &pTab->base.zErrMsg;
6674: int rc = SQLITE_OK;
6675:
6676: if( aBlob==0 ){
6677: aRead = pTab->aBuffer;
6678: rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
6679: }else{
6680: aRead = (u8*)&aBlob[iOff];
6681: }
6682:
6683: if( rc==SQLITE_OK ){
6684: sqlite3_int64 nAlloc;
6685: ZipfileEntry *pNew;
6686:
6687: int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
6688: int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
6689: nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
6690:
6691: nAlloc = sizeof(ZipfileEntry) + nExtra;
6692: if( aBlob ){
6693: nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
6694: }
6695:
6696: pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
6697: if( pNew==0 ){
6698: rc = SQLITE_NOMEM;
6699: }else{
6700: memset(pNew, 0, sizeof(ZipfileEntry));
6701: rc = zipfileReadCDS(aRead, &pNew->cds);
6702: if( rc!=SQLITE_OK ){
6703: *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
6704: }else if( aBlob==0 ){
6705: rc = zipfileReadData(
6706: pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
6707: );
6708: }else{
6709: aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
6710: }
6711: }
6712:
6713: if( rc==SQLITE_OK ){
6714: u32 *pt = &pNew->mUnixTime;
6715: pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
6716: pNew->aExtra = (u8*)&pNew[1];
6717: memcpy(pNew->aExtra, &aRead[nFile], nExtra);
6718: if( pNew->cds.zFile==0 ){
6719: rc = SQLITE_NOMEM;
6720: }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
6721: pNew->mUnixTime = zipfileMtime(&pNew->cds);
6722: }
6723: }
6724:
6725: if( rc==SQLITE_OK ){
6726: static const int szFix = ZIPFILE_LFH_FIXED_SZ;
6727: ZipfileLFH lfh;
6728: if( pFile ){
6729: rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
6730: }else{
6731: aRead = (u8*)&aBlob[pNew->cds.iOffset];
6732: }
6733:
6734: rc = zipfileReadLFH(aRead, &lfh);
6735: if( rc==SQLITE_OK ){
6736: pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
6737: pNew->iDataOff += lfh.nFile + lfh.nExtra;
6738: if( aBlob && pNew->cds.szCompressed ){
6739: pNew->aData = &pNew->aExtra[nExtra];
6740: memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
6741: }
6742: }else{
6743: *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
6744: (int)pNew->cds.iOffset
6745: );
6746: }
6747: }
6748:
6749: if( rc!=SQLITE_OK ){
6750: zipfileEntryFree(pNew);
6751: }else{
6752: *ppEntry = pNew;
6753: }
6754: }
6755:
6756: return rc;
6757: }
6758:
6759: /*
6760: ** Advance an ZipfileCsr to its next row of output.
6761: */
6762: static int zipfileNext(sqlite3_vtab_cursor *cur){
6763: ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6764: int rc = SQLITE_OK;
6765:
6766: if( pCsr->pFile ){
6767: i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
6768: zipfileEntryFree(pCsr->pCurrent);
6769: pCsr->pCurrent = 0;
6770: if( pCsr->iNextOff>=iEof ){
6771: pCsr->bEof = 1;
6772: }else{
6773: ZipfileEntry *p = 0;
6774: ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
6775: rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
6776: if( rc==SQLITE_OK ){
6777: pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
6778: pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
6779: }
6780: pCsr->pCurrent = p;
6781: }
6782: }else{
6783: if( !pCsr->bNoop ){
6784: pCsr->pCurrent = pCsr->pCurrent->pNext;
6785: }
6786: if( pCsr->pCurrent==0 ){
6787: pCsr->bEof = 1;
6788: }
6789: }
6790:
6791: pCsr->bNoop = 0;
6792: return rc;
6793: }
6794:
6795: static void zipfileFree(void *p) {
6796: sqlite3_free(p);
6797: }
6798:
6799: /*
6800: ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
6801: ** size is nOut bytes. This function uncompresses the data and sets the
6802: ** return value in context pCtx to the result (a blob).
6803: **
6804: ** If an error occurs, an error code is left in pCtx instead.
6805: */
6806: static void zipfileInflate(
6807: sqlite3_context *pCtx, /* Store result here */
6808: const u8 *aIn, /* Compressed data */
6809: int nIn, /* Size of buffer aIn[] in bytes */
6810: int nOut /* Expected output size */
6811: ){
6812: u8 *aRes = sqlite3_malloc(nOut);
6813: if( aRes==0 ){
6814: sqlite3_result_error_nomem(pCtx);
6815: }else{
6816: int err;
6817: z_stream str;
6818: memset(&str, 0, sizeof(str));
6819:
6820: str.next_in = (Byte*)aIn;
6821: str.avail_in = nIn;
6822: str.next_out = (Byte*)aRes;
6823: str.avail_out = nOut;
6824:
6825: err = inflateInit2(&str, -15);
6826: if( err!=Z_OK ){
6827: zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
6828: }else{
6829: err = inflate(&str, Z_NO_FLUSH);
6830: if( err!=Z_STREAM_END ){
6831: zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
6832: }else{
6833: sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
6834: aRes = 0;
6835: }
6836: }
6837: sqlite3_free(aRes);
6838: inflateEnd(&str);
6839: }
6840: }
6841:
6842: /*
6843: ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
6844: ** compresses it and sets (*ppOut) to point to a buffer containing the
6845: ** compressed data. The caller is responsible for eventually calling
6846: ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
6847: ** is set to the size of buffer (*ppOut) in bytes.
6848: **
6849: ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
6850: ** code is returned and an error message left in virtual-table handle
6851: ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
6852: ** case.
6853: */
6854: static int zipfileDeflate(
6855: const u8 *aIn, int nIn, /* Input */
6856: u8 **ppOut, int *pnOut, /* Output */
6857: char **pzErr /* OUT: Error message */
6858: ){
6859: int rc = SQLITE_OK;
6860: sqlite3_int64 nAlloc;
6861: z_stream str;
6862: u8 *aOut;
6863:
6864: memset(&str, 0, sizeof(str));
6865: str.next_in = (Bytef*)aIn;
6866: str.avail_in = nIn;
6867: deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
6868:
6869: nAlloc = deflateBound(&str, nIn);
6870: aOut = (u8*)sqlite3_malloc64(nAlloc);
6871: if( aOut==0 ){
6872: rc = SQLITE_NOMEM;
6873: }else{
6874: int res;
6875: str.next_out = aOut;
6876: str.avail_out = nAlloc;
6877: res = deflate(&str, Z_FINISH);
6878: if( res==Z_STREAM_END ){
6879: *ppOut = aOut;
6880: *pnOut = (int)str.total_out;
6881: }else{
6882: sqlite3_free(aOut);
6883: *pzErr = sqlite3_mprintf("zipfile: deflate() error");
6884: rc = SQLITE_ERROR;
6885: }
6886: deflateEnd(&str);
6887: }
6888:
6889: return rc;
6890: }
6891:
6892:
6893: /*
6894: ** Return values of columns for the row at which the series_cursor
6895: ** is currently pointing.
6896: */
6897: static int zipfileColumn(
6898: sqlite3_vtab_cursor *cur, /* The cursor */
6899: sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
6900: int i /* Which column to return */
6901: ){
6902: ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6903: ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
6904: int rc = SQLITE_OK;
6905: switch( i ){
6906: case 0: /* name */
6907: sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
6908: break;
6909: case 1: /* mode */
6910: /* TODO: Whether or not the following is correct surely depends on
6911: ** the platform on which the archive was created. */
6912: sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
6913: break;
6914: case 2: { /* mtime */
6915: sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
6916: break;
6917: }
6918: case 3: { /* sz */
6919: if( sqlite3_vtab_nochange(ctx)==0 ){
6920: sqlite3_result_int64(ctx, pCDS->szUncompressed);
6921: }
6922: break;
6923: }
6924: case 4: /* rawdata */
6925: if( sqlite3_vtab_nochange(ctx) ) break;
6926: case 5: { /* data */
6927: if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
6928: int sz = pCDS->szCompressed;
6929: int szFinal = pCDS->szUncompressed;
6930: if( szFinal>0 ){
6931: u8 *aBuf;
6932: u8 *aFree = 0;
6933: if( pCsr->pCurrent->aData ){
6934: aBuf = pCsr->pCurrent->aData;
6935: }else{
6936: aBuf = aFree = sqlite3_malloc64(sz);
6937: if( aBuf==0 ){
6938: rc = SQLITE_NOMEM;
6939: }else{
6940: FILE *pFile = pCsr->pFile;
6941: if( pFile==0 ){
6942: pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
6943: }
6944: rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
6945: &pCsr->base.pVtab->zErrMsg
6946: );
6947: }
6948: }
6949: if( rc==SQLITE_OK ){
6950: if( i==5 && pCDS->iCompression ){
6951: zipfileInflate(ctx, aBuf, sz, szFinal);
6952: }else{
6953: sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
6954: }
6955: }
6956: sqlite3_free(aFree);
6957: }else{
6958: /* Figure out if this is a directory or a zero-sized file. Consider
6959: ** it to be a directory either if the mode suggests so, or if
6960: ** the final character in the name is '/'. */
6961: u32 mode = pCDS->iExternalAttr >> 16;
6962: if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
6963: sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
6964: }
6965: }
6966: }
6967: break;
6968: }
6969: case 6: /* method */
6970: sqlite3_result_int(ctx, pCDS->iCompression);
6971: break;
6972: default: /* z */
6973: assert( i==7 );
6974: sqlite3_result_int64(ctx, pCsr->iId);
6975: break;
6976: }
6977:
6978: return rc;
6979: }
6980:
6981: /*
6982: ** Return TRUE if the cursor is at EOF.
6983: */
6984: static int zipfileEof(sqlite3_vtab_cursor *cur){
6985: ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6986: return pCsr->bEof;
6987: }
6988:
6989: /*
6990: ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
6991: ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
6992: ** is guaranteed to be a file-handle open on a zip file.
6993: **
6994: ** This function attempts to locate the EOCD record within the zip archive
6995: ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
6996: ** returned if successful. Otherwise, an SQLite error code is returned and
6997: ** an English language error message may be left in virtual-table pTab.
6998: */
6999: static int zipfileReadEOCD(
7000: ZipfileTab *pTab, /* Return errors here */
7001: const u8 *aBlob, /* Pointer to in-memory file image */
7002: int nBlob, /* Size of aBlob[] in bytes */
7003: FILE *pFile, /* Read from this file if aBlob==0 */
7004: ZipfileEOCD *pEOCD /* Object to populate */
7005: ){
7006: u8 *aRead = pTab->aBuffer; /* Temporary buffer */
7007: int nRead; /* Bytes to read from file */
7008: int rc = SQLITE_OK;
7009:
7010: if( aBlob==0 ){
7011: i64 iOff; /* Offset to read from */
7012: i64 szFile; /* Total size of file in bytes */
7013: fseek(pFile, 0, SEEK_END);
7014: szFile = (i64)ftell(pFile);
7015: if( szFile==0 ){
7016: memset(pEOCD, 0, sizeof(ZipfileEOCD));
7017: return SQLITE_OK;
7018: }
7019: nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7020: iOff = szFile - nRead;
7021: rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7022: }else{
7023: nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7024: aRead = (u8*)&aBlob[nBlob-nRead];
7025: }
7026:
7027: if( rc==SQLITE_OK ){
7028: int i;
7029:
7030: /* Scan backwards looking for the signature bytes */
7031: for(i=nRead-20; i>=0; i--){
7032: if( aRead[i]==0x50 && aRead[i+1]==0x4b
7033: && aRead[i+2]==0x05 && aRead[i+3]==0x06
7034: ){
7035: break;
7036: }
7037: }
7038: if( i<0 ){
7039: pTab->base.zErrMsg = sqlite3_mprintf(
7040: "cannot find end of central directory record"
7041: );
7042: return SQLITE_ERROR;
7043: }
7044:
7045: aRead += i+4;
7046: pEOCD->iDisk = zipfileRead16(aRead);
7047: pEOCD->iFirstDisk = zipfileRead16(aRead);
7048: pEOCD->nEntry = zipfileRead16(aRead);
7049: pEOCD->nEntryTotal = zipfileRead16(aRead);
7050: pEOCD->nSize = zipfileRead32(aRead);
7051: pEOCD->iOffset = zipfileRead32(aRead);
7052: }
7053:
7054: return rc;
7055: }
7056:
7057: /*
7058: ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
7059: ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7060: ** to the end of the list. Otherwise, it is added to the list immediately
7061: ** before pBefore (which is guaranteed to be a part of said list).
7062: */
7063: static void zipfileAddEntry(
7064: ZipfileTab *pTab,
7065: ZipfileEntry *pBefore,
7066: ZipfileEntry *pNew
7067: ){
7068: assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7069: assert( pNew->pNext==0 );
7070: if( pBefore==0 ){
7071: if( pTab->pFirstEntry==0 ){
7072: pTab->pFirstEntry = pTab->pLastEntry = pNew;
7073: }else{
7074: assert( pTab->pLastEntry->pNext==0 );
7075: pTab->pLastEntry->pNext = pNew;
7076: pTab->pLastEntry = pNew;
7077: }
7078: }else{
7079: ZipfileEntry **pp;
7080: for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7081: pNew->pNext = pBefore;
7082: *pp = pNew;
7083: }
7084: }
7085:
7086: static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7087: ZipfileEOCD eocd;
7088: int rc;
7089: int i;
7090: i64 iOff;
7091:
7092: rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7093: iOff = eocd.iOffset;
7094: for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7095: ZipfileEntry *pNew = 0;
7096: rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7097:
7098: if( rc==SQLITE_OK ){
7099: zipfileAddEntry(pTab, 0, pNew);
7100: iOff += ZIPFILE_CDS_FIXED_SZ;
7101: iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7102: }
7103: }
7104: return rc;
7105: }
7106:
7107: /*
7108: ** xFilter callback.
7109: */
7110: static int zipfileFilter(
7111: sqlite3_vtab_cursor *cur,
7112: int idxNum, const char *idxStr,
7113: int argc, sqlite3_value **argv
7114: ){
7115: ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7116: ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7117: const char *zFile = 0; /* Zip file to scan */
7118: int rc = SQLITE_OK; /* Return Code */
7119: int bInMemory = 0; /* True for an in-memory zipfile */
7120:
7121: zipfileResetCursor(pCsr);
7122:
7123: if( pTab->zFile ){
7124: zFile = pTab->zFile;
7125: }else if( idxNum==0 ){
7126: zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7127: return SQLITE_ERROR;
7128: }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7129: const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7130: int nBlob = sqlite3_value_bytes(argv[0]);
7131: assert( pTab->pFirstEntry==0 );
7132: rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7133: pCsr->pFreeEntry = pTab->pFirstEntry;
7134: pTab->pFirstEntry = pTab->pLastEntry = 0;
7135: if( rc!=SQLITE_OK ) return rc;
7136: bInMemory = 1;
7137: }else{
7138: zFile = (const char*)sqlite3_value_text(argv[0]);
7139: }
7140:
7141: if( 0==pTab->pWriteFd && 0==bInMemory ){
7142: pCsr->pFile = fopen(zFile, "rb");
7143: if( pCsr->pFile==0 ){
7144: zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7145: rc = SQLITE_ERROR;
7146: }else{
7147: rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7148: if( rc==SQLITE_OK ){
7149: if( pCsr->eocd.nEntry==0 ){
7150: pCsr->bEof = 1;
7151: }else{
7152: pCsr->iNextOff = pCsr->eocd.iOffset;
7153: rc = zipfileNext(cur);
7154: }
7155: }
7156: }
7157: }else{
7158: pCsr->bNoop = 1;
7159: pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7160: rc = zipfileNext(cur);
7161: }
7162:
7163: return rc;
7164: }
7165:
7166: /*
7167: ** xBestIndex callback.
7168: */
7169: static int zipfileBestIndex(
7170: sqlite3_vtab *tab,
7171: sqlite3_index_info *pIdxInfo
7172: ){
7173: int i;
7174: int idx = -1;
7175: int unusable = 0;
7176:
7177: for(i=0; i<pIdxInfo->nConstraint; i++){
7178: const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7179: if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7180: if( pCons->usable==0 ){
7181: unusable = 1;
7182: }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7183: idx = i;
7184: }
7185: }
7186: pIdxInfo->estimatedCost = 1000.0;
7187: if( idx>=0 ){
7188: pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7189: pIdxInfo->aConstraintUsage[idx].omit = 1;
7190: pIdxInfo->idxNum = 1;
7191: }else if( unusable ){
7192: return SQLITE_CONSTRAINT;
7193: }
7194: return SQLITE_OK;
7195: }
7196:
7197: static ZipfileEntry *zipfileNewEntry(const char *zPath){
7198: ZipfileEntry *pNew;
7199: pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7200: if( pNew ){
7201: memset(pNew, 0, sizeof(ZipfileEntry));
7202: pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7203: if( pNew->cds.zFile==0 ){
7204: sqlite3_free(pNew);
7205: pNew = 0;
7206: }
7207: }
7208: return pNew;
7209: }
7210:
7211: static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
7212: ZipfileCDS *pCds = &pEntry->cds;
7213: u8 *a = aBuf;
7214:
7215: pCds->nExtra = 9;
7216:
7217: /* Write the LFH itself */
7218: zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
7219: zipfileWrite16(a, pCds->iVersionExtract);
7220: zipfileWrite16(a, pCds->flags);
7221: zipfileWrite16(a, pCds->iCompression);
7222: zipfileWrite16(a, pCds->mTime);
7223: zipfileWrite16(a, pCds->mDate);
7224: zipfileWrite32(a, pCds->crc32);
7225: zipfileWrite32(a, pCds->szCompressed);
7226: zipfileWrite32(a, pCds->szUncompressed);
7227: zipfileWrite16(a, (u16)pCds->nFile);
7228: zipfileWrite16(a, pCds->nExtra);
7229: assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
7230:
7231: /* Add the file name */
7232: memcpy(a, pCds->zFile, (int)pCds->nFile);
7233: a += (int)pCds->nFile;
7234:
7235: /* The "extra" data */
7236: zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7237: zipfileWrite16(a, 5);
7238: *a++ = 0x01;
7239: zipfileWrite32(a, pEntry->mUnixTime);
7240:
7241: return a-aBuf;
7242: }
7243:
7244: static int zipfileAppendEntry(
7245: ZipfileTab *pTab,
7246: ZipfileEntry *pEntry,
7247: const u8 *pData,
7248: int nData
7249: ){
7250: u8 *aBuf = pTab->aBuffer;
7251: int nBuf;
7252: int rc;
7253:
7254: nBuf = zipfileSerializeLFH(pEntry, aBuf);
7255: rc = zipfileAppendData(pTab, aBuf, nBuf);
7256: if( rc==SQLITE_OK ){
7257: pEntry->iDataOff = pTab->szCurrent;
7258: rc = zipfileAppendData(pTab, pData, nData);
7259: }
7260:
7261: return rc;
7262: }
7263:
7264: static int zipfileGetMode(
7265: sqlite3_value *pVal,
7266: int bIsDir, /* If true, default to directory */
7267: u32 *pMode, /* OUT: Mode value */
7268: char **pzErr /* OUT: Error message */
7269: ){
7270: const char *z = (const char*)sqlite3_value_text(pVal);
7271: u32 mode = 0;
7272: if( z==0 ){
7273: mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
7274: }else if( z[0]>='0' && z[0]<='9' ){
7275: mode = (unsigned int)sqlite3_value_int(pVal);
7276: }else{
7277: const char zTemplate[11] = "-rwxrwxrwx";
7278: int i;
7279: if( strlen(z)!=10 ) goto parse_error;
7280: switch( z[0] ){
7281: case '-': mode |= S_IFREG; break;
7282: case 'd': mode |= S_IFDIR; break;
7283: case 'l': mode |= S_IFLNK; break;
7284: default: goto parse_error;
7285: }
7286: for(i=1; i<10; i++){
7287: if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
7288: else if( z[i]!='-' ) goto parse_error;
7289: }
7290: }
7291: if( ((mode & S_IFDIR)==0)==bIsDir ){
7292: /* The "mode" attribute is a directory, but data has been specified.
7293: ** Or vice-versa - no data but "mode" is a file or symlink. */
7294: *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
7295: return SQLITE_CONSTRAINT;
7296: }
7297: *pMode = mode;
7298: return SQLITE_OK;
7299:
7300: parse_error:
7301: *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
7302: return SQLITE_ERROR;
7303: }
7304:
7305: /*
7306: ** Both (const char*) arguments point to nul-terminated strings. Argument
7307: ** nB is the value of strlen(zB). This function returns 0 if the strings are
7308: ** identical, ignoring any trailing '/' character in either path. */
7309: static int zipfileComparePath(const char *zA, const char *zB, int nB){
7310: int nA = (int)strlen(zA);
7311: if( nA>0 && zA[nA-1]=='/' ) nA--;
7312: if( nB>0 && zB[nB-1]=='/' ) nB--;
7313: if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
7314: return 1;
7315: }
7316:
7317: static int zipfileBegin(sqlite3_vtab *pVtab){
7318: ZipfileTab *pTab = (ZipfileTab*)pVtab;
7319: int rc = SQLITE_OK;
7320:
7321: assert( pTab->pWriteFd==0 );
7322: if( pTab->zFile==0 || pTab->zFile[0]==0 ){
7323: pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
7324: return SQLITE_ERROR;
7325: }
7326:
7327: /* Open a write fd on the file. Also load the entire central directory
7328: ** structure into memory. During the transaction any new file data is
7329: ** appended to the archive file, but the central directory is accumulated
7330: ** in main-memory until the transaction is committed. */
7331: pTab->pWriteFd = fopen(pTab->zFile, "ab+");
7332: if( pTab->pWriteFd==0 ){
7333: pTab->base.zErrMsg = sqlite3_mprintf(
7334: "zipfile: failed to open file %s for writing", pTab->zFile
7335: );
7336: rc = SQLITE_ERROR;
7337: }else{
7338: fseek(pTab->pWriteFd, 0, SEEK_END);
7339: pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
7340: rc = zipfileLoadDirectory(pTab, 0, 0);
7341: }
7342:
7343: if( rc!=SQLITE_OK ){
7344: zipfileCleanupTransaction(pTab);
7345: }
7346:
7347: return rc;
7348: }
7349:
7350: /*
7351: ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
7352: ** time(2)).
7353: */
7354: static u32 zipfileTime(void){
7355: sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
7356: u32 ret;
7357: if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
7358: i64 ms;
7359: pVfs->xCurrentTimeInt64(pVfs, &ms);
7360: ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
7361: }else{
7362: double day;
7363: pVfs->xCurrentTime(pVfs, &day);
7364: ret = (u32)((day - 2440587.5) * 86400);
7365: }
7366: return ret;
7367: }
7368:
7369: /*
7370: ** Return a 32-bit timestamp in UNIX epoch format.
7371: **
7372: ** If the value passed as the only argument is either NULL or an SQL NULL,
7373: ** return the current time. Otherwise, return the value stored in (*pVal)
7374: ** cast to a 32-bit unsigned integer.
7375: */
7376: static u32 zipfileGetTime(sqlite3_value *pVal){
7377: if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
7378: return zipfileTime();
7379: }
7380: return (u32)sqlite3_value_int64(pVal);
7381: }
7382:
7383: /*
7384: ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
7385: ** linked list. Remove it from the list and free the object.
7386: */
7387: static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
7388: if( pOld ){
7389: ZipfileEntry **pp;
7390: for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
7391: *pp = (*pp)->pNext;
7392: zipfileEntryFree(pOld);
7393: }
7394: }
7395:
7396: /*
7397: ** xUpdate method.
7398: */
7399: static int zipfileUpdate(
7400: sqlite3_vtab *pVtab,
7401: int nVal,
7402: sqlite3_value **apVal,
7403: sqlite_int64 *pRowid
7404: ){
7405: ZipfileTab *pTab = (ZipfileTab*)pVtab;
7406: int rc = SQLITE_OK; /* Return Code */
7407: ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
7408:
7409: u32 mode = 0; /* Mode for new entry */
7410: u32 mTime = 0; /* Modification time for new entry */
7411: i64 sz = 0; /* Uncompressed size */
7412: const char *zPath = 0; /* Path for new entry */
7413: int nPath = 0; /* strlen(zPath) */
7414: const u8 *pData = 0; /* Pointer to buffer containing content */
7415: int nData = 0; /* Size of pData buffer in bytes */
7416: int iMethod = 0; /* Compression method for new entry */
7417: u8 *pFree = 0; /* Free this */
7418: char *zFree = 0; /* Also free this */
7419: ZipfileEntry *pOld = 0;
7420: ZipfileEntry *pOld2 = 0;
7421: int bUpdate = 0; /* True for an update that modifies "name" */
7422: int bIsDir = 0;
7423: u32 iCrc32 = 0;
7424:
7425: if( pTab->pWriteFd==0 ){
7426: rc = zipfileBegin(pVtab);
7427: if( rc!=SQLITE_OK ) return rc;
7428: }
7429:
7430: /* If this is a DELETE or UPDATE, find the archive entry to delete. */
7431: if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
7432: const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
7433: int nDelete = (int)strlen(zDelete);
7434: if( nVal>1 ){
7435: const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
7436: if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
7437: bUpdate = 1;
7438: }
7439: }
7440: for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
7441: if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
7442: break;
7443: }
7444: assert( pOld->pNext );
7445: }
7446: }
7447:
7448: if( nVal>1 ){
7449: /* Check that "sz" and "rawdata" are both NULL: */
7450: if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
7451: zipfileTableErr(pTab, "sz must be NULL");
7452: rc = SQLITE_CONSTRAINT;
7453: }
7454: if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
7455: zipfileTableErr(pTab, "rawdata must be NULL");
7456: rc = SQLITE_CONSTRAINT;
7457: }
7458:
7459: if( rc==SQLITE_OK ){
7460: if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
7461: /* data=NULL. A directory */
7462: bIsDir = 1;
7463: }else{
7464: /* Value specified for "data", and possibly "method". This must be
7465: ** a regular file or a symlink. */
7466: const u8 *aIn = sqlite3_value_blob(apVal[7]);
7467: int nIn = sqlite3_value_bytes(apVal[7]);
7468: int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
7469:
7470: iMethod = sqlite3_value_int(apVal[8]);
7471: sz = nIn;
7472: pData = aIn;
7473: nData = nIn;
7474: if( iMethod!=0 && iMethod!=8 ){
7475: zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
7476: rc = SQLITE_CONSTRAINT;
7477: }else{
7478: if( bAuto || iMethod ){
7479: int nCmp;
7480: rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
7481: if( rc==SQLITE_OK ){
7482: if( iMethod || nCmp<nIn ){
7483: iMethod = 8;
7484: pData = pFree;
7485: nData = nCmp;
7486: }
7487: }
7488: }
7489: iCrc32 = crc32(0, aIn, nIn);
7490: }
7491: }
7492: }
7493:
7494: if( rc==SQLITE_OK ){
7495: rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
7496: }
7497:
7498: if( rc==SQLITE_OK ){
7499: zPath = (const char*)sqlite3_value_text(apVal[2]);
7500: if( zPath==0 ) zPath = "";
7501: nPath = (int)strlen(zPath);
7502: mTime = zipfileGetTime(apVal[4]);
7503: }
7504:
7505: if( rc==SQLITE_OK && bIsDir ){
7506: /* For a directory, check that the last character in the path is a
7507: ** '/'. This appears to be required for compatibility with info-zip
7508: ** (the unzip command on unix). It does not create directories
7509: ** otherwise. */
7510: if( nPath<=0 || zPath[nPath-1]!='/' ){
7511: zFree = sqlite3_mprintf("%s/", zPath);
7512: zPath = (const char*)zFree;
7513: if( zFree==0 ){
7514: rc = SQLITE_NOMEM;
7515: nPath = 0;
7516: }else{
7517: nPath = (int)strlen(zPath);
7518: }
7519: }
7520: }
7521:
7522: /* Check that we're not inserting a duplicate entry -OR- updating an
7523: ** entry with a path, thereby making it into a duplicate. */
7524: if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
7525: ZipfileEntry *p;
7526: for(p=pTab->pFirstEntry; p; p=p->pNext){
7527: if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
7528: switch( sqlite3_vtab_on_conflict(pTab->db) ){
7529: case SQLITE_IGNORE: {
7530: goto zipfile_update_done;
7531: }
7532: case SQLITE_REPLACE: {
7533: pOld2 = p;
7534: break;
7535: }
7536: default: {
7537: zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
7538: rc = SQLITE_CONSTRAINT;
7539: break;
7540: }
7541: }
7542: break;
7543: }
7544: }
7545: }
7546:
7547: if( rc==SQLITE_OK ){
7548: /* Create the new CDS record. */
7549: pNew = zipfileNewEntry(zPath);
7550: if( pNew==0 ){
7551: rc = SQLITE_NOMEM;
7552: }else{
7553: pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7554: pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7555: pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7556: pNew->cds.iCompression = (u16)iMethod;
7557: zipfileMtimeToDos(&pNew->cds, mTime);
7558: pNew->cds.crc32 = iCrc32;
7559: pNew->cds.szCompressed = nData;
7560: pNew->cds.szUncompressed = (u32)sz;
7561: pNew->cds.iExternalAttr = (mode<<16);
7562: pNew->cds.iOffset = (u32)pTab->szCurrent;
7563: pNew->cds.nFile = (u16)nPath;
7564: pNew->mUnixTime = (u32)mTime;
7565: rc = zipfileAppendEntry(pTab, pNew, pData, nData);
7566: zipfileAddEntry(pTab, pOld, pNew);
7567: }
7568: }
7569: }
7570:
7571: if( rc==SQLITE_OK && (pOld || pOld2) ){
7572: ZipfileCsr *pCsr;
7573: for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7574: if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
7575: pCsr->pCurrent = pCsr->pCurrent->pNext;
7576: pCsr->bNoop = 1;
7577: }
7578: }
7579:
7580: zipfileRemoveEntryFromList(pTab, pOld);
7581: zipfileRemoveEntryFromList(pTab, pOld2);
7582: }
7583:
7584: zipfile_update_done:
7585: sqlite3_free(pFree);
7586: sqlite3_free(zFree);
7587: return rc;
7588: }
7589:
7590: static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
7591: u8 *a = aBuf;
7592: zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
7593: zipfileWrite16(a, p->iDisk);
7594: zipfileWrite16(a, p->iFirstDisk);
7595: zipfileWrite16(a, p->nEntry);
7596: zipfileWrite16(a, p->nEntryTotal);
7597: zipfileWrite32(a, p->nSize);
7598: zipfileWrite32(a, p->iOffset);
7599: zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
7600:
7601: return a-aBuf;
7602: }
7603:
7604: static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
7605: int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
7606: assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
7607: return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
7608: }
7609:
7610: /*
7611: ** Serialize the CDS structure into buffer aBuf[]. Return the number
7612: ** of bytes written.
7613: */
7614: static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
7615: u8 *a = aBuf;
7616: ZipfileCDS *pCDS = &pEntry->cds;
7617:
7618: if( pEntry->aExtra==0 ){
7619: pCDS->nExtra = 9;
7620: }
7621:
7622: zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
7623: zipfileWrite16(a, pCDS->iVersionMadeBy);
7624: zipfileWrite16(a, pCDS->iVersionExtract);
7625: zipfileWrite16(a, pCDS->flags);
7626: zipfileWrite16(a, pCDS->iCompression);
7627: zipfileWrite16(a, pCDS->mTime);
7628: zipfileWrite16(a, pCDS->mDate);
7629: zipfileWrite32(a, pCDS->crc32);
7630: zipfileWrite32(a, pCDS->szCompressed);
7631: zipfileWrite32(a, pCDS->szUncompressed);
7632: assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7633: zipfileWrite16(a, pCDS->nFile);
7634: zipfileWrite16(a, pCDS->nExtra);
7635: zipfileWrite16(a, pCDS->nComment);
7636: zipfileWrite16(a, pCDS->iDiskStart);
7637: zipfileWrite16(a, pCDS->iInternalAttr);
7638: zipfileWrite32(a, pCDS->iExternalAttr);
7639: zipfileWrite32(a, pCDS->iOffset);
7640:
7641: memcpy(a, pCDS->zFile, pCDS->nFile);
7642: a += pCDS->nFile;
7643:
7644: if( pEntry->aExtra ){
7645: int n = (int)pCDS->nExtra + (int)pCDS->nComment;
7646: memcpy(a, pEntry->aExtra, n);
7647: a += n;
7648: }else{
7649: assert( pCDS->nExtra==9 );
7650: zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7651: zipfileWrite16(a, 5);
7652: *a++ = 0x01;
7653: zipfileWrite32(a, pEntry->mUnixTime);
7654: }
7655:
7656: return a-aBuf;
7657: }
7658:
7659: static int zipfileCommit(sqlite3_vtab *pVtab){
7660: ZipfileTab *pTab = (ZipfileTab*)pVtab;
7661: int rc = SQLITE_OK;
7662: if( pTab->pWriteFd ){
7663: i64 iOffset = pTab->szCurrent;
7664: ZipfileEntry *p;
7665: ZipfileEOCD eocd;
7666: int nEntry = 0;
7667:
7668: /* Write out all entries */
7669: for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
7670: int n = zipfileSerializeCDS(p, pTab->aBuffer);
7671: rc = zipfileAppendData(pTab, pTab->aBuffer, n);
7672: nEntry++;
7673: }
7674:
7675: /* Write out the EOCD record */
7676: eocd.iDisk = 0;
7677: eocd.iFirstDisk = 0;
7678: eocd.nEntry = (u16)nEntry;
7679: eocd.nEntryTotal = (u16)nEntry;
7680: eocd.nSize = (u32)(pTab->szCurrent - iOffset);
7681: eocd.iOffset = (u32)iOffset;
7682: rc = zipfileAppendEOCD(pTab, &eocd);
7683:
7684: zipfileCleanupTransaction(pTab);
7685: }
7686: return rc;
7687: }
7688:
7689: static int zipfileRollback(sqlite3_vtab *pVtab){
7690: return zipfileCommit(pVtab);
7691: }
7692:
7693: static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
7694: ZipfileCsr *pCsr;
7695: for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7696: if( iId==pCsr->iId ) break;
7697: }
7698: return pCsr;
7699: }
7700:
7701: static void zipfileFunctionCds(
7702: sqlite3_context *context,
7703: int argc,
7704: sqlite3_value **argv
7705: ){
7706: ZipfileCsr *pCsr;
7707: ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
7708: assert( argc>0 );
7709:
7710: pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
7711: if( pCsr ){
7712: ZipfileCDS *p = &pCsr->pCurrent->cds;
7713: char *zRes = sqlite3_mprintf("{"
7714: "\"version-made-by\" : %u, "
7715: "\"version-to-extract\" : %u, "
7716: "\"flags\" : %u, "
7717: "\"compression\" : %u, "
7718: "\"time\" : %u, "
7719: "\"date\" : %u, "
7720: "\"crc32\" : %u, "
7721: "\"compressed-size\" : %u, "
7722: "\"uncompressed-size\" : %u, "
7723: "\"file-name-length\" : %u, "
7724: "\"extra-field-length\" : %u, "
7725: "\"file-comment-length\" : %u, "
7726: "\"disk-number-start\" : %u, "
7727: "\"internal-attr\" : %u, "
7728: "\"external-attr\" : %u, "
7729: "\"offset\" : %u }",
7730: (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
7731: (u32)p->flags, (u32)p->iCompression,
7732: (u32)p->mTime, (u32)p->mDate,
7733: (u32)p->crc32, (u32)p->szCompressed,
7734: (u32)p->szUncompressed, (u32)p->nFile,
7735: (u32)p->nExtra, (u32)p->nComment,
7736: (u32)p->iDiskStart, (u32)p->iInternalAttr,
7737: (u32)p->iExternalAttr, (u32)p->iOffset
7738: );
7739:
7740: if( zRes==0 ){
7741: sqlite3_result_error_nomem(context);
7742: }else{
7743: sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
7744: sqlite3_free(zRes);
7745: }
7746: }
7747: }
7748:
7749: /*
7750: ** xFindFunction method.
7751: */
7752: static int zipfileFindFunction(
7753: sqlite3_vtab *pVtab, /* Virtual table handle */
7754: int nArg, /* Number of SQL function arguments */
7755: const char *zName, /* Name of SQL function */
7756: void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
7757: void **ppArg /* OUT: User data for *pxFunc */
7758: ){
7759: if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
7760: *pxFunc = zipfileFunctionCds;
7761: *ppArg = (void*)pVtab;
7762: return 1;
7763: }
7764: return 0;
7765: }
7766:
7767: typedef struct ZipfileBuffer ZipfileBuffer;
7768: struct ZipfileBuffer {
7769: u8 *a; /* Pointer to buffer */
7770: int n; /* Size of buffer in bytes */
7771: int nAlloc; /* Byte allocated at a[] */
7772: };
7773:
7774: typedef struct ZipfileCtx ZipfileCtx;
7775: struct ZipfileCtx {
7776: int nEntry;
7777: ZipfileBuffer body;
7778: ZipfileBuffer cds;
7779: };
7780:
7781: static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
7782: if( pBuf->n+nByte>pBuf->nAlloc ){
7783: u8 *aNew;
7784: sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
7785: int nReq = pBuf->n + nByte;
7786:
7787: while( nNew<nReq ) nNew = nNew*2;
7788: aNew = sqlite3_realloc64(pBuf->a, nNew);
7789: if( aNew==0 ) return SQLITE_NOMEM;
7790: pBuf->a = aNew;
7791: pBuf->nAlloc = (int)nNew;
7792: }
7793: return SQLITE_OK;
7794: }
7795:
7796: /*
7797: ** xStep() callback for the zipfile() aggregate. This can be called in
7798: ** any of the following ways:
7799: **
7800: ** SELECT zipfile(name,data) ...
7801: ** SELECT zipfile(name,mode,mtime,data) ...
7802: ** SELECT zipfile(name,mode,mtime,data,method) ...
7803: */
7804: void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
7805: ZipfileCtx *p; /* Aggregate function context */
7806: ZipfileEntry e; /* New entry to add to zip archive */
7807:
7808: sqlite3_value *pName = 0;
7809: sqlite3_value *pMode = 0;
7810: sqlite3_value *pMtime = 0;
7811: sqlite3_value *pData = 0;
7812: sqlite3_value *pMethod = 0;
7813:
7814: int bIsDir = 0;
7815: u32 mode;
7816: int rc = SQLITE_OK;
7817: char *zErr = 0;
7818:
7819: int iMethod = -1; /* Compression method to use (0 or 8) */
7820:
7821: const u8 *aData = 0; /* Possibly compressed data for new entry */
7822: int nData = 0; /* Size of aData[] in bytes */
7823: int szUncompressed = 0; /* Size of data before compression */
7824: u8 *aFree = 0; /* Free this before returning */
7825: u32 iCrc32 = 0; /* crc32 of uncompressed data */
7826:
7827: char *zName = 0; /* Path (name) of new entry */
7828: int nName = 0; /* Size of zName in bytes */
7829: char *zFree = 0; /* Free this before returning */
7830: int nByte;
7831:
7832: memset(&e, 0, sizeof(e));
7833: p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7834: if( p==0 ) return;
7835:
7836: /* Martial the arguments into stack variables */
7837: if( nVal!=2 && nVal!=4 && nVal!=5 ){
7838: zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
7839: rc = SQLITE_ERROR;
7840: goto zipfile_step_out;
7841: }
7842: pName = apVal[0];
7843: if( nVal==2 ){
7844: pData = apVal[1];
7845: }else{
7846: pMode = apVal[1];
7847: pMtime = apVal[2];
7848: pData = apVal[3];
7849: if( nVal==5 ){
7850: pMethod = apVal[4];
7851: }
7852: }
7853:
7854: /* Check that the 'name' parameter looks ok. */
7855: zName = (char*)sqlite3_value_text(pName);
7856: nName = sqlite3_value_bytes(pName);
7857: if( zName==0 ){
7858: zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
7859: rc = SQLITE_ERROR;
7860: goto zipfile_step_out;
7861: }
7862:
7863: /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
7864: ** deflate compression) or NULL (choose automatically). */
7865: if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
7866: iMethod = (int)sqlite3_value_int64(pMethod);
7867: if( iMethod!=0 && iMethod!=8 ){
7868: zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
7869: rc = SQLITE_ERROR;
7870: goto zipfile_step_out;
7871: }
7872: }
7873:
7874: /* Now inspect the data. If this is NULL, then the new entry must be a
7875: ** directory. Otherwise, figure out whether or not the data should
7876: ** be deflated or simply stored in the zip archive. */
7877: if( sqlite3_value_type(pData)==SQLITE_NULL ){
7878: bIsDir = 1;
7879: iMethod = 0;
7880: }else{
7881: aData = sqlite3_value_blob(pData);
7882: szUncompressed = nData = sqlite3_value_bytes(pData);
7883: iCrc32 = crc32(0, aData, nData);
7884: if( iMethod<0 || iMethod==8 ){
7885: int nOut = 0;
7886: rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
7887: if( rc!=SQLITE_OK ){
7888: goto zipfile_step_out;
7889: }
7890: if( iMethod==8 || nOut<nData ){
7891: aData = aFree;
7892: nData = nOut;
7893: iMethod = 8;
7894: }else{
7895: iMethod = 0;
7896: }
7897: }
7898: }
7899:
7900: /* Decode the "mode" argument. */
7901: rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
7902: if( rc ) goto zipfile_step_out;
7903:
7904: /* Decode the "mtime" argument. */
7905: e.mUnixTime = zipfileGetTime(pMtime);
7906:
7907: /* If this is a directory entry, ensure that there is exactly one '/'
7908: ** at the end of the path. Or, if this is not a directory and the path
7909: ** ends in '/' it is an error. */
7910: if( bIsDir==0 ){
7911: if( nName>0 && zName[nName-1]=='/' ){
7912: zErr = sqlite3_mprintf("non-directory name must not end with /");
7913: rc = SQLITE_ERROR;
7914: goto zipfile_step_out;
7915: }
7916: }else{
7917: if( nName==0 || zName[nName-1]!='/' ){
7918: zName = zFree = sqlite3_mprintf("%s/", zName);
7919: if( zName==0 ){
7920: rc = SQLITE_NOMEM;
7921: goto zipfile_step_out;
7922: }
7923: nName = (int)strlen(zName);
7924: }else{
7925: while( nName>1 && zName[nName-2]=='/' ) nName--;
7926: }
7927: }
7928:
7929: /* Assemble the ZipfileEntry object for the new zip archive entry */
7930: e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7931: e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7932: e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7933: e.cds.iCompression = (u16)iMethod;
7934: zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
7935: e.cds.crc32 = iCrc32;
7936: e.cds.szCompressed = nData;
7937: e.cds.szUncompressed = szUncompressed;
7938: e.cds.iExternalAttr = (mode<<16);
7939: e.cds.iOffset = p->body.n;
7940: e.cds.nFile = (u16)nName;
7941: e.cds.zFile = zName;
7942:
7943: /* Append the LFH to the body of the new archive */
7944: nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
7945: if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
7946: p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
7947:
7948: /* Append the data to the body of the new archive */
7949: if( nData>0 ){
7950: if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
7951: memcpy(&p->body.a[p->body.n], aData, nData);
7952: p->body.n += nData;
7953: }
7954:
7955: /* Append the CDS record to the directory of the new archive */
7956: nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
7957: if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
7958: p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
7959:
7960: /* Increment the count of entries in the archive */
7961: p->nEntry++;
7962:
7963: zipfile_step_out:
7964: sqlite3_free(aFree);
7965: sqlite3_free(zFree);
7966: if( rc ){
7967: if( zErr ){
7968: sqlite3_result_error(pCtx, zErr, -1);
7969: }else{
7970: sqlite3_result_error_code(pCtx, rc);
7971: }
7972: }
7973: sqlite3_free(zErr);
7974: }
7975:
7976: /*
7977: ** xFinalize() callback for zipfile aggregate function.
7978: */
7979: void zipfileFinal(sqlite3_context *pCtx){
7980: ZipfileCtx *p;
7981: ZipfileEOCD eocd;
7982: sqlite3_int64 nZip;
7983: u8 *aZip;
7984:
7985: p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7986: if( p==0 ) return;
7987: if( p->nEntry>0 ){
7988: memset(&eocd, 0, sizeof(eocd));
7989: eocd.nEntry = (u16)p->nEntry;
7990: eocd.nEntryTotal = (u16)p->nEntry;
7991: eocd.nSize = p->cds.n;
7992: eocd.iOffset = p->body.n;
7993:
7994: nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
7995: aZip = (u8*)sqlite3_malloc64(nZip);
7996: if( aZip==0 ){
7997: sqlite3_result_error_nomem(pCtx);
7998: }else{
7999: memcpy(aZip, p->body.a, p->body.n);
8000: memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8001: zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8002: sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8003: }
8004: }
8005:
8006: sqlite3_free(p->body.a);
8007: sqlite3_free(p->cds.a);
8008: }
8009:
8010:
8011: /*
8012: ** Register the "zipfile" virtual table.
8013: */
8014: static int zipfileRegister(sqlite3 *db){
8015: static sqlite3_module zipfileModule = {
8016: 1, /* iVersion */
8017: zipfileConnect, /* xCreate */
8018: zipfileConnect, /* xConnect */
8019: zipfileBestIndex, /* xBestIndex */
8020: zipfileDisconnect, /* xDisconnect */
8021: zipfileDisconnect, /* xDestroy */
8022: zipfileOpen, /* xOpen - open a cursor */
8023: zipfileClose, /* xClose - close a cursor */
8024: zipfileFilter, /* xFilter - configure scan constraints */
8025: zipfileNext, /* xNext - advance a cursor */
8026: zipfileEof, /* xEof - check for end of scan */
8027: zipfileColumn, /* xColumn - read data */
8028: 0, /* xRowid - read data */
8029: zipfileUpdate, /* xUpdate */
8030: zipfileBegin, /* xBegin */
8031: 0, /* xSync */
8032: zipfileCommit, /* xCommit */
8033: zipfileRollback, /* xRollback */
8034: zipfileFindFunction, /* xFindMethod */
8035: 0, /* xRename */
8036: };
8037:
8038: int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
8039: if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8040: if( rc==SQLITE_OK ){
8041: rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
8042: zipfileStep, zipfileFinal
8043: );
8044: }
8045: return rc;
8046: }
8047: #else /* SQLITE_OMIT_VIRTUALTABLE */
8048: # define zipfileRegister(x) SQLITE_OK
8049: #endif
8050:
8051: #ifdef _WIN32
8052:
8053: #endif
8054: int sqlite3_zipfile_init(
8055: sqlite3 *db,
8056: char **pzErrMsg,
8057: const sqlite3_api_routines *pApi
8058: ){
8059: SQLITE_EXTENSION_INIT2(pApi);
8060: (void)pzErrMsg; /* Unused parameter */
8061: return zipfileRegister(db);
8062: }
8063:
8064: /************************* End ../ext/misc/zipfile.c ********************/
8065: /************************* Begin ../ext/misc/sqlar.c ******************/
8066: /*
8067: ** 2017-12-17
8068: **
8069: ** The author disclaims copyright to this source code. In place of
8070: ** a legal notice, here is a blessing:
8071: **
8072: ** May you do good and not evil.
8073: ** May you find forgiveness for yourself and forgive others.
8074: ** May you share freely, never taking more than you give.
8075: **
8076: ******************************************************************************
8077: **
8078: ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8079: ** for working with sqlar archives and used by the shell tool's built-in
8080: ** sqlar support.
8081: */
8082: /* #include "sqlite3ext.h" */
8083: SQLITE_EXTENSION_INIT1
8084: #include <zlib.h>
8085: #include <assert.h>
8086:
8087: /*
8088: ** Implementation of the "sqlar_compress(X)" SQL function.
8089: **
8090: ** If the type of X is SQLITE_BLOB, and compressing that blob using
8091: ** zlib utility function compress() yields a smaller blob, return the
8092: ** compressed blob. Otherwise, return a copy of X.
8093: **
8094: ** SQLar uses the "zlib format" for compressed content. The zlib format
8095: ** contains a two-byte identification header and a four-byte checksum at
8096: ** the end. This is different from ZIP which uses the raw deflate format.
8097: **
8098: ** Future enhancements to SQLar might add support for new compression formats.
8099: ** If so, those new formats will be identified by alternative headers in the
8100: ** compressed data.
8101: */
8102: static void sqlarCompressFunc(
8103: sqlite3_context *context,
8104: int argc,
8105: sqlite3_value **argv
8106: ){
8107: assert( argc==1 );
8108: if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8109: const Bytef *pData = sqlite3_value_blob(argv[0]);
8110: uLong nData = sqlite3_value_bytes(argv[0]);
8111: uLongf nOut = compressBound(nData);
8112: Bytef *pOut;
8113:
8114: pOut = (Bytef*)sqlite3_malloc(nOut);
8115: if( pOut==0 ){
8116: sqlite3_result_error_nomem(context);
8117: return;
8118: }else{
8119: if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8120: sqlite3_result_error(context, "error in compress()", -1);
8121: }else if( nOut<nData ){
8122: sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8123: }else{
8124: sqlite3_result_value(context, argv[0]);
8125: }
8126: sqlite3_free(pOut);
8127: }
8128: }else{
8129: sqlite3_result_value(context, argv[0]);
8130: }
8131: }
8132:
8133: /*
8134: ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8135: **
8136: ** Parameter SZ is interpreted as an integer. If it is less than or
8137: ** equal to zero, then this function returns a copy of X. Or, if
8138: ** SZ is equal to the size of X when interpreted as a blob, also
8139: ** return a copy of X. Otherwise, decompress blob X using zlib
8140: ** utility function uncompress() and return the results (another
8141: ** blob).
8142: */
8143: static void sqlarUncompressFunc(
8144: sqlite3_context *context,
8145: int argc,
8146: sqlite3_value **argv
8147: ){
8148: uLong nData;
8149: uLongf sz;
8150:
8151: assert( argc==2 );
8152: sz = sqlite3_value_int(argv[1]);
8153:
8154: if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8155: sqlite3_result_value(context, argv[0]);
8156: }else{
8157: const Bytef *pData= sqlite3_value_blob(argv[0]);
8158: Bytef *pOut = sqlite3_malloc(sz);
8159: if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8160: sqlite3_result_error(context, "error in uncompress()", -1);
8161: }else{
8162: sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8163: }
8164: sqlite3_free(pOut);
8165: }
8166: }
8167:
8168:
8169: #ifdef _WIN32
8170:
8171: #endif
8172: int sqlite3_sqlar_init(
8173: sqlite3 *db,
8174: char **pzErrMsg,
8175: const sqlite3_api_routines *pApi
8176: ){
8177: int rc = SQLITE_OK;
8178: SQLITE_EXTENSION_INIT2(pApi);
8179: (void)pzErrMsg; /* Unused parameter */
8180: rc = sqlite3_create_function(db, "sqlar_compress", 1,
8181: SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8182: sqlarCompressFunc, 0, 0);
8183: if( rc==SQLITE_OK ){
8184: rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8185: SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8186: sqlarUncompressFunc, 0, 0);
8187: }
8188: return rc;
8189: }
8190:
8191: /************************* End ../ext/misc/sqlar.c ********************/
8192: #endif
8193: /************************* Begin ../ext/expert/sqlite3expert.h ******************/
8194: /*
8195: ** 2017 April 07
8196: **
8197: ** The author disclaims copyright to this source code. In place of
8198: ** a legal notice, here is a blessing:
8199: **
8200: ** May you do good and not evil.
8201: ** May you find forgiveness for yourself and forgive others.
8202: ** May you share freely, never taking more than you give.
8203: **
8204: *************************************************************************
8205: */
8206: #if !defined(SQLITEEXPERT_H)
8207: #define SQLITEEXPERT_H 1
8208: /* #include "sqlite3.h" */
8209:
8210: typedef struct sqlite3expert sqlite3expert;
8211:
8212: /*
8213: ** Create a new sqlite3expert object.
8214: **
8215: ** If successful, a pointer to the new object is returned and (*pzErr) set
8216: ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
8217: ** an English-language error message. In this case it is the responsibility
8218: ** of the caller to eventually free the error message buffer using
8219: ** sqlite3_free().
8220: */
8221: sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
8222:
8223: /*
8224: ** Configure an sqlite3expert object.
8225: **
8226: ** EXPERT_CONFIG_SAMPLE:
8227: ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
8228: ** each candidate index. This involves scanning and sorting the entire
8229: ** contents of each user database table once for each candidate index
8230: ** associated with the table. For large databases, this can be
8231: ** prohibitively slow. This option allows the sqlite3expert object to
8232: ** be configured so that sqlite_stat1 data is instead generated based on a
8233: ** subset of each table, or so that no sqlite_stat1 data is used at all.
8234: **
8235: ** A single integer argument is passed to this option. If the value is less
8236: ** than or equal to zero, then no sqlite_stat1 data is generated or used by
8237: ** the analysis - indexes are recommended based on the database schema only.
8238: ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
8239: ** generated for each candidate index (this is the default). Finally, if the
8240: ** value falls between 0 and 100, then it represents the percentage of user
8241: ** table rows that should be considered when generating sqlite_stat1 data.
8242: **
8243: ** Examples:
8244: **
8245: ** // Do not generate any sqlite_stat1 data
8246: ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
8247: **
8248: ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
8249: ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
8250: */
8251: int sqlite3_expert_config(sqlite3expert *p, int op, ...);
8252:
8253: #define EXPERT_CONFIG_SAMPLE 1 /* int */
8254:
8255: /*
8256: ** Specify zero or more SQL statements to be included in the analysis.
8257: **
8258: ** Buffer zSql must contain zero or more complete SQL statements. This
8259: ** function parses all statements contained in the buffer and adds them
8260: ** to the internal list of statements to analyze. If successful, SQLITE_OK
8261: ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
8262: ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
8263: ** may be set to point to an English language error message. In this case
8264: ** the caller is responsible for eventually freeing the error message buffer
8265: ** using sqlite3_free().
8266: **
8267: ** If an error does occur while processing one of the statements in the
8268: ** buffer passed as the second argument, none of the statements in the
8269: ** buffer are added to the analysis.
8270: **
8271: ** This function must be called before sqlite3_expert_analyze(). If a call
8272: ** to this function is made on an sqlite3expert object that has already
8273: ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
8274: ** immediately and no statements are added to the analysis.
8275: */
8276: int sqlite3_expert_sql(
8277: sqlite3expert *p, /* From a successful sqlite3_expert_new() */
8278: const char *zSql, /* SQL statement(s) to add */
8279: char **pzErr /* OUT: Error message (if any) */
8280: );
8281:
8282:
8283: /*
8284: ** This function is called after the sqlite3expert object has been configured
8285: ** with all SQL statements using sqlite3_expert_sql() to actually perform
8286: ** the analysis. Once this function has been called, it is not possible to
8287: ** add further SQL statements to the analysis.
8288: **
8289: ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
8290: ** an error occurs, an SQLite error code is returned and (*pzErr) set to
8291: ** point to a buffer containing an English language error message. In this
8292: ** case it is the responsibility of the caller to eventually free the buffer
8293: ** using sqlite3_free().
8294: **
8295: ** If an error does occur within this function, the sqlite3expert object
8296: ** is no longer useful for any purpose. At that point it is no longer
8297: ** possible to add further SQL statements to the object or to re-attempt
8298: ** the analysis. The sqlite3expert object must still be freed using a call
8299: ** sqlite3_expert_destroy().
8300: */
8301: int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
8302:
8303: /*
8304: ** Return the total number of statements loaded using sqlite3_expert_sql().
8305: ** The total number of SQL statements may be different from the total number
8306: ** to calls to sqlite3_expert_sql().
8307: */
8308: int sqlite3_expert_count(sqlite3expert*);
8309:
8310: /*
8311: ** Return a component of the report.
8312: **
8313: ** This function is called after sqlite3_expert_analyze() to extract the
8314: ** results of the analysis. Each call to this function returns either a
8315: ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
8316: ** The value passed as the third argument must be one of the EXPERT_REPORT_*
8317: ** #define constants defined below.
8318: **
8319: ** For some EXPERT_REPORT_* parameters, the buffer returned contains
8320: ** information relating to a specific SQL statement. In these cases that
8321: ** SQL statement is identified by the value passed as the second argument.
8322: ** SQL statements are numbered from 0 in the order in which they are parsed.
8323: ** If an out-of-range value (less than zero or equal to or greater than the
8324: ** value returned by sqlite3_expert_count()) is passed as the second argument
8325: ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
8326: **
8327: ** EXPERT_REPORT_SQL:
8328: ** Return the text of SQL statement iStmt.
8329: **
8330: ** EXPERT_REPORT_INDEXES:
8331: ** Return a buffer containing the CREATE INDEX statements for all recommended
8332: ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
8333: ** is returned.
8334: **
8335: ** EXPERT_REPORT_PLAN:
8336: ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
8337: ** iStmt after the proposed indexes have been added to the database schema.
8338: **
8339: ** EXPERT_REPORT_CANDIDATES:
8340: ** Return a pointer to a buffer containing the CREATE INDEX statements
8341: ** for all indexes that were tested (for all SQL statements). The iStmt
8342: ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
8343: */
8344: const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
8345:
8346: /*
8347: ** Values for the third argument passed to sqlite3_expert_report().
8348: */
8349: #define EXPERT_REPORT_SQL 1
8350: #define EXPERT_REPORT_INDEXES 2
8351: #define EXPERT_REPORT_PLAN 3
8352: #define EXPERT_REPORT_CANDIDATES 4
8353:
8354: /*
8355: ** Free an (sqlite3expert*) handle and all associated resources. There
8356: ** should be one call to this function for each successful call to
8357: ** sqlite3-expert_new().
8358: */
8359: void sqlite3_expert_destroy(sqlite3expert*);
8360:
8361: #endif /* !defined(SQLITEEXPERT_H) */
8362:
8363: /************************* End ../ext/expert/sqlite3expert.h ********************/
8364: /************************* Begin ../ext/expert/sqlite3expert.c ******************/
8365: /*
8366: ** 2017 April 09
8367: **
8368: ** The author disclaims copyright to this source code. In place of
8369: ** a legal notice, here is a blessing:
8370: **
8371: ** May you do good and not evil.
8372: ** May you find forgiveness for yourself and forgive others.
8373: ** May you share freely, never taking more than you give.
8374: **
8375: *************************************************************************
8376: */
8377: /* #include "sqlite3expert.h" */
8378: #include <assert.h>
8379: #include <string.h>
8380: #include <stdio.h>
8381:
8382: #ifndef SQLITE_OMIT_VIRTUALTABLE
8383:
8384: /* typedef sqlite3_int64 i64; */
8385: /* typedef sqlite3_uint64 u64; */
8386:
8387: typedef struct IdxColumn IdxColumn;
8388: typedef struct IdxConstraint IdxConstraint;
8389: typedef struct IdxScan IdxScan;
8390: typedef struct IdxStatement IdxStatement;
8391: typedef struct IdxTable IdxTable;
8392: typedef struct IdxWrite IdxWrite;
8393:
8394: #define STRLEN (int)strlen
8395:
8396: /*
8397: ** A temp table name that we assume no user database will actually use.
8398: ** If this assumption proves incorrect triggers on the table with the
8399: ** conflicting name will be ignored.
8400: */
8401: #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
8402:
8403: /*
8404: ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
8405: ** any other type of single-ended range constraint on a column).
8406: **
8407: ** pLink:
8408: ** Used to temporarily link IdxConstraint objects into lists while
8409: ** creating candidate indexes.
8410: */
8411: struct IdxConstraint {
8412: char *zColl; /* Collation sequence */
8413: int bRange; /* True for range, false for eq */
8414: int iCol; /* Constrained table column */
8415: int bFlag; /* Used by idxFindCompatible() */
8416: int bDesc; /* True if ORDER BY <expr> DESC */
8417: IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
8418: IdxConstraint *pLink; /* See above */
8419: };
8420:
8421: /*
8422: ** A single scan of a single table.
8423: */
8424: struct IdxScan {
8425: IdxTable *pTab; /* Associated table object */
8426: int iDb; /* Database containing table zTable */
8427: i64 covering; /* Mask of columns required for cov. index */
8428: IdxConstraint *pOrder; /* ORDER BY columns */
8429: IdxConstraint *pEq; /* List of == constraints */
8430: IdxConstraint *pRange; /* List of < constraints */
8431: IdxScan *pNextScan; /* Next IdxScan object for same analysis */
8432: };
8433:
8434: /*
8435: ** Information regarding a single database table. Extracted from
8436: ** "PRAGMA table_info" by function idxGetTableInfo().
8437: */
8438: struct IdxColumn {
8439: char *zName;
8440: char *zColl;
8441: int iPk;
8442: };
8443: struct IdxTable {
8444: int nCol;
8445: char *zName; /* Table name */
8446: IdxColumn *aCol;
8447: IdxTable *pNext; /* Next table in linked list of all tables */
8448: };
8449:
8450: /*
8451: ** An object of the following type is created for each unique table/write-op
8452: ** seen. The objects are stored in a singly-linked list beginning at
8453: ** sqlite3expert.pWrite.
8454: */
8455: struct IdxWrite {
8456: IdxTable *pTab;
8457: int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
8458: IdxWrite *pNext;
8459: };
8460:
8461: /*
8462: ** Each statement being analyzed is represented by an instance of this
8463: ** structure.
8464: */
8465: struct IdxStatement {
8466: int iId; /* Statement number */
8467: char *zSql; /* SQL statement */
8468: char *zIdx; /* Indexes */
8469: char *zEQP; /* Plan */
8470: IdxStatement *pNext;
8471: };
8472:
8473:
8474: /*
8475: ** A hash table for storing strings. With space for a payload string
8476: ** with each entry. Methods are:
8477: **
8478: ** idxHashInit()
8479: ** idxHashClear()
8480: ** idxHashAdd()
8481: ** idxHashSearch()
8482: */
8483: #define IDX_HASH_SIZE 1023
8484: typedef struct IdxHashEntry IdxHashEntry;
8485: typedef struct IdxHash IdxHash;
8486: struct IdxHashEntry {
8487: char *zKey; /* nul-terminated key */
8488: char *zVal; /* nul-terminated value string */
8489: char *zVal2; /* nul-terminated value string 2 */
8490: IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
8491: IdxHashEntry *pNext; /* Next entry in hash */
8492: };
8493: struct IdxHash {
8494: IdxHashEntry *pFirst;
8495: IdxHashEntry *aHash[IDX_HASH_SIZE];
8496: };
8497:
8498: /*
8499: ** sqlite3expert object.
8500: */
8501: struct sqlite3expert {
8502: int iSample; /* Percentage of tables to sample for stat1 */
8503: sqlite3 *db; /* User database */
8504: sqlite3 *dbm; /* In-memory db for this analysis */
8505: sqlite3 *dbv; /* Vtab schema for this analysis */
8506: IdxTable *pTable; /* List of all IdxTable objects */
8507: IdxScan *pScan; /* List of scan objects */
8508: IdxWrite *pWrite; /* List of write objects */
8509: IdxStatement *pStatement; /* List of IdxStatement objects */
8510: int bRun; /* True once analysis has run */
8511: char **pzErrmsg;
8512: int rc; /* Error code from whereinfo hook */
8513: IdxHash hIdx; /* Hash containing all candidate indexes */
8514: char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
8515: };
8516:
8517:
8518: /*
8519: ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
8520: ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
8521: */
8522: static void *idxMalloc(int *pRc, int nByte){
8523: void *pRet;
8524: assert( *pRc==SQLITE_OK );
8525: assert( nByte>0 );
8526: pRet = sqlite3_malloc(nByte);
8527: if( pRet ){
8528: memset(pRet, 0, nByte);
8529: }else{
8530: *pRc = SQLITE_NOMEM;
8531: }
8532: return pRet;
8533: }
8534:
8535: /*
8536: ** Initialize an IdxHash hash table.
8537: */
8538: static void idxHashInit(IdxHash *pHash){
8539: memset(pHash, 0, sizeof(IdxHash));
8540: }
8541:
8542: /*
8543: ** Reset an IdxHash hash table.
8544: */
8545: static void idxHashClear(IdxHash *pHash){
8546: int i;
8547: for(i=0; i<IDX_HASH_SIZE; i++){
8548: IdxHashEntry *pEntry;
8549: IdxHashEntry *pNext;
8550: for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
8551: pNext = pEntry->pHashNext;
8552: sqlite3_free(pEntry->zVal2);
8553: sqlite3_free(pEntry);
8554: }
8555: }
8556: memset(pHash, 0, sizeof(IdxHash));
8557: }
8558:
8559: /*
8560: ** Return the index of the hash bucket that the string specified by the
8561: ** arguments to this function belongs.
8562: */
8563: static int idxHashString(const char *z, int n){
8564: unsigned int ret = 0;
8565: int i;
8566: for(i=0; i<n; i++){
8567: ret += (ret<<3) + (unsigned char)(z[i]);
8568: }
8569: return (int)(ret % IDX_HASH_SIZE);
8570: }
8571:
8572: /*
8573: ** If zKey is already present in the hash table, return non-zero and do
8574: ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
8575: ** the hash table passed as the second argument.
8576: */
8577: static int idxHashAdd(
8578: int *pRc,
8579: IdxHash *pHash,
8580: const char *zKey,
8581: const char *zVal
8582: ){
8583: int nKey = STRLEN(zKey);
8584: int iHash = idxHashString(zKey, nKey);
8585: int nVal = (zVal ? STRLEN(zVal) : 0);
8586: IdxHashEntry *pEntry;
8587: assert( iHash>=0 );
8588: for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8589: if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8590: return 1;
8591: }
8592: }
8593: pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
8594: if( pEntry ){
8595: pEntry->zKey = (char*)&pEntry[1];
8596: memcpy(pEntry->zKey, zKey, nKey);
8597: if( zVal ){
8598: pEntry->zVal = &pEntry->zKey[nKey+1];
8599: memcpy(pEntry->zVal, zVal, nVal);
8600: }
8601: pEntry->pHashNext = pHash->aHash[iHash];
8602: pHash->aHash[iHash] = pEntry;
8603:
8604: pEntry->pNext = pHash->pFirst;
8605: pHash->pFirst = pEntry;
8606: }
8607: return 0;
8608: }
8609:
8610: /*
8611: ** If zKey/nKey is present in the hash table, return a pointer to the
8612: ** hash-entry object.
8613: */
8614: static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
8615: int iHash;
8616: IdxHashEntry *pEntry;
8617: if( nKey<0 ) nKey = STRLEN(zKey);
8618: iHash = idxHashString(zKey, nKey);
8619: assert( iHash>=0 );
8620: for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8621: if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8622: return pEntry;
8623: }
8624: }
8625: return 0;
8626: }
8627:
8628: /*
8629: ** If the hash table contains an entry with a key equal to the string
8630: ** passed as the final two arguments to this function, return a pointer
8631: ** to the payload string. Otherwise, if zKey/nKey is not present in the
8632: ** hash table, return NULL.
8633: */
8634: static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
8635: IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
8636: if( pEntry ) return pEntry->zVal;
8637: return 0;
8638: }
8639:
8640: /*
8641: ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
8642: ** variable to point to a copy of nul-terminated string zColl.
8643: */
8644: static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
8645: IdxConstraint *pNew;
8646: int nColl = STRLEN(zColl);
8647:
8648: assert( *pRc==SQLITE_OK );
8649: pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
8650: if( pNew ){
8651: pNew->zColl = (char*)&pNew[1];
8652: memcpy(pNew->zColl, zColl, nColl+1);
8653: }
8654: return pNew;
8655: }
8656:
8657: /*
8658: ** An error associated with database handle db has just occurred. Pass
8659: ** the error message to callback function xOut.
8660: */
8661: static void idxDatabaseError(
8662: sqlite3 *db, /* Database handle */
8663: char **pzErrmsg /* Write error here */
8664: ){
8665: *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
8666: }
8667:
8668: /*
8669: ** Prepare an SQL statement.
8670: */
8671: static int idxPrepareStmt(
8672: sqlite3 *db, /* Database handle to compile against */
8673: sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8674: char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8675: const char *zSql /* SQL statement to compile */
8676: ){
8677: int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
8678: if( rc!=SQLITE_OK ){
8679: *ppStmt = 0;
8680: idxDatabaseError(db, pzErrmsg);
8681: }
8682: return rc;
8683: }
8684:
8685: /*
8686: ** Prepare an SQL statement using the results of a printf() formatting.
8687: */
8688: static int idxPrintfPrepareStmt(
8689: sqlite3 *db, /* Database handle to compile against */
8690: sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8691: char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8692: const char *zFmt, /* printf() format of SQL statement */
8693: ... /* Trailing printf() arguments */
8694: ){
8695: va_list ap;
8696: int rc;
8697: char *zSql;
8698: va_start(ap, zFmt);
8699: zSql = sqlite3_vmprintf(zFmt, ap);
8700: if( zSql==0 ){
8701: rc = SQLITE_NOMEM;
8702: }else{
8703: rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
8704: sqlite3_free(zSql);
8705: }
8706: va_end(ap);
8707: return rc;
8708: }
8709:
8710:
8711: /*************************************************************************
8712: ** Beginning of virtual table implementation.
8713: */
8714: typedef struct ExpertVtab ExpertVtab;
8715: struct ExpertVtab {
8716: sqlite3_vtab base;
8717: IdxTable *pTab;
8718: sqlite3expert *pExpert;
8719: };
8720:
8721: typedef struct ExpertCsr ExpertCsr;
8722: struct ExpertCsr {
8723: sqlite3_vtab_cursor base;
8724: sqlite3_stmt *pData;
8725: };
8726:
8727: static char *expertDequote(const char *zIn){
8728: int n = STRLEN(zIn);
8729: char *zRet = sqlite3_malloc(n);
8730:
8731: assert( zIn[0]=='\'' );
8732: assert( zIn[n-1]=='\'' );
8733:
8734: if( zRet ){
8735: int iOut = 0;
8736: int iIn = 0;
8737: for(iIn=1; iIn<(n-1); iIn++){
8738: if( zIn[iIn]=='\'' ){
8739: assert( zIn[iIn+1]=='\'' );
8740: iIn++;
8741: }
8742: zRet[iOut++] = zIn[iIn];
8743: }
8744: zRet[iOut] = '\0';
8745: }
8746:
8747: return zRet;
8748: }
8749:
8750: /*
8751: ** This function is the implementation of both the xConnect and xCreate
8752: ** methods of the r-tree virtual table.
8753: **
8754: ** argv[0] -> module name
8755: ** argv[1] -> database name
8756: ** argv[2] -> table name
8757: ** argv[...] -> column names...
8758: */
8759: static int expertConnect(
8760: sqlite3 *db,
8761: void *pAux,
8762: int argc, const char *const*argv,
8763: sqlite3_vtab **ppVtab,
8764: char **pzErr
8765: ){
8766: sqlite3expert *pExpert = (sqlite3expert*)pAux;
8767: ExpertVtab *p = 0;
8768: int rc;
8769:
8770: if( argc!=4 ){
8771: *pzErr = sqlite3_mprintf("internal error!");
8772: rc = SQLITE_ERROR;
8773: }else{
8774: char *zCreateTable = expertDequote(argv[3]);
8775: if( zCreateTable ){
8776: rc = sqlite3_declare_vtab(db, zCreateTable);
8777: if( rc==SQLITE_OK ){
8778: p = idxMalloc(&rc, sizeof(ExpertVtab));
8779: }
8780: if( rc==SQLITE_OK ){
8781: p->pExpert = pExpert;
8782: p->pTab = pExpert->pTable;
8783: assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
8784: }
8785: sqlite3_free(zCreateTable);
8786: }else{
8787: rc = SQLITE_NOMEM;
8788: }
8789: }
8790:
8791: *ppVtab = (sqlite3_vtab*)p;
8792: return rc;
8793: }
8794:
8795: static int expertDisconnect(sqlite3_vtab *pVtab){
8796: ExpertVtab *p = (ExpertVtab*)pVtab;
8797: sqlite3_free(p);
8798: return SQLITE_OK;
8799: }
8800:
8801: static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
8802: ExpertVtab *p = (ExpertVtab*)pVtab;
8803: int rc = SQLITE_OK;
8804: int n = 0;
8805: IdxScan *pScan;
8806: const int opmask =
8807: SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
8808: SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
8809: SQLITE_INDEX_CONSTRAINT_LE;
8810:
8811: pScan = idxMalloc(&rc, sizeof(IdxScan));
8812: if( pScan ){
8813: int i;
8814:
8815: /* Link the new scan object into the list */
8816: pScan->pTab = p->pTab;
8817: pScan->pNextScan = p->pExpert->pScan;
8818: p->pExpert->pScan = pScan;
8819:
8820: /* Add the constraints to the IdxScan object */
8821: for(i=0; i<pIdxInfo->nConstraint; i++){
8822: struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8823: if( pCons->usable
8824: && pCons->iColumn>=0
8825: && p->pTab->aCol[pCons->iColumn].iPk==0
8826: && (pCons->op & opmask)
8827: ){
8828: IdxConstraint *pNew;
8829: const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
8830: pNew = idxNewConstraint(&rc, zColl);
8831: if( pNew ){
8832: pNew->iCol = pCons->iColumn;
8833: if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8834: pNew->pNext = pScan->pEq;
8835: pScan->pEq = pNew;
8836: }else{
8837: pNew->bRange = 1;
8838: pNew->pNext = pScan->pRange;
8839: pScan->pRange = pNew;
8840: }
8841: }
8842: n++;
8843: pIdxInfo->aConstraintUsage[i].argvIndex = n;
8844: }
8845: }
8846:
8847: /* Add the ORDER BY to the IdxScan object */
8848: for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
8849: int iCol = pIdxInfo->aOrderBy[i].iColumn;
8850: if( iCol>=0 ){
8851: IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
8852: if( pNew ){
8853: pNew->iCol = iCol;
8854: pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
8855: pNew->pNext = pScan->pOrder;
8856: pNew->pLink = pScan->pOrder;
8857: pScan->pOrder = pNew;
8858: n++;
8859: }
8860: }
8861: }
8862: }
8863:
8864: pIdxInfo->estimatedCost = 1000000.0 / (n+1);
8865: return rc;
8866: }
8867:
8868: static int expertUpdate(
8869: sqlite3_vtab *pVtab,
8870: int nData,
8871: sqlite3_value **azData,
8872: sqlite_int64 *pRowid
8873: ){
8874: (void)pVtab;
8875: (void)nData;
8876: (void)azData;
8877: (void)pRowid;
8878: return SQLITE_OK;
8879: }
8880:
8881: /*
8882: ** Virtual table module xOpen method.
8883: */
8884: static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8885: int rc = SQLITE_OK;
8886: ExpertCsr *pCsr;
8887: (void)pVTab;
8888: pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
8889: *ppCursor = (sqlite3_vtab_cursor*)pCsr;
8890: return rc;
8891: }
8892:
8893: /*
8894: ** Virtual table module xClose method.
8895: */
8896: static int expertClose(sqlite3_vtab_cursor *cur){
8897: ExpertCsr *pCsr = (ExpertCsr*)cur;
8898: sqlite3_finalize(pCsr->pData);
8899: sqlite3_free(pCsr);
8900: return SQLITE_OK;
8901: }
8902:
8903: /*
8904: ** Virtual table module xEof method.
8905: **
8906: ** Return non-zero if the cursor does not currently point to a valid
8907: ** record (i.e if the scan has finished), or zero otherwise.
8908: */
8909: static int expertEof(sqlite3_vtab_cursor *cur){
8910: ExpertCsr *pCsr = (ExpertCsr*)cur;
8911: return pCsr->pData==0;
8912: }
8913:
8914: /*
8915: ** Virtual table module xNext method.
8916: */
8917: static int expertNext(sqlite3_vtab_cursor *cur){
8918: ExpertCsr *pCsr = (ExpertCsr*)cur;
8919: int rc = SQLITE_OK;
8920:
8921: assert( pCsr->pData );
8922: rc = sqlite3_step(pCsr->pData);
8923: if( rc!=SQLITE_ROW ){
8924: rc = sqlite3_finalize(pCsr->pData);
8925: pCsr->pData = 0;
8926: }else{
8927: rc = SQLITE_OK;
8928: }
8929:
8930: return rc;
8931: }
8932:
8933: /*
8934: ** Virtual table module xRowid method.
8935: */
8936: static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8937: (void)cur;
8938: *pRowid = 0;
8939: return SQLITE_OK;
8940: }
8941:
8942: /*
8943: ** Virtual table module xColumn method.
8944: */
8945: static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
8946: ExpertCsr *pCsr = (ExpertCsr*)cur;
8947: sqlite3_value *pVal;
8948: pVal = sqlite3_column_value(pCsr->pData, i);
8949: if( pVal ){
8950: sqlite3_result_value(ctx, pVal);
8951: }
8952: return SQLITE_OK;
8953: }
8954:
8955: /*
8956: ** Virtual table module xFilter method.
8957: */
8958: static int expertFilter(
8959: sqlite3_vtab_cursor *cur,
8960: int idxNum, const char *idxStr,
8961: int argc, sqlite3_value **argv
8962: ){
8963: ExpertCsr *pCsr = (ExpertCsr*)cur;
8964: ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
8965: sqlite3expert *pExpert = pVtab->pExpert;
8966: int rc;
8967:
8968: (void)idxNum;
8969: (void)idxStr;
8970: (void)argc;
8971: (void)argv;
8972: rc = sqlite3_finalize(pCsr->pData);
8973: pCsr->pData = 0;
8974: if( rc==SQLITE_OK ){
8975: rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
8976: "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
8977: );
8978: }
8979:
8980: if( rc==SQLITE_OK ){
8981: rc = expertNext(cur);
8982: }
8983: return rc;
8984: }
8985:
8986: static int idxRegisterVtab(sqlite3expert *p){
8987: static sqlite3_module expertModule = {
8988: 2, /* iVersion */
8989: expertConnect, /* xCreate - create a table */
8990: expertConnect, /* xConnect - connect to an existing table */
8991: expertBestIndex, /* xBestIndex - Determine search strategy */
8992: expertDisconnect, /* xDisconnect - Disconnect from a table */
8993: expertDisconnect, /* xDestroy - Drop a table */
8994: expertOpen, /* xOpen - open a cursor */
8995: expertClose, /* xClose - close a cursor */
8996: expertFilter, /* xFilter - configure scan constraints */
8997: expertNext, /* xNext - advance a cursor */
8998: expertEof, /* xEof */
8999: expertColumn, /* xColumn - read data */
9000: expertRowid, /* xRowid - read data */
9001: expertUpdate, /* xUpdate - write data */
9002: 0, /* xBegin - begin transaction */
9003: 0, /* xSync - sync transaction */
9004: 0, /* xCommit - commit transaction */
9005: 0, /* xRollback - rollback transaction */
9006: 0, /* xFindFunction - function overloading */
9007: 0, /* xRename - rename the table */
9008: 0, /* xSavepoint */
9009: 0, /* xRelease */
9010: 0, /* xRollbackTo */
9011: 0, /* xShadowName */
9012: };
9013:
9014: return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9015: }
9016: /*
9017: ** End of virtual table implementation.
9018: *************************************************************************/
9019: /*
9020: ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9021: ** is called, set it to the return value of sqlite3_finalize() before
9022: ** returning. Otherwise, discard the sqlite3_finalize() return value.
9023: */
9024: static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9025: int rc = sqlite3_finalize(pStmt);
9026: if( *pRc==SQLITE_OK ) *pRc = rc;
9027: }
9028:
9029: /*
9030: ** Attempt to allocate an IdxTable structure corresponding to table zTab
9031: ** in the main database of connection db. If successful, set (*ppOut) to
9032: ** point to the new object and return SQLITE_OK. Otherwise, return an
9033: ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9034: ** set to point to an error string.
9035: **
9036: ** It is the responsibility of the caller to eventually free either the
9037: ** IdxTable object or error message using sqlite3_free().
9038: */
9039: static int idxGetTableInfo(
9040: sqlite3 *db, /* Database connection to read details from */
9041: const char *zTab, /* Table name */
9042: IdxTable **ppOut, /* OUT: New object (if successful) */
9043: char **pzErrmsg /* OUT: Error message (if not) */
9044: ){
9045: sqlite3_stmt *p1 = 0;
9046: int nCol = 0;
9047: int nTab = STRLEN(zTab);
9048: int nByte = sizeof(IdxTable) + nTab + 1;
9049: IdxTable *pNew = 0;
9050: int rc, rc2;
9051: char *pCsr = 0;
9052: int nPk = 0;
9053:
9054: rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9055: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9056: const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9057: nByte += 1 + STRLEN(zCol);
9058: rc = sqlite3_table_column_metadata(
9059: db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9060: );
9061: nByte += 1 + STRLEN(zCol);
9062: nCol++;
9063: nPk += (sqlite3_column_int(p1, 5)>0);
9064: }
9065: rc2 = sqlite3_reset(p1);
9066: if( rc==SQLITE_OK ) rc = rc2;
9067:
9068: nByte += sizeof(IdxColumn) * nCol;
9069: if( rc==SQLITE_OK ){
9070: pNew = idxMalloc(&rc, nByte);
9071: }
9072: if( rc==SQLITE_OK ){
9073: pNew->aCol = (IdxColumn*)&pNew[1];
9074: pNew->nCol = nCol;
9075: pCsr = (char*)&pNew->aCol[nCol];
9076: }
9077:
9078: nCol = 0;
9079: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9080: const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9081: int nCopy = STRLEN(zCol) + 1;
9082: pNew->aCol[nCol].zName = pCsr;
9083: pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9084: memcpy(pCsr, zCol, nCopy);
9085: pCsr += nCopy;
9086:
9087: rc = sqlite3_table_column_metadata(
9088: db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9089: );
9090: if( rc==SQLITE_OK ){
9091: nCopy = STRLEN(zCol) + 1;
9092: pNew->aCol[nCol].zColl = pCsr;
9093: memcpy(pCsr, zCol, nCopy);
9094: pCsr += nCopy;
9095: }
9096:
9097: nCol++;
9098: }
9099: idxFinalize(&rc, p1);
9100:
9101: if( rc!=SQLITE_OK ){
9102: sqlite3_free(pNew);
9103: pNew = 0;
9104: }else{
9105: pNew->zName = pCsr;
9106: memcpy(pNew->zName, zTab, nTab+1);
9107: }
9108:
9109: *ppOut = pNew;
9110: return rc;
9111: }
9112:
9113: /*
9114: ** This function is a no-op if *pRc is set to anything other than
9115: ** SQLITE_OK when it is called.
9116: **
9117: ** If *pRc is initially set to SQLITE_OK, then the text specified by
9118: ** the printf() style arguments is appended to zIn and the result returned
9119: ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9120: ** zIn before returning.
9121: */
9122: static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9123: va_list ap;
9124: char *zAppend = 0;
9125: char *zRet = 0;
9126: int nIn = zIn ? STRLEN(zIn) : 0;
9127: int nAppend = 0;
9128: va_start(ap, zFmt);
9129: if( *pRc==SQLITE_OK ){
9130: zAppend = sqlite3_vmprintf(zFmt, ap);
9131: if( zAppend ){
9132: nAppend = STRLEN(zAppend);
9133: zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9134: }
9135: if( zAppend && zRet ){
9136: if( nIn ) memcpy(zRet, zIn, nIn);
9137: memcpy(&zRet[nIn], zAppend, nAppend+1);
9138: }else{
9139: sqlite3_free(zRet);
9140: zRet = 0;
9141: *pRc = SQLITE_NOMEM;
9142: }
9143: sqlite3_free(zAppend);
9144: sqlite3_free(zIn);
9145: }
9146: va_end(ap);
9147: return zRet;
9148: }
9149:
9150: /*
9151: ** Return true if zId must be quoted in order to use it as an SQL
9152: ** identifier, or false otherwise.
9153: */
9154: static int idxIdentifierRequiresQuotes(const char *zId){
9155: int i;
9156: for(i=0; zId[i]; i++){
9157: if( !(zId[i]=='_')
9158: && !(zId[i]>='0' && zId[i]<='9')
9159: && !(zId[i]>='a' && zId[i]<='z')
9160: && !(zId[i]>='A' && zId[i]<='Z')
9161: ){
9162: return 1;
9163: }
9164: }
9165: return 0;
9166: }
9167:
9168: /*
9169: ** This function appends an index column definition suitable for constraint
9170: ** pCons to the string passed as zIn and returns the result.
9171: */
9172: static char *idxAppendColDefn(
9173: int *pRc, /* IN/OUT: Error code */
9174: char *zIn, /* Column defn accumulated so far */
9175: IdxTable *pTab, /* Table index will be created on */
9176: IdxConstraint *pCons
9177: ){
9178: char *zRet = zIn;
9179: IdxColumn *p = &pTab->aCol[pCons->iCol];
9180: if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9181:
9182: if( idxIdentifierRequiresQuotes(p->zName) ){
9183: zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9184: }else{
9185: zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9186: }
9187:
9188: if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9189: if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9190: zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9191: }else{
9192: zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9193: }
9194: }
9195:
9196: if( pCons->bDesc ){
9197: zRet = idxAppendText(pRc, zRet, " DESC");
9198: }
9199: return zRet;
9200: }
9201:
9202: /*
9203: ** Search database dbm for an index compatible with the one idxCreateFromCons()
9204: ** would create from arguments pScan, pEq and pTail. If no error occurs and
9205: ** such an index is found, return non-zero. Or, if no such index is found,
9206: ** return zero.
9207: **
9208: ** If an error occurs, set *pRc to an SQLite error code and return zero.
9209: */
9210: static int idxFindCompatible(
9211: int *pRc, /* OUT: Error code */
9212: sqlite3* dbm, /* Database to search */
9213: IdxScan *pScan, /* Scan for table to search for index on */
9214: IdxConstraint *pEq, /* List of == constraints */
9215: IdxConstraint *pTail /* List of range constraints */
9216: ){
9217: const char *zTbl = pScan->pTab->zName;
9218: sqlite3_stmt *pIdxList = 0;
9219: IdxConstraint *pIter;
9220: int nEq = 0; /* Number of elements in pEq */
9221: int rc;
9222:
9223: /* Count the elements in list pEq */
9224: for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
9225:
9226: rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
9227: while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
9228: int bMatch = 1;
9229: IdxConstraint *pT = pTail;
9230: sqlite3_stmt *pInfo = 0;
9231: const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
9232:
9233: /* Zero the IdxConstraint.bFlag values in the pEq list */
9234: for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
9235:
9236: rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
9237: while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
9238: int iIdx = sqlite3_column_int(pInfo, 0);
9239: int iCol = sqlite3_column_int(pInfo, 1);
9240: const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
9241:
9242: if( iIdx<nEq ){
9243: for(pIter=pEq; pIter; pIter=pIter->pLink){
9244: if( pIter->bFlag ) continue;
9245: if( pIter->iCol!=iCol ) continue;
9246: if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
9247: pIter->bFlag = 1;
9248: break;
9249: }
9250: if( pIter==0 ){
9251: bMatch = 0;
9252: break;
9253: }
9254: }else{
9255: if( pT ){
9256: if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
9257: bMatch = 0;
9258: break;
9259: }
9260: pT = pT->pLink;
9261: }
9262: }
9263: }
9264: idxFinalize(&rc, pInfo);
9265:
9266: if( rc==SQLITE_OK && bMatch ){
9267: sqlite3_finalize(pIdxList);
9268: return 1;
9269: }
9270: }
9271: idxFinalize(&rc, pIdxList);
9272:
9273: *pRc = rc;
9274: return 0;
9275: }
9276:
9277: static int idxCreateFromCons(
9278: sqlite3expert *p,
9279: IdxScan *pScan,
9280: IdxConstraint *pEq,
9281: IdxConstraint *pTail
9282: ){
9283: sqlite3 *dbm = p->dbm;
9284: int rc = SQLITE_OK;
9285: if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
9286: IdxTable *pTab = pScan->pTab;
9287: char *zCols = 0;
9288: char *zIdx = 0;
9289: IdxConstraint *pCons;
9290: unsigned int h = 0;
9291: const char *zFmt;
9292:
9293: for(pCons=pEq; pCons; pCons=pCons->pLink){
9294: zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9295: }
9296: for(pCons=pTail; pCons; pCons=pCons->pLink){
9297: zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9298: }
9299:
9300: if( rc==SQLITE_OK ){
9301: /* Hash the list of columns to come up with a name for the index */
9302: const char *zTable = pScan->pTab->zName;
9303: char *zName; /* Index name */
9304: int i;
9305: for(i=0; zCols[i]; i++){
9306: h += ((h<<3) + zCols[i]);
9307: }
9308: zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
9309: if( zName==0 ){
9310: rc = SQLITE_NOMEM;
9311: }else{
9312: if( idxIdentifierRequiresQuotes(zTable) ){
9313: zFmt = "CREATE INDEX '%q' ON %Q(%s)";
9314: }else{
9315: zFmt = "CREATE INDEX %s ON %s(%s)";
9316: }
9317: zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
9318: if( !zIdx ){
9319: rc = SQLITE_NOMEM;
9320: }else{
9321: rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
9322: idxHashAdd(&rc, &p->hIdx, zName, zIdx);
9323: }
9324: sqlite3_free(zName);
9325: sqlite3_free(zIdx);
9326: }
9327: }
9328:
9329: sqlite3_free(zCols);
9330: }
9331: return rc;
9332: }
9333:
9334: /*
9335: ** Return true if list pList (linked by IdxConstraint.pLink) contains
9336: ** a constraint compatible with *p. Otherwise return false.
9337: */
9338: static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
9339: IdxConstraint *pCmp;
9340: for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
9341: if( p->iCol==pCmp->iCol ) return 1;
9342: }
9343: return 0;
9344: }
9345:
9346: static int idxCreateFromWhere(
9347: sqlite3expert *p,
9348: IdxScan *pScan, /* Create indexes for this scan */
9349: IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
9350: ){
9351: IdxConstraint *p1 = 0;
9352: IdxConstraint *pCon;
9353: int rc;
9354:
9355: /* Gather up all the == constraints. */
9356: for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
9357: if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9358: pCon->pLink = p1;
9359: p1 = pCon;
9360: }
9361: }
9362:
9363: /* Create an index using the == constraints collected above. And the
9364: ** range constraint/ORDER BY terms passed in by the caller, if any. */
9365: rc = idxCreateFromCons(p, pScan, p1, pTail);
9366:
9367: /* If no range/ORDER BY passed by the caller, create a version of the
9368: ** index for each range constraint. */
9369: if( pTail==0 ){
9370: for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
9371: assert( pCon->pLink==0 );
9372: if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9373: rc = idxCreateFromCons(p, pScan, p1, pCon);
9374: }
9375: }
9376: }
9377:
9378: return rc;
9379: }
9380:
9381: /*
9382: ** Create candidate indexes in database [dbm] based on the data in
9383: ** linked-list pScan.
9384: */
9385: static int idxCreateCandidates(sqlite3expert *p){
9386: int rc = SQLITE_OK;
9387: IdxScan *pIter;
9388:
9389: for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
9390: rc = idxCreateFromWhere(p, pIter, 0);
9391: if( rc==SQLITE_OK && pIter->pOrder ){
9392: rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
9393: }
9394: }
9395:
9396: return rc;
9397: }
9398:
9399: /*
9400: ** Free all elements of the linked list starting at pConstraint.
9401: */
9402: static void idxConstraintFree(IdxConstraint *pConstraint){
9403: IdxConstraint *pNext;
9404: IdxConstraint *p;
9405:
9406: for(p=pConstraint; p; p=pNext){
9407: pNext = p->pNext;
9408: sqlite3_free(p);
9409: }
9410: }
9411:
9412: /*
9413: ** Free all elements of the linked list starting from pScan up until pLast
9414: ** (pLast is not freed).
9415: */
9416: static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
9417: IdxScan *p;
9418: IdxScan *pNext;
9419: for(p=pScan; p!=pLast; p=pNext){
9420: pNext = p->pNextScan;
9421: idxConstraintFree(p->pOrder);
9422: idxConstraintFree(p->pEq);
9423: idxConstraintFree(p->pRange);
9424: sqlite3_free(p);
9425: }
9426: }
9427:
9428: /*
9429: ** Free all elements of the linked list starting from pStatement up
9430: ** until pLast (pLast is not freed).
9431: */
9432: static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
9433: IdxStatement *p;
9434: IdxStatement *pNext;
9435: for(p=pStatement; p!=pLast; p=pNext){
9436: pNext = p->pNext;
9437: sqlite3_free(p->zEQP);
9438: sqlite3_free(p->zIdx);
9439: sqlite3_free(p);
9440: }
9441: }
9442:
9443: /*
9444: ** Free the linked list of IdxTable objects starting at pTab.
9445: */
9446: static void idxTableFree(IdxTable *pTab){
9447: IdxTable *pIter;
9448: IdxTable *pNext;
9449: for(pIter=pTab; pIter; pIter=pNext){
9450: pNext = pIter->pNext;
9451: sqlite3_free(pIter);
9452: }
9453: }
9454:
9455: /*
9456: ** Free the linked list of IdxWrite objects starting at pTab.
9457: */
9458: static void idxWriteFree(IdxWrite *pTab){
9459: IdxWrite *pIter;
9460: IdxWrite *pNext;
9461: for(pIter=pTab; pIter; pIter=pNext){
9462: pNext = pIter->pNext;
9463: sqlite3_free(pIter);
9464: }
9465: }
9466:
9467:
9468:
9469: /*
9470: ** This function is called after candidate indexes have been created. It
9471: ** runs all the queries to see which indexes they prefer, and populates
9472: ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
9473: */
9474: int idxFindIndexes(
9475: sqlite3expert *p,
9476: char **pzErr /* OUT: Error message (sqlite3_malloc) */
9477: ){
9478: IdxStatement *pStmt;
9479: sqlite3 *dbm = p->dbm;
9480: int rc = SQLITE_OK;
9481:
9482: IdxHash hIdx;
9483: idxHashInit(&hIdx);
9484:
9485: for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
9486: IdxHashEntry *pEntry;
9487: sqlite3_stmt *pExplain = 0;
9488: idxHashClear(&hIdx);
9489: rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
9490: "EXPLAIN QUERY PLAN %s", pStmt->zSql
9491: );
9492: while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
9493: /* int iId = sqlite3_column_int(pExplain, 0); */
9494: /* int iParent = sqlite3_column_int(pExplain, 1); */
9495: /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
9496: const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
9497: int nDetail;
9498: int i;
9499:
9500: if( !zDetail ) continue;
9501: nDetail = STRLEN(zDetail);
9502:
9503: for(i=0; i<nDetail; i++){
9504: const char *zIdx = 0;
9505: if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
9506: zIdx = &zDetail[i+13];
9507: }else if( i+22<nDetail
9508: && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
9509: ){
9510: zIdx = &zDetail[i+22];
9511: }
9512: if( zIdx ){
9513: const char *zSql;
9514: int nIdx = 0;
9515: while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
9516: nIdx++;
9517: }
9518: zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
9519: if( zSql ){
9520: idxHashAdd(&rc, &hIdx, zSql, 0);
9521: if( rc ) goto find_indexes_out;
9522: }
9523: break;
9524: }
9525: }
9526:
9527: if( zDetail[0]!='-' ){
9528: pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
9529: }
9530: }
9531:
9532: for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
9533: pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
9534: }
9535:
9536: idxFinalize(&rc, pExplain);
9537: }
9538:
9539: find_indexes_out:
9540: idxHashClear(&hIdx);
9541: return rc;
9542: }
9543:
9544: static int idxAuthCallback(
9545: void *pCtx,
9546: int eOp,
9547: const char *z3,
9548: const char *z4,
9549: const char *zDb,
9550: const char *zTrigger
9551: ){
9552: int rc = SQLITE_OK;
9553: (void)z4;
9554: (void)zTrigger;
9555: if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
9556: if( sqlite3_stricmp(zDb, "main")==0 ){
9557: sqlite3expert *p = (sqlite3expert*)pCtx;
9558: IdxTable *pTab;
9559: for(pTab=p->pTable; pTab; pTab=pTab->pNext){
9560: if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
9561: }
9562: if( pTab ){
9563: IdxWrite *pWrite;
9564: for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
9565: if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
9566: }
9567: if( pWrite==0 ){
9568: pWrite = idxMalloc(&rc, sizeof(IdxWrite));
9569: if( rc==SQLITE_OK ){
9570: pWrite->pTab = pTab;
9571: pWrite->eOp = eOp;
9572: pWrite->pNext = p->pWrite;
9573: p->pWrite = pWrite;
9574: }
9575: }
9576: }
9577: }
9578: }
9579: return rc;
9580: }
9581:
9582: static int idxProcessOneTrigger(
9583: sqlite3expert *p,
9584: IdxWrite *pWrite,
9585: char **pzErr
9586: ){
9587: static const char *zInt = UNIQUE_TABLE_NAME;
9588: static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
9589: IdxTable *pTab = pWrite->pTab;
9590: const char *zTab = pTab->zName;
9591: const char *zSql =
9592: "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
9593: "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
9594: "ORDER BY type;";
9595: sqlite3_stmt *pSelect = 0;
9596: int rc = SQLITE_OK;
9597: char *zWrite = 0;
9598:
9599: /* Create the table and its triggers in the temp schema */
9600: rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
9601: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
9602: const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
9603: rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
9604: }
9605: idxFinalize(&rc, pSelect);
9606:
9607: /* Rename the table in the temp schema to zInt */
9608: if( rc==SQLITE_OK ){
9609: char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
9610: if( z==0 ){
9611: rc = SQLITE_NOMEM;
9612: }else{
9613: rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
9614: sqlite3_free(z);
9615: }
9616: }
9617:
9618: switch( pWrite->eOp ){
9619: case SQLITE_INSERT: {
9620: int i;
9621: zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
9622: for(i=0; i<pTab->nCol; i++){
9623: zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
9624: }
9625: zWrite = idxAppendText(&rc, zWrite, ")");
9626: break;
9627: }
9628: case SQLITE_UPDATE: {
9629: int i;
9630: zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
9631: for(i=0; i<pTab->nCol; i++){
9632: zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
9633: pTab->aCol[i].zName
9634: );
9635: }
9636: break;
9637: }
9638: default: {
9639: assert( pWrite->eOp==SQLITE_DELETE );
9640: if( rc==SQLITE_OK ){
9641: zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
9642: if( zWrite==0 ) rc = SQLITE_NOMEM;
9643: }
9644: }
9645: }
9646:
9647: if( rc==SQLITE_OK ){
9648: sqlite3_stmt *pX = 0;
9649: rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
9650: idxFinalize(&rc, pX);
9651: if( rc!=SQLITE_OK ){
9652: idxDatabaseError(p->dbv, pzErr);
9653: }
9654: }
9655: sqlite3_free(zWrite);
9656:
9657: if( rc==SQLITE_OK ){
9658: rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
9659: }
9660:
9661: return rc;
9662: }
9663:
9664: static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
9665: int rc = SQLITE_OK;
9666: IdxWrite *pEnd = 0;
9667: IdxWrite *pFirst = p->pWrite;
9668:
9669: while( rc==SQLITE_OK && pFirst!=pEnd ){
9670: IdxWrite *pIter;
9671: for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
9672: rc = idxProcessOneTrigger(p, pIter, pzErr);
9673: }
9674: pEnd = pFirst;
9675: pFirst = p->pWrite;
9676: }
9677:
9678: return rc;
9679: }
9680:
9681:
9682: static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9683: int rc = idxRegisterVtab(p);
9684: sqlite3_stmt *pSchema = 0;
9685:
9686: /* For each table in the main db schema:
9687: **
9688: ** 1) Add an entry to the p->pTable list, and
9689: ** 2) Create the equivalent virtual table in dbv.
9690: */
9691: rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9692: "SELECT type, name, sql, 1 FROM sqlite_schema "
9693: "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9694: " UNION ALL "
9695: "SELECT type, name, sql, 2 FROM sqlite_schema "
9696: "WHERE type = 'trigger'"
9697: " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9698: "ORDER BY 4, 1"
9699: );
9700: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9701: const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9702: const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9703: const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9704:
9705: if( zType[0]=='v' || zType[1]=='r' ){
9706: rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9707: }else{
9708: IdxTable *pTab;
9709: rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9710: if( rc==SQLITE_OK ){
9711: int i;
9712: char *zInner = 0;
9713: char *zOuter = 0;
9714: pTab->pNext = p->pTable;
9715: p->pTable = pTab;
9716:
9717: /* The statement the vtab will pass to sqlite3_declare_vtab() */
9718: zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9719: for(i=0; i<pTab->nCol; i++){
9720: zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9721: (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9722: );
9723: }
9724: zInner = idxAppendText(&rc, zInner, ")");
9725:
9726: /* The CVT statement to create the vtab */
9727: zOuter = idxAppendText(&rc, 0,
9728: "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9729: );
9730: if( rc==SQLITE_OK ){
9731: rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9732: }
9733: sqlite3_free(zInner);
9734: sqlite3_free(zOuter);
9735: }
9736: }
9737: }
9738: idxFinalize(&rc, pSchema);
9739: return rc;
9740: }
9741:
9742: struct IdxSampleCtx {
9743: int iTarget;
9744: double target; /* Target nRet/nRow value */
9745: double nRow; /* Number of rows seen */
9746: double nRet; /* Number of rows returned */
9747: };
9748:
9749: static void idxSampleFunc(
9750: sqlite3_context *pCtx,
9751: int argc,
9752: sqlite3_value **argv
9753: ){
9754: struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
9755: int bRet;
9756:
9757: (void)argv;
9758: assert( argc==0 );
9759: if( p->nRow==0.0 ){
9760: bRet = 1;
9761: }else{
9762: bRet = (p->nRet / p->nRow) <= p->target;
9763: if( bRet==0 ){
9764: unsigned short rnd;
9765: sqlite3_randomness(2, (void*)&rnd);
9766: bRet = ((int)rnd % 100) <= p->iTarget;
9767: }
9768: }
9769:
9770: sqlite3_result_int(pCtx, bRet);
9771: p->nRow += 1.0;
9772: p->nRet += (double)bRet;
9773: }
9774:
9775: struct IdxRemCtx {
9776: int nSlot;
9777: struct IdxRemSlot {
9778: int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
9779: i64 iVal; /* SQLITE_INTEGER value */
9780: double rVal; /* SQLITE_FLOAT value */
9781: int nByte; /* Bytes of space allocated at z */
9782: int n; /* Size of buffer z */
9783: char *z; /* SQLITE_TEXT/BLOB value */
9784: } aSlot[1];
9785: };
9786:
9787: /*
9788: ** Implementation of scalar function rem().
9789: */
9790: static void idxRemFunc(
9791: sqlite3_context *pCtx,
9792: int argc,
9793: sqlite3_value **argv
9794: ){
9795: struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
9796: struct IdxRemSlot *pSlot;
9797: int iSlot;
9798: assert( argc==2 );
9799:
9800: iSlot = sqlite3_value_int(argv[0]);
9801: assert( iSlot<=p->nSlot );
9802: pSlot = &p->aSlot[iSlot];
9803:
9804: switch( pSlot->eType ){
9805: case SQLITE_NULL:
9806: /* no-op */
9807: break;
9808:
9809: case SQLITE_INTEGER:
9810: sqlite3_result_int64(pCtx, pSlot->iVal);
9811: break;
9812:
9813: case SQLITE_FLOAT:
9814: sqlite3_result_double(pCtx, pSlot->rVal);
9815: break;
9816:
9817: case SQLITE_BLOB:
9818: sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9819: break;
9820:
9821: case SQLITE_TEXT:
9822: sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9823: break;
9824: }
9825:
9826: pSlot->eType = sqlite3_value_type(argv[1]);
9827: switch( pSlot->eType ){
9828: case SQLITE_NULL:
9829: /* no-op */
9830: break;
9831:
9832: case SQLITE_INTEGER:
9833: pSlot->iVal = sqlite3_value_int64(argv[1]);
9834: break;
9835:
9836: case SQLITE_FLOAT:
9837: pSlot->rVal = sqlite3_value_double(argv[1]);
9838: break;
9839:
9840: case SQLITE_BLOB:
9841: case SQLITE_TEXT: {
9842: int nByte = sqlite3_value_bytes(argv[1]);
9843: if( nByte>pSlot->nByte ){
9844: char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
9845: if( zNew==0 ){
9846: sqlite3_result_error_nomem(pCtx);
9847: return;
9848: }
9849: pSlot->nByte = nByte*2;
9850: pSlot->z = zNew;
9851: }
9852: pSlot->n = nByte;
9853: if( pSlot->eType==SQLITE_BLOB ){
9854: memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
9855: }else{
9856: memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
9857: }
9858: break;
9859: }
9860: }
9861: }
9862:
9863: static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
9864: int rc = SQLITE_OK;
9865: const char *zMax =
9866: "SELECT max(i.seqno) FROM "
9867: " sqlite_schema AS s, "
9868: " pragma_index_list(s.name) AS l, "
9869: " pragma_index_info(l.name) AS i "
9870: "WHERE s.type = 'table'";
9871: sqlite3_stmt *pMax = 0;
9872:
9873: *pnMax = 0;
9874: rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
9875: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
9876: *pnMax = sqlite3_column_int(pMax, 0) + 1;
9877: }
9878: idxFinalize(&rc, pMax);
9879:
9880: return rc;
9881: }
9882:
9883: static int idxPopulateOneStat1(
9884: sqlite3expert *p,
9885: sqlite3_stmt *pIndexXInfo,
9886: sqlite3_stmt *pWriteStat,
9887: const char *zTab,
9888: const char *zIdx,
9889: char **pzErr
9890: ){
9891: char *zCols = 0;
9892: char *zOrder = 0;
9893: char *zQuery = 0;
9894: int nCol = 0;
9895: int i;
9896: sqlite3_stmt *pQuery = 0;
9897: int *aStat = 0;
9898: int rc = SQLITE_OK;
9899:
9900: assert( p->iSample>0 );
9901:
9902: /* Formulate the query text */
9903: sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
9904: while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
9905: const char *zComma = zCols==0 ? "" : ", ";
9906: const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
9907: const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
9908: zCols = idxAppendText(&rc, zCols,
9909: "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
9910: );
9911: zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
9912: }
9913: sqlite3_reset(pIndexXInfo);
9914: if( rc==SQLITE_OK ){
9915: if( p->iSample==100 ){
9916: zQuery = sqlite3_mprintf(
9917: "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
9918: );
9919: }else{
9920: zQuery = sqlite3_mprintf(
9921: "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
9922: );
9923: }
9924: }
9925: sqlite3_free(zCols);
9926: sqlite3_free(zOrder);
9927:
9928: /* Formulate the query text */
9929: if( rc==SQLITE_OK ){
9930: sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9931: rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
9932: }
9933: sqlite3_free(zQuery);
9934:
9935: if( rc==SQLITE_OK ){
9936: aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
9937: }
9938: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9939: IdxHashEntry *pEntry;
9940: char *zStat = 0;
9941: for(i=0; i<=nCol; i++) aStat[i] = 1;
9942: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9943: aStat[0]++;
9944: for(i=0; i<nCol; i++){
9945: if( sqlite3_column_int(pQuery, i)==0 ) break;
9946: }
9947: for(/*no-op*/; i<nCol; i++){
9948: aStat[i+1]++;
9949: }
9950: }
9951:
9952: if( rc==SQLITE_OK ){
9953: int s0 = aStat[0];
9954: zStat = sqlite3_mprintf("%d", s0);
9955: if( zStat==0 ) rc = SQLITE_NOMEM;
9956: for(i=1; rc==SQLITE_OK && i<=nCol; i++){
9957: zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
9958: }
9959: }
9960:
9961: if( rc==SQLITE_OK ){
9962: sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
9963: sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
9964: sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
9965: sqlite3_step(pWriteStat);
9966: rc = sqlite3_reset(pWriteStat);
9967: }
9968:
9969: pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
9970: if( pEntry ){
9971: assert( pEntry->zVal2==0 );
9972: pEntry->zVal2 = zStat;
9973: }else{
9974: sqlite3_free(zStat);
9975: }
9976: }
9977: sqlite3_free(aStat);
9978: idxFinalize(&rc, pQuery);
9979:
9980: return rc;
9981: }
9982:
9983: static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
9984: int rc;
9985: char *zSql;
9986:
9987: rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9988: if( rc!=SQLITE_OK ) return rc;
9989:
9990: zSql = sqlite3_mprintf(
9991: "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
9992: );
9993: if( zSql==0 ) return SQLITE_NOMEM;
9994: rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
9995: sqlite3_free(zSql);
9996:
9997: return rc;
9998: }
9999:
10000: /*
10001: ** This function is called as part of sqlite3_expert_analyze(). Candidate
10002: ** indexes have already been created in database sqlite3expert.dbm, this
10003: ** function populates sqlite_stat1 table in the same database.
10004: **
10005: ** The stat1 data is generated by querying the
10006: */
10007: static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10008: int rc = SQLITE_OK;
10009: int nMax =0;
10010: struct IdxRemCtx *pCtx = 0;
10011: struct IdxSampleCtx samplectx;
10012: int i;
10013: i64 iPrev = -100000;
10014: sqlite3_stmt *pAllIndex = 0;
10015: sqlite3_stmt *pIndexXInfo = 0;
10016: sqlite3_stmt *pWrite = 0;
10017:
10018: const char *zAllIndex =
10019: "SELECT s.rowid, s.name, l.name FROM "
10020: " sqlite_schema AS s, "
10021: " pragma_index_list(s.name) AS l "
10022: "WHERE s.type = 'table'";
10023: const char *zIndexXInfo =
10024: "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10025: const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10026:
10027: /* If iSample==0, no sqlite_stat1 data is required. */
10028: if( p->iSample==0 ) return SQLITE_OK;
10029:
10030: rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10031: if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10032:
10033: rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10034:
10035: if( rc==SQLITE_OK ){
10036: int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10037: pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10038: }
10039:
10040: if( rc==SQLITE_OK ){
10041: sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10042: rc = sqlite3_create_function(
10043: dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10044: );
10045: }
10046: if( rc==SQLITE_OK ){
10047: rc = sqlite3_create_function(
10048: p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10049: );
10050: }
10051:
10052: if( rc==SQLITE_OK ){
10053: pCtx->nSlot = nMax+1;
10054: rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10055: }
10056: if( rc==SQLITE_OK ){
10057: rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10058: }
10059: if( rc==SQLITE_OK ){
10060: rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
10061: }
10062:
10063: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10064: i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10065: const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10066: const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10067: if( p->iSample<100 && iPrev!=iRowid ){
10068: samplectx.target = (double)p->iSample / 100.0;
10069: samplectx.iTarget = p->iSample;
10070: samplectx.nRow = 0.0;
10071: samplectx.nRet = 0.0;
10072: rc = idxBuildSampleTable(p, zTab);
10073: if( rc!=SQLITE_OK ) break;
10074: }
10075: rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
10076: iPrev = iRowid;
10077: }
10078: if( rc==SQLITE_OK && p->iSample<100 ){
10079: rc = sqlite3_exec(p->dbv,
10080: "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
10081: );
10082: }
10083:
10084: idxFinalize(&rc, pAllIndex);
10085: idxFinalize(&rc, pIndexXInfo);
10086: idxFinalize(&rc, pWrite);
10087:
10088: if( pCtx ){
10089: for(i=0; i<pCtx->nSlot; i++){
10090: sqlite3_free(pCtx->aSlot[i].z);
10091: }
10092: sqlite3_free(pCtx);
10093: }
10094:
10095: if( rc==SQLITE_OK ){
10096: rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
10097: }
10098:
10099: sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10100: return rc;
10101: }
10102:
10103: /*
10104: ** Allocate a new sqlite3expert object.
10105: */
10106: sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
10107: int rc = SQLITE_OK;
10108: sqlite3expert *pNew;
10109:
10110: pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
10111:
10112: /* Open two in-memory databases to work with. The "vtab database" (dbv)
10113: ** will contain a virtual table corresponding to each real table in
10114: ** the user database schema, and a copy of each view. It is used to
10115: ** collect information regarding the WHERE, ORDER BY and other clauses
10116: ** of the user's query.
10117: */
10118: if( rc==SQLITE_OK ){
10119: pNew->db = db;
10120: pNew->iSample = 100;
10121: rc = sqlite3_open(":memory:", &pNew->dbv);
10122: }
10123: if( rc==SQLITE_OK ){
10124: rc = sqlite3_open(":memory:", &pNew->dbm);
10125: if( rc==SQLITE_OK ){
10126: sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10127: }
10128: }
10129:
10130:
10131: /* Copy the entire schema of database [db] into [dbm]. */
10132: if( rc==SQLITE_OK ){
10133: sqlite3_stmt *pSql;
10134: rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10135: "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10136: " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10137: );
10138: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10139: const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10140: rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10141: }
10142: idxFinalize(&rc, pSql);
10143: }
10144:
10145: /* Create the vtab schema */
10146: if( rc==SQLITE_OK ){
10147: rc = idxCreateVtabSchema(pNew, pzErrmsg);
10148: }
10149:
10150: /* Register the auth callback with dbv */
10151: if( rc==SQLITE_OK ){
10152: sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10153: }
10154:
10155: /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10156: ** return the new sqlite3expert handle. */
10157: if( rc!=SQLITE_OK ){
10158: sqlite3_expert_destroy(pNew);
10159: pNew = 0;
10160: }
10161: return pNew;
10162: }
10163:
10164: /*
10165: ** Configure an sqlite3expert object.
10166: */
10167: int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10168: int rc = SQLITE_OK;
10169: va_list ap;
10170: va_start(ap, op);
10171: switch( op ){
10172: case EXPERT_CONFIG_SAMPLE: {
10173: int iVal = va_arg(ap, int);
10174: if( iVal<0 ) iVal = 0;
10175: if( iVal>100 ) iVal = 100;
10176: p->iSample = iVal;
10177: break;
10178: }
10179: default:
10180: rc = SQLITE_NOTFOUND;
10181: break;
10182: }
10183:
10184: va_end(ap);
10185: return rc;
10186: }
10187:
10188: /*
10189: ** Add an SQL statement to the analysis.
10190: */
10191: int sqlite3_expert_sql(
10192: sqlite3expert *p, /* From sqlite3_expert_new() */
10193: const char *zSql, /* SQL statement to add */
10194: char **pzErr /* OUT: Error message (if any) */
10195: ){
10196: IdxScan *pScanOrig = p->pScan;
10197: IdxStatement *pStmtOrig = p->pStatement;
10198: int rc = SQLITE_OK;
10199: const char *zStmt = zSql;
10200:
10201: if( p->bRun ) return SQLITE_MISUSE;
10202:
10203: while( rc==SQLITE_OK && zStmt && zStmt[0] ){
10204: sqlite3_stmt *pStmt = 0;
10205: rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
10206: if( rc==SQLITE_OK ){
10207: if( pStmt ){
10208: IdxStatement *pNew;
10209: const char *z = sqlite3_sql(pStmt);
10210: int n = STRLEN(z);
10211: pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
10212: if( rc==SQLITE_OK ){
10213: pNew->zSql = (char*)&pNew[1];
10214: memcpy(pNew->zSql, z, n+1);
10215: pNew->pNext = p->pStatement;
10216: if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
10217: p->pStatement = pNew;
10218: }
10219: sqlite3_finalize(pStmt);
10220: }
10221: }else{
10222: idxDatabaseError(p->dbv, pzErr);
10223: }
10224: }
10225:
10226: if( rc!=SQLITE_OK ){
10227: idxScanFree(p->pScan, pScanOrig);
10228: idxStatementFree(p->pStatement, pStmtOrig);
10229: p->pScan = pScanOrig;
10230: p->pStatement = pStmtOrig;
10231: }
10232:
10233: return rc;
10234: }
10235:
10236: int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
10237: int rc;
10238: IdxHashEntry *pEntry;
10239:
10240: /* Do trigger processing to collect any extra IdxScan structures */
10241: rc = idxProcessTriggers(p, pzErr);
10242:
10243: /* Create candidate indexes within the in-memory database file */
10244: if( rc==SQLITE_OK ){
10245: rc = idxCreateCandidates(p);
10246: }
10247:
10248: /* Generate the stat1 data */
10249: if( rc==SQLITE_OK ){
10250: rc = idxPopulateStat1(p, pzErr);
10251: }
10252:
10253: /* Formulate the EXPERT_REPORT_CANDIDATES text */
10254: for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10255: p->zCandidates = idxAppendText(&rc, p->zCandidates,
10256: "%s;%s%s\n", pEntry->zVal,
10257: pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
10258: );
10259: }
10260:
10261: /* Figure out which of the candidate indexes are preferred by the query
10262: ** planner and report the results to the user. */
10263: if( rc==SQLITE_OK ){
10264: rc = idxFindIndexes(p, pzErr);
10265: }
10266:
10267: if( rc==SQLITE_OK ){
10268: p->bRun = 1;
10269: }
10270: return rc;
10271: }
10272:
10273: /*
10274: ** Return the total number of statements that have been added to this
10275: ** sqlite3expert using sqlite3_expert_sql().
10276: */
10277: int sqlite3_expert_count(sqlite3expert *p){
10278: int nRet = 0;
10279: if( p->pStatement ) nRet = p->pStatement->iId+1;
10280: return nRet;
10281: }
10282:
10283: /*
10284: ** Return a component of the report.
10285: */
10286: const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
10287: const char *zRet = 0;
10288: IdxStatement *pStmt;
10289:
10290: if( p->bRun==0 ) return 0;
10291: for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
10292: switch( eReport ){
10293: case EXPERT_REPORT_SQL:
10294: if( pStmt ) zRet = pStmt->zSql;
10295: break;
10296: case EXPERT_REPORT_INDEXES:
10297: if( pStmt ) zRet = pStmt->zIdx;
10298: break;
10299: case EXPERT_REPORT_PLAN:
10300: if( pStmt ) zRet = pStmt->zEQP;
10301: break;
10302: case EXPERT_REPORT_CANDIDATES:
10303: zRet = p->zCandidates;
10304: break;
10305: }
10306: return zRet;
10307: }
10308:
10309: /*
10310: ** Free an sqlite3expert object.
10311: */
10312: void sqlite3_expert_destroy(sqlite3expert *p){
10313: if( p ){
10314: sqlite3_close(p->dbm);
10315: sqlite3_close(p->dbv);
10316: idxScanFree(p->pScan, 0);
10317: idxStatementFree(p->pStatement, 0);
10318: idxTableFree(p->pTable);
10319: idxWriteFree(p->pWrite);
10320: idxHashClear(&p->hIdx);
10321: sqlite3_free(p->zCandidates);
10322: sqlite3_free(p);
10323: }
10324: }
10325:
10326: #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10327:
10328: /************************* End ../ext/expert/sqlite3expert.c ********************/
10329:
10330: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
10331: /************************* Begin ../ext/misc/dbdata.c ******************/
10332: /*
10333: ** 2019-04-17
10334: **
10335: ** The author disclaims copyright to this source code. In place of
10336: ** a legal notice, here is a blessing:
10337: **
10338: ** May you do good and not evil.
10339: ** May you find forgiveness for yourself and forgive others.
10340: ** May you share freely, never taking more than you give.
10341: **
10342: ******************************************************************************
10343: **
10344: ** This file contains an implementation of two eponymous virtual tables,
10345: ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
10346: ** "sqlite_dbpage" eponymous virtual table be available.
10347: **
10348: ** SQLITE_DBDATA:
10349: ** sqlite_dbdata is used to extract data directly from a database b-tree
10350: ** page and its associated overflow pages, bypassing the b-tree layer.
10351: ** The table schema is equivalent to:
10352: **
10353: ** CREATE TABLE sqlite_dbdata(
10354: ** pgno INTEGER,
10355: ** cell INTEGER,
10356: ** field INTEGER,
10357: ** value ANY,
10358: ** schema TEXT HIDDEN
10359: ** );
10360: **
10361: ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
10362: ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
10363: ** "schema".
10364: **
10365: ** Each page of the database is inspected. If it cannot be interpreted as
10366: ** a b-tree page, or if it is a b-tree page containing 0 entries, the
10367: ** sqlite_dbdata table contains no rows for that page. Otherwise, the
10368: ** table contains one row for each field in the record associated with
10369: ** each cell on the page. For intkey b-trees, the key value is stored in
10370: ** field -1.
10371: **
10372: ** For example, for the database:
10373: **
10374: ** CREATE TABLE t1(a, b); -- root page is page 2
10375: ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
10376: ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
10377: **
10378: ** the sqlite_dbdata table contains, as well as from entries related to
10379: ** page 1, content equivalent to:
10380: **
10381: ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
10382: ** (2, 0, -1, 5 ),
10383: ** (2, 0, 0, 'v' ),
10384: ** (2, 0, 1, 'five'),
10385: ** (2, 1, -1, 10 ),
10386: ** (2, 1, 0, 'x' ),
10387: ** (2, 1, 1, 'ten' );
10388: **
10389: ** If database corruption is encountered, this module does not report an
10390: ** error. Instead, it attempts to extract as much data as possible and
10391: ** ignores the corruption.
10392: **
10393: ** SQLITE_DBPTR:
10394: ** The sqlite_dbptr table has the following schema:
10395: **
10396: ** CREATE TABLE sqlite_dbptr(
10397: ** pgno INTEGER,
10398: ** child INTEGER,
10399: ** schema TEXT HIDDEN
10400: ** );
10401: **
10402: ** It contains one entry for each b-tree pointer between a parent and
10403: ** child page in the database.
10404: */
10405: #if !defined(SQLITEINT_H)
10406: /* #include "sqlite3ext.h" */
10407:
10408: /* typedef unsigned char u8; */
10409:
10410: #endif
10411: SQLITE_EXTENSION_INIT1
10412: #include <string.h>
10413: #include <assert.h>
10414:
10415: #define DBDATA_PADDING_BYTES 100
10416:
10417: typedef struct DbdataTable DbdataTable;
10418: typedef struct DbdataCursor DbdataCursor;
10419:
10420: /* Cursor object */
10421: struct DbdataCursor {
10422: sqlite3_vtab_cursor base; /* Base class. Must be first */
10423: sqlite3_stmt *pStmt; /* For fetching database pages */
10424:
10425: int iPgno; /* Current page number */
10426: u8 *aPage; /* Buffer containing page */
10427: int nPage; /* Size of aPage[] in bytes */
10428: int nCell; /* Number of cells on aPage[] */
10429: int iCell; /* Current cell number */
10430: int bOnePage; /* True to stop after one page */
10431: int szDb;
10432: sqlite3_int64 iRowid;
10433:
10434: /* Only for the sqlite_dbdata table */
10435: u8 *pRec; /* Buffer containing current record */
10436: int nRec; /* Size of pRec[] in bytes */
10437: int nHdr; /* Size of header in bytes */
10438: int iField; /* Current field number */
10439: u8 *pHdrPtr;
10440: u8 *pPtr;
10441:
10442: sqlite3_int64 iIntkey; /* Integer key value */
10443: };
10444:
10445: /* Table object */
10446: struct DbdataTable {
10447: sqlite3_vtab base; /* Base class. Must be first */
10448: sqlite3 *db; /* The database connection */
10449: sqlite3_stmt *pStmt; /* For fetching database pages */
10450: int bPtr; /* True for sqlite3_dbptr table */
10451: };
10452:
10453: /* Column and schema definitions for sqlite_dbdata */
10454: #define DBDATA_COLUMN_PGNO 0
10455: #define DBDATA_COLUMN_CELL 1
10456: #define DBDATA_COLUMN_FIELD 2
10457: #define DBDATA_COLUMN_VALUE 3
10458: #define DBDATA_COLUMN_SCHEMA 4
10459: #define DBDATA_SCHEMA \
10460: "CREATE TABLE x(" \
10461: " pgno INTEGER," \
10462: " cell INTEGER," \
10463: " field INTEGER," \
10464: " value ANY," \
10465: " schema TEXT HIDDEN" \
10466: ")"
10467:
10468: /* Column and schema definitions for sqlite_dbptr */
10469: #define DBPTR_COLUMN_PGNO 0
10470: #define DBPTR_COLUMN_CHILD 1
10471: #define DBPTR_COLUMN_SCHEMA 2
10472: #define DBPTR_SCHEMA \
10473: "CREATE TABLE x(" \
10474: " pgno INTEGER," \
10475: " child INTEGER," \
10476: " schema TEXT HIDDEN" \
10477: ")"
10478:
10479: /*
10480: ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
10481: ** table.
10482: */
10483: static int dbdataConnect(
10484: sqlite3 *db,
10485: void *pAux,
10486: int argc, const char *const*argv,
10487: sqlite3_vtab **ppVtab,
10488: char **pzErr
10489: ){
10490: DbdataTable *pTab = 0;
10491: int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
10492:
10493: if( rc==SQLITE_OK ){
10494: pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
10495: if( pTab==0 ){
10496: rc = SQLITE_NOMEM;
10497: }else{
10498: memset(pTab, 0, sizeof(DbdataTable));
10499: pTab->db = db;
10500: pTab->bPtr = (pAux!=0);
10501: }
10502: }
10503:
10504: *ppVtab = (sqlite3_vtab*)pTab;
10505: return rc;
10506: }
10507:
10508: /*
10509: ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
10510: */
10511: static int dbdataDisconnect(sqlite3_vtab *pVtab){
10512: DbdataTable *pTab = (DbdataTable*)pVtab;
10513: if( pTab ){
10514: sqlite3_finalize(pTab->pStmt);
10515: sqlite3_free(pVtab);
10516: }
10517: return SQLITE_OK;
10518: }
10519:
10520: /*
10521: ** This function interprets two types of constraints:
10522: **
10523: ** schema=?
10524: ** pgno=?
10525: **
10526: ** If neither are present, idxNum is set to 0. If schema=? is present,
10527: ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
10528: ** in idxNum is set.
10529: **
10530: ** If both parameters are present, schema is in position 0 and pgno in
10531: ** position 1.
10532: */
10533: static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
10534: DbdataTable *pTab = (DbdataTable*)tab;
10535: int i;
10536: int iSchema = -1;
10537: int iPgno = -1;
10538: int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
10539:
10540: for(i=0; i<pIdx->nConstraint; i++){
10541: struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
10542: if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10543: if( p->iColumn==colSchema ){
10544: if( p->usable==0 ) return SQLITE_CONSTRAINT;
10545: iSchema = i;
10546: }
10547: if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
10548: iPgno = i;
10549: }
10550: }
10551: }
10552:
10553: if( iSchema>=0 ){
10554: pIdx->aConstraintUsage[iSchema].argvIndex = 1;
10555: pIdx->aConstraintUsage[iSchema].omit = 1;
10556: }
10557: if( iPgno>=0 ){
10558: pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
10559: pIdx->aConstraintUsage[iPgno].omit = 1;
10560: pIdx->estimatedCost = 100;
10561: pIdx->estimatedRows = 50;
10562:
10563: if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
10564: int iCol = pIdx->aOrderBy[0].iColumn;
10565: if( pIdx->nOrderBy==1 ){
10566: pIdx->orderByConsumed = (iCol==0 || iCol==1);
10567: }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
10568: pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
10569: }
10570: }
10571:
10572: }else{
10573: pIdx->estimatedCost = 100000000;
10574: pIdx->estimatedRows = 1000000000;
10575: }
10576: pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
10577: return SQLITE_OK;
10578: }
10579:
10580: /*
10581: ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
10582: */
10583: static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
10584: DbdataCursor *pCsr;
10585:
10586: pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
10587: if( pCsr==0 ){
10588: return SQLITE_NOMEM;
10589: }else{
10590: memset(pCsr, 0, sizeof(DbdataCursor));
10591: pCsr->base.pVtab = pVTab;
10592: }
10593:
10594: *ppCursor = (sqlite3_vtab_cursor *)pCsr;
10595: return SQLITE_OK;
10596: }
10597:
10598: /*
10599: ** Restore a cursor object to the state it was in when first allocated
10600: ** by dbdataOpen().
10601: */
10602: static void dbdataResetCursor(DbdataCursor *pCsr){
10603: DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
10604: if( pTab->pStmt==0 ){
10605: pTab->pStmt = pCsr->pStmt;
10606: }else{
10607: sqlite3_finalize(pCsr->pStmt);
10608: }
10609: pCsr->pStmt = 0;
10610: pCsr->iPgno = 1;
10611: pCsr->iCell = 0;
10612: pCsr->iField = 0;
10613: pCsr->bOnePage = 0;
10614: sqlite3_free(pCsr->aPage);
10615: sqlite3_free(pCsr->pRec);
10616: pCsr->pRec = 0;
10617: pCsr->aPage = 0;
10618: }
10619:
10620: /*
10621: ** Close an sqlite_dbdata or sqlite_dbptr cursor.
10622: */
10623: static int dbdataClose(sqlite3_vtab_cursor *pCursor){
10624: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10625: dbdataResetCursor(pCsr);
10626: sqlite3_free(pCsr);
10627: return SQLITE_OK;
10628: }
10629:
10630: /*
10631: ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
10632: */
10633: static unsigned int get_uint16(unsigned char *a){
10634: return (a[0]<<8)|a[1];
10635: }
10636: static unsigned int get_uint32(unsigned char *a){
10637: return ((unsigned int)a[0]<<24)
10638: | ((unsigned int)a[1]<<16)
10639: | ((unsigned int)a[2]<<8)
10640: | ((unsigned int)a[3]);
10641: }
10642:
10643: /*
10644: ** Load page pgno from the database via the sqlite_dbpage virtual table.
10645: ** If successful, set (*ppPage) to point to a buffer containing the page
10646: ** data, (*pnPage) to the size of that buffer in bytes and return
10647: ** SQLITE_OK. In this case it is the responsibility of the caller to
10648: ** eventually free the buffer using sqlite3_free().
10649: **
10650: ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
10651: ** return an SQLite error code.
10652: */
10653: static int dbdataLoadPage(
10654: DbdataCursor *pCsr, /* Cursor object */
10655: unsigned int pgno, /* Page number of page to load */
10656: u8 **ppPage, /* OUT: pointer to page buffer */
10657: int *pnPage /* OUT: Size of (*ppPage) in bytes */
10658: ){
10659: int rc2;
10660: int rc = SQLITE_OK;
10661: sqlite3_stmt *pStmt = pCsr->pStmt;
10662:
10663: *ppPage = 0;
10664: *pnPage = 0;
10665: sqlite3_bind_int64(pStmt, 2, pgno);
10666: if( SQLITE_ROW==sqlite3_step(pStmt) ){
10667: int nCopy = sqlite3_column_bytes(pStmt, 0);
10668: if( nCopy>0 ){
10669: u8 *pPage;
10670: pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
10671: if( pPage==0 ){
10672: rc = SQLITE_NOMEM;
10673: }else{
10674: const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
10675: memcpy(pPage, pCopy, nCopy);
10676: memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
10677: }
10678: *ppPage = pPage;
10679: *pnPage = nCopy;
10680: }
10681: }
10682: rc2 = sqlite3_reset(pStmt);
10683: if( rc==SQLITE_OK ) rc = rc2;
10684:
10685: return rc;
10686: }
10687:
10688: /*
10689: ** Read a varint. Put the value in *pVal and return the number of bytes.
10690: */
10691: static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
10692: sqlite3_int64 v = 0;
10693: int i;
10694: for(i=0; i<8; i++){
10695: v = (v<<7) + (z[i]&0x7f);
10696: if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
10697: }
10698: v = (v<<8) + (z[i]&0xff);
10699: *pVal = v;
10700: return 9;
10701: }
10702:
10703: /*
10704: ** Return the number of bytes of space used by an SQLite value of type
10705: ** eType.
10706: */
10707: static int dbdataValueBytes(int eType){
10708: switch( eType ){
10709: case 0: case 8: case 9:
10710: case 10: case 11:
10711: return 0;
10712: case 1:
10713: return 1;
10714: case 2:
10715: return 2;
10716: case 3:
10717: return 3;
10718: case 4:
10719: return 4;
10720: case 5:
10721: return 6;
10722: case 6:
10723: case 7:
10724: return 8;
10725: default:
10726: if( eType>0 ){
10727: return ((eType-12) / 2);
10728: }
10729: return 0;
10730: }
10731: }
10732:
10733: /*
10734: ** Load a value of type eType from buffer pData and use it to set the
10735: ** result of context object pCtx.
10736: */
10737: static void dbdataValue(
10738: sqlite3_context *pCtx,
10739: int eType,
10740: u8 *pData,
10741: int nData
10742: ){
10743: if( eType>=0 && dbdataValueBytes(eType)<=nData ){
10744: switch( eType ){
10745: case 0:
10746: case 10:
10747: case 11:
10748: sqlite3_result_null(pCtx);
10749: break;
10750:
10751: case 8:
10752: sqlite3_result_int(pCtx, 0);
10753: break;
10754: case 9:
10755: sqlite3_result_int(pCtx, 1);
10756: break;
10757:
10758: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
10759: sqlite3_uint64 v = (signed char)pData[0];
10760: pData++;
10761: switch( eType ){
10762: case 7:
10763: case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10764: case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10765: case 4: v = (v<<8) + pData[0]; pData++;
10766: case 3: v = (v<<8) + pData[0]; pData++;
10767: case 2: v = (v<<8) + pData[0]; pData++;
10768: }
10769:
10770: if( eType==7 ){
10771: double r;
10772: memcpy(&r, &v, sizeof(r));
10773: sqlite3_result_double(pCtx, r);
10774: }else{
10775: sqlite3_result_int64(pCtx, (sqlite3_int64)v);
10776: }
10777: break;
10778: }
10779:
10780: default: {
10781: int n = ((eType-12) / 2);
10782: if( eType % 2 ){
10783: sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
10784: }else{
10785: sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
10786: }
10787: }
10788: }
10789: }
10790: }
10791:
10792: /*
10793: ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
10794: */
10795: static int dbdataNext(sqlite3_vtab_cursor *pCursor){
10796: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10797: DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10798:
10799: pCsr->iRowid++;
10800: while( 1 ){
10801: int rc;
10802: int iOff = (pCsr->iPgno==1 ? 100 : 0);
10803: int bNextPage = 0;
10804:
10805: if( pCsr->aPage==0 ){
10806: while( 1 ){
10807: if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
10808: rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
10809: if( rc!=SQLITE_OK ) return rc;
10810: if( pCsr->aPage ) break;
10811: pCsr->iPgno++;
10812: }
10813: pCsr->iCell = pTab->bPtr ? -2 : 0;
10814: pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
10815: }
10816:
10817: if( pTab->bPtr ){
10818: if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
10819: pCsr->iCell = pCsr->nCell;
10820: }
10821: pCsr->iCell++;
10822: if( pCsr->iCell>=pCsr->nCell ){
10823: sqlite3_free(pCsr->aPage);
10824: pCsr->aPage = 0;
10825: if( pCsr->bOnePage ) return SQLITE_OK;
10826: pCsr->iPgno++;
10827: }else{
10828: return SQLITE_OK;
10829: }
10830: }else{
10831: /* If there is no record loaded, load it now. */
10832: if( pCsr->pRec==0 ){
10833: int bHasRowid = 0;
10834: int nPointer = 0;
10835: sqlite3_int64 nPayload = 0;
10836: sqlite3_int64 nHdr = 0;
10837: int iHdr;
10838: int U, X;
10839: int nLocal;
10840:
10841: switch( pCsr->aPage[iOff] ){
10842: case 0x02:
10843: nPointer = 4;
10844: break;
10845: case 0x0a:
10846: break;
10847: case 0x0d:
10848: bHasRowid = 1;
10849: break;
10850: default:
10851: /* This is not a b-tree page with records on it. Continue. */
10852: pCsr->iCell = pCsr->nCell;
10853: break;
10854: }
10855:
10856: if( pCsr->iCell>=pCsr->nCell ){
10857: bNextPage = 1;
10858: }else{
10859:
10860: iOff += 8 + nPointer + pCsr->iCell*2;
10861: if( iOff>pCsr->nPage ){
10862: bNextPage = 1;
10863: }else{
10864: iOff = get_uint16(&pCsr->aPage[iOff]);
10865: }
10866:
10867: /* For an interior node cell, skip past the child-page number */
10868: iOff += nPointer;
10869:
10870: /* Load the "byte of payload including overflow" field */
10871: if( bNextPage || iOff>pCsr->nPage ){
10872: bNextPage = 1;
10873: }else{
10874: iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
10875: }
10876:
10877: /* If this is a leaf intkey cell, load the rowid */
10878: if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
10879: iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
10880: }
10881:
10882: /* Figure out how much data to read from the local page */
10883: U = pCsr->nPage;
10884: if( bHasRowid ){
10885: X = U-35;
10886: }else{
10887: X = ((U-12)*64/255)-23;
10888: }
10889: if( nPayload<=X ){
10890: nLocal = nPayload;
10891: }else{
10892: int M, K;
10893: M = ((U-12)*32/255)-23;
10894: K = M+((nPayload-M)%(U-4));
10895: if( K<=X ){
10896: nLocal = K;
10897: }else{
10898: nLocal = M;
10899: }
10900: }
10901:
10902: if( bNextPage || nLocal+iOff>pCsr->nPage ){
10903: bNextPage = 1;
10904: }else{
10905:
10906: /* Allocate space for payload. And a bit more to catch small buffer
10907: ** overruns caused by attempting to read a varint or similar from
10908: ** near the end of a corrupt record. */
10909: pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
10910: if( pCsr->pRec==0 ) return SQLITE_NOMEM;
10911: memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
10912: pCsr->nRec = nPayload;
10913:
10914: /* Load the nLocal bytes of payload */
10915: memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
10916: iOff += nLocal;
10917:
10918: /* Load content from overflow pages */
10919: if( nPayload>nLocal ){
10920: sqlite3_int64 nRem = nPayload - nLocal;
10921: unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
10922: while( nRem>0 ){
10923: u8 *aOvfl = 0;
10924: int nOvfl = 0;
10925: int nCopy;
10926: rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
10927: assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
10928: if( rc!=SQLITE_OK ) return rc;
10929: if( aOvfl==0 ) break;
10930:
10931: nCopy = U-4;
10932: if( nCopy>nRem ) nCopy = nRem;
10933: memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
10934: nRem -= nCopy;
10935:
10936: pgnoOvfl = get_uint32(aOvfl);
10937: sqlite3_free(aOvfl);
10938: }
10939: }
10940:
10941: iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
10942: pCsr->nHdr = nHdr;
10943: pCsr->pHdrPtr = &pCsr->pRec[iHdr];
10944: pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
10945: pCsr->iField = (bHasRowid ? -1 : 0);
10946: }
10947: }
10948: }else{
10949: pCsr->iField++;
10950: if( pCsr->iField>0 ){
10951: sqlite3_int64 iType;
10952: if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
10953: bNextPage = 1;
10954: }else{
10955: pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
10956: pCsr->pPtr += dbdataValueBytes(iType);
10957: }
10958: }
10959: }
10960:
10961: if( bNextPage ){
10962: sqlite3_free(pCsr->aPage);
10963: sqlite3_free(pCsr->pRec);
10964: pCsr->aPage = 0;
10965: pCsr->pRec = 0;
10966: if( pCsr->bOnePage ) return SQLITE_OK;
10967: pCsr->iPgno++;
10968: }else{
10969: if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
10970: return SQLITE_OK;
10971: }
10972:
10973: /* Advance to the next cell. The next iteration of the loop will load
10974: ** the record and so on. */
10975: sqlite3_free(pCsr->pRec);
10976: pCsr->pRec = 0;
10977: pCsr->iCell++;
10978: }
10979: }
10980: }
10981:
10982: assert( !"can't get here" );
10983: return SQLITE_OK;
10984: }
10985:
10986: /*
10987: ** Return true if the cursor is at EOF.
10988: */
10989: static int dbdataEof(sqlite3_vtab_cursor *pCursor){
10990: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10991: return pCsr->aPage==0;
10992: }
10993:
10994: /*
10995: ** Determine the size in pages of database zSchema (where zSchema is
10996: ** "main", "temp" or the name of an attached database) and set
10997: ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
10998: ** an SQLite error code.
10999: */
11000: static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11001: DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11002: char *zSql = 0;
11003: int rc, rc2;
11004: sqlite3_stmt *pStmt = 0;
11005:
11006: zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11007: if( zSql==0 ) return SQLITE_NOMEM;
11008: rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11009: sqlite3_free(zSql);
11010: if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11011: pCsr->szDb = sqlite3_column_int(pStmt, 0);
11012: }
11013: rc2 = sqlite3_finalize(pStmt);
11014: if( rc==SQLITE_OK ) rc = rc2;
11015: return rc;
11016: }
11017:
11018: /*
11019: ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11020: */
11021: static int dbdataFilter(
11022: sqlite3_vtab_cursor *pCursor,
11023: int idxNum, const char *idxStr,
11024: int argc, sqlite3_value **argv
11025: ){
11026: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11027: DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11028: int rc = SQLITE_OK;
11029: const char *zSchema = "main";
11030:
11031: dbdataResetCursor(pCsr);
11032: assert( pCsr->iPgno==1 );
11033: if( idxNum & 0x01 ){
11034: zSchema = (const char*)sqlite3_value_text(argv[0]);
11035: }
11036: if( idxNum & 0x02 ){
11037: pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11038: pCsr->bOnePage = 1;
11039: }else{
11040: pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11041: rc = dbdataDbsize(pCsr, zSchema);
11042: }
11043:
11044: if( rc==SQLITE_OK ){
11045: if( pTab->pStmt ){
11046: pCsr->pStmt = pTab->pStmt;
11047: pTab->pStmt = 0;
11048: }else{
11049: rc = sqlite3_prepare_v2(pTab->db,
11050: "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11051: &pCsr->pStmt, 0
11052: );
11053: }
11054: }
11055: if( rc==SQLITE_OK ){
11056: rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
11057: }else{
11058: pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11059: }
11060: if( rc==SQLITE_OK ){
11061: rc = dbdataNext(pCursor);
11062: }
11063: return rc;
11064: }
11065:
11066: /*
11067: ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
11068: */
11069: static int dbdataColumn(
11070: sqlite3_vtab_cursor *pCursor,
11071: sqlite3_context *ctx,
11072: int i
11073: ){
11074: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11075: DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11076: if( pTab->bPtr ){
11077: switch( i ){
11078: case DBPTR_COLUMN_PGNO:
11079: sqlite3_result_int64(ctx, pCsr->iPgno);
11080: break;
11081: case DBPTR_COLUMN_CHILD: {
11082: int iOff = pCsr->iPgno==1 ? 100 : 0;
11083: if( pCsr->iCell<0 ){
11084: iOff += 8;
11085: }else{
11086: iOff += 12 + pCsr->iCell*2;
11087: if( iOff>pCsr->nPage ) return SQLITE_OK;
11088: iOff = get_uint16(&pCsr->aPage[iOff]);
11089: }
11090: if( iOff<=pCsr->nPage ){
11091: sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
11092: }
11093: break;
11094: }
11095: }
11096: }else{
11097: switch( i ){
11098: case DBDATA_COLUMN_PGNO:
11099: sqlite3_result_int64(ctx, pCsr->iPgno);
11100: break;
11101: case DBDATA_COLUMN_CELL:
11102: sqlite3_result_int(ctx, pCsr->iCell);
11103: break;
11104: case DBDATA_COLUMN_FIELD:
11105: sqlite3_result_int(ctx, pCsr->iField);
11106: break;
11107: case DBDATA_COLUMN_VALUE: {
11108: if( pCsr->iField<0 ){
11109: sqlite3_result_int64(ctx, pCsr->iIntkey);
11110: }else{
11111: sqlite3_int64 iType;
11112: dbdataGetVarint(pCsr->pHdrPtr, &iType);
11113: dbdataValue(
11114: ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11115: );
11116: }
11117: break;
11118: }
11119: }
11120: }
11121: return SQLITE_OK;
11122: }
11123:
11124: /*
11125: ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11126: */
11127: static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11128: DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11129: *pRowid = pCsr->iRowid;
11130: return SQLITE_OK;
11131: }
11132:
11133:
11134: /*
11135: ** Invoke this routine to register the "sqlite_dbdata" virtual table module
11136: */
11137: static int sqlite3DbdataRegister(sqlite3 *db){
11138: static sqlite3_module dbdata_module = {
11139: 0, /* iVersion */
11140: 0, /* xCreate */
11141: dbdataConnect, /* xConnect */
11142: dbdataBestIndex, /* xBestIndex */
11143: dbdataDisconnect, /* xDisconnect */
11144: 0, /* xDestroy */
11145: dbdataOpen, /* xOpen - open a cursor */
11146: dbdataClose, /* xClose - close a cursor */
11147: dbdataFilter, /* xFilter - configure scan constraints */
11148: dbdataNext, /* xNext - advance a cursor */
11149: dbdataEof, /* xEof - check for end of scan */
11150: dbdataColumn, /* xColumn - read data */
11151: dbdataRowid, /* xRowid - read data */
11152: 0, /* xUpdate */
11153: 0, /* xBegin */
11154: 0, /* xSync */
11155: 0, /* xCommit */
11156: 0, /* xRollback */
11157: 0, /* xFindMethod */
11158: 0, /* xRename */
11159: 0, /* xSavepoint */
11160: 0, /* xRelease */
11161: 0, /* xRollbackTo */
11162: 0 /* xShadowName */
11163: };
11164:
11165: int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11166: if( rc==SQLITE_OK ){
11167: rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11168: }
11169: return rc;
11170: }
11171:
11172: #ifdef _WIN32
11173:
11174: #endif
11175: int sqlite3_dbdata_init(
11176: sqlite3 *db,
11177: char **pzErrMsg,
11178: const sqlite3_api_routines *pApi
11179: ){
11180: SQLITE_EXTENSION_INIT2(pApi);
11181: return sqlite3DbdataRegister(db);
11182: }
11183:
11184: /************************* End ../ext/misc/dbdata.c ********************/
11185: #endif
11186:
11187: #if defined(SQLITE_ENABLE_SESSION)
11188: /*
11189: ** State information for a single open session
11190: */
11191: typedef struct OpenSession OpenSession;
11192: struct OpenSession {
11193: char *zName; /* Symbolic name for this session */
11194: int nFilter; /* Number of xFilter rejection GLOB patterns */
11195: char **azFilter; /* Array of xFilter rejection GLOB patterns */
11196: sqlite3_session *p; /* The open session */
11197: };
11198: #endif
11199:
11200: typedef struct ExpertInfo ExpertInfo;
11201: struct ExpertInfo {
11202: sqlite3expert *pExpert;
11203: int bVerbose;
11204: };
11205:
11206: /* A single line in the EQP output */
11207: typedef struct EQPGraphRow EQPGraphRow;
11208: struct EQPGraphRow {
11209: int iEqpId; /* ID for this row */
11210: int iParentId; /* ID of the parent row */
11211: EQPGraphRow *pNext; /* Next row in sequence */
11212: char zText[1]; /* Text to display for this row */
11213: };
11214:
11215: /* All EQP output is collected into an instance of the following */
11216: typedef struct EQPGraph EQPGraph;
11217: struct EQPGraph {
11218: EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
11219: EQPGraphRow *pLast; /* Last element of the pRow list */
11220: char zPrefix[100]; /* Graph prefix */
11221: };
11222:
11223: /*
11224: ** State information about the database connection is contained in an
11225: ** instance of the following structure.
11226: */
11227: typedef struct ShellState ShellState;
11228: struct ShellState {
11229: sqlite3 *db; /* The database */
11230: u8 autoExplain; /* Automatically turn on .explain mode */
11231: u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
11232: u8 autoEQPtest; /* autoEQP is in test mode */
11233: u8 autoEQPtrace; /* autoEQP is in trace mode */
11234: u8 scanstatsOn; /* True to display scan stats before each finalize */
11235: u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
11236: u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
11237: u8 nEqpLevel; /* Depth of the EQP output graph */
11238: u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
11239: unsigned statsOn; /* True to display memory stats before each finalize */
11240: unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
11241: int outCount; /* Revert to stdout when reaching zero */
11242: int cnt; /* Number of records displayed so far */
11243: int lineno; /* Line number of last line read from in */
11244: int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
11245: FILE *in; /* Read commands from this stream */
11246: FILE *out; /* Write results here */
11247: FILE *traceOut; /* Output for sqlite3_trace() */
11248: int nErr; /* Number of errors seen */
11249: int mode; /* An output mode setting */
11250: int modePrior; /* Saved mode */
11251: int cMode; /* temporary output mode for the current query */
11252: int normalMode; /* Output mode before ".explain on" */
11253: int writableSchema; /* True if PRAGMA writable_schema=ON */
11254: int showHeader; /* True to show column names in List or Column mode */
11255: int nCheck; /* Number of ".check" commands run */
11256: unsigned nProgress; /* Number of progress callbacks encountered */
11257: unsigned mxProgress; /* Maximum progress callbacks before failing */
11258: unsigned flgProgress; /* Flags for the progress callback */
11259: unsigned shellFlgs; /* Various flags */
11260: unsigned priorShFlgs; /* Saved copy of flags */
11261: sqlite3_int64 szMax; /* --maxsize argument to .open */
11262: char *zDestTable; /* Name of destination table when MODE_Insert */
11263: char *zTempFile; /* Temporary file that might need deleting */
11264: char zTestcase[30]; /* Name of current test case */
11265: char colSeparator[20]; /* Column separator character for several modes */
11266: char rowSeparator[20]; /* Row separator character for MODE_Ascii */
11267: char colSepPrior[20]; /* Saved column separator */
11268: char rowSepPrior[20]; /* Saved row separator */
11269: int *colWidth; /* Requested width of each column in columnar modes */
11270: int *actualWidth; /* Actual width of each column */
11271: int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
11272: char nullValue[20]; /* The text to print when a NULL comes back from
11273: ** the database */
11274: char outfile[FILENAME_MAX]; /* Filename for *out */
11275: const char *zDbFilename; /* name of the database file */
11276: char *zFreeOnClose; /* Filename to free when closing */
11277: const char *zVfs; /* Name of VFS to use */
11278: sqlite3_stmt *pStmt; /* Current statement if any. */
11279: FILE *pLog; /* Write log output here */
11280: int *aiIndent; /* Array of indents used in MODE_Explain */
11281: int nIndent; /* Size of array aiIndent[] */
11282: int iIndent; /* Index of current op in aiIndent[] */
11283: EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
11284: #if defined(SQLITE_ENABLE_SESSION)
11285: int nSession; /* Number of active sessions */
11286: OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
11287: #endif
11288: ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
11289: };
11290:
11291:
11292: /* Allowed values for ShellState.autoEQP
11293: */
11294: #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
11295: #define AUTOEQP_on 1 /* Automatic EQP is on */
11296: #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
11297: #define AUTOEQP_full 3 /* Show full EXPLAIN */
11298:
11299: /* Allowed values for ShellState.openMode
11300: */
11301: #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
11302: #define SHELL_OPEN_NORMAL 1 /* Normal database file */
11303: #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
11304: #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
11305: #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
11306: #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
11307: #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
11308:
11309: /* Allowed values for ShellState.eTraceType
11310: */
11311: #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
11312: #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
11313: #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
11314:
11315: /* Bits in the ShellState.flgProgress variable */
11316: #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
11317: #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
11318: ** callback limit is reached, and for each
11319: ** top-level SQL statement */
11320: #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
11321:
11322: /*
11323: ** These are the allowed shellFlgs values
11324: */
11325: #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
11326: #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
11327: #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
11328: #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
11329: #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
11330: #define SHFLG_CountChanges 0x00000020 /* .changes setting */
11331: #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
11332: #define SHFLG_HeaderSet 0x00000080 /* .header has been used */
11333: #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
11334: #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
11335:
11336: /*
11337: ** Macros for testing and setting shellFlgs
11338: */
11339: #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
11340: #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
11341: #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
11342:
11343: /*
11344: ** These are the allowed modes.
11345: */
11346: #define MODE_Line 0 /* One column per line. Blank line between records */
11347: #define MODE_Column 1 /* One record per line in neat columns */
11348: #define MODE_List 2 /* One record per line with a separator */
11349: #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
11350: #define MODE_Html 4 /* Generate an XHTML table */
11351: #define MODE_Insert 5 /* Generate SQL "insert" statements */
11352: #define MODE_Quote 6 /* Quote values as for SQL */
11353: #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
11354: #define MODE_Csv 8 /* Quote strings, numbers are plain */
11355: #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
11356: #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
11357: #define MODE_Pretty 11 /* Pretty-print schemas */
11358: #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
11359: #define MODE_Json 13 /* Output JSON */
11360: #define MODE_Markdown 14 /* Markdown formatting */
11361: #define MODE_Table 15 /* MySQL-style table formatting */
11362: #define MODE_Box 16 /* Unicode box-drawing characters */
11363:
11364: static const char *modeDescr[] = {
11365: "line",
11366: "column",
11367: "list",
11368: "semi",
11369: "html",
11370: "insert",
11371: "quote",
11372: "tcl",
11373: "csv",
11374: "explain",
11375: "ascii",
11376: "prettyprint",
11377: "eqp",
11378: "json",
11379: "markdown",
11380: "table",
11381: "box"
11382: };
11383:
11384: /*
11385: ** These are the column/row/line separators used by the various
11386: ** import/export modes.
11387: */
11388: #define SEP_Column "|"
11389: #define SEP_Row "\n"
11390: #define SEP_Tab "\t"
11391: #define SEP_Space " "
11392: #define SEP_Comma ","
11393: #define SEP_CrLf "\r\n"
11394: #define SEP_Unit "\x1F"
11395: #define SEP_Record "\x1E"
11396:
11397: /*
11398: ** A callback for the sqlite3_log() interface.
11399: */
11400: static void shellLog(void *pArg, int iErrCode, const char *zMsg){
11401: ShellState *p = (ShellState*)pArg;
11402: if( p->pLog==0 ) return;
11403: utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
11404: fflush(p->pLog);
11405: }
11406:
11407: /*
11408: ** SQL function: shell_putsnl(X)
11409: **
11410: ** Write the text X to the screen (or whatever output is being directed)
11411: ** adding a newline at the end, and then return X.
11412: */
11413: static void shellPutsFunc(
11414: sqlite3_context *pCtx,
11415: int nVal,
11416: sqlite3_value **apVal
11417: ){
11418: ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
11419: (void)nVal;
11420: utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
11421: sqlite3_result_value(pCtx, apVal[0]);
11422: }
11423:
11424: /*
11425: ** SQL function: edit(VALUE)
11426: ** edit(VALUE,EDITOR)
11427: **
11428: ** These steps:
11429: **
11430: ** (1) Write VALUE into a temporary file.
11431: ** (2) Run program EDITOR on that temporary file.
11432: ** (3) Read the temporary file back and return its content as the result.
11433: ** (4) Delete the temporary file
11434: **
11435: ** If the EDITOR argument is omitted, use the value in the VISUAL
11436: ** environment variable. If still there is no EDITOR, through an error.
11437: **
11438: ** Also throw an error if the EDITOR program returns a non-zero exit code.
11439: */
11440: #ifndef SQLITE_NOHAVE_SYSTEM
11441: static void editFunc(
11442: sqlite3_context *context,
11443: int argc,
11444: sqlite3_value **argv
11445: ){
11446: const char *zEditor;
11447: char *zTempFile = 0;
11448: sqlite3 *db;
11449: char *zCmd = 0;
11450: int bBin;
11451: int rc;
11452: int hasCRNL = 0;
11453: FILE *f = 0;
11454: sqlite3_int64 sz;
11455: sqlite3_int64 x;
11456: unsigned char *p = 0;
11457:
11458: if( argc==2 ){
11459: zEditor = (const char*)sqlite3_value_text(argv[1]);
11460: }else{
11461: zEditor = getenv("VISUAL");
11462: }
11463: if( zEditor==0 ){
11464: sqlite3_result_error(context, "no editor for edit()", -1);
11465: return;
11466: }
11467: if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
11468: sqlite3_result_error(context, "NULL input to edit()", -1);
11469: return;
11470: }
11471: db = sqlite3_context_db_handle(context);
11472: zTempFile = 0;
11473: sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
11474: if( zTempFile==0 ){
11475: sqlite3_uint64 r = 0;
11476: sqlite3_randomness(sizeof(r), &r);
11477: zTempFile = sqlite3_mprintf("temp%llx", r);
11478: if( zTempFile==0 ){
11479: sqlite3_result_error_nomem(context);
11480: return;
11481: }
11482: }
11483: bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
11484: /* When writing the file to be edited, do \n to \r\n conversions on systems
11485: ** that want \r\n line endings */
11486: f = fopen(zTempFile, bBin ? "wb" : "w");
11487: if( f==0 ){
11488: sqlite3_result_error(context, "edit() cannot open temp file", -1);
11489: goto edit_func_end;
11490: }
11491: sz = sqlite3_value_bytes(argv[0]);
11492: if( bBin ){
11493: x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
11494: }else{
11495: const char *z = (const char*)sqlite3_value_text(argv[0]);
11496: /* Remember whether or not the value originally contained \r\n */
11497: if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
11498: x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
11499: }
11500: fclose(f);
11501: f = 0;
11502: if( x!=sz ){
11503: sqlite3_result_error(context, "edit() could not write the whole file", -1);
11504: goto edit_func_end;
11505: }
11506: zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
11507: if( zCmd==0 ){
11508: sqlite3_result_error_nomem(context);
11509: goto edit_func_end;
11510: }
11511: rc = system(zCmd);
11512: sqlite3_free(zCmd);
11513: if( rc ){
11514: sqlite3_result_error(context, "EDITOR returned non-zero", -1);
11515: goto edit_func_end;
11516: }
11517: f = fopen(zTempFile, "rb");
11518: if( f==0 ){
11519: sqlite3_result_error(context,
11520: "edit() cannot reopen temp file after edit", -1);
11521: goto edit_func_end;
11522: }
11523: fseek(f, 0, SEEK_END);
11524: sz = ftell(f);
11525: rewind(f);
11526: p = sqlite3_malloc64( sz+1 );
11527: if( p==0 ){
11528: sqlite3_result_error_nomem(context);
11529: goto edit_func_end;
11530: }
11531: x = fread(p, 1, (size_t)sz, f);
11532: fclose(f);
11533: f = 0;
11534: if( x!=sz ){
11535: sqlite3_result_error(context, "could not read back the whole file", -1);
11536: goto edit_func_end;
11537: }
11538: if( bBin ){
11539: sqlite3_result_blob64(context, p, sz, sqlite3_free);
11540: }else{
11541: sqlite3_int64 i, j;
11542: if( hasCRNL ){
11543: /* If the original contains \r\n then do no conversions back to \n */
11544: j = sz;
11545: }else{
11546: /* If the file did not originally contain \r\n then convert any new
11547: ** \r\n back into \n */
11548: for(i=j=0; i<sz; i++){
11549: if( p[i]=='\r' && p[i+1]=='\n' ) i++;
11550: p[j++] = p[i];
11551: }
11552: sz = j;
11553: p[sz] = 0;
11554: }
11555: sqlite3_result_text64(context, (const char*)p, sz,
11556: sqlite3_free, SQLITE_UTF8);
11557: }
11558: p = 0;
11559:
11560: edit_func_end:
11561: if( f ) fclose(f);
11562: unlink(zTempFile);
11563: sqlite3_free(zTempFile);
11564: sqlite3_free(p);
11565: }
11566: #endif /* SQLITE_NOHAVE_SYSTEM */
11567:
11568: /*
11569: ** Save or restore the current output mode
11570: */
11571: static void outputModePush(ShellState *p){
11572: p->modePrior = p->mode;
11573: p->priorShFlgs = p->shellFlgs;
11574: memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
11575: memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
11576: }
11577: static void outputModePop(ShellState *p){
11578: p->mode = p->modePrior;
11579: p->shellFlgs = p->priorShFlgs;
11580: memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
11581: memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
11582: }
11583:
11584: /*
11585: ** Output the given string as a hex-encoded blob (eg. X'1234' )
11586: */
11587: static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
11588: int i;
11589: char *zBlob = (char *)pBlob;
11590: raw_printf(out,"X'");
11591: for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
11592: raw_printf(out,"'");
11593: }
11594:
11595: /*
11596: ** Find a string that is not found anywhere in z[]. Return a pointer
11597: ** to that string.
11598: **
11599: ** Try to use zA and zB first. If both of those are already found in z[]
11600: ** then make up some string and store it in the buffer zBuf.
11601: */
11602: static const char *unused_string(
11603: const char *z, /* Result must not appear anywhere in z */
11604: const char *zA, const char *zB, /* Try these first */
11605: char *zBuf /* Space to store a generated string */
11606: ){
11607: unsigned i = 0;
11608: if( strstr(z, zA)==0 ) return zA;
11609: if( strstr(z, zB)==0 ) return zB;
11610: do{
11611: sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
11612: }while( strstr(z,zBuf)!=0 );
11613: return zBuf;
11614: }
11615:
11616: /*
11617: ** Output the given string as a quoted string using SQL quoting conventions.
11618: **
11619: ** See also: output_quoted_escaped_string()
11620: */
11621: static void output_quoted_string(FILE *out, const char *z){
11622: int i;
11623: char c;
11624: setBinaryMode(out, 1);
11625: for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11626: if( c==0 ){
11627: utf8_printf(out,"'%s'",z);
11628: }else{
11629: raw_printf(out, "'");
11630: while( *z ){
11631: for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11632: if( c=='\'' ) i++;
11633: if( i ){
11634: utf8_printf(out, "%.*s", i, z);
11635: z += i;
11636: }
11637: if( c=='\'' ){
11638: raw_printf(out, "'");
11639: continue;
11640: }
11641: if( c==0 ){
11642: break;
11643: }
11644: z++;
11645: }
11646: raw_printf(out, "'");
11647: }
11648: setTextMode(out, 1);
11649: }
11650:
11651: /*
11652: ** Output the given string as a quoted string using SQL quoting conventions.
11653: ** Additionallly , escape the "\n" and "\r" characters so that they do not
11654: ** get corrupted by end-of-line translation facilities in some operating
11655: ** systems.
11656: **
11657: ** This is like output_quoted_string() but with the addition of the \r\n
11658: ** escape mechanism.
11659: */
11660: static void output_quoted_escaped_string(FILE *out, const char *z){
11661: int i;
11662: char c;
11663: setBinaryMode(out, 1);
11664: for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
11665: if( c==0 ){
11666: utf8_printf(out,"'%s'",z);
11667: }else{
11668: const char *zNL = 0;
11669: const char *zCR = 0;
11670: int nNL = 0;
11671: int nCR = 0;
11672: char zBuf1[20], zBuf2[20];
11673: for(i=0; z[i]; i++){
11674: if( z[i]=='\n' ) nNL++;
11675: if( z[i]=='\r' ) nCR++;
11676: }
11677: if( nNL ){
11678: raw_printf(out, "replace(");
11679: zNL = unused_string(z, "\\n", "\\012", zBuf1);
11680: }
11681: if( nCR ){
11682: raw_printf(out, "replace(");
11683: zCR = unused_string(z, "\\r", "\\015", zBuf2);
11684: }
11685: raw_printf(out, "'");
11686: while( *z ){
11687: for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
11688: if( c=='\'' ) i++;
11689: if( i ){
11690: utf8_printf(out, "%.*s", i, z);
11691: z += i;
11692: }
11693: if( c=='\'' ){
11694: raw_printf(out, "'");
11695: continue;
11696: }
11697: if( c==0 ){
11698: break;
11699: }
11700: z++;
11701: if( c=='\n' ){
11702: raw_printf(out, "%s", zNL);
11703: continue;
11704: }
11705: raw_printf(out, "%s", zCR);
11706: }
11707: raw_printf(out, "'");
11708: if( nCR ){
11709: raw_printf(out, ",'%s',char(13))", zCR);
11710: }
11711: if( nNL ){
11712: raw_printf(out, ",'%s',char(10))", zNL);
11713: }
11714: }
11715: setTextMode(out, 1);
11716: }
11717:
11718: /*
11719: ** Output the given string as a quoted according to C or TCL quoting rules.
11720: */
11721: static void output_c_string(FILE *out, const char *z){
11722: unsigned int c;
11723: fputc('"', out);
11724: while( (c = *(z++))!=0 ){
11725: if( c=='\\' ){
11726: fputc(c, out);
11727: fputc(c, out);
11728: }else if( c=='"' ){
11729: fputc('\\', out);
11730: fputc('"', out);
11731: }else if( c=='\t' ){
11732: fputc('\\', out);
11733: fputc('t', out);
11734: }else if( c=='\n' ){
11735: fputc('\\', out);
11736: fputc('n', out);
11737: }else if( c=='\r' ){
11738: fputc('\\', out);
11739: fputc('r', out);
11740: }else if( !isprint(c&0xff) ){
11741: raw_printf(out, "\\%03o", c&0xff);
11742: }else{
11743: fputc(c, out);
11744: }
11745: }
11746: fputc('"', out);
11747: }
11748:
11749: /*
11750: ** Output the given string as a quoted according to JSON quoting rules.
11751: */
11752: static void output_json_string(FILE *out, const char *z, int n){
11753: unsigned int c;
11754: if( n<0 ) n = (int)strlen(z);
11755: fputc('"', out);
11756: while( n-- ){
11757: c = *(z++);
11758: if( c=='\\' || c=='"' ){
11759: fputc('\\', out);
11760: fputc(c, out);
11761: }else if( c<=0x1f ){
11762: fputc('\\', out);
11763: if( c=='\b' ){
11764: fputc('b', out);
11765: }else if( c=='\f' ){
11766: fputc('f', out);
11767: }else if( c=='\n' ){
11768: fputc('n', out);
11769: }else if( c=='\r' ){
11770: fputc('r', out);
11771: }else if( c=='\t' ){
11772: fputc('t', out);
11773: }else{
11774: raw_printf(out, "u%04x",c);
11775: }
11776: }else{
11777: fputc(c, out);
11778: }
11779: }
11780: fputc('"', out);
11781: }
11782:
11783: /*
11784: ** Output the given string with characters that are special to
11785: ** HTML escaped.
11786: */
11787: static void output_html_string(FILE *out, const char *z){
11788: int i;
11789: if( z==0 ) z = "";
11790: while( *z ){
11791: for(i=0; z[i]
11792: && z[i]!='<'
11793: && z[i]!='&'
11794: && z[i]!='>'
11795: && z[i]!='\"'
11796: && z[i]!='\'';
11797: i++){}
11798: if( i>0 ){
11799: utf8_printf(out,"%.*s",i,z);
11800: }
11801: if( z[i]=='<' ){
11802: raw_printf(out,"<");
11803: }else if( z[i]=='&' ){
11804: raw_printf(out,"&");
11805: }else if( z[i]=='>' ){
11806: raw_printf(out,">");
11807: }else if( z[i]=='\"' ){
11808: raw_printf(out,""");
11809: }else if( z[i]=='\'' ){
11810: raw_printf(out,"'");
11811: }else{
11812: break;
11813: }
11814: z += i + 1;
11815: }
11816: }
11817:
11818: /*
11819: ** If a field contains any character identified by a 1 in the following
11820: ** array, then the string must be quoted for CSV.
11821: */
11822: static const char needCsvQuote[] = {
11823: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11824: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11825: 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
11826: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11827: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11828: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11829: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11830: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
11831: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11832: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11833: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11834: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11835: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11836: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11837: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11838: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11839: };
11840:
11841: /*
11842: ** Output a single term of CSV. Actually, p->colSeparator is used for
11843: ** the separator, which may or may not be a comma. p->nullValue is
11844: ** the null value. Strings are quoted if necessary. The separator
11845: ** is only issued if bSep is true.
11846: */
11847: static void output_csv(ShellState *p, const char *z, int bSep){
11848: FILE *out = p->out;
11849: if( z==0 ){
11850: utf8_printf(out,"%s",p->nullValue);
11851: }else{
11852: int i;
11853: int nSep = strlen30(p->colSeparator);
11854: for(i=0; z[i]; i++){
11855: if( needCsvQuote[((unsigned char*)z)[i]]
11856: || (z[i]==p->colSeparator[0] &&
11857: (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
11858: i = 0;
11859: break;
11860: }
11861: }
11862: if( i==0 ){
11863: char *zQuoted = sqlite3_mprintf("\"%w\"", z);
11864: utf8_printf(out, "%s", zQuoted);
11865: sqlite3_free(zQuoted);
11866: }else{
11867: utf8_printf(out, "%s", z);
11868: }
11869: }
11870: if( bSep ){
11871: utf8_printf(p->out, "%s", p->colSeparator);
11872: }
11873: }
11874:
11875: /*
11876: ** This routine runs when the user presses Ctrl-C
11877: */
11878: static void interrupt_handler(int NotUsed){
11879: UNUSED_PARAMETER(NotUsed);
11880: seenInterrupt++;
11881: if( seenInterrupt>2 ) exit(1);
11882: if( globalDb ) sqlite3_interrupt(globalDb);
11883: }
11884:
11885: #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11886: /*
11887: ** This routine runs for console events (e.g. Ctrl-C) on Win32
11888: */
11889: static BOOL WINAPI ConsoleCtrlHandler(
11890: DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
11891: ){
11892: if( dwCtrlType==CTRL_C_EVENT ){
11893: interrupt_handler(0);
11894: return TRUE;
11895: }
11896: return FALSE;
11897: }
11898: #endif
11899:
11900: #ifndef SQLITE_OMIT_AUTHORIZATION
11901: /*
11902: ** When the ".auth ON" is set, the following authorizer callback is
11903: ** invoked. It always returns SQLITE_OK.
11904: */
11905: static int shellAuth(
11906: void *pClientData,
11907: int op,
11908: const char *zA1,
11909: const char *zA2,
11910: const char *zA3,
11911: const char *zA4
11912: ){
11913: ShellState *p = (ShellState*)pClientData;
11914: static const char *azAction[] = { 0,
11915: "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
11916: "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
11917: "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
11918: "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
11919: "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
11920: "DROP_TRIGGER", "DROP_VIEW", "INSERT",
11921: "PRAGMA", "READ", "SELECT",
11922: "TRANSACTION", "UPDATE", "ATTACH",
11923: "DETACH", "ALTER_TABLE", "REINDEX",
11924: "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
11925: "FUNCTION", "SAVEPOINT", "RECURSIVE"
11926: };
11927: int i;
11928: const char *az[4];
11929: az[0] = zA1;
11930: az[1] = zA2;
11931: az[2] = zA3;
11932: az[3] = zA4;
11933: utf8_printf(p->out, "authorizer: %s", azAction[op]);
11934: for(i=0; i<4; i++){
11935: raw_printf(p->out, " ");
11936: if( az[i] ){
11937: output_c_string(p->out, az[i]);
11938: }else{
11939: raw_printf(p->out, "NULL");
11940: }
11941: }
11942: raw_printf(p->out, "\n");
11943: return SQLITE_OK;
11944: }
11945: #endif
11946:
11947: /*
11948: ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
11949: **
11950: ** This routine converts some CREATE TABLE statements for shadow tables
11951: ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
11952: */
11953: static void printSchemaLine(FILE *out, const char *z, const char *zTail){
11954: if( z==0 ) return;
11955: if( zTail==0 ) return;
11956: if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
11957: utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
11958: }else{
11959: utf8_printf(out, "%s%s", z, zTail);
11960: }
11961: }
11962: static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
11963: char c = z[n];
11964: z[n] = 0;
11965: printSchemaLine(out, z, zTail);
11966: z[n] = c;
11967: }
11968:
11969: /*
11970: ** Return true if string z[] has nothing but whitespace and comments to the
11971: ** end of the first line.
11972: */
11973: static int wsToEol(const char *z){
11974: int i;
11975: for(i=0; z[i]; i++){
11976: if( z[i]=='\n' ) return 1;
11977: if( IsSpace(z[i]) ) continue;
11978: if( z[i]=='-' && z[i+1]=='-' ) return 1;
11979: return 0;
11980: }
11981: return 1;
11982: }
11983:
11984: /*
11985: ** Add a new entry to the EXPLAIN QUERY PLAN data
11986: */
11987: static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
11988: EQPGraphRow *pNew;
11989: int nText = strlen30(zText);
11990: if( p->autoEQPtest ){
11991: utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
11992: }
11993: pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
11994: if( pNew==0 ) shell_out_of_memory();
11995: pNew->iEqpId = iEqpId;
11996: pNew->iParentId = p2;
11997: memcpy(pNew->zText, zText, nText+1);
11998: pNew->pNext = 0;
11999: if( p->sGraph.pLast ){
12000: p->sGraph.pLast->pNext = pNew;
12001: }else{
12002: p->sGraph.pRow = pNew;
12003: }
12004: p->sGraph.pLast = pNew;
12005: }
12006:
12007: /*
12008: ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
12009: ** in p->sGraph.
12010: */
12011: static void eqp_reset(ShellState *p){
12012: EQPGraphRow *pRow, *pNext;
12013: for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
12014: pNext = pRow->pNext;
12015: sqlite3_free(pRow);
12016: }
12017: memset(&p->sGraph, 0, sizeof(p->sGraph));
12018: }
12019:
12020: /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
12021: ** pOld, or return the first such line if pOld is NULL
12022: */
12023: static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
12024: EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
12025: while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
12026: return pRow;
12027: }
12028:
12029: /* Render a single level of the graph that has iEqpId as its parent. Called
12030: ** recursively to render sublevels.
12031: */
12032: static void eqp_render_level(ShellState *p, int iEqpId){
12033: EQPGraphRow *pRow, *pNext;
12034: int n = strlen30(p->sGraph.zPrefix);
12035: char *z;
12036: for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
12037: pNext = eqp_next_row(p, iEqpId, pRow);
12038: z = pRow->zText;
12039: utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
12040: pNext ? "|--" : "`--", z);
12041: if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
12042: memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
12043: eqp_render_level(p, pRow->iEqpId);
12044: p->sGraph.zPrefix[n] = 0;
12045: }
12046: }
12047: }
12048:
12049: /*
12050: ** Display and reset the EXPLAIN QUERY PLAN data
12051: */
12052: static void eqp_render(ShellState *p){
12053: EQPGraphRow *pRow = p->sGraph.pRow;
12054: if( pRow ){
12055: if( pRow->zText[0]=='-' ){
12056: if( pRow->pNext==0 ){
12057: eqp_reset(p);
12058: return;
12059: }
12060: utf8_printf(p->out, "%s\n", pRow->zText+3);
12061: p->sGraph.pRow = pRow->pNext;
12062: sqlite3_free(pRow);
12063: }else{
12064: utf8_printf(p->out, "QUERY PLAN\n");
12065: }
12066: p->sGraph.zPrefix[0] = 0;
12067: eqp_render_level(p, 0);
12068: eqp_reset(p);
12069: }
12070: }
12071:
12072: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12073: /*
12074: ** Progress handler callback.
12075: */
12076: static int progress_handler(void *pClientData) {
12077: ShellState *p = (ShellState*)pClientData;
12078: p->nProgress++;
12079: if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
12080: raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
12081: if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12082: if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
12083: return 1;
12084: }
12085: if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
12086: raw_printf(p->out, "Progress %u\n", p->nProgress);
12087: }
12088: return 0;
12089: }
12090: #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
12091:
12092: /*
12093: ** Print N dashes
12094: */
12095: static void print_dashes(FILE *out, int N){
12096: const char zDash[] = "--------------------------------------------------";
12097: const int nDash = sizeof(zDash) - 1;
12098: while( N>nDash ){
12099: fputs(zDash, out);
12100: N -= nDash;
12101: }
12102: raw_printf(out, "%.*s", N, zDash);
12103: }
12104:
12105: /*
12106: ** Print a markdown or table-style row separator using ascii-art
12107: */
12108: static void print_row_separator(
12109: ShellState *p,
12110: int nArg,
12111: const char *zSep
12112: ){
12113: int i;
12114: if( nArg>0 ){
12115: fputs(zSep, p->out);
12116: print_dashes(p->out, p->actualWidth[0]+2);
12117: for(i=1; i<nArg; i++){
12118: fputs(zSep, p->out);
12119: print_dashes(p->out, p->actualWidth[i]+2);
12120: }
12121: fputs(zSep, p->out);
12122: }
12123: fputs("\n", p->out);
12124: }
12125:
12126: /*
12127: ** This is the callback routine that the shell
12128: ** invokes for each row of a query result.
12129: */
12130: static int shell_callback(
12131: void *pArg,
12132: int nArg, /* Number of result columns */
12133: char **azArg, /* Text of each result column */
12134: char **azCol, /* Column names */
12135: int *aiType /* Column types. Might be NULL */
12136: ){
12137: int i;
12138: ShellState *p = (ShellState*)pArg;
12139:
12140: if( azArg==0 ) return 0;
12141: switch( p->cMode ){
12142: case MODE_Line: {
12143: int w = 5;
12144: if( azArg==0 ) break;
12145: for(i=0; i<nArg; i++){
12146: int len = strlen30(azCol[i] ? azCol[i] : "");
12147: if( len>w ) w = len;
12148: }
12149: if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12150: for(i=0; i<nArg; i++){
12151: utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12152: azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12153: }
12154: break;
12155: }
12156: case MODE_Explain: {
12157: static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12158: if( nArg>ArraySize(aExplainWidth) ){
12159: nArg = ArraySize(aExplainWidth);
12160: }
12161: if( p->cnt++==0 ){
12162: for(i=0; i<nArg; i++){
12163: int w = aExplainWidth[i];
12164: utf8_width_print(p->out, w, azCol[i]);
12165: fputs(i==nArg-1 ? "\n" : " ", p->out);
12166: }
12167: for(i=0; i<nArg; i++){
12168: int w = aExplainWidth[i];
12169: print_dashes(p->out, w);
12170: fputs(i==nArg-1 ? "\n" : " ", p->out);
12171: }
12172: }
12173: if( azArg==0 ) break;
12174: for(i=0; i<nArg; i++){
12175: int w = aExplainWidth[i];
12176: if( i==nArg-1 ) w = 0;
12177: if( azArg[i] && strlenChar(azArg[i])>w ){
12178: w = strlenChar(azArg[i]);
12179: }
12180: if( i==1 && p->aiIndent && p->pStmt ){
12181: if( p->iIndent<p->nIndent ){
12182: utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12183: }
12184: p->iIndent++;
12185: }
12186: utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12187: fputs(i==nArg-1 ? "\n" : " ", p->out);
12188: }
12189: break;
12190: }
12191: case MODE_Semi: { /* .schema and .fullschema output */
12192: printSchemaLine(p->out, azArg[0], ";\n");
12193: break;
12194: }
12195: case MODE_Pretty: { /* .schema and .fullschema with --indent */
12196: char *z;
12197: int j;
12198: int nParen = 0;
12199: char cEnd = 0;
12200: char c;
12201: int nLine = 0;
12202: assert( nArg==1 );
12203: if( azArg[0]==0 ) break;
12204: if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
12205: || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
12206: ){
12207: utf8_printf(p->out, "%s;\n", azArg[0]);
12208: break;
12209: }
12210: z = sqlite3_mprintf("%s", azArg[0]);
12211: j = 0;
12212: for(i=0; IsSpace(z[i]); i++){}
12213: for(; (c = z[i])!=0; i++){
12214: if( IsSpace(c) ){
12215: if( z[j-1]=='\r' ) z[j-1] = '\n';
12216: if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
12217: }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
12218: j--;
12219: }
12220: z[j++] = c;
12221: }
12222: while( j>0 && IsSpace(z[j-1]) ){ j--; }
12223: z[j] = 0;
12224: if( strlen30(z)>=79 ){
12225: for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
12226: if( c==cEnd ){
12227: cEnd = 0;
12228: }else if( c=='"' || c=='\'' || c=='`' ){
12229: cEnd = c;
12230: }else if( c=='[' ){
12231: cEnd = ']';
12232: }else if( c=='-' && z[i+1]=='-' ){
12233: cEnd = '\n';
12234: }else if( c=='(' ){
12235: nParen++;
12236: }else if( c==')' ){
12237: nParen--;
12238: if( nLine>0 && nParen==0 && j>0 ){
12239: printSchemaLineN(p->out, z, j, "\n");
12240: j = 0;
12241: }
12242: }
12243: z[j++] = c;
12244: if( nParen==1 && cEnd==0
12245: && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
12246: ){
12247: if( c=='\n' ) j--;
12248: printSchemaLineN(p->out, z, j, "\n ");
12249: j = 0;
12250: nLine++;
12251: while( IsSpace(z[i+1]) ){ i++; }
12252: }
12253: }
12254: z[j] = 0;
12255: }
12256: printSchemaLine(p->out, z, ";\n");
12257: sqlite3_free(z);
12258: break;
12259: }
12260: case MODE_List: {
12261: if( p->cnt++==0 && p->showHeader ){
12262: for(i=0; i<nArg; i++){
12263: utf8_printf(p->out,"%s%s",azCol[i],
12264: i==nArg-1 ? p->rowSeparator : p->colSeparator);
12265: }
12266: }
12267: if( azArg==0 ) break;
12268: for(i=0; i<nArg; i++){
12269: char *z = azArg[i];
12270: if( z==0 ) z = p->nullValue;
12271: utf8_printf(p->out, "%s", z);
12272: if( i<nArg-1 ){
12273: utf8_printf(p->out, "%s", p->colSeparator);
12274: }else{
12275: utf8_printf(p->out, "%s", p->rowSeparator);
12276: }
12277: }
12278: break;
12279: }
12280: case MODE_Html: {
12281: if( p->cnt++==0 && p->showHeader ){
12282: raw_printf(p->out,"<TR>");
12283: for(i=0; i<nArg; i++){
12284: raw_printf(p->out,"<TH>");
12285: output_html_string(p->out, azCol[i]);
12286: raw_printf(p->out,"</TH>\n");
12287: }
12288: raw_printf(p->out,"</TR>\n");
12289: }
12290: if( azArg==0 ) break;
12291: raw_printf(p->out,"<TR>");
12292: for(i=0; i<nArg; i++){
12293: raw_printf(p->out,"<TD>");
12294: output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12295: raw_printf(p->out,"</TD>\n");
12296: }
12297: raw_printf(p->out,"</TR>\n");
12298: break;
12299: }
12300: case MODE_Tcl: {
12301: if( p->cnt++==0 && p->showHeader ){
12302: for(i=0; i<nArg; i++){
12303: output_c_string(p->out,azCol[i] ? azCol[i] : "");
12304: if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12305: }
12306: utf8_printf(p->out, "%s", p->rowSeparator);
12307: }
12308: if( azArg==0 ) break;
12309: for(i=0; i<nArg; i++){
12310: output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12311: if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12312: }
12313: utf8_printf(p->out, "%s", p->rowSeparator);
12314: break;
12315: }
12316: case MODE_Csv: {
12317: setBinaryMode(p->out, 1);
12318: if( p->cnt++==0 && p->showHeader ){
12319: for(i=0; i<nArg; i++){
12320: output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
12321: }
12322: utf8_printf(p->out, "%s", p->rowSeparator);
12323: }
12324: if( nArg>0 ){
12325: for(i=0; i<nArg; i++){
12326: output_csv(p, azArg[i], i<nArg-1);
12327: }
12328: utf8_printf(p->out, "%s", p->rowSeparator);
12329: }
12330: setTextMode(p->out, 1);
12331: break;
12332: }
12333: case MODE_Insert: {
12334: if( azArg==0 ) break;
12335: utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
12336: if( p->showHeader ){
12337: raw_printf(p->out,"(");
12338: for(i=0; i<nArg; i++){
12339: if( i>0 ) raw_printf(p->out, ",");
12340: if( quoteChar(azCol[i]) ){
12341: char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
12342: utf8_printf(p->out, "%s", z);
12343: sqlite3_free(z);
12344: }else{
12345: raw_printf(p->out, "%s", azCol[i]);
12346: }
12347: }
12348: raw_printf(p->out,")");
12349: }
12350: p->cnt++;
12351: for(i=0; i<nArg; i++){
12352: raw_printf(p->out, i>0 ? "," : " VALUES(");
12353: if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12354: utf8_printf(p->out,"NULL");
12355: }else if( aiType && aiType[i]==SQLITE_TEXT ){
12356: if( ShellHasFlag(p, SHFLG_Newlines) ){
12357: output_quoted_string(p->out, azArg[i]);
12358: }else{
12359: output_quoted_escaped_string(p->out, azArg[i]);
12360: }
12361: }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12362: utf8_printf(p->out,"%s", azArg[i]);
12363: }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12364: char z[50];
12365: double r = sqlite3_column_double(p->pStmt, i);
12366: sqlite3_uint64 ur;
12367: memcpy(&ur,&r,sizeof(r));
12368: if( ur==0x7ff0000000000000LL ){
12369: raw_printf(p->out, "1e999");
12370: }else if( ur==0xfff0000000000000LL ){
12371: raw_printf(p->out, "-1e999");
12372: }else{
12373: sqlite3_snprintf(50,z,"%!.20g", r);
12374: raw_printf(p->out, "%s", z);
12375: }
12376: }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12377: const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12378: int nBlob = sqlite3_column_bytes(p->pStmt, i);
12379: output_hex_blob(p->out, pBlob, nBlob);
12380: }else if( isNumber(azArg[i], 0) ){
12381: utf8_printf(p->out,"%s", azArg[i]);
12382: }else if( ShellHasFlag(p, SHFLG_Newlines) ){
12383: output_quoted_string(p->out, azArg[i]);
12384: }else{
12385: output_quoted_escaped_string(p->out, azArg[i]);
12386: }
12387: }
12388: raw_printf(p->out,");\n");
12389: break;
12390: }
12391: case MODE_Json: {
12392: if( azArg==0 ) break;
12393: if( p->cnt==0 ){
12394: fputs("[{", p->out);
12395: }else{
12396: fputs(",\n{", p->out);
12397: }
12398: p->cnt++;
12399: for(i=0; i<nArg; i++){
12400: output_json_string(p->out, azCol[i], -1);
12401: putc(':', p->out);
12402: if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12403: fputs("null",p->out);
12404: }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12405: char z[50];
12406: double r = sqlite3_column_double(p->pStmt, i);
12407: sqlite3_uint64 ur;
12408: memcpy(&ur,&r,sizeof(r));
12409: if( ur==0x7ff0000000000000LL ){
12410: raw_printf(p->out, "1e999");
12411: }else if( ur==0xfff0000000000000LL ){
12412: raw_printf(p->out, "-1e999");
12413: }else{
12414: sqlite3_snprintf(50,z,"%!.20g", r);
12415: raw_printf(p->out, "%s", z);
12416: }
12417: }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12418: const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12419: int nBlob = sqlite3_column_bytes(p->pStmt, i);
12420: output_json_string(p->out, pBlob, nBlob);
12421: }else if( aiType && aiType[i]==SQLITE_TEXT ){
12422: output_json_string(p->out, azArg[i], -1);
12423: }else{
12424: utf8_printf(p->out,"%s", azArg[i]);
12425: }
12426: if( i<nArg-1 ){
12427: putc(',', p->out);
12428: }
12429: }
12430: putc('}', p->out);
12431: break;
12432: }
12433: case MODE_Quote: {
12434: if( azArg==0 ) break;
12435: if( p->cnt==0 && p->showHeader ){
12436: for(i=0; i<nArg; i++){
12437: if( i>0 ) fputs(p->colSeparator, p->out);
12438: output_quoted_string(p->out, azCol[i]);
12439: }
12440: fputs(p->rowSeparator, p->out);
12441: }
12442: p->cnt++;
12443: for(i=0; i<nArg; i++){
12444: if( i>0 ) fputs(p->colSeparator, p->out);
12445: if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12446: utf8_printf(p->out,"NULL");
12447: }else if( aiType && aiType[i]==SQLITE_TEXT ){
12448: output_quoted_string(p->out, azArg[i]);
12449: }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12450: utf8_printf(p->out,"%s", azArg[i]);
12451: }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12452: char z[50];
12453: double r = sqlite3_column_double(p->pStmt, i);
12454: sqlite3_snprintf(50,z,"%!.20g", r);
12455: raw_printf(p->out, "%s", z);
12456: }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12457: const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12458: int nBlob = sqlite3_column_bytes(p->pStmt, i);
12459: output_hex_blob(p->out, pBlob, nBlob);
12460: }else if( isNumber(azArg[i], 0) ){
12461: utf8_printf(p->out,"%s", azArg[i]);
12462: }else{
12463: output_quoted_string(p->out, azArg[i]);
12464: }
12465: }
12466: fputs(p->rowSeparator, p->out);
12467: break;
12468: }
12469: case MODE_Ascii: {
12470: if( p->cnt++==0 && p->showHeader ){
12471: for(i=0; i<nArg; i++){
12472: if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12473: utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
12474: }
12475: utf8_printf(p->out, "%s", p->rowSeparator);
12476: }
12477: if( azArg==0 ) break;
12478: for(i=0; i<nArg; i++){
12479: if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12480: utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
12481: }
12482: utf8_printf(p->out, "%s", p->rowSeparator);
12483: break;
12484: }
12485: case MODE_EQP: {
12486: eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
12487: break;
12488: }
12489: }
12490: return 0;
12491: }
12492:
12493: /*
12494: ** This is the callback routine that the SQLite library
12495: ** invokes for each row of a query result.
12496: */
12497: static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12498: /* since we don't have type info, call the shell_callback with a NULL value */
12499: return shell_callback(pArg, nArg, azArg, azCol, NULL);
12500: }
12501:
12502: /*
12503: ** This is the callback routine from sqlite3_exec() that appends all
12504: ** output onto the end of a ShellText object.
12505: */
12506: static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
12507: ShellText *p = (ShellText*)pArg;
12508: int i;
12509: UNUSED_PARAMETER(az);
12510: if( azArg==0 ) return 0;
12511: if( p->n ) appendText(p, "|", 0);
12512: for(i=0; i<nArg; i++){
12513: if( i ) appendText(p, ",", 0);
12514: if( azArg[i] ) appendText(p, azArg[i], 0);
12515: }
12516: return 0;
12517: }
12518:
12519: /*
12520: ** Generate an appropriate SELFTEST table in the main database.
12521: */
12522: static void createSelftestTable(ShellState *p){
12523: char *zErrMsg = 0;
12524: sqlite3_exec(p->db,
12525: "SAVEPOINT selftest_init;\n"
12526: "CREATE TABLE IF NOT EXISTS selftest(\n"
12527: " tno INTEGER PRIMARY KEY,\n" /* Test number */
12528: " op TEXT,\n" /* Operator: memo run */
12529: " cmd TEXT,\n" /* Command text */
12530: " ans TEXT\n" /* Desired answer */
12531: ");"
12532: "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
12533: "INSERT INTO [_shell$self](rowid,op,cmd)\n"
12534: " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
12535: " 'memo','Tests generated by --init');\n"
12536: "INSERT INTO [_shell$self]\n"
12537: " SELECT 'run',\n"
12538: " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
12539: "FROM sqlite_schema ORDER BY 2'',224))',\n"
12540: " hex(sha3_query('SELECT type,name,tbl_name,sql "
12541: "FROM sqlite_schema ORDER BY 2',224));\n"
12542: "INSERT INTO [_shell$self]\n"
12543: " SELECT 'run',"
12544: " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
12545: " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
12546: " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
12547: " FROM (\n"
12548: " SELECT name FROM sqlite_schema\n"
12549: " WHERE type='table'\n"
12550: " AND name<>'selftest'\n"
12551: " AND coalesce(rootpage,0)>0\n"
12552: " )\n"
12553: " ORDER BY name;\n"
12554: "INSERT INTO [_shell$self]\n"
12555: " VALUES('run','PRAGMA integrity_check','ok');\n"
12556: "INSERT INTO selftest(tno,op,cmd,ans)"
12557: " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
12558: "DROP TABLE [_shell$self];"
12559: ,0,0,&zErrMsg);
12560: if( zErrMsg ){
12561: utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
12562: sqlite3_free(zErrMsg);
12563: }
12564: sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
12565: }
12566:
12567:
12568: /*
12569: ** Set the destination table field of the ShellState structure to
12570: ** the name of the table given. Escape any quote characters in the
12571: ** table name.
12572: */
12573: static void set_table_name(ShellState *p, const char *zName){
12574: int i, n;
12575: char cQuote;
12576: char *z;
12577:
12578: if( p->zDestTable ){
12579: free(p->zDestTable);
12580: p->zDestTable = 0;
12581: }
12582: if( zName==0 ) return;
12583: cQuote = quoteChar(zName);
12584: n = strlen30(zName);
12585: if( cQuote ) n += n+2;
12586: z = p->zDestTable = malloc( n+1 );
12587: if( z==0 ) shell_out_of_memory();
12588: n = 0;
12589: if( cQuote ) z[n++] = cQuote;
12590: for(i=0; zName[i]; i++){
12591: z[n++] = zName[i];
12592: if( zName[i]==cQuote ) z[n++] = cQuote;
12593: }
12594: if( cQuote ) z[n++] = cQuote;
12595: z[n] = 0;
12596: }
12597:
12598:
12599: /*
12600: ** Execute a query statement that will generate SQL output. Print
12601: ** the result columns, comma-separated, on a line and then add a
12602: ** semicolon terminator to the end of that line.
12603: **
12604: ** If the number of columns is 1 and that column contains text "--"
12605: ** then write the semicolon on a separate line. That way, if a
12606: ** "--" comment occurs at the end of the statement, the comment
12607: ** won't consume the semicolon terminator.
12608: */
12609: static int run_table_dump_query(
12610: ShellState *p, /* Query context */
12611: const char *zSelect /* SELECT statement to extract content */
12612: ){
12613: sqlite3_stmt *pSelect;
12614: int rc;
12615: int nResult;
12616: int i;
12617: const char *z;
12618: rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
12619: if( rc!=SQLITE_OK || !pSelect ){
12620: utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12621: sqlite3_errmsg(p->db));
12622: if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12623: return rc;
12624: }
12625: rc = sqlite3_step(pSelect);
12626: nResult = sqlite3_column_count(pSelect);
12627: while( rc==SQLITE_ROW ){
12628: z = (const char*)sqlite3_column_text(pSelect, 0);
12629: utf8_printf(p->out, "%s", z);
12630: for(i=1; i<nResult; i++){
12631: utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
12632: }
12633: if( z==0 ) z = "";
12634: while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
12635: if( z[0] ){
12636: raw_printf(p->out, "\n;\n");
12637: }else{
12638: raw_printf(p->out, ";\n");
12639: }
12640: rc = sqlite3_step(pSelect);
12641: }
12642: rc = sqlite3_finalize(pSelect);
12643: if( rc!=SQLITE_OK ){
12644: utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12645: sqlite3_errmsg(p->db));
12646: if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12647: }
12648: return rc;
12649: }
12650:
12651: /*
12652: ** Allocate space and save off current error string.
12653: */
12654: static char *save_err_msg(
12655: sqlite3 *db /* Database to query */
12656: ){
12657: int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
12658: char *zErrMsg = sqlite3_malloc64(nErrMsg);
12659: if( zErrMsg ){
12660: memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
12661: }
12662: return zErrMsg;
12663: }
12664:
12665: #ifdef __linux__
12666: /*
12667: ** Attempt to display I/O stats on Linux using /proc/PID/io
12668: */
12669: static void displayLinuxIoStats(FILE *out){
12670: FILE *in;
12671: char z[200];
12672: sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
12673: in = fopen(z, "rb");
12674: if( in==0 ) return;
12675: while( fgets(z, sizeof(z), in)!=0 ){
12676: static const struct {
12677: const char *zPattern;
12678: const char *zDesc;
12679: } aTrans[] = {
12680: { "rchar: ", "Bytes received by read():" },
12681: { "wchar: ", "Bytes sent to write():" },
12682: { "syscr: ", "Read() system calls:" },
12683: { "syscw: ", "Write() system calls:" },
12684: { "read_bytes: ", "Bytes read from storage:" },
12685: { "write_bytes: ", "Bytes written to storage:" },
12686: { "cancelled_write_bytes: ", "Cancelled write bytes:" },
12687: };
12688: int i;
12689: for(i=0; i<ArraySize(aTrans); i++){
12690: int n = strlen30(aTrans[i].zPattern);
12691: if( strncmp(aTrans[i].zPattern, z, n)==0 ){
12692: utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
12693: break;
12694: }
12695: }
12696: }
12697: fclose(in);
12698: }
12699: #endif
12700:
12701: /*
12702: ** Display a single line of status using 64-bit values.
12703: */
12704: static void displayStatLine(
12705: ShellState *p, /* The shell context */
12706: char *zLabel, /* Label for this one line */
12707: char *zFormat, /* Format for the result */
12708: int iStatusCtrl, /* Which status to display */
12709: int bReset /* True to reset the stats */
12710: ){
12711: sqlite3_int64 iCur = -1;
12712: sqlite3_int64 iHiwtr = -1;
12713: int i, nPercent;
12714: char zLine[200];
12715: sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
12716: for(i=0, nPercent=0; zFormat[i]; i++){
12717: if( zFormat[i]=='%' ) nPercent++;
12718: }
12719: if( nPercent>1 ){
12720: sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
12721: }else{
12722: sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
12723: }
12724: raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
12725: }
12726:
12727: /*
12728: ** Display memory stats.
12729: */
12730: static int display_stats(
12731: sqlite3 *db, /* Database to query */
12732: ShellState *pArg, /* Pointer to ShellState */
12733: int bReset /* True to reset the stats */
12734: ){
12735: int iCur;
12736: int iHiwtr;
12737: FILE *out;
12738: if( pArg==0 || pArg->out==0 ) return 0;
12739: out = pArg->out;
12740:
12741: if( pArg->pStmt && pArg->statsOn==2 ){
12742: int nCol, i, x;
12743: sqlite3_stmt *pStmt = pArg->pStmt;
12744: char z[100];
12745: nCol = sqlite3_column_count(pStmt);
12746: raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
12747: for(i=0; i<nCol; i++){
12748: sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
12749: utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
12750: #ifndef SQLITE_OMIT_DECLTYPE
12751: sqlite3_snprintf(30, z+x, "declared type:");
12752: utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
12753: #endif
12754: #ifdef SQLITE_ENABLE_COLUMN_METADATA
12755: sqlite3_snprintf(30, z+x, "database name:");
12756: utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
12757: sqlite3_snprintf(30, z+x, "table name:");
12758: utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
12759: sqlite3_snprintf(30, z+x, "origin name:");
12760: utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
12761: #endif
12762: }
12763: }
12764:
12765: if( pArg->statsOn==3 ){
12766: if( pArg->pStmt ){
12767: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12768: raw_printf(pArg->out, "VM-steps: %d\n", iCur);
12769: }
12770: return 0;
12771: }
12772:
12773: displayStatLine(pArg, "Memory Used:",
12774: "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
12775: displayStatLine(pArg, "Number of Outstanding Allocations:",
12776: "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
12777: if( pArg->shellFlgs & SHFLG_Pagecache ){
12778: displayStatLine(pArg, "Number of Pcache Pages Used:",
12779: "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
12780: }
12781: displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
12782: "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
12783: displayStatLine(pArg, "Largest Allocation:",
12784: "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
12785: displayStatLine(pArg, "Largest Pcache Allocation:",
12786: "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
12787: #ifdef YYTRACKMAXSTACKDEPTH
12788: displayStatLine(pArg, "Deepest Parser Stack:",
12789: "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
12790: #endif
12791:
12792: if( db ){
12793: if( pArg->shellFlgs & SHFLG_Lookaside ){
12794: iHiwtr = iCur = -1;
12795: sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
12796: &iCur, &iHiwtr, bReset);
12797: raw_printf(pArg->out,
12798: "Lookaside Slots Used: %d (max %d)\n",
12799: iCur, iHiwtr);
12800: sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
12801: &iCur, &iHiwtr, bReset);
12802: raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
12803: iHiwtr);
12804: sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
12805: &iCur, &iHiwtr, bReset);
12806: raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
12807: iHiwtr);
12808: sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
12809: &iCur, &iHiwtr, bReset);
12810: raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
12811: iHiwtr);
12812: }
12813: iHiwtr = iCur = -1;
12814: sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
12815: raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
12816: iCur);
12817: iHiwtr = iCur = -1;
12818: sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
12819: raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
12820: iHiwtr = iCur = -1;
12821: sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
12822: raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
12823: iHiwtr = iCur = -1;
12824: sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
12825: raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
12826: iHiwtr = iCur = -1;
12827: sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
12828: raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
12829: iHiwtr = iCur = -1;
12830: sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
12831: raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
12832: iCur);
12833: iHiwtr = iCur = -1;
12834: sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
12835: raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
12836: iCur);
12837: }
12838:
12839: if( pArg->pStmt ){
12840: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
12841: bReset);
12842: raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
12843: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
12844: raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
12845: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
12846: raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
12847: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12848: raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
12849: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
12850: raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
12851: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
12852: raw_printf(pArg->out, "Number of times run: %d\n", iCur);
12853: iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
12854: raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
12855: }
12856:
12857: #ifdef __linux__
12858: displayLinuxIoStats(pArg->out);
12859: #endif
12860:
12861: /* Do not remove this machine readable comment: extra-stats-output-here */
12862:
12863: return 0;
12864: }
12865:
12866: /*
12867: ** Display scan stats.
12868: */
12869: static void display_scanstats(
12870: sqlite3 *db, /* Database to query */
12871: ShellState *pArg /* Pointer to ShellState */
12872: ){
12873: #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
12874: UNUSED_PARAMETER(db);
12875: UNUSED_PARAMETER(pArg);
12876: #else
12877: int i, k, n, mx;
12878: raw_printf(pArg->out, "-------- scanstats --------\n");
12879: mx = 0;
12880: for(k=0; k<=mx; k++){
12881: double rEstLoop = 1.0;
12882: for(i=n=0; 1; i++){
12883: sqlite3_stmt *p = pArg->pStmt;
12884: sqlite3_int64 nLoop, nVisit;
12885: double rEst;
12886: int iSid;
12887: const char *zExplain;
12888: if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
12889: break;
12890: }
12891: sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
12892: if( iSid>mx ) mx = iSid;
12893: if( iSid!=k ) continue;
12894: if( n==0 ){
12895: rEstLoop = (double)nLoop;
12896: if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
12897: }
12898: n++;
12899: sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
12900: sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
12901: sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
12902: utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
12903: rEstLoop *= rEst;
12904: raw_printf(pArg->out,
12905: " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
12906: nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
12907: );
12908: }
12909: }
12910: raw_printf(pArg->out, "---------------------------\n");
12911: #endif
12912: }
12913:
12914: /*
12915: ** Parameter azArray points to a zero-terminated array of strings. zStr
12916: ** points to a single nul-terminated string. Return non-zero if zStr
12917: ** is equal, according to strcmp(), to any of the strings in the array.
12918: ** Otherwise, return zero.
12919: */
12920: static int str_in_array(const char *zStr, const char **azArray){
12921: int i;
12922: for(i=0; azArray[i]; i++){
12923: if( 0==strcmp(zStr, azArray[i]) ) return 1;
12924: }
12925: return 0;
12926: }
12927:
12928: /*
12929: ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
12930: ** and populate the ShellState.aiIndent[] array with the number of
12931: ** spaces each opcode should be indented before it is output.
12932: **
12933: ** The indenting rules are:
12934: **
12935: ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
12936: ** all opcodes that occur between the p2 jump destination and the opcode
12937: ** itself by 2 spaces.
12938: **
12939: ** * For each "Goto", if the jump destination is earlier in the program
12940: ** and ends on one of:
12941: ** Yield SeekGt SeekLt RowSetRead Rewind
12942: ** or if the P1 parameter is one instead of zero,
12943: ** then indent all opcodes between the earlier instruction
12944: ** and "Goto" by 2 spaces.
12945: */
12946: static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12947: const char *zSql; /* The text of the SQL statement */
12948: const char *z; /* Used to check if this is an EXPLAIN */
12949: int *abYield = 0; /* True if op is an OP_Yield */
12950: int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12951: int iOp; /* Index of operation in p->aiIndent[] */
12952:
12953: const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
12954: const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
12955: "Rewind", 0 };
12956: const char *azGoto[] = { "Goto", 0 };
12957:
12958: /* Try to figure out if this is really an EXPLAIN statement. If this
12959: ** cannot be verified, return early. */
12960: if( sqlite3_column_count(pSql)!=8 ){
12961: p->cMode = p->mode;
12962: return;
12963: }
12964: zSql = sqlite3_sql(pSql);
12965: if( zSql==0 ) return;
12966: for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
12967: if( sqlite3_strnicmp(z, "explain", 7) ){
12968: p->cMode = p->mode;
12969: return;
12970: }
12971:
12972: for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12973: int i;
12974: int iAddr = sqlite3_column_int(pSql, 0);
12975: const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
12976:
12977: /* Set p2 to the P2 field of the current opcode. Then, assuming that
12978: ** p2 is an instruction address, set variable p2op to the index of that
12979: ** instruction in the aiIndent[] array. p2 and p2op may be different if
12980: ** the current instruction is part of a sub-program generated by an
12981: ** SQL trigger or foreign key. */
12982: int p2 = sqlite3_column_int(pSql, 3);
12983: int p2op = (p2 + (iOp-iAddr));
12984:
12985: /* Grow the p->aiIndent array as required */
12986: if( iOp>=nAlloc ){
12987: if( iOp==0 ){
12988: /* Do further verfication that this is explain output. Abort if
12989: ** it is not */
12990: static const char *explainCols[] = {
12991: "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
12992: int jj;
12993: for(jj=0; jj<ArraySize(explainCols); jj++){
12994: if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
12995: p->cMode = p->mode;
12996: sqlite3_reset(pSql);
12997: return;
12998: }
12999: }
13000: }
13001: nAlloc += 100;
13002: p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
13003: if( p->aiIndent==0 ) shell_out_of_memory();
13004: abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
13005: if( abYield==0 ) shell_out_of_memory();
13006: }
13007: abYield[iOp] = str_in_array(zOp, azYield);
13008: p->aiIndent[iOp] = 0;
13009: p->nIndent = iOp+1;
13010:
13011: if( str_in_array(zOp, azNext) ){
13012: for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13013: }
13014: if( str_in_array(zOp, azGoto) && p2op<p->nIndent
13015: && (abYield[p2op] || sqlite3_column_int(pSql, 2))
13016: ){
13017: for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13018: }
13019: }
13020:
13021: p->iIndent = 0;
13022: sqlite3_free(abYield);
13023: sqlite3_reset(pSql);
13024: }
13025:
13026: /*
13027: ** Free the array allocated by explain_data_prepare().
13028: */
13029: static void explain_data_delete(ShellState *p){
13030: sqlite3_free(p->aiIndent);
13031: p->aiIndent = 0;
13032: p->nIndent = 0;
13033: p->iIndent = 0;
13034: }
13035:
13036: /*
13037: ** Disable and restore .wheretrace and .selecttrace settings.
13038: */
13039: static unsigned int savedSelectTrace;
13040: static unsigned int savedWhereTrace;
13041: static void disable_debug_trace_modes(void){
13042: unsigned int zero = 0;
13043: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
13044: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
13045: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
13046: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
13047: }
13048: static void restore_debug_trace_modes(void){
13049: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
13050: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
13051: }
13052:
13053: /* Create the TEMP table used to store parameter bindings */
13054: static void bind_table_init(ShellState *p){
13055: int wrSchema = 0;
13056: int defensiveMode = 0;
13057: sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
13058: sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
13059: sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
13060: sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
13061: sqlite3_exec(p->db,
13062: "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
13063: " key TEXT PRIMARY KEY,\n"
13064: " value ANY\n"
13065: ") WITHOUT ROWID;",
13066: 0, 0, 0);
13067: sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
13068: sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
13069: }
13070:
13071: /*
13072: ** Bind parameters on a prepared statement.
13073: **
13074: ** Parameter bindings are taken from a TEMP table of the form:
13075: **
13076: ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
13077: ** WITHOUT ROWID;
13078: **
13079: ** No bindings occur if this table does not exist. The name of the table
13080: ** begins with "sqlite_" so that it will not collide with ordinary application
13081: ** tables. The table must be in the TEMP schema.
13082: */
13083: static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
13084: int nVar;
13085: int i;
13086: int rc;
13087: sqlite3_stmt *pQ = 0;
13088:
13089: nVar = sqlite3_bind_parameter_count(pStmt);
13090: if( nVar==0 ) return; /* Nothing to do */
13091: if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
13092: "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
13093: return; /* Parameter table does not exist */
13094: }
13095: rc = sqlite3_prepare_v2(pArg->db,
13096: "SELECT value FROM temp.sqlite_parameters"
13097: " WHERE key=?1", -1, &pQ, 0);
13098: if( rc || pQ==0 ) return;
13099: for(i=1; i<=nVar; i++){
13100: char zNum[30];
13101: const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
13102: if( zVar==0 ){
13103: sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
13104: zVar = zNum;
13105: }
13106: sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
13107: if( sqlite3_step(pQ)==SQLITE_ROW ){
13108: sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
13109: }else{
13110: sqlite3_bind_null(pStmt, i);
13111: }
13112: sqlite3_reset(pQ);
13113: }
13114: sqlite3_finalize(pQ);
13115: }
13116:
13117: /*
13118: ** UTF8 box-drawing characters. Imagine box lines like this:
13119: **
13120: ** 1
13121: ** |
13122: ** 4 --+-- 2
13123: ** |
13124: ** 3
13125: **
13126: ** Each box characters has between 2 and 4 of the lines leading from
13127: ** the center. The characters are here identified by the numbers of
13128: ** their corresponding lines.
13129: */
13130: #define BOX_24 "\342\224\200" /* U+2500 --- */
13131: #define BOX_13 "\342\224\202" /* U+2502 | */
13132: #define BOX_23 "\342\224\214" /* U+250c ,- */
13133: #define BOX_34 "\342\224\220" /* U+2510 -, */
13134: #define BOX_12 "\342\224\224" /* U+2514 '- */
13135: #define BOX_14 "\342\224\230" /* U+2518 -' */
13136: #define BOX_123 "\342\224\234" /* U+251c |- */
13137: #define BOX_134 "\342\224\244" /* U+2524 -| */
13138: #define BOX_234 "\342\224\254" /* U+252c -,- */
13139: #define BOX_124 "\342\224\264" /* U+2534 -'- */
13140: #define BOX_1234 "\342\224\274" /* U+253c -|- */
13141:
13142: /* Draw horizontal line N characters long using unicode box
13143: ** characters
13144: */
13145: static void print_box_line(FILE *out, int N){
13146: const char zDash[] =
13147: BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13148: BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13149: const int nDash = sizeof(zDash) - 1;
13150: N *= 3;
13151: while( N>nDash ){
13152: utf8_printf(out, zDash);
13153: N -= nDash;
13154: }
13155: utf8_printf(out, "%.*s", N, zDash);
13156: }
13157:
13158: /*
13159: ** Draw a horizontal separator for a MODE_Box table.
13160: */
13161: static void print_box_row_separator(
13162: ShellState *p,
13163: int nArg,
13164: const char *zSep1,
13165: const char *zSep2,
13166: const char *zSep3
13167: ){
13168: int i;
13169: if( nArg>0 ){
13170: utf8_printf(p->out, "%s", zSep1);
13171: print_box_line(p->out, p->actualWidth[0]+2);
13172: for(i=1; i<nArg; i++){
13173: utf8_printf(p->out, "%s", zSep2);
13174: print_box_line(p->out, p->actualWidth[i]+2);
13175: }
13176: utf8_printf(p->out, "%s", zSep3);
13177: }
13178: fputs("\n", p->out);
13179: }
13180:
13181:
13182:
13183: /*
13184: ** Run a prepared statement and output the result in one of the
13185: ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13186: ** or MODE_Box.
13187: **
13188: ** This is different from ordinary exec_prepared_stmt() in that
13189: ** it has to run the entire query and gather the results into memory
13190: ** first, in order to determine column widths, before providing
13191: ** any output.
13192: */
13193: static void exec_prepared_stmt_columnar(
13194: ShellState *p, /* Pointer to ShellState */
13195: sqlite3_stmt *pStmt /* Statment to run */
13196: ){
13197: sqlite3_int64 nRow = 0;
13198: int nColumn = 0;
13199: char **azData = 0;
13200: sqlite3_int64 nAlloc = 0;
13201: const char *z;
13202: int rc;
13203: sqlite3_int64 i, nData;
13204: int j, nTotal, w, n;
13205: const char *colSep = 0;
13206: const char *rowSep = 0;
13207:
13208: rc = sqlite3_step(pStmt);
13209: if( rc!=SQLITE_ROW ) return;
13210: nColumn = sqlite3_column_count(pStmt);
13211: nAlloc = nColumn*4;
13212: azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
13213: if( azData==0 ) shell_out_of_memory();
13214: for(i=0; i<nColumn; i++){
13215: azData[i] = strdup(sqlite3_column_name(pStmt,i));
13216: }
13217: do{
13218: if( (nRow+2)*nColumn >= nAlloc ){
13219: nAlloc *= 2;
13220: azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
13221: if( azData==0 ) shell_out_of_memory();
13222: }
13223: nRow++;
13224: for(i=0; i<nColumn; i++){
13225: z = (const char*)sqlite3_column_text(pStmt,i);
13226: azData[nRow*nColumn + i] = z ? strdup(z) : 0;
13227: }
13228: }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
13229: if( nColumn>p->nWidth ){
13230: p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
13231: if( p->colWidth==0 ) shell_out_of_memory();
13232: for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
13233: p->nWidth = nColumn;
13234: p->actualWidth = &p->colWidth[nColumn];
13235: }
13236: memset(p->actualWidth, 0, nColumn*sizeof(int));
13237: for(i=0; i<nColumn; i++){
13238: w = p->colWidth[i];
13239: if( w<0 ) w = -w;
13240: p->actualWidth[i] = w;
13241: }
13242: nTotal = nColumn*(nRow+1);
13243: for(i=0; i<nTotal; i++){
13244: z = azData[i];
13245: if( z==0 ) z = p->nullValue;
13246: n = strlenChar(z);
13247: j = i%nColumn;
13248: if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
13249: }
13250: if( seenInterrupt ) goto columnar_end;
13251: switch( p->cMode ){
13252: case MODE_Column: {
13253: colSep = " ";
13254: rowSep = "\n";
13255: if( p->showHeader ){
13256: for(i=0; i<nColumn; i++){
13257: w = p->actualWidth[i];
13258: if( p->colWidth[i]<0 ) w = -w;
13259: utf8_width_print(p->out, w, azData[i]);
13260: fputs(i==nColumn-1?"\n":" ", p->out);
13261: }
13262: for(i=0; i<nColumn; i++){
13263: print_dashes(p->out, p->actualWidth[i]);
13264: fputs(i==nColumn-1?"\n":" ", p->out);
13265: }
13266: }
13267: break;
13268: }
13269: case MODE_Table: {
13270: colSep = " | ";
13271: rowSep = " |\n";
13272: print_row_separator(p, nColumn, "+");
13273: fputs("| ", p->out);
13274: for(i=0; i<nColumn; i++){
13275: w = p->actualWidth[i];
13276: n = strlenChar(azData[i]);
13277: utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13278: fputs(i==nColumn-1?" |\n":" | ", p->out);
13279: }
13280: print_row_separator(p, nColumn, "+");
13281: break;
13282: }
13283: case MODE_Markdown: {
13284: colSep = " | ";
13285: rowSep = " |\n";
13286: fputs("| ", p->out);
13287: for(i=0; i<nColumn; i++){
13288: w = p->actualWidth[i];
13289: n = strlenChar(azData[i]);
13290: utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13291: fputs(i==nColumn-1?" |\n":" | ", p->out);
13292: }
13293: print_row_separator(p, nColumn, "|");
13294: break;
13295: }
13296: case MODE_Box: {
13297: colSep = " " BOX_13 " ";
13298: rowSep = " " BOX_13 "\n";
13299: print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
13300: utf8_printf(p->out, BOX_13 " ");
13301: for(i=0; i<nColumn; i++){
13302: w = p->actualWidth[i];
13303: n = strlenChar(azData[i]);
13304: utf8_printf(p->out, "%*s%s%*s%s",
13305: (w-n)/2, "", azData[i], (w-n+1)/2, "",
13306: i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
13307: }
13308: print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
13309: break;
13310: }
13311: }
13312: for(i=nColumn, j=0; i<nTotal; i++, j++){
13313: if( j==0 && p->cMode!=MODE_Column ){
13314: utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
13315: }
13316: z = azData[i];
13317: if( z==0 ) z = p->nullValue;
13318: w = p->actualWidth[j];
13319: if( p->colWidth[j]<0 ) w = -w;
13320: utf8_width_print(p->out, w, z);
13321: if( j==nColumn-1 ){
13322: utf8_printf(p->out, "%s", rowSep);
13323: j = -1;
13324: if( seenInterrupt ) goto columnar_end;
13325: }else{
13326: utf8_printf(p->out, "%s", colSep);
13327: }
13328: }
13329: if( p->cMode==MODE_Table ){
13330: print_row_separator(p, nColumn, "+");
13331: }else if( p->cMode==MODE_Box ){
13332: print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
13333: }
13334: columnar_end:
13335: if( seenInterrupt ){
13336: utf8_printf(p->out, "Interrupt\n");
13337: }
13338: nData = (nRow+1)*nColumn;
13339: for(i=0; i<nData; i++) free(azData[i]);
13340: sqlite3_free(azData);
13341: }
13342:
13343: /*
13344: ** Run a prepared statement
13345: */
13346: static void exec_prepared_stmt(
13347: ShellState *pArg, /* Pointer to ShellState */
13348: sqlite3_stmt *pStmt /* Statment to run */
13349: ){
13350: int rc;
13351:
13352: if( pArg->cMode==MODE_Column
13353: || pArg->cMode==MODE_Table
13354: || pArg->cMode==MODE_Box
13355: || pArg->cMode==MODE_Markdown
13356: ){
13357: exec_prepared_stmt_columnar(pArg, pStmt);
13358: return;
13359: }
13360:
13361: /* perform the first step. this will tell us if we
13362: ** have a result set or not and how wide it is.
13363: */
13364: rc = sqlite3_step(pStmt);
13365: /* if we have a result set... */
13366: if( SQLITE_ROW == rc ){
13367: /* allocate space for col name ptr, value ptr, and type */
13368: int nCol = sqlite3_column_count(pStmt);
13369: void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
13370: if( !pData ){
13371: rc = SQLITE_NOMEM;
13372: }else{
13373: char **azCols = (char **)pData; /* Names of result columns */
13374: char **azVals = &azCols[nCol]; /* Results */
13375: int *aiTypes = (int *)&azVals[nCol]; /* Result types */
13376: int i, x;
13377: assert(sizeof(int) <= sizeof(char *));
13378: /* save off ptrs to column names */
13379: for(i=0; i<nCol; i++){
13380: azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13381: }
13382: do{
13383: /* extract the data and data types */
13384: for(i=0; i<nCol; i++){
13385: aiTypes[i] = x = sqlite3_column_type(pStmt, i);
13386: if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
13387: azVals[i] = "";
13388: }else{
13389: azVals[i] = (char*)sqlite3_column_text(pStmt, i);
13390: }
13391: if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
13392: rc = SQLITE_NOMEM;
13393: break; /* from for */
13394: }
13395: } /* end for */
13396:
13397: /* if data and types extracted successfully... */
13398: if( SQLITE_ROW == rc ){
13399: /* call the supplied callback with the result row data */
13400: if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
13401: rc = SQLITE_ABORT;
13402: }else{
13403: rc = sqlite3_step(pStmt);
13404: }
13405: }
13406: } while( SQLITE_ROW == rc );
13407: sqlite3_free(pData);
13408: if( pArg->cMode==MODE_Json ){
13409: fputs("]\n", pArg->out);
13410: }
13411: }
13412: }
13413: }
13414:
13415: #ifndef SQLITE_OMIT_VIRTUALTABLE
13416: /*
13417: ** This function is called to process SQL if the previous shell command
13418: ** was ".expert". It passes the SQL in the second argument directly to
13419: ** the sqlite3expert object.
13420: **
13421: ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13422: ** code. In this case, (*pzErr) may be set to point to a buffer containing
13423: ** an English language error message. It is the responsibility of the
13424: ** caller to eventually free this buffer using sqlite3_free().
13425: */
13426: static int expertHandleSQL(
13427: ShellState *pState,
13428: const char *zSql,
13429: char **pzErr
13430: ){
13431: assert( pState->expert.pExpert );
13432: assert( pzErr==0 || *pzErr==0 );
13433: return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
13434: }
13435:
13436: /*
13437: ** This function is called either to silently clean up the object
13438: ** created by the ".expert" command (if bCancel==1), or to generate a
13439: ** report from it and then clean it up (if bCancel==0).
13440: **
13441: ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13442: ** code. In this case, (*pzErr) may be set to point to a buffer containing
13443: ** an English language error message. It is the responsibility of the
13444: ** caller to eventually free this buffer using sqlite3_free().
13445: */
13446: static int expertFinish(
13447: ShellState *pState,
13448: int bCancel,
13449: char **pzErr
13450: ){
13451: int rc = SQLITE_OK;
13452: sqlite3expert *p = pState->expert.pExpert;
13453: assert( p );
13454: assert( bCancel || pzErr==0 || *pzErr==0 );
13455: if( bCancel==0 ){
13456: FILE *out = pState->out;
13457: int bVerbose = pState->expert.bVerbose;
13458:
13459: rc = sqlite3_expert_analyze(p, pzErr);
13460: if( rc==SQLITE_OK ){
13461: int nQuery = sqlite3_expert_count(p);
13462: int i;
13463:
13464: if( bVerbose ){
13465: const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
13466: raw_printf(out, "-- Candidates -----------------------------\n");
13467: raw_printf(out, "%s\n", zCand);
13468: }
13469: for(i=0; i<nQuery; i++){
13470: const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
13471: const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
13472: const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
13473: if( zIdx==0 ) zIdx = "(no new indexes)\n";
13474: if( bVerbose ){
13475: raw_printf(out, "-- Query %d --------------------------------\n",i+1);
13476: raw_printf(out, "%s\n\n", zSql);
13477: }
13478: raw_printf(out, "%s\n", zIdx);
13479: raw_printf(out, "%s\n", zEQP);
13480: }
13481: }
13482: }
13483: sqlite3_expert_destroy(p);
13484: pState->expert.pExpert = 0;
13485: return rc;
13486: }
13487:
13488: /*
13489: ** Implementation of ".expert" dot command.
13490: */
13491: static int expertDotCommand(
13492: ShellState *pState, /* Current shell tool state */
13493: char **azArg, /* Array of arguments passed to dot command */
13494: int nArg /* Number of entries in azArg[] */
13495: ){
13496: int rc = SQLITE_OK;
13497: char *zErr = 0;
13498: int i;
13499: int iSample = 0;
13500:
13501: assert( pState->expert.pExpert==0 );
13502: memset(&pState->expert, 0, sizeof(ExpertInfo));
13503:
13504: for(i=1; rc==SQLITE_OK && i<nArg; i++){
13505: char *z = azArg[i];
13506: int n;
13507: if( z[0]=='-' && z[1]=='-' ) z++;
13508: n = strlen30(z);
13509: if( n>=2 && 0==strncmp(z, "-verbose", n) ){
13510: pState->expert.bVerbose = 1;
13511: }
13512: else if( n>=2 && 0==strncmp(z, "-sample", n) ){
13513: if( i==(nArg-1) ){
13514: raw_printf(stderr, "option requires an argument: %s\n", z);
13515: rc = SQLITE_ERROR;
13516: }else{
13517: iSample = (int)integerValue(azArg[++i]);
13518: if( iSample<0 || iSample>100 ){
13519: raw_printf(stderr, "value out of range: %s\n", azArg[i]);
13520: rc = SQLITE_ERROR;
13521: }
13522: }
13523: }
13524: else{
13525: raw_printf(stderr, "unknown option: %s\n", z);
13526: rc = SQLITE_ERROR;
13527: }
13528: }
13529:
13530: if( rc==SQLITE_OK ){
13531: pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
13532: if( pState->expert.pExpert==0 ){
13533: raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
13534: rc = SQLITE_ERROR;
13535: }else{
13536: sqlite3_expert_config(
13537: pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
13538: );
13539: }
13540: }
13541:
13542: return rc;
13543: }
13544: #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13545:
13546: /*
13547: ** Execute a statement or set of statements. Print
13548: ** any result rows/columns depending on the current mode
13549: ** set via the supplied callback.
13550: **
13551: ** This is very similar to SQLite's built-in sqlite3_exec()
13552: ** function except it takes a slightly different callback
13553: ** and callback data argument.
13554: */
13555: static int shell_exec(
13556: ShellState *pArg, /* Pointer to ShellState */
13557: const char *zSql, /* SQL to be evaluated */
13558: char **pzErrMsg /* Error msg written here */
13559: ){
13560: sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
13561: int rc = SQLITE_OK; /* Return Code */
13562: int rc2;
13563: const char *zLeftover; /* Tail of unprocessed SQL */
13564: sqlite3 *db = pArg->db;
13565:
13566: if( pzErrMsg ){
13567: *pzErrMsg = NULL;
13568: }
13569:
13570: #ifndef SQLITE_OMIT_VIRTUALTABLE
13571: if( pArg->expert.pExpert ){
13572: rc = expertHandleSQL(pArg, zSql, pzErrMsg);
13573: return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
13574: }
13575: #endif
13576:
13577: while( zSql[0] && (SQLITE_OK == rc) ){
13578: static const char *zStmtSql;
13579: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13580: if( SQLITE_OK != rc ){
13581: if( pzErrMsg ){
13582: *pzErrMsg = save_err_msg(db);
13583: }
13584: }else{
13585: if( !pStmt ){
13586: /* this happens for a comment or white-space */
13587: zSql = zLeftover;
13588: while( IsSpace(zSql[0]) ) zSql++;
13589: continue;
13590: }
13591: zStmtSql = sqlite3_sql(pStmt);
13592: if( zStmtSql==0 ) zStmtSql = "";
13593: while( IsSpace(zStmtSql[0]) ) zStmtSql++;
13594:
13595: /* save off the prepared statment handle and reset row count */
13596: if( pArg ){
13597: pArg->pStmt = pStmt;
13598: pArg->cnt = 0;
13599: }
13600:
13601: /* echo the sql statement if echo on */
13602: if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
13603: utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13604: }
13605:
13606: /* Show the EXPLAIN QUERY PLAN if .eqp is on */
13607: if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
13608: sqlite3_stmt *pExplain;
13609: char *zEQP;
13610: int triggerEQP = 0;
13611: disable_debug_trace_modes();
13612: sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
13613: if( pArg->autoEQP>=AUTOEQP_trigger ){
13614: sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
13615: }
13616: zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
13617: rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13618: if( rc==SQLITE_OK ){
13619: while( sqlite3_step(pExplain)==SQLITE_ROW ){
13620: const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
13621: int iEqpId = sqlite3_column_int(pExplain, 0);
13622: int iParentId = sqlite3_column_int(pExplain, 1);
13623: if( zEQPLine==0 ) zEQPLine = "";
13624: if( zEQPLine[0]=='-' ) eqp_render(pArg);
13625: eqp_append(pArg, iEqpId, iParentId, zEQPLine);
13626: }
13627: eqp_render(pArg);
13628: }
13629: sqlite3_finalize(pExplain);
13630: sqlite3_free(zEQP);
13631: if( pArg->autoEQP>=AUTOEQP_full ){
13632: /* Also do an EXPLAIN for ".eqp full" mode */
13633: zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
13634: rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13635: if( rc==SQLITE_OK ){
13636: pArg->cMode = MODE_Explain;
13637: explain_data_prepare(pArg, pExplain);
13638: exec_prepared_stmt(pArg, pExplain);
13639: explain_data_delete(pArg);
13640: }
13641: sqlite3_finalize(pExplain);
13642: sqlite3_free(zEQP);
13643: }
13644: if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
13645: sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
13646: /* Reprepare pStmt before reactiving trace modes */
13647: sqlite3_finalize(pStmt);
13648: sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
13649: if( pArg ) pArg->pStmt = pStmt;
13650: }
13651: restore_debug_trace_modes();
13652: }
13653:
13654: if( pArg ){
13655: pArg->cMode = pArg->mode;
13656: if( pArg->autoExplain ){
13657: if( sqlite3_stmt_isexplain(pStmt)==1 ){
13658: pArg->cMode = MODE_Explain;
13659: }
13660: if( sqlite3_stmt_isexplain(pStmt)==2 ){
13661: pArg->cMode = MODE_EQP;
13662: }
13663: }
13664:
13665: /* If the shell is currently in ".explain" mode, gather the extra
13666: ** data required to add indents to the output.*/
13667: if( pArg->cMode==MODE_Explain ){
13668: explain_data_prepare(pArg, pStmt);
13669: }
13670: }
13671:
13672: bind_prepared_stmt(pArg, pStmt);
13673: exec_prepared_stmt(pArg, pStmt);
13674: explain_data_delete(pArg);
13675: eqp_render(pArg);
13676:
13677: /* print usage stats if stats on */
13678: if( pArg && pArg->statsOn ){
13679: display_stats(db, pArg, 0);
13680: }
13681:
13682: /* print loop-counters if required */
13683: if( pArg && pArg->scanstatsOn ){
13684: display_scanstats(db, pArg);
13685: }
13686:
13687: /* Finalize the statement just executed. If this fails, save a
13688: ** copy of the error message. Otherwise, set zSql to point to the
13689: ** next statement to execute. */
13690: rc2 = sqlite3_finalize(pStmt);
13691: if( rc!=SQLITE_NOMEM ) rc = rc2;
13692: if( rc==SQLITE_OK ){
13693: zSql = zLeftover;
13694: while( IsSpace(zSql[0]) ) zSql++;
13695: }else if( pzErrMsg ){
13696: *pzErrMsg = save_err_msg(db);
13697: }
13698:
13699: /* clear saved stmt handle */
13700: if( pArg ){
13701: pArg->pStmt = NULL;
13702: }
13703: }
13704: } /* end while */
13705:
13706: return rc;
13707: }
13708:
13709: /*
13710: ** Release memory previously allocated by tableColumnList().
13711: */
13712: static void freeColumnList(char **azCol){
13713: int i;
13714: for(i=1; azCol[i]; i++){
13715: sqlite3_free(azCol[i]);
13716: }
13717: /* azCol[0] is a static string */
13718: sqlite3_free(azCol);
13719: }
13720:
13721: /*
13722: ** Return a list of pointers to strings which are the names of all
13723: ** columns in table zTab. The memory to hold the names is dynamically
13724: ** allocated and must be released by the caller using a subsequent call
13725: ** to freeColumnList().
13726: **
13727: ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
13728: ** value that needs to be preserved, then azCol[0] is filled in with the
13729: ** name of the rowid column.
13730: **
13731: ** The first regular column in the table is azCol[1]. The list is terminated
13732: ** by an entry with azCol[i]==0.
13733: */
13734: static char **tableColumnList(ShellState *p, const char *zTab){
13735: char **azCol = 0;
13736: sqlite3_stmt *pStmt;
13737: char *zSql;
13738: int nCol = 0;
13739: int nAlloc = 0;
13740: int nPK = 0; /* Number of PRIMARY KEY columns seen */
13741: int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
13742: int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
13743: int rc;
13744:
13745: zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
13746: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13747: sqlite3_free(zSql);
13748: if( rc ) return 0;
13749: while( sqlite3_step(pStmt)==SQLITE_ROW ){
13750: if( nCol>=nAlloc-2 ){
13751: nAlloc = nAlloc*2 + nCol + 10;
13752: azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
13753: if( azCol==0 ) shell_out_of_memory();
13754: }
13755: azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
13756: if( sqlite3_column_int(pStmt, 5) ){
13757: nPK++;
13758: if( nPK==1
13759: && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
13760: "INTEGER")==0
13761: ){
13762: isIPK = 1;
13763: }else{
13764: isIPK = 0;
13765: }
13766: }
13767: }
13768: sqlite3_finalize(pStmt);
13769: if( azCol==0 ) return 0;
13770: azCol[0] = 0;
13771: azCol[nCol+1] = 0;
13772:
13773: /* The decision of whether or not a rowid really needs to be preserved
13774: ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
13775: ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
13776: ** rowids on tables where the rowid is inaccessible because there are other
13777: ** columns in the table named "rowid", "_rowid_", and "oid".
13778: */
13779: if( preserveRowid && isIPK ){
13780: /* If a single PRIMARY KEY column with type INTEGER was seen, then it
13781: ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
13782: ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
13783: ** ROWID aliases. To distinguish these cases, check to see if
13784: ** there is a "pk" entry in "PRAGMA index_list". There will be
13785: ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
13786: */
13787: zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
13788: " WHERE origin='pk'", zTab);
13789: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13790: sqlite3_free(zSql);
13791: if( rc ){
13792: freeColumnList(azCol);
13793: return 0;
13794: }
13795: rc = sqlite3_step(pStmt);
13796: sqlite3_finalize(pStmt);
13797: preserveRowid = rc==SQLITE_ROW;
13798: }
13799: if( preserveRowid ){
13800: /* Only preserve the rowid if we can find a name to use for the
13801: ** rowid */
13802: static char *azRowid[] = { "rowid", "_rowid_", "oid" };
13803: int i, j;
13804: for(j=0; j<3; j++){
13805: for(i=1; i<=nCol; i++){
13806: if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
13807: }
13808: if( i>nCol ){
13809: /* At this point, we know that azRowid[j] is not the name of any
13810: ** ordinary column in the table. Verify that azRowid[j] is a valid
13811: ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
13812: ** tables will fail this last check */
13813: rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
13814: if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
13815: break;
13816: }
13817: }
13818: }
13819: return azCol;
13820: }
13821:
13822: /*
13823: ** Toggle the reverse_unordered_selects setting.
13824: */
13825: static void toggleSelectOrder(sqlite3 *db){
13826: sqlite3_stmt *pStmt = 0;
13827: int iSetting = 0;
13828: char zStmt[100];
13829: sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
13830: if( sqlite3_step(pStmt)==SQLITE_ROW ){
13831: iSetting = sqlite3_column_int(pStmt, 0);
13832: }
13833: sqlite3_finalize(pStmt);
13834: sqlite3_snprintf(sizeof(zStmt), zStmt,
13835: "PRAGMA reverse_unordered_selects(%d)", !iSetting);
13836: sqlite3_exec(db, zStmt, 0, 0, 0);
13837: }
13838:
13839: /*
13840: ** This is a different callback routine used for dumping the database.
13841: ** Each row received by this callback consists of a table name,
13842: ** the table type ("index" or "table") and SQL to create the table.
13843: ** This routine should print text sufficient to recreate the table.
13844: */
13845: static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
13846: int rc;
13847: const char *zTable;
13848: const char *zType;
13849: const char *zSql;
13850: ShellState *p = (ShellState *)pArg;
13851: int dataOnly;
13852: int noSys;
13853:
13854: UNUSED_PARAMETER(azNotUsed);
13855: if( nArg!=3 || azArg==0 ) return 0;
13856: zTable = azArg[0];
13857: zType = azArg[1];
13858: zSql = azArg[2];
13859: dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
13860: noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
13861:
13862: if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
13863: if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
13864: }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
13865: if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
13866: }else if( strncmp(zTable, "sqlite_", 7)==0 ){
13867: return 0;
13868: }else if( dataOnly ){
13869: /* no-op */
13870: }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
13871: char *zIns;
13872: if( !p->writableSchema ){
13873: raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
13874: p->writableSchema = 1;
13875: }
13876: zIns = sqlite3_mprintf(
13877: "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
13878: "VALUES('table','%q','%q',0,'%q');",
13879: zTable, zTable, zSql);
13880: utf8_printf(p->out, "%s\n", zIns);
13881: sqlite3_free(zIns);
13882: return 0;
13883: }else{
13884: printSchemaLine(p->out, zSql, ";\n");
13885: }
13886:
13887: if( strcmp(zType, "table")==0 ){
13888: ShellText sSelect;
13889: ShellText sTable;
13890: char **azCol;
13891: int i;
13892: char *savedDestTable;
13893: int savedMode;
13894:
13895: azCol = tableColumnList(p, zTable);
13896: if( azCol==0 ){
13897: p->nErr++;
13898: return 0;
13899: }
13900:
13901: /* Always quote the table name, even if it appears to be pure ascii,
13902: ** in case it is a keyword. Ex: INSERT INTO "table" ... */
13903: initText(&sTable);
13904: appendText(&sTable, zTable, quoteChar(zTable));
13905: /* If preserving the rowid, add a column list after the table name.
13906: ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
13907: ** instead of the usual "INSERT INTO tab VALUES(...)".
13908: */
13909: if( azCol[0] ){
13910: appendText(&sTable, "(", 0);
13911: appendText(&sTable, azCol[0], 0);
13912: for(i=1; azCol[i]; i++){
13913: appendText(&sTable, ",", 0);
13914: appendText(&sTable, azCol[i], quoteChar(azCol[i]));
13915: }
13916: appendText(&sTable, ")", 0);
13917: }
13918:
13919: /* Build an appropriate SELECT statement */
13920: initText(&sSelect);
13921: appendText(&sSelect, "SELECT ", 0);
13922: if( azCol[0] ){
13923: appendText(&sSelect, azCol[0], 0);
13924: appendText(&sSelect, ",", 0);
13925: }
13926: for(i=1; azCol[i]; i++){
13927: appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
13928: if( azCol[i+1] ){
13929: appendText(&sSelect, ",", 0);
13930: }
13931: }
13932: freeColumnList(azCol);
13933: appendText(&sSelect, " FROM ", 0);
13934: appendText(&sSelect, zTable, quoteChar(zTable));
13935:
13936: savedDestTable = p->zDestTable;
13937: savedMode = p->mode;
13938: p->zDestTable = sTable.z;
13939: p->mode = p->cMode = MODE_Insert;
13940: rc = shell_exec(p, sSelect.z, 0);
13941: if( (rc&0xff)==SQLITE_CORRUPT ){
13942: raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13943: toggleSelectOrder(p->db);
13944: shell_exec(p, sSelect.z, 0);
13945: toggleSelectOrder(p->db);
13946: }
13947: p->zDestTable = savedDestTable;
13948: p->mode = savedMode;
13949: freeText(&sTable);
13950: freeText(&sSelect);
13951: if( rc ) p->nErr++;
13952: }
13953: return 0;
13954: }
13955:
13956: /*
13957: ** Run zQuery. Use dump_callback() as the callback routine so that
13958: ** the contents of the query are output as SQL statements.
13959: **
13960: ** If we get a SQLITE_CORRUPT error, rerun the query after appending
13961: ** "ORDER BY rowid DESC" to the end.
13962: */
13963: static int run_schema_dump_query(
13964: ShellState *p,
13965: const char *zQuery
13966: ){
13967: int rc;
13968: char *zErr = 0;
13969: rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
13970: if( rc==SQLITE_CORRUPT ){
13971: char *zQ2;
13972: int len = strlen30(zQuery);
13973: raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13974: if( zErr ){
13975: utf8_printf(p->out, "/****** %s ******/\n", zErr);
13976: sqlite3_free(zErr);
13977: zErr = 0;
13978: }
13979: zQ2 = malloc( len+100 );
13980: if( zQ2==0 ) return rc;
13981: sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
13982: rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
13983: if( rc ){
13984: utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
13985: }else{
13986: rc = SQLITE_CORRUPT;
13987: }
13988: sqlite3_free(zErr);
13989: free(zQ2);
13990: }
13991: return rc;
13992: }
13993:
13994: /*
13995: ** Text of help messages.
13996: **
13997: ** The help text for each individual command begins with a line that starts
13998: ** with ".". Subsequent lines are supplimental information.
13999: **
14000: ** There must be two or more spaces between the end of the command and the
14001: ** start of the description of what that command does.
14002: */
14003: static const char *(azHelp[]) = {
14004: #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
14005: ".archive ... Manage SQL archives",
14006: " Each command must have exactly one of the following options:",
14007: " -c, --create Create a new archive",
14008: " -u, --update Add or update files with changed mtime",
14009: " -i, --insert Like -u but always add even if unchanged",
14010: " -t, --list List contents of archive",
14011: " -x, --extract Extract files from archive",
14012: " Optional arguments:",
14013: " -v, --verbose Print each filename as it is processed",
14014: " -f FILE, --file FILE Use archive FILE (default is current db)",
14015: " -a FILE, --append FILE Open FILE using the apndvfs VFS",
14016: " -C DIR, --directory DIR Read/extract files from directory DIR",
14017: " -n, --dryrun Show the SQL that would have occurred",
14018: " Examples:",
14019: " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
14020: " .ar -tf ARCHIVE # List members of ARCHIVE",
14021: " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
14022: " See also:",
14023: " http://sqlite.org/cli.html#sqlar_archive_support",
14024: #endif
14025: #ifndef SQLITE_OMIT_AUTHORIZATION
14026: ".auth ON|OFF Show authorizer callbacks",
14027: #endif
14028: ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
14029: " --append Use the appendvfs",
14030: " --async Write to FILE without journal and fsync()",
14031: ".bail on|off Stop after hitting an error. Default OFF",
14032: ".binary on|off Turn binary output on or off. Default OFF",
14033: ".cd DIRECTORY Change the working directory to DIRECTORY",
14034: ".changes on|off Show number of rows changed by SQL",
14035: ".check GLOB Fail if output since .testcase does not match",
14036: ".clone NEWDB Clone data into NEWDB from the existing database",
14037: ".databases List names and files of attached databases",
14038: ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
14039: ".dbinfo ?DB? Show status information about the database",
14040: ".dump ?OBJECTS? Render database content as SQL",
14041: " Options:",
14042: " --data-only Output only INSERT statements",
14043: " --newlines Allow unescaped newline characters in output",
14044: " --nosys Omit system tables (ex: \"sqlite_stat1\")",
14045: " --preserve-rowids Include ROWID values in the output",
14046: " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
14047: " Additional LIKE patterns can be given in subsequent arguments",
14048: ".echo on|off Turn command echo on or off",
14049: ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
14050: " Other Modes:",
14051: #ifdef SQLITE_DEBUG
14052: " test Show raw EXPLAIN QUERY PLAN output",
14053: " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
14054: #endif
14055: " trigger Like \"full\" but also show trigger bytecode",
14056: ".excel Display the output of next command in spreadsheet",
14057: " --bom Put a UTF8 byte-order mark on intermediate file",
14058: ".exit ?CODE? Exit this program with return-code CODE",
14059: ".expert EXPERIMENTAL. Suggest indexes for queries",
14060: ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
14061: ".filectrl CMD ... Run various sqlite3_file_control() operations",
14062: " --schema SCHEMA Use SCHEMA instead of \"main\"",
14063: " --help Show CMD details",
14064: ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
14065: ".headers on|off Turn display of headers on or off",
14066: ".help ?-all? ?PATTERN? Show help text for PATTERN",
14067: ".import FILE TABLE Import data from FILE into TABLE",
14068: " Options:",
14069: " --ascii Use \\037 and \\036 as column and row separators",
14070: " --csv Use , and \\n as column and row separators",
14071: " --skip N Skip the first N rows of input",
14072: " -v \"Verbose\" - increase auxiliary output",
14073: " Notes:",
14074: " * If TABLE does not exist, it is created. The first row of input",
14075: " determines the column names.",
14076: " * If neither --csv or --ascii are used, the input mode is derived",
14077: " from the \".mode\" output mode",
14078: " * If FILE begins with \"|\" then it is a command that generates the",
14079: " input text.",
14080: #ifndef SQLITE_OMIT_TEST_CONTROL
14081: ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
14082: #endif
14083: ".indexes ?TABLE? Show names of indexes",
14084: " If TABLE is specified, only show indexes for",
14085: " tables matching TABLE using the LIKE operator.",
14086: #ifdef SQLITE_ENABLE_IOTRACE
14087: ".iotrace FILE Enable I/O diagnostic logging to FILE",
14088: #endif
14089: ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
14090: ".lint OPTIONS Report potential schema issues.",
14091: " Options:",
14092: " fkey-indexes Find missing foreign key indexes",
14093: #ifndef SQLITE_OMIT_LOAD_EXTENSION
14094: ".load FILE ?ENTRY? Load an extension library",
14095: #endif
14096: ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
14097: ".mode MODE ?TABLE? Set output mode",
14098: " MODE is one of:",
14099: " ascii Columns/rows delimited by 0x1F and 0x1E",
14100: " box Tables using unicode box-drawing characters",
14101: " csv Comma-separated values",
14102: " column Output in columns. (See .width)",
14103: " html HTML <table> code",
14104: " insert SQL insert statements for TABLE",
14105: " json Results in a JSON array",
14106: " line One value per line",
14107: " list Values delimited by \"|\"",
14108: " markdown Markdown table format",
14109: " quote Escape answers as for SQL",
14110: " table ASCII-art table",
14111: " tabs Tab-separated values",
14112: " tcl TCL list elements",
14113: ".nullvalue STRING Use STRING in place of NULL values",
14114: ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
14115: " If FILE begins with '|' then open as a pipe",
14116: " --bom Put a UTF8 byte-order mark at the beginning",
14117: " -e Send output to the system text editor",
14118: " -x Send output as CSV to a spreadsheet (same as \".excel\")",
14119: #ifdef SQLITE_DEBUG
14120: ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
14121: #endif
14122: ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
14123: " Options:",
14124: " --append Use appendvfs to append database to the end of FILE",
14125: #ifdef SQLITE_ENABLE_DESERIALIZE
14126: " --deserialize Load into memory useing sqlite3_deserialize()",
14127: " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
14128: " --maxsize N Maximum size for --hexdb or --deserialized database",
14129: #endif
14130: " --new Initialize FILE to an empty database",
14131: " --nofollow Do not follow symbolic links",
14132: " --readonly Open FILE readonly",
14133: " --zip FILE is a ZIP archive",
14134: ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
14135: " If FILE begins with '|' then open it as a pipe.",
14136: " Options:",
14137: " --bom Prefix output with a UTF8 byte-order mark",
14138: " -e Send output to the system text editor",
14139: " -x Send output as CSV to a spreadsheet",
14140: ".parameter CMD ... Manage SQL parameter bindings",
14141: " clear Erase all bindings",
14142: " init Initialize the TEMP table that holds bindings",
14143: " list List the current parameter bindings",
14144: " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
14145: " PARAMETER should start with one of: $ : @ ?",
14146: " unset PARAMETER Remove PARAMETER from the binding table",
14147: ".print STRING... Print literal STRING",
14148: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14149: ".progress N Invoke progress handler after every N opcodes",
14150: " --limit N Interrupt after N progress callbacks",
14151: " --once Do no more than one progress interrupt",
14152: " --quiet|-q No output except at interrupts",
14153: " --reset Reset the count for each input and interrupt",
14154: #endif
14155: ".prompt MAIN CONTINUE Replace the standard prompts",
14156: ".quit Exit this program",
14157: ".read FILE Read input from FILE",
14158: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14159: ".recover Recover as much data as possible from corrupt db.",
14160: " --freelist-corrupt Assume the freelist is corrupt",
14161: " --recovery-db NAME Store recovery metadata in database file NAME",
14162: " --lost-and-found TABLE Alternative name for the lost-and-found table",
14163: " --no-rowids Do not attempt to recover rowid values",
14164: " that are not also INTEGER PRIMARY KEYs",
14165: #endif
14166: ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
14167: ".save FILE Write in-memory database into FILE",
14168: ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
14169: ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
14170: " Options:",
14171: " --indent Try to pretty-print the schema",
14172: " --nosys Omit objects whose names start with \"sqlite_\"",
14173: ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
14174: " Options:",
14175: " --init Create a new SELFTEST table",
14176: " -v Verbose output",
14177: ".separator COL ?ROW? Change the column and row separators",
14178: #if defined(SQLITE_ENABLE_SESSION)
14179: ".session ?NAME? CMD ... Create or control sessions",
14180: " Subcommands:",
14181: " attach TABLE Attach TABLE",
14182: " changeset FILE Write a changeset into FILE",
14183: " close Close one session",
14184: " enable ?BOOLEAN? Set or query the enable bit",
14185: " filter GLOB... Reject tables matching GLOBs",
14186: " indirect ?BOOLEAN? Mark or query the indirect status",
14187: " isempty Query whether the session is empty",
14188: " list List currently open session names",
14189: " open DB NAME Open a new session on DB",
14190: " patchset FILE Write a patchset into FILE",
14191: " If ?NAME? is omitted, the first defined session is used.",
14192: #endif
14193: ".sha3sum ... Compute a SHA3 hash of database content",
14194: " Options:",
14195: " --schema Also hash the sqlite_schema table",
14196: " --sha3-224 Use the sha3-224 algorithm",
14197: " --sha3-256 Use the sha3-256 algorithm (default)",
14198: " --sha3-384 Use the sha3-384 algorithm",
14199: " --sha3-512 Use the sha3-512 algorithm",
14200: " Any other argument is a LIKE pattern for tables to hash",
14201: #ifndef SQLITE_NOHAVE_SYSTEM
14202: ".shell CMD ARGS... Run CMD ARGS... in a system shell",
14203: #endif
14204: ".show Show the current values for various settings",
14205: ".stats ?ARG? Show stats or turn stats on or off",
14206: " off Turn off automatic stat display",
14207: " on Turn on automatic stat display",
14208: " stmt Show statement stats",
14209: " vmstep Show the virtual machine step count only",
14210: #ifndef SQLITE_NOHAVE_SYSTEM
14211: ".system CMD ARGS... Run CMD ARGS... in a system shell",
14212: #endif
14213: ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
14214: ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
14215: ".testctrl CMD ... Run various sqlite3_test_control() operations",
14216: " Run \".testctrl\" with no arguments for details",
14217: ".timeout MS Try opening locked tables for MS milliseconds",
14218: ".timer on|off Turn SQL timer on or off",
14219: #ifndef SQLITE_OMIT_TRACE
14220: ".trace ?OPTIONS? Output each SQL statement as it is run",
14221: " FILE Send output to FILE",
14222: " stdout Send output to stdout",
14223: " stderr Send output to stderr",
14224: " off Disable tracing",
14225: " --expanded Expand query parameters",
14226: #ifdef SQLITE_ENABLE_NORMALIZE
14227: " --normalized Normal the SQL statements",
14228: #endif
14229: " --plain Show SQL as it is input",
14230: " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
14231: " --profile Profile statements (SQLITE_TRACE_PROFILE)",
14232: " --row Trace each row (SQLITE_TRACE_ROW)",
14233: " --close Trace connection close (SQLITE_TRACE_CLOSE)",
14234: #endif /* SQLITE_OMIT_TRACE */
14235: #ifdef SQLITE_DEBUG
14236: ".unmodule NAME ... Unregister virtual table modules",
14237: " --allexcept Unregister everything except those named",
14238: #endif
14239: ".vfsinfo ?AUX? Information about the top-level VFS",
14240: ".vfslist List all available VFSes",
14241: ".vfsname ?AUX? Print the name of the VFS stack",
14242: ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
14243: " Negative values right-justify",
14244: };
14245:
14246: /*
14247: ** Output help text.
14248: **
14249: ** zPattern describes the set of commands for which help text is provided.
14250: ** If zPattern is NULL, then show all commands, but only give a one-line
14251: ** description of each.
14252: **
14253: ** Return the number of matches.
14254: */
14255: static int showHelp(FILE *out, const char *zPattern){
14256: int i = 0;
14257: int j = 0;
14258: int n = 0;
14259: char *zPat;
14260: if( zPattern==0
14261: || zPattern[0]=='0'
14262: || strcmp(zPattern,"-a")==0
14263: || strcmp(zPattern,"-all")==0
14264: || strcmp(zPattern,"--all")==0
14265: ){
14266: /* Show all commands, but only one line per command */
14267: if( zPattern==0 ) zPattern = "";
14268: for(i=0; i<ArraySize(azHelp); i++){
14269: if( azHelp[i][0]=='.' || zPattern[0] ){
14270: utf8_printf(out, "%s\n", azHelp[i]);
14271: n++;
14272: }
14273: }
14274: }else{
14275: /* Look for commands that for which zPattern is an exact prefix */
14276: zPat = sqlite3_mprintf(".%s*", zPattern);
14277: for(i=0; i<ArraySize(azHelp); i++){
14278: if( sqlite3_strglob(zPat, azHelp[i])==0 ){
14279: utf8_printf(out, "%s\n", azHelp[i]);
14280: j = i+1;
14281: n++;
14282: }
14283: }
14284: sqlite3_free(zPat);
14285: if( n ){
14286: if( n==1 ){
14287: /* when zPattern is a prefix of exactly one command, then include the
14288: ** details of that command, which should begin at offset j */
14289: while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
14290: utf8_printf(out, "%s\n", azHelp[j]);
14291: j++;
14292: }
14293: }
14294: return n;
14295: }
14296: /* Look for commands that contain zPattern anywhere. Show the complete
14297: ** text of all commands that match. */
14298: zPat = sqlite3_mprintf("%%%s%%", zPattern);
14299: for(i=0; i<ArraySize(azHelp); i++){
14300: if( azHelp[i][0]=='.' ) j = i;
14301: if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
14302: utf8_printf(out, "%s\n", azHelp[j]);
14303: while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
14304: j++;
14305: utf8_printf(out, "%s\n", azHelp[j]);
14306: }
14307: i = j;
14308: n++;
14309: }
14310: }
14311: sqlite3_free(zPat);
14312: }
14313: return n;
14314: }
14315:
14316: /* Forward reference */
14317: static int process_input(ShellState *p);
14318:
14319: /*
14320: ** Read the content of file zName into memory obtained from sqlite3_malloc64()
14321: ** and return a pointer to the buffer. The caller is responsible for freeing
14322: ** the memory.
14323: **
14324: ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
14325: ** read.
14326: **
14327: ** For convenience, a nul-terminator byte is always appended to the data read
14328: ** from the file before the buffer is returned. This byte is not included in
14329: ** the final value of (*pnByte), if applicable.
14330: **
14331: ** NULL is returned if any error is encountered. The final value of *pnByte
14332: ** is undefined in this case.
14333: */
14334: static char *readFile(const char *zName, int *pnByte){
14335: FILE *in = fopen(zName, "rb");
14336: long nIn;
14337: size_t nRead;
14338: char *pBuf;
14339: if( in==0 ) return 0;
14340: fseek(in, 0, SEEK_END);
14341: nIn = ftell(in);
14342: rewind(in);
14343: pBuf = sqlite3_malloc64( nIn+1 );
14344: if( pBuf==0 ){ fclose(in); return 0; }
14345: nRead = fread(pBuf, nIn, 1, in);
14346: fclose(in);
14347: if( nRead!=1 ){
14348: sqlite3_free(pBuf);
14349: return 0;
14350: }
14351: pBuf[nIn] = 0;
14352: if( pnByte ) *pnByte = nIn;
14353: return pBuf;
14354: }
14355:
14356: #if defined(SQLITE_ENABLE_SESSION)
14357: /*
14358: ** Close a single OpenSession object and release all of its associated
14359: ** resources.
14360: */
14361: static void session_close(OpenSession *pSession){
14362: int i;
14363: sqlite3session_delete(pSession->p);
14364: sqlite3_free(pSession->zName);
14365: for(i=0; i<pSession->nFilter; i++){
14366: sqlite3_free(pSession->azFilter[i]);
14367: }
14368: sqlite3_free(pSession->azFilter);
14369: memset(pSession, 0, sizeof(OpenSession));
14370: }
14371: #endif
14372:
14373: /*
14374: ** Close all OpenSession objects and release all associated resources.
14375: */
14376: #if defined(SQLITE_ENABLE_SESSION)
14377: static void session_close_all(ShellState *p){
14378: int i;
14379: for(i=0; i<p->nSession; i++){
14380: session_close(&p->aSession[i]);
14381: }
14382: p->nSession = 0;
14383: }
14384: #else
14385: # define session_close_all(X)
14386: #endif
14387:
14388: /*
14389: ** Implementation of the xFilter function for an open session. Omit
14390: ** any tables named by ".session filter" but let all other table through.
14391: */
14392: #if defined(SQLITE_ENABLE_SESSION)
14393: static int session_filter(void *pCtx, const char *zTab){
14394: OpenSession *pSession = (OpenSession*)pCtx;
14395: int i;
14396: for(i=0; i<pSession->nFilter; i++){
14397: if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
14398: }
14399: return 1;
14400: }
14401: #endif
14402:
14403: /*
14404: ** Try to deduce the type of file for zName based on its content. Return
14405: ** one of the SHELL_OPEN_* constants.
14406: **
14407: ** If the file does not exist or is empty but its name looks like a ZIP
14408: ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
14409: ** Otherwise, assume an ordinary database regardless of the filename if
14410: ** the type cannot be determined from content.
14411: */
14412: int deduceDatabaseType(const char *zName, int dfltZip){
14413: FILE *f = fopen(zName, "rb");
14414: size_t n;
14415: int rc = SHELL_OPEN_UNSPEC;
14416: char zBuf[100];
14417: if( f==0 ){
14418: if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14419: return SHELL_OPEN_ZIPFILE;
14420: }else{
14421: return SHELL_OPEN_NORMAL;
14422: }
14423: }
14424: n = fread(zBuf, 16, 1, f);
14425: if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
14426: fclose(f);
14427: return SHELL_OPEN_NORMAL;
14428: }
14429: fseek(f, -25, SEEK_END);
14430: n = fread(zBuf, 25, 1, f);
14431: if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
14432: rc = SHELL_OPEN_APPENDVFS;
14433: }else{
14434: fseek(f, -22, SEEK_END);
14435: n = fread(zBuf, 22, 1, f);
14436: if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
14437: && zBuf[3]==0x06 ){
14438: rc = SHELL_OPEN_ZIPFILE;
14439: }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14440: rc = SHELL_OPEN_ZIPFILE;
14441: }
14442: }
14443: fclose(f);
14444: return rc;
14445: }
14446:
14447: #ifdef SQLITE_ENABLE_DESERIALIZE
14448: /*
14449: ** Reconstruct an in-memory database using the output from the "dbtotxt"
14450: ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
14451: ** is 0, then read from standard input.
14452: */
14453: static unsigned char *readHexDb(ShellState *p, int *pnData){
14454: unsigned char *a = 0;
14455: int nLine;
14456: int n = 0;
14457: int pgsz = 0;
14458: int iOffset = 0;
14459: int j, k;
14460: int rc;
14461: FILE *in;
14462: unsigned int x[16];
14463: char zLine[1000];
14464: if( p->zDbFilename ){
14465: in = fopen(p->zDbFilename, "r");
14466: if( in==0 ){
14467: utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
14468: return 0;
14469: }
14470: nLine = 0;
14471: }else{
14472: in = p->in;
14473: nLine = p->lineno;
14474: if( in==0 ) in = stdin;
14475: }
14476: *pnData = 0;
14477: nLine++;
14478: if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
14479: rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
14480: if( rc!=2 ) goto readHexDb_error;
14481: if( n<0 ) goto readHexDb_error;
14482: if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
14483: n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
14484: a = sqlite3_malloc( n ? n : 1 );
14485: if( a==0 ){
14486: utf8_printf(stderr, "Out of memory!\n");
14487: goto readHexDb_error;
14488: }
14489: memset(a, 0, n);
14490: if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
14491: utf8_printf(stderr, "invalid pagesize\n");
14492: goto readHexDb_error;
14493: }
14494: for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
14495: rc = sscanf(zLine, "| page %d offset %d", &j, &k);
14496: if( rc==2 ){
14497: iOffset = k;
14498: continue;
14499: }
14500: if( strncmp(zLine, "| end ", 6)==0 ){
14501: break;
14502: }
14503: rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
14504: &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
14505: &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
14506: if( rc==17 ){
14507: k = iOffset+j;
14508: if( k+16<=n ){
14509: int ii;
14510: for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
14511: }
14512: }
14513: }
14514: *pnData = n;
14515: if( in!=p->in ){
14516: fclose(in);
14517: }else{
14518: p->lineno = nLine;
14519: }
14520: return a;
14521:
14522: readHexDb_error:
14523: if( in!=p->in ){
14524: fclose(in);
14525: }else{
14526: while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
14527: nLine++;
14528: if(strncmp(zLine, "| end ", 6)==0 ) break;
14529: }
14530: p->lineno = nLine;
14531: }
14532: sqlite3_free(a);
14533: utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
14534: return 0;
14535: }
14536: #endif /* SQLITE_ENABLE_DESERIALIZE */
14537:
14538: /*
14539: ** Scalar function "shell_int32". The first argument to this function
14540: ** must be a blob. The second a non-negative integer. This function
14541: ** reads and returns a 32-bit big-endian integer from byte
14542: ** offset (4*<arg2>) of the blob.
14543: */
14544: static void shellInt32(
14545: sqlite3_context *context,
14546: int argc,
14547: sqlite3_value **argv
14548: ){
14549: const unsigned char *pBlob;
14550: int nBlob;
14551: int iInt;
14552:
14553: UNUSED_PARAMETER(argc);
14554: nBlob = sqlite3_value_bytes(argv[0]);
14555: pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
14556: iInt = sqlite3_value_int(argv[1]);
14557:
14558: if( iInt>=0 && (iInt+1)*4<=nBlob ){
14559: const unsigned char *a = &pBlob[iInt*4];
14560: sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
14561: + ((sqlite3_int64)a[1]<<16)
14562: + ((sqlite3_int64)a[2]<< 8)
14563: + ((sqlite3_int64)a[3]<< 0);
14564: sqlite3_result_int64(context, iVal);
14565: }
14566: }
14567:
14568: /*
14569: ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
14570: ** using "..." with internal double-quote characters doubled.
14571: */
14572: static void shellIdQuote(
14573: sqlite3_context *context,
14574: int argc,
14575: sqlite3_value **argv
14576: ){
14577: const char *zName = (const char*)sqlite3_value_text(argv[0]);
14578: UNUSED_PARAMETER(argc);
14579: if( zName ){
14580: char *z = sqlite3_mprintf("\"%w\"", zName);
14581: sqlite3_result_text(context, z, -1, sqlite3_free);
14582: }
14583: }
14584:
14585: /*
14586: ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
14587: */
14588: static void shellUSleepFunc(
14589: sqlite3_context *context,
14590: int argcUnused,
14591: sqlite3_value **argv
14592: ){
14593: int sleep = sqlite3_value_int(argv[0]);
14594: (void)argcUnused;
14595: sqlite3_sleep(sleep/1000);
14596: sqlite3_result_int(context, sleep);
14597: }
14598:
14599: /*
14600: ** Scalar function "shell_escape_crnl" used by the .recover command.
14601: ** The argument passed to this function is the output of built-in
14602: ** function quote(). If the first character of the input is "'",
14603: ** indicating that the value passed to quote() was a text value,
14604: ** then this function searches the input for "\n" and "\r" characters
14605: ** and adds a wrapper similar to the following:
14606: **
14607: ** replace(replace(<input>, '\n', char(10), '\r', char(13));
14608: **
14609: ** Or, if the first character of the input is not "'", then a copy
14610: ** of the input is returned.
14611: */
14612: static void shellEscapeCrnl(
14613: sqlite3_context *context,
14614: int argc,
14615: sqlite3_value **argv
14616: ){
14617: const char *zText = (const char*)sqlite3_value_text(argv[0]);
14618: UNUSED_PARAMETER(argc);
14619: if( zText[0]=='\'' ){
14620: int nText = sqlite3_value_bytes(argv[0]);
14621: int i;
14622: char zBuf1[20];
14623: char zBuf2[20];
14624: const char *zNL = 0;
14625: const char *zCR = 0;
14626: int nCR = 0;
14627: int nNL = 0;
14628:
14629: for(i=0; zText[i]; i++){
14630: if( zNL==0 && zText[i]=='\n' ){
14631: zNL = unused_string(zText, "\\n", "\\012", zBuf1);
14632: nNL = (int)strlen(zNL);
14633: }
14634: if( zCR==0 && zText[i]=='\r' ){
14635: zCR = unused_string(zText, "\\r", "\\015", zBuf2);
14636: nCR = (int)strlen(zCR);
14637: }
14638: }
14639:
14640: if( zNL || zCR ){
14641: int iOut = 0;
14642: i64 nMax = (nNL > nCR) ? nNL : nCR;
14643: i64 nAlloc = nMax * nText + (nMax+64)*2;
14644: char *zOut = (char*)sqlite3_malloc64(nAlloc);
14645: if( zOut==0 ){
14646: sqlite3_result_error_nomem(context);
14647: return;
14648: }
14649:
14650: if( zNL && zCR ){
14651: memcpy(&zOut[iOut], "replace(replace(", 16);
14652: iOut += 16;
14653: }else{
14654: memcpy(&zOut[iOut], "replace(", 8);
14655: iOut += 8;
14656: }
14657: for(i=0; zText[i]; i++){
14658: if( zText[i]=='\n' ){
14659: memcpy(&zOut[iOut], zNL, nNL);
14660: iOut += nNL;
14661: }else if( zText[i]=='\r' ){
14662: memcpy(&zOut[iOut], zCR, nCR);
14663: iOut += nCR;
14664: }else{
14665: zOut[iOut] = zText[i];
14666: iOut++;
14667: }
14668: }
14669:
14670: if( zNL ){
14671: memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14672: memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
14673: memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
14674: }
14675: if( zCR ){
14676: memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14677: memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
14678: memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
14679: }
14680:
14681: sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
14682: sqlite3_free(zOut);
14683: return;
14684: }
14685: }
14686:
14687: sqlite3_result_value(context, argv[0]);
14688: }
14689:
14690: /* Flags for open_db().
14691: **
14692: ** The default behavior of open_db() is to exit(1) if the database fails to
14693: ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
14694: ** but still returns without calling exit.
14695: **
14696: ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
14697: ** ZIP archive if the file does not exist or is empty and its name matches
14698: ** the *.zip pattern.
14699: */
14700: #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
14701: #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
14702:
14703: /*
14704: ** Make sure the database is open. If it is not, then open it. If
14705: ** the database fails to open, print an error message and exit.
14706: */
14707: static void open_db(ShellState *p, int openFlags){
14708: if( p->db==0 ){
14709: if( p->openMode==SHELL_OPEN_UNSPEC ){
14710: if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
14711: p->openMode = SHELL_OPEN_NORMAL;
14712: }else{
14713: p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
14714: (openFlags & OPEN_DB_ZIPFILE)!=0);
14715: }
14716: }
14717: switch( p->openMode ){
14718: case SHELL_OPEN_APPENDVFS: {
14719: sqlite3_open_v2(p->zDbFilename, &p->db,
14720: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
14721: break;
14722: }
14723: case SHELL_OPEN_HEXDB:
14724: case SHELL_OPEN_DESERIALIZE: {
14725: sqlite3_open(0, &p->db);
14726: break;
14727: }
14728: case SHELL_OPEN_ZIPFILE: {
14729: sqlite3_open(":memory:", &p->db);
14730: break;
14731: }
14732: case SHELL_OPEN_READONLY: {
14733: sqlite3_open_v2(p->zDbFilename, &p->db,
14734: SQLITE_OPEN_READONLY|p->openFlags, 0);
14735: break;
14736: }
14737: case SHELL_OPEN_UNSPEC:
14738: case SHELL_OPEN_NORMAL: {
14739: sqlite3_open_v2(p->zDbFilename, &p->db,
14740: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
14741: break;
14742: }
14743: }
14744: globalDb = p->db;
14745: if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
14746: utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
14747: p->zDbFilename, sqlite3_errmsg(p->db));
14748: if( openFlags & OPEN_DB_KEEPALIVE ){
14749: sqlite3_open(":memory:", &p->db);
14750: return;
14751: }
14752: exit(1);
14753: }
14754: #ifndef SQLITE_OMIT_LOAD_EXTENSION
14755: sqlite3_enable_load_extension(p->db, 1);
14756: #endif
14757: sqlite3_fileio_init(p->db, 0, 0);
14758: sqlite3_shathree_init(p->db, 0, 0);
14759: sqlite3_completion_init(p->db, 0, 0);
14760: sqlite3_uint_init(p->db, 0, 0);
14761: sqlite3_decimal_init(p->db, 0, 0);
14762: sqlite3_ieee_init(p->db, 0, 0);
14763: sqlite3_series_init(p->db, 0, 0);
14764: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14765: sqlite3_dbdata_init(p->db, 0, 0);
14766: #endif
14767: #ifdef SQLITE_HAVE_ZLIB
14768: sqlite3_zipfile_init(p->db, 0, 0);
14769: sqlite3_sqlar_init(p->db, 0, 0);
14770: #endif
14771: sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
14772: shellAddSchemaName, 0, 0);
14773: sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
14774: shellModuleSchema, 0, 0);
14775: sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
14776: shellPutsFunc, 0, 0);
14777: sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
14778: shellEscapeCrnl, 0, 0);
14779: sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
14780: shellInt32, 0, 0);
14781: sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
14782: shellIdQuote, 0, 0);
14783: sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
14784: shellUSleepFunc, 0, 0);
14785: #ifndef SQLITE_NOHAVE_SYSTEM
14786: sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
14787: editFunc, 0, 0);
14788: sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
14789: editFunc, 0, 0);
14790: #endif
14791: if( p->openMode==SHELL_OPEN_ZIPFILE ){
14792: char *zSql = sqlite3_mprintf(
14793: "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
14794: sqlite3_exec(p->db, zSql, 0, 0, 0);
14795: sqlite3_free(zSql);
14796: }
14797: #ifdef SQLITE_ENABLE_DESERIALIZE
14798: else
14799: if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
14800: int rc;
14801: int nData = 0;
14802: unsigned char *aData;
14803: if( p->openMode==SHELL_OPEN_DESERIALIZE ){
14804: aData = (unsigned char*)readFile(p->zDbFilename, &nData);
14805: }else{
14806: aData = readHexDb(p, &nData);
14807: if( aData==0 ){
14808: return;
14809: }
14810: }
14811: rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
14812: SQLITE_DESERIALIZE_RESIZEABLE |
14813: SQLITE_DESERIALIZE_FREEONCLOSE);
14814: if( rc ){
14815: utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
14816: }
14817: if( p->szMax>0 ){
14818: sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
14819: }
14820: }
14821: #endif
14822: }
14823: }
14824:
14825: /*
14826: ** Attempt to close the databaes connection. Report errors.
14827: */
14828: void close_db(sqlite3 *db){
14829: int rc = sqlite3_close(db);
14830: if( rc ){
14831: utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
14832: rc, sqlite3_errmsg(db));
14833: }
14834: }
14835:
14836: #if HAVE_READLINE || HAVE_EDITLINE
14837: /*
14838: ** Readline completion callbacks
14839: */
14840: static char *readline_completion_generator(const char *text, int state){
14841: static sqlite3_stmt *pStmt = 0;
14842: char *zRet;
14843: if( state==0 ){
14844: char *zSql;
14845: sqlite3_finalize(pStmt);
14846: zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14847: " FROM completion(%Q) ORDER BY 1", text);
14848: sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14849: sqlite3_free(zSql);
14850: }
14851: if( sqlite3_step(pStmt)==SQLITE_ROW ){
14852: zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
14853: }else{
14854: sqlite3_finalize(pStmt);
14855: pStmt = 0;
14856: zRet = 0;
14857: }
14858: return zRet;
14859: }
14860: static char **readline_completion(const char *zText, int iStart, int iEnd){
14861: rl_attempted_completion_over = 1;
14862: return rl_completion_matches(zText, readline_completion_generator);
14863: }
14864:
14865: #elif HAVE_LINENOISE
14866: /*
14867: ** Linenoise completion callback
14868: */
14869: static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
14870: int nLine = strlen30(zLine);
14871: int i, iStart;
14872: sqlite3_stmt *pStmt = 0;
14873: char *zSql;
14874: char zBuf[1000];
14875:
14876: if( nLine>sizeof(zBuf)-30 ) return;
14877: if( zLine[0]=='.' || zLine[0]=='#') return;
14878: for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
14879: if( i==nLine-1 ) return;
14880: iStart = i+1;
14881: memcpy(zBuf, zLine, iStart);
14882: zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14883: " FROM completion(%Q,%Q) ORDER BY 1",
14884: &zLine[iStart], zLine);
14885: sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14886: sqlite3_free(zSql);
14887: sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
14888: while( sqlite3_step(pStmt)==SQLITE_ROW ){
14889: const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
14890: int nCompletion = sqlite3_column_bytes(pStmt, 0);
14891: if( iStart+nCompletion < sizeof(zBuf)-1 ){
14892: memcpy(zBuf+iStart, zCompletion, nCompletion+1);
14893: linenoiseAddCompletion(lc, zBuf);
14894: }
14895: }
14896: sqlite3_finalize(pStmt);
14897: }
14898: #endif
14899:
14900: /*
14901: ** Do C-language style dequoting.
14902: **
14903: ** \a -> alarm
14904: ** \b -> backspace
14905: ** \t -> tab
14906: ** \n -> newline
14907: ** \v -> vertical tab
14908: ** \f -> form feed
14909: ** \r -> carriage return
14910: ** \s -> space
14911: ** \" -> "
14912: ** \' -> '
14913: ** \\ -> backslash
14914: ** \NNN -> ascii character NNN in octal
14915: */
14916: static void resolve_backslashes(char *z){
14917: int i, j;
14918: char c;
14919: while( *z && *z!='\\' ) z++;
14920: for(i=j=0; (c = z[i])!=0; i++, j++){
14921: if( c=='\\' && z[i+1]!=0 ){
14922: c = z[++i];
14923: if( c=='a' ){
14924: c = '\a';
14925: }else if( c=='b' ){
14926: c = '\b';
14927: }else if( c=='t' ){
14928: c = '\t';
14929: }else if( c=='n' ){
14930: c = '\n';
14931: }else if( c=='v' ){
14932: c = '\v';
14933: }else if( c=='f' ){
14934: c = '\f';
14935: }else if( c=='r' ){
14936: c = '\r';
14937: }else if( c=='"' ){
14938: c = '"';
14939: }else if( c=='\'' ){
14940: c = '\'';
14941: }else if( c=='\\' ){
14942: c = '\\';
14943: }else if( c>='0' && c<='7' ){
14944: c -= '0';
14945: if( z[i+1]>='0' && z[i+1]<='7' ){
14946: i++;
14947: c = (c<<3) + z[i] - '0';
14948: if( z[i+1]>='0' && z[i+1]<='7' ){
14949: i++;
14950: c = (c<<3) + z[i] - '0';
14951: }
14952: }
14953: }
14954: }
14955: z[j] = c;
14956: }
14957: if( j<i ) z[j] = 0;
14958: }
14959:
14960: /*
14961: ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
14962: ** for TRUE and FALSE. Return the integer value if appropriate.
14963: */
14964: static int booleanValue(const char *zArg){
14965: int i;
14966: if( zArg[0]=='0' && zArg[1]=='x' ){
14967: for(i=2; hexDigitValue(zArg[i])>=0; i++){}
14968: }else{
14969: for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
14970: }
14971: if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
14972: if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
14973: return 1;
14974: }
14975: if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
14976: return 0;
14977: }
14978: utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
14979: zArg);
14980: return 0;
14981: }
14982:
14983: /*
14984: ** Set or clear a shell flag according to a boolean value.
14985: */
14986: static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
14987: if( booleanValue(zArg) ){
14988: ShellSetFlag(p, mFlag);
14989: }else{
14990: ShellClearFlag(p, mFlag);
14991: }
14992: }
14993:
14994: /*
14995: ** Close an output file, assuming it is not stderr or stdout
14996: */
14997: static void output_file_close(FILE *f){
14998: if( f && f!=stdout && f!=stderr ) fclose(f);
14999: }
15000:
15001: /*
15002: ** Try to open an output file. The names "stdout" and "stderr" are
15003: ** recognized and do the right thing. NULL is returned if the output
15004: ** filename is "off".
15005: */
15006: static FILE *output_file_open(const char *zFile, int bTextMode){
15007: FILE *f;
15008: if( strcmp(zFile,"stdout")==0 ){
15009: f = stdout;
15010: }else if( strcmp(zFile, "stderr")==0 ){
15011: f = stderr;
15012: }else if( strcmp(zFile, "off")==0 ){
15013: f = 0;
15014: }else{
15015: f = fopen(zFile, bTextMode ? "w" : "wb");
15016: if( f==0 ){
15017: utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
15018: }
15019: }
15020: return f;
15021: }
15022:
15023: #ifndef SQLITE_OMIT_TRACE
15024: /*
15025: ** A routine for handling output from sqlite3_trace().
15026: */
15027: static int sql_trace_callback(
15028: unsigned mType, /* The trace type */
15029: void *pArg, /* The ShellState pointer */
15030: void *pP, /* Usually a pointer to sqlite_stmt */
15031: void *pX /* Auxiliary output */
15032: ){
15033: ShellState *p = (ShellState*)pArg;
15034: sqlite3_stmt *pStmt;
15035: const char *zSql;
15036: int nSql;
15037: if( p->traceOut==0 ) return 0;
15038: if( mType==SQLITE_TRACE_CLOSE ){
15039: utf8_printf(p->traceOut, "-- closing database connection\n");
15040: return 0;
15041: }
15042: if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
15043: zSql = (const char*)pX;
15044: }else{
15045: pStmt = (sqlite3_stmt*)pP;
15046: switch( p->eTraceType ){
15047: case SHELL_TRACE_EXPANDED: {
15048: zSql = sqlite3_expanded_sql(pStmt);
15049: break;
15050: }
15051: #ifdef SQLITE_ENABLE_NORMALIZE
15052: case SHELL_TRACE_NORMALIZED: {
15053: zSql = sqlite3_normalized_sql(pStmt);
15054: break;
15055: }
15056: #endif
15057: default: {
15058: zSql = sqlite3_sql(pStmt);
15059: break;
15060: }
15061: }
15062: }
15063: if( zSql==0 ) return 0;
15064: nSql = strlen30(zSql);
15065: while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
15066: switch( mType ){
15067: case SQLITE_TRACE_ROW:
15068: case SQLITE_TRACE_STMT: {
15069: utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
15070: break;
15071: }
15072: case SQLITE_TRACE_PROFILE: {
15073: sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
15074: utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
15075: break;
15076: }
15077: }
15078: return 0;
15079: }
15080: #endif
15081:
15082: /*
15083: ** A no-op routine that runs with the ".breakpoint" doc-command. This is
15084: ** a useful spot to set a debugger breakpoint.
15085: */
15086: static void test_breakpoint(void){
15087: static int nCall = 0;
15088: nCall++;
15089: }
15090:
15091: /*
15092: ** An object used to read a CSV and other files for import.
15093: */
15094: typedef struct ImportCtx ImportCtx;
15095: struct ImportCtx {
15096: const char *zFile; /* Name of the input file */
15097: FILE *in; /* Read the CSV text from this input stream */
15098: int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
15099: char *z; /* Accumulated text for a field */
15100: int n; /* Number of bytes in z */
15101: int nAlloc; /* Space allocated for z[] */
15102: int nLine; /* Current line number */
15103: int nRow; /* Number of rows imported */
15104: int nErr; /* Number of errors encountered */
15105: int bNotFirst; /* True if one or more bytes already read */
15106: int cTerm; /* Character that terminated the most recent field */
15107: int cColSep; /* The column separator character. (Usually ",") */
15108: int cRowSep; /* The row separator character. (Usually "\n") */
15109: };
15110:
15111: /* Clean up resourced used by an ImportCtx */
15112: static void import_cleanup(ImportCtx *p){
15113: if( p->in!=0 && p->xCloser!=0 ){
15114: p->xCloser(p->in);
15115: p->in = 0;
15116: }
15117: sqlite3_free(p->z);
15118: p->z = 0;
15119: }
15120:
15121: /* Append a single byte to z[] */
15122: static void import_append_char(ImportCtx *p, int c){
15123: if( p->n+1>=p->nAlloc ){
15124: p->nAlloc += p->nAlloc + 100;
15125: p->z = sqlite3_realloc64(p->z, p->nAlloc);
15126: if( p->z==0 ) shell_out_of_memory();
15127: }
15128: p->z[p->n++] = (char)c;
15129: }
15130:
15131: /* Read a single field of CSV text. Compatible with rfc4180 and extended
15132: ** with the option of having a separator other than ",".
15133: **
15134: ** + Input comes from p->in.
15135: ** + Store results in p->z of length p->n. Space to hold p->z comes
15136: ** from sqlite3_malloc64().
15137: ** + Use p->cSep as the column separator. The default is ",".
15138: ** + Use p->rSep as the row separator. The default is "\n".
15139: ** + Keep track of the line number in p->nLine.
15140: ** + Store the character that terminates the field in p->cTerm. Store
15141: ** EOF on end-of-file.
15142: ** + Report syntax errors on stderr
15143: */
15144: static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15145: int c;
15146: int cSep = p->cColSep;
15147: int rSep = p->cRowSep;
15148: p->n = 0;
15149: c = fgetc(p->in);
15150: if( c==EOF || seenInterrupt ){
15151: p->cTerm = EOF;
15152: return 0;
15153: }
15154: if( c=='"' ){
15155: int pc, ppc;
15156: int startLine = p->nLine;
15157: int cQuote = c;
15158: pc = ppc = 0;
15159: while( 1 ){
15160: c = fgetc(p->in);
15161: if( c==rSep ) p->nLine++;
15162: if( c==cQuote ){
15163: if( pc==cQuote ){
15164: pc = 0;
15165: continue;
15166: }
15167: }
15168: if( (c==cSep && pc==cQuote)
15169: || (c==rSep && pc==cQuote)
15170: || (c==rSep && pc=='\r' && ppc==cQuote)
15171: || (c==EOF && pc==cQuote)
15172: ){
15173: do{ p->n--; }while( p->z[p->n]!=cQuote );
15174: p->cTerm = c;
15175: break;
15176: }
15177: if( pc==cQuote && c!='\r' ){
15178: utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15179: p->zFile, p->nLine, cQuote);
15180: }
15181: if( c==EOF ){
15182: utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15183: p->zFile, startLine, cQuote);
15184: p->cTerm = c;
15185: break;
15186: }
15187: import_append_char(p, c);
15188: ppc = pc;
15189: pc = c;
15190: }
15191: }else{
15192: /* If this is the first field being parsed and it begins with the
15193: ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
15194: if( (c&0xff)==0xef && p->bNotFirst==0 ){
15195: import_append_char(p, c);
15196: c = fgetc(p->in);
15197: if( (c&0xff)==0xbb ){
15198: import_append_char(p, c);
15199: c = fgetc(p->in);
15200: if( (c&0xff)==0xbf ){
15201: p->bNotFirst = 1;
15202: p->n = 0;
15203: return csv_read_one_field(p);
15204: }
15205: }
15206: }
15207: while( c!=EOF && c!=cSep && c!=rSep ){
15208: import_append_char(p, c);
15209: c = fgetc(p->in);
15210: }
15211: if( c==rSep ){
15212: p->nLine++;
15213: if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
15214: }
15215: p->cTerm = c;
15216: }
15217: if( p->z ) p->z[p->n] = 0;
15218: p->bNotFirst = 1;
15219: return p->z;
15220: }
15221:
15222: /* Read a single field of ASCII delimited text.
15223: **
15224: ** + Input comes from p->in.
15225: ** + Store results in p->z of length p->n. Space to hold p->z comes
15226: ** from sqlite3_malloc64().
15227: ** + Use p->cSep as the column separator. The default is "\x1F".
15228: ** + Use p->rSep as the row separator. The default is "\x1E".
15229: ** + Keep track of the row number in p->nLine.
15230: ** + Store the character that terminates the field in p->cTerm. Store
15231: ** EOF on end-of-file.
15232: ** + Report syntax errors on stderr
15233: */
15234: static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
15235: int c;
15236: int cSep = p->cColSep;
15237: int rSep = p->cRowSep;
15238: p->n = 0;
15239: c = fgetc(p->in);
15240: if( c==EOF || seenInterrupt ){
15241: p->cTerm = EOF;
15242: return 0;
15243: }
15244: while( c!=EOF && c!=cSep && c!=rSep ){
15245: import_append_char(p, c);
15246: c = fgetc(p->in);
15247: }
15248: if( c==rSep ){
15249: p->nLine++;
15250: }
15251: p->cTerm = c;
15252: if( p->z ) p->z[p->n] = 0;
15253: return p->z;
15254: }
15255:
15256: /*
15257: ** Try to transfer data for table zTable. If an error is seen while
15258: ** moving forward, try to go backwards. The backwards movement won't
15259: ** work for WITHOUT ROWID tables.
15260: */
15261: static void tryToCloneData(
15262: ShellState *p,
15263: sqlite3 *newDb,
15264: const char *zTable
15265: ){
15266: sqlite3_stmt *pQuery = 0;
15267: sqlite3_stmt *pInsert = 0;
15268: char *zQuery = 0;
15269: char *zInsert = 0;
15270: int rc;
15271: int i, j, n;
15272: int nTable = strlen30(zTable);
15273: int k = 0;
15274: int cnt = 0;
15275: const int spinRate = 10000;
15276:
15277: zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
15278: rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15279: if( rc ){
15280: utf8_printf(stderr, "Error %d: %s on [%s]\n",
15281: sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15282: zQuery);
15283: goto end_data_xfer;
15284: }
15285: n = sqlite3_column_count(pQuery);
15286: zInsert = sqlite3_malloc64(200 + nTable + n*3);
15287: if( zInsert==0 ) shell_out_of_memory();
15288: sqlite3_snprintf(200+nTable,zInsert,
15289: "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
15290: i = strlen30(zInsert);
15291: for(j=1; j<n; j++){
15292: memcpy(zInsert+i, ",?", 2);
15293: i += 2;
15294: }
15295: memcpy(zInsert+i, ");", 3);
15296: rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
15297: if( rc ){
15298: utf8_printf(stderr, "Error %d: %s on [%s]\n",
15299: sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
15300: zQuery);
15301: goto end_data_xfer;
15302: }
15303: for(k=0; k<2; k++){
15304: while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15305: for(i=0; i<n; i++){
15306: switch( sqlite3_column_type(pQuery, i) ){
15307: case SQLITE_NULL: {
15308: sqlite3_bind_null(pInsert, i+1);
15309: break;
15310: }
15311: case SQLITE_INTEGER: {
15312: sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
15313: break;
15314: }
15315: case SQLITE_FLOAT: {
15316: sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
15317: break;
15318: }
15319: case SQLITE_TEXT: {
15320: sqlite3_bind_text(pInsert, i+1,
15321: (const char*)sqlite3_column_text(pQuery,i),
15322: -1, SQLITE_STATIC);
15323: break;
15324: }
15325: case SQLITE_BLOB: {
15326: sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
15327: sqlite3_column_bytes(pQuery,i),
15328: SQLITE_STATIC);
15329: break;
15330: }
15331: }
15332: } /* End for */
15333: rc = sqlite3_step(pInsert);
15334: if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
15335: utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
15336: sqlite3_errmsg(newDb));
15337: }
15338: sqlite3_reset(pInsert);
15339: cnt++;
15340: if( (cnt%spinRate)==0 ){
15341: printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
15342: fflush(stdout);
15343: }
15344: } /* End while */
15345: if( rc==SQLITE_DONE ) break;
15346: sqlite3_finalize(pQuery);
15347: sqlite3_free(zQuery);
15348: zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
15349: zTable);
15350: rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15351: if( rc ){
15352: utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
15353: break;
15354: }
15355: } /* End for(k=0...) */
15356:
15357: end_data_xfer:
15358: sqlite3_finalize(pQuery);
15359: sqlite3_finalize(pInsert);
15360: sqlite3_free(zQuery);
15361: sqlite3_free(zInsert);
15362: }
15363:
15364:
15365: /*
15366: ** Try to transfer all rows of the schema that match zWhere. For
15367: ** each row, invoke xForEach() on the object defined by that row.
15368: ** If an error is encountered while moving forward through the
15369: ** sqlite_schema table, try again moving backwards.
15370: */
15371: static void tryToCloneSchema(
15372: ShellState *p,
15373: sqlite3 *newDb,
15374: const char *zWhere,
15375: void (*xForEach)(ShellState*,sqlite3*,const char*)
15376: ){
15377: sqlite3_stmt *pQuery = 0;
15378: char *zQuery = 0;
15379: int rc;
15380: const unsigned char *zName;
15381: const unsigned char *zSql;
15382: char *zErrMsg = 0;
15383:
15384: zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15385: " WHERE %s", zWhere);
15386: rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15387: if( rc ){
15388: utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15389: sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15390: zQuery);
15391: goto end_schema_xfer;
15392: }
15393: while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15394: zName = sqlite3_column_text(pQuery, 0);
15395: zSql = sqlite3_column_text(pQuery, 1);
15396: printf("%s... ", zName); fflush(stdout);
15397: sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15398: if( zErrMsg ){
15399: utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15400: sqlite3_free(zErrMsg);
15401: zErrMsg = 0;
15402: }
15403: if( xForEach ){
15404: xForEach(p, newDb, (const char*)zName);
15405: }
15406: printf("done\n");
15407: }
15408: if( rc!=SQLITE_DONE ){
15409: sqlite3_finalize(pQuery);
15410: sqlite3_free(zQuery);
15411: zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15412: " WHERE %s ORDER BY rowid DESC", zWhere);
15413: rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15414: if( rc ){
15415: utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15416: sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15417: zQuery);
15418: goto end_schema_xfer;
15419: }
15420: while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15421: zName = sqlite3_column_text(pQuery, 0);
15422: zSql = sqlite3_column_text(pQuery, 1);
15423: printf("%s... ", zName); fflush(stdout);
15424: sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15425: if( zErrMsg ){
15426: utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15427: sqlite3_free(zErrMsg);
15428: zErrMsg = 0;
15429: }
15430: if( xForEach ){
15431: xForEach(p, newDb, (const char*)zName);
15432: }
15433: printf("done\n");
15434: }
15435: }
15436: end_schema_xfer:
15437: sqlite3_finalize(pQuery);
15438: sqlite3_free(zQuery);
15439: }
15440:
15441: /*
15442: ** Open a new database file named "zNewDb". Try to recover as much information
15443: ** as possible out of the main database (which might be corrupt) and write it
15444: ** into zNewDb.
15445: */
15446: static void tryToClone(ShellState *p, const char *zNewDb){
15447: int rc;
15448: sqlite3 *newDb = 0;
15449: if( access(zNewDb,0)==0 ){
15450: utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
15451: return;
15452: }
15453: rc = sqlite3_open(zNewDb, &newDb);
15454: if( rc ){
15455: utf8_printf(stderr, "Cannot create output database: %s\n",
15456: sqlite3_errmsg(newDb));
15457: }else{
15458: sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
15459: sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
15460: tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
15461: tryToCloneSchema(p, newDb, "type!='table'", 0);
15462: sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
15463: sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15464: }
15465: close_db(newDb);
15466: }
15467:
15468: /*
15469: ** Change the output file back to stdout.
15470: **
15471: ** If the p->doXdgOpen flag is set, that means the output was being
15472: ** redirected to a temporary file named by p->zTempFile. In that case,
15473: ** launch start/open/xdg-open on that temporary file.
15474: */
15475: static void output_reset(ShellState *p){
15476: if( p->outfile[0]=='|' ){
15477: #ifndef SQLITE_OMIT_POPEN
15478: pclose(p->out);
15479: #endif
15480: }else{
15481: output_file_close(p->out);
15482: #ifndef SQLITE_NOHAVE_SYSTEM
15483: if( p->doXdgOpen ){
15484: const char *zXdgOpenCmd =
15485: #if defined(_WIN32)
15486: "start";
15487: #elif defined(__APPLE__)
15488: "open";
15489: #else
15490: "xdg-open";
15491: #endif
15492: char *zCmd;
15493: zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
15494: if( system(zCmd) ){
15495: utf8_printf(stderr, "Failed: [%s]\n", zCmd);
15496: }else{
15497: /* Give the start/open/xdg-open command some time to get
15498: ** going before we continue, and potential delete the
15499: ** p->zTempFile data file out from under it */
15500: sqlite3_sleep(2000);
15501: }
15502: sqlite3_free(zCmd);
15503: outputModePop(p);
15504: p->doXdgOpen = 0;
15505: }
15506: #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15507: }
15508: p->outfile[0] = 0;
15509: p->out = stdout;
15510: }
15511:
15512: /*
15513: ** Run an SQL command and return the single integer result.
15514: */
15515: static int db_int(ShellState *p, const char *zSql){
15516: sqlite3_stmt *pStmt;
15517: int res = 0;
15518: sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15519: if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
15520: res = sqlite3_column_int(pStmt,0);
15521: }
15522: sqlite3_finalize(pStmt);
15523: return res;
15524: }
15525:
15526: /*
15527: ** Convert a 2-byte or 4-byte big-endian integer into a native integer
15528: */
15529: static unsigned int get2byteInt(unsigned char *a){
15530: return (a[0]<<8) + a[1];
15531: }
15532: static unsigned int get4byteInt(unsigned char *a){
15533: return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
15534: }
15535:
15536: /*
15537: ** Implementation of the ".dbinfo" command.
15538: **
15539: ** Return 1 on error, 2 to exit, and 0 otherwise.
15540: */
15541: static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
15542: static const struct { const char *zName; int ofst; } aField[] = {
15543: { "file change counter:", 24 },
15544: { "database page count:", 28 },
15545: { "freelist page count:", 36 },
15546: { "schema cookie:", 40 },
15547: { "schema format:", 44 },
15548: { "default cache size:", 48 },
15549: { "autovacuum top root:", 52 },
15550: { "incremental vacuum:", 64 },
15551: { "text encoding:", 56 },
15552: { "user version:", 60 },
15553: { "application id:", 68 },
15554: { "software version:", 96 },
15555: };
15556: static const struct { const char *zName; const char *zSql; } aQuery[] = {
15557: { "number of tables:",
15558: "SELECT count(*) FROM %s WHERE type='table'" },
15559: { "number of indexes:",
15560: "SELECT count(*) FROM %s WHERE type='index'" },
15561: { "number of triggers:",
15562: "SELECT count(*) FROM %s WHERE type='trigger'" },
15563: { "number of views:",
15564: "SELECT count(*) FROM %s WHERE type='view'" },
15565: { "schema size:",
15566: "SELECT total(length(sql)) FROM %s" },
15567: };
15568: int i, rc;
15569: unsigned iDataVersion;
15570: char *zSchemaTab;
15571: char *zDb = nArg>=2 ? azArg[1] : "main";
15572: sqlite3_stmt *pStmt = 0;
15573: unsigned char aHdr[100];
15574: open_db(p, 0);
15575: if( p->db==0 ) return 1;
15576: rc = sqlite3_prepare_v2(p->db,
15577: "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
15578: -1, &pStmt, 0);
15579: if( rc ){
15580: utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
15581: sqlite3_finalize(pStmt);
15582: return 1;
15583: }
15584: sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
15585: if( sqlite3_step(pStmt)==SQLITE_ROW
15586: && sqlite3_column_bytes(pStmt,0)>100
15587: ){
15588: memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
15589: sqlite3_finalize(pStmt);
15590: }else{
15591: raw_printf(stderr, "unable to read database header\n");
15592: sqlite3_finalize(pStmt);
15593: return 1;
15594: }
15595: i = get2byteInt(aHdr+16);
15596: if( i==1 ) i = 65536;
15597: utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
15598: utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
15599: utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
15600: utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
15601: for(i=0; i<ArraySize(aField); i++){
15602: int ofst = aField[i].ofst;
15603: unsigned int val = get4byteInt(aHdr + ofst);
15604: utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
15605: switch( ofst ){
15606: case 56: {
15607: if( val==1 ) raw_printf(p->out, " (utf8)");
15608: if( val==2 ) raw_printf(p->out, " (utf16le)");
15609: if( val==3 ) raw_printf(p->out, " (utf16be)");
15610: }
15611: }
15612: raw_printf(p->out, "\n");
15613: }
15614: if( zDb==0 ){
15615: zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
15616: }else if( strcmp(zDb,"temp")==0 ){
15617: zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
15618: }else{
15619: zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
15620: }
15621: for(i=0; i<ArraySize(aQuery); i++){
15622: char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
15623: int val = db_int(p, zSql);
15624: sqlite3_free(zSql);
15625: utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
15626: }
15627: sqlite3_free(zSchemaTab);
15628: sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
15629: utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
15630: return 0;
15631: }
15632:
15633: /*
15634: ** Print the current sqlite3_errmsg() value to stderr and return 1.
15635: */
15636: static int shellDatabaseError(sqlite3 *db){
15637: const char *zErr = sqlite3_errmsg(db);
15638: utf8_printf(stderr, "Error: %s\n", zErr);
15639: return 1;
15640: }
15641:
15642: /*
15643: ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
15644: ** if they match and FALSE (0) if they do not match.
15645: **
15646: ** Globbing rules:
15647: **
15648: ** '*' Matches any sequence of zero or more characters.
15649: **
15650: ** '?' Matches exactly one character.
15651: **
15652: ** [...] Matches one character from the enclosed list of
15653: ** characters.
15654: **
15655: ** [^...] Matches one character not in the enclosed list.
15656: **
15657: ** '#' Matches any sequence of one or more digits with an
15658: ** optional + or - sign in front
15659: **
15660: ** ' ' Any span of whitespace matches any other span of
15661: ** whitespace.
15662: **
15663: ** Extra whitespace at the end of z[] is ignored.
15664: */
15665: static int testcase_glob(const char *zGlob, const char *z){
15666: int c, c2;
15667: int invert;
15668: int seen;
15669:
15670: while( (c = (*(zGlob++)))!=0 ){
15671: if( IsSpace(c) ){
15672: if( !IsSpace(*z) ) return 0;
15673: while( IsSpace(*zGlob) ) zGlob++;
15674: while( IsSpace(*z) ) z++;
15675: }else if( c=='*' ){
15676: while( (c=(*(zGlob++))) == '*' || c=='?' ){
15677: if( c=='?' && (*(z++))==0 ) return 0;
15678: }
15679: if( c==0 ){
15680: return 1;
15681: }else if( c=='[' ){
15682: while( *z && testcase_glob(zGlob-1,z)==0 ){
15683: z++;
15684: }
15685: return (*z)!=0;
15686: }
15687: while( (c2 = (*(z++)))!=0 ){
15688: while( c2!=c ){
15689: c2 = *(z++);
15690: if( c2==0 ) return 0;
15691: }
15692: if( testcase_glob(zGlob,z) ) return 1;
15693: }
15694: return 0;
15695: }else if( c=='?' ){
15696: if( (*(z++))==0 ) return 0;
15697: }else if( c=='[' ){
15698: int prior_c = 0;
15699: seen = 0;
15700: invert = 0;
15701: c = *(z++);
15702: if( c==0 ) return 0;
15703: c2 = *(zGlob++);
15704: if( c2=='^' ){
15705: invert = 1;
15706: c2 = *(zGlob++);
15707: }
15708: if( c2==']' ){
15709: if( c==']' ) seen = 1;
15710: c2 = *(zGlob++);
15711: }
15712: while( c2 && c2!=']' ){
15713: if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
15714: c2 = *(zGlob++);
15715: if( c>=prior_c && c<=c2 ) seen = 1;
15716: prior_c = 0;
15717: }else{
15718: if( c==c2 ){
15719: seen = 1;
15720: }
15721: prior_c = c2;
15722: }
15723: c2 = *(zGlob++);
15724: }
15725: if( c2==0 || (seen ^ invert)==0 ) return 0;
15726: }else if( c=='#' ){
15727: if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
15728: if( !IsDigit(z[0]) ) return 0;
15729: z++;
15730: while( IsDigit(z[0]) ){ z++; }
15731: }else{
15732: if( c!=(*(z++)) ) return 0;
15733: }
15734: }
15735: while( IsSpace(*z) ){ z++; }
15736: return *z==0;
15737: }
15738:
15739:
15740: /*
15741: ** Compare the string as a command-line option with either one or two
15742: ** initial "-" characters.
15743: */
15744: static int optionMatch(const char *zStr, const char *zOpt){
15745: if( zStr[0]!='-' ) return 0;
15746: zStr++;
15747: if( zStr[0]=='-' ) zStr++;
15748: return strcmp(zStr, zOpt)==0;
15749: }
15750:
15751: /*
15752: ** Delete a file.
15753: */
15754: int shellDeleteFile(const char *zFilename){
15755: int rc;
15756: #ifdef _WIN32
15757: wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
15758: rc = _wunlink(z);
15759: sqlite3_free(z);
15760: #else
15761: rc = unlink(zFilename);
15762: #endif
15763: return rc;
15764: }
15765:
15766: /*
15767: ** Try to delete the temporary file (if there is one) and free the
15768: ** memory used to hold the name of the temp file.
15769: */
15770: static void clearTempFile(ShellState *p){
15771: if( p->zTempFile==0 ) return;
15772: if( p->doXdgOpen ) return;
15773: if( shellDeleteFile(p->zTempFile) ) return;
15774: sqlite3_free(p->zTempFile);
15775: p->zTempFile = 0;
15776: }
15777:
15778: /*
15779: ** Create a new temp file name with the given suffix.
15780: */
15781: static void newTempFile(ShellState *p, const char *zSuffix){
15782: clearTempFile(p);
15783: sqlite3_free(p->zTempFile);
15784: p->zTempFile = 0;
15785: if( p->db ){
15786: sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
15787: }
15788: if( p->zTempFile==0 ){
15789: /* If p->db is an in-memory database then the TEMPFILENAME file-control
15790: ** will not work and we will need to fallback to guessing */
15791: char *zTemp;
15792: sqlite3_uint64 r;
15793: sqlite3_randomness(sizeof(r), &r);
15794: zTemp = getenv("TEMP");
15795: if( zTemp==0 ) zTemp = getenv("TMP");
15796: if( zTemp==0 ){
15797: #ifdef _WIN32
15798: zTemp = "\\tmp";
15799: #else
15800: zTemp = "/tmp";
15801: #endif
15802: }
15803: p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
15804: }else{
15805: p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
15806: }
15807: if( p->zTempFile==0 ){
15808: raw_printf(stderr, "out of memory\n");
15809: exit(1);
15810: }
15811: }
15812:
15813:
15814: /*
15815: ** The implementation of SQL scalar function fkey_collate_clause(), used
15816: ** by the ".lint fkey-indexes" command. This scalar function is always
15817: ** called with four arguments - the parent table name, the parent column name,
15818: ** the child table name and the child column name.
15819: **
15820: ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
15821: **
15822: ** If either of the named tables or columns do not exist, this function
15823: ** returns an empty string. An empty string is also returned if both tables
15824: ** and columns exist but have the same default collation sequence. Or,
15825: ** if both exist but the default collation sequences are different, this
15826: ** function returns the string " COLLATE <parent-collation>", where
15827: ** <parent-collation> is the default collation sequence of the parent column.
15828: */
15829: static void shellFkeyCollateClause(
15830: sqlite3_context *pCtx,
15831: int nVal,
15832: sqlite3_value **apVal
15833: ){
15834: sqlite3 *db = sqlite3_context_db_handle(pCtx);
15835: const char *zParent;
15836: const char *zParentCol;
15837: const char *zParentSeq;
15838: const char *zChild;
15839: const char *zChildCol;
15840: const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
15841: int rc;
15842:
15843: assert( nVal==4 );
15844: zParent = (const char*)sqlite3_value_text(apVal[0]);
15845: zParentCol = (const char*)sqlite3_value_text(apVal[1]);
15846: zChild = (const char*)sqlite3_value_text(apVal[2]);
15847: zChildCol = (const char*)sqlite3_value_text(apVal[3]);
15848:
15849: sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
15850: rc = sqlite3_table_column_metadata(
15851: db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
15852: );
15853: if( rc==SQLITE_OK ){
15854: rc = sqlite3_table_column_metadata(
15855: db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
15856: );
15857: }
15858:
15859: if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
15860: char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
15861: sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
15862: sqlite3_free(z);
15863: }
15864: }
15865:
15866:
15867: /*
15868: ** The implementation of dot-command ".lint fkey-indexes".
15869: */
15870: static int lintFkeyIndexes(
15871: ShellState *pState, /* Current shell tool state */
15872: char **azArg, /* Array of arguments passed to dot command */
15873: int nArg /* Number of entries in azArg[] */
15874: ){
15875: sqlite3 *db = pState->db; /* Database handle to query "main" db of */
15876: FILE *out = pState->out; /* Stream to write non-error output to */
15877: int bVerbose = 0; /* If -verbose is present */
15878: int bGroupByParent = 0; /* If -groupbyparent is present */
15879: int i; /* To iterate through azArg[] */
15880: const char *zIndent = ""; /* How much to indent CREATE INDEX by */
15881: int rc; /* Return code */
15882: sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
15883:
15884: /*
15885: ** This SELECT statement returns one row for each foreign key constraint
15886: ** in the schema of the main database. The column values are:
15887: **
15888: ** 0. The text of an SQL statement similar to:
15889: **
15890: ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
15891: **
15892: ** This SELECT is similar to the one that the foreign keys implementation
15893: ** needs to run internally on child tables. If there is an index that can
15894: ** be used to optimize this query, then it can also be used by the FK
15895: ** implementation to optimize DELETE or UPDATE statements on the parent
15896: ** table.
15897: **
15898: ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
15899: ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
15900: ** contains an index that can be used to optimize the query.
15901: **
15902: ** 2. Human readable text that describes the child table and columns. e.g.
15903: **
15904: ** "child_table(child_key1, child_key2)"
15905: **
15906: ** 3. Human readable text that describes the parent table and columns. e.g.
15907: **
15908: ** "parent_table(parent_key1, parent_key2)"
15909: **
15910: ** 4. A full CREATE INDEX statement for an index that could be used to
15911: ** optimize DELETE or UPDATE statements on the parent table. e.g.
15912: **
15913: ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
15914: **
15915: ** 5. The name of the parent table.
15916: **
15917: ** These six values are used by the C logic below to generate the report.
15918: */
15919: const char *zSql =
15920: "SELECT "
15921: " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
15922: " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
15923: " || fkey_collate_clause("
15924: " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
15925: ", "
15926: " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
15927: " || group_concat('*=?', ' AND ') || ')'"
15928: ", "
15929: " s.name || '(' || group_concat(f.[from], ', ') || ')'"
15930: ", "
15931: " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
15932: ", "
15933: " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
15934: " || ' ON ' || quote(s.name) || '('"
15935: " || group_concat(quote(f.[from]) ||"
15936: " fkey_collate_clause("
15937: " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
15938: " || ');'"
15939: ", "
15940: " f.[table] "
15941: "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
15942: "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
15943: "GROUP BY s.name, f.id "
15944: "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
15945: ;
15946: const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
15947:
15948: for(i=2; i<nArg; i++){
15949: int n = strlen30(azArg[i]);
15950: if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
15951: bVerbose = 1;
15952: }
15953: else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
15954: bGroupByParent = 1;
15955: zIndent = " ";
15956: }
15957: else{
15958: raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
15959: azArg[0], azArg[1]
15960: );
15961: return SQLITE_ERROR;
15962: }
15963: }
15964:
15965: /* Register the fkey_collate_clause() SQL function */
15966: rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
15967: 0, shellFkeyCollateClause, 0, 0
15968: );
15969:
15970:
15971: if( rc==SQLITE_OK ){
15972: rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
15973: }
15974: if( rc==SQLITE_OK ){
15975: sqlite3_bind_int(pSql, 1, bGroupByParent);
15976: }
15977:
15978: if( rc==SQLITE_OK ){
15979: int rc2;
15980: char *zPrev = 0;
15981: while( SQLITE_ROW==sqlite3_step(pSql) ){
15982: int res = -1;
15983: sqlite3_stmt *pExplain = 0;
15984: const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
15985: const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
15986: const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
15987: const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
15988: const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
15989: const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
15990:
15991: rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
15992: if( rc!=SQLITE_OK ) break;
15993: if( SQLITE_ROW==sqlite3_step(pExplain) ){
15994: const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
15995: res = (
15996: 0==sqlite3_strglob(zGlob, zPlan)
15997: || 0==sqlite3_strglob(zGlobIPK, zPlan)
15998: );
15999: }
16000: rc = sqlite3_finalize(pExplain);
16001: if( rc!=SQLITE_OK ) break;
16002:
16003: if( res<0 ){
16004: raw_printf(stderr, "Error: internal error");
16005: break;
16006: }else{
16007: if( bGroupByParent
16008: && (bVerbose || res==0)
16009: && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
16010: ){
16011: raw_printf(out, "-- Parent table %s\n", zParent);
16012: sqlite3_free(zPrev);
16013: zPrev = sqlite3_mprintf("%s", zParent);
16014: }
16015:
16016: if( res==0 ){
16017: raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
16018: }else if( bVerbose ){
16019: raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
16020: zIndent, zFrom, zTarget
16021: );
16022: }
16023: }
16024: }
16025: sqlite3_free(zPrev);
16026:
16027: if( rc!=SQLITE_OK ){
16028: raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16029: }
16030:
16031: rc2 = sqlite3_finalize(pSql);
16032: if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
16033: rc = rc2;
16034: raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16035: }
16036: }else{
16037: raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16038: }
16039:
16040: return rc;
16041: }
16042:
16043: /*
16044: ** Implementation of ".lint" dot command.
16045: */
16046: static int lintDotCommand(
16047: ShellState *pState, /* Current shell tool state */
16048: char **azArg, /* Array of arguments passed to dot command */
16049: int nArg /* Number of entries in azArg[] */
16050: ){
16051: int n;
16052: n = (nArg>=2 ? strlen30(azArg[1]) : 0);
16053: if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
16054: return lintFkeyIndexes(pState, azArg, nArg);
16055:
16056: usage:
16057: raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
16058: raw_printf(stderr, "Where sub-commands are:\n");
16059: raw_printf(stderr, " fkey-indexes\n");
16060: return SQLITE_ERROR;
16061: }
16062:
16063: #if !defined SQLITE_OMIT_VIRTUALTABLE
16064: static void shellPrepare(
16065: sqlite3 *db,
16066: int *pRc,
16067: const char *zSql,
16068: sqlite3_stmt **ppStmt
16069: ){
16070: *ppStmt = 0;
16071: if( *pRc==SQLITE_OK ){
16072: int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
16073: if( rc!=SQLITE_OK ){
16074: raw_printf(stderr, "sql error: %s (%d)\n",
16075: sqlite3_errmsg(db), sqlite3_errcode(db)
16076: );
16077: *pRc = rc;
16078: }
16079: }
16080: }
16081:
16082: /*
16083: ** Create a prepared statement using printf-style arguments for the SQL.
16084: **
16085: ** This routine is could be marked "static". But it is not always used,
16086: ** depending on compile-time options. By omitting the "static", we avoid
16087: ** nuisance compiler warnings about "defined but not used".
16088: */
16089: void shellPreparePrintf(
16090: sqlite3 *db,
16091: int *pRc,
16092: sqlite3_stmt **ppStmt,
16093: const char *zFmt,
16094: ...
16095: ){
16096: *ppStmt = 0;
16097: if( *pRc==SQLITE_OK ){
16098: va_list ap;
16099: char *z;
16100: va_start(ap, zFmt);
16101: z = sqlite3_vmprintf(zFmt, ap);
16102: va_end(ap);
16103: if( z==0 ){
16104: *pRc = SQLITE_NOMEM;
16105: }else{
16106: shellPrepare(db, pRc, z, ppStmt);
16107: sqlite3_free(z);
16108: }
16109: }
16110: }
16111:
16112: /* Finalize the prepared statement created using shellPreparePrintf().
16113: **
16114: ** This routine is could be marked "static". But it is not always used,
16115: ** depending on compile-time options. By omitting the "static", we avoid
16116: ** nuisance compiler warnings about "defined but not used".
16117: */
16118: void shellFinalize(
16119: int *pRc,
16120: sqlite3_stmt *pStmt
16121: ){
16122: if( pStmt ){
16123: sqlite3 *db = sqlite3_db_handle(pStmt);
16124: int rc = sqlite3_finalize(pStmt);
16125: if( *pRc==SQLITE_OK ){
16126: if( rc!=SQLITE_OK ){
16127: raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16128: }
16129: *pRc = rc;
16130: }
16131: }
16132: }
16133:
16134: /* Reset the prepared statement created using shellPreparePrintf().
16135: **
16136: ** This routine is could be marked "static". But it is not always used,
16137: ** depending on compile-time options. By omitting the "static", we avoid
16138: ** nuisance compiler warnings about "defined but not used".
16139: */
16140: void shellReset(
16141: int *pRc,
16142: sqlite3_stmt *pStmt
16143: ){
16144: int rc = sqlite3_reset(pStmt);
16145: if( *pRc==SQLITE_OK ){
16146: if( rc!=SQLITE_OK ){
16147: sqlite3 *db = sqlite3_db_handle(pStmt);
16148: raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16149: }
16150: *pRc = rc;
16151: }
16152: }
16153: #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16154:
16155: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16156: /******************************************************************************
16157: ** The ".archive" or ".ar" command.
16158: */
16159: /*
16160: ** Structure representing a single ".ar" command.
16161: */
16162: typedef struct ArCommand ArCommand;
16163: struct ArCommand {
16164: u8 eCmd; /* An AR_CMD_* value */
16165: u8 bVerbose; /* True if --verbose */
16166: u8 bZip; /* True if the archive is a ZIP */
16167: u8 bDryRun; /* True if --dry-run */
16168: u8 bAppend; /* True if --append */
16169: u8 fromCmdLine; /* Run from -A instead of .archive */
16170: int nArg; /* Number of command arguments */
16171: char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
16172: const char *zFile; /* --file argument, or NULL */
16173: const char *zDir; /* --directory argument, or NULL */
16174: char **azArg; /* Array of command arguments */
16175: ShellState *p; /* Shell state */
16176: sqlite3 *db; /* Database containing the archive */
16177: };
16178:
16179: /*
16180: ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16181: */
16182: static int arUsage(FILE *f){
16183: showHelp(f,"archive");
16184: return SQLITE_ERROR;
16185: }
16186:
16187: /*
16188: ** Print an error message for the .ar command to stderr and return
16189: ** SQLITE_ERROR.
16190: */
16191: static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16192: va_list ap;
16193: char *z;
16194: va_start(ap, zFmt);
16195: z = sqlite3_vmprintf(zFmt, ap);
16196: va_end(ap);
16197: utf8_printf(stderr, "Error: %s\n", z);
16198: if( pAr->fromCmdLine ){
16199: utf8_printf(stderr, "Use \"-A\" for more help\n");
16200: }else{
16201: utf8_printf(stderr, "Use \".archive --help\" for more help\n");
16202: }
16203: sqlite3_free(z);
16204: return SQLITE_ERROR;
16205: }
16206:
16207: /*
16208: ** Values for ArCommand.eCmd.
16209: */
16210: #define AR_CMD_CREATE 1
16211: #define AR_CMD_UPDATE 2
16212: #define AR_CMD_INSERT 3
16213: #define AR_CMD_EXTRACT 4
16214: #define AR_CMD_LIST 5
16215: #define AR_CMD_HELP 6
16216:
16217: /*
16218: ** Other (non-command) switches.
16219: */
16220: #define AR_SWITCH_VERBOSE 7
16221: #define AR_SWITCH_FILE 8
16222: #define AR_SWITCH_DIRECTORY 9
16223: #define AR_SWITCH_APPEND 10
16224: #define AR_SWITCH_DRYRUN 11
16225:
16226: static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
16227: switch( eSwitch ){
16228: case AR_CMD_CREATE:
16229: case AR_CMD_EXTRACT:
16230: case AR_CMD_LIST:
16231: case AR_CMD_UPDATE:
16232: case AR_CMD_INSERT:
16233: case AR_CMD_HELP:
16234: if( pAr->eCmd ){
16235: return arErrorMsg(pAr, "multiple command options");
16236: }
16237: pAr->eCmd = eSwitch;
16238: break;
16239:
16240: case AR_SWITCH_DRYRUN:
16241: pAr->bDryRun = 1;
16242: break;
16243: case AR_SWITCH_VERBOSE:
16244: pAr->bVerbose = 1;
16245: break;
16246: case AR_SWITCH_APPEND:
16247: pAr->bAppend = 1;
16248: /* Fall thru into --file */
16249: case AR_SWITCH_FILE:
16250: pAr->zFile = zArg;
16251: break;
16252: case AR_SWITCH_DIRECTORY:
16253: pAr->zDir = zArg;
16254: break;
16255: }
16256:
16257: return SQLITE_OK;
16258: }
16259:
16260: /*
16261: ** Parse the command line for an ".ar" command. The results are written into
16262: ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
16263: ** successfully, otherwise an error message is written to stderr and
16264: ** SQLITE_ERROR returned.
16265: */
16266: static int arParseCommand(
16267: char **azArg, /* Array of arguments passed to dot command */
16268: int nArg, /* Number of entries in azArg[] */
16269: ArCommand *pAr /* Populate this object */
16270: ){
16271: struct ArSwitch {
16272: const char *zLong;
16273: char cShort;
16274: u8 eSwitch;
16275: u8 bArg;
16276: } aSwitch[] = {
16277: { "create", 'c', AR_CMD_CREATE, 0 },
16278: { "extract", 'x', AR_CMD_EXTRACT, 0 },
16279: { "insert", 'i', AR_CMD_INSERT, 0 },
16280: { "list", 't', AR_CMD_LIST, 0 },
16281: { "update", 'u', AR_CMD_UPDATE, 0 },
16282: { "help", 'h', AR_CMD_HELP, 0 },
16283: { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
16284: { "file", 'f', AR_SWITCH_FILE, 1 },
16285: { "append", 'a', AR_SWITCH_APPEND, 1 },
16286: { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
16287: { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
16288: };
16289: int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
16290: struct ArSwitch *pEnd = &aSwitch[nSwitch];
16291:
16292: if( nArg<=1 ){
16293: utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
16294: return arUsage(stderr);
16295: }else{
16296: char *z = azArg[1];
16297: if( z[0]!='-' ){
16298: /* Traditional style [tar] invocation */
16299: int i;
16300: int iArg = 2;
16301: for(i=0; z[i]; i++){
16302: const char *zArg = 0;
16303: struct ArSwitch *pOpt;
16304: for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16305: if( z[i]==pOpt->cShort ) break;
16306: }
16307: if( pOpt==pEnd ){
16308: return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16309: }
16310: if( pOpt->bArg ){
16311: if( iArg>=nArg ){
16312: return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
16313: }
16314: zArg = azArg[iArg++];
16315: }
16316: if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16317: }
16318: pAr->nArg = nArg-iArg;
16319: if( pAr->nArg>0 ){
16320: pAr->azArg = &azArg[iArg];
16321: }
16322: }else{
16323: /* Non-traditional invocation */
16324: int iArg;
16325: for(iArg=1; iArg<nArg; iArg++){
16326: int n;
16327: z = azArg[iArg];
16328: if( z[0]!='-' ){
16329: /* All remaining command line words are command arguments. */
16330: pAr->azArg = &azArg[iArg];
16331: pAr->nArg = nArg-iArg;
16332: break;
16333: }
16334: n = strlen30(z);
16335:
16336: if( z[1]!='-' ){
16337: int i;
16338: /* One or more short options */
16339: for(i=1; i<n; i++){
16340: const char *zArg = 0;
16341: struct ArSwitch *pOpt;
16342: for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16343: if( z[i]==pOpt->cShort ) break;
16344: }
16345: if( pOpt==pEnd ){
16346: return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16347: }
16348: if( pOpt->bArg ){
16349: if( i<(n-1) ){
16350: zArg = &z[i+1];
16351: i = n;
16352: }else{
16353: if( iArg>=(nArg-1) ){
16354: return arErrorMsg(pAr, "option requires an argument: %c",
16355: z[i]);
16356: }
16357: zArg = azArg[++iArg];
16358: }
16359: }
16360: if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16361: }
16362: }else if( z[2]=='\0' ){
16363: /* A -- option, indicating that all remaining command line words
16364: ** are command arguments. */
16365: pAr->azArg = &azArg[iArg+1];
16366: pAr->nArg = nArg-iArg-1;
16367: break;
16368: }else{
16369: /* A long option */
16370: const char *zArg = 0; /* Argument for option, if any */
16371: struct ArSwitch *pMatch = 0; /* Matching option */
16372: struct ArSwitch *pOpt; /* Iterator */
16373: for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16374: const char *zLong = pOpt->zLong;
16375: if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
16376: if( pMatch ){
16377: return arErrorMsg(pAr, "ambiguous option: %s",z);
16378: }else{
16379: pMatch = pOpt;
16380: }
16381: }
16382: }
16383:
16384: if( pMatch==0 ){
16385: return arErrorMsg(pAr, "unrecognized option: %s", z);
16386: }
16387: if( pMatch->bArg ){
16388: if( iArg>=(nArg-1) ){
16389: return arErrorMsg(pAr, "option requires an argument: %s", z);
16390: }
16391: zArg = azArg[++iArg];
16392: }
16393: if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
16394: }
16395: }
16396: }
16397: }
16398:
16399: return SQLITE_OK;
16400: }
16401:
16402: /*
16403: ** This function assumes that all arguments within the ArCommand.azArg[]
16404: ** array refer to archive members, as for the --extract or --list commands.
16405: ** It checks that each of them are present. If any specified file is not
16406: ** present in the archive, an error is printed to stderr and an error
16407: ** code returned. Otherwise, if all specified arguments are present in
16408: ** the archive, SQLITE_OK is returned.
16409: **
16410: ** This function strips any trailing '/' characters from each argument.
16411: ** This is consistent with the way the [tar] command seems to work on
16412: ** Linux.
16413: */
16414: static int arCheckEntries(ArCommand *pAr){
16415: int rc = SQLITE_OK;
16416: if( pAr->nArg ){
16417: int i, j;
16418: sqlite3_stmt *pTest = 0;
16419:
16420: shellPreparePrintf(pAr->db, &rc, &pTest,
16421: "SELECT name FROM %s WHERE name=$name",
16422: pAr->zSrcTable
16423: );
16424: j = sqlite3_bind_parameter_index(pTest, "$name");
16425: for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16426: char *z = pAr->azArg[i];
16427: int n = strlen30(z);
16428: int bOk = 0;
16429: while( n>0 && z[n-1]=='/' ) n--;
16430: z[n] = '\0';
16431: sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
16432: if( SQLITE_ROW==sqlite3_step(pTest) ){
16433: bOk = 1;
16434: }
16435: shellReset(&rc, pTest);
16436: if( rc==SQLITE_OK && bOk==0 ){
16437: utf8_printf(stderr, "not found in archive: %s\n", z);
16438: rc = SQLITE_ERROR;
16439: }
16440: }
16441: shellFinalize(&rc, pTest);
16442: }
16443: return rc;
16444: }
16445:
16446: /*
16447: ** Format a WHERE clause that can be used against the "sqlar" table to
16448: ** identify all archive members that match the command arguments held
16449: ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
16450: ** The caller is responsible for eventually calling sqlite3_free() on
16451: ** any non-NULL (*pzWhere) value.
16452: */
16453: static void arWhereClause(
16454: int *pRc,
16455: ArCommand *pAr,
16456: char **pzWhere /* OUT: New WHERE clause */
16457: ){
16458: char *zWhere = 0;
16459: if( *pRc==SQLITE_OK ){
16460: if( pAr->nArg==0 ){
16461: zWhere = sqlite3_mprintf("1");
16462: }else{
16463: int i;
16464: const char *zSep = "";
16465: for(i=0; i<pAr->nArg; i++){
16466: const char *z = pAr->azArg[i];
16467: zWhere = sqlite3_mprintf(
16468: "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
16469: zWhere, zSep, z, strlen30(z)+1, z
16470: );
16471: if( zWhere==0 ){
16472: *pRc = SQLITE_NOMEM;
16473: break;
16474: }
16475: zSep = " OR ";
16476: }
16477: }
16478: }
16479: *pzWhere = zWhere;
16480: }
16481:
16482: /*
16483: ** Implementation of .ar "lisT" command.
16484: */
16485: static int arListCommand(ArCommand *pAr){
16486: const char *zSql = "SELECT %s FROM %s WHERE %s";
16487: const char *azCols[] = {
16488: "name",
16489: "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
16490: };
16491:
16492: char *zWhere = 0;
16493: sqlite3_stmt *pSql = 0;
16494: int rc;
16495:
16496: rc = arCheckEntries(pAr);
16497: arWhereClause(&rc, pAr, &zWhere);
16498:
16499: shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
16500: pAr->zSrcTable, zWhere);
16501: if( pAr->bDryRun ){
16502: utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16503: }else{
16504: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16505: if( pAr->bVerbose ){
16506: utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
16507: sqlite3_column_text(pSql, 0),
16508: sqlite3_column_int(pSql, 1),
16509: sqlite3_column_text(pSql, 2),
16510: sqlite3_column_text(pSql, 3)
16511: );
16512: }else{
16513: utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16514: }
16515: }
16516: }
16517: shellFinalize(&rc, pSql);
16518: sqlite3_free(zWhere);
16519: return rc;
16520: }
16521:
16522:
16523: /*
16524: ** Implementation of .ar "eXtract" command.
16525: */
16526: static int arExtractCommand(ArCommand *pAr){
16527: const char *zSql1 =
16528: "SELECT "
16529: " ($dir || name),"
16530: " writefile(($dir || name), %s, mode, mtime) "
16531: "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
16532: " AND name NOT GLOB '*..[/\\]*'";
16533:
16534: const char *azExtraArg[] = {
16535: "sqlar_uncompress(data, sz)",
16536: "data"
16537: };
16538:
16539: sqlite3_stmt *pSql = 0;
16540: int rc = SQLITE_OK;
16541: char *zDir = 0;
16542: char *zWhere = 0;
16543: int i, j;
16544:
16545: /* If arguments are specified, check that they actually exist within
16546: ** the archive before proceeding. And formulate a WHERE clause to
16547: ** match them. */
16548: rc = arCheckEntries(pAr);
16549: arWhereClause(&rc, pAr, &zWhere);
16550:
16551: if( rc==SQLITE_OK ){
16552: if( pAr->zDir ){
16553: zDir = sqlite3_mprintf("%s/", pAr->zDir);
16554: }else{
16555: zDir = sqlite3_mprintf("");
16556: }
16557: if( zDir==0 ) rc = SQLITE_NOMEM;
16558: }
16559:
16560: shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
16561: azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
16562: );
16563:
16564: if( rc==SQLITE_OK ){
16565: j = sqlite3_bind_parameter_index(pSql, "$dir");
16566: sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
16567:
16568: /* Run the SELECT statement twice. The first time, writefile() is called
16569: ** for all archive members that should be extracted. The second time,
16570: ** only for the directories. This is because the timestamps for
16571: ** extracted directories must be reset after they are populated (as
16572: ** populating them changes the timestamp). */
16573: for(i=0; i<2; i++){
16574: j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
16575: sqlite3_bind_int(pSql, j, i);
16576: if( pAr->bDryRun ){
16577: utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16578: }else{
16579: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16580: if( i==0 && pAr->bVerbose ){
16581: utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16582: }
16583: }
16584: }
16585: shellReset(&rc, pSql);
16586: }
16587: shellFinalize(&rc, pSql);
16588: }
16589:
16590: sqlite3_free(zDir);
16591: sqlite3_free(zWhere);
16592: return rc;
16593: }
16594:
16595: /*
16596: ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
16597: */
16598: static int arExecSql(ArCommand *pAr, const char *zSql){
16599: int rc;
16600: if( pAr->bDryRun ){
16601: utf8_printf(pAr->p->out, "%s\n", zSql);
16602: rc = SQLITE_OK;
16603: }else{
16604: char *zErr = 0;
16605: rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
16606: if( zErr ){
16607: utf8_printf(stdout, "ERROR: %s\n", zErr);
16608: sqlite3_free(zErr);
16609: }
16610: }
16611: return rc;
16612: }
16613:
16614:
16615: /*
16616: ** Implementation of .ar "create", "insert", and "update" commands.
16617: **
16618: ** create -> Create a new SQL archive
16619: ** insert -> Insert or reinsert all files listed
16620: ** update -> Insert files that have changed or that were not
16621: ** previously in the archive
16622: **
16623: ** Create the "sqlar" table in the database if it does not already exist.
16624: ** Then add each file in the azFile[] array to the archive. Directories
16625: ** are added recursively. If argument bVerbose is non-zero, a message is
16626: ** printed on stdout for each file archived.
16627: **
16628: ** The create command is the same as update, except that it drops
16629: ** any existing "sqlar" table before beginning. The "insert" command
16630: ** always overwrites every file named on the command-line, where as
16631: ** "update" only overwrites if the size or mtime or mode has changed.
16632: */
16633: static int arCreateOrUpdateCommand(
16634: ArCommand *pAr, /* Command arguments and options */
16635: int bUpdate, /* true for a --create. */
16636: int bOnlyIfChanged /* Only update if file has changed */
16637: ){
16638: const char *zCreate =
16639: "CREATE TABLE IF NOT EXISTS sqlar(\n"
16640: " name TEXT PRIMARY KEY, -- name of the file\n"
16641: " mode INT, -- access permissions\n"
16642: " mtime INT, -- last modification time\n"
16643: " sz INT, -- original file size\n"
16644: " data BLOB -- compressed content\n"
16645: ")";
16646: const char *zDrop = "DROP TABLE IF EXISTS sqlar";
16647: const char *zInsertFmt[2] = {
16648: "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
16649: " SELECT\n"
16650: " %s,\n"
16651: " mode,\n"
16652: " mtime,\n"
16653: " CASE substr(lsmode(mode),1,1)\n"
16654: " WHEN '-' THEN length(data)\n"
16655: " WHEN 'd' THEN 0\n"
16656: " ELSE -1 END,\n"
16657: " sqlar_compress(data)\n"
16658: " FROM fsdir(%Q,%Q) AS disk\n"
16659: " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16660: ,
16661: "REPLACE INTO %s(name,mode,mtime,data)\n"
16662: " SELECT\n"
16663: " %s,\n"
16664: " mode,\n"
16665: " mtime,\n"
16666: " data\n"
16667: " FROM fsdir(%Q,%Q) AS disk\n"
16668: " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16669: };
16670: int i; /* For iterating through azFile[] */
16671: int rc; /* Return code */
16672: const char *zTab = 0; /* SQL table into which to insert */
16673: char *zSql;
16674: char zTemp[50];
16675: char *zExists = 0;
16676:
16677: arExecSql(pAr, "PRAGMA page_size=512");
16678: rc = arExecSql(pAr, "SAVEPOINT ar;");
16679: if( rc!=SQLITE_OK ) return rc;
16680: zTemp[0] = 0;
16681: if( pAr->bZip ){
16682: /* Initialize the zipfile virtual table, if necessary */
16683: if( pAr->zFile ){
16684: sqlite3_uint64 r;
16685: sqlite3_randomness(sizeof(r),&r);
16686: sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
16687: zTab = zTemp;
16688: zSql = sqlite3_mprintf(
16689: "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
16690: zTab, pAr->zFile
16691: );
16692: rc = arExecSql(pAr, zSql);
16693: sqlite3_free(zSql);
16694: }else{
16695: zTab = "zip";
16696: }
16697: }else{
16698: /* Initialize the table for an SQLAR */
16699: zTab = "sqlar";
16700: if( bUpdate==0 ){
16701: rc = arExecSql(pAr, zDrop);
16702: if( rc!=SQLITE_OK ) goto end_ar_transaction;
16703: }
16704: rc = arExecSql(pAr, zCreate);
16705: }
16706: if( bOnlyIfChanged ){
16707: zExists = sqlite3_mprintf(
16708: " AND NOT EXISTS("
16709: "SELECT 1 FROM %s AS mem"
16710: " WHERE mem.name=disk.name"
16711: " AND mem.mtime=disk.mtime"
16712: " AND mem.mode=disk.mode)", zTab);
16713: }else{
16714: zExists = sqlite3_mprintf("");
16715: }
16716: if( zExists==0 ) rc = SQLITE_NOMEM;
16717: for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16718: char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
16719: pAr->bVerbose ? "shell_putsnl(name)" : "name",
16720: pAr->azArg[i], pAr->zDir, zExists);
16721: rc = arExecSql(pAr, zSql2);
16722: sqlite3_free(zSql2);
16723: }
16724: end_ar_transaction:
16725: if( rc!=SQLITE_OK ){
16726: sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
16727: }else{
16728: rc = arExecSql(pAr, "RELEASE ar;");
16729: if( pAr->bZip && pAr->zFile ){
16730: zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
16731: arExecSql(pAr, zSql);
16732: sqlite3_free(zSql);
16733: }
16734: }
16735: sqlite3_free(zExists);
16736: return rc;
16737: }
16738:
16739: /*
16740: ** Implementation of ".ar" dot command.
16741: */
16742: static int arDotCommand(
16743: ShellState *pState, /* Current shell tool state */
16744: int fromCmdLine, /* True if -A command-line option, not .ar cmd */
16745: char **azArg, /* Array of arguments passed to dot command */
16746: int nArg /* Number of entries in azArg[] */
16747: ){
16748: ArCommand cmd;
16749: int rc;
16750: memset(&cmd, 0, sizeof(cmd));
16751: cmd.fromCmdLine = fromCmdLine;
16752: rc = arParseCommand(azArg, nArg, &cmd);
16753: if( rc==SQLITE_OK ){
16754: int eDbType = SHELL_OPEN_UNSPEC;
16755: cmd.p = pState;
16756: cmd.db = pState->db;
16757: if( cmd.zFile ){
16758: eDbType = deduceDatabaseType(cmd.zFile, 1);
16759: }else{
16760: eDbType = pState->openMode;
16761: }
16762: if( eDbType==SHELL_OPEN_ZIPFILE ){
16763: if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
16764: if( cmd.zFile==0 ){
16765: cmd.zSrcTable = sqlite3_mprintf("zip");
16766: }else{
16767: cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
16768: }
16769: }
16770: cmd.bZip = 1;
16771: }else if( cmd.zFile ){
16772: int flags;
16773: if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
16774: if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
16775: || cmd.eCmd==AR_CMD_UPDATE ){
16776: flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
16777: }else{
16778: flags = SQLITE_OPEN_READONLY;
16779: }
16780: cmd.db = 0;
16781: if( cmd.bDryRun ){
16782: utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
16783: eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
16784: }
16785: rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
16786: eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
16787: if( rc!=SQLITE_OK ){
16788: utf8_printf(stderr, "cannot open file: %s (%s)\n",
16789: cmd.zFile, sqlite3_errmsg(cmd.db)
16790: );
16791: goto end_ar_command;
16792: }
16793: sqlite3_fileio_init(cmd.db, 0, 0);
16794: sqlite3_sqlar_init(cmd.db, 0, 0);
16795: sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
16796: shellPutsFunc, 0, 0);
16797:
16798: }
16799: if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
16800: if( cmd.eCmd!=AR_CMD_CREATE
16801: && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
16802: ){
16803: utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
16804: rc = SQLITE_ERROR;
16805: goto end_ar_command;
16806: }
16807: cmd.zSrcTable = sqlite3_mprintf("sqlar");
16808: }
16809:
16810: switch( cmd.eCmd ){
16811: case AR_CMD_CREATE:
16812: rc = arCreateOrUpdateCommand(&cmd, 0, 0);
16813: break;
16814:
16815: case AR_CMD_EXTRACT:
16816: rc = arExtractCommand(&cmd);
16817: break;
16818:
16819: case AR_CMD_LIST:
16820: rc = arListCommand(&cmd);
16821: break;
16822:
16823: case AR_CMD_HELP:
16824: arUsage(pState->out);
16825: break;
16826:
16827: case AR_CMD_INSERT:
16828: rc = arCreateOrUpdateCommand(&cmd, 1, 0);
16829: break;
16830:
16831: default:
16832: assert( cmd.eCmd==AR_CMD_UPDATE );
16833: rc = arCreateOrUpdateCommand(&cmd, 1, 1);
16834: break;
16835: }
16836: }
16837: end_ar_command:
16838: if( cmd.db!=pState->db ){
16839: close_db(cmd.db);
16840: }
16841: sqlite3_free(cmd.zSrcTable);
16842:
16843: return rc;
16844: }
16845: /* End of the ".archive" or ".ar" command logic
16846: *******************************************************************************/
16847: #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
16848:
16849: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16850: /*
16851: ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
16852: ** Otherwise, the SQL statement or statements in zSql are executed using
16853: ** database connection db and the error code written to *pRc before
16854: ** this function returns.
16855: */
16856: static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
16857: int rc = *pRc;
16858: if( rc==SQLITE_OK ){
16859: char *zErr = 0;
16860: rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16861: if( rc!=SQLITE_OK ){
16862: raw_printf(stderr, "SQL error: %s\n", zErr);
16863: }
16864: *pRc = rc;
16865: }
16866: }
16867:
16868: /*
16869: ** Like shellExec(), except that zFmt is a printf() style format string.
16870: */
16871: static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
16872: char *z = 0;
16873: if( *pRc==SQLITE_OK ){
16874: va_list ap;
16875: va_start(ap, zFmt);
16876: z = sqlite3_vmprintf(zFmt, ap);
16877: va_end(ap);
16878: if( z==0 ){
16879: *pRc = SQLITE_NOMEM;
16880: }else{
16881: shellExec(db, pRc, z);
16882: }
16883: sqlite3_free(z);
16884: }
16885: }
16886:
16887: /*
16888: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16889: ** Otherwise, an attempt is made to allocate, zero and return a pointer
16890: ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
16891: ** to SQLITE_NOMEM and NULL returned.
16892: */
16893: static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
16894: void *pRet = 0;
16895: if( *pRc==SQLITE_OK ){
16896: pRet = sqlite3_malloc64(nByte);
16897: if( pRet==0 ){
16898: *pRc = SQLITE_NOMEM;
16899: }else{
16900: memset(pRet, 0, nByte);
16901: }
16902: }
16903: return pRet;
16904: }
16905:
16906: /*
16907: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16908: ** Otherwise, zFmt is treated as a printf() style string. The result of
16909: ** formatting it along with any trailing arguments is written into a
16910: ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
16911: ** It is the responsibility of the caller to eventually free this buffer
16912: ** using a call to sqlite3_free().
16913: **
16914: ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
16915: ** pointer returned.
16916: */
16917: static char *shellMPrintf(int *pRc, const char *zFmt, ...){
16918: char *z = 0;
16919: if( *pRc==SQLITE_OK ){
16920: va_list ap;
16921: va_start(ap, zFmt);
16922: z = sqlite3_vmprintf(zFmt, ap);
16923: va_end(ap);
16924: if( z==0 ){
16925: *pRc = SQLITE_NOMEM;
16926: }
16927: }
16928: return z;
16929: }
16930:
16931: /*
16932: ** When running the ".recover" command, each output table, and the special
16933: ** orphaned row table if it is required, is represented by an instance
16934: ** of the following struct.
16935: */
16936: typedef struct RecoverTable RecoverTable;
16937: struct RecoverTable {
16938: char *zQuoted; /* Quoted version of table name */
16939: int nCol; /* Number of columns in table */
16940: char **azlCol; /* Array of column lists */
16941: int iPk; /* Index of IPK column */
16942: };
16943:
16944: /*
16945: ** Free a RecoverTable object allocated by recoverFindTable() or
16946: ** recoverOrphanTable().
16947: */
16948: static void recoverFreeTable(RecoverTable *pTab){
16949: if( pTab ){
16950: sqlite3_free(pTab->zQuoted);
16951: if( pTab->azlCol ){
16952: int i;
16953: for(i=0; i<=pTab->nCol; i++){
16954: sqlite3_free(pTab->azlCol[i]);
16955: }
16956: sqlite3_free(pTab->azlCol);
16957: }
16958: sqlite3_free(pTab);
16959: }
16960: }
16961:
16962: /*
16963: ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
16964: ** Otherwise, it allocates and returns a RecoverTable object based on the
16965: ** final four arguments passed to this function. It is the responsibility
16966: ** of the caller to eventually free the returned object using
16967: ** recoverFreeTable().
16968: */
16969: static RecoverTable *recoverNewTable(
16970: int *pRc, /* IN/OUT: Error code */
16971: const char *zName, /* Name of table */
16972: const char *zSql, /* CREATE TABLE statement */
16973: int bIntkey,
16974: int nCol
16975: ){
16976: sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
16977: int rc = *pRc;
16978: RecoverTable *pTab = 0;
16979:
16980: pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
16981: if( rc==SQLITE_OK ){
16982: int nSqlCol = 0;
16983: int bSqlIntkey = 0;
16984: sqlite3_stmt *pStmt = 0;
16985:
16986: rc = sqlite3_open("", &dbtmp);
16987: if( rc==SQLITE_OK ){
16988: sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
16989: shellIdQuote, 0, 0);
16990: }
16991: if( rc==SQLITE_OK ){
16992: rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
16993: }
16994: if( rc==SQLITE_OK ){
16995: rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
16996: if( rc==SQLITE_ERROR ){
16997: rc = SQLITE_OK;
16998: goto finished;
16999: }
17000: }
17001: shellPreparePrintf(dbtmp, &rc, &pStmt,
17002: "SELECT count(*) FROM pragma_table_info(%Q)", zName
17003: );
17004: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17005: nSqlCol = sqlite3_column_int(pStmt, 0);
17006: }
17007: shellFinalize(&rc, pStmt);
17008:
17009: if( rc!=SQLITE_OK || nSqlCol<nCol ){
17010: goto finished;
17011: }
17012:
17013: shellPreparePrintf(dbtmp, &rc, &pStmt,
17014: "SELECT ("
17015: " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
17016: ") FROM sqlite_schema WHERE name = %Q", zName
17017: );
17018: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17019: bSqlIntkey = sqlite3_column_int(pStmt, 0);
17020: }
17021: shellFinalize(&rc, pStmt);
17022:
17023: if( bIntkey==bSqlIntkey ){
17024: int i;
17025: const char *zPk = "_rowid_";
17026: sqlite3_stmt *pPkFinder = 0;
17027:
17028: /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
17029: ** set zPk to the name of the PK column, and pTab->iPk to the index
17030: ** of the column, where columns are 0-numbered from left to right.
17031: ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
17032: ** leave zPk as "_rowid_" and pTab->iPk at -2. */
17033: pTab->iPk = -2;
17034: if( bIntkey ){
17035: shellPreparePrintf(dbtmp, &rc, &pPkFinder,
17036: "SELECT cid, name FROM pragma_table_info(%Q) "
17037: " WHERE pk=1 AND type='integer' COLLATE nocase"
17038: " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
17039: , zName, zName
17040: );
17041: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
17042: pTab->iPk = sqlite3_column_int(pPkFinder, 0);
17043: zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
17044: }
17045: }
17046:
17047: pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
17048: pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
17049: pTab->nCol = nSqlCol;
17050:
17051: if( bIntkey ){
17052: pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
17053: }else{
17054: pTab->azlCol[0] = shellMPrintf(&rc, "");
17055: }
17056: i = 1;
17057: shellPreparePrintf(dbtmp, &rc, &pStmt,
17058: "SELECT %Q || group_concat(shell_idquote(name), ', ') "
17059: " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
17060: "FROM pragma_table_info(%Q)",
17061: bIntkey ? ", " : "", pTab->iPk,
17062: bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
17063: zName
17064: );
17065: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17066: const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
17067: pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
17068: i++;
17069: }
17070: shellFinalize(&rc, pStmt);
17071:
17072: shellFinalize(&rc, pPkFinder);
17073: }
17074: }
17075:
17076: finished:
17077: sqlite3_close(dbtmp);
17078: *pRc = rc;
17079: if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
17080: recoverFreeTable(pTab);
17081: pTab = 0;
17082: }
17083: return pTab;
17084: }
17085:
17086: /*
17087: ** This function is called to search the schema recovered from the
17088: ** sqlite_schema table of the (possibly) corrupt database as part
17089: ** of a ".recover" command. Specifically, for a table with root page
17090: ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
17091: ** table must be a WITHOUT ROWID table, or if non-zero, not one of
17092: ** those.
17093: **
17094: ** If a table is found, a (RecoverTable*) object is returned. Or, if
17095: ** no such table is found, but bIntkey is false and iRoot is the
17096: ** root page of an index in the recovered schema, then (*pbNoop) is
17097: ** set to true and NULL returned. Or, if there is no such table or
17098: ** index, NULL is returned and (*pbNoop) set to 0, indicating that
17099: ** the caller should write data to the orphans table.
17100: */
17101: static RecoverTable *recoverFindTable(
17102: ShellState *pState, /* Shell state object */
17103: int *pRc, /* IN/OUT: Error code */
17104: int iRoot, /* Root page of table */
17105: int bIntkey, /* True for an intkey table */
17106: int nCol, /* Number of columns in table */
17107: int *pbNoop /* OUT: True if iRoot is root of index */
17108: ){
17109: sqlite3_stmt *pStmt = 0;
17110: RecoverTable *pRet = 0;
17111: int bNoop = 0;
17112: const char *zSql = 0;
17113: const char *zName = 0;
17114:
17115: /* Search the recovered schema for an object with root page iRoot. */
17116: shellPreparePrintf(pState->db, pRc, &pStmt,
17117: "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17118: );
17119: while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17120: const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17121: if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17122: bNoop = 1;
17123: break;
17124: }
17125: if( sqlite3_stricmp(zType, "table")==0 ){
17126: zName = (const char*)sqlite3_column_text(pStmt, 1);
17127: zSql = (const char*)sqlite3_column_text(pStmt, 2);
17128: pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17129: break;
17130: }
17131: }
17132:
17133: shellFinalize(pRc, pStmt);
17134: *pbNoop = bNoop;
17135: return pRet;
17136: }
17137:
17138: /*
17139: ** Return a RecoverTable object representing the orphans table.
17140: */
17141: static RecoverTable *recoverOrphanTable(
17142: ShellState *pState, /* Shell state object */
17143: int *pRc, /* IN/OUT: Error code */
17144: const char *zLostAndFound, /* Base name for orphans table */
17145: int nCol /* Number of user data columns */
17146: ){
17147: RecoverTable *pTab = 0;
17148: if( nCol>=0 && *pRc==SQLITE_OK ){
17149: int i;
17150:
17151: /* This block determines the name of the orphan table. The prefered
17152: ** name is zLostAndFound. But if that clashes with another name
17153: ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17154: ** and so on until a non-clashing name is found. */
17155: int iTab = 0;
17156: char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17157: sqlite3_stmt *pTest = 0;
17158: shellPrepare(pState->db, pRc,
17159: "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17160: );
17161: if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17162: while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17163: shellReset(pRc, pTest);
17164: sqlite3_free(zTab);
17165: zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17166: sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17167: }
17168: shellFinalize(pRc, pTest);
17169:
17170: pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17171: if( pTab ){
17172: pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17173: pTab->nCol = nCol;
17174: pTab->iPk = -2;
17175: if( nCol>0 ){
17176: pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17177: if( pTab->azlCol ){
17178: pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17179: for(i=nCol-1; i>=0; i--){
17180: pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17181: }
17182: }
17183: }
17184:
17185: if( *pRc!=SQLITE_OK ){
17186: recoverFreeTable(pTab);
17187: pTab = 0;
17188: }else{
17189: raw_printf(pState->out,
17190: "CREATE TABLE %s(rootpgno INTEGER, "
17191: "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17192: );
17193: for(i=0; i<nCol; i++){
17194: raw_printf(pState->out, ", c%d", i);
17195: }
17196: raw_printf(pState->out, ");\n");
17197: }
17198: }
17199: sqlite3_free(zTab);
17200: }
17201: return pTab;
17202: }
17203:
17204: /*
17205: ** This function is called to recover data from the database. A script
17206: ** to construct a new database containing all recovered data is output
17207: ** on stream pState->out.
17208: */
17209: static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
17210: int rc = SQLITE_OK;
17211: sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
17212: sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
17213: sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
17214: const char *zRecoveryDb = ""; /* Name of "recovery" database */
17215: const char *zLostAndFound = "lost_and_found";
17216: int i;
17217: int nOrphan = -1;
17218: RecoverTable *pOrphan = 0;
17219:
17220: int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
17221: int bRowids = 1; /* 0 if --no-rowids */
17222: for(i=1; i<nArg; i++){
17223: char *z = azArg[i];
17224: int n;
17225: if( z[0]=='-' && z[1]=='-' ) z++;
17226: n = strlen30(z);
17227: if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
17228: bFreelist = 0;
17229: }else
17230: if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
17231: i++;
17232: zRecoveryDb = azArg[i];
17233: }else
17234: if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
17235: i++;
17236: zLostAndFound = azArg[i];
17237: }else
17238: if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
17239: bRowids = 0;
17240: }
17241: else{
17242: utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
17243: showHelp(pState->out, azArg[0]);
17244: return 1;
17245: }
17246: }
17247:
17248: shellExecPrintf(pState->db, &rc,
17249: /* Attach an in-memory database named 'recovery'. Create an indexed
17250: ** cache of the sqlite_dbptr virtual table. */
17251: "PRAGMA writable_schema = on;"
17252: "ATTACH %Q AS recovery;"
17253: "DROP TABLE IF EXISTS recovery.dbptr;"
17254: "DROP TABLE IF EXISTS recovery.freelist;"
17255: "DROP TABLE IF EXISTS recovery.map;"
17256: "DROP TABLE IF EXISTS recovery.schema;"
17257: "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
17258: );
17259:
17260: if( bFreelist ){
17261: shellExec(pState->db, &rc,
17262: "WITH trunk(pgno) AS ("
17263: " SELECT shell_int32("
17264: " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
17265: " WHERE x>0"
17266: " UNION"
17267: " SELECT shell_int32("
17268: " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
17269: " FROM trunk WHERE x>0"
17270: "),"
17271: "freelist(data, n, freepgno) AS ("
17272: " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
17273: " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
17274: " UNION ALL"
17275: " SELECT data, n-1, shell_int32(data, 2+n) "
17276: " FROM freelist WHERE n>=0"
17277: ")"
17278: "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
17279: );
17280: }
17281:
17282: /* If this is an auto-vacuum database, add all pointer-map pages to
17283: ** the freelist table. Do this regardless of whether or not
17284: ** --freelist-corrupt was specified. */
17285: shellExec(pState->db, &rc,
17286: "WITH ptrmap(pgno) AS ("
17287: " SELECT 2 WHERE shell_int32("
17288: " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
17289: " )"
17290: " UNION ALL "
17291: " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
17292: " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
17293: ")"
17294: "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
17295: );
17296:
17297: shellExec(pState->db, &rc,
17298: "CREATE TABLE recovery.dbptr("
17299: " pgno, child, PRIMARY KEY(child, pgno)"
17300: ") WITHOUT ROWID;"
17301: "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
17302: " SELECT * FROM sqlite_dbptr"
17303: " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
17304:
17305: /* Delete any pointer to page 1. This ensures that page 1 is considered
17306: ** a root page, regardless of how corrupt the db is. */
17307: "DELETE FROM recovery.dbptr WHERE child = 1;"
17308:
17309: /* Delete all pointers to any pages that have more than one pointer
17310: ** to them. Such pages will be treated as root pages when recovering
17311: ** data. */
17312: "DELETE FROM recovery.dbptr WHERE child IN ("
17313: " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
17314: ");"
17315:
17316: /* Create the "map" table that will (eventually) contain instructions
17317: ** for dealing with each page in the db that contains one or more
17318: ** records. */
17319: "CREATE TABLE recovery.map("
17320: "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
17321: ");"
17322:
17323: /* Populate table [map]. If there are circular loops of pages in the
17324: ** database, the following adds all pages in such a loop to the map
17325: ** as individual root pages. This could be handled better. */
17326: "WITH pages(i, maxlen) AS ("
17327: " SELECT page_count, ("
17328: " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
17329: " ) FROM pragma_page_count WHERE page_count>0"
17330: " UNION ALL"
17331: " SELECT i-1, ("
17332: " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
17333: " ) FROM pages WHERE i>=2"
17334: ")"
17335: "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
17336: " SELECT i, maxlen, NULL, ("
17337: " WITH p(orig, pgno, parent) AS ("
17338: " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
17339: " UNION "
17340: " SELECT i, p.parent, "
17341: " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
17342: " )"
17343: " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
17344: ") "
17345: "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
17346: "UPDATE recovery.map AS o SET intkey = ("
17347: " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
17348: ");"
17349:
17350: /* Extract data from page 1 and any linked pages into table
17351: ** recovery.schema. With the same schema as an sqlite_schema table. */
17352: "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
17353: "INSERT INTO recovery.schema SELECT "
17354: " max(CASE WHEN field=0 THEN value ELSE NULL END),"
17355: " max(CASE WHEN field=1 THEN value ELSE NULL END),"
17356: " max(CASE WHEN field=2 THEN value ELSE NULL END),"
17357: " max(CASE WHEN field=3 THEN value ELSE NULL END),"
17358: " max(CASE WHEN field=4 THEN value ELSE NULL END)"
17359: "FROM sqlite_dbdata WHERE pgno IN ("
17360: " SELECT pgno FROM recovery.map WHERE root=1"
17361: ")"
17362: "GROUP BY pgno, cell;"
17363: "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
17364: );
17365:
17366: /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
17367: ** CREATE TABLE statements that extracted from the existing schema. */
17368: if( rc==SQLITE_OK ){
17369: sqlite3_stmt *pStmt = 0;
17370: /* ".recover" might output content in an order which causes immediate
17371: ** foreign key constraints to be violated. So disable foreign-key
17372: ** constraint enforcement to prevent problems when running the output
17373: ** script. */
17374: raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
17375: raw_printf(pState->out, "BEGIN;\n");
17376: raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
17377: shellPrepare(pState->db, &rc,
17378: "SELECT sql FROM recovery.schema "
17379: "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
17380: );
17381: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17382: const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
17383: raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
17384: &zCreateTable[12]
17385: );
17386: }
17387: shellFinalize(&rc, pStmt);
17388: }
17389:
17390: /* Figure out if an orphan table will be required. And if so, how many
17391: ** user columns it should contain */
17392: shellPrepare(pState->db, &rc,
17393: "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
17394: , &pLoop
17395: );
17396: if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17397: nOrphan = sqlite3_column_int(pLoop, 0);
17398: }
17399: shellFinalize(&rc, pLoop);
17400: pLoop = 0;
17401:
17402: shellPrepare(pState->db, &rc,
17403: "SELECT pgno FROM recovery.map WHERE root=?", &pPages
17404: );
17405:
17406: shellPrepare(pState->db, &rc,
17407: "SELECT max(field), group_concat(shell_escape_crnl(quote"
17408: "(case when (? AND field<0) then NULL else value end)"
17409: "), ', ')"
17410: ", min(field) "
17411: "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
17412: "GROUP BY cell", &pCells
17413: );
17414:
17415: /* Loop through each root page. */
17416: shellPrepare(pState->db, &rc,
17417: "SELECT root, intkey, max(maxlen) FROM recovery.map"
17418: " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
17419: " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
17420: ")", &pLoop
17421: );
17422: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17423: int iRoot = sqlite3_column_int(pLoop, 0);
17424: int bIntkey = sqlite3_column_int(pLoop, 1);
17425: int nCol = sqlite3_column_int(pLoop, 2);
17426: int bNoop = 0;
17427: RecoverTable *pTab;
17428:
17429: assert( bIntkey==0 || bIntkey==1 );
17430: pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
17431: if( bNoop || rc ) continue;
17432: if( pTab==0 ){
17433: if( pOrphan==0 ){
17434: pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17435: }
17436: pTab = pOrphan;
17437: if( pTab==0 ) break;
17438: }
17439:
17440: if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
17441: raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
17442: }
17443: sqlite3_bind_int(pPages, 1, iRoot);
17444: if( bRowids==0 && pTab->iPk<0 ){
17445: sqlite3_bind_int(pCells, 1, 1);
17446: }else{
17447: sqlite3_bind_int(pCells, 1, 0);
17448: }
17449: sqlite3_bind_int(pCells, 3, pTab->iPk);
17450:
17451: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
17452: int iPgno = sqlite3_column_int(pPages, 0);
17453: sqlite3_bind_int(pCells, 2, iPgno);
17454: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
17455: int nField = sqlite3_column_int(pCells, 0);
17456: int iMin = sqlite3_column_int(pCells, 2);
17457: const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
17458:
17459: RecoverTable *pTab2 = pTab;
17460: if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
17461: if( pOrphan==0 ){
17462: pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17463: }
17464: pTab2 = pOrphan;
17465: if( pTab2==0 ) break;
17466: }
17467:
17468: nField = nField+1;
17469: if( pTab2==pOrphan ){
17470: raw_printf(pState->out,
17471: "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
17472: pTab2->zQuoted, iRoot, iPgno, nField,
17473: iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
17474: );
17475: }else{
17476: raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
17477: pTab2->zQuoted, pTab2->azlCol[nField], zVal
17478: );
17479: }
17480: }
17481: shellReset(&rc, pCells);
17482: }
17483: shellReset(&rc, pPages);
17484: if( pTab!=pOrphan ) recoverFreeTable(pTab);
17485: }
17486: shellFinalize(&rc, pLoop);
17487: shellFinalize(&rc, pPages);
17488: shellFinalize(&rc, pCells);
17489: recoverFreeTable(pOrphan);
17490:
17491: /* The rest of the schema */
17492: if( rc==SQLITE_OK ){
17493: sqlite3_stmt *pStmt = 0;
17494: shellPrepare(pState->db, &rc,
17495: "SELECT sql, name FROM recovery.schema "
17496: "WHERE sql NOT LIKE 'create table%'", &pStmt
17497: );
17498: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17499: const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
17500: if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
17501: const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
17502: char *zPrint = shellMPrintf(&rc,
17503: "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
17504: zName, zName, zSql
17505: );
17506: raw_printf(pState->out, "%s;\n", zPrint);
17507: sqlite3_free(zPrint);
17508: }else{
17509: raw_printf(pState->out, "%s;\n", zSql);
17510: }
17511: }
17512: shellFinalize(&rc, pStmt);
17513: }
17514:
17515: if( rc==SQLITE_OK ){
17516: raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
17517: raw_printf(pState->out, "COMMIT;\n");
17518: }
17519: sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
17520: return rc;
17521: }
17522: #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17523:
17524:
17525: /*
17526: ** If an input line begins with "." then invoke this routine to
17527: ** process that line.
17528: **
17529: ** Return 1 on error, 2 to exit, and 0 otherwise.
17530: */
17531: static int do_meta_command(char *zLine, ShellState *p){
17532: int h = 1;
17533: int nArg = 0;
17534: int n, c;
17535: int rc = 0;
17536: char *azArg[52];
17537:
17538: #ifndef SQLITE_OMIT_VIRTUALTABLE
17539: if( p->expert.pExpert ){
17540: expertFinish(p, 1, 0);
17541: }
17542: #endif
17543:
17544: /* Parse the input line into tokens.
17545: */
17546: while( zLine[h] && nArg<ArraySize(azArg)-1 ){
17547: while( IsSpace(zLine[h]) ){ h++; }
17548: if( zLine[h]==0 ) break;
17549: if( zLine[h]=='\'' || zLine[h]=='"' ){
17550: int delim = zLine[h++];
17551: azArg[nArg++] = &zLine[h];
17552: while( zLine[h] && zLine[h]!=delim ){
17553: if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
17554: h++;
17555: }
17556: if( zLine[h]==delim ){
17557: zLine[h++] = 0;
17558: }
17559: if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
17560: }else{
17561: azArg[nArg++] = &zLine[h];
17562: while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
17563: if( zLine[h] ) zLine[h++] = 0;
17564: resolve_backslashes(azArg[nArg-1]);
17565: }
17566: }
17567: azArg[nArg] = 0;
17568:
17569: /* Process the input line.
17570: */
17571: if( nArg==0 ) return 0; /* no tokens, no error */
17572: n = strlen30(azArg[0]);
17573: c = azArg[0][0];
17574: clearTempFile(p);
17575:
17576: #ifndef SQLITE_OMIT_AUTHORIZATION
17577: if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
17578: if( nArg!=2 ){
17579: raw_printf(stderr, "Usage: .auth ON|OFF\n");
17580: rc = 1;
17581: goto meta_command_exit;
17582: }
17583: open_db(p, 0);
17584: if( booleanValue(azArg[1]) ){
17585: sqlite3_set_authorizer(p->db, shellAuth, p);
17586: }else{
17587: sqlite3_set_authorizer(p->db, 0, 0);
17588: }
17589: }else
17590: #endif
17591:
17592: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17593: if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
17594: open_db(p, 0);
17595: rc = arDotCommand(p, 0, azArg, nArg);
17596: }else
17597: #endif
17598:
17599: if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
17600: || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
17601: ){
17602: const char *zDestFile = 0;
17603: const char *zDb = 0;
17604: sqlite3 *pDest;
17605: sqlite3_backup *pBackup;
17606: int j;
17607: int bAsync = 0;
17608: const char *zVfs = 0;
17609: for(j=1; j<nArg; j++){
17610: const char *z = azArg[j];
17611: if( z[0]=='-' ){
17612: if( z[1]=='-' ) z++;
17613: if( strcmp(z, "-append")==0 ){
17614: zVfs = "apndvfs";
17615: }else
17616: if( strcmp(z, "-async")==0 ){
17617: bAsync = 1;
17618: }else
17619: {
17620: utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
17621: return 1;
17622: }
17623: }else if( zDestFile==0 ){
17624: zDestFile = azArg[j];
17625: }else if( zDb==0 ){
17626: zDb = zDestFile;
17627: zDestFile = azArg[j];
17628: }else{
17629: raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
17630: return 1;
17631: }
17632: }
17633: if( zDestFile==0 ){
17634: raw_printf(stderr, "missing FILENAME argument on .backup\n");
17635: return 1;
17636: }
17637: if( zDb==0 ) zDb = "main";
17638: rc = sqlite3_open_v2(zDestFile, &pDest,
17639: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
17640: if( rc!=SQLITE_OK ){
17641: utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
17642: close_db(pDest);
17643: return 1;
17644: }
17645: if( bAsync ){
17646: sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
17647: 0, 0, 0);
17648: }
17649: open_db(p, 0);
17650: pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
17651: if( pBackup==0 ){
17652: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17653: close_db(pDest);
17654: return 1;
17655: }
17656: while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
17657: sqlite3_backup_finish(pBackup);
17658: if( rc==SQLITE_DONE ){
17659: rc = 0;
17660: }else{
17661: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17662: rc = 1;
17663: }
17664: close_db(pDest);
17665: }else
17666:
17667: if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
17668: if( nArg==2 ){
17669: bail_on_error = booleanValue(azArg[1]);
17670: }else{
17671: raw_printf(stderr, "Usage: .bail on|off\n");
17672: rc = 1;
17673: }
17674: }else
17675:
17676: if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
17677: if( nArg==2 ){
17678: if( booleanValue(azArg[1]) ){
17679: setBinaryMode(p->out, 1);
17680: }else{
17681: setTextMode(p->out, 1);
17682: }
17683: }else{
17684: raw_printf(stderr, "Usage: .binary on|off\n");
17685: rc = 1;
17686: }
17687: }else
17688:
17689: if( c=='c' && strcmp(azArg[0],"cd")==0 ){
17690: if( nArg==2 ){
17691: #if defined(_WIN32) || defined(WIN32)
17692: wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
17693: rc = !SetCurrentDirectoryW(z);
17694: sqlite3_free(z);
17695: #else
17696: rc = chdir(azArg[1]);
17697: #endif
17698: if( rc ){
17699: utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
17700: rc = 1;
17701: }
17702: }else{
17703: raw_printf(stderr, "Usage: .cd DIRECTORY\n");
17704: rc = 1;
17705: }
17706: }else
17707:
17708: /* The undocumented ".breakpoint" command causes a call to the no-op
17709: ** routine named test_breakpoint().
17710: */
17711: if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
17712: test_breakpoint();
17713: }else
17714:
17715: if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
17716: if( nArg==2 ){
17717: setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
17718: }else{
17719: raw_printf(stderr, "Usage: .changes on|off\n");
17720: rc = 1;
17721: }
17722: }else
17723:
17724: /* Cancel output redirection, if it is currently set (by .testcase)
17725: ** Then read the content of the testcase-out.txt file and compare against
17726: ** azArg[1]. If there are differences, report an error and exit.
17727: */
17728: if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
17729: char *zRes = 0;
17730: output_reset(p);
17731: if( nArg!=2 ){
17732: raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
17733: rc = 2;
17734: }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
17735: raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
17736: rc = 2;
17737: }else if( testcase_glob(azArg[1],zRes)==0 ){
17738: utf8_printf(stderr,
17739: "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
17740: p->zTestcase, azArg[1], zRes);
17741: rc = 1;
17742: }else{
17743: utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
17744: p->nCheck++;
17745: }
17746: sqlite3_free(zRes);
17747: }else
17748:
17749: if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
17750: if( nArg==2 ){
17751: tryToClone(p, azArg[1]);
17752: }else{
17753: raw_printf(stderr, "Usage: .clone FILENAME\n");
17754: rc = 1;
17755: }
17756: }else
17757:
17758: if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
17759: char **azName = 0;
17760: int nName = 0;
17761: sqlite3_stmt *pStmt;
17762: int i;
17763: open_db(p, 0);
17764: rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17765: if( rc ){
17766: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17767: rc = 1;
17768: }else{
17769: while( sqlite3_step(pStmt)==SQLITE_ROW ){
17770: const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
17771: const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
17772: azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
17773: if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
17774: azName[nName*2] = strdup(zSchema);
17775: azName[nName*2+1] = strdup(zFile);
17776: nName++;
17777: }
17778: }
17779: sqlite3_finalize(pStmt);
17780: for(i=0; i<nName; i++){
17781: int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
17782: int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
17783: const char *z = azName[i*2+1];
17784: utf8_printf(p->out, "%s: %s %s%s\n",
17785: azName[i*2],
17786: z && z[0] ? z : "\"\"",
17787: bRdonly ? "r/o" : "r/w",
17788: eTxn==SQLITE_TXN_NONE ? "" :
17789: eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
17790: free(azName[i*2]);
17791: free(azName[i*2+1]);
17792: }
17793: sqlite3_free(azName);
17794: }else
17795:
17796: if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
17797: static const struct DbConfigChoices {
17798: const char *zName;
17799: int op;
17800: } aDbConfig[] = {
17801: { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
17802: { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
17803: { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
17804: { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
17805: { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
17806: { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
17807: { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
17808: { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
17809: { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
17810: { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
17811: { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
17812: { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
17813: { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
17814: { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
17815: { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
17816: { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
17817: };
17818: int ii, v;
17819: open_db(p, 0);
17820: for(ii=0; ii<ArraySize(aDbConfig); ii++){
17821: if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
17822: if( nArg>=3 ){
17823: sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
17824: }
17825: sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
17826: utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
17827: if( nArg>1 ) break;
17828: }
17829: if( nArg>1 && ii==ArraySize(aDbConfig) ){
17830: utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
17831: utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
17832: }
17833: }else
17834:
17835: if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
17836: rc = shell_dbinfo_command(p, nArg, azArg);
17837: }else
17838:
17839: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17840: if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
17841: open_db(p, 0);
17842: rc = recoverDatabaseCmd(p, nArg, azArg);
17843: }else
17844: #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17845:
17846: if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
17847: char *zLike = 0;
17848: char *zSql;
17849: int i;
17850: int savedShowHeader = p->showHeader;
17851: int savedShellFlags = p->shellFlgs;
17852: ShellClearFlag(p,
17853: SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
17854: |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
17855: for(i=1; i<nArg; i++){
17856: if( azArg[i][0]=='-' ){
17857: const char *z = azArg[i]+1;
17858: if( z[0]=='-' ) z++;
17859: if( strcmp(z,"preserve-rowids")==0 ){
17860: #ifdef SQLITE_OMIT_VIRTUALTABLE
17861: raw_printf(stderr, "The --preserve-rowids option is not compatible"
17862: " with SQLITE_OMIT_VIRTUALTABLE\n");
17863: rc = 1;
17864: sqlite3_free(zLike);
17865: goto meta_command_exit;
17866: #else
17867: ShellSetFlag(p, SHFLG_PreserveRowid);
17868: #endif
17869: }else
17870: if( strcmp(z,"newlines")==0 ){
17871: ShellSetFlag(p, SHFLG_Newlines);
17872: }else
17873: if( strcmp(z,"data-only")==0 ){
17874: ShellSetFlag(p, SHFLG_DumpDataOnly);
17875: }else
17876: if( strcmp(z,"nosys")==0 ){
17877: ShellSetFlag(p, SHFLG_DumpNoSys);
17878: }else
17879: {
17880: raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
17881: rc = 1;
17882: sqlite3_free(zLike);
17883: goto meta_command_exit;
17884: }
17885: }else if( zLike ){
17886: zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
17887: zLike, azArg[i]);
17888: }else{
17889: zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
17890: }
17891: }
17892:
17893: open_db(p, 0);
17894:
17895: if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17896: /* When playing back a "dump", the content might appear in an order
17897: ** which causes immediate foreign key constraints to be violated.
17898: ** So disable foreign-key constraint enforcement to prevent problems. */
17899: raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
17900: raw_printf(p->out, "BEGIN TRANSACTION;\n");
17901: }
17902: p->writableSchema = 0;
17903: p->showHeader = 0;
17904: /* Set writable_schema=ON since doing so forces SQLite to initialize
17905: ** as much of the schema as it can even if the sqlite_schema table is
17906: ** corrupt. */
17907: sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
17908: p->nErr = 0;
17909: if( zLike==0 ) zLike = sqlite3_mprintf("true");
17910: zSql = sqlite3_mprintf(
17911: "SELECT name, type, sql FROM sqlite_schema "
17912: "WHERE (%s) AND type=='table'"
17913: " AND sql NOT NULL"
17914: " ORDER BY tbl_name='sqlite_sequence', rowid",
17915: zLike
17916: );
17917: run_schema_dump_query(p,zSql);
17918: sqlite3_free(zSql);
17919: if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17920: zSql = sqlite3_mprintf(
17921: "SELECT sql FROM sqlite_schema "
17922: "WHERE (%s) AND sql NOT NULL"
17923: " AND type IN ('index','trigger','view')",
17924: zLike
17925: );
17926: run_table_dump_query(p, zSql);
17927: sqlite3_free(zSql);
17928: }
17929: sqlite3_free(zLike);
17930: if( p->writableSchema ){
17931: raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
17932: p->writableSchema = 0;
17933: }
17934: sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
17935: sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
17936: if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17937: raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
17938: }
17939: p->showHeader = savedShowHeader;
17940: p->shellFlgs = savedShellFlags;
17941: }else
17942:
17943: if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
17944: if( nArg==2 ){
17945: setOrClearFlag(p, SHFLG_Echo, azArg[1]);
17946: }else{
17947: raw_printf(stderr, "Usage: .echo on|off\n");
17948: rc = 1;
17949: }
17950: }else
17951:
17952: if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
17953: if( nArg==2 ){
17954: p->autoEQPtest = 0;
17955: if( p->autoEQPtrace ){
17956: if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
17957: p->autoEQPtrace = 0;
17958: }
17959: if( strcmp(azArg[1],"full")==0 ){
17960: p->autoEQP = AUTOEQP_full;
17961: }else if( strcmp(azArg[1],"trigger")==0 ){
17962: p->autoEQP = AUTOEQP_trigger;
17963: #ifdef SQLITE_DEBUG
17964: }else if( strcmp(azArg[1],"test")==0 ){
17965: p->autoEQP = AUTOEQP_on;
17966: p->autoEQPtest = 1;
17967: }else if( strcmp(azArg[1],"trace")==0 ){
17968: p->autoEQP = AUTOEQP_full;
17969: p->autoEQPtrace = 1;
17970: open_db(p, 0);
17971: sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
17972: sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
17973: #endif
17974: }else{
17975: p->autoEQP = (u8)booleanValue(azArg[1]);
17976: }
17977: }else{
17978: raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
17979: rc = 1;
17980: }
17981: }else
17982:
17983: if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
17984: if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
17985: rc = 2;
17986: }else
17987:
17988: /* The ".explain" command is automatic now. It is largely pointless. It
17989: ** retained purely for backwards compatibility */
17990: if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
17991: int val = 1;
17992: if( nArg>=2 ){
17993: if( strcmp(azArg[1],"auto")==0 ){
17994: val = 99;
17995: }else{
17996: val = booleanValue(azArg[1]);
17997: }
17998: }
17999: if( val==1 && p->mode!=MODE_Explain ){
18000: p->normalMode = p->mode;
18001: p->mode = MODE_Explain;
18002: p->autoExplain = 0;
18003: }else if( val==0 ){
18004: if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18005: p->autoExplain = 0;
18006: }else if( val==99 ){
18007: if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18008: p->autoExplain = 1;
18009: }
18010: }else
18011:
18012: #ifndef SQLITE_OMIT_VIRTUALTABLE
18013: if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
18014: open_db(p, 0);
18015: expertDotCommand(p, azArg, nArg);
18016: }else
18017: #endif
18018:
18019: if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
18020: static const struct {
18021: const char *zCtrlName; /* Name of a test-control option */
18022: int ctrlCode; /* Integer code for that option */
18023: const char *zUsage; /* Usage notes */
18024: } aCtrl[] = {
18025: { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
18026: { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
18027: { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
18028: { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
18029: { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
18030: /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
18031: { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
18032: { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
18033: { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
18034: { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
18035: /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
18036: };
18037: int filectrl = -1;
18038: int iCtrl = -1;
18039: sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
18040: int isOk = 0; /* 0: usage 1: %lld 2: no-result */
18041: int n2, i;
18042: const char *zCmd = 0;
18043: const char *zSchema = 0;
18044:
18045: open_db(p, 0);
18046: zCmd = nArg>=2 ? azArg[1] : "help";
18047:
18048: if( zCmd[0]=='-'
18049: && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
18050: && nArg>=4
18051: ){
18052: zSchema = azArg[2];
18053: for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
18054: nArg -= 2;
18055: zCmd = azArg[1];
18056: }
18057:
18058: /* The argument can optionally begin with "-" or "--" */
18059: if( zCmd[0]=='-' && zCmd[1] ){
18060: zCmd++;
18061: if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18062: }
18063:
18064: /* --help lists all file-controls */
18065: if( strcmp(zCmd,"help")==0 ){
18066: utf8_printf(p->out, "Available file-controls:\n");
18067: for(i=0; i<ArraySize(aCtrl); i++){
18068: utf8_printf(p->out, " .filectrl %s %s\n",
18069: aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18070: }
18071: rc = 1;
18072: goto meta_command_exit;
18073: }
18074:
18075: /* convert filectrl text option to value. allow any unique prefix
18076: ** of the option name, or a numerical value. */
18077: n2 = strlen30(zCmd);
18078: for(i=0; i<ArraySize(aCtrl); i++){
18079: if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18080: if( filectrl<0 ){
18081: filectrl = aCtrl[i].ctrlCode;
18082: iCtrl = i;
18083: }else{
18084: utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
18085: "Use \".filectrl --help\" for help\n", zCmd);
18086: rc = 1;
18087: goto meta_command_exit;
18088: }
18089: }
18090: }
18091: if( filectrl<0 ){
18092: utf8_printf(stderr,"Error: unknown file-control: %s\n"
18093: "Use \".filectrl --help\" for help\n", zCmd);
18094: }else{
18095: switch(filectrl){
18096: case SQLITE_FCNTL_SIZE_LIMIT: {
18097: if( nArg!=2 && nArg!=3 ) break;
18098: iRes = nArg==3 ? integerValue(azArg[2]) : -1;
18099: sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
18100: isOk = 1;
18101: break;
18102: }
18103: case SQLITE_FCNTL_LOCK_TIMEOUT:
18104: case SQLITE_FCNTL_CHUNK_SIZE: {
18105: int x;
18106: if( nArg!=3 ) break;
18107: x = (int)integerValue(azArg[2]);
18108: sqlite3_file_control(p->db, zSchema, filectrl, &x);
18109: isOk = 2;
18110: break;
18111: }
18112: case SQLITE_FCNTL_PERSIST_WAL:
18113: case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
18114: int x;
18115: if( nArg!=2 && nArg!=3 ) break;
18116: x = nArg==3 ? booleanValue(azArg[2]) : -1;
18117: sqlite3_file_control(p->db, zSchema, filectrl, &x);
18118: iRes = x;
18119: isOk = 1;
18120: break;
18121: }
18122: case SQLITE_FCNTL_DATA_VERSION:
18123: case SQLITE_FCNTL_HAS_MOVED: {
18124: int x;
18125: if( nArg!=2 ) break;
18126: sqlite3_file_control(p->db, zSchema, filectrl, &x);
18127: iRes = x;
18128: isOk = 1;
18129: break;
18130: }
18131: case SQLITE_FCNTL_TEMPFILENAME: {
18132: char *z = 0;
18133: if( nArg!=2 ) break;
18134: sqlite3_file_control(p->db, zSchema, filectrl, &z);
18135: if( z ){
18136: utf8_printf(p->out, "%s\n", z);
18137: sqlite3_free(z);
18138: }
18139: isOk = 2;
18140: break;
18141: }
18142: case SQLITE_FCNTL_RESERVE_BYTES: {
18143: int x;
18144: if( nArg>=3 ){
18145: x = atoi(azArg[2]);
18146: sqlite3_file_control(p->db, zSchema, filectrl, &x);
18147: }
18148: x = -1;
18149: sqlite3_file_control(p->db, zSchema, filectrl, &x);
18150: utf8_printf(p->out,"%d\n", x);
18151: isOk = 2;
18152: break;
18153: }
18154: }
18155: }
18156: if( isOk==0 && iCtrl>=0 ){
18157: utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18158: rc = 1;
18159: }else if( isOk==1 ){
18160: char zBuf[100];
18161: sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18162: raw_printf(p->out, "%s\n", zBuf);
18163: }
18164: }else
18165:
18166: if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18167: ShellState data;
18168: char *zErrMsg = 0;
18169: int doStats = 0;
18170: memcpy(&data, p, sizeof(data));
18171: data.showHeader = 0;
18172: data.cMode = data.mode = MODE_Semi;
18173: if( nArg==2 && optionMatch(azArg[1], "indent") ){
18174: data.cMode = data.mode = MODE_Pretty;
18175: nArg = 1;
18176: }
18177: if( nArg!=1 ){
18178: raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18179: rc = 1;
18180: goto meta_command_exit;
18181: }
18182: open_db(p, 0);
18183: rc = sqlite3_exec(p->db,
18184: "SELECT sql FROM"
18185: " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
18186: " FROM sqlite_schema UNION ALL"
18187: " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
18188: "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
18189: "ORDER BY rowid",
18190: callback, &data, &zErrMsg
18191: );
18192: if( rc==SQLITE_OK ){
18193: sqlite3_stmt *pStmt;
18194: rc = sqlite3_prepare_v2(p->db,
18195: "SELECT rowid FROM sqlite_schema"
18196: " WHERE name GLOB 'sqlite_stat[134]'",
18197: -1, &pStmt, 0);
18198: doStats = sqlite3_step(pStmt)==SQLITE_ROW;
18199: sqlite3_finalize(pStmt);
18200: }
18201: if( doStats==0 ){
18202: raw_printf(p->out, "/* No STAT tables available */\n");
18203: }else{
18204: raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18205: sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
18206: callback, &data, &zErrMsg);
18207: data.cMode = data.mode = MODE_Insert;
18208: data.zDestTable = "sqlite_stat1";
18209: shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
18210: data.zDestTable = "sqlite_stat4";
18211: shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
18212: raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18213: }
18214: }else
18215:
18216: if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
18217: if( nArg==2 ){
18218: p->showHeader = booleanValue(azArg[1]);
18219: p->shellFlgs |= SHFLG_HeaderSet;
18220: }else{
18221: raw_printf(stderr, "Usage: .headers on|off\n");
18222: rc = 1;
18223: }
18224: }else
18225:
18226: if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
18227: if( nArg>=2 ){
18228: n = showHelp(p->out, azArg[1]);
18229: if( n==0 ){
18230: utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
18231: }
18232: }else{
18233: showHelp(p->out, 0);
18234: }
18235: }else
18236:
18237: if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
18238: char *zTable = 0; /* Insert data into this table */
18239: char *zFile = 0; /* Name of file to extra content from */
18240: sqlite3_stmt *pStmt = NULL; /* A statement */
18241: int nCol; /* Number of columns in the table */
18242: int nByte; /* Number of bytes in an SQL string */
18243: int i, j; /* Loop counters */
18244: int needCommit; /* True to COMMIT or ROLLBACK at end */
18245: int nSep; /* Number of bytes in p->colSeparator[] */
18246: char *zSql; /* An SQL statement */
18247: ImportCtx sCtx; /* Reader context */
18248: char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
18249: int eVerbose = 0; /* Larger for more console output */
18250: int nSkip = 0; /* Initial lines to skip */
18251: int useOutputMode = 1; /* Use output mode to determine separators */
18252:
18253: memset(&sCtx, 0, sizeof(sCtx));
18254: if( p->mode==MODE_Ascii ){
18255: xRead = ascii_read_one_field;
18256: }else{
18257: xRead = csv_read_one_field;
18258: }
18259: for(i=1; i<nArg; i++){
18260: char *z = azArg[i];
18261: if( z[0]=='-' && z[1]=='-' ) z++;
18262: if( z[0]!='-' ){
18263: if( zFile==0 ){
18264: zFile = z;
18265: }else if( zTable==0 ){
18266: zTable = z;
18267: }else{
18268: utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
18269: showHelp(p->out, "import");
18270: rc = 1;
18271: goto meta_command_exit;
18272: }
18273: }else if( strcmp(z,"-v")==0 ){
18274: eVerbose++;
18275: }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
18276: nSkip = integerValue(azArg[++i]);
18277: }else if( strcmp(z,"-ascii")==0 ){
18278: sCtx.cColSep = SEP_Unit[0];
18279: sCtx.cRowSep = SEP_Record[0];
18280: xRead = ascii_read_one_field;
18281: useOutputMode = 0;
18282: }else if( strcmp(z,"-csv")==0 ){
18283: sCtx.cColSep = ',';
18284: sCtx.cRowSep = '\n';
18285: xRead = csv_read_one_field;
18286: useOutputMode = 0;
18287: }else{
18288: utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
18289: showHelp(p->out, "import");
18290: rc = 1;
18291: goto meta_command_exit;
18292: }
18293: }
18294: if( zTable==0 ){
18295: utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
18296: zFile==0 ? "FILE" : "TABLE");
18297: showHelp(p->out, "import");
18298: rc = 1;
18299: goto meta_command_exit;
18300: }
18301: seenInterrupt = 0;
18302: open_db(p, 0);
18303: if( useOutputMode ){
18304: /* If neither the --csv or --ascii options are specified, then set
18305: ** the column and row separator characters from the output mode. */
18306: nSep = strlen30(p->colSeparator);
18307: if( nSep==0 ){
18308: raw_printf(stderr,
18309: "Error: non-null column separator required for import\n");
18310: rc = 1;
18311: goto meta_command_exit;
18312: }
18313: if( nSep>1 ){
18314: raw_printf(stderr,
18315: "Error: multi-character column separators not allowed"
18316: " for import\n");
18317: rc = 1;
18318: goto meta_command_exit;
18319: }
18320: nSep = strlen30(p->rowSeparator);
18321: if( nSep==0 ){
18322: raw_printf(stderr,
18323: "Error: non-null row separator required for import\n");
18324: rc = 1;
18325: goto meta_command_exit;
18326: }
18327: if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
18328: /* When importing CSV (only), if the row separator is set to the
18329: ** default output row separator, change it to the default input
18330: ** row separator. This avoids having to maintain different input
18331: ** and output row separators. */
18332: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18333: nSep = strlen30(p->rowSeparator);
18334: }
18335: if( nSep>1 ){
18336: raw_printf(stderr, "Error: multi-character row separators not allowed"
18337: " for import\n");
18338: rc = 1;
18339: goto meta_command_exit;
18340: }
18341: sCtx.cColSep = p->colSeparator[0];
18342: sCtx.cRowSep = p->rowSeparator[0];
18343: }
18344: sCtx.zFile = zFile;
18345: sCtx.nLine = 1;
18346: if( sCtx.zFile[0]=='|' ){
18347: #ifdef SQLITE_OMIT_POPEN
18348: raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18349: rc = 1;
18350: goto meta_command_exit;
18351: #else
18352: sCtx.in = popen(sCtx.zFile+1, "r");
18353: sCtx.zFile = "<pipe>";
18354: sCtx.xCloser = pclose;
18355: #endif
18356: }else{
18357: sCtx.in = fopen(sCtx.zFile, "rb");
18358: sCtx.xCloser = fclose;
18359: }
18360: if( sCtx.in==0 ){
18361: utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
18362: rc = 1;
18363: goto meta_command_exit;
18364: }
18365: if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
18366: char zSep[2];
18367: zSep[1] = 0;
18368: zSep[0] = sCtx.cColSep;
18369: utf8_printf(p->out, "Column separator ");
18370: output_c_string(p->out, zSep);
18371: utf8_printf(p->out, ", row separator ");
18372: zSep[0] = sCtx.cRowSep;
18373: output_c_string(p->out, zSep);
18374: utf8_printf(p->out, "\n");
18375: }
18376: while( (nSkip--)>0 ){
18377: while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
18378: }
18379: zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
18380: if( zSql==0 ){
18381: import_cleanup(&sCtx);
18382: shell_out_of_memory();
18383: }
18384: nByte = strlen30(zSql);
18385: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18386: import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
18387: if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
18388: char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
18389: char cSep = '(';
18390: while( xRead(&sCtx) ){
18391: zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
18392: cSep = ',';
18393: if( sCtx.cTerm!=sCtx.cColSep ) break;
18394: }
18395: if( cSep=='(' ){
18396: sqlite3_free(zCreate);
18397: import_cleanup(&sCtx);
18398: utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
18399: rc = 1;
18400: goto meta_command_exit;
18401: }
18402: zCreate = sqlite3_mprintf("%z\n)", zCreate);
18403: if( eVerbose>=1 ){
18404: utf8_printf(p->out, "%s\n", zCreate);
18405: }
18406: rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
18407: sqlite3_free(zCreate);
18408: if( rc ){
18409: utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
18410: sqlite3_errmsg(p->db));
18411: import_cleanup(&sCtx);
18412: rc = 1;
18413: goto meta_command_exit;
18414: }
18415: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18416: }
18417: sqlite3_free(zSql);
18418: if( rc ){
18419: if (pStmt) sqlite3_finalize(pStmt);
18420: utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
18421: import_cleanup(&sCtx);
18422: rc = 1;
18423: goto meta_command_exit;
18424: }
18425: nCol = sqlite3_column_count(pStmt);
18426: sqlite3_finalize(pStmt);
18427: pStmt = 0;
18428: if( nCol==0 ) return 0; /* no columns, no error */
18429: zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
18430: if( zSql==0 ){
18431: import_cleanup(&sCtx);
18432: shell_out_of_memory();
18433: }
18434: sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
18435: j = strlen30(zSql);
18436: for(i=1; i<nCol; i++){
18437: zSql[j++] = ',';
18438: zSql[j++] = '?';
18439: }
18440: zSql[j++] = ')';
18441: zSql[j] = 0;
18442: if( eVerbose>=2 ){
18443: utf8_printf(p->out, "Insert using: %s\n", zSql);
18444: }
18445: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18446: sqlite3_free(zSql);
18447: if( rc ){
18448: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18449: if (pStmt) sqlite3_finalize(pStmt);
18450: import_cleanup(&sCtx);
18451: rc = 1;
18452: goto meta_command_exit;
18453: }
18454: needCommit = sqlite3_get_autocommit(p->db);
18455: if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
18456: do{
18457: int startLine = sCtx.nLine;
18458: for(i=0; i<nCol; i++){
18459: char *z = xRead(&sCtx);
18460: /*
18461: ** Did we reach end-of-file before finding any columns?
18462: ** If so, stop instead of NULL filling the remaining columns.
18463: */
18464: if( z==0 && i==0 ) break;
18465: /*
18466: ** Did we reach end-of-file OR end-of-line before finding any
18467: ** columns in ASCII mode? If so, stop instead of NULL filling
18468: ** the remaining columns.
18469: */
18470: if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
18471: sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
18472: if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
18473: utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18474: "filling the rest with NULL\n",
18475: sCtx.zFile, startLine, nCol, i+1);
18476: i += 2;
18477: while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
18478: }
18479: }
18480: if( sCtx.cTerm==sCtx.cColSep ){
18481: do{
18482: xRead(&sCtx);
18483: i++;
18484: }while( sCtx.cTerm==sCtx.cColSep );
18485: utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18486: "extras ignored\n",
18487: sCtx.zFile, startLine, nCol, i);
18488: }
18489: if( i>=nCol ){
18490: sqlite3_step(pStmt);
18491: rc = sqlite3_reset(pStmt);
18492: if( rc!=SQLITE_OK ){
18493: utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
18494: startLine, sqlite3_errmsg(p->db));
18495: sCtx.nErr++;
18496: }else{
18497: sCtx.nRow++;
18498: }
18499: }
18500: }while( sCtx.cTerm!=EOF );
18501:
18502: import_cleanup(&sCtx);
18503: sqlite3_finalize(pStmt);
18504: if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
18505: if( eVerbose>0 ){
18506: utf8_printf(p->out,
18507: "Added %d rows with %d errors using %d lines of input\n",
18508: sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
18509: }
18510: }else
18511:
18512: #ifndef SQLITE_UNTESTABLE
18513: if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
18514: char *zSql;
18515: char *zCollist = 0;
18516: sqlite3_stmt *pStmt;
18517: int tnum = 0;
18518: int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
18519: int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
18520: int i;
18521: if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
18522: utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
18523: " .imposter off\n");
18524: /* Also allowed, but not documented:
18525: **
18526: ** .imposter TABLE IMPOSTER
18527: **
18528: ** where TABLE is a WITHOUT ROWID table. In that case, the
18529: ** imposter is another WITHOUT ROWID table with the columns in
18530: ** storage order. */
18531: rc = 1;
18532: goto meta_command_exit;
18533: }
18534: open_db(p, 0);
18535: if( nArg==2 ){
18536: sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
18537: goto meta_command_exit;
18538: }
18539: zSql = sqlite3_mprintf(
18540: "SELECT rootpage, 0 FROM sqlite_schema"
18541: " WHERE name='%q' AND type='index'"
18542: "UNION ALL "
18543: "SELECT rootpage, 1 FROM sqlite_schema"
18544: " WHERE name='%q' AND type='table'"
18545: " AND sql LIKE '%%without%%rowid%%'",
18546: azArg[1], azArg[1]
18547: );
18548: sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18549: sqlite3_free(zSql);
18550: if( sqlite3_step(pStmt)==SQLITE_ROW ){
18551: tnum = sqlite3_column_int(pStmt, 0);
18552: isWO = sqlite3_column_int(pStmt, 1);
18553: }
18554: sqlite3_finalize(pStmt);
18555: zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
18556: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18557: sqlite3_free(zSql);
18558: i = 0;
18559: while( sqlite3_step(pStmt)==SQLITE_ROW ){
18560: char zLabel[20];
18561: const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
18562: i++;
18563: if( zCol==0 ){
18564: if( sqlite3_column_int(pStmt,1)==-1 ){
18565: zCol = "_ROWID_";
18566: }else{
18567: sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
18568: zCol = zLabel;
18569: }
18570: }
18571: if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
18572: lenPK = (int)strlen(zCollist);
18573: }
18574: if( zCollist==0 ){
18575: zCollist = sqlite3_mprintf("\"%w\"", zCol);
18576: }else{
18577: zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
18578: }
18579: }
18580: sqlite3_finalize(pStmt);
18581: if( i==0 || tnum==0 ){
18582: utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
18583: rc = 1;
18584: sqlite3_free(zCollist);
18585: goto meta_command_exit;
18586: }
18587: if( lenPK==0 ) lenPK = 100000;
18588: zSql = sqlite3_mprintf(
18589: "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
18590: azArg[2], zCollist, lenPK, zCollist);
18591: sqlite3_free(zCollist);
18592: rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
18593: if( rc==SQLITE_OK ){
18594: rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
18595: sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
18596: if( rc ){
18597: utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
18598: }else{
18599: utf8_printf(stdout, "%s;\n", zSql);
18600: raw_printf(stdout,
18601: "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
18602: azArg[1], isWO ? "table" : "index"
18603: );
18604: }
18605: }else{
18606: raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
18607: rc = 1;
18608: }
18609: sqlite3_free(zSql);
18610: }else
18611: #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
18612:
18613: #ifdef SQLITE_ENABLE_IOTRACE
18614: if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
18615: SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
18616: if( iotrace && iotrace!=stdout ) fclose(iotrace);
18617: iotrace = 0;
18618: if( nArg<2 ){
18619: sqlite3IoTrace = 0;
18620: }else if( strcmp(azArg[1], "-")==0 ){
18621: sqlite3IoTrace = iotracePrintf;
18622: iotrace = stdout;
18623: }else{
18624: iotrace = fopen(azArg[1], "w");
18625: if( iotrace==0 ){
18626: utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
18627: sqlite3IoTrace = 0;
18628: rc = 1;
18629: }else{
18630: sqlite3IoTrace = iotracePrintf;
18631: }
18632: }
18633: }else
18634: #endif
18635:
18636: if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
18637: static const struct {
18638: const char *zLimitName; /* Name of a limit */
18639: int limitCode; /* Integer code for that limit */
18640: } aLimit[] = {
18641: { "length", SQLITE_LIMIT_LENGTH },
18642: { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
18643: { "column", SQLITE_LIMIT_COLUMN },
18644: { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
18645: { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
18646: { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
18647: { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
18648: { "attached", SQLITE_LIMIT_ATTACHED },
18649: { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
18650: { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
18651: { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
18652: { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
18653: };
18654: int i, n2;
18655: open_db(p, 0);
18656: if( nArg==1 ){
18657: for(i=0; i<ArraySize(aLimit); i++){
18658: printf("%20s %d\n", aLimit[i].zLimitName,
18659: sqlite3_limit(p->db, aLimit[i].limitCode, -1));
18660: }
18661: }else if( nArg>3 ){
18662: raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
18663: rc = 1;
18664: goto meta_command_exit;
18665: }else{
18666: int iLimit = -1;
18667: n2 = strlen30(azArg[1]);
18668: for(i=0; i<ArraySize(aLimit); i++){
18669: if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
18670: if( iLimit<0 ){
18671: iLimit = i;
18672: }else{
18673: utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
18674: rc = 1;
18675: goto meta_command_exit;
18676: }
18677: }
18678: }
18679: if( iLimit<0 ){
18680: utf8_printf(stderr, "unknown limit: \"%s\"\n"
18681: "enter \".limits\" with no arguments for a list.\n",
18682: azArg[1]);
18683: rc = 1;
18684: goto meta_command_exit;
18685: }
18686: if( nArg==3 ){
18687: sqlite3_limit(p->db, aLimit[iLimit].limitCode,
18688: (int)integerValue(azArg[2]));
18689: }
18690: printf("%20s %d\n", aLimit[iLimit].zLimitName,
18691: sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
18692: }
18693: }else
18694:
18695: if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
18696: open_db(p, 0);
18697: lintDotCommand(p, azArg, nArg);
18698: }else
18699:
18700: #ifndef SQLITE_OMIT_LOAD_EXTENSION
18701: if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
18702: const char *zFile, *zProc;
18703: char *zErrMsg = 0;
18704: if( nArg<2 ){
18705: raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
18706: rc = 1;
18707: goto meta_command_exit;
18708: }
18709: zFile = azArg[1];
18710: zProc = nArg>=3 ? azArg[2] : 0;
18711: open_db(p, 0);
18712: rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
18713: if( rc!=SQLITE_OK ){
18714: utf8_printf(stderr, "Error: %s\n", zErrMsg);
18715: sqlite3_free(zErrMsg);
18716: rc = 1;
18717: }
18718: }else
18719: #endif
18720:
18721: if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
18722: if( nArg!=2 ){
18723: raw_printf(stderr, "Usage: .log FILENAME\n");
18724: rc = 1;
18725: }else{
18726: const char *zFile = azArg[1];
18727: output_file_close(p->pLog);
18728: p->pLog = output_file_open(zFile, 0);
18729: }
18730: }else
18731:
18732: if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
18733: const char *zMode = nArg>=2 ? azArg[1] : "";
18734: int n2 = strlen30(zMode);
18735: int c2 = zMode[0];
18736: if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
18737: p->mode = MODE_Line;
18738: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18739: }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
18740: p->mode = MODE_Column;
18741: if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
18742: p->showHeader = 1;
18743: }
18744: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18745: }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
18746: p->mode = MODE_List;
18747: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
18748: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18749: }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
18750: p->mode = MODE_Html;
18751: }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
18752: p->mode = MODE_Tcl;
18753: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
18754: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18755: }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
18756: p->mode = MODE_Csv;
18757: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18758: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18759: }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
18760: p->mode = MODE_List;
18761: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
18762: }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
18763: p->mode = MODE_Insert;
18764: set_table_name(p, nArg>=3 ? azArg[2] : "table");
18765: }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
18766: p->mode = MODE_Quote;
18767: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18768: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18769: }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
18770: p->mode = MODE_Ascii;
18771: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
18772: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
18773: }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
18774: p->mode = MODE_Markdown;
18775: }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
18776: p->mode = MODE_Table;
18777: }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
18778: p->mode = MODE_Box;
18779: }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
18780: p->mode = MODE_Json;
18781: }else if( nArg==1 ){
18782: raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
18783: }else{
18784: raw_printf(stderr, "Error: mode should be one of: "
18785: "ascii box column csv html insert json line list markdown "
18786: "quote table tabs tcl\n");
18787: rc = 1;
18788: }
18789: p->cMode = p->mode;
18790: }else
18791:
18792: if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
18793: if( nArg==2 ){
18794: sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
18795: "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
18796: }else{
18797: raw_printf(stderr, "Usage: .nullvalue STRING\n");
18798: rc = 1;
18799: }
18800: }else
18801:
18802: #ifdef SQLITE_DEBUG
18803: if( c=='o' && strcmp(azArg[0],"oom")==0 ){
18804: int i;
18805: for(i=1; i<nArg; i++){
18806: const char *z = azArg[i];
18807: if( z[0]=='-' && z[1]=='-' ) z++;
18808: if( strcmp(z,"-repeat")==0 ){
18809: if( i==nArg-1 ){
18810: raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
18811: rc = 1;
18812: }else{
18813: oomRepeat = (int)integerValue(azArg[++i]);
18814: }
18815: }else if( IsDigit(z[0]) ){
18816: oomCounter = (int)integerValue(azArg[i]);
18817: }else{
18818: raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
18819: raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
18820: rc = 1;
18821: }
18822: }
18823: if( rc==0 ){
18824: raw_printf(p->out, "oomCounter = %d\n", oomCounter);
18825: raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
18826: }
18827: }else
18828: #endif /* SQLITE_DEBUG */
18829:
18830: if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
18831: char *zNewFilename = 0; /* Name of the database file to open */
18832: int iName = 1; /* Index in azArg[] of the filename */
18833: int newFlag = 0; /* True to delete file before opening */
18834: /* Close the existing database */
18835: session_close_all(p);
18836: close_db(p->db);
18837: p->db = 0;
18838: p->zDbFilename = 0;
18839: sqlite3_free(p->zFreeOnClose);
18840: p->zFreeOnClose = 0;
18841: p->openMode = SHELL_OPEN_UNSPEC;
18842: p->openFlags = 0;
18843: p->szMax = 0;
18844: /* Check for command-line arguments */
18845: for(iName=1; iName<nArg; iName++){
18846: const char *z = azArg[iName];
18847: if( optionMatch(z,"new") ){
18848: newFlag = 1;
18849: #ifdef SQLITE_HAVE_ZLIB
18850: }else if( optionMatch(z, "zip") ){
18851: p->openMode = SHELL_OPEN_ZIPFILE;
18852: #endif
18853: }else if( optionMatch(z, "append") ){
18854: p->openMode = SHELL_OPEN_APPENDVFS;
18855: }else if( optionMatch(z, "readonly") ){
18856: p->openMode = SHELL_OPEN_READONLY;
18857: }else if( optionMatch(z, "nofollow") ){
18858: p->openFlags |= SQLITE_OPEN_NOFOLLOW;
18859: #ifdef SQLITE_ENABLE_DESERIALIZE
18860: }else if( optionMatch(z, "deserialize") ){
18861: p->openMode = SHELL_OPEN_DESERIALIZE;
18862: }else if( optionMatch(z, "hexdb") ){
18863: p->openMode = SHELL_OPEN_HEXDB;
18864: }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
18865: p->szMax = integerValue(azArg[++iName]);
18866: #endif /* SQLITE_ENABLE_DESERIALIZE */
18867: }else if( z[0]=='-' ){
18868: utf8_printf(stderr, "unknown option: %s\n", z);
18869: rc = 1;
18870: goto meta_command_exit;
18871: }else if( zNewFilename ){
18872: utf8_printf(stderr, "extra argument: \"%s\"\n", z);
18873: rc = 1;
18874: goto meta_command_exit;
18875: }else{
18876: zNewFilename = sqlite3_mprintf("%s", z);
18877: }
18878: }
18879: /* If a filename is specified, try to open it first */
18880: if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
18881: if( newFlag ) shellDeleteFile(zNewFilename);
18882: p->zDbFilename = zNewFilename;
18883: open_db(p, OPEN_DB_KEEPALIVE);
18884: if( p->db==0 ){
18885: utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
18886: sqlite3_free(zNewFilename);
18887: }else{
18888: p->zFreeOnClose = zNewFilename;
18889: }
18890: }
18891: if( p->db==0 ){
18892: /* As a fall-back open a TEMP database */
18893: p->zDbFilename = 0;
18894: open_db(p, 0);
18895: }
18896: }else
18897:
18898: if( (c=='o'
18899: && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
18900: || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
18901: ){
18902: char *zFile = 0;
18903: int bTxtMode = 0;
18904: int i;
18905: int eMode = 0;
18906: int bBOM = 0;
18907: int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
18908:
18909: if( c=='e' ){
18910: eMode = 'x';
18911: bOnce = 2;
18912: }else if( strncmp(azArg[0],"once",n)==0 ){
18913: bOnce = 1;
18914: }
18915: for(i=1; i<nArg; i++){
18916: char *z = azArg[i];
18917: if( z[0]=='-' ){
18918: if( z[1]=='-' ) z++;
18919: if( strcmp(z,"-bom")==0 ){
18920: bBOM = 1;
18921: }else if( c!='e' && strcmp(z,"-x")==0 ){
18922: eMode = 'x'; /* spreadsheet */
18923: }else if( c!='e' && strcmp(z,"-e")==0 ){
18924: eMode = 'e'; /* text editor */
18925: }else{
18926: utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
18927: azArg[i]);
18928: showHelp(p->out, azArg[0]);
18929: rc = 1;
18930: goto meta_command_exit;
18931: }
18932: }else if( zFile==0 && eMode!='e' && eMode!='x' ){
18933: zFile = sqlite3_mprintf("%s", z);
18934: if( zFile[0]=='|' ){
18935: while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
18936: break;
18937: }
18938: }else{
18939: utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
18940: azArg[i]);
18941: showHelp(p->out, azArg[0]);
18942: rc = 1;
18943: sqlite3_free(zFile);
18944: goto meta_command_exit;
18945: }
18946: }
18947: if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
18948: if( bOnce ){
18949: p->outCount = 2;
18950: }else{
18951: p->outCount = 0;
18952: }
18953: output_reset(p);
18954: #ifndef SQLITE_NOHAVE_SYSTEM
18955: if( eMode=='e' || eMode=='x' ){
18956: p->doXdgOpen = 1;
18957: outputModePush(p);
18958: if( eMode=='x' ){
18959: /* spreadsheet mode. Output as CSV. */
18960: newTempFile(p, "csv");
18961: ShellClearFlag(p, SHFLG_Echo);
18962: p->mode = MODE_Csv;
18963: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18964: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18965: }else{
18966: /* text editor mode */
18967: newTempFile(p, "txt");
18968: bTxtMode = 1;
18969: }
18970: sqlite3_free(zFile);
18971: zFile = sqlite3_mprintf("%s", p->zTempFile);
18972: }
18973: #endif /* SQLITE_NOHAVE_SYSTEM */
18974: if( zFile[0]=='|' ){
18975: #ifdef SQLITE_OMIT_POPEN
18976: raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18977: rc = 1;
18978: p->out = stdout;
18979: #else
18980: p->out = popen(zFile + 1, "w");
18981: if( p->out==0 ){
18982: utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
18983: p->out = stdout;
18984: rc = 1;
18985: }else{
18986: if( bBOM ) fprintf(p->out,"\357\273\277");
18987: sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18988: }
18989: #endif
18990: }else{
18991: p->out = output_file_open(zFile, bTxtMode);
18992: if( p->out==0 ){
18993: if( strcmp(zFile,"off")!=0 ){
18994: utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
18995: }
18996: p->out = stdout;
18997: rc = 1;
18998: } else {
18999: if( bBOM ) fprintf(p->out,"\357\273\277");
19000: sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19001: }
19002: }
19003: sqlite3_free(zFile);
19004: }else
19005:
19006: if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
19007: open_db(p,0);
19008: if( nArg<=1 ) goto parameter_syntax_error;
19009:
19010: /* .parameter clear
19011: ** Clear all bind parameters by dropping the TEMP table that holds them.
19012: */
19013: if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
19014: sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
19015: 0, 0, 0);
19016: }else
19017:
19018: /* .parameter list
19019: ** List all bind parameters.
19020: */
19021: if( nArg==2 && strcmp(azArg[1],"list")==0 ){
19022: sqlite3_stmt *pStmt = 0;
19023: int rx;
19024: int len = 0;
19025: rx = sqlite3_prepare_v2(p->db,
19026: "SELECT max(length(key)) "
19027: "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19028: if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19029: len = sqlite3_column_int(pStmt, 0);
19030: if( len>40 ) len = 40;
19031: }
19032: sqlite3_finalize(pStmt);
19033: pStmt = 0;
19034: if( len ){
19035: rx = sqlite3_prepare_v2(p->db,
19036: "SELECT key, quote(value) "
19037: "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19038: while( sqlite3_step(pStmt)==SQLITE_ROW ){
19039: utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
19040: sqlite3_column_text(pStmt,1));
19041: }
19042: sqlite3_finalize(pStmt);
19043: }
19044: }else
19045:
19046: /* .parameter init
19047: ** Make sure the TEMP table used to hold bind parameters exists.
19048: ** Create it if necessary.
19049: */
19050: if( nArg==2 && strcmp(azArg[1],"init")==0 ){
19051: bind_table_init(p);
19052: }else
19053:
19054: /* .parameter set NAME VALUE
19055: ** Set or reset a bind parameter. NAME should be the full parameter
19056: ** name exactly as it appears in the query. (ex: $abc, @def). The
19057: ** VALUE can be in either SQL literal notation, or if not it will be
19058: ** understood to be a text string.
19059: */
19060: if( nArg==4 && strcmp(azArg[1],"set")==0 ){
19061: int rx;
19062: char *zSql;
19063: sqlite3_stmt *pStmt;
19064: const char *zKey = azArg[2];
19065: const char *zValue = azArg[3];
19066: bind_table_init(p);
19067: zSql = sqlite3_mprintf(
19068: "REPLACE INTO temp.sqlite_parameters(key,value)"
19069: "VALUES(%Q,%s);", zKey, zValue);
19070: if( zSql==0 ) shell_out_of_memory();
19071: pStmt = 0;
19072: rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19073: sqlite3_free(zSql);
19074: if( rx!=SQLITE_OK ){
19075: sqlite3_finalize(pStmt);
19076: pStmt = 0;
19077: zSql = sqlite3_mprintf(
19078: "REPLACE INTO temp.sqlite_parameters(key,value)"
19079: "VALUES(%Q,%Q);", zKey, zValue);
19080: if( zSql==0 ) shell_out_of_memory();
19081: rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19082: sqlite3_free(zSql);
19083: if( rx!=SQLITE_OK ){
19084: utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
19085: sqlite3_finalize(pStmt);
19086: pStmt = 0;
19087: rc = 1;
19088: }
19089: }
19090: sqlite3_step(pStmt);
19091: sqlite3_finalize(pStmt);
19092: }else
19093:
19094: /* .parameter unset NAME
19095: ** Remove the NAME binding from the parameter binding table, if it
19096: ** exists.
19097: */
19098: if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
19099: char *zSql = sqlite3_mprintf(
19100: "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
19101: if( zSql==0 ) shell_out_of_memory();
19102: sqlite3_exec(p->db, zSql, 0, 0, 0);
19103: sqlite3_free(zSql);
19104: }else
19105: /* If no command name matches, show a syntax error */
19106: parameter_syntax_error:
19107: showHelp(p->out, "parameter");
19108: }else
19109:
19110: if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
19111: int i;
19112: for(i=1; i<nArg; i++){
19113: if( i>1 ) raw_printf(p->out, " ");
19114: utf8_printf(p->out, "%s", azArg[i]);
19115: }
19116: raw_printf(p->out, "\n");
19117: }else
19118:
19119: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19120: if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
19121: int i;
19122: int nn = 0;
19123: p->flgProgress = 0;
19124: p->mxProgress = 0;
19125: p->nProgress = 0;
19126: for(i=1; i<nArg; i++){
19127: const char *z = azArg[i];
19128: if( z[0]=='-' ){
19129: z++;
19130: if( z[0]=='-' ) z++;
19131: if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19132: p->flgProgress |= SHELL_PROGRESS_QUIET;
19133: continue;
19134: }
19135: if( strcmp(z,"reset")==0 ){
19136: p->flgProgress |= SHELL_PROGRESS_RESET;
19137: continue;
19138: }
19139: if( strcmp(z,"once")==0 ){
19140: p->flgProgress |= SHELL_PROGRESS_ONCE;
19141: continue;
19142: }
19143: if( strcmp(z,"limit")==0 ){
19144: if( i+1>=nArg ){
19145: utf8_printf(stderr, "Error: missing argument on --limit\n");
19146: rc = 1;
19147: goto meta_command_exit;
19148: }else{
19149: p->mxProgress = (int)integerValue(azArg[++i]);
19150: }
19151: continue;
19152: }
19153: utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19154: rc = 1;
19155: goto meta_command_exit;
19156: }else{
19157: nn = (int)integerValue(z);
19158: }
19159: }
19160: open_db(p, 0);
19161: sqlite3_progress_handler(p->db, nn, progress_handler, p);
19162: }else
19163: #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19164:
19165: if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19166: if( nArg >= 2) {
19167: strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19168: }
19169: if( nArg >= 3) {
19170: strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19171: }
19172: }else
19173:
19174: if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19175: rc = 2;
19176: }else
19177:
19178: if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19179: FILE *inSaved = p->in;
19180: int savedLineno = p->lineno;
19181: if( nArg!=2 ){
19182: raw_printf(stderr, "Usage: .read FILE\n");
19183: rc = 1;
19184: goto meta_command_exit;
19185: }
19186: if( azArg[1][0]=='|' ){
19187: #ifdef SQLITE_OMIT_POPEN
19188: raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19189: rc = 1;
19190: p->out = stdout;
19191: #else
19192: p->in = popen(azArg[1]+1, "r");
19193: if( p->in==0 ){
19194: utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19195: rc = 1;
19196: }else{
19197: rc = process_input(p);
19198: pclose(p->in);
19199: }
19200: #endif
19201: }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
19202: utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
19203: rc = 1;
19204: }else{
19205: rc = process_input(p);
19206: fclose(p->in);
19207: }
19208: p->in = inSaved;
19209: p->lineno = savedLineno;
19210: }else
19211:
19212: if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
19213: const char *zSrcFile;
19214: const char *zDb;
19215: sqlite3 *pSrc;
19216: sqlite3_backup *pBackup;
19217: int nTimeout = 0;
19218:
19219: if( nArg==2 ){
19220: zSrcFile = azArg[1];
19221: zDb = "main";
19222: }else if( nArg==3 ){
19223: zSrcFile = azArg[2];
19224: zDb = azArg[1];
19225: }else{
19226: raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
19227: rc = 1;
19228: goto meta_command_exit;
19229: }
19230: rc = sqlite3_open(zSrcFile, &pSrc);
19231: if( rc!=SQLITE_OK ){
19232: utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
19233: close_db(pSrc);
19234: return 1;
19235: }
19236: open_db(p, 0);
19237: pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
19238: if( pBackup==0 ){
19239: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19240: close_db(pSrc);
19241: return 1;
19242: }
19243: while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
19244: || rc==SQLITE_BUSY ){
19245: if( rc==SQLITE_BUSY ){
19246: if( nTimeout++ >= 3 ) break;
19247: sqlite3_sleep(100);
19248: }
19249: }
19250: sqlite3_backup_finish(pBackup);
19251: if( rc==SQLITE_DONE ){
19252: rc = 0;
19253: }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
19254: raw_printf(stderr, "Error: source database is busy\n");
19255: rc = 1;
19256: }else{
19257: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19258: rc = 1;
19259: }
19260: close_db(pSrc);
19261: }else
19262:
19263: if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
19264: if( nArg==2 ){
19265: p->scanstatsOn = (u8)booleanValue(azArg[1]);
19266: #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19267: raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
19268: #endif
19269: }else{
19270: raw_printf(stderr, "Usage: .scanstats on|off\n");
19271: rc = 1;
19272: }
19273: }else
19274:
19275: if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
19276: ShellText sSelect;
19277: ShellState data;
19278: char *zErrMsg = 0;
19279: const char *zDiv = "(";
19280: const char *zName = 0;
19281: int iSchema = 0;
19282: int bDebug = 0;
19283: int bNoSystemTabs = 0;
19284: int ii;
19285:
19286: open_db(p, 0);
19287: memcpy(&data, p, sizeof(data));
19288: data.showHeader = 0;
19289: data.cMode = data.mode = MODE_Semi;
19290: initText(&sSelect);
19291: for(ii=1; ii<nArg; ii++){
19292: if( optionMatch(azArg[ii],"indent") ){
19293: data.cMode = data.mode = MODE_Pretty;
19294: }else if( optionMatch(azArg[ii],"debug") ){
19295: bDebug = 1;
19296: }else if( optionMatch(azArg[ii],"nosys") ){
19297: bNoSystemTabs = 1;
19298: }else if( azArg[ii][0]=='-' ){
19299: utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
19300: rc = 1;
19301: goto meta_command_exit;
19302: }else if( zName==0 ){
19303: zName = azArg[ii];
19304: }else{
19305: raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
19306: rc = 1;
19307: goto meta_command_exit;
19308: }
19309: }
19310: if( zName!=0 ){
19311: int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
19312: || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
19313: || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
19314: || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
19315: if( isSchema ){
19316: char *new_argv[2], *new_colv[2];
19317: new_argv[0] = sqlite3_mprintf(
19318: "CREATE TABLE %s (\n"
19319: " type text,\n"
19320: " name text,\n"
19321: " tbl_name text,\n"
19322: " rootpage integer,\n"
19323: " sql text\n"
19324: ")", zName);
19325: new_argv[1] = 0;
19326: new_colv[0] = "sql";
19327: new_colv[1] = 0;
19328: callback(&data, 1, new_argv, new_colv);
19329: sqlite3_free(new_argv[0]);
19330: }
19331: }
19332: if( zDiv ){
19333: sqlite3_stmt *pStmt = 0;
19334: rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
19335: -1, &pStmt, 0);
19336: if( rc ){
19337: utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19338: sqlite3_finalize(pStmt);
19339: rc = 1;
19340: goto meta_command_exit;
19341: }
19342: appendText(&sSelect, "SELECT sql FROM", 0);
19343: iSchema = 0;
19344: while( sqlite3_step(pStmt)==SQLITE_ROW ){
19345: const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
19346: char zScNum[30];
19347: sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
19348: appendText(&sSelect, zDiv, 0);
19349: zDiv = " UNION ALL ";
19350: appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
19351: if( sqlite3_stricmp(zDb, "main")!=0 ){
19352: appendText(&sSelect, zDb, '\'');
19353: }else{
19354: appendText(&sSelect, "NULL", 0);
19355: }
19356: appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
19357: appendText(&sSelect, zScNum, 0);
19358: appendText(&sSelect, " AS snum, ", 0);
19359: appendText(&sSelect, zDb, '\'');
19360: appendText(&sSelect, " AS sname FROM ", 0);
19361: appendText(&sSelect, zDb, quoteChar(zDb));
19362: appendText(&sSelect, ".sqlite_schema", 0);
19363: }
19364: sqlite3_finalize(pStmt);
19365: #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
19366: if( zName ){
19367: appendText(&sSelect,
19368: " UNION ALL SELECT shell_module_schema(name),"
19369: " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
19370: 0);
19371: }
19372: #endif
19373: appendText(&sSelect, ") WHERE ", 0);
19374: if( zName ){
19375: char *zQarg = sqlite3_mprintf("%Q", zName);
19376: int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
19377: strchr(zName, '[') != 0;
19378: if( strchr(zName, '.') ){
19379: appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
19380: }else{
19381: appendText(&sSelect, "lower(tbl_name)", 0);
19382: }
19383: appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
19384: appendText(&sSelect, zQarg, 0);
19385: if( !bGlob ){
19386: appendText(&sSelect, " ESCAPE '\\' ", 0);
19387: }
19388: appendText(&sSelect, " AND ", 0);
19389: sqlite3_free(zQarg);
19390: }
19391: if( bNoSystemTabs ){
19392: appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
19393: }
19394: appendText(&sSelect, "sql IS NOT NULL"
19395: " ORDER BY snum, rowid", 0);
19396: if( bDebug ){
19397: utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
19398: }else{
19399: rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
19400: }
19401: freeText(&sSelect);
19402: }
19403: if( zErrMsg ){
19404: utf8_printf(stderr,"Error: %s\n", zErrMsg);
19405: sqlite3_free(zErrMsg);
19406: rc = 1;
19407: }else if( rc != SQLITE_OK ){
19408: raw_printf(stderr,"Error: querying schema information\n");
19409: rc = 1;
19410: }else{
19411: rc = 0;
19412: }
19413: }else
19414:
19415: if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
19416: unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
19417: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
19418: }else
19419:
19420: #if defined(SQLITE_ENABLE_SESSION)
19421: if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
19422: OpenSession *pSession = &p->aSession[0];
19423: char **azCmd = &azArg[1];
19424: int iSes = 0;
19425: int nCmd = nArg - 1;
19426: int i;
19427: if( nArg<=1 ) goto session_syntax_error;
19428: open_db(p, 0);
19429: if( nArg>=3 ){
19430: for(iSes=0; iSes<p->nSession; iSes++){
19431: if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
19432: }
19433: if( iSes<p->nSession ){
19434: pSession = &p->aSession[iSes];
19435: azCmd++;
19436: nCmd--;
19437: }else{
19438: pSession = &p->aSession[0];
19439: iSes = 0;
19440: }
19441: }
19442:
19443: /* .session attach TABLE
19444: ** Invoke the sqlite3session_attach() interface to attach a particular
19445: ** table so that it is never filtered.
19446: */
19447: if( strcmp(azCmd[0],"attach")==0 ){
19448: if( nCmd!=2 ) goto session_syntax_error;
19449: if( pSession->p==0 ){
19450: session_not_open:
19451: raw_printf(stderr, "ERROR: No sessions are open\n");
19452: }else{
19453: rc = sqlite3session_attach(pSession->p, azCmd[1]);
19454: if( rc ){
19455: raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
19456: rc = 0;
19457: }
19458: }
19459: }else
19460:
19461: /* .session changeset FILE
19462: ** .session patchset FILE
19463: ** Write a changeset or patchset into a file. The file is overwritten.
19464: */
19465: if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
19466: FILE *out = 0;
19467: if( nCmd!=2 ) goto session_syntax_error;
19468: if( pSession->p==0 ) goto session_not_open;
19469: out = fopen(azCmd[1], "wb");
19470: if( out==0 ){
19471: utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
19472: azCmd[1]);
19473: }else{
19474: int szChng;
19475: void *pChng;
19476: if( azCmd[0][0]=='c' ){
19477: rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
19478: }else{
19479: rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
19480: }
19481: if( rc ){
19482: printf("Error: error code %d\n", rc);
19483: rc = 0;
19484: }
19485: if( pChng
19486: && fwrite(pChng, szChng, 1, out)!=1 ){
19487: raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
19488: szChng);
19489: }
19490: sqlite3_free(pChng);
19491: fclose(out);
19492: }
19493: }else
19494:
19495: /* .session close
19496: ** Close the identified session
19497: */
19498: if( strcmp(azCmd[0], "close")==0 ){
19499: if( nCmd!=1 ) goto session_syntax_error;
19500: if( p->nSession ){
19501: session_close(pSession);
19502: p->aSession[iSes] = p->aSession[--p->nSession];
19503: }
19504: }else
19505:
19506: /* .session enable ?BOOLEAN?
19507: ** Query or set the enable flag
19508: */
19509: if( strcmp(azCmd[0], "enable")==0 ){
19510: int ii;
19511: if( nCmd>2 ) goto session_syntax_error;
19512: ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19513: if( p->nSession ){
19514: ii = sqlite3session_enable(pSession->p, ii);
19515: utf8_printf(p->out, "session %s enable flag = %d\n",
19516: pSession->zName, ii);
19517: }
19518: }else
19519:
19520: /* .session filter GLOB ....
19521: ** Set a list of GLOB patterns of table names to be excluded.
19522: */
19523: if( strcmp(azCmd[0], "filter")==0 ){
19524: int ii, nByte;
19525: if( nCmd<2 ) goto session_syntax_error;
19526: if( p->nSession ){
19527: for(ii=0; ii<pSession->nFilter; ii++){
19528: sqlite3_free(pSession->azFilter[ii]);
19529: }
19530: sqlite3_free(pSession->azFilter);
19531: nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
19532: pSession->azFilter = sqlite3_malloc( nByte );
19533: if( pSession->azFilter==0 ){
19534: raw_printf(stderr, "Error: out or memory\n");
19535: exit(1);
19536: }
19537: for(ii=1; ii<nCmd; ii++){
19538: pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
19539: }
19540: pSession->nFilter = ii-1;
19541: }
19542: }else
19543:
19544: /* .session indirect ?BOOLEAN?
19545: ** Query or set the indirect flag
19546: */
19547: if( strcmp(azCmd[0], "indirect")==0 ){
19548: int ii;
19549: if( nCmd>2 ) goto session_syntax_error;
19550: ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19551: if( p->nSession ){
19552: ii = sqlite3session_indirect(pSession->p, ii);
19553: utf8_printf(p->out, "session %s indirect flag = %d\n",
19554: pSession->zName, ii);
19555: }
19556: }else
19557:
19558: /* .session isempty
19559: ** Determine if the session is empty
19560: */
19561: if( strcmp(azCmd[0], "isempty")==0 ){
19562: int ii;
19563: if( nCmd!=1 ) goto session_syntax_error;
19564: if( p->nSession ){
19565: ii = sqlite3session_isempty(pSession->p);
19566: utf8_printf(p->out, "session %s isempty flag = %d\n",
19567: pSession->zName, ii);
19568: }
19569: }else
19570:
19571: /* .session list
19572: ** List all currently open sessions
19573: */
19574: if( strcmp(azCmd[0],"list")==0 ){
19575: for(i=0; i<p->nSession; i++){
19576: utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
19577: }
19578: }else
19579:
19580: /* .session open DB NAME
19581: ** Open a new session called NAME on the attached database DB.
19582: ** DB is normally "main".
19583: */
19584: if( strcmp(azCmd[0],"open")==0 ){
19585: char *zName;
19586: if( nCmd!=3 ) goto session_syntax_error;
19587: zName = azCmd[2];
19588: if( zName[0]==0 ) goto session_syntax_error;
19589: for(i=0; i<p->nSession; i++){
19590: if( strcmp(p->aSession[i].zName,zName)==0 ){
19591: utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
19592: goto meta_command_exit;
19593: }
19594: }
19595: if( p->nSession>=ArraySize(p->aSession) ){
19596: raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
19597: goto meta_command_exit;
19598: }
19599: pSession = &p->aSession[p->nSession];
19600: rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
19601: if( rc ){
19602: raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
19603: rc = 0;
19604: goto meta_command_exit;
19605: }
19606: pSession->nFilter = 0;
19607: sqlite3session_table_filter(pSession->p, session_filter, pSession);
19608: p->nSession++;
19609: pSession->zName = sqlite3_mprintf("%s", zName);
19610: }else
19611: /* If no command name matches, show a syntax error */
19612: session_syntax_error:
19613: showHelp(p->out, "session");
19614: }else
19615: #endif
19616:
19617: #ifdef SQLITE_DEBUG
19618: /* Undocumented commands for internal testing. Subject to change
19619: ** without notice. */
19620: if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
19621: if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
19622: int i, v;
19623: for(i=1; i<nArg; i++){
19624: v = booleanValue(azArg[i]);
19625: utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
19626: }
19627: }
19628: if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
19629: int i; sqlite3_int64 v;
19630: for(i=1; i<nArg; i++){
19631: char zBuf[200];
19632: v = integerValue(azArg[i]);
19633: sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
19634: utf8_printf(p->out, "%s", zBuf);
19635: }
19636: }
19637: }else
19638: #endif
19639:
19640: if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
19641: int bIsInit = 0; /* True to initialize the SELFTEST table */
19642: int bVerbose = 0; /* Verbose output */
19643: int bSelftestExists; /* True if SELFTEST already exists */
19644: int i, k; /* Loop counters */
19645: int nTest = 0; /* Number of tests runs */
19646: int nErr = 0; /* Number of errors seen */
19647: ShellText str; /* Answer for a query */
19648: sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
19649:
19650: open_db(p,0);
19651: for(i=1; i<nArg; i++){
19652: const char *z = azArg[i];
19653: if( z[0]=='-' && z[1]=='-' ) z++;
19654: if( strcmp(z,"-init")==0 ){
19655: bIsInit = 1;
19656: }else
19657: if( strcmp(z,"-v")==0 ){
19658: bVerbose++;
19659: }else
19660: {
19661: utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19662: azArg[i], azArg[0]);
19663: raw_printf(stderr, "Should be one of: --init -v\n");
19664: rc = 1;
19665: goto meta_command_exit;
19666: }
19667: }
19668: if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
19669: != SQLITE_OK ){
19670: bSelftestExists = 0;
19671: }else{
19672: bSelftestExists = 1;
19673: }
19674: if( bIsInit ){
19675: createSelftestTable(p);
19676: bSelftestExists = 1;
19677: }
19678: initText(&str);
19679: appendText(&str, "x", 0);
19680: for(k=bSelftestExists; k>=0; k--){
19681: if( k==1 ){
19682: rc = sqlite3_prepare_v2(p->db,
19683: "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
19684: -1, &pStmt, 0);
19685: }else{
19686: rc = sqlite3_prepare_v2(p->db,
19687: "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
19688: " (1,'run','PRAGMA integrity_check','ok')",
19689: -1, &pStmt, 0);
19690: }
19691: if( rc ){
19692: raw_printf(stderr, "Error querying the selftest table\n");
19693: rc = 1;
19694: sqlite3_finalize(pStmt);
19695: goto meta_command_exit;
19696: }
19697: for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
19698: int tno = sqlite3_column_int(pStmt, 0);
19699: const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
19700: const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
19701: const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
19702:
19703: k = 0;
19704: if( bVerbose>0 ){
19705: char *zQuote = sqlite3_mprintf("%q", zSql);
19706: printf("%d: %s %s\n", tno, zOp, zSql);
19707: sqlite3_free(zQuote);
19708: }
19709: if( strcmp(zOp,"memo")==0 ){
19710: utf8_printf(p->out, "%s\n", zSql);
19711: }else
19712: if( strcmp(zOp,"run")==0 ){
19713: char *zErrMsg = 0;
19714: str.n = 0;
19715: str.z[0] = 0;
19716: rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
19717: nTest++;
19718: if( bVerbose ){
19719: utf8_printf(p->out, "Result: %s\n", str.z);
19720: }
19721: if( rc || zErrMsg ){
19722: nErr++;
19723: rc = 1;
19724: utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
19725: sqlite3_free(zErrMsg);
19726: }else if( strcmp(zAns,str.z)!=0 ){
19727: nErr++;
19728: rc = 1;
19729: utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
19730: utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
19731: }
19732: }else
19733: {
19734: utf8_printf(stderr,
19735: "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
19736: rc = 1;
19737: break;
19738: }
19739: } /* End loop over rows of content from SELFTEST */
19740: sqlite3_finalize(pStmt);
19741: } /* End loop over k */
19742: freeText(&str);
19743: utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
19744: }else
19745:
19746: if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
19747: if( nArg<2 || nArg>3 ){
19748: raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
19749: rc = 1;
19750: }
19751: if( nArg>=2 ){
19752: sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
19753: "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
19754: }
19755: if( nArg>=3 ){
19756: sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
19757: "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
19758: }
19759: }else
19760:
19761: if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
19762: const char *zLike = 0; /* Which table to checksum. 0 means everything */
19763: int i; /* Loop counter */
19764: int bSchema = 0; /* Also hash the schema */
19765: int bSeparate = 0; /* Hash each table separately */
19766: int iSize = 224; /* Hash algorithm to use */
19767: int bDebug = 0; /* Only show the query that would have run */
19768: sqlite3_stmt *pStmt; /* For querying tables names */
19769: char *zSql; /* SQL to be run */
19770: char *zSep; /* Separator */
19771: ShellText sSql; /* Complete SQL for the query to run the hash */
19772: ShellText sQuery; /* Set of queries used to read all content */
19773: open_db(p, 0);
19774: for(i=1; i<nArg; i++){
19775: const char *z = azArg[i];
19776: if( z[0]=='-' ){
19777: z++;
19778: if( z[0]=='-' ) z++;
19779: if( strcmp(z,"schema")==0 ){
19780: bSchema = 1;
19781: }else
19782: if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
19783: || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
19784: ){
19785: iSize = atoi(&z[5]);
19786: }else
19787: if( strcmp(z,"debug")==0 ){
19788: bDebug = 1;
19789: }else
19790: {
19791: utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19792: azArg[i], azArg[0]);
19793: showHelp(p->out, azArg[0]);
19794: rc = 1;
19795: goto meta_command_exit;
19796: }
19797: }else if( zLike ){
19798: raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
19799: rc = 1;
19800: goto meta_command_exit;
19801: }else{
19802: zLike = z;
19803: bSeparate = 1;
19804: if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
19805: }
19806: }
19807: if( bSchema ){
19808: zSql = "SELECT lower(name) FROM sqlite_schema"
19809: " WHERE type='table' AND coalesce(rootpage,0)>1"
19810: " UNION ALL SELECT 'sqlite_schema'"
19811: " ORDER BY 1 collate nocase";
19812: }else{
19813: zSql = "SELECT lower(name) FROM sqlite_schema"
19814: " WHERE type='table' AND coalesce(rootpage,0)>1"
19815: " AND name NOT LIKE 'sqlite_%'"
19816: " ORDER BY 1 collate nocase";
19817: }
19818: sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19819: initText(&sQuery);
19820: initText(&sSql);
19821: appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
19822: zSep = "VALUES(";
19823: while( SQLITE_ROW==sqlite3_step(pStmt) ){
19824: const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
19825: if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
19826: if( strncmp(zTab, "sqlite_",7)!=0 ){
19827: appendText(&sQuery,"SELECT * FROM ", 0);
19828: appendText(&sQuery,zTab,'"');
19829: appendText(&sQuery," NOT INDEXED;", 0);
19830: }else if( strcmp(zTab, "sqlite_schema")==0 ){
19831: appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
19832: " ORDER BY name;", 0);
19833: }else if( strcmp(zTab, "sqlite_sequence")==0 ){
19834: appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
19835: " ORDER BY name;", 0);
19836: }else if( strcmp(zTab, "sqlite_stat1")==0 ){
19837: appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
19838: " ORDER BY tbl,idx;", 0);
19839: }else if( strcmp(zTab, "sqlite_stat4")==0 ){
19840: appendText(&sQuery, "SELECT * FROM ", 0);
19841: appendText(&sQuery, zTab, 0);
19842: appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
19843: }
19844: appendText(&sSql, zSep, 0);
19845: appendText(&sSql, sQuery.z, '\'');
19846: sQuery.n = 0;
19847: appendText(&sSql, ",", 0);
19848: appendText(&sSql, zTab, '\'');
19849: zSep = "),(";
19850: }
19851: sqlite3_finalize(pStmt);
19852: if( bSeparate ){
19853: zSql = sqlite3_mprintf(
19854: "%s))"
19855: " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
19856: " FROM [sha3sum$query]",
19857: sSql.z, iSize);
19858: }else{
19859: zSql = sqlite3_mprintf(
19860: "%s))"
19861: " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
19862: " FROM [sha3sum$query]",
19863: sSql.z, iSize);
19864: }
19865: freeText(&sQuery);
19866: freeText(&sSql);
19867: if( bDebug ){
19868: utf8_printf(p->out, "%s\n", zSql);
19869: }else{
19870: shell_exec(p, zSql, 0);
19871: }
19872: sqlite3_free(zSql);
19873: }else
19874:
19875: #ifndef SQLITE_NOHAVE_SYSTEM
19876: if( c=='s'
19877: && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
19878: ){
19879: char *zCmd;
19880: int i, x;
19881: if( nArg<2 ){
19882: raw_printf(stderr, "Usage: .system COMMAND\n");
19883: rc = 1;
19884: goto meta_command_exit;
19885: }
19886: zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
19887: for(i=2; i<nArg; i++){
19888: zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
19889: zCmd, azArg[i]);
19890: }
19891: x = system(zCmd);
19892: sqlite3_free(zCmd);
19893: if( x ) raw_printf(stderr, "System command returns %d\n", x);
19894: }else
19895: #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
19896:
19897: if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
19898: static const char *azBool[] = { "off", "on", "trigger", "full"};
19899: const char *zOut;
19900: int i;
19901: if( nArg!=1 ){
19902: raw_printf(stderr, "Usage: .show\n");
19903: rc = 1;
19904: goto meta_command_exit;
19905: }
19906: utf8_printf(p->out, "%12.12s: %s\n","echo",
19907: azBool[ShellHasFlag(p, SHFLG_Echo)]);
19908: utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
19909: utf8_printf(p->out, "%12.12s: %s\n","explain",
19910: p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
19911: utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
19912: utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
19913: utf8_printf(p->out, "%12.12s: ", "nullvalue");
19914: output_c_string(p->out, p->nullValue);
19915: raw_printf(p->out, "\n");
19916: utf8_printf(p->out,"%12.12s: %s\n","output",
19917: strlen30(p->outfile) ? p->outfile : "stdout");
19918: utf8_printf(p->out,"%12.12s: ", "colseparator");
19919: output_c_string(p->out, p->colSeparator);
19920: raw_printf(p->out, "\n");
19921: utf8_printf(p->out,"%12.12s: ", "rowseparator");
19922: output_c_string(p->out, p->rowSeparator);
19923: raw_printf(p->out, "\n");
19924: switch( p->statsOn ){
19925: case 0: zOut = "off"; break;
19926: default: zOut = "on"; break;
19927: case 2: zOut = "stmt"; break;
19928: case 3: zOut = "vmstep"; break;
19929: }
19930: utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
19931: utf8_printf(p->out, "%12.12s: ", "width");
19932: for (i=0;i<p->nWidth;i++) {
19933: raw_printf(p->out, "%d ", p->colWidth[i]);
19934: }
19935: raw_printf(p->out, "\n");
19936: utf8_printf(p->out, "%12.12s: %s\n", "filename",
19937: p->zDbFilename ? p->zDbFilename : "");
19938: }else
19939:
19940: if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
19941: if( nArg==2 ){
19942: if( strcmp(azArg[1],"stmt")==0 ){
19943: p->statsOn = 2;
19944: }else if( strcmp(azArg[1],"vmstep")==0 ){
19945: p->statsOn = 3;
19946: }else{
19947: p->statsOn = (u8)booleanValue(azArg[1]);
19948: }
19949: }else if( nArg==1 ){
19950: display_stats(p->db, p, 0);
19951: }else{
19952: raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
19953: rc = 1;
19954: }
19955: }else
19956:
19957: if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
19958: || (c=='i' && (strncmp(azArg[0], "indices", n)==0
19959: || strncmp(azArg[0], "indexes", n)==0) )
19960: ){
19961: sqlite3_stmt *pStmt;
19962: char **azResult;
19963: int nRow, nAlloc;
19964: int ii;
19965: ShellText s;
19966: initText(&s);
19967: open_db(p, 0);
19968: rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19969: if( rc ){
19970: sqlite3_finalize(pStmt);
19971: return shellDatabaseError(p->db);
19972: }
19973:
19974: if( nArg>2 && c=='i' ){
19975: /* It is an historical accident that the .indexes command shows an error
19976: ** when called with the wrong number of arguments whereas the .tables
19977: ** command does not. */
19978: raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
19979: rc = 1;
19980: sqlite3_finalize(pStmt);
19981: goto meta_command_exit;
19982: }
19983: for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
19984: const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
19985: if( zDbName==0 ) continue;
19986: if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
19987: if( sqlite3_stricmp(zDbName, "main")==0 ){
19988: appendText(&s, "SELECT name FROM ", 0);
19989: }else{
19990: appendText(&s, "SELECT ", 0);
19991: appendText(&s, zDbName, '\'');
19992: appendText(&s, "||'.'||name FROM ", 0);
19993: }
19994: appendText(&s, zDbName, '"');
19995: appendText(&s, ".sqlite_schema ", 0);
19996: if( c=='t' ){
19997: appendText(&s," WHERE type IN ('table','view')"
19998: " AND name NOT LIKE 'sqlite_%'"
19999: " AND name LIKE ?1", 0);
20000: }else{
20001: appendText(&s," WHERE type='index'"
20002: " AND tbl_name LIKE ?1", 0);
20003: }
20004: }
20005: rc = sqlite3_finalize(pStmt);
20006: appendText(&s, " ORDER BY 1", 0);
20007: rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
20008: freeText(&s);
20009: if( rc ) return shellDatabaseError(p->db);
20010:
20011: /* Run the SQL statement prepared by the above block. Store the results
20012: ** as an array of nul-terminated strings in azResult[]. */
20013: nRow = nAlloc = 0;
20014: azResult = 0;
20015: if( nArg>1 ){
20016: sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
20017: }else{
20018: sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
20019: }
20020: while( sqlite3_step(pStmt)==SQLITE_ROW ){
20021: if( nRow>=nAlloc ){
20022: char **azNew;
20023: int n2 = nAlloc*2 + 10;
20024: azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
20025: if( azNew==0 ) shell_out_of_memory();
20026: nAlloc = n2;
20027: azResult = azNew;
20028: }
20029: azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
20030: if( 0==azResult[nRow] ) shell_out_of_memory();
20031: nRow++;
20032: }
20033: if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
20034: rc = shellDatabaseError(p->db);
20035: }
20036:
20037: /* Pretty-print the contents of array azResult[] to the output */
20038: if( rc==0 && nRow>0 ){
20039: int len, maxlen = 0;
20040: int i, j;
20041: int nPrintCol, nPrintRow;
20042: for(i=0; i<nRow; i++){
20043: len = strlen30(azResult[i]);
20044: if( len>maxlen ) maxlen = len;
20045: }
20046: nPrintCol = 80/(maxlen+2);
20047: if( nPrintCol<1 ) nPrintCol = 1;
20048: nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
20049: for(i=0; i<nPrintRow; i++){
20050: for(j=i; j<nRow; j+=nPrintRow){
20051: char *zSp = j<nPrintRow ? "" : " ";
20052: utf8_printf(p->out, "%s%-*s", zSp, maxlen,
20053: azResult[j] ? azResult[j]:"");
20054: }
20055: raw_printf(p->out, "\n");
20056: }
20057: }
20058:
20059: for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
20060: sqlite3_free(azResult);
20061: }else
20062:
20063: /* Begin redirecting output to the file "testcase-out.txt" */
20064: if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
20065: output_reset(p);
20066: p->out = output_file_open("testcase-out.txt", 0);
20067: if( p->out==0 ){
20068: raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
20069: }
20070: if( nArg>=2 ){
20071: sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
20072: }else{
20073: sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
20074: }
20075: }else
20076:
20077: #ifndef SQLITE_UNTESTABLE
20078: if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
20079: static const struct {
20080: const char *zCtrlName; /* Name of a test-control option */
20081: int ctrlCode; /* Integer code for that option */
20082: const char *zUsage; /* Usage notes */
20083: } aCtrl[] = {
20084: { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
20085: { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
20086: /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
20087: /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
20088: { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
20089: { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
20090: /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
20091: { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
20092: { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
20093: { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
20094: { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
20095: { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
20096: #ifdef YYCOVERAGE
20097: { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
20098: #endif
20099: { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
20100: { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
20101: { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
20102: { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
20103: { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" },
20104: };
20105: int testctrl = -1;
20106: int iCtrl = -1;
20107: int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
20108: int isOk = 0;
20109: int i, n2;
20110: const char *zCmd = 0;
20111:
20112: open_db(p, 0);
20113: zCmd = nArg>=2 ? azArg[1] : "help";
20114:
20115: /* The argument can optionally begin with "-" or "--" */
20116: if( zCmd[0]=='-' && zCmd[1] ){
20117: zCmd++;
20118: if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
20119: }
20120:
20121: /* --help lists all test-controls */
20122: if( strcmp(zCmd,"help")==0 ){
20123: utf8_printf(p->out, "Available test-controls:\n");
20124: for(i=0; i<ArraySize(aCtrl); i++){
20125: utf8_printf(p->out, " .testctrl %s %s\n",
20126: aCtrl[i].zCtrlName, aCtrl[i].zUsage);
20127: }
20128: rc = 1;
20129: goto meta_command_exit;
20130: }
20131:
20132: /* convert testctrl text option to value. allow any unique prefix
20133: ** of the option name, or a numerical value. */
20134: n2 = strlen30(zCmd);
20135: for(i=0; i<ArraySize(aCtrl); i++){
20136: if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20137: if( testctrl<0 ){
20138: testctrl = aCtrl[i].ctrlCode;
20139: iCtrl = i;
20140: }else{
20141: utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
20142: "Use \".testctrl --help\" for help\n", zCmd);
20143: rc = 1;
20144: goto meta_command_exit;
20145: }
20146: }
20147: }
20148: if( testctrl<0 ){
20149: utf8_printf(stderr,"Error: unknown test-control: %s\n"
20150: "Use \".testctrl --help\" for help\n", zCmd);
20151: }else{
20152: switch(testctrl){
20153:
20154: /* sqlite3_test_control(int, db, int) */
20155: case SQLITE_TESTCTRL_OPTIMIZATIONS:
20156: if( nArg==3 ){
20157: unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
20158: rc2 = sqlite3_test_control(testctrl, p->db, opt);
20159: isOk = 3;
20160: }
20161: break;
20162:
20163: /* sqlite3_test_control(int) */
20164: case SQLITE_TESTCTRL_PRNG_SAVE:
20165: case SQLITE_TESTCTRL_PRNG_RESTORE:
20166: case SQLITE_TESTCTRL_BYTEORDER:
20167: if( nArg==2 ){
20168: rc2 = sqlite3_test_control(testctrl);
20169: isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20170: }
20171: break;
20172:
20173: /* sqlite3_test_control(int, uint) */
20174: case SQLITE_TESTCTRL_PENDING_BYTE:
20175: if( nArg==3 ){
20176: unsigned int opt = (unsigned int)integerValue(azArg[2]);
20177: rc2 = sqlite3_test_control(testctrl, opt);
20178: isOk = 3;
20179: }
20180: break;
20181:
20182: /* sqlite3_test_control(int, int, sqlite3*) */
20183: case SQLITE_TESTCTRL_PRNG_SEED:
20184: if( nArg==3 || nArg==4 ){
20185: int ii = (int)integerValue(azArg[2]);
20186: sqlite3 *db;
20187: if( ii==0 && strcmp(azArg[2],"random")==0 ){
20188: sqlite3_randomness(sizeof(ii),&ii);
20189: printf("-- random seed: %d\n", ii);
20190: }
20191: if( nArg==3 ){
20192: db = 0;
20193: }else{
20194: db = p->db;
20195: /* Make sure the schema has been loaded */
20196: sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
20197: }
20198: rc2 = sqlite3_test_control(testctrl, ii, db);
20199: isOk = 3;
20200: }
20201: break;
20202:
20203: /* sqlite3_test_control(int, int) */
20204: case SQLITE_TESTCTRL_ASSERT:
20205: case SQLITE_TESTCTRL_ALWAYS:
20206: if( nArg==3 ){
20207: int opt = booleanValue(azArg[2]);
20208: rc2 = sqlite3_test_control(testctrl, opt);
20209: isOk = 1;
20210: }
20211: break;
20212:
20213: /* sqlite3_test_control(int, int) */
20214: case SQLITE_TESTCTRL_LOCALTIME_FAULT:
20215: case SQLITE_TESTCTRL_NEVER_CORRUPT:
20216: if( nArg==3 ){
20217: int opt = booleanValue(azArg[2]);
20218: rc2 = sqlite3_test_control(testctrl, opt);
20219: isOk = 3;
20220: }
20221: break;
20222:
20223: /* sqlite3_test_control(sqlite3*) */
20224: case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
20225: rc2 = sqlite3_test_control(testctrl, p->db);
20226: isOk = 3;
20227: break;
20228:
20229: case SQLITE_TESTCTRL_IMPOSTER:
20230: if( nArg==5 ){
20231: rc2 = sqlite3_test_control(testctrl, p->db,
20232: azArg[2],
20233: integerValue(azArg[3]),
20234: integerValue(azArg[4]));
20235: isOk = 3;
20236: }
20237: break;
20238:
20239: case SQLITE_TESTCTRL_SEEK_COUNT: {
20240: u64 x = 0;
20241: rc2 = sqlite3_test_control(testctrl, p->db, &x);
20242: utf8_printf(p->out, "%llu\n", x);
20243: isOk = 3;
20244: break;
20245: }
20246:
20247: #ifdef YYCOVERAGE
20248: case SQLITE_TESTCTRL_PARSER_COVERAGE:
20249: if( nArg==2 ){
20250: sqlite3_test_control(testctrl, p->out);
20251: isOk = 3;
20252: }
20253: #endif
20254: }
20255: }
20256: if( isOk==0 && iCtrl>=0 ){
20257: utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
20258: rc = 1;
20259: }else if( isOk==1 ){
20260: raw_printf(p->out, "%d\n", rc2);
20261: }else if( isOk==2 ){
20262: raw_printf(p->out, "0x%08x\n", rc2);
20263: }
20264: }else
20265: #endif /* !defined(SQLITE_UNTESTABLE) */
20266:
20267: if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
20268: open_db(p, 0);
20269: sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
20270: }else
20271:
20272: if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
20273: if( nArg==2 ){
20274: enableTimer = booleanValue(azArg[1]);
20275: if( enableTimer && !HAS_TIMER ){
20276: raw_printf(stderr, "Error: timer not available on this system.\n");
20277: enableTimer = 0;
20278: }
20279: }else{
20280: raw_printf(stderr, "Usage: .timer on|off\n");
20281: rc = 1;
20282: }
20283: }else
20284:
20285: #ifndef SQLITE_OMIT_TRACE
20286: if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
20287: int mType = 0;
20288: int jj;
20289: open_db(p, 0);
20290: for(jj=1; jj<nArg; jj++){
20291: const char *z = azArg[jj];
20292: if( z[0]=='-' ){
20293: if( optionMatch(z, "expanded") ){
20294: p->eTraceType = SHELL_TRACE_EXPANDED;
20295: }
20296: #ifdef SQLITE_ENABLE_NORMALIZE
20297: else if( optionMatch(z, "normalized") ){
20298: p->eTraceType = SHELL_TRACE_NORMALIZED;
20299: }
20300: #endif
20301: else if( optionMatch(z, "plain") ){
20302: p->eTraceType = SHELL_TRACE_PLAIN;
20303: }
20304: else if( optionMatch(z, "profile") ){
20305: mType |= SQLITE_TRACE_PROFILE;
20306: }
20307: else if( optionMatch(z, "row") ){
20308: mType |= SQLITE_TRACE_ROW;
20309: }
20310: else if( optionMatch(z, "stmt") ){
20311: mType |= SQLITE_TRACE_STMT;
20312: }
20313: else if( optionMatch(z, "close") ){
20314: mType |= SQLITE_TRACE_CLOSE;
20315: }
20316: else {
20317: raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
20318: rc = 1;
20319: goto meta_command_exit;
20320: }
20321: }else{
20322: output_file_close(p->traceOut);
20323: p->traceOut = output_file_open(azArg[1], 0);
20324: }
20325: }
20326: if( p->traceOut==0 ){
20327: sqlite3_trace_v2(p->db, 0, 0, 0);
20328: }else{
20329: if( mType==0 ) mType = SQLITE_TRACE_STMT;
20330: sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
20331: }
20332: }else
20333: #endif /* !defined(SQLITE_OMIT_TRACE) */
20334:
20335: #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20336: if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
20337: int ii;
20338: int lenOpt;
20339: char *zOpt;
20340: if( nArg<2 ){
20341: raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
20342: rc = 1;
20343: goto meta_command_exit;
20344: }
20345: open_db(p, 0);
20346: zOpt = azArg[1];
20347: if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
20348: lenOpt = (int)strlen(zOpt);
20349: if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
20350: assert( azArg[nArg]==0 );
20351: sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
20352: }else{
20353: for(ii=1; ii<nArg; ii++){
20354: sqlite3_create_module(p->db, azArg[ii], 0, 0);
20355: }
20356: }
20357: }else
20358: #endif
20359:
20360: #if SQLITE_USER_AUTHENTICATION
20361: if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
20362: if( nArg<2 ){
20363: raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
20364: rc = 1;
20365: goto meta_command_exit;
20366: }
20367: open_db(p, 0);
20368: if( strcmp(azArg[1],"login")==0 ){
20369: if( nArg!=4 ){
20370: raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
20371: rc = 1;
20372: goto meta_command_exit;
20373: }
20374: rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
20375: strlen30(azArg[3]));
20376: if( rc ){
20377: utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
20378: rc = 1;
20379: }
20380: }else if( strcmp(azArg[1],"add")==0 ){
20381: if( nArg!=5 ){
20382: raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
20383: rc = 1;
20384: goto meta_command_exit;
20385: }
20386: rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20387: booleanValue(azArg[4]));
20388: if( rc ){
20389: raw_printf(stderr, "User-Add failed: %d\n", rc);
20390: rc = 1;
20391: }
20392: }else if( strcmp(azArg[1],"edit")==0 ){
20393: if( nArg!=5 ){
20394: raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
20395: rc = 1;
20396: goto meta_command_exit;
20397: }
20398: rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20399: booleanValue(azArg[4]));
20400: if( rc ){
20401: raw_printf(stderr, "User-Edit failed: %d\n", rc);
20402: rc = 1;
20403: }
20404: }else if( strcmp(azArg[1],"delete")==0 ){
20405: if( nArg!=3 ){
20406: raw_printf(stderr, "Usage: .user delete USER\n");
20407: rc = 1;
20408: goto meta_command_exit;
20409: }
20410: rc = sqlite3_user_delete(p->db, azArg[2]);
20411: if( rc ){
20412: raw_printf(stderr, "User-Delete failed: %d\n", rc);
20413: rc = 1;
20414: }
20415: }else{
20416: raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
20417: rc = 1;
20418: goto meta_command_exit;
20419: }
20420: }else
20421: #endif /* SQLITE_USER_AUTHENTICATION */
20422:
20423: if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
20424: utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
20425: sqlite3_libversion(), sqlite3_sourceid());
20426: #if SQLITE_HAVE_ZLIB
20427: utf8_printf(p->out, "zlib version %s\n", zlibVersion());
20428: #endif
20429: #define CTIMEOPT_VAL_(opt) #opt
20430: #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20431: #if defined(__clang__) && defined(__clang_major__)
20432: utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
20433: CTIMEOPT_VAL(__clang_minor__) "."
20434: CTIMEOPT_VAL(__clang_patchlevel__) "\n");
20435: #elif defined(_MSC_VER)
20436: utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
20437: #elif defined(__GNUC__) && defined(__VERSION__)
20438: utf8_printf(p->out, "gcc-" __VERSION__ "\n");
20439: #endif
20440: }else
20441:
20442: if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
20443: const char *zDbName = nArg==2 ? azArg[1] : "main";
20444: sqlite3_vfs *pVfs = 0;
20445: if( p->db ){
20446: sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
20447: if( pVfs ){
20448: utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
20449: raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20450: raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20451: raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20452: }
20453: }
20454: }else
20455:
20456: if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
20457: sqlite3_vfs *pVfs;
20458: sqlite3_vfs *pCurrent = 0;
20459: if( p->db ){
20460: sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
20461: }
20462: for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
20463: utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
20464: pVfs==pCurrent ? " <--- CURRENT" : "");
20465: raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20466: raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20467: raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20468: if( pVfs->pNext ){
20469: raw_printf(p->out, "-----------------------------------\n");
20470: }
20471: }
20472: }else
20473:
20474: if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
20475: const char *zDbName = nArg==2 ? azArg[1] : "main";
20476: char *zVfsName = 0;
20477: if( p->db ){
20478: sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
20479: if( zVfsName ){
20480: utf8_printf(p->out, "%s\n", zVfsName);
20481: sqlite3_free(zVfsName);
20482: }
20483: }
20484: }else
20485:
20486: if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
20487: unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20488: sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
20489: }else
20490:
20491: if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
20492: int j;
20493: assert( nArg<=ArraySize(azArg) );
20494: p->nWidth = nArg-1;
20495: p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
20496: if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
20497: if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
20498: for(j=1; j<nArg; j++){
20499: p->colWidth[j-1] = (int)integerValue(azArg[j]);
20500: }
20501: }else
20502:
20503: {
20504: utf8_printf(stderr, "Error: unknown command or invalid arguments: "
20505: " \"%s\". Enter \".help\" for help\n", azArg[0]);
20506: rc = 1;
20507: }
20508:
20509: meta_command_exit:
20510: if( p->outCount ){
20511: p->outCount--;
20512: if( p->outCount==0 ) output_reset(p);
20513: }
20514: return rc;
20515: }
20516:
20517: /*
20518: ** Return TRUE if a semicolon occurs anywhere in the first N characters
20519: ** of string z[].
20520: */
20521: static int line_contains_semicolon(const char *z, int N){
20522: int i;
20523: for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
20524: return 0;
20525: }
20526:
20527: /*
20528: ** Test to see if a line consists entirely of whitespace.
20529: */
20530: static int _all_whitespace(const char *z){
20531: for(; *z; z++){
20532: if( IsSpace(z[0]) ) continue;
20533: if( *z=='/' && z[1]=='*' ){
20534: z += 2;
20535: while( *z && (*z!='*' || z[1]!='/') ){ z++; }
20536: if( *z==0 ) return 0;
20537: z++;
20538: continue;
20539: }
20540: if( *z=='-' && z[1]=='-' ){
20541: z += 2;
20542: while( *z && *z!='\n' ){ z++; }
20543: if( *z==0 ) return 1;
20544: continue;
20545: }
20546: return 0;
20547: }
20548: return 1;
20549: }
20550:
20551: /*
20552: ** Return TRUE if the line typed in is an SQL command terminator other
20553: ** than a semi-colon. The SQL Server style "go" command is understood
20554: ** as is the Oracle "/".
20555: */
20556: static int line_is_command_terminator(const char *zLine){
20557: while( IsSpace(zLine[0]) ){ zLine++; };
20558: if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
20559: return 1; /* Oracle */
20560: }
20561: if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
20562: && _all_whitespace(&zLine[2]) ){
20563: return 1; /* SQL Server */
20564: }
20565: return 0;
20566: }
20567:
20568: /*
20569: ** We need a default sqlite3_complete() implementation to use in case
20570: ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
20571: ** any arbitrary text is a complete SQL statement. This is not very
20572: ** user-friendly, but it does seem to work.
20573: */
20574: #ifdef SQLITE_OMIT_COMPLETE
20575: #define sqlite3_complete(x) 1
20576: #endif
20577:
20578: /*
20579: ** Return true if zSql is a complete SQL statement. Return false if it
20580: ** ends in the middle of a string literal or C-style comment.
20581: */
20582: static int line_is_complete(char *zSql, int nSql){
20583: int rc;
20584: if( zSql==0 ) return 1;
20585: zSql[nSql] = ';';
20586: zSql[nSql+1] = 0;
20587: rc = sqlite3_complete(zSql);
20588: zSql[nSql] = 0;
20589: return rc;
20590: }
20591:
20592: /*
20593: ** Run a single line of SQL. Return the number of errors.
20594: */
20595: static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
20596: int rc;
20597: char *zErrMsg = 0;
20598:
20599: open_db(p, 0);
20600: if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
20601: if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
20602: BEGIN_TIMER;
20603: rc = shell_exec(p, zSql, &zErrMsg);
20604: END_TIMER;
20605: if( rc || zErrMsg ){
20606: char zPrefix[100];
20607: if( in!=0 || !stdin_is_interactive ){
20608: sqlite3_snprintf(sizeof(zPrefix), zPrefix,
20609: "Error: near line %d:", startline);
20610: }else{
20611: sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
20612: }
20613: if( zErrMsg!=0 ){
20614: utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
20615: sqlite3_free(zErrMsg);
20616: zErrMsg = 0;
20617: }else{
20618: utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
20619: }
20620: return 1;
20621: }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
20622: raw_printf(p->out, "changes: %3d total_changes: %d\n",
20623: sqlite3_changes(p->db), sqlite3_total_changes(p->db));
20624: }
20625: return 0;
20626: }
20627:
20628:
20629: /*
20630: ** Read input from *in and process it. If *in==0 then input
20631: ** is interactive - the user is typing it it. Otherwise, input
20632: ** is coming from a file or device. A prompt is issued and history
20633: ** is saved only if input is interactive. An interrupt signal will
20634: ** cause this routine to exit immediately, unless input is interactive.
20635: **
20636: ** Return the number of errors.
20637: */
20638: static int process_input(ShellState *p){
20639: char *zLine = 0; /* A single input line */
20640: char *zSql = 0; /* Accumulated SQL text */
20641: int nLine; /* Length of current line */
20642: int nSql = 0; /* Bytes of zSql[] used */
20643: int nAlloc = 0; /* Allocated zSql[] space */
20644: int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
20645: int rc; /* Error code */
20646: int errCnt = 0; /* Number of errors seen */
20647: int startline = 0; /* Line number for start of current input */
20648:
20649: p->lineno = 0;
20650: while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
20651: fflush(p->out);
20652: zLine = one_input_line(p->in, zLine, nSql>0);
20653: if( zLine==0 ){
20654: /* End of input */
20655: if( p->in==0 && stdin_is_interactive ) printf("\n");
20656: break;
20657: }
20658: if( seenInterrupt ){
20659: if( p->in!=0 ) break;
20660: seenInterrupt = 0;
20661: }
20662: p->lineno++;
20663: if( nSql==0 && _all_whitespace(zLine) ){
20664: if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20665: continue;
20666: }
20667: if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
20668: if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20669: if( zLine[0]=='.' ){
20670: rc = do_meta_command(zLine, p);
20671: if( rc==2 ){ /* exit requested */
20672: break;
20673: }else if( rc ){
20674: errCnt++;
20675: }
20676: }
20677: continue;
20678: }
20679: if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
20680: memcpy(zLine,";",2);
20681: }
20682: nLine = strlen30(zLine);
20683: if( nSql+nLine+2>=nAlloc ){
20684: nAlloc = nSql+nLine+100;
20685: zSql = realloc(zSql, nAlloc);
20686: if( zSql==0 ) shell_out_of_memory();
20687: }
20688: nSqlPrior = nSql;
20689: if( nSql==0 ){
20690: int i;
20691: for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
20692: assert( nAlloc>0 && zSql!=0 );
20693: memcpy(zSql, zLine+i, nLine+1-i);
20694: startline = p->lineno;
20695: nSql = nLine-i;
20696: }else{
20697: zSql[nSql++] = '\n';
20698: memcpy(zSql+nSql, zLine, nLine+1);
20699: nSql += nLine;
20700: }
20701: if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
20702: && sqlite3_complete(zSql) ){
20703: errCnt += runOneSqlLine(p, zSql, p->in, startline);
20704: nSql = 0;
20705: if( p->outCount ){
20706: output_reset(p);
20707: p->outCount = 0;
20708: }else{
20709: clearTempFile(p);
20710: }
20711: }else if( nSql && _all_whitespace(zSql) ){
20712: if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
20713: nSql = 0;
20714: }
20715: }
20716: if( nSql && !_all_whitespace(zSql) ){
20717: errCnt += runOneSqlLine(p, zSql, p->in, startline);
20718: }
20719: free(zSql);
20720: free(zLine);
20721: return errCnt>0;
20722: }
20723:
20724: /*
20725: ** Return a pathname which is the user's home directory. A
20726: ** 0 return indicates an error of some kind.
20727: */
20728: static char *find_home_dir(int clearFlag){
20729: static char *home_dir = NULL;
20730: if( clearFlag ){
20731: free(home_dir);
20732: home_dir = 0;
20733: return 0;
20734: }
20735: if( home_dir ) return home_dir;
20736:
20737: #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
20738: && !defined(__RTP__) && !defined(_WRS_KERNEL)
20739: {
20740: struct passwd *pwent;
20741: uid_t uid = getuid();
20742: if( (pwent=getpwuid(uid)) != NULL) {
20743: home_dir = pwent->pw_dir;
20744: }
20745: }
20746: #endif
20747:
20748: #if defined(_WIN32_WCE)
20749: /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
20750: */
20751: home_dir = "/";
20752: #else
20753:
20754: #if defined(_WIN32) || defined(WIN32)
20755: if (!home_dir) {
20756: home_dir = getenv("USERPROFILE");
20757: }
20758: #endif
20759:
20760: if (!home_dir) {
20761: home_dir = getenv("HOME");
20762: }
20763:
20764: #if defined(_WIN32) || defined(WIN32)
20765: if (!home_dir) {
20766: char *zDrive, *zPath;
20767: int n;
20768: zDrive = getenv("HOMEDRIVE");
20769: zPath = getenv("HOMEPATH");
20770: if( zDrive && zPath ){
20771: n = strlen30(zDrive) + strlen30(zPath) + 1;
20772: home_dir = malloc( n );
20773: if( home_dir==0 ) return 0;
20774: sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
20775: return home_dir;
20776: }
20777: home_dir = "c:\\";
20778: }
20779: #endif
20780:
20781: #endif /* !_WIN32_WCE */
20782:
20783: if( home_dir ){
20784: int n = strlen30(home_dir) + 1;
20785: char *z = malloc( n );
20786: if( z ) memcpy(z, home_dir, n);
20787: home_dir = z;
20788: }
20789:
20790: return home_dir;
20791: }
20792:
20793: /*
20794: ** Read input from the file given by sqliterc_override. Or if that
20795: ** parameter is NULL, take input from ~/.sqliterc
20796: **
20797: ** Returns the number of errors.
20798: */
20799: static void process_sqliterc(
20800: ShellState *p, /* Configuration data */
20801: const char *sqliterc_override /* Name of config file. NULL to use default */
20802: ){
20803: char *home_dir = NULL;
20804: const char *sqliterc = sqliterc_override;
20805: char *zBuf = 0;
20806: FILE *inSaved = p->in;
20807: int savedLineno = p->lineno;
20808:
20809: if (sqliterc == NULL) {
20810: home_dir = find_home_dir(0);
20811: if( home_dir==0 ){
20812: raw_printf(stderr, "-- warning: cannot find home directory;"
20813: " cannot read ~/.sqliterc\n");
20814: return;
20815: }
20816: zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
20817: sqliterc = zBuf;
20818: }
20819: p->in = fopen(sqliterc,"rb");
20820: if( p->in ){
20821: if( stdin_is_interactive ){
20822: utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
20823: }
20824: if( process_input(p) && bail_on_error ) exit(1);
20825: fclose(p->in);
20826: }else if( sqliterc_override!=0 ){
20827: utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
20828: if( bail_on_error ) exit(1);
20829: }
20830: p->in = inSaved;
20831: p->lineno = savedLineno;
20832: sqlite3_free(zBuf);
20833: }
20834:
20835: /*
20836: ** Show available command line options
20837: */
20838: static const char zOptions[] =
20839: #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20840: " -A ARGS... run \".archive ARGS\" and exit\n"
20841: #endif
20842: " -append append the database to the end of the file\n"
20843: " -ascii set output mode to 'ascii'\n"
20844: " -bail stop after hitting an error\n"
20845: " -batch force batch I/O\n"
20846: " -box set output mode to 'box'\n"
20847: " -column set output mode to 'column'\n"
20848: " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
20849: " -csv set output mode to 'csv'\n"
20850: #if defined(SQLITE_ENABLE_DESERIALIZE)
20851: " -deserialize open the database using sqlite3_deserialize()\n"
20852: #endif
20853: " -echo print commands before execution\n"
20854: " -init FILENAME read/process named file\n"
20855: " -[no]header turn headers on or off\n"
20856: #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20857: " -heap SIZE Size of heap for memsys3 or memsys5\n"
20858: #endif
20859: " -help show this message\n"
20860: " -html set output mode to HTML\n"
20861: " -interactive force interactive I/O\n"
20862: " -json set output mode to 'json'\n"
20863: " -line set output mode to 'line'\n"
20864: " -list set output mode to 'list'\n"
20865: " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
20866: " -markdown set output mode to 'markdown'\n"
20867: #if defined(SQLITE_ENABLE_DESERIALIZE)
20868: " -maxsize N maximum size for a --deserialize database\n"
20869: #endif
20870: " -memtrace trace all memory allocations and deallocations\n"
20871: " -mmap N default mmap size set to N\n"
20872: #ifdef SQLITE_ENABLE_MULTIPLEX
20873: " -multiplex enable the multiplexor VFS\n"
20874: #endif
20875: " -newline SEP set output row separator. Default: '\\n'\n"
20876: " -nofollow refuse to open symbolic links to database files\n"
20877: " -nullvalue TEXT set text string for NULL values. Default ''\n"
20878: " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
20879: " -quote set output mode to 'quote'\n"
20880: " -readonly open the database read-only\n"
20881: " -separator SEP set output column separator. Default: '|'\n"
20882: #ifdef SQLITE_ENABLE_SORTER_REFERENCES
20883: " -sorterref SIZE sorter references threshold size\n"
20884: #endif
20885: " -stats print memory stats before each finalize\n"
20886: " -table set output mode to 'table'\n"
20887: " -tabs set output mode to 'tabs'\n"
20888: " -version show SQLite version\n"
20889: " -vfs NAME use NAME as the default VFS\n"
20890: #ifdef SQLITE_ENABLE_VFSTRACE
20891: " -vfstrace enable tracing of all VFS calls\n"
20892: #endif
20893: #ifdef SQLITE_HAVE_ZLIB
20894: " -zip open the file as a ZIP Archive\n"
20895: #endif
20896: ;
20897: static void usage(int showDetail){
20898: utf8_printf(stderr,
20899: "Usage: %s [OPTIONS] FILENAME [SQL]\n"
20900: "FILENAME is the name of an SQLite database. A new database is created\n"
20901: "if the file does not previously exist.\n", Argv0);
20902: if( showDetail ){
20903: utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
20904: }else{
20905: raw_printf(stderr, "Use the -help option for additional information\n");
20906: }
20907: exit(1);
20908: }
20909:
20910: /*
20911: ** Internal check: Verify that the SQLite is uninitialized. Print a
20912: ** error message if it is initialized.
20913: */
20914: static void verify_uninitialized(void){
20915: if( sqlite3_config(-1)==SQLITE_MISUSE ){
20916: utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
20917: " initialization.\n");
20918: }
20919: }
20920:
20921: /*
20922: ** Initialize the state information in data
20923: */
20924: static void main_init(ShellState *data) {
20925: memset(data, 0, sizeof(*data));
20926: data->normalMode = data->cMode = data->mode = MODE_List;
20927: data->autoExplain = 1;
20928: memcpy(data->colSeparator,SEP_Column, 2);
20929: memcpy(data->rowSeparator,SEP_Row, 2);
20930: data->showHeader = 0;
20931: data->shellFlgs = SHFLG_Lookaside;
20932: verify_uninitialized();
20933: sqlite3_config(SQLITE_CONFIG_URI, 1);
20934: sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
20935: sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
20936: sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
20937: sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
20938: }
20939:
20940: /*
20941: ** Output text to the console in a font that attracts extra attention.
20942: */
20943: #ifdef _WIN32
20944: static void printBold(const char *zText){
20945: #if !SQLITE_OS_WINRT
20946: HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
20947: CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
20948: GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
20949: SetConsoleTextAttribute(out,
20950: FOREGROUND_RED|FOREGROUND_INTENSITY
20951: );
20952: #endif
20953: printf("%s", zText);
20954: #if !SQLITE_OS_WINRT
20955: SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
20956: #endif
20957: }
20958: #else
20959: static void printBold(const char *zText){
20960: printf("\033[1m%s\033[0m", zText);
20961: }
20962: #endif
20963:
20964: /*
20965: ** Get the argument to an --option. Throw an error and die if no argument
20966: ** is available.
20967: */
20968: static char *cmdline_option_value(int argc, char **argv, int i){
20969: if( i==argc ){
20970: utf8_printf(stderr, "%s: Error: missing argument to %s\n",
20971: argv[0], argv[argc-1]);
20972: exit(1);
20973: }
20974: return argv[i];
20975: }
20976:
20977: #ifndef SQLITE_SHELL_IS_UTF8
20978: # if (defined(_WIN32) || defined(WIN32)) \
20979: && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
20980: # define SQLITE_SHELL_IS_UTF8 (0)
20981: # else
20982: # define SQLITE_SHELL_IS_UTF8 (1)
20983: # endif
20984: #endif
20985:
20986: #if SQLITE_SHELL_IS_UTF8
20987: int SQLITE_CDECL main(int argc, char **argv){
20988: #else
20989: int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
20990: char **argv;
20991: #endif
20992: char *zErrMsg = 0;
20993: ShellState data;
20994: const char *zInitFile = 0;
20995: int i;
20996: int rc = 0;
20997: int warnInmemoryDb = 0;
20998: int readStdin = 1;
20999: int nCmd = 0;
21000: char **azCmd = 0;
21001: const char *zVfs = 0; /* Value of -vfs command-line option */
21002: #if !SQLITE_SHELL_IS_UTF8
21003: char **argvToFree = 0;
21004: int argcToFree = 0;
21005: #endif
21006:
21007: setBinaryMode(stdin, 0);
21008: setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
21009: stdin_is_interactive = isatty(0);
21010: stdout_is_console = isatty(1);
21011:
21012: #ifdef SQLITE_DEBUG
21013: registerOomSimulator();
21014: #endif
21015:
21016: #if !defined(_WIN32_WCE)
21017: if( getenv("SQLITE_DEBUG_BREAK") ){
21018: if( isatty(0) && isatty(2) ){
21019: fprintf(stderr,
21020: "attach debugger to process %d and press any key to continue.\n",
21021: GETPID());
21022: fgetc(stdin);
21023: }else{
21024: #if defined(_WIN32) || defined(WIN32)
21025: #if SQLITE_OS_WINRT
21026: __debugbreak();
21027: #else
21028: DebugBreak();
21029: #endif
21030: #elif defined(SIGTRAP)
21031: raise(SIGTRAP);
21032: #endif
21033: }
21034: }
21035: #endif
21036:
21037: #if USE_SYSTEM_SQLITE+0!=1
21038: if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
21039: utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
21040: sqlite3_sourceid(), SQLITE_SOURCE_ID);
21041: exit(1);
21042: }
21043: #endif
21044: main_init(&data);
21045:
21046: /* On Windows, we must translate command-line arguments into UTF-8.
21047: ** The SQLite memory allocator subsystem has to be enabled in order to
21048: ** do this. But we want to run an sqlite3_shutdown() afterwards so that
21049: ** subsequent sqlite3_config() calls will work. So copy all results into
21050: ** memory that does not come from the SQLite memory allocator.
21051: */
21052: #if !SQLITE_SHELL_IS_UTF8
21053: sqlite3_initialize();
21054: argvToFree = malloc(sizeof(argv[0])*argc*2);
21055: argcToFree = argc;
21056: argv = argvToFree + argc;
21057: if( argv==0 ) shell_out_of_memory();
21058: for(i=0; i<argc; i++){
21059: char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
21060: int n;
21061: if( z==0 ) shell_out_of_memory();
21062: n = (int)strlen(z);
21063: argv[i] = malloc( n+1 );
21064: if( argv[i]==0 ) shell_out_of_memory();
21065: memcpy(argv[i], z, n+1);
21066: argvToFree[i] = argv[i];
21067: sqlite3_free(z);
21068: }
21069: sqlite3_shutdown();
21070: #endif
21071:
21072: assert( argc>=1 && argv && argv[0] );
21073: Argv0 = argv[0];
21074:
21075: /* Make sure we have a valid signal handler early, before anything
21076: ** else is done.
21077: */
21078: #ifdef SIGINT
21079: signal(SIGINT, interrupt_handler);
21080: #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
21081: SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
21082: #endif
21083:
21084: #ifdef SQLITE_SHELL_DBNAME_PROC
21085: {
21086: /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
21087: ** of a C-function that will provide the name of the database file. Use
21088: ** this compile-time option to embed this shell program in larger
21089: ** applications. */
21090: extern void SQLITE_SHELL_DBNAME_PROC(const char**);
21091: SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
21092: warnInmemoryDb = 0;
21093: }
21094: #endif
21095:
21096: /* Do an initial pass through the command-line argument to locate
21097: ** the name of the database file, the name of the initialization file,
21098: ** the size of the alternative malloc heap,
21099: ** and the first command to execute.
21100: */
21101: verify_uninitialized();
21102: for(i=1; i<argc; i++){
21103: char *z;
21104: z = argv[i];
21105: if( z[0]!='-' ){
21106: if( data.zDbFilename==0 ){
21107: data.zDbFilename = z;
21108: }else{
21109: /* Excesss arguments are interpreted as SQL (or dot-commands) and
21110: ** mean that nothing is read from stdin */
21111: readStdin = 0;
21112: nCmd++;
21113: azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
21114: if( azCmd==0 ) shell_out_of_memory();
21115: azCmd[nCmd-1] = z;
21116: }
21117: }
21118: if( z[1]=='-' ) z++;
21119: if( strcmp(z,"-separator")==0
21120: || strcmp(z,"-nullvalue")==0
21121: || strcmp(z,"-newline")==0
21122: || strcmp(z,"-cmd")==0
21123: ){
21124: (void)cmdline_option_value(argc, argv, ++i);
21125: }else if( strcmp(z,"-init")==0 ){
21126: zInitFile = cmdline_option_value(argc, argv, ++i);
21127: }else if( strcmp(z,"-batch")==0 ){
21128: /* Need to check for batch mode here to so we can avoid printing
21129: ** informational messages (like from process_sqliterc) before
21130: ** we do the actual processing of arguments later in a second pass.
21131: */
21132: stdin_is_interactive = 0;
21133: }else if( strcmp(z,"-heap")==0 ){
21134: #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21135: const char *zSize;
21136: sqlite3_int64 szHeap;
21137:
21138: zSize = cmdline_option_value(argc, argv, ++i);
21139: szHeap = integerValue(zSize);
21140: if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
21141: sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
21142: #else
21143: (void)cmdline_option_value(argc, argv, ++i);
21144: #endif
21145: }else if( strcmp(z,"-pagecache")==0 ){
21146: sqlite3_int64 n, sz;
21147: sz = integerValue(cmdline_option_value(argc,argv,++i));
21148: if( sz>70000 ) sz = 70000;
21149: if( sz<0 ) sz = 0;
21150: n = integerValue(cmdline_option_value(argc,argv,++i));
21151: if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21152: n = 0xffffffffffffLL/sz;
21153: }
21154: sqlite3_config(SQLITE_CONFIG_PAGECACHE,
21155: (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
21156: data.shellFlgs |= SHFLG_Pagecache;
21157: }else if( strcmp(z,"-lookaside")==0 ){
21158: int n, sz;
21159: sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
21160: if( sz<0 ) sz = 0;
21161: n = (int)integerValue(cmdline_option_value(argc,argv,++i));
21162: if( n<0 ) n = 0;
21163: sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
21164: if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
21165: #ifdef SQLITE_ENABLE_VFSTRACE
21166: }else if( strcmp(z,"-vfstrace")==0 ){
21167: extern int vfstrace_register(
21168: const char *zTraceName,
21169: const char *zOldVfsName,
21170: int (*xOut)(const char*,void*),
21171: void *pOutArg,
21172: int makeDefault
21173: );
21174: vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
21175: #endif
21176: #ifdef SQLITE_ENABLE_MULTIPLEX
21177: }else if( strcmp(z,"-multiplex")==0 ){
21178: extern int sqlite3_multiple_initialize(const char*,int);
21179: sqlite3_multiplex_initialize(0, 1);
21180: #endif
21181: }else if( strcmp(z,"-mmap")==0 ){
21182: sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21183: sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
21184: #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21185: }else if( strcmp(z,"-sorterref")==0 ){
21186: sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21187: sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
21188: #endif
21189: }else if( strcmp(z,"-vfs")==0 ){
21190: zVfs = cmdline_option_value(argc, argv, ++i);
21191: #ifdef SQLITE_HAVE_ZLIB
21192: }else if( strcmp(z,"-zip")==0 ){
21193: data.openMode = SHELL_OPEN_ZIPFILE;
21194: #endif
21195: }else if( strcmp(z,"-append")==0 ){
21196: data.openMode = SHELL_OPEN_APPENDVFS;
21197: #ifdef SQLITE_ENABLE_DESERIALIZE
21198: }else if( strcmp(z,"-deserialize")==0 ){
21199: data.openMode = SHELL_OPEN_DESERIALIZE;
21200: }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21201: data.szMax = integerValue(argv[++i]);
21202: #endif
21203: }else if( strcmp(z,"-readonly")==0 ){
21204: data.openMode = SHELL_OPEN_READONLY;
21205: }else if( strcmp(z,"-nofollow")==0 ){
21206: data.openFlags = SQLITE_OPEN_NOFOLLOW;
21207: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21208: }else if( strncmp(z, "-A",2)==0 ){
21209: /* All remaining command-line arguments are passed to the ".archive"
21210: ** command, so ignore them */
21211: break;
21212: #endif
21213: }else if( strcmp(z, "-memtrace")==0 ){
21214: sqlite3MemTraceActivate(stderr);
21215: }else if( strcmp(z,"-bail")==0 ){
21216: bail_on_error = 1;
21217: }
21218: }
21219: verify_uninitialized();
21220:
21221:
21222: #ifdef SQLITE_SHELL_INIT_PROC
21223: {
21224: /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
21225: ** of a C-function that will perform initialization actions on SQLite that
21226: ** occur just before or after sqlite3_initialize(). Use this compile-time
21227: ** option to embed this shell program in larger applications. */
21228: extern void SQLITE_SHELL_INIT_PROC(void);
21229: SQLITE_SHELL_INIT_PROC();
21230: }
21231: #else
21232: /* All the sqlite3_config() calls have now been made. So it is safe
21233: ** to call sqlite3_initialize() and process any command line -vfs option. */
21234: sqlite3_initialize();
21235: #endif
21236:
21237: if( zVfs ){
21238: sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
21239: if( pVfs ){
21240: sqlite3_vfs_register(pVfs, 1);
21241: }else{
21242: utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
21243: exit(1);
21244: }
21245: }
21246:
21247: if( data.zDbFilename==0 ){
21248: #ifndef SQLITE_OMIT_MEMORYDB
21249: data.zDbFilename = ":memory:";
21250: warnInmemoryDb = argc==1;
21251: #else
21252: utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
21253: return 1;
21254: #endif
21255: }
21256: data.out = stdout;
21257: sqlite3_appendvfs_init(0,0,0);
21258:
21259: /* Go ahead and open the database file if it already exists. If the
21260: ** file does not exist, delay opening it. This prevents empty database
21261: ** files from being created if a user mistypes the database name argument
21262: ** to the sqlite command-line tool.
21263: */
21264: if( access(data.zDbFilename, 0)==0 ){
21265: open_db(&data, 0);
21266: }
21267:
21268: /* Process the initialization file if there is one. If no -init option
21269: ** is given on the command line, look for a file named ~/.sqliterc and
21270: ** try to process it.
21271: */
21272: process_sqliterc(&data,zInitFile);
21273:
21274: /* Make a second pass through the command-line argument and set
21275: ** options. This second pass is delayed until after the initialization
21276: ** file is processed so that the command-line arguments will override
21277: ** settings in the initialization file.
21278: */
21279: for(i=1; i<argc; i++){
21280: char *z = argv[i];
21281: if( z[0]!='-' ) continue;
21282: if( z[1]=='-' ){ z++; }
21283: if( strcmp(z,"-init")==0 ){
21284: i++;
21285: }else if( strcmp(z,"-html")==0 ){
21286: data.mode = MODE_Html;
21287: }else if( strcmp(z,"-list")==0 ){
21288: data.mode = MODE_List;
21289: }else if( strcmp(z,"-quote")==0 ){
21290: data.mode = MODE_Quote;
21291: sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
21292: sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21293: }else if( strcmp(z,"-line")==0 ){
21294: data.mode = MODE_Line;
21295: }else if( strcmp(z,"-column")==0 ){
21296: data.mode = MODE_Column;
21297: }else if( strcmp(z,"-json")==0 ){
21298: data.mode = MODE_Json;
21299: }else if( strcmp(z,"-markdown")==0 ){
21300: data.mode = MODE_Markdown;
21301: }else if( strcmp(z,"-table")==0 ){
21302: data.mode = MODE_Table;
21303: }else if( strcmp(z,"-box")==0 ){
21304: data.mode = MODE_Box;
21305: }else if( strcmp(z,"-csv")==0 ){
21306: data.mode = MODE_Csv;
21307: memcpy(data.colSeparator,",",2);
21308: #ifdef SQLITE_HAVE_ZLIB
21309: }else if( strcmp(z,"-zip")==0 ){
21310: data.openMode = SHELL_OPEN_ZIPFILE;
21311: #endif
21312: }else if( strcmp(z,"-append")==0 ){
21313: data.openMode = SHELL_OPEN_APPENDVFS;
21314: #ifdef SQLITE_ENABLE_DESERIALIZE
21315: }else if( strcmp(z,"-deserialize")==0 ){
21316: data.openMode = SHELL_OPEN_DESERIALIZE;
21317: }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21318: data.szMax = integerValue(argv[++i]);
21319: #endif
21320: }else if( strcmp(z,"-readonly")==0 ){
21321: data.openMode = SHELL_OPEN_READONLY;
21322: }else if( strcmp(z,"-nofollow")==0 ){
21323: data.openFlags |= SQLITE_OPEN_NOFOLLOW;
21324: }else if( strcmp(z,"-ascii")==0 ){
21325: data.mode = MODE_Ascii;
21326: sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
21327: sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
21328: }else if( strcmp(z,"-tabs")==0 ){
21329: data.mode = MODE_List;
21330: sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
21331: sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21332: }else if( strcmp(z,"-separator")==0 ){
21333: sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
21334: "%s",cmdline_option_value(argc,argv,++i));
21335: }else if( strcmp(z,"-newline")==0 ){
21336: sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
21337: "%s",cmdline_option_value(argc,argv,++i));
21338: }else if( strcmp(z,"-nullvalue")==0 ){
21339: sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
21340: "%s",cmdline_option_value(argc,argv,++i));
21341: }else if( strcmp(z,"-header")==0 ){
21342: data.showHeader = 1;
21343: }else if( strcmp(z,"-noheader")==0 ){
21344: data.showHeader = 0;
21345: }else if( strcmp(z,"-echo")==0 ){
21346: ShellSetFlag(&data, SHFLG_Echo);
21347: }else if( strcmp(z,"-eqp")==0 ){
21348: data.autoEQP = AUTOEQP_on;
21349: }else if( strcmp(z,"-eqpfull")==0 ){
21350: data.autoEQP = AUTOEQP_full;
21351: }else if( strcmp(z,"-stats")==0 ){
21352: data.statsOn = 1;
21353: }else if( strcmp(z,"-scanstats")==0 ){
21354: data.scanstatsOn = 1;
21355: }else if( strcmp(z,"-backslash")==0 ){
21356: /* Undocumented command-line option: -backslash
21357: ** Causes C-style backslash escapes to be evaluated in SQL statements
21358: ** prior to sending the SQL into SQLite. Useful for injecting
21359: ** crazy bytes in the middle of SQL statements for testing and debugging.
21360: */
21361: ShellSetFlag(&data, SHFLG_Backslash);
21362: }else if( strcmp(z,"-bail")==0 ){
21363: /* No-op. The bail_on_error flag should already be set. */
21364: }else if( strcmp(z,"-version")==0 ){
21365: printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
21366: return 0;
21367: }else if( strcmp(z,"-interactive")==0 ){
21368: stdin_is_interactive = 1;
21369: }else if( strcmp(z,"-batch")==0 ){
21370: stdin_is_interactive = 0;
21371: }else if( strcmp(z,"-heap")==0 ){
21372: i++;
21373: }else if( strcmp(z,"-pagecache")==0 ){
21374: i+=2;
21375: }else if( strcmp(z,"-lookaside")==0 ){
21376: i+=2;
21377: }else if( strcmp(z,"-mmap")==0 ){
21378: i++;
21379: }else if( strcmp(z,"-memtrace")==0 ){
21380: i++;
21381: #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21382: }else if( strcmp(z,"-sorterref")==0 ){
21383: i++;
21384: #endif
21385: }else if( strcmp(z,"-vfs")==0 ){
21386: i++;
21387: #ifdef SQLITE_ENABLE_VFSTRACE
21388: }else if( strcmp(z,"-vfstrace")==0 ){
21389: i++;
21390: #endif
21391: #ifdef SQLITE_ENABLE_MULTIPLEX
21392: }else if( strcmp(z,"-multiplex")==0 ){
21393: i++;
21394: #endif
21395: }else if( strcmp(z,"-help")==0 ){
21396: usage(1);
21397: }else if( strcmp(z,"-cmd")==0 ){
21398: /* Run commands that follow -cmd first and separately from commands
21399: ** that simply appear on the command-line. This seems goofy. It would
21400: ** be better if all commands ran in the order that they appear. But
21401: ** we retain the goofy behavior for historical compatibility. */
21402: if( i==argc-1 ) break;
21403: z = cmdline_option_value(argc,argv,++i);
21404: if( z[0]=='.' ){
21405: rc = do_meta_command(z, &data);
21406: if( rc && bail_on_error ) return rc==2 ? 0 : rc;
21407: }else{
21408: open_db(&data, 0);
21409: rc = shell_exec(&data, z, &zErrMsg);
21410: if( zErrMsg!=0 ){
21411: utf8_printf(stderr,"Error: %s\n", zErrMsg);
21412: if( bail_on_error ) return rc!=0 ? rc : 1;
21413: }else if( rc!=0 ){
21414: utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
21415: if( bail_on_error ) return rc;
21416: }
21417: }
21418: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21419: }else if( strncmp(z, "-A", 2)==0 ){
21420: if( nCmd>0 ){
21421: utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
21422: " with \"%s\"\n", z);
21423: return 1;
21424: }
21425: open_db(&data, OPEN_DB_ZIPFILE);
21426: if( z[2] ){
21427: argv[i] = &z[2];
21428: arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
21429: }else{
21430: arDotCommand(&data, 1, argv+i, argc-i);
21431: }
21432: readStdin = 0;
21433: break;
21434: #endif
21435: }else{
21436: utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
21437: raw_printf(stderr,"Use -help for a list of options.\n");
21438: return 1;
21439: }
21440: data.cMode = data.mode;
21441: }
21442:
21443: if( !readStdin ){
21444: /* Run all arguments that do not begin with '-' as if they were separate
21445: ** command-line inputs, except for the argToSkip argument which contains
21446: ** the database filename.
21447: */
21448: for(i=0; i<nCmd; i++){
21449: if( azCmd[i][0]=='.' ){
21450: rc = do_meta_command(azCmd[i], &data);
21451: if( rc ){
21452: free(azCmd);
21453: return rc==2 ? 0 : rc;
21454: }
21455: }else{
21456: open_db(&data, 0);
21457: rc = shell_exec(&data, azCmd[i], &zErrMsg);
21458: if( zErrMsg || rc ){
21459: if( zErrMsg!=0 ){
21460: utf8_printf(stderr,"Error: %s\n", zErrMsg);
21461: }else{
21462: utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
21463: }
21464: sqlite3_free(zErrMsg);
21465: free(azCmd);
21466: return rc!=0 ? rc : 1;
21467: }
21468: }
21469: }
21470: }else{
21471: /* Run commands received from standard input
21472: */
21473: if( stdin_is_interactive ){
21474: char *zHome;
21475: char *zHistory;
21476: int nHistory;
21477: printf(
21478: "SQLite version %s %.19s\n" /*extra-version-info*/
21479: "Enter \".help\" for usage hints.\n",
21480: sqlite3_libversion(), sqlite3_sourceid()
21481: );
21482: if( warnInmemoryDb ){
21483: printf("Connected to a ");
21484: printBold("transient in-memory database");
21485: printf(".\nUse \".open FILENAME\" to reopen on a "
21486: "persistent database.\n");
21487: }
21488: zHistory = getenv("SQLITE_HISTORY");
21489: if( zHistory ){
21490: zHistory = strdup(zHistory);
21491: }else if( (zHome = find_home_dir(0))!=0 ){
21492: nHistory = strlen30(zHome) + 20;
21493: if( (zHistory = malloc(nHistory))!=0 ){
21494: sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
21495: }
21496: }
21497: if( zHistory ){ shell_read_history(zHistory); }
21498: #if HAVE_READLINE || HAVE_EDITLINE
21499: rl_attempted_completion_function = readline_completion;
21500: #elif HAVE_LINENOISE
21501: linenoiseSetCompletionCallback(linenoise_completion);
21502: #endif
21503: data.in = 0;
21504: rc = process_input(&data);
21505: if( zHistory ){
21506: shell_stifle_history(2000);
21507: shell_write_history(zHistory);
21508: free(zHistory);
21509: }
21510: }else{
21511: data.in = stdin;
21512: rc = process_input(&data);
21513: }
21514: }
21515: free(azCmd);
21516: set_table_name(&data, 0);
21517: if( data.db ){
21518: session_close_all(&data);
21519: close_db(data.db);
21520: }
21521: sqlite3_free(data.zFreeOnClose);
21522: find_home_dir(1);
21523: output_reset(&data);
21524: data.doXdgOpen = 0;
21525: clearTempFile(&data);
21526: #if !SQLITE_SHELL_IS_UTF8
21527: for(i=0; i<argcToFree; i++) free(argvToFree[i]);
21528: free(argvToFree);
21529: #endif
21530: free(data.colWidth);
21531: /* Clear the global data structure so that valgrind will detect memory
21532: ** leaks */
21533: memset(&data, 0, sizeof(data));
21534: return rc;
21535: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>