2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl. That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree. That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
22 ** The author disclaims copyright to this source code. In place of
23 ** a legal notice, here is a blessing:
25 ** May you do good and not evil.
26 ** May you find forgiveness for yourself and forgive others.
27 ** May you share freely, never taking more than you give.
29 *************************************************************************
30 ** This file contains code to implement the "sqlite" command line
31 ** utility for accessing SQLite databases.
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
37 typedef unsigned int u32
;
38 typedef unsigned short int u16
;
41 ** Optionally #include a user-defined header, whereby compilation options
42 ** may be set prior to where they take effect, but after platform setup.
43 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
44 ** file. Note that this macro has a like effect on sqlite3.c compilation.
46 # define SHELL_STRINGIFY_(f) #f
47 # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
48 #ifdef SQLITE_CUSTOM_INCLUDE
49 # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
53 ** Determine if we are dealing with WinRT, which provides only a subset of
54 ** the full Win32 API.
56 #if !defined(SQLITE_OS_WINRT)
57 # define SQLITE_OS_WINRT 0
61 ** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
62 ** somewhat for use as a WASM module in a web browser. This flag
63 ** should only be used when building the "fiddle" web application, as
64 ** the browser-mode build has much different user input requirements
65 ** and this build mode rewires the user input subsystem to account for
70 ** Warning pragmas copied from msvc.h in the core.
73 #pragma warning(disable : 4054)
74 #pragma warning(disable : 4055)
75 #pragma warning(disable : 4100)
76 #pragma warning(disable : 4127)
77 #pragma warning(disable : 4130)
78 #pragma warning(disable : 4152)
79 #pragma warning(disable : 4189)
80 #pragma warning(disable : 4206)
81 #pragma warning(disable : 4210)
82 #pragma warning(disable : 4232)
83 #pragma warning(disable : 4244)
84 #pragma warning(disable : 4305)
85 #pragma warning(disable : 4306)
86 #pragma warning(disable : 4702)
87 #pragma warning(disable : 4706)
88 #endif /* defined(_MSC_VER) */
91 ** No support for loadable extensions in VxWorks.
93 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
94 # define SQLITE_OMIT_LOAD_EXTENSION 1
98 ** Enable large-file support for fopen() and friends on unix.
100 #ifndef SQLITE_DISABLE_LFS
101 # define _LARGE_FILE 1
102 # ifndef _FILE_OFFSET_BITS
103 # define _FILE_OFFSET_BITS 64
105 # define _LARGEFILE_SOURCE 1
108 #if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
110 ** emcc requires _POSIX_SOURCE (or one of several similar defines)
111 ** to expose strdup().
113 # define _POSIX_SOURCE
122 typedef sqlite3_int64 i64
;
123 typedef sqlite3_uint64 u64
;
124 typedef unsigned char u8
;
125 #if SQLITE_USER_AUTHENTICATION
126 # include "sqlite3userauth.h"
131 #if !defined(_WIN32) && !defined(WIN32)
133 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
137 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
140 # define GETPID getpid
141 # if defined(__MINGW32__)
142 # define DIRENT dirent
144 # define S_ISLNK(mode) (0)
148 # define GETPID (int)GetCurrentProcessId
150 #include <sys/types.h>
151 #include <sys/stat.h>
154 # include <readline/readline.h>
155 # include <readline/history.h>
159 # include <editline/readline.h>
162 #if HAVE_EDITLINE || HAVE_READLINE
164 # define shell_add_history(X) add_history(X)
165 # define shell_read_history(X) read_history(X)
166 # define shell_write_history(X) write_history(X)
167 # define shell_stifle_history(X) stifle_history(X)
168 # define shell_readline(X) readline(X)
172 # include "linenoise.h"
173 # define shell_add_history(X) linenoiseHistoryAdd(X)
174 # define shell_read_history(X) linenoiseHistoryLoad(X)
175 # define shell_write_history(X) linenoiseHistorySave(X)
176 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
177 # define shell_readline(X) linenoise(X)
181 # define shell_read_history(X)
182 # define shell_write_history(X)
183 # define shell_stifle_history(X)
185 # define SHELL_USE_LOCAL_GETLINE 1
188 #ifndef deliberate_fall_through
189 /* Quiet some compilers about some of our intentional code. */
190 # if defined(GCC_VERSION) && GCC_VERSION>=7000000
191 # define deliberate_fall_through __attribute__((fallthrough));
193 # define deliberate_fall_through
197 #if defined(_WIN32) || defined(WIN32)
199 # define SQLITE_OMIT_POPEN 1
203 # define isatty(h) _isatty(h)
205 # define access(f,m) _access((f),(m))
208 # define unlink _unlink
211 # define strdup _strdup
214 # define popen _popen
216 # define pclose _pclose
219 /* Make sure isatty() has a prototype. */
220 extern int isatty(int);
222 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
223 /* popen and pclose are not C89 functions and so are
224 ** sometimes omitted from the <stdio.h> header */
225 extern FILE *popen(const char*,const char*);
226 extern int pclose(FILE*);
228 # define SQLITE_OMIT_POPEN 1
232 #if defined(_WIN32_WCE)
233 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
234 * thus we always assume that we have a console. That can be
235 * overridden with the -batch command line option.
240 /* ctype macros that work with signed characters */
241 #define IsSpace(X) isspace((unsigned char)X)
242 #define IsDigit(X) isdigit((unsigned char)X)
243 #define ToLower(X) (char)tolower((unsigned char)X)
245 #if defined(_WIN32) || defined(WIN32)
249 #undef WIN32_LEAN_AND_MEAN
250 #define WIN32_LEAN_AND_MEAN
253 /* string conversion routines only needed on Win32 */
254 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR
);
255 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
256 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
257 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char *zText
);
260 /* On Windows, we normally run with output mode of TEXT so that \n characters
261 ** are automatically translated into \r\n. However, this behavior needs
262 ** to be disabled in some cases (ex: when generating CSV output and when
263 ** rendering quoted strings that contain \n characters). The following
264 ** routines take care of that.
266 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
267 static void setBinaryMode(FILE *file
, int isOutput
){
268 if( isOutput
) fflush(file
);
269 _setmode(_fileno(file
), _O_BINARY
);
271 static void setTextMode(FILE *file
, int isOutput
){
272 if( isOutput
) fflush(file
);
273 _setmode(_fileno(file
), _O_TEXT
);
276 # define setBinaryMode(X,Y)
277 # define setTextMode(X,Y)
280 /* True if the timer is enabled */
281 static int enableTimer
= 0;
283 /* A version of strcmp() that works with NULL values */
284 static int cli_strcmp(const char *a
, const char *b
){
289 static int cli_strncmp(const char *a
, const char *b
, size_t n
){
292 return strncmp(a
,b
,n
);
295 /* Return the current wall-clock time */
296 static sqlite3_int64
timeOfDay(void){
297 static sqlite3_vfs
*clockVfs
= 0;
299 if( clockVfs
==0 ) clockVfs
= sqlite3_vfs_find(0);
300 if( clockVfs
==0 ) return 0; /* Never actually happens */
301 if( clockVfs
->iVersion
>=2 && clockVfs
->xCurrentTimeInt64
!=0 ){
302 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
305 clockVfs
->xCurrentTime(clockVfs
, &r
);
306 t
= (sqlite3_int64
)(r
*86400000.0);
311 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
312 #include <sys/time.h>
313 #include <sys/resource.h>
315 /* VxWorks does not support getrusage() as far as we can determine */
316 #if defined(_WRS_KERNEL) || defined(__RTP__)
318 struct timeval ru_utime
; /* user CPU time used */
319 struct timeval ru_stime
; /* system CPU time used */
321 #define getrusage(A,B) memset(B,0,sizeof(*B))
324 /* Saved resource information for the beginning of an operation */
325 static struct rusage sBegin
; /* CPU time at start */
326 static sqlite3_int64 iBegin
; /* Wall-clock time at start */
329 ** Begin timing an operation
331 static void beginTimer(void){
333 getrusage(RUSAGE_SELF
, &sBegin
);
334 iBegin
= timeOfDay();
338 /* Return the difference of two time_structs in seconds */
339 static double timeDiff(struct timeval
*pStart
, struct timeval
*pEnd
){
340 return (pEnd
->tv_usec
- pStart
->tv_usec
)*0.000001 +
341 (double)(pEnd
->tv_sec
- pStart
->tv_sec
);
345 ** Print the timing results.
347 static void endTimer(void){
349 sqlite3_int64 iEnd
= timeOfDay();
351 getrusage(RUSAGE_SELF
, &sEnd
);
352 printf("Run Time: real %.3f user %f sys %f\n",
353 (iEnd
- iBegin
)*0.001,
354 timeDiff(&sBegin
.ru_utime
, &sEnd
.ru_utime
),
355 timeDiff(&sBegin
.ru_stime
, &sEnd
.ru_stime
));
359 #define BEGIN_TIMER beginTimer()
360 #define END_TIMER endTimer()
363 #elif (defined(_WIN32) || defined(WIN32))
365 /* Saved resource information for the beginning of an operation */
366 static HANDLE hProcess
;
367 static FILETIME ftKernelBegin
;
368 static FILETIME ftUserBegin
;
369 static sqlite3_int64 ftWallBegin
;
370 typedef BOOL (WINAPI
*GETPROCTIMES
)(HANDLE
, LPFILETIME
, LPFILETIME
,
371 LPFILETIME
, LPFILETIME
);
372 static GETPROCTIMES getProcessTimesAddr
= NULL
;
375 ** Check to see if we have timer support. Return 1 if necessary
376 ** support found (or found previously).
378 static int hasTimer(void){
379 if( getProcessTimesAddr
){
383 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
384 ** versions. See if the version we are running on has it, and if it
385 ** does, save off a pointer to it and the current process handle.
387 hProcess
= GetCurrentProcess();
389 HINSTANCE hinstLib
= LoadLibrary(TEXT("Kernel32.dll"));
390 if( NULL
!= hinstLib
){
391 getProcessTimesAddr
=
392 (GETPROCTIMES
) GetProcAddress(hinstLib
, "GetProcessTimes");
393 if( NULL
!= getProcessTimesAddr
){
396 FreeLibrary(hinstLib
);
405 ** Begin timing an operation
407 static void beginTimer(void){
408 if( enableTimer
&& getProcessTimesAddr
){
409 FILETIME ftCreation
, ftExit
;
410 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,
411 &ftKernelBegin
,&ftUserBegin
);
412 ftWallBegin
= timeOfDay();
416 /* Return the difference of two FILETIME structs in seconds */
417 static double timeDiff(FILETIME
*pStart
, FILETIME
*pEnd
){
418 sqlite_int64 i64Start
= *((sqlite_int64
*) pStart
);
419 sqlite_int64 i64End
= *((sqlite_int64
*) pEnd
);
420 return (double) ((i64End
- i64Start
) / 10000000.0);
424 ** Print the timing results.
426 static void endTimer(void){
427 if( enableTimer
&& getProcessTimesAddr
){
428 FILETIME ftCreation
, ftExit
, ftKernelEnd
, ftUserEnd
;
429 sqlite3_int64 ftWallEnd
= timeOfDay();
430 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,&ftKernelEnd
,&ftUserEnd
);
431 printf("Run Time: real %.3f user %f sys %f\n",
432 (ftWallEnd
- ftWallBegin
)*0.001,
433 timeDiff(&ftUserBegin
, &ftUserEnd
),
434 timeDiff(&ftKernelBegin
, &ftKernelEnd
));
438 #define BEGIN_TIMER beginTimer()
439 #define END_TIMER endTimer()
440 #define HAS_TIMER hasTimer()
449 ** Used to prevent warnings about unused parameters
451 #define UNUSED_PARAMETER(x) (void)(x)
454 ** Number of elements in an array
456 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
459 ** If the following flag is set, then command execution stops
460 ** at an error if we are not interactive.
462 static int bail_on_error
= 0;
465 ** Treat stdin as an interactive input if the following variable
466 ** is true. Otherwise, assume stdin is connected to a file or pipe.
468 static int stdin_is_interactive
= 1;
470 #if (defined(_WIN32) || defined(WIN32)) && SHELL_USE_LOCAL_GETLINE \
471 && !defined(SHELL_OMIT_WIN_UTF8)
472 # define SHELL_WIN_UTF8_OPT 1
474 # define SHELL_WIN_UTF8_OPT 0
477 #if SHELL_WIN_UTF8_OPT
479 ** Setup console for UTF-8 input/output when following variable true.
481 static int console_utf8
= 0;
485 ** On Windows systems we have to know if standard output is a console
486 ** in order to translate UTF-8 into MBCS. The following variable is
487 ** true if translation is required.
489 static int stdout_is_console
= 1;
492 ** The following is the open SQLite database. We make a pointer
493 ** to this database a static variable so that it can be accessed
494 ** by the SIGINT handler to interrupt database processing.
496 static sqlite3
*globalDb
= 0;
499 ** True if an interrupt (Control-C) has been received.
501 static volatile int seenInterrupt
= 0;
504 ** This is the name of our program. It is set in main(), used
505 ** in a number of other places, mostly for error messages.
510 ** Prompt strings. Initialized in main. Settable with
511 ** .prompt main continue
513 #define PROMPT_LEN_MAX 20
514 /* First line prompt. default: "sqlite> " */
515 static char mainPrompt
[PROMPT_LEN_MAX
];
516 /* Continuation prompt. default: " ...> " */
517 static char continuePrompt
[PROMPT_LEN_MAX
];
519 /* This is variant of the standard-library strncpy() routine with the
520 ** one change that the destination string is always zero-terminated, even
521 ** if there is no zero-terminator in the first n-1 characters of the source
524 static char *shell_strncpy(char *dest
, const char *src
, size_t n
){
526 for(i
=0; i
<n
-1 && src
[i
]!=0; i
++) dest
[i
] = src
[i
];
532 ** Optionally disable dynamic continuation prompt.
533 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
534 ** or open parentheses level if non-zero, or continuation prompt as set.
535 ** This facility interacts with the scanner and process_input() where the
536 ** below 5 macros are used.
538 #ifdef SQLITE_OMIT_DYNAPROMPT
539 # define CONTINUATION_PROMPT continuePrompt
540 # define CONTINUE_PROMPT_RESET
541 # define CONTINUE_PROMPT_AWAITS(p,s)
542 # define CONTINUE_PROMPT_AWAITC(p,c)
543 # define CONTINUE_PAREN_INCR(p,n)
544 # define CONTINUE_PROMPT_PSTATE 0
545 typedef void *t_NoDynaPrompt
;
546 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
548 # define CONTINUATION_PROMPT dynamicContinuePrompt()
549 # define CONTINUE_PROMPT_RESET \
550 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
551 # define CONTINUE_PROMPT_AWAITS(p,s) \
552 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
553 # define CONTINUE_PROMPT_AWAITC(p,c) \
554 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
555 # define CONTINUE_PAREN_INCR(p,n) \
556 if(p && stdin_is_interactive) (trackParenLevel(p,n))
557 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
558 typedef struct DynaPrompt
*t_DynaPromptRef
;
559 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
561 static struct DynaPrompt
{
562 char dynamicPrompt
[PROMPT_LEN_MAX
];
565 char *zScannerAwaits
;
566 } dynPrompt
= { {0}, {0}, 0, 0 };
568 /* Record parenthesis nesting level change, or force level to 0. */
569 static void trackParenLevel(struct DynaPrompt
*p
, int ni
){
570 p
->inParenLevel
+= ni
;
571 if( ni
==0 ) p
->inParenLevel
= 0;
572 p
->zScannerAwaits
= 0;
575 /* Record that a lexeme is opened, or closed with args==0. */
576 static void setLexemeOpen(struct DynaPrompt
*p
, char *s
, char c
){
578 p
->zScannerAwaits
= s
;
582 p
->zScannerAwaits
= p
->acAwait
;
586 /* Upon demand, derive the continuation prompt to display. */
587 static char *dynamicContinuePrompt(void){
588 if( continuePrompt
[0]==0
589 || (dynPrompt
.zScannerAwaits
==0 && dynPrompt
.inParenLevel
== 0) ){
590 return continuePrompt
;
592 if( dynPrompt
.zScannerAwaits
){
593 size_t ncp
= strlen(continuePrompt
);
594 size_t ndp
= strlen(dynPrompt
.zScannerAwaits
);
595 if( ndp
> ncp
-3 ) return continuePrompt
;
596 strcpy(dynPrompt
.dynamicPrompt
, dynPrompt
.zScannerAwaits
);
597 while( ndp
<3 ) dynPrompt
.dynamicPrompt
[ndp
++] = ' ';
598 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3,
601 if( dynPrompt
.inParenLevel
>9 ){
602 shell_strncpy(dynPrompt
.dynamicPrompt
, "(..", 4);
603 }else if( dynPrompt
.inParenLevel
<0 ){
604 shell_strncpy(dynPrompt
.dynamicPrompt
, ")x!", 4);
606 shell_strncpy(dynPrompt
.dynamicPrompt
, "(x.", 4);
607 dynPrompt
.dynamicPrompt
[2] = (char)('0'+dynPrompt
.inParenLevel
);
609 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3, PROMPT_LEN_MAX
-4);
612 return dynPrompt
.dynamicPrompt
;
614 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
616 #if SHELL_WIN_UTF8_OPT
617 /* Following struct is used for -utf8 operation. */
618 static struct ConsoleState
{
619 int stdinEof
; /* EOF has been seen on console input */
620 int infsMode
; /* Input file stream mode upon shell start */
621 UINT inCodePage
; /* Input code page upon shell start */
622 UINT outCodePage
; /* Output code page upon shell start */
623 HANDLE hConsoleIn
; /* Console input handle */
624 DWORD consoleMode
; /* Console mode upon shell start */
625 } conState
= { 0, 0, 0, 0, INVALID_HANDLE_VALUE
, 0 };
627 #ifndef _O_U16TEXT /* For build environments lacking this constant: */
628 # define _O_U16TEXT 0x20000
632 ** Prepare console, (if known to be a WIN32 console), for UTF-8
633 ** input (from either typing or suitable paste operations) and for
634 ** UTF-8 rendering. This may "fail" with a message to stderr, where
635 ** the preparation is not done and common "code page" issues occur.
637 static void console_prepare(void){
638 HANDLE hCI
= GetStdHandle(STD_INPUT_HANDLE
);
639 DWORD consoleMode
= 0;
640 if( isatty(0) && GetFileType(hCI
)==FILE_TYPE_CHAR
641 && GetConsoleMode( hCI
, &consoleMode
) ){
642 if( !IsValidCodePage(CP_UTF8
) ){
643 fprintf(stderr
, "Cannot use UTF-8 code page.\n");
647 conState
.hConsoleIn
= hCI
;
648 conState
.consoleMode
= consoleMode
;
649 conState
.inCodePage
= GetConsoleCP();
650 conState
.outCodePage
= GetConsoleOutputCP();
651 SetConsoleCP(CP_UTF8
);
652 SetConsoleOutputCP(CP_UTF8
);
653 consoleMode
|= ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
;
654 SetConsoleMode(conState
.hConsoleIn
, consoleMode
);
655 conState
.infsMode
= _setmode(_fileno(stdin
), _O_U16TEXT
);
663 ** Undo the effects of console_prepare(), if any.
665 static void SQLITE_CDECL
console_restore(void){
666 if( console_utf8
&& conState
.inCodePage
!=0
667 && conState
.hConsoleIn
!=INVALID_HANDLE_VALUE
){
668 _setmode(_fileno(stdin
), conState
.infsMode
);
669 SetConsoleCP(conState
.inCodePage
);
670 SetConsoleOutputCP(conState
.outCodePage
);
671 SetConsoleMode(conState
.hConsoleIn
, conState
.consoleMode
);
672 /* Avoid multiple calls. */
673 conState
.hConsoleIn
= INVALID_HANDLE_VALUE
;
674 conState
.consoleMode
= 0;
680 ** Collect input like fgets(...) with special provisions for input
681 ** from the Windows console to get around its strange coding issues.
682 ** Defers to plain fgets() when input is not interactive or when the
683 ** startup option, -utf8, has not been provided or taken effect.
685 static char* utf8_fgets(char *buf
, int ncmax
, FILE *fin
){
686 if( fin
==0 ) fin
= stdin
;
687 if( fin
==stdin
&& stdin_is_interactive
&& console_utf8
){
688 # define SQLITE_IALIM 150
689 wchar_t wbuf
[SQLITE_IALIM
];
692 if( ncmax
==0 || conState
.stdinEof
) return 0;
694 while( noc
<ncmax
-7-1 && !lend
){
695 /* There is room for at least 2 more characters and a 0-terminator. */
696 int na
= (ncmax
> SQLITE_IALIM
*4+1 + noc
)
697 ? SQLITE_IALIM
: (ncmax
-1 - noc
)/4;
700 BOOL bRC
= ReadConsoleW(conState
.hConsoleIn
, wbuf
, na
, &nbr
, 0);
701 if( !bRC
|| (noc
==0 && nbr
==0) ) return 0;
703 int nmb
= WideCharToMultiByte(CP_UTF8
,WC_COMPOSITECHECK
|WC_DEFAULTCHAR
,
705 if( nmb
!=0 && noc
+nmb
<= ncmax
){
707 nmb
= WideCharToMultiByte(CP_UTF8
,WC_COMPOSITECHECK
|WC_DEFAULTCHAR
,
708 wbuf
,nbr
,buf
+noc
,nmb
,0,0);
710 /* Fixup line-ends as coded by Windows for CR (or "Enter".)*/
712 if( buf
[noc
-1]=='\n' ){
714 if( noc
> 1 && buf
[noc
-2]=='\r' ){
720 /* Check for ^Z (anywhere in line) too. */
722 if( buf
[iseg
]==0x1a ){
723 conState
.stdinEof
= 1;
724 noc
= iseg
; /* Chop ^Z and anything following. */
729 }else break; /* Drop apparent garbage in. (Could assert.) */
732 /* If got nothing, (after ^Z chop), must be at end-of-file. */
733 if( noc
== 0 ) return 0;
737 return fgets(buf
, ncmax
, fin
);
741 # define fgets(b,n,f) utf8_fgets(b,n,f)
742 #endif /* SHELL_WIN_UTF8_OPT */
745 ** Render output like fprintf(). Except, if the output is going to the
746 ** console and if this is running on a Windows machine, and if the -utf8
747 ** option is unavailable or (available and inactive), translate the
748 ** output from UTF-8 into MBCS for output through 8-bit stdout stream.
749 ** (With -utf8 active, no translation is needed and must not be done.)
751 #if defined(_WIN32) || defined(WIN32)
752 void utf8_printf(FILE *out
, const char *zFormat
, ...){
754 va_start(ap
, zFormat
);
755 if( stdout_is_console
&& (out
==stdout
|| out
==stderr
)
756 # if SHELL_WIN_UTF8_OPT
760 char *z1
= sqlite3_vmprintf(zFormat
, ap
);
761 char *z2
= sqlite3_win32_utf8_to_mbcs_v2(z1
, 0);
766 vfprintf(out
, zFormat
, ap
);
770 #elif !defined(utf8_printf)
771 # define utf8_printf fprintf
775 ** Render output like fprintf(). This should not be used on anything that
776 ** includes string formatting (e.g. "%s").
778 #if !defined(raw_printf)
779 # define raw_printf fprintf
782 /* Indicate out-of-memory and exit. */
783 static void shell_out_of_memory(void){
784 raw_printf(stderr
,"Error: out of memory\n");
788 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
789 ** out-of-memory error.
791 static void shell_check_oom(const void *p
){
792 if( p
==0 ) shell_out_of_memory();
796 ** Write I/O traces to the following stream.
798 #ifdef SQLITE_ENABLE_IOTRACE
799 static FILE *iotrace
= 0;
803 ** This routine works like printf in that its first argument is a
804 ** format string and subsequent arguments are values to be substituted
805 ** in place of % fields. The result of formatting this string
806 ** is written to iotrace.
808 #ifdef SQLITE_ENABLE_IOTRACE
809 static void SQLITE_CDECL
iotracePrintf(const char *zFormat
, ...){
812 if( iotrace
==0 ) return;
813 va_start(ap
, zFormat
);
814 z
= sqlite3_vmprintf(zFormat
, ap
);
816 utf8_printf(iotrace
, "%s", z
);
822 ** Output string zUtf to stream pOut as w characters. If w is negative,
823 ** then right-justify the text. W is the width in UTF-8 characters, not
824 ** in bytes. This is different from the %*.*s specification in printf
825 ** since with %*.*s the width is measured in bytes, not characters.
827 static void utf8_width_print(FILE *pOut
, int w
, const char *zUtf
){
830 int aw
= w
<0 ? -w
: w
;
831 if( zUtf
==0 ) zUtf
= "";
832 for(i
=n
=0; zUtf
[i
]; i
++){
833 if( (zUtf
[i
]&0xc0)!=0x80 ){
836 do{ i
++; }while( (zUtf
[i
]&0xc0)==0x80 );
842 utf8_printf(pOut
, "%.*s", i
, zUtf
);
844 utf8_printf(pOut
, "%*s%s", aw
-n
, "", zUtf
);
846 utf8_printf(pOut
, "%s%*s", zUtf
, aw
-n
, "");
852 ** Determines if a string is a number of not.
854 static int isNumber(const char *z
, int *realnum
){
855 if( *z
=='-' || *z
=='+' ) z
++;
860 if( realnum
) *realnum
= 0;
861 while( IsDigit(*z
) ){ z
++; }
864 if( !IsDigit(*z
) ) return 0;
865 while( IsDigit(*z
) ){ z
++; }
866 if( realnum
) *realnum
= 1;
868 if( *z
=='e' || *z
=='E' ){
870 if( *z
=='+' || *z
=='-' ) z
++;
871 if( !IsDigit(*z
) ) return 0;
872 while( IsDigit(*z
) ){ z
++; }
873 if( realnum
) *realnum
= 1;
879 ** Compute a string length that is limited to what can be stored in
880 ** lower 30 bits of a 32-bit signed integer.
882 static int strlen30(const char *z
){
884 while( *z2
){ z2
++; }
885 return 0x3fffffff & (int)(z2
- z
);
889 ** Return the length of a string in characters. Multibyte UTF8 characters
890 ** count as a single character.
892 static int strlenChar(const char *z
){
895 if( (0xc0&*(z
++))!=0x80 ) n
++;
901 ** Return open FILE * if zFile exists, can be opened for read
902 ** and is an ordinary file or a character stream source.
903 ** Otherwise return 0.
905 static FILE * openChrSource(const char *zFile
){
907 struct _stat x
= {0};
908 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
909 /* On Windows, open first, then check the stream nature. This order
910 ** is necessary because _stat() and sibs, when checking a named pipe,
911 ** effectively break the pipe as its supplier sees it. */
912 FILE *rv
= fopen(zFile
, "rb");
913 if( rv
==0 ) return 0;
914 if( _fstat(_fileno(rv
), &x
) != 0
915 || !STAT_CHR_SRC(x
.st_mode
)){
922 int rc
= stat(zFile
, &x
);
923 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
924 if( rc
!=0 ) return 0;
925 if( STAT_CHR_SRC(x
.st_mode
) ){
926 return fopen(zFile
, "rb");
935 ** This routine reads a line of text from FILE in, stores
936 ** the text in memory obtained from malloc() and returns a pointer
937 ** to the text. NULL is returned at end of file, or if malloc()
940 ** If zLine is not NULL then it is a malloced buffer returned from
941 ** a previous call to this routine that may be reused.
943 static char *local_getline(char *zLine
, FILE *in
){
944 int nLine
= zLine
==0 ? 0 : 100;
949 nLine
= nLine
*2 + 100;
950 zLine
= realloc(zLine
, nLine
);
951 shell_check_oom(zLine
);
953 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
961 while( zLine
[n
] ) n
++;
962 if( n
>0 && zLine
[n
-1]=='\n' ){
964 if( n
>0 && zLine
[n
-1]=='\r' ) n
--;
969 #if defined(_WIN32) || defined(WIN32)
970 /* For interactive input on Windows systems, without -utf8,
971 ** translate the multi-byte characterset characters into UTF-8.
972 ** This is the translation that predates the -utf8 option. */
973 if( stdin_is_interactive
&& in
==stdin
974 # if SHELL_WIN_UTF8_OPT
976 # endif /* SHELL_WIN_UTF8_OPT */
978 char *zTrans
= sqlite3_win32_mbcs_to_utf8_v2(zLine
, 0);
980 i64 nTrans
= strlen(zTrans
)+1;
982 zLine
= realloc(zLine
, nTrans
);
983 shell_check_oom(zLine
);
985 memcpy(zLine
, zTrans
, nTrans
);
986 sqlite3_free(zTrans
);
989 #endif /* defined(_WIN32) || defined(WIN32) */
994 ** Retrieve a single line of input text.
996 ** If in==0 then read from standard input and prompt before each line.
997 ** If isContinuation is true, then a continuation prompt is appropriate.
998 ** If isContinuation is zero, then the main prompt should be used.
1000 ** If zPrior is not NULL then it is a buffer from a prior call to this
1001 ** routine that can be reused.
1003 ** The result is stored in space obtained from malloc() and must either
1004 ** be freed by the caller or else passed back into this routine via the
1005 ** zPrior argument for reuse.
1007 #ifndef SQLITE_SHELL_FIDDLE
1008 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
1012 zResult
= local_getline(zPrior
, in
);
1014 zPrompt
= isContinuation
? CONTINUATION_PROMPT
: mainPrompt
;
1015 #if SHELL_USE_LOCAL_GETLINE
1016 printf("%s", zPrompt
);
1019 zResult
= local_getline(zPrior
, stdin
);
1021 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1022 if( zResult
==0 ) sqlite3_sleep(50);
1023 }while( zResult
==0 && seenInterrupt
>0 );
1026 zResult
= shell_readline(zPrompt
);
1027 while( zResult
==0 ){
1028 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1030 if( seenInterrupt
==0 ) break;
1031 zResult
= shell_readline("");
1033 if( zResult
&& *zResult
) shell_add_history(zResult
);
1038 #endif /* !SQLITE_SHELL_FIDDLE */
1041 ** Return the value of a hexadecimal digit. Return -1 if the input
1042 ** is not a hex digit.
1044 static int hexDigitValue(char c
){
1045 if( c
>='0' && c
<='9' ) return c
- '0';
1046 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
1047 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
1052 ** Interpret zArg as an integer value, possibly with suffixes.
1054 static sqlite3_int64
integerValue(const char *zArg
){
1055 sqlite3_int64 v
= 0;
1056 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
1058 { "MiB", 1024*1024 },
1059 { "GiB", 1024*1024*1024 },
1062 { "GB", 1000000000 },
1065 { "G", 1000000000 },
1072 }else if( zArg
[0]=='+' ){
1075 if( zArg
[0]=='0' && zArg
[1]=='x' ){
1078 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
1083 while( IsDigit(zArg
[0]) ){
1084 v
= v
*10 + zArg
[0] - '0';
1088 for(i
=0; i
<ArraySize(aMult
); i
++){
1089 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
1090 v
*= aMult
[i
].iMult
;
1094 return isNeg
? -v
: v
;
1098 ** A variable length string to which one can append text.
1100 typedef struct ShellText ShellText
;
1108 ** Initialize and destroy a ShellText object
1110 static void initText(ShellText
*p
){
1111 memset(p
, 0, sizeof(*p
));
1113 static void freeText(ShellText
*p
){
1118 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1119 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1120 ** added to zIn, and the result returned in memory obtained from malloc().
1121 ** zIn, if it was not NULL, is freed.
1123 ** If the third argument, quote, is not '\0', then it is used as a
1124 ** quote character for zAppend.
1126 static void appendText(ShellText
*p
, const char *zAppend
, char quote
){
1129 i64 nAppend
= strlen30(zAppend
);
1131 len
= nAppend
+p
->n
+1;
1134 for(i
=0; i
<nAppend
; i
++){
1135 if( zAppend
[i
]==quote
) len
++;
1139 if( p
->z
==0 || p
->n
+len
>=p
->nAlloc
){
1140 p
->nAlloc
= p
->nAlloc
*2 + len
+ 20;
1141 p
->z
= realloc(p
->z
, p
->nAlloc
);
1142 shell_check_oom(p
->z
);
1146 char *zCsr
= p
->z
+p
->n
;
1148 for(i
=0; i
<nAppend
; i
++){
1149 *zCsr
++ = zAppend
[i
];
1150 if( zAppend
[i
]==quote
) *zCsr
++ = quote
;
1153 p
->n
= (int)(zCsr
- p
->z
);
1156 memcpy(p
->z
+p
->n
, zAppend
, nAppend
);
1163 ** Attempt to determine if identifier zName needs to be quoted, either
1164 ** because it contains non-alphanumeric characters, or because it is an
1165 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1166 ** that quoting is required.
1168 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1170 static char quoteChar(const char *zName
){
1172 if( zName
==0 ) return '"';
1173 if( !isalpha((unsigned char)zName
[0]) && zName
[0]!='_' ) return '"';
1174 for(i
=0; zName
[i
]; i
++){
1175 if( !isalnum((unsigned char)zName
[i
]) && zName
[i
]!='_' ) return '"';
1177 return sqlite3_keyword_check(zName
, i
) ? '"' : 0;
1181 ** Construct a fake object name and column list to describe the structure
1182 ** of the view, virtual table, or table valued function zSchema.zName.
1184 static char *shellFakeSchema(
1185 sqlite3
*db
, /* The database connection containing the vtab */
1186 const char *zSchema
, /* Schema of the database holding the vtab */
1187 const char *zName
/* The name of the virtual table */
1189 sqlite3_stmt
*pStmt
= 0;
1196 zSql
= sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1197 zSchema
? zSchema
: "main", zName
);
1198 shell_check_oom(zSql
);
1199 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
1203 cQuote
= quoteChar(zSchema
);
1204 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")==0 ) cQuote
= 0;
1205 appendText(&s
, zSchema
, cQuote
);
1206 appendText(&s
, ".", 0);
1208 cQuote
= quoteChar(zName
);
1209 appendText(&s
, zName
, cQuote
);
1210 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
1211 const char *zCol
= (const char*)sqlite3_column_text(pStmt
, 1);
1213 appendText(&s
, zDiv
, 0);
1215 if( zCol
==0 ) zCol
= "";
1216 cQuote
= quoteChar(zCol
);
1217 appendText(&s
, zCol
, cQuote
);
1219 appendText(&s
, ")", 0);
1220 sqlite3_finalize(pStmt
);
1229 ** SQL function: strtod(X)
1231 ** Use the C-library strtod() function to convert string X into a double.
1232 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1233 ** routines against the C-library.
1235 static void shellStrtod(
1236 sqlite3_context
*pCtx
,
1238 sqlite3_value
**apVal
1240 char *z
= (char*)sqlite3_value_text(apVal
[0]);
1241 UNUSED_PARAMETER(nVal
);
1243 sqlite3_result_double(pCtx
, strtod(z
,0));
1247 ** SQL function: dtostr(X)
1249 ** Use the C-library printf() function to convert real value X into a string.
1250 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1251 ** routines against the C-library.
1253 static void shellDtostr(
1254 sqlite3_context
*pCtx
,
1256 sqlite3_value
**apVal
1258 double r
= sqlite3_value_double(apVal
[0]);
1259 int n
= nVal
>=2 ? sqlite3_value_int(apVal
[1]) : 26;
1262 if( n
>350 ) n
= 350;
1263 sprintf(z
, "%#+.*e", n
, r
);
1264 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
1269 ** SQL function: shell_module_schema(X)
1271 ** Return a fake schema for the table-valued function or eponymous virtual
1274 static void shellModuleSchema(
1275 sqlite3_context
*pCtx
,
1277 sqlite3_value
**apVal
1281 UNUSED_PARAMETER(nVal
);
1282 zName
= (const char*)sqlite3_value_text(apVal
[0]);
1283 zFake
= zName
? shellFakeSchema(sqlite3_context_db_handle(pCtx
), 0, zName
) : 0;
1285 sqlite3_result_text(pCtx
, sqlite3_mprintf("/* %s */", zFake
),
1292 ** SQL function: shell_add_schema(S,X)
1294 ** Add the schema name X to the CREATE statement in S and return the result.
1297 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1302 ** CREATE UNIQUE INDEX
1305 ** CREATE VIRTUAL TABLE
1307 ** This UDF is used by the .schema command to insert the schema name of
1308 ** attached databases into the middle of the sqlite_schema.sql field.
1310 static void shellAddSchemaName(
1311 sqlite3_context
*pCtx
,
1313 sqlite3_value
**apVal
1315 static const char *aPrefix
[] = {
1324 const char *zIn
= (const char*)sqlite3_value_text(apVal
[0]);
1325 const char *zSchema
= (const char*)sqlite3_value_text(apVal
[1]);
1326 const char *zName
= (const char*)sqlite3_value_text(apVal
[2]);
1327 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
1328 UNUSED_PARAMETER(nVal
);
1329 if( zIn
!=0 && cli_strncmp(zIn
, "CREATE ", 7)==0 ){
1330 for(i
=0; i
<ArraySize(aPrefix
); i
++){
1331 int n
= strlen30(aPrefix
[i
]);
1332 if( cli_strncmp(zIn
+7, aPrefix
[i
], n
)==0 && zIn
[n
+7]==' ' ){
1336 char cQuote
= quoteChar(zSchema
);
1337 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")!=0 ){
1338 z
= sqlite3_mprintf("%.*s \"%w\".%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1340 z
= sqlite3_mprintf("%.*s %s.%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1344 && aPrefix
[i
][0]=='V'
1345 && (zFake
= shellFakeSchema(db
, zSchema
, zName
))!=0
1348 z
= sqlite3_mprintf("%s\n/* %s */", zIn
, zFake
);
1350 z
= sqlite3_mprintf("%z\n/* %s */", z
, zFake
);
1355 sqlite3_result_text(pCtx
, z
, -1, sqlite3_free
);
1361 sqlite3_result_value(pCtx
, apVal
[0]);
1365 ** The source code for several run-time loadable extensions is inserted
1366 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1367 ** code, we need to override some macros to make the included program code
1368 ** work here in the middle of this regular program.
1370 #define SQLITE_EXTENSION_INIT1
1371 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1373 #if defined(_WIN32) && defined(_MSC_VER)
1374 /************************* Begin test_windirent.h ******************/
1378 ** The author disclaims copyright to this source code. In place of
1379 ** a legal notice, here is a blessing:
1381 ** May you do good and not evil.
1382 ** May you find forgiveness for yourself and forgive others.
1383 ** May you share freely, never taking more than you give.
1385 *************************************************************************
1386 ** This file contains declarations for most of the opendir() family of
1387 ** POSIX functions on Win32 using the MSVCRT.
1390 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1391 #define SQLITE_WINDIRENT_H
1394 ** We need several data types from the Windows SDK header.
1397 #ifndef WIN32_LEAN_AND_MEAN
1398 #define WIN32_LEAN_AND_MEAN
1401 #include "windows.h"
1404 ** We need several support functions from the SQLite core.
1407 /* #include "sqlite3.h" */
1410 ** We need several things from the ANSI and MSVCRT headers.
1418 #include <sys/types.h>
1419 #include <sys/stat.h>
1422 ** We may need several defines that should have been in "sys/stat.h".
1426 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1430 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1434 #define S_ISLNK(mode) (0)
1438 ** We may need to provide the "mode_t" type.
1441 #ifndef MODE_T_DEFINED
1442 #define MODE_T_DEFINED
1443 typedef unsigned short mode_t
;
1447 ** We may need to provide the "ino_t" type.
1450 #ifndef INO_T_DEFINED
1451 #define INO_T_DEFINED
1452 typedef unsigned short ino_t
;
1456 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1460 # ifdef FILENAME_MAX
1461 # define NAME_MAX (FILENAME_MAX)
1463 # define NAME_MAX (260)
1468 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1471 #ifndef NULL_INTPTR_T
1472 # define NULL_INTPTR_T ((intptr_t)(0))
1475 #ifndef BAD_INTPTR_T
1476 # define BAD_INTPTR_T ((intptr_t)(-1))
1480 ** We need to provide the necessary structures and related types.
1483 #ifndef DIRENT_DEFINED
1484 #define DIRENT_DEFINED
1485 typedef struct DIRENT DIRENT
;
1486 typedef DIRENT
*LPDIRENT
;
1488 ino_t d_ino
; /* Sequence number, do not use. */
1489 unsigned d_attributes
; /* Win32 file attributes. */
1490 char d_name
[NAME_MAX
+ 1]; /* Name within the directory. */
1496 typedef struct DIR DIR;
1499 intptr_t d_handle
; /* Value returned by "_findfirst". */
1500 DIRENT d_first
; /* DIRENT constructed based on "_findfirst". */
1501 DIRENT d_next
; /* DIRENT constructed based on "_findnext". */
1506 ** Provide a macro, for use by the implementation, to determine if a
1507 ** particular directory entry should be skipped over when searching for
1508 ** the next directory entry that should be returned by the readdir() or
1509 ** readdir_r() functions.
1513 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1517 ** Provide the function prototype for the POSIX compatible getenv()
1518 ** function. This function is not thread-safe.
1521 extern const char *windirent_getenv(const char *name
);
1524 ** Finally, we can provide the function prototypes for the opendir(),
1525 ** readdir(), readdir_r(), and closedir() POSIX functions.
1528 extern LPDIR
opendir(const char *dirname
);
1529 extern LPDIRENT
readdir(LPDIR dirp
);
1530 extern INT
readdir_r(LPDIR dirp
, LPDIRENT entry
, LPDIRENT
*result
);
1531 extern INT
closedir(LPDIR dirp
);
1533 #endif /* defined(WIN32) && defined(_MSC_VER) */
1535 /************************* End test_windirent.h ********************/
1536 /************************* Begin test_windirent.c ******************/
1540 ** The author disclaims copyright to this source code. In place of
1541 ** a legal notice, here is a blessing:
1543 ** May you do good and not evil.
1544 ** May you find forgiveness for yourself and forgive others.
1545 ** May you share freely, never taking more than you give.
1547 *************************************************************************
1548 ** This file contains code to implement most of the opendir() family of
1549 ** POSIX functions on Win32 using the MSVCRT.
1552 #if defined(_WIN32) && defined(_MSC_VER)
1553 /* #include "test_windirent.h" */
1556 ** Implementation of the POSIX getenv() function using the Win32 API.
1557 ** This function is not thread-safe.
1559 const char *windirent_getenv(
1562 static char value
[32768]; /* Maximum length, per MSDN */
1563 DWORD dwSize
= sizeof(value
) / sizeof(char); /* Size in chars */
1564 DWORD dwRet
; /* Value returned by GetEnvironmentVariableA() */
1566 memset(value
, 0, sizeof(value
));
1567 dwRet
= GetEnvironmentVariableA(name
, value
, dwSize
);
1568 if( dwRet
==0 || dwRet
>dwSize
){
1570 ** The function call to GetEnvironmentVariableA() failed -OR-
1571 ** the buffer is not large enough. Either way, return NULL.
1576 ** The function call to GetEnvironmentVariableA() succeeded
1577 ** -AND- the buffer contains the entire value.
1584 ** Implementation of the POSIX opendir() function using the MSVCRT.
1589 struct _finddata_t data
;
1590 LPDIR dirp
= (LPDIR
)sqlite3_malloc(sizeof(DIR));
1591 SIZE_T namesize
= sizeof(data
.name
) / sizeof(data
.name
[0]);
1593 if( dirp
==NULL
) return NULL
;
1594 memset(dirp
, 0, sizeof(DIR));
1596 /* TODO: Remove this if Unix-style root paths are not used. */
1597 if( sqlite3_stricmp(dirname
, "/")==0 ){
1598 dirname
= windirent_getenv("SystemDrive");
1601 memset(&data
, 0, sizeof(struct _finddata_t
));
1602 _snprintf(data
.name
, namesize
, "%s\\*", dirname
);
1603 dirp
->d_handle
= _findfirst(data
.name
, &data
);
1605 if( dirp
->d_handle
==BAD_INTPTR_T
){
1610 /* TODO: Remove this block to allow hidden and/or system files. */
1611 if( is_filtered(data
) ){
1614 memset(&data
, 0, sizeof(struct _finddata_t
));
1615 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
1620 /* TODO: Remove this block to allow hidden and/or system files. */
1621 if( is_filtered(data
) ) goto next
;
1624 dirp
->d_first
.d_attributes
= data
.attrib
;
1625 strncpy(dirp
->d_first
.d_name
, data
.name
, NAME_MAX
);
1626 dirp
->d_first
.d_name
[NAME_MAX
] = '\0';
1632 ** Implementation of the POSIX readdir() function using the MSVCRT.
1637 struct _finddata_t data
;
1639 if( dirp
==NULL
) return NULL
;
1641 if( dirp
->d_first
.d_ino
==0 ){
1642 dirp
->d_first
.d_ino
++;
1643 dirp
->d_next
.d_ino
++;
1645 return &dirp
->d_first
;
1650 memset(&data
, 0, sizeof(struct _finddata_t
));
1651 if( _findnext(dirp
->d_handle
, &data
)==-1 ) return NULL
;
1653 /* TODO: Remove this block to allow hidden and/or system files. */
1654 if( is_filtered(data
) ) goto next
;
1656 dirp
->d_next
.d_ino
++;
1657 dirp
->d_next
.d_attributes
= data
.attrib
;
1658 strncpy(dirp
->d_next
.d_name
, data
.name
, NAME_MAX
);
1659 dirp
->d_next
.d_name
[NAME_MAX
] = '\0';
1661 return &dirp
->d_next
;
1665 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1672 struct _finddata_t data
;
1674 if( dirp
==NULL
) return EBADF
;
1676 if( dirp
->d_first
.d_ino
==0 ){
1677 dirp
->d_first
.d_ino
++;
1678 dirp
->d_next
.d_ino
++;
1680 entry
->d_ino
= dirp
->d_first
.d_ino
;
1681 entry
->d_attributes
= dirp
->d_first
.d_attributes
;
1682 strncpy(entry
->d_name
, dirp
->d_first
.d_name
, NAME_MAX
);
1683 entry
->d_name
[NAME_MAX
] = '\0';
1691 memset(&data
, 0, sizeof(struct _finddata_t
));
1692 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
1697 /* TODO: Remove this block to allow hidden and/or system files. */
1698 if( is_filtered(data
) ) goto next
;
1700 entry
->d_ino
= (ino_t
)-1; /* not available */
1701 entry
->d_attributes
= data
.attrib
;
1702 strncpy(entry
->d_name
, data
.name
, NAME_MAX
);
1703 entry
->d_name
[NAME_MAX
] = '\0';
1710 ** Implementation of the POSIX closedir() function using the MSVCRT.
1717 if( dirp
==NULL
) return EINVAL
;
1719 if( dirp
->d_handle
!=NULL_INTPTR_T
&& dirp
->d_handle
!=BAD_INTPTR_T
){
1720 result
= _findclose(dirp
->d_handle
);
1727 #endif /* defined(WIN32) && defined(_MSC_VER) */
1729 /************************* End test_windirent.c ********************/
1730 #define dirent DIRENT
1732 /************************* Begin ../ext/misc/memtrace.c ******************/
1736 ** The author disclaims copyright to this source code. In place of
1737 ** a legal notice, here is a blessing:
1739 ** May you do good and not evil.
1740 ** May you find forgiveness for yourself and forgive others.
1741 ** May you share freely, never taking more than you give.
1743 *************************************************************************
1745 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
1746 ** mechanism to add a tracing layer on top of SQLite. If this extension
1747 ** is registered prior to sqlite3_initialize(), it will cause all memory
1748 ** allocation activities to be logged on standard output, or to some other
1749 ** FILE specified by the initializer.
1751 ** This file needs to be compiled into the application that uses it.
1753 ** This extension is used to implement the --memtrace option of the
1754 ** command-line shell.
1760 /* The original memory allocation routines */
1761 static sqlite3_mem_methods memtraceBase
;
1762 static FILE *memtraceOut
;
1764 /* Methods that trace memory allocations */
1765 static void *memtraceMalloc(int n
){
1767 fprintf(memtraceOut
, "MEMTRACE: allocate %d bytes\n",
1768 memtraceBase
.xRoundup(n
));
1770 return memtraceBase
.xMalloc(n
);
1772 static void memtraceFree(void *p
){
1775 fprintf(memtraceOut
, "MEMTRACE: free %d bytes\n", memtraceBase
.xSize(p
));
1777 memtraceBase
.xFree(p
);
1779 static void *memtraceRealloc(void *p
, int n
){
1780 if( p
==0 ) return memtraceMalloc(n
);
1786 fprintf(memtraceOut
, "MEMTRACE: resize %d -> %d bytes\n",
1787 memtraceBase
.xSize(p
), memtraceBase
.xRoundup(n
));
1789 return memtraceBase
.xRealloc(p
, n
);
1791 static int memtraceSize(void *p
){
1792 return memtraceBase
.xSize(p
);
1794 static int memtraceRoundup(int n
){
1795 return memtraceBase
.xRoundup(n
);
1797 static int memtraceInit(void *p
){
1798 return memtraceBase
.xInit(p
);
1800 static void memtraceShutdown(void *p
){
1801 memtraceBase
.xShutdown(p
);
1804 /* The substitute memory allocator */
1805 static sqlite3_mem_methods ersaztMethods
= {
1816 /* Begin tracing memory allocations to out. */
1817 int sqlite3MemTraceActivate(FILE *out
){
1819 if( memtraceBase
.xMalloc
==0 ){
1820 rc
= sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &memtraceBase
);
1821 if( rc
==SQLITE_OK
){
1822 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &ersaztMethods
);
1829 /* Deactivate memory tracing */
1830 int sqlite3MemTraceDeactivate(void){
1832 if( memtraceBase
.xMalloc
!=0 ){
1833 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &memtraceBase
);
1834 if( rc
==SQLITE_OK
){
1835 memset(&memtraceBase
, 0, sizeof(memtraceBase
));
1842 /************************* End ../ext/misc/memtrace.c ********************/
1843 /************************* Begin ../ext/misc/pcachetrace.c ******************/
1847 ** The author disclaims copyright to this source code. In place of
1848 ** a legal notice, here is a blessing:
1850 ** May you do good and not evil.
1851 ** May you find forgiveness for yourself and forgive others.
1852 ** May you share freely, never taking more than you give.
1854 *************************************************************************
1856 ** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2
1857 ** mechanism to add a tracing layer on top of pluggable page cache of
1858 ** SQLite. If this extension is registered prior to sqlite3_initialize(),
1859 ** it will cause all page cache activities to be logged on standard output,
1860 ** or to some other FILE specified by the initializer.
1862 ** This file needs to be compiled into the application that uses it.
1864 ** This extension is used to implement the --pcachetrace option of the
1865 ** command-line shell.
1871 /* The original page cache routines */
1872 static sqlite3_pcache_methods2 pcacheBase
;
1873 static FILE *pcachetraceOut
;
1875 /* Methods that trace pcache activity */
1876 static int pcachetraceInit(void *pArg
){
1878 if( pcachetraceOut
){
1879 fprintf(pcachetraceOut
, "PCACHETRACE: xInit(%p)\n", pArg
);
1881 nRes
= pcacheBase
.xInit(pArg
);
1882 if( pcachetraceOut
){
1883 fprintf(pcachetraceOut
, "PCACHETRACE: xInit(%p) -> %d\n", pArg
, nRes
);
1887 static void pcachetraceShutdown(void *pArg
){
1888 if( pcachetraceOut
){
1889 fprintf(pcachetraceOut
, "PCACHETRACE: xShutdown(%p)\n", pArg
);
1891 pcacheBase
.xShutdown(pArg
);
1893 static sqlite3_pcache
*pcachetraceCreate(int szPage
, int szExtra
, int bPurge
){
1894 sqlite3_pcache
*pRes
;
1895 if( pcachetraceOut
){
1896 fprintf(pcachetraceOut
, "PCACHETRACE: xCreate(%d,%d,%d)\n",
1897 szPage
, szExtra
, bPurge
);
1899 pRes
= pcacheBase
.xCreate(szPage
, szExtra
, bPurge
);
1900 if( pcachetraceOut
){
1901 fprintf(pcachetraceOut
, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n",
1902 szPage
, szExtra
, bPurge
, pRes
);
1906 static void pcachetraceCachesize(sqlite3_pcache
*p
, int nCachesize
){
1907 if( pcachetraceOut
){
1908 fprintf(pcachetraceOut
, "PCACHETRACE: xCachesize(%p, %d)\n", p
, nCachesize
);
1910 pcacheBase
.xCachesize(p
, nCachesize
);
1912 static int pcachetracePagecount(sqlite3_pcache
*p
){
1914 if( pcachetraceOut
){
1915 fprintf(pcachetraceOut
, "PCACHETRACE: xPagecount(%p)\n", p
);
1917 nRes
= pcacheBase
.xPagecount(p
);
1918 if( pcachetraceOut
){
1919 fprintf(pcachetraceOut
, "PCACHETRACE: xPagecount(%p) -> %d\n", p
, nRes
);
1923 static sqlite3_pcache_page
*pcachetraceFetch(
1928 sqlite3_pcache_page
*pRes
;
1929 if( pcachetraceOut
){
1930 fprintf(pcachetraceOut
, "PCACHETRACE: xFetch(%p,%u,%d)\n", p
, key
, crFg
);
1932 pRes
= pcacheBase
.xFetch(p
, key
, crFg
);
1933 if( pcachetraceOut
){
1934 fprintf(pcachetraceOut
, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n",
1935 p
, key
, crFg
, pRes
);
1939 static void pcachetraceUnpin(
1941 sqlite3_pcache_page
*pPg
,
1944 if( pcachetraceOut
){
1945 fprintf(pcachetraceOut
, "PCACHETRACE: xUnpin(%p, %p, %d)\n",
1948 pcacheBase
.xUnpin(p
, pPg
, bDiscard
);
1950 static void pcachetraceRekey(
1952 sqlite3_pcache_page
*pPg
,
1956 if( pcachetraceOut
){
1957 fprintf(pcachetraceOut
, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n",
1958 p
, pPg
, oldKey
, newKey
);
1960 pcacheBase
.xRekey(p
, pPg
, oldKey
, newKey
);
1962 static void pcachetraceTruncate(sqlite3_pcache
*p
, unsigned n
){
1963 if( pcachetraceOut
){
1964 fprintf(pcachetraceOut
, "PCACHETRACE: xTruncate(%p, %u)\n", p
, n
);
1966 pcacheBase
.xTruncate(p
, n
);
1968 static void pcachetraceDestroy(sqlite3_pcache
*p
){
1969 if( pcachetraceOut
){
1970 fprintf(pcachetraceOut
, "PCACHETRACE: xDestroy(%p)\n", p
);
1972 pcacheBase
.xDestroy(p
);
1974 static void pcachetraceShrink(sqlite3_pcache
*p
){
1975 if( pcachetraceOut
){
1976 fprintf(pcachetraceOut
, "PCACHETRACE: xShrink(%p)\n", p
);
1978 pcacheBase
.xShrink(p
);
1981 /* The substitute pcache methods */
1982 static sqlite3_pcache_methods2 ersaztPcacheMethods
= {
1986 pcachetraceShutdown
,
1988 pcachetraceCachesize
,
1989 pcachetracePagecount
,
1993 pcachetraceTruncate
,
1998 /* Begin tracing memory allocations to out. */
1999 int sqlite3PcacheTraceActivate(FILE *out
){
2001 if( pcacheBase
.xFetch
==0 ){
2002 rc
= sqlite3_config(SQLITE_CONFIG_GETPCACHE2
, &pcacheBase
);
2003 if( rc
==SQLITE_OK
){
2004 rc
= sqlite3_config(SQLITE_CONFIG_PCACHE2
, &ersaztPcacheMethods
);
2007 pcachetraceOut
= out
;
2011 /* Deactivate memory tracing */
2012 int sqlite3PcacheTraceDeactivate(void){
2014 if( pcacheBase
.xFetch
!=0 ){
2015 rc
= sqlite3_config(SQLITE_CONFIG_PCACHE2
, &pcacheBase
);
2016 if( rc
==SQLITE_OK
){
2017 memset(&pcacheBase
, 0, sizeof(pcacheBase
));
2024 /************************* End ../ext/misc/pcachetrace.c ********************/
2025 /************************* Begin ../ext/misc/shathree.c ******************/
2029 ** The author disclaims copyright to this source code. In place of
2030 ** a legal notice, here is a blessing:
2032 ** May you do good and not evil.
2033 ** May you find forgiveness for yourself and forgive others.
2034 ** May you share freely, never taking more than you give.
2036 ******************************************************************************
2038 ** This SQLite extension implements functions that compute SHA3 hashes
2039 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2040 ** Two SQL functions are implemented:
2043 ** sha3_query(Y,SIZE)
2045 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
2048 ** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
2049 ** and returns a hash of their results.
2051 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
2052 ** is used. If SIZE is included it must be one of the integers 224, 256,
2053 ** 384, or 512, to determine SHA3 hash variant that is computed.
2055 /* #include "sqlite3ext.h" */
2056 SQLITE_EXTENSION_INIT1
2061 #ifndef SQLITE_AMALGAMATION
2062 /* typedef sqlite3_uint64 u64; */
2063 #endif /* SQLITE_AMALGAMATION */
2065 /******************************************************************************
2069 ** Macros to determine whether the machine is big or little endian,
2070 ** and whether or not that determination is run-time or compile-time.
2072 ** For best performance, an attempt is made to guess at the byte-order
2073 ** using C-preprocessor macros. If that is unsuccessful, or if
2074 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
2077 #ifndef SHA3_BYTEORDER
2078 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
2079 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
2080 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
2082 # define SHA3_BYTEORDER 1234
2083 # elif defined(sparc) || defined(__ppc__)
2084 # define SHA3_BYTEORDER 4321
2086 # define SHA3_BYTEORDER 0
2092 ** State structure for a SHA3 hash in progress
2094 typedef struct SHA3Context SHA3Context
;
2095 struct SHA3Context
{
2097 u64 s
[25]; /* Keccak state. 5x5 lines of 64 bits each */
2098 unsigned char x
[1600]; /* ... or 1600 bytes */
2100 unsigned nRate
; /* Bytes of input accepted per Keccak iteration */
2101 unsigned nLoaded
; /* Input bytes loaded into u.x[] so far this cycle */
2102 unsigned ixMask
; /* Insert next input into u.x[nLoaded^ixMask]. */
2106 ** A single step of the Keccak mixing function for a 1600-bit state
2108 static void KeccakF1600Step(SHA3Context
*p
){
2110 u64 b0
, b1
, b2
, b3
, b4
;
2111 u64 c0
, c1
, c2
, c3
, c4
;
2112 u64 d0
, d1
, d2
, d3
, d4
;
2113 static const u64 RC
[] = {
2114 0x0000000000000001ULL
, 0x0000000000008082ULL
,
2115 0x800000000000808aULL
, 0x8000000080008000ULL
,
2116 0x000000000000808bULL
, 0x0000000080000001ULL
,
2117 0x8000000080008081ULL
, 0x8000000000008009ULL
,
2118 0x000000000000008aULL
, 0x0000000000000088ULL
,
2119 0x0000000080008009ULL
, 0x000000008000000aULL
,
2120 0x000000008000808bULL
, 0x800000000000008bULL
,
2121 0x8000000000008089ULL
, 0x8000000000008003ULL
,
2122 0x8000000000008002ULL
, 0x8000000000000080ULL
,
2123 0x000000000000800aULL
, 0x800000008000000aULL
,
2124 0x8000000080008081ULL
, 0x8000000000008080ULL
,
2125 0x0000000080000001ULL
, 0x8000000080008008ULL
2127 # define a00 (p->u.s[0])
2128 # define a01 (p->u.s[1])
2129 # define a02 (p->u.s[2])
2130 # define a03 (p->u.s[3])
2131 # define a04 (p->u.s[4])
2132 # define a10 (p->u.s[5])
2133 # define a11 (p->u.s[6])
2134 # define a12 (p->u.s[7])
2135 # define a13 (p->u.s[8])
2136 # define a14 (p->u.s[9])
2137 # define a20 (p->u.s[10])
2138 # define a21 (p->u.s[11])
2139 # define a22 (p->u.s[12])
2140 # define a23 (p->u.s[13])
2141 # define a24 (p->u.s[14])
2142 # define a30 (p->u.s[15])
2143 # define a31 (p->u.s[16])
2144 # define a32 (p->u.s[17])
2145 # define a33 (p->u.s[18])
2146 # define a34 (p->u.s[19])
2147 # define a40 (p->u.s[20])
2148 # define a41 (p->u.s[21])
2149 # define a42 (p->u.s[22])
2150 # define a43 (p->u.s[23])
2151 # define a44 (p->u.s[24])
2152 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
2154 for(i
=0; i
<24; i
+=4){
2155 c0
= a00
^a10
^a20
^a30
^a40
;
2156 c1
= a01
^a11
^a21
^a31
^a41
;
2157 c2
= a02
^a12
^a22
^a32
^a42
;
2158 c3
= a03
^a13
^a23
^a33
^a43
;
2159 c4
= a04
^a14
^a24
^a34
^a44
;
2160 d0
= c4
^ROL64(c1
, 1);
2161 d1
= c0
^ROL64(c2
, 1);
2162 d2
= c1
^ROL64(c3
, 1);
2163 d3
= c2
^ROL64(c4
, 1);
2164 d4
= c3
^ROL64(c0
, 1);
2167 b1
= ROL64((a11
^d1
), 44);
2168 b2
= ROL64((a22
^d2
), 43);
2169 b3
= ROL64((a33
^d3
), 21);
2170 b4
= ROL64((a44
^d4
), 14);
2171 a00
= b0
^((~b1
)& b2
);
2173 a11
= b1
^((~b2
)& b3
);
2174 a22
= b2
^((~b3
)& b4
);
2175 a33
= b3
^((~b4
)& b0
);
2176 a44
= b4
^((~b0
)& b1
);
2178 b2
= ROL64((a20
^d0
), 3);
2179 b3
= ROL64((a31
^d1
), 45);
2180 b4
= ROL64((a42
^d2
), 61);
2181 b0
= ROL64((a03
^d3
), 28);
2182 b1
= ROL64((a14
^d4
), 20);
2183 a20
= b0
^((~b1
)& b2
);
2184 a31
= b1
^((~b2
)& b3
);
2185 a42
= b2
^((~b3
)& b4
);
2186 a03
= b3
^((~b4
)& b0
);
2187 a14
= b4
^((~b0
)& b1
);
2189 b4
= ROL64((a40
^d0
), 18);
2190 b0
= ROL64((a01
^d1
), 1);
2191 b1
= ROL64((a12
^d2
), 6);
2192 b2
= ROL64((a23
^d3
), 25);
2193 b3
= ROL64((a34
^d4
), 8);
2194 a40
= b0
^((~b1
)& b2
);
2195 a01
= b1
^((~b2
)& b3
);
2196 a12
= b2
^((~b3
)& b4
);
2197 a23
= b3
^((~b4
)& b0
);
2198 a34
= b4
^((~b0
)& b1
);
2200 b1
= ROL64((a10
^d0
), 36);
2201 b2
= ROL64((a21
^d1
), 10);
2202 b3
= ROL64((a32
^d2
), 15);
2203 b4
= ROL64((a43
^d3
), 56);
2204 b0
= ROL64((a04
^d4
), 27);
2205 a10
= b0
^((~b1
)& b2
);
2206 a21
= b1
^((~b2
)& b3
);
2207 a32
= b2
^((~b3
)& b4
);
2208 a43
= b3
^((~b4
)& b0
);
2209 a04
= b4
^((~b0
)& b1
);
2211 b3
= ROL64((a30
^d0
), 41);
2212 b4
= ROL64((a41
^d1
), 2);
2213 b0
= ROL64((a02
^d2
), 62);
2214 b1
= ROL64((a13
^d3
), 55);
2215 b2
= ROL64((a24
^d4
), 39);
2216 a30
= b0
^((~b1
)& b2
);
2217 a41
= b1
^((~b2
)& b3
);
2218 a02
= b2
^((~b3
)& b4
);
2219 a13
= b3
^((~b4
)& b0
);
2220 a24
= b4
^((~b0
)& b1
);
2222 c0
= a00
^a20
^a40
^a10
^a30
;
2223 c1
= a11
^a31
^a01
^a21
^a41
;
2224 c2
= a22
^a42
^a12
^a32
^a02
;
2225 c3
= a33
^a03
^a23
^a43
^a13
;
2226 c4
= a44
^a14
^a34
^a04
^a24
;
2227 d0
= c4
^ROL64(c1
, 1);
2228 d1
= c0
^ROL64(c2
, 1);
2229 d2
= c1
^ROL64(c3
, 1);
2230 d3
= c2
^ROL64(c4
, 1);
2231 d4
= c3
^ROL64(c0
, 1);
2234 b1
= ROL64((a31
^d1
), 44);
2235 b2
= ROL64((a12
^d2
), 43);
2236 b3
= ROL64((a43
^d3
), 21);
2237 b4
= ROL64((a24
^d4
), 14);
2238 a00
= b0
^((~b1
)& b2
);
2240 a31
= b1
^((~b2
)& b3
);
2241 a12
= b2
^((~b3
)& b4
);
2242 a43
= b3
^((~b4
)& b0
);
2243 a24
= b4
^((~b0
)& b1
);
2245 b2
= ROL64((a40
^d0
), 3);
2246 b3
= ROL64((a21
^d1
), 45);
2247 b4
= ROL64((a02
^d2
), 61);
2248 b0
= ROL64((a33
^d3
), 28);
2249 b1
= ROL64((a14
^d4
), 20);
2250 a40
= b0
^((~b1
)& b2
);
2251 a21
= b1
^((~b2
)& b3
);
2252 a02
= b2
^((~b3
)& b4
);
2253 a33
= b3
^((~b4
)& b0
);
2254 a14
= b4
^((~b0
)& b1
);
2256 b4
= ROL64((a30
^d0
), 18);
2257 b0
= ROL64((a11
^d1
), 1);
2258 b1
= ROL64((a42
^d2
), 6);
2259 b2
= ROL64((a23
^d3
), 25);
2260 b3
= ROL64((a04
^d4
), 8);
2261 a30
= b0
^((~b1
)& b2
);
2262 a11
= b1
^((~b2
)& b3
);
2263 a42
= b2
^((~b3
)& b4
);
2264 a23
= b3
^((~b4
)& b0
);
2265 a04
= b4
^((~b0
)& b1
);
2267 b1
= ROL64((a20
^d0
), 36);
2268 b2
= ROL64((a01
^d1
), 10);
2269 b3
= ROL64((a32
^d2
), 15);
2270 b4
= ROL64((a13
^d3
), 56);
2271 b0
= ROL64((a44
^d4
), 27);
2272 a20
= b0
^((~b1
)& b2
);
2273 a01
= b1
^((~b2
)& b3
);
2274 a32
= b2
^((~b3
)& b4
);
2275 a13
= b3
^((~b4
)& b0
);
2276 a44
= b4
^((~b0
)& b1
);
2278 b3
= ROL64((a10
^d0
), 41);
2279 b4
= ROL64((a41
^d1
), 2);
2280 b0
= ROL64((a22
^d2
), 62);
2281 b1
= ROL64((a03
^d3
), 55);
2282 b2
= ROL64((a34
^d4
), 39);
2283 a10
= b0
^((~b1
)& b2
);
2284 a41
= b1
^((~b2
)& b3
);
2285 a22
= b2
^((~b3
)& b4
);
2286 a03
= b3
^((~b4
)& b0
);
2287 a34
= b4
^((~b0
)& b1
);
2289 c0
= a00
^a40
^a30
^a20
^a10
;
2290 c1
= a31
^a21
^a11
^a01
^a41
;
2291 c2
= a12
^a02
^a42
^a32
^a22
;
2292 c3
= a43
^a33
^a23
^a13
^a03
;
2293 c4
= a24
^a14
^a04
^a44
^a34
;
2294 d0
= c4
^ROL64(c1
, 1);
2295 d1
= c0
^ROL64(c2
, 1);
2296 d2
= c1
^ROL64(c3
, 1);
2297 d3
= c2
^ROL64(c4
, 1);
2298 d4
= c3
^ROL64(c0
, 1);
2301 b1
= ROL64((a21
^d1
), 44);
2302 b2
= ROL64((a42
^d2
), 43);
2303 b3
= ROL64((a13
^d3
), 21);
2304 b4
= ROL64((a34
^d4
), 14);
2305 a00
= b0
^((~b1
)& b2
);
2307 a21
= b1
^((~b2
)& b3
);
2308 a42
= b2
^((~b3
)& b4
);
2309 a13
= b3
^((~b4
)& b0
);
2310 a34
= b4
^((~b0
)& b1
);
2312 b2
= ROL64((a30
^d0
), 3);
2313 b3
= ROL64((a01
^d1
), 45);
2314 b4
= ROL64((a22
^d2
), 61);
2315 b0
= ROL64((a43
^d3
), 28);
2316 b1
= ROL64((a14
^d4
), 20);
2317 a30
= b0
^((~b1
)& b2
);
2318 a01
= b1
^((~b2
)& b3
);
2319 a22
= b2
^((~b3
)& b4
);
2320 a43
= b3
^((~b4
)& b0
);
2321 a14
= b4
^((~b0
)& b1
);
2323 b4
= ROL64((a10
^d0
), 18);
2324 b0
= ROL64((a31
^d1
), 1);
2325 b1
= ROL64((a02
^d2
), 6);
2326 b2
= ROL64((a23
^d3
), 25);
2327 b3
= ROL64((a44
^d4
), 8);
2328 a10
= b0
^((~b1
)& b2
);
2329 a31
= b1
^((~b2
)& b3
);
2330 a02
= b2
^((~b3
)& b4
);
2331 a23
= b3
^((~b4
)& b0
);
2332 a44
= b4
^((~b0
)& b1
);
2334 b1
= ROL64((a40
^d0
), 36);
2335 b2
= ROL64((a11
^d1
), 10);
2336 b3
= ROL64((a32
^d2
), 15);
2337 b4
= ROL64((a03
^d3
), 56);
2338 b0
= ROL64((a24
^d4
), 27);
2339 a40
= b0
^((~b1
)& b2
);
2340 a11
= b1
^((~b2
)& b3
);
2341 a32
= b2
^((~b3
)& b4
);
2342 a03
= b3
^((~b4
)& b0
);
2343 a24
= b4
^((~b0
)& b1
);
2345 b3
= ROL64((a20
^d0
), 41);
2346 b4
= ROL64((a41
^d1
), 2);
2347 b0
= ROL64((a12
^d2
), 62);
2348 b1
= ROL64((a33
^d3
), 55);
2349 b2
= ROL64((a04
^d4
), 39);
2350 a20
= b0
^((~b1
)& b2
);
2351 a41
= b1
^((~b2
)& b3
);
2352 a12
= b2
^((~b3
)& b4
);
2353 a33
= b3
^((~b4
)& b0
);
2354 a04
= b4
^((~b0
)& b1
);
2356 c0
= a00
^a30
^a10
^a40
^a20
;
2357 c1
= a21
^a01
^a31
^a11
^a41
;
2358 c2
= a42
^a22
^a02
^a32
^a12
;
2359 c3
= a13
^a43
^a23
^a03
^a33
;
2360 c4
= a34
^a14
^a44
^a24
^a04
;
2361 d0
= c4
^ROL64(c1
, 1);
2362 d1
= c0
^ROL64(c2
, 1);
2363 d2
= c1
^ROL64(c3
, 1);
2364 d3
= c2
^ROL64(c4
, 1);
2365 d4
= c3
^ROL64(c0
, 1);
2368 b1
= ROL64((a01
^d1
), 44);
2369 b2
= ROL64((a02
^d2
), 43);
2370 b3
= ROL64((a03
^d3
), 21);
2371 b4
= ROL64((a04
^d4
), 14);
2372 a00
= b0
^((~b1
)& b2
);
2374 a01
= b1
^((~b2
)& b3
);
2375 a02
= b2
^((~b3
)& b4
);
2376 a03
= b3
^((~b4
)& b0
);
2377 a04
= b4
^((~b0
)& b1
);
2379 b2
= ROL64((a10
^d0
), 3);
2380 b3
= ROL64((a11
^d1
), 45);
2381 b4
= ROL64((a12
^d2
), 61);
2382 b0
= ROL64((a13
^d3
), 28);
2383 b1
= ROL64((a14
^d4
), 20);
2384 a10
= b0
^((~b1
)& b2
);
2385 a11
= b1
^((~b2
)& b3
);
2386 a12
= b2
^((~b3
)& b4
);
2387 a13
= b3
^((~b4
)& b0
);
2388 a14
= b4
^((~b0
)& b1
);
2390 b4
= ROL64((a20
^d0
), 18);
2391 b0
= ROL64((a21
^d1
), 1);
2392 b1
= ROL64((a22
^d2
), 6);
2393 b2
= ROL64((a23
^d3
), 25);
2394 b3
= ROL64((a24
^d4
), 8);
2395 a20
= b0
^((~b1
)& b2
);
2396 a21
= b1
^((~b2
)& b3
);
2397 a22
= b2
^((~b3
)& b4
);
2398 a23
= b3
^((~b4
)& b0
);
2399 a24
= b4
^((~b0
)& b1
);
2401 b1
= ROL64((a30
^d0
), 36);
2402 b2
= ROL64((a31
^d1
), 10);
2403 b3
= ROL64((a32
^d2
), 15);
2404 b4
= ROL64((a33
^d3
), 56);
2405 b0
= ROL64((a34
^d4
), 27);
2406 a30
= b0
^((~b1
)& b2
);
2407 a31
= b1
^((~b2
)& b3
);
2408 a32
= b2
^((~b3
)& b4
);
2409 a33
= b3
^((~b4
)& b0
);
2410 a34
= b4
^((~b0
)& b1
);
2412 b3
= ROL64((a40
^d0
), 41);
2413 b4
= ROL64((a41
^d1
), 2);
2414 b0
= ROL64((a42
^d2
), 62);
2415 b1
= ROL64((a43
^d3
), 55);
2416 b2
= ROL64((a44
^d4
), 39);
2417 a40
= b0
^((~b1
)& b2
);
2418 a41
= b1
^((~b2
)& b3
);
2419 a42
= b2
^((~b3
)& b4
);
2420 a43
= b3
^((~b4
)& b0
);
2421 a44
= b4
^((~b0
)& b1
);
2426 ** Initialize a new hash. iSize determines the size of the hash
2427 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
2428 ** can be zero to use the default hash size of 256 bits.
2430 static void SHA3Init(SHA3Context
*p
, int iSize
){
2431 memset(p
, 0, sizeof(*p
));
2432 if( iSize
>=128 && iSize
<=512 ){
2433 p
->nRate
= (1600 - ((iSize
+ 31)&~31)*2)/8;
2435 p
->nRate
= (1600 - 2*256)/8;
2437 #if SHA3_BYTEORDER==1234
2438 /* Known to be little-endian at compile-time. No-op */
2439 #elif SHA3_BYTEORDER==4321
2440 p
->ixMask
= 7; /* Big-endian */
2443 static unsigned int one
= 1;
2444 if( 1==*(unsigned char*)&one
){
2445 /* Little endian. No byte swapping. */
2448 /* Big endian. Byte swap. */
2456 ** Make consecutive calls to the SHA3Update function to add new content
2459 static void SHA3Update(
2461 const unsigned char *aData
,
2465 if( aData
==0 ) return;
2466 #if SHA3_BYTEORDER==1234
2467 if( (p
->nLoaded
% 8)==0 && ((aData
- (const unsigned char*)0)&7)==0 ){
2468 for(; i
+7<nData
; i
+=8){
2469 p
->u
.s
[p
->nLoaded
/8] ^= *(u64
*)&aData
[i
];
2471 if( p
->nLoaded
>=p
->nRate
){
2478 for(; i
<nData
; i
++){
2479 #if SHA3_BYTEORDER==1234
2480 p
->u
.x
[p
->nLoaded
] ^= aData
[i
];
2481 #elif SHA3_BYTEORDER==4321
2482 p
->u
.x
[p
->nLoaded
^0x07] ^= aData
[i
];
2484 p
->u
.x
[p
->nLoaded
^p
->ixMask
] ^= aData
[i
];
2487 if( p
->nLoaded
==p
->nRate
){
2495 ** After all content has been added, invoke SHA3Final() to compute
2496 ** the final hash. The function returns a pointer to the binary
2499 static unsigned char *SHA3Final(SHA3Context
*p
){
2501 if( p
->nLoaded
==p
->nRate
-1 ){
2502 const unsigned char c1
= 0x86;
2503 SHA3Update(p
, &c1
, 1);
2505 const unsigned char c2
= 0x06;
2506 const unsigned char c3
= 0x80;
2507 SHA3Update(p
, &c2
, 1);
2508 p
->nLoaded
= p
->nRate
- 1;
2509 SHA3Update(p
, &c3
, 1);
2511 for(i
=0; i
<p
->nRate
; i
++){
2512 p
->u
.x
[i
+p
->nRate
] = p
->u
.x
[i
^p
->ixMask
];
2514 return &p
->u
.x
[p
->nRate
];
2516 /* End of the hashing logic
2517 *****************************************************************************/
2520 ** Implementation of the sha3(X,SIZE) function.
2522 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
2523 ** size is 256. If X is a BLOB, it is hashed as is.
2524 ** For all other non-NULL types of input, X is converted into a UTF-8 string
2525 ** and the string is hashed without the trailing 0x00 terminator. The hash
2526 ** of a NULL value is NULL.
2528 static void sha3Func(
2529 sqlite3_context
*context
,
2531 sqlite3_value
**argv
2534 int eType
= sqlite3_value_type(argv
[0]);
2535 int nByte
= sqlite3_value_bytes(argv
[0]);
2540 iSize
= sqlite3_value_int(argv
[1]);
2541 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
2542 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
2547 if( eType
==SQLITE_NULL
) return;
2548 SHA3Init(&cx
, iSize
);
2549 if( eType
==SQLITE_BLOB
){
2550 SHA3Update(&cx
, sqlite3_value_blob(argv
[0]), nByte
);
2552 SHA3Update(&cx
, sqlite3_value_text(argv
[0]), nByte
);
2554 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
2557 /* Compute a string using sqlite3_vsnprintf() with a maximum length
2558 ** of 50 bytes and add it to the hash.
2560 static void sha3_step_vformat(
2561 SHA3Context
*p
, /* Add content to this context */
2562 const char *zFormat
,
2568 va_start(ap
, zFormat
);
2569 sqlite3_vsnprintf(sizeof(zBuf
),zBuf
,zFormat
,ap
);
2571 n
= (int)strlen(zBuf
);
2572 SHA3Update(p
, (unsigned char*)zBuf
, n
);
2576 ** Implementation of the sha3_query(SQL,SIZE) function.
2578 ** This function compiles and runs the SQL statement(s) given in the
2579 ** argument. The results are hashed using a SIZE-bit SHA3. The default
2582 ** The format of the byte stream that is hashed is summarized as follows:
2592 ** <sql> is the original SQL text for each statement run and <n> is
2593 ** the size of that text. The SQL text is UTF-8. A single R character
2594 ** occurs before the start of each row. N means a NULL value.
2595 ** I mean an 8-byte little-endian integer <int>. F is a floating point
2596 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
2597 ** B means blobs of <size> bytes. T means text rendered as <size>
2598 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
2601 ** For each SQL statement in the X input, there is one S segment. Each
2602 ** S segment is followed by zero or more R segments, one for each row in the
2603 ** result set. After each R, there are one or more N, I, F, B, or T segments,
2604 ** one for each column in the result set. Segments are concatentated directly
2605 ** with no delimiters of any kind.
2607 static void sha3QueryFunc(
2608 sqlite3_context
*context
,
2610 sqlite3_value
**argv
2612 sqlite3
*db
= sqlite3_context_db_handle(context
);
2613 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
2614 sqlite3_stmt
*pStmt
= 0;
2615 int nCol
; /* Number of columns in the result set */
2616 int i
; /* Loop counter */
2626 iSize
= sqlite3_value_int(argv
[1]);
2627 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
2628 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
2633 if( zSql
==0 ) return;
2634 SHA3Init(&cx
, iSize
);
2636 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zSql
);
2638 char *zMsg
= sqlite3_mprintf("error SQL statement [%s]: %s",
2639 zSql
, sqlite3_errmsg(db
));
2640 sqlite3_finalize(pStmt
);
2641 sqlite3_result_error(context
, zMsg
, -1);
2645 if( !sqlite3_stmt_readonly(pStmt
) ){
2646 char *zMsg
= sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt
));
2647 sqlite3_finalize(pStmt
);
2648 sqlite3_result_error(context
, zMsg
, -1);
2652 nCol
= sqlite3_column_count(pStmt
);
2653 z
= sqlite3_sql(pStmt
);
2656 sha3_step_vformat(&cx
,"S%d:",n
);
2657 SHA3Update(&cx
,(unsigned char*)z
,n
);
2660 /* Compute a hash over the result of the query */
2661 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
2662 SHA3Update(&cx
,(const unsigned char*)"R",1);
2663 for(i
=0; i
<nCol
; i
++){
2664 switch( sqlite3_column_type(pStmt
,i
) ){
2666 SHA3Update(&cx
, (const unsigned char*)"N",1);
2669 case SQLITE_INTEGER
: {
2673 sqlite3_int64 v
= sqlite3_column_int64(pStmt
,i
);
2675 for(j
=8; j
>=1; j
--){
2680 SHA3Update(&cx
, x
, 9);
2683 case SQLITE_FLOAT
: {
2687 double r
= sqlite3_column_double(pStmt
,i
);
2689 for(j
=8; j
>=1; j
--){
2694 SHA3Update(&cx
,x
,9);
2698 int n2
= sqlite3_column_bytes(pStmt
, i
);
2699 const unsigned char *z2
= sqlite3_column_text(pStmt
, i
);
2700 sha3_step_vformat(&cx
,"T%d:",n2
);
2701 SHA3Update(&cx
, z2
, n2
);
2705 int n2
= sqlite3_column_bytes(pStmt
, i
);
2706 const unsigned char *z2
= sqlite3_column_blob(pStmt
, i
);
2707 sha3_step_vformat(&cx
,"B%d:",n2
);
2708 SHA3Update(&cx
, z2
, n2
);
2714 sqlite3_finalize(pStmt
);
2716 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
2723 int sqlite3_shathree_init(
2726 const sqlite3_api_routines
*pApi
2729 SQLITE_EXTENSION_INIT2(pApi
);
2730 (void)pzErrMsg
; /* Unused parameter */
2731 rc
= sqlite3_create_function(db
, "sha3", 1,
2732 SQLITE_UTF8
| SQLITE_INNOCUOUS
| SQLITE_DETERMINISTIC
,
2734 if( rc
==SQLITE_OK
){
2735 rc
= sqlite3_create_function(db
, "sha3", 2,
2736 SQLITE_UTF8
| SQLITE_INNOCUOUS
| SQLITE_DETERMINISTIC
,
2739 if( rc
==SQLITE_OK
){
2740 rc
= sqlite3_create_function(db
, "sha3_query", 1,
2741 SQLITE_UTF8
| SQLITE_DIRECTONLY
,
2742 0, sha3QueryFunc
, 0, 0);
2744 if( rc
==SQLITE_OK
){
2745 rc
= sqlite3_create_function(db
, "sha3_query", 2,
2746 SQLITE_UTF8
| SQLITE_DIRECTONLY
,
2747 0, sha3QueryFunc
, 0, 0);
2752 /************************* End ../ext/misc/shathree.c ********************/
2753 /************************* Begin ../ext/misc/uint.c ******************/
2757 ** The author disclaims copyright to this source code. In place of
2758 ** a legal notice, here is a blessing:
2760 ** May you do good and not evil.
2761 ** May you find forgiveness for yourself and forgive others.
2762 ** May you share freely, never taking more than you give.
2764 ******************************************************************************
2766 ** This SQLite extension implements the UINT collating sequence.
2768 ** UINT works like BINARY for text, except that embedded strings
2769 ** of digits compare in numeric order.
2771 ** * Leading zeros are handled properly, in the sense that
2772 ** they do not mess of the maginitude comparison of embedded
2773 ** strings of digits. "x00123y" is equal to "x123y".
2775 ** * Only unsigned integers are recognized. Plus and minus
2776 ** signs are ignored. Decimal points and exponential notation
2779 ** * Embedded integers can be of arbitrary length. Comparison
2780 ** is *not* limited integers that can be expressed as a
2781 ** 64-bit machine integer.
2783 /* #include "sqlite3ext.h" */
2784 SQLITE_EXTENSION_INIT1
2790 ** Compare text in lexicographic order, except strings of digits
2791 ** compare in numeric order.
2793 static int uintCollFunc(
2795 int nKey1
, const void *pKey1
,
2796 int nKey2
, const void *pKey2
2798 const unsigned char *zA
= (const unsigned char*)pKey1
;
2799 const unsigned char *zB
= (const unsigned char*)pKey2
;
2802 while( i
<nKey1
&& j
<nKey2
){
2804 if( isdigit(zA
[i
]) ){
2806 if( !isdigit(zB
[j
]) ) return x
;
2807 while( i
<nKey1
&& zA
[i
]=='0' ){ i
++; }
2808 while( j
<nKey2
&& zB
[j
]=='0' ){ j
++; }
2810 while( i
+k
<nKey1
&& isdigit(zA
[i
+k
])
2811 && j
+k
<nKey2
&& isdigit(zB
[j
+k
]) ){
2814 if( i
+k
<nKey1
&& isdigit(zA
[i
+k
]) ){
2816 }else if( j
+k
<nKey2
&& isdigit(zB
[j
+k
]) ){
2819 x
= memcmp(zA
+i
, zB
+j
, k
);
2831 return (nKey1
- i
) - (nKey2
- j
);
2837 int sqlite3_uint_init(
2840 const sqlite3_api_routines
*pApi
2842 SQLITE_EXTENSION_INIT2(pApi
);
2843 (void)pzErrMsg
; /* Unused parameter */
2844 return sqlite3_create_collation(db
, "uint", SQLITE_UTF8
, 0, uintCollFunc
);
2847 /************************* End ../ext/misc/uint.c ********************/
2848 /************************* Begin ../ext/misc/decimal.c ******************/
2852 ** The author disclaims copyright to this source code. In place of
2853 ** a legal notice, here is a blessing:
2855 ** May you do good and not evil.
2856 ** May you find forgiveness for yourself and forgive others.
2857 ** May you share freely, never taking more than you give.
2859 ******************************************************************************
2861 ** Routines to implement arbitrary-precision decimal math.
2863 ** The focus here is on simplicity and correctness, not performance.
2865 /* #include "sqlite3ext.h" */
2866 SQLITE_EXTENSION_INIT1
2872 /* Mark a function parameter as unused, to suppress nuisance compiler
2874 #ifndef UNUSED_PARAMETER
2875 # define UNUSED_PARAMETER(X) (void)(X)
2879 /* A decimal object */
2880 typedef struct Decimal Decimal
;
2882 char sign
; /* 0 for positive, 1 for negative */
2883 char oom
; /* True if an OOM is encountered */
2884 char isNull
; /* True if holds a NULL rather than a number */
2885 char isInit
; /* True upon initialization */
2886 int nDigit
; /* Total number of digits */
2887 int nFrac
; /* Number of digits to the right of the decimal point */
2888 signed char *a
; /* Array of digits. Most significant first. */
2892 ** Release memory held by a Decimal, but do not free the object itself.
2894 static void decimal_clear(Decimal
*p
){
2899 ** Destroy a Decimal object
2901 static void decimal_free(Decimal
*p
){
2909 ** Allocate a new Decimal object initialized to the text in zIn[].
2910 ** Return NULL if any kind of error occurs.
2912 static Decimal
*decimalNewFromText(const char *zIn
, int n
){
2917 p
= sqlite3_malloc( sizeof(*p
) );
2918 if( p
==0 ) goto new_from_text_failed
;
2925 p
->a
= sqlite3_malloc64( n
+1 );
2926 if( p
->a
==0 ) goto new_from_text_failed
;
2927 for(i
=0; isspace(zIn
[i
]); i
++){}
2931 }else if( zIn
[i
]=='+' ){
2934 while( i
<n
&& zIn
[i
]=='0' ) i
++;
2937 if( c
>='0' && c
<='9' ){
2938 p
->a
[p
->nDigit
++] = c
- '0';
2940 p
->nFrac
= p
->nDigit
+ 1;
2941 }else if( c
=='e' || c
=='E' ){
2948 }else if( zIn
[j
]=='+' ){
2951 while( j
<n
&& iExp
<1000000 ){
2952 if( zIn
[j
]>='0' && zIn
[j
]<='9' ){
2953 iExp
= iExp
*10 + zIn
[j
] - '0';
2957 if( neg
) iExp
= -iExp
;
2963 p
->nFrac
= p
->nDigit
- (p
->nFrac
- 1);
2967 if( iExp
<=p
->nFrac
){
2976 p
->a
= sqlite3_realloc64(p
->a
, p
->nDigit
+ iExp
+ 1 );
2977 if( p
->a
==0 ) goto new_from_text_failed
;
2978 memset(p
->a
+p
->nDigit
, 0, iExp
);
2984 nExtra
= p
->nDigit
- p
->nFrac
- 1;
2991 p
->nFrac
= p
->nDigit
- 1;
2995 p
->a
= sqlite3_realloc64(p
->a
, p
->nDigit
+ iExp
+ 1 );
2996 if( p
->a
==0 ) goto new_from_text_failed
;
2997 memmove(p
->a
+iExp
, p
->a
, p
->nDigit
);
2998 memset(p
->a
, 0, iExp
);
3005 new_from_text_failed
:
3007 if( p
->a
) sqlite3_free(p
->a
);
3013 /* Forward reference */
3014 static Decimal
*decimalFromDouble(double);
3017 ** Allocate a new Decimal object from an sqlite3_value. Return a pointer
3018 ** to the new object, or NULL if there is an error. If the pCtx argument
3019 ** is not NULL, then errors are reported on it as well.
3021 ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
3022 ** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length
3023 ** 8 bytes, the resulting double value is expanded into its decimal equivalent.
3024 ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
3025 ** then NULL is returned.
3027 static Decimal
*decimal_new(
3028 sqlite3_context
*pCtx
, /* Report error here, if not null */
3029 sqlite3_value
*pIn
, /* Construct the decimal object from this */
3030 int bTextOnly
/* Always interpret pIn as text if true */
3033 int eType
= sqlite3_value_type(pIn
);
3034 if( bTextOnly
&& (eType
==SQLITE_FLOAT
|| eType
==SQLITE_BLOB
) ){
3035 eType
= SQLITE_TEXT
;
3039 case SQLITE_INTEGER
: {
3040 const char *zIn
= (const char*)sqlite3_value_text(pIn
);
3041 int n
= sqlite3_value_bytes(pIn
);
3042 p
= decimalNewFromText(zIn
, n
);
3043 if( p
==0 ) goto new_failed
;
3047 case SQLITE_FLOAT
: {
3048 p
= decimalFromDouble(sqlite3_value_double(pIn
));
3053 const unsigned char *x
;
3055 sqlite3_uint64 v
= 0;
3058 if( sqlite3_value_bytes(pIn
)!=sizeof(r
) ) break;
3059 x
= sqlite3_value_blob(pIn
);
3060 for(i
=0; i
<sizeof(r
); i
++){
3063 memcpy(&r
, &v
, sizeof(r
));
3064 p
= decimalFromDouble(r
);
3075 if( pCtx
) sqlite3_result_error_nomem(pCtx
);
3081 ** Make the given Decimal the result.
3083 static void decimal_result(sqlite3_context
*pCtx
, Decimal
*p
){
3087 if( p
==0 || p
->oom
){
3088 sqlite3_result_error_nomem(pCtx
);
3092 sqlite3_result_null(pCtx
);
3095 z
= sqlite3_malloc( p
->nDigit
+4 );
3097 sqlite3_result_error_nomem(pCtx
);
3101 if( p
->nDigit
==0 || (p
->nDigit
==1 && p
->a
[0]==0) ){
3108 n
= p
->nDigit
- p
->nFrac
;
3113 while( n
>1 && p
->a
[j
]==0 ){
3118 z
[i
++] = p
->a
[j
] + '0';
3125 z
[i
++] = p
->a
[j
] + '0';
3127 }while( j
<p
->nDigit
);
3130 sqlite3_result_text(pCtx
, z
, i
, sqlite3_free
);
3134 ** Make the given Decimal the result in an format similar to '%+#e'.
3135 ** In other words, show exponential notation with leading and trailing
3138 static void decimal_result_sci(sqlite3_context
*pCtx
, Decimal
*p
){
3139 char *z
; /* The output buffer */
3140 int i
; /* Loop counter */
3141 int nZero
; /* Number of leading zeros */
3142 int nDigit
; /* Number of digits not counting trailing zeros */
3143 int nFrac
; /* Digits to the right of the decimal point */
3144 int exp
; /* Exponent value */
3145 signed char zero
; /* Zero value */
3146 signed char *a
; /* Array of digits */
3148 if( p
==0 || p
->oom
){
3149 sqlite3_result_error_nomem(pCtx
);
3153 sqlite3_result_null(pCtx
);
3156 for(nDigit
=p
->nDigit
; nDigit
>0 && p
->a
[nDigit
-1]==0; nDigit
--){}
3157 for(nZero
=0; nZero
<nDigit
&& p
->a
[nZero
]==0; nZero
++){}
3158 nFrac
= p
->nFrac
+ (nDigit
- p
->nDigit
);
3160 z
= sqlite3_malloc( nDigit
+20 );
3162 sqlite3_result_error_nomem(pCtx
);
3173 if( p
->sign
&& nDigit
>0 ){
3184 for(i
=1; i
<nDigit
; i
++){
3189 exp
= nDigit
- nFrac
- 1;
3190 sqlite3_snprintf(nDigit
+20-i
, &z
[i
], "e%+03d", exp
);
3191 sqlite3_result_text(pCtx
, z
, -1, sqlite3_free
);
3195 ** Compare to Decimal objects. Return negative, 0, or positive if the
3196 ** first object is less than, equal to, or greater than the second.
3198 ** Preconditions for this routine:
3205 static int decimal_cmp(const Decimal
*pA
, const Decimal
*pB
){
3206 int nASig
, nBSig
, rc
, n
;
3207 if( pA
->sign
!=pB
->sign
){
3208 return pA
->sign
? -1 : +1;
3211 const Decimal
*pTemp
= pA
;
3215 nASig
= pA
->nDigit
- pA
->nFrac
;
3216 nBSig
= pB
->nDigit
- pB
->nFrac
;
3218 return nASig
- nBSig
;
3221 if( n
>pB
->nDigit
) n
= pB
->nDigit
;
3222 rc
= memcmp(pA
->a
, pB
->a
, n
);
3224 rc
= pA
->nDigit
- pB
->nDigit
;
3230 ** SQL Function: decimal_cmp(X, Y)
3232 ** Return negative, zero, or positive if X is less then, equal to, or
3235 static void decimalCmpFunc(
3236 sqlite3_context
*context
,
3238 sqlite3_value
**argv
3240 Decimal
*pA
= 0, *pB
= 0;
3243 UNUSED_PARAMETER(argc
);
3244 pA
= decimal_new(context
, argv
[0], 1);
3245 if( pA
==0 || pA
->isNull
) goto cmp_done
;
3246 pB
= decimal_new(context
, argv
[1], 1);
3247 if( pB
==0 || pB
->isNull
) goto cmp_done
;
3248 rc
= decimal_cmp(pA
, pB
);
3250 else if( rc
>0 ) rc
= +1;
3251 sqlite3_result_int(context
, rc
);
3258 ** Expand the Decimal so that it has a least nDigit digits and nFrac
3259 ** digits to the right of the decimal point.
3261 static void decimal_expand(Decimal
*p
, int nDigit
, int nFrac
){
3265 nAddFrac
= nFrac
- p
->nFrac
;
3266 nAddSig
= (nDigit
- p
->nDigit
) - nAddFrac
;
3267 if( nAddFrac
==0 && nAddSig
==0 ) return;
3268 p
->a
= sqlite3_realloc64(p
->a
, nDigit
+1);
3274 memmove(p
->a
+nAddSig
, p
->a
, p
->nDigit
);
3275 memset(p
->a
, 0, nAddSig
);
3276 p
->nDigit
+= nAddSig
;
3279 memset(p
->a
+p
->nDigit
, 0, nAddFrac
);
3280 p
->nDigit
+= nAddFrac
;
3281 p
->nFrac
+= nAddFrac
;
3286 ** Add the value pB into pA. A := A + B.
3288 ** Both pA and pB might become denormalized by this routine.
3290 static void decimal_add(Decimal
*pA
, Decimal
*pB
){
3291 int nSig
, nFrac
, nDigit
;
3296 if( pA
->oom
|| pB
==0 || pB
->oom
){
3300 if( pA
->isNull
|| pB
->isNull
){
3304 nSig
= pA
->nDigit
- pA
->nFrac
;
3305 if( nSig
&& pA
->a
[0]==0 ) nSig
--;
3306 if( nSig
<pB
->nDigit
-pB
->nFrac
){
3307 nSig
= pB
->nDigit
- pB
->nFrac
;
3310 if( nFrac
<pB
->nFrac
) nFrac
= pB
->nFrac
;
3311 nDigit
= nSig
+ nFrac
+ 1;
3312 decimal_expand(pA
, nDigit
, nFrac
);
3313 decimal_expand(pB
, nDigit
, nFrac
);
3314 if( pA
->oom
|| pB
->oom
){
3317 if( pA
->sign
==pB
->sign
){
3319 for(i
=nDigit
-1; i
>=0; i
--){
3320 int x
= pA
->a
[i
] + pB
->a
[i
] + carry
;
3330 signed char *aA
, *aB
;
3332 rc
= memcmp(pA
->a
, pB
->a
, nDigit
);
3336 pA
->sign
= !pA
->sign
;
3341 for(i
=nDigit
-1; i
>=0; i
--){
3342 int x
= aA
[i
] - aB
[i
] - borrow
;
3356 ** Multiply A by B. A := A * B
3358 ** All significant digits after the decimal point are retained.
3359 ** Trailing zeros after the decimal point are omitted as long as
3360 ** the number of digits after the decimal point is no less than
3361 ** either the number of digits in either input.
3363 static void decimalMul(Decimal
*pA
, Decimal
*pB
){
3364 signed char *acc
= 0;
3368 if( pA
==0 || pA
->oom
|| pA
->isNull
3369 || pB
==0 || pB
->oom
|| pB
->isNull
3373 acc
= sqlite3_malloc64( pA
->nDigit
+ pB
->nDigit
+ 2 );
3378 memset(acc
, 0, pA
->nDigit
+ pB
->nDigit
+ 2);
3379 minFrac
= pA
->nFrac
;
3380 if( pB
->nFrac
<minFrac
) minFrac
= pB
->nFrac
;
3381 for(i
=pA
->nDigit
-1; i
>=0; i
--){
3382 signed char f
= pA
->a
[i
];
3384 for(j
=pB
->nDigit
-1, k
=i
+j
+3; j
>=0; j
--, k
--){
3385 x
= acc
[k
] + f
*pB
->a
[j
] + carry
;
3393 sqlite3_free(pA
->a
);
3396 pA
->nDigit
+= pB
->nDigit
+ 2;
3397 pA
->nFrac
+= pB
->nFrac
;
3398 pA
->sign
^= pB
->sign
;
3399 while( pA
->nFrac
>minFrac
&& pA
->a
[pA
->nDigit
-1]==0 ){
3409 ** Create a new Decimal object that contains an integer power of 2.
3411 static Decimal
*decimalPow2(int N
){
3412 Decimal
*pA
= 0; /* The result to be returned */
3413 Decimal
*pX
= 0; /* Multiplier */
3414 if( N
<-20000 || N
>20000 ) goto pow2_fault
;
3415 pA
= decimalNewFromText("1.0", 3);
3416 if( pA
==0 || pA
->oom
) goto pow2_fault
;
3417 if( N
==0 ) return pA
;
3419 pX
= decimalNewFromText("2.0", 3);
3422 pX
= decimalNewFromText("0.5", 3);
3424 if( pX
==0 || pX
->oom
) goto pow2_fault
;
3425 while( 1 /* Exit by break */ ){
3428 if( pA
->oom
) goto pow2_fault
;
3444 ** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
3446 static Decimal
*decimalFromDouble(double r
){
3459 memcpy(&a
,&r
,sizeof(a
));
3465 m
= a
& ((((sqlite3_int64
)1)<<52)-1);
3469 m
|= ((sqlite3_int64
)1)<<52;
3471 while( e
<1075 && m
>0 && (m
&1)==0 ){
3478 return 0; /* A NaN or an Infinity */
3482 /* At this point m is the integer significand and e is the exponent */
3483 sqlite3_snprintf(sizeof(zNum
), zNum
, "%lld", m
);
3484 pA
= decimalNewFromText(zNum
, (int)strlen(zNum
));
3485 pX
= decimalPow2(e
);
3492 ** SQL Function: decimal(X)
3493 ** OR: decimal_exp(X)
3495 ** Convert input X into decimal and then back into text.
3497 ** If X is originally a float, then a full decimal expansion of that floating
3498 ** point value is done. Or if X is an 8-byte blob, it is interpreted
3499 ** as a float and similarly expanded.
3501 ** The decimal_exp(X) function returns the result in exponential notation.
3502 ** decimal(X) returns a complete decimal, without the e+NNN at the end.
3504 static void decimalFunc(
3505 sqlite3_context
*context
,
3507 sqlite3_value
**argv
3509 Decimal
*p
= decimal_new(context
, argv
[0], 0);
3510 UNUSED_PARAMETER(argc
);
3512 if( sqlite3_user_data(context
)!=0 ){
3513 decimal_result_sci(context
, p
);
3515 decimal_result(context
, p
);
3522 ** Compare text in decimal order.
3524 static int decimalCollFunc(
3526 int nKey1
, const void *pKey1
,
3527 int nKey2
, const void *pKey2
3529 const unsigned char *zA
= (const unsigned char*)pKey1
;
3530 const unsigned char *zB
= (const unsigned char*)pKey2
;
3531 Decimal
*pA
= decimalNewFromText((const char*)zA
, nKey1
);
3532 Decimal
*pB
= decimalNewFromText((const char*)zB
, nKey2
);
3534 UNUSED_PARAMETER(notUsed
);
3535 if( pA
==0 || pB
==0 ){
3538 rc
= decimal_cmp(pA
, pB
);
3547 ** SQL Function: decimal_add(X, Y)
3548 ** decimal_sub(X, Y)
3550 ** Return the sum or difference of X and Y.
3552 static void decimalAddFunc(
3553 sqlite3_context
*context
,
3555 sqlite3_value
**argv
3557 Decimal
*pA
= decimal_new(context
, argv
[0], 1);
3558 Decimal
*pB
= decimal_new(context
, argv
[1], 1);
3559 UNUSED_PARAMETER(argc
);
3560 decimal_add(pA
, pB
);
3561 decimal_result(context
, pA
);
3565 static void decimalSubFunc(
3566 sqlite3_context
*context
,
3568 sqlite3_value
**argv
3570 Decimal
*pA
= decimal_new(context
, argv
[0], 1);
3571 Decimal
*pB
= decimal_new(context
, argv
[1], 1);
3572 UNUSED_PARAMETER(argc
);
3574 pB
->sign
= !pB
->sign
;
3575 decimal_add(pA
, pB
);
3576 decimal_result(context
, pA
);
3582 /* Aggregate funcion: decimal_sum(X)
3584 ** Works like sum() except that it uses decimal arithmetic for unlimited
3587 static void decimalSumStep(
3588 sqlite3_context
*context
,
3590 sqlite3_value
**argv
3594 UNUSED_PARAMETER(argc
);
3595 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
3599 p
->a
= sqlite3_malloc(2);
3608 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
3609 pArg
= decimal_new(context
, argv
[0], 1);
3610 decimal_add(p
, pArg
);
3613 static void decimalSumInverse(
3614 sqlite3_context
*context
,
3616 sqlite3_value
**argv
3620 UNUSED_PARAMETER(argc
);
3621 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
3623 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
3624 pArg
= decimal_new(context
, argv
[0], 1);
3625 if( pArg
) pArg
->sign
= !pArg
->sign
;
3626 decimal_add(p
, pArg
);
3629 static void decimalSumValue(sqlite3_context
*context
){
3630 Decimal
*p
= sqlite3_aggregate_context(context
, 0);
3632 decimal_result(context
, p
);
3634 static void decimalSumFinalize(sqlite3_context
*context
){
3635 Decimal
*p
= sqlite3_aggregate_context(context
, 0);
3637 decimal_result(context
, p
);
3642 ** SQL Function: decimal_mul(X, Y)
3644 ** Return the product of X and Y.
3646 static void decimalMulFunc(
3647 sqlite3_context
*context
,
3649 sqlite3_value
**argv
3651 Decimal
*pA
= decimal_new(context
, argv
[0], 1);
3652 Decimal
*pB
= decimal_new(context
, argv
[1], 1);
3653 UNUSED_PARAMETER(argc
);
3654 if( pA
==0 || pA
->oom
|| pA
->isNull
3655 || pB
==0 || pB
->oom
|| pB
->isNull
3663 decimal_result(context
, pA
);
3671 ** SQL Function: decimal_pow2(N)
3673 ** Return the N-th power of 2. N must be an integer.
3675 static void decimalPow2Func(
3676 sqlite3_context
*context
,
3678 sqlite3_value
**argv
3680 UNUSED_PARAMETER(argc
);
3681 if( sqlite3_value_type(argv
[0])==SQLITE_INTEGER
){
3682 Decimal
*pA
= decimalPow2(sqlite3_value_int(argv
[0]));
3683 decimal_result_sci(context
, pA
);
3691 int sqlite3_decimal_init(
3694 const sqlite3_api_routines
*pApi
3697 static const struct {
3698 const char *zFuncName
;
3701 void (*xFunc
)(sqlite3_context
*,int,sqlite3_value
**);
3703 { "decimal", 1, 0, decimalFunc
},
3704 { "decimal_exp", 1, 1, decimalFunc
},
3705 { "decimal_cmp", 2, 0, decimalCmpFunc
},
3706 { "decimal_add", 2, 0, decimalAddFunc
},
3707 { "decimal_sub", 2, 0, decimalSubFunc
},
3708 { "decimal_mul", 2, 0, decimalMulFunc
},
3709 { "decimal_pow2", 1, 0, decimalPow2Func
},
3712 (void)pzErrMsg
; /* Unused parameter */
3714 SQLITE_EXTENSION_INIT2(pApi
);
3716 for(i
=0; i
<(int)(sizeof(aFunc
)/sizeof(aFunc
[0])) && rc
==SQLITE_OK
; i
++){
3717 rc
= sqlite3_create_function(db
, aFunc
[i
].zFuncName
, aFunc
[i
].nArg
,
3718 SQLITE_UTF8
|SQLITE_INNOCUOUS
|SQLITE_DETERMINISTIC
,
3719 aFunc
[i
].iArg
? db
: 0, aFunc
[i
].xFunc
, 0, 0);
3721 if( rc
==SQLITE_OK
){
3722 rc
= sqlite3_create_window_function(db
, "decimal_sum", 1,
3723 SQLITE_UTF8
|SQLITE_INNOCUOUS
|SQLITE_DETERMINISTIC
, 0,
3724 decimalSumStep
, decimalSumFinalize
,
3725 decimalSumValue
, decimalSumInverse
, 0);
3727 if( rc
==SQLITE_OK
){
3728 rc
= sqlite3_create_collation(db
, "decimal", SQLITE_UTF8
,
3729 0, decimalCollFunc
);
3734 /************************* End ../ext/misc/decimal.c ********************/
3735 #undef sqlite3_base_init
3736 #define sqlite3_base_init sqlite3_base64_init
3737 /************************* Begin ../ext/misc/base64.c ******************/
3741 ** The author disclaims copyright to this source code. In place of
3742 ** a legal notice, here is a blessing:
3744 ** May you do good and not evil.
3745 ** May you find forgiveness for yourself and forgive others.
3746 ** May you share freely, never taking more than you give.
3748 *************************************************************************
3750 ** This is a SQLite extension for converting in either direction
3751 ** between a (binary) blob and base64 text. Base64 can transit a
3752 ** sane USASCII channel unmolested. It also plays nicely in CSV or
3753 ** written as TCL brace-enclosed literals or SQL string literals,
3754 ** and can be used unmodified in XML-like documents.
3756 ** This is an independent implementation of conversions specified in
3757 ** RFC 4648, done on the above date by the author (Larry Brasfield)
3758 ** who thereby has the right to put this into the public domain.
3760 ** The conversions meet RFC 4648 requirements, provided that this
3761 ** C source specifies that line-feeds are included in the encoded
3762 ** data to limit visible line lengths to 72 characters and to
3763 ** terminate any encoded blob having non-zero length.
3765 ** Length limitations are not imposed except that the runtime
3766 ** SQLite string or blob length limits are respected. Otherwise,
3767 ** any length binary sequence can be represented and recovered.
3768 ** Generated base64 sequences, with their line-feeds included,
3769 ** can be concatenated; the result converted back to binary will
3770 ** be the concatenation of the represented binary sequences.
3772 ** This SQLite3 extension creates a function, base64(x), which
3773 ** either: converts text x containing base64 to a returned blob;
3774 ** or converts a blob x to returned text containing base64. An
3775 ** error will be thrown for other input argument types.
3777 ** This code relies on UTF-8 encoding only with respect to the
3778 ** meaning of the first 128 (7-bit) codes matching that of USASCII.
3779 ** It will fail miserably if somehow made to try to convert EBCDIC.
3780 ** Because it is table-driven, it could be enhanced to handle that,
3781 ** but the world and SQLite have moved on from that anachronism.
3783 ** To build the extension:
3784 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
3785 ** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c
3786 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c
3787 ** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c
3788 ** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll
3793 /* #include "sqlite3ext.h" */
3795 #ifndef deliberate_fall_through
3796 /* Quiet some compilers about some of our intentional code. */
3797 # if GCC_VERSION>=7000000
3798 # define deliberate_fall_through __attribute__((fallthrough));
3800 # define deliberate_fall_through
3804 SQLITE_EXTENSION_INIT1
;
3806 #define PC 0x80 /* pad character */
3807 #define WS 0x81 /* whitespace */
3808 #define ND 0x82 /* Not above or digit-value */
3809 #define PAD_CHAR '='
3812 /* typedef unsigned char u8; */
3816 /* Decoding table, ASCII (7-bit) value to base 64 digit value or other */
3817 static const u8 b64DigitValues
[128] = {
3818 /* HT LF VT FF CR */
3819 ND
,ND
,ND
,ND
, ND
,ND
,ND
,ND
, ND
,WS
,WS
,WS
, WS
,WS
,ND
,ND
,
3821 ND
,ND
,ND
,ND
, ND
,ND
,ND
,ND
, ND
,ND
,ND
,ND
, ND
,ND
,ND
,ND
,
3823 WS
,ND
,ND
,ND
, ND
,ND
,ND
,ND
, ND
,ND
,ND
,62, ND
,ND
,ND
,63,
3825 52,53,54,55, 56,57,58,59, 60,61,ND
,ND
, ND
,PC
,ND
,ND
,
3827 ND
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
3829 15,16,17,18, 19,20,21,22, 23,24,25,ND
, ND
,ND
,ND
,ND
,
3831 ND
,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
3833 41,42,43,44, 45,46,47,48, 49,50,51,ND
, ND
,ND
,ND
,ND
3836 static const char b64Numerals
[64+1]
3837 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3839 #define BX_DV_PROTO(c) \
3840 ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80)
3841 #define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80)
3842 #define IS_BX_WS(bdp) ((bdp)==WS)
3843 #define IS_BX_PAD(bdp) ((bdp)==PC)
3844 #define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)])
3845 /* Width of base64 lines. Should be an integer multiple of 4. */
3846 #define B64_DARK_MAX 72
3848 /* Encode a byte buffer into base64 text with linefeeds appended to limit
3849 ** encoded group lengths to B64_DARK_MAX or to terminate the last group.
3851 static char* toBase64( u8
*pIn
, int nbIn
, char *pOut
){
3854 /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */
3855 pOut
[0] = BX_NUMERAL(pIn
[0]>>2);
3856 pOut
[1] = BX_NUMERAL(((pIn
[0]<<4)|(pIn
[1]>>4))&0x3f);
3857 pOut
[2] = BX_NUMERAL(((pIn
[1]&0xf)<<2)|(pIn
[2]>>6));
3858 pOut
[3] = BX_NUMERAL(pIn
[2]&0x3f);
3862 if( (nCol
+= 4)>=B64_DARK_MAX
|| nbIn
<=0 ){
3868 signed char nco
= nbIn
+1;
3870 unsigned long qv
= *pIn
++;
3871 for( nbe
=1; nbe
<3; ++nbe
){
3873 if( nbe
<nbIn
) qv
|= *pIn
++;
3875 for( nbe
=3; nbe
>=0; --nbe
){
3876 char ce
= (nbe
<nco
)? BX_NUMERAL((u8
)(qv
& 0x3f)) : PAD_CHAR
;
3887 /* Skip over text which is not base64 numeral(s). */
3888 static char * skipNonB64( char *s
, int nc
){
3890 while( nc
-- > 0 && (c
= *s
) && !IS_BX_DIGIT(BX_DV_PROTO(c
)) ) ++s
;
3894 /* Decode base64 text into a byte buffer. */
3895 static u8
* fromBase64( char *pIn
, int ncIn
, u8
*pOut
){
3896 if( ncIn
>0 && pIn
[ncIn
-1]=='\n' ) --ncIn
;
3897 while( ncIn
>0 && *pIn
!=PAD_CHAR
){
3898 static signed char nboi
[] = { 0, 0, 1, 2, 3 };
3899 char *pUse
= skipNonB64(pIn
, ncIn
);
3900 unsigned long qv
= 0L;
3902 ncIn
-= (pUse
- pIn
);
3904 nti
= (ncIn
>4)? 4 : ncIn
;
3908 for( nac
=0; nac
<4; ++nac
){
3909 char c
= (nac
<nti
)? *pIn
++ : b64Numerals
[0];
3910 u8 bdp
= BX_DV_PROTO(c
);
3913 /* Treat dark non-digits as pad, but they terminate decode too. */
3915 deliberate_fall_through
;
3917 /* Treat whitespace as pad and terminate this group.*/
3919 deliberate_fall_through
;
3923 deliberate_fall_through
;
3924 default: /* bdp is the digit value. */
3931 pOut
[2] = (qv
) & 0xff;
3933 pOut
[1] = (qv
>>8) & 0xff;
3935 pOut
[0] = (qv
>>16) & 0xff;
3942 /* This function does the work for the SQLite base64(x) UDF. */
3943 static void base64(sqlite3_context
*context
, int na
, sqlite3_value
*av
[]){
3944 int nb
, nc
, nv
= sqlite3_value_bytes(av
[0]);
3945 int nvMax
= sqlite3_limit(sqlite3_context_db_handle(context
),
3946 SQLITE_LIMIT_LENGTH
, -1);
3950 switch( sqlite3_value_type(av
[0]) ){
3953 nc
= 4*(nv
+2/3); /* quads needed */
3954 nc
+= (nc
+(B64_DARK_MAX
-1))/B64_DARK_MAX
+ 1; /* LFs and a 0-terminator */
3956 sqlite3_result_error(context
, "blob expanded to base64 too big", -1);
3959 bBuf
= (u8
*)sqlite3_value_blob(av
[0]);
3961 if( SQLITE_NOMEM
==sqlite3_errcode(sqlite3_context_db_handle(context
)) ){
3964 sqlite3_result_text(context
,"",-1,SQLITE_STATIC
);
3967 cBuf
= sqlite3_malloc(nc
);
3968 if( !cBuf
) goto memFail
;
3969 nc
= (int)(toBase64(bBuf
, nb
, cBuf
) - cBuf
);
3970 sqlite3_result_text(context
, cBuf
, nc
, sqlite3_free
);
3974 nb
= 3*((nv
+3)/4); /* may overestimate due to LF and padding */
3976 sqlite3_result_error(context
, "blob from base64 may be too big", -1);
3981 cBuf
= (char *)sqlite3_value_text(av
[0]);
3983 if( SQLITE_NOMEM
==sqlite3_errcode(sqlite3_context_db_handle(context
)) ){
3986 sqlite3_result_zeroblob(context
, 0);
3989 bBuf
= sqlite3_malloc(nb
);
3990 if( !bBuf
) goto memFail
;
3991 nb
= (int)(fromBase64(cBuf
, nc
, bBuf
) - bBuf
);
3992 sqlite3_result_blob(context
, bBuf
, nb
, sqlite3_free
);
3995 sqlite3_result_error(context
, "base64 accepts only blob or text", -1);
4000 sqlite3_result_error(context
, "base64 OOM", -1);
4004 ** Establish linkage to running SQLite library.
4006 #ifndef SQLITE_SHELL_EXTFUNCS
4010 int sqlite3_base_init
4012 static int sqlite3_base64_init
4014 (sqlite3
*db
, char **pzErr
, const sqlite3_api_routines
*pApi
){
4015 SQLITE_EXTENSION_INIT2(pApi
);
4017 return sqlite3_create_function
4019 SQLITE_DETERMINISTIC
|SQLITE_INNOCUOUS
|SQLITE_DIRECTONLY
|SQLITE_UTF8
,
4024 ** Define some macros to allow this extension to be built into the shell
4025 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
4026 ** allows shell.c, as distributed, to have this extension built in.
4028 #define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0)
4029 #define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
4031 /************************* End ../ext/misc/base64.c ********************/
4032 #undef sqlite3_base_init
4033 #define sqlite3_base_init sqlite3_base85_init
4034 #define OMIT_BASE85_CHECKER
4035 /************************* Begin ../ext/misc/base85.c ******************/
4039 ** The author disclaims copyright to this source code. In place of
4040 ** a legal notice, here is a blessing:
4042 ** May you do good and not evil.
4043 ** May you find forgiveness for yourself and forgive others.
4044 ** May you share freely, never taking more than you give.
4046 *************************************************************************
4048 ** This is a utility for converting binary to base85 or vice-versa.
4049 ** It can be built as a standalone program or an SQLite3 extension.
4051 ** Much like base64 representations, base85 can be sent through a
4052 ** sane USASCII channel unmolested. It also plays nicely in CSV or
4053 ** written as TCL brace-enclosed literals or SQL string literals.
4054 ** It is not suited for unmodified use in XML-like documents.
4056 ** The encoding used resembles Ascii85, but was devised by the author
4057 ** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85
4058 ** variant sources existed, in the 1984 timeframe on a VAX mainframe.
4059 ** Further, this is an independent implementation of a base85 system.
4060 ** Hence, the author has rightfully put this into the public domain.
4062 ** Base85 numerals are taken from the set of 7-bit USASCII codes,
4063 ** excluding control characters and Space ! " ' ( ) { | } ~ Del
4064 ** in code order representing digit values 0 to 84 (base 10.)
4066 ** Groups of 4 bytes, interpreted as big-endian 32-bit values,
4067 ** are represented as 5-digit base85 numbers with MS to LS digit
4068 ** order. Groups of 1-3 bytes are represented with 2-4 digits,
4069 ** still big-endian but 8-24 bit values. (Using big-endian yields
4070 ** the simplest transition to byte groups smaller than 4 bytes.
4071 ** These byte groups can also be considered base-256 numbers.)
4072 ** Groups of 0 bytes are represented with 0 digits and vice-versa.
4073 ** No pad characters are used; Encoded base85 numeral sequence
4074 ** (aka "group") length maps 1-to-1 to the decoded binary length.
4076 ** Any character not in the base85 numeral set delimits groups.
4077 ** When base85 is streamed or stored in containers of indefinite
4078 ** size, newline is used to separate it into sub-sequences of no
4079 ** more than 80 digits so that fgets() can be used to read it.
4081 ** Length limitations are not imposed except that the runtime
4082 ** SQLite string or blob length limits are respected. Otherwise,
4083 ** any length binary sequence can be represented and recovered.
4084 ** Base85 sequences can be concatenated by separating them with
4085 ** a non-base85 character; the conversion to binary will then
4086 ** be the concatenation of the represented binary sequences.
4088 ** The standalone program either converts base85 on stdin to create
4089 ** a binary file or converts a binary file to base85 on stdout.
4090 ** Read or make it blurt its help for invocation details.
4092 ** The SQLite3 extension creates a function, base85(x), which will
4093 ** either convert text base85 to a blob or a blob to text base85
4094 ** and return the result (or throw an error for other types.)
4095 ** Unless built with OMIT_BASE85_CHECKER defined, it also creates a
4096 ** function, is_base85(t), which returns 1 iff the text t contains
4097 ** nothing other than base85 numerals and whitespace, or 0 otherwise.
4099 ** To build the extension:
4100 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
4101 ** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted.
4102 ** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c
4103 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c
4104 ** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c
4105 ** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll
4107 ** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg.
4108 ** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85
4109 ** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c
4110 ** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c
4117 #ifndef OMIT_BASE85_CHECKER
4121 #ifndef BASE85_STANDALONE
4123 /* # include "sqlite3ext.h" */
4125 SQLITE_EXTENSION_INIT1
;
4133 # define setmode(fd,m)
4136 static char *zHelp
=
4137 "Usage: base85 <dirFlag> <binFile>\n"
4138 " <dirFlag> is either -r to read or -w to write <binFile>,\n"
4139 " content to be converted to/from base85 on stdout/stdin.\n"
4140 " <binFile> names a binary file to be rendered or created.\n"
4141 " Or, the name '-' refers to the stdin or stdout stream.\n"
4144 static void sayHelp(){
4145 printf("%s", zHelp
);
4150 /* typedef unsigned char u8; */
4154 /* Classify c according to interval within USASCII set w.r.t. base85
4155 * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not.
4157 #define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z'))
4159 /* Provide digitValue to b85Numeral offset as a function of above class. */
4160 static u8 b85_cOffset
[] = { 0, '#', 0, '*'-4, 0 };
4161 #define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)]
4163 /* Say whether c is a base85 numeral. */
4164 #define IS_B85( c ) (B85_CLASS(c) & 1)
4166 #if 0 /* Not used, */
4167 static u8
base85DigitValue( char c
){
4168 u8 dv
= (u8
)(c
- '#');
4169 if( dv
>87 ) return 0xff;
4170 return (dv
> 3)? dv
-3 : dv
;
4174 /* Width of base64 lines. Should be an integer multiple of 5. */
4175 #define B85_DARK_MAX 80
4178 static char * skipNonB85( char *s
, int nc
){
4180 while( nc
-- > 0 && (c
= *s
) && !IS_B85(c
) ) ++s
;
4184 /* Convert small integer, known to be in 0..84 inclusive, to base85 numeral.
4185 * Do not use the macro form with argument expression having a side-effect.*/
4187 static char base85Numeral( u8 b
){
4188 return (b
< 4)? (char)(b
+ '#') : (char)(b
- 4 + '*');
4191 # define base85Numeral( dn )\
4192 ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*')))
4195 static char *putcs(char *pc
, char *s
){
4197 while( (c
= *s
++)!=0 ) *pc
++ = c
;
4201 /* Encode a byte buffer into base85 text. If pSep!=0, it's a C string
4202 ** to be appended to encoded groups to limit their length to B85_DARK_MAX
4203 ** or to terminate the last group (to aid concatenation.)
4205 static char* toBase85( u8
*pIn
, int nbIn
, char *pOut
, char *pSep
){
4209 unsigned long qbv
= (((unsigned long)pIn
[0])<<24) |
4210 (pIn
[1]<<16) | (pIn
[2]<<8) | pIn
[3];
4212 unsigned nqv
= (unsigned)(qbv
/85UL);
4213 unsigned char dv
= qbv
- 85UL*nqv
;
4215 pOut
[--nco
] = base85Numeral(dv
);
4220 if( pSep
&& (nCol
+= 5)>=B85_DARK_MAX
){
4221 pOut
= putcs(pOut
, pSep
);
4227 unsigned long qv
= *pIn
++;
4229 while( nbe
++ < nbIn
){
4230 qv
= (qv
<<8) | *pIn
++;
4234 u8 dv
= (u8
)(qv
% 85);
4236 pOut
[--nco
] = base85Numeral(dv
);
4240 if( pSep
&& nCol
>0 ) pOut
= putcs(pOut
, pSep
);
4245 /* Decode base85 text into a byte buffer. */
4246 static u8
* fromBase85( char *pIn
, int ncIn
, u8
*pOut
){
4247 if( ncIn
>0 && pIn
[ncIn
-1]=='\n' ) --ncIn
;
4249 static signed char nboi
[] = { 0, 0, 1, 2, 3, 4 };
4250 char *pUse
= skipNonB85(pIn
, ncIn
);
4251 unsigned long qv
= 0L;
4253 ncIn
-= (pUse
- pIn
);
4255 nti
= (ncIn
>5)? 5 : ncIn
;
4260 u8 cdo
= B85_DNOS(c
);
4263 qv
= 85 * qv
+ (c
- cdo
);
4266 nbo
-= nti
; /* Adjust for early (non-digit) end of group. */
4269 *pOut
++ = (qv
>> 24)&0xff;
4271 *pOut
++ = (qv
>> 16)&0xff;
4273 *pOut
++ = (qv
>> 8)&0xff;
4283 #ifndef OMIT_BASE85_CHECKER
4284 /* Say whether input char sequence is all (base85 and/or whitespace).*/
4285 static int allBase85( char *p
, int len
){
4287 while( len
-- > 0 && (c
= *p
++) != 0 ){
4288 if( !IS_B85(c
) && !isspace(c
) ) return 0;
4294 #ifndef BASE85_STANDALONE
4296 # ifndef OMIT_BASE85_CHECKER
4297 /* This function does the work for the SQLite is_base85(t) UDF. */
4298 static void is_base85(sqlite3_context
*context
, int na
, sqlite3_value
*av
[]){
4300 switch( sqlite3_value_type(av
[0]) ){
4303 int rv
= allBase85( (char *)sqlite3_value_text(av
[0]),
4304 sqlite3_value_bytes(av
[0]) );
4305 sqlite3_result_int(context
, rv
);
4309 sqlite3_result_null(context
);
4312 sqlite3_result_error(context
, "is_base85 accepts only text or NULL", -1);
4318 /* This function does the work for the SQLite base85(x) UDF. */
4319 static void base85(sqlite3_context
*context
, int na
, sqlite3_value
*av
[]){
4320 int nb
, nc
, nv
= sqlite3_value_bytes(av
[0]);
4321 int nvMax
= sqlite3_limit(sqlite3_context_db_handle(context
),
4322 SQLITE_LIMIT_LENGTH
, -1);
4326 switch( sqlite3_value_type(av
[0]) ){
4329 /* ulongs tail newlines tailenc+nul*/
4330 nc
= 5*(nv
/4) + nv
%4 + nv
/64+1 + 2;
4332 sqlite3_result_error(context
, "blob expanded to base85 too big", -1);
4335 bBuf
= (u8
*)sqlite3_value_blob(av
[0]);
4337 if( SQLITE_NOMEM
==sqlite3_errcode(sqlite3_context_db_handle(context
)) ){
4340 sqlite3_result_text(context
,"",-1,SQLITE_STATIC
);
4343 cBuf
= sqlite3_malloc(nc
);
4344 if( !cBuf
) goto memFail
;
4345 nc
= (int)(toBase85(bBuf
, nb
, cBuf
, "\n") - cBuf
);
4346 sqlite3_result_text(context
, cBuf
, nc
, sqlite3_free
);
4350 nb
= 4*(nv
/5) + nv
%5; /* may overestimate */
4352 sqlite3_result_error(context
, "blob from base85 may be too big", -1);
4357 cBuf
= (char *)sqlite3_value_text(av
[0]);
4359 if( SQLITE_NOMEM
==sqlite3_errcode(sqlite3_context_db_handle(context
)) ){
4362 sqlite3_result_zeroblob(context
, 0);
4365 bBuf
= sqlite3_malloc(nb
);
4366 if( !bBuf
) goto memFail
;
4367 nb
= (int)(fromBase85(cBuf
, nc
, bBuf
) - bBuf
);
4368 sqlite3_result_blob(context
, bBuf
, nb
, sqlite3_free
);
4371 sqlite3_result_error(context
, "base85 accepts only blob or text.", -1);
4376 sqlite3_result_error(context
, "base85 OOM", -1);
4380 ** Establish linkage to running SQLite library.
4382 #ifndef SQLITE_SHELL_EXTFUNCS
4386 int sqlite3_base_init
4388 static int sqlite3_base85_init
4390 (sqlite3
*db
, char **pzErr
, const sqlite3_api_routines
*pApi
){
4391 SQLITE_EXTENSION_INIT2(pApi
);
4393 # ifndef OMIT_BASE85_CHECKER
4395 int rc
= sqlite3_create_function
4396 (db
, "is_base85", 1,
4397 SQLITE_DETERMINISTIC
|SQLITE_INNOCUOUS
|SQLITE_UTF8
,
4398 0, is_base85
, 0, 0);
4399 if( rc
!=SQLITE_OK
) return rc
;
4402 return sqlite3_create_function
4404 SQLITE_DETERMINISTIC
|SQLITE_INNOCUOUS
|SQLITE_DIRECTONLY
|SQLITE_UTF8
,
4409 ** Define some macros to allow this extension to be built into the shell
4410 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
4411 ** allows shell.c, as distributed, to have this extension built in.
4413 # define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0)
4414 # define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
4416 #else /* standalone program */
4418 int main(int na
, char *av
[]){
4421 u8 bBuf
[4*(B85_DARK_MAX
/5)];
4422 char cBuf
[5*(sizeof(bBuf
)/4)+2];
4424 # ifndef OMIT_BASE85_CHECKER
4428 FILE *fb
= 0, *foc
= 0;
4429 char fmode
[3] = "xb";
4430 if( na
< 3 || av
[1][0]!='-' || (rw
= av
[1][1])==0 || (rw
!='r' && rw
!='w') ){
4435 if( av
[2][0]=='-' && av
[2][1]==0 ){
4439 setmode(fileno(stdin
), O_BINARY
);
4443 setmode(fileno(stdout
), O_BINARY
);
4447 fb
= fopen(av
[2], fmode
);
4451 fprintf(stderr
, "Cannot open %s for %c\n", av
[2], rw
);
4456 while( (nio
= fread( bBuf
, 1, sizeof(bBuf
), fb
))>0 ){
4457 toBase85( bBuf
, (int)nio
, cBuf
, 0 );
4458 fprintf(stdout
, "%s\n", cBuf
);
4462 while( 0 != fgets(cBuf
, sizeof(cBuf
), stdin
) ){
4463 int nc
= strlen(cBuf
);
4464 size_t nbo
= fromBase85( cBuf
, nc
, bBuf
) - bBuf
;
4465 if( 1 != fwrite(bBuf
, nbo
, 1, fb
) ) rc
= 1;
4466 # ifndef OMIT_BASE85_CHECKER
4467 b85Clean
&= allBase85( cBuf
, nc
);
4475 if( foc
) fclose(foc
);
4477 # ifndef OMIT_BASE85_CHECKER
4479 fprintf(stderr
, "Base85 input had non-base85 dark or control content.\n");
4487 /************************* End ../ext/misc/base85.c ********************/
4488 /************************* Begin ../ext/misc/ieee754.c ******************/
4492 ** The author disclaims copyright to this source code. In place of
4493 ** a legal notice, here is a blessing:
4495 ** May you do good and not evil.
4496 ** May you find forgiveness for yourself and forgive others.
4497 ** May you share freely, never taking more than you give.
4499 ******************************************************************************
4501 ** This SQLite extension implements functions for the exact display
4502 ** and input of IEEE754 Binary64 floating-point numbers.
4507 ** In the first form, the value X should be a floating-point number.
4508 ** The function will return a string of the form 'ieee754(Y,Z)' where
4509 ** Y and Z are integers such that X==Y*pow(2,Z).
4511 ** In the second form, Y and Z are integers which are the mantissa and
4512 ** base-2 exponent of a new floating point number. The function returns
4513 ** a floating-point value equal to Y*pow(2,Z).
4517 ** ieee754(2.0) -> 'ieee754(2,0)'
4518 ** ieee754(45.25) -> 'ieee754(181,-2)'
4519 ** ieee754(2, 0) -> 2.0
4520 ** ieee754(181, -2) -> 45.25
4522 ** Two additional functions break apart the one-argument ieee754()
4523 ** result into separate integer values:
4525 ** ieee754_mantissa(45.25) -> 181
4526 ** ieee754_exponent(45.25) -> -2
4528 ** These functions convert binary64 numbers into blobs and back again.
4530 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
4531 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
4533 ** In all single-argument functions, if the argument is an 8-byte blob
4534 ** then that blob is interpreted as a big-endian binary64 value.
4537 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
4538 ** -----------------------------------------------
4540 ** This extension in combination with the separate 'decimal' extension
4541 ** can be used to compute the exact decimal representation of binary64
4542 ** values. To begin, first compute a table of exponent values:
4544 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
4545 ** WITH RECURSIVE c(x,v) AS (
4548 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
4549 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
4550 ** WITH RECURSIVE c(x,v) AS (
4553 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
4554 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
4556 ** Then, to compute the exact decimal representation of a floating
4557 ** point value (the value 47.49 is used in the example) do:
4559 ** WITH c(n) AS (VALUES(47.49))
4560 ** ---------------^^^^^---- Replace with whatever you want
4561 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
4562 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
4564 ** Here is a query to show various boundry values for the binary64
4567 ** WITH c(name,bin) AS (VALUES
4568 ** ('minimum positive value', x'0000000000000001'),
4569 ** ('maximum subnormal value', x'000fffffffffffff'),
4570 ** ('mininum positive nornal value', x'0010000000000000'),
4571 ** ('maximum value', x'7fefffffffffffff'))
4572 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
4573 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
4576 /* #include "sqlite3ext.h" */
4577 SQLITE_EXTENSION_INIT1
4581 /* Mark a function parameter as unused, to suppress nuisance compiler
4583 #ifndef UNUSED_PARAMETER
4584 # define UNUSED_PARAMETER(X) (void)(X)
4588 ** Implementation of the ieee754() function
4590 static void ieee754func(
4591 sqlite3_context
*context
,
4593 sqlite3_value
**argv
4601 assert( sizeof(m
)==sizeof(r
) );
4602 if( sqlite3_value_type(argv
[0])==SQLITE_BLOB
4603 && sqlite3_value_bytes(argv
[0])==sizeof(r
)
4605 const unsigned char *x
= sqlite3_value_blob(argv
[0]);
4607 sqlite3_uint64 v
= 0;
4608 for(i
=0; i
<sizeof(r
); i
++){
4611 memcpy(&r
, &v
, sizeof(r
));
4613 r
= sqlite3_value_double(argv
[0]);
4621 memcpy(&a
,&r
,sizeof(a
));
4627 m
= a
& ((((sqlite3_int64
)1)<<52)-1);
4631 m
|= ((sqlite3_int64
)1)<<52;
4633 while( e
<1075 && m
>0 && (m
&1)==0 ){
4639 switch( *(int*)sqlite3_user_data(context
) ){
4641 sqlite3_snprintf(sizeof(zResult
), zResult
, "ieee754(%lld,%d)",
4643 sqlite3_result_text(context
, zResult
, -1, SQLITE_TRANSIENT
);
4646 sqlite3_result_int64(context
, m
);
4649 sqlite3_result_int(context
, e
-1075);
4653 sqlite3_int64 m
, e
, a
;
4656 m
= sqlite3_value_int64(argv
[0]);
4657 e
= sqlite3_value_int64(argv
[1]);
4659 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */
4662 }else if( e
<-10000 ){
4670 }else if( m
==0 && e
>-1000 && e
<1000 ){
4671 sqlite3_result_double(context
, 0.0);
4674 while( (m
>>32)&0xffe00000 ){
4678 while( m
!=0 && ((m
>>32)&0xfff00000)==0 ){
4691 }else if( e
>0x7ff ){
4694 a
= m
& ((((sqlite3_int64
)1)<<52)-1);
4696 if( isNeg
) a
|= ((sqlite3_uint64
)1)<<63;
4697 memcpy(&r
, &a
, sizeof(r
));
4698 sqlite3_result_double(context
, r
);
4703 ** Functions to convert between blobs and floats.
4705 static void ieee754func_from_blob(
4706 sqlite3_context
*context
,
4708 sqlite3_value
**argv
4710 UNUSED_PARAMETER(argc
);
4711 if( sqlite3_value_type(argv
[0])==SQLITE_BLOB
4712 && sqlite3_value_bytes(argv
[0])==sizeof(double)
4715 const unsigned char *x
= sqlite3_value_blob(argv
[0]);
4717 sqlite3_uint64 v
= 0;
4718 for(i
=0; i
<sizeof(r
); i
++){
4721 memcpy(&r
, &v
, sizeof(r
));
4722 sqlite3_result_double(context
, r
);
4725 static void ieee754func_to_blob(
4726 sqlite3_context
*context
,
4728 sqlite3_value
**argv
4730 UNUSED_PARAMETER(argc
);
4731 if( sqlite3_value_type(argv
[0])==SQLITE_FLOAT
4732 || sqlite3_value_type(argv
[0])==SQLITE_INTEGER
4734 double r
= sqlite3_value_double(argv
[0]);
4736 unsigned char a
[sizeof(r
)];
4738 memcpy(&v
, &r
, sizeof(r
));
4739 for(i
=1; i
<=sizeof(r
); i
++){
4740 a
[sizeof(r
)-i
] = v
&0xff;
4743 sqlite3_result_blob(context
, a
, sizeof(r
), SQLITE_TRANSIENT
);
4748 ** SQL Function: ieee754_inc(r,N)
4750 ** Move the floating point value r by N quantums and return the new
4753 ** Behind the scenes: this routine merely casts r into a 64-bit unsigned
4754 ** integer, adds N, then casts the value back into float.
4756 ** Example: To find the smallest positive number:
4758 ** SELECT ieee754_inc(0.0,+1);
4760 static void ieee754inc(
4761 sqlite3_context
*context
,
4763 sqlite3_value
**argv
4767 sqlite3_uint64 m1
, m2
;
4769 UNUSED_PARAMETER(argc
);
4770 r
= sqlite3_value_double(argv
[0]);
4771 N
= sqlite3_value_int64(argv
[1]);
4774 memcpy(&r2
, &m2
, 8);
4775 sqlite3_result_double(context
, r2
);
4782 int sqlite3_ieee_init(
4785 const sqlite3_api_routines
*pApi
4787 static const struct {
4791 void (*xFunc
)(sqlite3_context
*,int,sqlite3_value
**);
4793 { "ieee754", 1, 0, ieee754func
},
4794 { "ieee754", 2, 0, ieee754func
},
4795 { "ieee754_mantissa", 1, 1, ieee754func
},
4796 { "ieee754_exponent", 1, 2, ieee754func
},
4797 { "ieee754_to_blob", 1, 0, ieee754func_to_blob
},
4798 { "ieee754_from_blob", 1, 0, ieee754func_from_blob
},
4799 { "ieee754_inc", 2, 0, ieee754inc
},
4803 SQLITE_EXTENSION_INIT2(pApi
);
4804 (void)pzErrMsg
; /* Unused parameter */
4805 for(i
=0; i
<sizeof(aFunc
)/sizeof(aFunc
[0]) && rc
==SQLITE_OK
; i
++){
4806 rc
= sqlite3_create_function(db
, aFunc
[i
].zFName
, aFunc
[i
].nArg
,
4807 SQLITE_UTF8
|SQLITE_INNOCUOUS
,
4808 (void*)&aFunc
[i
].iAux
,
4809 aFunc
[i
].xFunc
, 0, 0);
4814 /************************* End ../ext/misc/ieee754.c ********************/
4815 /************************* Begin ../ext/misc/series.c ******************/
4817 ** 2015-08-18, 2023-04-28
4819 ** The author disclaims copyright to this source code. In place of
4820 ** a legal notice, here is a blessing:
4822 ** May you do good and not evil.
4823 ** May you find forgiveness for yourself and forgive others.
4824 ** May you share freely, never taking more than you give.
4826 *************************************************************************
4828 ** This file demonstrates how to create a table-valued-function using
4829 ** a virtual table. This demo implements the generate_series() function
4830 ** which gives the same results as the eponymous function in PostgreSQL,
4831 ** within the limitation that its arguments are signed 64-bit integers.
4833 ** Considering its equivalents to generate_series(start,stop,step): A
4834 ** value V[n] sequence is produced for integer n ascending from 0 where
4835 ** ( V[n] == start + n * step && sgn(V[n] - stop) * sgn(step) >= 0 )
4836 ** for each produced value (independent of production time ordering.)
4838 ** All parameters must be either integer or convertable to integer.
4839 ** The start parameter is required.
4840 ** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
4841 ** The step parameter defaults to 1 and 0 is treated as 1.
4845 ** SELECT * FROM generate_series(0,100,5);
4847 ** The query above returns integers from 0 through 100 counting by steps
4850 ** SELECT * FROM generate_series(0,100);
4852 ** Integers from 0 through 100 with a step size of 1.
4854 ** SELECT * FROM generate_series(20) LIMIT 10;
4856 ** Integers 20 through 29.
4858 ** SELECT * FROM generate_series(0,-100,-5);
4860 ** Integers 0 -5 -10 ... -100.
4862 ** SELECT * FROM generate_series(0,-1);
4868 ** The generate_series "function" is really a virtual table with the
4869 ** following schema:
4871 ** CREATE TABLE generate_series(
4878 ** The virtual table also has a rowid, logically equivalent to n+1 where
4879 ** "n" is the ascending integer in the aforesaid production definition.
4881 ** Function arguments in queries against this virtual table are translated
4882 ** into equality constraints against successive hidden columns. In other
4883 ** words, the following pairs of queries are equivalent to each other:
4885 ** SELECT * FROM generate_series(0,100,5);
4886 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
4888 ** SELECT * FROM generate_series(0,100);
4889 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
4891 ** SELECT * FROM generate_series(20) LIMIT 10;
4892 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
4894 ** The generate_series virtual table implementation leaves the xCreate method
4895 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
4896 ** TABLE command with "generate_series" as the USING argument. Instead, there
4897 ** is a single generate_series virtual table that is always available without
4898 ** having to be created first.
4900 ** The xBestIndex method looks for equality constraints against the hidden
4901 ** start, stop, and step columns, and if present, it uses those constraints
4902 ** to bound the sequence of generated values. If the equality constraints
4903 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
4904 ** xBestIndex returns a small cost when both start and stop are available,
4905 ** and a very large cost if either start or stop are unavailable. This
4906 ** encourages the query planner to order joins such that the bounds of the
4907 ** series are well-defined.
4909 /* #include "sqlite3ext.h" */
4910 SQLITE_EXTENSION_INIT1
4915 #ifndef SQLITE_OMIT_VIRTUALTABLE
4917 ** Return that member of a generate_series(...) sequence whose 0-based
4918 ** index is ix. The 0th member is given by smBase. The sequence members
4919 ** progress per ix increment by smStep.
4921 static sqlite3_int64
genSeqMember(sqlite3_int64 smBase
,
4922 sqlite3_int64 smStep
,
4924 if( ix
>=(sqlite3_uint64
)LLONG_MAX
){
4925 /* Get ix into signed i64 range. */
4926 ix
-= (sqlite3_uint64
)LLONG_MAX
;
4927 /* With 2's complement ALU, this next can be 1 step, but is split into
4928 * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
4929 smBase
+= (LLONG_MAX
/2) * smStep
;
4930 smBase
+= (LLONG_MAX
- LLONG_MAX
/2) * smStep
;
4932 /* Under UBSAN (or on 1's complement machines), must do this last term
4933 * in steps to avoid the dreaded (and harmless) signed multiply overlow. */
4935 sqlite3_int64 ix2
= (sqlite3_int64
)ix
/2;
4936 smBase
+= ix2
*smStep
;
4939 return smBase
+ ((sqlite3_int64
)ix
)*smStep
;
4942 /* typedef unsigned char u8; */
4944 typedef struct SequenceSpec
{
4945 sqlite3_int64 iBase
; /* Starting value ("start") */
4946 sqlite3_int64 iTerm
; /* Given terminal value ("stop") */
4947 sqlite3_int64 iStep
; /* Increment ("step") */
4948 sqlite3_uint64 uSeqIndexMax
; /* maximum sequence index (aka "n") */
4949 sqlite3_uint64 uSeqIndexNow
; /* Current index during generation */
4950 sqlite3_int64 iValueNow
; /* Current value during generation */
4951 u8 isNotEOF
; /* Sequence generation not exhausted */
4952 u8 isReversing
; /* Sequence is being reverse generated */
4956 ** Prepare a SequenceSpec for use in generating an integer series
4957 ** given initialized iBase, iTerm and iStep values. Sequence is
4958 ** initialized per given isReversing. Other members are computed.
4960 static void setupSequence( SequenceSpec
*pss
){
4962 pss
->uSeqIndexMax
= 0;
4964 bSameSigns
= (pss
->iBase
< 0)==(pss
->iTerm
< 0);
4965 if( pss
->iTerm
< pss
->iBase
){
4966 sqlite3_uint64 nuspan
= 0;
4968 nuspan
= (sqlite3_uint64
)(pss
->iBase
- pss
->iTerm
);
4970 /* Under UBSAN (or on 1's complement machines), must do this in steps.
4971 * In this clause, iBase>=0 and iTerm<0 . */
4973 nuspan
+= pss
->iBase
;
4974 nuspan
+= -(pss
->iTerm
+1);
4978 if( nuspan
==ULONG_MAX
){
4979 pss
->uSeqIndexMax
= ( pss
->iStep
>LLONG_MIN
)? nuspan
/-pss
->iStep
: 1;
4980 }else if( pss
->iStep
>LLONG_MIN
){
4981 pss
->uSeqIndexMax
= nuspan
/-pss
->iStep
;
4984 }else if( pss
->iTerm
> pss
->iBase
){
4985 sqlite3_uint64 puspan
= 0;
4987 puspan
= (sqlite3_uint64
)(pss
->iTerm
- pss
->iBase
);
4989 /* Under UBSAN (or on 1's complement machines), must do this in steps.
4990 * In this clause, iTerm>=0 and iBase<0 . */
4992 puspan
+= pss
->iTerm
;
4993 puspan
+= -(pss
->iBase
+1);
4997 pss
->uSeqIndexMax
= puspan
/pss
->iStep
;
4999 }else if( pss
->iTerm
== pss
->iBase
){
5001 pss
->uSeqIndexMax
= 0;
5003 pss
->uSeqIndexNow
= (pss
->isReversing
)? pss
->uSeqIndexMax
: 0;
5004 pss
->iValueNow
= (pss
->isReversing
)
5005 ? genSeqMember(pss
->iBase
, pss
->iStep
, pss
->uSeqIndexMax
)
5010 ** Progress sequence generator to yield next value, if any.
5011 ** Leave its state to either yield next value or be at EOF.
5012 ** Return whether there is a next value, or 0 at EOF.
5014 static int progressSequence( SequenceSpec
*pss
){
5015 if( !pss
->isNotEOF
) return 0;
5016 if( pss
->isReversing
){
5017 if( pss
->uSeqIndexNow
> 0 ){
5018 pss
->uSeqIndexNow
--;
5019 pss
->iValueNow
-= pss
->iStep
;
5024 if( pss
->uSeqIndexNow
< pss
->uSeqIndexMax
){
5025 pss
->uSeqIndexNow
++;
5026 pss
->iValueNow
+= pss
->iStep
;
5031 return pss
->isNotEOF
;
5034 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5035 ** serve as the underlying representation of a cursor that scans
5036 ** over rows of the result
5038 typedef struct series_cursor series_cursor
;
5039 struct series_cursor
{
5040 sqlite3_vtab_cursor base
; /* Base class - must be first */
5041 SequenceSpec ss
; /* (this) Derived class data */
5045 ** The seriesConnect() method is invoked to create a new
5046 ** series_vtab that describes the generate_series virtual table.
5048 ** Think of this routine as the constructor for series_vtab objects.
5050 ** All this routine needs to do is:
5052 ** (1) Allocate the series_vtab object and initialize all fields.
5054 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5055 ** result set of queries against generate_series will look like.
5057 static int seriesConnect(
5060 int argcUnused
, const char *const*argvUnused
,
5061 sqlite3_vtab
**ppVtab
,
5067 /* Column numbers */
5068 #define SERIES_COLUMN_VALUE 0
5069 #define SERIES_COLUMN_START 1
5070 #define SERIES_COLUMN_STOP 2
5071 #define SERIES_COLUMN_STEP 3
5077 rc
= sqlite3_declare_vtab(db
,
5078 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5079 if( rc
==SQLITE_OK
){
5080 pNew
= *ppVtab
= sqlite3_malloc( sizeof(*pNew
) );
5081 if( pNew
==0 ) return SQLITE_NOMEM
;
5082 memset(pNew
, 0, sizeof(*pNew
));
5083 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
5089 ** This method is the destructor for series_cursor objects.
5091 static int seriesDisconnect(sqlite3_vtab
*pVtab
){
5092 sqlite3_free(pVtab
);
5097 ** Constructor for a new series_cursor object.
5099 static int seriesOpen(sqlite3_vtab
*pUnused
, sqlite3_vtab_cursor
**ppCursor
){
5100 series_cursor
*pCur
;
5102 pCur
= sqlite3_malloc( sizeof(*pCur
) );
5103 if( pCur
==0 ) return SQLITE_NOMEM
;
5104 memset(pCur
, 0, sizeof(*pCur
));
5105 *ppCursor
= &pCur
->base
;
5110 ** Destructor for a series_cursor.
5112 static int seriesClose(sqlite3_vtab_cursor
*cur
){
5119 ** Advance a series_cursor to its next row of output.
5121 static int seriesNext(sqlite3_vtab_cursor
*cur
){
5122 series_cursor
*pCur
= (series_cursor
*)cur
;
5123 progressSequence( & pCur
->ss
);
5128 ** Return values of columns for the row at which the series_cursor
5129 ** is currently pointing.
5131 static int seriesColumn(
5132 sqlite3_vtab_cursor
*cur
, /* The cursor */
5133 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
5134 int i
/* Which column to return */
5136 series_cursor
*pCur
= (series_cursor
*)cur
;
5137 sqlite3_int64 x
= 0;
5139 case SERIES_COLUMN_START
: x
= pCur
->ss
.iBase
; break;
5140 case SERIES_COLUMN_STOP
: x
= pCur
->ss
.iTerm
; break;
5141 case SERIES_COLUMN_STEP
: x
= pCur
->ss
.iStep
; break;
5142 default: x
= pCur
->ss
.iValueNow
; break;
5144 sqlite3_result_int64(ctx
, x
);
5148 #ifndef LARGEST_UINT64
5149 #define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
5153 ** Return the rowid for the current row, logically equivalent to n+1 where
5154 ** "n" is the ascending integer in the aforesaid production definition.
5156 static int seriesRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
5157 series_cursor
*pCur
= (series_cursor
*)cur
;
5158 sqlite3_uint64 n
= pCur
->ss
.uSeqIndexNow
;
5159 *pRowid
= (sqlite3_int64
)((n
<LARGEST_UINT64
)? n
+1 : 0);
5164 ** Return TRUE if the cursor has been moved off of the last
5167 static int seriesEof(sqlite3_vtab_cursor
*cur
){
5168 series_cursor
*pCur
= (series_cursor
*)cur
;
5169 return !pCur
->ss
.isNotEOF
;
5172 /* True to cause run-time checking of the start=, stop=, and/or step=
5173 ** parameters. The only reason to do this is for testing the
5174 ** constraint checking logic for virtual tables in the SQLite core.
5176 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5177 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5181 ** This method is called to "rewind" the series_cursor object back
5182 ** to the first row of output. This method is always called at least
5183 ** once prior to any call to seriesColumn() or seriesRowid() or
5186 ** The query plan selected by seriesBestIndex is passed in the idxNum
5187 ** parameter. (idxStr is not used in this implementation.) idxNum
5188 ** is a bitmask showing which constraints are available:
5194 ** Also, if bit 8 is set, that means that the series should be output
5195 ** in descending order rather than in ascending order. If bit 16 is
5196 ** set, then output must appear in ascending order.
5198 ** This routine should initialize the cursor and position it so that it
5199 ** is pointing at the first row, or pointing off the end of the table
5200 ** (so that seriesEof() will return true) if the table is empty.
5202 static int seriesFilter(
5203 sqlite3_vtab_cursor
*pVtabCursor
,
5204 int idxNum
, const char *idxStrUnused
,
5205 int argc
, sqlite3_value
**argv
5207 series_cursor
*pCur
= (series_cursor
*)pVtabCursor
;
5211 pCur
->ss
.iBase
= sqlite3_value_int64(argv
[i
++]);
5216 pCur
->ss
.iTerm
= sqlite3_value_int64(argv
[i
++]);
5218 pCur
->ss
.iTerm
= 0xffffffff;
5221 pCur
->ss
.iStep
= sqlite3_value_int64(argv
[i
++]);
5222 if( pCur
->ss
.iStep
==0 ){
5224 }else if( pCur
->ss
.iStep
<0 ){
5225 if( (idxNum
& 16)==0 ) idxNum
|= 8;
5230 for(i
=0; i
<argc
; i
++){
5231 if( sqlite3_value_type(argv
[i
])==SQLITE_NULL
){
5232 /* If any of the constraints have a NULL value, then return no rows.
5233 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5241 pCur
->ss
.isReversing
= pCur
->ss
.iStep
> 0;
5243 pCur
->ss
.isReversing
= pCur
->ss
.iStep
< 0;
5245 setupSequence( &pCur
->ss
);
5250 ** SQLite will invoke this method one or more times while planning a query
5251 ** that uses the generate_series virtual table. This routine needs to create
5252 ** a query plan for each invocation and compute an estimated cost for that
5255 ** In this implementation idxNum is used to represent the
5256 ** query plan. idxStr is unused.
5258 ** The query plan is represented by bits in idxNum:
5260 ** (1) start = $value -- constraint exists
5261 ** (2) stop = $value -- constraint exists
5262 ** (4) step = $value -- constraint exists
5263 ** (8) output in descending order
5265 static int seriesBestIndex(
5266 sqlite3_vtab
*pVTab
,
5267 sqlite3_index_info
*pIdxInfo
5269 int i
, j
; /* Loop over constraints */
5270 int idxNum
= 0; /* The query plan bitmask */
5271 int bStartSeen
= 0; /* EQ constraint seen on the START column */
5272 int unusableMask
= 0; /* Mask of unusable constraints */
5273 int nArg
= 0; /* Number of arguments that seriesFilter() expects */
5274 int aIdx
[3]; /* Constraints on start, stop, and step */
5275 const struct sqlite3_index_constraint
*pConstraint
;
5277 /* This implementation assumes that the start, stop, and step columns
5278 ** are the last three columns in the virtual table. */
5279 assert( SERIES_COLUMN_STOP
== SERIES_COLUMN_START
+1 );
5280 assert( SERIES_COLUMN_STEP
== SERIES_COLUMN_START
+2 );
5282 aIdx
[0] = aIdx
[1] = aIdx
[2] = -1;
5283 pConstraint
= pIdxInfo
->aConstraint
;
5284 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
5285 int iCol
; /* 0 for start, 1 for stop, 2 for step */
5286 int iMask
; /* bitmask for those column */
5287 if( pConstraint
->iColumn
<SERIES_COLUMN_START
) continue;
5288 iCol
= pConstraint
->iColumn
- SERIES_COLUMN_START
;
5289 assert( iCol
>=0 && iCol
<=2 );
5291 if( iCol
==0 ) bStartSeen
= 1;
5292 if( pConstraint
->usable
==0 ){
5293 unusableMask
|= iMask
;
5295 }else if( pConstraint
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
5301 if( (j
= aIdx
[i
])>=0 ){
5302 pIdxInfo
->aConstraintUsage
[j
].argvIndex
= ++nArg
;
5303 pIdxInfo
->aConstraintUsage
[j
].omit
= !SQLITE_SERIES_CONSTRAINT_VERIFY
;
5306 /* The current generate_column() implementation requires at least one
5307 ** argument (the START value). Legacy versions assumed START=0 if the
5308 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
5309 ** to obtain the legacy behavior */
5310 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
5312 sqlite3_free(pVTab
->zErrMsg
);
5313 pVTab
->zErrMsg
= sqlite3_mprintf(
5314 "first argument to \"generate_series()\" missing or unusable");
5315 return SQLITE_ERROR
;
5318 if( (unusableMask
& ~idxNum
)!=0 ){
5319 /* The start, stop, and step columns are inputs. Therefore if there
5320 ** are unusable constraints on any of start, stop, or step then
5321 ** this plan is unusable */
5322 return SQLITE_CONSTRAINT
;
5324 if( (idxNum
& 3)==3 ){
5325 /* Both start= and stop= boundaries are available. This is the
5326 ** the preferred case */
5327 pIdxInfo
->estimatedCost
= (double)(2 - ((idxNum
&4)!=0));
5328 pIdxInfo
->estimatedRows
= 1000;
5329 if( pIdxInfo
->nOrderBy
>=1 && pIdxInfo
->aOrderBy
[0].iColumn
==0 ){
5330 if( pIdxInfo
->aOrderBy
[0].desc
){
5335 pIdxInfo
->orderByConsumed
= 1;
5338 /* If either boundary is missing, we have to generate a huge span
5339 ** of numbers. Make this case very expensive so that the query
5340 ** planner will work hard to avoid it. */
5341 pIdxInfo
->estimatedRows
= 2147483647;
5343 pIdxInfo
->idxNum
= idxNum
;
5348 ** This following structure defines all the methods for the
5349 ** generate_series virtual table.
5351 static sqlite3_module seriesModule
= {
5354 seriesConnect
, /* xConnect */
5355 seriesBestIndex
, /* xBestIndex */
5356 seriesDisconnect
, /* xDisconnect */
5358 seriesOpen
, /* xOpen - open a cursor */
5359 seriesClose
, /* xClose - close a cursor */
5360 seriesFilter
, /* xFilter - configure scan constraints */
5361 seriesNext
, /* xNext - advance a cursor */
5362 seriesEof
, /* xEof - check for end of scan */
5363 seriesColumn
, /* xColumn - read data */
5364 seriesRowid
, /* xRowid - read data */
5370 0, /* xFindMethod */
5374 0, /* xRollbackTo */
5378 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5383 int sqlite3_series_init(
5386 const sqlite3_api_routines
*pApi
5389 SQLITE_EXTENSION_INIT2(pApi
);
5390 #ifndef SQLITE_OMIT_VIRTUALTABLE
5391 if( sqlite3_libversion_number()<3008012 && pzErrMsg
!=0 ){
5392 *pzErrMsg
= sqlite3_mprintf(
5393 "generate_series() requires SQLite 3.8.12 or later");
5394 return SQLITE_ERROR
;
5396 rc
= sqlite3_create_module(db
, "generate_series", &seriesModule
, 0);
5401 /************************* End ../ext/misc/series.c ********************/
5402 /************************* Begin ../ext/misc/regexp.c ******************/
5406 ** The author disclaims copyright to this source code. In place of
5407 ** a legal notice, here is a blessing:
5409 ** May you do good and not evil.
5410 ** May you find forgiveness for yourself and forgive others.
5411 ** May you share freely, never taking more than you give.
5413 ******************************************************************************
5415 ** The code in this file implements a compact but reasonably
5416 ** efficient regular-expression matcher for posix extended regular
5417 ** expressions against UTF8 text.
5419 ** This file is an SQLite extension. It registers a single function
5420 ** named "regexp(A,B)" where A is the regular expression and B is the
5421 ** string to be matched. By registering this function, SQLite will also
5422 ** then implement the "B regexp A" operator. Note that with the function
5423 ** the regular expression comes first, but with the operator it comes
5426 ** The following regular expression syntax is supported:
5428 ** X* zero or more occurrences of X
5429 ** X+ one or more occurrences of X
5430 ** X? zero or one occurrences of X
5431 ** X{p,q} between p and q occurrences of X
5434 ** ^X X occurring at the beginning of the string
5435 ** X$ X occurring at the end of the string
5436 ** . Match any single character
5437 ** \c Character c where c is one of \{}()[]|*+?.
5438 ** \c C-language escapes for c in afnrtv. ex: \t or \n
5439 ** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX
5440 ** \xXX Where XX is exactly 2 hex digits, unicode value XX
5441 ** [abc] Any single character from the set abc
5442 ** [^abc] Any single character not in the set abc
5443 ** [a-z] Any single character in the range a-z
5444 ** [^a-z] Any single character not in the range a-z
5446 ** \w Word character. [A-Za-z0-9_]
5447 ** \W Non-word character
5450 ** \s Whitespace character
5451 ** \S Non-whitespace character
5453 ** A nondeterministic finite automaton (NFA) is used for matching, so the
5454 ** performance is bounded by O(N*M) where N is the size of the regular
5455 ** expression and M is the size of the input string. The matcher never
5456 ** exhibits exponential behavior. Note that the X{p,q} operator expands
5457 ** to p copies of X following by q-p copies of X? and that the size of the
5458 ** regular expression in the O(N*M) performance bound is computed after
5463 /* #include "sqlite3ext.h" */
5464 SQLITE_EXTENSION_INIT1
5467 ** The following #defines change the names of some functions implemented in
5468 ** this file to prevent name collisions with C-library functions of the
5471 #define re_match sqlite3re_match
5472 #define re_compile sqlite3re_compile
5473 #define re_free sqlite3re_free
5475 /* The end-of-input character */
5476 #define RE_EOF 0 /* End of input */
5477 #define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
5479 /* The NFA is implemented as sequence of opcodes taken from the following
5480 ** set. Each opcode has a single integer argument.
5482 #define RE_OP_MATCH 1 /* Match the one character in the argument */
5483 #define RE_OP_ANY 2 /* Match any one character. (Implements ".") */
5484 #define RE_OP_ANYSTAR 3 /* Special optimized version of .* */
5485 #define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */
5486 #define RE_OP_GOTO 5 /* Jump to opcode at iArg */
5487 #define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */
5488 #define RE_OP_CC_INC 7 /* Beginning of a [...] character class */
5489 #define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */
5490 #define RE_OP_CC_VALUE 9 /* Single value in a character class */
5491 #define RE_OP_CC_RANGE 10 /* Range of values in a character class */
5492 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
5493 #define RE_OP_NOTWORD 12 /* Not a perl word character */
5494 #define RE_OP_DIGIT 13 /* digit: [0-9] */
5495 #define RE_OP_NOTDIGIT 14 /* Not a digit */
5496 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
5497 #define RE_OP_NOTSPACE 16 /* Not a digit */
5498 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
5499 #define RE_OP_ATSTART 18 /* Currently at the start of the string */
5501 #if defined(SQLITE_DEBUG)
5502 /* Opcode names used for symbolic debugging */
5503 static const char *ReOpName
[] = {
5524 #endif /* SQLITE_DEBUG */
5527 /* Each opcode is a "state" in the NFA */
5528 typedef unsigned short ReStateNumber
;
5530 /* Because this is an NFA and not a DFA, multiple states can be active at
5531 ** once. An instance of the following object records all active states in
5532 ** the NFA. The implementation is optimized for the common case where the
5533 ** number of actives states is small.
5535 typedef struct ReStateSet
{
5536 unsigned nState
; /* Number of current states */
5537 ReStateNumber
*aState
; /* Current states */
5540 /* An input string read one character at a time.
5542 typedef struct ReInput ReInput
;
5544 const unsigned char *z
; /* All text */
5545 int i
; /* Next byte to read */
5546 int mx
; /* EOF when i>=mx */
5549 /* A compiled NFA (or an NFA that is in the process of being compiled) is
5550 ** an instance of the following object.
5552 typedef struct ReCompiled ReCompiled
;
5554 ReInput sIn
; /* Regular expression text */
5555 const char *zErr
; /* Error message to return */
5556 char *aOp
; /* Operators for the virtual machine */
5557 int *aArg
; /* Arguments to each operator */
5558 unsigned (*xNextChar
)(ReInput
*); /* Next character function */
5559 unsigned char zInit
[12]; /* Initial text to match */
5560 int nInit
; /* Number of bytes in zInit */
5561 unsigned nState
; /* Number of entries in aOp[] and aArg[] */
5562 unsigned nAlloc
; /* Slots allocated for aOp[] and aArg[] */
5565 /* Add a state to the given state set if it is not already there */
5566 static void re_add_state(ReStateSet
*pSet
, int newState
){
5568 for(i
=0; i
<pSet
->nState
; i
++) if( pSet
->aState
[i
]==newState
) return;
5569 pSet
->aState
[pSet
->nState
++] = (ReStateNumber
)newState
;
5572 /* Extract the next unicode character from *pzIn and return it. Advance
5573 ** *pzIn to the first byte past the end of the character returned. To
5574 ** be clear: this routine converts utf8 to unicode. This routine is
5575 ** optimized for the common case where the next character is a single byte.
5577 static unsigned re_next_char(ReInput
*p
){
5579 if( p
->i
>=p
->mx
) return 0;
5582 if( (c
&0xe0)==0xc0 && p
->i
<p
->mx
&& (p
->z
[p
->i
]&0xc0)==0x80 ){
5583 c
= (c
&0x1f)<<6 | (p
->z
[p
->i
++]&0x3f);
5584 if( c
<0x80 ) c
= 0xfffd;
5585 }else if( (c
&0xf0)==0xe0 && p
->i
+1<p
->mx
&& (p
->z
[p
->i
]&0xc0)==0x80
5586 && (p
->z
[p
->i
+1]&0xc0)==0x80 ){
5587 c
= (c
&0x0f)<<12 | ((p
->z
[p
->i
]&0x3f)<<6) | (p
->z
[p
->i
+1]&0x3f);
5589 if( c
<=0x7ff || (c
>=0xd800 && c
<=0xdfff) ) c
= 0xfffd;
5590 }else if( (c
&0xf8)==0xf0 && p
->i
+2<p
->mx
&& (p
->z
[p
->i
]&0xc0)==0x80
5591 && (p
->z
[p
->i
+1]&0xc0)==0x80 && (p
->z
[p
->i
+2]&0xc0)==0x80 ){
5592 c
= (c
&0x07)<<18 | ((p
->z
[p
->i
]&0x3f)<<12) | ((p
->z
[p
->i
+1]&0x3f)<<6)
5593 | (p
->z
[p
->i
+2]&0x3f);
5595 if( c
<=0xffff || c
>0x10ffff ) c
= 0xfffd;
5602 static unsigned re_next_char_nocase(ReInput
*p
){
5603 unsigned c
= re_next_char(p
);
5604 if( c
>='A' && c
<='Z' ) c
+= 'a' - 'A';
5608 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
5609 static int re_word_char(int c
){
5610 return (c
>='0' && c
<='9') || (c
>='a' && c
<='z')
5611 || (c
>='A' && c
<='Z') || c
=='_';
5614 /* Return true if c is a "digit" character: [0-9] */
5615 static int re_digit_char(int c
){
5616 return (c
>='0' && c
<='9');
5619 /* Return true if c is a perl "space" character: [ \t\r\n\v\f] */
5620 static int re_space_char(int c
){
5621 return c
==' ' || c
=='\t' || c
=='\n' || c
=='\r' || c
=='\v' || c
=='\f';
5624 /* Run a compiled regular expression on the zero-terminated input
5625 ** string zIn[]. Return true on a match and false if there is no match.
5627 static int re_match(ReCompiled
*pRe
, const unsigned char *zIn
, int nIn
){
5628 ReStateSet aStateSet
[2], *pThis
, *pNext
;
5629 ReStateNumber aSpace
[100];
5630 ReStateNumber
*pToFree
;
5632 unsigned int iSwap
= 0;
5640 in
.mx
= nIn
>=0 ? nIn
: (int)strlen((char const*)zIn
);
5642 /* Look for the initial prefix match, if there is one. */
5644 unsigned char x
= pRe
->zInit
[0];
5645 while( in
.i
+pRe
->nInit
<=in
.mx
5647 strncmp((const char*)zIn
+in
.i
, (const char*)pRe
->zInit
, pRe
->nInit
)!=0)
5651 if( in
.i
+pRe
->nInit
>in
.mx
) return 0;
5655 if( pRe
->nState
<=(sizeof(aSpace
)/(sizeof(aSpace
[0])*2)) ){
5657 aStateSet
[0].aState
= aSpace
;
5659 pToFree
= sqlite3_malloc64( sizeof(ReStateNumber
)*2*pRe
->nState
);
5660 if( pToFree
==0 ) return -1;
5661 aStateSet
[0].aState
= pToFree
;
5663 aStateSet
[1].aState
= &aStateSet
[0].aState
[pRe
->nState
];
5664 pNext
= &aStateSet
[1];
5666 re_add_state(pNext
, 0);
5667 while( c
!=RE_EOF
&& pNext
->nState
>0 ){
5669 c
= pRe
->xNextChar(&in
);
5671 pNext
= &aStateSet
[iSwap
];
5674 for(i
=0; i
<pThis
->nState
; i
++){
5675 int x
= pThis
->aState
[i
];
5676 switch( pRe
->aOp
[x
] ){
5678 if( pRe
->aArg
[x
]==c
) re_add_state(pNext
, x
+1);
5681 case RE_OP_ATSTART
: {
5682 if( cPrev
==RE_START
) re_add_state(pThis
, x
+1);
5686 if( c
!=0 ) re_add_state(pNext
, x
+1);
5690 if( re_word_char(c
) ) re_add_state(pNext
, x
+1);
5693 case RE_OP_NOTWORD
: {
5694 if( !re_word_char(c
) && c
!=0 ) re_add_state(pNext
, x
+1);
5698 if( re_digit_char(c
) ) re_add_state(pNext
, x
+1);
5701 case RE_OP_NOTDIGIT
: {
5702 if( !re_digit_char(c
) && c
!=0 ) re_add_state(pNext
, x
+1);
5706 if( re_space_char(c
) ) re_add_state(pNext
, x
+1);
5709 case RE_OP_NOTSPACE
: {
5710 if( !re_space_char(c
) && c
!=0 ) re_add_state(pNext
, x
+1);
5713 case RE_OP_BOUNDARY
: {
5714 if( re_word_char(c
)!=re_word_char(cPrev
) ) re_add_state(pThis
, x
+1);
5717 case RE_OP_ANYSTAR
: {
5718 re_add_state(pNext
, x
);
5719 re_add_state(pThis
, x
+1);
5723 re_add_state(pThis
, x
+pRe
->aArg
[x
]);
5724 re_add_state(pThis
, x
+1);
5728 re_add_state(pThis
, x
+pRe
->aArg
[x
]);
5731 case RE_OP_ACCEPT
: {
5735 case RE_OP_CC_EXC
: {
5737 /* fall-through */ goto re_op_cc_inc
;
5739 case RE_OP_CC_INC
: re_op_cc_inc
: {
5741 int n
= pRe
->aArg
[x
];
5743 for(j
=1; j
>0 && j
<n
; j
++){
5744 if( pRe
->aOp
[x
+j
]==RE_OP_CC_VALUE
){
5745 if( pRe
->aArg
[x
+j
]==c
){
5750 if( pRe
->aArg
[x
+j
]<=c
&& pRe
->aArg
[x
+j
+1]>=c
){
5758 if( pRe
->aOp
[x
]==RE_OP_CC_EXC
) hit
= !hit
;
5759 if( hit
) re_add_state(pNext
, x
+n
);
5765 for(i
=0; i
<pNext
->nState
; i
++){
5766 int x
= pNext
->aState
[i
];
5767 while( pRe
->aOp
[x
]==RE_OP_GOTO
) x
+= pRe
->aArg
[x
];
5768 if( pRe
->aOp
[x
]==RE_OP_ACCEPT
){ rc
= 1; break; }
5771 sqlite3_free(pToFree
);
5775 /* Resize the opcode and argument arrays for an RE under construction.
5777 static int re_resize(ReCompiled
*p
, int N
){
5780 aOp
= sqlite3_realloc64(p
->aOp
, N
*sizeof(p
->aOp
[0]));
5781 if( aOp
==0 ) return 1;
5783 aArg
= sqlite3_realloc64(p
->aArg
, N
*sizeof(p
->aArg
[0]));
5784 if( aArg
==0 ) return 1;
5790 /* Insert a new opcode and argument into an RE under construction. The
5791 ** insertion point is just prior to existing opcode iBefore.
5793 static int re_insert(ReCompiled
*p
, int iBefore
, int op
, int arg
){
5795 if( p
->nAlloc
<=p
->nState
&& re_resize(p
, p
->nAlloc
*2) ) return 0;
5796 for(i
=p
->nState
; i
>iBefore
; i
--){
5797 p
->aOp
[i
] = p
->aOp
[i
-1];
5798 p
->aArg
[i
] = p
->aArg
[i
-1];
5801 p
->aOp
[iBefore
] = (char)op
;
5802 p
->aArg
[iBefore
] = arg
;
5806 /* Append a new opcode and argument to the end of the RE under construction.
5808 static int re_append(ReCompiled
*p
, int op
, int arg
){
5809 return re_insert(p
, p
->nState
, op
, arg
);
5812 /* Make a copy of N opcodes starting at iStart onto the end of the RE
5813 ** under construction.
5815 static void re_copy(ReCompiled
*p
, int iStart
, int N
){
5816 if( p
->nState
+N
>=p
->nAlloc
&& re_resize(p
, p
->nAlloc
*2+N
) ) return;
5817 memcpy(&p
->aOp
[p
->nState
], &p
->aOp
[iStart
], N
*sizeof(p
->aOp
[0]));
5818 memcpy(&p
->aArg
[p
->nState
], &p
->aArg
[iStart
], N
*sizeof(p
->aArg
[0]));
5822 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
5823 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If
5824 ** c is not a hex digit *pV is unchanged.
5826 static int re_hex(int c
, int *pV
){
5827 if( c
>='0' && c
<='9' ){
5829 }else if( c
>='a' && c
<='f' ){
5831 }else if( c
>='A' && c
<='F' ){
5836 *pV
= (*pV
)*16 + (c
& 0xff);
5840 /* A backslash character has been seen, read the next character and
5841 ** return its interpretation.
5843 static unsigned re_esc_char(ReCompiled
*p
){
5844 static const char zEsc
[] = "afnrtv\\()*.+?[$^{|}]";
5845 static const char zTrans
[] = "\a\f\n\r\t\v";
5848 if( p
->sIn
.i
>=p
->sIn
.mx
) return 0;
5849 c
= p
->sIn
.z
[p
->sIn
.i
];
5850 if( c
=='u' && p
->sIn
.i
+4<p
->sIn
.mx
){
5851 const unsigned char *zIn
= p
->sIn
.z
+ p
->sIn
.i
;
5852 if( re_hex(zIn
[1],&v
)
5853 && re_hex(zIn
[2],&v
)
5854 && re_hex(zIn
[3],&v
)
5855 && re_hex(zIn
[4],&v
)
5861 if( c
=='x' && p
->sIn
.i
+2<p
->sIn
.mx
){
5862 const unsigned char *zIn
= p
->sIn
.z
+ p
->sIn
.i
;
5863 if( re_hex(zIn
[1],&v
)
5864 && re_hex(zIn
[2],&v
)
5870 for(i
=0; zEsc
[i
] && zEsc
[i
]!=c
; i
++){}
5872 if( i
<6 ) c
= zTrans
[i
];
5875 p
->zErr
= "unknown \\ escape";
5880 /* Forward declaration */
5881 static const char *re_subcompile_string(ReCompiled
*);
5883 /* Peek at the next byte of input */
5884 static unsigned char rePeek(ReCompiled
*p
){
5885 return p
->sIn
.i
<p
->sIn
.mx
? p
->sIn
.z
[p
->sIn
.i
] : 0;
5888 /* Compile RE text into a sequence of opcodes. Continue up to the
5889 ** first unmatched ")" character, then return. If an error is found,
5890 ** return a pointer to the error message string.
5892 static const char *re_subcompile_re(ReCompiled
*p
){
5894 int iStart
, iEnd
, iGoto
;
5896 zErr
= re_subcompile_string(p
);
5897 if( zErr
) return zErr
;
5898 while( rePeek(p
)=='|' ){
5900 re_insert(p
, iStart
, RE_OP_FORK
, iEnd
+ 2 - iStart
);
5901 iGoto
= re_append(p
, RE_OP_GOTO
, 0);
5903 zErr
= re_subcompile_string(p
);
5904 if( zErr
) return zErr
;
5905 p
->aArg
[iGoto
] = p
->nState
- iGoto
;
5910 /* Compile an element of regular expression text (anything that can be
5911 ** an operand to the "|" operator). Return NULL on success or a pointer
5912 ** to the error message if there is a problem.
5914 static const char *re_subcompile_string(ReCompiled
*p
){
5919 while( (c
= p
->xNextChar(&p
->sIn
))!=0 ){
5928 zErr
= re_subcompile_re(p
);
5929 if( zErr
) return zErr
;
5930 if( rePeek(p
)!=')' ) return "unmatched '('";
5935 if( rePeek(p
)=='*' ){
5936 re_append(p
, RE_OP_ANYSTAR
, 0);
5939 re_append(p
, RE_OP_ANY
, 0);
5944 if( iPrev
<0 ) return "'*' without operand";
5945 re_insert(p
, iPrev
, RE_OP_GOTO
, p
->nState
- iPrev
+ 1);
5946 re_append(p
, RE_OP_FORK
, iPrev
- p
->nState
+ 1);
5950 if( iPrev
<0 ) return "'+' without operand";
5951 re_append(p
, RE_OP_FORK
, iPrev
- p
->nState
);
5955 if( iPrev
<0 ) return "'?' without operand";
5956 re_insert(p
, iPrev
, RE_OP_FORK
, p
->nState
- iPrev
+1);
5960 re_append(p
, RE_OP_MATCH
, RE_EOF
);
5964 re_append(p
, RE_OP_ATSTART
, 0);
5970 if( iPrev
<0 ) return "'{m,n}' without operand";
5971 while( (c
=rePeek(p
))>='0' && c
<='9' ){ m
= m
*10 + c
- '0'; p
->sIn
.i
++; }
5976 while( (c
=rePeek(p
))>='0' && c
<='9' ){ n
= n
*10 + c
-'0'; p
->sIn
.i
++; }
5978 if( c
!='}' ) return "unmatched '{'";
5979 if( n
>0 && n
<m
) return "n less than m in '{m,n}'";
5981 sz
= p
->nState
- iPrev
;
5983 if( n
==0 ) return "both m and n are zero in '{m,n}'";
5984 re_insert(p
, iPrev
, RE_OP_FORK
, sz
+1);
5988 for(j
=1; j
<m
; j
++) re_copy(p
, iPrev
, sz
);
5991 re_append(p
, RE_OP_FORK
, sz
+1);
5992 re_copy(p
, iPrev
, sz
);
5995 re_append(p
, RE_OP_FORK
, -sz
);
6000 unsigned int iFirst
= p
->nState
;
6001 if( rePeek(p
)=='^' ){
6002 re_append(p
, RE_OP_CC_EXC
, 0);
6005 re_append(p
, RE_OP_CC_INC
, 0);
6007 while( (c
= p
->xNextChar(&p
->sIn
))!=0 ){
6008 if( c
=='[' && rePeek(p
)==':' ){
6009 return "POSIX character classes not supported";
6011 if( c
=='\\' ) c
= re_esc_char(p
);
6012 if( rePeek(p
)=='-' ){
6013 re_append(p
, RE_OP_CC_RANGE
, c
);
6015 c
= p
->xNextChar(&p
->sIn
);
6016 if( c
=='\\' ) c
= re_esc_char(p
);
6017 re_append(p
, RE_OP_CC_RANGE
, c
);
6019 re_append(p
, RE_OP_CC_VALUE
, c
);
6021 if( rePeek(p
)==']' ){ p
->sIn
.i
++; break; }
6023 if( c
==0 ) return "unclosed '['";
6024 if( p
->nState
>iFirst
) p
->aArg
[iFirst
] = p
->nState
- iFirst
;
6029 switch( rePeek(p
) ){
6030 case 'b': specialOp
= RE_OP_BOUNDARY
; break;
6031 case 'd': specialOp
= RE_OP_DIGIT
; break;
6032 case 'D': specialOp
= RE_OP_NOTDIGIT
; break;
6033 case 's': specialOp
= RE_OP_SPACE
; break;
6034 case 'S': specialOp
= RE_OP_NOTSPACE
; break;
6035 case 'w': specialOp
= RE_OP_WORD
; break;
6036 case 'W': specialOp
= RE_OP_NOTWORD
; break;
6040 re_append(p
, specialOp
, 0);
6043 re_append(p
, RE_OP_MATCH
, c
);
6048 re_append(p
, RE_OP_MATCH
, c
);
6057 /* Free and reclaim all the memory used by a previously compiled
6058 ** regular expression. Applications should invoke this routine once
6059 ** for every call to re_compile() to avoid memory leaks.
6061 static void re_free(ReCompiled
*pRe
){
6063 sqlite3_free(pRe
->aOp
);
6064 sqlite3_free(pRe
->aArg
);
6070 ** Compile a textual regular expression in zIn[] into a compiled regular
6071 ** expression suitable for us by re_match() and return a pointer to the
6072 ** compiled regular expression in *ppRe. Return NULL on success or an
6073 ** error message if something goes wrong.
6075 static const char *re_compile(ReCompiled
**ppRe
, const char *zIn
, int noCase
){
6081 pRe
= sqlite3_malloc( sizeof(*pRe
) );
6083 return "out of memory";
6085 memset(pRe
, 0, sizeof(*pRe
));
6086 pRe
->xNextChar
= noCase
? re_next_char_nocase
: re_next_char
;
6087 if( re_resize(pRe
, 30) ){
6089 return "out of memory";
6094 re_append(pRe
, RE_OP_ANYSTAR
, 0);
6096 pRe
->sIn
.z
= (unsigned char*)zIn
;
6098 pRe
->sIn
.mx
= (int)strlen(zIn
);
6099 zErr
= re_subcompile_re(pRe
);
6104 if( pRe
->sIn
.i
>=pRe
->sIn
.mx
){
6105 re_append(pRe
, RE_OP_ACCEPT
, 0);
6109 return "unrecognized character";
6112 /* The following is a performance optimization. If the regex begins with
6113 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6114 ** one or more matching characters, enter those matching characters into
6115 ** zInit[]. The re_match() routine can then search ahead in the input
6116 ** string looking for the initial match without having to run the whole
6117 ** regex engine over the string. Do not worry about trying to match
6118 ** unicode characters beyond plane 0 - those are very rare and this is
6119 ** just an optimization. */
6120 if( pRe
->aOp
[0]==RE_OP_ANYSTAR
&& !noCase
){
6121 for(j
=0, i
=1; j
<(int)sizeof(pRe
->zInit
)-2 && pRe
->aOp
[i
]==RE_OP_MATCH
; i
++){
6122 unsigned x
= pRe
->aArg
[i
];
6124 pRe
->zInit
[j
++] = (unsigned char)x
;
6125 }else if( x
<=0x7ff ){
6126 pRe
->zInit
[j
++] = (unsigned char)(0xc0 | (x
>>6));
6127 pRe
->zInit
[j
++] = 0x80 | (x
&0x3f);
6128 }else if( x
<=0xffff ){
6129 pRe
->zInit
[j
++] = (unsigned char)(0xe0 | (x
>>12));
6130 pRe
->zInit
[j
++] = 0x80 | ((x
>>6)&0x3f);
6131 pRe
->zInit
[j
++] = 0x80 | (x
&0x3f);
6136 if( j
>0 && pRe
->zInit
[j
-1]==0 ) j
--;
6143 ** Implementation of the regexp() SQL function. This function implements
6144 ** the build-in REGEXP operator. The first argument to the function is the
6145 ** pattern and the second argument is the string. So, the SQL statements:
6149 ** is implemented as regexp(B,A).
6151 static void re_sql_func(
6152 sqlite3_context
*context
,
6154 sqlite3_value
**argv
6156 ReCompiled
*pRe
; /* Compiled regular expression */
6157 const char *zPattern
; /* The regular expression */
6158 const unsigned char *zStr
;/* String being searched */
6159 const char *zErr
; /* Compile error message */
6160 int setAux
= 0; /* True to invoke sqlite3_set_auxdata() */
6162 (void)argc
; /* Unused */
6163 pRe
= sqlite3_get_auxdata(context
, 0);
6165 zPattern
= (const char*)sqlite3_value_text(argv
[0]);
6166 if( zPattern
==0 ) return;
6167 zErr
= re_compile(&pRe
, zPattern
, sqlite3_user_data(context
)!=0);
6170 sqlite3_result_error(context
, zErr
, -1);
6174 sqlite3_result_error_nomem(context
);
6179 zStr
= (const unsigned char*)sqlite3_value_text(argv
[1]);
6181 sqlite3_result_int(context
, re_match(pRe
, zStr
, -1));
6184 sqlite3_set_auxdata(context
, 0, pRe
, (void(*)(void*))re_free
);
6188 #if defined(SQLITE_DEBUG)
6190 ** This function is used for testing and debugging only. It is only available
6191 ** if the SQLITE_DEBUG compile-time option is used.
6193 ** Compile a regular expression and then convert the compiled expression into
6194 ** text and return that text.
6196 static void re_bytecode_func(
6197 sqlite3_context
*context
,
6199 sqlite3_value
**argv
6201 const char *zPattern
;
6210 zPattern
= (const char*)sqlite3_value_text(argv
[0]);
6211 if( zPattern
==0 ) return;
6212 zErr
= re_compile(&pRe
, zPattern
, sqlite3_user_data(context
)!=0);
6215 sqlite3_result_error(context
, zErr
, -1);
6219 sqlite3_result_error_nomem(context
);
6222 pStr
= sqlite3_str_new(0);
6223 if( pStr
==0 ) goto re_bytecode_func_err
;
6225 sqlite3_str_appendf(pStr
, "INIT ");
6226 for(i
=0; i
<pRe
->nInit
; i
++){
6227 sqlite3_str_appendf(pStr
, "%02x", pRe
->zInit
[i
]);
6229 sqlite3_str_appendf(pStr
, "\n");
6231 for(i
=0; (unsigned)i
<pRe
->nState
; i
++){
6232 sqlite3_str_appendf(pStr
, "%-8s %4d\n",
6233 ReOpName
[(unsigned char)pRe
->aOp
[i
]], pRe
->aArg
[i
]);
6235 n
= sqlite3_str_length(pStr
);
6236 z
= sqlite3_str_finish(pStr
);
6240 sqlite3_result_text(context
, z
, n
-1, sqlite3_free
);
6243 re_bytecode_func_err
:
6247 #endif /* SQLITE_DEBUG */
6251 ** Invoke this routine to register the regexp() function with the
6252 ** SQLite database connection.
6257 int sqlite3_regexp_init(
6260 const sqlite3_api_routines
*pApi
6263 SQLITE_EXTENSION_INIT2(pApi
);
6264 (void)pzErrMsg
; /* Unused */
6265 rc
= sqlite3_create_function(db
, "regexp", 2,
6266 SQLITE_UTF8
|SQLITE_INNOCUOUS
|SQLITE_DETERMINISTIC
,
6267 0, re_sql_func
, 0, 0);
6268 if( rc
==SQLITE_OK
){
6269 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6270 ** of regexp(PATTERN,STRING). */
6271 rc
= sqlite3_create_function(db
, "regexpi", 2,
6272 SQLITE_UTF8
|SQLITE_INNOCUOUS
|SQLITE_DETERMINISTIC
,
6273 (void*)db
, re_sql_func
, 0, 0);
6274 #if defined(SQLITE_DEBUG)
6275 if( rc
==SQLITE_OK
){
6276 rc
= sqlite3_create_function(db
, "regexp_bytecode", 1,
6277 SQLITE_UTF8
|SQLITE_INNOCUOUS
|SQLITE_DETERMINISTIC
,
6278 0, re_bytecode_func
, 0, 0);
6280 #endif /* SQLITE_DEBUG */
6285 /************************* End ../ext/misc/regexp.c ********************/
6286 #ifndef SQLITE_SHELL_FIDDLE
6287 /************************* Begin ../ext/misc/fileio.c ******************/
6291 ** The author disclaims copyright to this source code. In place of
6292 ** a legal notice, here is a blessing:
6294 ** May you do good and not evil.
6295 ** May you find forgiveness for yourself and forgive others.
6296 ** May you share freely, never taking more than you give.
6298 ******************************************************************************
6300 ** This SQLite extension implements SQL functions readfile() and
6301 ** writefile(), and eponymous virtual type "fsdir".
6303 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
6305 ** If neither of the optional arguments is present, then this UDF
6306 ** function writes blob DATA to file FILE. If successful, the number
6307 ** of bytes written is returned. If an error occurs, NULL is returned.
6309 ** If the first option argument - MODE - is present, then it must
6310 ** be passed an integer value that corresponds to a POSIX mode
6311 ** value (file type + permissions, as returned in the stat.st_mode
6312 ** field by the stat() system call). Three types of files may
6313 ** be written/created:
6315 ** regular files: (mode & 0170000)==0100000
6316 ** symbolic links: (mode & 0170000)==0120000
6317 ** directories: (mode & 0170000)==0040000
6319 ** For a directory, the DATA is ignored. For a symbolic link, it is
6320 ** interpreted as text and used as the target of the link. For a
6321 ** regular file, it is interpreted as a blob and written into the
6322 ** named file. Regardless of the type of file, its permissions are
6323 ** set to (mode & 0777) before returning.
6325 ** If the optional MTIME argument is present, then it is interpreted
6326 ** as an integer - the number of seconds since the unix epoch. The
6327 ** modification-time of the target file is set to this value before
6330 ** If three or more arguments are passed to this function and an
6331 ** error is encountered, an exception is raised.
6335 ** Read and return the contents of file FILE (type blob) from disk.
6341 ** SELECT * FROM fsdir($path [, $dir]);
6343 ** Parameter $path is an absolute or relative pathname. If the file that it
6344 ** refers to does not exist, it is an error. If the path refers to a regular
6345 ** file or symbolic link, it returns a single row. Or, if the path refers
6346 ** to a directory, it returns one row for the directory, and one row for each
6347 ** file within the hierarchy rooted at $path.
6349 ** Each row has the following columns:
6351 ** name: Path to file or directory (text value).
6352 ** mode: Value of stat.st_mode for directory entry (an integer).
6353 ** mtime: Value of stat.st_mtime for directory entry (an integer).
6354 ** data: For a regular file, a blob containing the file data. For a
6355 ** symlink, a text value containing the text of the link. For a
6358 ** If a non-NULL value is specified for the optional $dir parameter and
6359 ** $path is a relative path, then $path is interpreted relative to $dir.
6360 ** And the paths returned in the "name" column of the table are also
6361 ** relative to directory $dir.
6363 ** Notes on building this extension for Windows:
6364 ** Unless linked statically with the SQLite library, a preprocessor
6365 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
6366 ** DLL form of this extension for WIN32. See its use below for details.
6368 /* #include "sqlite3ext.h" */
6369 SQLITE_EXTENSION_INIT1
6374 #include <sys/types.h>
6375 #include <sys/stat.h>
6377 #if !defined(_WIN32) && !defined(WIN32)
6378 # include <unistd.h>
6379 # include <dirent.h>
6381 # include <sys/time.h>
6383 # include "windows.h"
6385 # include <direct.h>
6386 /* # include "test_windirent.h" */
6387 # define dirent DIRENT
6389 # define chmod _chmod
6394 # define mkdir(path,mode) _mkdir(path)
6395 # define lstat(path,buf) stat(path,buf)
6402 ** Structure of the fsdir() table-valued function
6405 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
6406 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
6407 #define FSDIR_COLUMN_MODE 1 /* Access mode */
6408 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
6409 #define FSDIR_COLUMN_DATA 3 /* File content */
6410 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
6411 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
6415 ** Set the result stored by context ctx to a blob containing the
6416 ** contents of file zName. Or, leave the result unchanged (NULL)
6417 ** if the file does not exist or is unreadable.
6419 ** If the file exceeds the SQLite blob size limit, through an
6420 ** SQLITE_TOOBIG error.
6422 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
6425 static void readFileContents(sqlite3_context
*ctx
, const char *zName
){
6432 in
= fopen(zName
, "rb");
6434 /* File does not exist or is unreadable. Leave the result set to NULL. */
6437 fseek(in
, 0, SEEK_END
);
6440 db
= sqlite3_context_db_handle(ctx
);
6441 mxBlob
= sqlite3_limit(db
, SQLITE_LIMIT_LENGTH
, -1);
6443 sqlite3_result_error_code(ctx
, SQLITE_TOOBIG
);
6447 pBuf
= sqlite3_malloc64( nIn
? nIn
: 1 );
6449 sqlite3_result_error_nomem(ctx
);
6453 if( nIn
==(sqlite3_int64
)fread(pBuf
, 1, (size_t)nIn
, in
) ){
6454 sqlite3_result_blob64(ctx
, pBuf
, nIn
, sqlite3_free
);
6456 sqlite3_result_error_code(ctx
, SQLITE_IOERR
);
6463 ** Implementation of the "readfile(X)" SQL function. The entire content
6464 ** of the file named X is read and returned as a BLOB. NULL is returned
6465 ** if the file does not exist or is unreadable.
6467 static void readfileFunc(
6468 sqlite3_context
*context
,
6470 sqlite3_value
**argv
6473 (void)(argc
); /* Unused parameter */
6474 zName
= (const char*)sqlite3_value_text(argv
[0]);
6475 if( zName
==0 ) return;
6476 readFileContents(context
, zName
);
6480 ** Set the error message contained in context ctx to the results of
6481 ** vprintf(zFmt, ...).
6483 static void ctxErrorMsg(sqlite3_context
*ctx
, const char *zFmt
, ...){
6487 zMsg
= sqlite3_vmprintf(zFmt
, ap
);
6488 sqlite3_result_error(ctx
, zMsg
, -1);
6495 ** This function is designed to convert a Win32 FILETIME structure into the
6496 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
6498 static sqlite3_uint64
fileTimeToUnixTime(
6499 LPFILETIME pFileTime
6501 SYSTEMTIME epochSystemTime
;
6502 ULARGE_INTEGER epochIntervals
;
6503 FILETIME epochFileTime
;
6504 ULARGE_INTEGER fileIntervals
;
6506 memset(&epochSystemTime
, 0, sizeof(SYSTEMTIME
));
6507 epochSystemTime
.wYear
= 1970;
6508 epochSystemTime
.wMonth
= 1;
6509 epochSystemTime
.wDay
= 1;
6510 SystemTimeToFileTime(&epochSystemTime
, &epochFileTime
);
6511 epochIntervals
.LowPart
= epochFileTime
.dwLowDateTime
;
6512 epochIntervals
.HighPart
= epochFileTime
.dwHighDateTime
;
6514 fileIntervals
.LowPart
= pFileTime
->dwLowDateTime
;
6515 fileIntervals
.HighPart
= pFileTime
->dwHighDateTime
;
6517 return (fileIntervals
.QuadPart
- epochIntervals
.QuadPart
) / 10000000;
6521 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
6522 # /* To allow a standalone DLL, use this next replacement function: */
6523 # undef sqlite3_win32_utf8_to_unicode
6524 # define sqlite3_win32_utf8_to_unicode utf8_to_utf16
6526 LPWSTR
utf8_to_utf16(const char *z
){
6527 int nAllot
= MultiByteToWideChar(CP_UTF8
, 0, z
, -1, NULL
, 0);
6528 LPWSTR rv
= sqlite3_malloc(nAllot
* sizeof(WCHAR
));
6529 if( rv
!=0 && 0 < MultiByteToWideChar(CP_UTF8
, 0, z
, -1, rv
, nAllot
) )
6537 ** This function attempts to normalize the time values found in the stat()
6538 ** buffer to UTC. This is necessary on Win32, where the runtime library
6539 ** appears to return these values as local times.
6541 static void statTimesToUtc(
6543 struct stat
*pStatBuf
6546 WIN32_FIND_DATAW fd
;
6547 LPWSTR zUnicodeName
;
6548 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char*);
6549 zUnicodeName
= sqlite3_win32_utf8_to_unicode(zPath
);
6551 memset(&fd
, 0, sizeof(WIN32_FIND_DATAW
));
6552 hFindFile
= FindFirstFileW(zUnicodeName
, &fd
);
6553 if( hFindFile
!=NULL
){
6554 pStatBuf
->st_ctime
= (time_t)fileTimeToUnixTime(&fd
.ftCreationTime
);
6555 pStatBuf
->st_atime
= (time_t)fileTimeToUnixTime(&fd
.ftLastAccessTime
);
6556 pStatBuf
->st_mtime
= (time_t)fileTimeToUnixTime(&fd
.ftLastWriteTime
);
6557 FindClose(hFindFile
);
6559 sqlite3_free(zUnicodeName
);
6565 ** This function is used in place of stat(). On Windows, special handling
6566 ** is required in order for the included time to be returned as UTC. On all
6567 ** other systems, this function simply calls stat().
6569 static int fileStat(
6571 struct stat
*pStatBuf
6574 int rc
= stat(zPath
, pStatBuf
);
6575 if( rc
==0 ) statTimesToUtc(zPath
, pStatBuf
);
6578 return stat(zPath
, pStatBuf
);
6583 ** This function is used in place of lstat(). On Windows, special handling
6584 ** is required in order for the included time to be returned as UTC. On all
6585 ** other systems, this function simply calls lstat().
6587 static int fileLinkStat(
6589 struct stat
*pStatBuf
6592 int rc
= lstat(zPath
, pStatBuf
);
6593 if( rc
==0 ) statTimesToUtc(zPath
, pStatBuf
);
6596 return lstat(zPath
, pStatBuf
);
6601 ** Argument zFile is the name of a file that will be created and/or written
6602 ** by SQL function writefile(). This function ensures that the directory
6603 ** zFile will be written to exists, creating it if required. The permissions
6604 ** for any path components created by this function are set in accordance
6605 ** with the current umask.
6607 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
6608 ** SQLITE_OK is returned if the directory is successfully created, or
6609 ** SQLITE_ERROR otherwise.
6611 static int makeDirectory(
6614 char *zCopy
= sqlite3_mprintf("%s", zFile
);
6620 int nCopy
= (int)strlen(zCopy
);
6623 while( rc
==SQLITE_OK
){
6627 for(; zCopy
[i
]!='/' && i
<nCopy
; i
++);
6628 if( i
==nCopy
) break;
6631 rc2
= fileStat(zCopy
, &sStat
);
6633 if( mkdir(zCopy
, 0777) ) rc
= SQLITE_ERROR
;
6635 if( !S_ISDIR(sStat
.st_mode
) ) rc
= SQLITE_ERROR
;
6641 sqlite3_free(zCopy
);
6648 ** This function does the work for the writefile() UDF. Refer to
6649 ** header comments at the top of this file for details.
6651 static int writeFile(
6652 sqlite3_context
*pCtx
, /* Context to return bytes written in */
6653 const char *zFile
, /* File to write */
6654 sqlite3_value
*pData
, /* Data to write */
6655 mode_t mode
, /* MODE parameter passed to writefile() */
6656 sqlite3_int64 mtime
/* MTIME parameter (or -1 to not set time) */
6658 if( zFile
==0 ) return 1;
6659 #if !defined(_WIN32) && !defined(WIN32)
6660 if( S_ISLNK(mode
) ){
6661 const char *zTo
= (const char*)sqlite3_value_text(pData
);
6662 if( zTo
==0 || symlink(zTo
, zFile
)<0 ) return 1;
6666 if( S_ISDIR(mode
) ){
6667 if( mkdir(zFile
, mode
) ){
6668 /* The mkdir() call to create the directory failed. This might not
6669 ** be an error though - if there is already a directory at the same
6670 ** path and either the permissions already match or can be changed
6671 ** to do so using chmod(), it is not an error. */
6674 || 0!=fileStat(zFile
, &sStat
)
6675 || !S_ISDIR(sStat
.st_mode
)
6676 || ((sStat
.st_mode
&0777)!=(mode
&0777) && 0!=chmod(zFile
, mode
&0777))
6682 sqlite3_int64 nWrite
= 0;
6685 FILE *out
= fopen(zFile
, "wb");
6686 if( out
==0 ) return 1;
6687 z
= (const char*)sqlite3_value_blob(pData
);
6689 sqlite3_int64 n
= fwrite(z
, 1, sqlite3_value_bytes(pData
), out
);
6690 nWrite
= sqlite3_value_bytes(pData
);
6696 if( rc
==0 && mode
&& chmod(zFile
, mode
& 0777) ){
6700 sqlite3_result_int64(pCtx
, nWrite
);
6706 #if !SQLITE_OS_WINRT
6708 FILETIME lastAccess
;
6710 SYSTEMTIME currentTime
;
6713 LPWSTR zUnicodeName
;
6714 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char*);
6716 GetSystemTime(¤tTime
);
6717 SystemTimeToFileTime(¤tTime
, &lastAccess
);
6718 intervals
= Int32x32To64(mtime
, 10000000) + 116444736000000000;
6719 lastWrite
.dwLowDateTime
= (DWORD
)intervals
;
6720 lastWrite
.dwHighDateTime
= intervals
>> 32;
6721 zUnicodeName
= sqlite3_win32_utf8_to_unicode(zFile
);
6722 if( zUnicodeName
==0 ){
6725 hFile
= CreateFileW(
6726 zUnicodeName
, FILE_WRITE_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
6727 FILE_FLAG_BACKUP_SEMANTICS
, NULL
6729 sqlite3_free(zUnicodeName
);
6730 if( hFile
!=INVALID_HANDLE_VALUE
){
6731 BOOL bResult
= SetFileTime(hFile
, NULL
, &lastAccess
, &lastWrite
);
6738 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
6740 struct timespec times
[2];
6741 times
[0].tv_nsec
= times
[1].tv_nsec
= 0;
6742 times
[0].tv_sec
= time(0);
6743 times
[1].tv_sec
= mtime
;
6744 if( utimensat(AT_FDCWD
, zFile
, times
, AT_SYMLINK_NOFOLLOW
) ){
6749 struct timeval times
[2];
6750 times
[0].tv_usec
= times
[1].tv_usec
= 0;
6751 times
[0].tv_sec
= time(0);
6752 times
[1].tv_sec
= mtime
;
6753 if( utimes(zFile
, times
) ){
6763 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
6764 ** Refer to header comments at the top of this file for details.
6766 static void writefileFunc(
6767 sqlite3_context
*context
,
6769 sqlite3_value
**argv
6774 sqlite3_int64 mtime
= -1;
6776 if( argc
<2 || argc
>4 ){
6777 sqlite3_result_error(context
,
6778 "wrong number of arguments to function writefile()", -1
6783 zFile
= (const char*)sqlite3_value_text(argv
[0]);
6784 if( zFile
==0 ) return;
6786 mode
= (mode_t
)sqlite3_value_int(argv
[2]);
6789 mtime
= sqlite3_value_int64(argv
[3]);
6792 res
= writeFile(context
, zFile
, argv
[1], mode
, mtime
);
6793 if( res
==1 && errno
==ENOENT
){
6794 if( makeDirectory(zFile
)==SQLITE_OK
){
6795 res
= writeFile(context
, zFile
, argv
[1], mode
, mtime
);
6799 if( argc
>2 && res
!=0 ){
6800 if( S_ISLNK(mode
) ){
6801 ctxErrorMsg(context
, "failed to create symlink: %s", zFile
);
6802 }else if( S_ISDIR(mode
) ){
6803 ctxErrorMsg(context
, "failed to create directory: %s", zFile
);
6805 ctxErrorMsg(context
, "failed to write file: %s", zFile
);
6811 ** SQL function: lsmode(MODE)
6813 ** Given a numberic st_mode from stat(), convert it into a human-readable
6814 ** text string in the style of "ls -l".
6816 static void lsModeFunc(
6817 sqlite3_context
*context
,
6819 sqlite3_value
**argv
6822 int iMode
= sqlite3_value_int(argv
[0]);
6825 if( S_ISLNK(iMode
) ){
6827 }else if( S_ISREG(iMode
) ){
6829 }else if( S_ISDIR(iMode
) ){
6835 int m
= (iMode
>> ((2-i
)*3));
6836 char *a
= &z
[1 + i
*3];
6837 a
[0] = (m
& 0x4) ? 'r' : '-';
6838 a
[1] = (m
& 0x2) ? 'w' : '-';
6839 a
[2] = (m
& 0x1) ? 'x' : '-';
6842 sqlite3_result_text(context
, z
, -1, SQLITE_TRANSIENT
);
6845 #ifndef SQLITE_OMIT_VIRTUALTABLE
6848 ** Cursor type for recursively iterating through a directory structure.
6850 typedef struct fsdir_cursor fsdir_cursor
;
6851 typedef struct FsdirLevel FsdirLevel
;
6854 DIR *pDir
; /* From opendir() */
6855 char *zDir
; /* Name of directory (nul-terminated) */
6858 struct fsdir_cursor
{
6859 sqlite3_vtab_cursor base
; /* Base class - must be first */
6861 int nLvl
; /* Number of entries in aLvl[] array */
6862 int iLvl
; /* Index of current entry */
6863 FsdirLevel
*aLvl
; /* Hierarchy of directories being traversed */
6868 struct stat sStat
; /* Current lstat() results */
6869 char *zPath
; /* Path to current entry */
6870 sqlite3_int64 iRowid
; /* Current rowid */
6873 typedef struct fsdir_tab fsdir_tab
;
6875 sqlite3_vtab base
; /* Base class - must be first */
6879 ** Construct a new fsdir virtual table object.
6881 static int fsdirConnect(
6884 int argc
, const char *const*argv
,
6885 sqlite3_vtab
**ppVtab
,
6888 fsdir_tab
*pNew
= 0;
6894 rc
= sqlite3_declare_vtab(db
, "CREATE TABLE x" FSDIR_SCHEMA
);
6895 if( rc
==SQLITE_OK
){
6896 pNew
= (fsdir_tab
*)sqlite3_malloc( sizeof(*pNew
) );
6897 if( pNew
==0 ) return SQLITE_NOMEM
;
6898 memset(pNew
, 0, sizeof(*pNew
));
6899 sqlite3_vtab_config(db
, SQLITE_VTAB_DIRECTONLY
);
6901 *ppVtab
= (sqlite3_vtab
*)pNew
;
6906 ** This method is the destructor for fsdir vtab objects.
6908 static int fsdirDisconnect(sqlite3_vtab
*pVtab
){
6909 sqlite3_free(pVtab
);
6914 ** Constructor for a new fsdir_cursor object.
6916 static int fsdirOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
6919 pCur
= sqlite3_malloc( sizeof(*pCur
) );
6920 if( pCur
==0 ) return SQLITE_NOMEM
;
6921 memset(pCur
, 0, sizeof(*pCur
));
6923 *ppCursor
= &pCur
->base
;
6928 ** Reset a cursor back to the state it was in when first returned
6931 static void fsdirResetCursor(fsdir_cursor
*pCur
){
6933 for(i
=0; i
<=pCur
->iLvl
; i
++){
6934 FsdirLevel
*pLvl
= &pCur
->aLvl
[i
];
6935 if( pLvl
->pDir
) closedir(pLvl
->pDir
);
6936 sqlite3_free(pLvl
->zDir
);
6938 sqlite3_free(pCur
->zPath
);
6939 sqlite3_free(pCur
->aLvl
);
6950 ** Destructor for an fsdir_cursor.
6952 static int fsdirClose(sqlite3_vtab_cursor
*cur
){
6953 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
6955 fsdirResetCursor(pCur
);
6961 ** Set the error message for the virtual table associated with cursor
6962 ** pCur to the results of vprintf(zFmt, ...).
6964 static void fsdirSetErrmsg(fsdir_cursor
*pCur
, const char *zFmt
, ...){
6967 pCur
->base
.pVtab
->zErrMsg
= sqlite3_vmprintf(zFmt
, ap
);
6973 ** Advance an fsdir_cursor to its next row of output.
6975 static int fsdirNext(sqlite3_vtab_cursor
*cur
){
6976 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
6977 mode_t m
= pCur
->sStat
.st_mode
;
6981 /* Descend into this directory */
6982 int iNew
= pCur
->iLvl
+ 1;
6984 if( iNew
>=pCur
->nLvl
){
6986 sqlite3_int64 nByte
= nNew
*sizeof(FsdirLevel
);
6987 FsdirLevel
*aNew
= (FsdirLevel
*)sqlite3_realloc64(pCur
->aLvl
, nByte
);
6988 if( aNew
==0 ) return SQLITE_NOMEM
;
6989 memset(&aNew
[pCur
->nLvl
], 0, sizeof(FsdirLevel
)*(nNew
-pCur
->nLvl
));
6994 pLvl
= &pCur
->aLvl
[iNew
];
6996 pLvl
->zDir
= pCur
->zPath
;
6998 pLvl
->pDir
= opendir(pLvl
->zDir
);
6999 if( pLvl
->pDir
==0 ){
7000 fsdirSetErrmsg(pCur
, "cannot read directory: %s", pCur
->zPath
);
7001 return SQLITE_ERROR
;
7005 while( pCur
->iLvl
>=0 ){
7006 FsdirLevel
*pLvl
= &pCur
->aLvl
[pCur
->iLvl
];
7007 struct dirent
*pEntry
= readdir(pLvl
->pDir
);
7009 if( pEntry
->d_name
[0]=='.' ){
7010 if( pEntry
->d_name
[1]=='.' && pEntry
->d_name
[2]=='\0' ) continue;
7011 if( pEntry
->d_name
[1]=='\0' ) continue;
7013 sqlite3_free(pCur
->zPath
);
7014 pCur
->zPath
= sqlite3_mprintf("%s/%s", pLvl
->zDir
, pEntry
->d_name
);
7015 if( pCur
->zPath
==0 ) return SQLITE_NOMEM
;
7016 if( fileLinkStat(pCur
->zPath
, &pCur
->sStat
) ){
7017 fsdirSetErrmsg(pCur
, "cannot stat file: %s", pCur
->zPath
);
7018 return SQLITE_ERROR
;
7022 closedir(pLvl
->pDir
);
7023 sqlite3_free(pLvl
->zDir
);
7030 sqlite3_free(pCur
->zPath
);
7036 ** Return values of columns for the row at which the series_cursor
7037 ** is currently pointing.
7039 static int fsdirColumn(
7040 sqlite3_vtab_cursor
*cur
, /* The cursor */
7041 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
7042 int i
/* Which column to return */
7044 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
7046 case FSDIR_COLUMN_NAME
: {
7047 sqlite3_result_text(ctx
, &pCur
->zPath
[pCur
->nBase
], -1, SQLITE_TRANSIENT
);
7051 case FSDIR_COLUMN_MODE
:
7052 sqlite3_result_int64(ctx
, pCur
->sStat
.st_mode
);
7055 case FSDIR_COLUMN_MTIME
:
7056 sqlite3_result_int64(ctx
, pCur
->sStat
.st_mtime
);
7059 case FSDIR_COLUMN_DATA
: {
7060 mode_t m
= pCur
->sStat
.st_mode
;
7062 sqlite3_result_null(ctx
);
7063 #if !defined(_WIN32) && !defined(WIN32)
7064 }else if( S_ISLNK(m
) ){
7066 char *aBuf
= aStatic
;
7067 sqlite3_int64 nBuf
= 64;
7071 n
= readlink(pCur
->zPath
, aBuf
, nBuf
);
7073 if( aBuf
!=aStatic
) sqlite3_free(aBuf
);
7075 aBuf
= sqlite3_malloc64(nBuf
);
7077 sqlite3_result_error_nomem(ctx
);
7078 return SQLITE_NOMEM
;
7082 sqlite3_result_text(ctx
, aBuf
, n
, SQLITE_TRANSIENT
);
7083 if( aBuf
!=aStatic
) sqlite3_free(aBuf
);
7086 readFileContents(ctx
, pCur
->zPath
);
7089 case FSDIR_COLUMN_PATH
:
7091 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
7092 ** always return their values as NULL */
7100 ** Return the rowid for the current row. In this implementation, the
7101 ** first row returned is assigned rowid value 1, and each subsequent
7102 ** row a value 1 more than that of the previous.
7104 static int fsdirRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
7105 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
7106 *pRowid
= pCur
->iRowid
;
7111 ** Return TRUE if the cursor has been moved off of the last
7114 static int fsdirEof(sqlite3_vtab_cursor
*cur
){
7115 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
7116 return (pCur
->zPath
==0);
7120 ** xFilter callback.
7122 ** idxNum==1 PATH parameter only
7123 ** idxNum==2 Both PATH and DIR supplied
7125 static int fsdirFilter(
7126 sqlite3_vtab_cursor
*cur
,
7127 int idxNum
, const char *idxStr
,
7128 int argc
, sqlite3_value
**argv
7130 const char *zDir
= 0;
7131 fsdir_cursor
*pCur
= (fsdir_cursor
*)cur
;
7133 fsdirResetCursor(pCur
);
7136 fsdirSetErrmsg(pCur
, "table function fsdir requires an argument");
7137 return SQLITE_ERROR
;
7140 assert( argc
==idxNum
&& (argc
==1 || argc
==2) );
7141 zDir
= (const char*)sqlite3_value_text(argv
[0]);
7143 fsdirSetErrmsg(pCur
, "table function fsdir requires a non-NULL argument");
7144 return SQLITE_ERROR
;
7147 pCur
->zBase
= (const char*)sqlite3_value_text(argv
[1]);
7150 pCur
->nBase
= (int)strlen(pCur
->zBase
)+1;
7151 pCur
->zPath
= sqlite3_mprintf("%s/%s", pCur
->zBase
, zDir
);
7153 pCur
->zPath
= sqlite3_mprintf("%s", zDir
);
7156 if( pCur
->zPath
==0 ){
7157 return SQLITE_NOMEM
;
7159 if( fileLinkStat(pCur
->zPath
, &pCur
->sStat
) ){
7160 fsdirSetErrmsg(pCur
, "cannot stat file: %s", pCur
->zPath
);
7161 return SQLITE_ERROR
;
7168 ** SQLite will invoke this method one or more times while planning a query
7169 ** that uses the generate_series virtual table. This routine needs to create
7170 ** a query plan for each invocation and compute an estimated cost for that
7173 ** In this implementation idxNum is used to represent the
7174 ** query plan. idxStr is unused.
7176 ** The query plan is represented by values of idxNum:
7178 ** (1) The path value is supplied by argv[0]
7179 ** (2) Path is in argv[0] and dir is in argv[1]
7181 static int fsdirBestIndex(
7183 sqlite3_index_info
*pIdxInfo
7185 int i
; /* Loop over constraints */
7186 int idxPath
= -1; /* Index in pIdxInfo->aConstraint of PATH= */
7187 int idxDir
= -1; /* Index in pIdxInfo->aConstraint of DIR= */
7188 int seenPath
= 0; /* True if an unusable PATH= constraint is seen */
7189 int seenDir
= 0; /* True if an unusable DIR= constraint is seen */
7190 const struct sqlite3_index_constraint
*pConstraint
;
7193 pConstraint
= pIdxInfo
->aConstraint
;
7194 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
7195 if( pConstraint
->op
!=SQLITE_INDEX_CONSTRAINT_EQ
) continue;
7196 switch( pConstraint
->iColumn
){
7197 case FSDIR_COLUMN_PATH
: {
7198 if( pConstraint
->usable
){
7201 }else if( idxPath
<0 ){
7206 case FSDIR_COLUMN_DIR
: {
7207 if( pConstraint
->usable
){
7210 }else if( idxDir
<0 ){
7217 if( seenPath
|| seenDir
){
7218 /* If input parameters are unusable, disallow this plan */
7219 return SQLITE_CONSTRAINT
;
7223 pIdxInfo
->idxNum
= 0;
7224 /* The pIdxInfo->estimatedCost should have been initialized to a huge
7225 ** number. Leave it unchanged. */
7226 pIdxInfo
->estimatedRows
= 0x7fffffff;
7228 pIdxInfo
->aConstraintUsage
[idxPath
].omit
= 1;
7229 pIdxInfo
->aConstraintUsage
[idxPath
].argvIndex
= 1;
7231 pIdxInfo
->aConstraintUsage
[idxDir
].omit
= 1;
7232 pIdxInfo
->aConstraintUsage
[idxDir
].argvIndex
= 2;
7233 pIdxInfo
->idxNum
= 2;
7234 pIdxInfo
->estimatedCost
= 10.0;
7236 pIdxInfo
->idxNum
= 1;
7237 pIdxInfo
->estimatedCost
= 100.0;
7245 ** Register the "fsdir" virtual table.
7247 static int fsdirRegister(sqlite3
*db
){
7248 static sqlite3_module fsdirModule
= {
7251 fsdirConnect
, /* xConnect */
7252 fsdirBestIndex
, /* xBestIndex */
7253 fsdirDisconnect
, /* xDisconnect */
7255 fsdirOpen
, /* xOpen - open a cursor */
7256 fsdirClose
, /* xClose - close a cursor */
7257 fsdirFilter
, /* xFilter - configure scan constraints */
7258 fsdirNext
, /* xNext - advance a cursor */
7259 fsdirEof
, /* xEof - check for end of scan */
7260 fsdirColumn
, /* xColumn - read data */
7261 fsdirRowid
, /* xRowid - read data */
7267 0, /* xFindMethod */
7271 0, /* xRollbackTo */
7272 0, /* xShadowName */
7275 int rc
= sqlite3_create_module(db
, "fsdir", &fsdirModule
, 0);
7278 #else /* SQLITE_OMIT_VIRTUALTABLE */
7279 # define fsdirRegister(x) SQLITE_OK
7285 int sqlite3_fileio_init(
7288 const sqlite3_api_routines
*pApi
7291 SQLITE_EXTENSION_INIT2(pApi
);
7292 (void)pzErrMsg
; /* Unused parameter */
7293 rc
= sqlite3_create_function(db
, "readfile", 1,
7294 SQLITE_UTF8
|SQLITE_DIRECTONLY
, 0,
7295 readfileFunc
, 0, 0);
7296 if( rc
==SQLITE_OK
){
7297 rc
= sqlite3_create_function(db
, "writefile", -1,
7298 SQLITE_UTF8
|SQLITE_DIRECTONLY
, 0,
7299 writefileFunc
, 0, 0);
7301 if( rc
==SQLITE_OK
){
7302 rc
= sqlite3_create_function(db
, "lsmode", 1, SQLITE_UTF8
, 0,
7305 if( rc
==SQLITE_OK
){
7306 rc
= fsdirRegister(db
);
7311 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
7312 /* To allow a standalone DLL, make test_windirent.c use the same
7313 * redefined SQLite API calls as the above extension code does.
7314 * Just pull in this .c to accomplish this. As a beneficial side
7315 * effect, this extension becomes a single translation unit. */
7316 # include "test_windirent.c"
7319 /************************* End ../ext/misc/fileio.c ********************/
7320 /************************* Begin ../ext/misc/completion.c ******************/
7324 ** The author disclaims copyright to this source code. In place of
7325 ** a legal notice, here is a blessing:
7327 ** May you do good and not evil.
7328 ** May you find forgiveness for yourself and forgive others.
7329 ** May you share freely, never taking more than you give.
7331 *************************************************************************
7333 ** This file implements an eponymous virtual table that returns suggested
7334 ** completions for a partial SQL input.
7338 ** SELECT DISTINCT candidate COLLATE nocase
7339 ** FROM completion($prefix,$wholeline)
7342 ** The two query parameters are optional. $prefix is the text of the
7343 ** current word being typed and that is to be completed. $wholeline is
7344 ** the complete input line, used for context.
7346 ** The raw completion() table might return the same candidate multiple
7347 ** times, for example if the same column name is used to two or more
7348 ** tables. And the candidates are returned in an arbitrary order. Hence,
7349 ** the DISTINCT and ORDER BY are recommended.
7351 ** This virtual table operates at the speed of human typing, and so there
7352 ** is no attempt to make it fast. Even a slow implementation will be much
7353 ** faster than any human can type.
7356 /* #include "sqlite3ext.h" */
7357 SQLITE_EXTENSION_INIT1
7362 #ifndef SQLITE_OMIT_VIRTUALTABLE
7364 /* completion_vtab is a subclass of sqlite3_vtab which will
7365 ** serve as the underlying representation of a completion virtual table
7367 typedef struct completion_vtab completion_vtab
;
7368 struct completion_vtab
{
7369 sqlite3_vtab base
; /* Base class - must be first */
7370 sqlite3
*db
; /* Database connection for this completion vtab */
7373 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
7374 ** serve as the underlying representation of a cursor that scans
7375 ** over rows of the result
7377 typedef struct completion_cursor completion_cursor
;
7378 struct completion_cursor
{
7379 sqlite3_vtab_cursor base
; /* Base class - must be first */
7380 sqlite3
*db
; /* Database connection for this cursor */
7381 int nPrefix
, nLine
; /* Number of bytes in zPrefix and zLine */
7382 char *zPrefix
; /* The prefix for the word we want to complete */
7383 char *zLine
; /* The whole that we want to complete */
7384 const char *zCurrentRow
; /* Current output row */
7385 int szRow
; /* Length of the zCurrentRow string */
7386 sqlite3_stmt
*pStmt
; /* Current statement */
7387 sqlite3_int64 iRowid
; /* The rowid */
7388 int ePhase
; /* Current phase */
7389 int j
; /* inter-phase counter */
7392 /* Values for ePhase:
7394 #define COMPLETION_FIRST_PHASE 1
7395 #define COMPLETION_KEYWORDS 1
7396 #define COMPLETION_PRAGMAS 2
7397 #define COMPLETION_FUNCTIONS 3
7398 #define COMPLETION_COLLATIONS 4
7399 #define COMPLETION_INDEXES 5
7400 #define COMPLETION_TRIGGERS 6
7401 #define COMPLETION_DATABASES 7
7402 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
7403 #define COMPLETION_COLUMNS 9
7404 #define COMPLETION_MODULES 10
7405 #define COMPLETION_EOF 11
7408 ** The completionConnect() method is invoked to create a new
7409 ** completion_vtab that describes the completion virtual table.
7411 ** Think of this routine as the constructor for completion_vtab objects.
7413 ** All this routine needs to do is:
7415 ** (1) Allocate the completion_vtab object and initialize all fields.
7417 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
7418 ** result set of queries against completion will look like.
7420 static int completionConnect(
7423 int argc
, const char *const*argv
,
7424 sqlite3_vtab
**ppVtab
,
7427 completion_vtab
*pNew
;
7430 (void)(pAux
); /* Unused parameter */
7431 (void)(argc
); /* Unused parameter */
7432 (void)(argv
); /* Unused parameter */
7433 (void)(pzErr
); /* Unused parameter */
7435 /* Column numbers */
7436 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
7437 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
7438 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
7439 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
7441 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
7442 rc
= sqlite3_declare_vtab(db
,
7445 " prefix TEXT HIDDEN,"
7446 " wholeline TEXT HIDDEN,"
7447 " phase INT HIDDEN" /* Used for debugging only */
7449 if( rc
==SQLITE_OK
){
7450 pNew
= sqlite3_malloc( sizeof(*pNew
) );
7451 *ppVtab
= (sqlite3_vtab
*)pNew
;
7452 if( pNew
==0 ) return SQLITE_NOMEM
;
7453 memset(pNew
, 0, sizeof(*pNew
));
7460 ** This method is the destructor for completion_cursor objects.
7462 static int completionDisconnect(sqlite3_vtab
*pVtab
){
7463 sqlite3_free(pVtab
);
7468 ** Constructor for a new completion_cursor object.
7470 static int completionOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
7471 completion_cursor
*pCur
;
7472 pCur
= sqlite3_malloc( sizeof(*pCur
) );
7473 if( pCur
==0 ) return SQLITE_NOMEM
;
7474 memset(pCur
, 0, sizeof(*pCur
));
7475 pCur
->db
= ((completion_vtab
*)p
)->db
;
7476 *ppCursor
= &pCur
->base
;
7481 ** Reset the completion_cursor.
7483 static void completionCursorReset(completion_cursor
*pCur
){
7484 sqlite3_free(pCur
->zPrefix
); pCur
->zPrefix
= 0; pCur
->nPrefix
= 0;
7485 sqlite3_free(pCur
->zLine
); pCur
->zLine
= 0; pCur
->nLine
= 0;
7486 sqlite3_finalize(pCur
->pStmt
); pCur
->pStmt
= 0;
7491 ** Destructor for a completion_cursor.
7493 static int completionClose(sqlite3_vtab_cursor
*cur
){
7494 completionCursorReset((completion_cursor
*)cur
);
7500 ** Advance a completion_cursor to its next row of output.
7502 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
7503 ** record the current state of the scan. This routine sets ->zCurrentRow
7504 ** to the current row of output and then returns. If no more rows remain,
7505 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
7506 ** table that has reached the end of its scan.
7508 ** The current implementation just lists potential identifiers and
7509 ** keywords and filters them by zPrefix. Future enhancements should
7510 ** take zLine into account to try to restrict the set of identifiers and
7511 ** keywords based on what would be legal at the current point of input.
7513 static int completionNext(sqlite3_vtab_cursor
*cur
){
7514 completion_cursor
*pCur
= (completion_cursor
*)cur
;
7515 int eNextPhase
= 0; /* Next phase to try if current phase reaches end */
7516 int iCol
= -1; /* If >=0, step pCur->pStmt and use the i-th column */
7518 while( pCur
->ePhase
!=COMPLETION_EOF
){
7519 switch( pCur
->ePhase
){
7520 case COMPLETION_KEYWORDS
: {
7521 if( pCur
->j
>= sqlite3_keyword_count() ){
7522 pCur
->zCurrentRow
= 0;
7523 pCur
->ePhase
= COMPLETION_DATABASES
;
7525 sqlite3_keyword_name(pCur
->j
++, &pCur
->zCurrentRow
, &pCur
->szRow
);
7530 case COMPLETION_DATABASES
: {
7531 if( pCur
->pStmt
==0 ){
7532 sqlite3_prepare_v2(pCur
->db
, "PRAGMA database_list", -1,
7536 eNextPhase
= COMPLETION_TABLES
;
7539 case COMPLETION_TABLES
: {
7540 if( pCur
->pStmt
==0 ){
7543 const char *zSep
= "";
7544 sqlite3_prepare_v2(pCur
->db
, "PRAGMA database_list", -1, &pS2
, 0);
7545 while( sqlite3_step(pS2
)==SQLITE_ROW
){
7546 const char *zDb
= (const char*)sqlite3_column_text(pS2
, 1);
7547 zSql
= sqlite3_mprintf(
7549 "SELECT name FROM \"%w\".sqlite_schema",
7552 if( zSql
==0 ) return SQLITE_NOMEM
;
7555 sqlite3_finalize(pS2
);
7556 sqlite3_prepare_v2(pCur
->db
, zSql
, -1, &pCur
->pStmt
, 0);
7560 eNextPhase
= COMPLETION_COLUMNS
;
7563 case COMPLETION_COLUMNS
: {
7564 if( pCur
->pStmt
==0 ){
7567 const char *zSep
= "";
7568 sqlite3_prepare_v2(pCur
->db
, "PRAGMA database_list", -1, &pS2
, 0);
7569 while( sqlite3_step(pS2
)==SQLITE_ROW
){
7570 const char *zDb
= (const char*)sqlite3_column_text(pS2
, 1);
7571 zSql
= sqlite3_mprintf(
7573 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
7574 " JOIN pragma_table_info(sm.name,%Q) AS pti"
7575 " WHERE sm.type='table'",
7576 zSql
, zSep
, zDb
, zDb
7578 if( zSql
==0 ) return SQLITE_NOMEM
;
7581 sqlite3_finalize(pS2
);
7582 sqlite3_prepare_v2(pCur
->db
, zSql
, -1, &pCur
->pStmt
, 0);
7586 eNextPhase
= COMPLETION_EOF
;
7591 /* This case is when the phase presets zCurrentRow */
7592 if( pCur
->zCurrentRow
==0 ) continue;
7594 if( sqlite3_step(pCur
->pStmt
)==SQLITE_ROW
){
7595 /* Extract the next row of content */
7596 pCur
->zCurrentRow
= (const char*)sqlite3_column_text(pCur
->pStmt
, iCol
);
7597 pCur
->szRow
= sqlite3_column_bytes(pCur
->pStmt
, iCol
);
7599 /* When all rows are finished, advance to the next phase */
7600 sqlite3_finalize(pCur
->pStmt
);
7602 pCur
->ePhase
= eNextPhase
;
7606 if( pCur
->nPrefix
==0 ) break;
7607 if( pCur
->nPrefix
<=pCur
->szRow
7608 && sqlite3_strnicmp(pCur
->zPrefix
, pCur
->zCurrentRow
, pCur
->nPrefix
)==0
7618 ** Return values of columns for the row at which the completion_cursor
7619 ** is currently pointing.
7621 static int completionColumn(
7622 sqlite3_vtab_cursor
*cur
, /* The cursor */
7623 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
7624 int i
/* Which column to return */
7626 completion_cursor
*pCur
= (completion_cursor
*)cur
;
7628 case COMPLETION_COLUMN_CANDIDATE
: {
7629 sqlite3_result_text(ctx
, pCur
->zCurrentRow
, pCur
->szRow
,SQLITE_TRANSIENT
);
7632 case COMPLETION_COLUMN_PREFIX
: {
7633 sqlite3_result_text(ctx
, pCur
->zPrefix
, -1, SQLITE_TRANSIENT
);
7636 case COMPLETION_COLUMN_WHOLELINE
: {
7637 sqlite3_result_text(ctx
, pCur
->zLine
, -1, SQLITE_TRANSIENT
);
7640 case COMPLETION_COLUMN_PHASE
: {
7641 sqlite3_result_int(ctx
, pCur
->ePhase
);
7649 ** Return the rowid for the current row. In this implementation, the
7650 ** rowid is the same as the output value.
7652 static int completionRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
7653 completion_cursor
*pCur
= (completion_cursor
*)cur
;
7654 *pRowid
= pCur
->iRowid
;
7659 ** Return TRUE if the cursor has been moved off of the last
7662 static int completionEof(sqlite3_vtab_cursor
*cur
){
7663 completion_cursor
*pCur
= (completion_cursor
*)cur
;
7664 return pCur
->ePhase
>= COMPLETION_EOF
;
7668 ** This method is called to "rewind" the completion_cursor object back
7669 ** to the first row of output. This method is always called at least
7670 ** once prior to any call to completionColumn() or completionRowid() or
7673 static int completionFilter(
7674 sqlite3_vtab_cursor
*pVtabCursor
,
7675 int idxNum
, const char *idxStr
,
7676 int argc
, sqlite3_value
**argv
7678 completion_cursor
*pCur
= (completion_cursor
*)pVtabCursor
;
7680 (void)(idxStr
); /* Unused parameter */
7681 (void)(argc
); /* Unused parameter */
7682 completionCursorReset(pCur
);
7684 pCur
->nPrefix
= sqlite3_value_bytes(argv
[iArg
]);
7685 if( pCur
->nPrefix
>0 ){
7686 pCur
->zPrefix
= sqlite3_mprintf("%s", sqlite3_value_text(argv
[iArg
]));
7687 if( pCur
->zPrefix
==0 ) return SQLITE_NOMEM
;
7692 pCur
->nLine
= sqlite3_value_bytes(argv
[iArg
]);
7693 if( pCur
->nLine
>0 ){
7694 pCur
->zLine
= sqlite3_mprintf("%s", sqlite3_value_text(argv
[iArg
]));
7695 if( pCur
->zLine
==0 ) return SQLITE_NOMEM
;
7698 if( pCur
->zLine
!=0 && pCur
->zPrefix
==0 ){
7699 int i
= pCur
->nLine
;
7700 while( i
>0 && (isalnum(pCur
->zLine
[i
-1]) || pCur
->zLine
[i
-1]=='_') ){
7703 pCur
->nPrefix
= pCur
->nLine
- i
;
7704 if( pCur
->nPrefix
>0 ){
7705 pCur
->zPrefix
= sqlite3_mprintf("%.*s", pCur
->nPrefix
, pCur
->zLine
+ i
);
7706 if( pCur
->zPrefix
==0 ) return SQLITE_NOMEM
;
7710 pCur
->ePhase
= COMPLETION_FIRST_PHASE
;
7711 return completionNext(pVtabCursor
);
7715 ** SQLite will invoke this method one or more times while planning a query
7716 ** that uses the completion virtual table. This routine needs to create
7717 ** a query plan for each invocation and compute an estimated cost for that
7720 ** There are two hidden parameters that act as arguments to the table-valued
7721 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
7722 ** is available and bit 1 is set if "wholeline" is available.
7724 static int completionBestIndex(
7726 sqlite3_index_info
*pIdxInfo
7728 int i
; /* Loop over constraints */
7729 int idxNum
= 0; /* The query plan bitmask */
7730 int prefixIdx
= -1; /* Index of the start= constraint, or -1 if none */
7731 int wholelineIdx
= -1; /* Index of the stop= constraint, or -1 if none */
7732 int nArg
= 0; /* Number of arguments that completeFilter() expects */
7733 const struct sqlite3_index_constraint
*pConstraint
;
7735 (void)(tab
); /* Unused parameter */
7736 pConstraint
= pIdxInfo
->aConstraint
;
7737 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
7738 if( pConstraint
->usable
==0 ) continue;
7739 if( pConstraint
->op
!=SQLITE_INDEX_CONSTRAINT_EQ
) continue;
7740 switch( pConstraint
->iColumn
){
7741 case COMPLETION_COLUMN_PREFIX
:
7745 case COMPLETION_COLUMN_WHOLELINE
:
7752 pIdxInfo
->aConstraintUsage
[prefixIdx
].argvIndex
= ++nArg
;
7753 pIdxInfo
->aConstraintUsage
[prefixIdx
].omit
= 1;
7755 if( wholelineIdx
>=0 ){
7756 pIdxInfo
->aConstraintUsage
[wholelineIdx
].argvIndex
= ++nArg
;
7757 pIdxInfo
->aConstraintUsage
[wholelineIdx
].omit
= 1;
7759 pIdxInfo
->idxNum
= idxNum
;
7760 pIdxInfo
->estimatedCost
= (double)5000 - 1000*nArg
;
7761 pIdxInfo
->estimatedRows
= 500 - 100*nArg
;
7766 ** This following structure defines all the methods for the
7767 ** completion virtual table.
7769 static sqlite3_module completionModule
= {
7772 completionConnect
, /* xConnect */
7773 completionBestIndex
, /* xBestIndex */
7774 completionDisconnect
, /* xDisconnect */
7776 completionOpen
, /* xOpen - open a cursor */
7777 completionClose
, /* xClose - close a cursor */
7778 completionFilter
, /* xFilter - configure scan constraints */
7779 completionNext
, /* xNext - advance a cursor */
7780 completionEof
, /* xEof - check for end of scan */
7781 completionColumn
, /* xColumn - read data */
7782 completionRowid
, /* xRowid - read data */
7788 0, /* xFindMethod */
7792 0, /* xRollbackTo */
7796 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7798 int sqlite3CompletionVtabInit(sqlite3
*db
){
7800 #ifndef SQLITE_OMIT_VIRTUALTABLE
7801 rc
= sqlite3_create_module(db
, "completion", &completionModule
, 0);
7809 int sqlite3_completion_init(
7812 const sqlite3_api_routines
*pApi
7815 SQLITE_EXTENSION_INIT2(pApi
);
7816 (void)(pzErrMsg
); /* Unused parameter */
7817 #ifndef SQLITE_OMIT_VIRTUALTABLE
7818 rc
= sqlite3CompletionVtabInit(db
);
7823 /************************* End ../ext/misc/completion.c ********************/
7824 /************************* Begin ../ext/misc/appendvfs.c ******************/
7828 ** The author disclaims copyright to this source code. In place of
7829 ** a legal notice, here is a blessing:
7831 ** May you do good and not evil.
7832 ** May you find forgiveness for yourself and forgive others.
7833 ** May you share freely, never taking more than you give.
7835 ******************************************************************************
7837 ** This file implements a VFS shim that allows an SQLite database to be
7838 ** appended onto the end of some other file, such as an executable.
7840 ** A special record must appear at the end of the file that identifies the
7841 ** file as an appended database and provides the offset to the first page
7842 ** of the exposed content. (Or, it is the length of the content prefix.)
7843 ** For best performance page 1 should be located at a disk page boundary,
7844 ** though that is not required.
7846 ** When opening a database using this VFS, the connection might treat
7847 ** the file as an ordinary SQLite database, or it might treat it as a
7848 ** database appended onto some other file. The decision is made by
7849 ** applying the following rules in order:
7851 ** (1) An empty file is an ordinary database.
7853 ** (2) If the file ends with the appendvfs trailer string
7854 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
7856 ** (3) If the file begins with the standard SQLite prefix string
7857 ** "SQLite format 3", that file is an ordinary database.
7859 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
7860 ** set, then a new database is appended to the already existing file.
7862 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
7864 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
7865 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
7866 ** This VFS will not read or write past the 1GiB mark. This restriction
7867 ** might be lifted in future versions. For now, if you need a larger
7868 ** database, then keep it in a separate file.
7870 ** If the file being opened is a plain database (not an appended one), then
7871 ** this shim is a pass-through into the default underlying VFS. (rule 3)
7873 /* #include "sqlite3ext.h" */
7874 SQLITE_EXTENSION_INIT1
7878 /* The append mark at the end of the database is:
7880 ** Start-Of-SQLite3-NNNNNNNN
7881 ** 123456789 123456789 12345
7883 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
7884 ** the offset to page 1, and also the length of the prefix content.
7886 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
7887 #define APND_MARK_PREFIX_SZ 17
7888 #define APND_MARK_FOS_SZ 8
7889 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
7892 ** Maximum size of the combined prefix + database + append-mark. This
7893 ** must be less than 0x40000000 to avoid locking issues on Windows.
7895 #define APND_MAX_SIZE (0x40000000)
7898 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
7900 #ifndef APND_ROUNDUP
7901 #define APND_ROUNDUP 4096
7903 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
7904 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
7907 ** Forward declaration of objects used by this utility
7909 typedef struct sqlite3_vfs ApndVfs
;
7910 typedef struct ApndFile ApndFile
;
7912 /* Access to a lower-level VFS that (might) implement dynamic loading,
7913 ** access to randomness, etc.
7915 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
7916 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
7918 /* An open appendvfs file
7920 ** An instance of this structure describes the appended database file.
7921 ** A separate sqlite3_file object is always appended. The appended
7922 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
7923 ** the entire file, including the prefix, the database, and the
7926 ** The structure of an AppendVFS database is like this:
7928 ** +-------------+---------+----------+-------------+
7929 ** | prefix-file | padding | database | append-mark |
7930 ** +-------------+---------+----------+-------------+
7936 ** "prefix file" - file onto which the database has been appended.
7937 ** "padding" - zero or more bytes inserted so that "database"
7938 ** starts on an APND_ROUNDUP boundary
7939 ** "database" - The SQLite database file
7940 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
7941 ** the offset from the start of prefix-file to the start
7944 ** The size of the database is iMark - iPgOne.
7946 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
7947 ** of iPgOne stored as a big-ending 64-bit integer.
7949 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
7950 ** Or, iMark is -1 to indicate that it has not yet been written.
7953 sqlite3_file base
; /* Subclass. MUST BE FIRST! */
7954 sqlite3_int64 iPgOne
; /* Offset to the start of the database */
7955 sqlite3_int64 iMark
; /* Offset of the append mark. -1 if unwritten */
7956 /* Always followed by another sqlite3_file that describes the whole file */
7960 ** Methods for ApndFile
7962 static int apndClose(sqlite3_file
*);
7963 static int apndRead(sqlite3_file
*, void*, int iAmt
, sqlite3_int64 iOfst
);
7964 static int apndWrite(sqlite3_file
*,const void*,int iAmt
, sqlite3_int64 iOfst
);
7965 static int apndTruncate(sqlite3_file
*, sqlite3_int64 size
);
7966 static int apndSync(sqlite3_file
*, int flags
);
7967 static int apndFileSize(sqlite3_file
*, sqlite3_int64
*pSize
);
7968 static int apndLock(sqlite3_file
*, int);
7969 static int apndUnlock(sqlite3_file
*, int);
7970 static int apndCheckReservedLock(sqlite3_file
*, int *pResOut
);
7971 static int apndFileControl(sqlite3_file
*, int op
, void *pArg
);
7972 static int apndSectorSize(sqlite3_file
*);
7973 static int apndDeviceCharacteristics(sqlite3_file
*);
7974 static int apndShmMap(sqlite3_file
*, int iPg
, int pgsz
, int, void volatile**);
7975 static int apndShmLock(sqlite3_file
*, int offset
, int n
, int flags
);
7976 static void apndShmBarrier(sqlite3_file
*);
7977 static int apndShmUnmap(sqlite3_file
*, int deleteFlag
);
7978 static int apndFetch(sqlite3_file
*, sqlite3_int64 iOfst
, int iAmt
, void **pp
);
7979 static int apndUnfetch(sqlite3_file
*, sqlite3_int64 iOfst
, void *p
);
7982 ** Methods for ApndVfs
7984 static int apndOpen(sqlite3_vfs
*, const char *, sqlite3_file
*, int , int *);
7985 static int apndDelete(sqlite3_vfs
*, const char *zName
, int syncDir
);
7986 static int apndAccess(sqlite3_vfs
*, const char *zName
, int flags
, int *);
7987 static int apndFullPathname(sqlite3_vfs
*, const char *zName
, int, char *zOut
);
7988 static void *apndDlOpen(sqlite3_vfs
*, const char *zFilename
);
7989 static void apndDlError(sqlite3_vfs
*, int nByte
, char *zErrMsg
);
7990 static void (*apndDlSym(sqlite3_vfs
*pVfs
, void *p
, const char*zSym
))(void);
7991 static void apndDlClose(sqlite3_vfs
*, void*);
7992 static int apndRandomness(sqlite3_vfs
*, int nByte
, char *zOut
);
7993 static int apndSleep(sqlite3_vfs
*, int microseconds
);
7994 static int apndCurrentTime(sqlite3_vfs
*, double*);
7995 static int apndGetLastError(sqlite3_vfs
*, int, char *);
7996 static int apndCurrentTimeInt64(sqlite3_vfs
*, sqlite3_int64
*);
7997 static int apndSetSystemCall(sqlite3_vfs
*, const char*,sqlite3_syscall_ptr
);
7998 static sqlite3_syscall_ptr
apndGetSystemCall(sqlite3_vfs
*, const char *z
);
7999 static const char *apndNextSystemCall(sqlite3_vfs
*, const char *zName
);
8001 static sqlite3_vfs apnd_vfs
= {
8002 3, /* iVersion (set when registered) */
8003 0, /* szOsFile (set when registered) */
8004 1024, /* mxPathname */
8006 "apndvfs", /* zName */
8007 0, /* pAppData (set when registered) */
8008 apndOpen
, /* xOpen */
8009 apndDelete
, /* xDelete */
8010 apndAccess
, /* xAccess */
8011 apndFullPathname
, /* xFullPathname */
8012 apndDlOpen
, /* xDlOpen */
8013 apndDlError
, /* xDlError */
8014 apndDlSym
, /* xDlSym */
8015 apndDlClose
, /* xDlClose */
8016 apndRandomness
, /* xRandomness */
8017 apndSleep
, /* xSleep */
8018 apndCurrentTime
, /* xCurrentTime */
8019 apndGetLastError
, /* xGetLastError */
8020 apndCurrentTimeInt64
, /* xCurrentTimeInt64 */
8021 apndSetSystemCall
, /* xSetSystemCall */
8022 apndGetSystemCall
, /* xGetSystemCall */
8023 apndNextSystemCall
/* xNextSystemCall */
8026 static const sqlite3_io_methods apnd_io_methods
= {
8028 apndClose
, /* xClose */
8029 apndRead
, /* xRead */
8030 apndWrite
, /* xWrite */
8031 apndTruncate
, /* xTruncate */
8032 apndSync
, /* xSync */
8033 apndFileSize
, /* xFileSize */
8034 apndLock
, /* xLock */
8035 apndUnlock
, /* xUnlock */
8036 apndCheckReservedLock
, /* xCheckReservedLock */
8037 apndFileControl
, /* xFileControl */
8038 apndSectorSize
, /* xSectorSize */
8039 apndDeviceCharacteristics
, /* xDeviceCharacteristics */
8040 apndShmMap
, /* xShmMap */
8041 apndShmLock
, /* xShmLock */
8042 apndShmBarrier
, /* xShmBarrier */
8043 apndShmUnmap
, /* xShmUnmap */
8044 apndFetch
, /* xFetch */
8045 apndUnfetch
/* xUnfetch */
8049 ** Close an apnd-file.
8051 static int apndClose(sqlite3_file
*pFile
){
8052 pFile
= ORIGFILE(pFile
);
8053 return pFile
->pMethods
->xClose(pFile
);
8057 ** Read data from an apnd-file.
8059 static int apndRead(
8060 sqlite3_file
*pFile
,
8065 ApndFile
*paf
= (ApndFile
*)pFile
;
8066 pFile
= ORIGFILE(pFile
);
8067 return pFile
->pMethods
->xRead(pFile
, zBuf
, iAmt
, paf
->iPgOne
+iOfst
);
8071 ** Add the append-mark onto what should become the end of the file.
8072 * If and only if this succeeds, internal ApndFile.iMark is updated.
8073 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
8075 static int apndWriteMark(
8077 sqlite3_file
*pFile
,
8078 sqlite_int64 iWriteEnd
8080 sqlite_int64 iPgOne
= paf
->iPgOne
;
8081 unsigned char a
[APND_MARK_SIZE
];
8082 int i
= APND_MARK_FOS_SZ
;
8084 assert(pFile
== ORIGFILE(paf
));
8085 memcpy(a
, APND_MARK_PREFIX
, APND_MARK_PREFIX_SZ
);
8087 a
[APND_MARK_PREFIX_SZ
+i
] = (unsigned char)(iPgOne
& 0xff);
8090 iWriteEnd
+= paf
->iPgOne
;
8091 if( SQLITE_OK
==(rc
= pFile
->pMethods
->xWrite
8092 (pFile
, a
, APND_MARK_SIZE
, iWriteEnd
)) ){
8093 paf
->iMark
= iWriteEnd
;
8099 ** Write data to an apnd-file.
8101 static int apndWrite(
8102 sqlite3_file
*pFile
,
8107 ApndFile
*paf
= (ApndFile
*)pFile
;
8108 sqlite_int64 iWriteEnd
= iOfst
+ iAmt
;
8109 if( iWriteEnd
>=APND_MAX_SIZE
) return SQLITE_FULL
;
8110 pFile
= ORIGFILE(pFile
);
8111 /* If append-mark is absent or will be overwritten, write it. */
8112 if( paf
->iMark
< 0 || paf
->iPgOne
+ iWriteEnd
> paf
->iMark
){
8113 int rc
= apndWriteMark(paf
, pFile
, iWriteEnd
);
8114 if( SQLITE_OK
!=rc
) return rc
;
8116 return pFile
->pMethods
->xWrite(pFile
, zBuf
, iAmt
, paf
->iPgOne
+iOfst
);
8120 ** Truncate an apnd-file.
8122 static int apndTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
8123 ApndFile
*paf
= (ApndFile
*)pFile
;
8124 pFile
= ORIGFILE(pFile
);
8125 /* The append mark goes out first so truncate failure does not lose it. */
8126 if( SQLITE_OK
!=apndWriteMark(paf
, pFile
, size
) ) return SQLITE_IOERR
;
8127 /* Truncate underlying file just past append mark */
8128 return pFile
->pMethods
->xTruncate(pFile
, paf
->iMark
+APND_MARK_SIZE
);
8132 ** Sync an apnd-file.
8134 static int apndSync(sqlite3_file
*pFile
, int flags
){
8135 pFile
= ORIGFILE(pFile
);
8136 return pFile
->pMethods
->xSync(pFile
, flags
);
8140 ** Return the current file-size of an apnd-file.
8141 ** If the append mark is not yet there, the file-size is 0.
8143 static int apndFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
8144 ApndFile
*paf
= (ApndFile
*)pFile
;
8145 *pSize
= ( paf
->iMark
>= 0 )? (paf
->iMark
- paf
->iPgOne
) : 0;
8150 ** Lock an apnd-file.
8152 static int apndLock(sqlite3_file
*pFile
, int eLock
){
8153 pFile
= ORIGFILE(pFile
);
8154 return pFile
->pMethods
->xLock(pFile
, eLock
);
8158 ** Unlock an apnd-file.
8160 static int apndUnlock(sqlite3_file
*pFile
, int eLock
){
8161 pFile
= ORIGFILE(pFile
);
8162 return pFile
->pMethods
->xUnlock(pFile
, eLock
);
8166 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
8168 static int apndCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
8169 pFile
= ORIGFILE(pFile
);
8170 return pFile
->pMethods
->xCheckReservedLock(pFile
, pResOut
);
8174 ** File control method. For custom operations on an apnd-file.
8176 static int apndFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
8177 ApndFile
*paf
= (ApndFile
*)pFile
;
8179 pFile
= ORIGFILE(pFile
);
8180 if( op
==SQLITE_FCNTL_SIZE_HINT
) *(sqlite3_int64
*)pArg
+= paf
->iPgOne
;
8181 rc
= pFile
->pMethods
->xFileControl(pFile
, op
, pArg
);
8182 if( rc
==SQLITE_OK
&& op
==SQLITE_FCNTL_VFSNAME
){
8183 *(char**)pArg
= sqlite3_mprintf("apnd(%lld)/%z", paf
->iPgOne
,*(char**)pArg
);
8189 ** Return the sector-size in bytes for an apnd-file.
8191 static int apndSectorSize(sqlite3_file
*pFile
){
8192 pFile
= ORIGFILE(pFile
);
8193 return pFile
->pMethods
->xSectorSize(pFile
);
8197 ** Return the device characteristic flags supported by an apnd-file.
8199 static int apndDeviceCharacteristics(sqlite3_file
*pFile
){
8200 pFile
= ORIGFILE(pFile
);
8201 return pFile
->pMethods
->xDeviceCharacteristics(pFile
);
8204 /* Create a shared memory file mapping */
8205 static int apndShmMap(
8206 sqlite3_file
*pFile
,
8212 pFile
= ORIGFILE(pFile
);
8213 return pFile
->pMethods
->xShmMap(pFile
,iPg
,pgsz
,bExtend
,pp
);
8216 /* Perform locking on a shared-memory segment */
8217 static int apndShmLock(sqlite3_file
*pFile
, int offset
, int n
, int flags
){
8218 pFile
= ORIGFILE(pFile
);
8219 return pFile
->pMethods
->xShmLock(pFile
,offset
,n
,flags
);
8222 /* Memory barrier operation on shared memory */
8223 static void apndShmBarrier(sqlite3_file
*pFile
){
8224 pFile
= ORIGFILE(pFile
);
8225 pFile
->pMethods
->xShmBarrier(pFile
);
8228 /* Unmap a shared memory segment */
8229 static int apndShmUnmap(sqlite3_file
*pFile
, int deleteFlag
){
8230 pFile
= ORIGFILE(pFile
);
8231 return pFile
->pMethods
->xShmUnmap(pFile
,deleteFlag
);
8234 /* Fetch a page of a memory-mapped file */
8235 static int apndFetch(
8236 sqlite3_file
*pFile
,
8237 sqlite3_int64 iOfst
,
8241 ApndFile
*p
= (ApndFile
*)pFile
;
8242 if( p
->iMark
< 0 || iOfst
+iAmt
> p
->iMark
){
8243 return SQLITE_IOERR
; /* Cannot read what is not yet there. */
8245 pFile
= ORIGFILE(pFile
);
8246 return pFile
->pMethods
->xFetch(pFile
, iOfst
+p
->iPgOne
, iAmt
, pp
);
8249 /* Release a memory-mapped page */
8250 static int apndUnfetch(sqlite3_file
*pFile
, sqlite3_int64 iOfst
, void *pPage
){
8251 ApndFile
*p
= (ApndFile
*)pFile
;
8252 pFile
= ORIGFILE(pFile
);
8253 return pFile
->pMethods
->xUnfetch(pFile
, iOfst
+p
->iPgOne
, pPage
);
8257 ** Try to read the append-mark off the end of a file. Return the
8258 ** start of the appended database if the append-mark is present.
8259 ** If there is no valid append-mark, return -1;
8261 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
8262 ** indicates that the appended database contains at least one page. The
8263 ** start-of-database value must be a multiple of 512.
8265 static sqlite3_int64
apndReadMark(sqlite3_int64 sz
, sqlite3_file
*pFile
){
8267 sqlite3_int64 iMark
;
8268 int msbs
= 8 * (APND_MARK_FOS_SZ
-1);
8269 unsigned char a
[APND_MARK_SIZE
];
8271 if( APND_MARK_SIZE
!=(sz
& 0x1ff) ) return -1;
8272 rc
= pFile
->pMethods
->xRead(pFile
, a
, APND_MARK_SIZE
, sz
-APND_MARK_SIZE
);
8274 if( memcmp(a
, APND_MARK_PREFIX
, APND_MARK_PREFIX_SZ
)!=0 ) return -1;
8275 iMark
= ((sqlite3_int64
)(a
[APND_MARK_PREFIX_SZ
] & 0x7f)) << msbs
;
8278 iMark
|= (sqlite3_int64
)a
[APND_MARK_PREFIX_SZ
+i
]<<msbs
;
8280 if( iMark
> (sz
- APND_MARK_SIZE
- 512) ) return -1;
8281 if( iMark
& 0x1ff ) return -1;
8285 static const char apvfsSqliteHdr
[] = "SQLite format 3";
8287 ** Check to see if the file is an appendvfs SQLite database file.
8288 ** Return true iff it is such. Parameter sz is the file's size.
8290 static int apndIsAppendvfsDatabase(sqlite3_int64 sz
, sqlite3_file
*pFile
){
8293 sqlite3_int64 iMark
= apndReadMark(sz
, pFile
);
8295 /* If file has the correct end-marker, the expected odd size, and the
8296 ** SQLite DB type marker where the end-marker puts it, then it
8297 ** is an appendvfs database.
8299 rc
= pFile
->pMethods
->xRead(pFile
, zHdr
, sizeof(zHdr
), iMark
);
8301 && memcmp(zHdr
, apvfsSqliteHdr
, sizeof(zHdr
))==0
8302 && (sz
& 0x1ff) == APND_MARK_SIZE
8303 && sz
>=512+APND_MARK_SIZE
8305 return 1; /* It's an appendvfs database */
8312 ** Check to see if the file is an ordinary SQLite database file.
8313 ** Return true iff so. Parameter sz is the file's size.
8315 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz
, sqlite3_file
*pFile
){
8317 if( apndIsAppendvfsDatabase(sz
, pFile
) /* rule 2 */
8318 || (sz
& 0x1ff) != 0
8319 || SQLITE_OK
!=pFile
->pMethods
->xRead(pFile
, zHdr
, sizeof(zHdr
), 0)
8320 || memcmp(zHdr
, apvfsSqliteHdr
, sizeof(zHdr
))!=0
8329 ** Open an apnd file handle.
8331 static int apndOpen(
8332 sqlite3_vfs
*pApndVfs
,
8334 sqlite3_file
*pFile
,
8338 ApndFile
*pApndFile
= (ApndFile
*)pFile
;
8339 sqlite3_file
*pBaseFile
= ORIGFILE(pFile
);
8340 sqlite3_vfs
*pBaseVfs
= ORIGVFS(pApndVfs
);
8342 sqlite3_int64 sz
= 0;
8343 if( (flags
& SQLITE_OPEN_MAIN_DB
)==0 ){
8344 /* The appendvfs is not to be used for transient or temporary databases.
8345 ** Just use the base VFS open to initialize the given file object and
8346 ** open the underlying file. (Appendvfs is then unused for this file.)
8348 return pBaseVfs
->xOpen(pBaseVfs
, zName
, pFile
, flags
, pOutFlags
);
8350 memset(pApndFile
, 0, sizeof(ApndFile
));
8351 pFile
->pMethods
= &apnd_io_methods
;
8352 pApndFile
->iMark
= -1; /* Append mark not yet written */
8354 rc
= pBaseVfs
->xOpen(pBaseVfs
, zName
, pBaseFile
, flags
, pOutFlags
);
8355 if( rc
==SQLITE_OK
){
8356 rc
= pBaseFile
->pMethods
->xFileSize(pBaseFile
, &sz
);
8358 pBaseFile
->pMethods
->xClose(pBaseFile
);
8362 pFile
->pMethods
= 0;
8365 if( apndIsOrdinaryDatabaseFile(sz
, pBaseFile
) ){
8366 /* The file being opened appears to be just an ordinary DB. Copy
8367 ** the base dispatch-table so this instance mimics the base VFS.
8369 memmove(pApndFile
, pBaseFile
, pBaseVfs
->szOsFile
);
8372 pApndFile
->iPgOne
= apndReadMark(sz
, pFile
);
8373 if( pApndFile
->iPgOne
>=0 ){
8374 pApndFile
->iMark
= sz
- APND_MARK_SIZE
; /* Append mark found */
8377 if( (flags
& SQLITE_OPEN_CREATE
)==0 ){
8378 pBaseFile
->pMethods
->xClose(pBaseFile
);
8379 rc
= SQLITE_CANTOPEN
;
8380 pFile
->pMethods
= 0;
8382 /* Round newly added appendvfs location to #define'd page boundary.
8383 ** Note that nothing has yet been written to the underlying file.
8384 ** The append mark will be written along with first content write.
8385 ** Until then, paf->iMark value indicates it is not yet written.
8387 pApndFile
->iPgOne
= APND_START_ROUNDUP(sz
);
8393 ** Delete an apnd file.
8394 ** For an appendvfs, this could mean delete the appendvfs portion,
8395 ** leaving the appendee as it was before it gained an appendvfs.
8396 ** For now, this code deletes the underlying file too.
8398 static int apndDelete(sqlite3_vfs
*pVfs
, const char *zPath
, int dirSync
){
8399 return ORIGVFS(pVfs
)->xDelete(ORIGVFS(pVfs
), zPath
, dirSync
);
8403 ** All other VFS methods are pass-thrus.
8405 static int apndAccess(
8411 return ORIGVFS(pVfs
)->xAccess(ORIGVFS(pVfs
), zPath
, flags
, pResOut
);
8413 static int apndFullPathname(
8419 return ORIGVFS(pVfs
)->xFullPathname(ORIGVFS(pVfs
),zPath
,nOut
,zOut
);
8421 static void *apndDlOpen(sqlite3_vfs
*pVfs
, const char *zPath
){
8422 return ORIGVFS(pVfs
)->xDlOpen(ORIGVFS(pVfs
), zPath
);
8424 static void apndDlError(sqlite3_vfs
*pVfs
, int nByte
, char *zErrMsg
){
8425 ORIGVFS(pVfs
)->xDlError(ORIGVFS(pVfs
), nByte
, zErrMsg
);
8427 static void (*apndDlSym(sqlite3_vfs
*pVfs
, void *p
, const char *zSym
))(void){
8428 return ORIGVFS(pVfs
)->xDlSym(ORIGVFS(pVfs
), p
, zSym
);
8430 static void apndDlClose(sqlite3_vfs
*pVfs
, void *pHandle
){
8431 ORIGVFS(pVfs
)->xDlClose(ORIGVFS(pVfs
), pHandle
);
8433 static int apndRandomness(sqlite3_vfs
*pVfs
, int nByte
, char *zBufOut
){
8434 return ORIGVFS(pVfs
)->xRandomness(ORIGVFS(pVfs
), nByte
, zBufOut
);
8436 static int apndSleep(sqlite3_vfs
*pVfs
, int nMicro
){
8437 return ORIGVFS(pVfs
)->xSleep(ORIGVFS(pVfs
), nMicro
);
8439 static int apndCurrentTime(sqlite3_vfs
*pVfs
, double *pTimeOut
){
8440 return ORIGVFS(pVfs
)->xCurrentTime(ORIGVFS(pVfs
), pTimeOut
);
8442 static int apndGetLastError(sqlite3_vfs
*pVfs
, int a
, char *b
){
8443 return ORIGVFS(pVfs
)->xGetLastError(ORIGVFS(pVfs
), a
, b
);
8445 static int apndCurrentTimeInt64(sqlite3_vfs
*pVfs
, sqlite3_int64
*p
){
8446 return ORIGVFS(pVfs
)->xCurrentTimeInt64(ORIGVFS(pVfs
), p
);
8448 static int apndSetSystemCall(
8451 sqlite3_syscall_ptr pCall
8453 return ORIGVFS(pVfs
)->xSetSystemCall(ORIGVFS(pVfs
),zName
,pCall
);
8455 static sqlite3_syscall_ptr
apndGetSystemCall(
8459 return ORIGVFS(pVfs
)->xGetSystemCall(ORIGVFS(pVfs
),zName
);
8461 static const char *apndNextSystemCall(sqlite3_vfs
*pVfs
, const char *zName
){
8462 return ORIGVFS(pVfs
)->xNextSystemCall(ORIGVFS(pVfs
), zName
);
8470 ** This routine is called when the extension is loaded.
8471 ** Register the new VFS.
8473 int sqlite3_appendvfs_init(
8476 const sqlite3_api_routines
*pApi
8480 SQLITE_EXTENSION_INIT2(pApi
);
8483 pOrig
= sqlite3_vfs_find(0);
8484 if( pOrig
==0 ) return SQLITE_ERROR
;
8485 apnd_vfs
.iVersion
= pOrig
->iVersion
;
8486 apnd_vfs
.pAppData
= pOrig
;
8487 apnd_vfs
.szOsFile
= pOrig
->szOsFile
+ sizeof(ApndFile
);
8488 rc
= sqlite3_vfs_register(&apnd_vfs
, 0);
8489 #ifdef APPENDVFS_TEST
8490 if( rc
==SQLITE_OK
){
8491 rc
= sqlite3_auto_extension((void(*)(void))apndvfsRegister
);
8494 if( rc
==SQLITE_OK
) rc
= SQLITE_OK_LOAD_PERMANENTLY
;
8498 /************************* End ../ext/misc/appendvfs.c ********************/
8500 #ifdef SQLITE_HAVE_ZLIB
8501 /************************* Begin ../ext/misc/zipfile.c ******************/
8505 ** The author disclaims copyright to this source code. In place of
8506 ** a legal notice, here is a blessing:
8508 ** May you do good and not evil.
8509 ** May you find forgiveness for yourself and forgive others.
8510 ** May you share freely, never taking more than you give.
8512 ******************************************************************************
8514 ** This file implements a virtual table for reading and writing ZIP archive
8519 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
8521 ** Current limitations:
8523 ** * No support for encryption
8524 ** * No support for ZIP archives spanning multiple files
8525 ** * No support for zip64 extensions
8526 ** * Only the "inflate/deflate" (zlib) compression method is supported
8528 /* #include "sqlite3ext.h" */
8529 SQLITE_EXTENSION_INIT1
8536 #ifndef SQLITE_OMIT_VIRTUALTABLE
8538 #ifndef SQLITE_AMALGAMATION
8541 # ifdef HAVE_UINT32_T
8542 # define UINT32_TYPE uint32_t
8544 # define UINT32_TYPE unsigned int
8548 # ifdef HAVE_UINT16_T
8549 # define UINT16_TYPE uint16_t
8551 # define UINT16_TYPE unsigned short int
8554 /* typedef sqlite3_int64 i64; */
8555 /* typedef unsigned char u8; */
8556 /* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
8557 /* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
8558 #define MIN(a,b) ((a)<(b) ? (a) : (b))
8560 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
8561 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
8563 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
8564 # define ALWAYS(X) (1)
8565 # define NEVER(X) (0)
8566 #elif !defined(NDEBUG)
8567 # define ALWAYS(X) ((X)?1:(assert(0),0))
8568 # define NEVER(X) ((X)?(assert(0),1):0)
8570 # define ALWAYS(X) (X)
8571 # define NEVER(X) (X)
8574 #endif /* SQLITE_AMALGAMATION */
8577 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
8579 ** In some ways it would be better to obtain these values from system
8580 ** header files. But, the dependency is undesirable and (a) these
8581 ** have been stable for decades, (b) the values are part of POSIX and
8582 ** are also made explicit in [man stat], and (c) are part of the
8583 ** file format for zip archives.
8586 # define S_IFDIR 0040000
8589 # define S_IFREG 0100000
8592 # define S_IFLNK 0120000
8595 static const char ZIPFILE_SCHEMA
[] =
8597 "name PRIMARY KEY," /* 0: Name of file in zip archive */
8598 "mode," /* 1: POSIX mode for file */
8599 "mtime," /* 2: Last modification time (secs since 1970)*/
8600 "sz," /* 3: Size of object */
8601 "rawdata," /* 4: Raw data */
8602 "data," /* 5: Uncompressed data */
8603 "method," /* 6: Compression method (integer) */
8604 "z HIDDEN" /* 7: Name of zip file */
8607 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
8608 #define ZIPFILE_BUFFER_SIZE (64*1024)
8612 ** Magic numbers used to read and write zip files.
8614 ** ZIPFILE_NEWENTRY_MADEBY:
8615 ** Use this value for the "version-made-by" field in new zip file
8616 ** entries. The upper byte indicates "unix", and the lower byte
8617 ** indicates that the zip file matches pkzip specification 3.0.
8618 ** This is what info-zip seems to do.
8620 ** ZIPFILE_NEWENTRY_REQUIRED:
8621 ** Value for "version-required-to-extract" field of new entries.
8622 ** Version 2.0 is required to support folders and deflate compression.
8624 ** ZIPFILE_NEWENTRY_FLAGS:
8625 ** Value for "general-purpose-bit-flags" field of new entries. Bit
8626 ** 11 means "utf-8 filename and comment".
8628 ** ZIPFILE_SIGNATURE_CDS:
8629 ** First 4 bytes of a valid CDS record.
8631 ** ZIPFILE_SIGNATURE_LFH:
8632 ** First 4 bytes of a valid LFH record.
8634 ** ZIPFILE_SIGNATURE_EOCD
8635 ** First 4 bytes of a valid EOCD record.
8637 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
8638 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
8639 #define ZIPFILE_NEWENTRY_REQUIRED 20
8640 #define ZIPFILE_NEWENTRY_FLAGS 0x800
8641 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
8642 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
8643 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
8646 ** The sizes of the fixed-size part of each of the three main data
8647 ** structures in a zip archive.
8649 #define ZIPFILE_LFH_FIXED_SZ 30
8650 #define ZIPFILE_EOCD_FIXED_SZ 22
8651 #define ZIPFILE_CDS_FIXED_SZ 46
8654 *** 4.3.16 End of central directory record:
8656 *** end of central dir signature 4 bytes (0x06054b50)
8657 *** number of this disk 2 bytes
8658 *** number of the disk with the
8659 *** start of the central directory 2 bytes
8660 *** total number of entries in the
8661 *** central directory on this disk 2 bytes
8662 *** total number of entries in
8663 *** the central directory 2 bytes
8664 *** size of the central directory 4 bytes
8665 *** offset of start of central
8666 *** directory with respect to
8667 *** the starting disk number 4 bytes
8668 *** .ZIP file comment length 2 bytes
8669 *** .ZIP file comment (variable size)
8671 typedef struct ZipfileEOCD ZipfileEOCD
;
8672 struct ZipfileEOCD
{
8682 *** 4.3.12 Central directory structure:
8686 *** central file header signature 4 bytes (0x02014b50)
8687 *** version made by 2 bytes
8688 *** version needed to extract 2 bytes
8689 *** general purpose bit flag 2 bytes
8690 *** compression method 2 bytes
8691 *** last mod file time 2 bytes
8692 *** last mod file date 2 bytes
8694 *** compressed size 4 bytes
8695 *** uncompressed size 4 bytes
8696 *** file name length 2 bytes
8697 *** extra field length 2 bytes
8698 *** file comment length 2 bytes
8699 *** disk number start 2 bytes
8700 *** internal file attributes 2 bytes
8701 *** external file attributes 4 bytes
8702 *** relative offset of local header 4 bytes
8704 typedef struct ZipfileCDS ZipfileCDS
;
8707 u16 iVersionExtract
;
8722 char *zFile
; /* Filename (sqlite3_malloc()) */
8726 *** 4.3.7 Local file header:
8728 *** local file header signature 4 bytes (0x04034b50)
8729 *** version needed to extract 2 bytes
8730 *** general purpose bit flag 2 bytes
8731 *** compression method 2 bytes
8732 *** last mod file time 2 bytes
8733 *** last mod file date 2 bytes
8735 *** compressed size 4 bytes
8736 *** uncompressed size 4 bytes
8737 *** file name length 2 bytes
8738 *** extra field length 2 bytes
8741 typedef struct ZipfileLFH ZipfileLFH
;
8743 u16 iVersionExtract
;
8755 typedef struct ZipfileEntry ZipfileEntry
;
8756 struct ZipfileEntry
{
8757 ZipfileCDS cds
; /* Parsed CDS record */
8758 u32 mUnixTime
; /* Modification time, in UNIX format */
8759 u8
*aExtra
; /* cds.nExtra+cds.nComment bytes of extra data */
8760 i64 iDataOff
; /* Offset to data in file (if aData==0) */
8761 u8
*aData
; /* cds.szCompressed bytes of compressed data */
8762 ZipfileEntry
*pNext
; /* Next element in in-memory CDS */
8766 ** Cursor type for zipfile tables.
8768 typedef struct ZipfileCsr ZipfileCsr
;
8770 sqlite3_vtab_cursor base
; /* Base class - must be first */
8771 i64 iId
; /* Cursor ID */
8772 u8 bEof
; /* True when at EOF */
8773 u8 bNoop
; /* If next xNext() call is no-op */
8775 /* Used outside of write transactions */
8776 FILE *pFile
; /* Zip file */
8777 i64 iNextOff
; /* Offset of next record in central directory */
8778 ZipfileEOCD eocd
; /* Parse of central directory record */
8780 ZipfileEntry
*pFreeEntry
; /* Free this list when cursor is closed or reset */
8781 ZipfileEntry
*pCurrent
; /* Current entry */
8782 ZipfileCsr
*pCsrNext
; /* Next cursor on same virtual table */
8785 typedef struct ZipfileTab ZipfileTab
;
8787 sqlite3_vtab base
; /* Base class - must be first */
8788 char *zFile
; /* Zip file this table accesses (may be NULL) */
8789 sqlite3
*db
; /* Host database connection */
8790 u8
*aBuffer
; /* Temporary buffer used for various tasks */
8792 ZipfileCsr
*pCsrList
; /* List of cursors */
8795 /* The following are used by write transactions only */
8796 ZipfileEntry
*pFirstEntry
; /* Linked list of all files (if pWriteFd!=0) */
8797 ZipfileEntry
*pLastEntry
; /* Last element in pFirstEntry list */
8798 FILE *pWriteFd
; /* File handle open on zip archive */
8799 i64 szCurrent
; /* Current size of zip archive */
8800 i64 szOrig
; /* Size of archive at start of transaction */
8804 ** Set the error message contained in context ctx to the results of
8805 ** vprintf(zFmt, ...).
8807 static void zipfileCtxErrorMsg(sqlite3_context
*ctx
, const char *zFmt
, ...){
8811 zMsg
= sqlite3_vmprintf(zFmt
, ap
);
8812 sqlite3_result_error(ctx
, zMsg
, -1);
8818 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
8819 ** is not quoted, do nothing.
8821 static void zipfileDequote(char *zIn
){
8823 if( q
=='"' || q
=='\'' || q
=='`' || q
=='[' ){
8826 if( q
=='[' ) q
= ']';
8827 while( ALWAYS(zIn
[iIn
]) ){
8828 char c
= zIn
[iIn
++];
8829 if( c
==q
&& zIn
[iIn
++]!=q
) break;
8837 ** Construct a new ZipfileTab virtual table object.
8839 ** argv[0] -> module name ("zipfile")
8840 ** argv[1] -> database name
8841 ** argv[2] -> table name
8842 ** argv[...] -> "column name" and other module argument fields.
8844 static int zipfileConnect(
8847 int argc
, const char *const*argv
,
8848 sqlite3_vtab
**ppVtab
,
8851 int nByte
= sizeof(ZipfileTab
) + ZIPFILE_BUFFER_SIZE
;
8853 const char *zFile
= 0;
8854 ZipfileTab
*pNew
= 0;
8858 /* If the table name is not "zipfile", require that the argument be
8859 ** specified. This stops zipfile tables from being created as:
8861 ** CREATE VIRTUAL TABLE zzz USING zipfile();
8863 ** It does not prevent:
8865 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
8867 assert( 0==sqlite3_stricmp(argv
[0], "zipfile") );
8868 if( (0!=sqlite3_stricmp(argv
[2], "zipfile") && argc
<4) || argc
>4 ){
8869 *pzErr
= sqlite3_mprintf("zipfile constructor requires one argument");
8870 return SQLITE_ERROR
;
8875 nFile
= (int)strlen(zFile
)+1;
8878 rc
= sqlite3_declare_vtab(db
, ZIPFILE_SCHEMA
);
8879 if( rc
==SQLITE_OK
){
8880 pNew
= (ZipfileTab
*)sqlite3_malloc64((sqlite3_int64
)nByte
+nFile
);
8881 if( pNew
==0 ) return SQLITE_NOMEM
;
8882 memset(pNew
, 0, nByte
+nFile
);
8884 pNew
->aBuffer
= (u8
*)&pNew
[1];
8886 pNew
->zFile
= (char*)&pNew
->aBuffer
[ZIPFILE_BUFFER_SIZE
];
8887 memcpy(pNew
->zFile
, zFile
, nFile
);
8888 zipfileDequote(pNew
->zFile
);
8891 sqlite3_vtab_config(db
, SQLITE_VTAB_DIRECTONLY
);
8892 *ppVtab
= (sqlite3_vtab
*)pNew
;
8897 ** Free the ZipfileEntry structure indicated by the only argument.
8899 static void zipfileEntryFree(ZipfileEntry
*p
){
8901 sqlite3_free(p
->cds
.zFile
);
8907 ** Release resources that should be freed at the end of a write
8910 static void zipfileCleanupTransaction(ZipfileTab
*pTab
){
8911 ZipfileEntry
*pEntry
;
8912 ZipfileEntry
*pNext
;
8914 if( pTab
->pWriteFd
){
8915 fclose(pTab
->pWriteFd
);
8918 for(pEntry
=pTab
->pFirstEntry
; pEntry
; pEntry
=pNext
){
8919 pNext
= pEntry
->pNext
;
8920 zipfileEntryFree(pEntry
);
8922 pTab
->pFirstEntry
= 0;
8923 pTab
->pLastEntry
= 0;
8924 pTab
->szCurrent
= 0;
8929 ** This method is the destructor for zipfile vtab objects.
8931 static int zipfileDisconnect(sqlite3_vtab
*pVtab
){
8932 zipfileCleanupTransaction((ZipfileTab
*)pVtab
);
8933 sqlite3_free(pVtab
);
8938 ** Constructor for a new ZipfileCsr object.
8940 static int zipfileOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCsr
){
8941 ZipfileTab
*pTab
= (ZipfileTab
*)p
;
8943 pCsr
= sqlite3_malloc(sizeof(*pCsr
));
8944 *ppCsr
= (sqlite3_vtab_cursor
*)pCsr
;
8946 return SQLITE_NOMEM
;
8948 memset(pCsr
, 0, sizeof(*pCsr
));
8949 pCsr
->iId
= ++pTab
->iNextCsrid
;
8950 pCsr
->pCsrNext
= pTab
->pCsrList
;
8951 pTab
->pCsrList
= pCsr
;
8956 ** Reset a cursor back to the state it was in when first returned
8957 ** by zipfileOpen().
8959 static void zipfileResetCursor(ZipfileCsr
*pCsr
){
8961 ZipfileEntry
*pNext
;
8965 fclose(pCsr
->pFile
);
8967 zipfileEntryFree(pCsr
->pCurrent
);
8971 for(p
=pCsr
->pFreeEntry
; p
; p
=pNext
){
8973 zipfileEntryFree(p
);
8978 ** Destructor for an ZipfileCsr.
8980 static int zipfileClose(sqlite3_vtab_cursor
*cur
){
8981 ZipfileCsr
*pCsr
= (ZipfileCsr
*)cur
;
8982 ZipfileTab
*pTab
= (ZipfileTab
*)(pCsr
->base
.pVtab
);
8984 zipfileResetCursor(pCsr
);
8986 /* Remove this cursor from the ZipfileTab.pCsrList list. */
8987 for(pp
=&pTab
->pCsrList
; *pp
!=pCsr
; pp
=&((*pp
)->pCsrNext
));
8988 *pp
= pCsr
->pCsrNext
;
8995 ** Set the error message for the virtual table associated with cursor
8996 ** pCsr to the results of vprintf(zFmt, ...).
8998 static void zipfileTableErr(ZipfileTab
*pTab
, const char *zFmt
, ...){
9001 sqlite3_free(pTab
->base
.zErrMsg
);
9002 pTab
->base
.zErrMsg
= sqlite3_vmprintf(zFmt
, ap
);
9005 static void zipfileCursorErr(ZipfileCsr
*pCsr
, const char *zFmt
, ...){
9008 sqlite3_free(pCsr
->base
.pVtab
->zErrMsg
);
9009 pCsr
->base
.pVtab
->zErrMsg
= sqlite3_vmprintf(zFmt
, ap
);
9014 ** Read nRead bytes of data from offset iOff of file pFile into buffer
9015 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
9018 ** If an error does occur, output variable (*pzErrmsg) may be set to point
9019 ** to an English language error message. It is the responsibility of the
9020 ** caller to eventually free this buffer using
9023 static int zipfileReadData(
9024 FILE *pFile
, /* Read from this file */
9025 u8
*aRead
, /* Read into this buffer */
9026 int nRead
, /* Number of bytes to read */
9027 i64 iOff
, /* Offset to read from */
9028 char **pzErrmsg
/* OUT: Error message (from sqlite3_malloc) */
9031 fseek(pFile
, (long)iOff
, SEEK_SET
);
9032 n
= fread(aRead
, 1, nRead
, pFile
);
9033 if( (int)n
!=nRead
){
9034 *pzErrmsg
= sqlite3_mprintf("error in fread()");
9035 return SQLITE_ERROR
;
9040 static int zipfileAppendData(
9047 fseek(pTab
->pWriteFd
, (long)pTab
->szCurrent
, SEEK_SET
);
9048 n
= fwrite(aWrite
, 1, nWrite
, pTab
->pWriteFd
);
9049 if( (int)n
!=nWrite
){
9050 pTab
->base
.zErrMsg
= sqlite3_mprintf("error in fwrite()");
9051 return SQLITE_ERROR
;
9053 pTab
->szCurrent
+= nWrite
;
9059 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
9061 static u16
zipfileGetU16(const u8
*aBuf
){
9062 return (aBuf
[1] << 8) + aBuf
[0];
9066 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
9068 static u32
zipfileGetU32(const u8
*aBuf
){
9069 if( aBuf
==0 ) return 0;
9070 return ((u32
)(aBuf
[3]) << 24)
9071 + ((u32
)(aBuf
[2]) << 16)
9072 + ((u32
)(aBuf
[1]) << 8)
9073 + ((u32
)(aBuf
[0]) << 0);
9077 ** Write a 16-bit little endiate integer into buffer aBuf.
9079 static void zipfilePutU16(u8
*aBuf
, u16 val
){
9080 aBuf
[0] = val
& 0xFF;
9081 aBuf
[1] = (val
>>8) & 0xFF;
9085 ** Write a 32-bit little endiate integer into buffer aBuf.
9087 static void zipfilePutU32(u8
*aBuf
, u32 val
){
9088 aBuf
[0] = val
& 0xFF;
9089 aBuf
[1] = (val
>>8) & 0xFF;
9090 aBuf
[2] = (val
>>16) & 0xFF;
9091 aBuf
[3] = (val
>>24) & 0xFF;
9094 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
9095 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
9097 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
9098 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
9101 ** Magic numbers used to read CDS records.
9103 #define ZIPFILE_CDS_NFILE_OFF 28
9104 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
9107 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
9108 ** if the record is not well-formed, or SQLITE_OK otherwise.
9110 static int zipfileReadCDS(u8
*aBuf
, ZipfileCDS
*pCDS
){
9112 u32 sig
= zipfileRead32(aRead
);
9114 if( sig
!=ZIPFILE_SIGNATURE_CDS
){
9117 pCDS
->iVersionMadeBy
= zipfileRead16(aRead
);
9118 pCDS
->iVersionExtract
= zipfileRead16(aRead
);
9119 pCDS
->flags
= zipfileRead16(aRead
);
9120 pCDS
->iCompression
= zipfileRead16(aRead
);
9121 pCDS
->mTime
= zipfileRead16(aRead
);
9122 pCDS
->mDate
= zipfileRead16(aRead
);
9123 pCDS
->crc32
= zipfileRead32(aRead
);
9124 pCDS
->szCompressed
= zipfileRead32(aRead
);
9125 pCDS
->szUncompressed
= zipfileRead32(aRead
);
9126 assert( aRead
==&aBuf
[ZIPFILE_CDS_NFILE_OFF
] );
9127 pCDS
->nFile
= zipfileRead16(aRead
);
9128 pCDS
->nExtra
= zipfileRead16(aRead
);
9129 pCDS
->nComment
= zipfileRead16(aRead
);
9130 pCDS
->iDiskStart
= zipfileRead16(aRead
);
9131 pCDS
->iInternalAttr
= zipfileRead16(aRead
);
9132 pCDS
->iExternalAttr
= zipfileRead32(aRead
);
9133 pCDS
->iOffset
= zipfileRead32(aRead
);
9134 assert( aRead
==&aBuf
[ZIPFILE_CDS_FIXED_SZ
] );
9141 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
9142 ** if the record is not well-formed, or SQLITE_OK otherwise.
9144 static int zipfileReadLFH(
9148 u8
*aRead
= aBuffer
;
9151 u32 sig
= zipfileRead32(aRead
);
9152 if( sig
!=ZIPFILE_SIGNATURE_LFH
){
9155 pLFH
->iVersionExtract
= zipfileRead16(aRead
);
9156 pLFH
->flags
= zipfileRead16(aRead
);
9157 pLFH
->iCompression
= zipfileRead16(aRead
);
9158 pLFH
->mTime
= zipfileRead16(aRead
);
9159 pLFH
->mDate
= zipfileRead16(aRead
);
9160 pLFH
->crc32
= zipfileRead32(aRead
);
9161 pLFH
->szCompressed
= zipfileRead32(aRead
);
9162 pLFH
->szUncompressed
= zipfileRead32(aRead
);
9163 pLFH
->nFile
= zipfileRead16(aRead
);
9164 pLFH
->nExtra
= zipfileRead16(aRead
);
9171 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
9172 ** Scan through this buffer to find an "extra-timestamp" field. If one
9173 ** exists, extract the 32-bit modification-timestamp from it and store
9174 ** the value in output parameter *pmTime.
9176 ** Zero is returned if no extra-timestamp record could be found (and so
9177 ** *pmTime is left unchanged), or non-zero otherwise.
9179 ** The general format of an extra field is:
9181 ** Header ID 2 bytes
9182 ** Data Size 2 bytes
9185 static int zipfileScanExtra(u8
*aExtra
, int nExtra
, u32
*pmTime
){
9188 u8
*pEnd
= &aExtra
[nExtra
];
9191 u16 id
= zipfileRead16(p
);
9192 u16 nByte
= zipfileRead16(p
);
9195 case ZIPFILE_EXTRA_TIMESTAMP
: {
9197 if( b
& 0x01 ){ /* 0x01 -> modtime is present */
9198 *pmTime
= zipfileGetU32(&p
[1]);
9211 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
9212 ** fields of the CDS structure passed as the only argument to a 32-bit
9213 ** UNIX seconds-since-the-epoch timestamp. Return the result.
9215 ** "Standard" MS-DOS time format:
9217 ** File modification time:
9218 ** Bits 00-04: seconds divided by 2
9219 ** Bits 05-10: minute
9221 ** File modification date:
9223 ** Bits 05-08: month (1-12)
9224 ** Bits 09-15: years from 1980
9226 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
9228 static u32
zipfileMtime(ZipfileCDS
*pCDS
){
9229 int Y
,M
,D
,X1
,X2
,A
,B
,sec
,min
,hr
;
9231 Y
= (1980 + ((pCDS
->mDate
>> 9) & 0x7F));
9232 M
= ((pCDS
->mDate
>> 5) & 0x0F);
9233 D
= (pCDS
->mDate
& 0x1F);
9234 sec
= (pCDS
->mTime
& 0x1F)*2;
9235 min
= (pCDS
->mTime
>> 5) & 0x3F;
9236 hr
= (pCDS
->mTime
>> 11) & 0x1F;
9241 X1
= 36525*(Y
+4716)/100;
9242 X2
= 306001*(M
+1)/10000;
9245 JDsec
= (i64
)((X1
+ X2
+ D
+ B
- 1524.5)*86400) + hr
*3600 + min
*60 + sec
;
9246 return (u32
)(JDsec
- (i64
)24405875*(i64
)8640);
9250 ** The opposite of zipfileMtime(). This function populates the mTime and
9251 ** mDate fields of the CDS structure passed as the first argument according
9252 ** to the UNIX timestamp value passed as the second.
9254 static void zipfileMtimeToDos(ZipfileCDS
*pCds
, u32 mUnixTime
){
9255 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
9256 i64 JD
= (i64
)2440588 + mUnixTime
/ (24*60*60);
9262 A
= (int)((JD
- 1867216.25)/36524.25);
9263 A
= (int)(JD
+ 1 + A
- (A
/4));
9265 C
= (int)((B
- 122.1)/365.25);
9266 D
= (36525*(C
&32767))/100;
9267 E
= (int)((B
-D
)/30.6001);
9269 day
= B
- D
- (int)(30.6001*E
);
9270 mon
= (E
<14 ? E
-1 : E
-13);
9271 yr
= mon
>2 ? C
-4716 : C
-4715;
9273 hr
= (mUnixTime
% (24*60*60)) / (60*60);
9274 min
= (mUnixTime
% (60*60)) / 60;
9275 sec
= (mUnixTime
% 60);
9278 pCds
->mDate
= (u16
)(day
+ (mon
<< 5) + ((yr
-1980) << 9));
9279 pCds
->mTime
= (u16
)(sec
/2 + (min
<<5) + (hr
<<11));
9281 pCds
->mDate
= pCds
->mTime
= 0;
9284 assert( mUnixTime
<315507600
9285 || mUnixTime
==zipfileMtime(pCds
)
9286 || ((mUnixTime
% 2) && mUnixTime
-1==zipfileMtime(pCds
))
9287 /* || (mUnixTime % 2) */
9292 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
9293 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
9294 ** then pFile is a file-handle open on a zip file. In either case, this
9295 ** function creates a ZipfileEntry object based on the zip archive entry
9296 ** for which the CDS record is at offset iOff.
9298 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
9299 ** the new object. Otherwise, an SQLite error code is returned and the
9300 ** final value of (*ppEntry) undefined.
9302 static int zipfileGetEntry(
9303 ZipfileTab
*pTab
, /* Store any error message here */
9304 const u8
*aBlob
, /* Pointer to in-memory file image */
9305 int nBlob
, /* Size of aBlob[] in bytes */
9306 FILE *pFile
, /* If aBlob==0, read from this file */
9307 i64 iOff
, /* Offset of CDS record */
9308 ZipfileEntry
**ppEntry
/* OUT: Pointer to new object */
9311 char **pzErr
= &pTab
->base
.zErrMsg
;
9316 aRead
= pTab
->aBuffer
;
9317 rc
= zipfileReadData(pFile
, aRead
, ZIPFILE_CDS_FIXED_SZ
, iOff
, pzErr
);
9319 aRead
= (u8
*)&aBlob
[iOff
];
9322 if( rc
==SQLITE_OK
){
9323 sqlite3_int64 nAlloc
;
9326 int nFile
= zipfileGetU16(&aRead
[ZIPFILE_CDS_NFILE_OFF
]);
9327 int nExtra
= zipfileGetU16(&aRead
[ZIPFILE_CDS_NFILE_OFF
+2]);
9328 nExtra
+= zipfileGetU16(&aRead
[ZIPFILE_CDS_NFILE_OFF
+4]);
9330 nAlloc
= sizeof(ZipfileEntry
) + nExtra
;
9332 nAlloc
+= zipfileGetU32(&aRead
[ZIPFILE_CDS_SZCOMPRESSED_OFF
]);
9335 pNew
= (ZipfileEntry
*)sqlite3_malloc64(nAlloc
);
9339 memset(pNew
, 0, sizeof(ZipfileEntry
));
9340 rc
= zipfileReadCDS(aRead
, &pNew
->cds
);
9341 if( rc
!=SQLITE_OK
){
9342 *pzErr
= sqlite3_mprintf("failed to read CDS at offset %lld", iOff
);
9343 }else if( aBlob
==0 ){
9344 rc
= zipfileReadData(
9345 pFile
, aRead
, nExtra
+nFile
, iOff
+ZIPFILE_CDS_FIXED_SZ
, pzErr
9348 aRead
= (u8
*)&aBlob
[iOff
+ ZIPFILE_CDS_FIXED_SZ
];
9352 if( rc
==SQLITE_OK
){
9353 u32
*pt
= &pNew
->mUnixTime
;
9354 pNew
->cds
.zFile
= sqlite3_mprintf("%.*s", nFile
, aRead
);
9355 pNew
->aExtra
= (u8
*)&pNew
[1];
9356 memcpy(pNew
->aExtra
, &aRead
[nFile
], nExtra
);
9357 if( pNew
->cds
.zFile
==0 ){
9359 }else if( 0==zipfileScanExtra(&aRead
[nFile
], pNew
->cds
.nExtra
, pt
) ){
9360 pNew
->mUnixTime
= zipfileMtime(&pNew
->cds
);
9364 if( rc
==SQLITE_OK
){
9365 static const int szFix
= ZIPFILE_LFH_FIXED_SZ
;
9368 rc
= zipfileReadData(pFile
, aRead
, szFix
, pNew
->cds
.iOffset
, pzErr
);
9370 aRead
= (u8
*)&aBlob
[pNew
->cds
.iOffset
];
9373 if( rc
==SQLITE_OK
) rc
= zipfileReadLFH(aRead
, &lfh
);
9374 if( rc
==SQLITE_OK
){
9375 pNew
->iDataOff
= pNew
->cds
.iOffset
+ ZIPFILE_LFH_FIXED_SZ
;
9376 pNew
->iDataOff
+= lfh
.nFile
+ lfh
.nExtra
;
9377 if( aBlob
&& pNew
->cds
.szCompressed
){
9378 pNew
->aData
= &pNew
->aExtra
[nExtra
];
9379 memcpy(pNew
->aData
, &aBlob
[pNew
->iDataOff
], pNew
->cds
.szCompressed
);
9382 *pzErr
= sqlite3_mprintf("failed to read LFH at offset %d",
9383 (int)pNew
->cds
.iOffset
9388 if( rc
!=SQLITE_OK
){
9389 zipfileEntryFree(pNew
);
9399 ** Advance an ZipfileCsr to its next row of output.
9401 static int zipfileNext(sqlite3_vtab_cursor
*cur
){
9402 ZipfileCsr
*pCsr
= (ZipfileCsr
*)cur
;
9406 i64 iEof
= pCsr
->eocd
.iOffset
+ pCsr
->eocd
.nSize
;
9407 zipfileEntryFree(pCsr
->pCurrent
);
9409 if( pCsr
->iNextOff
>=iEof
){
9412 ZipfileEntry
*p
= 0;
9413 ZipfileTab
*pTab
= (ZipfileTab
*)(cur
->pVtab
);
9414 rc
= zipfileGetEntry(pTab
, 0, 0, pCsr
->pFile
, pCsr
->iNextOff
, &p
);
9415 if( rc
==SQLITE_OK
){
9416 pCsr
->iNextOff
+= ZIPFILE_CDS_FIXED_SZ
;
9417 pCsr
->iNextOff
+= (int)p
->cds
.nExtra
+ p
->cds
.nFile
+ p
->cds
.nComment
;
9423 pCsr
->pCurrent
= pCsr
->pCurrent
->pNext
;
9425 if( pCsr
->pCurrent
==0 ){
9434 static void zipfileFree(void *p
) {
9439 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
9440 ** size is nOut bytes. This function uncompresses the data and sets the
9441 ** return value in context pCtx to the result (a blob).
9443 ** If an error occurs, an error code is left in pCtx instead.
9445 static void zipfileInflate(
9446 sqlite3_context
*pCtx
, /* Store result here */
9447 const u8
*aIn
, /* Compressed data */
9448 int nIn
, /* Size of buffer aIn[] in bytes */
9449 int nOut
/* Expected output size */
9451 u8
*aRes
= sqlite3_malloc(nOut
);
9453 sqlite3_result_error_nomem(pCtx
);
9457 memset(&str
, 0, sizeof(str
));
9459 str
.next_in
= (Byte
*)aIn
;
9461 str
.next_out
= (Byte
*)aRes
;
9462 str
.avail_out
= nOut
;
9464 err
= inflateInit2(&str
, -15);
9466 zipfileCtxErrorMsg(pCtx
, "inflateInit2() failed (%d)", err
);
9468 err
= inflate(&str
, Z_NO_FLUSH
);
9469 if( err
!=Z_STREAM_END
){
9470 zipfileCtxErrorMsg(pCtx
, "inflate() failed (%d)", err
);
9472 sqlite3_result_blob(pCtx
, aRes
, nOut
, zipfileFree
);
9482 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
9483 ** compresses it and sets (*ppOut) to point to a buffer containing the
9484 ** compressed data. The caller is responsible for eventually calling
9485 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
9486 ** is set to the size of buffer (*ppOut) in bytes.
9488 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
9489 ** code is returned and an error message left in virtual-table handle
9490 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
9493 static int zipfileDeflate(
9494 const u8
*aIn
, int nIn
, /* Input */
9495 u8
**ppOut
, int *pnOut
, /* Output */
9496 char **pzErr
/* OUT: Error message */
9499 sqlite3_int64 nAlloc
;
9503 memset(&str
, 0, sizeof(str
));
9504 str
.next_in
= (Bytef
*)aIn
;
9506 deflateInit2(&str
, 9, Z_DEFLATED
, -15, 8, Z_DEFAULT_STRATEGY
);
9508 nAlloc
= deflateBound(&str
, nIn
);
9509 aOut
= (u8
*)sqlite3_malloc64(nAlloc
);
9514 str
.next_out
= aOut
;
9515 str
.avail_out
= nAlloc
;
9516 res
= deflate(&str
, Z_FINISH
);
9517 if( res
==Z_STREAM_END
){
9519 *pnOut
= (int)str
.total_out
;
9522 *pzErr
= sqlite3_mprintf("zipfile: deflate() error");
9533 ** Return values of columns for the row at which the series_cursor
9534 ** is currently pointing.
9536 static int zipfileColumn(
9537 sqlite3_vtab_cursor
*cur
, /* The cursor */
9538 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
9539 int i
/* Which column to return */
9541 ZipfileCsr
*pCsr
= (ZipfileCsr
*)cur
;
9542 ZipfileCDS
*pCDS
= &pCsr
->pCurrent
->cds
;
9546 sqlite3_result_text(ctx
, pCDS
->zFile
, -1, SQLITE_TRANSIENT
);
9549 /* TODO: Whether or not the following is correct surely depends on
9550 ** the platform on which the archive was created. */
9551 sqlite3_result_int(ctx
, pCDS
->iExternalAttr
>> 16);
9553 case 2: { /* mtime */
9554 sqlite3_result_int64(ctx
, pCsr
->pCurrent
->mUnixTime
);
9558 if( sqlite3_vtab_nochange(ctx
)==0 ){
9559 sqlite3_result_int64(ctx
, pCDS
->szUncompressed
);
9563 case 4: /* rawdata */
9564 if( sqlite3_vtab_nochange(ctx
) ) break;
9565 case 5: { /* data */
9566 if( i
==4 || pCDS
->iCompression
==0 || pCDS
->iCompression
==8 ){
9567 int sz
= pCDS
->szCompressed
;
9568 int szFinal
= pCDS
->szUncompressed
;
9572 if( pCsr
->pCurrent
->aData
){
9573 aBuf
= pCsr
->pCurrent
->aData
;
9575 aBuf
= aFree
= sqlite3_malloc64(sz
);
9579 FILE *pFile
= pCsr
->pFile
;
9581 pFile
= ((ZipfileTab
*)(pCsr
->base
.pVtab
))->pWriteFd
;
9583 rc
= zipfileReadData(pFile
, aBuf
, sz
, pCsr
->pCurrent
->iDataOff
,
9584 &pCsr
->base
.pVtab
->zErrMsg
9588 if( rc
==SQLITE_OK
){
9589 if( i
==5 && pCDS
->iCompression
){
9590 zipfileInflate(ctx
, aBuf
, sz
, szFinal
);
9592 sqlite3_result_blob(ctx
, aBuf
, sz
, SQLITE_TRANSIENT
);
9595 sqlite3_free(aFree
);
9597 /* Figure out if this is a directory or a zero-sized file. Consider
9598 ** it to be a directory either if the mode suggests so, or if
9599 ** the final character in the name is '/'. */
9600 u32 mode
= pCDS
->iExternalAttr
>> 16;
9601 if( !(mode
& S_IFDIR
)
9603 && pCDS
->zFile
[pCDS
->nFile
-1]!='/'
9605 sqlite3_result_blob(ctx
, "", 0, SQLITE_STATIC
);
9611 case 6: /* method */
9612 sqlite3_result_int(ctx
, pCDS
->iCompression
);
9616 sqlite3_result_int64(ctx
, pCsr
->iId
);
9624 ** Return TRUE if the cursor is at EOF.
9626 static int zipfileEof(sqlite3_vtab_cursor
*cur
){
9627 ZipfileCsr
*pCsr
= (ZipfileCsr
*)cur
;
9632 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
9633 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
9634 ** is guaranteed to be a file-handle open on a zip file.
9636 ** This function attempts to locate the EOCD record within the zip archive
9637 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
9638 ** returned if successful. Otherwise, an SQLite error code is returned and
9639 ** an English language error message may be left in virtual-table pTab.
9641 static int zipfileReadEOCD(
9642 ZipfileTab
*pTab
, /* Return errors here */
9643 const u8
*aBlob
, /* Pointer to in-memory file image */
9644 int nBlob
, /* Size of aBlob[] in bytes */
9645 FILE *pFile
, /* Read from this file if aBlob==0 */
9646 ZipfileEOCD
*pEOCD
/* Object to populate */
9648 u8
*aRead
= pTab
->aBuffer
; /* Temporary buffer */
9649 int nRead
; /* Bytes to read from file */
9652 memset(pEOCD
, 0, sizeof(ZipfileEOCD
));
9654 i64 iOff
; /* Offset to read from */
9655 i64 szFile
; /* Total size of file in bytes */
9656 fseek(pFile
, 0, SEEK_END
);
9657 szFile
= (i64
)ftell(pFile
);
9661 nRead
= (int)(MIN(szFile
, ZIPFILE_BUFFER_SIZE
));
9662 iOff
= szFile
- nRead
;
9663 rc
= zipfileReadData(pFile
, aRead
, nRead
, iOff
, &pTab
->base
.zErrMsg
);
9665 nRead
= (int)(MIN(nBlob
, ZIPFILE_BUFFER_SIZE
));
9666 aRead
= (u8
*)&aBlob
[nBlob
-nRead
];
9669 if( rc
==SQLITE_OK
){
9672 /* Scan backwards looking for the signature bytes */
9673 for(i
=nRead
-20; i
>=0; i
--){
9674 if( aRead
[i
]==0x50 && aRead
[i
+1]==0x4b
9675 && aRead
[i
+2]==0x05 && aRead
[i
+3]==0x06
9681 pTab
->base
.zErrMsg
= sqlite3_mprintf(
9682 "cannot find end of central directory record"
9684 return SQLITE_ERROR
;
9688 pEOCD
->iDisk
= zipfileRead16(aRead
);
9689 pEOCD
->iFirstDisk
= zipfileRead16(aRead
);
9690 pEOCD
->nEntry
= zipfileRead16(aRead
);
9691 pEOCD
->nEntryTotal
= zipfileRead16(aRead
);
9692 pEOCD
->nSize
= zipfileRead32(aRead
);
9693 pEOCD
->iOffset
= zipfileRead32(aRead
);
9700 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
9701 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
9702 ** to the end of the list. Otherwise, it is added to the list immediately
9703 ** before pBefore (which is guaranteed to be a part of said list).
9705 static void zipfileAddEntry(
9707 ZipfileEntry
*pBefore
,
9710 assert( (pTab
->pFirstEntry
==0)==(pTab
->pLastEntry
==0) );
9711 assert( pNew
->pNext
==0 );
9713 if( pTab
->pFirstEntry
==0 ){
9714 pTab
->pFirstEntry
= pTab
->pLastEntry
= pNew
;
9716 assert( pTab
->pLastEntry
->pNext
==0 );
9717 pTab
->pLastEntry
->pNext
= pNew
;
9718 pTab
->pLastEntry
= pNew
;
9722 for(pp
=&pTab
->pFirstEntry
; *pp
!=pBefore
; pp
=&((*pp
)->pNext
));
9723 pNew
->pNext
= pBefore
;
9728 static int zipfileLoadDirectory(ZipfileTab
*pTab
, const u8
*aBlob
, int nBlob
){
9734 rc
= zipfileReadEOCD(pTab
, aBlob
, nBlob
, pTab
->pWriteFd
, &eocd
);
9735 iOff
= eocd
.iOffset
;
9736 for(i
=0; rc
==SQLITE_OK
&& i
<eocd
.nEntry
; i
++){
9737 ZipfileEntry
*pNew
= 0;
9738 rc
= zipfileGetEntry(pTab
, aBlob
, nBlob
, pTab
->pWriteFd
, iOff
, &pNew
);
9740 if( rc
==SQLITE_OK
){
9741 zipfileAddEntry(pTab
, 0, pNew
);
9742 iOff
+= ZIPFILE_CDS_FIXED_SZ
;
9743 iOff
+= (int)pNew
->cds
.nExtra
+ pNew
->cds
.nFile
+ pNew
->cds
.nComment
;
9750 ** xFilter callback.
9752 static int zipfileFilter(
9753 sqlite3_vtab_cursor
*cur
,
9754 int idxNum
, const char *idxStr
,
9755 int argc
, sqlite3_value
**argv
9757 ZipfileTab
*pTab
= (ZipfileTab
*)cur
->pVtab
;
9758 ZipfileCsr
*pCsr
= (ZipfileCsr
*)cur
;
9759 const char *zFile
= 0; /* Zip file to scan */
9760 int rc
= SQLITE_OK
; /* Return Code */
9761 int bInMemory
= 0; /* True for an in-memory zipfile */
9766 zipfileResetCursor(pCsr
);
9769 zFile
= pTab
->zFile
;
9770 }else if( idxNum
==0 ){
9771 zipfileCursorErr(pCsr
, "zipfile() function requires an argument");
9772 return SQLITE_ERROR
;
9773 }else if( sqlite3_value_type(argv
[0])==SQLITE_BLOB
){
9774 static const u8 aEmptyBlob
= 0;
9775 const u8
*aBlob
= (const u8
*)sqlite3_value_blob(argv
[0]);
9776 int nBlob
= sqlite3_value_bytes(argv
[0]);
9777 assert( pTab
->pFirstEntry
==0 );
9779 aBlob
= &aEmptyBlob
;
9782 rc
= zipfileLoadDirectory(pTab
, aBlob
, nBlob
);
9783 pCsr
->pFreeEntry
= pTab
->pFirstEntry
;
9784 pTab
->pFirstEntry
= pTab
->pLastEntry
= 0;
9785 if( rc
!=SQLITE_OK
) return rc
;
9788 zFile
= (const char*)sqlite3_value_text(argv
[0]);
9791 if( 0==pTab
->pWriteFd
&& 0==bInMemory
){
9792 pCsr
->pFile
= zFile
? fopen(zFile
, "rb") : 0;
9793 if( pCsr
->pFile
==0 ){
9794 zipfileCursorErr(pCsr
, "cannot open file: %s", zFile
);
9797 rc
= zipfileReadEOCD(pTab
, 0, 0, pCsr
->pFile
, &pCsr
->eocd
);
9798 if( rc
==SQLITE_OK
){
9799 if( pCsr
->eocd
.nEntry
==0 ){
9802 pCsr
->iNextOff
= pCsr
->eocd
.iOffset
;
9803 rc
= zipfileNext(cur
);
9809 pCsr
->pCurrent
= pCsr
->pFreeEntry
? pCsr
->pFreeEntry
: pTab
->pFirstEntry
;
9810 rc
= zipfileNext(cur
);
9817 ** xBestIndex callback.
9819 static int zipfileBestIndex(
9821 sqlite3_index_info
*pIdxInfo
9828 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++){
9829 const struct sqlite3_index_constraint
*pCons
= &pIdxInfo
->aConstraint
[i
];
9830 if( pCons
->iColumn
!=ZIPFILE_F_COLUMN_IDX
) continue;
9831 if( pCons
->usable
==0 ){
9833 }else if( pCons
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
9837 pIdxInfo
->estimatedCost
= 1000.0;
9839 pIdxInfo
->aConstraintUsage
[idx
].argvIndex
= 1;
9840 pIdxInfo
->aConstraintUsage
[idx
].omit
= 1;
9841 pIdxInfo
->idxNum
= 1;
9842 }else if( unusable
){
9843 return SQLITE_CONSTRAINT
;
9848 static ZipfileEntry
*zipfileNewEntry(const char *zPath
){
9850 pNew
= sqlite3_malloc(sizeof(ZipfileEntry
));
9852 memset(pNew
, 0, sizeof(ZipfileEntry
));
9853 pNew
->cds
.zFile
= sqlite3_mprintf("%s", zPath
);
9854 if( pNew
->cds
.zFile
==0 ){
9862 static int zipfileSerializeLFH(ZipfileEntry
*pEntry
, u8
*aBuf
){
9863 ZipfileCDS
*pCds
= &pEntry
->cds
;
9868 /* Write the LFH itself */
9869 zipfileWrite32(a
, ZIPFILE_SIGNATURE_LFH
);
9870 zipfileWrite16(a
, pCds
->iVersionExtract
);
9871 zipfileWrite16(a
, pCds
->flags
);
9872 zipfileWrite16(a
, pCds
->iCompression
);
9873 zipfileWrite16(a
, pCds
->mTime
);
9874 zipfileWrite16(a
, pCds
->mDate
);
9875 zipfileWrite32(a
, pCds
->crc32
);
9876 zipfileWrite32(a
, pCds
->szCompressed
);
9877 zipfileWrite32(a
, pCds
->szUncompressed
);
9878 zipfileWrite16(a
, (u16
)pCds
->nFile
);
9879 zipfileWrite16(a
, pCds
->nExtra
);
9880 assert( a
==&aBuf
[ZIPFILE_LFH_FIXED_SZ
] );
9882 /* Add the file name */
9883 memcpy(a
, pCds
->zFile
, (int)pCds
->nFile
);
9884 a
+= (int)pCds
->nFile
;
9886 /* The "extra" data */
9887 zipfileWrite16(a
, ZIPFILE_EXTRA_TIMESTAMP
);
9888 zipfileWrite16(a
, 5);
9890 zipfileWrite32(a
, pEntry
->mUnixTime
);
9895 static int zipfileAppendEntry(
9897 ZipfileEntry
*pEntry
,
9901 u8
*aBuf
= pTab
->aBuffer
;
9905 nBuf
= zipfileSerializeLFH(pEntry
, aBuf
);
9906 rc
= zipfileAppendData(pTab
, aBuf
, nBuf
);
9907 if( rc
==SQLITE_OK
){
9908 pEntry
->iDataOff
= pTab
->szCurrent
;
9909 rc
= zipfileAppendData(pTab
, pData
, nData
);
9915 static int zipfileGetMode(
9916 sqlite3_value
*pVal
,
9917 int bIsDir
, /* If true, default to directory */
9918 u32
*pMode
, /* OUT: Mode value */
9919 char **pzErr
/* OUT: Error message */
9921 const char *z
= (const char*)sqlite3_value_text(pVal
);
9924 mode
= (bIsDir
? (S_IFDIR
+ 0755) : (S_IFREG
+ 0644));
9925 }else if( z
[0]>='0' && z
[0]<='9' ){
9926 mode
= (unsigned int)sqlite3_value_int(pVal
);
9928 const char zTemplate
[11] = "-rwxrwxrwx";
9930 if( strlen(z
)!=10 ) goto parse_error
;
9932 case '-': mode
|= S_IFREG
; break;
9933 case 'd': mode
|= S_IFDIR
; break;
9934 case 'l': mode
|= S_IFLNK
; break;
9935 default: goto parse_error
;
9937 for(i
=1; i
<10; i
++){
9938 if( z
[i
]==zTemplate
[i
] ) mode
|= 1 << (9-i
);
9939 else if( z
[i
]!='-' ) goto parse_error
;
9942 if( ((mode
& S_IFDIR
)==0)==bIsDir
){
9943 /* The "mode" attribute is a directory, but data has been specified.
9944 ** Or vice-versa - no data but "mode" is a file or symlink. */
9945 *pzErr
= sqlite3_mprintf("zipfile: mode does not match data");
9946 return SQLITE_CONSTRAINT
;
9952 *pzErr
= sqlite3_mprintf("zipfile: parse error in mode: %s", z
);
9953 return SQLITE_ERROR
;
9957 ** Both (const char*) arguments point to nul-terminated strings. Argument
9958 ** nB is the value of strlen(zB). This function returns 0 if the strings are
9959 ** identical, ignoring any trailing '/' character in either path. */
9960 static int zipfileComparePath(const char *zA
, const char *zB
, int nB
){
9961 int nA
= (int)strlen(zA
);
9962 if( nA
>0 && zA
[nA
-1]=='/' ) nA
--;
9963 if( nB
>0 && zB
[nB
-1]=='/' ) nB
--;
9964 if( nA
==nB
&& memcmp(zA
, zB
, nA
)==0 ) return 0;
9968 static int zipfileBegin(sqlite3_vtab
*pVtab
){
9969 ZipfileTab
*pTab
= (ZipfileTab
*)pVtab
;
9972 assert( pTab
->pWriteFd
==0 );
9973 if( pTab
->zFile
==0 || pTab
->zFile
[0]==0 ){
9974 pTab
->base
.zErrMsg
= sqlite3_mprintf("zipfile: missing filename");
9975 return SQLITE_ERROR
;
9978 /* Open a write fd on the file. Also load the entire central directory
9979 ** structure into memory. During the transaction any new file data is
9980 ** appended to the archive file, but the central directory is accumulated
9981 ** in main-memory until the transaction is committed. */
9982 pTab
->pWriteFd
= fopen(pTab
->zFile
, "ab+");
9983 if( pTab
->pWriteFd
==0 ){
9984 pTab
->base
.zErrMsg
= sqlite3_mprintf(
9985 "zipfile: failed to open file %s for writing", pTab
->zFile
9989 fseek(pTab
->pWriteFd
, 0, SEEK_END
);
9990 pTab
->szCurrent
= pTab
->szOrig
= (i64
)ftell(pTab
->pWriteFd
);
9991 rc
= zipfileLoadDirectory(pTab
, 0, 0);
9994 if( rc
!=SQLITE_OK
){
9995 zipfileCleanupTransaction(pTab
);
10002 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
10005 static u32
zipfileTime(void){
10006 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(0);
10008 if( pVfs
==0 ) return 0;
10009 if( pVfs
->iVersion
>=2 && pVfs
->xCurrentTimeInt64
){
10011 pVfs
->xCurrentTimeInt64(pVfs
, &ms
);
10012 ret
= (u32
)((ms
/1000) - ((i64
)24405875 * 8640));
10015 pVfs
->xCurrentTime(pVfs
, &day
);
10016 ret
= (u32
)((day
- 2440587.5) * 86400);
10022 ** Return a 32-bit timestamp in UNIX epoch format.
10024 ** If the value passed as the only argument is either NULL or an SQL NULL,
10025 ** return the current time. Otherwise, return the value stored in (*pVal)
10026 ** cast to a 32-bit unsigned integer.
10028 static u32
zipfileGetTime(sqlite3_value
*pVal
){
10029 if( pVal
==0 || sqlite3_value_type(pVal
)==SQLITE_NULL
){
10030 return zipfileTime();
10032 return (u32
)sqlite3_value_int64(pVal
);
10036 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
10037 ** linked list. Remove it from the list and free the object.
10039 static void zipfileRemoveEntryFromList(ZipfileTab
*pTab
, ZipfileEntry
*pOld
){
10041 if( pTab
->pFirstEntry
==pOld
){
10042 pTab
->pFirstEntry
= pOld
->pNext
;
10043 if( pTab
->pLastEntry
==pOld
) pTab
->pLastEntry
= 0;
10046 for(p
=pTab
->pFirstEntry
; p
; p
=p
->pNext
){
10047 if( p
->pNext
==pOld
){
10048 p
->pNext
= pOld
->pNext
;
10049 if( pTab
->pLastEntry
==pOld
) pTab
->pLastEntry
= p
;
10054 zipfileEntryFree(pOld
);
10061 static int zipfileUpdate(
10062 sqlite3_vtab
*pVtab
,
10064 sqlite3_value
**apVal
,
10065 sqlite_int64
*pRowid
10067 ZipfileTab
*pTab
= (ZipfileTab
*)pVtab
;
10068 int rc
= SQLITE_OK
; /* Return Code */
10069 ZipfileEntry
*pNew
= 0; /* New in-memory CDS entry */
10071 u32 mode
= 0; /* Mode for new entry */
10072 u32 mTime
= 0; /* Modification time for new entry */
10073 i64 sz
= 0; /* Uncompressed size */
10074 const char *zPath
= 0; /* Path for new entry */
10075 int nPath
= 0; /* strlen(zPath) */
10076 const u8
*pData
= 0; /* Pointer to buffer containing content */
10077 int nData
= 0; /* Size of pData buffer in bytes */
10078 int iMethod
= 0; /* Compression method for new entry */
10079 u8
*pFree
= 0; /* Free this */
10080 char *zFree
= 0; /* Also free this */
10081 ZipfileEntry
*pOld
= 0;
10082 ZipfileEntry
*pOld2
= 0;
10083 int bUpdate
= 0; /* True for an update that modifies "name" */
10089 if( pTab
->pWriteFd
==0 ){
10090 rc
= zipfileBegin(pVtab
);
10091 if( rc
!=SQLITE_OK
) return rc
;
10094 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
10095 if( sqlite3_value_type(apVal
[0])!=SQLITE_NULL
){
10096 const char *zDelete
= (const char*)sqlite3_value_text(apVal
[0]);
10097 int nDelete
= (int)strlen(zDelete
);
10099 const char *zUpdate
= (const char*)sqlite3_value_text(apVal
[1]);
10100 if( zUpdate
&& zipfileComparePath(zUpdate
, zDelete
, nDelete
)!=0 ){
10104 for(pOld
=pTab
->pFirstEntry
; 1; pOld
=pOld
->pNext
){
10105 if( zipfileComparePath(pOld
->cds
.zFile
, zDelete
, nDelete
)==0 ){
10108 assert( pOld
->pNext
);
10113 /* Check that "sz" and "rawdata" are both NULL: */
10114 if( sqlite3_value_type(apVal
[5])!=SQLITE_NULL
){
10115 zipfileTableErr(pTab
, "sz must be NULL");
10116 rc
= SQLITE_CONSTRAINT
;
10118 if( sqlite3_value_type(apVal
[6])!=SQLITE_NULL
){
10119 zipfileTableErr(pTab
, "rawdata must be NULL");
10120 rc
= SQLITE_CONSTRAINT
;
10123 if( rc
==SQLITE_OK
){
10124 if( sqlite3_value_type(apVal
[7])==SQLITE_NULL
){
10125 /* data=NULL. A directory */
10128 /* Value specified for "data", and possibly "method". This must be
10129 ** a regular file or a symlink. */
10130 const u8
*aIn
= sqlite3_value_blob(apVal
[7]);
10131 int nIn
= sqlite3_value_bytes(apVal
[7]);
10132 int bAuto
= sqlite3_value_type(apVal
[8])==SQLITE_NULL
;
10134 iMethod
= sqlite3_value_int(apVal
[8]);
10138 if( iMethod
!=0 && iMethod
!=8 ){
10139 zipfileTableErr(pTab
, "unknown compression method: %d", iMethod
);
10140 rc
= SQLITE_CONSTRAINT
;
10142 if( bAuto
|| iMethod
){
10144 rc
= zipfileDeflate(aIn
, nIn
, &pFree
, &nCmp
, &pTab
->base
.zErrMsg
);
10145 if( rc
==SQLITE_OK
){
10146 if( iMethod
|| nCmp
<nIn
){
10153 iCrc32
= crc32(0, aIn
, nIn
);
10158 if( rc
==SQLITE_OK
){
10159 rc
= zipfileGetMode(apVal
[3], bIsDir
, &mode
, &pTab
->base
.zErrMsg
);
10162 if( rc
==SQLITE_OK
){
10163 zPath
= (const char*)sqlite3_value_text(apVal
[2]);
10164 if( zPath
==0 ) zPath
= "";
10165 nPath
= (int)strlen(zPath
);
10166 mTime
= zipfileGetTime(apVal
[4]);
10169 if( rc
==SQLITE_OK
&& bIsDir
){
10170 /* For a directory, check that the last character in the path is a
10171 ** '/'. This appears to be required for compatibility with info-zip
10172 ** (the unzip command on unix). It does not create directories
10174 if( nPath
<=0 || zPath
[nPath
-1]!='/' ){
10175 zFree
= sqlite3_mprintf("%s/", zPath
);
10176 zPath
= (const char*)zFree
;
10181 nPath
= (int)strlen(zPath
);
10186 /* Check that we're not inserting a duplicate entry -OR- updating an
10187 ** entry with a path, thereby making it into a duplicate. */
10188 if( (pOld
==0 || bUpdate
) && rc
==SQLITE_OK
){
10190 for(p
=pTab
->pFirstEntry
; p
; p
=p
->pNext
){
10191 if( zipfileComparePath(p
->cds
.zFile
, zPath
, nPath
)==0 ){
10192 switch( sqlite3_vtab_on_conflict(pTab
->db
) ){
10193 case SQLITE_IGNORE
: {
10194 goto zipfile_update_done
;
10196 case SQLITE_REPLACE
: {
10201 zipfileTableErr(pTab
, "duplicate name: \"%s\"", zPath
);
10202 rc
= SQLITE_CONSTRAINT
;
10211 if( rc
==SQLITE_OK
){
10212 /* Create the new CDS record. */
10213 pNew
= zipfileNewEntry(zPath
);
10217 pNew
->cds
.iVersionMadeBy
= ZIPFILE_NEWENTRY_MADEBY
;
10218 pNew
->cds
.iVersionExtract
= ZIPFILE_NEWENTRY_REQUIRED
;
10219 pNew
->cds
.flags
= ZIPFILE_NEWENTRY_FLAGS
;
10220 pNew
->cds
.iCompression
= (u16
)iMethod
;
10221 zipfileMtimeToDos(&pNew
->cds
, mTime
);
10222 pNew
->cds
.crc32
= iCrc32
;
10223 pNew
->cds
.szCompressed
= nData
;
10224 pNew
->cds
.szUncompressed
= (u32
)sz
;
10225 pNew
->cds
.iExternalAttr
= (mode
<<16);
10226 pNew
->cds
.iOffset
= (u32
)pTab
->szCurrent
;
10227 pNew
->cds
.nFile
= (u16
)nPath
;
10228 pNew
->mUnixTime
= (u32
)mTime
;
10229 rc
= zipfileAppendEntry(pTab
, pNew
, pData
, nData
);
10230 zipfileAddEntry(pTab
, pOld
, pNew
);
10235 if( rc
==SQLITE_OK
&& (pOld
|| pOld2
) ){
10237 for(pCsr
=pTab
->pCsrList
; pCsr
; pCsr
=pCsr
->pCsrNext
){
10238 if( pCsr
->pCurrent
&& (pCsr
->pCurrent
==pOld
|| pCsr
->pCurrent
==pOld2
) ){
10239 pCsr
->pCurrent
= pCsr
->pCurrent
->pNext
;
10244 zipfileRemoveEntryFromList(pTab
, pOld
);
10245 zipfileRemoveEntryFromList(pTab
, pOld2
);
10248 zipfile_update_done
:
10249 sqlite3_free(pFree
);
10250 sqlite3_free(zFree
);
10254 static int zipfileSerializeEOCD(ZipfileEOCD
*p
, u8
*aBuf
){
10256 zipfileWrite32(a
, ZIPFILE_SIGNATURE_EOCD
);
10257 zipfileWrite16(a
, p
->iDisk
);
10258 zipfileWrite16(a
, p
->iFirstDisk
);
10259 zipfileWrite16(a
, p
->nEntry
);
10260 zipfileWrite16(a
, p
->nEntryTotal
);
10261 zipfileWrite32(a
, p
->nSize
);
10262 zipfileWrite32(a
, p
->iOffset
);
10263 zipfileWrite16(a
, 0); /* Size of trailing comment in bytes*/
10268 static int zipfileAppendEOCD(ZipfileTab
*pTab
, ZipfileEOCD
*p
){
10269 int nBuf
= zipfileSerializeEOCD(p
, pTab
->aBuffer
);
10270 assert( nBuf
==ZIPFILE_EOCD_FIXED_SZ
);
10271 return zipfileAppendData(pTab
, pTab
->aBuffer
, nBuf
);
10275 ** Serialize the CDS structure into buffer aBuf[]. Return the number
10276 ** of bytes written.
10278 static int zipfileSerializeCDS(ZipfileEntry
*pEntry
, u8
*aBuf
){
10280 ZipfileCDS
*pCDS
= &pEntry
->cds
;
10282 if( pEntry
->aExtra
==0 ){
10286 zipfileWrite32(a
, ZIPFILE_SIGNATURE_CDS
);
10287 zipfileWrite16(a
, pCDS
->iVersionMadeBy
);
10288 zipfileWrite16(a
, pCDS
->iVersionExtract
);
10289 zipfileWrite16(a
, pCDS
->flags
);
10290 zipfileWrite16(a
, pCDS
->iCompression
);
10291 zipfileWrite16(a
, pCDS
->mTime
);
10292 zipfileWrite16(a
, pCDS
->mDate
);
10293 zipfileWrite32(a
, pCDS
->crc32
);
10294 zipfileWrite32(a
, pCDS
->szCompressed
);
10295 zipfileWrite32(a
, pCDS
->szUncompressed
);
10296 assert( a
==&aBuf
[ZIPFILE_CDS_NFILE_OFF
] );
10297 zipfileWrite16(a
, pCDS
->nFile
);
10298 zipfileWrite16(a
, pCDS
->nExtra
);
10299 zipfileWrite16(a
, pCDS
->nComment
);
10300 zipfileWrite16(a
, pCDS
->iDiskStart
);
10301 zipfileWrite16(a
, pCDS
->iInternalAttr
);
10302 zipfileWrite32(a
, pCDS
->iExternalAttr
);
10303 zipfileWrite32(a
, pCDS
->iOffset
);
10305 memcpy(a
, pCDS
->zFile
, pCDS
->nFile
);
10308 if( pEntry
->aExtra
){
10309 int n
= (int)pCDS
->nExtra
+ (int)pCDS
->nComment
;
10310 memcpy(a
, pEntry
->aExtra
, n
);
10313 assert( pCDS
->nExtra
==9 );
10314 zipfileWrite16(a
, ZIPFILE_EXTRA_TIMESTAMP
);
10315 zipfileWrite16(a
, 5);
10317 zipfileWrite32(a
, pEntry
->mUnixTime
);
10323 static int zipfileCommit(sqlite3_vtab
*pVtab
){
10324 ZipfileTab
*pTab
= (ZipfileTab
*)pVtab
;
10325 int rc
= SQLITE_OK
;
10326 if( pTab
->pWriteFd
){
10327 i64 iOffset
= pTab
->szCurrent
;
10332 /* Write out all entries */
10333 for(p
=pTab
->pFirstEntry
; rc
==SQLITE_OK
&& p
; p
=p
->pNext
){
10334 int n
= zipfileSerializeCDS(p
, pTab
->aBuffer
);
10335 rc
= zipfileAppendData(pTab
, pTab
->aBuffer
, n
);
10339 /* Write out the EOCD record */
10341 eocd
.iFirstDisk
= 0;
10342 eocd
.nEntry
= (u16
)nEntry
;
10343 eocd
.nEntryTotal
= (u16
)nEntry
;
10344 eocd
.nSize
= (u32
)(pTab
->szCurrent
- iOffset
);
10345 eocd
.iOffset
= (u32
)iOffset
;
10346 rc
= zipfileAppendEOCD(pTab
, &eocd
);
10348 zipfileCleanupTransaction(pTab
);
10353 static int zipfileRollback(sqlite3_vtab
*pVtab
){
10354 return zipfileCommit(pVtab
);
10357 static ZipfileCsr
*zipfileFindCursor(ZipfileTab
*pTab
, i64 iId
){
10359 for(pCsr
=pTab
->pCsrList
; pCsr
; pCsr
=pCsr
->pCsrNext
){
10360 if( iId
==pCsr
->iId
) break;
10365 static void zipfileFunctionCds(
10366 sqlite3_context
*context
,
10368 sqlite3_value
**argv
10371 ZipfileTab
*pTab
= (ZipfileTab
*)sqlite3_user_data(context
);
10374 pCsr
= zipfileFindCursor(pTab
, sqlite3_value_int64(argv
[0]));
10376 ZipfileCDS
*p
= &pCsr
->pCurrent
->cds
;
10377 char *zRes
= sqlite3_mprintf("{"
10378 "\"version-made-by\" : %u, "
10379 "\"version-to-extract\" : %u, "
10381 "\"compression\" : %u, "
10385 "\"compressed-size\" : %u, "
10386 "\"uncompressed-size\" : %u, "
10387 "\"file-name-length\" : %u, "
10388 "\"extra-field-length\" : %u, "
10389 "\"file-comment-length\" : %u, "
10390 "\"disk-number-start\" : %u, "
10391 "\"internal-attr\" : %u, "
10392 "\"external-attr\" : %u, "
10393 "\"offset\" : %u }",
10394 (u32
)p
->iVersionMadeBy
, (u32
)p
->iVersionExtract
,
10395 (u32
)p
->flags
, (u32
)p
->iCompression
,
10396 (u32
)p
->mTime
, (u32
)p
->mDate
,
10397 (u32
)p
->crc32
, (u32
)p
->szCompressed
,
10398 (u32
)p
->szUncompressed
, (u32
)p
->nFile
,
10399 (u32
)p
->nExtra
, (u32
)p
->nComment
,
10400 (u32
)p
->iDiskStart
, (u32
)p
->iInternalAttr
,
10401 (u32
)p
->iExternalAttr
, (u32
)p
->iOffset
10405 sqlite3_result_error_nomem(context
);
10407 sqlite3_result_text(context
, zRes
, -1, SQLITE_TRANSIENT
);
10408 sqlite3_free(zRes
);
10414 ** xFindFunction method.
10416 static int zipfileFindFunction(
10417 sqlite3_vtab
*pVtab
, /* Virtual table handle */
10418 int nArg
, /* Number of SQL function arguments */
10419 const char *zName
, /* Name of SQL function */
10420 void (**pxFunc
)(sqlite3_context
*,int,sqlite3_value
**), /* OUT: Result */
10421 void **ppArg
/* OUT: User data for *pxFunc */
10424 if( sqlite3_stricmp("zipfile_cds", zName
)==0 ){
10425 *pxFunc
= zipfileFunctionCds
;
10426 *ppArg
= (void*)pVtab
;
10432 typedef struct ZipfileBuffer ZipfileBuffer
;
10433 struct ZipfileBuffer
{
10434 u8
*a
; /* Pointer to buffer */
10435 int n
; /* Size of buffer in bytes */
10436 int nAlloc
; /* Byte allocated at a[] */
10439 typedef struct ZipfileCtx ZipfileCtx
;
10440 struct ZipfileCtx
{
10442 ZipfileBuffer body
;
10446 static int zipfileBufferGrow(ZipfileBuffer
*pBuf
, int nByte
){
10447 if( pBuf
->n
+nByte
>pBuf
->nAlloc
){
10449 sqlite3_int64 nNew
= pBuf
->n
? pBuf
->n
*2 : 512;
10450 int nReq
= pBuf
->n
+ nByte
;
10452 while( nNew
<nReq
) nNew
= nNew
*2;
10453 aNew
= sqlite3_realloc64(pBuf
->a
, nNew
);
10454 if( aNew
==0 ) return SQLITE_NOMEM
;
10456 pBuf
->nAlloc
= (int)nNew
;
10462 ** xStep() callback for the zipfile() aggregate. This can be called in
10463 ** any of the following ways:
10465 ** SELECT zipfile(name,data) ...
10466 ** SELECT zipfile(name,mode,mtime,data) ...
10467 ** SELECT zipfile(name,mode,mtime,data,method) ...
10469 static void zipfileStep(sqlite3_context
*pCtx
, int nVal
, sqlite3_value
**apVal
){
10470 ZipfileCtx
*p
; /* Aggregate function context */
10471 ZipfileEntry e
; /* New entry to add to zip archive */
10473 sqlite3_value
*pName
= 0;
10474 sqlite3_value
*pMode
= 0;
10475 sqlite3_value
*pMtime
= 0;
10476 sqlite3_value
*pData
= 0;
10477 sqlite3_value
*pMethod
= 0;
10481 int rc
= SQLITE_OK
;
10484 int iMethod
= -1; /* Compression method to use (0 or 8) */
10486 const u8
*aData
= 0; /* Possibly compressed data for new entry */
10487 int nData
= 0; /* Size of aData[] in bytes */
10488 int szUncompressed
= 0; /* Size of data before compression */
10489 u8
*aFree
= 0; /* Free this before returning */
10490 u32 iCrc32
= 0; /* crc32 of uncompressed data */
10492 char *zName
= 0; /* Path (name) of new entry */
10493 int nName
= 0; /* Size of zName in bytes */
10494 char *zFree
= 0; /* Free this before returning */
10497 memset(&e
, 0, sizeof(e
));
10498 p
= (ZipfileCtx
*)sqlite3_aggregate_context(pCtx
, sizeof(ZipfileCtx
));
10501 /* Martial the arguments into stack variables */
10502 if( nVal
!=2 && nVal
!=4 && nVal
!=5 ){
10503 zErr
= sqlite3_mprintf("wrong number of arguments to function zipfile()");
10505 goto zipfile_step_out
;
10515 pMethod
= apVal
[4];
10519 /* Check that the 'name' parameter looks ok. */
10520 zName
= (char*)sqlite3_value_text(pName
);
10521 nName
= sqlite3_value_bytes(pName
);
10523 zErr
= sqlite3_mprintf("first argument to zipfile() must be non-NULL");
10525 goto zipfile_step_out
;
10528 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
10529 ** deflate compression) or NULL (choose automatically). */
10530 if( pMethod
&& SQLITE_NULL
!=sqlite3_value_type(pMethod
) ){
10531 iMethod
= (int)sqlite3_value_int64(pMethod
);
10532 if( iMethod
!=0 && iMethod
!=8 ){
10533 zErr
= sqlite3_mprintf("illegal method value: %d", iMethod
);
10535 goto zipfile_step_out
;
10539 /* Now inspect the data. If this is NULL, then the new entry must be a
10540 ** directory. Otherwise, figure out whether or not the data should
10541 ** be deflated or simply stored in the zip archive. */
10542 if( sqlite3_value_type(pData
)==SQLITE_NULL
){
10546 aData
= sqlite3_value_blob(pData
);
10547 szUncompressed
= nData
= sqlite3_value_bytes(pData
);
10548 iCrc32
= crc32(0, aData
, nData
);
10549 if( iMethod
<0 || iMethod
==8 ){
10551 rc
= zipfileDeflate(aData
, nData
, &aFree
, &nOut
, &zErr
);
10552 if( rc
!=SQLITE_OK
){
10553 goto zipfile_step_out
;
10555 if( iMethod
==8 || nOut
<nData
){
10565 /* Decode the "mode" argument. */
10566 rc
= zipfileGetMode(pMode
, bIsDir
, &mode
, &zErr
);
10567 if( rc
) goto zipfile_step_out
;
10569 /* Decode the "mtime" argument. */
10570 e
.mUnixTime
= zipfileGetTime(pMtime
);
10572 /* If this is a directory entry, ensure that there is exactly one '/'
10573 ** at the end of the path. Or, if this is not a directory and the path
10574 ** ends in '/' it is an error. */
10576 if( nName
>0 && zName
[nName
-1]=='/' ){
10577 zErr
= sqlite3_mprintf("non-directory name must not end with /");
10579 goto zipfile_step_out
;
10582 if( nName
==0 || zName
[nName
-1]!='/' ){
10583 zName
= zFree
= sqlite3_mprintf("%s/", zName
);
10586 goto zipfile_step_out
;
10588 nName
= (int)strlen(zName
);
10590 while( nName
>1 && zName
[nName
-2]=='/' ) nName
--;
10594 /* Assemble the ZipfileEntry object for the new zip archive entry */
10595 e
.cds
.iVersionMadeBy
= ZIPFILE_NEWENTRY_MADEBY
;
10596 e
.cds
.iVersionExtract
= ZIPFILE_NEWENTRY_REQUIRED
;
10597 e
.cds
.flags
= ZIPFILE_NEWENTRY_FLAGS
;
10598 e
.cds
.iCompression
= (u16
)iMethod
;
10599 zipfileMtimeToDos(&e
.cds
, (u32
)e
.mUnixTime
);
10600 e
.cds
.crc32
= iCrc32
;
10601 e
.cds
.szCompressed
= nData
;
10602 e
.cds
.szUncompressed
= szUncompressed
;
10603 e
.cds
.iExternalAttr
= (mode
<<16);
10604 e
.cds
.iOffset
= p
->body
.n
;
10605 e
.cds
.nFile
= (u16
)nName
;
10606 e
.cds
.zFile
= zName
;
10608 /* Append the LFH to the body of the new archive */
10609 nByte
= ZIPFILE_LFH_FIXED_SZ
+ e
.cds
.nFile
+ 9;
10610 if( (rc
= zipfileBufferGrow(&p
->body
, nByte
)) ) goto zipfile_step_out
;
10611 p
->body
.n
+= zipfileSerializeLFH(&e
, &p
->body
.a
[p
->body
.n
]);
10613 /* Append the data to the body of the new archive */
10615 if( (rc
= zipfileBufferGrow(&p
->body
, nData
)) ) goto zipfile_step_out
;
10616 memcpy(&p
->body
.a
[p
->body
.n
], aData
, nData
);
10617 p
->body
.n
+= nData
;
10620 /* Append the CDS record to the directory of the new archive */
10621 nByte
= ZIPFILE_CDS_FIXED_SZ
+ e
.cds
.nFile
+ 9;
10622 if( (rc
= zipfileBufferGrow(&p
->cds
, nByte
)) ) goto zipfile_step_out
;
10623 p
->cds
.n
+= zipfileSerializeCDS(&e
, &p
->cds
.a
[p
->cds
.n
]);
10625 /* Increment the count of entries in the archive */
10629 sqlite3_free(aFree
);
10630 sqlite3_free(zFree
);
10633 sqlite3_result_error(pCtx
, zErr
, -1);
10635 sqlite3_result_error_code(pCtx
, rc
);
10638 sqlite3_free(zErr
);
10642 ** xFinalize() callback for zipfile aggregate function.
10644 static void zipfileFinal(sqlite3_context
*pCtx
){
10647 sqlite3_int64 nZip
;
10650 p
= (ZipfileCtx
*)sqlite3_aggregate_context(pCtx
, sizeof(ZipfileCtx
));
10653 memset(&eocd
, 0, sizeof(eocd
));
10654 eocd
.nEntry
= (u16
)p
->nEntry
;
10655 eocd
.nEntryTotal
= (u16
)p
->nEntry
;
10656 eocd
.nSize
= p
->cds
.n
;
10657 eocd
.iOffset
= p
->body
.n
;
10659 nZip
= p
->body
.n
+ p
->cds
.n
+ ZIPFILE_EOCD_FIXED_SZ
;
10660 aZip
= (u8
*)sqlite3_malloc64(nZip
);
10662 sqlite3_result_error_nomem(pCtx
);
10664 memcpy(aZip
, p
->body
.a
, p
->body
.n
);
10665 memcpy(&aZip
[p
->body
.n
], p
->cds
.a
, p
->cds
.n
);
10666 zipfileSerializeEOCD(&eocd
, &aZip
[p
->body
.n
+ p
->cds
.n
]);
10667 sqlite3_result_blob(pCtx
, aZip
, (int)nZip
, zipfileFree
);
10671 sqlite3_free(p
->body
.a
);
10672 sqlite3_free(p
->cds
.a
);
10677 ** Register the "zipfile" virtual table.
10679 static int zipfileRegister(sqlite3
*db
){
10680 static sqlite3_module zipfileModule
= {
10682 zipfileConnect
, /* xCreate */
10683 zipfileConnect
, /* xConnect */
10684 zipfileBestIndex
, /* xBestIndex */
10685 zipfileDisconnect
, /* xDisconnect */
10686 zipfileDisconnect
, /* xDestroy */
10687 zipfileOpen
, /* xOpen - open a cursor */
10688 zipfileClose
, /* xClose - close a cursor */
10689 zipfileFilter
, /* xFilter - configure scan constraints */
10690 zipfileNext
, /* xNext - advance a cursor */
10691 zipfileEof
, /* xEof - check for end of scan */
10692 zipfileColumn
, /* xColumn - read data */
10693 0, /* xRowid - read data */
10694 zipfileUpdate
, /* xUpdate */
10695 zipfileBegin
, /* xBegin */
10697 zipfileCommit
, /* xCommit */
10698 zipfileRollback
, /* xRollback */
10699 zipfileFindFunction
, /* xFindMethod */
10701 0, /* xSavepoint */
10704 0 /* xShadowName */
10707 int rc
= sqlite3_create_module(db
, "zipfile" , &zipfileModule
, 0);
10708 if( rc
==SQLITE_OK
) rc
= sqlite3_overload_function(db
, "zipfile_cds", -1);
10709 if( rc
==SQLITE_OK
){
10710 rc
= sqlite3_create_function(db
, "zipfile", -1, SQLITE_UTF8
, 0, 0,
10711 zipfileStep
, zipfileFinal
10714 assert( sizeof(i64
)==8 );
10715 assert( sizeof(u32
)==4 );
10716 assert( sizeof(u16
)==2 );
10717 assert( sizeof(u8
)==1 );
10720 #else /* SQLITE_OMIT_VIRTUALTABLE */
10721 # define zipfileRegister(x) SQLITE_OK
10727 int sqlite3_zipfile_init(
10730 const sqlite3_api_routines
*pApi
10732 SQLITE_EXTENSION_INIT2(pApi
);
10733 (void)pzErrMsg
; /* Unused parameter */
10734 return zipfileRegister(db
);
10737 /************************* End ../ext/misc/zipfile.c ********************/
10738 /************************* Begin ../ext/misc/sqlar.c ******************/
10742 ** The author disclaims copyright to this source code. In place of
10743 ** a legal notice, here is a blessing:
10745 ** May you do good and not evil.
10746 ** May you find forgiveness for yourself and forgive others.
10747 ** May you share freely, never taking more than you give.
10749 ******************************************************************************
10751 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
10752 ** for working with sqlar archives and used by the shell tool's built-in
10755 /* #include "sqlite3ext.h" */
10756 SQLITE_EXTENSION_INIT1
10758 #include <assert.h>
10761 ** Implementation of the "sqlar_compress(X)" SQL function.
10763 ** If the type of X is SQLITE_BLOB, and compressing that blob using
10764 ** zlib utility function compress() yields a smaller blob, return the
10765 ** compressed blob. Otherwise, return a copy of X.
10767 ** SQLar uses the "zlib format" for compressed content. The zlib format
10768 ** contains a two-byte identification header and a four-byte checksum at
10769 ** the end. This is different from ZIP which uses the raw deflate format.
10771 ** Future enhancements to SQLar might add support for new compression formats.
10772 ** If so, those new formats will be identified by alternative headers in the
10773 ** compressed data.
10775 static void sqlarCompressFunc(
10776 sqlite3_context
*context
,
10778 sqlite3_value
**argv
10781 if( sqlite3_value_type(argv
[0])==SQLITE_BLOB
){
10782 const Bytef
*pData
= sqlite3_value_blob(argv
[0]);
10783 uLong nData
= sqlite3_value_bytes(argv
[0]);
10784 uLongf nOut
= compressBound(nData
);
10787 pOut
= (Bytef
*)sqlite3_malloc(nOut
);
10789 sqlite3_result_error_nomem(context
);
10792 if( Z_OK
!=compress(pOut
, &nOut
, pData
, nData
) ){
10793 sqlite3_result_error(context
, "error in compress()", -1);
10794 }else if( nOut
<nData
){
10795 sqlite3_result_blob(context
, pOut
, nOut
, SQLITE_TRANSIENT
);
10797 sqlite3_result_value(context
, argv
[0]);
10799 sqlite3_free(pOut
);
10802 sqlite3_result_value(context
, argv
[0]);
10807 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
10809 ** Parameter SZ is interpreted as an integer. If it is less than or
10810 ** equal to zero, then this function returns a copy of X. Or, if
10811 ** SZ is equal to the size of X when interpreted as a blob, also
10812 ** return a copy of X. Otherwise, decompress blob X using zlib
10813 ** utility function uncompress() and return the results (another
10816 static void sqlarUncompressFunc(
10817 sqlite3_context
*context
,
10819 sqlite3_value
**argv
10825 sz
= sqlite3_value_int(argv
[1]);
10827 if( sz
<=0 || sz
==(nData
= sqlite3_value_bytes(argv
[0])) ){
10828 sqlite3_result_value(context
, argv
[0]);
10830 const Bytef
*pData
= sqlite3_value_blob(argv
[0]);
10831 Bytef
*pOut
= sqlite3_malloc(sz
);
10833 sqlite3_result_error_nomem(context
);
10834 }else if( Z_OK
!=uncompress(pOut
, &sz
, pData
, nData
) ){
10835 sqlite3_result_error(context
, "error in uncompress()", -1);
10837 sqlite3_result_blob(context
, pOut
, sz
, SQLITE_TRANSIENT
);
10839 sqlite3_free(pOut
);
10846 int sqlite3_sqlar_init(
10849 const sqlite3_api_routines
*pApi
10851 int rc
= SQLITE_OK
;
10852 SQLITE_EXTENSION_INIT2(pApi
);
10853 (void)pzErrMsg
; /* Unused parameter */
10854 rc
= sqlite3_create_function(db
, "sqlar_compress", 1,
10855 SQLITE_UTF8
|SQLITE_INNOCUOUS
, 0,
10856 sqlarCompressFunc
, 0, 0);
10857 if( rc
==SQLITE_OK
){
10858 rc
= sqlite3_create_function(db
, "sqlar_uncompress", 2,
10859 SQLITE_UTF8
|SQLITE_INNOCUOUS
, 0,
10860 sqlarUncompressFunc
, 0, 0);
10865 /************************* End ../ext/misc/sqlar.c ********************/
10867 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
10871 ** The author disclaims copyright to this source code. In place of
10872 ** a legal notice, here is a blessing:
10874 ** May you do good and not evil.
10875 ** May you find forgiveness for yourself and forgive others.
10876 ** May you share freely, never taking more than you give.
10878 *************************************************************************
10880 #if !defined(SQLITEEXPERT_H)
10881 #define SQLITEEXPERT_H 1
10882 /* #include "sqlite3.h" */
10884 typedef struct sqlite3expert sqlite3expert
;
10887 ** Create a new sqlite3expert object.
10889 ** If successful, a pointer to the new object is returned and (*pzErr) set
10890 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
10891 ** an English-language error message. In this case it is the responsibility
10892 ** of the caller to eventually free the error message buffer using
10895 sqlite3expert
*sqlite3_expert_new(sqlite3
*db
, char **pzErr
);
10898 ** Configure an sqlite3expert object.
10900 ** EXPERT_CONFIG_SAMPLE:
10901 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
10902 ** each candidate index. This involves scanning and sorting the entire
10903 ** contents of each user database table once for each candidate index
10904 ** associated with the table. For large databases, this can be
10905 ** prohibitively slow. This option allows the sqlite3expert object to
10906 ** be configured so that sqlite_stat1 data is instead generated based on a
10907 ** subset of each table, or so that no sqlite_stat1 data is used at all.
10909 ** A single integer argument is passed to this option. If the value is less
10910 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
10911 ** the analysis - indexes are recommended based on the database schema only.
10912 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
10913 ** generated for each candidate index (this is the default). Finally, if the
10914 ** value falls between 0 and 100, then it represents the percentage of user
10915 ** table rows that should be considered when generating sqlite_stat1 data.
10919 ** // Do not generate any sqlite_stat1 data
10920 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
10922 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
10923 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
10925 int sqlite3_expert_config(sqlite3expert
*p
, int op
, ...);
10927 #define EXPERT_CONFIG_SAMPLE 1 /* int */
10930 ** Specify zero or more SQL statements to be included in the analysis.
10932 ** Buffer zSql must contain zero or more complete SQL statements. This
10933 ** function parses all statements contained in the buffer and adds them
10934 ** to the internal list of statements to analyze. If successful, SQLITE_OK
10935 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
10936 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
10937 ** may be set to point to an English language error message. In this case
10938 ** the caller is responsible for eventually freeing the error message buffer
10939 ** using sqlite3_free().
10941 ** If an error does occur while processing one of the statements in the
10942 ** buffer passed as the second argument, none of the statements in the
10943 ** buffer are added to the analysis.
10945 ** This function must be called before sqlite3_expert_analyze(). If a call
10946 ** to this function is made on an sqlite3expert object that has already
10947 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
10948 ** immediately and no statements are added to the analysis.
10950 int sqlite3_expert_sql(
10951 sqlite3expert
*p
, /* From a successful sqlite3_expert_new() */
10952 const char *zSql
, /* SQL statement(s) to add */
10953 char **pzErr
/* OUT: Error message (if any) */
10958 ** This function is called after the sqlite3expert object has been configured
10959 ** with all SQL statements using sqlite3_expert_sql() to actually perform
10960 ** the analysis. Once this function has been called, it is not possible to
10961 ** add further SQL statements to the analysis.
10963 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
10964 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
10965 ** point to a buffer containing an English language error message. In this
10966 ** case it is the responsibility of the caller to eventually free the buffer
10967 ** using sqlite3_free().
10969 ** If an error does occur within this function, the sqlite3expert object
10970 ** is no longer useful for any purpose. At that point it is no longer
10971 ** possible to add further SQL statements to the object or to re-attempt
10972 ** the analysis. The sqlite3expert object must still be freed using a call
10973 ** sqlite3_expert_destroy().
10975 int sqlite3_expert_analyze(sqlite3expert
*p
, char **pzErr
);
10978 ** Return the total number of statements loaded using sqlite3_expert_sql().
10979 ** The total number of SQL statements may be different from the total number
10980 ** to calls to sqlite3_expert_sql().
10982 int sqlite3_expert_count(sqlite3expert
*);
10985 ** Return a component of the report.
10987 ** This function is called after sqlite3_expert_analyze() to extract the
10988 ** results of the analysis. Each call to this function returns either a
10989 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
10990 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
10991 ** #define constants defined below.
10993 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
10994 ** information relating to a specific SQL statement. In these cases that
10995 ** SQL statement is identified by the value passed as the second argument.
10996 ** SQL statements are numbered from 0 in the order in which they are parsed.
10997 ** If an out-of-range value (less than zero or equal to or greater than the
10998 ** value returned by sqlite3_expert_count()) is passed as the second argument
10999 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
11001 ** EXPERT_REPORT_SQL:
11002 ** Return the text of SQL statement iStmt.
11004 ** EXPERT_REPORT_INDEXES:
11005 ** Return a buffer containing the CREATE INDEX statements for all recommended
11006 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
11009 ** EXPERT_REPORT_PLAN:
11010 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
11011 ** iStmt after the proposed indexes have been added to the database schema.
11013 ** EXPERT_REPORT_CANDIDATES:
11014 ** Return a pointer to a buffer containing the CREATE INDEX statements
11015 ** for all indexes that were tested (for all SQL statements). The iStmt
11016 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
11018 const char *sqlite3_expert_report(sqlite3expert
*, int iStmt
, int eReport
);
11021 ** Values for the third argument passed to sqlite3_expert_report().
11023 #define EXPERT_REPORT_SQL 1
11024 #define EXPERT_REPORT_INDEXES 2
11025 #define EXPERT_REPORT_PLAN 3
11026 #define EXPERT_REPORT_CANDIDATES 4
11029 ** Free an (sqlite3expert*) handle and all associated resources. There
11030 ** should be one call to this function for each successful call to
11031 ** sqlite3-expert_new().
11033 void sqlite3_expert_destroy(sqlite3expert
*);
11035 #endif /* !defined(SQLITEEXPERT_H) */
11037 /************************* End ../ext/expert/sqlite3expert.h ********************/
11038 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
11042 ** The author disclaims copyright to this source code. In place of
11043 ** a legal notice, here is a blessing:
11045 ** May you do good and not evil.
11046 ** May you find forgiveness for yourself and forgive others.
11047 ** May you share freely, never taking more than you give.
11049 *************************************************************************
11051 /* #include "sqlite3expert.h" */
11052 #include <assert.h>
11053 #include <string.h>
11056 #if !defined(SQLITE_AMALGAMATION)
11057 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
11058 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
11060 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
11061 # define ALWAYS(X) (1)
11062 # define NEVER(X) (0)
11063 #elif !defined(NDEBUG)
11064 # define ALWAYS(X) ((X)?1:(assert(0),0))
11065 # define NEVER(X) ((X)?(assert(0),1):0)
11067 # define ALWAYS(X) (X)
11068 # define NEVER(X) (X)
11070 #endif /* !defined(SQLITE_AMALGAMATION) */
11073 #ifndef SQLITE_OMIT_VIRTUALTABLE
11075 /* typedef sqlite3_int64 i64; */
11076 /* typedef sqlite3_uint64 u64; */
11078 typedef struct IdxColumn IdxColumn
;
11079 typedef struct IdxConstraint IdxConstraint
;
11080 typedef struct IdxScan IdxScan
;
11081 typedef struct IdxStatement IdxStatement
;
11082 typedef struct IdxTable IdxTable
;
11083 typedef struct IdxWrite IdxWrite
;
11085 #define STRLEN (int)strlen
11088 ** A temp table name that we assume no user database will actually use.
11089 ** If this assumption proves incorrect triggers on the table with the
11090 ** conflicting name will be ignored.
11092 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
11095 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
11096 ** any other type of single-ended range constraint on a column).
11099 ** Used to temporarily link IdxConstraint objects into lists while
11100 ** creating candidate indexes.
11102 struct IdxConstraint
{
11103 char *zColl
; /* Collation sequence */
11104 int bRange
; /* True for range, false for eq */
11105 int iCol
; /* Constrained table column */
11106 int bFlag
; /* Used by idxFindCompatible() */
11107 int bDesc
; /* True if ORDER BY <expr> DESC */
11108 IdxConstraint
*pNext
; /* Next constraint in pEq or pRange list */
11109 IdxConstraint
*pLink
; /* See above */
11113 ** A single scan of a single table.
11116 IdxTable
*pTab
; /* Associated table object */
11117 int iDb
; /* Database containing table zTable */
11118 i64 covering
; /* Mask of columns required for cov. index */
11119 IdxConstraint
*pOrder
; /* ORDER BY columns */
11120 IdxConstraint
*pEq
; /* List of == constraints */
11121 IdxConstraint
*pRange
; /* List of < constraints */
11122 IdxScan
*pNextScan
; /* Next IdxScan object for same analysis */
11126 ** Information regarding a single database table. Extracted from
11127 ** "PRAGMA table_info" by function idxGetTableInfo().
11136 char *zName
; /* Table name */
11138 IdxTable
*pNext
; /* Next table in linked list of all tables */
11142 ** An object of the following type is created for each unique table/write-op
11143 ** seen. The objects are stored in a singly-linked list beginning at
11144 ** sqlite3expert.pWrite.
11148 int eOp
; /* SQLITE_UPDATE, DELETE or INSERT */
11153 ** Each statement being analyzed is represented by an instance of this
11156 struct IdxStatement
{
11157 int iId
; /* Statement number */
11158 char *zSql
; /* SQL statement */
11159 char *zIdx
; /* Indexes */
11160 char *zEQP
; /* Plan */
11161 IdxStatement
*pNext
;
11166 ** A hash table for storing strings. With space for a payload string
11167 ** with each entry. Methods are:
11174 #define IDX_HASH_SIZE 1023
11175 typedef struct IdxHashEntry IdxHashEntry
;
11176 typedef struct IdxHash IdxHash
;
11177 struct IdxHashEntry
{
11178 char *zKey
; /* nul-terminated key */
11179 char *zVal
; /* nul-terminated value string */
11180 char *zVal2
; /* nul-terminated value string 2 */
11181 IdxHashEntry
*pHashNext
; /* Next entry in same hash bucket */
11182 IdxHashEntry
*pNext
; /* Next entry in hash */
11185 IdxHashEntry
*pFirst
;
11186 IdxHashEntry
*aHash
[IDX_HASH_SIZE
];
11190 ** sqlite3expert object.
11192 struct sqlite3expert
{
11193 int iSample
; /* Percentage of tables to sample for stat1 */
11194 sqlite3
*db
; /* User database */
11195 sqlite3
*dbm
; /* In-memory db for this analysis */
11196 sqlite3
*dbv
; /* Vtab schema for this analysis */
11197 IdxTable
*pTable
; /* List of all IdxTable objects */
11198 IdxScan
*pScan
; /* List of scan objects */
11199 IdxWrite
*pWrite
; /* List of write objects */
11200 IdxStatement
*pStatement
; /* List of IdxStatement objects */
11201 int bRun
; /* True once analysis has run */
11203 int rc
; /* Error code from whereinfo hook */
11204 IdxHash hIdx
; /* Hash containing all candidate indexes */
11205 char *zCandidates
; /* For EXPERT_REPORT_CANDIDATES */
11210 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
11211 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
11213 static void *idxMalloc(int *pRc
, int nByte
){
11215 assert( *pRc
==SQLITE_OK
);
11217 pRet
= sqlite3_malloc(nByte
);
11219 memset(pRet
, 0, nByte
);
11221 *pRc
= SQLITE_NOMEM
;
11227 ** Initialize an IdxHash hash table.
11229 static void idxHashInit(IdxHash
*pHash
){
11230 memset(pHash
, 0, sizeof(IdxHash
));
11234 ** Reset an IdxHash hash table.
11236 static void idxHashClear(IdxHash
*pHash
){
11238 for(i
=0; i
<IDX_HASH_SIZE
; i
++){
11239 IdxHashEntry
*pEntry
;
11240 IdxHashEntry
*pNext
;
11241 for(pEntry
=pHash
->aHash
[i
]; pEntry
; pEntry
=pNext
){
11242 pNext
= pEntry
->pHashNext
;
11243 sqlite3_free(pEntry
->zVal2
);
11244 sqlite3_free(pEntry
);
11247 memset(pHash
, 0, sizeof(IdxHash
));
11251 ** Return the index of the hash bucket that the string specified by the
11252 ** arguments to this function belongs.
11254 static int idxHashString(const char *z
, int n
){
11255 unsigned int ret
= 0;
11257 for(i
=0; i
<n
; i
++){
11258 ret
+= (ret
<<3) + (unsigned char)(z
[i
]);
11260 return (int)(ret
% IDX_HASH_SIZE
);
11264 ** If zKey is already present in the hash table, return non-zero and do
11265 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
11266 ** the hash table passed as the second argument.
11268 static int idxHashAdd(
11274 int nKey
= STRLEN(zKey
);
11275 int iHash
= idxHashString(zKey
, nKey
);
11276 int nVal
= (zVal
? STRLEN(zVal
) : 0);
11277 IdxHashEntry
*pEntry
;
11278 assert( iHash
>=0 );
11279 for(pEntry
=pHash
->aHash
[iHash
]; pEntry
; pEntry
=pEntry
->pHashNext
){
11280 if( STRLEN(pEntry
->zKey
)==nKey
&& 0==memcmp(pEntry
->zKey
, zKey
, nKey
) ){
11284 pEntry
= idxMalloc(pRc
, sizeof(IdxHashEntry
) + nKey
+1 + nVal
+1);
11286 pEntry
->zKey
= (char*)&pEntry
[1];
11287 memcpy(pEntry
->zKey
, zKey
, nKey
);
11289 pEntry
->zVal
= &pEntry
->zKey
[nKey
+1];
11290 memcpy(pEntry
->zVal
, zVal
, nVal
);
11292 pEntry
->pHashNext
= pHash
->aHash
[iHash
];
11293 pHash
->aHash
[iHash
] = pEntry
;
11295 pEntry
->pNext
= pHash
->pFirst
;
11296 pHash
->pFirst
= pEntry
;
11302 ** If zKey/nKey is present in the hash table, return a pointer to the
11303 ** hash-entry object.
11305 static IdxHashEntry
*idxHashFind(IdxHash
*pHash
, const char *zKey
, int nKey
){
11307 IdxHashEntry
*pEntry
;
11308 if( nKey
<0 ) nKey
= STRLEN(zKey
);
11309 iHash
= idxHashString(zKey
, nKey
);
11310 assert( iHash
>=0 );
11311 for(pEntry
=pHash
->aHash
[iHash
]; pEntry
; pEntry
=pEntry
->pHashNext
){
11312 if( STRLEN(pEntry
->zKey
)==nKey
&& 0==memcmp(pEntry
->zKey
, zKey
, nKey
) ){
11320 ** If the hash table contains an entry with a key equal to the string
11321 ** passed as the final two arguments to this function, return a pointer
11322 ** to the payload string. Otherwise, if zKey/nKey is not present in the
11323 ** hash table, return NULL.
11325 static const char *idxHashSearch(IdxHash
*pHash
, const char *zKey
, int nKey
){
11326 IdxHashEntry
*pEntry
= idxHashFind(pHash
, zKey
, nKey
);
11327 if( pEntry
) return pEntry
->zVal
;
11332 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
11333 ** variable to point to a copy of nul-terminated string zColl.
11335 static IdxConstraint
*idxNewConstraint(int *pRc
, const char *zColl
){
11336 IdxConstraint
*pNew
;
11337 int nColl
= STRLEN(zColl
);
11339 assert( *pRc
==SQLITE_OK
);
11340 pNew
= (IdxConstraint
*)idxMalloc(pRc
, sizeof(IdxConstraint
) * nColl
+ 1);
11342 pNew
->zColl
= (char*)&pNew
[1];
11343 memcpy(pNew
->zColl
, zColl
, nColl
+1);
11349 ** An error associated with database handle db has just occurred. Pass
11350 ** the error message to callback function xOut.
11352 static void idxDatabaseError(
11353 sqlite3
*db
, /* Database handle */
11354 char **pzErrmsg
/* Write error here */
11356 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
11360 ** Prepare an SQL statement.
11362 static int idxPrepareStmt(
11363 sqlite3
*db
, /* Database handle to compile against */
11364 sqlite3_stmt
**ppStmt
, /* OUT: Compiled SQL statement */
11365 char **pzErrmsg
, /* OUT: sqlite3_malloc()ed error message */
11366 const char *zSql
/* SQL statement to compile */
11368 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
11369 if( rc
!=SQLITE_OK
){
11371 idxDatabaseError(db
, pzErrmsg
);
11377 ** Prepare an SQL statement using the results of a printf() formatting.
11379 static int idxPrintfPrepareStmt(
11380 sqlite3
*db
, /* Database handle to compile against */
11381 sqlite3_stmt
**ppStmt
, /* OUT: Compiled SQL statement */
11382 char **pzErrmsg
, /* OUT: sqlite3_malloc()ed error message */
11383 const char *zFmt
, /* printf() format of SQL statement */
11384 ... /* Trailing printf() arguments */
11389 va_start(ap
, zFmt
);
11390 zSql
= sqlite3_vmprintf(zFmt
, ap
);
11394 rc
= idxPrepareStmt(db
, ppStmt
, pzErrmsg
, zSql
);
11395 sqlite3_free(zSql
);
11402 /*************************************************************************
11403 ** Beginning of virtual table implementation.
11405 typedef struct ExpertVtab ExpertVtab
;
11406 struct ExpertVtab
{
11409 sqlite3expert
*pExpert
;
11412 typedef struct ExpertCsr ExpertCsr
;
11414 sqlite3_vtab_cursor base
;
11415 sqlite3_stmt
*pData
;
11418 static char *expertDequote(const char *zIn
){
11419 int n
= STRLEN(zIn
);
11420 char *zRet
= sqlite3_malloc(n
);
11422 assert( zIn
[0]=='\'' );
11423 assert( zIn
[n
-1]=='\'' );
11428 for(iIn
=1; iIn
<(n
-1); iIn
++){
11429 if( zIn
[iIn
]=='\'' ){
11430 assert( zIn
[iIn
+1]=='\'' );
11433 zRet
[iOut
++] = zIn
[iIn
];
11442 ** This function is the implementation of both the xConnect and xCreate
11443 ** methods of the r-tree virtual table.
11445 ** argv[0] -> module name
11446 ** argv[1] -> database name
11447 ** argv[2] -> table name
11448 ** argv[...] -> column names...
11450 static int expertConnect(
11453 int argc
, const char *const*argv
,
11454 sqlite3_vtab
**ppVtab
,
11457 sqlite3expert
*pExpert
= (sqlite3expert
*)pAux
;
11462 *pzErr
= sqlite3_mprintf("internal error!");
11465 char *zCreateTable
= expertDequote(argv
[3]);
11466 if( zCreateTable
){
11467 rc
= sqlite3_declare_vtab(db
, zCreateTable
);
11468 if( rc
==SQLITE_OK
){
11469 p
= idxMalloc(&rc
, sizeof(ExpertVtab
));
11471 if( rc
==SQLITE_OK
){
11472 p
->pExpert
= pExpert
;
11473 p
->pTab
= pExpert
->pTable
;
11474 assert( sqlite3_stricmp(p
->pTab
->zName
, argv
[2])==0 );
11476 sqlite3_free(zCreateTable
);
11482 *ppVtab
= (sqlite3_vtab
*)p
;
11486 static int expertDisconnect(sqlite3_vtab
*pVtab
){
11487 ExpertVtab
*p
= (ExpertVtab
*)pVtab
;
11492 static int expertBestIndex(sqlite3_vtab
*pVtab
, sqlite3_index_info
*pIdxInfo
){
11493 ExpertVtab
*p
= (ExpertVtab
*)pVtab
;
11494 int rc
= SQLITE_OK
;
11498 SQLITE_INDEX_CONSTRAINT_EQ
| SQLITE_INDEX_CONSTRAINT_GT
|
11499 SQLITE_INDEX_CONSTRAINT_LT
| SQLITE_INDEX_CONSTRAINT_GE
|
11500 SQLITE_INDEX_CONSTRAINT_LE
;
11502 pScan
= idxMalloc(&rc
, sizeof(IdxScan
));
11506 /* Link the new scan object into the list */
11507 pScan
->pTab
= p
->pTab
;
11508 pScan
->pNextScan
= p
->pExpert
->pScan
;
11509 p
->pExpert
->pScan
= pScan
;
11511 /* Add the constraints to the IdxScan object */
11512 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++){
11513 struct sqlite3_index_constraint
*pCons
= &pIdxInfo
->aConstraint
[i
];
11515 && pCons
->iColumn
>=0
11516 && p
->pTab
->aCol
[pCons
->iColumn
].iPk
==0
11517 && (pCons
->op
& opmask
)
11519 IdxConstraint
*pNew
;
11520 const char *zColl
= sqlite3_vtab_collation(pIdxInfo
, i
);
11521 pNew
= idxNewConstraint(&rc
, zColl
);
11523 pNew
->iCol
= pCons
->iColumn
;
11524 if( pCons
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
11525 pNew
->pNext
= pScan
->pEq
;
11529 pNew
->pNext
= pScan
->pRange
;
11530 pScan
->pRange
= pNew
;
11534 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= n
;
11538 /* Add the ORDER BY to the IdxScan object */
11539 for(i
=pIdxInfo
->nOrderBy
-1; i
>=0; i
--){
11540 int iCol
= pIdxInfo
->aOrderBy
[i
].iColumn
;
11542 IdxConstraint
*pNew
= idxNewConstraint(&rc
, p
->pTab
->aCol
[iCol
].zColl
);
11545 pNew
->bDesc
= pIdxInfo
->aOrderBy
[i
].desc
;
11546 pNew
->pNext
= pScan
->pOrder
;
11547 pNew
->pLink
= pScan
->pOrder
;
11548 pScan
->pOrder
= pNew
;
11555 pIdxInfo
->estimatedCost
= 1000000.0 / (n
+1);
11559 static int expertUpdate(
11560 sqlite3_vtab
*pVtab
,
11562 sqlite3_value
**azData
,
11563 sqlite_int64
*pRowid
11573 ** Virtual table module xOpen method.
11575 static int expertOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
11576 int rc
= SQLITE_OK
;
11579 pCsr
= idxMalloc(&rc
, sizeof(ExpertCsr
));
11580 *ppCursor
= (sqlite3_vtab_cursor
*)pCsr
;
11585 ** Virtual table module xClose method.
11587 static int expertClose(sqlite3_vtab_cursor
*cur
){
11588 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
11589 sqlite3_finalize(pCsr
->pData
);
11590 sqlite3_free(pCsr
);
11595 ** Virtual table module xEof method.
11597 ** Return non-zero if the cursor does not currently point to a valid
11598 ** record (i.e if the scan has finished), or zero otherwise.
11600 static int expertEof(sqlite3_vtab_cursor
*cur
){
11601 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
11602 return pCsr
->pData
==0;
11606 ** Virtual table module xNext method.
11608 static int expertNext(sqlite3_vtab_cursor
*cur
){
11609 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
11610 int rc
= SQLITE_OK
;
11612 assert( pCsr
->pData
);
11613 rc
= sqlite3_step(pCsr
->pData
);
11614 if( rc
!=SQLITE_ROW
){
11615 rc
= sqlite3_finalize(pCsr
->pData
);
11625 ** Virtual table module xRowid method.
11627 static int expertRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
11634 ** Virtual table module xColumn method.
11636 static int expertColumn(sqlite3_vtab_cursor
*cur
, sqlite3_context
*ctx
, int i
){
11637 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
11638 sqlite3_value
*pVal
;
11639 pVal
= sqlite3_column_value(pCsr
->pData
, i
);
11641 sqlite3_result_value(ctx
, pVal
);
11647 ** Virtual table module xFilter method.
11649 static int expertFilter(
11650 sqlite3_vtab_cursor
*cur
,
11651 int idxNum
, const char *idxStr
,
11652 int argc
, sqlite3_value
**argv
11654 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
11655 ExpertVtab
*pVtab
= (ExpertVtab
*)(cur
->pVtab
);
11656 sqlite3expert
*pExpert
= pVtab
->pExpert
;
11663 rc
= sqlite3_finalize(pCsr
->pData
);
11665 if( rc
==SQLITE_OK
){
11666 rc
= idxPrintfPrepareStmt(pExpert
->db
, &pCsr
->pData
, &pVtab
->base
.zErrMsg
,
11667 "SELECT * FROM main.%Q WHERE sample()", pVtab
->pTab
->zName
11671 if( rc
==SQLITE_OK
){
11672 rc
= expertNext(cur
);
11677 static int idxRegisterVtab(sqlite3expert
*p
){
11678 static sqlite3_module expertModule
= {
11680 expertConnect
, /* xCreate - create a table */
11681 expertConnect
, /* xConnect - connect to an existing table */
11682 expertBestIndex
, /* xBestIndex - Determine search strategy */
11683 expertDisconnect
, /* xDisconnect - Disconnect from a table */
11684 expertDisconnect
, /* xDestroy - Drop a table */
11685 expertOpen
, /* xOpen - open a cursor */
11686 expertClose
, /* xClose - close a cursor */
11687 expertFilter
, /* xFilter - configure scan constraints */
11688 expertNext
, /* xNext - advance a cursor */
11689 expertEof
, /* xEof */
11690 expertColumn
, /* xColumn - read data */
11691 expertRowid
, /* xRowid - read data */
11692 expertUpdate
, /* xUpdate - write data */
11693 0, /* xBegin - begin transaction */
11694 0, /* xSync - sync transaction */
11695 0, /* xCommit - commit transaction */
11696 0, /* xRollback - rollback transaction */
11697 0, /* xFindFunction - function overloading */
11698 0, /* xRename - rename the table */
11699 0, /* xSavepoint */
11701 0, /* xRollbackTo */
11702 0, /* xShadowName */
11705 return sqlite3_create_module(p
->dbv
, "expert", &expertModule
, (void*)p
);
11708 ** End of virtual table implementation.
11709 *************************************************************************/
11711 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
11712 ** is called, set it to the return value of sqlite3_finalize() before
11713 ** returning. Otherwise, discard the sqlite3_finalize() return value.
11715 static void idxFinalize(int *pRc
, sqlite3_stmt
*pStmt
){
11716 int rc
= sqlite3_finalize(pStmt
);
11717 if( *pRc
==SQLITE_OK
) *pRc
= rc
;
11721 ** Attempt to allocate an IdxTable structure corresponding to table zTab
11722 ** in the main database of connection db. If successful, set (*ppOut) to
11723 ** point to the new object and return SQLITE_OK. Otherwise, return an
11724 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
11725 ** set to point to an error string.
11727 ** It is the responsibility of the caller to eventually free either the
11728 ** IdxTable object or error message using sqlite3_free().
11730 static int idxGetTableInfo(
11731 sqlite3
*db
, /* Database connection to read details from */
11732 const char *zTab
, /* Table name */
11733 IdxTable
**ppOut
, /* OUT: New object (if successful) */
11734 char **pzErrmsg
/* OUT: Error message (if not) */
11736 sqlite3_stmt
*p1
= 0;
11740 IdxTable
*pNew
= 0;
11746 if( zTab
==0 ) return SQLITE_ERROR
;
11747 nTab
= STRLEN(zTab
);
11748 nByte
= sizeof(IdxTable
) + nTab
+ 1;
11749 rc
= idxPrintfPrepareStmt(db
, &p1
, pzErrmsg
, "PRAGMA table_xinfo=%Q", zTab
);
11750 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(p1
) ){
11751 const char *zCol
= (const char*)sqlite3_column_text(p1
, 1);
11752 const char *zColSeq
= 0;
11757 nByte
+= 1 + STRLEN(zCol
);
11758 rc
= sqlite3_table_column_metadata(
11759 db
, "main", zTab
, zCol
, 0, &zColSeq
, 0, 0, 0
11761 if( zColSeq
==0 ) zColSeq
= "binary";
11762 nByte
+= 1 + STRLEN(zColSeq
);
11764 nPk
+= (sqlite3_column_int(p1
, 5)>0);
11766 rc2
= sqlite3_reset(p1
);
11767 if( rc
==SQLITE_OK
) rc
= rc2
;
11769 nByte
+= sizeof(IdxColumn
) * nCol
;
11770 if( rc
==SQLITE_OK
){
11771 pNew
= idxMalloc(&rc
, nByte
);
11773 if( rc
==SQLITE_OK
){
11774 pNew
->aCol
= (IdxColumn
*)&pNew
[1];
11776 pCsr
= (char*)&pNew
->aCol
[nCol
];
11780 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(p1
) ){
11781 const char *zCol
= (const char*)sqlite3_column_text(p1
, 1);
11782 const char *zColSeq
= 0;
11784 if( zCol
==0 ) continue;
11785 nCopy
= STRLEN(zCol
) + 1;
11786 pNew
->aCol
[nCol
].zName
= pCsr
;
11787 pNew
->aCol
[nCol
].iPk
= (sqlite3_column_int(p1
, 5)==1 && nPk
==1);
11788 memcpy(pCsr
, zCol
, nCopy
);
11791 rc
= sqlite3_table_column_metadata(
11792 db
, "main", zTab
, zCol
, 0, &zColSeq
, 0, 0, 0
11794 if( rc
==SQLITE_OK
){
11795 if( zColSeq
==0 ) zColSeq
= "binary";
11796 nCopy
= STRLEN(zColSeq
) + 1;
11797 pNew
->aCol
[nCol
].zColl
= pCsr
;
11798 memcpy(pCsr
, zColSeq
, nCopy
);
11804 idxFinalize(&rc
, p1
);
11806 if( rc
!=SQLITE_OK
){
11807 sqlite3_free(pNew
);
11809 }else if( ALWAYS(pNew
!=0) ){
11810 pNew
->zName
= pCsr
;
11811 if( ALWAYS(pNew
->zName
!=0) ) memcpy(pNew
->zName
, zTab
, nTab
+1);
11819 ** This function is a no-op if *pRc is set to anything other than
11820 ** SQLITE_OK when it is called.
11822 ** If *pRc is initially set to SQLITE_OK, then the text specified by
11823 ** the printf() style arguments is appended to zIn and the result returned
11824 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
11825 ** zIn before returning.
11827 static char *idxAppendText(int *pRc
, char *zIn
, const char *zFmt
, ...){
11831 int nIn
= zIn
? STRLEN(zIn
) : 0;
11833 va_start(ap
, zFmt
);
11834 if( *pRc
==SQLITE_OK
){
11835 zAppend
= sqlite3_vmprintf(zFmt
, ap
);
11837 nAppend
= STRLEN(zAppend
);
11838 zRet
= (char*)sqlite3_malloc(nIn
+ nAppend
+ 1);
11840 if( zAppend
&& zRet
){
11841 if( nIn
) memcpy(zRet
, zIn
, nIn
);
11842 memcpy(&zRet
[nIn
], zAppend
, nAppend
+1);
11844 sqlite3_free(zRet
);
11846 *pRc
= SQLITE_NOMEM
;
11848 sqlite3_free(zAppend
);
11856 ** Return true if zId must be quoted in order to use it as an SQL
11857 ** identifier, or false otherwise.
11859 static int idxIdentifierRequiresQuotes(const char *zId
){
11861 int nId
= STRLEN(zId
);
11863 if( sqlite3_keyword_check(zId
, nId
) ) return 1;
11865 for(i
=0; zId
[i
]; i
++){
11867 && !(zId
[i
]>='0' && zId
[i
]<='9')
11868 && !(zId
[i
]>='a' && zId
[i
]<='z')
11869 && !(zId
[i
]>='A' && zId
[i
]<='Z')
11878 ** This function appends an index column definition suitable for constraint
11879 ** pCons to the string passed as zIn and returns the result.
11881 static char *idxAppendColDefn(
11882 int *pRc
, /* IN/OUT: Error code */
11883 char *zIn
, /* Column defn accumulated so far */
11884 IdxTable
*pTab
, /* Table index will be created on */
11885 IdxConstraint
*pCons
11888 IdxColumn
*p
= &pTab
->aCol
[pCons
->iCol
];
11889 if( zRet
) zRet
= idxAppendText(pRc
, zRet
, ", ");
11891 if( idxIdentifierRequiresQuotes(p
->zName
) ){
11892 zRet
= idxAppendText(pRc
, zRet
, "%Q", p
->zName
);
11894 zRet
= idxAppendText(pRc
, zRet
, "%s", p
->zName
);
11897 if( sqlite3_stricmp(p
->zColl
, pCons
->zColl
) ){
11898 if( idxIdentifierRequiresQuotes(pCons
->zColl
) ){
11899 zRet
= idxAppendText(pRc
, zRet
, " COLLATE %Q", pCons
->zColl
);
11901 zRet
= idxAppendText(pRc
, zRet
, " COLLATE %s", pCons
->zColl
);
11905 if( pCons
->bDesc
){
11906 zRet
= idxAppendText(pRc
, zRet
, " DESC");
11912 ** Search database dbm for an index compatible with the one idxCreateFromCons()
11913 ** would create from arguments pScan, pEq and pTail. If no error occurs and
11914 ** such an index is found, return non-zero. Or, if no such index is found,
11917 ** If an error occurs, set *pRc to an SQLite error code and return zero.
11919 static int idxFindCompatible(
11920 int *pRc
, /* OUT: Error code */
11921 sqlite3
* dbm
, /* Database to search */
11922 IdxScan
*pScan
, /* Scan for table to search for index on */
11923 IdxConstraint
*pEq
, /* List of == constraints */
11924 IdxConstraint
*pTail
/* List of range constraints */
11926 const char *zTbl
= pScan
->pTab
->zName
;
11927 sqlite3_stmt
*pIdxList
= 0;
11928 IdxConstraint
*pIter
;
11929 int nEq
= 0; /* Number of elements in pEq */
11932 /* Count the elements in list pEq */
11933 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
) nEq
++;
11935 rc
= idxPrintfPrepareStmt(dbm
, &pIdxList
, 0, "PRAGMA index_list=%Q", zTbl
);
11936 while( rc
==SQLITE_OK
&& sqlite3_step(pIdxList
)==SQLITE_ROW
){
11938 IdxConstraint
*pT
= pTail
;
11939 sqlite3_stmt
*pInfo
= 0;
11940 const char *zIdx
= (const char*)sqlite3_column_text(pIdxList
, 1);
11941 if( zIdx
==0 ) continue;
11943 /* Zero the IdxConstraint.bFlag values in the pEq list */
11944 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
) pIter
->bFlag
= 0;
11946 rc
= idxPrintfPrepareStmt(dbm
, &pInfo
, 0, "PRAGMA index_xInfo=%Q", zIdx
);
11947 while( rc
==SQLITE_OK
&& sqlite3_step(pInfo
)==SQLITE_ROW
){
11948 int iIdx
= sqlite3_column_int(pInfo
, 0);
11949 int iCol
= sqlite3_column_int(pInfo
, 1);
11950 const char *zColl
= (const char*)sqlite3_column_text(pInfo
, 4);
11953 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
){
11954 if( pIter
->bFlag
) continue;
11955 if( pIter
->iCol
!=iCol
) continue;
11956 if( sqlite3_stricmp(pIter
->zColl
, zColl
) ) continue;
11966 if( pT
->iCol
!=iCol
|| sqlite3_stricmp(pT
->zColl
, zColl
) ){
11974 idxFinalize(&rc
, pInfo
);
11976 if( rc
==SQLITE_OK
&& bMatch
){
11977 sqlite3_finalize(pIdxList
);
11981 idxFinalize(&rc
, pIdxList
);
11987 /* Callback for sqlite3_exec() with query with leading count(*) column.
11988 * The first argument is expected to be an int*, referent to be incremented
11989 * if that leading column is not exactly '0'.
11991 static int countNonzeros(void* pCount
, int nc
,
11992 char* azResults
[], char* azColumns
[]){
11993 (void)azColumns
; /* Suppress unused parameter warning */
11994 if( nc
>0 && (azResults
[0][0]!='0' || azResults
[0][1]!=0) ){
11995 *((int *)pCount
) += 1;
12000 static int idxCreateFromCons(
12003 IdxConstraint
*pEq
,
12004 IdxConstraint
*pTail
12006 sqlite3
*dbm
= p
->dbm
;
12007 int rc
= SQLITE_OK
;
12008 if( (pEq
|| pTail
) && 0==idxFindCompatible(&rc
, dbm
, pScan
, pEq
, pTail
) ){
12009 IdxTable
*pTab
= pScan
->pTab
;
12012 IdxConstraint
*pCons
;
12013 unsigned int h
= 0;
12016 for(pCons
=pEq
; pCons
; pCons
=pCons
->pLink
){
12017 zCols
= idxAppendColDefn(&rc
, zCols
, pTab
, pCons
);
12019 for(pCons
=pTail
; pCons
; pCons
=pCons
->pLink
){
12020 zCols
= idxAppendColDefn(&rc
, zCols
, pTab
, pCons
);
12023 if( rc
==SQLITE_OK
){
12024 /* Hash the list of columns to come up with a name for the index */
12025 const char *zTable
= pScan
->pTab
->zName
;
12026 int quoteTable
= idxIdentifierRequiresQuotes(zTable
);
12027 char *zName
= 0; /* Index name */
12028 int collisions
= 0;
12032 for(i
=0; zCols
[i
]; i
++){
12033 h
+= ((h
<<3) + zCols
[i
]);
12035 sqlite3_free(zName
);
12036 zName
= sqlite3_mprintf("%s_idx_%08x", zTable
, h
);
12037 if( zName
==0 ) break;
12038 /* Is is unique among table, view and index names? */
12039 zFmt
= "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
12040 " AND type in ('index','table','view')";
12041 zFind
= sqlite3_mprintf(zFmt
, zName
);
12043 rc
= sqlite3_exec(dbm
, zFind
, countNonzeros
, &i
, 0);
12044 assert(rc
==SQLITE_OK
);
12045 sqlite3_free(zFind
);
12051 }while( collisions
<50 && zName
!=0 );
12053 /* This return means "Gave up trying to find a unique index name." */
12054 rc
= SQLITE_BUSY_TIMEOUT
;
12055 }else if( zName
==0 ){
12059 zFmt
= "CREATE INDEX \"%w\" ON \"%w\"(%s)";
12061 zFmt
= "CREATE INDEX %s ON %s(%s)";
12063 zIdx
= sqlite3_mprintf(zFmt
, zName
, zTable
, zCols
);
12067 rc
= sqlite3_exec(dbm
, zIdx
, 0, 0, p
->pzErrmsg
);
12068 if( rc
!=SQLITE_OK
){
12069 rc
= SQLITE_BUSY_TIMEOUT
;
12071 idxHashAdd(&rc
, &p
->hIdx
, zName
, zIdx
);
12074 sqlite3_free(zName
);
12075 sqlite3_free(zIdx
);
12079 sqlite3_free(zCols
);
12085 ** Return true if list pList (linked by IdxConstraint.pLink) contains
12086 ** a constraint compatible with *p. Otherwise return false.
12088 static int idxFindConstraint(IdxConstraint
*pList
, IdxConstraint
*p
){
12089 IdxConstraint
*pCmp
;
12090 for(pCmp
=pList
; pCmp
; pCmp
=pCmp
->pLink
){
12091 if( p
->iCol
==pCmp
->iCol
) return 1;
12096 static int idxCreateFromWhere(
12098 IdxScan
*pScan
, /* Create indexes for this scan */
12099 IdxConstraint
*pTail
/* range/ORDER BY constraints for inclusion */
12101 IdxConstraint
*p1
= 0;
12102 IdxConstraint
*pCon
;
12105 /* Gather up all the == constraints. */
12106 for(pCon
=pScan
->pEq
; pCon
; pCon
=pCon
->pNext
){
12107 if( !idxFindConstraint(p1
, pCon
) && !idxFindConstraint(pTail
, pCon
) ){
12113 /* Create an index using the == constraints collected above. And the
12114 ** range constraint/ORDER BY terms passed in by the caller, if any. */
12115 rc
= idxCreateFromCons(p
, pScan
, p1
, pTail
);
12117 /* If no range/ORDER BY passed by the caller, create a version of the
12118 ** index for each range constraint. */
12120 for(pCon
=pScan
->pRange
; rc
==SQLITE_OK
&& pCon
; pCon
=pCon
->pNext
){
12121 assert( pCon
->pLink
==0 );
12122 if( !idxFindConstraint(p1
, pCon
) && !idxFindConstraint(pTail
, pCon
) ){
12123 rc
= idxCreateFromCons(p
, pScan
, p1
, pCon
);
12132 ** Create candidate indexes in database [dbm] based on the data in
12133 ** linked-list pScan.
12135 static int idxCreateCandidates(sqlite3expert
*p
){
12136 int rc
= SQLITE_OK
;
12139 for(pIter
=p
->pScan
; pIter
&& rc
==SQLITE_OK
; pIter
=pIter
->pNextScan
){
12140 rc
= idxCreateFromWhere(p
, pIter
, 0);
12141 if( rc
==SQLITE_OK
&& pIter
->pOrder
){
12142 rc
= idxCreateFromWhere(p
, pIter
, pIter
->pOrder
);
12150 ** Free all elements of the linked list starting at pConstraint.
12152 static void idxConstraintFree(IdxConstraint
*pConstraint
){
12153 IdxConstraint
*pNext
;
12156 for(p
=pConstraint
; p
; p
=pNext
){
12163 ** Free all elements of the linked list starting from pScan up until pLast
12164 ** (pLast is not freed).
12166 static void idxScanFree(IdxScan
*pScan
, IdxScan
*pLast
){
12169 for(p
=pScan
; p
!=pLast
; p
=pNext
){
12170 pNext
= p
->pNextScan
;
12171 idxConstraintFree(p
->pOrder
);
12172 idxConstraintFree(p
->pEq
);
12173 idxConstraintFree(p
->pRange
);
12179 ** Free all elements of the linked list starting from pStatement up
12180 ** until pLast (pLast is not freed).
12182 static void idxStatementFree(IdxStatement
*pStatement
, IdxStatement
*pLast
){
12184 IdxStatement
*pNext
;
12185 for(p
=pStatement
; p
!=pLast
; p
=pNext
){
12187 sqlite3_free(p
->zEQP
);
12188 sqlite3_free(p
->zIdx
);
12194 ** Free the linked list of IdxTable objects starting at pTab.
12196 static void idxTableFree(IdxTable
*pTab
){
12199 for(pIter
=pTab
; pIter
; pIter
=pNext
){
12200 pNext
= pIter
->pNext
;
12201 sqlite3_free(pIter
);
12206 ** Free the linked list of IdxWrite objects starting at pTab.
12208 static void idxWriteFree(IdxWrite
*pTab
){
12211 for(pIter
=pTab
; pIter
; pIter
=pNext
){
12212 pNext
= pIter
->pNext
;
12213 sqlite3_free(pIter
);
12220 ** This function is called after candidate indexes have been created. It
12221 ** runs all the queries to see which indexes they prefer, and populates
12222 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
12224 static int idxFindIndexes(
12226 char **pzErr
/* OUT: Error message (sqlite3_malloc) */
12228 IdxStatement
*pStmt
;
12229 sqlite3
*dbm
= p
->dbm
;
12230 int rc
= SQLITE_OK
;
12233 idxHashInit(&hIdx
);
12235 for(pStmt
=p
->pStatement
; rc
==SQLITE_OK
&& pStmt
; pStmt
=pStmt
->pNext
){
12236 IdxHashEntry
*pEntry
;
12237 sqlite3_stmt
*pExplain
= 0;
12238 idxHashClear(&hIdx
);
12239 rc
= idxPrintfPrepareStmt(dbm
, &pExplain
, pzErr
,
12240 "EXPLAIN QUERY PLAN %s", pStmt
->zSql
12242 while( rc
==SQLITE_OK
&& sqlite3_step(pExplain
)==SQLITE_ROW
){
12243 /* int iId = sqlite3_column_int(pExplain, 0); */
12244 /* int iParent = sqlite3_column_int(pExplain, 1); */
12245 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
12246 const char *zDetail
= (const char*)sqlite3_column_text(pExplain
, 3);
12250 if( !zDetail
) continue;
12251 nDetail
= STRLEN(zDetail
);
12253 for(i
=0; i
<nDetail
; i
++){
12254 const char *zIdx
= 0;
12255 if( i
+13<nDetail
&& memcmp(&zDetail
[i
], " USING INDEX ", 13)==0 ){
12256 zIdx
= &zDetail
[i
+13];
12257 }else if( i
+22<nDetail
12258 && memcmp(&zDetail
[i
], " USING COVERING INDEX ", 22)==0
12260 zIdx
= &zDetail
[i
+22];
12265 while( zIdx
[nIdx
]!='\0' && (zIdx
[nIdx
]!=' ' || zIdx
[nIdx
+1]!='(') ){
12268 zSql
= idxHashSearch(&p
->hIdx
, zIdx
, nIdx
);
12270 idxHashAdd(&rc
, &hIdx
, zSql
, 0);
12271 if( rc
) goto find_indexes_out
;
12277 if( zDetail
[0]!='-' ){
12278 pStmt
->zEQP
= idxAppendText(&rc
, pStmt
->zEQP
, "%s\n", zDetail
);
12282 for(pEntry
=hIdx
.pFirst
; pEntry
; pEntry
=pEntry
->pNext
){
12283 pStmt
->zIdx
= idxAppendText(&rc
, pStmt
->zIdx
, "%s;\n", pEntry
->zKey
);
12286 idxFinalize(&rc
, pExplain
);
12290 idxHashClear(&hIdx
);
12294 static int idxAuthCallback(
12300 const char *zTrigger
12302 int rc
= SQLITE_OK
;
12305 if( eOp
==SQLITE_INSERT
|| eOp
==SQLITE_UPDATE
|| eOp
==SQLITE_DELETE
){
12306 if( sqlite3_stricmp(zDb
, "main")==0 ){
12307 sqlite3expert
*p
= (sqlite3expert
*)pCtx
;
12309 for(pTab
=p
->pTable
; pTab
; pTab
=pTab
->pNext
){
12310 if( 0==sqlite3_stricmp(z3
, pTab
->zName
) ) break;
12314 for(pWrite
=p
->pWrite
; pWrite
; pWrite
=pWrite
->pNext
){
12315 if( pWrite
->pTab
==pTab
&& pWrite
->eOp
==eOp
) break;
12318 pWrite
= idxMalloc(&rc
, sizeof(IdxWrite
));
12319 if( rc
==SQLITE_OK
){
12320 pWrite
->pTab
= pTab
;
12322 pWrite
->pNext
= p
->pWrite
;
12323 p
->pWrite
= pWrite
;
12332 static int idxProcessOneTrigger(
12337 static const char *zInt
= UNIQUE_TABLE_NAME
;
12338 static const char *zDrop
= "DROP TABLE " UNIQUE_TABLE_NAME
;
12339 IdxTable
*pTab
= pWrite
->pTab
;
12340 const char *zTab
= pTab
->zName
;
12342 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
12343 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
12345 sqlite3_stmt
*pSelect
= 0;
12346 int rc
= SQLITE_OK
;
12349 /* Create the table and its triggers in the temp schema */
12350 rc
= idxPrintfPrepareStmt(p
->db
, &pSelect
, pzErr
, zSql
, zTab
, zTab
);
12351 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSelect
) ){
12352 const char *zCreate
= (const char*)sqlite3_column_text(pSelect
, 0);
12353 if( zCreate
==0 ) continue;
12354 rc
= sqlite3_exec(p
->dbv
, zCreate
, 0, 0, pzErr
);
12356 idxFinalize(&rc
, pSelect
);
12358 /* Rename the table in the temp schema to zInt */
12359 if( rc
==SQLITE_OK
){
12360 char *z
= sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab
, zInt
);
12364 rc
= sqlite3_exec(p
->dbv
, z
, 0, 0, pzErr
);
12369 switch( pWrite
->eOp
){
12370 case SQLITE_INSERT
: {
12372 zWrite
= idxAppendText(&rc
, zWrite
, "INSERT INTO %Q VALUES(", zInt
);
12373 for(i
=0; i
<pTab
->nCol
; i
++){
12374 zWrite
= idxAppendText(&rc
, zWrite
, "%s?", i
==0 ? "" : ", ");
12376 zWrite
= idxAppendText(&rc
, zWrite
, ")");
12379 case SQLITE_UPDATE
: {
12381 zWrite
= idxAppendText(&rc
, zWrite
, "UPDATE %Q SET ", zInt
);
12382 for(i
=0; i
<pTab
->nCol
; i
++){
12383 zWrite
= idxAppendText(&rc
, zWrite
, "%s%Q=?", i
==0 ? "" : ", ",
12384 pTab
->aCol
[i
].zName
12390 assert( pWrite
->eOp
==SQLITE_DELETE
);
12391 if( rc
==SQLITE_OK
){
12392 zWrite
= sqlite3_mprintf("DELETE FROM %Q", zInt
);
12393 if( zWrite
==0 ) rc
= SQLITE_NOMEM
;
12398 if( rc
==SQLITE_OK
){
12399 sqlite3_stmt
*pX
= 0;
12400 rc
= sqlite3_prepare_v2(p
->dbv
, zWrite
, -1, &pX
, 0);
12401 idxFinalize(&rc
, pX
);
12402 if( rc
!=SQLITE_OK
){
12403 idxDatabaseError(p
->dbv
, pzErr
);
12406 sqlite3_free(zWrite
);
12408 if( rc
==SQLITE_OK
){
12409 rc
= sqlite3_exec(p
->dbv
, zDrop
, 0, 0, pzErr
);
12415 static int idxProcessTriggers(sqlite3expert
*p
, char **pzErr
){
12416 int rc
= SQLITE_OK
;
12417 IdxWrite
*pEnd
= 0;
12418 IdxWrite
*pFirst
= p
->pWrite
;
12420 while( rc
==SQLITE_OK
&& pFirst
!=pEnd
){
12422 for(pIter
=pFirst
; rc
==SQLITE_OK
&& pIter
!=pEnd
; pIter
=pIter
->pNext
){
12423 rc
= idxProcessOneTrigger(p
, pIter
, pzErr
);
12426 pFirst
= p
->pWrite
;
12433 static int idxCreateVtabSchema(sqlite3expert
*p
, char **pzErrmsg
){
12434 int rc
= idxRegisterVtab(p
);
12435 sqlite3_stmt
*pSchema
= 0;
12437 /* For each table in the main db schema:
12439 ** 1) Add an entry to the p->pTable list, and
12440 ** 2) Create the equivalent virtual table in dbv.
12442 rc
= idxPrepareStmt(p
->db
, &pSchema
, pzErrmsg
,
12443 "SELECT type, name, sql, 1 FROM sqlite_schema "
12444 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
12446 "SELECT type, name, sql, 2 FROM sqlite_schema "
12447 "WHERE type = 'trigger'"
12448 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
12451 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSchema
) ){
12452 const char *zType
= (const char*)sqlite3_column_text(pSchema
, 0);
12453 const char *zName
= (const char*)sqlite3_column_text(pSchema
, 1);
12454 const char *zSql
= (const char*)sqlite3_column_text(pSchema
, 2);
12456 if( zType
==0 || zName
==0 ) continue;
12457 if( zType
[0]=='v' || zType
[1]=='r' ){
12458 if( zSql
) rc
= sqlite3_exec(p
->dbv
, zSql
, 0, 0, pzErrmsg
);
12461 rc
= idxGetTableInfo(p
->db
, zName
, &pTab
, pzErrmsg
);
12462 if( rc
==SQLITE_OK
){
12466 pTab
->pNext
= p
->pTable
;
12469 /* The statement the vtab will pass to sqlite3_declare_vtab() */
12470 zInner
= idxAppendText(&rc
, 0, "CREATE TABLE x(");
12471 for(i
=0; i
<pTab
->nCol
; i
++){
12472 zInner
= idxAppendText(&rc
, zInner
, "%s%Q COLLATE %s",
12473 (i
==0 ? "" : ", "), pTab
->aCol
[i
].zName
, pTab
->aCol
[i
].zColl
12476 zInner
= idxAppendText(&rc
, zInner
, ")");
12478 /* The CVT statement to create the vtab */
12479 zOuter
= idxAppendText(&rc
, 0,
12480 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName
, zInner
12482 if( rc
==SQLITE_OK
){
12483 rc
= sqlite3_exec(p
->dbv
, zOuter
, 0, 0, pzErrmsg
);
12485 sqlite3_free(zInner
);
12486 sqlite3_free(zOuter
);
12490 idxFinalize(&rc
, pSchema
);
12494 struct IdxSampleCtx
{
12496 double target
; /* Target nRet/nRow value */
12497 double nRow
; /* Number of rows seen */
12498 double nRet
; /* Number of rows returned */
12501 static void idxSampleFunc(
12502 sqlite3_context
*pCtx
,
12504 sqlite3_value
**argv
12506 struct IdxSampleCtx
*p
= (struct IdxSampleCtx
*)sqlite3_user_data(pCtx
);
12511 if( p
->nRow
==0.0 ){
12514 bRet
= (p
->nRet
/ p
->nRow
) <= p
->target
;
12516 unsigned short rnd
;
12517 sqlite3_randomness(2, (void*)&rnd
);
12518 bRet
= ((int)rnd
% 100) <= p
->iTarget
;
12522 sqlite3_result_int(pCtx
, bRet
);
12524 p
->nRet
+= (double)bRet
;
12529 struct IdxRemSlot
{
12530 int eType
; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
12531 i64 iVal
; /* SQLITE_INTEGER value */
12532 double rVal
; /* SQLITE_FLOAT value */
12533 int nByte
; /* Bytes of space allocated at z */
12534 int n
; /* Size of buffer z */
12535 char *z
; /* SQLITE_TEXT/BLOB value */
12540 ** Implementation of scalar function rem().
12542 static void idxRemFunc(
12543 sqlite3_context
*pCtx
,
12545 sqlite3_value
**argv
12547 struct IdxRemCtx
*p
= (struct IdxRemCtx
*)sqlite3_user_data(pCtx
);
12548 struct IdxRemSlot
*pSlot
;
12552 iSlot
= sqlite3_value_int(argv
[0]);
12553 assert( iSlot
<=p
->nSlot
);
12554 pSlot
= &p
->aSlot
[iSlot
];
12556 switch( pSlot
->eType
){
12561 case SQLITE_INTEGER
:
12562 sqlite3_result_int64(pCtx
, pSlot
->iVal
);
12566 sqlite3_result_double(pCtx
, pSlot
->rVal
);
12570 sqlite3_result_blob(pCtx
, pSlot
->z
, pSlot
->n
, SQLITE_TRANSIENT
);
12574 sqlite3_result_text(pCtx
, pSlot
->z
, pSlot
->n
, SQLITE_TRANSIENT
);
12578 pSlot
->eType
= sqlite3_value_type(argv
[1]);
12579 switch( pSlot
->eType
){
12584 case SQLITE_INTEGER
:
12585 pSlot
->iVal
= sqlite3_value_int64(argv
[1]);
12589 pSlot
->rVal
= sqlite3_value_double(argv
[1]);
12593 case SQLITE_TEXT
: {
12594 int nByte
= sqlite3_value_bytes(argv
[1]);
12595 const void *pData
= 0;
12596 if( nByte
>pSlot
->nByte
){
12597 char *zNew
= (char*)sqlite3_realloc(pSlot
->z
, nByte
*2);
12599 sqlite3_result_error_nomem(pCtx
);
12602 pSlot
->nByte
= nByte
*2;
12606 if( pSlot
->eType
==SQLITE_BLOB
){
12607 pData
= sqlite3_value_blob(argv
[1]);
12608 if( pData
) memcpy(pSlot
->z
, pData
, nByte
);
12610 pData
= sqlite3_value_text(argv
[1]);
12611 memcpy(pSlot
->z
, pData
, nByte
);
12618 static int idxLargestIndex(sqlite3
*db
, int *pnMax
, char **pzErr
){
12619 int rc
= SQLITE_OK
;
12621 "SELECT max(i.seqno) FROM "
12622 " sqlite_schema AS s, "
12623 " pragma_index_list(s.name) AS l, "
12624 " pragma_index_info(l.name) AS i "
12625 "WHERE s.type = 'table'";
12626 sqlite3_stmt
*pMax
= 0;
12629 rc
= idxPrepareStmt(db
, &pMax
, pzErr
, zMax
);
12630 if( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pMax
) ){
12631 *pnMax
= sqlite3_column_int(pMax
, 0) + 1;
12633 idxFinalize(&rc
, pMax
);
12638 static int idxPopulateOneStat1(
12640 sqlite3_stmt
*pIndexXInfo
,
12641 sqlite3_stmt
*pWriteStat
,
12651 sqlite3_stmt
*pQuery
= 0;
12653 int rc
= SQLITE_OK
;
12655 assert( p
->iSample
>0 );
12657 /* Formulate the query text */
12658 sqlite3_bind_text(pIndexXInfo
, 1, zIdx
, -1, SQLITE_STATIC
);
12659 while( SQLITE_OK
==rc
&& SQLITE_ROW
==sqlite3_step(pIndexXInfo
) ){
12660 const char *zComma
= zCols
==0 ? "" : ", ";
12661 const char *zName
= (const char*)sqlite3_column_text(pIndexXInfo
, 0);
12662 const char *zColl
= (const char*)sqlite3_column_text(pIndexXInfo
, 1);
12663 zCols
= idxAppendText(&rc
, zCols
,
12664 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma
, zName
, nCol
, zName
, zColl
12666 zOrder
= idxAppendText(&rc
, zOrder
, "%s%d", zComma
, ++nCol
);
12668 sqlite3_reset(pIndexXInfo
);
12669 if( rc
==SQLITE_OK
){
12670 if( p
->iSample
==100 ){
12671 zQuery
= sqlite3_mprintf(
12672 "SELECT %s FROM %Q x ORDER BY %s", zCols
, zTab
, zOrder
12675 zQuery
= sqlite3_mprintf(
12676 "SELECT %s FROM temp."UNIQUE_TABLE_NAME
" x ORDER BY %s", zCols
, zOrder
12680 sqlite3_free(zCols
);
12681 sqlite3_free(zOrder
);
12683 /* Formulate the query text */
12684 if( rc
==SQLITE_OK
){
12685 sqlite3
*dbrem
= (p
->iSample
==100 ? p
->db
: p
->dbv
);
12686 rc
= idxPrepareStmt(dbrem
, &pQuery
, pzErr
, zQuery
);
12688 sqlite3_free(zQuery
);
12690 if( rc
==SQLITE_OK
){
12691 aStat
= (int*)idxMalloc(&rc
, sizeof(int)*(nCol
+1));
12693 if( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pQuery
) ){
12694 IdxHashEntry
*pEntry
;
12696 for(i
=0; i
<=nCol
; i
++) aStat
[i
] = 1;
12697 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pQuery
) ){
12699 for(i
=0; i
<nCol
; i
++){
12700 if( sqlite3_column_int(pQuery
, i
)==0 ) break;
12702 for(/*no-op*/; i
<nCol
; i
++){
12707 if( rc
==SQLITE_OK
){
12709 zStat
= sqlite3_mprintf("%d", s0
);
12710 if( zStat
==0 ) rc
= SQLITE_NOMEM
;
12711 for(i
=1; rc
==SQLITE_OK
&& i
<=nCol
; i
++){
12712 zStat
= idxAppendText(&rc
, zStat
, " %d", (s0
+aStat
[i
]/2) / aStat
[i
]);
12716 if( rc
==SQLITE_OK
){
12717 sqlite3_bind_text(pWriteStat
, 1, zTab
, -1, SQLITE_STATIC
);
12718 sqlite3_bind_text(pWriteStat
, 2, zIdx
, -1, SQLITE_STATIC
);
12719 sqlite3_bind_text(pWriteStat
, 3, zStat
, -1, SQLITE_STATIC
);
12720 sqlite3_step(pWriteStat
);
12721 rc
= sqlite3_reset(pWriteStat
);
12724 pEntry
= idxHashFind(&p
->hIdx
, zIdx
, STRLEN(zIdx
));
12726 assert( pEntry
->zVal2
==0 );
12727 pEntry
->zVal2
= zStat
;
12729 sqlite3_free(zStat
);
12732 sqlite3_free(aStat
);
12733 idxFinalize(&rc
, pQuery
);
12738 static int idxBuildSampleTable(sqlite3expert
*p
, const char *zTab
){
12742 rc
= sqlite3_exec(p
->dbv
,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME
,0,0,0);
12743 if( rc
!=SQLITE_OK
) return rc
;
12745 zSql
= sqlite3_mprintf(
12746 "CREATE TABLE temp." UNIQUE_TABLE_NAME
" AS SELECT * FROM %Q", zTab
12748 if( zSql
==0 ) return SQLITE_NOMEM
;
12749 rc
= sqlite3_exec(p
->dbv
, zSql
, 0, 0, 0);
12750 sqlite3_free(zSql
);
12756 ** This function is called as part of sqlite3_expert_analyze(). Candidate
12757 ** indexes have already been created in database sqlite3expert.dbm, this
12758 ** function populates sqlite_stat1 table in the same database.
12760 ** The stat1 data is generated by querying the
12762 static int idxPopulateStat1(sqlite3expert
*p
, char **pzErr
){
12763 int rc
= SQLITE_OK
;
12765 struct IdxRemCtx
*pCtx
= 0;
12766 struct IdxSampleCtx samplectx
;
12768 i64 iPrev
= -100000;
12769 sqlite3_stmt
*pAllIndex
= 0;
12770 sqlite3_stmt
*pIndexXInfo
= 0;
12771 sqlite3_stmt
*pWrite
= 0;
12773 const char *zAllIndex
=
12774 "SELECT s.rowid, s.name, l.name FROM "
12775 " sqlite_schema AS s, "
12776 " pragma_index_list(s.name) AS l "
12777 "WHERE s.type = 'table'";
12778 const char *zIndexXInfo
=
12779 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
12780 const char *zWrite
= "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
12782 /* If iSample==0, no sqlite_stat1 data is required. */
12783 if( p
->iSample
==0 ) return SQLITE_OK
;
12785 rc
= idxLargestIndex(p
->dbm
, &nMax
, pzErr
);
12786 if( nMax
<=0 || rc
!=SQLITE_OK
) return rc
;
12788 rc
= sqlite3_exec(p
->dbm
, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
12790 if( rc
==SQLITE_OK
){
12791 int nByte
= sizeof(struct IdxRemCtx
) + (sizeof(struct IdxRemSlot
) * nMax
);
12792 pCtx
= (struct IdxRemCtx
*)idxMalloc(&rc
, nByte
);
12795 if( rc
==SQLITE_OK
){
12796 sqlite3
*dbrem
= (p
->iSample
==100 ? p
->db
: p
->dbv
);
12797 rc
= sqlite3_create_function(
12798 dbrem
, "rem", 2, SQLITE_UTF8
, (void*)pCtx
, idxRemFunc
, 0, 0
12801 if( rc
==SQLITE_OK
){
12802 rc
= sqlite3_create_function(
12803 p
->db
, "sample", 0, SQLITE_UTF8
, (void*)&samplectx
, idxSampleFunc
, 0, 0
12807 if( rc
==SQLITE_OK
){
12808 pCtx
->nSlot
= nMax
+1;
12809 rc
= idxPrepareStmt(p
->dbm
, &pAllIndex
, pzErr
, zAllIndex
);
12811 if( rc
==SQLITE_OK
){
12812 rc
= idxPrepareStmt(p
->dbm
, &pIndexXInfo
, pzErr
, zIndexXInfo
);
12814 if( rc
==SQLITE_OK
){
12815 rc
= idxPrepareStmt(p
->dbm
, &pWrite
, pzErr
, zWrite
);
12818 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pAllIndex
) ){
12819 i64 iRowid
= sqlite3_column_int64(pAllIndex
, 0);
12820 const char *zTab
= (const char*)sqlite3_column_text(pAllIndex
, 1);
12821 const char *zIdx
= (const char*)sqlite3_column_text(pAllIndex
, 2);
12822 if( zTab
==0 || zIdx
==0 ) continue;
12823 if( p
->iSample
<100 && iPrev
!=iRowid
){
12824 samplectx
.target
= (double)p
->iSample
/ 100.0;
12825 samplectx
.iTarget
= p
->iSample
;
12826 samplectx
.nRow
= 0.0;
12827 samplectx
.nRet
= 0.0;
12828 rc
= idxBuildSampleTable(p
, zTab
);
12829 if( rc
!=SQLITE_OK
) break;
12831 rc
= idxPopulateOneStat1(p
, pIndexXInfo
, pWrite
, zTab
, zIdx
, pzErr
);
12834 if( rc
==SQLITE_OK
&& p
->iSample
<100 ){
12835 rc
= sqlite3_exec(p
->dbv
,
12836 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME
, 0,0,0
12840 idxFinalize(&rc
, pAllIndex
);
12841 idxFinalize(&rc
, pIndexXInfo
);
12842 idxFinalize(&rc
, pWrite
);
12845 for(i
=0; i
<pCtx
->nSlot
; i
++){
12846 sqlite3_free(pCtx
->aSlot
[i
].z
);
12848 sqlite3_free(pCtx
);
12851 if( rc
==SQLITE_OK
){
12852 rc
= sqlite3_exec(p
->dbm
, "ANALYZE sqlite_schema", 0, 0, 0);
12855 sqlite3_exec(p
->db
, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME
,0,0,0);
12860 ** Allocate a new sqlite3expert object.
12862 sqlite3expert
*sqlite3_expert_new(sqlite3
*db
, char **pzErrmsg
){
12863 int rc
= SQLITE_OK
;
12864 sqlite3expert
*pNew
;
12866 pNew
= (sqlite3expert
*)idxMalloc(&rc
, sizeof(sqlite3expert
));
12868 /* Open two in-memory databases to work with. The "vtab database" (dbv)
12869 ** will contain a virtual table corresponding to each real table in
12870 ** the user database schema, and a copy of each view. It is used to
12871 ** collect information regarding the WHERE, ORDER BY and other clauses
12872 ** of the user's query.
12874 if( rc
==SQLITE_OK
){
12876 pNew
->iSample
= 100;
12877 rc
= sqlite3_open(":memory:", &pNew
->dbv
);
12879 if( rc
==SQLITE_OK
){
12880 rc
= sqlite3_open(":memory:", &pNew
->dbm
);
12881 if( rc
==SQLITE_OK
){
12882 sqlite3_db_config(pNew
->dbm
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, (int*)0);
12887 /* Copy the entire schema of database [db] into [dbm]. */
12888 if( rc
==SQLITE_OK
){
12889 sqlite3_stmt
*pSql
= 0;
12890 rc
= idxPrintfPrepareStmt(pNew
->db
, &pSql
, pzErrmsg
,
12891 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
12892 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
12894 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
12895 const char *zSql
= (const char*)sqlite3_column_text(pSql
, 0);
12896 if( zSql
) rc
= sqlite3_exec(pNew
->dbm
, zSql
, 0, 0, pzErrmsg
);
12898 idxFinalize(&rc
, pSql
);
12901 /* Create the vtab schema */
12902 if( rc
==SQLITE_OK
){
12903 rc
= idxCreateVtabSchema(pNew
, pzErrmsg
);
12906 /* Register the auth callback with dbv */
12907 if( rc
==SQLITE_OK
){
12908 sqlite3_set_authorizer(pNew
->dbv
, idxAuthCallback
, (void*)pNew
);
12911 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
12912 ** return the new sqlite3expert handle. */
12913 if( rc
!=SQLITE_OK
){
12914 sqlite3_expert_destroy(pNew
);
12921 ** Configure an sqlite3expert object.
12923 int sqlite3_expert_config(sqlite3expert
*p
, int op
, ...){
12924 int rc
= SQLITE_OK
;
12928 case EXPERT_CONFIG_SAMPLE
: {
12929 int iVal
= va_arg(ap
, int);
12930 if( iVal
<0 ) iVal
= 0;
12931 if( iVal
>100 ) iVal
= 100;
12936 rc
= SQLITE_NOTFOUND
;
12945 ** Add an SQL statement to the analysis.
12947 int sqlite3_expert_sql(
12948 sqlite3expert
*p
, /* From sqlite3_expert_new() */
12949 const char *zSql
, /* SQL statement to add */
12950 char **pzErr
/* OUT: Error message (if any) */
12952 IdxScan
*pScanOrig
= p
->pScan
;
12953 IdxStatement
*pStmtOrig
= p
->pStatement
;
12954 int rc
= SQLITE_OK
;
12955 const char *zStmt
= zSql
;
12957 if( p
->bRun
) return SQLITE_MISUSE
;
12959 while( rc
==SQLITE_OK
&& zStmt
&& zStmt
[0] ){
12960 sqlite3_stmt
*pStmt
= 0;
12961 rc
= sqlite3_prepare_v2(p
->dbv
, zStmt
, -1, &pStmt
, &zStmt
);
12962 if( rc
==SQLITE_OK
){
12964 IdxStatement
*pNew
;
12965 const char *z
= sqlite3_sql(pStmt
);
12967 pNew
= (IdxStatement
*)idxMalloc(&rc
, sizeof(IdxStatement
) + n
+1);
12968 if( rc
==SQLITE_OK
){
12969 pNew
->zSql
= (char*)&pNew
[1];
12970 memcpy(pNew
->zSql
, z
, n
+1);
12971 pNew
->pNext
= p
->pStatement
;
12972 if( p
->pStatement
) pNew
->iId
= p
->pStatement
->iId
+1;
12973 p
->pStatement
= pNew
;
12975 sqlite3_finalize(pStmt
);
12978 idxDatabaseError(p
->dbv
, pzErr
);
12982 if( rc
!=SQLITE_OK
){
12983 idxScanFree(p
->pScan
, pScanOrig
);
12984 idxStatementFree(p
->pStatement
, pStmtOrig
);
12985 p
->pScan
= pScanOrig
;
12986 p
->pStatement
= pStmtOrig
;
12992 int sqlite3_expert_analyze(sqlite3expert
*p
, char **pzErr
){
12994 IdxHashEntry
*pEntry
;
12996 /* Do trigger processing to collect any extra IdxScan structures */
12997 rc
= idxProcessTriggers(p
, pzErr
);
12999 /* Create candidate indexes within the in-memory database file */
13000 if( rc
==SQLITE_OK
){
13001 rc
= idxCreateCandidates(p
);
13002 }else if ( rc
==SQLITE_BUSY_TIMEOUT
){
13004 *pzErr
= sqlite3_mprintf("Cannot find a unique index name to propose.");
13008 /* Generate the stat1 data */
13009 if( rc
==SQLITE_OK
){
13010 rc
= idxPopulateStat1(p
, pzErr
);
13013 /* Formulate the EXPERT_REPORT_CANDIDATES text */
13014 for(pEntry
=p
->hIdx
.pFirst
; pEntry
; pEntry
=pEntry
->pNext
){
13015 p
->zCandidates
= idxAppendText(&rc
, p
->zCandidates
,
13016 "%s;%s%s\n", pEntry
->zVal
,
13017 pEntry
->zVal2
? " -- stat1: " : "", pEntry
->zVal2
13021 /* Figure out which of the candidate indexes are preferred by the query
13022 ** planner and report the results to the user. */
13023 if( rc
==SQLITE_OK
){
13024 rc
= idxFindIndexes(p
, pzErr
);
13027 if( rc
==SQLITE_OK
){
13034 ** Return the total number of statements that have been added to this
13035 ** sqlite3expert using sqlite3_expert_sql().
13037 int sqlite3_expert_count(sqlite3expert
*p
){
13039 if( p
->pStatement
) nRet
= p
->pStatement
->iId
+1;
13044 ** Return a component of the report.
13046 const char *sqlite3_expert_report(sqlite3expert
*p
, int iStmt
, int eReport
){
13047 const char *zRet
= 0;
13048 IdxStatement
*pStmt
;
13050 if( p
->bRun
==0 ) return 0;
13051 for(pStmt
=p
->pStatement
; pStmt
&& pStmt
->iId
!=iStmt
; pStmt
=pStmt
->pNext
);
13053 case EXPERT_REPORT_SQL
:
13054 if( pStmt
) zRet
= pStmt
->zSql
;
13056 case EXPERT_REPORT_INDEXES
:
13057 if( pStmt
) zRet
= pStmt
->zIdx
;
13059 case EXPERT_REPORT_PLAN
:
13060 if( pStmt
) zRet
= pStmt
->zEQP
;
13062 case EXPERT_REPORT_CANDIDATES
:
13063 zRet
= p
->zCandidates
;
13070 ** Free an sqlite3expert object.
13072 void sqlite3_expert_destroy(sqlite3expert
*p
){
13074 sqlite3_close(p
->dbm
);
13075 sqlite3_close(p
->dbv
);
13076 idxScanFree(p
->pScan
, 0);
13077 idxStatementFree(p
->pStatement
, 0);
13078 idxTableFree(p
->pTable
);
13079 idxWriteFree(p
->pWrite
);
13080 idxHashClear(&p
->hIdx
);
13081 sqlite3_free(p
->zCandidates
);
13086 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13088 /************************* End ../ext/expert/sqlite3expert.c ********************/
13090 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
13091 #define SQLITE_SHELL_HAVE_RECOVER 1
13093 #define SQLITE_SHELL_HAVE_RECOVER 0
13095 #if SQLITE_SHELL_HAVE_RECOVER
13096 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
13100 ** The author disclaims copyright to this source code. In place of
13101 ** a legal notice, here is a blessing:
13103 ** May you do good and not evil.
13104 ** May you find forgiveness for yourself and forgive others.
13105 ** May you share freely, never taking more than you give.
13107 *************************************************************************
13109 ** This file contains the public interface to the "recover" extension -
13110 ** an SQLite extension designed to recover data from corrupted database
13117 ** To use the API to recover data from a corrupted database, an
13120 ** 1) Creates an sqlite3_recover handle by calling either
13121 ** sqlite3_recover_init() or sqlite3_recover_init_sql().
13123 ** 2) Configures the new handle using one or more calls to
13124 ** sqlite3_recover_config().
13126 ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
13127 ** the handle until it returns something other than SQLITE_OK. If it
13128 ** returns SQLITE_DONE, then the recovery operation completed without
13129 ** error. If it returns some other non-SQLITE_OK value, then an error
13132 ** 4) Retrieves any error code and English language error message using the
13133 ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
13136 ** 5) Destroys the sqlite3_recover handle and frees all resources
13137 ** using sqlite3_recover_finish().
13139 ** The application may abandon the recovery operation at any point
13140 ** before it is finished by passing the sqlite3_recover handle to
13141 ** sqlite3_recover_finish(). This is not an error, but the final state
13142 ** of the output database, or the results of running the partial script
13143 ** delivered to the SQL callback, are undefined.
13146 #ifndef _SQLITE_RECOVER_H
13147 #define _SQLITE_RECOVER_H
13149 /* #include "sqlite3.h" */
13156 ** An instance of the sqlite3_recover object represents a recovery
13157 ** operation in progress.
13161 ** sqlite3_recover_init()
13162 ** sqlite3_recover_init_sql()
13166 ** sqlite3_recover_finish()
13170 ** sqlite3_recover_config()
13171 ** sqlite3_recover_errcode()
13172 ** sqlite3_recover_errmsg()
13173 ** sqlite3_recover_run()
13174 ** sqlite3_recover_step()
13176 typedef struct sqlite3_recover sqlite3_recover
;
13179 ** These two APIs attempt to create and return a new sqlite3_recover object.
13180 ** In both cases the first two arguments identify the (possibly
13181 ** corrupt) database to recover data from. The first argument is an open
13182 ** database handle and the second the name of a database attached to that
13183 ** handle (i.e. "main", "temp" or the name of an attached database).
13185 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
13186 ** handle, then data is recovered into a new database, identified by
13187 ** string parameter zUri. zUri may be an absolute or relative file path,
13188 ** or may be an SQLite URI. If the identified database file already exists,
13189 ** it is overwritten.
13191 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
13192 ** be returned to the user as a series of SQL statements. Executing these
13193 ** SQL statements results in the same database as would have been created
13194 ** had sqlite3_recover_init() been used. For each SQL statement in the
13195 ** output, the callback function passed as the third argument (xSql) is
13196 ** invoked once. The first parameter is a passed a copy of the fourth argument
13197 ** to this function (pCtx) as its first parameter, and a pointer to a
13198 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
13199 ** the second. If the xSql callback returns any value other than SQLITE_OK,
13200 ** then processing is immediately abandoned and the value returned used as
13201 ** the recover handle error code (see below).
13203 ** If an out-of-memory error occurs, NULL may be returned instead of
13204 ** a valid handle. In all other cases, it is the responsibility of the
13205 ** application to avoid resource leaks by ensuring that
13206 ** sqlite3_recover_finish() is called on all allocated handles.
13208 sqlite3_recover
*sqlite3_recover_init(
13213 sqlite3_recover
*sqlite3_recover_init_sql(
13216 int (*xSql
)(void*, const char*),
13221 ** Configure an sqlite3_recover object that has just been created using
13222 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
13223 ** may only be called before the first call to sqlite3_recover_step()
13224 ** or sqlite3_recover_run() on the object.
13226 ** The second argument passed to this function must be one of the
13227 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
13228 ** depend on the specific SQLITE_RECOVER_* symbol in use.
13230 ** SQLITE_OK is returned if the configuration operation was successful,
13231 ** or an SQLite error code otherwise.
13233 int sqlite3_recover_config(sqlite3_recover
*, int op
, void *pArg
);
13236 ** SQLITE_RECOVER_LOST_AND_FOUND:
13237 ** The pArg argument points to a string buffer containing the name
13238 ** of a "lost-and-found" table in the output database, or NULL. If
13239 ** the argument is non-NULL and the database contains seemingly
13240 ** valid pages that cannot be associated with any table in the
13241 ** recovered part of the schema, data is extracted from these
13242 ** pages to add to the lost-and-found table.
13244 ** SQLITE_RECOVER_FREELIST_CORRUPT:
13245 ** The pArg value must actually be a pointer to a value of type
13246 ** int containing value 0 or 1 cast as a (void*). If this option is set
13247 ** (argument is 1) and a lost-and-found table has been configured using
13248 ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
13249 ** corrupt and an attempt is made to recover records from pages that
13250 ** appear to be linked into the freelist. Otherwise, pages on the freelist
13251 ** are ignored. Setting this option can recover more data from the
13252 ** database, but often ends up "recovering" deleted records. The default
13253 ** value is 0 (clear).
13255 ** SQLITE_RECOVER_ROWIDS:
13256 ** The pArg value must actually be a pointer to a value of type
13257 ** int containing value 0 or 1 cast as a (void*). If this option is set
13258 ** (argument is 1), then an attempt is made to recover rowid values
13259 ** that are not also INTEGER PRIMARY KEY values. If this option is
13260 ** clear, then new rowids are assigned to all recovered rows. The
13261 ** default value is 1 (set).
13263 ** SQLITE_RECOVER_SLOWINDEXES:
13264 ** The pArg value must actually be a pointer to a value of type
13265 ** int containing value 0 or 1 cast as a (void*). If this option is clear
13266 ** (argument is 0), then when creating an output database, the recover
13267 ** module creates and populates non-UNIQUE indexes right at the end of the
13268 ** recovery operation - after all recoverable data has been inserted
13269 ** into the new database. This is faster overall, but means that the
13270 ** final call to sqlite3_recover_step() for a recovery operation may
13271 ** be need to create a large number of indexes, which may be very slow.
13273 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
13274 ** are created in the output database before it is populated with
13275 ** recovered data. This is slower overall, but avoids the slow call
13276 ** to sqlite3_recover_step() at the end of the recovery operation.
13278 ** The default option value is 0.
13280 #define SQLITE_RECOVER_LOST_AND_FOUND 1
13281 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
13282 #define SQLITE_RECOVER_ROWIDS 3
13283 #define SQLITE_RECOVER_SLOWINDEXES 4
13286 ** Perform a unit of work towards the recovery operation. This function
13287 ** must normally be called multiple times to complete database recovery.
13289 ** If no error occurs but the recovery operation is not completed, this
13290 ** function returns SQLITE_OK. If recovery has been completed successfully
13291 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
13292 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
13293 ** considered an error if some or all of the data cannot be recovered
13294 ** due to database corruption.
13296 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
13297 ** all further such calls on the same recover handle are no-ops that return
13298 ** the same non-SQLITE_OK value.
13300 int sqlite3_recover_step(sqlite3_recover
*);
13303 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
13304 ** or an SQLite error code otherwise. Calling this function is the same
13307 ** while( SQLITE_OK==sqlite3_recover_step(p) );
13308 ** return sqlite3_recover_errcode(p);
13310 int sqlite3_recover_run(sqlite3_recover
*);
13313 ** If an error has been encountered during a prior call to
13314 ** sqlite3_recover_step(), then this function attempts to return a
13315 ** pointer to a buffer containing an English language explanation of
13316 ** the error. If no error message is available, or if an out-of memory
13317 ** error occurs while attempting to allocate a buffer in which to format
13318 ** the error message, NULL is returned.
13320 ** The returned buffer remains valid until the sqlite3_recover handle is
13321 ** destroyed using sqlite3_recover_finish().
13323 const char *sqlite3_recover_errmsg(sqlite3_recover
*);
13326 ** If this function is called on an sqlite3_recover handle after
13327 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
13329 int sqlite3_recover_errcode(sqlite3_recover
*);
13332 ** Clean up a recovery object created by a call to sqlite3_recover_init().
13333 ** The results of using a recovery object with any API after it has been
13334 ** passed to this function are undefined.
13336 ** This function returns the same value as sqlite3_recover_errcode().
13338 int sqlite3_recover_finish(sqlite3_recover
*);
13342 } /* end of the 'extern "C"' block */
13345 #endif /* ifndef _SQLITE_RECOVER_H */
13347 /************************* End ../ext/recover/sqlite3recover.h ********************/
13348 # ifndef SQLITE_HAVE_SQLITE3R
13349 /************************* Begin ../ext/recover/dbdata.c ******************/
13353 ** The author disclaims copyright to this source code. In place of
13354 ** a legal notice, here is a blessing:
13356 ** May you do good and not evil.
13357 ** May you find forgiveness for yourself and forgive others.
13358 ** May you share freely, never taking more than you give.
13360 ******************************************************************************
13362 ** This file contains an implementation of two eponymous virtual tables,
13363 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
13364 ** "sqlite_dbpage" eponymous virtual table be available.
13367 ** sqlite_dbdata is used to extract data directly from a database b-tree
13368 ** page and its associated overflow pages, bypassing the b-tree layer.
13369 ** The table schema is equivalent to:
13371 ** CREATE TABLE sqlite_dbdata(
13376 ** schema TEXT HIDDEN
13379 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
13380 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
13383 ** Each page of the database is inspected. If it cannot be interpreted as
13384 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
13385 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
13386 ** table contains one row for each field in the record associated with
13387 ** each cell on the page. For intkey b-trees, the key value is stored in
13390 ** For example, for the database:
13392 ** CREATE TABLE t1(a, b); -- root page is page 2
13393 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
13394 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
13396 ** the sqlite_dbdata table contains, as well as from entries related to
13397 ** page 1, content equivalent to:
13399 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
13401 ** (2, 0, 0, 'v' ),
13402 ** (2, 0, 1, 'five'),
13403 ** (2, 1, -1, 10 ),
13404 ** (2, 1, 0, 'x' ),
13405 ** (2, 1, 1, 'ten' );
13407 ** If database corruption is encountered, this module does not report an
13408 ** error. Instead, it attempts to extract as much data as possible and
13409 ** ignores the corruption.
13412 ** The sqlite_dbptr table has the following schema:
13414 ** CREATE TABLE sqlite_dbptr(
13417 ** schema TEXT HIDDEN
13420 ** It contains one entry for each b-tree pointer between a parent and
13421 ** child page in the database.
13424 #if !defined(SQLITEINT_H)
13425 /* #include "sqlite3.h" */
13427 /* typedef unsigned char u8; */
13428 /* typedef unsigned int u32; */
13431 #include <string.h>
13432 #include <assert.h>
13434 #ifndef SQLITE_OMIT_VIRTUALTABLE
13436 #define DBDATA_PADDING_BYTES 100
13438 typedef struct DbdataTable DbdataTable
;
13439 typedef struct DbdataCursor DbdataCursor
;
13441 /* Cursor object */
13442 struct DbdataCursor
{
13443 sqlite3_vtab_cursor base
; /* Base class. Must be first */
13444 sqlite3_stmt
*pStmt
; /* For fetching database pages */
13446 int iPgno
; /* Current page number */
13447 u8
*aPage
; /* Buffer containing page */
13448 int nPage
; /* Size of aPage[] in bytes */
13449 int nCell
; /* Number of cells on aPage[] */
13450 int iCell
; /* Current cell number */
13451 int bOnePage
; /* True to stop after one page */
13453 sqlite3_int64 iRowid
;
13455 /* Only for the sqlite_dbdata table */
13456 u8
*pRec
; /* Buffer containing current record */
13457 sqlite3_int64 nRec
; /* Size of pRec[] in bytes */
13458 sqlite3_int64 nHdr
; /* Size of header in bytes */
13459 int iField
; /* Current field number */
13462 u32 enc
; /* Text encoding */
13464 sqlite3_int64 iIntkey
; /* Integer key value */
13468 struct DbdataTable
{
13469 sqlite3_vtab base
; /* Base class. Must be first */
13470 sqlite3
*db
; /* The database connection */
13471 sqlite3_stmt
*pStmt
; /* For fetching database pages */
13472 int bPtr
; /* True for sqlite3_dbptr table */
13475 /* Column and schema definitions for sqlite_dbdata */
13476 #define DBDATA_COLUMN_PGNO 0
13477 #define DBDATA_COLUMN_CELL 1
13478 #define DBDATA_COLUMN_FIELD 2
13479 #define DBDATA_COLUMN_VALUE 3
13480 #define DBDATA_COLUMN_SCHEMA 4
13481 #define DBDATA_SCHEMA \
13482 "CREATE TABLE x(" \
13485 " field INTEGER," \
13487 " schema TEXT HIDDEN" \
13490 /* Column and schema definitions for sqlite_dbptr */
13491 #define DBPTR_COLUMN_PGNO 0
13492 #define DBPTR_COLUMN_CHILD 1
13493 #define DBPTR_COLUMN_SCHEMA 2
13494 #define DBPTR_SCHEMA \
13495 "CREATE TABLE x(" \
13497 " child INTEGER," \
13498 " schema TEXT HIDDEN" \
13502 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
13505 static int dbdataConnect(
13508 int argc
, const char *const*argv
,
13509 sqlite3_vtab
**ppVtab
,
13512 DbdataTable
*pTab
= 0;
13513 int rc
= sqlite3_declare_vtab(db
, pAux
? DBPTR_SCHEMA
: DBDATA_SCHEMA
);
13518 sqlite3_vtab_config(db
, SQLITE_VTAB_USES_ALL_SCHEMAS
);
13519 if( rc
==SQLITE_OK
){
13520 pTab
= (DbdataTable
*)sqlite3_malloc64(sizeof(DbdataTable
));
13524 memset(pTab
, 0, sizeof(DbdataTable
));
13526 pTab
->bPtr
= (pAux
!=0);
13530 *ppVtab
= (sqlite3_vtab
*)pTab
;
13535 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
13537 static int dbdataDisconnect(sqlite3_vtab
*pVtab
){
13538 DbdataTable
*pTab
= (DbdataTable
*)pVtab
;
13540 sqlite3_finalize(pTab
->pStmt
);
13541 sqlite3_free(pVtab
);
13547 ** This function interprets two types of constraints:
13552 ** If neither are present, idxNum is set to 0. If schema=? is present,
13553 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
13554 ** in idxNum is set.
13556 ** If both parameters are present, schema is in position 0 and pgno in
13559 static int dbdataBestIndex(sqlite3_vtab
*tab
, sqlite3_index_info
*pIdx
){
13560 DbdataTable
*pTab
= (DbdataTable
*)tab
;
13564 int colSchema
= (pTab
->bPtr
? DBPTR_COLUMN_SCHEMA
: DBDATA_COLUMN_SCHEMA
);
13566 for(i
=0; i
<pIdx
->nConstraint
; i
++){
13567 struct sqlite3_index_constraint
*p
= &pIdx
->aConstraint
[i
];
13568 if( p
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
13569 if( p
->iColumn
==colSchema
){
13570 if( p
->usable
==0 ) return SQLITE_CONSTRAINT
;
13573 if( p
->iColumn
==DBDATA_COLUMN_PGNO
&& p
->usable
){
13580 pIdx
->aConstraintUsage
[iSchema
].argvIndex
= 1;
13581 pIdx
->aConstraintUsage
[iSchema
].omit
= 1;
13584 pIdx
->aConstraintUsage
[iPgno
].argvIndex
= 1 + (iSchema
>=0);
13585 pIdx
->aConstraintUsage
[iPgno
].omit
= 1;
13586 pIdx
->estimatedCost
= 100;
13587 pIdx
->estimatedRows
= 50;
13589 if( pTab
->bPtr
==0 && pIdx
->nOrderBy
&& pIdx
->aOrderBy
[0].desc
==0 ){
13590 int iCol
= pIdx
->aOrderBy
[0].iColumn
;
13591 if( pIdx
->nOrderBy
==1 ){
13592 pIdx
->orderByConsumed
= (iCol
==0 || iCol
==1);
13593 }else if( pIdx
->nOrderBy
==2 && pIdx
->aOrderBy
[1].desc
==0 && iCol
==0 ){
13594 pIdx
->orderByConsumed
= (pIdx
->aOrderBy
[1].iColumn
==1);
13599 pIdx
->estimatedCost
= 100000000;
13600 pIdx
->estimatedRows
= 1000000000;
13602 pIdx
->idxNum
= (iSchema
>=0 ? 0x01 : 0x00) | (iPgno
>=0 ? 0x02 : 0x00);
13607 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
13609 static int dbdataOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
13610 DbdataCursor
*pCsr
;
13612 pCsr
= (DbdataCursor
*)sqlite3_malloc64(sizeof(DbdataCursor
));
13614 return SQLITE_NOMEM
;
13616 memset(pCsr
, 0, sizeof(DbdataCursor
));
13617 pCsr
->base
.pVtab
= pVTab
;
13620 *ppCursor
= (sqlite3_vtab_cursor
*)pCsr
;
13625 ** Restore a cursor object to the state it was in when first allocated
13626 ** by dbdataOpen().
13628 static void dbdataResetCursor(DbdataCursor
*pCsr
){
13629 DbdataTable
*pTab
= (DbdataTable
*)(pCsr
->base
.pVtab
);
13630 if( pTab
->pStmt
==0 ){
13631 pTab
->pStmt
= pCsr
->pStmt
;
13633 sqlite3_finalize(pCsr
->pStmt
);
13639 pCsr
->bOnePage
= 0;
13640 sqlite3_free(pCsr
->aPage
);
13641 sqlite3_free(pCsr
->pRec
);
13647 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
13649 static int dbdataClose(sqlite3_vtab_cursor
*pCursor
){
13650 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
13651 dbdataResetCursor(pCsr
);
13652 sqlite3_free(pCsr
);
13657 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
13659 static u32
get_uint16(unsigned char *a
){
13660 return (a
[0]<<8)|a
[1];
13662 static u32
get_uint32(unsigned char *a
){
13663 return ((u32
)a
[0]<<24)
13670 ** Load page pgno from the database via the sqlite_dbpage virtual table.
13671 ** If successful, set (*ppPage) to point to a buffer containing the page
13672 ** data, (*pnPage) to the size of that buffer in bytes and return
13673 ** SQLITE_OK. In this case it is the responsibility of the caller to
13674 ** eventually free the buffer using sqlite3_free().
13676 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
13677 ** return an SQLite error code.
13679 static int dbdataLoadPage(
13680 DbdataCursor
*pCsr
, /* Cursor object */
13681 u32 pgno
, /* Page number of page to load */
13682 u8
**ppPage
, /* OUT: pointer to page buffer */
13683 int *pnPage
/* OUT: Size of (*ppPage) in bytes */
13686 int rc
= SQLITE_OK
;
13687 sqlite3_stmt
*pStmt
= pCsr
->pStmt
;
13692 sqlite3_bind_int64(pStmt
, 2, pgno
);
13693 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
13694 int nCopy
= sqlite3_column_bytes(pStmt
, 0);
13697 pPage
= (u8
*)sqlite3_malloc64(nCopy
+ DBDATA_PADDING_BYTES
);
13701 const u8
*pCopy
= sqlite3_column_blob(pStmt
, 0);
13702 memcpy(pPage
, pCopy
, nCopy
);
13703 memset(&pPage
[nCopy
], 0, DBDATA_PADDING_BYTES
);
13709 rc2
= sqlite3_reset(pStmt
);
13710 if( rc
==SQLITE_OK
) rc
= rc2
;
13717 ** Read a varint. Put the value in *pVal and return the number of bytes.
13719 static int dbdataGetVarint(const u8
*z
, sqlite3_int64
*pVal
){
13720 sqlite3_uint64 u
= 0;
13722 for(i
=0; i
<8; i
++){
13723 u
= (u
<<7) + (z
[i
]&0x7f);
13724 if( (z
[i
]&0x80)==0 ){ *pVal
= (sqlite3_int64
)u
; return i
+1; }
13726 u
= (u
<<8) + (z
[i
]&0xff);
13727 *pVal
= (sqlite3_int64
)u
;
13732 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
13733 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
13734 ** SQLite database except for key values in intkey tables.
13736 static int dbdataGetVarintU32(const u8
*z
, sqlite3_int64
*pVal
){
13738 int nRet
= dbdataGetVarint(z
, &val
);
13739 if( val
<0 || val
>0xFFFFFFFF ) val
= 0;
13745 ** Return the number of bytes of space used by an SQLite value of type
13748 static int dbdataValueBytes(int eType
){
13750 case 0: case 8: case 9:
13768 return ((eType
-12) / 2);
13775 ** Load a value of type eType from buffer pData and use it to set the
13776 ** result of context object pCtx.
13778 static void dbdataValue(
13779 sqlite3_context
*pCtx
,
13783 sqlite3_int64 nData
13785 if( eType
>=0 && dbdataValueBytes(eType
)<=nData
){
13790 sqlite3_result_null(pCtx
);
13794 sqlite3_result_int(pCtx
, 0);
13797 sqlite3_result_int(pCtx
, 1);
13800 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
13801 sqlite3_uint64 v
= (signed char)pData
[0];
13805 case 6: v
= (v
<<16) + (pData
[0]<<8) + pData
[1]; pData
+= 2;
13806 case 5: v
= (v
<<16) + (pData
[0]<<8) + pData
[1]; pData
+= 2;
13807 case 4: v
= (v
<<8) + pData
[0]; pData
++;
13808 case 3: v
= (v
<<8) + pData
[0]; pData
++;
13809 case 2: v
= (v
<<8) + pData
[0]; pData
++;
13814 memcpy(&r
, &v
, sizeof(r
));
13815 sqlite3_result_double(pCtx
, r
);
13817 sqlite3_result_int64(pCtx
, (sqlite3_int64
)v
);
13823 int n
= ((eType
-12) / 2);
13826 #ifndef SQLITE_OMIT_UTF16
13827 case SQLITE_UTF16BE
:
13828 sqlite3_result_text16be(pCtx
, (void*)pData
, n
, SQLITE_TRANSIENT
);
13830 case SQLITE_UTF16LE
:
13831 sqlite3_result_text16le(pCtx
, (void*)pData
, n
, SQLITE_TRANSIENT
);
13835 sqlite3_result_text(pCtx
, (char*)pData
, n
, SQLITE_TRANSIENT
);
13839 sqlite3_result_blob(pCtx
, pData
, n
, SQLITE_TRANSIENT
);
13847 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
13849 static int dbdataNext(sqlite3_vtab_cursor
*pCursor
){
13850 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
13851 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
13856 int iOff
= (pCsr
->iPgno
==1 ? 100 : 0);
13859 if( pCsr
->aPage
==0 ){
13861 if( pCsr
->bOnePage
==0 && pCsr
->iPgno
>pCsr
->szDb
) return SQLITE_OK
;
13862 rc
= dbdataLoadPage(pCsr
, pCsr
->iPgno
, &pCsr
->aPage
, &pCsr
->nPage
);
13863 if( rc
!=SQLITE_OK
) return rc
;
13864 if( pCsr
->aPage
&& pCsr
->nPage
>=256 ) break;
13865 sqlite3_free(pCsr
->aPage
);
13867 if( pCsr
->bOnePage
) return SQLITE_OK
;
13871 assert( iOff
+3+2<=pCsr
->nPage
);
13872 pCsr
->iCell
= pTab
->bPtr
? -2 : 0;
13873 pCsr
->nCell
= get_uint16(&pCsr
->aPage
[iOff
+3]);
13877 if( pCsr
->aPage
[iOff
]!=0x02 && pCsr
->aPage
[iOff
]!=0x05 ){
13878 pCsr
->iCell
= pCsr
->nCell
;
13881 if( pCsr
->iCell
>=pCsr
->nCell
){
13882 sqlite3_free(pCsr
->aPage
);
13884 if( pCsr
->bOnePage
) return SQLITE_OK
;
13890 /* If there is no record loaded, load it now. */
13891 if( pCsr
->pRec
==0 ){
13894 sqlite3_int64 nPayload
= 0;
13895 sqlite3_int64 nHdr
= 0;
13900 switch( pCsr
->aPage
[iOff
] ){
13910 /* This is not a b-tree page with records on it. Continue. */
13911 pCsr
->iCell
= pCsr
->nCell
;
13915 if( pCsr
->iCell
>=pCsr
->nCell
){
13919 iOff
+= 8 + nPointer
+ pCsr
->iCell
*2;
13920 if( iOff
>pCsr
->nPage
){
13923 iOff
= get_uint16(&pCsr
->aPage
[iOff
]);
13926 /* For an interior node cell, skip past the child-page number */
13929 /* Load the "byte of payload including overflow" field */
13930 if( bNextPage
|| iOff
>pCsr
->nPage
){
13933 iOff
+= dbdataGetVarintU32(&pCsr
->aPage
[iOff
], &nPayload
);
13936 /* If this is a leaf intkey cell, load the rowid */
13937 if( bHasRowid
&& !bNextPage
&& iOff
<pCsr
->nPage
){
13938 iOff
+= dbdataGetVarint(&pCsr
->aPage
[iOff
], &pCsr
->iIntkey
);
13941 /* Figure out how much data to read from the local page */
13946 X
= ((U
-12)*64/255)-23;
13952 M
= ((U
-12)*32/255)-23;
13953 K
= M
+((nPayload
-M
)%(U
-4));
13961 if( bNextPage
|| nLocal
+iOff
>pCsr
->nPage
){
13965 /* Allocate space for payload. And a bit more to catch small buffer
13966 ** overruns caused by attempting to read a varint or similar from
13967 ** near the end of a corrupt record. */
13968 pCsr
->pRec
= (u8
*)sqlite3_malloc64(nPayload
+DBDATA_PADDING_BYTES
);
13969 if( pCsr
->pRec
==0 ) return SQLITE_NOMEM
;
13970 memset(pCsr
->pRec
, 0, nPayload
+DBDATA_PADDING_BYTES
);
13971 pCsr
->nRec
= nPayload
;
13973 /* Load the nLocal bytes of payload */
13974 memcpy(pCsr
->pRec
, &pCsr
->aPage
[iOff
], nLocal
);
13977 /* Load content from overflow pages */
13978 if( nPayload
>nLocal
){
13979 sqlite3_int64 nRem
= nPayload
- nLocal
;
13980 u32 pgnoOvfl
= get_uint32(&pCsr
->aPage
[iOff
]);
13985 rc
= dbdataLoadPage(pCsr
, pgnoOvfl
, &aOvfl
, &nOvfl
);
13986 assert( rc
!=SQLITE_OK
|| aOvfl
==0 || nOvfl
==pCsr
->nPage
);
13987 if( rc
!=SQLITE_OK
) return rc
;
13988 if( aOvfl
==0 ) break;
13991 if( nCopy
>nRem
) nCopy
= nRem
;
13992 memcpy(&pCsr
->pRec
[nPayload
-nRem
], &aOvfl
[4], nCopy
);
13995 pgnoOvfl
= get_uint32(aOvfl
);
13996 sqlite3_free(aOvfl
);
14000 iHdr
= dbdataGetVarintU32(pCsr
->pRec
, &nHdr
);
14001 if( nHdr
>nPayload
) nHdr
= 0;
14003 pCsr
->pHdrPtr
= &pCsr
->pRec
[iHdr
];
14004 pCsr
->pPtr
= &pCsr
->pRec
[pCsr
->nHdr
];
14005 pCsr
->iField
= (bHasRowid
? -1 : 0);
14010 if( pCsr
->iField
>0 ){
14011 sqlite3_int64 iType
;
14012 if( pCsr
->pHdrPtr
>&pCsr
->pRec
[pCsr
->nRec
] ){
14016 pCsr
->pHdrPtr
+= dbdataGetVarintU32(pCsr
->pHdrPtr
, &iType
);
14017 szField
= dbdataValueBytes(iType
);
14018 if( (pCsr
->nRec
- (pCsr
->pPtr
- pCsr
->pRec
))<szField
){
14019 pCsr
->pPtr
= &pCsr
->pRec
[pCsr
->nRec
];
14021 pCsr
->pPtr
+= szField
;
14028 sqlite3_free(pCsr
->aPage
);
14029 sqlite3_free(pCsr
->pRec
);
14032 if( pCsr
->bOnePage
) return SQLITE_OK
;
14035 if( pCsr
->iField
<0 || pCsr
->pHdrPtr
<&pCsr
->pRec
[pCsr
->nHdr
] ){
14039 /* Advance to the next cell. The next iteration of the loop will load
14040 ** the record and so on. */
14041 sqlite3_free(pCsr
->pRec
);
14048 assert( !"can't get here" );
14053 ** Return true if the cursor is at EOF.
14055 static int dbdataEof(sqlite3_vtab_cursor
*pCursor
){
14056 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
14057 return pCsr
->aPage
==0;
14061 ** Return true if nul-terminated string zSchema ends in "()". Or false
14064 static int dbdataIsFunction(const char *zSchema
){
14065 size_t n
= strlen(zSchema
);
14066 if( n
>2 && zSchema
[n
-2]=='(' && zSchema
[n
-1]==')' ){
14073 ** Determine the size in pages of database zSchema (where zSchema is
14074 ** "main", "temp" or the name of an attached database) and set
14075 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
14076 ** an SQLite error code.
14078 static int dbdataDbsize(DbdataCursor
*pCsr
, const char *zSchema
){
14079 DbdataTable
*pTab
= (DbdataTable
*)pCsr
->base
.pVtab
;
14083 sqlite3_stmt
*pStmt
= 0;
14085 if( (nFunc
= dbdataIsFunction(zSchema
))>0 ){
14086 zSql
= sqlite3_mprintf("SELECT %.*s(0)", nFunc
, zSchema
);
14088 zSql
= sqlite3_mprintf("PRAGMA %Q.page_count", zSchema
);
14090 if( zSql
==0 ) return SQLITE_NOMEM
;
14092 rc
= sqlite3_prepare_v2(pTab
->db
, zSql
, -1, &pStmt
, 0);
14093 sqlite3_free(zSql
);
14094 if( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
14095 pCsr
->szDb
= sqlite3_column_int(pStmt
, 0);
14097 rc2
= sqlite3_finalize(pStmt
);
14098 if( rc
==SQLITE_OK
) rc
= rc2
;
14103 ** Attempt to figure out the encoding of the database by retrieving page 1
14104 ** and inspecting the header field. If successful, set the pCsr->enc variable
14105 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
14107 static int dbdataGetEncoding(DbdataCursor
*pCsr
){
14108 int rc
= SQLITE_OK
;
14111 rc
= dbdataLoadPage(pCsr
, 1, &aPg1
, &nPg1
);
14112 if( rc
==SQLITE_OK
&& nPg1
>=(56+4) ){
14113 pCsr
->enc
= get_uint32(&aPg1
[56]);
14115 sqlite3_free(aPg1
);
14121 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
14123 static int dbdataFilter(
14124 sqlite3_vtab_cursor
*pCursor
,
14125 int idxNum
, const char *idxStr
,
14126 int argc
, sqlite3_value
**argv
14128 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
14129 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
14130 int rc
= SQLITE_OK
;
14131 const char *zSchema
= "main";
14135 dbdataResetCursor(pCsr
);
14136 assert( pCsr
->iPgno
==1 );
14137 if( idxNum
& 0x01 ){
14138 zSchema
= (const char*)sqlite3_value_text(argv
[0]);
14139 if( zSchema
==0 ) zSchema
= "";
14141 if( idxNum
& 0x02 ){
14142 pCsr
->iPgno
= sqlite3_value_int(argv
[(idxNum
& 0x01)]);
14143 pCsr
->bOnePage
= 1;
14145 rc
= dbdataDbsize(pCsr
, zSchema
);
14148 if( rc
==SQLITE_OK
){
14151 pCsr
->pStmt
= pTab
->pStmt
;
14153 }else if( (nFunc
= dbdataIsFunction(zSchema
))>0 ){
14154 char *zSql
= sqlite3_mprintf("SELECT %.*s(?2)", nFunc
, zSchema
);
14158 rc
= sqlite3_prepare_v2(pTab
->db
, zSql
, -1, &pCsr
->pStmt
, 0);
14159 sqlite3_free(zSql
);
14162 rc
= sqlite3_prepare_v2(pTab
->db
,
14163 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
14168 if( rc
==SQLITE_OK
){
14169 rc
= sqlite3_bind_text(pCsr
->pStmt
, 1, zSchema
, -1, SQLITE_TRANSIENT
);
14172 /* Try to determine the encoding of the db by inspecting the header
14173 ** field on page 1. */
14174 if( rc
==SQLITE_OK
){
14175 rc
= dbdataGetEncoding(pCsr
);
14178 if( rc
!=SQLITE_OK
){
14179 pTab
->base
.zErrMsg
= sqlite3_mprintf("%s", sqlite3_errmsg(pTab
->db
));
14182 if( rc
==SQLITE_OK
){
14183 rc
= dbdataNext(pCursor
);
14189 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
14191 static int dbdataColumn(
14192 sqlite3_vtab_cursor
*pCursor
,
14193 sqlite3_context
*ctx
,
14196 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
14197 DbdataTable
*pTab
= (DbdataTable
*)pCursor
->pVtab
;
14200 case DBPTR_COLUMN_PGNO
:
14201 sqlite3_result_int64(ctx
, pCsr
->iPgno
);
14203 case DBPTR_COLUMN_CHILD
: {
14204 int iOff
= pCsr
->iPgno
==1 ? 100 : 0;
14205 if( pCsr
->iCell
<0 ){
14208 iOff
+= 12 + pCsr
->iCell
*2;
14209 if( iOff
>pCsr
->nPage
) return SQLITE_OK
;
14210 iOff
= get_uint16(&pCsr
->aPage
[iOff
]);
14212 if( iOff
<=pCsr
->nPage
){
14213 sqlite3_result_int64(ctx
, get_uint32(&pCsr
->aPage
[iOff
]));
14220 case DBDATA_COLUMN_PGNO
:
14221 sqlite3_result_int64(ctx
, pCsr
->iPgno
);
14223 case DBDATA_COLUMN_CELL
:
14224 sqlite3_result_int(ctx
, pCsr
->iCell
);
14226 case DBDATA_COLUMN_FIELD
:
14227 sqlite3_result_int(ctx
, pCsr
->iField
);
14229 case DBDATA_COLUMN_VALUE
: {
14230 if( pCsr
->iField
<0 ){
14231 sqlite3_result_int64(ctx
, pCsr
->iIntkey
);
14232 }else if( &pCsr
->pRec
[pCsr
->nRec
] >= pCsr
->pPtr
){
14233 sqlite3_int64 iType
;
14234 dbdataGetVarintU32(pCsr
->pHdrPtr
, &iType
);
14236 ctx
, pCsr
->enc
, iType
, pCsr
->pPtr
,
14237 &pCsr
->pRec
[pCsr
->nRec
] - pCsr
->pPtr
14248 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
14250 static int dbdataRowid(sqlite3_vtab_cursor
*pCursor
, sqlite_int64
*pRowid
){
14251 DbdataCursor
*pCsr
= (DbdataCursor
*)pCursor
;
14252 *pRowid
= pCsr
->iRowid
;
14258 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
14260 static int sqlite3DbdataRegister(sqlite3
*db
){
14261 static sqlite3_module dbdata_module
= {
14264 dbdataConnect
, /* xConnect */
14265 dbdataBestIndex
, /* xBestIndex */
14266 dbdataDisconnect
, /* xDisconnect */
14268 dbdataOpen
, /* xOpen - open a cursor */
14269 dbdataClose
, /* xClose - close a cursor */
14270 dbdataFilter
, /* xFilter - configure scan constraints */
14271 dbdataNext
, /* xNext - advance a cursor */
14272 dbdataEof
, /* xEof - check for end of scan */
14273 dbdataColumn
, /* xColumn - read data */
14274 dbdataRowid
, /* xRowid - read data */
14280 0, /* xFindMethod */
14282 0, /* xSavepoint */
14284 0, /* xRollbackTo */
14285 0 /* xShadowName */
14288 int rc
= sqlite3_create_module(db
, "sqlite_dbdata", &dbdata_module
, 0);
14289 if( rc
==SQLITE_OK
){
14290 rc
= sqlite3_create_module(db
, "sqlite_dbptr", &dbdata_module
, (void*)1);
14295 int sqlite3_dbdata_init(
14298 const sqlite3_api_routines
*pApi
14301 return sqlite3DbdataRegister(db
);
14304 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14306 /************************* End ../ext/recover/dbdata.c ********************/
14307 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
14311 ** The author disclaims copyright to this source code. In place of
14312 ** a legal notice, here is a blessing:
14314 ** May you do good and not evil.
14315 ** May you find forgiveness for yourself and forgive others.
14316 ** May you share freely, never taking more than you give.
14318 *************************************************************************
14323 /* #include "sqlite3recover.h" */
14324 #include <assert.h>
14325 #include <string.h>
14327 #ifndef SQLITE_OMIT_VIRTUALTABLE
14330 ** Declaration for public API function in file dbdata.c. This may be called
14331 ** with NULL as the final two arguments to register the sqlite_dbptr and
14332 ** sqlite_dbdata virtual tables with a database handle.
14337 int sqlite3_dbdata_init(sqlite3
*, char**, const sqlite3_api_routines
*);
14339 /* typedef unsigned int u32; */
14340 /* typedef unsigned char u8; */
14341 /* typedef sqlite3_int64 i64; */
14343 typedef struct RecoverTable RecoverTable
;
14344 typedef struct RecoverColumn RecoverColumn
;
14347 ** When recovering rows of data that can be associated with table
14348 ** definitions recovered from the sqlite_schema table, each table is
14349 ** represented by an instance of the following object.
14352 ** The root page in the original database. Not necessarily (and usually
14353 ** not) the same in the recovered database.
14356 ** Name of the table.
14359 ** aCol[] is an array of nCol columns. In the order in which they appear
14363 ** Set to true for intkey tables, false for WITHOUT ROWID.
14366 ** Each column in the aCol[] array has associated with it the index of
14367 ** the bind parameter its values will be bound to in the INSERT statement
14368 ** used to construct the output database. If the table does has a rowid
14369 ** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
14370 ** index of the bind paramater to which the rowid value should be bound.
14371 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
14372 ** KEY column, then the rowid value should be bound to the index associated
14373 ** with the column.
14376 ** All RecoverTable objects used by the recovery operation are allocated
14377 ** and populated as part of creating the recovered database schema in
14378 ** the output database, before any non-schema data are recovered. They
14379 ** are then stored in a singly-linked list linked by this variable beginning
14380 ** at sqlite3_recover.pTblList.
14382 struct RecoverTable
{
14383 u32 iRoot
; /* Root page in original database */
14384 char *zTab
; /* Name of table */
14385 int nCol
; /* Number of columns in table */
14386 RecoverColumn
*aCol
; /* Array of columns */
14387 int bIntkey
; /* True for intkey, false for without rowid */
14388 int iRowidBind
; /* If >0, bind rowid to INSERT here */
14389 RecoverTable
*pNext
;
14393 ** Each database column is represented by an instance of the following object
14394 ** stored in the RecoverTable.aCol[] array of the associated table.
14397 ** The index of the associated field within database records. Or -1 if
14398 ** there is no associated field (e.g. for virtual generated columns).
14401 ** The bind index of the INSERT statement to bind this columns values
14402 ** to. Or 0 if there is no such index (iff (iField<0)).
14405 ** True if this is the INTEGER PRIMARY KEY column.
14411 ** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
14413 struct RecoverColumn
{
14414 int iField
; /* Field in record on disk */
14415 int iBind
; /* Binding to use in INSERT */
14416 int bIPK
; /* True for IPK column */
14421 #define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
14422 #define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
14423 #define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
14424 #define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
14427 ** Bitmap object used to track pages in the input database. Allocated
14428 ** and manipulated only by the following functions:
14430 ** recoverBitmapAlloc()
14431 ** recoverBitmapFree()
14432 ** recoverBitmapSet()
14433 ** recoverBitmapQuery()
14436 ** Largest page number that may be stored in the bitmap. The range
14437 ** of valid keys is 1 to nPg, inclusive.
14440 ** Array large enough to contain a bit for each key. For key value
14441 ** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
14442 ** In other words, the following is true if bit iKey is set, or
14443 ** false if it is clear:
14445 ** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
14447 typedef struct RecoverBitmap RecoverBitmap
;
14448 struct RecoverBitmap
{
14449 i64 nPg
; /* Size of bitmap */
14450 u32 aElem
[1]; /* Array of 32-bit bitmasks */
14454 ** State variables (part of the sqlite3_recover structure) used while
14455 ** recovering data for tables identified in the recovered schema (state
14456 ** RECOVER_STATE_WRITING).
14458 typedef struct RecoverStateW1 RecoverStateW1
;
14459 struct RecoverStateW1
{
14460 sqlite3_stmt
*pTbls
;
14461 sqlite3_stmt
*pSel
;
14462 sqlite3_stmt
*pInsert
;
14465 RecoverTable
*pTab
; /* Table currently being written */
14466 int nMax
; /* Max column count in any schema table */
14467 sqlite3_value
**apVal
; /* Array of nMax values */
14468 int nVal
; /* Number of valid entries in apVal[] */
14476 ** State variables (part of the sqlite3_recover structure) used while
14477 ** recovering data destined for the lost and found table (states
14478 ** RECOVER_STATE_LOSTANDFOUND[123]).
14480 typedef struct RecoverStateLAF RecoverStateLAF
;
14481 struct RecoverStateLAF
{
14482 RecoverBitmap
*pUsed
;
14483 i64 nPg
; /* Size of db in pages */
14484 sqlite3_stmt
*pAllAndParent
;
14485 sqlite3_stmt
*pMapInsert
;
14486 sqlite3_stmt
*pMaxField
;
14487 sqlite3_stmt
*pUsedPages
;
14488 sqlite3_stmt
*pFindRoot
;
14489 sqlite3_stmt
*pInsert
; /* INSERT INTO lost_and_found ... */
14490 sqlite3_stmt
*pAllPage
;
14491 sqlite3_stmt
*pPageData
;
14492 sqlite3_value
**apVal
;
14497 ** Main recover handle structure.
14499 struct sqlite3_recover
{
14500 /* Copies of sqlite3_recover_init[_sql]() parameters */
14501 sqlite3
*dbIn
; /* Input database */
14502 char *zDb
; /* Name of input db ("main" etc.) */
14503 char *zUri
; /* URI for output database */
14504 void *pSqlCtx
; /* SQL callback context */
14505 int (*xSql
)(void*,const char*); /* Pointer to SQL callback function */
14507 /* Values configured by sqlite3_recover_config() */
14508 char *zStateDb
; /* State database to use (or NULL) */
14509 char *zLostAndFound
; /* Name of lost-and-found table (or NULL) */
14510 int bFreelistCorrupt
; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
14511 int bRecoverRowid
; /* SQLITE_RECOVER_ROWIDS setting */
14512 int bSlowIndexes
; /* SQLITE_RECOVER_SLOWINDEXES setting */
14520 /* Error code and error message */
14521 int errCode
; /* For sqlite3_recover_errcode() */
14522 char *zErrMsg
; /* For sqlite3_recover_errmsg() */
14525 int bCloseTransaction
;
14527 /* Variables used with eState==RECOVER_STATE_WRITING */
14530 /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
14531 RecoverStateLAF laf
;
14533 /* Fields used within sqlite3_recover_run() */
14534 sqlite3
*dbOut
; /* Output database */
14535 sqlite3_stmt
*pGetPage
; /* SELECT against input db sqlite_dbdata */
14536 RecoverTable
*pTblList
; /* List of tables recovered from schema */
14540 ** The various states in which an sqlite3_recover object may exist:
14542 ** RECOVER_STATE_INIT:
14543 ** The object is initially created in this state. sqlite3_recover_step()
14544 ** has yet to be called. This is the only state in which it is permitted
14545 ** to call sqlite3_recover_config().
14547 ** RECOVER_STATE_WRITING:
14549 ** RECOVER_STATE_LOSTANDFOUND1:
14550 ** State to populate the bitmap of pages used by other tables or the
14551 ** database freelist.
14553 ** RECOVER_STATE_LOSTANDFOUND2:
14554 ** Populate the recovery.map table - used to figure out a "root" page
14555 ** for each lost page from in the database from which records are
14558 ** RECOVER_STATE_LOSTANDFOUND3:
14559 ** Populate the lost-and-found table itself.
14561 #define RECOVER_STATE_INIT 0
14562 #define RECOVER_STATE_WRITING 1
14563 #define RECOVER_STATE_LOSTANDFOUND1 2
14564 #define RECOVER_STATE_LOSTANDFOUND2 3
14565 #define RECOVER_STATE_LOSTANDFOUND3 4
14566 #define RECOVER_STATE_SCHEMA2 5
14567 #define RECOVER_STATE_DONE 6
14571 ** Global variables used by this extension.
14573 typedef struct RecoverGlobal RecoverGlobal
;
14574 struct RecoverGlobal
{
14575 const sqlite3_io_methods
*pMethods
;
14576 sqlite3_recover
*p
;
14578 static RecoverGlobal recover_g
;
14581 ** Use this static SQLite mutex to protect the globals during the
14582 ** first call to sqlite3_recover_step().
14584 #define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
14588 ** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
14590 #define RECOVER_ROWID_DEFAULT 1
14595 ** recoverEnterMutex() - Enter the recovery mutex
14596 ** recoverLeaveMutex() - Leave the recovery mutex
14597 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
14599 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
14600 # define recoverEnterMutex()
14601 # define recoverLeaveMutex()
14603 static void recoverEnterMutex(void){
14604 sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID
));
14606 static void recoverLeaveMutex(void){
14607 sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID
));
14610 #if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
14611 static void recoverAssertMutexHeld(void){
14612 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID
)) );
14615 # define recoverAssertMutexHeld()
14620 ** Like strlen(). But handles NULL pointer arguments.
14622 static int recoverStrlen(const char *zStr
){
14623 if( zStr
==0 ) return 0;
14624 return (int)(strlen(zStr
)&0x7fffffff);
14628 ** This function is a no-op if the recover handle passed as the first
14629 ** argument already contains an error (if p->errCode!=SQLITE_OK).
14631 ** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
14632 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
14633 ** if an OOM error occurs, NULL is returned and the handle error code
14634 ** (p->errCode) set to SQLITE_NOMEM.
14636 static void *recoverMalloc(sqlite3_recover
*p
, i64 nByte
){
14639 if( p
->errCode
==SQLITE_OK
){
14640 pRet
= sqlite3_malloc64(nByte
);
14642 memset(pRet
, 0, nByte
);
14644 p
->errCode
= SQLITE_NOMEM
;
14651 ** Set the error code and error message for the recover handle passed as
14652 ** the first argument. The error code is set to the value of parameter
14655 ** Parameter zFmt must be a printf() style formatting string. The handle
14656 ** error message is set to the result of using any trailing arguments for
14657 ** parameter substitutions in the formatting string.
14661 ** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
14663 static int recoverError(
14664 sqlite3_recover
*p
,
14666 const char *zFmt
, ...
14670 va_start(ap
, zFmt
);
14672 z
= sqlite3_vmprintf(zFmt
, ap
);
14675 sqlite3_free(p
->zErrMsg
);
14677 p
->errCode
= errCode
;
14683 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
14684 ** In this case it returns NULL.
14686 ** Otherwise, an attempt is made to allocate and return a bitmap object
14687 ** large enough to store a bit for all page numbers between 1 and nPg,
14688 ** inclusive. The bitmap is initially zeroed.
14690 static RecoverBitmap
*recoverBitmapAlloc(sqlite3_recover
*p
, i64 nPg
){
14691 int nElem
= (nPg
+1+31) / 32;
14692 int nByte
= sizeof(RecoverBitmap
) + nElem
*sizeof(u32
);
14693 RecoverBitmap
*pRet
= (RecoverBitmap
*)recoverMalloc(p
, nByte
);
14702 ** Free a bitmap object allocated by recoverBitmapAlloc().
14704 static void recoverBitmapFree(RecoverBitmap
*pMap
){
14705 sqlite3_free(pMap
);
14709 ** Set the bit associated with page iPg in bitvec pMap.
14711 static void recoverBitmapSet(RecoverBitmap
*pMap
, i64 iPg
){
14712 if( iPg
<=pMap
->nPg
){
14713 int iElem
= (iPg
/ 32);
14714 int iBit
= (iPg
% 32);
14715 pMap
->aElem
[iElem
] |= (((u32
)1) << iBit
);
14720 ** Query bitmap object pMap for the state of the bit associated with page
14721 ** iPg. Return 1 if it is set, or 0 otherwise.
14723 static int recoverBitmapQuery(RecoverBitmap
*pMap
, i64 iPg
){
14725 if( iPg
<=pMap
->nPg
&& iPg
>0 ){
14726 int iElem
= (iPg
/ 32);
14727 int iBit
= (iPg
% 32);
14728 ret
= (pMap
->aElem
[iElem
] & (((u32
)1) << iBit
)) ? 1 : 0;
14734 ** Set the recover handle error to the error code and message returned by
14735 ** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
14738 static int recoverDbError(sqlite3_recover
*p
, sqlite3
*db
){
14739 return recoverError(p
, sqlite3_errcode(db
), "%s", sqlite3_errmsg(db
));
14743 ** This function is a no-op if recover handle p already contains an error
14744 ** (if p->errCode!=SQLITE_OK).
14746 ** Otherwise, it attempts to prepare the SQL statement in zSql against
14747 ** database handle db. If successful, the statement handle is returned.
14748 ** Or, if an error occurs, NULL is returned and an error left in the
14751 static sqlite3_stmt
*recoverPrepare(
14752 sqlite3_recover
*p
,
14756 sqlite3_stmt
*pStmt
= 0;
14757 if( p
->errCode
==SQLITE_OK
){
14758 if( sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0) ){
14759 recoverDbError(p
, db
);
14766 ** This function is a no-op if recover handle p already contains an error
14767 ** (if p->errCode!=SQLITE_OK).
14769 ** Otherwise, argument zFmt is used as a printf() style format string,
14770 ** along with any trailing arguments, to create an SQL statement. This
14771 ** SQL statement is prepared against database handle db and, if successful,
14772 ** the statment handle returned. Or, if an error occurs - either during
14773 ** the printf() formatting or when preparing the resulting SQL - an
14774 ** error code and message are left in the recover handle.
14776 static sqlite3_stmt
*recoverPreparePrintf(
14777 sqlite3_recover
*p
,
14779 const char *zFmt
, ...
14781 sqlite3_stmt
*pStmt
= 0;
14782 if( p
->errCode
==SQLITE_OK
){
14785 va_start(ap
, zFmt
);
14786 z
= sqlite3_vmprintf(zFmt
, ap
);
14789 p
->errCode
= SQLITE_NOMEM
;
14791 pStmt
= recoverPrepare(p
, db
, z
);
14799 ** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
14800 ** indicates that an error occurred, and there is not already an error
14801 ** in the recover handle passed as the first argument, set the error
14802 ** code and error message appropriately.
14804 ** This function returns a copy of the statement handle pointer passed
14805 ** as the second argument.
14807 static sqlite3_stmt
*recoverReset(sqlite3_recover
*p
, sqlite3_stmt
*pStmt
){
14808 int rc
= sqlite3_reset(pStmt
);
14809 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_CONSTRAINT
&& p
->errCode
==SQLITE_OK
){
14810 recoverDbError(p
, sqlite3_db_handle(pStmt
));
14816 ** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
14817 ** indicates that an error occurred, and there is not already an error
14818 ** in the recover handle passed as the first argument, set the error
14819 ** code and error message appropriately.
14821 static void recoverFinalize(sqlite3_recover
*p
, sqlite3_stmt
*pStmt
){
14822 sqlite3
*db
= sqlite3_db_handle(pStmt
);
14823 int rc
= sqlite3_finalize(pStmt
);
14824 if( rc
!=SQLITE_OK
&& p
->errCode
==SQLITE_OK
){
14825 recoverDbError(p
, db
);
14830 ** This function is a no-op if recover handle p already contains an error
14831 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
14834 ** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
14835 ** Or, if an error occurs, leave an error code and message in the recover
14836 ** handle and return a copy of the error code.
14838 static int recoverExec(sqlite3_recover
*p
, sqlite3
*db
, const char *zSql
){
14839 if( p
->errCode
==SQLITE_OK
){
14840 int rc
= sqlite3_exec(db
, zSql
, 0, 0, 0);
14842 recoverDbError(p
, db
);
14849 ** Bind the value pVal to parameter iBind of statement pStmt. Leave an
14850 ** error in the recover handle passed as the first argument if an error
14851 ** (e.g. an OOM) occurs.
14853 static void recoverBindValue(
14854 sqlite3_recover
*p
,
14855 sqlite3_stmt
*pStmt
,
14857 sqlite3_value
*pVal
14859 if( p
->errCode
==SQLITE_OK
){
14860 int rc
= sqlite3_bind_value(pStmt
, iBind
, pVal
);
14861 if( rc
) recoverError(p
, rc
, 0);
14866 ** This function is a no-op if recover handle p already contains an error
14867 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
14869 ** Otherwise, an attempt is made to interpret zFmt as a printf() style
14870 ** formatting string and the result of using the trailing arguments for
14871 ** parameter substitution with it written into a buffer obtained from
14872 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
14873 ** It is the responsibility of the caller to eventually free the buffer
14874 ** using sqlite3_free().
14876 ** Or, if an error occurs, an error code and message is left in the recover
14877 ** handle and NULL returned.
14879 static char *recoverMPrintf(sqlite3_recover
*p
, const char *zFmt
, ...){
14882 va_start(ap
, zFmt
);
14883 z
= sqlite3_vmprintf(zFmt
, ap
);
14885 if( p
->errCode
==SQLITE_OK
){
14886 if( z
==0 ) p
->errCode
= SQLITE_NOMEM
;
14895 ** This function is a no-op if recover handle p already contains an error
14896 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
14898 ** Otherwise, execute "PRAGMA page_count" against the input database. If
14899 ** successful, return the integer result. Or, if an error occurs, leave an
14900 ** error code and error message in the sqlite3_recover handle and return
14903 static i64
recoverPageCount(sqlite3_recover
*p
){
14905 if( p
->errCode
==SQLITE_OK
){
14906 sqlite3_stmt
*pStmt
= 0;
14907 pStmt
= recoverPreparePrintf(p
, p
->dbIn
, "PRAGMA %Q.page_count", p
->zDb
);
14909 sqlite3_step(pStmt
);
14910 nPg
= sqlite3_column_int64(pStmt
, 0);
14912 recoverFinalize(p
, pStmt
);
14918 ** Implementation of SQL scalar function "read_i32". The first argument to
14919 ** this function must be a blob. The second a non-negative integer. This
14920 ** function reads and returns a 32-bit big-endian integer from byte
14921 ** offset (4*<arg2>) of the blob.
14923 ** SELECT read_i32(<blob>, <idx>)
14925 static void recoverReadI32(
14926 sqlite3_context
*context
,
14928 sqlite3_value
**argv
14930 const unsigned char *pBlob
;
14935 nBlob
= sqlite3_value_bytes(argv
[0]);
14936 pBlob
= (const unsigned char*)sqlite3_value_blob(argv
[0]);
14937 iInt
= sqlite3_value_int(argv
[1]) & 0xFFFF;
14939 if( (iInt
+1)*4<=nBlob
){
14940 const unsigned char *a
= &pBlob
[iInt
*4];
14941 i64 iVal
= ((i64
)a
[0]<<24)
14945 sqlite3_result_int64(context
, iVal
);
14950 ** Implementation of SQL scalar function "page_is_used". This function
14951 ** is used as part of the procedure for locating orphan rows for the
14952 ** lost-and-found table, and it depends on those routines having populated
14953 ** the sqlite3_recover.laf.pUsed variable.
14955 ** The only argument to this function is a page-number. It returns true
14956 ** if the page has already been used somehow during data recovery, or false
14959 ** SELECT page_is_used(<pgno>);
14961 static void recoverPageIsUsed(
14962 sqlite3_context
*pCtx
,
14964 sqlite3_value
**apArg
14966 sqlite3_recover
*p
= (sqlite3_recover
*)sqlite3_user_data(pCtx
);
14967 i64 pgno
= sqlite3_value_int64(apArg
[0]);
14969 sqlite3_result_int(pCtx
, recoverBitmapQuery(p
->laf
.pUsed
, pgno
));
14973 ** The implementation of a user-defined SQL function invoked by the
14974 ** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
14975 ** of the database being recovered.
14977 ** This function always takes a single integer argument. If the argument
14978 ** is zero, then the value returned is the number of pages in the db being
14979 ** recovered. If the argument is greater than zero, it is a page number.
14980 ** The value returned in this case is an SQL blob containing the data for
14981 ** the identified page of the db being recovered. e.g.
14983 ** SELECT getpage(0); -- return number of pages in db
14984 ** SELECT getpage(4); -- return page 4 of db as a blob of data
14986 static void recoverGetPage(
14987 sqlite3_context
*pCtx
,
14989 sqlite3_value
**apArg
14991 sqlite3_recover
*p
= (sqlite3_recover
*)sqlite3_user_data(pCtx
);
14992 i64 pgno
= sqlite3_value_int64(apArg
[0]);
14993 sqlite3_stmt
*pStmt
= 0;
14997 i64 nPg
= recoverPageCount(p
);
14998 sqlite3_result_int64(pCtx
, nPg
);
15001 if( p
->pGetPage
==0 ){
15002 pStmt
= p
->pGetPage
= recoverPreparePrintf(
15003 p
, p
->dbIn
, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p
->zDb
15005 }else if( p
->errCode
==SQLITE_OK
){
15006 pStmt
= p
->pGetPage
;
15010 sqlite3_bind_int64(pStmt
, 1, pgno
);
15011 if( SQLITE_ROW
==sqlite3_step(pStmt
) ){
15014 assert( p
->errCode
==SQLITE_OK
);
15015 aPg
= sqlite3_column_blob(pStmt
, 0);
15016 nPg
= sqlite3_column_bytes(pStmt
, 0);
15017 if( pgno
==1 && nPg
==p
->pgsz
&& 0==memcmp(p
->pPage1Cache
, aPg
, nPg
) ){
15018 aPg
= p
->pPage1Disk
;
15020 sqlite3_result_blob(pCtx
, aPg
, nPg
-p
->nReserve
, SQLITE_TRANSIENT
);
15022 recoverReset(p
, pStmt
);
15027 if( p
->zErrMsg
) sqlite3_result_error(pCtx
, p
->zErrMsg
, -1);
15028 sqlite3_result_error_code(pCtx
, p
->errCode
);
15033 ** Find a string that is not found anywhere in z[]. Return a pointer
15036 ** Try to use zA and zB first. If both of those are already found in z[]
15037 ** then make up some string and store it in the buffer zBuf.
15039 static const char *recoverUnusedString(
15040 const char *z
, /* Result must not appear anywhere in z */
15041 const char *zA
, const char *zB
, /* Try these first */
15042 char *zBuf
/* Space to store a generated string */
15045 if( strstr(z
, zA
)==0 ) return zA
;
15046 if( strstr(z
, zB
)==0 ) return zB
;
15048 sqlite3_snprintf(20,zBuf
,"(%s%u)", zA
, i
++);
15049 }while( strstr(z
,zBuf
)!=0 );
15054 ** Implementation of scalar SQL function "escape_crnl". The argument passed to
15055 ** this function is the output of built-in function quote(). If the first
15056 ** character of the input is "'", indicating that the value passed to quote()
15057 ** was a text value, then this function searches the input for "\n" and "\r"
15058 ** characters and adds a wrapper similar to the following:
15060 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
15062 ** Or, if the first character of the input is not "'", then a copy of the input
15065 static void recoverEscapeCrnl(
15066 sqlite3_context
*context
,
15068 sqlite3_value
**argv
15070 const char *zText
= (const char*)sqlite3_value_text(argv
[0]);
15072 if( zText
&& zText
[0]=='\'' ){
15073 int nText
= sqlite3_value_bytes(argv
[0]);
15077 const char *zNL
= 0;
15078 const char *zCR
= 0;
15082 for(i
=0; zText
[i
]; i
++){
15083 if( zNL
==0 && zText
[i
]=='\n' ){
15084 zNL
= recoverUnusedString(zText
, "\\n", "\\012", zBuf1
);
15085 nNL
= (int)strlen(zNL
);
15087 if( zCR
==0 && zText
[i
]=='\r' ){
15088 zCR
= recoverUnusedString(zText
, "\\r", "\\015", zBuf2
);
15089 nCR
= (int)strlen(zCR
);
15095 i64 nMax
= (nNL
> nCR
) ? nNL
: nCR
;
15096 i64 nAlloc
= nMax
* nText
+ (nMax
+64)*2;
15097 char *zOut
= (char*)sqlite3_malloc64(nAlloc
);
15099 sqlite3_result_error_nomem(context
);
15104 memcpy(&zOut
[iOut
], "replace(replace(", 16);
15107 memcpy(&zOut
[iOut
], "replace(", 8);
15110 for(i
=0; zText
[i
]; i
++){
15111 if( zText
[i
]=='\n' ){
15112 memcpy(&zOut
[iOut
], zNL
, nNL
);
15114 }else if( zText
[i
]=='\r' ){
15115 memcpy(&zOut
[iOut
], zCR
, nCR
);
15118 zOut
[iOut
] = zText
[i
];
15124 memcpy(&zOut
[iOut
], ",'", 2); iOut
+= 2;
15125 memcpy(&zOut
[iOut
], zNL
, nNL
); iOut
+= nNL
;
15126 memcpy(&zOut
[iOut
], "', char(10))", 12); iOut
+= 12;
15129 memcpy(&zOut
[iOut
], ",'", 2); iOut
+= 2;
15130 memcpy(&zOut
[iOut
], zCR
, nCR
); iOut
+= nCR
;
15131 memcpy(&zOut
[iOut
], "', char(13))", 12); iOut
+= 12;
15134 sqlite3_result_text(context
, zOut
, iOut
, SQLITE_TRANSIENT
);
15135 sqlite3_free(zOut
);
15140 sqlite3_result_value(context
, argv
[0]);
15144 ** This function is a no-op if recover handle p already contains an error
15145 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
15148 ** Otherwise, attempt to populate temporary table "recovery.schema" with the
15149 ** parts of the database schema that can be extracted from the input database.
15151 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
15152 ** and error message are left in the recover handle and a copy of the
15153 ** error code returned. It is not considered an error if part of all of
15154 ** the database schema cannot be recovered due to corruption.
15156 static int recoverCacheSchema(sqlite3_recover
*p
){
15157 return recoverExec(p
, p
->dbOut
,
15158 "WITH RECURSIVE pages(p) AS ("
15161 " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
15163 "INSERT INTO recovery.schema SELECT"
15164 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
15165 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
15166 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
15167 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
15168 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
15169 "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
15170 " SELECT p FROM pages"
15171 ") GROUP BY pgno, cell"
15176 ** If this recover handle is not in SQL callback mode (i.e. was not created
15177 ** using sqlite3_recover_init_sql()) of if an error has already occurred,
15178 ** this function is a no-op. Otherwise, issue a callback with SQL statement
15179 ** zSql as the parameter.
15181 ** If the callback returns non-zero, set the recover handle error code to
15182 ** the value returned (so that the caller will abandon processing).
15184 static void recoverSqlCallback(sqlite3_recover
*p
, const char *zSql
){
15185 if( p
->errCode
==SQLITE_OK
&& p
->xSql
){
15186 int res
= p
->xSql(p
->pSqlCtx
, zSql
);
15188 recoverError(p
, SQLITE_ERROR
, "callback returned an error - %d", res
);
15194 ** Transfer the following settings from the input database to the output
15198 ** + auto-vacuum settings,
15199 ** + database encoding,
15200 ** + user-version (PRAGMA user_version), and
15201 ** + application-id (PRAGMA application_id), and
15203 static void recoverTransferSettings(sqlite3_recover
*p
){
15204 const char *aPragma
[] = {
15213 /* Truncate the output database to 0 pages in size. This is done by
15214 ** opening a new, empty, temp db, then using the backup API to clobber
15215 ** any existing output db with a copy of it. */
15216 if( p
->errCode
==SQLITE_OK
){
15218 int rc
= sqlite3_open("", &db2
);
15219 if( rc
!=SQLITE_OK
){
15220 recoverDbError(p
, db2
);
15224 for(ii
=0; ii
<(int)(sizeof(aPragma
)/sizeof(aPragma
[0])); ii
++){
15225 const char *zPrag
= aPragma
[ii
];
15226 sqlite3_stmt
*p1
= 0;
15227 p1
= recoverPreparePrintf(p
, p
->dbIn
, "PRAGMA %Q.%s", p
->zDb
, zPrag
);
15228 if( p
->errCode
==SQLITE_OK
&& sqlite3_step(p1
)==SQLITE_ROW
){
15229 const char *zArg
= (const char*)sqlite3_column_text(p1
, 0);
15230 char *z2
= recoverMPrintf(p
, "PRAGMA %s = %Q", zPrag
, zArg
);
15231 recoverSqlCallback(p
, z2
);
15232 recoverExec(p
, db2
, z2
);
15235 recoverError(p
, SQLITE_NOMEM
, 0);
15238 recoverFinalize(p
, p1
);
15240 recoverExec(p
, db2
, "CREATE TABLE t1(a); DROP TABLE t1;");
15242 if( p
->errCode
==SQLITE_OK
){
15243 sqlite3
*db
= p
->dbOut
;
15244 sqlite3_backup
*pBackup
= sqlite3_backup_init(db
, "main", db2
, "main");
15246 sqlite3_backup_step(pBackup
, -1);
15247 p
->errCode
= sqlite3_backup_finish(pBackup
);
15249 recoverDbError(p
, db
);
15253 sqlite3_close(db2
);
15258 ** This function is a no-op if recover handle p already contains an error
15259 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
15262 ** Otherwise, an attempt is made to open the output database, attach
15263 ** and create the schema of the temporary database used to store
15264 ** intermediate data, and to register all required user functions and
15265 ** virtual table modules with the output handle.
15267 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
15268 ** and error message are left in the recover handle and a copy of the
15269 ** error code returned.
15271 static int recoverOpenOutput(sqlite3_recover
*p
){
15275 void (*xFunc
)(sqlite3_context
*,int,sqlite3_value
**);
15277 { "getpage", 1, recoverGetPage
},
15278 { "page_is_used", 1, recoverPageIsUsed
},
15279 { "read_i32", 2, recoverReadI32
},
15280 { "escape_crnl", 1, recoverEscapeCrnl
},
15283 const int flags
= SQLITE_OPEN_URI
|SQLITE_OPEN_CREATE
|SQLITE_OPEN_READWRITE
;
15284 sqlite3
*db
= 0; /* New database handle */
15285 int ii
; /* For iterating through aFunc[] */
15287 assert( p
->dbOut
==0 );
15289 if( sqlite3_open_v2(p
->zUri
, &db
, flags
, 0) ){
15290 recoverDbError(p
, db
);
15293 /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
15294 ** These two are registered with the output database handle - this
15295 ** module depends on the input handle supporting the sqlite_dbpage
15296 ** virtual table only. */
15297 if( p
->errCode
==SQLITE_OK
){
15298 p
->errCode
= sqlite3_dbdata_init(db
, 0, 0);
15301 /* Register the custom user-functions with the output handle. */
15303 p
->errCode
==SQLITE_OK
&& ii
<(int)(sizeof(aFunc
)/sizeof(aFunc
[0]));
15305 p
->errCode
= sqlite3_create_function(db
, aFunc
[ii
].zName
,
15306 aFunc
[ii
].nArg
, SQLITE_UTF8
, (void*)p
, aFunc
[ii
].xFunc
, 0, 0
15315 ** Attach the auxiliary database 'recovery' to the output database handle.
15316 ** This temporary database is used during the recovery process and then
15319 static void recoverOpenRecovery(sqlite3_recover
*p
){
15320 char *zSql
= recoverMPrintf(p
, "ATTACH %Q AS recovery;", p
->zStateDb
);
15321 recoverExec(p
, p
->dbOut
, zSql
);
15322 recoverExec(p
, p
->dbOut
,
15323 "PRAGMA writable_schema = 1;"
15324 "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
15325 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
15327 sqlite3_free(zSql
);
15332 ** This function is a no-op if recover handle p already contains an error
15333 ** (if p->errCode!=SQLITE_OK).
15335 ** Otherwise, argument zName must be the name of a table that has just been
15336 ** created in the output database. This function queries the output db
15337 ** for the schema of said table, and creates a RecoverTable object to
15338 ** store the schema in memory. The new RecoverTable object is linked into
15339 ** the list at sqlite3_recover.pTblList.
15341 ** Parameter iRoot must be the root page of table zName in the INPUT
15344 static void recoverAddTable(
15345 sqlite3_recover
*p
,
15346 const char *zName
, /* Name of table created in output db */
15347 i64 iRoot
/* Root page of same table in INPUT db */
15349 sqlite3_stmt
*pStmt
= recoverPreparePrintf(p
, p
->dbOut
,
15350 "PRAGMA table_xinfo(%Q)", zName
15356 RecoverTable
*pNew
= 0;
15358 int nName
= recoverStrlen(zName
);
15360 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
15362 nByte
+= (sqlite3_column_bytes(pStmt
, 1)+1);
15364 nByte
+= sizeof(RecoverTable
) + nCol
*sizeof(RecoverColumn
) + nName
+1;
15365 recoverReset(p
, pStmt
);
15367 pNew
= recoverMalloc(p
, nByte
);
15372 pNew
->aCol
= (RecoverColumn
*)&pNew
[1];
15373 pNew
->zTab
= csr
= (char*)&pNew
->aCol
[nCol
];
15375 pNew
->iRoot
= iRoot
;
15376 memcpy(csr
, zName
, nName
);
15379 for(i
=0; sqlite3_step(pStmt
)==SQLITE_ROW
; i
++){
15380 int iPKF
= sqlite3_column_int(pStmt
, 5);
15381 int n
= sqlite3_column_bytes(pStmt
, 1);
15382 const char *z
= (const char*)sqlite3_column_text(pStmt
, 1);
15383 const char *zType
= (const char*)sqlite3_column_text(pStmt
, 2);
15384 int eHidden
= sqlite3_column_int(pStmt
, 6);
15386 if( iPk
==-1 && iPKF
==1 && !sqlite3_stricmp("integer", zType
) ) iPk
= i
;
15387 if( iPKF
>1 ) iPk
= -2;
15388 pNew
->aCol
[i
].zCol
= csr
;
15389 pNew
->aCol
[i
].eHidden
= eHidden
;
15390 if( eHidden
==RECOVER_EHIDDEN_VIRTUAL
){
15391 pNew
->aCol
[i
].iField
= -1;
15393 pNew
->aCol
[i
].iField
= iField
++;
15395 if( eHidden
!=RECOVER_EHIDDEN_VIRTUAL
15396 && eHidden
!=RECOVER_EHIDDEN_STORED
15398 pNew
->aCol
[i
].iBind
= iBind
++;
15404 pNew
->pNext
= p
->pTblList
;
15405 p
->pTblList
= pNew
;
15409 recoverFinalize(p
, pStmt
);
15411 pStmt
= recoverPreparePrintf(p
, p
->dbOut
, "PRAGMA index_xinfo(%Q)", zName
);
15412 while( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
15413 int iField
= sqlite3_column_int(pStmt
, 0);
15414 int iCol
= sqlite3_column_int(pStmt
, 1);
15416 assert( iCol
<pNew
->nCol
);
15417 pNew
->aCol
[iCol
].iField
= iField
;
15422 recoverFinalize(p
, pStmt
);
15424 if( p
->errCode
==SQLITE_OK
){
15426 pNew
->aCol
[iPk
].bIPK
= 1;
15427 }else if( pNew
->bIntkey
){
15428 pNew
->iRowidBind
= iBind
++;
15435 ** This function is called after recoverCacheSchema() has cached those parts
15436 ** of the input database schema that could be recovered in temporary table
15437 ** "recovery.schema". This function creates in the output database copies
15438 ** of all parts of that schema that must be created before the tables can
15439 ** be populated. Specifically, this means:
15441 ** * all tables that are not VIRTUAL, and
15442 ** * UNIQUE indexes.
15444 ** If the recovery handle uses SQL callbacks, then callbacks containing
15445 ** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
15447 ** Additionally, records are added to the sqlite_schema table of the
15448 ** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
15449 ** records are written directly to sqlite_schema, not actually executed.
15450 ** If the handle is in SQL callback mode, then callbacks are invoked
15451 ** with equivalent SQL statements.
15453 static int recoverWriteSchema1(sqlite3_recover
*p
){
15454 sqlite3_stmt
*pSelect
= 0;
15455 sqlite3_stmt
*pTblname
= 0;
15457 pSelect
= recoverPrepare(p
, p
->dbOut
,
15458 "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
15459 " SELECT rootpage, name, sql, "
15461 " sql LIKE 'create virtual%',"
15462 " (type='index' AND (sql LIKE '%unique%' OR ?1))"
15463 " FROM recovery.schema"
15465 "SELECT rootpage, tbl, isVirtual, name, sql"
15467 " WHERE tbl OR isIndex"
15468 " ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
15471 pTblname
= recoverPrepare(p
, p
->dbOut
,
15472 "SELECT name FROM sqlite_schema "
15473 "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
15477 sqlite3_bind_int(pSelect
, 1, p
->bSlowIndexes
);
15478 while( sqlite3_step(pSelect
)==SQLITE_ROW
){
15479 i64 iRoot
= sqlite3_column_int64(pSelect
, 0);
15480 int bTable
= sqlite3_column_int(pSelect
, 1);
15481 int bVirtual
= sqlite3_column_int(pSelect
, 2);
15482 const char *zName
= (const char*)sqlite3_column_text(pSelect
, 3);
15483 const char *zSql
= (const char*)sqlite3_column_text(pSelect
, 4);
15485 int rc
= SQLITE_OK
;
15488 zSql
= (const char*)(zFree
= recoverMPrintf(p
,
15489 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
15493 rc
= sqlite3_exec(p
->dbOut
, zSql
, 0, 0, 0);
15494 if( rc
==SQLITE_OK
){
15495 recoverSqlCallback(p
, zSql
);
15496 if( bTable
&& !bVirtual
){
15497 if( SQLITE_ROW
==sqlite3_step(pTblname
) ){
15498 const char *zTbl
= (const char*)sqlite3_column_text(pTblname
, 0);
15499 recoverAddTable(p
, zTbl
, iRoot
);
15501 recoverReset(p
, pTblname
);
15503 }else if( rc
!=SQLITE_ERROR
){
15504 recoverDbError(p
, p
->dbOut
);
15506 sqlite3_free(zFree
);
15509 recoverFinalize(p
, pSelect
);
15510 recoverFinalize(p
, pTblname
);
15516 ** This function is called after the output database has been populated. It
15517 ** adds all recovered schema elements that were not created in the output
15518 ** database by recoverWriteSchema1() - everything except for tables and
15519 ** UNIQUE indexes. Specifically:
15523 ** * non-UNIQUE indexes.
15525 ** If the recover handle is in SQL callback mode, then equivalent callbacks
15526 ** are issued to create the schema elements.
15528 static int recoverWriteSchema2(sqlite3_recover
*p
){
15529 sqlite3_stmt
*pSelect
= 0;
15531 pSelect
= recoverPrepare(p
, p
->dbOut
,
15533 "SELECT rootpage, sql FROM recovery.schema "
15534 " WHERE type!='table' AND type!='index'"
15536 "SELECT rootpage, sql FROM recovery.schema "
15537 " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
15541 while( sqlite3_step(pSelect
)==SQLITE_ROW
){
15542 const char *zSql
= (const char*)sqlite3_column_text(pSelect
, 1);
15543 int rc
= sqlite3_exec(p
->dbOut
, zSql
, 0, 0, 0);
15544 if( rc
==SQLITE_OK
){
15545 recoverSqlCallback(p
, zSql
);
15546 }else if( rc
!=SQLITE_ERROR
){
15547 recoverDbError(p
, p
->dbOut
);
15551 recoverFinalize(p
, pSelect
);
15557 ** This function is a no-op if recover handle p already contains an error
15558 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
15560 ** Otherwise, if the recover handle is configured to create an output
15561 ** database (was created by sqlite3_recover_init()), then this function
15562 ** prepares and returns an SQL statement to INSERT a new record into table
15563 ** pTab, assuming the first nField fields of a record extracted from disk
15566 ** For example, if table pTab is:
15568 ** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
15570 ** And nField is 4, then the SQL statement prepared and returned is:
15572 ** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
15574 ** In this case even though 4 values were extracted from the input db,
15575 ** only 3 are written to the output, as the generated STORED column
15576 ** cannot be written.
15578 ** If the recover handle is in SQL callback mode, then the SQL statement
15579 ** prepared is such that evaluating it returns a single row containing
15580 ** a single text value - itself an SQL statement similar to the above,
15581 ** except with SQL literals in place of the variables. For example:
15583 ** SELECT 'INSERT INTO (a, c, d) VALUES ('
15584 ** || quote(?1) || ', '
15585 ** || quote(?2) || ', '
15586 ** || quote(?3) || ')';
15588 ** In either case, it is the responsibility of the caller to eventually
15589 ** free the statement handle using sqlite3_finalize().
15591 static sqlite3_stmt
*recoverInsertStmt(
15592 sqlite3_recover
*p
,
15593 RecoverTable
*pTab
,
15596 sqlite3_stmt
*pRet
= 0;
15597 const char *zSep
= "";
15598 const char *zSqlSep
= "";
15603 int bSql
= p
->xSql
? 1 : 0;
15605 if( nField
<=0 ) return 0;
15607 assert( nField
<=pTab
->nCol
);
15609 zSql
= recoverMPrintf(p
, "INSERT OR IGNORE INTO %Q(", pTab
->zTab
);
15611 if( pTab
->iRowidBind
){
15612 assert( pTab
->bIntkey
);
15613 zSql
= recoverMPrintf(p
, "%z_rowid_", zSql
);
15615 zBind
= recoverMPrintf(p
, "%zquote(?%d)", zBind
, pTab
->iRowidBind
);
15617 zBind
= recoverMPrintf(p
, "%z?%d", zBind
, pTab
->iRowidBind
);
15619 zSqlSep
= "||', '||";
15623 for(ii
=0; ii
<nField
; ii
++){
15624 int eHidden
= pTab
->aCol
[ii
].eHidden
;
15625 if( eHidden
!=RECOVER_EHIDDEN_VIRTUAL
15626 && eHidden
!=RECOVER_EHIDDEN_STORED
15628 assert( pTab
->aCol
[ii
].iField
>=0 && pTab
->aCol
[ii
].iBind
>=1 );
15629 zSql
= recoverMPrintf(p
, "%z%s%Q", zSql
, zSep
, pTab
->aCol
[ii
].zCol
);
15632 zBind
= recoverMPrintf(p
,
15633 "%z%sescape_crnl(quote(?%d))", zBind
, zSqlSep
, pTab
->aCol
[ii
].iBind
15635 zSqlSep
= "||', '||";
15637 zBind
= recoverMPrintf(p
, "%z%s?%d", zBind
, zSep
, pTab
->aCol
[ii
].iBind
);
15644 zFinal
= recoverMPrintf(p
, "SELECT %Q || ') VALUES (' || %s || ')'",
15648 zFinal
= recoverMPrintf(p
, "%s) VALUES (%s)", zSql
, zBind
);
15651 pRet
= recoverPrepare(p
, p
->dbOut
, zFinal
);
15652 sqlite3_free(zSql
);
15653 sqlite3_free(zBind
);
15654 sqlite3_free(zFinal
);
15661 ** Search the list of RecoverTable objects at p->pTblList for one that
15662 ** has root page iRoot in the input database. If such an object is found,
15663 ** return a pointer to it. Otherwise, return NULL.
15665 static RecoverTable
*recoverFindTable(sqlite3_recover
*p
, u32 iRoot
){
15666 RecoverTable
*pRet
= 0;
15667 for(pRet
=p
->pTblList
; pRet
&& pRet
->iRoot
!=iRoot
; pRet
=pRet
->pNext
);
15672 ** This function attempts to create a lost and found table within the
15673 ** output db. If successful, it returns a pointer to a buffer containing
15674 ** the name of the new table. It is the responsibility of the caller to
15675 ** eventually free this buffer using sqlite3_free().
15677 ** If an error occurs, NULL is returned and an error code and error
15678 ** message left in the recover handle.
15680 static char *recoverLostAndFoundCreate(
15681 sqlite3_recover
*p
, /* Recover object */
15682 int nField
/* Number of column fields in new table */
15685 sqlite3_stmt
*pProbe
= 0;
15688 pProbe
= recoverPrepare(p
, p
->dbOut
,
15689 "SELECT 1 FROM sqlite_schema WHERE name=?"
15691 for(ii
=-1; zTbl
==0 && p
->errCode
==SQLITE_OK
&& ii
<1000; ii
++){
15694 zTbl
= recoverMPrintf(p
, "%s", p
->zLostAndFound
);
15696 zTbl
= recoverMPrintf(p
, "%s_%d", p
->zLostAndFound
, ii
);
15699 if( p
->errCode
==SQLITE_OK
){
15700 sqlite3_bind_text(pProbe
, 1, zTbl
, -1, SQLITE_STATIC
);
15701 if( SQLITE_ROW
==sqlite3_step(pProbe
) ){
15704 recoverReset(p
, pProbe
);
15708 sqlite3_clear_bindings(pProbe
);
15709 sqlite3_free(zTbl
);
15713 recoverFinalize(p
, pProbe
);
15716 const char *zSep
= 0;
15720 zSep
= "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
15721 for(ii
=0; p
->errCode
==SQLITE_OK
&& ii
<nField
; ii
++){
15722 zField
= recoverMPrintf(p
, "%z%sc%d", zField
, zSep
, ii
);
15726 zSql
= recoverMPrintf(p
, "CREATE TABLE %s(%s)", zTbl
, zField
);
15727 sqlite3_free(zField
);
15729 recoverExec(p
, p
->dbOut
, zSql
);
15730 recoverSqlCallback(p
, zSql
);
15731 sqlite3_free(zSql
);
15732 }else if( p
->errCode
==SQLITE_OK
){
15734 p
, SQLITE_ERROR
, "failed to create %s output table", p
->zLostAndFound
15742 ** Synthesize and prepare an INSERT statement to write to the lost_and_found
15743 ** table in the output database. The name of the table is zTab, and it has
15744 ** nField c* fields.
15746 static sqlite3_stmt
*recoverLostAndFoundInsert(
15747 sqlite3_recover
*p
,
15751 int nTotal
= nField
+ 4;
15754 sqlite3_stmt
*pRet
= 0;
15757 for(ii
=0; ii
<nTotal
; ii
++){
15758 zBind
= recoverMPrintf(p
, "%z%s?", zBind
, zBind
?", ":"", ii
);
15760 pRet
= recoverPreparePrintf(
15761 p
, p
->dbOut
, "INSERT INTO %s VALUES(%s)", zTab
, zBind
15764 const char *zSep
= "";
15765 for(ii
=0; ii
<nTotal
; ii
++){
15766 zBind
= recoverMPrintf(p
, "%z%squote(?)", zBind
, zSep
);
15767 zSep
= "|| ', ' ||";
15769 pRet
= recoverPreparePrintf(
15770 p
, p
->dbOut
, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab
, zBind
15774 sqlite3_free(zBind
);
15779 ** Input database page iPg contains data that will be written to the
15780 ** lost-and-found table of the output database. This function attempts
15781 ** to identify the root page of the tree that page iPg belonged to.
15782 ** If successful, it sets output variable (*piRoot) to the page number
15783 ** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
15784 ** an SQLite error code is returned and the final value of *piRoot
15787 static int recoverLostAndFoundFindRoot(
15788 sqlite3_recover
*p
,
15792 RecoverStateLAF
*pLaf
= &p
->laf
;
15794 if( pLaf
->pFindRoot
==0 ){
15795 pLaf
->pFindRoot
= recoverPrepare(p
, p
->dbOut
,
15796 "WITH RECURSIVE p(pgno) AS ("
15799 " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
15801 "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
15802 " AND m.parent IS NULL"
15805 if( p
->errCode
==SQLITE_OK
){
15806 sqlite3_bind_int64(pLaf
->pFindRoot
, 1, iPg
);
15807 if( sqlite3_step(pLaf
->pFindRoot
)==SQLITE_ROW
){
15808 *piRoot
= sqlite3_column_int64(pLaf
->pFindRoot
, 0);
15812 recoverReset(p
, pLaf
->pFindRoot
);
15818 ** Recover data from page iPage of the input database and write it to
15819 ** the lost-and-found table in the output database.
15821 static void recoverLostAndFoundOnePage(sqlite3_recover
*p
, i64 iPage
){
15822 RecoverStateLAF
*pLaf
= &p
->laf
;
15823 sqlite3_value
**apVal
= pLaf
->apVal
;
15824 sqlite3_stmt
*pPageData
= pLaf
->pPageData
;
15825 sqlite3_stmt
*pInsert
= pLaf
->pInsert
;
15830 int bHaveRowid
= 0;
15834 if( recoverLostAndFoundFindRoot(p
, iPage
, &iRoot
) ) return;
15835 sqlite3_bind_int64(pPageData
, 1, iPage
);
15836 while( p
->errCode
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pPageData
) ){
15837 int iCell
= sqlite3_column_int64(pPageData
, 0);
15838 int iField
= sqlite3_column_int64(pPageData
, 1);
15840 if( iPrevCell
!=iCell
&& nVal
>=0 ){
15841 /* Insert the new row */
15842 sqlite3_bind_int64(pInsert
, 1, iRoot
); /* rootpgno */
15843 sqlite3_bind_int64(pInsert
, 2, iPage
); /* pgno */
15844 sqlite3_bind_int(pInsert
, 3, nVal
); /* nfield */
15846 sqlite3_bind_int64(pInsert
, 4, iRowid
); /* id */
15848 for(ii
=0; ii
<nVal
; ii
++){
15849 recoverBindValue(p
, pInsert
, 5+ii
, apVal
[ii
]);
15851 if( sqlite3_step(pInsert
)==SQLITE_ROW
){
15852 recoverSqlCallback(p
, (const char*)sqlite3_column_text(pInsert
, 0));
15854 recoverReset(p
, pInsert
);
15856 /* Discard the accumulated row data */
15857 for(ii
=0; ii
<nVal
; ii
++){
15858 sqlite3_value_free(apVal
[ii
]);
15861 sqlite3_clear_bindings(pInsert
);
15866 if( iCell
<0 ) break;
15869 assert( nVal
==-1 );
15870 iRowid
= sqlite3_column_int64(pPageData
, 2);
15873 }else if( iField
<pLaf
->nMaxField
){
15874 sqlite3_value
*pVal
= sqlite3_column_value(pPageData
, 2);
15875 apVal
[iField
] = sqlite3_value_dup(pVal
);
15876 assert( iField
==nVal
|| (nVal
==-1 && iField
==0) );
15878 if( apVal
[iField
]==0 ){
15879 recoverError(p
, SQLITE_NOMEM
, 0);
15885 recoverReset(p
, pPageData
);
15887 for(ii
=0; ii
<nVal
; ii
++){
15888 sqlite3_value_free(apVal
[ii
]);
15894 ** Perform one step (sqlite3_recover_step()) of work for the connection
15895 ** passed as the only argument, which is guaranteed to be in
15896 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
15897 ** table of the output database is populated with recovered data that can
15898 ** not be assigned to any recovered schema object.
15900 static int recoverLostAndFound3Step(sqlite3_recover
*p
){
15901 RecoverStateLAF
*pLaf
= &p
->laf
;
15902 if( p
->errCode
==SQLITE_OK
){
15903 if( pLaf
->pInsert
==0 ){
15904 return SQLITE_DONE
;
15906 if( p
->errCode
==SQLITE_OK
){
15907 int res
= sqlite3_step(pLaf
->pAllPage
);
15908 if( res
==SQLITE_ROW
){
15909 i64 iPage
= sqlite3_column_int64(pLaf
->pAllPage
, 0);
15910 if( recoverBitmapQuery(pLaf
->pUsed
, iPage
)==0 ){
15911 recoverLostAndFoundOnePage(p
, iPage
);
15914 recoverReset(p
, pLaf
->pAllPage
);
15915 return SQLITE_DONE
;
15924 ** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
15925 ** state - during which the lost-and-found table of the output database
15926 ** is populated with recovered data that can not be assigned to any
15927 ** recovered schema object.
15929 static void recoverLostAndFound3Init(sqlite3_recover
*p
){
15930 RecoverStateLAF
*pLaf
= &p
->laf
;
15932 if( pLaf
->nMaxField
>0 ){
15933 char *zTab
= 0; /* Name of lost_and_found table */
15935 zTab
= recoverLostAndFoundCreate(p
, pLaf
->nMaxField
);
15936 pLaf
->pInsert
= recoverLostAndFoundInsert(p
, zTab
, pLaf
->nMaxField
);
15937 sqlite3_free(zTab
);
15939 pLaf
->pAllPage
= recoverPreparePrintf(p
, p
->dbOut
,
15940 "WITH RECURSIVE seq(ii) AS ("
15941 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
15943 "SELECT ii FROM seq" , p
->laf
.nPg
15945 pLaf
->pPageData
= recoverPrepare(p
, p
->dbOut
,
15946 "SELECT cell, field, value "
15947 "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
15949 "SELECT -1, -1, -1"
15952 pLaf
->apVal
= (sqlite3_value
**)recoverMalloc(p
,
15953 pLaf
->nMaxField
*sizeof(sqlite3_value
*)
15959 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
15960 ** tables recovered from the schema of the input database are populated with
15963 static int recoverWriteDataInit(sqlite3_recover
*p
){
15964 RecoverStateW1
*p1
= &p
->w1
;
15965 RecoverTable
*pTbl
= 0;
15968 /* Figure out the maximum number of columns for any table in the schema */
15969 assert( p1
->nMax
==0 );
15970 for(pTbl
=p
->pTblList
; pTbl
; pTbl
=pTbl
->pNext
){
15971 if( pTbl
->nCol
>p1
->nMax
) p1
->nMax
= pTbl
->nCol
;
15974 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
15975 ** that will be written to the output database in a single row. */
15976 nByte
= sizeof(sqlite3_value
*) * (p1
->nMax
+1);
15977 p1
->apVal
= (sqlite3_value
**)recoverMalloc(p
, nByte
);
15978 if( p1
->apVal
==0 ) return p
->errCode
;
15980 /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
15981 ** to loop through cells that appear to belong to a single table (pSel). */
15982 p1
->pTbls
= recoverPrepare(p
, p
->dbOut
,
15983 "SELECT rootpage FROM recovery.schema "
15984 " WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
15985 " ORDER BY (tbl_name='sqlite_sequence') ASC"
15987 p1
->pSel
= recoverPrepare(p
, p
->dbOut
,
15988 "WITH RECURSIVE pages(page) AS ("
15991 " SELECT child FROM sqlite_dbptr('getpage()'), pages "
15994 "SELECT page, cell, field, value "
15995 "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
15997 "SELECT 0, 0, 0, 0"
16004 ** Clean up resources allocated by recoverWriteDataInit() (stuff in
16005 ** sqlite3_recover.w1).
16007 static void recoverWriteDataCleanup(sqlite3_recover
*p
){
16008 RecoverStateW1
*p1
= &p
->w1
;
16010 for(ii
=0; ii
<p1
->nVal
; ii
++){
16011 sqlite3_value_free(p1
->apVal
[ii
]);
16013 sqlite3_free(p1
->apVal
);
16014 recoverFinalize(p
, p1
->pInsert
);
16015 recoverFinalize(p
, p1
->pTbls
);
16016 recoverFinalize(p
, p1
->pSel
);
16017 memset(p1
, 0, sizeof(*p1
));
16021 ** Perform one step (sqlite3_recover_step()) of work for the connection
16022 ** passed as the only argument, which is guaranteed to be in
16023 ** RECOVER_STATE_WRITING state - during which tables recovered from the
16024 ** schema of the input database are populated with recovered data.
16026 static int recoverWriteDataStep(sqlite3_recover
*p
){
16027 RecoverStateW1
*p1
= &p
->w1
;
16028 sqlite3_stmt
*pSel
= p1
->pSel
;
16029 sqlite3_value
**apVal
= p1
->apVal
;
16031 if( p
->errCode
==SQLITE_OK
&& p1
->pTab
==0 ){
16032 if( sqlite3_step(p1
->pTbls
)==SQLITE_ROW
){
16033 i64 iRoot
= sqlite3_column_int64(p1
->pTbls
, 0);
16034 p1
->pTab
= recoverFindTable(p
, iRoot
);
16036 recoverFinalize(p
, p1
->pInsert
);
16039 /* If this table is unknown, return early. The caller will invoke this
16040 ** function again and it will move on to the next table. */
16041 if( p1
->pTab
==0 ) return p
->errCode
;
16043 /* If this is the sqlite_sequence table, delete any rows added by
16044 ** earlier INSERT statements on tables with AUTOINCREMENT primary
16045 ** keys before recovering its contents. The p1->pTbls SELECT statement
16046 ** is rigged to deliver "sqlite_sequence" last of all, so we don't
16047 ** worry about it being modified after it is recovered. */
16048 if( sqlite3_stricmp("sqlite_sequence", p1
->pTab
->zTab
)==0 ){
16049 recoverExec(p
, p
->dbOut
, "DELETE FROM sqlite_sequence");
16050 recoverSqlCallback(p
, "DELETE FROM sqlite_sequence");
16053 /* Bind the root page of this table within the original database to
16054 ** SELECT statement p1->pSel. The SELECT statement will then iterate
16055 ** through cells that look like they belong to table pTab. */
16056 sqlite3_bind_int64(pSel
, 1, iRoot
);
16059 p1
->bHaveRowid
= 0;
16060 p1
->iPrevPage
= -1;
16061 p1
->iPrevCell
= -1;
16063 return SQLITE_DONE
;
16066 assert( p
->errCode
!=SQLITE_OK
|| p1
->pTab
);
16068 if( p
->errCode
==SQLITE_OK
&& sqlite3_step(pSel
)==SQLITE_ROW
){
16069 RecoverTable
*pTab
= p1
->pTab
;
16071 i64 iPage
= sqlite3_column_int64(pSel
, 0);
16072 int iCell
= sqlite3_column_int(pSel
, 1);
16073 int iField
= sqlite3_column_int(pSel
, 2);
16074 sqlite3_value
*pVal
= sqlite3_column_value(pSel
, 3);
16075 int bNewCell
= (p1
->iPrevPage
!=iPage
|| p1
->iPrevCell
!=iCell
);
16077 assert( bNewCell
==0 || (iField
==-1 || iField
==0) );
16078 assert( bNewCell
|| iField
==p1
->nVal
|| p1
->nVal
==pTab
->nCol
);
16083 if( p1
->pInsert
==0 || p1
->nVal
!=p1
->nInsert
){
16084 recoverFinalize(p
, p1
->pInsert
);
16085 p1
->pInsert
= recoverInsertStmt(p
, pTab
, p1
->nVal
);
16086 p1
->nInsert
= p1
->nVal
;
16089 sqlite3_stmt
*pInsert
= p1
->pInsert
;
16090 for(ii
=0; ii
<pTab
->nCol
; ii
++){
16091 RecoverColumn
*pCol
= &pTab
->aCol
[ii
];
16092 int iBind
= pCol
->iBind
;
16095 sqlite3_bind_int64(pInsert
, iBind
, p1
->iRowid
);
16096 }else if( pCol
->iField
<p1
->nVal
){
16097 recoverBindValue(p
, pInsert
, iBind
, apVal
[pCol
->iField
]);
16101 if( p
->bRecoverRowid
&& pTab
->iRowidBind
>0 && p1
->bHaveRowid
){
16102 sqlite3_bind_int64(pInsert
, pTab
->iRowidBind
, p1
->iRowid
);
16104 if( SQLITE_ROW
==sqlite3_step(pInsert
) ){
16105 const char *z
= (const char*)sqlite3_column_text(pInsert
, 0);
16106 recoverSqlCallback(p
, z
);
16108 recoverReset(p
, pInsert
);
16109 assert( p
->errCode
|| pInsert
);
16110 if( pInsert
) sqlite3_clear_bindings(pInsert
);
16114 for(ii
=0; ii
<p1
->nVal
; ii
++){
16115 sqlite3_value_free(apVal
[ii
]);
16119 p1
->bHaveRowid
= 0;
16124 p1
->iRowid
= sqlite3_column_int64(pSel
, 3);
16125 assert( p1
->nVal
==-1 );
16127 p1
->bHaveRowid
= 1;
16128 }else if( iField
<pTab
->nCol
){
16129 assert( apVal
[iField
]==0 );
16130 apVal
[iField
] = sqlite3_value_dup( pVal
);
16131 if( apVal
[iField
]==0 ){
16132 recoverError(p
, SQLITE_NOMEM
, 0);
16134 p1
->nVal
= iField
+1;
16136 p1
->iPrevCell
= iCell
;
16137 p1
->iPrevPage
= iPage
;
16140 recoverReset(p
, pSel
);
16148 ** Initialize resources required by sqlite3_recover_step() in
16149 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
16150 ** already allocated to a recovered schema element is determined.
16152 static void recoverLostAndFound1Init(sqlite3_recover
*p
){
16153 RecoverStateLAF
*pLaf
= &p
->laf
;
16154 sqlite3_stmt
*pStmt
= 0;
16156 assert( p
->laf
.pUsed
==0 );
16157 pLaf
->nPg
= recoverPageCount(p
);
16158 pLaf
->pUsed
= recoverBitmapAlloc(p
, pLaf
->nPg
);
16160 /* Prepare a statement to iterate through all pages that are part of any tree
16161 ** in the recoverable part of the input database schema to the bitmap. And,
16162 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
16164 pStmt
= recoverPrepare(
16166 "WITH trunk(pgno) AS ("
16167 " SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
16169 " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
16171 "trunkdata(pgno, data) AS ("
16172 " SELECT pgno, getpage(pgno) FROM trunk"
16174 "freelist(data, n, freepgno) AS ("
16175 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
16177 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
16181 " SELECT 1 UNION ALL"
16182 " SELECT rootpage FROM recovery.schema WHERE rootpage>0"
16185 " SELECT r FROM roots"
16187 " SELECT child FROM sqlite_dbptr('getpage()'), used "
16190 "SELECT page FROM used"
16192 "SELECT freepgno FROM freelist WHERE NOT ?"
16194 if( pStmt
) sqlite3_bind_int(pStmt
, 1, p
->bFreelistCorrupt
);
16195 pLaf
->pUsedPages
= pStmt
;
16199 ** Perform one step (sqlite3_recover_step()) of work for the connection
16200 ** passed as the only argument, which is guaranteed to be in
16201 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
16202 ** already allocated to a recovered schema element is determined.
16204 static int recoverLostAndFound1Step(sqlite3_recover
*p
){
16205 RecoverStateLAF
*pLaf
= &p
->laf
;
16206 int rc
= p
->errCode
;
16207 if( rc
==SQLITE_OK
){
16208 rc
= sqlite3_step(pLaf
->pUsedPages
);
16209 if( rc
==SQLITE_ROW
){
16210 i64 iPg
= sqlite3_column_int64(pLaf
->pUsedPages
, 0);
16211 recoverBitmapSet(pLaf
->pUsed
, iPg
);
16214 recoverFinalize(p
, pLaf
->pUsedPages
);
16215 pLaf
->pUsedPages
= 0;
16222 ** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
16223 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
16224 ** are sorted into sets that likely belonged to the same database tree.
16226 static void recoverLostAndFound2Init(sqlite3_recover
*p
){
16227 RecoverStateLAF
*pLaf
= &p
->laf
;
16229 assert( p
->laf
.pAllAndParent
==0 );
16230 assert( p
->laf
.pMapInsert
==0 );
16231 assert( p
->laf
.pMaxField
==0 );
16232 assert( p
->laf
.nMaxField
==0 );
16234 pLaf
->pMapInsert
= recoverPrepare(p
, p
->dbOut
,
16235 "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
16237 pLaf
->pAllAndParent
= recoverPreparePrintf(p
, p
->dbOut
,
16238 "WITH RECURSIVE seq(ii) AS ("
16239 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
16241 "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
16243 "SELECT NULL, ii FROM seq", p
->laf
.nPg
16245 pLaf
->pMaxField
= recoverPreparePrintf(p
, p
->dbOut
,
16246 "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
16251 ** Perform one step (sqlite3_recover_step()) of work for the connection
16252 ** passed as the only argument, which is guaranteed to be in
16253 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
16254 ** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
16255 ** to the same database tree.
16257 static int recoverLostAndFound2Step(sqlite3_recover
*p
){
16258 RecoverStateLAF
*pLaf
= &p
->laf
;
16259 if( p
->errCode
==SQLITE_OK
){
16260 int res
= sqlite3_step(pLaf
->pAllAndParent
);
16261 if( res
==SQLITE_ROW
){
16262 i64 iChild
= sqlite3_column_int(pLaf
->pAllAndParent
, 1);
16263 if( recoverBitmapQuery(pLaf
->pUsed
, iChild
)==0 ){
16264 sqlite3_bind_int64(pLaf
->pMapInsert
, 1, iChild
);
16265 sqlite3_bind_value(pLaf
->pMapInsert
, 2,
16266 sqlite3_column_value(pLaf
->pAllAndParent
, 0)
16268 sqlite3_step(pLaf
->pMapInsert
);
16269 recoverReset(p
, pLaf
->pMapInsert
);
16270 sqlite3_bind_int64(pLaf
->pMaxField
, 1, iChild
);
16271 if( SQLITE_ROW
==sqlite3_step(pLaf
->pMaxField
) ){
16272 int nMax
= sqlite3_column_int(pLaf
->pMaxField
, 0);
16273 if( nMax
>pLaf
->nMaxField
) pLaf
->nMaxField
= nMax
;
16275 recoverReset(p
, pLaf
->pMaxField
);
16278 recoverFinalize(p
, pLaf
->pAllAndParent
);
16279 pLaf
->pAllAndParent
=0;
16280 return SQLITE_DONE
;
16287 ** Free all resources allocated as part of sqlite3_recover_step() calls
16288 ** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
16290 static void recoverLostAndFoundCleanup(sqlite3_recover
*p
){
16291 recoverBitmapFree(p
->laf
.pUsed
);
16293 sqlite3_finalize(p
->laf
.pUsedPages
);
16294 sqlite3_finalize(p
->laf
.pAllAndParent
);
16295 sqlite3_finalize(p
->laf
.pMapInsert
);
16296 sqlite3_finalize(p
->laf
.pMaxField
);
16297 sqlite3_finalize(p
->laf
.pFindRoot
);
16298 sqlite3_finalize(p
->laf
.pInsert
);
16299 sqlite3_finalize(p
->laf
.pAllPage
);
16300 sqlite3_finalize(p
->laf
.pPageData
);
16301 p
->laf
.pUsedPages
= 0;
16302 p
->laf
.pAllAndParent
= 0;
16303 p
->laf
.pMapInsert
= 0;
16304 p
->laf
.pMaxField
= 0;
16305 p
->laf
.pFindRoot
= 0;
16306 p
->laf
.pInsert
= 0;
16307 p
->laf
.pAllPage
= 0;
16308 p
->laf
.pPageData
= 0;
16309 sqlite3_free(p
->laf
.apVal
);
16314 ** Free all resources allocated as part of sqlite3_recover_step() calls.
16316 static void recoverFinalCleanup(sqlite3_recover
*p
){
16317 RecoverTable
*pTab
= 0;
16318 RecoverTable
*pNext
= 0;
16320 recoverWriteDataCleanup(p
);
16321 recoverLostAndFoundCleanup(p
);
16323 for(pTab
=p
->pTblList
; pTab
; pTab
=pNext
){
16324 pNext
= pTab
->pNext
;
16325 sqlite3_free(pTab
);
16328 sqlite3_finalize(p
->pGetPage
);
16330 sqlite3_file_control(p
->dbIn
, p
->zDb
, SQLITE_FCNTL_RESET_CACHE
, 0);
16336 sqlite3_close(p
->dbOut
);
16337 assert( res
==SQLITE_OK
);
16343 ** Decode and return an unsigned 16-bit big-endian integer value from
16346 static u32
recoverGetU16(const u8
*a
){
16347 return (((u32
)a
[0])<<8) + ((u32
)a
[1]);
16351 ** Decode and return an unsigned 32-bit big-endian integer value from
16354 static u32
recoverGetU32(const u8
*a
){
16355 return (((u32
)a
[0])<<24) + (((u32
)a
[1])<<16) + (((u32
)a
[2])<<8) + ((u32
)a
[3]);
16359 ** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
16360 ** and return the number of bytes consumed.
16362 static int recoverGetVarint(const u8
*a
, i64
*pVal
){
16363 sqlite3_uint64 u
= 0;
16365 for(i
=0; i
<8; i
++){
16366 u
= (u
<<7) + (a
[i
]&0x7f);
16367 if( (a
[i
]&0x80)==0 ){ *pVal
= (sqlite3_int64
)u
; return i
+1; }
16369 u
= (u
<<8) + (a
[i
]&0xff);
16370 *pVal
= (sqlite3_int64
)u
;
16375 ** The second argument points to a buffer n bytes in size. If this buffer
16376 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
16377 ** return the page-size in bytes. Otherwise, if the buffer does not
16378 ** appear to contain a well-formed b-tree page, return 0.
16380 static int recoverIsValidPage(u8
*aTmp
, const u8
*a
, int n
){
16385 int nCell
= 0; /* Number of cells on page */
16386 int iCellOff
= 0; /* Offset of cell array in page */
16392 if( eType
!=0x02 && eType
!=0x05 && eType
!=0x0A && eType
!=0x0D ) return 0;
16394 iFree
= (int)recoverGetU16(&a
[1]);
16395 nCell
= (int)recoverGetU16(&a
[3]);
16396 iContent
= (int)recoverGetU16(&a
[5]);
16397 if( iContent
==0 ) iContent
= 65536;
16400 if( iContent
>n
) return 0;
16402 memset(aUsed
, 0, n
);
16403 memset(aUsed
, 0xFF, iContent
);
16405 /* Follow the free-list. This is the same format for all b-tree pages. */
16406 if( iFree
&& iFree
<=iContent
) return 0;
16410 if( iFree
>(n
-4) ) return 0;
16411 iNext
= recoverGetU16(&a
[iFree
]);
16412 nByte
= recoverGetU16(&a
[iFree
+2]);
16413 if( iFree
+nByte
>n
|| nByte
<4 ) return 0;
16414 if( iNext
&& iNext
<iFree
+nByte
) return 0;
16415 memset(&aUsed
[iFree
], 0xFF, nByte
);
16419 /* Run through the cells */
16420 if( eType
==0x02 || eType
==0x05 ){
16425 if( (iCellOff
+ 2*nCell
)>iContent
) return 0;
16426 for(ii
=0; ii
<nCell
; ii
++){
16430 int iOff
= recoverGetU16(&a
[iCellOff
+ 2*ii
]);
16431 if( iOff
<iContent
|| iOff
>n
){
16434 if( eType
==0x05 || eType
==0x02 ) nByte
+= 4;
16435 nByte
+= recoverGetVarint(&a
[iOff
+nByte
], &nPayload
);
16438 nByte
+= recoverGetVarint(&a
[iOff
+nByte
], &dummy
);
16441 int X
= (eType
==0x0D) ? n
-35 : (((n
-12)*64/255)-23);
16442 int M
= ((n
-12)*32/255)-23;
16443 int K
= M
+((nPayload
-M
)%(n
-4));
16454 if( iOff
+nByte
>n
){
16457 for(iByte
=iOff
; iByte
<(iOff
+nByte
); iByte
++){
16458 if( aUsed
[iByte
]!=0 ){
16461 aUsed
[iByte
] = 0xFF;
16466 for(ii
=0; ii
<n
; ii
++){
16467 if( aUsed
[ii
]==0 ) nActual
++;
16469 return (nActual
==nFrag
);
16473 static int recoverVfsClose(sqlite3_file
*);
16474 static int recoverVfsRead(sqlite3_file
*, void*, int iAmt
, sqlite3_int64 iOfst
);
16475 static int recoverVfsWrite(sqlite3_file
*, const void*, int, sqlite3_int64
);
16476 static int recoverVfsTruncate(sqlite3_file
*, sqlite3_int64 size
);
16477 static int recoverVfsSync(sqlite3_file
*, int flags
);
16478 static int recoverVfsFileSize(sqlite3_file
*, sqlite3_int64
*pSize
);
16479 static int recoverVfsLock(sqlite3_file
*, int);
16480 static int recoverVfsUnlock(sqlite3_file
*, int);
16481 static int recoverVfsCheckReservedLock(sqlite3_file
*, int *pResOut
);
16482 static int recoverVfsFileControl(sqlite3_file
*, int op
, void *pArg
);
16483 static int recoverVfsSectorSize(sqlite3_file
*);
16484 static int recoverVfsDeviceCharacteristics(sqlite3_file
*);
16485 static int recoverVfsShmMap(sqlite3_file
*, int, int, int, void volatile**);
16486 static int recoverVfsShmLock(sqlite3_file
*, int offset
, int n
, int flags
);
16487 static void recoverVfsShmBarrier(sqlite3_file
*);
16488 static int recoverVfsShmUnmap(sqlite3_file
*, int deleteFlag
);
16489 static int recoverVfsFetch(sqlite3_file
*, sqlite3_int64
, int, void**);
16490 static int recoverVfsUnfetch(sqlite3_file
*pFd
, sqlite3_int64 iOff
, void *p
);
16492 static sqlite3_io_methods recover_methods
= {
16497 recoverVfsTruncate
,
16499 recoverVfsFileSize
,
16502 recoverVfsCheckReservedLock
,
16503 recoverVfsFileControl
,
16504 recoverVfsSectorSize
,
16505 recoverVfsDeviceCharacteristics
,
16508 recoverVfsShmBarrier
,
16509 recoverVfsShmUnmap
,
16514 static int recoverVfsClose(sqlite3_file
*pFd
){
16515 assert( pFd
->pMethods
!=&recover_methods
);
16516 return pFd
->pMethods
->xClose(pFd
);
16520 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
16522 static void recoverPutU16(u8
*a
, u32 v
){
16523 a
[0] = (v
>>8) & 0x00FF;
16524 a
[1] = (v
>>0) & 0x00FF;
16528 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
16530 static void recoverPutU32(u8
*a
, u32 v
){
16531 a
[0] = (v
>>24) & 0x00FF;
16532 a
[1] = (v
>>16) & 0x00FF;
16533 a
[2] = (v
>>8) & 0x00FF;
16534 a
[3] = (v
>>0) & 0x00FF;
16538 ** Detect the page-size of the database opened by file-handle pFd by
16539 ** searching the first part of the file for a well-formed SQLite b-tree
16540 ** page. If parameter nReserve is non-zero, then as well as searching for
16541 ** a b-tree page with zero reserved bytes, this function searches for one
16542 ** with nReserve reserved bytes at the end of it.
16544 ** If successful, set variable p->detected_pgsz to the detected page-size
16545 ** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
16546 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
16547 ** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
16548 ** is returned. The final value of p->detected_pgsz is undefined in this
16551 static int recoverVfsDetectPagesize(
16552 sqlite3_recover
*p
, /* Recover handle */
16553 sqlite3_file
*pFd
, /* File-handle open on input database */
16554 u32 nReserve
, /* Possible nReserve value */
16555 i64 nSz
/* Size of database file in bytes */
16557 int rc
= SQLITE_OK
;
16558 const int nMin
= 512;
16559 const int nMax
= 65536;
16560 const int nMaxBlk
= 4;
16567 aPg
= (u8
*)sqlite3_malloc(2*nMax
);
16568 if( aPg
==0 ) return SQLITE_NOMEM
;
16571 nBlk
= (nSz
+nMax
-1)/nMax
;
16572 if( nBlk
>nMaxBlk
) nBlk
= nMaxBlk
;
16575 for(iBlk
=0; rc
==SQLITE_OK
&& iBlk
<nBlk
; iBlk
++){
16576 int nByte
= (nSz
>=((iBlk
+1)*nMax
)) ? nMax
: (nSz
% nMax
);
16577 memset(aPg
, 0, nMax
);
16578 rc
= pFd
->pMethods
->xRead(pFd
, aPg
, nByte
, iBlk
*nMax
);
16579 if( rc
==SQLITE_OK
){
16581 for(pgsz2
=(pgsz
? pgsz
*2 : nMin
); pgsz2
<=nMax
; pgsz2
=pgsz2
*2){
16583 for(iOff
=0; iOff
<nMax
; iOff
+=pgsz2
){
16584 if( recoverIsValidPage(aTmp
, &aPg
[iOff
], pgsz2
-nReserve
) ){
16592 if( pgsz
>(u32
)p
->detected_pgsz
){
16593 p
->detected_pgsz
= pgsz
;
16594 p
->nReserve
= nReserve
;
16596 if( nReserve
==0 ) break;
16600 p
->detected_pgsz
= pgsz
;
16606 ** The xRead() method of the wrapper VFS. This is used to intercept calls
16607 ** to read page 1 of the input database.
16609 static int recoverVfsRead(sqlite3_file
*pFd
, void *aBuf
, int nByte
, i64 iOff
){
16610 int rc
= SQLITE_OK
;
16611 if( pFd
->pMethods
==&recover_methods
){
16612 pFd
->pMethods
= recover_g
.pMethods
;
16613 rc
= pFd
->pMethods
->xRead(pFd
, aBuf
, nByte
, iOff
);
16615 sqlite3_randomness(16, aBuf
);
16617 if( rc
==SQLITE_OK
&& iOff
==0 && nByte
>=108 ){
16618 /* Ensure that the database has a valid header file. The only fields
16619 ** that really matter to recovery are:
16621 ** + Database page size (16-bits at offset 16)
16622 ** + Size of db in pages (32-bits at offset 28)
16623 ** + Database encoding (32-bits at offset 56)
16625 ** Also preserved are:
16627 ** + first freelist page (32-bits at offset 32)
16628 ** + size of freelist (32-bits at offset 36)
16629 ** + the wal-mode flags (16-bits at offset 18)
16631 ** We also try to preserve the auto-vacuum, incr-value, user-version
16632 ** and application-id fields - all 32 bit quantities at offsets
16633 ** 52, 60, 64 and 68. All other fields are set to known good values.
16635 ** Byte offset 105 should also contain the page-size as a 16-bit
16638 const int aPreserve
[] = {32, 36, 52, 60, 64, 68};
16640 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
16641 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
16642 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
16643 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
16644 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
16645 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
16646 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
16647 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
16648 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
16649 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16650 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16652 0x00, 0x2e, 0x5b, 0x30,
16654 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
16658 u32 pgsz
= recoverGetU16(&a
[16]);
16659 u32 nReserve
= a
[20];
16660 u32 enc
= recoverGetU32(&a
[56]);
16662 i64 dbFileSize
= 0;
16664 sqlite3_recover
*p
= recover_g
.p
;
16666 if( pgsz
==0x01 ) pgsz
= 65536;
16667 rc
= pFd
->pMethods
->xFileSize(pFd
, &dbFileSize
);
16669 if( rc
==SQLITE_OK
&& p
->detected_pgsz
==0 ){
16670 rc
= recoverVfsDetectPagesize(p
, pFd
, nReserve
, dbFileSize
);
16672 if( p
->detected_pgsz
){
16673 pgsz
= p
->detected_pgsz
;
16674 nReserve
= p
->nReserve
;
16678 dbsz
= dbFileSize
/ pgsz
;
16680 if( enc
!=SQLITE_UTF8
&& enc
!=SQLITE_UTF16BE
&& enc
!=SQLITE_UTF16LE
){
16684 sqlite3_free(p
->pPage1Cache
);
16685 p
->pPage1Cache
= 0;
16689 p
->pPage1Cache
= (u8
*)recoverMalloc(p
, nByte
*2);
16690 if( p
->pPage1Cache
){
16691 p
->pPage1Disk
= &p
->pPage1Cache
[nByte
];
16692 memcpy(p
->pPage1Disk
, aBuf
, nByte
);
16695 recoverPutU32(&aHdr
[28], dbsz
);
16696 recoverPutU32(&aHdr
[56], enc
);
16697 recoverPutU16(&aHdr
[105], pgsz
-nReserve
);
16698 if( pgsz
==65536 ) pgsz
= 1;
16699 recoverPutU16(&aHdr
[16], pgsz
);
16700 aHdr
[20] = nReserve
;
16701 for(ii
=0; ii
<(int)(sizeof(aPreserve
)/sizeof(aPreserve
[0])); ii
++){
16702 memcpy(&aHdr
[aPreserve
[ii
]], &a
[aPreserve
[ii
]], 4);
16704 memcpy(aBuf
, aHdr
, sizeof(aHdr
));
16705 memset(&((u8
*)aBuf
)[sizeof(aHdr
)], 0, nByte
-sizeof(aHdr
));
16707 memcpy(p
->pPage1Cache
, aBuf
, nByte
);
16713 pFd
->pMethods
= &recover_methods
;
16715 rc
= pFd
->pMethods
->xRead(pFd
, aBuf
, nByte
, iOff
);
16721 ** Used to make sqlite3_io_methods wrapper methods less verbose.
16723 #define RECOVER_VFS_WRAPPER(code) \
16724 int rc = SQLITE_OK; \
16725 if( pFd->pMethods==&recover_methods ){ \
16726 pFd->pMethods = recover_g.pMethods; \
16728 pFd->pMethods = &recover_methods; \
16735 ** Methods of the wrapper VFS. All methods except for xRead() and xClose()
16736 ** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
16737 ** method on the lower level VFS, then reinstall the wrapper before returning.
16738 ** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
16740 static int recoverVfsWrite(
16741 sqlite3_file
*pFd
, const void *aBuf
, int nByte
, i64 iOff
16743 RECOVER_VFS_WRAPPER (
16744 pFd
->pMethods
->xWrite(pFd
, aBuf
, nByte
, iOff
)
16747 static int recoverVfsTruncate(sqlite3_file
*pFd
, sqlite3_int64 size
){
16748 RECOVER_VFS_WRAPPER (
16749 pFd
->pMethods
->xTruncate(pFd
, size
)
16752 static int recoverVfsSync(sqlite3_file
*pFd
, int flags
){
16753 RECOVER_VFS_WRAPPER (
16754 pFd
->pMethods
->xSync(pFd
, flags
)
16757 static int recoverVfsFileSize(sqlite3_file
*pFd
, sqlite3_int64
*pSize
){
16758 RECOVER_VFS_WRAPPER (
16759 pFd
->pMethods
->xFileSize(pFd
, pSize
)
16762 static int recoverVfsLock(sqlite3_file
*pFd
, int eLock
){
16763 RECOVER_VFS_WRAPPER (
16764 pFd
->pMethods
->xLock(pFd
, eLock
)
16767 static int recoverVfsUnlock(sqlite3_file
*pFd
, int eLock
){
16768 RECOVER_VFS_WRAPPER (
16769 pFd
->pMethods
->xUnlock(pFd
, eLock
)
16772 static int recoverVfsCheckReservedLock(sqlite3_file
*pFd
, int *pResOut
){
16773 RECOVER_VFS_WRAPPER (
16774 pFd
->pMethods
->xCheckReservedLock(pFd
, pResOut
)
16777 static int recoverVfsFileControl(sqlite3_file
*pFd
, int op
, void *pArg
){
16778 RECOVER_VFS_WRAPPER (
16779 (pFd
->pMethods
? pFd
->pMethods
->xFileControl(pFd
, op
, pArg
) : SQLITE_NOTFOUND
)
16782 static int recoverVfsSectorSize(sqlite3_file
*pFd
){
16783 RECOVER_VFS_WRAPPER (
16784 pFd
->pMethods
->xSectorSize(pFd
)
16787 static int recoverVfsDeviceCharacteristics(sqlite3_file
*pFd
){
16788 RECOVER_VFS_WRAPPER (
16789 pFd
->pMethods
->xDeviceCharacteristics(pFd
)
16792 static int recoverVfsShmMap(
16793 sqlite3_file
*pFd
, int iPg
, int pgsz
, int bExtend
, void volatile **pp
16795 RECOVER_VFS_WRAPPER (
16796 pFd
->pMethods
->xShmMap(pFd
, iPg
, pgsz
, bExtend
, pp
)
16799 static int recoverVfsShmLock(sqlite3_file
*pFd
, int offset
, int n
, int flags
){
16800 RECOVER_VFS_WRAPPER (
16801 pFd
->pMethods
->xShmLock(pFd
, offset
, n
, flags
)
16804 static void recoverVfsShmBarrier(sqlite3_file
*pFd
){
16805 if( pFd
->pMethods
==&recover_methods
){
16806 pFd
->pMethods
= recover_g
.pMethods
;
16807 pFd
->pMethods
->xShmBarrier(pFd
);
16808 pFd
->pMethods
= &recover_methods
;
16810 pFd
->pMethods
->xShmBarrier(pFd
);
16813 static int recoverVfsShmUnmap(sqlite3_file
*pFd
, int deleteFlag
){
16814 RECOVER_VFS_WRAPPER (
16815 pFd
->pMethods
->xShmUnmap(pFd
, deleteFlag
)
16819 static int recoverVfsFetch(
16821 sqlite3_int64 iOff
,
16831 static int recoverVfsUnfetch(sqlite3_file
*pFd
, sqlite3_int64 iOff
, void *p
){
16839 ** Install the VFS wrapper around the file-descriptor open on the input
16840 ** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
16841 ** when this function is called.
16843 static void recoverInstallWrapper(sqlite3_recover
*p
){
16844 sqlite3_file
*pFd
= 0;
16845 assert( recover_g
.pMethods
==0 );
16846 recoverAssertMutexHeld();
16847 sqlite3_file_control(p
->dbIn
, p
->zDb
, SQLITE_FCNTL_FILE_POINTER
, (void*)&pFd
);
16848 assert( pFd
==0 || pFd
->pMethods
!=&recover_methods
);
16849 if( pFd
&& pFd
->pMethods
){
16850 int iVersion
= 1 + (pFd
->pMethods
->iVersion
>1 && pFd
->pMethods
->xShmMap
!=0);
16851 recover_g
.pMethods
= pFd
->pMethods
;
16853 recover_methods
.iVersion
= iVersion
;
16854 pFd
->pMethods
= &recover_methods
;
16859 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
16860 ** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
16861 ** held when this function is called.
16863 static void recoverUninstallWrapper(sqlite3_recover
*p
){
16864 sqlite3_file
*pFd
= 0;
16865 recoverAssertMutexHeld();
16866 sqlite3_file_control(p
->dbIn
, p
->zDb
,SQLITE_FCNTL_FILE_POINTER
,(void*)&pFd
);
16867 if( pFd
&& pFd
->pMethods
){
16868 pFd
->pMethods
= recover_g
.pMethods
;
16869 recover_g
.pMethods
= 0;
16875 ** This function does the work of a single sqlite3_recover_step() call. It
16876 ** is guaranteed that the handle is not in an error state when this
16877 ** function is called.
16879 static void recoverStep(sqlite3_recover
*p
){
16880 assert( p
&& p
->errCode
==SQLITE_OK
);
16881 switch( p
->eState
){
16882 case RECOVER_STATE_INIT
:
16883 /* This is the very first call to sqlite3_recover_step() on this object.
16885 recoverSqlCallback(p
, "BEGIN");
16886 recoverSqlCallback(p
, "PRAGMA writable_schema = on");
16888 recoverEnterMutex();
16889 recoverInstallWrapper(p
);
16891 /* Open the output database. And register required virtual tables and
16892 ** user functions with the new handle. */
16893 recoverOpenOutput(p
);
16895 /* Open transactions on both the input and output databases. */
16896 sqlite3_file_control(p
->dbIn
, p
->zDb
, SQLITE_FCNTL_RESET_CACHE
, 0);
16897 recoverExec(p
, p
->dbIn
, "PRAGMA writable_schema = on");
16898 recoverExec(p
, p
->dbIn
, "BEGIN");
16899 if( p
->errCode
==SQLITE_OK
) p
->bCloseTransaction
= 1;
16900 recoverExec(p
, p
->dbIn
, "SELECT 1 FROM sqlite_schema");
16901 recoverTransferSettings(p
);
16902 recoverOpenRecovery(p
);
16903 recoverCacheSchema(p
);
16905 recoverUninstallWrapper(p
);
16906 recoverLeaveMutex();
16908 recoverExec(p
, p
->dbOut
, "BEGIN");
16910 recoverWriteSchema1(p
);
16911 p
->eState
= RECOVER_STATE_WRITING
;
16914 case RECOVER_STATE_WRITING
: {
16915 if( p
->w1
.pTbls
==0 ){
16916 recoverWriteDataInit(p
);
16918 if( SQLITE_DONE
==recoverWriteDataStep(p
) ){
16919 recoverWriteDataCleanup(p
);
16920 if( p
->zLostAndFound
){
16921 p
->eState
= RECOVER_STATE_LOSTANDFOUND1
;
16923 p
->eState
= RECOVER_STATE_SCHEMA2
;
16929 case RECOVER_STATE_LOSTANDFOUND1
: {
16930 if( p
->laf
.pUsed
==0 ){
16931 recoverLostAndFound1Init(p
);
16933 if( SQLITE_DONE
==recoverLostAndFound1Step(p
) ){
16934 p
->eState
= RECOVER_STATE_LOSTANDFOUND2
;
16938 case RECOVER_STATE_LOSTANDFOUND2
: {
16939 if( p
->laf
.pAllAndParent
==0 ){
16940 recoverLostAndFound2Init(p
);
16942 if( SQLITE_DONE
==recoverLostAndFound2Step(p
) ){
16943 p
->eState
= RECOVER_STATE_LOSTANDFOUND3
;
16948 case RECOVER_STATE_LOSTANDFOUND3
: {
16949 if( p
->laf
.pInsert
==0 ){
16950 recoverLostAndFound3Init(p
);
16952 if( SQLITE_DONE
==recoverLostAndFound3Step(p
) ){
16953 p
->eState
= RECOVER_STATE_SCHEMA2
;
16958 case RECOVER_STATE_SCHEMA2
: {
16959 int rc
= SQLITE_OK
;
16961 recoverWriteSchema2(p
);
16962 p
->eState
= RECOVER_STATE_DONE
;
16964 /* If no error has occurred, commit the write transaction on the output
16965 ** database. Regardless of whether or not an error has occurred, make
16966 ** an attempt to end the read transaction on the input database. */
16967 recoverExec(p
, p
->dbOut
, "COMMIT");
16968 rc
= sqlite3_exec(p
->dbIn
, "END", 0, 0, 0);
16969 if( p
->errCode
==SQLITE_OK
) p
->errCode
= rc
;
16971 recoverSqlCallback(p
, "PRAGMA writable_schema = off");
16972 recoverSqlCallback(p
, "COMMIT");
16973 p
->eState
= RECOVER_STATE_DONE
;
16974 recoverFinalCleanup(p
);
16978 case RECOVER_STATE_DONE
: {
16987 ** This is a worker function that does the heavy lifting for both init
16990 ** sqlite3_recover_init()
16991 ** sqlite3_recover_init_sql()
16993 ** All this function does is allocate space for the recover handle and
16994 ** take copies of the input parameters. All the real work is done within
16995 ** sqlite3_recover_run().
16997 sqlite3_recover
*recoverInit(
17000 const char *zUri
, /* Output URI for _recover_init() */
17001 int (*xSql
)(void*, const char*),/* SQL callback for _recover_init_sql() */
17002 void *pSqlCtx
/* Context arg for _recover_init_sql() */
17004 sqlite3_recover
*pRet
= 0;
17009 if( zDb
==0 ){ zDb
= "main"; }
17011 nDb
= recoverStrlen(zDb
);
17012 nUri
= recoverStrlen(zUri
);
17014 nByte
= sizeof(sqlite3_recover
) + nDb
+1 + nUri
+1;
17015 pRet
= (sqlite3_recover
*)sqlite3_malloc(nByte
);
17017 memset(pRet
, 0, nByte
);
17019 pRet
->zDb
= (char*)&pRet
[1];
17020 pRet
->zUri
= &pRet
->zDb
[nDb
+1];
17021 memcpy(pRet
->zDb
, zDb
, nDb
);
17022 if( nUri
>0 && zUri
) memcpy(pRet
->zUri
, zUri
, nUri
);
17024 pRet
->pSqlCtx
= pSqlCtx
;
17025 pRet
->bRecoverRowid
= RECOVER_ROWID_DEFAULT
;
17032 ** Initialize a recovery handle that creates a new database containing
17033 ** the recovered data.
17035 sqlite3_recover
*sqlite3_recover_init(
17040 return recoverInit(db
, zDb
, zUri
, 0, 0);
17044 ** Initialize a recovery handle that returns recovered data in the
17045 ** form of SQL statements via a callback.
17047 sqlite3_recover
*sqlite3_recover_init_sql(
17050 int (*xSql
)(void*, const char*),
17053 return recoverInit(db
, zDb
, 0, xSql
, pSqlCtx
);
17057 ** Return the handle error message, if any.
17059 const char *sqlite3_recover_errmsg(sqlite3_recover
*p
){
17060 return (p
&& p
->errCode
!=SQLITE_NOMEM
) ? p
->zErrMsg
: "out of memory";
17064 ** Return the handle error code.
17066 int sqlite3_recover_errcode(sqlite3_recover
*p
){
17067 return p
? p
->errCode
: SQLITE_NOMEM
;
17071 ** Configure the handle.
17073 int sqlite3_recover_config(sqlite3_recover
*p
, int op
, void *pArg
){
17074 int rc
= SQLITE_OK
;
17077 }else if( p
->eState
!=RECOVER_STATE_INIT
){
17078 rc
= SQLITE_MISUSE
;
17082 /* This undocumented magic configuration option is used to set the
17083 ** name of the auxiliary database that is ATTACH-ed to the database
17084 ** connection and used to hold state information during the
17085 ** recovery process. This option is for debugging use only and
17086 ** is subject to change or removal at any time. */
17087 sqlite3_free(p
->zStateDb
);
17088 p
->zStateDb
= recoverMPrintf(p
, "%s", (char*)pArg
);
17091 case SQLITE_RECOVER_LOST_AND_FOUND
: {
17092 const char *zArg
= (const char*)pArg
;
17093 sqlite3_free(p
->zLostAndFound
);
17095 p
->zLostAndFound
= recoverMPrintf(p
, "%s", zArg
);
17097 p
->zLostAndFound
= 0;
17102 case SQLITE_RECOVER_FREELIST_CORRUPT
:
17103 p
->bFreelistCorrupt
= *(int*)pArg
;
17106 case SQLITE_RECOVER_ROWIDS
:
17107 p
->bRecoverRowid
= *(int*)pArg
;
17110 case SQLITE_RECOVER_SLOWINDEXES
:
17111 p
->bSlowIndexes
= *(int*)pArg
;
17115 rc
= SQLITE_NOTFOUND
;
17124 ** Do a unit of work towards the recovery job. Return SQLITE_OK if
17125 ** no error has occurred but database recovery is not finished, SQLITE_DONE
17126 ** if database recovery has been successfully completed, or an SQLite
17127 ** error code if an error has occurred.
17129 int sqlite3_recover_step(sqlite3_recover
*p
){
17130 if( p
==0 ) return SQLITE_NOMEM
;
17131 if( p
->errCode
==SQLITE_OK
) recoverStep(p
);
17132 if( p
->eState
==RECOVER_STATE_DONE
&& p
->errCode
==SQLITE_OK
){
17133 return SQLITE_DONE
;
17139 ** Do the configured recovery operation. Return SQLITE_OK if successful, or
17140 ** else an SQLite error code.
17142 int sqlite3_recover_run(sqlite3_recover
*p
){
17143 while( SQLITE_OK
==sqlite3_recover_step(p
) );
17144 return sqlite3_recover_errcode(p
);
17149 ** Free all resources associated with the recover handle passed as the only
17150 ** argument. The results of using a handle with any sqlite3_recover_**
17151 ** API function after it has been passed to this function are undefined.
17153 ** A copy of the value returned by the first call made to sqlite3_recover_run()
17154 ** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
17155 ** not been called on this handle.
17157 int sqlite3_recover_finish(sqlite3_recover
*p
){
17162 recoverFinalCleanup(p
);
17163 if( p
->bCloseTransaction
&& sqlite3_get_autocommit(p
->dbIn
)==0 ){
17164 rc
= sqlite3_exec(p
->dbIn
, "END", 0, 0, 0);
17165 if( p
->errCode
==SQLITE_OK
) p
->errCode
= rc
;
17168 sqlite3_free(p
->zErrMsg
);
17169 sqlite3_free(p
->zStateDb
);
17170 sqlite3_free(p
->zLostAndFound
);
17171 sqlite3_free(p
->pPage1Cache
);
17177 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
17179 /************************* End ../ext/recover/sqlite3recover.c ********************/
17180 # endif /* SQLITE_HAVE_SQLITE3R */
17182 #ifdef SQLITE_SHELL_EXTSRC
17183 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
17186 #if defined(SQLITE_ENABLE_SESSION)
17188 ** State information for a single open session
17190 typedef struct OpenSession OpenSession
;
17191 struct OpenSession
{
17192 char *zName
; /* Symbolic name for this session */
17193 int nFilter
; /* Number of xFilter rejection GLOB patterns */
17194 char **azFilter
; /* Array of xFilter rejection GLOB patterns */
17195 sqlite3_session
*p
; /* The open session */
17199 typedef struct ExpertInfo ExpertInfo
;
17200 struct ExpertInfo
{
17201 sqlite3expert
*pExpert
;
17205 /* A single line in the EQP output */
17206 typedef struct EQPGraphRow EQPGraphRow
;
17207 struct EQPGraphRow
{
17208 int iEqpId
; /* ID for this row */
17209 int iParentId
; /* ID of the parent row */
17210 EQPGraphRow
*pNext
; /* Next row in sequence */
17211 char zText
[1]; /* Text to display for this row */
17214 /* All EQP output is collected into an instance of the following */
17215 typedef struct EQPGraph EQPGraph
;
17217 EQPGraphRow
*pRow
; /* Linked list of all rows of the EQP output */
17218 EQPGraphRow
*pLast
; /* Last element of the pRow list */
17219 char zPrefix
[100]; /* Graph prefix */
17222 /* Parameters affecting columnar mode result display (defaulting together) */
17223 typedef struct ColModeOpts
{
17224 int iWrap
; /* In columnar modes, wrap lines reaching this limit */
17225 u8 bQuote
; /* Quote results for .mode box and table */
17226 u8 bWordWrap
; /* In columnar modes, wrap at word boundaries */
17228 #define ColModeOpts_default { 60, 0, 0 }
17229 #define ColModeOpts_default_qbox { 60, 1, 0 }
17232 ** State information about the database connection is contained in an
17233 ** instance of the following structure.
17235 typedef struct ShellState ShellState
;
17236 struct ShellState
{
17237 sqlite3
*db
; /* The database */
17238 u8 autoExplain
; /* Automatically turn on .explain mode */
17239 u8 autoEQP
; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
17240 u8 autoEQPtest
; /* autoEQP is in test mode */
17241 u8 autoEQPtrace
; /* autoEQP is in trace mode */
17242 u8 scanstatsOn
; /* True to display scan stats before each finalize */
17243 u8 openMode
; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
17244 u8 doXdgOpen
; /* Invoke start/open/xdg-open in output_reset() */
17245 u8 nEqpLevel
; /* Depth of the EQP output graph */
17246 u8 eTraceType
; /* SHELL_TRACE_* value for type of trace */
17247 u8 bSafeMode
; /* True to prohibit unsafe operations */
17248 u8 bSafeModePersist
; /* The long-term value of bSafeMode */
17249 ColModeOpts cmOpts
; /* Option values affecting columnar mode output */
17250 unsigned statsOn
; /* True to display memory stats before each finalize */
17251 unsigned mEqpLines
; /* Mask of vertical lines in the EQP output graph */
17252 int inputNesting
; /* Track nesting level of .read and other redirects */
17253 int outCount
; /* Revert to stdout when reaching zero */
17254 int cnt
; /* Number of records displayed so far */
17255 int lineno
; /* Line number of last line read from in */
17256 int openFlags
; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
17257 FILE *in
; /* Read commands from this stream */
17258 FILE *out
; /* Write results here */
17259 FILE *traceOut
; /* Output for sqlite3_trace() */
17260 int nErr
; /* Number of errors seen */
17261 int mode
; /* An output mode setting */
17262 int modePrior
; /* Saved mode */
17263 int cMode
; /* temporary output mode for the current query */
17264 int normalMode
; /* Output mode before ".explain on" */
17265 int writableSchema
; /* True if PRAGMA writable_schema=ON */
17266 int showHeader
; /* True to show column names in List or Column mode */
17267 int nCheck
; /* Number of ".check" commands run */
17268 unsigned nProgress
; /* Number of progress callbacks encountered */
17269 unsigned mxProgress
; /* Maximum progress callbacks before failing */
17270 unsigned flgProgress
; /* Flags for the progress callback */
17271 unsigned shellFlgs
; /* Various flags */
17272 unsigned priorShFlgs
; /* Saved copy of flags */
17273 sqlite3_int64 szMax
; /* --maxsize argument to .open */
17274 char *zDestTable
; /* Name of destination table when MODE_Insert */
17275 char *zTempFile
; /* Temporary file that might need deleting */
17276 char zTestcase
[30]; /* Name of current test case */
17277 char colSeparator
[20]; /* Column separator character for several modes */
17278 char rowSeparator
[20]; /* Row separator character for MODE_Ascii */
17279 char colSepPrior
[20]; /* Saved column separator */
17280 char rowSepPrior
[20]; /* Saved row separator */
17281 int *colWidth
; /* Requested width of each column in columnar modes */
17282 int *actualWidth
; /* Actual width of each column */
17283 int nWidth
; /* Number of slots in colWidth[] and actualWidth[] */
17284 char nullValue
[20]; /* The text to print when a NULL comes back from
17286 char outfile
[FILENAME_MAX
]; /* Filename for *out */
17287 sqlite3_stmt
*pStmt
; /* Current statement if any. */
17288 FILE *pLog
; /* Write log output here */
17289 struct AuxDb
{ /* Storage space for auxiliary database connections */
17290 sqlite3
*db
; /* Connection pointer */
17291 const char *zDbFilename
; /* Filename used to open the connection */
17292 char *zFreeOnClose
; /* Free this memory allocation on close */
17293 #if defined(SQLITE_ENABLE_SESSION)
17294 int nSession
; /* Number of active sessions */
17295 OpenSession aSession
[4]; /* Array of sessions. [0] is in focus. */
17297 } aAuxDb
[5], /* Array of all database connections */
17298 *pAuxDb
; /* Currently active database connection */
17299 int *aiIndent
; /* Array of indents used in MODE_Explain */
17300 int nIndent
; /* Size of array aiIndent[] */
17301 int iIndent
; /* Index of current op in aiIndent[] */
17302 char *zNonce
; /* Nonce for temporary safe-mode escapes */
17303 EQPGraph sGraph
; /* Information for the graphical EXPLAIN QUERY PLAN */
17304 ExpertInfo expert
; /* Valid if previous command was ".expert OPT..." */
17305 #ifdef SQLITE_SHELL_FIDDLE
17307 const char * zInput
; /* Input string from wasm/JS proxy */
17308 const char * zPos
; /* Cursor pos into zInput */
17309 const char * zDefaultDbName
; /* Default name for db file */
17314 #ifdef SQLITE_SHELL_FIDDLE
17315 static ShellState shellState
;
17319 /* Allowed values for ShellState.autoEQP
17321 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
17322 #define AUTOEQP_on 1 /* Automatic EQP is on */
17323 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
17324 #define AUTOEQP_full 3 /* Show full EXPLAIN */
17326 /* Allowed values for ShellState.openMode
17328 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
17329 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
17330 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
17331 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
17332 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
17333 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
17334 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
17336 /* Allowed values for ShellState.eTraceType
17338 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
17339 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
17340 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
17342 /* Bits in the ShellState.flgProgress variable */
17343 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
17344 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
17345 ** callback limit is reached, and for each
17346 ** top-level SQL statement */
17347 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
17350 ** These are the allowed shellFlgs values
17352 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
17353 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
17354 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
17355 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
17356 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
17357 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
17358 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
17359 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
17360 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
17361 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
17362 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
17365 ** Macros for testing and setting shellFlgs
17367 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
17368 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
17369 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
17372 ** These are the allowed modes.
17374 #define MODE_Line 0 /* One column per line. Blank line between records */
17375 #define MODE_Column 1 /* One record per line in neat columns */
17376 #define MODE_List 2 /* One record per line with a separator */
17377 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
17378 #define MODE_Html 4 /* Generate an XHTML table */
17379 #define MODE_Insert 5 /* Generate SQL "insert" statements */
17380 #define MODE_Quote 6 /* Quote values as for SQL */
17381 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
17382 #define MODE_Csv 8 /* Quote strings, numbers are plain */
17383 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
17384 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
17385 #define MODE_Pretty 11 /* Pretty-print schemas */
17386 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
17387 #define MODE_Json 13 /* Output JSON */
17388 #define MODE_Markdown 14 /* Markdown formatting */
17389 #define MODE_Table 15 /* MySQL-style table formatting */
17390 #define MODE_Box 16 /* Unicode box-drawing characters */
17391 #define MODE_Count 17 /* Output only a count of the rows of output */
17392 #define MODE_Off 18 /* No query output shown */
17393 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
17395 static const char *modeDescr
[] = {
17418 ** These are the column/row/line separators used by the various
17419 ** import/export modes.
17421 #define SEP_Column "|"
17422 #define SEP_Row "\n"
17423 #define SEP_Tab "\t"
17424 #define SEP_Space " "
17425 #define SEP_Comma ","
17426 #define SEP_CrLf "\r\n"
17427 #define SEP_Unit "\x1F"
17428 #define SEP_Record "\x1E"
17431 ** Limit input nesting via .read or any other input redirect.
17432 ** It's not too expensive, so a generous allowance can be made.
17434 #define MAX_INPUT_NESTING 25
17437 ** A callback for the sqlite3_log() interface.
17439 static void shellLog(void *pArg
, int iErrCode
, const char *zMsg
){
17440 ShellState
*p
= (ShellState
*)pArg
;
17441 if( p
->pLog
==0 ) return;
17442 utf8_printf(p
->pLog
, "(%d) %s\n", iErrCode
, zMsg
);
17447 ** SQL function: shell_putsnl(X)
17449 ** Write the text X to the screen (or whatever output is being directed)
17450 ** adding a newline at the end, and then return X.
17452 static void shellPutsFunc(
17453 sqlite3_context
*pCtx
,
17455 sqlite3_value
**apVal
17457 ShellState
*p
= (ShellState
*)sqlite3_user_data(pCtx
);
17459 utf8_printf(p
->out
, "%s\n", sqlite3_value_text(apVal
[0]));
17460 sqlite3_result_value(pCtx
, apVal
[0]);
17464 ** If in safe mode, print an error message described by the arguments
17465 ** and exit immediately.
17467 static void failIfSafeMode(
17469 const char *zErrMsg
,
17472 if( p
->bSafeMode
){
17475 va_start(ap
, zErrMsg
);
17476 zMsg
= sqlite3_vmprintf(zErrMsg
, ap
);
17478 raw_printf(stderr
, "line %d: ", p
->lineno
);
17479 utf8_printf(stderr
, "%s\n", zMsg
);
17485 ** SQL function: edit(VALUE)
17486 ** edit(VALUE,EDITOR)
17490 ** (1) Write VALUE into a temporary file.
17491 ** (2) Run program EDITOR on that temporary file.
17492 ** (3) Read the temporary file back and return its content as the result.
17493 ** (4) Delete the temporary file
17495 ** If the EDITOR argument is omitted, use the value in the VISUAL
17496 ** environment variable. If still there is no EDITOR, through an error.
17498 ** Also throw an error if the EDITOR program returns a non-zero exit code.
17500 #ifndef SQLITE_NOHAVE_SYSTEM
17501 static void editFunc(
17502 sqlite3_context
*context
,
17504 sqlite3_value
**argv
17506 const char *zEditor
;
17507 char *zTempFile
= 0;
17516 unsigned char *p
= 0;
17519 zEditor
= (const char*)sqlite3_value_text(argv
[1]);
17521 zEditor
= getenv("VISUAL");
17524 sqlite3_result_error(context
, "no editor for edit()", -1);
17527 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
){
17528 sqlite3_result_error(context
, "NULL input to edit()", -1);
17531 db
= sqlite3_context_db_handle(context
);
17533 sqlite3_file_control(db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &zTempFile
);
17534 if( zTempFile
==0 ){
17535 sqlite3_uint64 r
= 0;
17536 sqlite3_randomness(sizeof(r
), &r
);
17537 zTempFile
= sqlite3_mprintf("temp%llx", r
);
17538 if( zTempFile
==0 ){
17539 sqlite3_result_error_nomem(context
);
17543 bBin
= sqlite3_value_type(argv
[0])==SQLITE_BLOB
;
17544 /* When writing the file to be edited, do \n to \r\n conversions on systems
17545 ** that want \r\n line endings */
17546 f
= fopen(zTempFile
, bBin
? "wb" : "w");
17548 sqlite3_result_error(context
, "edit() cannot open temp file", -1);
17549 goto edit_func_end
;
17551 sz
= sqlite3_value_bytes(argv
[0]);
17553 x
= fwrite(sqlite3_value_blob(argv
[0]), 1, (size_t)sz
, f
);
17555 const char *z
= (const char*)sqlite3_value_text(argv
[0]);
17556 /* Remember whether or not the value originally contained \r\n */
17557 if( z
&& strstr(z
,"\r\n")!=0 ) hasCRNL
= 1;
17558 x
= fwrite(sqlite3_value_text(argv
[0]), 1, (size_t)sz
, f
);
17563 sqlite3_result_error(context
, "edit() could not write the whole file", -1);
17564 goto edit_func_end
;
17566 zCmd
= sqlite3_mprintf("%s \"%s\"", zEditor
, zTempFile
);
17568 sqlite3_result_error_nomem(context
);
17569 goto edit_func_end
;
17572 sqlite3_free(zCmd
);
17574 sqlite3_result_error(context
, "EDITOR returned non-zero", -1);
17575 goto edit_func_end
;
17577 f
= fopen(zTempFile
, "rb");
17579 sqlite3_result_error(context
,
17580 "edit() cannot reopen temp file after edit", -1);
17581 goto edit_func_end
;
17583 fseek(f
, 0, SEEK_END
);
17586 p
= sqlite3_malloc64( sz
+1 );
17588 sqlite3_result_error_nomem(context
);
17589 goto edit_func_end
;
17591 x
= fread(p
, 1, (size_t)sz
, f
);
17595 sqlite3_result_error(context
, "could not read back the whole file", -1);
17596 goto edit_func_end
;
17599 sqlite3_result_blob64(context
, p
, sz
, sqlite3_free
);
17601 sqlite3_int64 i
, j
;
17603 /* If the original contains \r\n then do no conversions back to \n */
17605 /* If the file did not originally contain \r\n then convert any new
17606 ** \r\n back into \n */
17608 for(i
=j
=0; i
<sz
; i
++){
17609 if( p
[i
]=='\r' && p
[i
+1]=='\n' ) i
++;
17615 sqlite3_result_text64(context
, (const char*)p
, sz
,
17616 sqlite3_free
, SQLITE_UTF8
);
17623 sqlite3_free(zTempFile
);
17626 #endif /* SQLITE_NOHAVE_SYSTEM */
17629 ** Save or restore the current output mode
17631 static void outputModePush(ShellState
*p
){
17632 p
->modePrior
= p
->mode
;
17633 p
->priorShFlgs
= p
->shellFlgs
;
17634 memcpy(p
->colSepPrior
, p
->colSeparator
, sizeof(p
->colSeparator
));
17635 memcpy(p
->rowSepPrior
, p
->rowSeparator
, sizeof(p
->rowSeparator
));
17637 static void outputModePop(ShellState
*p
){
17638 p
->mode
= p
->modePrior
;
17639 p
->shellFlgs
= p
->priorShFlgs
;
17640 memcpy(p
->colSeparator
, p
->colSepPrior
, sizeof(p
->colSeparator
));
17641 memcpy(p
->rowSeparator
, p
->rowSepPrior
, sizeof(p
->rowSeparator
));
17645 ** Output the given string as a hex-encoded blob (eg. X'1234' )
17647 static void output_hex_blob(FILE *out
, const void *pBlob
, int nBlob
){
17649 unsigned char *aBlob
= (unsigned char*)pBlob
;
17651 char *zStr
= sqlite3_malloc(nBlob
*2 + 1);
17652 shell_check_oom(zStr
);
17654 for(i
=0; i
<nBlob
; i
++){
17655 static const char aHex
[] = {
17656 '0', '1', '2', '3', '4', '5', '6', '7',
17657 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
17659 zStr
[i
*2] = aHex
[ (aBlob
[i
] >> 4) ];
17660 zStr
[i
*2+1] = aHex
[ (aBlob
[i
] & 0x0F) ];
17664 raw_printf(out
,"X'%s'", zStr
);
17665 sqlite3_free(zStr
);
17669 ** Find a string that is not found anywhere in z[]. Return a pointer
17672 ** Try to use zA and zB first. If both of those are already found in z[]
17673 ** then make up some string and store it in the buffer zBuf.
17675 static const char *unused_string(
17676 const char *z
, /* Result must not appear anywhere in z */
17677 const char *zA
, const char *zB
, /* Try these first */
17678 char *zBuf
/* Space to store a generated string */
17681 if( strstr(z
, zA
)==0 ) return zA
;
17682 if( strstr(z
, zB
)==0 ) return zB
;
17684 sqlite3_snprintf(20,zBuf
,"(%s%u)", zA
, i
++);
17685 }while( strstr(z
,zBuf
)!=0 );
17690 ** Output the given string as a quoted string using SQL quoting conventions.
17692 ** See also: output_quoted_escaped_string()
17694 static void output_quoted_string(FILE *out
, const char *z
){
17697 setBinaryMode(out
, 1);
17699 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
17701 utf8_printf(out
,"'%s'",z
);
17703 raw_printf(out
, "'");
17705 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
17708 utf8_printf(out
, "%.*s", i
, z
);
17712 raw_printf(out
, "'");
17720 raw_printf(out
, "'");
17722 setTextMode(out
, 1);
17726 ** Output the given string as a quoted string using SQL quoting conventions.
17727 ** Additionallly , escape the "\n" and "\r" characters so that they do not
17728 ** get corrupted by end-of-line translation facilities in some operating
17731 ** This is like output_quoted_string() but with the addition of the \r\n
17732 ** escape mechanism.
17734 static void output_quoted_escaped_string(FILE *out
, const char *z
){
17737 setBinaryMode(out
, 1);
17738 for(i
=0; (c
= z
[i
])!=0 && c
!='\'' && c
!='\n' && c
!='\r'; i
++){}
17740 utf8_printf(out
,"'%s'",z
);
17742 const char *zNL
= 0;
17743 const char *zCR
= 0;
17746 char zBuf1
[20], zBuf2
[20];
17747 for(i
=0; z
[i
]; i
++){
17748 if( z
[i
]=='\n' ) nNL
++;
17749 if( z
[i
]=='\r' ) nCR
++;
17752 raw_printf(out
, "replace(");
17753 zNL
= unused_string(z
, "\\n", "\\012", zBuf1
);
17756 raw_printf(out
, "replace(");
17757 zCR
= unused_string(z
, "\\r", "\\015", zBuf2
);
17759 raw_printf(out
, "'");
17761 for(i
=0; (c
= z
[i
])!=0 && c
!='\n' && c
!='\r' && c
!='\''; i
++){}
17764 utf8_printf(out
, "%.*s", i
, z
);
17768 raw_printf(out
, "'");
17776 raw_printf(out
, "%s", zNL
);
17779 raw_printf(out
, "%s", zCR
);
17781 raw_printf(out
, "'");
17783 raw_printf(out
, ",'%s',char(13))", zCR
);
17786 raw_printf(out
, ",'%s',char(10))", zNL
);
17789 setTextMode(out
, 1);
17793 ** Output the given string as a quoted according to C or TCL quoting rules.
17795 static void output_c_string(FILE *out
, const char *z
){
17798 while( (c
= *(z
++))!=0 ){
17802 }else if( c
=='"' ){
17805 }else if( c
=='\t' ){
17808 }else if( c
=='\n' ){
17811 }else if( c
=='\r' ){
17814 }else if( !isprint(c
&0xff) ){
17815 raw_printf(out
, "\\%03o", c
&0xff);
17824 ** Output the given string as a quoted according to JSON quoting rules.
17826 static void output_json_string(FILE *out
, const char *z
, i64 n
){
17829 if( n
<0 ) n
= strlen(z
);
17833 if( c
=='\\' || c
=='"' ){
17836 }else if( c
<=0x1f ){
17840 }else if( c
=='\f' ){
17842 }else if( c
=='\n' ){
17844 }else if( c
=='\r' ){
17846 }else if( c
=='\t' ){
17849 raw_printf(out
, "u%04x",c
);
17859 ** Output the given string with characters that are special to
17862 static void output_html_string(FILE *out
, const char *z
){
17874 utf8_printf(out
,"%.*s",i
,z
);
17877 raw_printf(out
,"<");
17878 }else if( z
[i
]=='&' ){
17879 raw_printf(out
,"&");
17880 }else if( z
[i
]=='>' ){
17881 raw_printf(out
,">");
17882 }else if( z
[i
]=='\"' ){
17883 raw_printf(out
,""");
17884 }else if( z
[i
]=='\'' ){
17885 raw_printf(out
,"'");
17894 ** If a field contains any character identified by a 1 in the following
17895 ** array, then the string must be quoted for CSV.
17897 static const char needCsvQuote
[] = {
17898 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17899 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17900 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
17901 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17902 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17903 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17904 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17905 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
17906 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17907 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17908 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17909 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17910 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17911 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17912 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17913 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17917 ** Output a single term of CSV. Actually, p->colSeparator is used for
17918 ** the separator, which may or may not be a comma. p->nullValue is
17919 ** the null value. Strings are quoted if necessary. The separator
17920 ** is only issued if bSep is true.
17922 static void output_csv(ShellState
*p
, const char *z
, int bSep
){
17923 FILE *out
= p
->out
;
17925 utf8_printf(out
,"%s",p
->nullValue
);
17928 for(i
=0; z
[i
]; i
++){
17929 if( needCsvQuote
[((unsigned char*)z
)[i
]] ){
17934 if( i
==0 || strstr(z
, p
->colSeparator
)!=0 ){
17935 char *zQuoted
= sqlite3_mprintf("\"%w\"", z
);
17936 shell_check_oom(zQuoted
);
17937 utf8_printf(out
, "%s", zQuoted
);
17938 sqlite3_free(zQuoted
);
17940 utf8_printf(out
, "%s", z
);
17944 utf8_printf(p
->out
, "%s", p
->colSeparator
);
17949 ** This routine runs when the user presses Ctrl-C
17951 static void interrupt_handler(int NotUsed
){
17952 UNUSED_PARAMETER(NotUsed
);
17953 if( ++seenInterrupt
>1 ) exit(1);
17954 if( globalDb
) sqlite3_interrupt(globalDb
);
17957 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
17959 ** This routine runs for console events (e.g. Ctrl-C) on Win32
17961 static BOOL WINAPI
ConsoleCtrlHandler(
17962 DWORD dwCtrlType
/* One of the CTRL_*_EVENT constants */
17964 if( dwCtrlType
==CTRL_C_EVENT
){
17965 interrupt_handler(0);
17972 #ifndef SQLITE_OMIT_AUTHORIZATION
17974 ** This authorizer runs in safe mode.
17976 static int safeModeAuth(
17984 ShellState
*p
= (ShellState
*)pClientData
;
17985 static const char *azProhibitedFunctions
[] = {
17994 UNUSED_PARAMETER(zA1
);
17995 UNUSED_PARAMETER(zA3
);
17996 UNUSED_PARAMETER(zA4
);
17998 case SQLITE_ATTACH
: {
17999 #ifndef SQLITE_SHELL_FIDDLE
18000 /* In WASM builds the filesystem is a virtual sandbox, so
18001 ** there's no harm in using ATTACH. */
18002 failIfSafeMode(p
, "cannot run ATTACH in safe mode");
18006 case SQLITE_FUNCTION
: {
18008 for(i
=0; i
<ArraySize(azProhibitedFunctions
); i
++){
18009 if( sqlite3_stricmp(zA2
, azProhibitedFunctions
[i
])==0 ){
18010 failIfSafeMode(p
, "cannot use the %s() function in safe mode",
18011 azProhibitedFunctions
[i
]);
18021 ** When the ".auth ON" is set, the following authorizer callback is
18022 ** invoked. It always returns SQLITE_OK.
18024 static int shellAuth(
18032 ShellState
*p
= (ShellState
*)pClientData
;
18033 static const char *azAction
[] = { 0,
18034 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
18035 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
18036 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
18037 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
18038 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
18039 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
18040 "PRAGMA", "READ", "SELECT",
18041 "TRANSACTION", "UPDATE", "ATTACH",
18042 "DETACH", "ALTER_TABLE", "REINDEX",
18043 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
18044 "FUNCTION", "SAVEPOINT", "RECURSIVE"
18052 utf8_printf(p
->out
, "authorizer: %s", azAction
[op
]);
18053 for(i
=0; i
<4; i
++){
18054 raw_printf(p
->out
, " ");
18056 output_c_string(p
->out
, az
[i
]);
18058 raw_printf(p
->out
, "NULL");
18061 raw_printf(p
->out
, "\n");
18062 if( p
->bSafeMode
) (void)safeModeAuth(pClientData
, op
, zA1
, zA2
, zA3
, zA4
);
18068 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
18070 ** This routine converts some CREATE TABLE statements for shadow tables
18071 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
18073 ** If the schema statement in z[] contains a start-of-comment and if
18074 ** sqlite3_complete() returns false, try to terminate the comment before
18075 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
18077 static void printSchemaLine(FILE *out
, const char *z
, const char *zTail
){
18080 if( zTail
==0 ) return;
18081 if( zTail
[0]==';' && (strstr(z
, "/*")!=0 || strstr(z
,"--")!=0) ){
18082 const char *zOrig
= z
;
18083 static const char *azTerm
[] = { "", "*/", "\n" };
18085 for(i
=0; i
<ArraySize(azTerm
); i
++){
18086 char *zNew
= sqlite3_mprintf("%s%s;", zOrig
, azTerm
[i
]);
18087 shell_check_oom(zNew
);
18088 if( sqlite3_complete(zNew
) ){
18089 size_t n
= strlen(zNew
);
18095 sqlite3_free(zNew
);
18098 if( sqlite3_strglob("CREATE TABLE ['\"]*", z
)==0 ){
18099 utf8_printf(out
, "CREATE TABLE IF NOT EXISTS %s%s", z
+13, zTail
);
18101 utf8_printf(out
, "%s%s", z
, zTail
);
18103 sqlite3_free(zToFree
);
18105 static void printSchemaLineN(FILE *out
, char *z
, int n
, const char *zTail
){
18108 printSchemaLine(out
, z
, zTail
);
18113 ** Return true if string z[] has nothing but whitespace and comments to the
18114 ** end of the first line.
18116 static int wsToEol(const char *z
){
18118 for(i
=0; z
[i
]; i
++){
18119 if( z
[i
]=='\n' ) return 1;
18120 if( IsSpace(z
[i
]) ) continue;
18121 if( z
[i
]=='-' && z
[i
+1]=='-' ) return 1;
18128 ** Add a new entry to the EXPLAIN QUERY PLAN data
18130 static void eqp_append(ShellState
*p
, int iEqpId
, int p2
, const char *zText
){
18133 if( zText
==0 ) return;
18134 nText
= strlen(zText
);
18135 if( p
->autoEQPtest
){
18136 utf8_printf(p
->out
, "%d,%d,%s\n", iEqpId
, p2
, zText
);
18138 pNew
= sqlite3_malloc64( sizeof(*pNew
) + nText
);
18139 shell_check_oom(pNew
);
18140 pNew
->iEqpId
= iEqpId
;
18141 pNew
->iParentId
= p2
;
18142 memcpy(pNew
->zText
, zText
, nText
+1);
18144 if( p
->sGraph
.pLast
){
18145 p
->sGraph
.pLast
->pNext
= pNew
;
18147 p
->sGraph
.pRow
= pNew
;
18149 p
->sGraph
.pLast
= pNew
;
18153 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
18156 static void eqp_reset(ShellState
*p
){
18157 EQPGraphRow
*pRow
, *pNext
;
18158 for(pRow
= p
->sGraph
.pRow
; pRow
; pRow
= pNext
){
18159 pNext
= pRow
->pNext
;
18160 sqlite3_free(pRow
);
18162 memset(&p
->sGraph
, 0, sizeof(p
->sGraph
));
18165 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
18166 ** pOld, or return the first such line if pOld is NULL
18168 static EQPGraphRow
*eqp_next_row(ShellState
*p
, int iEqpId
, EQPGraphRow
*pOld
){
18169 EQPGraphRow
*pRow
= pOld
? pOld
->pNext
: p
->sGraph
.pRow
;
18170 while( pRow
&& pRow
->iParentId
!=iEqpId
) pRow
= pRow
->pNext
;
18174 /* Render a single level of the graph that has iEqpId as its parent. Called
18175 ** recursively to render sublevels.
18177 static void eqp_render_level(ShellState
*p
, int iEqpId
){
18178 EQPGraphRow
*pRow
, *pNext
;
18179 i64 n
= strlen(p
->sGraph
.zPrefix
);
18181 for(pRow
= eqp_next_row(p
, iEqpId
, 0); pRow
; pRow
= pNext
){
18182 pNext
= eqp_next_row(p
, iEqpId
, pRow
);
18184 utf8_printf(p
->out
, "%s%s%s\n", p
->sGraph
.zPrefix
,
18185 pNext
? "|--" : "`--", z
);
18186 if( n
<(i64
)sizeof(p
->sGraph
.zPrefix
)-7 ){
18187 memcpy(&p
->sGraph
.zPrefix
[n
], pNext
? "| " : " ", 4);
18188 eqp_render_level(p
, pRow
->iEqpId
);
18189 p
->sGraph
.zPrefix
[n
] = 0;
18195 ** Display and reset the EXPLAIN QUERY PLAN data
18197 static void eqp_render(ShellState
*p
, i64 nCycle
){
18198 EQPGraphRow
*pRow
= p
->sGraph
.pRow
;
18200 if( pRow
->zText
[0]=='-' ){
18201 if( pRow
->pNext
==0 ){
18205 utf8_printf(p
->out
, "%s\n", pRow
->zText
+3);
18206 p
->sGraph
.pRow
= pRow
->pNext
;
18207 sqlite3_free(pRow
);
18208 }else if( nCycle
>0 ){
18209 utf8_printf(p
->out
, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle
);
18211 utf8_printf(p
->out
, "QUERY PLAN\n");
18213 p
->sGraph
.zPrefix
[0] = 0;
18214 eqp_render_level(p
, 0);
18219 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
18221 ** Progress handler callback.
18223 static int progress_handler(void *pClientData
) {
18224 ShellState
*p
= (ShellState
*)pClientData
;
18226 if( p
->nProgress
>=p
->mxProgress
&& p
->mxProgress
>0 ){
18227 raw_printf(p
->out
, "Progress limit reached (%u)\n", p
->nProgress
);
18228 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
18229 if( p
->flgProgress
& SHELL_PROGRESS_ONCE
) p
->mxProgress
= 0;
18232 if( (p
->flgProgress
& SHELL_PROGRESS_QUIET
)==0 ){
18233 raw_printf(p
->out
, "Progress %u\n", p
->nProgress
);
18237 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
18242 static void print_dashes(FILE *out
, int N
){
18243 const char zDash
[] = "--------------------------------------------------";
18244 const int nDash
= sizeof(zDash
) - 1;
18249 raw_printf(out
, "%.*s", N
, zDash
);
18253 ** Print a markdown or table-style row separator using ascii-art
18255 static void print_row_separator(
18262 fputs(zSep
, p
->out
);
18263 print_dashes(p
->out
, p
->actualWidth
[0]+2);
18264 for(i
=1; i
<nArg
; i
++){
18265 fputs(zSep
, p
->out
);
18266 print_dashes(p
->out
, p
->actualWidth
[i
]+2);
18268 fputs(zSep
, p
->out
);
18270 fputs("\n", p
->out
);
18274 ** This is the callback routine that the shell
18275 ** invokes for each row of a query result.
18277 static int shell_callback(
18279 int nArg
, /* Number of result columns */
18280 char **azArg
, /* Text of each result column */
18281 char **azCol
, /* Column names */
18282 int *aiType
/* Column types. Might be NULL */
18285 ShellState
*p
= (ShellState
*)pArg
;
18287 if( azArg
==0 ) return 0;
18288 switch( p
->cMode
){
18295 if( azArg
==0 ) break;
18296 for(i
=0; i
<nArg
; i
++){
18297 int len
= strlen30(azCol
[i
] ? azCol
[i
] : "");
18298 if( len
>w
) w
= len
;
18300 if( p
->cnt
++>0 ) utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18301 for(i
=0; i
<nArg
; i
++){
18302 utf8_printf(p
->out
,"%*s = %s%s", w
, azCol
[i
],
18303 azArg
[i
] ? azArg
[i
] : p
->nullValue
, p
->rowSeparator
);
18308 case MODE_Explain
: {
18309 static const int aExplainWidth
[] = {4, 13, 4, 4, 4, 13, 2, 13};
18310 static const int aExplainMap
[] = {0, 1, 2, 3, 4, 5, 6, 7 };
18311 static const int aScanExpWidth
[] = {4, 6, 6, 13, 4, 4, 4, 13, 2, 13};
18312 static const int aScanExpMap
[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
18314 const int *aWidth
= aExplainWidth
;
18315 const int *aMap
= aExplainMap
;
18316 int nWidth
= ArraySize(aExplainWidth
);
18319 if( p
->cMode
==MODE_ScanExp
){
18320 aWidth
= aScanExpWidth
;
18321 aMap
= aScanExpMap
;
18322 nWidth
= ArraySize(aScanExpWidth
);
18325 if( nArg
>nWidth
) nArg
= nWidth
;
18327 /* If this is the first row seen, print out the headers */
18329 for(i
=0; i
<nArg
; i
++){
18330 utf8_width_print(p
->out
, aWidth
[i
], azCol
[ aMap
[i
] ]);
18331 fputs(i
==nArg
-1 ? "\n" : " ", p
->out
);
18333 for(i
=0; i
<nArg
; i
++){
18334 print_dashes(p
->out
, aWidth
[i
]);
18335 fputs(i
==nArg
-1 ? "\n" : " ", p
->out
);
18339 /* If there is no data, exit early. */
18340 if( azArg
==0 ) break;
18342 for(i
=0; i
<nArg
; i
++){
18343 const char *zSep
= " ";
18345 const char *zVal
= azArg
[ aMap
[i
] ];
18346 if( i
==nArg
-1 ) w
= 0;
18347 if( zVal
&& strlenChar(zVal
)>w
){
18348 w
= strlenChar(zVal
);
18351 if( i
==iIndent
&& p
->aiIndent
&& p
->pStmt
){
18352 if( p
->iIndent
<p
->nIndent
){
18353 utf8_printf(p
->out
, "%*.s", p
->aiIndent
[p
->iIndent
], "");
18357 utf8_width_print(p
->out
, w
, zVal
? zVal
: p
->nullValue
);
18358 fputs(i
==nArg
-1 ? "\n" : zSep
, p
->out
);
18362 case MODE_Semi
: { /* .schema and .fullschema output */
18363 printSchemaLine(p
->out
, azArg
[0], ";\n");
18366 case MODE_Pretty
: { /* .schema and .fullschema with --indent */
18374 if( azArg
[0]==0 ) break;
18375 if( sqlite3_strlike("CREATE VIEW%", azArg
[0], 0)==0
18376 || sqlite3_strlike("CREATE TRIG%", azArg
[0], 0)==0
18378 utf8_printf(p
->out
, "%s;\n", azArg
[0]);
18381 z
= sqlite3_mprintf("%s", azArg
[0]);
18382 shell_check_oom(z
);
18384 for(i
=0; IsSpace(z
[i
]); i
++){}
18385 for(; (c
= z
[i
])!=0; i
++){
18387 if( z
[j
-1]=='\r' ) z
[j
-1] = '\n';
18388 if( IsSpace(z
[j
-1]) || z
[j
-1]=='(' ) continue;
18389 }else if( (c
=='(' || c
==')') && j
>0 && IsSpace(z
[j
-1]) ){
18394 while( j
>0 && IsSpace(z
[j
-1]) ){ j
--; }
18396 if( strlen30(z
)>=79 ){
18397 for(i
=j
=0; (c
= z
[i
])!=0; i
++){ /* Copy from z[i] back to z[j] */
18400 }else if( c
=='"' || c
=='\'' || c
=='`' ){
18402 }else if( c
=='[' ){
18404 }else if( c
=='-' && z
[i
+1]=='-' ){
18406 }else if( c
=='(' ){
18408 }else if( c
==')' ){
18410 if( nLine
>0 && nParen
==0 && j
>0 ){
18411 printSchemaLineN(p
->out
, z
, j
, "\n");
18416 if( nParen
==1 && cEnd
==0
18417 && (c
=='(' || c
=='\n' || (c
==',' && !wsToEol(z
+i
+1)))
18420 printSchemaLineN(p
->out
, z
, j
, "\n ");
18423 while( IsSpace(z
[i
+1]) ){ i
++; }
18428 printSchemaLine(p
->out
, z
, ";\n");
18433 if( p
->cnt
++==0 && p
->showHeader
){
18434 for(i
=0; i
<nArg
; i
++){
18435 utf8_printf(p
->out
,"%s%s",azCol
[i
],
18436 i
==nArg
-1 ? p
->rowSeparator
: p
->colSeparator
);
18439 if( azArg
==0 ) break;
18440 for(i
=0; i
<nArg
; i
++){
18441 char *z
= azArg
[i
];
18442 if( z
==0 ) z
= p
->nullValue
;
18443 utf8_printf(p
->out
, "%s", z
);
18445 utf8_printf(p
->out
, "%s", p
->colSeparator
);
18447 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18453 if( p
->cnt
++==0 && p
->showHeader
){
18454 raw_printf(p
->out
,"<TR>");
18455 for(i
=0; i
<nArg
; i
++){
18456 raw_printf(p
->out
,"<TH>");
18457 output_html_string(p
->out
, azCol
[i
]);
18458 raw_printf(p
->out
,"</TH>\n");
18460 raw_printf(p
->out
,"</TR>\n");
18462 if( azArg
==0 ) break;
18463 raw_printf(p
->out
,"<TR>");
18464 for(i
=0; i
<nArg
; i
++){
18465 raw_printf(p
->out
,"<TD>");
18466 output_html_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
18467 raw_printf(p
->out
,"</TD>\n");
18469 raw_printf(p
->out
,"</TR>\n");
18473 if( p
->cnt
++==0 && p
->showHeader
){
18474 for(i
=0; i
<nArg
; i
++){
18475 output_c_string(p
->out
,azCol
[i
] ? azCol
[i
] : "");
18476 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
18478 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18480 if( azArg
==0 ) break;
18481 for(i
=0; i
<nArg
; i
++){
18482 output_c_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
18483 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
18485 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18489 setBinaryMode(p
->out
, 1);
18490 if( p
->cnt
++==0 && p
->showHeader
){
18491 for(i
=0; i
<nArg
; i
++){
18492 output_csv(p
, azCol
[i
] ? azCol
[i
] : "", i
<nArg
-1);
18494 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18497 for(i
=0; i
<nArg
; i
++){
18498 output_csv(p
, azArg
[i
], i
<nArg
-1);
18500 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18502 setTextMode(p
->out
, 1);
18505 case MODE_Insert
: {
18506 if( azArg
==0 ) break;
18507 utf8_printf(p
->out
,"INSERT INTO %s",p
->zDestTable
);
18508 if( p
->showHeader
){
18509 raw_printf(p
->out
,"(");
18510 for(i
=0; i
<nArg
; i
++){
18511 if( i
>0 ) raw_printf(p
->out
, ",");
18512 if( quoteChar(azCol
[i
]) ){
18513 char *z
= sqlite3_mprintf("\"%w\"", azCol
[i
]);
18514 shell_check_oom(z
);
18515 utf8_printf(p
->out
, "%s", z
);
18518 raw_printf(p
->out
, "%s", azCol
[i
]);
18521 raw_printf(p
->out
,")");
18524 for(i
=0; i
<nArg
; i
++){
18525 raw_printf(p
->out
, i
>0 ? "," : " VALUES(");
18526 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
18527 utf8_printf(p
->out
,"NULL");
18528 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
18529 if( ShellHasFlag(p
, SHFLG_Newlines
) ){
18530 output_quoted_string(p
->out
, azArg
[i
]);
18532 output_quoted_escaped_string(p
->out
, azArg
[i
]);
18534 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
18535 utf8_printf(p
->out
,"%s", azArg
[i
]);
18536 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
18538 double r
= sqlite3_column_double(p
->pStmt
, i
);
18540 memcpy(&ur
,&r
,sizeof(r
));
18541 if( ur
==0x7ff0000000000000LL
){
18542 raw_printf(p
->out
, "9.0e+999");
18543 }else if( ur
==0xfff0000000000000LL
){
18544 raw_printf(p
->out
, "-9.0e+999");
18546 sqlite3_int64 ir
= (sqlite3_int64
)r
;
18547 if( r
==(double)ir
){
18548 sqlite3_snprintf(50,z
,"%lld.0", ir
);
18550 sqlite3_snprintf(50,z
,"%!.20g", r
);
18552 raw_printf(p
->out
, "%s", z
);
18554 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
18555 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
18556 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
18557 output_hex_blob(p
->out
, pBlob
, nBlob
);
18558 }else if( isNumber(azArg
[i
], 0) ){
18559 utf8_printf(p
->out
,"%s", azArg
[i
]);
18560 }else if( ShellHasFlag(p
, SHFLG_Newlines
) ){
18561 output_quoted_string(p
->out
, azArg
[i
]);
18563 output_quoted_escaped_string(p
->out
, azArg
[i
]);
18566 raw_printf(p
->out
,");\n");
18570 if( azArg
==0 ) break;
18572 fputs("[{", p
->out
);
18574 fputs(",\n{", p
->out
);
18577 for(i
=0; i
<nArg
; i
++){
18578 output_json_string(p
->out
, azCol
[i
], -1);
18580 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
18581 fputs("null",p
->out
);
18582 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
18584 double r
= sqlite3_column_double(p
->pStmt
, i
);
18586 memcpy(&ur
,&r
,sizeof(r
));
18587 if( ur
==0x7ff0000000000000LL
){
18588 raw_printf(p
->out
, "9.0e+999");
18589 }else if( ur
==0xfff0000000000000LL
){
18590 raw_printf(p
->out
, "-9.0e+999");
18592 sqlite3_snprintf(50,z
,"%!.20g", r
);
18593 raw_printf(p
->out
, "%s", z
);
18595 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
18596 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
18597 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
18598 output_json_string(p
->out
, pBlob
, nBlob
);
18599 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
18600 output_json_string(p
->out
, azArg
[i
], -1);
18602 utf8_printf(p
->out
,"%s", azArg
[i
]);
18612 if( azArg
==0 ) break;
18613 if( p
->cnt
==0 && p
->showHeader
){
18614 for(i
=0; i
<nArg
; i
++){
18615 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
18616 output_quoted_string(p
->out
, azCol
[i
]);
18618 fputs(p
->rowSeparator
, p
->out
);
18621 for(i
=0; i
<nArg
; i
++){
18622 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
18623 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
18624 utf8_printf(p
->out
,"NULL");
18625 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
18626 output_quoted_string(p
->out
, azArg
[i
]);
18627 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
18628 utf8_printf(p
->out
,"%s", azArg
[i
]);
18629 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
18631 double r
= sqlite3_column_double(p
->pStmt
, i
);
18632 sqlite3_snprintf(50,z
,"%!.20g", r
);
18633 raw_printf(p
->out
, "%s", z
);
18634 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
18635 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
18636 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
18637 output_hex_blob(p
->out
, pBlob
, nBlob
);
18638 }else if( isNumber(azArg
[i
], 0) ){
18639 utf8_printf(p
->out
,"%s", azArg
[i
]);
18641 output_quoted_string(p
->out
, azArg
[i
]);
18644 fputs(p
->rowSeparator
, p
->out
);
18648 if( p
->cnt
++==0 && p
->showHeader
){
18649 for(i
=0; i
<nArg
; i
++){
18650 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
18651 utf8_printf(p
->out
,"%s",azCol
[i
] ? azCol
[i
] : "");
18653 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18655 if( azArg
==0 ) break;
18656 for(i
=0; i
<nArg
; i
++){
18657 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
18658 utf8_printf(p
->out
,"%s",azArg
[i
] ? azArg
[i
] : p
->nullValue
);
18660 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
18664 eqp_append(p
, atoi(azArg
[0]), atoi(azArg
[1]), azArg
[3]);
18672 ** This is the callback routine that the SQLite library
18673 ** invokes for each row of a query result.
18675 static int callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
18676 /* since we don't have type info, call the shell_callback with a NULL value */
18677 return shell_callback(pArg
, nArg
, azArg
, azCol
, NULL
);
18681 ** This is the callback routine from sqlite3_exec() that appends all
18682 ** output onto the end of a ShellText object.
18684 static int captureOutputCallback(void *pArg
, int nArg
, char **azArg
, char **az
){
18685 ShellText
*p
= (ShellText
*)pArg
;
18687 UNUSED_PARAMETER(az
);
18688 if( azArg
==0 ) return 0;
18689 if( p
->n
) appendText(p
, "|", 0);
18690 for(i
=0; i
<nArg
; i
++){
18691 if( i
) appendText(p
, ",", 0);
18692 if( azArg
[i
] ) appendText(p
, azArg
[i
], 0);
18698 ** Generate an appropriate SELFTEST table in the main database.
18700 static void createSelftestTable(ShellState
*p
){
18702 sqlite3_exec(p
->db
,
18703 "SAVEPOINT selftest_init;\n"
18704 "CREATE TABLE IF NOT EXISTS selftest(\n"
18705 " tno INTEGER PRIMARY KEY,\n" /* Test number */
18706 " op TEXT,\n" /* Operator: memo run */
18707 " cmd TEXT,\n" /* Command text */
18708 " ans TEXT\n" /* Desired answer */
18710 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
18711 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
18712 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
18713 " 'memo','Tests generated by --init');\n"
18714 "INSERT INTO [_shell$self]\n"
18716 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
18717 "FROM sqlite_schema ORDER BY 2'',224))',\n"
18718 " hex(sha3_query('SELECT type,name,tbl_name,sql "
18719 "FROM sqlite_schema ORDER BY 2',224));\n"
18720 "INSERT INTO [_shell$self]\n"
18722 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
18723 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
18724 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
18726 " SELECT name FROM sqlite_schema\n"
18727 " WHERE type='table'\n"
18728 " AND name<>'selftest'\n"
18729 " AND coalesce(rootpage,0)>0\n"
18731 " ORDER BY name;\n"
18732 "INSERT INTO [_shell$self]\n"
18733 " VALUES('run','PRAGMA integrity_check','ok');\n"
18734 "INSERT INTO selftest(tno,op,cmd,ans)"
18735 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
18736 "DROP TABLE [_shell$self];"
18739 utf8_printf(stderr
, "SELFTEST initialization failure: %s\n", zErrMsg
);
18740 sqlite3_free(zErrMsg
);
18742 sqlite3_exec(p
->db
, "RELEASE selftest_init",0,0,0);
18747 ** Set the destination table field of the ShellState structure to
18748 ** the name of the table given. Escape any quote characters in the
18751 static void set_table_name(ShellState
*p
, const char *zName
){
18756 if( p
->zDestTable
){
18757 free(p
->zDestTable
);
18760 if( zName
==0 ) return;
18761 cQuote
= quoteChar(zName
);
18762 n
= strlen30(zName
);
18763 if( cQuote
) n
+= n
+2;
18764 z
= p
->zDestTable
= malloc( n
+1 );
18765 shell_check_oom(z
);
18767 if( cQuote
) z
[n
++] = cQuote
;
18768 for(i
=0; zName
[i
]; i
++){
18770 if( zName
[i
]==cQuote
) z
[n
++] = cQuote
;
18772 if( cQuote
) z
[n
++] = cQuote
;
18777 ** Maybe construct two lines of text that point out the position of a
18778 ** syntax error. Return a pointer to the text, in memory obtained from
18779 ** sqlite3_malloc(). Or, if the most recent error does not involve a
18780 ** specific token that we can point to, return an empty string.
18782 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
18783 ** and should be released by the caller invoking sqlite3_free().
18785 static char *shell_error_context(const char *zSql
, sqlite3
*db
){
18793 || (iOffset
= sqlite3_error_offset(db
))<0
18794 || iOffset
>=(int)strlen(zSql
)
18796 return sqlite3_mprintf("");
18798 while( iOffset
>50 ){
18801 while( (zSql
[0]&0xc0)==0x80 ){ zSql
++; iOffset
--; }
18803 len
= strlen(zSql
);
18806 while( len
>0 && (zSql
[len
]&0xc0)==0x80 ) len
--;
18808 zCode
= sqlite3_mprintf("%.*s", len
, zSql
);
18809 shell_check_oom(zCode
);
18810 for(i
=0; zCode
[i
]; i
++){ if( IsSpace(zSql
[i
]) ) zCode
[i
] = ' '; }
18812 zMsg
= sqlite3_mprintf("\n %z\n %*s^--- error here", zCode
,iOffset
,"");
18814 zMsg
= sqlite3_mprintf("\n %z\n %*serror here ---^", zCode
,iOffset
-14,"");
18821 ** Execute a query statement that will generate SQL output. Print
18822 ** the result columns, comma-separated, on a line and then add a
18823 ** semicolon terminator to the end of that line.
18825 ** If the number of columns is 1 and that column contains text "--"
18826 ** then write the semicolon on a separate line. That way, if a
18827 ** "--" comment occurs at the end of the statement, the comment
18828 ** won't consume the semicolon terminator.
18830 static int run_table_dump_query(
18831 ShellState
*p
, /* Query context */
18832 const char *zSelect
/* SELECT statement to extract content */
18834 sqlite3_stmt
*pSelect
;
18839 rc
= sqlite3_prepare_v2(p
->db
, zSelect
, -1, &pSelect
, 0);
18840 if( rc
!=SQLITE_OK
|| !pSelect
){
18841 char *zContext
= shell_error_context(zSelect
, p
->db
);
18842 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n%s", rc
,
18843 sqlite3_errmsg(p
->db
), zContext
);
18844 sqlite3_free(zContext
);
18845 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
18848 rc
= sqlite3_step(pSelect
);
18849 nResult
= sqlite3_column_count(pSelect
);
18850 while( rc
==SQLITE_ROW
){
18851 z
= (const char*)sqlite3_column_text(pSelect
, 0);
18852 utf8_printf(p
->out
, "%s", z
);
18853 for(i
=1; i
<nResult
; i
++){
18854 utf8_printf(p
->out
, ",%s", sqlite3_column_text(pSelect
, i
));
18857 while( z
[0] && (z
[0]!='-' || z
[1]!='-') ) z
++;
18859 raw_printf(p
->out
, "\n;\n");
18861 raw_printf(p
->out
, ";\n");
18863 rc
= sqlite3_step(pSelect
);
18865 rc
= sqlite3_finalize(pSelect
);
18866 if( rc
!=SQLITE_OK
){
18867 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n", rc
,
18868 sqlite3_errmsg(p
->db
));
18869 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
18875 ** Allocate space and save off string indicating current error.
18877 static char *save_err_msg(
18878 sqlite3
*db
, /* Database to query */
18879 const char *zPhase
, /* When the error occurs */
18880 int rc
, /* Error code returned from API */
18881 const char *zSql
/* SQL string, or NULL */
18885 sqlite3_str
*pStr
= sqlite3_str_new(0);
18886 sqlite3_str_appendf(pStr
, "%s, %s", zPhase
, sqlite3_errmsg(db
));
18888 sqlite3_str_appendf(pStr
, " (%d)", rc
);
18890 zContext
= shell_error_context(zSql
, db
);
18892 sqlite3_str_appendall(pStr
, zContext
);
18893 sqlite3_free(zContext
);
18895 zErr
= sqlite3_str_finish(pStr
);
18896 shell_check_oom(zErr
);
18902 ** Attempt to display I/O stats on Linux using /proc/PID/io
18904 static void displayLinuxIoStats(FILE *out
){
18907 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
18908 in
= fopen(z
, "rb");
18909 if( in
==0 ) return;
18910 while( fgets(z
, sizeof(z
), in
)!=0 ){
18911 static const struct {
18912 const char *zPattern
;
18915 { "rchar: ", "Bytes received by read():" },
18916 { "wchar: ", "Bytes sent to write():" },
18917 { "syscr: ", "Read() system calls:" },
18918 { "syscw: ", "Write() system calls:" },
18919 { "read_bytes: ", "Bytes read from storage:" },
18920 { "write_bytes: ", "Bytes written to storage:" },
18921 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
18924 for(i
=0; i
<ArraySize(aTrans
); i
++){
18925 int n
= strlen30(aTrans
[i
].zPattern
);
18926 if( cli_strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
18927 utf8_printf(out
, "%-36s %s", aTrans
[i
].zDesc
, &z
[n
]);
18937 ** Display a single line of status using 64-bit values.
18939 static void displayStatLine(
18940 ShellState
*p
, /* The shell context */
18941 char *zLabel
, /* Label for this one line */
18942 char *zFormat
, /* Format for the result */
18943 int iStatusCtrl
, /* Which status to display */
18944 int bReset
/* True to reset the stats */
18946 sqlite3_int64 iCur
= -1;
18947 sqlite3_int64 iHiwtr
= -1;
18950 sqlite3_status64(iStatusCtrl
, &iCur
, &iHiwtr
, bReset
);
18951 for(i
=0, nPercent
=0; zFormat
[i
]; i
++){
18952 if( zFormat
[i
]=='%' ) nPercent
++;
18955 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iCur
, iHiwtr
);
18957 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iHiwtr
);
18959 raw_printf(p
->out
, "%-36s %s\n", zLabel
, zLine
);
18963 ** Display memory stats.
18965 static int display_stats(
18966 sqlite3
*db
, /* Database to query */
18967 ShellState
*pArg
, /* Pointer to ShellState */
18968 int bReset
/* True to reset the stats */
18973 if( pArg
==0 || pArg
->out
==0 ) return 0;
18976 if( pArg
->pStmt
&& pArg
->statsOn
==2 ){
18978 sqlite3_stmt
*pStmt
= pArg
->pStmt
;
18980 nCol
= sqlite3_column_count(pStmt
);
18981 raw_printf(out
, "%-36s %d\n", "Number of output columns:", nCol
);
18982 for(i
=0; i
<nCol
; i
++){
18983 sqlite3_snprintf(sizeof(z
),z
,"Column %d %nname:", i
, &x
);
18984 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_name(pStmt
,i
));
18985 #ifndef SQLITE_OMIT_DECLTYPE
18986 sqlite3_snprintf(30, z
+x
, "declared type:");
18987 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_decltype(pStmt
, i
));
18989 #ifdef SQLITE_ENABLE_COLUMN_METADATA
18990 sqlite3_snprintf(30, z
+x
, "database name:");
18991 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_database_name(pStmt
,i
));
18992 sqlite3_snprintf(30, z
+x
, "table name:");
18993 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_table_name(pStmt
,i
));
18994 sqlite3_snprintf(30, z
+x
, "origin name:");
18995 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_origin_name(pStmt
,i
));
19000 if( pArg
->statsOn
==3 ){
19002 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
,bReset
);
19003 raw_printf(pArg
->out
, "VM-steps: %d\n", iCur
);
19008 displayStatLine(pArg
, "Memory Used:",
19009 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED
, bReset
);
19010 displayStatLine(pArg
, "Number of Outstanding Allocations:",
19011 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT
, bReset
);
19012 if( pArg
->shellFlgs
& SHFLG_Pagecache
){
19013 displayStatLine(pArg
, "Number of Pcache Pages Used:",
19014 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED
, bReset
);
19016 displayStatLine(pArg
, "Number of Pcache Overflow Bytes:",
19017 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW
, bReset
);
19018 displayStatLine(pArg
, "Largest Allocation:",
19019 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE
, bReset
);
19020 displayStatLine(pArg
, "Largest Pcache Allocation:",
19021 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE
, bReset
);
19022 #ifdef YYTRACKMAXSTACKDEPTH
19023 displayStatLine(pArg
, "Deepest Parser Stack:",
19024 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK
, bReset
);
19028 if( pArg
->shellFlgs
& SHFLG_Lookaside
){
19029 iHiwtr
= iCur
= -1;
19030 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_USED
,
19031 &iCur
, &iHiwtr
, bReset
);
19032 raw_printf(pArg
->out
,
19033 "Lookaside Slots Used: %d (max %d)\n",
19035 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
,
19036 &iCur
, &iHiwtr
, bReset
);
19037 raw_printf(pArg
->out
, "Successful lookaside attempts: %d\n",
19039 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
,
19040 &iCur
, &iHiwtr
, bReset
);
19041 raw_printf(pArg
->out
, "Lookaside failures due to size: %d\n",
19043 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
,
19044 &iCur
, &iHiwtr
, bReset
);
19045 raw_printf(pArg
->out
, "Lookaside failures due to OOM: %d\n",
19048 iHiwtr
= iCur
= -1;
19049 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHiwtr
, bReset
);
19050 raw_printf(pArg
->out
, "Pager Heap Usage: %d bytes\n",
19052 iHiwtr
= iCur
= -1;
19053 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHiwtr
, 1);
19054 raw_printf(pArg
->out
, "Page cache hits: %d\n", iCur
);
19055 iHiwtr
= iCur
= -1;
19056 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHiwtr
, 1);
19057 raw_printf(pArg
->out
, "Page cache misses: %d\n", iCur
);
19058 iHiwtr
= iCur
= -1;
19059 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHiwtr
, 1);
19060 raw_printf(pArg
->out
, "Page cache writes: %d\n", iCur
);
19061 iHiwtr
= iCur
= -1;
19062 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_SPILL
, &iCur
, &iHiwtr
, 1);
19063 raw_printf(pArg
->out
, "Page cache spills: %d\n", iCur
);
19064 iHiwtr
= iCur
= -1;
19065 sqlite3_db_status(db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHiwtr
, bReset
);
19066 raw_printf(pArg
->out
, "Schema Heap Usage: %d bytes\n",
19068 iHiwtr
= iCur
= -1;
19069 sqlite3_db_status(db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHiwtr
, bReset
);
19070 raw_printf(pArg
->out
, "Statement Heap/Lookaside Usage: %d bytes\n",
19076 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FULLSCAN_STEP
,
19078 raw_printf(pArg
->out
, "Fullscan Steps: %d\n", iCur
);
19079 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_SORT
, bReset
);
19080 raw_printf(pArg
->out
, "Sort Operations: %d\n", iCur
);
19081 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_AUTOINDEX
,bReset
);
19082 raw_printf(pArg
->out
, "Autoindex Inserts: %d\n", iCur
);
19083 iHit
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_HIT
,
19085 iMiss
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_MISS
,
19087 if( iHit
|| iMiss
){
19088 raw_printf(pArg
->out
, "Bloom filter bypass taken: %d/%d\n",
19091 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
, bReset
);
19092 raw_printf(pArg
->out
, "Virtual Machine Steps: %d\n", iCur
);
19093 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_REPREPARE
,bReset
);
19094 raw_printf(pArg
->out
, "Reprepare operations: %d\n", iCur
);
19095 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_RUN
, bReset
);
19096 raw_printf(pArg
->out
, "Number of times run: %d\n", iCur
);
19097 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_MEMUSED
, bReset
);
19098 raw_printf(pArg
->out
, "Memory used by prepared stmt: %d\n", iCur
);
19102 displayLinuxIoStats(pArg
->out
);
19105 /* Do not remove this machine readable comment: extra-stats-output-here */
19111 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
19112 static int scanStatsHeight(sqlite3_stmt
*p
, int iEntry
){
19115 sqlite3_stmt_scanstatus_v2(p
, iEntry
,
19116 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
19120 for(ii
=0; 1; ii
++){
19123 res
= sqlite3_stmt_scanstatus_v2(p
, ii
,
19124 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iId
19128 sqlite3_stmt_scanstatus_v2(p
, ii
,
19129 SQLITE_SCANSTAT_PARENTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
19139 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
19140 static void display_explain_scanstats(
19141 sqlite3
*db
, /* Database to query */
19142 ShellState
*pArg
/* Pointer to ShellState */
19144 static const int f
= SQLITE_SCANSTAT_COMPLEX
;
19145 sqlite3_stmt
*p
= pArg
->pStmt
;
19151 for(ii
=0; 1; ii
++){
19154 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&z
) ){
19157 n
= strlen(z
) + scanStatsHeight(p
, ii
)*3;
19158 if( n
>nWidth
) nWidth
= n
;
19162 sqlite3_stmt_scanstatus_v2(p
, -1, SQLITE_SCANSTAT_NCYCLE
, f
, (void*)&nTotal
);
19163 for(ii
=0; 1; ii
++){
19170 const char *zName
= 0;
19174 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&z
) ){
19177 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_EST
,f
,(void*)&rEst
);
19178 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NLOOP
,f
,(void*)&nLoop
);
19179 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NVISIT
,f
,(void*)&nRow
);
19180 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NCYCLE
,f
,(void*)&nCycle
);
19181 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_SELECTID
,f
,(void*)&iId
);
19182 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_PARENTID
,f
,(void*)&iPid
);
19183 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NAME
,f
,(void*)&zName
);
19185 zText
= sqlite3_mprintf("%s", z
);
19186 if( nCycle
>=0 || nLoop
>=0 || nRow
>=0 ){
19188 if( nCycle
>=0 && nTotal
>0 ){
19189 z
= sqlite3_mprintf("%zcycles=%lld [%d%%]", z
,
19190 nCycle
, ((nCycle
*100)+nTotal
/2) / nTotal
19194 z
= sqlite3_mprintf("%z%sloops=%lld", z
, z
? " " : "", nLoop
);
19197 z
= sqlite3_mprintf("%z%srows=%lld", z
, z
? " " : "", nRow
);
19200 if( zName
&& pArg
->scanstatsOn
>1 ){
19201 double rpl
= (double)nRow
/ (double)nLoop
;
19202 z
= sqlite3_mprintf("%z rpl=%.1f est=%.1f", z
, rpl
, rEst
);
19205 zText
= sqlite3_mprintf(
19206 "% *z (%z)", -1*(nWidth
-scanStatsHeight(p
, ii
)*3), zText
, z
19210 eqp_append(pArg
, iId
, iPid
, zText
);
19211 sqlite3_free(zText
);
19214 eqp_render(pArg
, nTotal
);
19220 ** Parameter azArray points to a zero-terminated array of strings. zStr
19221 ** points to a single nul-terminated string. Return non-zero if zStr
19222 ** is equal, according to strcmp(), to any of the strings in the array.
19223 ** Otherwise, return zero.
19225 static int str_in_array(const char *zStr
, const char **azArray
){
19227 for(i
=0; azArray
[i
]; i
++){
19228 if( 0==cli_strcmp(zStr
, azArray
[i
]) ) return 1;
19234 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
19235 ** and populate the ShellState.aiIndent[] array with the number of
19236 ** spaces each opcode should be indented before it is output.
19238 ** The indenting rules are:
19240 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
19241 ** all opcodes that occur between the p2 jump destination and the opcode
19242 ** itself by 2 spaces.
19244 ** * Do the previous for "Return" instructions for when P2 is positive.
19245 ** See tag-20220407a in wherecode.c and vdbe.c.
19247 ** * For each "Goto", if the jump destination is earlier in the program
19248 ** and ends on one of:
19249 ** Yield SeekGt SeekLt RowSetRead Rewind
19250 ** or if the P1 parameter is one instead of zero,
19251 ** then indent all opcodes between the earlier instruction
19252 ** and "Goto" by 2 spaces.
19254 static void explain_data_prepare(ShellState
*p
, sqlite3_stmt
*pSql
){
19255 int *abYield
= 0; /* True if op is an OP_Yield */
19256 int nAlloc
= 0; /* Allocated size of p->aiIndent[], abYield */
19257 int iOp
; /* Index of operation in p->aiIndent[] */
19259 const char *azNext
[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
19261 const char *azYield
[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
19263 const char *azGoto
[] = { "Goto", 0 };
19265 /* The caller guarantees that the leftmost 4 columns of the statement
19266 ** passed to this function are equivalent to the leftmost 4 columns
19267 ** of EXPLAIN statement output. In practice the statement may be
19268 ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
19269 assert( sqlite3_column_count(pSql
)>=4 );
19270 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 0), "addr" ) );
19271 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 1), "opcode" ) );
19272 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 2), "p1" ) );
19273 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 3), "p2" ) );
19275 for(iOp
=0; SQLITE_ROW
==sqlite3_step(pSql
); iOp
++){
19277 int iAddr
= sqlite3_column_int(pSql
, 0);
19278 const char *zOp
= (const char*)sqlite3_column_text(pSql
, 1);
19279 int p1
= sqlite3_column_int(pSql
, 2);
19280 int p2
= sqlite3_column_int(pSql
, 3);
19282 /* Assuming that p2 is an instruction address, set variable p2op to the
19283 ** index of that instruction in the aiIndent[] array. p2 and p2op may be
19284 ** different if the current instruction is part of a sub-program generated
19285 ** by an SQL trigger or foreign key. */
19286 int p2op
= (p2
+ (iOp
-iAddr
));
19288 /* Grow the p->aiIndent array as required */
19291 p
->aiIndent
= (int*)sqlite3_realloc64(p
->aiIndent
, nAlloc
*sizeof(int));
19292 shell_check_oom(p
->aiIndent
);
19293 abYield
= (int*)sqlite3_realloc64(abYield
, nAlloc
*sizeof(int));
19294 shell_check_oom(abYield
);
19297 abYield
[iOp
] = str_in_array(zOp
, azYield
);
19298 p
->aiIndent
[iOp
] = 0;
19299 p
->nIndent
= iOp
+1;
19300 if( str_in_array(zOp
, azNext
) && p2op
>0 ){
19301 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
19303 if( str_in_array(zOp
, azGoto
) && p2op
<iOp
&& (abYield
[p2op
] || p1
) ){
19304 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
19309 sqlite3_free(abYield
);
19310 sqlite3_reset(pSql
);
19314 ** Free the array allocated by explain_data_prepare().
19316 static void explain_data_delete(ShellState
*p
){
19317 sqlite3_free(p
->aiIndent
);
19323 static void exec_prepared_stmt(ShellState
*, sqlite3_stmt
*);
19326 ** Display scan stats.
19328 static void display_scanstats(
19329 sqlite3
*db
, /* Database to query */
19330 ShellState
*pArg
/* Pointer to ShellState */
19332 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19333 UNUSED_PARAMETER(db
);
19334 UNUSED_PARAMETER(pArg
);
19336 if( pArg
->scanstatsOn
==3 ){
19338 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
19339 " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
19340 " FROM bytecode(?)";
19342 int rc
= SQLITE_OK
;
19343 sqlite3_stmt
*pStmt
= 0;
19344 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
19345 if( rc
==SQLITE_OK
){
19346 sqlite3_stmt
*pSave
= pArg
->pStmt
;
19347 pArg
->pStmt
= pStmt
;
19348 sqlite3_bind_pointer(pStmt
, 1, pSave
, "stmt-pointer", 0);
19351 pArg
->cMode
= MODE_ScanExp
;
19352 explain_data_prepare(pArg
, pStmt
);
19353 exec_prepared_stmt(pArg
, pStmt
);
19354 explain_data_delete(pArg
);
19356 sqlite3_finalize(pStmt
);
19357 pArg
->pStmt
= pSave
;
19360 display_explain_scanstats(db
, pArg
);
19366 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
19368 static unsigned int savedSelectTrace
;
19369 static unsigned int savedWhereTrace
;
19370 static void disable_debug_trace_modes(void){
19371 unsigned int zero
= 0;
19372 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 0, &savedSelectTrace
);
19373 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &zero
);
19374 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 2, &savedWhereTrace
);
19375 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &zero
);
19377 static void restore_debug_trace_modes(void){
19378 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &savedSelectTrace
);
19379 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &savedWhereTrace
);
19382 /* Create the TEMP table used to store parameter bindings */
19383 static void bind_table_init(ShellState
*p
){
19385 int defensiveMode
= 0;
19386 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, -1, &defensiveMode
);
19387 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, 0, 0);
19388 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, -1, &wrSchema
);
19389 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, 1, 0);
19390 sqlite3_exec(p
->db
,
19391 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
19392 " key TEXT PRIMARY KEY,\n"
19394 ") WITHOUT ROWID;",
19396 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, wrSchema
, 0);
19397 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, defensiveMode
, 0);
19401 ** Bind parameters on a prepared statement.
19403 ** Parameter bindings are taken from a TEMP table of the form:
19405 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
19408 ** No bindings occur if this table does not exist. The name of the table
19409 ** begins with "sqlite_" so that it will not collide with ordinary application
19410 ** tables. The table must be in the TEMP schema.
19412 static void bind_prepared_stmt(ShellState
*pArg
, sqlite3_stmt
*pStmt
){
19416 sqlite3_stmt
*pQ
= 0;
19418 nVar
= sqlite3_bind_parameter_count(pStmt
);
19419 if( nVar
==0 ) return; /* Nothing to do */
19420 if( sqlite3_table_column_metadata(pArg
->db
, "TEMP", "sqlite_parameters",
19421 "key", 0, 0, 0, 0, 0)!=SQLITE_OK
){
19422 rc
= SQLITE_NOTFOUND
;
19425 rc
= sqlite3_prepare_v2(pArg
->db
,
19426 "SELECT value FROM temp.sqlite_parameters"
19427 " WHERE key=?1", -1, &pQ
, 0);
19429 for(i
=1; i
<=nVar
; i
++){
19431 const char *zVar
= sqlite3_bind_parameter_name(pStmt
, i
);
19433 sqlite3_snprintf(sizeof(zNum
),zNum
,"?%d",i
);
19436 sqlite3_bind_text(pQ
, 1, zVar
, -1, SQLITE_STATIC
);
19437 if( rc
==SQLITE_OK
&& pQ
&& sqlite3_step(pQ
)==SQLITE_ROW
){
19438 sqlite3_bind_value(pStmt
, i
, sqlite3_column_value(pQ
, 0));
19440 }else if( sqlite3_strlike("_NAN", zVar
, 0)==0 ){
19441 sqlite3_bind_double(pStmt
, i
, NAN
);
19444 }else if( sqlite3_strlike("_INF", zVar
, 0)==0 ){
19445 sqlite3_bind_double(pStmt
, i
, INFINITY
);
19448 sqlite3_bind_null(pStmt
, i
);
19452 sqlite3_finalize(pQ
);
19456 ** UTF8 box-drawing characters. Imagine box lines like this:
19464 ** Each box characters has between 2 and 4 of the lines leading from
19465 ** the center. The characters are here identified by the numbers of
19466 ** their corresponding lines.
19468 #define BOX_24 "\342\224\200" /* U+2500 --- */
19469 #define BOX_13 "\342\224\202" /* U+2502 | */
19470 #define BOX_23 "\342\224\214" /* U+250c ,- */
19471 #define BOX_34 "\342\224\220" /* U+2510 -, */
19472 #define BOX_12 "\342\224\224" /* U+2514 '- */
19473 #define BOX_14 "\342\224\230" /* U+2518 -' */
19474 #define BOX_123 "\342\224\234" /* U+251c |- */
19475 #define BOX_134 "\342\224\244" /* U+2524 -| */
19476 #define BOX_234 "\342\224\254" /* U+252c -,- */
19477 #define BOX_124 "\342\224\264" /* U+2534 -'- */
19478 #define BOX_1234 "\342\224\274" /* U+253c -|- */
19480 /* Draw horizontal line N characters long using unicode box
19483 static void print_box_line(FILE *out
, int N
){
19484 const char zDash
[] =
19485 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
19486 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
;
19487 const int nDash
= sizeof(zDash
) - 1;
19490 utf8_printf(out
, zDash
);
19493 utf8_printf(out
, "%.*s", N
, zDash
);
19497 ** Draw a horizontal separator for a MODE_Box table.
19499 static void print_box_row_separator(
19508 utf8_printf(p
->out
, "%s", zSep1
);
19509 print_box_line(p
->out
, p
->actualWidth
[0]+2);
19510 for(i
=1; i
<nArg
; i
++){
19511 utf8_printf(p
->out
, "%s", zSep2
);
19512 print_box_line(p
->out
, p
->actualWidth
[i
]+2);
19514 utf8_printf(p
->out
, "%s", zSep3
);
19516 fputs("\n", p
->out
);
19520 ** z[] is a line of text that is to be displayed the .mode box or table or
19521 ** similar tabular formats. z[] might contain control characters such
19522 ** as \n, \t, \f, or \r.
19524 ** Compute characters to display on the first line of z[]. Stop at the
19525 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
19526 ** from malloc()) of that first line, which caller should free sometime.
19527 ** Write anything to display on the next line into *pzTail. If this is
19528 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
19530 static char *translateForDisplayAndDup(
19531 const unsigned char *z
, /* Input text to be transformed */
19532 const unsigned char **pzTail
, /* OUT: Tail of the input for next line */
19533 int mxWidth
, /* Max width. 0 means no limit */
19534 u8 bWordWrap
/* If true, avoid breaking mid-word */
19536 int i
; /* Input bytes consumed */
19537 int j
; /* Output bytes generated */
19538 int k
; /* Input bytes to be displayed */
19539 int n
; /* Output column number */
19540 unsigned char *zOut
; /* Output text */
19546 if( mxWidth
<0 ) mxWidth
= -mxWidth
;
19547 if( mxWidth
==0 ) mxWidth
= 1000000;
19549 while( n
<mxWidth
){
19552 do{ i
++; j
++; }while( (z
[i
]&0xc0)==0x80 );
19559 }while( (n
&7)!=0 && n
<mxWidth
);
19565 if( n
>=mxWidth
&& bWordWrap
){
19566 /* Perhaps try to back up to a better place to break the line */
19567 for(k
=i
; k
>i
/2; k
--){
19568 if( isspace(z
[k
-1]) ) break;
19571 for(k
=i
; k
>i
/2; k
--){
19572 if( isalnum(z
[k
-1])!=isalnum(z
[k
]) && (z
[k
]&0xc0)!=0x80 ) break;
19579 while( z
[i
]==' ' ) i
++;
19584 if( n
>=mxWidth
&& z
[i
]>=' ' ){
19586 }else if( z
[i
]=='\r' && z
[i
+1]=='\n' ){
19587 *pzTail
= z
[i
+2] ? &z
[i
+2] : 0;
19588 }else if( z
[i
]==0 || z
[i
+1]==0 ){
19593 zOut
= malloc( j
+1 );
19594 shell_check_oom(zOut
);
19599 do{ zOut
[j
++] = z
[i
++]; }while( (z
[i
]&0xc0)==0x80 );
19606 }while( (n
&7)!=0 && n
<mxWidth
);
19613 return (char*)zOut
;
19616 /* Extract the value of the i-th current column for pStmt as an SQL literal
19617 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
19620 static char *quoted_column(sqlite3_stmt
*pStmt
, int i
){
19621 switch( sqlite3_column_type(pStmt
, i
) ){
19622 case SQLITE_NULL
: {
19623 return sqlite3_mprintf("NULL");
19625 case SQLITE_INTEGER
:
19626 case SQLITE_FLOAT
: {
19627 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt
,i
));
19629 case SQLITE_TEXT
: {
19630 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt
,i
));
19632 case SQLITE_BLOB
: {
19634 sqlite3_str
*pStr
= sqlite3_str_new(0);
19635 const unsigned char *a
= sqlite3_column_blob(pStmt
,i
);
19636 int n
= sqlite3_column_bytes(pStmt
,i
);
19637 sqlite3_str_append(pStr
, "x'", 2);
19638 for(j
=0; j
<n
; j
++){
19639 sqlite3_str_appendf(pStr
, "%02x", a
[j
]);
19641 sqlite3_str_append(pStr
, "'", 1);
19642 return sqlite3_str_finish(pStr
);
19645 return 0; /* Not reached */
19649 ** Run a prepared statement and output the result in one of the
19650 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
19653 ** This is different from ordinary exec_prepared_stmt() in that
19654 ** it has to run the entire query and gather the results into memory
19655 ** first, in order to determine column widths, before providing
19658 static void exec_prepared_stmt_columnar(
19659 ShellState
*p
, /* Pointer to ShellState */
19660 sqlite3_stmt
*pStmt
/* Statement to run */
19662 sqlite3_int64 nRow
= 0;
19665 sqlite3_int64 nAlloc
= 0;
19666 char *abRowDiv
= 0;
19667 const unsigned char *uz
;
19669 char **azQuoted
= 0;
19671 sqlite3_int64 i
, nData
;
19672 int j
, nTotal
, w
, n
;
19673 const char *colSep
= 0;
19674 const char *rowSep
= 0;
19675 const unsigned char **azNextLine
= 0;
19677 int bMultiLineRowExists
= 0;
19678 int bw
= p
->cmOpts
.bWordWrap
;
19679 const char *zEmpty
= "";
19680 const char *zShowNull
= p
->nullValue
;
19682 rc
= sqlite3_step(pStmt
);
19683 if( rc
!=SQLITE_ROW
) return;
19684 nColumn
= sqlite3_column_count(pStmt
);
19685 nAlloc
= nColumn
*4;
19686 if( nAlloc
<=0 ) nAlloc
= 1;
19687 azData
= sqlite3_malloc64( nAlloc
*sizeof(char*) );
19688 shell_check_oom(azData
);
19689 azNextLine
= sqlite3_malloc64( nColumn
*sizeof(char*) );
19690 shell_check_oom(azNextLine
);
19691 memset((void*)azNextLine
, 0, nColumn
*sizeof(char*) );
19692 if( p
->cmOpts
.bQuote
){
19693 azQuoted
= sqlite3_malloc64( nColumn
*sizeof(char*) );
19694 shell_check_oom(azQuoted
);
19695 memset(azQuoted
, 0, nColumn
*sizeof(char*) );
19697 abRowDiv
= sqlite3_malloc64( nAlloc
/nColumn
);
19698 shell_check_oom(abRowDiv
);
19699 if( nColumn
>p
->nWidth
){
19700 p
->colWidth
= realloc(p
->colWidth
, (nColumn
+1)*2*sizeof(int));
19701 shell_check_oom(p
->colWidth
);
19702 for(i
=p
->nWidth
; i
<nColumn
; i
++) p
->colWidth
[i
] = 0;
19703 p
->nWidth
= nColumn
;
19704 p
->actualWidth
= &p
->colWidth
[nColumn
];
19706 memset(p
->actualWidth
, 0, nColumn
*sizeof(int));
19707 for(i
=0; i
<nColumn
; i
++){
19708 w
= p
->colWidth
[i
];
19710 p
->actualWidth
[i
] = w
;
19712 for(i
=0; i
<nColumn
; i
++){
19713 const unsigned char *zNotUsed
;
19714 int wx
= p
->colWidth
[i
];
19716 wx
= p
->cmOpts
.iWrap
;
19718 if( wx
<0 ) wx
= -wx
;
19719 uz
= (const unsigned char*)sqlite3_column_name(pStmt
,i
);
19720 if( uz
==0 ) uz
= (u8
*)"";
19721 azData
[i
] = translateForDisplayAndDup(uz
, &zNotUsed
, wx
, bw
);
19724 int useNextLine
= bNextLine
;
19726 if( (nRow
+2)*nColumn
>= nAlloc
){
19728 azData
= sqlite3_realloc64(azData
, nAlloc
*sizeof(char*));
19729 shell_check_oom(azData
);
19730 abRowDiv
= sqlite3_realloc64(abRowDiv
, nAlloc
/nColumn
);
19731 shell_check_oom(abRowDiv
);
19733 abRowDiv
[nRow
] = 1;
19735 for(i
=0; i
<nColumn
; i
++){
19736 int wx
= p
->colWidth
[i
];
19738 wx
= p
->cmOpts
.iWrap
;
19740 if( wx
<0 ) wx
= -wx
;
19742 uz
= azNextLine
[i
];
19743 if( uz
==0 ) uz
= (u8
*)zEmpty
;
19744 }else if( p
->cmOpts
.bQuote
){
19745 sqlite3_free(azQuoted
[i
]);
19746 azQuoted
[i
] = quoted_column(pStmt
,i
);
19747 uz
= (const unsigned char*)azQuoted
[i
];
19749 uz
= (const unsigned char*)sqlite3_column_text(pStmt
,i
);
19750 if( uz
==0 ) uz
= (u8
*)zShowNull
;
19752 azData
[nRow
*nColumn
+ i
]
19753 = translateForDisplayAndDup(uz
, &azNextLine
[i
], wx
, bw
);
19754 if( azNextLine
[i
] ){
19756 abRowDiv
[nRow
-1] = 0;
19757 bMultiLineRowExists
= 1;
19760 }while( bNextLine
|| sqlite3_step(pStmt
)==SQLITE_ROW
);
19761 nTotal
= nColumn
*(nRow
+1);
19762 for(i
=0; i
<nTotal
; i
++){
19764 if( z
==0 ) z
= (char*)zEmpty
;
19767 if( n
>p
->actualWidth
[j
] ) p
->actualWidth
[j
] = n
;
19769 if( seenInterrupt
) goto columnar_end
;
19770 if( nColumn
==0 ) goto columnar_end
;
19771 switch( p
->cMode
){
19772 case MODE_Column
: {
19775 if( p
->showHeader
){
19776 for(i
=0; i
<nColumn
; i
++){
19777 w
= p
->actualWidth
[i
];
19778 if( p
->colWidth
[i
]<0 ) w
= -w
;
19779 utf8_width_print(p
->out
, w
, azData
[i
]);
19780 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
19782 for(i
=0; i
<nColumn
; i
++){
19783 print_dashes(p
->out
, p
->actualWidth
[i
]);
19784 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
19792 print_row_separator(p
, nColumn
, "+");
19793 fputs("| ", p
->out
);
19794 for(i
=0; i
<nColumn
; i
++){
19795 w
= p
->actualWidth
[i
];
19796 n
= strlenChar(azData
[i
]);
19797 utf8_printf(p
->out
, "%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
19798 fputs(i
==nColumn
-1?" |\n":" | ", p
->out
);
19800 print_row_separator(p
, nColumn
, "+");
19803 case MODE_Markdown
: {
19806 fputs("| ", p
->out
);
19807 for(i
=0; i
<nColumn
; i
++){
19808 w
= p
->actualWidth
[i
];
19809 n
= strlenChar(azData
[i
]);
19810 utf8_printf(p
->out
, "%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
19811 fputs(i
==nColumn
-1?" |\n":" | ", p
->out
);
19813 print_row_separator(p
, nColumn
, "|");
19817 colSep
= " " BOX_13
" ";
19818 rowSep
= " " BOX_13
"\n";
19819 print_box_row_separator(p
, nColumn
, BOX_23
, BOX_234
, BOX_34
);
19820 utf8_printf(p
->out
, BOX_13
" ");
19821 for(i
=0; i
<nColumn
; i
++){
19822 w
= p
->actualWidth
[i
];
19823 n
= strlenChar(azData
[i
]);
19824 utf8_printf(p
->out
, "%*s%s%*s%s",
19825 (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "",
19826 i
==nColumn
-1?" "BOX_13
"\n":" "BOX_13
" ");
19828 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
19832 for(i
=nColumn
, j
=0; i
<nTotal
; i
++, j
++){
19833 if( j
==0 && p
->cMode
!=MODE_Column
){
19834 utf8_printf(p
->out
, "%s", p
->cMode
==MODE_Box
?BOX_13
" ":"| ");
19837 if( z
==0 ) z
= p
->nullValue
;
19838 w
= p
->actualWidth
[j
];
19839 if( p
->colWidth
[j
]<0 ) w
= -w
;
19840 utf8_width_print(p
->out
, w
, z
);
19841 if( j
==nColumn
-1 ){
19842 utf8_printf(p
->out
, "%s", rowSep
);
19843 if( bMultiLineRowExists
&& abRowDiv
[i
/nColumn
-1] && i
+1<nTotal
){
19844 if( p
->cMode
==MODE_Table
){
19845 print_row_separator(p
, nColumn
, "+");
19846 }else if( p
->cMode
==MODE_Box
){
19847 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
19848 }else if( p
->cMode
==MODE_Column
){
19849 raw_printf(p
->out
, "\n");
19853 if( seenInterrupt
) goto columnar_end
;
19855 utf8_printf(p
->out
, "%s", colSep
);
19858 if( p
->cMode
==MODE_Table
){
19859 print_row_separator(p
, nColumn
, "+");
19860 }else if( p
->cMode
==MODE_Box
){
19861 print_box_row_separator(p
, nColumn
, BOX_12
, BOX_124
, BOX_14
);
19864 if( seenInterrupt
){
19865 utf8_printf(p
->out
, "Interrupt\n");
19867 nData
= (nRow
+1)*nColumn
;
19868 for(i
=0; i
<nData
; i
++){
19870 if( z
!=zEmpty
&& z
!=zShowNull
) free(azData
[i
]);
19872 sqlite3_free(azData
);
19873 sqlite3_free((void*)azNextLine
);
19874 sqlite3_free(abRowDiv
);
19876 for(i
=0; i
<nColumn
; i
++) sqlite3_free(azQuoted
[i
]);
19877 sqlite3_free(azQuoted
);
19882 ** Run a prepared statement
19884 static void exec_prepared_stmt(
19885 ShellState
*pArg
, /* Pointer to ShellState */
19886 sqlite3_stmt
*pStmt
/* Statement to run */
19889 sqlite3_uint64 nRow
= 0;
19891 if( pArg
->cMode
==MODE_Column
19892 || pArg
->cMode
==MODE_Table
19893 || pArg
->cMode
==MODE_Box
19894 || pArg
->cMode
==MODE_Markdown
19896 exec_prepared_stmt_columnar(pArg
, pStmt
);
19900 /* perform the first step. this will tell us if we
19901 ** have a result set or not and how wide it is.
19903 rc
= sqlite3_step(pStmt
);
19904 /* if we have a result set... */
19905 if( SQLITE_ROW
== rc
){
19906 /* allocate space for col name ptr, value ptr, and type */
19907 int nCol
= sqlite3_column_count(pStmt
);
19908 void *pData
= sqlite3_malloc64(3*nCol
*sizeof(const char*) + 1);
19910 shell_out_of_memory();
19912 char **azCols
= (char **)pData
; /* Names of result columns */
19913 char **azVals
= &azCols
[nCol
]; /* Results */
19914 int *aiTypes
= (int *)&azVals
[nCol
]; /* Result types */
19916 assert(sizeof(int) <= sizeof(char *));
19917 /* save off ptrs to column names */
19918 for(i
=0; i
<nCol
; i
++){
19919 azCols
[i
] = (char *)sqlite3_column_name(pStmt
, i
);
19923 /* extract the data and data types */
19924 for(i
=0; i
<nCol
; i
++){
19925 aiTypes
[i
] = x
= sqlite3_column_type(pStmt
, i
);
19928 && (pArg
->cMode
==MODE_Insert
|| pArg
->cMode
==MODE_Quote
)
19932 azVals
[i
] = (char*)sqlite3_column_text(pStmt
, i
);
19934 if( !azVals
[i
] && (aiTypes
[i
]!=SQLITE_NULL
) ){
19936 break; /* from for */
19940 /* if data and types extracted successfully... */
19941 if( SQLITE_ROW
== rc
){
19942 /* call the supplied callback with the result row data */
19943 if( shell_callback(pArg
, nCol
, azVals
, azCols
, aiTypes
) ){
19946 rc
= sqlite3_step(pStmt
);
19949 } while( SQLITE_ROW
== rc
);
19950 sqlite3_free(pData
);
19951 if( pArg
->cMode
==MODE_Json
){
19952 fputs("]\n", pArg
->out
);
19953 }else if( pArg
->cMode
==MODE_Count
){
19955 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%llu row%s\n",
19956 nRow
, nRow
!=1 ? "s" : "");
19957 printf("%s", zBuf
);
19963 #ifndef SQLITE_OMIT_VIRTUALTABLE
19965 ** This function is called to process SQL if the previous shell command
19966 ** was ".expert". It passes the SQL in the second argument directly to
19967 ** the sqlite3expert object.
19969 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
19970 ** code. In this case, (*pzErr) may be set to point to a buffer containing
19971 ** an English language error message. It is the responsibility of the
19972 ** caller to eventually free this buffer using sqlite3_free().
19974 static int expertHandleSQL(
19975 ShellState
*pState
,
19979 assert( pState
->expert
.pExpert
);
19980 assert( pzErr
==0 || *pzErr
==0 );
19981 return sqlite3_expert_sql(pState
->expert
.pExpert
, zSql
, pzErr
);
19985 ** This function is called either to silently clean up the object
19986 ** created by the ".expert" command (if bCancel==1), or to generate a
19987 ** report from it and then clean it up (if bCancel==0).
19989 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
19990 ** code. In this case, (*pzErr) may be set to point to a buffer containing
19991 ** an English language error message. It is the responsibility of the
19992 ** caller to eventually free this buffer using sqlite3_free().
19994 static int expertFinish(
19995 ShellState
*pState
,
19999 int rc
= SQLITE_OK
;
20000 sqlite3expert
*p
= pState
->expert
.pExpert
;
20002 assert( bCancel
|| pzErr
==0 || *pzErr
==0 );
20004 FILE *out
= pState
->out
;
20005 int bVerbose
= pState
->expert
.bVerbose
;
20007 rc
= sqlite3_expert_analyze(p
, pzErr
);
20008 if( rc
==SQLITE_OK
){
20009 int nQuery
= sqlite3_expert_count(p
);
20013 const char *zCand
= sqlite3_expert_report(p
,0,EXPERT_REPORT_CANDIDATES
);
20014 raw_printf(out
, "-- Candidates -----------------------------\n");
20015 raw_printf(out
, "%s\n", zCand
);
20017 for(i
=0; i
<nQuery
; i
++){
20018 const char *zSql
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_SQL
);
20019 const char *zIdx
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_INDEXES
);
20020 const char *zEQP
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_PLAN
);
20021 if( zIdx
==0 ) zIdx
= "(no new indexes)\n";
20023 raw_printf(out
, "-- Query %d --------------------------------\n",i
+1);
20024 raw_printf(out
, "%s\n\n", zSql
);
20026 raw_printf(out
, "%s\n", zIdx
);
20027 raw_printf(out
, "%s\n", zEQP
);
20031 sqlite3_expert_destroy(p
);
20032 pState
->expert
.pExpert
= 0;
20037 ** Implementation of ".expert" dot command.
20039 static int expertDotCommand(
20040 ShellState
*pState
, /* Current shell tool state */
20041 char **azArg
, /* Array of arguments passed to dot command */
20042 int nArg
/* Number of entries in azArg[] */
20044 int rc
= SQLITE_OK
;
20049 assert( pState
->expert
.pExpert
==0 );
20050 memset(&pState
->expert
, 0, sizeof(ExpertInfo
));
20052 for(i
=1; rc
==SQLITE_OK
&& i
<nArg
; i
++){
20053 char *z
= azArg
[i
];
20055 if( z
[0]=='-' && z
[1]=='-' ) z
++;
20057 if( n
>=2 && 0==cli_strncmp(z
, "-verbose", n
) ){
20058 pState
->expert
.bVerbose
= 1;
20060 else if( n
>=2 && 0==cli_strncmp(z
, "-sample", n
) ){
20062 raw_printf(stderr
, "option requires an argument: %s\n", z
);
20065 iSample
= (int)integerValue(azArg
[++i
]);
20066 if( iSample
<0 || iSample
>100 ){
20067 raw_printf(stderr
, "value out of range: %s\n", azArg
[i
]);
20073 raw_printf(stderr
, "unknown option: %s\n", z
);
20078 if( rc
==SQLITE_OK
){
20079 pState
->expert
.pExpert
= sqlite3_expert_new(pState
->db
, &zErr
);
20080 if( pState
->expert
.pExpert
==0 ){
20081 raw_printf(stderr
, "sqlite3_expert_new: %s\n",
20082 zErr
? zErr
: "out of memory");
20085 sqlite3_expert_config(
20086 pState
->expert
.pExpert
, EXPERT_CONFIG_SAMPLE
, iSample
20090 sqlite3_free(zErr
);
20094 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
20097 ** Execute a statement or set of statements. Print
20098 ** any result rows/columns depending on the current mode
20099 ** set via the supplied callback.
20101 ** This is very similar to SQLite's built-in sqlite3_exec()
20102 ** function except it takes a slightly different callback
20103 ** and callback data argument.
20105 static int shell_exec(
20106 ShellState
*pArg
, /* Pointer to ShellState */
20107 const char *zSql
, /* SQL to be evaluated */
20108 char **pzErrMsg
/* Error msg written here */
20110 sqlite3_stmt
*pStmt
= NULL
; /* Statement to execute. */
20111 int rc
= SQLITE_OK
; /* Return Code */
20113 const char *zLeftover
; /* Tail of unprocessed SQL */
20114 sqlite3
*db
= pArg
->db
;
20120 #ifndef SQLITE_OMIT_VIRTUALTABLE
20121 if( pArg
->expert
.pExpert
){
20122 rc
= expertHandleSQL(pArg
, zSql
, pzErrMsg
);
20123 return expertFinish(pArg
, (rc
!=SQLITE_OK
), pzErrMsg
);
20127 while( zSql
[0] && (SQLITE_OK
== rc
) ){
20128 static const char *zStmtSql
;
20129 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zLeftover
);
20130 if( SQLITE_OK
!= rc
){
20132 *pzErrMsg
= save_err_msg(db
, "in prepare", rc
, zSql
);
20136 /* this happens for a comment or white-space */
20138 while( IsSpace(zSql
[0]) ) zSql
++;
20141 zStmtSql
= sqlite3_sql(pStmt
);
20142 if( zStmtSql
==0 ) zStmtSql
= "";
20143 while( IsSpace(zStmtSql
[0]) ) zStmtSql
++;
20145 /* save off the prepared statement handle and reset row count */
20147 pArg
->pStmt
= pStmt
;
20151 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
20152 if( pArg
&& pArg
->autoEQP
&& sqlite3_stmt_isexplain(pStmt
)==0 ){
20153 sqlite3_stmt
*pExplain
;
20154 int triggerEQP
= 0;
20155 disable_debug_trace_modes();
20156 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, -1, &triggerEQP
);
20157 if( pArg
->autoEQP
>=AUTOEQP_trigger
){
20158 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, 0);
20161 sqlite3_reset(pExplain
);
20162 rc
= sqlite3_stmt_explain(pExplain
, 2);
20163 if( rc
==SQLITE_OK
){
20164 while( sqlite3_step(pExplain
)==SQLITE_ROW
){
20165 const char *zEQPLine
= (const char*)sqlite3_column_text(pExplain
,3);
20166 int iEqpId
= sqlite3_column_int(pExplain
, 0);
20167 int iParentId
= sqlite3_column_int(pExplain
, 1);
20168 if( zEQPLine
==0 ) zEQPLine
= "";
20169 if( zEQPLine
[0]=='-' ) eqp_render(pArg
, 0);
20170 eqp_append(pArg
, iEqpId
, iParentId
, zEQPLine
);
20172 eqp_render(pArg
, 0);
20174 if( pArg
->autoEQP
>=AUTOEQP_full
){
20175 /* Also do an EXPLAIN for ".eqp full" mode */
20176 sqlite3_reset(pExplain
);
20177 rc
= sqlite3_stmt_explain(pExplain
, 1);
20178 if( rc
==SQLITE_OK
){
20179 pArg
->cMode
= MODE_Explain
;
20180 assert( sqlite3_stmt_isexplain(pExplain
)==1 );
20181 explain_data_prepare(pArg
, pExplain
);
20182 exec_prepared_stmt(pArg
, pExplain
);
20183 explain_data_delete(pArg
);
20186 if( pArg
->autoEQP
>=AUTOEQP_trigger
&& triggerEQP
==0 ){
20187 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 0, 0);
20189 sqlite3_reset(pStmt
);
20190 sqlite3_stmt_explain(pStmt
, 0);
20191 restore_debug_trace_modes();
20195 int bIsExplain
= (sqlite3_stmt_isexplain(pStmt
)==1);
20196 pArg
->cMode
= pArg
->mode
;
20197 if( pArg
->autoExplain
){
20199 pArg
->cMode
= MODE_Explain
;
20201 if( sqlite3_stmt_isexplain(pStmt
)==2 ){
20202 pArg
->cMode
= MODE_EQP
;
20206 /* If the shell is currently in ".explain" mode, gather the extra
20207 ** data required to add indents to the output.*/
20208 if( pArg
->cMode
==MODE_Explain
&& bIsExplain
){
20209 explain_data_prepare(pArg
, pStmt
);
20213 bind_prepared_stmt(pArg
, pStmt
);
20214 exec_prepared_stmt(pArg
, pStmt
);
20215 explain_data_delete(pArg
);
20216 eqp_render(pArg
, 0);
20218 /* print usage stats if stats on */
20219 if( pArg
&& pArg
->statsOn
){
20220 display_stats(db
, pArg
, 0);
20223 /* print loop-counters if required */
20224 if( pArg
&& pArg
->scanstatsOn
){
20225 display_scanstats(db
, pArg
);
20228 /* Finalize the statement just executed. If this fails, save a
20229 ** copy of the error message. Otherwise, set zSql to point to the
20230 ** next statement to execute. */
20231 rc2
= sqlite3_finalize(pStmt
);
20232 if( rc
!=SQLITE_NOMEM
) rc
= rc2
;
20233 if( rc
==SQLITE_OK
){
20235 while( IsSpace(zSql
[0]) ) zSql
++;
20236 }else if( pzErrMsg
){
20237 *pzErrMsg
= save_err_msg(db
, "stepping", rc
, 0);
20240 /* clear saved stmt handle */
20242 pArg
->pStmt
= NULL
;
20251 ** Release memory previously allocated by tableColumnList().
20253 static void freeColumnList(char **azCol
){
20255 for(i
=1; azCol
[i
]; i
++){
20256 sqlite3_free(azCol
[i
]);
20258 /* azCol[0] is a static string */
20259 sqlite3_free(azCol
);
20263 ** Return a list of pointers to strings which are the names of all
20264 ** columns in table zTab. The memory to hold the names is dynamically
20265 ** allocated and must be released by the caller using a subsequent call
20266 ** to freeColumnList().
20268 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
20269 ** value that needs to be preserved, then azCol[0] is filled in with the
20270 ** name of the rowid column.
20272 ** The first regular column in the table is azCol[1]. The list is terminated
20273 ** by an entry with azCol[i]==0.
20275 static char **tableColumnList(ShellState
*p
, const char *zTab
){
20277 sqlite3_stmt
*pStmt
;
20281 int nPK
= 0; /* Number of PRIMARY KEY columns seen */
20282 int isIPK
= 0; /* True if one PRIMARY KEY column of type INTEGER */
20283 int preserveRowid
= ShellHasFlag(p
, SHFLG_PreserveRowid
);
20286 zSql
= sqlite3_mprintf("PRAGMA table_info=%Q", zTab
);
20287 shell_check_oom(zSql
);
20288 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
20289 sqlite3_free(zSql
);
20291 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
20292 if( nCol
>=nAlloc
-2 ){
20293 nAlloc
= nAlloc
*2 + nCol
+ 10;
20294 azCol
= sqlite3_realloc(azCol
, nAlloc
*sizeof(azCol
[0]));
20295 shell_check_oom(azCol
);
20297 azCol
[++nCol
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 1));
20298 shell_check_oom(azCol
[nCol
]);
20299 if( sqlite3_column_int(pStmt
, 5) ){
20302 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt
,2),
20311 sqlite3_finalize(pStmt
);
20312 if( azCol
==0 ) return 0;
20316 /* The decision of whether or not a rowid really needs to be preserved
20317 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
20318 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
20319 ** rowids on tables where the rowid is inaccessible because there are other
20320 ** columns in the table named "rowid", "_rowid_", and "oid".
20322 if( preserveRowid
&& isIPK
){
20323 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
20324 ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID
20325 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
20326 ** ROWID aliases. To distinguish these cases, check to see if
20327 ** there is a "pk" entry in "PRAGMA index_list". There will be
20328 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
20330 zSql
= sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
20331 " WHERE origin='pk'", zTab
);
20332 shell_check_oom(zSql
);
20333 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
20334 sqlite3_free(zSql
);
20336 freeColumnList(azCol
);
20339 rc
= sqlite3_step(pStmt
);
20340 sqlite3_finalize(pStmt
);
20341 preserveRowid
= rc
==SQLITE_ROW
;
20343 if( preserveRowid
){
20344 /* Only preserve the rowid if we can find a name to use for the
20346 static char *azRowid
[] = { "rowid", "_rowid_", "oid" };
20348 for(j
=0; j
<3; j
++){
20349 for(i
=1; i
<=nCol
; i
++){
20350 if( sqlite3_stricmp(azRowid
[j
],azCol
[i
])==0 ) break;
20353 /* At this point, we know that azRowid[j] is not the name of any
20354 ** ordinary column in the table. Verify that azRowid[j] is a valid
20355 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
20356 ** tables will fail this last check */
20357 rc
= sqlite3_table_column_metadata(p
->db
,0,zTab
,azRowid
[j
],0,0,0,0,0);
20358 if( rc
==SQLITE_OK
) azCol
[0] = azRowid
[j
];
20367 ** Toggle the reverse_unordered_selects setting.
20369 static void toggleSelectOrder(sqlite3
*db
){
20370 sqlite3_stmt
*pStmt
= 0;
20373 sqlite3_prepare_v2(db
, "PRAGMA reverse_unordered_selects", -1, &pStmt
, 0);
20374 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
20375 iSetting
= sqlite3_column_int(pStmt
, 0);
20377 sqlite3_finalize(pStmt
);
20378 sqlite3_snprintf(sizeof(zStmt
), zStmt
,
20379 "PRAGMA reverse_unordered_selects(%d)", !iSetting
);
20380 sqlite3_exec(db
, zStmt
, 0, 0, 0);
20384 ** This is a different callback routine used for dumping the database.
20385 ** Each row received by this callback consists of a table name,
20386 ** the table type ("index" or "table") and SQL to create the table.
20387 ** This routine should print text sufficient to recreate the table.
20389 static int dump_callback(void *pArg
, int nArg
, char **azArg
, char **azNotUsed
){
20391 const char *zTable
;
20394 ShellState
*p
= (ShellState
*)pArg
;
20398 UNUSED_PARAMETER(azNotUsed
);
20399 if( nArg
!=3 || azArg
==0 ) return 0;
20403 if( zTable
==0 ) return 0;
20404 if( zType
==0 ) return 0;
20405 dataOnly
= (p
->shellFlgs
& SHFLG_DumpDataOnly
)!=0;
20406 noSys
= (p
->shellFlgs
& SHFLG_DumpNoSys
)!=0;
20408 if( cli_strcmp(zTable
, "sqlite_sequence")==0 && !noSys
){
20409 if( !dataOnly
) raw_printf(p
->out
, "DELETE FROM sqlite_sequence;\n");
20410 }else if( sqlite3_strglob("sqlite_stat?", zTable
)==0 && !noSys
){
20411 if( !dataOnly
) raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
20412 }else if( cli_strncmp(zTable
, "sqlite_", 7)==0 ){
20414 }else if( dataOnly
){
20416 }else if( cli_strncmp(zSql
, "CREATE VIRTUAL TABLE", 20)==0 ){
20418 if( !p
->writableSchema
){
20419 raw_printf(p
->out
, "PRAGMA writable_schema=ON;\n");
20420 p
->writableSchema
= 1;
20422 zIns
= sqlite3_mprintf(
20423 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
20424 "VALUES('table','%q','%q',0,'%q');",
20425 zTable
, zTable
, zSql
);
20426 shell_check_oom(zIns
);
20427 utf8_printf(p
->out
, "%s\n", zIns
);
20428 sqlite3_free(zIns
);
20431 printSchemaLine(p
->out
, zSql
, ";\n");
20434 if( cli_strcmp(zType
, "table")==0 ){
20439 char *savedDestTable
;
20442 azCol
= tableColumnList(p
, zTable
);
20448 /* Always quote the table name, even if it appears to be pure ascii,
20449 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
20451 appendText(&sTable
, zTable
, quoteChar(zTable
));
20452 /* If preserving the rowid, add a column list after the table name.
20453 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
20454 ** instead of the usual "INSERT INTO tab VALUES(...)".
20457 appendText(&sTable
, "(", 0);
20458 appendText(&sTable
, azCol
[0], 0);
20459 for(i
=1; azCol
[i
]; i
++){
20460 appendText(&sTable
, ",", 0);
20461 appendText(&sTable
, azCol
[i
], quoteChar(azCol
[i
]));
20463 appendText(&sTable
, ")", 0);
20466 /* Build an appropriate SELECT statement */
20467 initText(&sSelect
);
20468 appendText(&sSelect
, "SELECT ", 0);
20470 appendText(&sSelect
, azCol
[0], 0);
20471 appendText(&sSelect
, ",", 0);
20473 for(i
=1; azCol
[i
]; i
++){
20474 appendText(&sSelect
, azCol
[i
], quoteChar(azCol
[i
]));
20476 appendText(&sSelect
, ",", 0);
20479 freeColumnList(azCol
);
20480 appendText(&sSelect
, " FROM ", 0);
20481 appendText(&sSelect
, zTable
, quoteChar(zTable
));
20483 savedDestTable
= p
->zDestTable
;
20484 savedMode
= p
->mode
;
20485 p
->zDestTable
= sTable
.z
;
20486 p
->mode
= p
->cMode
= MODE_Insert
;
20487 rc
= shell_exec(p
, sSelect
.z
, 0);
20488 if( (rc
&0xff)==SQLITE_CORRUPT
){
20489 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
20490 toggleSelectOrder(p
->db
);
20491 shell_exec(p
, sSelect
.z
, 0);
20492 toggleSelectOrder(p
->db
);
20494 p
->zDestTable
= savedDestTable
;
20495 p
->mode
= savedMode
;
20497 freeText(&sSelect
);
20498 if( rc
) p
->nErr
++;
20504 ** Run zQuery. Use dump_callback() as the callback routine so that
20505 ** the contents of the query are output as SQL statements.
20507 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
20508 ** "ORDER BY rowid DESC" to the end.
20510 static int run_schema_dump_query(
20516 rc
= sqlite3_exec(p
->db
, zQuery
, dump_callback
, p
, &zErr
);
20517 if( rc
==SQLITE_CORRUPT
){
20519 int len
= strlen30(zQuery
);
20520 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
20522 utf8_printf(p
->out
, "/****** %s ******/\n", zErr
);
20523 sqlite3_free(zErr
);
20526 zQ2
= malloc( len
+100 );
20527 if( zQ2
==0 ) return rc
;
20528 sqlite3_snprintf(len
+100, zQ2
, "%s ORDER BY rowid DESC", zQuery
);
20529 rc
= sqlite3_exec(p
->db
, zQ2
, dump_callback
, p
, &zErr
);
20531 utf8_printf(p
->out
, "/****** ERROR: %s ******/\n", zErr
);
20533 rc
= SQLITE_CORRUPT
;
20535 sqlite3_free(zErr
);
20542 ** Text of help messages.
20544 ** The help text for each individual command begins with a line that starts
20545 ** with ".". Subsequent lines are supplemental information.
20547 ** There must be two or more spaces between the end of the command and the
20548 ** start of the description of what that command does.
20550 static const char *(azHelp
[]) = {
20551 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
20552 && !defined(SQLITE_SHELL_FIDDLE)
20553 ".archive ... Manage SQL archives",
20554 " Each command must have exactly one of the following options:",
20555 " -c, --create Create a new archive",
20556 " -u, --update Add or update files with changed mtime",
20557 " -i, --insert Like -u but always add even if unchanged",
20558 " -r, --remove Remove files from archive",
20559 " -t, --list List contents of archive",
20560 " -x, --extract Extract files from archive",
20561 " Optional arguments:",
20562 " -v, --verbose Print each filename as it is processed",
20563 " -f FILE, --file FILE Use archive FILE (default is current db)",
20564 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
20565 " -C DIR, --directory DIR Read/extract files from directory DIR",
20566 " -g, --glob Use glob matching for names in archive",
20567 " -n, --dryrun Show the SQL that would have occurred",
20569 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
20570 " .ar -tf ARCHIVE # List members of ARCHIVE",
20571 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
20573 " http://sqlite.org/cli.html#sqlite_archive_support",
20575 #ifndef SQLITE_OMIT_AUTHORIZATION
20576 ".auth ON|OFF Show authorizer callbacks",
20578 #ifndef SQLITE_SHELL_FIDDLE
20579 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
20581 " --append Use the appendvfs",
20582 " --async Write to FILE without journal and fsync()",
20584 ".bail on|off Stop after hitting an error. Default OFF",
20585 #ifndef SQLITE_SHELL_FIDDLE
20586 ".cd DIRECTORY Change the working directory to DIRECTORY",
20588 ".changes on|off Show number of rows changed by SQL",
20589 #ifndef SQLITE_SHELL_FIDDLE
20590 ".check GLOB Fail if output since .testcase does not match",
20591 ".clone NEWDB Clone data into NEWDB from the existing database",
20593 ".connection [close] [#] Open or close an auxiliary database connection",
20594 #if defined(_WIN32) || defined(WIN32)
20595 ".crnl on|off Translate \\n to \\r\\n. Default ON",
20597 ".databases List names and files of attached databases",
20598 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
20599 #if SQLITE_SHELL_HAVE_RECOVER
20600 ".dbinfo ?DB? Show status information about the database",
20602 ".dump ?OBJECTS? Render database content as SQL",
20604 " --data-only Output only INSERT statements",
20605 " --newlines Allow unescaped newline characters in output",
20606 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
20607 " --preserve-rowids Include ROWID values in the output",
20608 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
20609 " Additional LIKE patterns can be given in subsequent arguments",
20610 ".echo on|off Turn command echo on or off",
20611 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
20613 #ifdef SQLITE_DEBUG
20614 " test Show raw EXPLAIN QUERY PLAN output",
20615 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
20617 " trigger Like \"full\" but also show trigger bytecode",
20618 #ifndef SQLITE_SHELL_FIDDLE
20619 ".excel Display the output of next command in spreadsheet",
20620 " --bom Put a UTF8 byte-order mark on intermediate file",
20622 #ifndef SQLITE_SHELL_FIDDLE
20623 ".exit ?CODE? Exit this program with return-code CODE",
20625 ".expert EXPERIMENTAL. Suggest indexes for queries",
20626 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
20627 ".filectrl CMD ... Run various sqlite3_file_control() operations",
20628 " --schema SCHEMA Use SCHEMA instead of \"main\"",
20629 " --help Show CMD details",
20630 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
20631 ".headers on|off Turn display of headers on or off",
20632 ".help ?-all? ?PATTERN? Show help text for PATTERN",
20633 #ifndef SQLITE_SHELL_FIDDLE
20634 ".import FILE TABLE Import data from FILE into TABLE",
20636 " --ascii Use \\037 and \\036 as column and row separators",
20637 " --csv Use , and \\n as column and row separators",
20638 " --skip N Skip the first N rows of input",
20639 " --schema S Target table to be S.TABLE",
20640 " -v \"Verbose\" - increase auxiliary output",
20642 " * If TABLE does not exist, it is created. The first row of input",
20643 " determines the column names.",
20644 " * If neither --csv or --ascii are used, the input mode is derived",
20645 " from the \".mode\" output mode",
20646 " * If FILE begins with \"|\" then it is a command that generates the",
20649 #ifndef SQLITE_OMIT_TEST_CONTROL
20650 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
20652 ".indexes ?TABLE? Show names of indexes",
20653 " If TABLE is specified, only show indexes for",
20654 " tables matching TABLE using the LIKE operator.",
20655 #ifdef SQLITE_ENABLE_IOTRACE
20656 ",iotrace FILE Enable I/O diagnostic logging to FILE",
20658 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
20659 ".lint OPTIONS Report potential schema issues.",
20661 " fkey-indexes Find missing foreign key indexes",
20662 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
20663 ".load FILE ?ENTRY? Load an extension library",
20665 #if !defined(SQLITE_SHELL_FIDDLE)
20666 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
20668 ".log on|off Turn logging on or off.",
20670 ".mode MODE ?OPTIONS? Set output mode",
20671 " MODE is one of:",
20672 " ascii Columns/rows delimited by 0x1F and 0x1E",
20673 " box Tables using unicode box-drawing characters",
20674 " csv Comma-separated values",
20675 " column Output in columns. (See .width)",
20676 " html HTML <table> code",
20677 " insert SQL insert statements for TABLE",
20678 " json Results in a JSON array",
20679 " line One value per line",
20680 " list Values delimited by \"|\"",
20681 " markdown Markdown table format",
20682 " qbox Shorthand for \"box --wrap 60 --quote\"",
20683 " quote Escape answers as for SQL",
20684 " table ASCII-art table",
20685 " tabs Tab-separated values",
20686 " tcl TCL list elements",
20687 " OPTIONS: (for columnar modes or insert mode):",
20688 " --wrap N Wrap output lines to no longer than N characters",
20689 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
20690 " --ww Shorthand for \"--wordwrap 1\"",
20691 " --quote Quote output text as SQL literals",
20692 " --noquote Do not quote output text",
20693 " TABLE The name of SQL table used for \"insert\" mode",
20694 #ifndef SQLITE_SHELL_FIDDLE
20695 ".nonce STRING Suspend safe mode for one command if nonce matches",
20697 ".nullvalue STRING Use STRING in place of NULL values",
20698 #ifndef SQLITE_SHELL_FIDDLE
20699 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
20700 " If FILE begins with '|' then open as a pipe",
20701 " --bom Put a UTF8 byte-order mark at the beginning",
20702 " -e Send output to the system text editor",
20703 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
20704 /* Note that .open is (partially) available in WASM builds but is
20705 ** currently only intended to be used by the fiddle tool, not
20706 ** end users, so is "undocumented." */
20707 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
20709 " --append Use appendvfs to append database to the end of FILE",
20711 #ifndef SQLITE_OMIT_DESERIALIZE
20712 " --deserialize Load into memory using sqlite3_deserialize()",
20713 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
20714 " --maxsize N Maximum size for --hexdb or --deserialized database",
20716 " --new Initialize FILE to an empty database",
20717 " --nofollow Do not follow symbolic links",
20718 " --readonly Open FILE readonly",
20719 " --zip FILE is a ZIP archive",
20720 #ifndef SQLITE_SHELL_FIDDLE
20721 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
20722 " If FILE begins with '|' then open it as a pipe.",
20724 " --bom Prefix output with a UTF8 byte-order mark",
20725 " -e Send output to the system text editor",
20726 " -x Send output as CSV to a spreadsheet",
20728 ".parameter CMD ... Manage SQL parameter bindings",
20729 " clear Erase all bindings",
20730 " init Initialize the TEMP table that holds bindings",
20731 " list List the current parameter bindings",
20732 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
20733 " PARAMETER should start with one of: $ : @ ?",
20734 " unset PARAMETER Remove PARAMETER from the binding table",
20735 ".print STRING... Print literal STRING",
20736 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
20737 ".progress N Invoke progress handler after every N opcodes",
20738 " --limit N Interrupt after N progress callbacks",
20739 " --once Do no more than one progress interrupt",
20740 " --quiet|-q No output except at interrupts",
20741 " --reset Reset the count for each input and interrupt",
20743 ".prompt MAIN CONTINUE Replace the standard prompts",
20744 #ifndef SQLITE_SHELL_FIDDLE
20745 ".quit Stop interpreting input stream, exit if primary.",
20746 ".read FILE Read input from FILE or command output",
20747 " If FILE begins with \"|\", it is a command that generates the input.",
20749 #if SQLITE_SHELL_HAVE_RECOVER
20750 ".recover Recover as much data as possible from corrupt db.",
20751 " --ignore-freelist Ignore pages that appear to be on db freelist",
20752 " --lost-and-found TABLE Alternative name for the lost-and-found table",
20753 " --no-rowids Do not attempt to recover rowid values",
20754 " that are not also INTEGER PRIMARY KEYs",
20756 #ifndef SQLITE_SHELL_FIDDLE
20757 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
20758 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
20760 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
20761 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
20763 " --indent Try to pretty-print the schema",
20764 " --nosys Omit objects whose names start with \"sqlite_\"",
20765 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
20767 " --init Create a new SELFTEST table",
20768 " -v Verbose output",
20769 ".separator COL ?ROW? Change the column and row separators",
20770 #if defined(SQLITE_ENABLE_SESSION)
20771 ".session ?NAME? CMD ... Create or control sessions",
20773 " attach TABLE Attach TABLE",
20774 " changeset FILE Write a changeset into FILE",
20775 " close Close one session",
20776 " enable ?BOOLEAN? Set or query the enable bit",
20777 " filter GLOB... Reject tables matching GLOBs",
20778 " indirect ?BOOLEAN? Mark or query the indirect status",
20779 " isempty Query whether the session is empty",
20780 " list List currently open session names",
20781 " open DB NAME Open a new session on DB",
20782 " patchset FILE Write a patchset into FILE",
20783 " If ?NAME? is omitted, the first defined session is used.",
20785 ".sha3sum ... Compute a SHA3 hash of database content",
20787 " --schema Also hash the sqlite_schema table",
20788 " --sha3-224 Use the sha3-224 algorithm",
20789 " --sha3-256 Use the sha3-256 algorithm (default)",
20790 " --sha3-384 Use the sha3-384 algorithm",
20791 " --sha3-512 Use the sha3-512 algorithm",
20792 " Any other argument is a LIKE pattern for tables to hash",
20793 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
20794 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
20796 ".show Show the current values for various settings",
20797 ".stats ?ARG? Show stats or turn stats on or off",
20798 " off Turn off automatic stat display",
20799 " on Turn on automatic stat display",
20800 " stmt Show statement stats",
20801 " vmstep Show the virtual machine step count only",
20802 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
20803 ".system CMD ARGS... Run CMD ARGS... in a system shell",
20805 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
20806 #ifndef SQLITE_SHELL_FIDDLE
20807 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
20809 ",testctrl CMD ... Run various sqlite3_test_control() operations",
20810 " Run \".testctrl\" with no arguments for details",
20811 ".timeout MS Try opening locked tables for MS milliseconds",
20812 ".timer on|off Turn SQL timer on or off",
20813 #ifndef SQLITE_OMIT_TRACE
20814 ".trace ?OPTIONS? Output each SQL statement as it is run",
20815 " FILE Send output to FILE",
20816 " stdout Send output to stdout",
20817 " stderr Send output to stderr",
20818 " off Disable tracing",
20819 " --expanded Expand query parameters",
20820 #ifdef SQLITE_ENABLE_NORMALIZE
20821 " --normalized Normal the SQL statements",
20823 " --plain Show SQL as it is input",
20824 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
20825 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
20826 " --row Trace each row (SQLITE_TRACE_ROW)",
20827 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
20828 #endif /* SQLITE_OMIT_TRACE */
20829 #ifdef SQLITE_DEBUG
20830 ".unmodule NAME ... Unregister virtual table modules",
20831 " --allexcept Unregister everything except those named",
20833 ".version Show source, library and compiler versions",
20834 ".vfsinfo ?AUX? Information about the top-level VFS",
20835 ".vfslist List all available VFSes",
20836 ".vfsname ?AUX? Print the name of the VFS stack",
20837 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
20838 " Negative values right-justify",
20842 ** Output help text.
20844 ** zPattern describes the set of commands for which help text is provided.
20845 ** If zPattern is NULL, then show all commands, but only give a one-line
20846 ** description of each.
20848 ** Return the number of matches.
20850 static int showHelp(FILE *out
, const char *zPattern
){
20856 || zPattern
[0]=='0'
20857 || cli_strcmp(zPattern
,"-a")==0
20858 || cli_strcmp(zPattern
,"-all")==0
20859 || cli_strcmp(zPattern
,"--all")==0
20861 enum HelpWanted
{ HW_NoCull
= 0, HW_SummaryOnly
= 1, HW_Undoc
= 2 };
20862 enum HelpHave
{ HH_Undoc
= 2, HH_Summary
= 1, HH_More
= 0 };
20863 /* Show all or most commands
20864 ** *zPattern==0 => summary of documented commands only
20865 ** *zPattern=='0' => whole help for undocumented commands
20866 ** Otherwise => whole help for documented commands
20868 enum HelpWanted hw
= HW_SummaryOnly
;
20869 enum HelpHave hh
= HH_More
;
20871 hw
= (*zPattern
=='0')? HW_NoCull
|HW_Undoc
: HW_NoCull
;
20873 for(i
=0; i
<ArraySize(azHelp
); i
++){
20874 switch( azHelp
[i
][0] ){
20876 hh
= HH_Summary
|HH_Undoc
;
20885 if( ((hw
^hh
)&HH_Undoc
)==0 ){
20886 if( (hh
&HH_Summary
)!=0 ){
20887 utf8_printf(out
, ".%s\n", azHelp
[i
]+1);
20889 }else if( (hw
&HW_SummaryOnly
)==0 ){
20890 utf8_printf(out
, "%s\n", azHelp
[i
]);
20895 /* Seek documented commands for which zPattern is an exact prefix */
20896 zPat
= sqlite3_mprintf(".%s*", zPattern
);
20897 shell_check_oom(zPat
);
20898 for(i
=0; i
<ArraySize(azHelp
); i
++){
20899 if( sqlite3_strglob(zPat
, azHelp
[i
])==0 ){
20900 utf8_printf(out
, "%s\n", azHelp
[i
]);
20905 sqlite3_free(zPat
);
20908 /* when zPattern is a prefix of exactly one command, then include
20909 ** the details of that command, which should begin at offset j */
20910 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
][0]==' ' ){
20911 utf8_printf(out
, "%s\n", azHelp
[j
]);
20917 /* Look for documented commands that contain zPattern anywhere.
20918 ** Show complete text of all documented commands that match. */
20919 zPat
= sqlite3_mprintf("%%%s%%", zPattern
);
20920 shell_check_oom(zPat
);
20921 for(i
=0; i
<ArraySize(azHelp
); i
++){
20922 if( azHelp
[i
][0]==',' ){
20923 while( i
<ArraySize(azHelp
)-1 && azHelp
[i
+1][0]==' ' ) ++i
;
20926 if( azHelp
[i
][0]=='.' ) j
= i
;
20927 if( sqlite3_strlike(zPat
, azHelp
[i
], 0)==0 ){
20928 utf8_printf(out
, "%s\n", azHelp
[j
]);
20929 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
+1][0]==' ' ){
20931 utf8_printf(out
, "%s\n", azHelp
[j
]);
20937 sqlite3_free(zPat
);
20942 /* Forward reference */
20943 static int process_input(ShellState
*p
);
20946 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
20947 ** and return a pointer to the buffer. The caller is responsible for freeing
20950 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
20953 ** For convenience, a nul-terminator byte is always appended to the data read
20954 ** from the file before the buffer is returned. This byte is not included in
20955 ** the final value of (*pnByte), if applicable.
20957 ** NULL is returned if any error is encountered. The final value of *pnByte
20958 ** is undefined in this case.
20960 static char *readFile(const char *zName
, int *pnByte
){
20961 FILE *in
= fopen(zName
, "rb");
20966 if( in
==0 ) return 0;
20967 rc
= fseek(in
, 0, SEEK_END
);
20969 raw_printf(stderr
, "Error: '%s' not seekable\n", zName
);
20975 pBuf
= sqlite3_malloc64( nIn
+1 );
20977 raw_printf(stderr
, "Error: out of memory\n");
20981 nRead
= fread(pBuf
, nIn
, 1, in
);
20984 sqlite3_free(pBuf
);
20985 raw_printf(stderr
, "Error: cannot read '%s'\n", zName
);
20989 if( pnByte
) *pnByte
= nIn
;
20993 #if defined(SQLITE_ENABLE_SESSION)
20995 ** Close a single OpenSession object and release all of its associated
20998 static void session_close(OpenSession
*pSession
){
21000 sqlite3session_delete(pSession
->p
);
21001 sqlite3_free(pSession
->zName
);
21002 for(i
=0; i
<pSession
->nFilter
; i
++){
21003 sqlite3_free(pSession
->azFilter
[i
]);
21005 sqlite3_free(pSession
->azFilter
);
21006 memset(pSession
, 0, sizeof(OpenSession
));
21011 ** Close all OpenSession objects and release all associated resources.
21013 #if defined(SQLITE_ENABLE_SESSION)
21014 static void session_close_all(ShellState
*p
, int i
){
21016 struct AuxDb
*pAuxDb
= i
<0 ? p
->pAuxDb
: &p
->aAuxDb
[i
];
21017 for(j
=0; j
<pAuxDb
->nSession
; j
++){
21018 session_close(&pAuxDb
->aSession
[j
]);
21020 pAuxDb
->nSession
= 0;
21023 # define session_close_all(X,Y)
21027 ** Implementation of the xFilter function for an open session. Omit
21028 ** any tables named by ".session filter" but let all other table through.
21030 #if defined(SQLITE_ENABLE_SESSION)
21031 static int session_filter(void *pCtx
, const char *zTab
){
21032 OpenSession
*pSession
= (OpenSession
*)pCtx
;
21034 for(i
=0; i
<pSession
->nFilter
; i
++){
21035 if( sqlite3_strglob(pSession
->azFilter
[i
], zTab
)==0 ) return 0;
21042 ** Try to deduce the type of file for zName based on its content. Return
21043 ** one of the SHELL_OPEN_* constants.
21045 ** If the file does not exist or is empty but its name looks like a ZIP
21046 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
21047 ** Otherwise, assume an ordinary database regardless of the filename if
21048 ** the type cannot be determined from content.
21050 int deduceDatabaseType(const char *zName
, int dfltZip
){
21051 FILE *f
= fopen(zName
, "rb");
21053 int rc
= SHELL_OPEN_UNSPEC
;
21056 if( dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
21057 return SHELL_OPEN_ZIPFILE
;
21059 return SHELL_OPEN_NORMAL
;
21062 n
= fread(zBuf
, 16, 1, f
);
21063 if( n
==1 && memcmp(zBuf
, "SQLite format 3", 16)==0 ){
21065 return SHELL_OPEN_NORMAL
;
21067 fseek(f
, -25, SEEK_END
);
21068 n
= fread(zBuf
, 25, 1, f
);
21069 if( n
==1 && memcmp(zBuf
, "Start-Of-SQLite3-", 17)==0 ){
21070 rc
= SHELL_OPEN_APPENDVFS
;
21072 fseek(f
, -22, SEEK_END
);
21073 n
= fread(zBuf
, 22, 1, f
);
21074 if( n
==1 && zBuf
[0]==0x50 && zBuf
[1]==0x4b && zBuf
[2]==0x05
21075 && zBuf
[3]==0x06 ){
21076 rc
= SHELL_OPEN_ZIPFILE
;
21077 }else if( n
==0 && dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
21078 rc
= SHELL_OPEN_ZIPFILE
;
21085 #ifndef SQLITE_OMIT_DESERIALIZE
21087 ** Reconstruct an in-memory database using the output from the "dbtotxt"
21088 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
21089 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
21091 static unsigned char *readHexDb(ShellState
*p
, int *pnData
){
21092 unsigned char *a
= 0;
21100 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
21101 unsigned int x
[16];
21104 in
= fopen(zDbFilename
, "r");
21106 utf8_printf(stderr
, "cannot open \"%s\" for reading\n", zDbFilename
);
21113 if( in
==0 ) in
= stdin
;
21117 if( fgets(zLine
, sizeof(zLine
), in
)==0 ) goto readHexDb_error
;
21118 rc
= sscanf(zLine
, "| size %d pagesize %d", &n
, &pgsz
);
21119 if( rc
!=2 ) goto readHexDb_error
;
21120 if( n
<0 ) goto readHexDb_error
;
21121 if( pgsz
<512 || pgsz
>65536 || (pgsz
&(pgsz
-1))!=0 ) goto readHexDb_error
;
21122 n
= (n
+pgsz
-1)&~(pgsz
-1); /* Round n up to the next multiple of pgsz */
21123 a
= sqlite3_malloc( n
? n
: 1 );
21124 shell_check_oom(a
);
21126 if( pgsz
<512 || pgsz
>65536 || (pgsz
& (pgsz
-1))!=0 ){
21127 utf8_printf(stderr
, "invalid pagesize\n");
21128 goto readHexDb_error
;
21130 for(nLine
++; fgets(zLine
, sizeof(zLine
), in
)!=0; nLine
++){
21131 rc
= sscanf(zLine
, "| page %d offset %d", &j
, &k
);
21136 if( cli_strncmp(zLine
, "| end ", 6)==0 ){
21139 rc
= sscanf(zLine
,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
21140 &j
, &x
[0], &x
[1], &x
[2], &x
[3], &x
[4], &x
[5], &x
[6], &x
[7],
21141 &x
[8], &x
[9], &x
[10], &x
[11], &x
[12], &x
[13], &x
[14], &x
[15]);
21144 if( k
+16<=n
&& k
>=0 ){
21146 for(ii
=0; ii
<16; ii
++) a
[k
+ii
] = x
[ii
]&0xff;
21162 while( fgets(zLine
, sizeof(zLine
), p
->in
)!=0 ){
21164 if(cli_strncmp(zLine
, "| end ", 6)==0 ) break;
21169 utf8_printf(stderr
,"Error on line %d of --hexdb input\n", nLine
);
21172 #endif /* SQLITE_OMIT_DESERIALIZE */
21175 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
21177 static void shellUSleepFunc(
21178 sqlite3_context
*context
,
21180 sqlite3_value
**argv
21182 int sleep
= sqlite3_value_int(argv
[0]);
21184 sqlite3_sleep(sleep
/1000);
21185 sqlite3_result_int(context
, sleep
);
21188 /* Flags for open_db().
21190 ** The default behavior of open_db() is to exit(1) if the database fails to
21191 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
21192 ** but still returns without calling exit.
21194 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
21195 ** ZIP archive if the file does not exist or is empty and its name matches
21196 ** the *.zip pattern.
21198 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
21199 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
21202 ** Make sure the database is open. If it is not, then open it. If
21203 ** the database fails to open, print an error message and exit.
21205 static void open_db(ShellState
*p
, int openFlags
){
21207 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
21208 if( p
->openMode
==SHELL_OPEN_UNSPEC
){
21209 if( zDbFilename
==0 || zDbFilename
[0]==0 ){
21210 p
->openMode
= SHELL_OPEN_NORMAL
;
21212 p
->openMode
= (u8
)deduceDatabaseType(zDbFilename
,
21213 (openFlags
& OPEN_DB_ZIPFILE
)!=0);
21216 switch( p
->openMode
){
21217 case SHELL_OPEN_APPENDVFS
: {
21218 sqlite3_open_v2(zDbFilename
, &p
->db
,
21219 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, "apndvfs");
21222 case SHELL_OPEN_HEXDB
:
21223 case SHELL_OPEN_DESERIALIZE
: {
21224 sqlite3_open(0, &p
->db
);
21227 case SHELL_OPEN_ZIPFILE
: {
21228 sqlite3_open(":memory:", &p
->db
);
21231 case SHELL_OPEN_READONLY
: {
21232 sqlite3_open_v2(zDbFilename
, &p
->db
,
21233 SQLITE_OPEN_READONLY
|p
->openFlags
, 0);
21236 case SHELL_OPEN_UNSPEC
:
21237 case SHELL_OPEN_NORMAL
: {
21238 sqlite3_open_v2(zDbFilename
, &p
->db
,
21239 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, 0);
21244 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
21245 utf8_printf(stderr
,"Error: unable to open database \"%s\": %s\n",
21246 zDbFilename
, sqlite3_errmsg(p
->db
));
21247 if( (openFlags
& OPEN_DB_KEEPALIVE
)==0 ){
21250 sqlite3_close(p
->db
);
21251 sqlite3_open(":memory:", &p
->db
);
21252 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
21253 utf8_printf(stderr
,
21254 "Also: unable to open substitute in-memory database.\n"
21258 utf8_printf(stderr
,
21259 "Notice: using substitute in-memory database instead of \"%s\"\n",
21263 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, (int)0, (int*)0);
21265 /* Reflect the use or absence of --unsafe-testing invocation. */
21267 int testmode_on
= ShellHasFlag(p
,SHFLG_TestingMode
);
21268 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_TRUSTED_SCHEMA
, testmode_on
,0);
21269 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, !testmode_on
,0);
21272 #ifndef SQLITE_OMIT_LOAD_EXTENSION
21273 sqlite3_enable_load_extension(p
->db
, 1);
21275 sqlite3_shathree_init(p
->db
, 0, 0);
21276 sqlite3_uint_init(p
->db
, 0, 0);
21277 sqlite3_decimal_init(p
->db
, 0, 0);
21278 sqlite3_base64_init(p
->db
, 0, 0);
21279 sqlite3_base85_init(p
->db
, 0, 0);
21280 sqlite3_regexp_init(p
->db
, 0, 0);
21281 sqlite3_ieee_init(p
->db
, 0, 0);
21282 sqlite3_series_init(p
->db
, 0, 0);
21283 #ifndef SQLITE_SHELL_FIDDLE
21284 sqlite3_fileio_init(p
->db
, 0, 0);
21285 sqlite3_completion_init(p
->db
, 0, 0);
21287 #ifdef SQLITE_HAVE_ZLIB
21288 if( !p
->bSafeModePersist
){
21289 sqlite3_zipfile_init(p
->db
, 0, 0);
21290 sqlite3_sqlar_init(p
->db
, 0, 0);
21293 #ifdef SQLITE_SHELL_EXTFUNCS
21294 /* Create a preprocessing mechanism for extensions to make
21295 * their own provisions for being built into the shell.
21296 * This is a short-span macro. See further below for usage.
21298 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
21299 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
21300 /* Let custom-included extensions get their ..._init() called.
21301 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
21302 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
21303 * initialization routine to be called.
21306 int irc
= SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, INIT
)(p
->db
);
21307 /* Let custom-included extensions expose their functionality.
21308 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
21309 * the SQL functions, virtual tables, collating sequences or
21310 * VFS's implemented by the extension to be registered.
21313 || irc
==SQLITE_OK_LOAD_PERMANENTLY
){
21314 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, EXPOSE
)(p
->db
, 0);
21316 #undef SHELL_SUB_MACRO
21317 #undef SHELL_SUBMACRO
21321 sqlite3_create_function(p
->db
, "strtod", 1, SQLITE_UTF8
, 0,
21322 shellStrtod
, 0, 0);
21323 sqlite3_create_function(p
->db
, "dtostr", 1, SQLITE_UTF8
, 0,
21324 shellDtostr
, 0, 0);
21325 sqlite3_create_function(p
->db
, "dtostr", 2, SQLITE_UTF8
, 0,
21326 shellDtostr
, 0, 0);
21327 sqlite3_create_function(p
->db
, "shell_add_schema", 3, SQLITE_UTF8
, 0,
21328 shellAddSchemaName
, 0, 0);
21329 sqlite3_create_function(p
->db
, "shell_module_schema", 1, SQLITE_UTF8
, 0,
21330 shellModuleSchema
, 0, 0);
21331 sqlite3_create_function(p
->db
, "shell_putsnl", 1, SQLITE_UTF8
, p
,
21332 shellPutsFunc
, 0, 0);
21333 sqlite3_create_function(p
->db
, "usleep",1,SQLITE_UTF8
,0,
21334 shellUSleepFunc
, 0, 0);
21335 #ifndef SQLITE_NOHAVE_SYSTEM
21336 sqlite3_create_function(p
->db
, "edit", 1, SQLITE_UTF8
, 0,
21338 sqlite3_create_function(p
->db
, "edit", 2, SQLITE_UTF8
, 0,
21342 if( p
->openMode
==SHELL_OPEN_ZIPFILE
){
21343 char *zSql
= sqlite3_mprintf(
21344 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename
);
21345 shell_check_oom(zSql
);
21346 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
21347 sqlite3_free(zSql
);
21349 #ifndef SQLITE_OMIT_DESERIALIZE
21351 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
|| p
->openMode
==SHELL_OPEN_HEXDB
){
21354 unsigned char *aData
;
21355 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
){
21356 aData
= (unsigned char*)readFile(zDbFilename
, &nData
);
21358 aData
= readHexDb(p
, &nData
);
21363 rc
= sqlite3_deserialize(p
->db
, "main", aData
, nData
, nData
,
21364 SQLITE_DESERIALIZE_RESIZEABLE
|
21365 SQLITE_DESERIALIZE_FREEONCLOSE
);
21367 utf8_printf(stderr
, "Error: sqlite3_deserialize() returns %d\n", rc
);
21370 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_SIZE_LIMIT
, &p
->szMax
);
21376 if( p
->bSafeModePersist
){
21377 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
21380 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
21386 ** Attempt to close the database connection. Report errors.
21388 void close_db(sqlite3
*db
){
21389 int rc
= sqlite3_close(db
);
21391 utf8_printf(stderr
, "Error: sqlite3_close() returns %d: %s\n",
21392 rc
, sqlite3_errmsg(db
));
21396 #if HAVE_READLINE || HAVE_EDITLINE
21398 ** Readline completion callbacks
21400 static char *readline_completion_generator(const char *text
, int state
){
21401 static sqlite3_stmt
*pStmt
= 0;
21405 sqlite3_finalize(pStmt
);
21406 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
21407 " FROM completion(%Q) ORDER BY 1", text
);
21408 shell_check_oom(zSql
);
21409 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
21410 sqlite3_free(zSql
);
21412 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
21413 const char *z
= (const char*)sqlite3_column_text(pStmt
,0);
21414 zRet
= z
? strdup(z
) : 0;
21416 sqlite3_finalize(pStmt
);
21422 static char **readline_completion(const char *zText
, int iStart
, int iEnd
){
21425 rl_attempted_completion_over
= 1;
21426 return rl_completion_matches(zText
, readline_completion_generator
);
21429 #elif HAVE_LINENOISE
21431 ** Linenoise completion callback
21433 static void linenoise_completion(const char *zLine
, linenoiseCompletions
*lc
){
21434 i64 nLine
= strlen(zLine
);
21436 sqlite3_stmt
*pStmt
= 0;
21440 if( nLine
>(i64
)sizeof(zBuf
)-30 ) return;
21441 if( zLine
[0]=='.' || zLine
[0]=='#') return;
21442 for(i
=nLine
-1; i
>=0 && (isalnum(zLine
[i
]) || zLine
[i
]=='_'); i
--){}
21443 if( i
==nLine
-1 ) return;
21445 memcpy(zBuf
, zLine
, iStart
);
21446 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
21447 " FROM completion(%Q,%Q) ORDER BY 1",
21448 &zLine
[iStart
], zLine
);
21449 shell_check_oom(zSql
);
21450 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
21451 sqlite3_free(zSql
);
21452 sqlite3_exec(globalDb
, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
21453 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
21454 const char *zCompletion
= (const char*)sqlite3_column_text(pStmt
, 0);
21455 int nCompletion
= sqlite3_column_bytes(pStmt
, 0);
21456 if( iStart
+nCompletion
< (i64
)sizeof(zBuf
)-1 && zCompletion
){
21457 memcpy(zBuf
+iStart
, zCompletion
, nCompletion
+1);
21458 linenoiseAddCompletion(lc
, zBuf
);
21461 sqlite3_finalize(pStmt
);
21466 ** Do C-language style dequoting.
21472 ** \v -> vertical tab
21474 ** \r -> carriage return
21479 ** \NNN -> ascii character NNN in octal
21480 ** \xHH -> ascii character HH in hexadecimal
21482 static void resolve_backslashes(char *z
){
21485 while( *z
&& *z
!='\\' ) z
++;
21486 for(i
=j
=0; (c
= z
[i
])!=0; i
++, j
++){
21487 if( c
=='\\' && z
[i
+1]!=0 ){
21491 }else if( c
=='b' ){
21493 }else if( c
=='t' ){
21495 }else if( c
=='n' ){
21497 }else if( c
=='v' ){
21499 }else if( c
=='f' ){
21501 }else if( c
=='r' ){
21503 }else if( c
=='"' ){
21505 }else if( c
=='\'' ){
21507 }else if( c
=='\\' ){
21509 }else if( c
=='x' ){
21512 while( nhd
<2 && (c
=z
[i
+1+nhd
])!=0 && (hdv
=hexDigitValue(c
))>=0 ){
21513 hv
= (u8
)((hv
<<4)|hdv
);
21518 }else if( c
>='0' && c
<='7' ){
21520 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
21522 c
= (c
<<3) + z
[i
] - '0';
21523 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
21525 c
= (c
<<3) + z
[i
] - '0';
21532 if( j
<i
) z
[j
] = 0;
21536 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
21537 ** for TRUE and FALSE. Return the integer value if appropriate.
21539 static int booleanValue(const char *zArg
){
21541 if( zArg
[0]=='0' && zArg
[1]=='x' ){
21542 for(i
=2; hexDigitValue(zArg
[i
])>=0; i
++){}
21544 for(i
=0; zArg
[i
]>='0' && zArg
[i
]<='9'; i
++){}
21546 if( i
>0 && zArg
[i
]==0 ) return (int)(integerValue(zArg
) & 0xffffffff);
21547 if( sqlite3_stricmp(zArg
, "on")==0 || sqlite3_stricmp(zArg
,"yes")==0 ){
21550 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
21553 utf8_printf(stderr
, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
21559 ** Set or clear a shell flag according to a boolean value.
21561 static void setOrClearFlag(ShellState
*p
, unsigned mFlag
, const char *zArg
){
21562 if( booleanValue(zArg
) ){
21563 ShellSetFlag(p
, mFlag
);
21565 ShellClearFlag(p
, mFlag
);
21570 ** Close an output file, assuming it is not stderr or stdout
21572 static void output_file_close(FILE *f
){
21573 if( f
&& f
!=stdout
&& f
!=stderr
) fclose(f
);
21577 ** Try to open an output file. The names "stdout" and "stderr" are
21578 ** recognized and do the right thing. NULL is returned if the output
21579 ** filename is "off".
21581 static FILE *output_file_open(const char *zFile
, int bTextMode
){
21583 if( cli_strcmp(zFile
,"stdout")==0 ){
21585 }else if( cli_strcmp(zFile
, "stderr")==0 ){
21587 }else if( cli_strcmp(zFile
, "off")==0 ){
21590 f
= fopen(zFile
, bTextMode
? "w" : "wb");
21592 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
21598 #ifndef SQLITE_OMIT_TRACE
21600 ** A routine for handling output from sqlite3_trace().
21602 static int sql_trace_callback(
21603 unsigned mType
, /* The trace type */
21604 void *pArg
, /* The ShellState pointer */
21605 void *pP
, /* Usually a pointer to sqlite_stmt */
21606 void *pX
/* Auxiliary output */
21608 ShellState
*p
= (ShellState
*)pArg
;
21609 sqlite3_stmt
*pStmt
;
21612 if( p
->traceOut
==0 ) return 0;
21613 if( mType
==SQLITE_TRACE_CLOSE
){
21614 utf8_printf(p
->traceOut
, "-- closing database connection\n");
21617 if( mType
!=SQLITE_TRACE_ROW
&& pX
!=0 && ((const char*)pX
)[0]=='-' ){
21618 zSql
= (const char*)pX
;
21620 pStmt
= (sqlite3_stmt
*)pP
;
21621 switch( p
->eTraceType
){
21622 case SHELL_TRACE_EXPANDED
: {
21623 zSql
= sqlite3_expanded_sql(pStmt
);
21626 #ifdef SQLITE_ENABLE_NORMALIZE
21627 case SHELL_TRACE_NORMALIZED
: {
21628 zSql
= sqlite3_normalized_sql(pStmt
);
21633 zSql
= sqlite3_sql(pStmt
);
21638 if( zSql
==0 ) return 0;
21639 nSql
= strlen(zSql
);
21640 if( nSql
>1000000000 ) nSql
= 1000000000;
21641 while( nSql
>0 && zSql
[nSql
-1]==';' ){ nSql
--; }
21643 case SQLITE_TRACE_ROW
:
21644 case SQLITE_TRACE_STMT
: {
21645 utf8_printf(p
->traceOut
, "%.*s;\n", (int)nSql
, zSql
);
21648 case SQLITE_TRACE_PROFILE
: {
21649 sqlite3_int64 nNanosec
= pX
? *(sqlite3_int64
*)pX
: 0;
21650 utf8_printf(p
->traceOut
, "%.*s; -- %lld ns\n", (int)nSql
, zSql
, nNanosec
);
21659 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
21660 ** a useful spot to set a debugger breakpoint.
21662 ** This routine does not do anything practical. The code are there simply
21663 ** to prevent the compiler from optimizing this routine out.
21665 static void test_breakpoint(void){
21666 static unsigned int nCall
= 0;
21667 if( (nCall
++)==0xffffffff ) printf("Many .breakpoints have run\n");
21671 ** An object used to read a CSV and other files for import.
21673 typedef struct ImportCtx ImportCtx
;
21675 const char *zFile
; /* Name of the input file */
21676 FILE *in
; /* Read the CSV text from this input stream */
21677 int (SQLITE_CDECL
*xCloser
)(FILE*); /* Func to close in */
21678 char *z
; /* Accumulated text for a field */
21679 int n
; /* Number of bytes in z */
21680 int nAlloc
; /* Space allocated for z[] */
21681 int nLine
; /* Current line number */
21682 int nRow
; /* Number of rows imported */
21683 int nErr
; /* Number of errors encountered */
21684 int bNotFirst
; /* True if one or more bytes already read */
21685 int cTerm
; /* Character that terminated the most recent field */
21686 int cColSep
; /* The column separator character. (Usually ",") */
21687 int cRowSep
; /* The row separator character. (Usually "\n") */
21690 /* Clean up resourced used by an ImportCtx */
21691 static void import_cleanup(ImportCtx
*p
){
21692 if( p
->in
!=0 && p
->xCloser
!=0 ){
21696 sqlite3_free(p
->z
);
21700 /* Append a single byte to z[] */
21701 static void import_append_char(ImportCtx
*p
, int c
){
21702 if( p
->n
+1>=p
->nAlloc
){
21703 p
->nAlloc
+= p
->nAlloc
+ 100;
21704 p
->z
= sqlite3_realloc64(p
->z
, p
->nAlloc
);
21705 shell_check_oom(p
->z
);
21707 p
->z
[p
->n
++] = (char)c
;
21710 /* Read a single field of CSV text. Compatible with rfc4180 and extended
21711 ** with the option of having a separator other than ",".
21713 ** + Input comes from p->in.
21714 ** + Store results in p->z of length p->n. Space to hold p->z comes
21715 ** from sqlite3_malloc64().
21716 ** + Use p->cSep as the column separator. The default is ",".
21717 ** + Use p->rSep as the row separator. The default is "\n".
21718 ** + Keep track of the line number in p->nLine.
21719 ** + Store the character that terminates the field in p->cTerm. Store
21720 ** EOF on end-of-file.
21721 ** + Report syntax errors on stderr
21723 static char *SQLITE_CDECL
csv_read_one_field(ImportCtx
*p
){
21725 int cSep
= (u8
)p
->cColSep
;
21726 int rSep
= (u8
)p
->cRowSep
;
21729 if( c
==EOF
|| seenInterrupt
){
21735 int startLine
= p
->nLine
;
21740 if( c
==rSep
) p
->nLine
++;
21747 if( (c
==cSep
&& pc
==cQuote
)
21748 || (c
==rSep
&& pc
==cQuote
)
21749 || (c
==rSep
&& pc
=='\r' && ppc
==cQuote
)
21750 || (c
==EOF
&& pc
==cQuote
)
21752 do{ p
->n
--; }while( p
->z
[p
->n
]!=cQuote
);
21756 if( pc
==cQuote
&& c
!='\r' ){
21757 utf8_printf(stderr
, "%s:%d: unescaped %c character\n",
21758 p
->zFile
, p
->nLine
, cQuote
);
21761 utf8_printf(stderr
, "%s:%d: unterminated %c-quoted field\n",
21762 p
->zFile
, startLine
, cQuote
);
21766 import_append_char(p
, c
);
21771 /* If this is the first field being parsed and it begins with the
21772 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
21773 if( (c
&0xff)==0xef && p
->bNotFirst
==0 ){
21774 import_append_char(p
, c
);
21776 if( (c
&0xff)==0xbb ){
21777 import_append_char(p
, c
);
21779 if( (c
&0xff)==0xbf ){
21782 return csv_read_one_field(p
);
21786 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
21787 import_append_char(p
, c
);
21792 if( p
->n
>0 && p
->z
[p
->n
-1]=='\r' ) p
->n
--;
21796 if( p
->z
) p
->z
[p
->n
] = 0;
21801 /* Read a single field of ASCII delimited text.
21803 ** + Input comes from p->in.
21804 ** + Store results in p->z of length p->n. Space to hold p->z comes
21805 ** from sqlite3_malloc64().
21806 ** + Use p->cSep as the column separator. The default is "\x1F".
21807 ** + Use p->rSep as the row separator. The default is "\x1E".
21808 ** + Keep track of the row number in p->nLine.
21809 ** + Store the character that terminates the field in p->cTerm. Store
21810 ** EOF on end-of-file.
21811 ** + Report syntax errors on stderr
21813 static char *SQLITE_CDECL
ascii_read_one_field(ImportCtx
*p
){
21815 int cSep
= (u8
)p
->cColSep
;
21816 int rSep
= (u8
)p
->cRowSep
;
21819 if( c
==EOF
|| seenInterrupt
){
21823 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
21824 import_append_char(p
, c
);
21831 if( p
->z
) p
->z
[p
->n
] = 0;
21836 ** Try to transfer data for table zTable. If an error is seen while
21837 ** moving forward, try to go backwards. The backwards movement won't
21838 ** work for WITHOUT ROWID tables.
21840 static void tryToCloneData(
21845 sqlite3_stmt
*pQuery
= 0;
21846 sqlite3_stmt
*pInsert
= 0;
21851 int nTable
= strlen30(zTable
);
21854 const int spinRate
= 10000;
21856 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\"", zTable
);
21857 shell_check_oom(zQuery
);
21858 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
21860 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
21861 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
21863 goto end_data_xfer
;
21865 n
= sqlite3_column_count(pQuery
);
21866 zInsert
= sqlite3_malloc64(200 + nTable
+ n
*3);
21867 shell_check_oom(zInsert
);
21868 sqlite3_snprintf(200+nTable
,zInsert
,
21869 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable
);
21870 i
= strlen30(zInsert
);
21871 for(j
=1; j
<n
; j
++){
21872 memcpy(zInsert
+i
, ",?", 2);
21875 memcpy(zInsert
+i
, ");", 3);
21876 rc
= sqlite3_prepare_v2(newDb
, zInsert
, -1, &pInsert
, 0);
21878 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
21879 sqlite3_extended_errcode(newDb
), sqlite3_errmsg(newDb
),
21881 goto end_data_xfer
;
21883 for(k
=0; k
<2; k
++){
21884 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
21885 for(i
=0; i
<n
; i
++){
21886 switch( sqlite3_column_type(pQuery
, i
) ){
21887 case SQLITE_NULL
: {
21888 sqlite3_bind_null(pInsert
, i
+1);
21891 case SQLITE_INTEGER
: {
21892 sqlite3_bind_int64(pInsert
, i
+1, sqlite3_column_int64(pQuery
,i
));
21895 case SQLITE_FLOAT
: {
21896 sqlite3_bind_double(pInsert
, i
+1, sqlite3_column_double(pQuery
,i
));
21899 case SQLITE_TEXT
: {
21900 sqlite3_bind_text(pInsert
, i
+1,
21901 (const char*)sqlite3_column_text(pQuery
,i
),
21902 -1, SQLITE_STATIC
);
21905 case SQLITE_BLOB
: {
21906 sqlite3_bind_blob(pInsert
, i
+1, sqlite3_column_blob(pQuery
,i
),
21907 sqlite3_column_bytes(pQuery
,i
),
21913 rc
= sqlite3_step(pInsert
);
21914 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_ROW
&& rc
!=SQLITE_DONE
){
21915 utf8_printf(stderr
, "Error %d: %s\n", sqlite3_extended_errcode(newDb
),
21916 sqlite3_errmsg(newDb
));
21918 sqlite3_reset(pInsert
);
21920 if( (cnt
%spinRate
)==0 ){
21921 printf("%c\b", "|/-\\"[(cnt
/spinRate
)%4]);
21925 if( rc
==SQLITE_DONE
) break;
21926 sqlite3_finalize(pQuery
);
21927 sqlite3_free(zQuery
);
21928 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
21930 shell_check_oom(zQuery
);
21931 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
21933 utf8_printf(stderr
, "Warning: cannot step \"%s\" backwards", zTable
);
21936 } /* End for(k=0...) */
21939 sqlite3_finalize(pQuery
);
21940 sqlite3_finalize(pInsert
);
21941 sqlite3_free(zQuery
);
21942 sqlite3_free(zInsert
);
21947 ** Try to transfer all rows of the schema that match zWhere. For
21948 ** each row, invoke xForEach() on the object defined by that row.
21949 ** If an error is encountered while moving forward through the
21950 ** sqlite_schema table, try again moving backwards.
21952 static void tryToCloneSchema(
21955 const char *zWhere
,
21956 void (*xForEach
)(ShellState
*,sqlite3
*,const char*)
21958 sqlite3_stmt
*pQuery
= 0;
21961 const unsigned char *zName
;
21962 const unsigned char *zSql
;
21965 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
21966 " WHERE %s ORDER BY rowid ASC", zWhere
);
21967 shell_check_oom(zQuery
);
21968 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
21970 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
21971 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
21973 goto end_schema_xfer
;
21975 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
21976 zName
= sqlite3_column_text(pQuery
, 0);
21977 zSql
= sqlite3_column_text(pQuery
, 1);
21978 if( zName
==0 || zSql
==0 ) continue;
21979 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")!=0 ){
21980 printf("%s... ", zName
); fflush(stdout
);
21981 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
21983 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
21984 sqlite3_free(zErrMsg
);
21989 xForEach(p
, newDb
, (const char*)zName
);
21993 if( rc
!=SQLITE_DONE
){
21994 sqlite3_finalize(pQuery
);
21995 sqlite3_free(zQuery
);
21996 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
21997 " WHERE %s ORDER BY rowid DESC", zWhere
);
21998 shell_check_oom(zQuery
);
21999 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
22001 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
22002 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
22004 goto end_schema_xfer
;
22006 while( sqlite3_step(pQuery
)==SQLITE_ROW
){
22007 zName
= sqlite3_column_text(pQuery
, 0);
22008 zSql
= sqlite3_column_text(pQuery
, 1);
22009 if( zName
==0 || zSql
==0 ) continue;
22010 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")==0 ) continue;
22011 printf("%s... ", zName
); fflush(stdout
);
22012 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
22014 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
22015 sqlite3_free(zErrMsg
);
22019 xForEach(p
, newDb
, (const char*)zName
);
22025 sqlite3_finalize(pQuery
);
22026 sqlite3_free(zQuery
);
22030 ** Open a new database file named "zNewDb". Try to recover as much information
22031 ** as possible out of the main database (which might be corrupt) and write it
22034 static void tryToClone(ShellState
*p
, const char *zNewDb
){
22036 sqlite3
*newDb
= 0;
22037 if( access(zNewDb
,0)==0 ){
22038 utf8_printf(stderr
, "File \"%s\" already exists.\n", zNewDb
);
22041 rc
= sqlite3_open(zNewDb
, &newDb
);
22043 utf8_printf(stderr
, "Cannot create output database: %s\n",
22044 sqlite3_errmsg(newDb
));
22046 sqlite3_exec(p
->db
, "PRAGMA writable_schema=ON;", 0, 0, 0);
22047 sqlite3_exec(newDb
, "BEGIN EXCLUSIVE;", 0, 0, 0);
22048 tryToCloneSchema(p
, newDb
, "type='table'", tryToCloneData
);
22049 tryToCloneSchema(p
, newDb
, "type!='table'", 0);
22050 sqlite3_exec(newDb
, "COMMIT;", 0, 0, 0);
22051 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
22057 ** Change the output file back to stdout.
22059 ** If the p->doXdgOpen flag is set, that means the output was being
22060 ** redirected to a temporary file named by p->zTempFile. In that case,
22061 ** launch start/open/xdg-open on that temporary file.
22063 static void output_reset(ShellState
*p
){
22064 if( p
->outfile
[0]=='|' ){
22065 #ifndef SQLITE_OMIT_POPEN
22069 output_file_close(p
->out
);
22070 #ifndef SQLITE_NOHAVE_SYSTEM
22071 if( p
->doXdgOpen
){
22072 const char *zXdgOpenCmd
=
22073 #if defined(_WIN32)
22075 #elif defined(__APPLE__)
22081 zCmd
= sqlite3_mprintf("%s %s", zXdgOpenCmd
, p
->zTempFile
);
22082 if( system(zCmd
) ){
22083 utf8_printf(stderr
, "Failed: [%s]\n", zCmd
);
22085 /* Give the start/open/xdg-open command some time to get
22086 ** going before we continue, and potential delete the
22087 ** p->zTempFile data file out from under it */
22088 sqlite3_sleep(2000);
22090 sqlite3_free(zCmd
);
22094 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
22101 ** Run an SQL command and return the single integer result.
22103 static int db_int(sqlite3
*db
, const char *zSql
){
22104 sqlite3_stmt
*pStmt
;
22106 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
22107 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
22108 res
= sqlite3_column_int(pStmt
,0);
22110 sqlite3_finalize(pStmt
);
22114 #if SQLITE_SHELL_HAVE_RECOVER
22116 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
22118 static unsigned int get2byteInt(unsigned char *a
){
22119 return (a
[0]<<8) + a
[1];
22121 static unsigned int get4byteInt(unsigned char *a
){
22122 return (a
[0]<<24) + (a
[1]<<16) + (a
[2]<<8) + a
[3];
22126 ** Implementation of the ".dbinfo" command.
22128 ** Return 1 on error, 2 to exit, and 0 otherwise.
22130 static int shell_dbinfo_command(ShellState
*p
, int nArg
, char **azArg
){
22131 static const struct { const char *zName
; int ofst
; } aField
[] = {
22132 { "file change counter:", 24 },
22133 { "database page count:", 28 },
22134 { "freelist page count:", 36 },
22135 { "schema cookie:", 40 },
22136 { "schema format:", 44 },
22137 { "default cache size:", 48 },
22138 { "autovacuum top root:", 52 },
22139 { "incremental vacuum:", 64 },
22140 { "text encoding:", 56 },
22141 { "user version:", 60 },
22142 { "application id:", 68 },
22143 { "software version:", 96 },
22145 static const struct { const char *zName
; const char *zSql
; } aQuery
[] = {
22146 { "number of tables:",
22147 "SELECT count(*) FROM %s WHERE type='table'" },
22148 { "number of indexes:",
22149 "SELECT count(*) FROM %s WHERE type='index'" },
22150 { "number of triggers:",
22151 "SELECT count(*) FROM %s WHERE type='trigger'" },
22152 { "number of views:",
22153 "SELECT count(*) FROM %s WHERE type='view'" },
22155 "SELECT total(length(sql)) FROM %s" },
22158 unsigned iDataVersion
;
22160 char *zDb
= nArg
>=2 ? azArg
[1] : "main";
22161 sqlite3_stmt
*pStmt
= 0;
22162 unsigned char aHdr
[100];
22164 if( p
->db
==0 ) return 1;
22165 rc
= sqlite3_prepare_v2(p
->db
,
22166 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
22169 utf8_printf(stderr
, "error: %s\n", sqlite3_errmsg(p
->db
));
22170 sqlite3_finalize(pStmt
);
22173 sqlite3_bind_text(pStmt
, 1, zDb
, -1, SQLITE_STATIC
);
22174 if( sqlite3_step(pStmt
)==SQLITE_ROW
22175 && sqlite3_column_bytes(pStmt
,0)>100
22177 const u8
*pb
= sqlite3_column_blob(pStmt
,0);
22178 shell_check_oom(pb
);
22179 memcpy(aHdr
, pb
, 100);
22180 sqlite3_finalize(pStmt
);
22182 raw_printf(stderr
, "unable to read database header\n");
22183 sqlite3_finalize(pStmt
);
22186 i
= get2byteInt(aHdr
+16);
22187 if( i
==1 ) i
= 65536;
22188 utf8_printf(p
->out
, "%-20s %d\n", "database page size:", i
);
22189 utf8_printf(p
->out
, "%-20s %d\n", "write format:", aHdr
[18]);
22190 utf8_printf(p
->out
, "%-20s %d\n", "read format:", aHdr
[19]);
22191 utf8_printf(p
->out
, "%-20s %d\n", "reserved bytes:", aHdr
[20]);
22192 for(i
=0; i
<ArraySize(aField
); i
++){
22193 int ofst
= aField
[i
].ofst
;
22194 unsigned int val
= get4byteInt(aHdr
+ ofst
);
22195 utf8_printf(p
->out
, "%-20s %u", aField
[i
].zName
, val
);
22198 if( val
==1 ) raw_printf(p
->out
, " (utf8)");
22199 if( val
==2 ) raw_printf(p
->out
, " (utf16le)");
22200 if( val
==3 ) raw_printf(p
->out
, " (utf16be)");
22203 raw_printf(p
->out
, "\n");
22206 zSchemaTab
= sqlite3_mprintf("main.sqlite_schema");
22207 }else if( cli_strcmp(zDb
,"temp")==0 ){
22208 zSchemaTab
= sqlite3_mprintf("%s", "sqlite_temp_schema");
22210 zSchemaTab
= sqlite3_mprintf("\"%w\".sqlite_schema", zDb
);
22212 for(i
=0; i
<ArraySize(aQuery
); i
++){
22213 char *zSql
= sqlite3_mprintf(aQuery
[i
].zSql
, zSchemaTab
);
22214 int val
= db_int(p
->db
, zSql
);
22215 sqlite3_free(zSql
);
22216 utf8_printf(p
->out
, "%-20s %d\n", aQuery
[i
].zName
, val
);
22218 sqlite3_free(zSchemaTab
);
22219 sqlite3_file_control(p
->db
, zDb
, SQLITE_FCNTL_DATA_VERSION
, &iDataVersion
);
22220 utf8_printf(p
->out
, "%-20s %u\n", "data version", iDataVersion
);
22223 #endif /* SQLITE_SHELL_HAVE_RECOVER */
22226 ** Print the current sqlite3_errmsg() value to stderr and return 1.
22228 static int shellDatabaseError(sqlite3
*db
){
22229 const char *zErr
= sqlite3_errmsg(db
);
22230 utf8_printf(stderr
, "Error: %s\n", zErr
);
22235 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
22236 ** if they match and FALSE (0) if they do not match.
22240 ** '*' Matches any sequence of zero or more characters.
22242 ** '?' Matches exactly one character.
22244 ** [...] Matches one character from the enclosed list of
22247 ** [^...] Matches one character not in the enclosed list.
22249 ** '#' Matches any sequence of one or more digits with an
22250 ** optional + or - sign in front
22252 ** ' ' Any span of whitespace matches any other span of
22255 ** Extra whitespace at the end of z[] is ignored.
22257 static int testcase_glob(const char *zGlob
, const char *z
){
22262 while( (c
= (*(zGlob
++)))!=0 ){
22264 if( !IsSpace(*z
) ) return 0;
22265 while( IsSpace(*zGlob
) ) zGlob
++;
22266 while( IsSpace(*z
) ) z
++;
22267 }else if( c
=='*' ){
22268 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
22269 if( c
=='?' && (*(z
++))==0 ) return 0;
22273 }else if( c
=='[' ){
22274 while( *z
&& testcase_glob(zGlob
-1,z
)==0 ){
22279 while( (c2
= (*(z
++)))!=0 ){
22282 if( c2
==0 ) return 0;
22284 if( testcase_glob(zGlob
,z
) ) return 1;
22287 }else if( c
=='?' ){
22288 if( (*(z
++))==0 ) return 0;
22289 }else if( c
=='[' ){
22294 if( c
==0 ) return 0;
22301 if( c
==']' ) seen
= 1;
22304 while( c2
&& c2
!=']' ){
22305 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
22307 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
22317 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
22318 }else if( c
=='#' ){
22319 if( (z
[0]=='-' || z
[0]=='+') && IsDigit(z
[1]) ) z
++;
22320 if( !IsDigit(z
[0]) ) return 0;
22322 while( IsDigit(z
[0]) ){ z
++; }
22324 if( c
!=(*(z
++)) ) return 0;
22327 while( IsSpace(*z
) ){ z
++; }
22333 ** Compare the string as a command-line option with either one or two
22334 ** initial "-" characters.
22336 static int optionMatch(const char *zStr
, const char *zOpt
){
22337 if( zStr
[0]!='-' ) return 0;
22339 if( zStr
[0]=='-' ) zStr
++;
22340 return cli_strcmp(zStr
, zOpt
)==0;
22346 int shellDeleteFile(const char *zFilename
){
22349 wchar_t *z
= sqlite3_win32_utf8_to_unicode(zFilename
);
22353 rc
= unlink(zFilename
);
22359 ** Try to delete the temporary file (if there is one) and free the
22360 ** memory used to hold the name of the temp file.
22362 static void clearTempFile(ShellState
*p
){
22363 if( p
->zTempFile
==0 ) return;
22364 if( p
->doXdgOpen
) return;
22365 if( shellDeleteFile(p
->zTempFile
) ) return;
22366 sqlite3_free(p
->zTempFile
);
22371 ** Create a new temp file name with the given suffix.
22373 static void newTempFile(ShellState
*p
, const char *zSuffix
){
22375 sqlite3_free(p
->zTempFile
);
22378 sqlite3_file_control(p
->db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &p
->zTempFile
);
22380 if( p
->zTempFile
==0 ){
22381 /* If p->db is an in-memory database then the TEMPFILENAME file-control
22382 ** will not work and we will need to fallback to guessing */
22385 sqlite3_randomness(sizeof(r
), &r
);
22386 zTemp
= getenv("TEMP");
22387 if( zTemp
==0 ) zTemp
= getenv("TMP");
22395 p
->zTempFile
= sqlite3_mprintf("%s/temp%llx.%s", zTemp
, r
, zSuffix
);
22397 p
->zTempFile
= sqlite3_mprintf("%z.%s", p
->zTempFile
, zSuffix
);
22399 shell_check_oom(p
->zTempFile
);
22404 ** The implementation of SQL scalar function fkey_collate_clause(), used
22405 ** by the ".lint fkey-indexes" command. This scalar function is always
22406 ** called with four arguments - the parent table name, the parent column name,
22407 ** the child table name and the child column name.
22409 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
22411 ** If either of the named tables or columns do not exist, this function
22412 ** returns an empty string. An empty string is also returned if both tables
22413 ** and columns exist but have the same default collation sequence. Or,
22414 ** if both exist but the default collation sequences are different, this
22415 ** function returns the string " COLLATE <parent-collation>", where
22416 ** <parent-collation> is the default collation sequence of the parent column.
22418 static void shellFkeyCollateClause(
22419 sqlite3_context
*pCtx
,
22421 sqlite3_value
**apVal
22423 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
22424 const char *zParent
;
22425 const char *zParentCol
;
22426 const char *zParentSeq
;
22427 const char *zChild
;
22428 const char *zChildCol
;
22429 const char *zChildSeq
= 0; /* Initialize to avoid false-positive warning */
22433 zParent
= (const char*)sqlite3_value_text(apVal
[0]);
22434 zParentCol
= (const char*)sqlite3_value_text(apVal
[1]);
22435 zChild
= (const char*)sqlite3_value_text(apVal
[2]);
22436 zChildCol
= (const char*)sqlite3_value_text(apVal
[3]);
22438 sqlite3_result_text(pCtx
, "", -1, SQLITE_STATIC
);
22439 rc
= sqlite3_table_column_metadata(
22440 db
, "main", zParent
, zParentCol
, 0, &zParentSeq
, 0, 0, 0
22442 if( rc
==SQLITE_OK
){
22443 rc
= sqlite3_table_column_metadata(
22444 db
, "main", zChild
, zChildCol
, 0, &zChildSeq
, 0, 0, 0
22448 if( rc
==SQLITE_OK
&& sqlite3_stricmp(zParentSeq
, zChildSeq
) ){
22449 char *z
= sqlite3_mprintf(" COLLATE %s", zParentSeq
);
22450 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
22457 ** The implementation of dot-command ".lint fkey-indexes".
22459 static int lintFkeyIndexes(
22460 ShellState
*pState
, /* Current shell tool state */
22461 char **azArg
, /* Array of arguments passed to dot command */
22462 int nArg
/* Number of entries in azArg[] */
22464 sqlite3
*db
= pState
->db
; /* Database handle to query "main" db of */
22465 FILE *out
= pState
->out
; /* Stream to write non-error output to */
22466 int bVerbose
= 0; /* If -verbose is present */
22467 int bGroupByParent
= 0; /* If -groupbyparent is present */
22468 int i
; /* To iterate through azArg[] */
22469 const char *zIndent
= ""; /* How much to indent CREATE INDEX by */
22470 int rc
; /* Return code */
22471 sqlite3_stmt
*pSql
= 0; /* Compiled version of SQL statement below */
22474 ** This SELECT statement returns one row for each foreign key constraint
22475 ** in the schema of the main database. The column values are:
22477 ** 0. The text of an SQL statement similar to:
22479 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
22481 ** This SELECT is similar to the one that the foreign keys implementation
22482 ** needs to run internally on child tables. If there is an index that can
22483 ** be used to optimize this query, then it can also be used by the FK
22484 ** implementation to optimize DELETE or UPDATE statements on the parent
22487 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
22488 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
22489 ** contains an index that can be used to optimize the query.
22491 ** 2. Human readable text that describes the child table and columns. e.g.
22493 ** "child_table(child_key1, child_key2)"
22495 ** 3. Human readable text that describes the parent table and columns. e.g.
22497 ** "parent_table(parent_key1, parent_key2)"
22499 ** 4. A full CREATE INDEX statement for an index that could be used to
22500 ** optimize DELETE or UPDATE statements on the parent table. e.g.
22502 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
22504 ** 5. The name of the parent table.
22506 ** These six values are used by the C logic below to generate the report.
22510 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
22511 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
22512 " || fkey_collate_clause("
22513 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
22515 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
22516 " || group_concat('*=?', ' AND ') || ')'"
22518 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
22520 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
22522 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
22523 " || ' ON ' || quote(s.name) || '('"
22524 " || group_concat(quote(f.[from]) ||"
22525 " fkey_collate_clause("
22526 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
22530 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
22531 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
22532 "GROUP BY s.name, f.id "
22533 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
22535 const char *zGlobIPK
= "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
22537 for(i
=2; i
<nArg
; i
++){
22538 int n
= strlen30(azArg
[i
]);
22539 if( n
>1 && sqlite3_strnicmp("-verbose", azArg
[i
], n
)==0 ){
22542 else if( n
>1 && sqlite3_strnicmp("-groupbyparent", azArg
[i
], n
)==0 ){
22543 bGroupByParent
= 1;
22547 raw_printf(stderr
, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
22550 return SQLITE_ERROR
;
22554 /* Register the fkey_collate_clause() SQL function */
22555 rc
= sqlite3_create_function(db
, "fkey_collate_clause", 4, SQLITE_UTF8
,
22556 0, shellFkeyCollateClause
, 0, 0
22560 if( rc
==SQLITE_OK
){
22561 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pSql
, 0);
22563 if( rc
==SQLITE_OK
){
22564 sqlite3_bind_int(pSql
, 1, bGroupByParent
);
22567 if( rc
==SQLITE_OK
){
22570 while( SQLITE_ROW
==sqlite3_step(pSql
) ){
22572 sqlite3_stmt
*pExplain
= 0;
22573 const char *zEQP
= (const char*)sqlite3_column_text(pSql
, 0);
22574 const char *zGlob
= (const char*)sqlite3_column_text(pSql
, 1);
22575 const char *zFrom
= (const char*)sqlite3_column_text(pSql
, 2);
22576 const char *zTarget
= (const char*)sqlite3_column_text(pSql
, 3);
22577 const char *zCI
= (const char*)sqlite3_column_text(pSql
, 4);
22578 const char *zParent
= (const char*)sqlite3_column_text(pSql
, 5);
22580 if( zEQP
==0 ) continue;
22581 if( zGlob
==0 ) continue;
22582 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
22583 if( rc
!=SQLITE_OK
) break;
22584 if( SQLITE_ROW
==sqlite3_step(pExplain
) ){
22585 const char *zPlan
= (const char*)sqlite3_column_text(pExplain
, 3);
22586 res
= zPlan
!=0 && ( 0==sqlite3_strglob(zGlob
, zPlan
)
22587 || 0==sqlite3_strglob(zGlobIPK
, zPlan
));
22589 rc
= sqlite3_finalize(pExplain
);
22590 if( rc
!=SQLITE_OK
) break;
22593 raw_printf(stderr
, "Error: internal error");
22597 && (bVerbose
|| res
==0)
22598 && (zPrev
==0 || sqlite3_stricmp(zParent
, zPrev
))
22600 raw_printf(out
, "-- Parent table %s\n", zParent
);
22601 sqlite3_free(zPrev
);
22602 zPrev
= sqlite3_mprintf("%s", zParent
);
22606 raw_printf(out
, "%s%s --> %s\n", zIndent
, zCI
, zTarget
);
22607 }else if( bVerbose
){
22608 raw_printf(out
, "%s/* no extra indexes required for %s -> %s */\n",
22609 zIndent
, zFrom
, zTarget
22614 sqlite3_free(zPrev
);
22616 if( rc
!=SQLITE_OK
){
22617 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
22620 rc2
= sqlite3_finalize(pSql
);
22621 if( rc
==SQLITE_OK
&& rc2
!=SQLITE_OK
){
22623 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
22626 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
22633 ** Implementation of ".lint" dot command.
22635 static int lintDotCommand(
22636 ShellState
*pState
, /* Current shell tool state */
22637 char **azArg
, /* Array of arguments passed to dot command */
22638 int nArg
/* Number of entries in azArg[] */
22641 n
= (nArg
>=2 ? strlen30(azArg
[1]) : 0);
22642 if( n
<1 || sqlite3_strnicmp(azArg
[1], "fkey-indexes", n
) ) goto usage
;
22643 return lintFkeyIndexes(pState
, azArg
, nArg
);
22646 raw_printf(stderr
, "Usage %s sub-command ?switches...?\n", azArg
[0]);
22647 raw_printf(stderr
, "Where sub-commands are:\n");
22648 raw_printf(stderr
, " fkey-indexes\n");
22649 return SQLITE_ERROR
;
22652 #if !defined SQLITE_OMIT_VIRTUALTABLE
22653 static void shellPrepare(
22657 sqlite3_stmt
**ppStmt
22660 if( *pRc
==SQLITE_OK
){
22661 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
22662 if( rc
!=SQLITE_OK
){
22663 raw_printf(stderr
, "sql error: %s (%d)\n",
22664 sqlite3_errmsg(db
), sqlite3_errcode(db
)
22672 ** Create a prepared statement using printf-style arguments for the SQL.
22674 ** This routine is could be marked "static". But it is not always used,
22675 ** depending on compile-time options. By omitting the "static", we avoid
22676 ** nuisance compiler warnings about "defined but not used".
22678 void shellPreparePrintf(
22681 sqlite3_stmt
**ppStmt
,
22686 if( *pRc
==SQLITE_OK
){
22689 va_start(ap
, zFmt
);
22690 z
= sqlite3_vmprintf(zFmt
, ap
);
22693 *pRc
= SQLITE_NOMEM
;
22695 shellPrepare(db
, pRc
, z
, ppStmt
);
22701 /* Finalize the prepared statement created using shellPreparePrintf().
22703 ** This routine is could be marked "static". But it is not always used,
22704 ** depending on compile-time options. By omitting the "static", we avoid
22705 ** nuisance compiler warnings about "defined but not used".
22707 void shellFinalize(
22709 sqlite3_stmt
*pStmt
22712 sqlite3
*db
= sqlite3_db_handle(pStmt
);
22713 int rc
= sqlite3_finalize(pStmt
);
22714 if( *pRc
==SQLITE_OK
){
22715 if( rc
!=SQLITE_OK
){
22716 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
22723 /* Reset the prepared statement created using shellPreparePrintf().
22725 ** This routine is could be marked "static". But it is not always used,
22726 ** depending on compile-time options. By omitting the "static", we avoid
22727 ** nuisance compiler warnings about "defined but not used".
22731 sqlite3_stmt
*pStmt
22733 int rc
= sqlite3_reset(pStmt
);
22734 if( *pRc
==SQLITE_OK
){
22735 if( rc
!=SQLITE_OK
){
22736 sqlite3
*db
= sqlite3_db_handle(pStmt
);
22737 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
22742 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
22744 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22745 /******************************************************************************
22746 ** The ".archive" or ".ar" command.
22749 ** Structure representing a single ".ar" command.
22751 typedef struct ArCommand ArCommand
;
22753 u8 eCmd
; /* An AR_CMD_* value */
22754 u8 bVerbose
; /* True if --verbose */
22755 u8 bZip
; /* True if the archive is a ZIP */
22756 u8 bDryRun
; /* True if --dry-run */
22757 u8 bAppend
; /* True if --append */
22758 u8 bGlob
; /* True if --glob */
22759 u8 fromCmdLine
; /* Run from -A instead of .archive */
22760 int nArg
; /* Number of command arguments */
22761 char *zSrcTable
; /* "sqlar", "zipfile($file)" or "zip" */
22762 const char *zFile
; /* --file argument, or NULL */
22763 const char *zDir
; /* --directory argument, or NULL */
22764 char **azArg
; /* Array of command arguments */
22765 ShellState
*p
; /* Shell state */
22766 sqlite3
*db
; /* Database containing the archive */
22770 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
22772 static int arUsage(FILE *f
){
22773 showHelp(f
,"archive");
22774 return SQLITE_ERROR
;
22778 ** Print an error message for the .ar command to stderr and return
22781 static int arErrorMsg(ArCommand
*pAr
, const char *zFmt
, ...){
22784 va_start(ap
, zFmt
);
22785 z
= sqlite3_vmprintf(zFmt
, ap
);
22787 utf8_printf(stderr
, "Error: %s\n", z
);
22788 if( pAr
->fromCmdLine
){
22789 utf8_printf(stderr
, "Use \"-A\" for more help\n");
22791 utf8_printf(stderr
, "Use \".archive --help\" for more help\n");
22794 return SQLITE_ERROR
;
22798 ** Values for ArCommand.eCmd.
22800 #define AR_CMD_CREATE 1
22801 #define AR_CMD_UPDATE 2
22802 #define AR_CMD_INSERT 3
22803 #define AR_CMD_EXTRACT 4
22804 #define AR_CMD_LIST 5
22805 #define AR_CMD_HELP 6
22806 #define AR_CMD_REMOVE 7
22809 ** Other (non-command) switches.
22811 #define AR_SWITCH_VERBOSE 8
22812 #define AR_SWITCH_FILE 9
22813 #define AR_SWITCH_DIRECTORY 10
22814 #define AR_SWITCH_APPEND 11
22815 #define AR_SWITCH_DRYRUN 12
22816 #define AR_SWITCH_GLOB 13
22818 static int arProcessSwitch(ArCommand
*pAr
, int eSwitch
, const char *zArg
){
22820 case AR_CMD_CREATE
:
22821 case AR_CMD_EXTRACT
:
22823 case AR_CMD_REMOVE
:
22824 case AR_CMD_UPDATE
:
22825 case AR_CMD_INSERT
:
22828 return arErrorMsg(pAr
, "multiple command options");
22830 pAr
->eCmd
= eSwitch
;
22833 case AR_SWITCH_DRYRUN
:
22836 case AR_SWITCH_GLOB
:
22839 case AR_SWITCH_VERBOSE
:
22842 case AR_SWITCH_APPEND
:
22844 deliberate_fall_through
;
22845 case AR_SWITCH_FILE
:
22848 case AR_SWITCH_DIRECTORY
:
22857 ** Parse the command line for an ".ar" command. The results are written into
22858 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
22859 ** successfully, otherwise an error message is written to stderr and
22860 ** SQLITE_ERROR returned.
22862 static int arParseCommand(
22863 char **azArg
, /* Array of arguments passed to dot command */
22864 int nArg
, /* Number of entries in azArg[] */
22865 ArCommand
*pAr
/* Populate this object */
22873 { "create", 'c', AR_CMD_CREATE
, 0 },
22874 { "extract", 'x', AR_CMD_EXTRACT
, 0 },
22875 { "insert", 'i', AR_CMD_INSERT
, 0 },
22876 { "list", 't', AR_CMD_LIST
, 0 },
22877 { "remove", 'r', AR_CMD_REMOVE
, 0 },
22878 { "update", 'u', AR_CMD_UPDATE
, 0 },
22879 { "help", 'h', AR_CMD_HELP
, 0 },
22880 { "verbose", 'v', AR_SWITCH_VERBOSE
, 0 },
22881 { "file", 'f', AR_SWITCH_FILE
, 1 },
22882 { "append", 'a', AR_SWITCH_APPEND
, 1 },
22883 { "directory", 'C', AR_SWITCH_DIRECTORY
, 1 },
22884 { "dryrun", 'n', AR_SWITCH_DRYRUN
, 0 },
22885 { "glob", 'g', AR_SWITCH_GLOB
, 0 },
22887 int nSwitch
= sizeof(aSwitch
) / sizeof(struct ArSwitch
);
22888 struct ArSwitch
*pEnd
= &aSwitch
[nSwitch
];
22891 utf8_printf(stderr
, "Wrong number of arguments. Usage:\n");
22892 return arUsage(stderr
);
22894 char *z
= azArg
[1];
22896 /* Traditional style [tar] invocation */
22899 for(i
=0; z
[i
]; i
++){
22900 const char *zArg
= 0;
22901 struct ArSwitch
*pOpt
;
22902 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
22903 if( z
[i
]==pOpt
->cShort
) break;
22906 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
22910 return arErrorMsg(pAr
, "option requires an argument: %c",z
[i
]);
22912 zArg
= azArg
[iArg
++];
22914 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
22916 pAr
->nArg
= nArg
-iArg
;
22918 pAr
->azArg
= &azArg
[iArg
];
22921 /* Non-traditional invocation */
22923 for(iArg
=1; iArg
<nArg
; iArg
++){
22927 /* All remaining command line words are command arguments. */
22928 pAr
->azArg
= &azArg
[iArg
];
22929 pAr
->nArg
= nArg
-iArg
;
22936 /* One or more short options */
22937 for(i
=1; i
<n
; i
++){
22938 const char *zArg
= 0;
22939 struct ArSwitch
*pOpt
;
22940 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
22941 if( z
[i
]==pOpt
->cShort
) break;
22944 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
22951 if( iArg
>=(nArg
-1) ){
22952 return arErrorMsg(pAr
, "option requires an argument: %c",
22955 zArg
= azArg
[++iArg
];
22958 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
22960 }else if( z
[2]=='\0' ){
22961 /* A -- option, indicating that all remaining command line words
22962 ** are command arguments. */
22963 pAr
->azArg
= &azArg
[iArg
+1];
22964 pAr
->nArg
= nArg
-iArg
-1;
22967 /* A long option */
22968 const char *zArg
= 0; /* Argument for option, if any */
22969 struct ArSwitch
*pMatch
= 0; /* Matching option */
22970 struct ArSwitch
*pOpt
; /* Iterator */
22971 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
22972 const char *zLong
= pOpt
->zLong
;
22973 if( (n
-2)<=strlen30(zLong
) && 0==memcmp(&z
[2], zLong
, n
-2) ){
22975 return arErrorMsg(pAr
, "ambiguous option: %s",z
);
22983 return arErrorMsg(pAr
, "unrecognized option: %s", z
);
22985 if( pMatch
->bArg
){
22986 if( iArg
>=(nArg
-1) ){
22987 return arErrorMsg(pAr
, "option requires an argument: %s", z
);
22989 zArg
= azArg
[++iArg
];
22991 if( arProcessSwitch(pAr
, pMatch
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
22996 if( pAr
->eCmd
==0 ){
22997 utf8_printf(stderr
, "Required argument missing. Usage:\n");
22998 return arUsage(stderr
);
23004 ** This function assumes that all arguments within the ArCommand.azArg[]
23005 ** array refer to archive members, as for the --extract, --list or --remove
23006 ** commands. It checks that each of them are "present". If any specified
23007 ** file is not present in the archive, an error is printed to stderr and an
23008 ** error code returned. Otherwise, if all specified arguments are present
23009 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
23010 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
23011 ** when pAr->bGlob is true.
23013 ** This function strips any trailing '/' characters from each argument.
23014 ** This is consistent with the way the [tar] command seems to work on
23017 static int arCheckEntries(ArCommand
*pAr
){
23018 int rc
= SQLITE_OK
;
23021 sqlite3_stmt
*pTest
= 0;
23022 const char *zSel
= (pAr
->bGlob
)
23023 ? "SELECT name FROM %s WHERE glob($name,name)"
23024 : "SELECT name FROM %s WHERE name=$name";
23026 shellPreparePrintf(pAr
->db
, &rc
, &pTest
, zSel
, pAr
->zSrcTable
);
23027 j
= sqlite3_bind_parameter_index(pTest
, "$name");
23028 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
23029 char *z
= pAr
->azArg
[i
];
23030 int n
= strlen30(z
);
23032 while( n
>0 && z
[n
-1]=='/' ) n
--;
23034 sqlite3_bind_text(pTest
, j
, z
, -1, SQLITE_STATIC
);
23035 if( SQLITE_ROW
==sqlite3_step(pTest
) ){
23038 shellReset(&rc
, pTest
);
23039 if( rc
==SQLITE_OK
&& bOk
==0 ){
23040 utf8_printf(stderr
, "not found in archive: %s\n", z
);
23044 shellFinalize(&rc
, pTest
);
23050 ** Format a WHERE clause that can be used against the "sqlar" table to
23051 ** identify all archive members that match the command arguments held
23052 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
23053 ** The caller is responsible for eventually calling sqlite3_free() on
23054 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
23055 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
23057 static void arWhereClause(
23060 char **pzWhere
/* OUT: New WHERE clause */
23063 const char *zSameOp
= (pAr
->bGlob
)? "GLOB" : "=";
23064 if( *pRc
==SQLITE_OK
){
23065 if( pAr
->nArg
==0 ){
23066 zWhere
= sqlite3_mprintf("1");
23069 const char *zSep
= "";
23070 for(i
=0; i
<pAr
->nArg
; i
++){
23071 const char *z
= pAr
->azArg
[i
];
23072 zWhere
= sqlite3_mprintf(
23073 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
23074 zWhere
, zSep
, zSameOp
, z
, strlen30(z
)+1, zSameOp
, z
23077 *pRc
= SQLITE_NOMEM
;
23088 ** Implementation of .ar "lisT" command.
23090 static int arListCommand(ArCommand
*pAr
){
23091 const char *zSql
= "SELECT %s FROM %s WHERE %s";
23092 const char *azCols
[] = {
23094 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
23098 sqlite3_stmt
*pSql
= 0;
23101 rc
= arCheckEntries(pAr
);
23102 arWhereClause(&rc
, pAr
, &zWhere
);
23104 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql
, azCols
[pAr
->bVerbose
],
23105 pAr
->zSrcTable
, zWhere
);
23106 if( pAr
->bDryRun
){
23107 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
23109 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
23110 if( pAr
->bVerbose
){
23111 utf8_printf(pAr
->p
->out
, "%s % 10d %s %s\n",
23112 sqlite3_column_text(pSql
, 0),
23113 sqlite3_column_int(pSql
, 1),
23114 sqlite3_column_text(pSql
, 2),
23115 sqlite3_column_text(pSql
, 3)
23118 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
23122 shellFinalize(&rc
, pSql
);
23123 sqlite3_free(zWhere
);
23129 ** Implementation of .ar "Remove" command.
23131 static int arRemoveCommand(ArCommand
*pAr
){
23137 /* Verify that args actually exist within the archive before proceeding.
23138 ** And formulate a WHERE clause to match them. */
23139 rc
= arCheckEntries(pAr
);
23140 arWhereClause(&rc
, pAr
, &zWhere
);
23142 if( rc
==SQLITE_OK
){
23143 zSql
= sqlite3_mprintf("DELETE FROM %s WHERE %s;",
23144 pAr
->zSrcTable
, zWhere
);
23145 if( pAr
->bDryRun
){
23146 utf8_printf(pAr
->p
->out
, "%s\n", zSql
);
23149 rc
= sqlite3_exec(pAr
->db
, "SAVEPOINT ar;", 0, 0, 0);
23150 if( rc
==SQLITE_OK
){
23151 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
23152 if( rc
!=SQLITE_OK
){
23153 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
23155 rc
= sqlite3_exec(pAr
->db
, "RELEASE ar;", 0, 0, 0);
23159 utf8_printf(stdout
, "ERROR: %s\n", zErr
);
23160 sqlite3_free(zErr
);
23164 sqlite3_free(zWhere
);
23165 sqlite3_free(zSql
);
23170 ** Implementation of .ar "eXtract" command.
23172 static int arExtractCommand(ArCommand
*pAr
){
23173 const char *zSql1
=
23176 " writefile(($dir || name), %s, mode, mtime) "
23177 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
23178 " AND name NOT GLOB '*..[/\\]*'";
23180 const char *azExtraArg
[] = {
23181 "sqlar_uncompress(data, sz)",
23185 sqlite3_stmt
*pSql
= 0;
23186 int rc
= SQLITE_OK
;
23191 /* If arguments are specified, check that they actually exist within
23192 ** the archive before proceeding. And formulate a WHERE clause to
23194 rc
= arCheckEntries(pAr
);
23195 arWhereClause(&rc
, pAr
, &zWhere
);
23197 if( rc
==SQLITE_OK
){
23199 zDir
= sqlite3_mprintf("%s/", pAr
->zDir
);
23201 zDir
= sqlite3_mprintf("");
23203 if( zDir
==0 ) rc
= SQLITE_NOMEM
;
23206 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql1
,
23207 azExtraArg
[pAr
->bZip
], pAr
->zSrcTable
, zWhere
23210 if( rc
==SQLITE_OK
){
23211 j
= sqlite3_bind_parameter_index(pSql
, "$dir");
23212 sqlite3_bind_text(pSql
, j
, zDir
, -1, SQLITE_STATIC
);
23214 /* Run the SELECT statement twice. The first time, writefile() is called
23215 ** for all archive members that should be extracted. The second time,
23216 ** only for the directories. This is because the timestamps for
23217 ** extracted directories must be reset after they are populated (as
23218 ** populating them changes the timestamp). */
23219 for(i
=0; i
<2; i
++){
23220 j
= sqlite3_bind_parameter_index(pSql
, "$dirOnly");
23221 sqlite3_bind_int(pSql
, j
, i
);
23222 if( pAr
->bDryRun
){
23223 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
23225 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
23226 if( i
==0 && pAr
->bVerbose
){
23227 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
23231 shellReset(&rc
, pSql
);
23233 shellFinalize(&rc
, pSql
);
23236 sqlite3_free(zDir
);
23237 sqlite3_free(zWhere
);
23242 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
23244 static int arExecSql(ArCommand
*pAr
, const char *zSql
){
23246 if( pAr
->bDryRun
){
23247 utf8_printf(pAr
->p
->out
, "%s\n", zSql
);
23251 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
23253 utf8_printf(stdout
, "ERROR: %s\n", zErr
);
23254 sqlite3_free(zErr
);
23262 ** Implementation of .ar "create", "insert", and "update" commands.
23264 ** create -> Create a new SQL archive
23265 ** insert -> Insert or reinsert all files listed
23266 ** update -> Insert files that have changed or that were not
23267 ** previously in the archive
23269 ** Create the "sqlar" table in the database if it does not already exist.
23270 ** Then add each file in the azFile[] array to the archive. Directories
23271 ** are added recursively. If argument bVerbose is non-zero, a message is
23272 ** printed on stdout for each file archived.
23274 ** The create command is the same as update, except that it drops
23275 ** any existing "sqlar" table before beginning. The "insert" command
23276 ** always overwrites every file named on the command-line, where as
23277 ** "update" only overwrites if the size or mtime or mode has changed.
23279 static int arCreateOrUpdateCommand(
23280 ArCommand
*pAr
, /* Command arguments and options */
23281 int bUpdate
, /* true for a --create. */
23282 int bOnlyIfChanged
/* Only update if file has changed */
23284 const char *zCreate
=
23285 "CREATE TABLE IF NOT EXISTS sqlar(\n"
23286 " name TEXT PRIMARY KEY, -- name of the file\n"
23287 " mode INT, -- access permissions\n"
23288 " mtime INT, -- last modification time\n"
23289 " sz INT, -- original file size\n"
23290 " data BLOB -- compressed content\n"
23292 const char *zDrop
= "DROP TABLE IF EXISTS sqlar";
23293 const char *zInsertFmt
[2] = {
23294 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
23299 " CASE substr(lsmode(mode),1,1)\n"
23300 " WHEN '-' THEN length(data)\n"
23301 " WHEN 'd' THEN 0\n"
23303 " sqlar_compress(data)\n"
23304 " FROM fsdir(%Q,%Q) AS disk\n"
23305 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
23307 "REPLACE INTO %s(name,mode,mtime,data)\n"
23313 " FROM fsdir(%Q,%Q) AS disk\n"
23314 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
23316 int i
; /* For iterating through azFile[] */
23317 int rc
; /* Return code */
23318 const char *zTab
= 0; /* SQL table into which to insert */
23323 arExecSql(pAr
, "PRAGMA page_size=512");
23324 rc
= arExecSql(pAr
, "SAVEPOINT ar;");
23325 if( rc
!=SQLITE_OK
) return rc
;
23328 /* Initialize the zipfile virtual table, if necessary */
23331 sqlite3_randomness(sizeof(r
),&r
);
23332 sqlite3_snprintf(sizeof(zTemp
),zTemp
,"zip%016llx",r
);
23334 zSql
= sqlite3_mprintf(
23335 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
23338 rc
= arExecSql(pAr
, zSql
);
23339 sqlite3_free(zSql
);
23344 /* Initialize the table for an SQLAR */
23347 rc
= arExecSql(pAr
, zDrop
);
23348 if( rc
!=SQLITE_OK
) goto end_ar_transaction
;
23350 rc
= arExecSql(pAr
, zCreate
);
23352 if( bOnlyIfChanged
){
23353 zExists
= sqlite3_mprintf(
23355 "SELECT 1 FROM %s AS mem"
23356 " WHERE mem.name=disk.name"
23357 " AND mem.mtime=disk.mtime"
23358 " AND mem.mode=disk.mode)", zTab
);
23360 zExists
= sqlite3_mprintf("");
23362 if( zExists
==0 ) rc
= SQLITE_NOMEM
;
23363 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
23364 char *zSql2
= sqlite3_mprintf(zInsertFmt
[pAr
->bZip
], zTab
,
23365 pAr
->bVerbose
? "shell_putsnl(name)" : "name",
23366 pAr
->azArg
[i
], pAr
->zDir
, zExists
);
23367 rc
= arExecSql(pAr
, zSql2
);
23368 sqlite3_free(zSql2
);
23370 end_ar_transaction
:
23371 if( rc
!=SQLITE_OK
){
23372 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
23374 rc
= arExecSql(pAr
, "RELEASE ar;");
23375 if( pAr
->bZip
&& pAr
->zFile
){
23376 zSql
= sqlite3_mprintf("DROP TABLE %s", zTemp
);
23377 arExecSql(pAr
, zSql
);
23378 sqlite3_free(zSql
);
23381 sqlite3_free(zExists
);
23386 ** Implementation of ".ar" dot command.
23388 static int arDotCommand(
23389 ShellState
*pState
, /* Current shell tool state */
23390 int fromCmdLine
, /* True if -A command-line option, not .ar cmd */
23391 char **azArg
, /* Array of arguments passed to dot command */
23392 int nArg
/* Number of entries in azArg[] */
23396 memset(&cmd
, 0, sizeof(cmd
));
23397 cmd
.fromCmdLine
= fromCmdLine
;
23398 rc
= arParseCommand(azArg
, nArg
, &cmd
);
23399 if( rc
==SQLITE_OK
){
23400 int eDbType
= SHELL_OPEN_UNSPEC
;
23402 cmd
.db
= pState
->db
;
23404 eDbType
= deduceDatabaseType(cmd
.zFile
, 1);
23406 eDbType
= pState
->openMode
;
23408 if( eDbType
==SHELL_OPEN_ZIPFILE
){
23409 if( cmd
.eCmd
==AR_CMD_EXTRACT
|| cmd
.eCmd
==AR_CMD_LIST
){
23410 if( cmd
.zFile
==0 ){
23411 cmd
.zSrcTable
= sqlite3_mprintf("zip");
23413 cmd
.zSrcTable
= sqlite3_mprintf("zipfile(%Q)", cmd
.zFile
);
23417 }else if( cmd
.zFile
){
23419 if( cmd
.bAppend
) eDbType
= SHELL_OPEN_APPENDVFS
;
23420 if( cmd
.eCmd
==AR_CMD_CREATE
|| cmd
.eCmd
==AR_CMD_INSERT
23421 || cmd
.eCmd
==AR_CMD_REMOVE
|| cmd
.eCmd
==AR_CMD_UPDATE
){
23422 flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
23424 flags
= SQLITE_OPEN_READONLY
;
23428 utf8_printf(pState
->out
, "-- open database '%s'%s\n", cmd
.zFile
,
23429 eDbType
==SHELL_OPEN_APPENDVFS
? " using 'apndvfs'" : "");
23431 rc
= sqlite3_open_v2(cmd
.zFile
, &cmd
.db
, flags
,
23432 eDbType
==SHELL_OPEN_APPENDVFS
? "apndvfs" : 0);
23433 if( rc
!=SQLITE_OK
){
23434 utf8_printf(stderr
, "cannot open file: %s (%s)\n",
23435 cmd
.zFile
, sqlite3_errmsg(cmd
.db
)
23437 goto end_ar_command
;
23439 sqlite3_fileio_init(cmd
.db
, 0, 0);
23440 sqlite3_sqlar_init(cmd
.db
, 0, 0);
23441 sqlite3_create_function(cmd
.db
, "shell_putsnl", 1, SQLITE_UTF8
, cmd
.p
,
23442 shellPutsFunc
, 0, 0);
23445 if( cmd
.zSrcTable
==0 && cmd
.bZip
==0 && cmd
.eCmd
!=AR_CMD_HELP
){
23446 if( cmd
.eCmd
!=AR_CMD_CREATE
23447 && sqlite3_table_column_metadata(cmd
.db
,0,"sqlar","name",0,0,0,0,0)
23449 utf8_printf(stderr
, "database does not contain an 'sqlar' table\n");
23451 goto end_ar_command
;
23453 cmd
.zSrcTable
= sqlite3_mprintf("sqlar");
23456 switch( cmd
.eCmd
){
23457 case AR_CMD_CREATE
:
23458 rc
= arCreateOrUpdateCommand(&cmd
, 0, 0);
23461 case AR_CMD_EXTRACT
:
23462 rc
= arExtractCommand(&cmd
);
23466 rc
= arListCommand(&cmd
);
23470 arUsage(pState
->out
);
23473 case AR_CMD_INSERT
:
23474 rc
= arCreateOrUpdateCommand(&cmd
, 1, 0);
23477 case AR_CMD_REMOVE
:
23478 rc
= arRemoveCommand(&cmd
);
23482 assert( cmd
.eCmd
==AR_CMD_UPDATE
);
23483 rc
= arCreateOrUpdateCommand(&cmd
, 1, 1);
23488 if( cmd
.db
!=pState
->db
){
23491 sqlite3_free(cmd
.zSrcTable
);
23495 /* End of the ".archive" or ".ar" command logic
23496 *******************************************************************************/
23497 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
23499 #if SQLITE_SHELL_HAVE_RECOVER
23502 ** This function is used as a callback by the recover extension. Simply
23503 ** print the supplied SQL statement to stdout.
23505 static int recoverSqlCb(void *pCtx
, const char *zSql
){
23506 ShellState
*pState
= (ShellState
*)pCtx
;
23507 utf8_printf(pState
->out
, "%s;\n", zSql
);
23512 ** This function is called to recover data from the database. A script
23513 ** to construct a new database containing all recovered data is output
23514 ** on stream pState->out.
23516 static int recoverDatabaseCmd(ShellState
*pState
, int nArg
, char **azArg
){
23517 int rc
= SQLITE_OK
;
23518 const char *zRecoveryDb
= ""; /* Name of "recovery" database. Debug only */
23519 const char *zLAF
= "lost_and_found";
23520 int bFreelist
= 1; /* 0 if --ignore-freelist is specified */
23521 int bRowids
= 1; /* 0 if --no-rowids */
23522 sqlite3_recover
*p
= 0;
23525 for(i
=1; i
<nArg
; i
++){
23526 char *z
= azArg
[i
];
23528 if( z
[0]=='-' && z
[1]=='-' ) z
++;
23530 if( n
<=17 && memcmp("-ignore-freelist", z
, n
)==0 ){
23533 if( n
<=12 && memcmp("-recovery-db", z
, n
)==0 && i
<(nArg
-1) ){
23534 /* This option determines the name of the ATTACH-ed database used
23535 ** internally by the recovery extension. The default is "" which
23536 ** means to use a temporary database that is automatically deleted
23537 ** when closed. This option is undocumented and might disappear at
23540 zRecoveryDb
= azArg
[i
];
23542 if( n
<=15 && memcmp("-lost-and-found", z
, n
)==0 && i
<(nArg
-1) ){
23546 if( n
<=10 && memcmp("-no-rowids", z
, n
)==0 ){
23550 utf8_printf(stderr
, "unexpected option: %s\n", azArg
[i
]);
23551 showHelp(pState
->out
, azArg
[0]);
23556 p
= sqlite3_recover_init_sql(
23557 pState
->db
, "main", recoverSqlCb
, (void*)pState
23560 sqlite3_recover_config(p
, 789, (void*)zRecoveryDb
); /* Debug use only */
23561 sqlite3_recover_config(p
, SQLITE_RECOVER_LOST_AND_FOUND
, (void*)zLAF
);
23562 sqlite3_recover_config(p
, SQLITE_RECOVER_ROWIDS
, (void*)&bRowids
);
23563 sqlite3_recover_config(p
, SQLITE_RECOVER_FREELIST_CORRUPT
,(void*)&bFreelist
);
23565 sqlite3_recover_run(p
);
23566 if( sqlite3_recover_errcode(p
)!=SQLITE_OK
){
23567 const char *zErr
= sqlite3_recover_errmsg(p
);
23568 int errCode
= sqlite3_recover_errcode(p
);
23569 raw_printf(stderr
, "sql error: %s (%d)\n", zErr
, errCode
);
23571 rc
= sqlite3_recover_finish(p
);
23574 #endif /* SQLITE_SHELL_HAVE_RECOVER */
23578 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
23579 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
23580 * close db and set it to 0, and return the columns spec, to later
23581 * be sqlite3_free()'ed by the caller.
23582 * The return is 0 when either:
23583 * (a) The db was not initialized and zCol==0 (There are no columns.)
23584 * (b) zCol!=0 (Column was added, db initialized as needed.)
23585 * The 3rd argument, pRenamed, references an out parameter. If the
23586 * pointer is non-zero, its referent will be set to a summary of renames
23587 * done if renaming was necessary, or set to 0 if none was done. The out
23588 * string (if any) must be sqlite3_free()'ed by the caller.
23591 #define rc_err_oom_die(rc) \
23592 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
23593 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
23594 fprintf(stderr,"E:%d\n",rc), assert(0)
23596 static void rc_err_oom_die(int rc
){
23597 if( rc
==SQLITE_NOMEM
) shell_check_oom(0);
23598 assert(rc
==SQLITE_OK
||rc
==SQLITE_DONE
);
23602 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
23603 static char zCOL_DB
[] = SHELL_STRINGIFY(SHELL_COLFIX_DB
);
23604 #else /* Otherwise, memory is faster/better for the transient DB. */
23605 static const char *zCOL_DB
= ":memory:";
23608 /* Define character (as C string) to separate generated column ordinal
23609 * from protected part of incoming column names. This defaults to "_"
23610 * so that incoming column identifiers that did not need not be quoted
23611 * remain usable without being quoted. It must be one character.
23613 #ifndef SHELL_AUTOCOLUMN_SEP
23614 # define AUTOCOLUMN_SEP "_"
23616 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
23619 static char *zAutoColumn(const char *zColNew
, sqlite3
**pDb
, char **pzRenamed
){
23620 /* Queries and D{D,M}L used here */
23621 static const char * const zTabMake
= "\
23622 CREATE TABLE ColNames(\
23623 cpos INTEGER PRIMARY KEY,\
23624 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
23625 CREATE VIEW RepeatedNames AS \
23626 SELECT DISTINCT t.name FROM ColNames t \
23627 WHERE t.name COLLATE NOCASE IN (\
23628 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
23631 static const char * const zTabFill
= "\
23632 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
23633 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
23635 static const char * const zHasDupes
= "\
23636 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
23637 <count(name) FROM ColNames\
23639 #ifdef SHELL_COLUMN_RENAME_CLEAN
23640 static const char * const zDedoctor
= "\
23641 UPDATE ColNames SET chop=iif(\
23642 (substring(name,nlen,1) BETWEEN '0' AND '9')\
23643 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP
"'),\
23644 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP
"0123456789')),\
23649 static const char * const zSetReps
= "\
23650 UPDATE ColNames AS t SET reps=\
23651 (SELECT count(*) FROM ColNames d \
23652 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
23656 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
23657 static const char * const zColDigits
= "\
23658 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
23661 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
23662 static const char * const zColDigits
= "\
23663 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
23664 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
23665 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
23668 static const char * const zRenameRank
=
23669 #ifdef SHELL_COLUMN_RENAME_CLEAN
23670 "UPDATE ColNames AS t SET suff="
23671 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP
"', $1, cpos), '')"
23672 #else /* ...RENAME_MINIMAL_ONE_PASS */
23673 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
23676 " SELECT nlz+1 AS nlz FROM Lzn"
23679 " FROM ColNames t, ColNames o"
23681 " iif(t.name IN (SELECT * FROM RepeatedNames),"
23682 " printf('%s"AUTOCOLUMN_SEP
"%s',"
23683 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
23687 " iif(o.name IN (SELECT * FROM RepeatedNames),"
23688 " printf('%s"AUTOCOLUMN_SEP
"%s',"
23689 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
23693 " AND o.cpos<>t.cpos"
23696 ") UPDATE Colnames AS t SET"
23697 " chop = 0," /* No chopping, never touch incoming names. */
23698 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
23699 " printf('"AUTOCOLUMN_SEP
"%s', substring("
23700 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
23705 static const char * const zCollectVar
= "\
23710 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
23711 ||')' AS ColsSpec \
23713 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
23714 FROM ColNames ORDER BY cpos\
23716 static const char * const zRenamesDone
=
23717 "SELECT group_concat("
23718 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
23720 "FROM ColNames WHERE suff<>'' OR chop!=0"
23723 sqlite3_stmt
*pStmt
= 0;
23726 /* Add initial or additional column. Init db if necessary. */
23728 if( SQLITE_OK
!=sqlite3_open(zCOL_DB
, pDb
) ) return 0;
23729 #ifdef SHELL_COLFIX_DB
23731 sqlite3_exec(*pDb
,"drop table if exists ColNames;"
23732 "drop view if exists RepeatedNames;",0,0,0);
23734 rc
= sqlite3_exec(*pDb
, zTabMake
, 0, 0, 0);
23735 rc_err_oom_die(rc
);
23738 rc
= sqlite3_prepare_v2(*pDb
, zTabFill
, -1, &pStmt
, 0);
23739 rc_err_oom_die(rc
);
23740 rc
= sqlite3_bind_text(pStmt
, 1, zColNew
, -1, 0);
23741 rc_err_oom_die(rc
);
23742 rc
= sqlite3_step(pStmt
);
23743 rc_err_oom_die(rc
);
23744 sqlite3_finalize(pStmt
);
23746 }else if( *pDb
==0 ){
23749 /* Formulate the columns spec, close the DB, zero *pDb. */
23750 char *zColsSpec
= 0;
23751 int hasDupes
= db_int(*pDb
, zHasDupes
);
23752 int nDigits
= (hasDupes
)? db_int(*pDb
, zColDigits
) : 0;
23754 #ifdef SHELL_COLUMN_RENAME_CLEAN
23755 rc
= sqlite3_exec(*pDb
, zDedoctor
, 0, 0, 0);
23756 rc_err_oom_die(rc
);
23758 rc
= sqlite3_exec(*pDb
, zSetReps
, 0, 0, 0);
23759 rc_err_oom_die(rc
);
23760 rc
= sqlite3_prepare_v2(*pDb
, zRenameRank
, -1, &pStmt
, 0);
23761 rc_err_oom_die(rc
);
23762 sqlite3_bind_int(pStmt
, 1, nDigits
);
23763 rc
= sqlite3_step(pStmt
);
23764 sqlite3_finalize(pStmt
);
23765 if( rc
!=SQLITE_DONE
) rc_err_oom_die(SQLITE_NOMEM
);
23767 assert(db_int(*pDb
, zHasDupes
)==0); /* Consider: remove this */
23768 rc
= sqlite3_prepare_v2(*pDb
, zCollectVar
, -1, &pStmt
, 0);
23769 rc_err_oom_die(rc
);
23770 rc
= sqlite3_step(pStmt
);
23771 if( rc
==SQLITE_ROW
){
23772 zColsSpec
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
23776 if( pzRenamed
!=0 ){
23777 if( !hasDupes
) *pzRenamed
= 0;
23779 sqlite3_finalize(pStmt
);
23780 if( SQLITE_OK
==sqlite3_prepare_v2(*pDb
, zRenamesDone
, -1, &pStmt
, 0)
23781 && SQLITE_ROW
==sqlite3_step(pStmt
) ){
23782 *pzRenamed
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
23787 sqlite3_finalize(pStmt
);
23788 sqlite3_close(*pDb
);
23795 ** If an input line begins with "." then invoke this routine to
23796 ** process that line.
23798 ** Return 1 on error, 2 to exit, and 0 otherwise.
23800 static int do_meta_command(char *zLine
, ShellState
*p
){
23807 #ifndef SQLITE_OMIT_VIRTUALTABLE
23808 if( p
->expert
.pExpert
){
23809 expertFinish(p
, 1, 0);
23813 /* Parse the input line into tokens.
23815 while( zLine
[h
] && nArg
<ArraySize(azArg
)-1 ){
23816 while( IsSpace(zLine
[h
]) ){ h
++; }
23817 if( zLine
[h
]==0 ) break;
23818 if( zLine
[h
]=='\'' || zLine
[h
]=='"' ){
23819 int delim
= zLine
[h
++];
23820 azArg
[nArg
++] = &zLine
[h
];
23821 while( zLine
[h
] && zLine
[h
]!=delim
){
23822 if( zLine
[h
]=='\\' && delim
=='"' && zLine
[h
+1]!=0 ) h
++;
23825 if( zLine
[h
]==delim
){
23828 if( delim
=='"' ) resolve_backslashes(azArg
[nArg
-1]);
23830 azArg
[nArg
++] = &zLine
[h
];
23831 while( zLine
[h
] && !IsSpace(zLine
[h
]) ){ h
++; }
23832 if( zLine
[h
] ) zLine
[h
++] = 0;
23833 resolve_backslashes(azArg
[nArg
-1]);
23838 /* Process the input line.
23840 if( nArg
==0 ) return 0; /* no tokens, no error */
23841 n
= strlen30(azArg
[0]);
23845 #ifndef SQLITE_OMIT_AUTHORIZATION
23846 if( c
=='a' && cli_strncmp(azArg
[0], "auth", n
)==0 ){
23848 raw_printf(stderr
, "Usage: .auth ON|OFF\n");
23850 goto meta_command_exit
;
23853 if( booleanValue(azArg
[1]) ){
23854 sqlite3_set_authorizer(p
->db
, shellAuth
, p
);
23855 }else if( p
->bSafeModePersist
){
23856 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
23858 sqlite3_set_authorizer(p
->db
, 0, 0);
23863 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
23864 && !defined(SQLITE_SHELL_FIDDLE)
23865 if( c
=='a' && cli_strncmp(azArg
[0], "archive", n
)==0 ){
23867 failIfSafeMode(p
, "cannot run .archive in safe mode");
23868 rc
= arDotCommand(p
, 0, azArg
, nArg
);
23872 #ifndef SQLITE_SHELL_FIDDLE
23873 if( (c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "backup", n
)==0)
23874 || (c
=='s' && n
>=3 && cli_strncmp(azArg
[0], "save", n
)==0)
23876 const char *zDestFile
= 0;
23877 const char *zDb
= 0;
23879 sqlite3_backup
*pBackup
;
23882 const char *zVfs
= 0;
23883 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
23884 for(j
=1; j
<nArg
; j
++){
23885 const char *z
= azArg
[j
];
23887 if( z
[1]=='-' ) z
++;
23888 if( cli_strcmp(z
, "-append")==0 ){
23891 if( cli_strcmp(z
, "-async")==0 ){
23895 utf8_printf(stderr
, "unknown option: %s\n", azArg
[j
]);
23898 }else if( zDestFile
==0 ){
23899 zDestFile
= azArg
[j
];
23900 }else if( zDb
==0 ){
23902 zDestFile
= azArg
[j
];
23904 raw_printf(stderr
, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
23908 if( zDestFile
==0 ){
23909 raw_printf(stderr
, "missing FILENAME argument on .backup\n");
23912 if( zDb
==0 ) zDb
= "main";
23913 rc
= sqlite3_open_v2(zDestFile
, &pDest
,
23914 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
, zVfs
);
23915 if( rc
!=SQLITE_OK
){
23916 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zDestFile
);
23921 sqlite3_exec(pDest
, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
23925 pBackup
= sqlite3_backup_init(pDest
, "main", p
->db
, zDb
);
23927 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
23931 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
){}
23932 sqlite3_backup_finish(pBackup
);
23933 if( rc
==SQLITE_DONE
){
23936 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
23941 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
23943 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "bail", n
)==0 ){
23945 bail_on_error
= booleanValue(azArg
[1]);
23947 raw_printf(stderr
, "Usage: .bail on|off\n");
23952 /* Undocumented. Legacy only. See "crnl" below */
23953 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "binary", n
)==0 ){
23955 if( booleanValue(azArg
[1]) ){
23956 setBinaryMode(p
->out
, 1);
23958 setTextMode(p
->out
, 1);
23961 raw_printf(stderr
, "The \".binary\" command is deprecated."
23962 " Use \".crnl\" instead.\n");
23963 raw_printf(stderr
, "Usage: .binary on|off\n");
23968 /* The undocumented ".breakpoint" command causes a call to the no-op
23969 ** routine named test_breakpoint().
23971 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "breakpoint", n
)==0 ){
23975 #ifndef SQLITE_SHELL_FIDDLE
23976 if( c
=='c' && cli_strcmp(azArg
[0],"cd")==0 ){
23977 failIfSafeMode(p
, "cannot run .cd in safe mode");
23979 #if defined(_WIN32) || defined(WIN32)
23980 wchar_t *z
= sqlite3_win32_utf8_to_unicode(azArg
[1]);
23981 rc
= !SetCurrentDirectoryW(z
);
23984 rc
= chdir(azArg
[1]);
23987 utf8_printf(stderr
, "Cannot change to directory \"%s\"\n", azArg
[1]);
23991 raw_printf(stderr
, "Usage: .cd DIRECTORY\n");
23995 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
23997 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "changes", n
)==0 ){
23999 setOrClearFlag(p
, SHFLG_CountChanges
, azArg
[1]);
24001 raw_printf(stderr
, "Usage: .changes on|off\n");
24006 #ifndef SQLITE_SHELL_FIDDLE
24007 /* Cancel output redirection, if it is currently set (by .testcase)
24008 ** Then read the content of the testcase-out.txt file and compare against
24009 ** azArg[1]. If there are differences, report an error and exit.
24011 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "check", n
)==0 ){
24015 raw_printf(stderr
, "Usage: .check GLOB-PATTERN\n");
24017 }else if( (zRes
= readFile("testcase-out.txt", 0))==0 ){
24019 }else if( testcase_glob(azArg
[1],zRes
)==0 ){
24020 utf8_printf(stderr
,
24021 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24022 p
->zTestcase
, azArg
[1], zRes
);
24025 utf8_printf(stdout
, "testcase-%s ok\n", p
->zTestcase
);
24028 sqlite3_free(zRes
);
24030 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24032 #ifndef SQLITE_SHELL_FIDDLE
24033 if( c
=='c' && cli_strncmp(azArg
[0], "clone", n
)==0 ){
24034 failIfSafeMode(p
, "cannot run .clone in safe mode");
24036 tryToClone(p
, azArg
[1]);
24038 raw_printf(stderr
, "Usage: .clone FILENAME\n");
24042 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24044 if( c
=='c' && cli_strncmp(azArg
[0], "connection", n
)==0 ){
24046 /* List available connections */
24048 for(i
=0; i
<ArraySize(p
->aAuxDb
); i
++){
24049 const char *zFile
= p
->aAuxDb
[i
].zDbFilename
;
24050 if( p
->aAuxDb
[i
].db
==0 && p
->pAuxDb
!=&p
->aAuxDb
[i
] ){
24051 zFile
= "(not open)";
24052 }else if( zFile
==0 ){
24053 zFile
= "(memory)";
24054 }else if( zFile
[0]==0 ){
24055 zFile
= "(temporary-file)";
24057 if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
24058 utf8_printf(stdout
, "ACTIVE %d: %s\n", i
, zFile
);
24059 }else if( p
->aAuxDb
[i
].db
!=0 ){
24060 utf8_printf(stdout
, " %d: %s\n", i
, zFile
);
24063 }else if( nArg
==2 && IsDigit(azArg
[1][0]) && azArg
[1][1]==0 ){
24064 int i
= azArg
[1][0] - '0';
24065 if( p
->pAuxDb
!= &p
->aAuxDb
[i
] && i
>=0 && i
<ArraySize(p
->aAuxDb
) ){
24066 p
->pAuxDb
->db
= p
->db
;
24067 p
->pAuxDb
= &p
->aAuxDb
[i
];
24068 globalDb
= p
->db
= p
->pAuxDb
->db
;
24071 }else if( nArg
==3 && cli_strcmp(azArg
[1], "close")==0
24072 && IsDigit(azArg
[2][0]) && azArg
[2][1]==0 ){
24073 int i
= azArg
[2][0] - '0';
24074 if( i
<0 || i
>=ArraySize(p
->aAuxDb
) ){
24076 }else if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
24077 raw_printf(stderr
, "cannot close the active database connection\n");
24079 }else if( p
->aAuxDb
[i
].db
){
24080 session_close_all(p
, i
);
24081 close_db(p
->aAuxDb
[i
].db
);
24082 p
->aAuxDb
[i
].db
= 0;
24085 raw_printf(stderr
, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
24090 if( c
=='c' && n
==4 && cli_strncmp(azArg
[0], "crnl", n
)==0 ){
24092 if( booleanValue(azArg
[1]) ){
24093 setTextMode(p
->out
, 1);
24095 setBinaryMode(p
->out
, 1);
24098 #if !defined(_WIN32) && !defined(WIN32)
24099 raw_printf(stderr
, "The \".crnl\" is a no-op on non-Windows machines.\n");
24101 raw_printf(stderr
, "Usage: .crnl on|off\n");
24106 if( c
=='d' && n
>1 && cli_strncmp(azArg
[0], "databases", n
)==0 ){
24109 sqlite3_stmt
*pStmt
;
24112 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
24114 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
24117 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
24118 const char *zSchema
= (const char *)sqlite3_column_text(pStmt
,1);
24119 const char *zFile
= (const char*)sqlite3_column_text(pStmt
,2);
24120 if( zSchema
==0 || zFile
==0 ) continue;
24121 azName
= sqlite3_realloc(azName
, (nName
+1)*2*sizeof(char*));
24122 shell_check_oom(azName
);
24123 azName
[nName
*2] = strdup(zSchema
);
24124 azName
[nName
*2+1] = strdup(zFile
);
24128 sqlite3_finalize(pStmt
);
24129 for(i
=0; i
<nName
; i
++){
24130 int eTxn
= sqlite3_txn_state(p
->db
, azName
[i
*2]);
24131 int bRdonly
= sqlite3_db_readonly(p
->db
, azName
[i
*2]);
24132 const char *z
= azName
[i
*2+1];
24133 utf8_printf(p
->out
, "%s: %s %s%s\n",
24135 z
&& z
[0] ? z
: "\"\"",
24136 bRdonly
? "r/o" : "r/w",
24137 eTxn
==SQLITE_TXN_NONE
? "" :
24138 eTxn
==SQLITE_TXN_READ
? " read-txn" : " write-txn");
24140 free(azName
[i
*2+1]);
24142 sqlite3_free(azName
);
24145 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbconfig", n
)==0 ){
24146 static const struct DbConfigChoices
{
24150 { "defensive", SQLITE_DBCONFIG_DEFENSIVE
},
24151 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL
},
24152 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML
},
24153 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY
},
24154 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG
},
24155 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER
},
24156 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW
},
24157 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
},
24158 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE
},
24159 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT
},
24160 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
},
24161 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
},
24162 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE
},
24163 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER
},
24164 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS
},
24165 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP
},
24166 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA
},
24167 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA
},
24171 for(ii
=0; ii
<ArraySize(aDbConfig
); ii
++){
24172 if( nArg
>1 && cli_strcmp(azArg
[1], aDbConfig
[ii
].zName
)!=0 ) continue;
24174 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, booleanValue(azArg
[2]), 0);
24176 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, -1, &v
);
24177 utf8_printf(p
->out
, "%19s %s\n", aDbConfig
[ii
].zName
, v
? "on" : "off");
24178 if( nArg
>1 ) break;
24180 if( nArg
>1 && ii
==ArraySize(aDbConfig
) ){
24181 utf8_printf(stderr
, "Error: unknown dbconfig \"%s\"\n", azArg
[1]);
24182 utf8_printf(stderr
, "Enter \".dbconfig\" with no arguments for a list\n");
24186 #if SQLITE_SHELL_HAVE_RECOVER
24187 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbinfo", n
)==0 ){
24188 rc
= shell_dbinfo_command(p
, nArg
, azArg
);
24191 if( c
=='r' && cli_strncmp(azArg
[0], "recover", n
)==0 ){
24193 rc
= recoverDatabaseCmd(p
, nArg
, azArg
);
24195 #endif /* SQLITE_SHELL_HAVE_RECOVER */
24197 if( c
=='d' && cli_strncmp(azArg
[0], "dump", n
)==0 ){
24201 int savedShowHeader
= p
->showHeader
;
24202 int savedShellFlags
= p
->shellFlgs
;
24204 SHFLG_PreserveRowid
|SHFLG_Newlines
|SHFLG_Echo
24205 |SHFLG_DumpDataOnly
|SHFLG_DumpNoSys
);
24206 for(i
=1; i
<nArg
; i
++){
24207 if( azArg
[i
][0]=='-' ){
24208 const char *z
= azArg
[i
]+1;
24209 if( z
[0]=='-' ) z
++;
24210 if( cli_strcmp(z
,"preserve-rowids")==0 ){
24211 #ifdef SQLITE_OMIT_VIRTUALTABLE
24212 raw_printf(stderr
, "The --preserve-rowids option is not compatible"
24213 " with SQLITE_OMIT_VIRTUALTABLE\n");
24215 sqlite3_free(zLike
);
24216 goto meta_command_exit
;
24218 ShellSetFlag(p
, SHFLG_PreserveRowid
);
24221 if( cli_strcmp(z
,"newlines")==0 ){
24222 ShellSetFlag(p
, SHFLG_Newlines
);
24224 if( cli_strcmp(z
,"data-only")==0 ){
24225 ShellSetFlag(p
, SHFLG_DumpDataOnly
);
24227 if( cli_strcmp(z
,"nosys")==0 ){
24228 ShellSetFlag(p
, SHFLG_DumpNoSys
);
24231 raw_printf(stderr
, "Unknown option \"%s\" on \".dump\"\n", azArg
[i
]);
24233 sqlite3_free(zLike
);
24234 goto meta_command_exit
;
24237 /* azArg[i] contains a LIKE pattern. This ".dump" request should
24238 ** only dump data for tables for which either the table name matches
24239 ** the LIKE pattern, or the table appears to be a shadow table of
24240 ** a virtual table for which the name matches the LIKE pattern.
24242 char *zExpr
= sqlite3_mprintf(
24243 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
24244 " SELECT 1 FROM sqlite_schema WHERE "
24245 " name LIKE %Q ESCAPE '\\' AND"
24246 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
24247 " substr(o.name, 1, length(name)+1) == (name||'_')"
24248 ")", azArg
[i
], azArg
[i
]
24252 zLike
= sqlite3_mprintf("%z OR %z", zLike
, zExpr
);
24261 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
24262 /* When playing back a "dump", the content might appear in an order
24263 ** which causes immediate foreign key constraints to be violated.
24264 ** So disable foreign-key constraint enforcement to prevent problems. */
24265 raw_printf(p
->out
, "PRAGMA foreign_keys=OFF;\n");
24266 raw_printf(p
->out
, "BEGIN TRANSACTION;\n");
24268 p
->writableSchema
= 0;
24270 /* Set writable_schema=ON since doing so forces SQLite to initialize
24271 ** as much of the schema as it can even if the sqlite_schema table is
24273 sqlite3_exec(p
->db
, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
24275 if( zLike
==0 ) zLike
= sqlite3_mprintf("true");
24276 zSql
= sqlite3_mprintf(
24277 "SELECT name, type, sql FROM sqlite_schema AS o "
24278 "WHERE (%s) AND type=='table'"
24279 " AND sql NOT NULL"
24280 " ORDER BY tbl_name='sqlite_sequence', rowid",
24283 run_schema_dump_query(p
,zSql
);
24284 sqlite3_free(zSql
);
24285 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
24286 zSql
= sqlite3_mprintf(
24287 "SELECT sql FROM sqlite_schema AS o "
24288 "WHERE (%s) AND sql NOT NULL"
24289 " AND type IN ('index','trigger','view')",
24292 run_table_dump_query(p
, zSql
);
24293 sqlite3_free(zSql
);
24295 sqlite3_free(zLike
);
24296 if( p
->writableSchema
){
24297 raw_printf(p
->out
, "PRAGMA writable_schema=OFF;\n");
24298 p
->writableSchema
= 0;
24300 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
24301 sqlite3_exec(p
->db
, "RELEASE dump;", 0, 0, 0);
24302 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
24303 raw_printf(p
->out
, p
->nErr
?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
24305 p
->showHeader
= savedShowHeader
;
24306 p
->shellFlgs
= savedShellFlags
;
24309 if( c
=='e' && cli_strncmp(azArg
[0], "echo", n
)==0 ){
24311 setOrClearFlag(p
, SHFLG_Echo
, azArg
[1]);
24313 raw_printf(stderr
, "Usage: .echo on|off\n");
24318 if( c
=='e' && cli_strncmp(azArg
[0], "eqp", n
)==0 ){
24320 p
->autoEQPtest
= 0;
24321 if( p
->autoEQPtrace
){
24322 if( p
->db
) sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
24323 p
->autoEQPtrace
= 0;
24325 if( cli_strcmp(azArg
[1],"full")==0 ){
24326 p
->autoEQP
= AUTOEQP_full
;
24327 }else if( cli_strcmp(azArg
[1],"trigger")==0 ){
24328 p
->autoEQP
= AUTOEQP_trigger
;
24329 #ifdef SQLITE_DEBUG
24330 }else if( cli_strcmp(azArg
[1],"test")==0 ){
24331 p
->autoEQP
= AUTOEQP_on
;
24332 p
->autoEQPtest
= 1;
24333 }else if( cli_strcmp(azArg
[1],"trace")==0 ){
24334 p
->autoEQP
= AUTOEQP_full
;
24335 p
->autoEQPtrace
= 1;
24337 sqlite3_exec(p
->db
, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
24338 sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
24341 p
->autoEQP
= (u8
)booleanValue(azArg
[1]);
24344 raw_printf(stderr
, "Usage: .eqp off|on|trace|trigger|full\n");
24349 #ifndef SQLITE_SHELL_FIDDLE
24350 if( c
=='e' && cli_strncmp(azArg
[0], "exit", n
)==0 ){
24351 if( nArg
>1 && (rc
= (int)integerValue(azArg
[1]))!=0 ) exit(rc
);
24356 /* The ".explain" command is automatic now. It is largely pointless. It
24357 ** retained purely for backwards compatibility */
24358 if( c
=='e' && cli_strncmp(azArg
[0], "explain", n
)==0 ){
24361 if( cli_strcmp(azArg
[1],"auto")==0 ){
24364 val
= booleanValue(azArg
[1]);
24367 if( val
==1 && p
->mode
!=MODE_Explain
){
24368 p
->normalMode
= p
->mode
;
24369 p
->mode
= MODE_Explain
;
24370 p
->autoExplain
= 0;
24371 }else if( val
==0 ){
24372 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
24373 p
->autoExplain
= 0;
24374 }else if( val
==99 ){
24375 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
24376 p
->autoExplain
= 1;
24380 #ifndef SQLITE_OMIT_VIRTUALTABLE
24381 if( c
=='e' && cli_strncmp(azArg
[0], "expert", n
)==0 ){
24382 if( p
->bSafeMode
){
24384 "Cannot run experimental commands such as \"%s\" in safe mode\n",
24389 expertDotCommand(p
, azArg
, nArg
);
24394 if( c
=='f' && cli_strncmp(azArg
[0], "filectrl", n
)==0 ){
24395 static const struct {
24396 const char *zCtrlName
; /* Name of a test-control option */
24397 int ctrlCode
; /* Integer code for that option */
24398 const char *zUsage
; /* Usage notes */
24400 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE
, "SIZE" },
24401 { "data_version", SQLITE_FCNTL_DATA_VERSION
, "" },
24402 { "has_moved", SQLITE_FCNTL_HAS_MOVED
, "" },
24403 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT
, "MILLISEC" },
24404 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL
, "[BOOLEAN]" },
24405 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
24406 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE
, "[BOOLEAN]" },
24407 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES
, "[N]" },
24408 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT
, "[LIMIT]" },
24409 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME
, "" },
24410 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
24414 sqlite3_int64 iRes
= 0; /* Integer result to display if rc2==1 */
24415 int isOk
= 0; /* 0: usage 1: %lld 2: no-result */
24417 const char *zCmd
= 0;
24418 const char *zSchema
= 0;
24421 zCmd
= nArg
>=2 ? azArg
[1] : "help";
24424 && (cli_strcmp(zCmd
,"--schema")==0 || cli_strcmp(zCmd
,"-schema")==0)
24427 zSchema
= azArg
[2];
24428 for(i
=3; i
<nArg
; i
++) azArg
[i
-2] = azArg
[i
];
24433 /* The argument can optionally begin with "-" or "--" */
24434 if( zCmd
[0]=='-' && zCmd
[1] ){
24436 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
24439 /* --help lists all file-controls */
24440 if( cli_strcmp(zCmd
,"help")==0 ){
24441 utf8_printf(p
->out
, "Available file-controls:\n");
24442 for(i
=0; i
<ArraySize(aCtrl
); i
++){
24443 utf8_printf(p
->out
, " .filectrl %s %s\n",
24444 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
24447 goto meta_command_exit
;
24450 /* convert filectrl text option to value. allow any unique prefix
24451 ** of the option name, or a numerical value. */
24452 n2
= strlen30(zCmd
);
24453 for(i
=0; i
<ArraySize(aCtrl
); i
++){
24454 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
24456 filectrl
= aCtrl
[i
].ctrlCode
;
24459 utf8_printf(stderr
, "Error: ambiguous file-control: \"%s\"\n"
24460 "Use \".filectrl --help\" for help\n", zCmd
);
24462 goto meta_command_exit
;
24467 utf8_printf(stderr
,"Error: unknown file-control: %s\n"
24468 "Use \".filectrl --help\" for help\n", zCmd
);
24471 case SQLITE_FCNTL_SIZE_LIMIT
: {
24472 if( nArg
!=2 && nArg
!=3 ) break;
24473 iRes
= nArg
==3 ? integerValue(azArg
[2]) : -1;
24474 sqlite3_file_control(p
->db
, zSchema
, SQLITE_FCNTL_SIZE_LIMIT
, &iRes
);
24478 case SQLITE_FCNTL_LOCK_TIMEOUT
:
24479 case SQLITE_FCNTL_CHUNK_SIZE
: {
24481 if( nArg
!=3 ) break;
24482 x
= (int)integerValue(azArg
[2]);
24483 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
24487 case SQLITE_FCNTL_PERSIST_WAL
:
24488 case SQLITE_FCNTL_POWERSAFE_OVERWRITE
: {
24490 if( nArg
!=2 && nArg
!=3 ) break;
24491 x
= nArg
==3 ? booleanValue(azArg
[2]) : -1;
24492 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
24497 case SQLITE_FCNTL_DATA_VERSION
:
24498 case SQLITE_FCNTL_HAS_MOVED
: {
24500 if( nArg
!=2 ) break;
24501 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
24506 case SQLITE_FCNTL_TEMPFILENAME
: {
24508 if( nArg
!=2 ) break;
24509 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &z
);
24511 utf8_printf(p
->out
, "%s\n", z
);
24517 case SQLITE_FCNTL_RESERVE_BYTES
: {
24520 x
= atoi(azArg
[2]);
24521 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
24524 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
24525 utf8_printf(p
->out
,"%d\n", x
);
24531 if( isOk
==0 && iCtrl
>=0 ){
24532 utf8_printf(p
->out
, "Usage: .filectrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
24534 }else if( isOk
==1 ){
24536 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%lld", iRes
);
24537 raw_printf(p
->out
, "%s\n", zBuf
);
24541 if( c
=='f' && cli_strncmp(azArg
[0], "fullschema", n
)==0 ){
24544 memcpy(&data
, p
, sizeof(data
));
24545 data
.showHeader
= 0;
24546 data
.cMode
= data
.mode
= MODE_Semi
;
24547 if( nArg
==2 && optionMatch(azArg
[1], "indent") ){
24548 data
.cMode
= data
.mode
= MODE_Pretty
;
24552 raw_printf(stderr
, "Usage: .fullschema ?--indent?\n");
24554 goto meta_command_exit
;
24557 rc
= sqlite3_exec(p
->db
,
24559 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
24560 " FROM sqlite_schema UNION ALL"
24561 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
24562 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
24566 if( rc
==SQLITE_OK
){
24567 sqlite3_stmt
*pStmt
;
24568 rc
= sqlite3_prepare_v2(p
->db
,
24569 "SELECT rowid FROM sqlite_schema"
24570 " WHERE name GLOB 'sqlite_stat[134]'",
24572 doStats
= sqlite3_step(pStmt
)==SQLITE_ROW
;
24573 sqlite3_finalize(pStmt
);
24576 raw_printf(p
->out
, "/* No STAT tables available */\n");
24578 raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
24579 data
.cMode
= data
.mode
= MODE_Insert
;
24580 data
.zDestTable
= "sqlite_stat1";
24581 shell_exec(&data
, "SELECT * FROM sqlite_stat1", 0);
24582 data
.zDestTable
= "sqlite_stat4";
24583 shell_exec(&data
, "SELECT * FROM sqlite_stat4", 0);
24584 raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
24588 if( c
=='h' && cli_strncmp(azArg
[0], "headers", n
)==0 ){
24590 p
->showHeader
= booleanValue(azArg
[1]);
24591 p
->shellFlgs
|= SHFLG_HeaderSet
;
24593 raw_printf(stderr
, "Usage: .headers on|off\n");
24598 if( c
=='h' && cli_strncmp(azArg
[0], "help", n
)==0 ){
24600 n
= showHelp(p
->out
, azArg
[1]);
24602 utf8_printf(p
->out
, "Nothing matches '%s'\n", azArg
[1]);
24605 showHelp(p
->out
, 0);
24609 #ifndef SQLITE_SHELL_FIDDLE
24610 if( c
=='i' && cli_strncmp(azArg
[0], "import", n
)==0 ){
24611 char *zTable
= 0; /* Insert data into this table */
24612 char *zSchema
= 0; /* within this schema (may default to "main") */
24613 char *zFile
= 0; /* Name of file to extra content from */
24614 sqlite3_stmt
*pStmt
= NULL
; /* A statement */
24615 int nCol
; /* Number of columns in the table */
24616 int nByte
; /* Number of bytes in an SQL string */
24617 int i
, j
; /* Loop counters */
24618 int needCommit
; /* True to COMMIT or ROLLBACK at end */
24619 int nSep
; /* Number of bytes in p->colSeparator[] */
24620 char *zSql
; /* An SQL statement */
24621 char *zFullTabName
; /* Table name with schema if applicable */
24622 ImportCtx sCtx
; /* Reader context */
24623 char *(SQLITE_CDECL
*xRead
)(ImportCtx
*); /* Func to read one value */
24624 int eVerbose
= 0; /* Larger for more console output */
24625 int nSkip
= 0; /* Initial lines to skip */
24626 int useOutputMode
= 1; /* Use output mode to determine separators */
24627 char *zCreate
= 0; /* CREATE TABLE statement text */
24629 failIfSafeMode(p
, "cannot run .import in safe mode");
24630 memset(&sCtx
, 0, sizeof(sCtx
));
24631 if( p
->mode
==MODE_Ascii
){
24632 xRead
= ascii_read_one_field
;
24634 xRead
= csv_read_one_field
;
24637 for(i
=1; i
<nArg
; i
++){
24638 char *z
= azArg
[i
];
24639 if( z
[0]=='-' && z
[1]=='-' ) z
++;
24643 }else if( zTable
==0 ){
24646 utf8_printf(p
->out
, "ERROR: extra argument: \"%s\". Usage:\n", z
);
24647 showHelp(p
->out
, "import");
24648 goto meta_command_exit
;
24650 }else if( cli_strcmp(z
,"-v")==0 ){
24652 }else if( cli_strcmp(z
,"-schema")==0 && i
<nArg
-1 ){
24653 zSchema
= azArg
[++i
];
24654 }else if( cli_strcmp(z
,"-skip")==0 && i
<nArg
-1 ){
24655 nSkip
= integerValue(azArg
[++i
]);
24656 }else if( cli_strcmp(z
,"-ascii")==0 ){
24657 sCtx
.cColSep
= SEP_Unit
[0];
24658 sCtx
.cRowSep
= SEP_Record
[0];
24659 xRead
= ascii_read_one_field
;
24661 }else if( cli_strcmp(z
,"-csv")==0 ){
24662 sCtx
.cColSep
= ',';
24663 sCtx
.cRowSep
= '\n';
24664 xRead
= csv_read_one_field
;
24667 utf8_printf(p
->out
, "ERROR: unknown option: \"%s\". Usage:\n", z
);
24668 showHelp(p
->out
, "import");
24669 goto meta_command_exit
;
24673 utf8_printf(p
->out
, "ERROR: missing %s argument. Usage:\n",
24674 zFile
==0 ? "FILE" : "TABLE");
24675 showHelp(p
->out
, "import");
24676 goto meta_command_exit
;
24680 if( useOutputMode
){
24681 /* If neither the --csv or --ascii options are specified, then set
24682 ** the column and row separator characters from the output mode. */
24683 nSep
= strlen30(p
->colSeparator
);
24686 "Error: non-null column separator required for import\n");
24687 goto meta_command_exit
;
24691 "Error: multi-character column separators not allowed"
24693 goto meta_command_exit
;
24695 nSep
= strlen30(p
->rowSeparator
);
24698 "Error: non-null row separator required for import\n");
24699 goto meta_command_exit
;
24701 if( nSep
==2 && p
->mode
==MODE_Csv
24702 && cli_strcmp(p
->rowSeparator
,SEP_CrLf
)==0
24704 /* When importing CSV (only), if the row separator is set to the
24705 ** default output row separator, change it to the default input
24706 ** row separator. This avoids having to maintain different input
24707 ** and output row separators. */
24708 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
24709 nSep
= strlen30(p
->rowSeparator
);
24712 raw_printf(stderr
, "Error: multi-character row separators not allowed"
24714 goto meta_command_exit
;
24716 sCtx
.cColSep
= (u8
)p
->colSeparator
[0];
24717 sCtx
.cRowSep
= (u8
)p
->rowSeparator
[0];
24719 sCtx
.zFile
= zFile
;
24721 if( sCtx
.zFile
[0]=='|' ){
24722 #ifdef SQLITE_OMIT_POPEN
24723 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
24724 goto meta_command_exit
;
24726 sCtx
.in
= popen(sCtx
.zFile
+1, "r");
24727 sCtx
.zFile
= "<pipe>";
24728 sCtx
.xCloser
= pclose
;
24731 sCtx
.in
= fopen(sCtx
.zFile
, "rb");
24732 sCtx
.xCloser
= fclose
;
24735 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
24736 goto meta_command_exit
;
24738 if( eVerbose
>=2 || (eVerbose
>=1 && useOutputMode
) ){
24741 zSep
[0] = sCtx
.cColSep
;
24742 utf8_printf(p
->out
, "Column separator ");
24743 output_c_string(p
->out
, zSep
);
24744 utf8_printf(p
->out
, ", row separator ");
24745 zSep
[0] = sCtx
.cRowSep
;
24746 output_c_string(p
->out
, zSep
);
24747 utf8_printf(p
->out
, "\n");
24749 sCtx
.z
= sqlite3_malloc64(120);
24751 import_cleanup(&sCtx
);
24752 shell_out_of_memory();
24754 /* Below, resources must be freed before exit. */
24755 while( (nSkip
--)>0 ){
24756 while( xRead(&sCtx
) && sCtx
.cTerm
==sCtx
.cColSep
){}
24759 zFullTabName
= sqlite3_mprintf("\"%w\".\"%w\"", zSchema
, zTable
);
24761 zFullTabName
= sqlite3_mprintf("\"%w\"", zTable
);
24763 zSql
= sqlite3_mprintf("SELECT * FROM %s", zFullTabName
);
24764 if( zSql
==0 || zFullTabName
==0 ){
24765 import_cleanup(&sCtx
);
24766 shell_out_of_memory();
24768 nByte
= strlen30(zSql
);
24769 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
24770 import_append_char(&sCtx
, 0); /* To ensure sCtx.z is allocated */
24771 if( rc
&& sqlite3_strglob("no such table: *", sqlite3_errmsg(p
->db
))==0 ){
24772 sqlite3
*dbCols
= 0;
24773 char *zRenames
= 0;
24775 zCreate
= sqlite3_mprintf("CREATE TABLE %s", zFullTabName
);
24776 while( xRead(&sCtx
) ){
24777 zAutoColumn(sCtx
.z
, &dbCols
, 0);
24778 if( sCtx
.cTerm
!=sCtx
.cColSep
) break;
24780 zColDefs
= zAutoColumn(0, &dbCols
, &zRenames
);
24782 utf8_printf((stdin_is_interactive
&& p
->in
==stdin
)? p
->out
: stderr
,
24783 "Columns renamed during .import %s due to duplicates:\n"
24784 "%s\n", sCtx
.zFile
, zRenames
);
24785 sqlite3_free(zRenames
);
24789 utf8_printf(stderr
,"%s: empty file\n", sCtx
.zFile
);
24791 sqlite3_free(zCreate
);
24792 sqlite3_free(zSql
);
24793 sqlite3_free(zFullTabName
);
24794 import_cleanup(&sCtx
);
24796 goto meta_command_exit
;
24798 zCreate
= sqlite3_mprintf("%z%z\n", zCreate
, zColDefs
);
24800 utf8_printf(p
->out
, "%s\n", zCreate
);
24802 rc
= sqlite3_exec(p
->db
, zCreate
, 0, 0, 0);
24804 utf8_printf(stderr
, "%s failed:\n%s\n", zCreate
, sqlite3_errmsg(p
->db
));
24807 sqlite3_free(zCreate
);
24809 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
24812 if (pStmt
) sqlite3_finalize(pStmt
);
24813 utf8_printf(stderr
,"Error: %s\n", sqlite3_errmsg(p
->db
));
24816 sqlite3_free(zSql
);
24817 nCol
= sqlite3_column_count(pStmt
);
24818 sqlite3_finalize(pStmt
);
24820 if( nCol
==0 ) return 0; /* no columns, no error */
24821 zSql
= sqlite3_malloc64( nByte
*2 + 20 + nCol
*2 );
24823 import_cleanup(&sCtx
);
24824 shell_out_of_memory();
24826 sqlite3_snprintf(nByte
+20, zSql
, "INSERT INTO %s VALUES(?", zFullTabName
);
24827 j
= strlen30(zSql
);
24828 for(i
=1; i
<nCol
; i
++){
24835 utf8_printf(p
->out
, "Insert using: %s\n", zSql
);
24837 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
24839 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
24840 if (pStmt
) sqlite3_finalize(pStmt
);
24843 sqlite3_free(zSql
);
24844 sqlite3_free(zFullTabName
);
24845 needCommit
= sqlite3_get_autocommit(p
->db
);
24846 if( needCommit
) sqlite3_exec(p
->db
, "BEGIN", 0, 0, 0);
24848 int startLine
= sCtx
.nLine
;
24849 for(i
=0; i
<nCol
; i
++){
24850 char *z
= xRead(&sCtx
);
24852 ** Did we reach end-of-file before finding any columns?
24853 ** If so, stop instead of NULL filling the remaining columns.
24855 if( z
==0 && i
==0 ) break;
24857 ** Did we reach end-of-file OR end-of-line before finding any
24858 ** columns in ASCII mode? If so, stop instead of NULL filling
24859 ** the remaining columns.
24861 if( p
->mode
==MODE_Ascii
&& (z
==0 || z
[0]==0) && i
==0 ) break;
24862 sqlite3_bind_text(pStmt
, i
+1, z
, -1, SQLITE_TRANSIENT
);
24863 if( i
<nCol
-1 && sCtx
.cTerm
!=sCtx
.cColSep
){
24864 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
24865 "filling the rest with NULL\n",
24866 sCtx
.zFile
, startLine
, nCol
, i
+1);
24868 while( i
<=nCol
){ sqlite3_bind_null(pStmt
, i
); i
++; }
24871 if( sCtx
.cTerm
==sCtx
.cColSep
){
24875 }while( sCtx
.cTerm
==sCtx
.cColSep
);
24876 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
24877 "extras ignored\n",
24878 sCtx
.zFile
, startLine
, nCol
, i
);
24881 sqlite3_step(pStmt
);
24882 rc
= sqlite3_reset(pStmt
);
24883 if( rc
!=SQLITE_OK
){
24884 utf8_printf(stderr
, "%s:%d: INSERT failed: %s\n", sCtx
.zFile
,
24885 startLine
, sqlite3_errmsg(p
->db
));
24891 }while( sCtx
.cTerm
!=EOF
);
24893 import_cleanup(&sCtx
);
24894 sqlite3_finalize(pStmt
);
24895 if( needCommit
) sqlite3_exec(p
->db
, "COMMIT", 0, 0, 0);
24897 utf8_printf(p
->out
,
24898 "Added %d rows with %d errors using %d lines of input\n",
24899 sCtx
.nRow
, sCtx
.nErr
, sCtx
.nLine
-1);
24902 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24904 #ifndef SQLITE_UNTESTABLE
24905 if( c
=='i' && cli_strncmp(azArg
[0], "imposter", n
)==0 ){
24907 char *zCollist
= 0;
24908 sqlite3_stmt
*pStmt
;
24910 int isWO
= 0; /* True if making an imposter of a WITHOUT ROWID table */
24911 int lenPK
= 0; /* Length of the PRIMARY KEY string for isWO tables */
24913 if( !ShellHasFlag(p
,SHFLG_TestingMode
) ){
24914 utf8_printf(stderr
, ".%s unavailable without --unsafe-testing\n",
24917 goto meta_command_exit
;
24919 if( !(nArg
==3 || (nArg
==2 && sqlite3_stricmp(azArg
[1],"off")==0)) ){
24920 utf8_printf(stderr
, "Usage: .imposter INDEX IMPOSTER\n"
24921 " .imposter off\n");
24922 /* Also allowed, but not documented:
24924 ** .imposter TABLE IMPOSTER
24926 ** where TABLE is a WITHOUT ROWID table. In that case, the
24927 ** imposter is another WITHOUT ROWID table with the columns in
24928 ** storage order. */
24930 goto meta_command_exit
;
24934 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 1);
24935 goto meta_command_exit
;
24937 zSql
= sqlite3_mprintf(
24938 "SELECT rootpage, 0 FROM sqlite_schema"
24939 " WHERE name='%q' AND type='index'"
24941 "SELECT rootpage, 1 FROM sqlite_schema"
24942 " WHERE name='%q' AND type='table'"
24943 " AND sql LIKE '%%without%%rowid%%'",
24946 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
24947 sqlite3_free(zSql
);
24948 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
24949 tnum
= sqlite3_column_int(pStmt
, 0);
24950 isWO
= sqlite3_column_int(pStmt
, 1);
24952 sqlite3_finalize(pStmt
);
24953 zSql
= sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg
[1]);
24954 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
24955 sqlite3_free(zSql
);
24957 while( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
24959 const char *zCol
= (const char*)sqlite3_column_text(pStmt
,2);
24962 if( sqlite3_column_int(pStmt
,1)==-1 ){
24965 sqlite3_snprintf(sizeof(zLabel
),zLabel
,"expr%d",i
);
24969 if( isWO
&& lenPK
==0 && sqlite3_column_int(pStmt
,5)==0 && zCollist
){
24970 lenPK
= (int)strlen(zCollist
);
24973 zCollist
= sqlite3_mprintf("\"%w\"", zCol
);
24975 zCollist
= sqlite3_mprintf("%z,\"%w\"", zCollist
, zCol
);
24978 sqlite3_finalize(pStmt
);
24979 if( i
==0 || tnum
==0 ){
24980 utf8_printf(stderr
, "no such index: \"%s\"\n", azArg
[1]);
24982 sqlite3_free(zCollist
);
24983 goto meta_command_exit
;
24985 if( lenPK
==0 ) lenPK
= 100000;
24986 zSql
= sqlite3_mprintf(
24987 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
24988 azArg
[2], zCollist
, lenPK
, zCollist
);
24989 sqlite3_free(zCollist
);
24990 rc
= sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 1, tnum
);
24991 if( rc
==SQLITE_OK
){
24992 rc
= sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
24993 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 0);
24995 utf8_printf(stderr
, "Error in [%s]: %s\n", zSql
, sqlite3_errmsg(p
->db
));
24997 utf8_printf(stdout
, "%s;\n", zSql
);
24999 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
25000 azArg
[1], isWO
? "table" : "index"
25004 raw_printf(stderr
, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc
);
25007 sqlite3_free(zSql
);
25009 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
25011 #ifdef SQLITE_ENABLE_IOTRACE
25012 if( c
=='i' && cli_strncmp(azArg
[0], "iotrace", n
)==0 ){
25013 SQLITE_API
extern void (SQLITE_CDECL
*sqlite3IoTrace
)(const char*, ...);
25014 if( iotrace
&& iotrace
!=stdout
) fclose(iotrace
);
25017 sqlite3IoTrace
= 0;
25018 }else if( cli_strcmp(azArg
[1], "-")==0 ){
25019 sqlite3IoTrace
= iotracePrintf
;
25022 iotrace
= fopen(azArg
[1], "w");
25024 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", azArg
[1]);
25025 sqlite3IoTrace
= 0;
25028 sqlite3IoTrace
= iotracePrintf
;
25034 if( c
=='l' && n
>=5 && cli_strncmp(azArg
[0], "limits", n
)==0 ){
25035 static const struct {
25036 const char *zLimitName
; /* Name of a limit */
25037 int limitCode
; /* Integer code for that limit */
25039 { "length", SQLITE_LIMIT_LENGTH
},
25040 { "sql_length", SQLITE_LIMIT_SQL_LENGTH
},
25041 { "column", SQLITE_LIMIT_COLUMN
},
25042 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH
},
25043 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT
},
25044 { "vdbe_op", SQLITE_LIMIT_VDBE_OP
},
25045 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG
},
25046 { "attached", SQLITE_LIMIT_ATTACHED
},
25047 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH
},
25048 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER
},
25049 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH
},
25050 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS
},
25055 for(i
=0; i
<ArraySize(aLimit
); i
++){
25056 printf("%20s %d\n", aLimit
[i
].zLimitName
,
25057 sqlite3_limit(p
->db
, aLimit
[i
].limitCode
, -1));
25059 }else if( nArg
>3 ){
25060 raw_printf(stderr
, "Usage: .limit NAME ?NEW-VALUE?\n");
25062 goto meta_command_exit
;
25065 n2
= strlen30(azArg
[1]);
25066 for(i
=0; i
<ArraySize(aLimit
); i
++){
25067 if( sqlite3_strnicmp(aLimit
[i
].zLimitName
, azArg
[1], n2
)==0 ){
25071 utf8_printf(stderr
, "ambiguous limit: \"%s\"\n", azArg
[1]);
25073 goto meta_command_exit
;
25078 utf8_printf(stderr
, "unknown limit: \"%s\"\n"
25079 "enter \".limits\" with no arguments for a list.\n",
25082 goto meta_command_exit
;
25085 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
,
25086 (int)integerValue(azArg
[2]));
25088 printf("%20s %d\n", aLimit
[iLimit
].zLimitName
,
25089 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
, -1));
25093 if( c
=='l' && n
>2 && cli_strncmp(azArg
[0], "lint", n
)==0 ){
25095 lintDotCommand(p
, azArg
, nArg
);
25098 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
25099 if( c
=='l' && cli_strncmp(azArg
[0], "load", n
)==0 ){
25100 const char *zFile
, *zProc
;
25102 failIfSafeMode(p
, "cannot run .load in safe mode");
25103 if( nArg
<2 || azArg
[1][0]==0 ){
25104 /* Must have a non-empty FILE. (Will not load self.) */
25105 raw_printf(stderr
, "Usage: .load FILE ?ENTRYPOINT?\n");
25107 goto meta_command_exit
;
25110 zProc
= nArg
>=3 ? azArg
[2] : 0;
25112 rc
= sqlite3_load_extension(p
->db
, zFile
, zProc
, &zErrMsg
);
25113 if( rc
!=SQLITE_OK
){
25114 utf8_printf(stderr
, "Error: %s\n", zErrMsg
);
25115 sqlite3_free(zErrMsg
);
25121 if( c
=='l' && cli_strncmp(azArg
[0], "log", n
)==0 ){
25123 raw_printf(stderr
, "Usage: .log FILENAME\n");
25126 const char *zFile
= azArg
[1];
25128 && cli_strcmp(zFile
,"on")!=0
25129 && cli_strcmp(zFile
,"off")!=0
25131 raw_printf(stdout
, "cannot set .log to anything other "
25132 "than \"on\" or \"off\"\n");
25135 output_file_close(p
->pLog
);
25136 if( cli_strcmp(zFile
,"on")==0 ) zFile
= "stdout";
25137 p
->pLog
= output_file_open(zFile
, 0);
25141 if( c
=='m' && cli_strncmp(azArg
[0], "mode", n
)==0 ){
25142 const char *zMode
= 0;
25143 const char *zTabname
= 0;
25145 ColModeOpts cmOpts
= ColModeOpts_default
;
25146 for(i
=1; i
<nArg
; i
++){
25147 const char *z
= azArg
[i
];
25148 if( optionMatch(z
,"wrap") && i
+1<nArg
){
25149 cmOpts
.iWrap
= integerValue(azArg
[++i
]);
25150 }else if( optionMatch(z
,"ww") ){
25151 cmOpts
.bWordWrap
= 1;
25152 }else if( optionMatch(z
,"wordwrap") && i
+1<nArg
){
25153 cmOpts
.bWordWrap
= (u8
)booleanValue(azArg
[++i
]);
25154 }else if( optionMatch(z
,"quote") ){
25156 }else if( optionMatch(z
,"noquote") ){
25158 }else if( zMode
==0 ){
25160 /* Apply defaults for qbox pseudo-mode. If that
25161 * overwrites already-set values, user was informed of this.
25163 if( cli_strcmp(z
, "qbox")==0 ){
25164 ColModeOpts cmo
= ColModeOpts_default_qbox
;
25168 }else if( zTabname
==0 ){
25170 }else if( z
[0]=='-' ){
25171 utf8_printf(stderr
, "unknown option: %s\n", z
);
25172 utf8_printf(stderr
, "options:\n"
25175 " --wordwrap on/off\n"
25179 goto meta_command_exit
;
25181 utf8_printf(stderr
, "extra argument: \"%s\"\n", z
);
25183 goto meta_command_exit
;
25187 if( p
->mode
==MODE_Column
25188 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
25192 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
25193 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
25194 p
->cmOpts
.bWordWrap
? "on" : "off",
25195 p
->cmOpts
.bQuote
? "" : "no");
25197 raw_printf(p
->out
, "current output mode: %s\n", modeDescr
[p
->mode
]);
25199 zMode
= modeDescr
[p
->mode
];
25201 n2
= strlen30(zMode
);
25202 if( cli_strncmp(zMode
,"lines",n2
)==0 ){
25203 p
->mode
= MODE_Line
;
25204 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
25205 }else if( cli_strncmp(zMode
,"columns",n2
)==0 ){
25206 p
->mode
= MODE_Column
;
25207 if( (p
->shellFlgs
& SHFLG_HeaderSet
)==0 ){
25210 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
25211 p
->cmOpts
= cmOpts
;
25212 }else if( cli_strncmp(zMode
,"list",n2
)==0 ){
25213 p
->mode
= MODE_List
;
25214 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Column
);
25215 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
25216 }else if( cli_strncmp(zMode
,"html",n2
)==0 ){
25217 p
->mode
= MODE_Html
;
25218 }else if( cli_strncmp(zMode
,"tcl",n2
)==0 ){
25219 p
->mode
= MODE_Tcl
;
25220 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Space
);
25221 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
25222 }else if( cli_strncmp(zMode
,"csv",n2
)==0 ){
25223 p
->mode
= MODE_Csv
;
25224 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
25225 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
25226 }else if( cli_strncmp(zMode
,"tabs",n2
)==0 ){
25227 p
->mode
= MODE_List
;
25228 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Tab
);
25229 }else if( cli_strncmp(zMode
,"insert",n2
)==0 ){
25230 p
->mode
= MODE_Insert
;
25231 set_table_name(p
, zTabname
? zTabname
: "table");
25232 }else if( cli_strncmp(zMode
,"quote",n2
)==0 ){
25233 p
->mode
= MODE_Quote
;
25234 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
25235 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
25236 }else if( cli_strncmp(zMode
,"ascii",n2
)==0 ){
25237 p
->mode
= MODE_Ascii
;
25238 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Unit
);
25239 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Record
);
25240 }else if( cli_strncmp(zMode
,"markdown",n2
)==0 ){
25241 p
->mode
= MODE_Markdown
;
25242 p
->cmOpts
= cmOpts
;
25243 }else if( cli_strncmp(zMode
,"table",n2
)==0 ){
25244 p
->mode
= MODE_Table
;
25245 p
->cmOpts
= cmOpts
;
25246 }else if( cli_strncmp(zMode
,"box",n2
)==0 ){
25247 p
->mode
= MODE_Box
;
25248 p
->cmOpts
= cmOpts
;
25249 }else if( cli_strncmp(zMode
,"count",n2
)==0 ){
25250 p
->mode
= MODE_Count
;
25251 }else if( cli_strncmp(zMode
,"off",n2
)==0 ){
25252 p
->mode
= MODE_Off
;
25253 }else if( cli_strncmp(zMode
,"json",n2
)==0 ){
25254 p
->mode
= MODE_Json
;
25256 raw_printf(stderr
, "Error: mode should be one of: "
25257 "ascii box column csv html insert json line list markdown "
25258 "qbox quote table tabs tcl\n");
25261 p
->cMode
= p
->mode
;
25264 #ifndef SQLITE_SHELL_FIDDLE
25265 if( c
=='n' && cli_strcmp(azArg
[0], "nonce")==0 ){
25267 raw_printf(stderr
, "Usage: .nonce NONCE\n");
25269 }else if( p
->zNonce
==0 || cli_strcmp(azArg
[1],p
->zNonce
)!=0 ){
25270 raw_printf(stderr
, "line %d: incorrect nonce: \"%s\"\n",
25271 p
->lineno
, azArg
[1]);
25275 return 0; /* Return immediately to bypass the safe mode reset
25276 ** at the end of this procedure */
25279 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25281 if( c
=='n' && cli_strncmp(azArg
[0], "nullvalue", n
)==0 ){
25283 sqlite3_snprintf(sizeof(p
->nullValue
), p
->nullValue
,
25284 "%.*s", (int)ArraySize(p
->nullValue
)-1, azArg
[1]);
25286 raw_printf(stderr
, "Usage: .nullvalue STRING\n");
25291 if( c
=='o' && cli_strncmp(azArg
[0], "open", n
)==0 && n
>=2 ){
25292 const char *zFN
= 0; /* Pointer to constant filename */
25293 char *zNewFilename
= 0; /* Name of the database file to open */
25294 int iName
= 1; /* Index in azArg[] of the filename */
25295 int newFlag
= 0; /* True to delete file before opening */
25296 int openMode
= SHELL_OPEN_UNSPEC
;
25298 /* Check for command-line arguments */
25299 for(iName
=1; iName
<nArg
; iName
++){
25300 const char *z
= azArg
[iName
];
25301 #ifndef SQLITE_SHELL_FIDDLE
25302 if( optionMatch(z
,"new") ){
25304 #ifdef SQLITE_HAVE_ZLIB
25305 }else if( optionMatch(z
, "zip") ){
25306 openMode
= SHELL_OPEN_ZIPFILE
;
25308 }else if( optionMatch(z
, "append") ){
25309 openMode
= SHELL_OPEN_APPENDVFS
;
25310 }else if( optionMatch(z
, "readonly") ){
25311 openMode
= SHELL_OPEN_READONLY
;
25312 }else if( optionMatch(z
, "nofollow") ){
25313 p
->openFlags
|= SQLITE_OPEN_NOFOLLOW
;
25314 #ifndef SQLITE_OMIT_DESERIALIZE
25315 }else if( optionMatch(z
, "deserialize") ){
25316 openMode
= SHELL_OPEN_DESERIALIZE
;
25317 }else if( optionMatch(z
, "hexdb") ){
25318 openMode
= SHELL_OPEN_HEXDB
;
25319 }else if( optionMatch(z
, "maxsize") && iName
+1<nArg
){
25320 p
->szMax
= integerValue(azArg
[++iName
]);
25321 #endif /* SQLITE_OMIT_DESERIALIZE */
25323 #endif /* !SQLITE_SHELL_FIDDLE */
25325 utf8_printf(stderr
, "unknown option: %s\n", z
);
25327 goto meta_command_exit
;
25329 utf8_printf(stderr
, "extra argument: \"%s\"\n", z
);
25331 goto meta_command_exit
;
25337 /* Close the existing database */
25338 session_close_all(p
, -1);
25341 p
->pAuxDb
->zDbFilename
= 0;
25342 sqlite3_free(p
->pAuxDb
->zFreeOnClose
);
25343 p
->pAuxDb
->zFreeOnClose
= 0;
25344 p
->openMode
= openMode
;
25348 /* If a filename is specified, try to open it first */
25349 if( zFN
|| p
->openMode
==SHELL_OPEN_HEXDB
){
25350 if( newFlag
&& zFN
&& !p
->bSafeMode
) shellDeleteFile(zFN
);
25351 #ifndef SQLITE_SHELL_FIDDLE
25353 && p
->openMode
!=SHELL_OPEN_HEXDB
25355 && cli_strcmp(zFN
,":memory:")!=0
25357 failIfSafeMode(p
, "cannot open disk-based database files in safe mode");
25360 /* WASM mode has its own sandboxed pseudo-filesystem. */
25363 zNewFilename
= sqlite3_mprintf("%s", zFN
);
25364 shell_check_oom(zNewFilename
);
25368 p
->pAuxDb
->zDbFilename
= zNewFilename
;
25369 open_db(p
, OPEN_DB_KEEPALIVE
);
25371 utf8_printf(stderr
, "Error: cannot open '%s'\n", zNewFilename
);
25372 sqlite3_free(zNewFilename
);
25374 p
->pAuxDb
->zFreeOnClose
= zNewFilename
;
25378 /* As a fall-back open a TEMP database */
25379 p
->pAuxDb
->zDbFilename
= 0;
25384 #ifndef SQLITE_SHELL_FIDDLE
25386 && (cli_strncmp(azArg
[0], "output", n
)==0
25387 || cli_strncmp(azArg
[0], "once", n
)==0))
25388 || (c
=='e' && n
==5 && cli_strcmp(azArg
[0],"excel")==0)
25394 int bOnce
= 0; /* 0: .output, 1: .once, 2: .excel */
25395 unsigned char zBOM
[4]; /* Byte-order mark to using if --bom is present */
25398 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
25402 }else if( cli_strncmp(azArg
[0],"once",n
)==0 ){
25405 for(i
=1; i
<nArg
; i
++){
25406 char *z
= azArg
[i
];
25408 if( z
[1]=='-' ) z
++;
25409 if( cli_strcmp(z
,"-bom")==0 ){
25414 }else if( c
!='e' && cli_strcmp(z
,"-x")==0 ){
25415 eMode
= 'x'; /* spreadsheet */
25416 }else if( c
!='e' && cli_strcmp(z
,"-e")==0 ){
25417 eMode
= 'e'; /* text editor */
25419 utf8_printf(p
->out
, "ERROR: unknown option: \"%s\". Usage:\n",
25421 showHelp(p
->out
, azArg
[0]);
25423 goto meta_command_exit
;
25425 }else if( zFile
==0 && eMode
!='e' && eMode
!='x' ){
25426 zFile
= sqlite3_mprintf("%s", z
);
25427 if( zFile
&& zFile
[0]=='|' ){
25428 while( i
+1<nArg
) zFile
= sqlite3_mprintf("%z %s", zFile
, azArg
[++i
]);
25432 utf8_printf(p
->out
,"ERROR: extra parameter: \"%s\". Usage:\n",
25434 showHelp(p
->out
, azArg
[0]);
25436 sqlite3_free(zFile
);
25437 goto meta_command_exit
;
25441 zFile
= sqlite3_mprintf("stdout");
25449 #ifndef SQLITE_NOHAVE_SYSTEM
25450 if( eMode
=='e' || eMode
=='x' ){
25454 /* spreadsheet mode. Output as CSV. */
25455 newTempFile(p
, "csv");
25456 ShellClearFlag(p
, SHFLG_Echo
);
25457 p
->mode
= MODE_Csv
;
25458 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
25459 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
25461 /* text editor mode */
25462 newTempFile(p
, "txt");
25465 sqlite3_free(zFile
);
25466 zFile
= sqlite3_mprintf("%s", p
->zTempFile
);
25468 #endif /* SQLITE_NOHAVE_SYSTEM */
25469 shell_check_oom(zFile
);
25470 if( zFile
[0]=='|' ){
25471 #ifdef SQLITE_OMIT_POPEN
25472 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
25476 p
->out
= popen(zFile
+ 1, "w");
25478 utf8_printf(stderr
,"Error: cannot open pipe \"%s\"\n", zFile
+ 1);
25482 if( zBOM
[0] ) fwrite(zBOM
, 1, 3, p
->out
);
25483 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
25487 p
->out
= output_file_open(zFile
, bTxtMode
);
25489 if( cli_strcmp(zFile
,"off")!=0 ){
25490 utf8_printf(stderr
,"Error: cannot write to \"%s\"\n", zFile
);
25495 if( zBOM
[0] ) fwrite(zBOM
, 1, 3, p
->out
);
25496 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
25499 sqlite3_free(zFile
);
25501 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25503 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "parameter", n
)==0 ){
25505 if( nArg
<=1 ) goto parameter_syntax_error
;
25507 /* .parameter clear
25508 ** Clear all bind parameters by dropping the TEMP table that holds them.
25510 if( nArg
==2 && cli_strcmp(azArg
[1],"clear")==0 ){
25511 sqlite3_exec(p
->db
, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
25516 ** List all bind parameters.
25518 if( nArg
==2 && cli_strcmp(azArg
[1],"list")==0 ){
25519 sqlite3_stmt
*pStmt
= 0;
25522 rx
= sqlite3_prepare_v2(p
->db
,
25523 "SELECT max(length(key)) "
25524 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
25525 if( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
25526 len
= sqlite3_column_int(pStmt
, 0);
25527 if( len
>40 ) len
= 40;
25529 sqlite3_finalize(pStmt
);
25532 rx
= sqlite3_prepare_v2(p
->db
,
25533 "SELECT key, quote(value) "
25534 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
25535 while( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
25536 utf8_printf(p
->out
, "%-*s %s\n", len
, sqlite3_column_text(pStmt
,0),
25537 sqlite3_column_text(pStmt
,1));
25539 sqlite3_finalize(pStmt
);
25544 ** Make sure the TEMP table used to hold bind parameters exists.
25545 ** Create it if necessary.
25547 if( nArg
==2 && cli_strcmp(azArg
[1],"init")==0 ){
25548 bind_table_init(p
);
25551 /* .parameter set NAME VALUE
25552 ** Set or reset a bind parameter. NAME should be the full parameter
25553 ** name exactly as it appears in the query. (ex: $abc, @def). The
25554 ** VALUE can be in either SQL literal notation, or if not it will be
25555 ** understood to be a text string.
25557 if( nArg
==4 && cli_strcmp(azArg
[1],"set")==0 ){
25560 sqlite3_stmt
*pStmt
;
25561 const char *zKey
= azArg
[2];
25562 const char *zValue
= azArg
[3];
25563 bind_table_init(p
);
25564 zSql
= sqlite3_mprintf(
25565 "REPLACE INTO temp.sqlite_parameters(key,value)"
25566 "VALUES(%Q,%s);", zKey
, zValue
);
25567 shell_check_oom(zSql
);
25569 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
25570 sqlite3_free(zSql
);
25571 if( rx
!=SQLITE_OK
){
25572 sqlite3_finalize(pStmt
);
25574 zSql
= sqlite3_mprintf(
25575 "REPLACE INTO temp.sqlite_parameters(key,value)"
25576 "VALUES(%Q,%Q);", zKey
, zValue
);
25577 shell_check_oom(zSql
);
25578 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
25579 sqlite3_free(zSql
);
25580 if( rx
!=SQLITE_OK
){
25581 utf8_printf(p
->out
, "Error: %s\n", sqlite3_errmsg(p
->db
));
25582 sqlite3_finalize(pStmt
);
25587 sqlite3_step(pStmt
);
25588 sqlite3_finalize(pStmt
);
25591 /* .parameter unset NAME
25592 ** Remove the NAME binding from the parameter binding table, if it
25595 if( nArg
==3 && cli_strcmp(azArg
[1],"unset")==0 ){
25596 char *zSql
= sqlite3_mprintf(
25597 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg
[2]);
25598 shell_check_oom(zSql
);
25599 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
25600 sqlite3_free(zSql
);
25602 /* If no command name matches, show a syntax error */
25603 parameter_syntax_error
:
25604 showHelp(p
->out
, "parameter");
25607 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "print", n
)==0 ){
25609 for(i
=1; i
<nArg
; i
++){
25610 if( i
>1 ) raw_printf(p
->out
, " ");
25611 utf8_printf(p
->out
, "%s", azArg
[i
]);
25613 raw_printf(p
->out
, "\n");
25616 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
25617 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "progress", n
)==0 ){
25620 p
->flgProgress
= 0;
25623 for(i
=1; i
<nArg
; i
++){
25624 const char *z
= azArg
[i
];
25627 if( z
[0]=='-' ) z
++;
25628 if( cli_strcmp(z
,"quiet")==0 || cli_strcmp(z
,"q")==0 ){
25629 p
->flgProgress
|= SHELL_PROGRESS_QUIET
;
25632 if( cli_strcmp(z
,"reset")==0 ){
25633 p
->flgProgress
|= SHELL_PROGRESS_RESET
;
25636 if( cli_strcmp(z
,"once")==0 ){
25637 p
->flgProgress
|= SHELL_PROGRESS_ONCE
;
25640 if( cli_strcmp(z
,"limit")==0 ){
25642 utf8_printf(stderr
, "Error: missing argument on --limit\n");
25644 goto meta_command_exit
;
25646 p
->mxProgress
= (int)integerValue(azArg
[++i
]);
25650 utf8_printf(stderr
, "Error: unknown option: \"%s\"\n", azArg
[i
]);
25652 goto meta_command_exit
;
25654 nn
= (int)integerValue(z
);
25658 sqlite3_progress_handler(p
->db
, nn
, progress_handler
, p
);
25660 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
25662 if( c
=='p' && cli_strncmp(azArg
[0], "prompt", n
)==0 ){
25664 shell_strncpy(mainPrompt
,azArg
[1],(int)ArraySize(mainPrompt
)-1);
25667 shell_strncpy(continuePrompt
,azArg
[2],(int)ArraySize(continuePrompt
)-1);
25671 #ifndef SQLITE_SHELL_FIDDLE
25672 if( c
=='q' && cli_strncmp(azArg
[0], "quit", n
)==0 ){
25677 #ifndef SQLITE_SHELL_FIDDLE
25678 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "read", n
)==0 ){
25679 FILE *inSaved
= p
->in
;
25680 int savedLineno
= p
->lineno
;
25681 failIfSafeMode(p
, "cannot run .read in safe mode");
25683 raw_printf(stderr
, "Usage: .read FILE\n");
25685 goto meta_command_exit
;
25687 if( azArg
[1][0]=='|' ){
25688 #ifdef SQLITE_OMIT_POPEN
25689 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
25693 p
->in
= popen(azArg
[1]+1, "r");
25695 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", azArg
[1]);
25698 rc
= process_input(p
);
25702 }else if( (p
->in
= openChrSource(azArg
[1]))==0 ){
25703 utf8_printf(stderr
,"Error: cannot open \"%s\"\n", azArg
[1]);
25706 rc
= process_input(p
);
25710 p
->lineno
= savedLineno
;
25712 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25714 #ifndef SQLITE_SHELL_FIDDLE
25715 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "restore", n
)==0 ){
25716 const char *zSrcFile
;
25719 sqlite3_backup
*pBackup
;
25722 failIfSafeMode(p
, "cannot run .restore in safe mode");
25724 zSrcFile
= azArg
[1];
25726 }else if( nArg
==3 ){
25727 zSrcFile
= azArg
[2];
25730 raw_printf(stderr
, "Usage: .restore ?DB? FILE\n");
25732 goto meta_command_exit
;
25734 rc
= sqlite3_open(zSrcFile
, &pSrc
);
25735 if( rc
!=SQLITE_OK
){
25736 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zSrcFile
);
25741 pBackup
= sqlite3_backup_init(p
->db
, zDb
, pSrc
, "main");
25743 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
25747 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
25748 || rc
==SQLITE_BUSY
){
25749 if( rc
==SQLITE_BUSY
){
25750 if( nTimeout
++ >= 3 ) break;
25751 sqlite3_sleep(100);
25754 sqlite3_backup_finish(pBackup
);
25755 if( rc
==SQLITE_DONE
){
25757 }else if( rc
==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
25758 raw_printf(stderr
, "Error: source database is busy\n");
25761 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
25766 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25768 if( c
=='s' && cli_strncmp(azArg
[0], "scanstats", n
)==0 ){
25770 if( cli_strcmp(azArg
[1], "vm")==0 ){
25771 p
->scanstatsOn
= 3;
25773 if( cli_strcmp(azArg
[1], "est")==0 ){
25774 p
->scanstatsOn
= 2;
25776 p
->scanstatsOn
= (u8
)booleanValue(azArg
[1]);
25780 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
25782 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
25783 raw_printf(stderr
, "Warning: .scanstats not available in this build.\n");
25786 raw_printf(stderr
, "Usage: .scanstats on|off|est\n");
25791 if( c
=='s' && cli_strncmp(azArg
[0], "schema", n
)==0 ){
25795 const char *zDiv
= "(";
25796 const char *zName
= 0;
25799 int bNoSystemTabs
= 0;
25803 memcpy(&data
, p
, sizeof(data
));
25804 data
.showHeader
= 0;
25805 data
.cMode
= data
.mode
= MODE_Semi
;
25806 initText(&sSelect
);
25807 for(ii
=1; ii
<nArg
; ii
++){
25808 if( optionMatch(azArg
[ii
],"indent") ){
25809 data
.cMode
= data
.mode
= MODE_Pretty
;
25810 }else if( optionMatch(azArg
[ii
],"debug") ){
25812 }else if( optionMatch(azArg
[ii
],"nosys") ){
25814 }else if( azArg
[ii
][0]=='-' ){
25815 utf8_printf(stderr
, "Unknown option: \"%s\"\n", azArg
[ii
]);
25817 goto meta_command_exit
;
25818 }else if( zName
==0 ){
25822 "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
25824 goto meta_command_exit
;
25828 int isSchema
= sqlite3_strlike(zName
, "sqlite_master", '\\')==0
25829 || sqlite3_strlike(zName
, "sqlite_schema", '\\')==0
25830 || sqlite3_strlike(zName
,"sqlite_temp_master", '\\')==0
25831 || sqlite3_strlike(zName
,"sqlite_temp_schema", '\\')==0;
25833 char *new_argv
[2], *new_colv
[2];
25834 new_argv
[0] = sqlite3_mprintf(
25835 "CREATE TABLE %s (\n"
25838 " tbl_name text,\n"
25839 " rootpage integer,\n"
25842 shell_check_oom(new_argv
[0]);
25844 new_colv
[0] = "sql";
25846 callback(&data
, 1, new_argv
, new_colv
);
25847 sqlite3_free(new_argv
[0]);
25851 sqlite3_stmt
*pStmt
= 0;
25852 rc
= sqlite3_prepare_v2(p
->db
, "SELECT name FROM pragma_database_list",
25855 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
25856 sqlite3_finalize(pStmt
);
25858 goto meta_command_exit
;
25860 appendText(&sSelect
, "SELECT sql FROM", 0);
25862 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
25863 const char *zDb
= (const char*)sqlite3_column_text(pStmt
, 0);
25865 sqlite3_snprintf(sizeof(zScNum
), zScNum
, "%d", ++iSchema
);
25866 appendText(&sSelect
, zDiv
, 0);
25867 zDiv
= " UNION ALL ";
25868 appendText(&sSelect
, "SELECT shell_add_schema(sql,", 0);
25869 if( sqlite3_stricmp(zDb
, "main")!=0 ){
25870 appendText(&sSelect
, zDb
, '\'');
25872 appendText(&sSelect
, "NULL", 0);
25874 appendText(&sSelect
, ",name) AS sql, type, tbl_name, name, rowid,", 0);
25875 appendText(&sSelect
, zScNum
, 0);
25876 appendText(&sSelect
, " AS snum, ", 0);
25877 appendText(&sSelect
, zDb
, '\'');
25878 appendText(&sSelect
, " AS sname FROM ", 0);
25879 appendText(&sSelect
, zDb
, quoteChar(zDb
));
25880 appendText(&sSelect
, ".sqlite_schema", 0);
25882 sqlite3_finalize(pStmt
);
25883 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
25885 appendText(&sSelect
,
25886 " UNION ALL SELECT shell_module_schema(name),"
25887 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
25891 appendText(&sSelect
, ") WHERE ", 0);
25893 char *zQarg
= sqlite3_mprintf("%Q", zName
);
25895 shell_check_oom(zQarg
);
25896 bGlob
= strchr(zName
, '*') != 0 || strchr(zName
, '?') != 0 ||
25897 strchr(zName
, '[') != 0;
25898 if( strchr(zName
, '.') ){
25899 appendText(&sSelect
, "lower(printf('%s.%s',sname,tbl_name))", 0);
25901 appendText(&sSelect
, "lower(tbl_name)", 0);
25903 appendText(&sSelect
, bGlob
? " GLOB " : " LIKE ", 0);
25904 appendText(&sSelect
, zQarg
, 0);
25906 appendText(&sSelect
, " ESCAPE '\\' ", 0);
25908 appendText(&sSelect
, " AND ", 0);
25909 sqlite3_free(zQarg
);
25911 if( bNoSystemTabs
){
25912 appendText(&sSelect
, "name NOT LIKE 'sqlite_%%' AND ", 0);
25914 appendText(&sSelect
, "sql IS NOT NULL"
25915 " ORDER BY snum, rowid", 0);
25917 utf8_printf(p
->out
, "SQL: %s;\n", sSelect
.z
);
25919 rc
= sqlite3_exec(p
->db
, sSelect
.z
, callback
, &data
, &zErrMsg
);
25921 freeText(&sSelect
);
25924 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
25925 sqlite3_free(zErrMsg
);
25927 }else if( rc
!= SQLITE_OK
){
25928 raw_printf(stderr
,"Error: querying schema information\n");
25935 if( (c
=='s' && n
==11 && cli_strncmp(azArg
[0], "selecttrace", n
)==0)
25936 || (c
=='t' && n
==9 && cli_strncmp(azArg
[0], "treetrace", n
)==0)
25938 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
25939 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &x
);
25942 #if defined(SQLITE_ENABLE_SESSION)
25943 if( c
=='s' && cli_strncmp(azArg
[0],"session",n
)==0 && n
>=3 ){
25944 struct AuxDb
*pAuxDb
= p
->pAuxDb
;
25945 OpenSession
*pSession
= &pAuxDb
->aSession
[0];
25946 char **azCmd
= &azArg
[1];
25948 int nCmd
= nArg
- 1;
25950 if( nArg
<=1 ) goto session_syntax_error
;
25953 for(iSes
=0; iSes
<pAuxDb
->nSession
; iSes
++){
25954 if( cli_strcmp(pAuxDb
->aSession
[iSes
].zName
, azArg
[1])==0 ) break;
25956 if( iSes
<pAuxDb
->nSession
){
25957 pSession
= &pAuxDb
->aSession
[iSes
];
25961 pSession
= &pAuxDb
->aSession
[0];
25966 /* .session attach TABLE
25967 ** Invoke the sqlite3session_attach() interface to attach a particular
25968 ** table so that it is never filtered.
25970 if( cli_strcmp(azCmd
[0],"attach")==0 ){
25971 if( nCmd
!=2 ) goto session_syntax_error
;
25972 if( pSession
->p
==0 ){
25974 raw_printf(stderr
, "ERROR: No sessions are open\n");
25976 rc
= sqlite3session_attach(pSession
->p
, azCmd
[1]);
25978 raw_printf(stderr
, "ERROR: sqlite3session_attach() returns %d\n", rc
);
25984 /* .session changeset FILE
25985 ** .session patchset FILE
25986 ** Write a changeset or patchset into a file. The file is overwritten.
25988 if( cli_strcmp(azCmd
[0],"changeset")==0
25989 || cli_strcmp(azCmd
[0],"patchset")==0
25992 failIfSafeMode(p
, "cannot run \".session %s\" in safe mode", azCmd
[0]);
25993 if( nCmd
!=2 ) goto session_syntax_error
;
25994 if( pSession
->p
==0 ) goto session_not_open
;
25995 out
= fopen(azCmd
[1], "wb");
25997 utf8_printf(stderr
, "ERROR: cannot open \"%s\" for writing\n",
26002 if( azCmd
[0][0]=='c' ){
26003 rc
= sqlite3session_changeset(pSession
->p
, &szChng
, &pChng
);
26005 rc
= sqlite3session_patchset(pSession
->p
, &szChng
, &pChng
);
26008 printf("Error: error code %d\n", rc
);
26012 && fwrite(pChng
, szChng
, 1, out
)!=1 ){
26013 raw_printf(stderr
, "ERROR: Failed to write entire %d-byte output\n",
26016 sqlite3_free(pChng
);
26022 ** Close the identified session
26024 if( cli_strcmp(azCmd
[0], "close")==0 ){
26025 if( nCmd
!=1 ) goto session_syntax_error
;
26026 if( pAuxDb
->nSession
){
26027 session_close(pSession
);
26028 pAuxDb
->aSession
[iSes
] = pAuxDb
->aSession
[--pAuxDb
->nSession
];
26032 /* .session enable ?BOOLEAN?
26033 ** Query or set the enable flag
26035 if( cli_strcmp(azCmd
[0], "enable")==0 ){
26037 if( nCmd
>2 ) goto session_syntax_error
;
26038 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
26039 if( pAuxDb
->nSession
){
26040 ii
= sqlite3session_enable(pSession
->p
, ii
);
26041 utf8_printf(p
->out
, "session %s enable flag = %d\n",
26042 pSession
->zName
, ii
);
26046 /* .session filter GLOB ....
26047 ** Set a list of GLOB patterns of table names to be excluded.
26049 if( cli_strcmp(azCmd
[0], "filter")==0 ){
26051 if( nCmd
<2 ) goto session_syntax_error
;
26052 if( pAuxDb
->nSession
){
26053 for(ii
=0; ii
<pSession
->nFilter
; ii
++){
26054 sqlite3_free(pSession
->azFilter
[ii
]);
26056 sqlite3_free(pSession
->azFilter
);
26057 nByte
= sizeof(pSession
->azFilter
[0])*(nCmd
-1);
26058 pSession
->azFilter
= sqlite3_malloc( nByte
);
26059 if( pSession
->azFilter
==0 ){
26060 raw_printf(stderr
, "Error: out or memory\n");
26063 for(ii
=1; ii
<nCmd
; ii
++){
26064 char *x
= pSession
->azFilter
[ii
-1] = sqlite3_mprintf("%s", azCmd
[ii
]);
26065 shell_check_oom(x
);
26067 pSession
->nFilter
= ii
-1;
26071 /* .session indirect ?BOOLEAN?
26072 ** Query or set the indirect flag
26074 if( cli_strcmp(azCmd
[0], "indirect")==0 ){
26076 if( nCmd
>2 ) goto session_syntax_error
;
26077 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
26078 if( pAuxDb
->nSession
){
26079 ii
= sqlite3session_indirect(pSession
->p
, ii
);
26080 utf8_printf(p
->out
, "session %s indirect flag = %d\n",
26081 pSession
->zName
, ii
);
26085 /* .session isempty
26086 ** Determine if the session is empty
26088 if( cli_strcmp(azCmd
[0], "isempty")==0 ){
26090 if( nCmd
!=1 ) goto session_syntax_error
;
26091 if( pAuxDb
->nSession
){
26092 ii
= sqlite3session_isempty(pSession
->p
);
26093 utf8_printf(p
->out
, "session %s isempty flag = %d\n",
26094 pSession
->zName
, ii
);
26099 ** List all currently open sessions
26101 if( cli_strcmp(azCmd
[0],"list")==0 ){
26102 for(i
=0; i
<pAuxDb
->nSession
; i
++){
26103 utf8_printf(p
->out
, "%d %s\n", i
, pAuxDb
->aSession
[i
].zName
);
26107 /* .session open DB NAME
26108 ** Open a new session called NAME on the attached database DB.
26109 ** DB is normally "main".
26111 if( cli_strcmp(azCmd
[0],"open")==0 ){
26113 if( nCmd
!=3 ) goto session_syntax_error
;
26115 if( zName
[0]==0 ) goto session_syntax_error
;
26116 for(i
=0; i
<pAuxDb
->nSession
; i
++){
26117 if( cli_strcmp(pAuxDb
->aSession
[i
].zName
,zName
)==0 ){
26118 utf8_printf(stderr
, "Session \"%s\" already exists\n", zName
);
26119 goto meta_command_exit
;
26122 if( pAuxDb
->nSession
>=ArraySize(pAuxDb
->aSession
) ){
26124 "Maximum of %d sessions\n", ArraySize(pAuxDb
->aSession
));
26125 goto meta_command_exit
;
26127 pSession
= &pAuxDb
->aSession
[pAuxDb
->nSession
];
26128 rc
= sqlite3session_create(p
->db
, azCmd
[1], &pSession
->p
);
26130 raw_printf(stderr
, "Cannot open session: error code=%d\n", rc
);
26132 goto meta_command_exit
;
26134 pSession
->nFilter
= 0;
26135 sqlite3session_table_filter(pSession
->p
, session_filter
, pSession
);
26136 pAuxDb
->nSession
++;
26137 pSession
->zName
= sqlite3_mprintf("%s", zName
);
26138 shell_check_oom(pSession
->zName
);
26140 /* If no command name matches, show a syntax error */
26141 session_syntax_error
:
26142 showHelp(p
->out
, "session");
26146 #ifdef SQLITE_DEBUG
26147 /* Undocumented commands for internal testing. Subject to change
26148 ** without notice. */
26149 if( c
=='s' && n
>=10 && cli_strncmp(azArg
[0], "selftest-", 9)==0 ){
26150 if( cli_strncmp(azArg
[0]+9, "boolean", n
-9)==0 ){
26152 for(i
=1; i
<nArg
; i
++){
26153 v
= booleanValue(azArg
[i
]);
26154 utf8_printf(p
->out
, "%s: %d 0x%x\n", azArg
[i
], v
, v
);
26157 if( cli_strncmp(azArg
[0]+9, "integer", n
-9)==0 ){
26158 int i
; sqlite3_int64 v
;
26159 for(i
=1; i
<nArg
; i
++){
26161 v
= integerValue(azArg
[i
]);
26162 sqlite3_snprintf(sizeof(zBuf
),zBuf
,"%s: %lld 0x%llx\n", azArg
[i
],v
,v
);
26163 utf8_printf(p
->out
, "%s", zBuf
);
26169 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"selftest",n
)==0 ){
26170 int bIsInit
= 0; /* True to initialize the SELFTEST table */
26171 int bVerbose
= 0; /* Verbose output */
26172 int bSelftestExists
; /* True if SELFTEST already exists */
26173 int i
, k
; /* Loop counters */
26174 int nTest
= 0; /* Number of tests runs */
26175 int nErr
= 0; /* Number of errors seen */
26176 ShellText str
; /* Answer for a query */
26177 sqlite3_stmt
*pStmt
= 0; /* Query against the SELFTEST table */
26180 for(i
=1; i
<nArg
; i
++){
26181 const char *z
= azArg
[i
];
26182 if( z
[0]=='-' && z
[1]=='-' ) z
++;
26183 if( cli_strcmp(z
,"-init")==0 ){
26186 if( cli_strcmp(z
,"-v")==0 ){
26190 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
26191 azArg
[i
], azArg
[0]);
26192 raw_printf(stderr
, "Should be one of: --init -v\n");
26194 goto meta_command_exit
;
26197 if( sqlite3_table_column_metadata(p
->db
,"main","selftest",0,0,0,0,0,0)
26199 bSelftestExists
= 0;
26201 bSelftestExists
= 1;
26204 createSelftestTable(p
);
26205 bSelftestExists
= 1;
26208 appendText(&str
, "x", 0);
26209 for(k
=bSelftestExists
; k
>=0; k
--){
26211 rc
= sqlite3_prepare_v2(p
->db
,
26212 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
26215 rc
= sqlite3_prepare_v2(p
->db
,
26216 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
26217 " (1,'run','PRAGMA integrity_check','ok')",
26221 raw_printf(stderr
, "Error querying the selftest table\n");
26223 sqlite3_finalize(pStmt
);
26224 goto meta_command_exit
;
26226 for(i
=1; sqlite3_step(pStmt
)==SQLITE_ROW
; i
++){
26227 int tno
= sqlite3_column_int(pStmt
, 0);
26228 const char *zOp
= (const char*)sqlite3_column_text(pStmt
, 1);
26229 const char *zSql
= (const char*)sqlite3_column_text(pStmt
, 2);
26230 const char *zAns
= (const char*)sqlite3_column_text(pStmt
, 3);
26232 if( zOp
==0 ) continue;
26233 if( zSql
==0 ) continue;
26234 if( zAns
==0 ) continue;
26237 printf("%d: %s %s\n", tno
, zOp
, zSql
);
26239 if( cli_strcmp(zOp
,"memo")==0 ){
26240 utf8_printf(p
->out
, "%s\n", zSql
);
26242 if( cli_strcmp(zOp
,"run")==0 ){
26246 rc
= sqlite3_exec(p
->db
, zSql
, captureOutputCallback
, &str
, &zErrMsg
);
26249 utf8_printf(p
->out
, "Result: %s\n", str
.z
);
26251 if( rc
|| zErrMsg
){
26254 utf8_printf(p
->out
, "%d: error-code-%d: %s\n", tno
, rc
, zErrMsg
);
26255 sqlite3_free(zErrMsg
);
26256 }else if( cli_strcmp(zAns
,str
.z
)!=0 ){
26259 utf8_printf(p
->out
, "%d: Expected: [%s]\n", tno
, zAns
);
26260 utf8_printf(p
->out
, "%d: Got: [%s]\n", tno
, str
.z
);
26264 utf8_printf(stderr
,
26265 "Unknown operation \"%s\" on selftest line %d\n", zOp
, tno
);
26269 } /* End loop over rows of content from SELFTEST */
26270 sqlite3_finalize(pStmt
);
26271 } /* End loop over k */
26273 utf8_printf(p
->out
, "%d errors out of %d tests\n", nErr
, nTest
);
26276 if( c
=='s' && cli_strncmp(azArg
[0], "separator", n
)==0 ){
26277 if( nArg
<2 || nArg
>3 ){
26278 raw_printf(stderr
, "Usage: .separator COL ?ROW?\n");
26282 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
,
26283 "%.*s", (int)ArraySize(p
->colSeparator
)-1, azArg
[1]);
26286 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
,
26287 "%.*s", (int)ArraySize(p
->rowSeparator
)-1, azArg
[2]);
26291 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"sha3sum",n
)==0 ){
26292 const char *zLike
= 0; /* Which table to checksum. 0 means everything */
26293 int i
; /* Loop counter */
26294 int bSchema
= 0; /* Also hash the schema */
26295 int bSeparate
= 0; /* Hash each table separately */
26296 int iSize
= 224; /* Hash algorithm to use */
26297 int bDebug
= 0; /* Only show the query that would have run */
26298 sqlite3_stmt
*pStmt
; /* For querying tables names */
26299 char *zSql
; /* SQL to be run */
26300 char *zSep
; /* Separator */
26301 ShellText sSql
; /* Complete SQL for the query to run the hash */
26302 ShellText sQuery
; /* Set of queries used to read all content */
26304 for(i
=1; i
<nArg
; i
++){
26305 const char *z
= azArg
[i
];
26308 if( z
[0]=='-' ) z
++;
26309 if( cli_strcmp(z
,"schema")==0 ){
26312 if( cli_strcmp(z
,"sha3-224")==0 || cli_strcmp(z
,"sha3-256")==0
26313 || cli_strcmp(z
,"sha3-384")==0 || cli_strcmp(z
,"sha3-512")==0
26315 iSize
= atoi(&z
[5]);
26317 if( cli_strcmp(z
,"debug")==0 ){
26321 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
26322 azArg
[i
], azArg
[0]);
26323 showHelp(p
->out
, azArg
[0]);
26325 goto meta_command_exit
;
26328 raw_printf(stderr
, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
26330 goto meta_command_exit
;
26334 if( sqlite3_strlike("sqlite\\_%", zLike
, '\\')==0 ) bSchema
= 1;
26338 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
26339 " WHERE type='table' AND coalesce(rootpage,0)>1"
26340 " UNION ALL SELECT 'sqlite_schema'"
26341 " ORDER BY 1 collate nocase";
26343 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
26344 " WHERE type='table' AND coalesce(rootpage,0)>1"
26345 " AND name NOT LIKE 'sqlite_%'"
26346 " ORDER BY 1 collate nocase";
26348 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
26351 appendText(&sSql
, "WITH [sha3sum$query](a,b) AS(",0);
26353 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
26354 const char *zTab
= (const char*)sqlite3_column_text(pStmt
,0);
26355 if( zTab
==0 ) continue;
26356 if( zLike
&& sqlite3_strlike(zLike
, zTab
, 0)!=0 ) continue;
26357 if( cli_strncmp(zTab
, "sqlite_",7)!=0 ){
26358 appendText(&sQuery
,"SELECT * FROM ", 0);
26359 appendText(&sQuery
,zTab
,'"');
26360 appendText(&sQuery
," NOT INDEXED;", 0);
26361 }else if( cli_strcmp(zTab
, "sqlite_schema")==0 ){
26362 appendText(&sQuery
,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
26363 " ORDER BY name;", 0);
26364 }else if( cli_strcmp(zTab
, "sqlite_sequence")==0 ){
26365 appendText(&sQuery
,"SELECT name,seq FROM sqlite_sequence"
26366 " ORDER BY name;", 0);
26367 }else if( cli_strcmp(zTab
, "sqlite_stat1")==0 ){
26368 appendText(&sQuery
,"SELECT tbl,idx,stat FROM sqlite_stat1"
26369 " ORDER BY tbl,idx;", 0);
26370 }else if( cli_strcmp(zTab
, "sqlite_stat4")==0 ){
26371 appendText(&sQuery
, "SELECT * FROM ", 0);
26372 appendText(&sQuery
, zTab
, 0);
26373 appendText(&sQuery
, " ORDER BY tbl, idx, rowid;\n", 0);
26375 appendText(&sSql
, zSep
, 0);
26376 appendText(&sSql
, sQuery
.z
, '\'');
26378 appendText(&sSql
, ",", 0);
26379 appendText(&sSql
, zTab
, '\'');
26382 sqlite3_finalize(pStmt
);
26384 zSql
= sqlite3_mprintf(
26386 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
26387 " FROM [sha3sum$query]",
26390 zSql
= sqlite3_mprintf(
26392 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
26393 " FROM [sha3sum$query]",
26396 shell_check_oom(zSql
);
26400 utf8_printf(p
->out
, "%s\n", zSql
);
26402 shell_exec(p
, zSql
, 0);
26404 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
26407 char *zRevText
= /* Query for reversible to-blob-to-text check */
26408 "SELECT lower(name) as tname FROM sqlite_schema\n"
26409 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
26410 "AND name NOT LIKE 'sqlite_%%'%s\n"
26411 "ORDER BY 1 collate nocase";
26412 zRevText
= sqlite3_mprintf(zRevText
, zLike
? " AND name LIKE $tspec" : "");
26413 zRevText
= sqlite3_mprintf(
26414 /* lower-case query is first run, producing upper-case query. */
26415 "with tabcols as materialized(\n"
26416 "select tname, cname\n"
26418 " select printf('\"%%w\"',ss.tname) as tname,"
26419 " printf('\"%%w\"',ti.name) as cname\n"
26420 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
26421 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
26422 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
26423 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
26424 "FROM '||tname||' WHERE '\n"
26425 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
26426 "|| ' AND typeof('||cname||')=''text'' ',\n"
26427 "' OR ') as query, tname from tabcols group by tname)"
26429 shell_check_oom(zRevText
);
26430 if( bDebug
) utf8_printf(p
->out
, "%s\n", zRevText
);
26431 lrc
= sqlite3_prepare_v2(p
->db
, zRevText
, -1, &pStmt
, 0);
26432 if( lrc
!=SQLITE_OK
){
26433 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
26434 ** user does cruel and unnatural things like ".limit expr_depth 0". */
26437 if( zLike
) sqlite3_bind_text(pStmt
,1,zLike
,-1,SQLITE_STATIC
);
26438 lrc
= SQLITE_ROW
==sqlite3_step(pStmt
);
26440 const char *zGenQuery
= (char*)sqlite3_column_text(pStmt
,0);
26441 sqlite3_stmt
*pCheckStmt
;
26442 lrc
= sqlite3_prepare_v2(p
->db
, zGenQuery
, -1, &pCheckStmt
, 0);
26443 if( bDebug
) utf8_printf(p
->out
, "%s\n", zGenQuery
);
26444 if( lrc
!=SQLITE_OK
){
26447 if( SQLITE_ROW
==sqlite3_step(pCheckStmt
) ){
26448 double countIrreversible
= sqlite3_column_double(pCheckStmt
, 0);
26449 if( countIrreversible
>0 ){
26450 int sz
= (int)(countIrreversible
+ 0.5);
26451 utf8_printf(stderr
,
26452 "Digest includes %d invalidly encoded text field%s.\n",
26453 sz
, (sz
>1)? "s": "");
26456 sqlite3_finalize(pCheckStmt
);
26458 sqlite3_finalize(pStmt
);
26461 if( rc
) utf8_printf(stderr
, ".sha3sum failed.\n");
26462 sqlite3_free(zRevText
);
26464 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
26465 sqlite3_free(zSql
);
26468 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
26470 && (cli_strncmp(azArg
[0], "shell", n
)==0
26471 || cli_strncmp(azArg
[0],"system",n
)==0)
26475 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
26477 raw_printf(stderr
, "Usage: .system COMMAND\n");
26479 goto meta_command_exit
;
26481 zCmd
= sqlite3_mprintf(strchr(azArg
[1],' ')==0?"%s":"\"%s\"", azArg
[1]);
26482 for(i
=2; i
<nArg
&& zCmd
!=0; i
++){
26483 zCmd
= sqlite3_mprintf(strchr(azArg
[i
],' ')==0?"%z %s":"%z \"%s\"",
26486 x
= zCmd
!=0 ? system(zCmd
) : 1;
26487 sqlite3_free(zCmd
);
26488 if( x
) raw_printf(stderr
, "System command returns %d\n", x
);
26490 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
26492 if( c
=='s' && cli_strncmp(azArg
[0], "show", n
)==0 ){
26493 static const char *azBool
[] = { "off", "on", "trigger", "full"};
26497 raw_printf(stderr
, "Usage: .show\n");
26499 goto meta_command_exit
;
26501 utf8_printf(p
->out
, "%12.12s: %s\n","echo",
26502 azBool
[ShellHasFlag(p
, SHFLG_Echo
)]);
26503 utf8_printf(p
->out
, "%12.12s: %s\n","eqp", azBool
[p
->autoEQP
&3]);
26504 utf8_printf(p
->out
, "%12.12s: %s\n","explain",
26505 p
->mode
==MODE_Explain
? "on" : p
->autoExplain
? "auto" : "off");
26506 utf8_printf(p
->out
,"%12.12s: %s\n","headers", azBool
[p
->showHeader
!=0]);
26507 if( p
->mode
==MODE_Column
26508 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
26511 (p
->out
, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
26512 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
26513 p
->cmOpts
.bWordWrap
? "on" : "off",
26514 p
->cmOpts
.bQuote
? "" : "no");
26516 utf8_printf(p
->out
, "%12.12s: %s\n","mode", modeDescr
[p
->mode
]);
26518 utf8_printf(p
->out
, "%12.12s: ", "nullvalue");
26519 output_c_string(p
->out
, p
->nullValue
);
26520 raw_printf(p
->out
, "\n");
26521 utf8_printf(p
->out
,"%12.12s: %s\n","output",
26522 strlen30(p
->outfile
) ? p
->outfile
: "stdout");
26523 utf8_printf(p
->out
,"%12.12s: ", "colseparator");
26524 output_c_string(p
->out
, p
->colSeparator
);
26525 raw_printf(p
->out
, "\n");
26526 utf8_printf(p
->out
,"%12.12s: ", "rowseparator");
26527 output_c_string(p
->out
, p
->rowSeparator
);
26528 raw_printf(p
->out
, "\n");
26529 switch( p
->statsOn
){
26530 case 0: zOut
= "off"; break;
26531 default: zOut
= "on"; break;
26532 case 2: zOut
= "stmt"; break;
26533 case 3: zOut
= "vmstep"; break;
26535 utf8_printf(p
->out
, "%12.12s: %s\n","stats", zOut
);
26536 utf8_printf(p
->out
, "%12.12s: ", "width");
26537 for (i
=0;i
<p
->nWidth
;i
++) {
26538 raw_printf(p
->out
, "%d ", p
->colWidth
[i
]);
26540 raw_printf(p
->out
, "\n");
26541 utf8_printf(p
->out
, "%12.12s: %s\n", "filename",
26542 p
->pAuxDb
->zDbFilename
? p
->pAuxDb
->zDbFilename
: "");
26545 if( c
=='s' && cli_strncmp(azArg
[0], "stats", n
)==0 ){
26547 if( cli_strcmp(azArg
[1],"stmt")==0 ){
26549 }else if( cli_strcmp(azArg
[1],"vmstep")==0 ){
26552 p
->statsOn
= (u8
)booleanValue(azArg
[1]);
26554 }else if( nArg
==1 ){
26555 display_stats(p
->db
, p
, 0);
26557 raw_printf(stderr
, "Usage: .stats ?on|off|stmt|vmstep?\n");
26562 if( (c
=='t' && n
>1 && cli_strncmp(azArg
[0], "tables", n
)==0)
26563 || (c
=='i' && (cli_strncmp(azArg
[0], "indices", n
)==0
26564 || cli_strncmp(azArg
[0], "indexes", n
)==0) )
26566 sqlite3_stmt
*pStmt
;
26573 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
26575 sqlite3_finalize(pStmt
);
26576 return shellDatabaseError(p
->db
);
26579 if( nArg
>2 && c
=='i' ){
26580 /* It is an historical accident that the .indexes command shows an error
26581 ** when called with the wrong number of arguments whereas the .tables
26582 ** command does not. */
26583 raw_printf(stderr
, "Usage: .indexes ?LIKE-PATTERN?\n");
26585 sqlite3_finalize(pStmt
);
26586 goto meta_command_exit
;
26588 for(ii
=0; sqlite3_step(pStmt
)==SQLITE_ROW
; ii
++){
26589 const char *zDbName
= (const char*)sqlite3_column_text(pStmt
, 1);
26590 if( zDbName
==0 ) continue;
26591 if( s
.z
&& s
.z
[0] ) appendText(&s
, " UNION ALL ", 0);
26592 if( sqlite3_stricmp(zDbName
, "main")==0 ){
26593 appendText(&s
, "SELECT name FROM ", 0);
26595 appendText(&s
, "SELECT ", 0);
26596 appendText(&s
, zDbName
, '\'');
26597 appendText(&s
, "||'.'||name FROM ", 0);
26599 appendText(&s
, zDbName
, '"');
26600 appendText(&s
, ".sqlite_schema ", 0);
26602 appendText(&s
," WHERE type IN ('table','view')"
26603 " AND name NOT LIKE 'sqlite_%'"
26604 " AND name LIKE ?1", 0);
26606 appendText(&s
," WHERE type='index'"
26607 " AND tbl_name LIKE ?1", 0);
26610 rc
= sqlite3_finalize(pStmt
);
26611 if( rc
==SQLITE_OK
){
26612 appendText(&s
, " ORDER BY 1", 0);
26613 rc
= sqlite3_prepare_v2(p
->db
, s
.z
, -1, &pStmt
, 0);
26616 if( rc
) return shellDatabaseError(p
->db
);
26618 /* Run the SQL statement prepared by the above block. Store the results
26619 ** as an array of nul-terminated strings in azResult[]. */
26623 sqlite3_bind_text(pStmt
, 1, azArg
[1], -1, SQLITE_TRANSIENT
);
26625 sqlite3_bind_text(pStmt
, 1, "%", -1, SQLITE_STATIC
);
26627 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
26628 if( nRow
>=nAlloc
){
26630 int n2
= nAlloc
*2 + 10;
26631 azNew
= sqlite3_realloc64(azResult
, sizeof(azResult
[0])*n2
);
26632 shell_check_oom(azNew
);
26636 azResult
[nRow
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
26637 shell_check_oom(azResult
[nRow
]);
26640 if( sqlite3_finalize(pStmt
)!=SQLITE_OK
){
26641 rc
= shellDatabaseError(p
->db
);
26644 /* Pretty-print the contents of array azResult[] to the output */
26645 if( rc
==0 && nRow
>0 ){
26646 int len
, maxlen
= 0;
26648 int nPrintCol
, nPrintRow
;
26649 for(i
=0; i
<nRow
; i
++){
26650 len
= strlen30(azResult
[i
]);
26651 if( len
>maxlen
) maxlen
= len
;
26653 nPrintCol
= 80/(maxlen
+2);
26654 if( nPrintCol
<1 ) nPrintCol
= 1;
26655 nPrintRow
= (nRow
+ nPrintCol
- 1)/nPrintCol
;
26656 for(i
=0; i
<nPrintRow
; i
++){
26657 for(j
=i
; j
<nRow
; j
+=nPrintRow
){
26658 char *zSp
= j
<nPrintRow
? "" : " ";
26659 utf8_printf(p
->out
, "%s%-*s", zSp
, maxlen
,
26660 azResult
[j
] ? azResult
[j
]:"");
26662 raw_printf(p
->out
, "\n");
26666 for(ii
=0; ii
<nRow
; ii
++) sqlite3_free(azResult
[ii
]);
26667 sqlite3_free(azResult
);
26670 #ifndef SQLITE_SHELL_FIDDLE
26671 /* Begin redirecting output to the file "testcase-out.txt" */
26672 if( c
=='t' && cli_strcmp(azArg
[0],"testcase")==0 ){
26674 p
->out
= output_file_open("testcase-out.txt", 0);
26676 raw_printf(stderr
, "Error: cannot open 'testcase-out.txt'\n");
26679 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "%s", azArg
[1]);
26681 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "?");
26684 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26686 #ifndef SQLITE_UNTESTABLE
26687 if( c
=='t' && n
>=8 && cli_strncmp(azArg
[0], "testctrl", n
)==0 ){
26688 static const struct {
26689 const char *zCtrlName
; /* Name of a test-control option */
26690 int ctrlCode
; /* Integer code for that option */
26691 int unSafe
; /* Not valid unless --unsafe-testing */
26692 const char *zUsage
; /* Usage notes */
26694 {"always", SQLITE_TESTCTRL_ALWAYS
, 1, "BOOLEAN" },
26695 {"assert", SQLITE_TESTCTRL_ASSERT
, 1, "BOOLEAN" },
26696 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
26697 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
26698 {"byteorder", SQLITE_TESTCTRL_BYTEORDER
, 0, "" },
26699 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS
,0,"BOOLEAN" },
26700 /*{"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/
26701 {"imposter", SQLITE_TESTCTRL_IMPOSTER
,1,"SCHEMA ON/OFF ROOTPAGE"},
26702 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
,0,"" },
26703 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT
,0,"BOOLEAN" },
26704 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT
,1, "BOOLEAN" },
26705 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS
,0,"DISABLE-MASK" },
26707 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE
,0,"" },
26709 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE
,0, "OFFSET " },
26710 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE
,0, "" },
26711 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE
, 0, "" },
26712 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED
, 0, "SEED ?db?" },
26713 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT
, 0, "" },
26714 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP
, 0, "NMAX" },
26715 {"tune", SQLITE_TESTCTRL_TUNE
, 1, "ID VALUE" },
26716 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE
,0,"?BOOLEAN|\"default\"?"},
26720 int rc2
= 0; /* 0: usage. 1: %d 2: %x 3: no-output */
26723 const char *zCmd
= 0;
26726 zCmd
= nArg
>=2 ? azArg
[1] : "help";
26728 /* The argument can optionally begin with "-" or "--" */
26729 if( zCmd
[0]=='-' && zCmd
[1] ){
26731 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
26734 /* --help lists all test-controls */
26735 if( cli_strcmp(zCmd
,"help")==0 ){
26736 utf8_printf(p
->out
, "Available test-controls:\n");
26737 for(i
=0; i
<ArraySize(aCtrl
); i
++){
26738 if( aCtrl
[i
].unSafe
&& !ShellHasFlag(p
,SHFLG_TestingMode
) ) continue;
26739 utf8_printf(p
->out
, " .testctrl %s %s\n",
26740 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
26743 goto meta_command_exit
;
26746 /* convert testctrl text option to value. allow any unique prefix
26747 ** of the option name, or a numerical value. */
26748 n2
= strlen30(zCmd
);
26749 for(i
=0; i
<ArraySize(aCtrl
); i
++){
26750 if( aCtrl
[i
].unSafe
&& !ShellHasFlag(p
,SHFLG_TestingMode
) ) continue;
26751 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
26753 testctrl
= aCtrl
[i
].ctrlCode
;
26756 utf8_printf(stderr
, "Error: ambiguous test-control: \"%s\"\n"
26757 "Use \".testctrl --help\" for help\n", zCmd
);
26759 goto meta_command_exit
;
26764 utf8_printf(stderr
,"Error: unknown test-control: %s\n"
26765 "Use \".testctrl --help\" for help\n", zCmd
);
26769 /* sqlite3_test_control(int, db, int) */
26770 case SQLITE_TESTCTRL_OPTIMIZATIONS
:
26772 unsigned int opt
= (unsigned int)strtol(azArg
[2], 0, 0);
26773 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
26778 /* sqlite3_test_control(int) */
26779 case SQLITE_TESTCTRL_PRNG_SAVE
:
26780 case SQLITE_TESTCTRL_PRNG_RESTORE
:
26781 case SQLITE_TESTCTRL_BYTEORDER
:
26783 rc2
= sqlite3_test_control(testctrl
);
26784 isOk
= testctrl
==SQLITE_TESTCTRL_BYTEORDER
? 1 : 3;
26788 /* sqlite3_test_control(int, uint) */
26789 case SQLITE_TESTCTRL_PENDING_BYTE
:
26791 unsigned int opt
= (unsigned int)integerValue(azArg
[2]);
26792 rc2
= sqlite3_test_control(testctrl
, opt
);
26797 /* sqlite3_test_control(int, int, sqlite3*) */
26798 case SQLITE_TESTCTRL_PRNG_SEED
:
26799 if( nArg
==3 || nArg
==4 ){
26800 int ii
= (int)integerValue(azArg
[2]);
26802 if( ii
==0 && cli_strcmp(azArg
[2],"random")==0 ){
26803 sqlite3_randomness(sizeof(ii
),&ii
);
26804 printf("-- random seed: %d\n", ii
);
26810 /* Make sure the schema has been loaded */
26811 sqlite3_table_column_metadata(db
, 0, "x", 0, 0, 0, 0, 0, 0);
26813 rc2
= sqlite3_test_control(testctrl
, ii
, db
);
26818 /* sqlite3_test_control(int, int) */
26819 case SQLITE_TESTCTRL_ASSERT
:
26820 case SQLITE_TESTCTRL_ALWAYS
:
26822 int opt
= booleanValue(azArg
[2]);
26823 rc2
= sqlite3_test_control(testctrl
, opt
);
26828 /* sqlite3_test_control(int, int) */
26829 case SQLITE_TESTCTRL_LOCALTIME_FAULT
:
26830 case SQLITE_TESTCTRL_NEVER_CORRUPT
:
26832 int opt
= booleanValue(azArg
[2]);
26833 rc2
= sqlite3_test_control(testctrl
, opt
);
26838 /* sqlite3_test_control(int, int) */
26839 case SQLITE_TESTCTRL_USELONGDOUBLE
: {
26842 if( cli_strcmp(azArg
[2],"default")==0 ){
26845 opt
= booleanValue(azArg
[2]);
26848 rc2
= sqlite3_test_control(testctrl
, opt
);
26853 /* sqlite3_test_control(sqlite3*) */
26854 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
:
26855 rc2
= sqlite3_test_control(testctrl
, p
->db
);
26859 case SQLITE_TESTCTRL_IMPOSTER
:
26861 rc2
= sqlite3_test_control(testctrl
, p
->db
,
26863 integerValue(azArg
[3]),
26864 integerValue(azArg
[4]));
26869 case SQLITE_TESTCTRL_SEEK_COUNT
: {
26871 rc2
= sqlite3_test_control(testctrl
, p
->db
, &x
);
26872 utf8_printf(p
->out
, "%llu\n", x
);
26878 case SQLITE_TESTCTRL_PARSER_COVERAGE
: {
26880 sqlite3_test_control(testctrl
, p
->out
);
26886 #ifdef SQLITE_DEBUG
26887 case SQLITE_TESTCTRL_TUNE
: {
26889 int id
= (int)integerValue(azArg
[2]);
26890 int val
= (int)integerValue(azArg
[3]);
26891 sqlite3_test_control(testctrl
, id
, &val
);
26893 }else if( nArg
==3 ){
26894 int id
= (int)integerValue(azArg
[2]);
26895 sqlite3_test_control(testctrl
, -id
, &rc2
);
26897 }else if( nArg
==2 ){
26901 rc2
= sqlite3_test_control(testctrl
, -id
, &val
);
26902 if( rc2
!=SQLITE_OK
) break;
26903 if( id
>1 ) utf8_printf(p
->out
, " ");
26904 utf8_printf(p
->out
, "%d: %d", id
, val
);
26907 if( id
>1 ) utf8_printf(p
->out
, "\n");
26913 case SQLITE_TESTCTRL_SORTER_MMAP
:
26915 int opt
= (unsigned int)integerValue(azArg
[2]);
26916 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
26922 if( isOk
==0 && iCtrl
>=0 ){
26923 utf8_printf(p
->out
, "Usage: .testctrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
26925 }else if( isOk
==1 ){
26926 raw_printf(p
->out
, "%d\n", rc2
);
26927 }else if( isOk
==2 ){
26928 raw_printf(p
->out
, "0x%08x\n", rc2
);
26931 #endif /* !defined(SQLITE_UNTESTABLE) */
26933 if( c
=='t' && n
>4 && cli_strncmp(azArg
[0], "timeout", n
)==0 ){
26935 sqlite3_busy_timeout(p
->db
, nArg
>=2 ? (int)integerValue(azArg
[1]) : 0);
26938 if( c
=='t' && n
>=5 && cli_strncmp(azArg
[0], "timer", n
)==0 ){
26940 enableTimer
= booleanValue(azArg
[1]);
26941 if( enableTimer
&& !HAS_TIMER
){
26942 raw_printf(stderr
, "Error: timer not available on this system.\n");
26946 raw_printf(stderr
, "Usage: .timer on|off\n");
26951 #ifndef SQLITE_OMIT_TRACE
26952 if( c
=='t' && cli_strncmp(azArg
[0], "trace", n
)==0 ){
26956 for(jj
=1; jj
<nArg
; jj
++){
26957 const char *z
= azArg
[jj
];
26959 if( optionMatch(z
, "expanded") ){
26960 p
->eTraceType
= SHELL_TRACE_EXPANDED
;
26962 #ifdef SQLITE_ENABLE_NORMALIZE
26963 else if( optionMatch(z
, "normalized") ){
26964 p
->eTraceType
= SHELL_TRACE_NORMALIZED
;
26967 else if( optionMatch(z
, "plain") ){
26968 p
->eTraceType
= SHELL_TRACE_PLAIN
;
26970 else if( optionMatch(z
, "profile") ){
26971 mType
|= SQLITE_TRACE_PROFILE
;
26973 else if( optionMatch(z
, "row") ){
26974 mType
|= SQLITE_TRACE_ROW
;
26976 else if( optionMatch(z
, "stmt") ){
26977 mType
|= SQLITE_TRACE_STMT
;
26979 else if( optionMatch(z
, "close") ){
26980 mType
|= SQLITE_TRACE_CLOSE
;
26983 raw_printf(stderr
, "Unknown option \"%s\" on \".trace\"\n", z
);
26985 goto meta_command_exit
;
26988 output_file_close(p
->traceOut
);
26989 p
->traceOut
= output_file_open(z
, 0);
26992 if( p
->traceOut
==0 ){
26993 sqlite3_trace_v2(p
->db
, 0, 0, 0);
26995 if( mType
==0 ) mType
= SQLITE_TRACE_STMT
;
26996 sqlite3_trace_v2(p
->db
, mType
, sql_trace_callback
, p
);
26999 #endif /* !defined(SQLITE_OMIT_TRACE) */
27001 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27002 if( c
=='u' && cli_strncmp(azArg
[0], "unmodule", n
)==0 ){
27007 raw_printf(stderr
, "Usage: .unmodule [--allexcept] NAME ...\n");
27009 goto meta_command_exit
;
27013 if( zOpt
[0]=='-' && zOpt
[1]=='-' && zOpt
[2]!=0 ) zOpt
++;
27014 lenOpt
= (int)strlen(zOpt
);
27015 if( lenOpt
>=3 && cli_strncmp(zOpt
, "-allexcept",lenOpt
)==0 ){
27016 assert( azArg
[nArg
]==0 );
27017 sqlite3_drop_modules(p
->db
, nArg
>2 ? (const char**)(azArg
+2) : 0);
27019 for(ii
=1; ii
<nArg
; ii
++){
27020 sqlite3_create_module(p
->db
, azArg
[ii
], 0, 0);
27026 #if SQLITE_USER_AUTHENTICATION
27027 if( c
=='u' && cli_strncmp(azArg
[0], "user", n
)==0 ){
27029 raw_printf(stderr
, "Usage: .user SUBCOMMAND ...\n");
27031 goto meta_command_exit
;
27034 if( cli_strcmp(azArg
[1],"login")==0 ){
27036 raw_printf(stderr
, "Usage: .user login USER PASSWORD\n");
27038 goto meta_command_exit
;
27040 rc
= sqlite3_user_authenticate(p
->db
, azArg
[2], azArg
[3],
27041 strlen30(azArg
[3]));
27043 utf8_printf(stderr
, "Authentication failed for user %s\n", azArg
[2]);
27046 }else if( cli_strcmp(azArg
[1],"add")==0 ){
27048 raw_printf(stderr
, "Usage: .user add USER PASSWORD ISADMIN\n");
27050 goto meta_command_exit
;
27052 rc
= sqlite3_user_add(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
27053 booleanValue(azArg
[4]));
27055 raw_printf(stderr
, "User-Add failed: %d\n", rc
);
27058 }else if( cli_strcmp(azArg
[1],"edit")==0 ){
27060 raw_printf(stderr
, "Usage: .user edit USER PASSWORD ISADMIN\n");
27062 goto meta_command_exit
;
27064 rc
= sqlite3_user_change(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
27065 booleanValue(azArg
[4]));
27067 raw_printf(stderr
, "User-Edit failed: %d\n", rc
);
27070 }else if( cli_strcmp(azArg
[1],"delete")==0 ){
27072 raw_printf(stderr
, "Usage: .user delete USER\n");
27074 goto meta_command_exit
;
27076 rc
= sqlite3_user_delete(p
->db
, azArg
[2]);
27078 raw_printf(stderr
, "User-Delete failed: %d\n", rc
);
27082 raw_printf(stderr
, "Usage: .user login|add|edit|delete ...\n");
27084 goto meta_command_exit
;
27087 #endif /* SQLITE_USER_AUTHENTICATION */
27089 if( c
=='v' && cli_strncmp(azArg
[0], "version", n
)==0 ){
27090 char *zPtrSz
= sizeof(void*)==8 ? "64-bit" : "32-bit";
27091 utf8_printf(p
->out
, "SQLite %s %s\n" /*extra-version-info*/,
27092 sqlite3_libversion(), sqlite3_sourceid());
27093 #if SQLITE_HAVE_ZLIB
27094 utf8_printf(p
->out
, "zlib version %s\n", zlibVersion());
27096 #define CTIMEOPT_VAL_(opt) #opt
27097 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
27098 #if defined(__clang__) && defined(__clang_major__)
27099 utf8_printf(p
->out
, "clang-" CTIMEOPT_VAL(__clang_major__
) "."
27100 CTIMEOPT_VAL(__clang_minor__
) "."
27101 CTIMEOPT_VAL(__clang_patchlevel__
) " (%s)\n", zPtrSz
);
27102 #elif defined(_MSC_VER)
27103 utf8_printf(p
->out
, "msvc-" CTIMEOPT_VAL(_MSC_VER
) " (%s)\n", zPtrSz
);
27104 #elif defined(__GNUC__) && defined(__VERSION__)
27105 utf8_printf(p
->out
, "gcc-" __VERSION__
" (%s)\n", zPtrSz
);
27109 if( c
=='v' && cli_strncmp(azArg
[0], "vfsinfo", n
)==0 ){
27110 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
27111 sqlite3_vfs
*pVfs
= 0;
27113 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
27115 utf8_printf(p
->out
, "vfs.zName = \"%s\"\n", pVfs
->zName
);
27116 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
27117 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
27118 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
27123 if( c
=='v' && cli_strncmp(azArg
[0], "vfslist", n
)==0 ){
27125 sqlite3_vfs
*pCurrent
= 0;
27127 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_VFS_POINTER
, &pCurrent
);
27129 for(pVfs
=sqlite3_vfs_find(0); pVfs
; pVfs
=pVfs
->pNext
){
27130 utf8_printf(p
->out
, "vfs.zName = \"%s\"%s\n", pVfs
->zName
,
27131 pVfs
==pCurrent
? " <--- CURRENT" : "");
27132 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
27133 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
27134 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
27136 raw_printf(p
->out
, "-----------------------------------\n");
27141 if( c
=='v' && cli_strncmp(azArg
[0], "vfsname", n
)==0 ){
27142 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
27143 char *zVfsName
= 0;
27145 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFSNAME
, &zVfsName
);
27147 utf8_printf(p
->out
, "%s\n", zVfsName
);
27148 sqlite3_free(zVfsName
);
27153 if( c
=='w' && cli_strncmp(azArg
[0], "wheretrace", n
)==0 ){
27154 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
27155 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &x
);
27158 if( c
=='w' && cli_strncmp(azArg
[0], "width", n
)==0 ){
27160 assert( nArg
<=ArraySize(azArg
) );
27161 p
->nWidth
= nArg
-1;
27162 p
->colWidth
= realloc(p
->colWidth
, (p
->nWidth
+1)*sizeof(int)*2);
27163 if( p
->colWidth
==0 && p
->nWidth
>0 ) shell_out_of_memory();
27164 if( p
->nWidth
) p
->actualWidth
= &p
->colWidth
[p
->nWidth
];
27165 for(j
=1; j
<nArg
; j
++){
27166 p
->colWidth
[j
-1] = (int)integerValue(azArg
[j
]);
27171 utf8_printf(stderr
, "Error: unknown command or invalid arguments: "
27172 " \"%s\". Enter \".help\" for help\n", azArg
[0]);
27179 if( p
->outCount
==0 ) output_reset(p
);
27181 p
->bSafeMode
= p
->bSafeModePersist
;
27185 /* Line scan result and intermediate states (supporting scan resumption)
27188 # define CHAR_BIT 8
27191 QSS_HasDark
= 1<<CHAR_BIT
, QSS_EndingSemi
= 2<<CHAR_BIT
,
27192 QSS_CharMask
= (1<<CHAR_BIT
)-1, QSS_ScanMask
= 3<<CHAR_BIT
,
27195 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
27196 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
27197 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
27198 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
27199 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
27202 ** Scan line for classification to guide shell's handling.
27203 ** The scan is resumable for subsequent lines when prior
27204 ** return values are passed as the 2nd argument.
27206 static QuickScanState
quickscan(char *zLine
, QuickScanState qss
,
27207 SCAN_TRACKER_REFTYPE pst
){
27209 char cWait
= (char)qss
; /* intentional narrowing loss */
27212 assert( cWait
==0 );
27213 while( (cin
= *zLine
++)!=0 ){
27220 while((cin
= *++zLine
)!=0 )
27225 qss
|= QSS_EndingSemi
;
27231 CONTINUE_PROMPT_AWAITS(pst
, "/*");
27232 qss
= QSS_SETV(qss
, cWait
);
27238 deliberate_fall_through
;
27239 case '`': case '\'': case '"':
27241 qss
= QSS_HasDark
| cWait
;
27242 CONTINUE_PROMPT_AWAITC(pst
, cin
);
27245 CONTINUE_PAREN_INCR(pst
, 1);
27248 CONTINUE_PAREN_INCR(pst
, -1);
27253 qss
= (qss
& ~QSS_EndingSemi
) | QSS_HasDark
;
27257 while( (cin
= *zLine
++)!=0 ){
27261 if( *zLine
!= '/' )
27265 CONTINUE_PROMPT_AWAITC(pst
, 0);
27266 qss
= QSS_SETV(qss
, 0);
27268 case '`': case '\'': case '"':
27270 /* Swallow doubled end-delimiter.*/
27274 deliberate_fall_through
;
27277 CONTINUE_PROMPT_AWAITC(pst
, 0);
27278 qss
= QSS_SETV(qss
, 0);
27280 default: assert(0);
27289 ** Return TRUE if the line typed in is an SQL command terminator other
27290 ** than a semi-colon. The SQL Server style "go" command is understood
27291 ** as is the Oracle "/".
27293 static int line_is_command_terminator(char *zLine
){
27294 while( IsSpace(zLine
[0]) ){ zLine
++; };
27295 if( zLine
[0]=='/' )
27296 zLine
+= 1; /* Oracle */
27297 else if ( ToLower(zLine
[0])=='g' && ToLower(zLine
[1])=='o' )
27298 zLine
+= 2; /* SQL Server */
27301 return quickscan(zLine
, QSS_Start
, 0)==QSS_Start
;
27305 ** The CLI needs a working sqlite3_complete() to work properly. So error
27306 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
27308 #ifdef SQLITE_OMIT_COMPLETE
27309 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
27313 ** Return true if zSql is a complete SQL statement. Return false if it
27314 ** ends in the middle of a string literal or C-style comment.
27316 static int line_is_complete(char *zSql
, int nSql
){
27318 if( zSql
==0 ) return 1;
27321 rc
= sqlite3_complete(zSql
);
27327 ** Run a single line of SQL. Return the number of errors.
27329 static int runOneSqlLine(ShellState
*p
, char *zSql
, FILE *in
, int startline
){
27334 if( ShellHasFlag(p
,SHFLG_Backslash
) ) resolve_backslashes(zSql
);
27335 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
27337 rc
= shell_exec(p
, zSql
, &zErrMsg
);
27339 if( rc
|| zErrMsg
){
27341 const char *zErrorTail
;
27342 const char *zErrorType
;
27344 zErrorType
= "Error";
27345 zErrorTail
= sqlite3_errmsg(p
->db
);
27346 }else if( cli_strncmp(zErrMsg
, "in prepare, ",12)==0 ){
27347 zErrorType
= "Parse error";
27348 zErrorTail
= &zErrMsg
[12];
27349 }else if( cli_strncmp(zErrMsg
, "stepping, ", 10)==0 ){
27350 zErrorType
= "Runtime error";
27351 zErrorTail
= &zErrMsg
[10];
27353 zErrorType
= "Error";
27354 zErrorTail
= zErrMsg
;
27356 if( in
!=0 || !stdin_is_interactive
){
27357 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
,
27358 "%s near line %d:", zErrorType
, startline
);
27360 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:", zErrorType
);
27362 utf8_printf(stderr
, "%s %s\n", zPrefix
, zErrorTail
);
27363 sqlite3_free(zErrMsg
);
27366 }else if( ShellHasFlag(p
, SHFLG_CountChanges
) ){
27367 char zLineBuf
[2000];
27368 sqlite3_snprintf(sizeof(zLineBuf
), zLineBuf
,
27369 "changes: %lld total_changes: %lld",
27370 sqlite3_changes64(p
->db
), sqlite3_total_changes64(p
->db
));
27371 raw_printf(p
->out
, "%s\n", zLineBuf
);
27376 static void echo_group_input(ShellState
*p
, const char *zDo
){
27377 if( ShellHasFlag(p
, SHFLG_Echo
) ) utf8_printf(p
->out
, "%s\n", zDo
);
27380 #ifdef SQLITE_SHELL_FIDDLE
27382 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
27383 ** impl because we need the global shellState and cannot access it from that
27384 ** function without moving lots of code around (creating a larger/messier diff).
27386 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
27387 /* Parse the next line from shellState.wasm.zInput. */
27388 const char *zBegin
= shellState
.wasm
.zPos
;
27389 const char *z
= zBegin
;
27393 UNUSED_PARAMETER(in
);
27394 UNUSED_PARAMETER(isContinuation
);
27398 while(*z
&& isspace(*z
)) ++z
;
27400 for(; *z
&& '\n'!=*z
; ++nZ
, ++z
){}
27401 if(nZ
>0 && '\r'==zBegin
[nZ
-1]){
27404 shellState
.wasm
.zPos
= z
;
27405 zLine
= realloc(zPrior
, nZ
+1);
27406 shell_check_oom(zLine
);
27407 memcpy(zLine
, zBegin
, nZ
);
27411 #endif /* SQLITE_SHELL_FIDDLE */
27414 ** Read input from *in and process it. If *in==0 then input
27415 ** is interactive - the user is typing it it. Otherwise, input
27416 ** is coming from a file or device. A prompt is issued and history
27417 ** is saved only if input is interactive. An interrupt signal will
27418 ** cause this routine to exit immediately, unless input is interactive.
27420 ** Return the number of errors.
27422 static int process_input(ShellState
*p
){
27423 char *zLine
= 0; /* A single input line */
27424 char *zSql
= 0; /* Accumulated SQL text */
27425 i64 nLine
; /* Length of current line */
27426 i64 nSql
= 0; /* Bytes of zSql[] used */
27427 i64 nAlloc
= 0; /* Allocated zSql[] space */
27428 int rc
; /* Error code */
27429 int errCnt
= 0; /* Number of errors seen */
27430 i64 startline
= 0; /* Line number for start of current input */
27431 QuickScanState qss
= QSS_Start
; /* Accumulated line status (so far) */
27433 if( p
->inputNesting
==MAX_INPUT_NESTING
){
27434 /* This will be more informative in a later version. */
27435 utf8_printf(stderr
,"Input nesting limit (%d) reached at line %d."
27436 " Check recursion.\n", MAX_INPUT_NESTING
, p
->lineno
);
27441 CONTINUE_PROMPT_RESET
;
27442 while( errCnt
==0 || !bail_on_error
|| (p
->in
==0 && stdin_is_interactive
) ){
27444 zLine
= one_input_line(p
->in
, zLine
, nSql
>0);
27447 if( p
->in
==0 && stdin_is_interactive
) printf("\n");
27450 if( seenInterrupt
){
27451 if( p
->in
!=0 ) break;
27455 if( QSS_INPLAIN(qss
)
27456 && line_is_command_terminator(zLine
)
27457 && line_is_complete(zSql
, nSql
) ){
27458 memcpy(zLine
,";",2);
27460 qss
= quickscan(zLine
, qss
, CONTINUE_PROMPT_PSTATE
);
27461 if( QSS_PLAINWHITE(qss
) && nSql
==0 ){
27462 /* Just swallow single-line whitespace */
27463 echo_group_input(p
, zLine
);
27467 if( zLine
&& (zLine
[0]=='.' || zLine
[0]=='#') && nSql
==0 ){
27468 CONTINUE_PROMPT_RESET
;
27469 echo_group_input(p
, zLine
);
27470 if( zLine
[0]=='.' ){
27471 rc
= do_meta_command(zLine
, p
);
27472 if( rc
==2 ){ /* exit requested */
27481 /* No single-line dispositions remain; accumulate line(s). */
27482 nLine
= strlen(zLine
);
27483 if( nSql
+nLine
+2>=nAlloc
){
27484 /* Grow buffer by half-again increments when big. */
27485 nAlloc
= nSql
+(nSql
>>1)+nLine
+100;
27486 zSql
= realloc(zSql
, nAlloc
);
27487 shell_check_oom(zSql
);
27491 for(i
=0; zLine
[i
] && IsSpace(zLine
[i
]); i
++){}
27492 assert( nAlloc
>0 && zSql
!=0 );
27493 memcpy(zSql
, zLine
+i
, nLine
+1-i
);
27494 startline
= p
->lineno
;
27497 zSql
[nSql
++] = '\n';
27498 memcpy(zSql
+nSql
, zLine
, nLine
+1);
27501 if( nSql
&& QSS_SEMITERM(qss
) && sqlite3_complete(zSql
) ){
27502 echo_group_input(p
, zSql
);
27503 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
27504 CONTINUE_PROMPT_RESET
;
27512 p
->bSafeMode
= p
->bSafeModePersist
;
27514 }else if( nSql
&& QSS_PLAINWHITE(qss
) ){
27515 echo_group_input(p
, zSql
);
27521 /* This may be incomplete. Let the SQL parser deal with that. */
27522 echo_group_input(p
, zSql
);
27523 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
27524 CONTINUE_PROMPT_RESET
;
27533 ** Return a pathname which is the user's home directory. A
27534 ** 0 return indicates an error of some kind.
27536 static char *find_home_dir(int clearFlag
){
27537 static char *home_dir
= NULL
;
27543 if( home_dir
) return home_dir
;
27545 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
27546 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
27548 struct passwd
*pwent
;
27549 uid_t uid
= getuid();
27550 if( (pwent
=getpwuid(uid
)) != NULL
) {
27551 home_dir
= pwent
->pw_dir
;
27556 #if defined(_WIN32_WCE)
27557 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
27562 #if defined(_WIN32) || defined(WIN32)
27564 home_dir
= getenv("USERPROFILE");
27569 home_dir
= getenv("HOME");
27572 #if defined(_WIN32) || defined(WIN32)
27574 char *zDrive
, *zPath
;
27576 zDrive
= getenv("HOMEDRIVE");
27577 zPath
= getenv("HOMEPATH");
27578 if( zDrive
&& zPath
){
27579 n
= strlen30(zDrive
) + strlen30(zPath
) + 1;
27580 home_dir
= malloc( n
);
27581 if( home_dir
==0 ) return 0;
27582 sqlite3_snprintf(n
, home_dir
, "%s%s", zDrive
, zPath
);
27589 #endif /* !_WIN32_WCE */
27592 i64 n
= strlen(home_dir
) + 1;
27593 char *z
= malloc( n
);
27594 if( z
) memcpy(z
, home_dir
, n
);
27602 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
27603 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
27604 ** the path to it, else return 0. The result is cached for
27605 ** subsequent calls.
27607 static const char *find_xdg_config(void){
27608 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
27609 || defined(__RTP__) || defined(_WRS_KERNEL)
27612 static int alreadyTried
= 0;
27613 static char *zConfig
= 0;
27614 const char *zXdgHome
;
27616 if( alreadyTried
!=0 ){
27620 zXdgHome
= getenv("XDG_CONFIG_HOME");
27624 zConfig
= sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome
);
27625 shell_check_oom(zConfig
);
27626 if( access(zConfig
,0)!=0 ){
27627 sqlite3_free(zConfig
);
27635 ** Read input from the file given by sqliterc_override. Or if that
27636 ** parameter is NULL, take input from the first of find_xdg_config()
27637 ** or ~/.sqliterc which is found.
27639 ** Returns the number of errors.
27641 static void process_sqliterc(
27642 ShellState
*p
, /* Configuration data */
27643 const char *sqliterc_override
/* Name of config file. NULL to use default */
27645 char *home_dir
= NULL
;
27646 const char *sqliterc
= sqliterc_override
;
27648 FILE *inSaved
= p
->in
;
27649 int savedLineno
= p
->lineno
;
27651 if( sqliterc
== NULL
){
27652 sqliterc
= find_xdg_config();
27654 if( sqliterc
== NULL
){
27655 home_dir
= find_home_dir(0);
27657 raw_printf(stderr
, "-- warning: cannot find home directory;"
27658 " cannot read ~/.sqliterc\n");
27661 zBuf
= sqlite3_mprintf("%s/.sqliterc",home_dir
);
27662 shell_check_oom(zBuf
);
27665 p
->in
= fopen(sqliterc
,"rb");
27667 if( stdin_is_interactive
){
27668 utf8_printf(stderr
,"-- Loading resources from %s\n",sqliterc
);
27670 if( process_input(p
) && bail_on_error
) exit(1);
27672 }else if( sqliterc_override
!=0 ){
27673 utf8_printf(stderr
,"cannot open: \"%s\"\n", sqliterc
);
27674 if( bail_on_error
) exit(1);
27677 p
->lineno
= savedLineno
;
27678 sqlite3_free(zBuf
);
27682 ** Show available command line options
27684 static const char zOptions
[] =
27685 " -- treat no subsequent arguments as options\n"
27686 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27687 " -A ARGS... run \".archive ARGS\" and exit\n"
27689 " -append append the database to the end of the file\n"
27690 " -ascii set output mode to 'ascii'\n"
27691 " -bail stop after hitting an error\n"
27692 " -batch force batch I/O\n"
27693 " -box set output mode to 'box'\n"
27694 " -column set output mode to 'column'\n"
27695 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
27696 " -csv set output mode to 'csv'\n"
27697 #if !defined(SQLITE_OMIT_DESERIALIZE)
27698 " -deserialize open the database using sqlite3_deserialize()\n"
27700 " -echo print inputs before execution\n"
27701 " -init FILENAME read/process named file\n"
27702 " -[no]header turn headers on or off\n"
27703 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
27704 " -heap SIZE Size of heap for memsys3 or memsys5\n"
27706 " -help show this message\n"
27707 " -html set output mode to HTML\n"
27708 " -interactive force interactive I/O\n"
27709 " -json set output mode to 'json'\n"
27710 " -line set output mode to 'line'\n"
27711 " -list set output mode to 'list'\n"
27712 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
27713 " -markdown set output mode to 'markdown'\n"
27714 #if !defined(SQLITE_OMIT_DESERIALIZE)
27715 " -maxsize N maximum size for a --deserialize database\n"
27717 " -memtrace trace all memory allocations and deallocations\n"
27718 " -mmap N default mmap size set to N\n"
27719 #ifdef SQLITE_ENABLE_MULTIPLEX
27720 " -multiplex enable the multiplexor VFS\n"
27722 " -newline SEP set output row separator. Default: '\\n'\n"
27723 " -nofollow refuse to open symbolic links to database files\n"
27724 " -nonce STRING set the safe-mode escape nonce\n"
27725 " -nullvalue TEXT set text string for NULL values. Default ''\n"
27726 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
27727 " -pcachetrace trace all page cache operations\n"
27728 " -quote set output mode to 'quote'\n"
27729 " -readonly open the database read-only\n"
27730 " -safe enable safe-mode\n"
27731 " -separator SEP set output column separator. Default: '|'\n"
27732 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
27733 " -sorterref SIZE sorter references threshold size\n"
27735 " -stats print memory stats before each finalize\n"
27736 " -table set output mode to 'table'\n"
27737 " -tabs set output mode to 'tabs'\n"
27738 " -unsafe-testing allow unsafe commands and modes for testing\n"
27739 #if SHELL_WIN_UTF8_OPT
27740 " -utf8 setup interactive console code page for UTF-8\n"
27742 " -version show SQLite version\n"
27743 " -vfs NAME use NAME as the default VFS\n"
27744 #ifdef SQLITE_ENABLE_VFSTRACE
27745 " -vfstrace enable tracing of all VFS calls\n"
27747 #ifdef SQLITE_HAVE_ZLIB
27748 " -zip open the file as a ZIP Archive\n"
27751 static void usage(int showDetail
){
27752 utf8_printf(stderr
,
27753 "Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
27754 "FILENAME is the name of an SQLite database. A new database is created\n"
27755 "if the file does not previously exist. Defaults to :memory:.\n", Argv0
);
27757 utf8_printf(stderr
, "OPTIONS include:\n%s", zOptions
);
27759 raw_printf(stderr
, "Use the -help option for additional information\n");
27765 ** Internal check: Verify that the SQLite is uninitialized. Print a
27766 ** error message if it is initialized.
27768 static void verify_uninitialized(void){
27769 if( sqlite3_config(-1)==SQLITE_MISUSE
){
27770 utf8_printf(stdout
, "WARNING: attempt to configure SQLite after"
27771 " initialization.\n");
27776 ** Initialize the state information in data
27778 static void main_init(ShellState
*data
) {
27779 memset(data
, 0, sizeof(*data
));
27780 data
->normalMode
= data
->cMode
= data
->mode
= MODE_List
;
27781 data
->autoExplain
= 1;
27782 data
->pAuxDb
= &data
->aAuxDb
[0];
27783 memcpy(data
->colSeparator
,SEP_Column
, 2);
27784 memcpy(data
->rowSeparator
,SEP_Row
, 2);
27785 data
->showHeader
= 0;
27786 data
->shellFlgs
= SHFLG_Lookaside
;
27787 sqlite3_config(SQLITE_CONFIG_LOG
, shellLog
, data
);
27788 #if !defined(SQLITE_SHELL_FIDDLE)
27789 verify_uninitialized();
27791 sqlite3_config(SQLITE_CONFIG_URI
, 1);
27792 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
27793 sqlite3_snprintf(sizeof(mainPrompt
), mainPrompt
,"sqlite> ");
27794 sqlite3_snprintf(sizeof(continuePrompt
), continuePrompt
," ...> ");
27798 ** Output text to the console in a font that attracts extra attention.
27801 static void printBold(const char *zText
){
27802 #if !SQLITE_OS_WINRT
27803 HANDLE out
= GetStdHandle(STD_OUTPUT_HANDLE
);
27804 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo
;
27805 GetConsoleScreenBufferInfo(out
, &defaultScreenInfo
);
27806 SetConsoleTextAttribute(out
,
27807 FOREGROUND_RED
|FOREGROUND_INTENSITY
27810 printf("%s", zText
);
27811 #if !SQLITE_OS_WINRT
27812 SetConsoleTextAttribute(out
, defaultScreenInfo
.wAttributes
);
27816 static void printBold(const char *zText
){
27817 printf("\033[1m%s\033[0m", zText
);
27822 ** Get the argument to an --option. Throw an error and die if no argument
27825 static char *cmdline_option_value(int argc
, char **argv
, int i
){
27827 utf8_printf(stderr
, "%s: Error: missing argument to %s\n",
27828 argv
[0], argv
[argc
-1]);
27834 static void sayAbnormalExit(void){
27835 if( seenInterrupt
) fprintf(stderr
, "Program interrupted.\n");
27838 #ifndef SQLITE_SHELL_IS_UTF8
27839 # if (defined(_WIN32) || defined(WIN32)) \
27840 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
27841 # define SQLITE_SHELL_IS_UTF8 (0)
27843 # define SQLITE_SHELL_IS_UTF8 (1)
27847 #ifdef SQLITE_SHELL_FIDDLE
27848 # define main fiddle_main
27851 #if SQLITE_SHELL_IS_UTF8
27852 int SQLITE_CDECL
main(int argc
, char **argv
){
27854 int SQLITE_CDECL
wmain(int argc
, wchar_t **wargv
){
27857 #ifdef SQLITE_DEBUG
27858 sqlite3_int64 mem_main_enter
= 0;
27861 #ifdef SQLITE_SHELL_FIDDLE
27862 # define data shellState
27866 const char *zInitFile
= 0;
27869 int warnInmemoryDb
= 0;
27872 int nOptsEnd
= argc
;
27874 const char *zVfs
= 0; /* Value of -vfs command-line option */
27875 #if !SQLITE_SHELL_IS_UTF8
27876 char **argvToFree
= 0;
27877 int argcToFree
= 0;
27879 setvbuf(stderr
, 0, _IONBF
, 0); /* Make sure stderr is unbuffered */
27881 #ifdef SQLITE_SHELL_FIDDLE
27882 stdin_is_interactive
= 0;
27883 stdout_is_console
= 1;
27884 data
.wasm
.zDefaultDbName
= "/fiddle.sqlite3";
27886 stdin_is_interactive
= isatty(0);
27887 stdout_is_console
= isatty(1);
27889 #if SHELL_WIN_UTF8_OPT
27890 atexit(console_restore
); /* Needs revision for CLI as library call */
27892 atexit(sayAbnormalExit
);
27893 #ifdef SQLITE_DEBUG
27894 mem_main_enter
= sqlite3_memory_used();
27896 #if !defined(_WIN32_WCE)
27897 if( getenv("SQLITE_DEBUG_BREAK") ){
27898 if( isatty(0) && isatty(2) ){
27900 "attach debugger to process %d and press any key to continue.\n",
27904 #if defined(_WIN32) || defined(WIN32)
27905 #if SQLITE_OS_WINRT
27910 #elif defined(SIGTRAP)
27916 /* Register a valid signal handler early, before much else is done. */
27918 signal(SIGINT
, interrupt_handler
);
27919 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
27920 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
) ){
27921 fprintf(stderr
, "No ^C handler.\n");
27925 #if USE_SYSTEM_SQLITE+0!=1
27926 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID
,60)!=0 ){
27927 utf8_printf(stderr
, "SQLite header and source version mismatch\n%s\n%s\n",
27928 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
27934 /* On Windows, we must translate command-line arguments into UTF-8.
27935 ** The SQLite memory allocator subsystem has to be enabled in order to
27936 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
27937 ** subsequent sqlite3_config() calls will work. So copy all results into
27938 ** memory that does not come from the SQLite memory allocator.
27940 #if !SQLITE_SHELL_IS_UTF8
27941 sqlite3_initialize();
27942 argvToFree
= malloc(sizeof(argv
[0])*argc
*2);
27943 shell_check_oom(argvToFree
);
27945 argv
= argvToFree
+ argc
;
27946 for(i
=0; i
<argc
; i
++){
27947 char *z
= sqlite3_win32_unicode_to_utf8(wargv
[i
]);
27949 shell_check_oom(z
);
27951 argv
[i
] = malloc( n
+1 );
27952 shell_check_oom(argv
[i
]);
27953 memcpy(argv
[i
], z
, n
+1);
27954 argvToFree
[i
] = argv
[i
];
27957 sqlite3_shutdown();
27960 assert( argc
>=1 && argv
&& argv
[0] );
27963 #ifdef SQLITE_SHELL_DBNAME_PROC
27965 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
27966 ** of a C-function that will provide the name of the database file. Use
27967 ** this compile-time option to embed this shell program in larger
27968 ** applications. */
27969 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
27970 SQLITE_SHELL_DBNAME_PROC(&data
.pAuxDb
->zDbFilename
);
27971 warnInmemoryDb
= 0;
27975 /* Do an initial pass through the command-line argument to locate
27976 ** the name of the database file, the name of the initialization file,
27977 ** the size of the alternative malloc heap,
27978 ** and the first command to execute.
27980 #ifndef SQLITE_SHELL_FIDDLE
27981 verify_uninitialized();
27983 for(i
=1; i
<argc
; i
++){
27986 if( z
[0]!='-' || i
>nOptsEnd
){
27987 if( data
.aAuxDb
->zDbFilename
==0 ){
27988 data
.aAuxDb
->zDbFilename
= z
;
27990 /* Excess arguments are interpreted as SQL (or dot-commands) and
27991 ** mean that nothing is read from stdin */
27994 azCmd
= realloc(azCmd
, sizeof(azCmd
[0])*nCmd
);
27995 shell_check_oom(azCmd
);
28000 if( z
[1]=='-' ) z
++;
28001 if( cli_strcmp(z
, "-")==0 ){
28004 }else if( cli_strcmp(z
,"-separator")==0
28005 || cli_strcmp(z
,"-nullvalue")==0
28006 || cli_strcmp(z
,"-newline")==0
28007 || cli_strcmp(z
,"-cmd")==0
28009 (void)cmdline_option_value(argc
, argv
, ++i
);
28010 }else if( cli_strcmp(z
,"-init")==0 ){
28011 zInitFile
= cmdline_option_value(argc
, argv
, ++i
);
28012 }else if( cli_strcmp(z
,"-batch")==0 ){
28013 /* Need to check for batch mode here to so we can avoid printing
28014 ** informational messages (like from process_sqliterc) before
28015 ** we do the actual processing of arguments later in a second pass.
28017 stdin_is_interactive
= 0;
28018 }else if( cli_strcmp(z
,"-heap")==0 ){
28019 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
28021 sqlite3_int64 szHeap
;
28023 zSize
= cmdline_option_value(argc
, argv
, ++i
);
28024 szHeap
= integerValue(zSize
);
28025 if( szHeap
>0x7fff0000 ) szHeap
= 0x7fff0000;
28026 verify_uninitialized();
28027 sqlite3_config(SQLITE_CONFIG_HEAP
, malloc((int)szHeap
), (int)szHeap
, 64);
28029 (void)cmdline_option_value(argc
, argv
, ++i
);
28031 }else if( cli_strcmp(z
,"-pagecache")==0 ){
28032 sqlite3_int64 n
, sz
;
28033 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
28034 if( sz
>70000 ) sz
= 70000;
28036 n
= integerValue(cmdline_option_value(argc
,argv
,++i
));
28037 if( sz
>0 && n
>0 && 0xffffffffffffLL
/sz
<n
){
28038 n
= 0xffffffffffffLL
/sz
;
28040 verify_uninitialized();
28041 sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
28042 (n
>0 && sz
>0) ? malloc(n
*sz
) : 0, sz
, n
);
28043 data
.shellFlgs
|= SHFLG_Pagecache
;
28044 }else if( cli_strcmp(z
,"-lookaside")==0 ){
28046 sz
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
28048 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
28050 verify_uninitialized();
28051 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, n
);
28052 if( sz
*n
==0 ) data
.shellFlgs
&= ~SHFLG_Lookaside
;
28053 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
28055 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
28056 verify_uninitialized();
28058 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD
); break;
28059 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD
); break;
28060 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED
); break;
28062 #ifdef SQLITE_ENABLE_VFSTRACE
28063 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
28064 extern int vfstrace_register(
28065 const char *zTraceName
,
28066 const char *zOldVfsName
,
28067 int (*xOut
)(const char*,void*),
28071 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs
,stderr
,1);
28073 #ifdef SQLITE_ENABLE_MULTIPLEX
28074 }else if( cli_strcmp(z
,"-multiplex")==0 ){
28075 extern int sqlite3_multiplex_initialize(const char*,int);
28076 sqlite3_multiplex_initialize(0, 1);
28078 }else if( cli_strcmp(z
,"-mmap")==0 ){
28079 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
28080 verify_uninitialized();
28081 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE
, sz
, sz
);
28082 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
28083 }else if( cli_strcmp(z
,"-sorterref")==0 ){
28084 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
28085 verify_uninitialized();
28086 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE
, (int)sz
);
28088 }else if( cli_strcmp(z
,"-vfs")==0 ){
28089 zVfs
= cmdline_option_value(argc
, argv
, ++i
);
28090 #ifdef SQLITE_HAVE_ZLIB
28091 }else if( cli_strcmp(z
,"-zip")==0 ){
28092 data
.openMode
= SHELL_OPEN_ZIPFILE
;
28094 }else if( cli_strcmp(z
,"-append")==0 ){
28095 data
.openMode
= SHELL_OPEN_APPENDVFS
;
28096 #ifndef SQLITE_OMIT_DESERIALIZE
28097 }else if( cli_strcmp(z
,"-deserialize")==0 ){
28098 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
28099 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
28100 data
.szMax
= integerValue(argv
[++i
]);
28102 }else if( cli_strcmp(z
,"-readonly")==0 ){
28103 data
.openMode
= SHELL_OPEN_READONLY
;
28104 }else if( cli_strcmp(z
,"-nofollow")==0 ){
28105 data
.openFlags
= SQLITE_OPEN_NOFOLLOW
;
28106 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
28107 }else if( cli_strncmp(z
, "-A",2)==0 ){
28108 /* All remaining command-line arguments are passed to the ".archive"
28109 ** command, so ignore them */
28112 }else if( cli_strcmp(z
, "-memtrace")==0 ){
28113 sqlite3MemTraceActivate(stderr
);
28114 }else if( cli_strcmp(z
, "-pcachetrace")==0 ){
28115 sqlite3PcacheTraceActivate(stderr
);
28116 }else if( cli_strcmp(z
,"-bail")==0 ){
28118 }else if( cli_strcmp(z
,"-nonce")==0 ){
28120 data
.zNonce
= strdup(cmdline_option_value(argc
, argv
, ++i
));
28121 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
28122 ShellSetFlag(&data
,SHFLG_TestingMode
);
28123 }else if( cli_strcmp(z
,"-safe")==0 ){
28124 /* no-op - catch this on the second pass */
28127 #ifndef SQLITE_SHELL_FIDDLE
28128 verify_uninitialized();
28132 #ifdef SQLITE_SHELL_INIT_PROC
28134 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
28135 ** of a C-function that will perform initialization actions on SQLite that
28136 ** occur just before or after sqlite3_initialize(). Use this compile-time
28137 ** option to embed this shell program in larger applications. */
28138 extern void SQLITE_SHELL_INIT_PROC(void);
28139 SQLITE_SHELL_INIT_PROC();
28142 /* All the sqlite3_config() calls have now been made. So it is safe
28143 ** to call sqlite3_initialize() and process any command line -vfs option. */
28144 sqlite3_initialize();
28148 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zVfs
);
28150 sqlite3_vfs_register(pVfs
, 1);
28152 utf8_printf(stderr
, "no such VFS: \"%s\"\n", zVfs
);
28157 if( data
.pAuxDb
->zDbFilename
==0 ){
28158 #ifndef SQLITE_OMIT_MEMORYDB
28159 data
.pAuxDb
->zDbFilename
= ":memory:";
28160 warnInmemoryDb
= argc
==1;
28162 utf8_printf(stderr
,"%s: Error: no database filename specified\n", Argv0
);
28167 #ifndef SQLITE_SHELL_FIDDLE
28168 sqlite3_appendvfs_init(0,0,0);
28171 /* Go ahead and open the database file if it already exists. If the
28172 ** file does not exist, delay opening it. This prevents empty database
28173 ** files from being created if a user mistypes the database name argument
28174 ** to the sqlite command-line tool.
28176 if( access(data
.pAuxDb
->zDbFilename
, 0)==0 ){
28180 /* Process the initialization file if there is one. If no -init option
28181 ** is given on the command line, look for a file named ~/.sqliterc and
28182 ** try to process it.
28184 process_sqliterc(&data
,zInitFile
);
28186 /* Make a second pass through the command-line argument and set
28187 ** options. This second pass is delayed until after the initialization
28188 ** file is processed so that the command-line arguments will override
28189 ** settings in the initialization file.
28191 for(i
=1; i
<argc
; i
++){
28193 if( z
[0]!='-' || i
>=nOptsEnd
) continue;
28194 if( z
[1]=='-' ){ z
++; }
28195 if( cli_strcmp(z
,"-init")==0 ){
28197 }else if( cli_strcmp(z
,"-html")==0 ){
28198 data
.mode
= MODE_Html
;
28199 }else if( cli_strcmp(z
,"-list")==0 ){
28200 data
.mode
= MODE_List
;
28201 }else if( cli_strcmp(z
,"-quote")==0 ){
28202 data
.mode
= MODE_Quote
;
28203 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
, SEP_Comma
);
28204 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
, SEP_Row
);
28205 }else if( cli_strcmp(z
,"-line")==0 ){
28206 data
.mode
= MODE_Line
;
28207 }else if( cli_strcmp(z
,"-column")==0 ){
28208 data
.mode
= MODE_Column
;
28209 }else if( cli_strcmp(z
,"-json")==0 ){
28210 data
.mode
= MODE_Json
;
28211 }else if( cli_strcmp(z
,"-markdown")==0 ){
28212 data
.mode
= MODE_Markdown
;
28213 }else if( cli_strcmp(z
,"-table")==0 ){
28214 data
.mode
= MODE_Table
;
28215 }else if( cli_strcmp(z
,"-box")==0 ){
28216 data
.mode
= MODE_Box
;
28217 }else if( cli_strcmp(z
,"-csv")==0 ){
28218 data
.mode
= MODE_Csv
;
28219 memcpy(data
.colSeparator
,",",2);
28220 #ifdef SQLITE_HAVE_ZLIB
28221 }else if( cli_strcmp(z
,"-zip")==0 ){
28222 data
.openMode
= SHELL_OPEN_ZIPFILE
;
28224 }else if( cli_strcmp(z
,"-append")==0 ){
28225 data
.openMode
= SHELL_OPEN_APPENDVFS
;
28226 #ifndef SQLITE_OMIT_DESERIALIZE
28227 }else if( cli_strcmp(z
,"-deserialize")==0 ){
28228 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
28229 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
28230 data
.szMax
= integerValue(argv
[++i
]);
28232 }else if( cli_strcmp(z
,"-readonly")==0 ){
28233 data
.openMode
= SHELL_OPEN_READONLY
;
28234 }else if( cli_strcmp(z
,"-nofollow")==0 ){
28235 data
.openFlags
|= SQLITE_OPEN_NOFOLLOW
;
28236 }else if( cli_strcmp(z
,"-ascii")==0 ){
28237 data
.mode
= MODE_Ascii
;
28238 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Unit
);
28239 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Record
);
28240 }else if( cli_strcmp(z
,"-tabs")==0 ){
28241 data
.mode
= MODE_List
;
28242 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Tab
);
28243 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Row
);
28244 }else if( cli_strcmp(z
,"-separator")==0 ){
28245 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,
28246 "%s",cmdline_option_value(argc
,argv
,++i
));
28247 }else if( cli_strcmp(z
,"-newline")==0 ){
28248 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,
28249 "%s",cmdline_option_value(argc
,argv
,++i
));
28250 }else if( cli_strcmp(z
,"-nullvalue")==0 ){
28251 sqlite3_snprintf(sizeof(data
.nullValue
), data
.nullValue
,
28252 "%s",cmdline_option_value(argc
,argv
,++i
));
28253 }else if( cli_strcmp(z
,"-header")==0 ){
28254 data
.showHeader
= 1;
28255 ShellSetFlag(&data
, SHFLG_HeaderSet
);
28256 }else if( cli_strcmp(z
,"-noheader")==0 ){
28257 data
.showHeader
= 0;
28258 ShellSetFlag(&data
, SHFLG_HeaderSet
);
28259 }else if( cli_strcmp(z
,"-echo")==0 ){
28260 ShellSetFlag(&data
, SHFLG_Echo
);
28261 }else if( cli_strcmp(z
,"-eqp")==0 ){
28262 data
.autoEQP
= AUTOEQP_on
;
28263 }else if( cli_strcmp(z
,"-eqpfull")==0 ){
28264 data
.autoEQP
= AUTOEQP_full
;
28265 }else if( cli_strcmp(z
,"-stats")==0 ){
28267 }else if( cli_strcmp(z
,"-scanstats")==0 ){
28268 data
.scanstatsOn
= 1;
28269 }else if( cli_strcmp(z
,"-backslash")==0 ){
28270 /* Undocumented command-line option: -backslash
28271 ** Causes C-style backslash escapes to be evaluated in SQL statements
28272 ** prior to sending the SQL into SQLite. Useful for injecting
28273 ** crazy bytes in the middle of SQL statements for testing and debugging.
28275 ShellSetFlag(&data
, SHFLG_Backslash
);
28276 }else if( cli_strcmp(z
,"-bail")==0 ){
28277 /* No-op. The bail_on_error flag should already be set. */
28278 }else if( cli_strcmp(z
,"-version")==0 ){
28279 printf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
28280 8*(int)sizeof(char*));
28282 }else if( cli_strcmp(z
,"-interactive")==0 ){
28283 stdin_is_interactive
= 1;
28284 }else if( cli_strcmp(z
,"-batch")==0 ){
28285 stdin_is_interactive
= 0;
28286 }else if( cli_strcmp(z
,"-utf8")==0 ){
28287 #if SHELL_WIN_UTF8_OPT
28289 #endif /* SHELL_WIN_UTF8_OPT */
28290 }else if( cli_strcmp(z
,"-heap")==0 ){
28292 }else if( cli_strcmp(z
,"-pagecache")==0 ){
28294 }else if( cli_strcmp(z
,"-lookaside")==0 ){
28296 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
28298 }else if( cli_strcmp(z
,"-nonce")==0 ){
28300 }else if( cli_strcmp(z
,"-mmap")==0 ){
28302 }else if( cli_strcmp(z
,"-memtrace")==0 ){
28304 }else if( cli_strcmp(z
,"-pcachetrace")==0 ){
28306 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
28307 }else if( cli_strcmp(z
,"-sorterref")==0 ){
28310 }else if( cli_strcmp(z
,"-vfs")==0 ){
28312 #ifdef SQLITE_ENABLE_VFSTRACE
28313 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
28316 #ifdef SQLITE_ENABLE_MULTIPLEX
28317 }else if( cli_strcmp(z
,"-multiplex")==0 ){
28320 }else if( cli_strcmp(z
,"-help")==0 ){
28322 }else if( cli_strcmp(z
,"-cmd")==0 ){
28323 /* Run commands that follow -cmd first and separately from commands
28324 ** that simply appear on the command-line. This seems goofy. It would
28325 ** be better if all commands ran in the order that they appear. But
28326 ** we retain the goofy behavior for historical compatibility. */
28327 if( i
==argc
-1 ) break;
28328 z
= cmdline_option_value(argc
,argv
,++i
);
28330 rc
= do_meta_command(z
, &data
);
28331 if( rc
&& bail_on_error
) return rc
==2 ? 0 : rc
;
28334 rc
= shell_exec(&data
, z
, &zErrMsg
);
28336 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
28337 if( bail_on_error
) return rc
!=0 ? rc
: 1;
28339 utf8_printf(stderr
,"Error: unable to process SQL \"%s\"\n", z
);
28340 if( bail_on_error
) return rc
;
28343 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
28344 }else if( cli_strncmp(z
, "-A", 2)==0 ){
28346 utf8_printf(stderr
, "Error: cannot mix regular SQL or dot-commands"
28347 " with \"%s\"\n", z
);
28350 open_db(&data
, OPEN_DB_ZIPFILE
);
28353 arDotCommand(&data
, 1, argv
+(i
-1), argc
-(i
-1));
28355 arDotCommand(&data
, 1, argv
+i
, argc
-i
);
28360 }else if( cli_strcmp(z
,"-safe")==0 ){
28361 data
.bSafeMode
= data
.bSafeModePersist
= 1;
28362 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
28363 /* Acted upon in first pass. */
28365 utf8_printf(stderr
,"%s: Error: unknown option: %s\n", Argv0
, z
);
28366 raw_printf(stderr
,"Use -help for a list of options.\n");
28369 data
.cMode
= data
.mode
;
28371 #if SHELL_WIN_UTF8_OPT
28372 if( console_utf8
&& stdin_is_interactive
){
28375 setBinaryMode(stdin
, 0);
28381 /* Run all arguments that do not begin with '-' as if they were separate
28382 ** command-line inputs, except for the argToSkip argument which contains
28383 ** the database filename.
28385 for(i
=0; i
<nCmd
; i
++){
28386 if( azCmd
[i
][0]=='.' ){
28387 rc
= do_meta_command(azCmd
[i
], &data
);
28390 return rc
==2 ? 0 : rc
;
28394 echo_group_input(&data
, azCmd
[i
]);
28395 rc
= shell_exec(&data
, azCmd
[i
], &zErrMsg
);
28396 if( zErrMsg
|| rc
){
28398 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
28400 utf8_printf(stderr
,"Error: unable to process SQL: %s\n", azCmd
[i
]);
28402 sqlite3_free(zErrMsg
);
28404 return rc
!=0 ? rc
: 1;
28409 /* Run commands received from standard input
28411 if( stdin_is_interactive
){
28416 "SQLite version %s %.19s\n" /*extra-version-info*/
28417 "Enter \".help\" for usage hints.\n",
28418 sqlite3_libversion(), sqlite3_sourceid()
28420 if( warnInmemoryDb
){
28421 printf("Connected to a ");
28422 printBold("transient in-memory database");
28423 printf(".\nUse \".open FILENAME\" to reopen on a "
28424 "persistent database.\n");
28426 zHistory
= getenv("SQLITE_HISTORY");
28428 zHistory
= strdup(zHistory
);
28429 }else if( (zHome
= find_home_dir(0))!=0 ){
28430 nHistory
= strlen30(zHome
) + 20;
28431 if( (zHistory
= malloc(nHistory
))!=0 ){
28432 sqlite3_snprintf(nHistory
, zHistory
,"%s/.sqlite_history", zHome
);
28435 if( zHistory
){ shell_read_history(zHistory
); }
28436 #if HAVE_READLINE || HAVE_EDITLINE
28437 rl_attempted_completion_function
= readline_completion
;
28438 #elif HAVE_LINENOISE
28439 linenoiseSetCompletionCallback(linenoise_completion
);
28442 rc
= process_input(&data
);
28444 shell_stifle_history(2000);
28445 shell_write_history(zHistory
);
28450 rc
= process_input(&data
);
28453 #ifndef SQLITE_SHELL_FIDDLE
28454 /* In WASM mode we have to leave the db state in place so that
28455 ** client code can "push" SQL into it after this call returns. */
28457 set_table_name(&data
, 0);
28459 session_close_all(&data
, -1);
28462 for(i
=0; i
<ArraySize(data
.aAuxDb
); i
++){
28463 sqlite3_free(data
.aAuxDb
[i
].zFreeOnClose
);
28464 if( data
.aAuxDb
[i
].db
){
28465 session_close_all(&data
, i
);
28466 close_db(data
.aAuxDb
[i
].db
);
28470 output_reset(&data
);
28471 data
.doXdgOpen
= 0;
28472 clearTempFile(&data
);
28473 #if !SQLITE_SHELL_IS_UTF8
28474 for(i
=0; i
<argcToFree
; i
++) free(argvToFree
[i
]);
28477 free(data
.colWidth
);
28479 /* Clear the global data structure so that valgrind will detect memory
28481 memset(&data
, 0, sizeof(data
));
28482 #ifdef SQLITE_DEBUG
28483 if( sqlite3_memory_used()>mem_main_enter
){
28484 utf8_printf(stderr
, "Memory leaked: %u bytes\n",
28485 (unsigned int)(sqlite3_memory_used()-mem_main_enter
));
28488 #endif /* !SQLITE_SHELL_FIDDLE */
28493 #ifdef SQLITE_SHELL_FIDDLE
28494 /* Only for emcc experimentation purposes. */
28495 int fiddle_experiment(int a
,int b
){
28500 ** Returns a pointer to the current DB handle.
28502 sqlite3
* fiddle_db_handle(){
28507 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
28508 ** "main" is assumed. Returns 0 if no db with the given name is
28511 sqlite3_vfs
* fiddle_db_vfs(const char *zDbName
){
28512 sqlite3_vfs
* pVfs
= 0;
28514 sqlite3_file_control(globalDb
, zDbName
? zDbName
: "main",
28515 SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
28520 /* Only for emcc experimentation purposes. */
28521 sqlite3
* fiddle_db_arg(sqlite3
*arg
){
28522 printf("fiddle_db_arg(%p)\n", (const void*)arg
);
28527 ** Intended to be called via a SharedWorker() while a separate
28528 ** SharedWorker() (which manages the wasm module) is performing work
28529 ** which should be interrupted. Unfortunately, SharedWorker is not
28530 ** portable enough to make real use of.
28532 void fiddle_interrupt(void){
28533 if( globalDb
) sqlite3_interrupt(globalDb
);
28537 ** Returns the filename of the given db name, assuming "main" if
28538 ** zDbName is NULL. Returns NULL if globalDb is not opened.
28540 const char * fiddle_db_filename(const char * zDbName
){
28542 ? sqlite3_db_filename(globalDb
, zDbName
? zDbName
: "main")
28547 ** Completely wipes out the contents of the currently-opened database
28548 ** but leaves its storage intact for reuse.
28550 void fiddle_reset_db(void){
28552 int rc
= sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 1, 0);
28553 if( 0==rc
) rc
= sqlite3_exec(globalDb
, "VACUUM", 0, 0, 0);
28554 sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 0, 0);
28559 ** Uses the current database's VFS xRead to stream the db file's
28560 ** contents out to the given callback. The callback gets a single
28561 ** chunk of size n (its 2nd argument) on each call and must return 0
28562 ** on success, non-0 on error. This function returns 0 on success,
28563 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
28564 ** code from the callback. Note that this is not thread-friendly: it
28565 ** expects that it will be the only thread reading the db file and
28566 ** takes no measures to ensure that is the case.
28568 int fiddle_export_db( int (*xCallback
)(unsigned const char *zOut
, int n
) ){
28569 sqlite3_int64 nSize
= 0;
28570 sqlite3_int64 nPos
= 0;
28571 sqlite3_file
* pFile
= 0;
28572 unsigned char buf
[1024 * 8];
28573 int nBuf
= (int)sizeof(buf
);
28574 int rc
= shellState
.db
28575 ? sqlite3_file_control(shellState
.db
, "main",
28576 SQLITE_FCNTL_FILE_POINTER
, &pFile
)
28578 if( rc
) return rc
;
28579 rc
= pFile
->pMethods
->xFileSize(pFile
, &nSize
);
28580 if( rc
) return rc
;
28582 /* DB size is not an even multiple of the buffer size. Reduce
28583 ** buffer size so that we do not unduly inflate the db size when
28585 if(0 == nSize
% 4096) nBuf
= 4096;
28586 else if(0 == nSize
% 2048) nBuf
= 2048;
28587 else if(0 == nSize
% 1024) nBuf
= 1024;
28590 for( ; 0==rc
&& nPos
<nSize
; nPos
+= nBuf
){
28591 rc
= pFile
->pMethods
->xRead(pFile
, buf
, nBuf
, nPos
);
28592 if(SQLITE_IOERR_SHORT_READ
== rc
){
28593 rc
= (nPos
+ nBuf
) < nSize
? rc
: 0/*assume EOF*/;
28595 if( 0==rc
) rc
= xCallback(buf
, nBuf
);
28601 ** Trivial exportable function for emscripten. It processes zSql as if
28602 ** it were input to the sqlite3 shell and redirects all output to the
28603 ** wasm binding. fiddle_main() must have been called before this
28604 ** is called, or results are undefined.
28606 void fiddle_exec(const char * zSql
){
28608 if('.'==*zSql
) puts(zSql
);
28609 shellState
.wasm
.zInput
= zSql
;
28610 shellState
.wasm
.zPos
= zSql
;
28611 process_input(&shellState
);
28612 shellState
.wasm
.zInput
= shellState
.wasm
.zPos
= 0;
28615 #endif /* SQLITE_SHELL_FIDDLE */