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 LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
258 /* Use console I/O package as a direct INCLUDE. */
259 #define SQLITE_INTERNAL_LINKAGE static
261 #ifdef SQLITE_SHELL_FIDDLE
262 /* Deselect most features from the console I/O package for Fiddle. */
263 # define SQLITE_CIO_NO_REDIRECT
264 # define SQLITE_CIO_NO_CLASSIFY
265 # define SQLITE_CIO_NO_TRANSLATE
266 # define SQLITE_CIO_NO_SETMODE
268 /************************* Begin ../ext/consio/console_io.h ******************/
272 ** The author disclaims copyright to this source code. In place of
273 ** a legal notice, here is a blessing:
275 ** May you do good and not evil.
276 ** May you find forgiveness for yourself and forgive others.
277 ** May you share freely, never taking more than you give.
279 ********************************************************************************
280 ** This file exposes various interfaces used for console and other I/O
281 ** by the SQLite project command-line tools. These interfaces are used
282 ** at either source conglomeration time, compilation time, or run time.
283 ** This source provides for either inclusion into conglomerated,
284 ** "single-source" forms or separate compilation then linking.
286 ** Platform dependencies are "hidden" here by various stratagems so
287 ** that, provided certain conditions are met, the programs using this
288 ** source or object code compiled from it need no explicit conditional
289 ** compilation in their source for their console and stream I/O.
291 ** The symbols and functionality exposed here are not a public API.
292 ** This code may change in tandem with other project code as needed.
294 ** When this .h file and its companion .c are directly incorporated into
295 ** a source conglomeration (such as shell.c), the preprocessor symbol
296 ** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
297 ** translation for Windows is effected for the build.
299 #define HAVE_CONSOLE_IO_H 1
300 #ifndef SQLITE_INTERNAL_LINKAGE
301 # define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
304 # define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
308 /* # include "sqlite3.h" */
311 #ifndef SQLITE_CIO_NO_CLASSIFY
313 /* Define enum for use with following function. */
314 typedef enum StreamsAreConsole {
316 SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
321 ** Classify the three standard I/O streams according to whether
322 ** they are connected to a console attached to the process.
324 ** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
325 ** or SAC_NoConsole if none of the streams reaches a console.
327 ** This function should be called before any I/O is done with
328 ** the given streams. As a side-effect, the given inputs are
329 ** recorded so that later I/O operations on them may be done
330 ** differently than the C library FILE* I/O would be done,
331 ** iff the stream is used for the I/O functions that follow,
332 ** and to support the ones that use an implicit stream.
334 ** On some platforms, stream or console mode alteration (aka
335 ** "Setup") may be made which is undone by consoleRestore().
337 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
338 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
339 /* A usual call for convenience: */
340 #define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
343 ** After an initial call to consoleClassifySetup(...), renew
344 ** the same setup it effected. (A call not after is an error.)
345 ** This will restore state altered by consoleRestore();
347 ** Applications which run an inferior (child) process which
348 ** inherits the same I/O streams may call this function after
349 ** such a process exits to guard against console mode changes.
351 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
354 ** Undo any side-effects left by consoleClassifySetup(...).
356 ** This should be called after consoleClassifySetup() and
357 ** before the process terminates normally. It is suitable
358 ** for use with the atexit() C library procedure. After
359 ** this call, no console I/O should be done until one of
360 ** console{Classify or Renew}Setup(...) is called again.
362 ** Applications which run an inferior (child) process that
363 ** inherits the same I/O streams might call this procedure
364 ** before so that said process will have a console setup
365 ** however users have configured it or come to expect.
367 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
369 #else /* defined(SQLITE_CIO_NO_CLASSIFY) */
370 # define consoleClassifySetup(i,o,e)
371 # define consoleRenewSetup()
372 # define consoleRestore()
373 #endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
375 #ifndef SQLITE_CIO_NO_REDIRECT
377 ** Set stream to be used for the functions below which write
378 ** to "the designated X stream", where X is Output or Error.
379 ** Returns the previous value.
381 ** Alternatively, pass the special value, invalidFileStream,
382 ** to get the designated stream value without setting it.
384 ** Before the designated streams are set, they default to
385 ** those passed to consoleClassifySetup(...), and before
386 ** that is called they default to stdout and stderr.
388 ** It is error to close a stream so designated, then, without
389 ** designating another, use the corresponding {o,e}Emit(...).
391 SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
392 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
393 # ifdef CONSIO_SET_ERROR_STREAM
394 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
397 # define setOutputStream(pf)
398 # define setErrorStream(pf)
399 #endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
401 #ifndef SQLITE_CIO_NO_TRANSLATE
403 ** Emit output like fprintf(). If the output is going to the
404 ** console and translation from UTF-8 is necessary, perform
405 ** the needed translation. Otherwise, write formatted output
406 ** to the provided stream almost as-is, possibly with newline
407 ** translation as specified by set{Binary,Text}Mode().
409 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
410 /* Like fPrintfUtf8 except stream is always the designated output. */
411 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
412 /* Like fPrintfUtf8 except stream is always the designated error. */
413 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
416 ** Emit output like fputs(). If the output is going to the
417 ** console and translation from UTF-8 is necessary, perform
418 ** the needed translation. Otherwise, write given text to the
419 ** provided stream almost as-is, possibly with newline
420 ** translation as specified by set{Binary,Text}Mode().
422 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
423 /* Like fPutsUtf8 except stream is always the designated output. */
424 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
425 /* Like fPutsUtf8 except stream is always the designated error. */
426 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
429 ** Emit output like fPutsUtf8(), except that the length of the
430 ** accepted char or character sequence is limited by nAccept.
432 ** Returns the number of accepted char values.
435 SQLITE_INTERNAL_LINKAGE int
436 fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
437 /* Like fPutbUtf8 except stream is always the designated output. */
439 SQLITE_INTERNAL_LINKAGE int
440 oPutbUtf8(const char *cBuf, int nAccept);
441 /* Like fPutbUtf8 except stream is always the designated error. */
443 SQLITE_INTERNAL_LINKAGE int
444 ePutbUtf8(const char *cBuf, int nAccept);
448 ** Collect input like fgets(...) with special provisions for input
449 ** from the console on platforms that require same. Defers to the
450 ** C library fgets() when input is not from the console. Newline
451 ** translation may be done as set by set{Binary,Text}Mode(). As a
452 ** convenience, pfIn==NULL is treated as stdin.
454 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
455 /* Like fGetsUtf8 except stream is always the designated input. */
456 /* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
458 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
460 #ifndef SQLITE_CIO_NO_SETMODE
462 ** Set given stream for binary mode, where newline translation is
463 ** not done, or for text mode where, for some platforms, newlines
464 ** are translated to the platform's conventional char sequence.
465 ** If bFlush true, flush the stream.
467 ** An additional side-effect is that if the stream is one passed
468 ** to consoleClassifySetup() as an output, it is flushed first.
470 ** Note that binary/text mode has no effect on console I/O
471 ** translation. On all platforms, newline to the console starts
472 ** a new line and CR,LF chars from the console become a newline.
474 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
475 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
478 #ifdef SQLITE_CIO_PROMPTED_IN
479 typedef struct Prompts {
481 const char **azPrompts;
485 ** Macros for use of a line editor.
487 ** The following macros define operations involving use of a
488 ** line-editing library or simple console interaction.
489 ** A "T" argument is a text (char *) buffer or filename.
490 ** A "N" argument is an integer.
492 ** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
493 ** SHELL_READ_HISTORY(T) // Read history from file named by T.
494 ** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
495 ** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
497 ** A console program which does interactive console input is
499 ** SHELL_READ_HISTORY(T) before collecting such input;
500 ** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
501 ** SHELL_STIFLE_HISTORY(N) after console input ceases; then
502 ** SHELL_WRITE_HISTORY(T) before the program exits.
506 ** Retrieve a single line of input text from an input stream.
508 ** If pfIn is the input stream passed to consoleClassifySetup(),
509 ** and azPrompt is not NULL, then a prompt is issued before the
510 ** line is collected, as selected by the isContinuation flag.
511 ** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
513 ** If zBufPrior is not NULL then it is a buffer from a prior
514 ** call to this routine that can be reused, or will be freed.
516 ** The result is stored in space obtained from malloc() and
517 ** must either be freed by the caller or else passed back to
518 ** this function as zBufPrior for reuse.
520 ** This function may call upon services of a line-editing
521 ** library to interactively collect line edited input.
523 SQLITE_INTERNAL_LINKAGE char *
524 shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
525 short isContinuation, Prompts azPrompt);
526 #endif /* defined(SQLITE_CIO_PROMPTED_IN) */
528 ** TBD: Define an interface for application(s) to generate
529 ** completion candidates for use by the line-editor.
531 ** This may be premature; the CLI is the only application
532 ** that does this. Yet, getting line-editing melded into
533 ** console I/O is desirable because a line-editing library
534 ** may have to establish console operating mode, possibly
535 ** in a way that interferes with the above functionality.
538 #if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
539 /* Skip over as much z[] input char sequence as is valid UTF-8,
540 ** limited per nAccept char's or whole characters and containing
541 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
542 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
543 ** Limit: nAccept>=0 => char count, nAccept<0 => character
545 SQLITE_INTERNAL_LINKAGE const char*
546 zSkipValidUtf8(const char *z, int nAccept, long ccm);
550 /************************* End ../ext/consio/console_io.h ********************/
551 /************************* Begin ../ext/consio/console_io.c ******************/
555 ** The author disclaims copyright to this source code. In place of
556 ** a legal notice, here is a blessing:
558 ** May you do good and not evil.
559 ** May you find forgiveness for yourself and forgive others.
560 ** May you share freely, never taking more than you give.
562 ********************************************************************************
563 ** This file implements various interfaces used for console and stream I/O
564 ** by the SQLite project command-line tools, as explained in console_io.h .
565 ** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
569 # define SQLITE_CDECL
572 #ifndef SHELL_NO_SYSINC
578 /* # include "sqlite3.h" */
580 #ifndef HAVE_CONSOLE_IO_H
581 # include "console_io.h"
584 #ifndef SQLITE_CIO_NO_TRANSLATE
585 # if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
586 # ifndef SHELL_NO_SYSINC
589 # undef WIN32_LEAN_AND_MEAN
590 # define WIN32_LEAN_AND_MEAN
591 # include <windows.h>
593 # define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
595 # ifndef SHELL_NO_SYSINC
598 # define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
601 # define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
605 /* Character used to represent a known-incomplete UTF-8 char group (�) */
606 static WCHAR cBadGroup = 0xfffd;
610 static HANDLE handleOfFile(FILE *pf){
611 int fileDesc = _fileno(pf);
612 union { intptr_t osfh; HANDLE fh; } fid = {
613 (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
619 #ifndef SQLITE_CIO_NO_TRANSLATE
620 typedef struct PerStreamTags {
621 # if CIO_WIN_WC_XLATE
624 char acIncomplete[4];
626 short reachesConsole;
631 /* Define NULL-like value for things which can validly be 0. */
632 # define SHELL_INVALID_FILE_PTR ((FILE *)~0)
633 # if CIO_WIN_WC_XLATE
634 # define SHELL_INVALID_CONS_MODE 0xFFFF0000
637 # if CIO_WIN_WC_XLATE
638 # define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
639 {0,0,0,0}, SHELL_INVALID_FILE_PTR }
641 # define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
644 /* Quickly say whether a known output is going to the console. */
645 # if CIO_WIN_WC_XLATE
646 static short pstReachesConsole(PerStreamTags *ppst){
647 return (ppst->hx != INVALID_HANDLE_VALUE);
650 # define pstReachesConsole(ppst) 0
653 # if CIO_WIN_WC_XLATE
654 static void restoreConsoleArb(PerStreamTags *ppst){
655 if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
658 # define restoreConsoleArb(ppst)
661 /* Say whether FILE* appears to be a console, collect associated info. */
662 static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
663 # if CIO_WIN_WC_XLATE
665 DWORD dwCM = SHELL_INVALID_CONS_MODE;
666 HANDLE fh = handleOfFile(pf);
668 if( INVALID_HANDLE_VALUE != fh ){
669 rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
671 ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
672 ppst->consMode = dwCM;
676 ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
677 return ppst->reachesConsole;
681 # if CIO_WIN_WC_XLATE
682 /* Define console modes for use with the Windows Console API. */
683 # define SHELL_CONI_MODE \
684 (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
685 | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
686 # define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
687 | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
690 typedef struct ConsoleInfo {
691 PerStreamTags pstSetup[3];
692 PerStreamTags pstDesignated[3];
693 StreamsAreConsole sacSetup;
696 static short isValidStreamInfo(PerStreamTags *ppst){
697 return (ppst->pf != SHELL_INVALID_FILE_PTR);
700 static ConsoleInfo consoleInfo = {
701 { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
702 { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
703 SAC_NoConsole /* sacSetup */
706 SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
708 # if CIO_WIN_WC_XLATE
709 static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
710 if( pstReachesConsole(ppst) ){
711 DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
712 SetConsoleMode(ppst->hx, cm);
716 # define maybeSetupAsConsole(ppst,odir)
719 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
720 # if CIO_WIN_WC_XLATE
723 PerStreamTags *ppst = (ix<3)?
724 &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
725 maybeSetupAsConsole(ppst, (ix % 3)>0);
731 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
732 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
733 StreamsAreConsole rv = SAC_NoConsole;
734 FILE* apf[3] = { pfIn, pfOut, pfErr };
736 for( ix = 2; ix >= 0; --ix ){
737 PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
738 if( streamOfConsole(apf[ix], ppst) ){
739 rv |= (SAC_InConsole<<ix);
741 consoleInfo.pstDesignated[ix] = *ppst;
742 if( ix > 0 ) fflush(apf[ix]);
744 consoleInfo.sacSetup = rv;
749 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
750 # if CIO_WIN_WC_XLATE
751 static ConsoleInfo *pci = &consoleInfo;
754 for( ix=0; ix<3; ++ix ){
755 if( pci->sacSetup & (SAC_InConsole<<ix) ){
756 PerStreamTags *ppst = &pci->pstSetup[ix];
757 SetConsoleMode(ppst->hx, ppst->consMode);
763 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
765 #ifdef SQLITE_CIO_INPUT_REDIR
766 /* Say whether given FILE* is among those known, via either
767 ** consoleClassifySetup() or set{Output,Error}Stream, as
768 ** readable, and return an associated PerStreamTags pointer
769 ** if so. Otherwise, return 0.
771 static PerStreamTags * isKnownReadable(FILE *pf){
772 static PerStreamTags *apst[] = {
773 &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
777 if( apst[ix]->pf == pf ) break;
778 } while( apst[++ix] != 0 );
783 #ifndef SQLITE_CIO_NO_TRANSLATE
784 /* Say whether given FILE* is among those known, via either
785 ** consoleClassifySetup() or set{Output,Error}Stream, as
786 ** writable, and return an associated PerStreamTags pointer
787 ** if so. Otherwise, return 0.
789 static PerStreamTags * isKnownWritable(FILE *pf){
790 static PerStreamTags *apst[] = {
791 &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
792 &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
796 if( apst[ix]->pf == pf ) break;
797 } while( apst[++ix] != 0 );
801 static FILE *designateEmitStream(FILE *pf, unsigned chix){
802 FILE *rv = consoleInfo.pstDesignated[chix].pf;
803 if( pf == invalidFileStream ) return rv;
805 /* Setting a possibly new output stream. */
806 PerStreamTags *ppst = isKnownWritable(pf);
808 PerStreamTags pst = *ppst;
809 consoleInfo.pstDesignated[chix] = pst;
810 }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
815 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
816 return designateEmitStream(pf, 1);
818 # ifdef CONSIO_SET_ERROR_STREAM
819 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
820 return designateEmitStream(pf, 2);
823 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
825 #ifndef SQLITE_CIO_NO_SETMODE
826 # if CIO_WIN_WC_XLATE
827 static void setModeFlushQ(FILE *pf, short bFlush, int mode){
828 if( bFlush ) fflush(pf);
829 _setmode(_fileno(pf), mode);
832 # define setModeFlushQ(f, b, m) if(b) fflush(f)
835 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
836 setModeFlushQ(pf, bFlush, _O_BINARY);
838 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
839 setModeFlushQ(pf, bFlush, _O_TEXT);
841 # undef setModeFlushQ
843 #else /* defined(SQLITE_CIO_NO_SETMODE) */
844 # define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
845 # define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
846 #endif /* defined(SQLITE_CIO_NO_SETMODE) */
848 #ifndef SQLITE_CIO_NO_TRANSLATE
849 # if CIO_WIN_WC_XLATE
850 /* Write buffer cBuf as output to stream known to reach console,
851 ** limited to ncTake char's. Return ncTake on success, else 0. */
852 static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
855 int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
857 WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
859 nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
861 /* Translation from UTF-8 to UTF-16, then WCHARs out. */
862 if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
873 /* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
874 static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
875 char *z = sqlite3_vmprintf(zFormat, ap);
877 int rv = conZstrEmit(ppst, z, (int)strlen(z));
882 # endif /* CIO_WIN_WC_XLATE */
884 # ifdef CONSIO_GET_EMIT_STREAM
885 static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
886 PerStreamTags *ppst){
887 PerStreamTags *rv = isKnownWritable(pf);
888 short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
889 if( rv != 0 && isValid ) return rv;
890 streamOfConsole(pf, ppst);
895 /* Get stream info, either for designated output or error stream when
896 ** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
897 ** In either case, ppst references a caller-owned PerStreamTags
898 ** struct which may be filled in if none of the known writable
899 ** streams is being held by consoleInfo. The ppf parameter is a
900 ** byref output when chix!=0 and a byref input when chix==0.
902 static PerStreamTags *
903 getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
904 /* in/out */ FILE **ppf){
905 PerStreamTags *ppstTry;
908 ppstTry = &consoleInfo.pstDesignated[chix];
909 if( !isValidStreamInfo(ppstTry) ){
910 ppstTry = &consoleInfo.pstSetup[chix];
912 }else pfEmit = ppstTry->pf;
913 if( !isValidStreamInfo(ppstTry) ){
914 pfEmit = (chix > 1)? stderr : stdout;
916 streamOfConsole(pfEmit, ppstTry);
920 ppstTry = isKnownWritable(*ppf);
921 if( ppstTry != 0 ) return ppstTry;
922 streamOfConsole(*ppf, ppst);
928 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
932 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
933 # if CIO_WIN_WC_XLATE
934 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
936 getEmitStreamInfo(1, &pst, &pfOut);
939 va_start(ap, zFormat);
940 # if CIO_WIN_WC_XLATE
941 if( pstReachesConsole(ppst) ){
942 rv = conioVmPrintf(ppst, zFormat, ap);
945 rv = vfprintf(pfOut, zFormat, ap);
946 # if CIO_WIN_WC_XLATE
953 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
957 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
958 # if CIO_WIN_WC_XLATE
959 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
961 getEmitStreamInfo(2, &pst, &pfErr);
964 va_start(ap, zFormat);
965 # if CIO_WIN_WC_XLATE
966 if( pstReachesConsole(ppst) ){
967 rv = conioVmPrintf(ppst, zFormat, ap);
970 rv = vfprintf(pfErr, zFormat, ap);
971 # if CIO_WIN_WC_XLATE
978 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
981 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
982 # if CIO_WIN_WC_XLATE
983 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
985 getEmitStreamInfo(0, &pst, &pfO);
988 va_start(ap, zFormat);
989 # if CIO_WIN_WC_XLATE
990 if( pstReachesConsole(ppst) ){
991 maybeSetupAsConsole(ppst, 1);
992 rv = conioVmPrintf(ppst, zFormat, ap);
993 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
996 rv = vfprintf(pfO, zFormat, ap);
997 # if CIO_WIN_WC_XLATE
1004 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1005 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1006 # if CIO_WIN_WC_XLATE
1007 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1009 getEmitStreamInfo(0, &pst, &pfO);
1012 # if CIO_WIN_WC_XLATE
1013 if( pstReachesConsole(ppst) ){
1015 maybeSetupAsConsole(ppst, 1);
1016 rv = conZstrEmit(ppst, z, (int)strlen(z));
1017 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1021 return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1022 # if CIO_WIN_WC_XLATE
1027 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1029 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1030 # if CIO_WIN_WC_XLATE
1031 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1033 getEmitStreamInfo(2, &pst, &pfErr);
1036 # if CIO_WIN_WC_XLATE
1037 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1040 return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1041 # if CIO_WIN_WC_XLATE
1046 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1048 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1049 # if CIO_WIN_WC_XLATE
1050 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1052 getEmitStreamInfo(1, &pst, &pfOut);
1055 # if CIO_WIN_WC_XLATE
1056 if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1059 return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1060 # if CIO_WIN_WC_XLATE
1065 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1067 #if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1068 /* Skip over as much z[] input char sequence as is valid UTF-8,
1069 ** limited per nAccept char's or whole characters and containing
1070 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1071 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
1072 ** Limit: nAccept>=0 => char count, nAccept<0 => character
1074 SQLITE_INTERNAL_LINKAGE const char*
1075 zSkipValidUtf8(const char *z, int nAccept, long ccm){
1076 int ng = (nAccept<0)? -nAccept : 0;
1077 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1079 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1081 if( (c & 0x80) == 0 ){
1082 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1084 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1086 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1088 if( pcLimit && zt >= pcLimit ) return z;
1091 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1092 /* Trailing bytes are too few, too many, or invalid. */
1096 } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1102 #endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1104 #ifndef SQLITE_CIO_NO_TRANSLATE
1105 # ifdef CONSIO_SPUTB
1106 SQLITE_INTERNAL_LINKAGE int
1107 fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1109 # if CIO_WIN_WC_XLATE
1110 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1111 PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1112 if( pstReachesConsole(ppst) ){
1114 maybeSetupAsConsole(ppst, 1);
1115 rv = conZstrEmit(ppst, cBuf, nAccept);
1116 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1120 return (int)fwrite(cBuf, 1, nAccept, pfO);
1121 # if CIO_WIN_WC_XLATE
1127 SQLITE_INTERNAL_LINKAGE int
1128 oPutbUtf8(const char *cBuf, int nAccept){
1130 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1131 # if CIO_WIN_WC_XLATE
1132 PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1134 getEmitStreamInfo(1, &pst, &pfOut);
1136 # if CIO_WIN_WC_XLATE
1137 if( pstReachesConsole(ppst) ){
1138 return conZstrEmit(ppst, cBuf, nAccept);
1141 return (int)fwrite(cBuf, 1, nAccept, pfOut);
1142 # if CIO_WIN_WC_XLATE
1147 # ifdef CONSIO_EPUTB
1148 SQLITE_INTERNAL_LINKAGE int
1149 ePutbUtf8(const char *cBuf, int nAccept){
1151 PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1152 PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1153 # if CIO_WIN_WC_XLATE
1154 if( pstReachesConsole(ppst) ){
1155 return conZstrEmit(ppst, cBuf, nAccept);
1158 return (int)fwrite(cBuf, 1, nAccept, pfErr);
1159 # if CIO_WIN_WC_XLATE
1163 # endif /* defined(CONSIO_EPUTB) */
1165 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1166 if( pfIn==0 ) pfIn = stdin;
1167 # if CIO_WIN_WC_XLATE
1168 if( pfIn == consoleInfo.pstSetup[0].pf
1169 && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1170 # if CIO_WIN_WC_XLATE==1
1171 # define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1172 WCHAR wcBuf[SHELL_GULP+1];
1173 int lend = 0, noc = 0;
1174 if( ncMax > 0 ) cBuf[0] = 0;
1175 while( noc < ncMax-8-1 && !lend ){
1176 /* There is room for at least 2 more characters and a 0-terminator. */
1177 int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1180 BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1181 if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1182 /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1184 bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1185 if( bRC ) nbr += nbrx;
1187 if( !bRC || (noc==0 && nbr==0) ) return 0;
1189 int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1190 if( nmb != 0 && noc+nmb <= ncMax ){
1192 nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1194 /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1195 ** This is done without regard for any setMode{Text,Binary}()
1196 ** call that might have been done on the interactive input.
1199 if( cBuf[noc-1]=='\n' ){
1201 if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1204 /* Check for ^Z (anywhere in line) too, to act as EOF. */
1205 while( iseg < noc ){
1206 if( cBuf[iseg]=='\x1a' ){
1207 noc = iseg; /* Chop ^Z and anything following. */
1208 lend = 1; /* Counts as end of line too. */
1213 }else break; /* Drop apparent garbage in. (Could assert.) */
1216 /* If got nothing, (after ^Z chop), must be at end-of-file. */
1224 return fgets(cBuf, ncMax, pfIn);
1225 # if CIO_WIN_WC_XLATE
1229 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1231 #undef SHELL_INVALID_FILE_PTR
1233 /************************* End ../ext/consio/console_io.c ********************/
1235 #ifndef SQLITE_SHELL_FIDDLE
1237 /* From here onward, fgets() is redirected to the console_io library. */
1238 # define fgets(b,n,f) fGetsUtf8(b,n,f)
1240 * Define macros for emitting output text in various ways:
1241 * sputz(s, z) => emit 0-terminated string z to given stream s
1242 * sputf(s, f, ...) => emit varargs per format f to given stream s
1243 * oputz(z) => emit 0-terminated string z to default stream
1244 * oputf(f, ...) => emit varargs per format f to default stream
1245 * eputz(z) => emit 0-terminated string z to error stream
1246 * eputf(f, ...) => emit varargs per format f to error stream
1247 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1249 * Note that the default stream is whatever has been last set via:
1250 * setOutputStream(FILE *pf)
1251 * This is normally the stream that CLI normal output goes to.
1252 * For the stand-alone CLI, it is stdout with no .output redirect.
1254 # define sputz(s,z) fPutsUtf8(z,s)
1255 # define sputf fPrintfUtf8
1256 # define oputz(z) oPutsUtf8(z)
1257 # define oputf oPrintfUtf8
1258 # define eputz(z) ePutsUtf8(z)
1259 # define eputf ePrintfUtf8
1260 # define oputb(buf,na) oPutbUtf8(buf,na)
1263 /* For Fiddle, all console handling and emit redirection is omitted. */
1264 # define sputz(fp,z) fputs(z,fp)
1265 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1266 # define oputz(z) fputs(z,stdout)
1267 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1268 # define eputz(z) fputs(z,stderr)
1269 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1270 # define oputb(buf,na) fwrite(buf,1,na,stdout)
1273 /* True if the timer is enabled */
1274 static int enableTimer = 0;
1276 /* A version of strcmp() that works with NULL values */
1277 static int cli_strcmp(const char *a, const char *b){
1282 static int cli_strncmp(const char *a, const char *b, size_t n){
1285 return strncmp(a,b,n);
1288 /* Return the current wall-clock time */
1289 static sqlite3_int64 timeOfDay(void){
1290 static sqlite3_vfs *clockVfs = 0;
1292 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
1293 if( clockVfs==0 ) return 0; /* Never actually happens */
1294 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
1295 clockVfs->xCurrentTimeInt64(clockVfs, &t);
1298 clockVfs->xCurrentTime(clockVfs, &r);
1299 t = (sqlite3_int64)(r*86400000.0);
1304 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
1305 #include <sys/time.h>
1306 #include <sys/resource.h>
1308 /* VxWorks does not support getrusage() as far as we can determine */
1309 #if defined(_WRS_KERNEL) || defined(__RTP__)
1311 struct timeval ru_utime; /* user CPU time used */
1312 struct timeval ru_stime; /* system CPU time used */
1314 #define getrusage(A,B) memset(B,0,sizeof(*B))
1317 /* Saved resource information for the beginning of an operation */
1318 static struct rusage sBegin; /* CPU time at start */
1319 static sqlite3_int64 iBegin; /* Wall-clock time at start */
1322 ** Begin timing an operation
1324 static void beginTimer(void){
1326 getrusage(RUSAGE_SELF, &sBegin);
1327 iBegin = timeOfDay();
1331 /* Return the difference of two time_structs in seconds */
1332 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
1333 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
1334 (double)(pEnd->tv_sec - pStart->tv_sec);
1338 ** Print the timing results.
1340 static void endTimer(void){
1342 sqlite3_int64 iEnd = timeOfDay();
1344 getrusage(RUSAGE_SELF, &sEnd);
1345 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1346 (iEnd - iBegin)*0.001,
1347 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1348 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1352 #define BEGIN_TIMER beginTimer()
1353 #define END_TIMER endTimer()
1356 #elif (defined(_WIN32) || defined(WIN32))
1358 /* Saved resource information for the beginning of an operation */
1359 static HANDLE hProcess;
1360 static FILETIME ftKernelBegin;
1361 static FILETIME ftUserBegin;
1362 static sqlite3_int64 ftWallBegin;
1363 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
1364 LPFILETIME, LPFILETIME);
1365 static GETPROCTIMES getProcessTimesAddr = NULL;
1368 ** Check to see if we have timer support. Return 1 if necessary
1369 ** support found (or found previously).
1371 static int hasTimer(void){
1372 if( getProcessTimesAddr ){
1375 #if !SQLITE_OS_WINRT
1376 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
1377 ** versions. See if the version we are running on has it, and if it
1378 ** does, save off a pointer to it and the current process handle.
1380 hProcess = GetCurrentProcess();
1382 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
1383 if( NULL != hinstLib ){
1384 getProcessTimesAddr =
1385 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
1386 if( NULL != getProcessTimesAddr ){
1389 FreeLibrary(hinstLib);
1398 ** Begin timing an operation
1400 static void beginTimer(void){
1401 if( enableTimer && getProcessTimesAddr ){
1402 FILETIME ftCreation, ftExit;
1403 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
1404 &ftKernelBegin,&ftUserBegin);
1405 ftWallBegin = timeOfDay();
1409 /* Return the difference of two FILETIME structs in seconds */
1410 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
1411 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
1412 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
1413 return (double) ((i64End - i64Start) / 10000000.0);
1417 ** Print the timing results.
1419 static void endTimer(void){
1420 if( enableTimer && getProcessTimesAddr){
1421 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
1422 sqlite3_int64 ftWallEnd = timeOfDay();
1423 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
1424 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1425 (ftWallEnd - ftWallBegin)*0.001,
1426 timeDiff(&ftUserBegin, &ftUserEnd),
1427 timeDiff(&ftKernelBegin, &ftKernelEnd));
1431 #define BEGIN_TIMER beginTimer()
1432 #define END_TIMER endTimer()
1433 #define HAS_TIMER hasTimer()
1442 ** Used to prevent warnings about unused parameters
1444 #define UNUSED_PARAMETER(x) (void)(x)
1447 ** Number of elements in an array
1449 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
1452 ** If the following flag is set, then command execution stops
1453 ** at an error if we are not interactive.
1455 static int bail_on_error = 0;
1458 ** Treat stdin as an interactive input if the following variable
1459 ** is true. Otherwise, assume stdin is connected to a file or pipe.
1461 static int stdin_is_interactive = 1;
1464 ** On Windows systems we need to know if standard output is a console
1465 ** in order to show that UTF-16 translation is done in the sign-on
1466 ** banner. The following variable is true if it is the console.
1468 static int stdout_is_console = 1;
1471 ** The following is the open SQLite database. We make a pointer
1472 ** to this database a static variable so that it can be accessed
1473 ** by the SIGINT handler to interrupt database processing.
1475 static sqlite3 *globalDb = 0;
1478 ** True if an interrupt (Control-C) has been received.
1480 static volatile int seenInterrupt = 0;
1483 ** This is the name of our program. It is set in main(), used
1484 ** in a number of other places, mostly for error messages.
1489 ** Prompt strings. Initialized in main. Settable with
1490 ** .prompt main continue
1492 #define PROMPT_LEN_MAX 20
1493 /* First line prompt. default: "sqlite> " */
1494 static char mainPrompt[PROMPT_LEN_MAX];
1495 /* Continuation prompt. default: " ...> " */
1496 static char continuePrompt[PROMPT_LEN_MAX];
1498 /* This is variant of the standard-library strncpy() routine with the
1499 ** one change that the destination string is always zero-terminated, even
1500 ** if there is no zero-terminator in the first n-1 characters of the source
1503 static char *shell_strncpy(char *dest, const char *src, size_t n){
1505 for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
1511 ** Optionally disable dynamic continuation prompt.
1512 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
1513 ** or open parentheses level if non-zero, or continuation prompt as set.
1514 ** This facility interacts with the scanner and process_input() where the
1515 ** below 5 macros are used.
1517 #ifdef SQLITE_OMIT_DYNAPROMPT
1518 # define CONTINUATION_PROMPT continuePrompt
1519 # define CONTINUE_PROMPT_RESET
1520 # define CONTINUE_PROMPT_AWAITS(p,s)
1521 # define CONTINUE_PROMPT_AWAITC(p,c)
1522 # define CONTINUE_PAREN_INCR(p,n)
1523 # define CONTINUE_PROMPT_PSTATE 0
1524 typedef void *t_NoDynaPrompt;
1525 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
1527 # define CONTINUATION_PROMPT dynamicContinuePrompt()
1528 # define CONTINUE_PROMPT_RESET \
1529 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
1530 # define CONTINUE_PROMPT_AWAITS(p,s) \
1531 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
1532 # define CONTINUE_PROMPT_AWAITC(p,c) \
1533 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
1534 # define CONTINUE_PAREN_INCR(p,n) \
1535 if(p && stdin_is_interactive) (trackParenLevel(p,n))
1536 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
1537 typedef struct DynaPrompt *t_DynaPromptRef;
1538 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
1540 static struct DynaPrompt {
1541 char dynamicPrompt[PROMPT_LEN_MAX];
1544 char *zScannerAwaits;
1545 } dynPrompt = { {0}, {0}, 0, 0 };
1547 /* Record parenthesis nesting level change, or force level to 0. */
1548 static void trackParenLevel(struct DynaPrompt *p, int ni){
1549 p->inParenLevel += ni;
1550 if( ni==0 ) p->inParenLevel = 0;
1551 p->zScannerAwaits = 0;
1554 /* Record that a lexeme is opened, or closed with args==0. */
1555 static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
1557 p->zScannerAwaits = s;
1561 p->zScannerAwaits = p->acAwait;
1565 /* Upon demand, derive the continuation prompt to display. */
1566 static char *dynamicContinuePrompt(void){
1567 if( continuePrompt[0]==0
1568 || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
1569 return continuePrompt;
1571 if( dynPrompt.zScannerAwaits ){
1572 size_t ncp = strlen(continuePrompt);
1573 size_t ndp = strlen(dynPrompt.zScannerAwaits);
1574 if( ndp > ncp-3 ) return continuePrompt;
1575 strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
1576 while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
1577 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1580 if( dynPrompt.inParenLevel>9 ){
1581 shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
1582 }else if( dynPrompt.inParenLevel<0 ){
1583 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
1585 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
1586 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
1588 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1592 return dynPrompt.dynamicPrompt;
1594 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
1596 /* Indicate out-of-memory and exit. */
1597 static void shell_out_of_memory(void){
1598 eputz("Error: out of memory\n");
1602 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
1603 ** out-of-memory error.
1605 static void shell_check_oom(const void *p){
1606 if( p==0 ) shell_out_of_memory();
1610 ** Write I/O traces to the following stream.
1612 #ifdef SQLITE_ENABLE_IOTRACE
1613 static FILE *iotrace = 0;
1617 ** This routine works like printf in that its first argument is a
1618 ** format string and subsequent arguments are values to be substituted
1619 ** in place of % fields. The result of formatting this string
1620 ** is written to iotrace.
1622 #ifdef SQLITE_ENABLE_IOTRACE
1623 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
1626 if( iotrace==0 ) return;
1627 va_start(ap, zFormat);
1628 z = sqlite3_vmprintf(zFormat, ap);
1630 sputf(iotrace, "%s", z);
1636 ** Output string zUtf to Out stream as w characters. If w is negative,
1637 ** then right-justify the text. W is the width in UTF-8 characters, not
1638 ** in bytes. This is different from the %*.*s specification in printf
1639 ** since with %*.*s the width is measured in bytes, not characters.
1641 static void utf8_width_print(int w, const char *zUtf){
1644 int aw = w<0 ? -w : w;
1645 if( zUtf==0 ) zUtf = "";
1646 for(i=n=0; zUtf[i]; i++){
1647 if( (zUtf[i]&0xc0)!=0x80 ){
1650 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
1656 oputf("%.*s", i, zUtf);
1658 oputf("%*s%s", aw-n, "", zUtf);
1660 oputf("%s%*s", zUtf, aw-n, "");
1666 ** Determines if a string is a number of not.
1668 static int isNumber(const char *z, int *realnum){
1669 if( *z=='-' || *z=='+' ) z++;
1674 if( realnum ) *realnum = 0;
1675 while( IsDigit(*z) ){ z++; }
1678 if( !IsDigit(*z) ) return 0;
1679 while( IsDigit(*z) ){ z++; }
1680 if( realnum ) *realnum = 1;
1682 if( *z=='e' || *z=='E' ){
1684 if( *z=='+' || *z=='-' ) z++;
1685 if( !IsDigit(*z) ) return 0;
1686 while( IsDigit(*z) ){ z++; }
1687 if( realnum ) *realnum = 1;
1693 ** Compute a string length that is limited to what can be stored in
1694 ** lower 30 bits of a 32-bit signed integer.
1696 static int strlen30(const char *z){
1698 while( *z2 ){ z2++; }
1699 return 0x3fffffff & (int)(z2 - z);
1703 ** Return the length of a string in characters. Multibyte UTF8 characters
1704 ** count as a single character.
1706 static int strlenChar(const char *z){
1709 if( (0xc0&*(z++))!=0x80 ) n++;
1715 ** Return open FILE * if zFile exists, can be opened for read
1716 ** and is an ordinary file or a character stream source.
1717 ** Otherwise return 0.
1719 static FILE * openChrSource(const char *zFile){
1720 #if defined(_WIN32) || defined(WIN32)
1721 struct __stat64 x = {0};
1722 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
1723 /* On Windows, open first, then check the stream nature. This order
1724 ** is necessary because _stat() and sibs, when checking a named pipe,
1725 ** effectively break the pipe as its supplier sees it. */
1726 FILE *rv = fopen(zFile, "rb");
1727 if( rv==0 ) return 0;
1728 if( _fstat64(_fileno(rv), &x) != 0
1729 || !STAT_CHR_SRC(x.st_mode)){
1735 struct stat x = {0};
1736 int rc = stat(zFile, &x);
1737 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
1738 if( rc!=0 ) return 0;
1739 if( STAT_CHR_SRC(x.st_mode) ){
1740 return fopen(zFile, "rb");
1749 ** This routine reads a line of text from FILE in, stores
1750 ** the text in memory obtained from malloc() and returns a pointer
1751 ** to the text. NULL is returned at end of file, or if malloc()
1754 ** If zLine is not NULL then it is a malloced buffer returned from
1755 ** a previous call to this routine that may be reused.
1757 static char *local_getline(char *zLine, FILE *in){
1758 int nLine = zLine==0 ? 0 : 100;
1763 nLine = nLine*2 + 100;
1764 zLine = realloc(zLine, nLine);
1765 shell_check_oom(zLine);
1767 if( fgets(&zLine[n], nLine - n, in)==0 ){
1775 while( zLine[n] ) n++;
1776 if( n>0 && zLine[n-1]=='\n' ){
1778 if( n>0 && zLine[n-1]=='\r' ) n--;
1787 ** Retrieve a single line of input text.
1789 ** If in==0 then read from standard input and prompt before each line.
1790 ** If isContinuation is true, then a continuation prompt is appropriate.
1791 ** If isContinuation is zero, then the main prompt should be used.
1793 ** If zPrior is not NULL then it is a buffer from a prior call to this
1794 ** routine that can be reused.
1796 ** The result is stored in space obtained from malloc() and must either
1797 ** be freed by the caller or else passed back into this routine via the
1798 ** zPrior argument for reuse.
1800 #ifndef SQLITE_SHELL_FIDDLE
1801 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
1805 zResult = local_getline(zPrior, in);
1807 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
1808 #if SHELL_USE_LOCAL_GETLINE
1809 sputz(stdout, zPrompt);
1812 zResult = local_getline(zPrior, stdin);
1814 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1815 if( zResult==0 ) sqlite3_sleep(50);
1816 }while( zResult==0 && seenInterrupt>0 );
1819 zResult = shell_readline(zPrompt);
1820 while( zResult==0 ){
1821 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1823 if( seenInterrupt==0 ) break;
1824 zResult = shell_readline("");
1826 if( zResult && *zResult ) shell_add_history(zResult);
1831 #endif /* !SQLITE_SHELL_FIDDLE */
1834 ** Return the value of a hexadecimal digit. Return -1 if the input
1835 ** is not a hex digit.
1837 static int hexDigitValue(char c){
1838 if( c>='0' && c<='9' ) return c - '0';
1839 if( c>='a' && c<='f' ) return c - 'a' + 10;
1840 if( c>='A' && c<='F' ) return c - 'A' + 10;
1845 ** Interpret zArg as an integer value, possibly with suffixes.
1847 static sqlite3_int64 integerValue(const char *zArg){
1848 sqlite3_int64 v = 0;
1849 static const struct { char *zSuffix; int iMult; } aMult[] = {
1851 { "MiB", 1024*1024 },
1852 { "GiB", 1024*1024*1024 },
1855 { "GB", 1000000000 },
1858 { "G", 1000000000 },
1865 }else if( zArg[0]=='+' ){
1868 if( zArg[0]=='0' && zArg[1]=='x' ){
1871 while( (x = hexDigitValue(zArg[0]))>=0 ){
1876 while( IsDigit(zArg[0]) ){
1877 v = v*10 + zArg[0] - '0';
1881 for(i=0; i<ArraySize(aMult); i++){
1882 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1883 v *= aMult[i].iMult;
1887 return isNeg? -v : v;
1891 ** A variable length string to which one can append text.
1893 typedef struct ShellText ShellText;
1901 ** Initialize and destroy a ShellText object
1903 static void initText(ShellText *p){
1904 memset(p, 0, sizeof(*p));
1906 static void freeText(ShellText *p){
1911 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1912 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1913 ** added to zIn, and the result returned in memory obtained from malloc().
1914 ** zIn, if it was not NULL, is freed.
1916 ** If the third argument, quote, is not '\0', then it is used as a
1917 ** quote character for zAppend.
1919 static void appendText(ShellText *p, const char *zAppend, char quote){
1922 i64 nAppend = strlen30(zAppend);
1924 len = nAppend+p->n+1;
1927 for(i=0; i<nAppend; i++){
1928 if( zAppend[i]==quote ) len++;
1932 if( p->z==0 || p->n+len>=p->nAlloc ){
1933 p->nAlloc = p->nAlloc*2 + len + 20;
1934 p->z = realloc(p->z, p->nAlloc);
1935 shell_check_oom(p->z);
1939 char *zCsr = p->z+p->n;
1941 for(i=0; i<nAppend; i++){
1942 *zCsr++ = zAppend[i];
1943 if( zAppend[i]==quote ) *zCsr++ = quote;
1946 p->n = (int)(zCsr - p->z);
1949 memcpy(p->z+p->n, zAppend, nAppend);
1956 ** Attempt to determine if identifier zName needs to be quoted, either
1957 ** because it contains non-alphanumeric characters, or because it is an
1958 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1959 ** that quoting is required.
1961 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1963 static char quoteChar(const char *zName){
1965 if( zName==0 ) return '"';
1966 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
1967 for(i=0; zName[i]; i++){
1968 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
1970 return sqlite3_keyword_check(zName, i) ? '"' : 0;
1974 ** Construct a fake object name and column list to describe the structure
1975 ** of the view, virtual table, or table valued function zSchema.zName.
1977 static char *shellFakeSchema(
1978 sqlite3 *db, /* The database connection containing the vtab */
1979 const char *zSchema, /* Schema of the database holding the vtab */
1980 const char *zName /* The name of the virtual table */
1982 sqlite3_stmt *pStmt = 0;
1989 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1990 zSchema ? zSchema : "main", zName);
1991 shell_check_oom(zSql);
1992 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1996 cQuote = quoteChar(zSchema);
1997 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
1998 appendText(&s, zSchema, cQuote);
1999 appendText(&s, ".", 0);
2001 cQuote = quoteChar(zName);
2002 appendText(&s, zName, cQuote);
2003 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2004 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
2006 appendText(&s, zDiv, 0);
2008 if( zCol==0 ) zCol = "";
2009 cQuote = quoteChar(zCol);
2010 appendText(&s, zCol, cQuote);
2012 appendText(&s, ")", 0);
2013 sqlite3_finalize(pStmt);
2022 ** SQL function: strtod(X)
2024 ** Use the C-library strtod() function to convert string X into a double.
2025 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
2026 ** routines against the C-library.
2028 static void shellStrtod(
2029 sqlite3_context *pCtx,
2031 sqlite3_value **apVal
2033 char *z = (char*)sqlite3_value_text(apVal[0]);
2034 UNUSED_PARAMETER(nVal);
2036 sqlite3_result_double(pCtx, strtod(z,0));
2040 ** SQL function: dtostr(X)
2042 ** Use the C-library printf() function to convert real value X into a string.
2043 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
2044 ** routines against the C-library.
2046 static void shellDtostr(
2047 sqlite3_context *pCtx,
2049 sqlite3_value **apVal
2051 double r = sqlite3_value_double(apVal[0]);
2052 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
2055 if( n>350 ) n = 350;
2056 sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
2057 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
2062 ** SQL function: shell_module_schema(X)
2064 ** Return a fake schema for the table-valued function or eponymous virtual
2067 static void shellModuleSchema(
2068 sqlite3_context *pCtx,
2070 sqlite3_value **apVal
2074 UNUSED_PARAMETER(nVal);
2075 zName = (const char*)sqlite3_value_text(apVal[0]);
2076 zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
2078 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
2085 ** SQL function: shell_add_schema(S,X)
2087 ** Add the schema name X to the CREATE statement in S and return the result.
2090 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
2095 ** CREATE UNIQUE INDEX
2098 ** CREATE VIRTUAL TABLE
2100 ** This UDF is used by the .schema command to insert the schema name of
2101 ** attached databases into the middle of the sqlite_schema.sql field.
2103 static void shellAddSchemaName(
2104 sqlite3_context *pCtx,
2106 sqlite3_value **apVal
2108 static const char *aPrefix[] = {
2117 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
2118 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
2119 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
2120 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2121 UNUSED_PARAMETER(nVal);
2122 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
2123 for(i=0; i<ArraySize(aPrefix); i++){
2124 int n = strlen30(aPrefix[i]);
2125 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
2129 char cQuote = quoteChar(zSchema);
2130 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
2131 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
2133 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
2137 && aPrefix[i][0]=='V'
2138 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
2141 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
2143 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
2148 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
2154 sqlite3_result_value(pCtx, apVal[0]);
2158 ** The source code for several run-time loadable extensions is inserted
2159 ** below by the ../tool/mkshellc.tcl script. Before processing that included
2160 ** code, we need to override some macros to make the included program code
2161 ** work here in the middle of this regular program.
2163 #define SQLITE_EXTENSION_INIT1
2164 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
2166 #if defined(_WIN32) && defined(_MSC_VER)
2167 /************************* Begin test_windirent.h ******************/
2171 ** The author disclaims copyright to this source code. In place of
2172 ** a legal notice, here is a blessing:
2174 ** May you do good and not evil.
2175 ** May you find forgiveness for yourself and forgive others.
2176 ** May you share freely, never taking more than you give.
2178 *************************************************************************
2179 ** This file contains declarations for most of the opendir() family of
2180 ** POSIX functions on Win32 using the MSVCRT.
2183 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
2184 #define SQLITE_WINDIRENT_H
2187 ** We need several data types from the Windows SDK header.
2190 #ifndef WIN32_LEAN_AND_MEAN
2191 #define WIN32_LEAN_AND_MEAN
2194 #include "windows.h"
2197 ** We need several support functions from the SQLite core.
2200 /* #include "sqlite3.h" */
2203 ** We need several things from the ANSI and MSVCRT headers.
2211 #include <sys/types.h>
2212 #include <sys/stat.h>
2215 ** We may need several defines that should have been in "sys/stat.h".
2219 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
2223 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
2227 #define S_ISLNK(mode) (0)
2231 ** We may need to provide the "mode_t" type.
2234 #ifndef MODE_T_DEFINED
2235 #define MODE_T_DEFINED
2236 typedef unsigned short mode_t;
2240 ** We may need to provide the "ino_t" type.
2243 #ifndef INO_T_DEFINED
2244 #define INO_T_DEFINED
2245 typedef unsigned short ino_t;
2249 ** We need to define "NAME_MAX" if it was not present in "limits.h".
2253 # ifdef FILENAME_MAX
2254 # define NAME_MAX (FILENAME_MAX)
2256 # define NAME_MAX (260)
2261 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
2264 #ifndef NULL_INTPTR_T
2265 # define NULL_INTPTR_T ((intptr_t)(0))
2268 #ifndef BAD_INTPTR_T
2269 # define BAD_INTPTR_T ((intptr_t)(-1))
2273 ** We need to provide the necessary structures and related types.
2276 #ifndef DIRENT_DEFINED
2277 #define DIRENT_DEFINED
2278 typedef struct DIRENT DIRENT;
2279 typedef DIRENT *LPDIRENT;
2281 ino_t d_ino; /* Sequence number, do not use. */
2282 unsigned d_attributes; /* Win32 file attributes. */
2283 char d_name[NAME_MAX + 1]; /* Name within the directory. */
2289 typedef struct DIR DIR;
2292 intptr_t d_handle; /* Value returned by "_findfirst". */
2293 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
2294 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
2299 ** Provide a macro, for use by the implementation, to determine if a
2300 ** particular directory entry should be skipped over when searching for
2301 ** the next directory entry that should be returned by the readdir() or
2302 ** readdir_r() functions.
2306 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
2310 ** Provide the function prototype for the POSIX compatible getenv()
2311 ** function. This function is not thread-safe.
2314 extern const char *windirent_getenv(const char *name);
2317 ** Finally, we can provide the function prototypes for the opendir(),
2318 ** readdir(), readdir_r(), and closedir() POSIX functions.
2321 extern LPDIR opendir(const char *dirname);
2322 extern LPDIRENT readdir(LPDIR dirp);
2323 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
2324 extern INT closedir(LPDIR dirp);
2326 #endif /* defined(WIN32) && defined(_MSC_VER) */
2328 /************************* End test_windirent.h ********************/
2329 /************************* Begin test_windirent.c ******************/
2333 ** The author disclaims copyright to this source code. In place of
2334 ** a legal notice, here is a blessing:
2336 ** May you do good and not evil.
2337 ** May you find forgiveness for yourself and forgive others.
2338 ** May you share freely, never taking more than you give.
2340 *************************************************************************
2341 ** This file contains code to implement most of the opendir() family of
2342 ** POSIX functions on Win32 using the MSVCRT.
2345 #if defined(_WIN32) && defined(_MSC_VER)
2346 /* #include "test_windirent.h" */
2349 ** Implementation of the POSIX getenv() function using the Win32 API.
2350 ** This function is not thread-safe.
2352 const char *windirent_getenv(
2355 static char value[32768]; /* Maximum length, per MSDN */
2356 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
2357 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
2359 memset(value, 0, sizeof(value));
2360 dwRet = GetEnvironmentVariableA(name, value, dwSize);
2361 if( dwRet==0 || dwRet>dwSize ){
2363 ** The function call to GetEnvironmentVariableA() failed -OR-
2364 ** the buffer is not large enough. Either way, return NULL.
2369 ** The function call to GetEnvironmentVariableA() succeeded
2370 ** -AND- the buffer contains the entire value.
2377 ** Implementation of the POSIX opendir() function using the MSVCRT.
2382 struct _finddata_t data;
2383 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
2384 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
2386 if( dirp==NULL ) return NULL;
2387 memset(dirp, 0, sizeof(DIR));
2389 /* TODO: Remove this if Unix-style root paths are not used. */
2390 if( sqlite3_stricmp(dirname, "/")==0 ){
2391 dirname = windirent_getenv("SystemDrive");
2394 memset(&data, 0, sizeof(struct _finddata_t));
2395 _snprintf(data.name, namesize, "%s\\*", dirname);
2396 dirp->d_handle = _findfirst(data.name, &data);
2398 if( dirp->d_handle==BAD_INTPTR_T ){
2403 /* TODO: Remove this block to allow hidden and/or system files. */
2404 if( is_filtered(data) ){
2407 memset(&data, 0, sizeof(struct _finddata_t));
2408 if( _findnext(dirp->d_handle, &data)==-1 ){
2413 /* TODO: Remove this block to allow hidden and/or system files. */
2414 if( is_filtered(data) ) goto next;
2417 dirp->d_first.d_attributes = data.attrib;
2418 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
2419 dirp->d_first.d_name[NAME_MAX] = '\0';
2425 ** Implementation of the POSIX readdir() function using the MSVCRT.
2430 struct _finddata_t data;
2432 if( dirp==NULL ) return NULL;
2434 if( dirp->d_first.d_ino==0 ){
2435 dirp->d_first.d_ino++;
2436 dirp->d_next.d_ino++;
2438 return &dirp->d_first;
2443 memset(&data, 0, sizeof(struct _finddata_t));
2444 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
2446 /* TODO: Remove this block to allow hidden and/or system files. */
2447 if( is_filtered(data) ) goto next;
2449 dirp->d_next.d_ino++;
2450 dirp->d_next.d_attributes = data.attrib;
2451 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
2452 dirp->d_next.d_name[NAME_MAX] = '\0';
2454 return &dirp->d_next;
2458 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
2465 struct _finddata_t data;
2467 if( dirp==NULL ) return EBADF;
2469 if( dirp->d_first.d_ino==0 ){
2470 dirp->d_first.d_ino++;
2471 dirp->d_next.d_ino++;
2473 entry->d_ino = dirp->d_first.d_ino;
2474 entry->d_attributes = dirp->d_first.d_attributes;
2475 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
2476 entry->d_name[NAME_MAX] = '\0';
2484 memset(&data, 0, sizeof(struct _finddata_t));
2485 if( _findnext(dirp->d_handle, &data)==-1 ){
2490 /* TODO: Remove this block to allow hidden and/or system files. */
2491 if( is_filtered(data) ) goto next;
2493 entry->d_ino = (ino_t)-1; /* not available */
2494 entry->d_attributes = data.attrib;
2495 strncpy(entry->d_name, data.name, NAME_MAX);
2496 entry->d_name[NAME_MAX] = '\0';
2503 ** Implementation of the POSIX closedir() function using the MSVCRT.
2510 if( dirp==NULL ) return EINVAL;
2512 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
2513 result = _findclose(dirp->d_handle);
2520 #endif /* defined(WIN32) && defined(_MSC_VER) */
2522 /************************* End test_windirent.c ********************/
2523 #define dirent DIRENT
2525 /************************* Begin ../ext/misc/memtrace.c ******************/
2529 ** The author disclaims copyright to this source code. In place of
2530 ** a legal notice, here is a blessing:
2532 ** May you do good and not evil.
2533 ** May you find forgiveness for yourself and forgive others.
2534 ** May you share freely, never taking more than you give.
2536 *************************************************************************
2538 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
2539 ** mechanism to add a tracing layer on top of SQLite. If this extension
2540 ** is registered prior to sqlite3_initialize(), it will cause all memory
2541 ** allocation activities to be logged on standard output, or to some other
2542 ** FILE specified by the initializer.
2544 ** This file needs to be compiled into the application that uses it.
2546 ** This extension is used to implement the --memtrace option of the
2547 ** command-line shell.
2553 /* The original memory allocation routines */
2554 static sqlite3_mem_methods memtraceBase;
2555 static FILE *memtraceOut;
2557 /* Methods that trace memory allocations */
2558 static void *memtraceMalloc(int n){
2560 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
2561 memtraceBase.xRoundup(n));
2563 return memtraceBase.xMalloc(n);
2565 static void memtraceFree(void *p){
2568 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
2570 memtraceBase.xFree(p);
2572 static void *memtraceRealloc(void *p, int n){
2573 if( p==0 ) return memtraceMalloc(n);
2579 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
2580 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
2582 return memtraceBase.xRealloc(p, n);
2584 static int memtraceSize(void *p){
2585 return memtraceBase.xSize(p);
2587 static int memtraceRoundup(int n){
2588 return memtraceBase.xRoundup(n);
2590 static int memtraceInit(void *p){
2591 return memtraceBase.xInit(p);
2593 static void memtraceShutdown(void *p){
2594 memtraceBase.xShutdown(p);
2597 /* The substitute memory allocator */
2598 static sqlite3_mem_methods ersaztMethods = {
2609 /* Begin tracing memory allocations to out. */
2610 int sqlite3MemTraceActivate(FILE *out){
2612 if( memtraceBase.xMalloc==0 ){
2613 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
2614 if( rc==SQLITE_OK ){
2615 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
2622 /* Deactivate memory tracing */
2623 int sqlite3MemTraceDeactivate(void){
2625 if( memtraceBase.xMalloc!=0 ){
2626 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
2627 if( rc==SQLITE_OK ){
2628 memset(&memtraceBase, 0, sizeof(memtraceBase));
2635 /************************* End ../ext/misc/memtrace.c ********************/
2636 /************************* Begin ../ext/misc/pcachetrace.c ******************/
2640 ** The author disclaims copyright to this source code. In place of
2641 ** a legal notice, here is a blessing:
2643 ** May you do good and not evil.
2644 ** May you find forgiveness for yourself and forgive others.
2645 ** May you share freely, never taking more than you give.
2647 *************************************************************************
2649 ** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2
2650 ** mechanism to add a tracing layer on top of pluggable page cache of
2651 ** SQLite. If this extension is registered prior to sqlite3_initialize(),
2652 ** it will cause all page cache activities to be logged on standard output,
2653 ** or to some other FILE specified by the initializer.
2655 ** This file needs to be compiled into the application that uses it.
2657 ** This extension is used to implement the --pcachetrace option of the
2658 ** command-line shell.
2664 /* The original page cache routines */
2665 static sqlite3_pcache_methods2 pcacheBase;
2666 static FILE *pcachetraceOut;
2668 /* Methods that trace pcache activity */
2669 static int pcachetraceInit(void *pArg){
2671 if( pcachetraceOut ){
2672 fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg);
2674 nRes = pcacheBase.xInit(pArg);
2675 if( pcachetraceOut ){
2676 fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes);
2680 static void pcachetraceShutdown(void *pArg){
2681 if( pcachetraceOut ){
2682 fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg);
2684 pcacheBase.xShutdown(pArg);
2686 static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){
2687 sqlite3_pcache *pRes;
2688 if( pcachetraceOut ){
2689 fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n",
2690 szPage, szExtra, bPurge);
2692 pRes = pcacheBase.xCreate(szPage, szExtra, bPurge);
2693 if( pcachetraceOut ){
2694 fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n",
2695 szPage, szExtra, bPurge, pRes);
2699 static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){
2700 if( pcachetraceOut ){
2701 fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize);
2703 pcacheBase.xCachesize(p, nCachesize);
2705 static int pcachetracePagecount(sqlite3_pcache *p){
2707 if( pcachetraceOut ){
2708 fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p);
2710 nRes = pcacheBase.xPagecount(p);
2711 if( pcachetraceOut ){
2712 fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes);
2716 static sqlite3_pcache_page *pcachetraceFetch(
2721 sqlite3_pcache_page *pRes;
2722 if( pcachetraceOut ){
2723 fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg);
2725 pRes = pcacheBase.xFetch(p, key, crFg);
2726 if( pcachetraceOut ){
2727 fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n",
2728 p, key, crFg, pRes);
2732 static void pcachetraceUnpin(
2734 sqlite3_pcache_page *pPg,
2737 if( pcachetraceOut ){
2738 fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n",
2741 pcacheBase.xUnpin(p, pPg, bDiscard);
2743 static void pcachetraceRekey(
2745 sqlite3_pcache_page *pPg,
2749 if( pcachetraceOut ){
2750 fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n",
2751 p, pPg, oldKey, newKey);
2753 pcacheBase.xRekey(p, pPg, oldKey, newKey);
2755 static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){
2756 if( pcachetraceOut ){
2757 fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n);
2759 pcacheBase.xTruncate(p, n);
2761 static void pcachetraceDestroy(sqlite3_pcache *p){
2762 if( pcachetraceOut ){
2763 fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p);
2765 pcacheBase.xDestroy(p);
2767 static void pcachetraceShrink(sqlite3_pcache *p){
2768 if( pcachetraceOut ){
2769 fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p);
2771 pcacheBase.xShrink(p);
2774 /* The substitute pcache methods */
2775 static sqlite3_pcache_methods2 ersaztPcacheMethods = {
2779 pcachetraceShutdown,
2781 pcachetraceCachesize,
2782 pcachetracePagecount,
2786 pcachetraceTruncate,
2791 /* Begin tracing memory allocations to out. */
2792 int sqlite3PcacheTraceActivate(FILE *out){
2794 if( pcacheBase.xFetch==0 ){
2795 rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase);
2796 if( rc==SQLITE_OK ){
2797 rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods);
2800 pcachetraceOut = out;
2804 /* Deactivate memory tracing */
2805 int sqlite3PcacheTraceDeactivate(void){
2807 if( pcacheBase.xFetch!=0 ){
2808 rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase);
2809 if( rc==SQLITE_OK ){
2810 memset(&pcacheBase, 0, sizeof(pcacheBase));
2817 /************************* End ../ext/misc/pcachetrace.c ********************/
2818 /************************* Begin ../ext/misc/shathree.c ******************/
2822 ** The author disclaims copyright to this source code. In place of
2823 ** a legal notice, here is a blessing:
2825 ** May you do good and not evil.
2826 ** May you find forgiveness for yourself and forgive others.
2827 ** May you share freely, never taking more than you give.
2829 ******************************************************************************
2831 ** This SQLite extension implements functions that compute SHA3 hashes
2832 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2833 ** Two SQL functions are implemented:
2836 ** sha3_query(Y,SIZE)
2838 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
2841 ** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
2842 ** and returns a hash of their results.
2844 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
2845 ** is used. If SIZE is included it must be one of the integers 224, 256,
2846 ** 384, or 512, to determine SHA3 hash variant that is computed.
2848 /* #include "sqlite3ext.h" */
2849 SQLITE_EXTENSION_INIT1
2854 #ifndef SQLITE_AMALGAMATION
2855 /* typedef sqlite3_uint64 u64; */
2856 #endif /* SQLITE_AMALGAMATION */
2858 /******************************************************************************
2862 ** Macros to determine whether the machine is big or little endian,
2863 ** and whether or not that determination is run-time or compile-time.
2865 ** For best performance, an attempt is made to guess at the byte-order
2866 ** using C-preprocessor macros. If that is unsuccessful, or if
2867 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
2870 #ifndef SHA3_BYTEORDER
2871 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
2872 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
2873 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
2875 # define SHA3_BYTEORDER 1234
2876 # elif defined(sparc) || defined(__ppc__)
2877 # define SHA3_BYTEORDER 4321
2879 # define SHA3_BYTEORDER 0
2885 ** State structure for a SHA3 hash in progress
2887 typedef struct SHA3Context SHA3Context;
2888 struct SHA3Context {
2890 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
2891 unsigned char x[1600]; /* ... or 1600 bytes */
2893 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
2894 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
2895 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
2899 ** A single step of the Keccak mixing function for a 1600-bit state
2901 static void KeccakF1600Step(SHA3Context *p){
2903 u64 b0, b1, b2, b3, b4;
2904 u64 c0, c1, c2, c3, c4;
2905 u64 d0, d1, d2, d3, d4;
2906 static const u64 RC[] = {
2907 0x0000000000000001ULL, 0x0000000000008082ULL,
2908 0x800000000000808aULL, 0x8000000080008000ULL,
2909 0x000000000000808bULL, 0x0000000080000001ULL,
2910 0x8000000080008081ULL, 0x8000000000008009ULL,
2911 0x000000000000008aULL, 0x0000000000000088ULL,
2912 0x0000000080008009ULL, 0x000000008000000aULL,
2913 0x000000008000808bULL, 0x800000000000008bULL,
2914 0x8000000000008089ULL, 0x8000000000008003ULL,
2915 0x8000000000008002ULL, 0x8000000000000080ULL,
2916 0x000000000000800aULL, 0x800000008000000aULL,
2917 0x8000000080008081ULL, 0x8000000000008080ULL,
2918 0x0000000080000001ULL, 0x8000000080008008ULL
2920 # define a00 (p->u.s[0])
2921 # define a01 (p->u.s[1])
2922 # define a02 (p->u.s[2])
2923 # define a03 (p->u.s[3])
2924 # define a04 (p->u.s[4])
2925 # define a10 (p->u.s[5])
2926 # define a11 (p->u.s[6])
2927 # define a12 (p->u.s[7])
2928 # define a13 (p->u.s[8])
2929 # define a14 (p->u.s[9])
2930 # define a20 (p->u.s[10])
2931 # define a21 (p->u.s[11])
2932 # define a22 (p->u.s[12])
2933 # define a23 (p->u.s[13])
2934 # define a24 (p->u.s[14])
2935 # define a30 (p->u.s[15])
2936 # define a31 (p->u.s[16])
2937 # define a32 (p->u.s[17])
2938 # define a33 (p->u.s[18])
2939 # define a34 (p->u.s[19])
2940 # define a40 (p->u.s[20])
2941 # define a41 (p->u.s[21])
2942 # define a42 (p->u.s[22])
2943 # define a43 (p->u.s[23])
2944 # define a44 (p->u.s[24])
2945 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
2947 for(i=0; i<24; i+=4){
2948 c0 = a00^a10^a20^a30^a40;
2949 c1 = a01^a11^a21^a31^a41;
2950 c2 = a02^a12^a22^a32^a42;
2951 c3 = a03^a13^a23^a33^a43;
2952 c4 = a04^a14^a24^a34^a44;
2953 d0 = c4^ROL64(c1, 1);
2954 d1 = c0^ROL64(c2, 1);
2955 d2 = c1^ROL64(c3, 1);
2956 d3 = c2^ROL64(c4, 1);
2957 d4 = c3^ROL64(c0, 1);
2960 b1 = ROL64((a11^d1), 44);
2961 b2 = ROL64((a22^d2), 43);
2962 b3 = ROL64((a33^d3), 21);
2963 b4 = ROL64((a44^d4), 14);
2964 a00 = b0 ^((~b1)& b2 );
2966 a11 = b1 ^((~b2)& b3 );
2967 a22 = b2 ^((~b3)& b4 );
2968 a33 = b3 ^((~b4)& b0 );
2969 a44 = b4 ^((~b0)& b1 );
2971 b2 = ROL64((a20^d0), 3);
2972 b3 = ROL64((a31^d1), 45);
2973 b4 = ROL64((a42^d2), 61);
2974 b0 = ROL64((a03^d3), 28);
2975 b1 = ROL64((a14^d4), 20);
2976 a20 = b0 ^((~b1)& b2 );
2977 a31 = b1 ^((~b2)& b3 );
2978 a42 = b2 ^((~b3)& b4 );
2979 a03 = b3 ^((~b4)& b0 );
2980 a14 = b4 ^((~b0)& b1 );
2982 b4 = ROL64((a40^d0), 18);
2983 b0 = ROL64((a01^d1), 1);
2984 b1 = ROL64((a12^d2), 6);
2985 b2 = ROL64((a23^d3), 25);
2986 b3 = ROL64((a34^d4), 8);
2987 a40 = b0 ^((~b1)& b2 );
2988 a01 = b1 ^((~b2)& b3 );
2989 a12 = b2 ^((~b3)& b4 );
2990 a23 = b3 ^((~b4)& b0 );
2991 a34 = b4 ^((~b0)& b1 );
2993 b1 = ROL64((a10^d0), 36);
2994 b2 = ROL64((a21^d1), 10);
2995 b3 = ROL64((a32^d2), 15);
2996 b4 = ROL64((a43^d3), 56);
2997 b0 = ROL64((a04^d4), 27);
2998 a10 = b0 ^((~b1)& b2 );
2999 a21 = b1 ^((~b2)& b3 );
3000 a32 = b2 ^((~b3)& b4 );
3001 a43 = b3 ^((~b4)& b0 );
3002 a04 = b4 ^((~b0)& b1 );
3004 b3 = ROL64((a30^d0), 41);
3005 b4 = ROL64((a41^d1), 2);
3006 b0 = ROL64((a02^d2), 62);
3007 b1 = ROL64((a13^d3), 55);
3008 b2 = ROL64((a24^d4), 39);
3009 a30 = b0 ^((~b1)& b2 );
3010 a41 = b1 ^((~b2)& b3 );
3011 a02 = b2 ^((~b3)& b4 );
3012 a13 = b3 ^((~b4)& b0 );
3013 a24 = b4 ^((~b0)& b1 );
3015 c0 = a00^a20^a40^a10^a30;
3016 c1 = a11^a31^a01^a21^a41;
3017 c2 = a22^a42^a12^a32^a02;
3018 c3 = a33^a03^a23^a43^a13;
3019 c4 = a44^a14^a34^a04^a24;
3020 d0 = c4^ROL64(c1, 1);
3021 d1 = c0^ROL64(c2, 1);
3022 d2 = c1^ROL64(c3, 1);
3023 d3 = c2^ROL64(c4, 1);
3024 d4 = c3^ROL64(c0, 1);
3027 b1 = ROL64((a31^d1), 44);
3028 b2 = ROL64((a12^d2), 43);
3029 b3 = ROL64((a43^d3), 21);
3030 b4 = ROL64((a24^d4), 14);
3031 a00 = b0 ^((~b1)& b2 );
3033 a31 = b1 ^((~b2)& b3 );
3034 a12 = b2 ^((~b3)& b4 );
3035 a43 = b3 ^((~b4)& b0 );
3036 a24 = b4 ^((~b0)& b1 );
3038 b2 = ROL64((a40^d0), 3);
3039 b3 = ROL64((a21^d1), 45);
3040 b4 = ROL64((a02^d2), 61);
3041 b0 = ROL64((a33^d3), 28);
3042 b1 = ROL64((a14^d4), 20);
3043 a40 = b0 ^((~b1)& b2 );
3044 a21 = b1 ^((~b2)& b3 );
3045 a02 = b2 ^((~b3)& b4 );
3046 a33 = b3 ^((~b4)& b0 );
3047 a14 = b4 ^((~b0)& b1 );
3049 b4 = ROL64((a30^d0), 18);
3050 b0 = ROL64((a11^d1), 1);
3051 b1 = ROL64((a42^d2), 6);
3052 b2 = ROL64((a23^d3), 25);
3053 b3 = ROL64((a04^d4), 8);
3054 a30 = b0 ^((~b1)& b2 );
3055 a11 = b1 ^((~b2)& b3 );
3056 a42 = b2 ^((~b3)& b4 );
3057 a23 = b3 ^((~b4)& b0 );
3058 a04 = b4 ^((~b0)& b1 );
3060 b1 = ROL64((a20^d0), 36);
3061 b2 = ROL64((a01^d1), 10);
3062 b3 = ROL64((a32^d2), 15);
3063 b4 = ROL64((a13^d3), 56);
3064 b0 = ROL64((a44^d4), 27);
3065 a20 = b0 ^((~b1)& b2 );
3066 a01 = b1 ^((~b2)& b3 );
3067 a32 = b2 ^((~b3)& b4 );
3068 a13 = b3 ^((~b4)& b0 );
3069 a44 = b4 ^((~b0)& b1 );
3071 b3 = ROL64((a10^d0), 41);
3072 b4 = ROL64((a41^d1), 2);
3073 b0 = ROL64((a22^d2), 62);
3074 b1 = ROL64((a03^d3), 55);
3075 b2 = ROL64((a34^d4), 39);
3076 a10 = b0 ^((~b1)& b2 );
3077 a41 = b1 ^((~b2)& b3 );
3078 a22 = b2 ^((~b3)& b4 );
3079 a03 = b3 ^((~b4)& b0 );
3080 a34 = b4 ^((~b0)& b1 );
3082 c0 = a00^a40^a30^a20^a10;
3083 c1 = a31^a21^a11^a01^a41;
3084 c2 = a12^a02^a42^a32^a22;
3085 c3 = a43^a33^a23^a13^a03;
3086 c4 = a24^a14^a04^a44^a34;
3087 d0 = c4^ROL64(c1, 1);
3088 d1 = c0^ROL64(c2, 1);
3089 d2 = c1^ROL64(c3, 1);
3090 d3 = c2^ROL64(c4, 1);
3091 d4 = c3^ROL64(c0, 1);
3094 b1 = ROL64((a21^d1), 44);
3095 b2 = ROL64((a42^d2), 43);
3096 b3 = ROL64((a13^d3), 21);
3097 b4 = ROL64((a34^d4), 14);
3098 a00 = b0 ^((~b1)& b2 );
3100 a21 = b1 ^((~b2)& b3 );
3101 a42 = b2 ^((~b3)& b4 );
3102 a13 = b3 ^((~b4)& b0 );
3103 a34 = b4 ^((~b0)& b1 );
3105 b2 = ROL64((a30^d0), 3);
3106 b3 = ROL64((a01^d1), 45);
3107 b4 = ROL64((a22^d2), 61);
3108 b0 = ROL64((a43^d3), 28);
3109 b1 = ROL64((a14^d4), 20);
3110 a30 = b0 ^((~b1)& b2 );
3111 a01 = b1 ^((~b2)& b3 );
3112 a22 = b2 ^((~b3)& b4 );
3113 a43 = b3 ^((~b4)& b0 );
3114 a14 = b4 ^((~b0)& b1 );
3116 b4 = ROL64((a10^d0), 18);
3117 b0 = ROL64((a31^d1), 1);
3118 b1 = ROL64((a02^d2), 6);
3119 b2 = ROL64((a23^d3), 25);
3120 b3 = ROL64((a44^d4), 8);
3121 a10 = b0 ^((~b1)& b2 );
3122 a31 = b1 ^((~b2)& b3 );
3123 a02 = b2 ^((~b3)& b4 );
3124 a23 = b3 ^((~b4)& b0 );
3125 a44 = b4 ^((~b0)& b1 );
3127 b1 = ROL64((a40^d0), 36);
3128 b2 = ROL64((a11^d1), 10);
3129 b3 = ROL64((a32^d2), 15);
3130 b4 = ROL64((a03^d3), 56);
3131 b0 = ROL64((a24^d4), 27);
3132 a40 = b0 ^((~b1)& b2 );
3133 a11 = b1 ^((~b2)& b3 );
3134 a32 = b2 ^((~b3)& b4 );
3135 a03 = b3 ^((~b4)& b0 );
3136 a24 = b4 ^((~b0)& b1 );
3138 b3 = ROL64((a20^d0), 41);
3139 b4 = ROL64((a41^d1), 2);
3140 b0 = ROL64((a12^d2), 62);
3141 b1 = ROL64((a33^d3), 55);
3142 b2 = ROL64((a04^d4), 39);
3143 a20 = b0 ^((~b1)& b2 );
3144 a41 = b1 ^((~b2)& b3 );
3145 a12 = b2 ^((~b3)& b4 );
3146 a33 = b3 ^((~b4)& b0 );
3147 a04 = b4 ^((~b0)& b1 );
3149 c0 = a00^a30^a10^a40^a20;
3150 c1 = a21^a01^a31^a11^a41;
3151 c2 = a42^a22^a02^a32^a12;
3152 c3 = a13^a43^a23^a03^a33;
3153 c4 = a34^a14^a44^a24^a04;
3154 d0 = c4^ROL64(c1, 1);
3155 d1 = c0^ROL64(c2, 1);
3156 d2 = c1^ROL64(c3, 1);
3157 d3 = c2^ROL64(c4, 1);
3158 d4 = c3^ROL64(c0, 1);
3161 b1 = ROL64((a01^d1), 44);
3162 b2 = ROL64((a02^d2), 43);
3163 b3 = ROL64((a03^d3), 21);
3164 b4 = ROL64((a04^d4), 14);
3165 a00 = b0 ^((~b1)& b2 );
3167 a01 = b1 ^((~b2)& b3 );
3168 a02 = b2 ^((~b3)& b4 );
3169 a03 = b3 ^((~b4)& b0 );
3170 a04 = b4 ^((~b0)& b1 );
3172 b2 = ROL64((a10^d0), 3);
3173 b3 = ROL64((a11^d1), 45);
3174 b4 = ROL64((a12^d2), 61);
3175 b0 = ROL64((a13^d3), 28);
3176 b1 = ROL64((a14^d4), 20);
3177 a10 = b0 ^((~b1)& b2 );
3178 a11 = b1 ^((~b2)& b3 );
3179 a12 = b2 ^((~b3)& b4 );
3180 a13 = b3 ^((~b4)& b0 );
3181 a14 = b4 ^((~b0)& b1 );
3183 b4 = ROL64((a20^d0), 18);
3184 b0 = ROL64((a21^d1), 1);
3185 b1 = ROL64((a22^d2), 6);
3186 b2 = ROL64((a23^d3), 25);
3187 b3 = ROL64((a24^d4), 8);
3188 a20 = b0 ^((~b1)& b2 );
3189 a21 = b1 ^((~b2)& b3 );
3190 a22 = b2 ^((~b3)& b4 );
3191 a23 = b3 ^((~b4)& b0 );
3192 a24 = b4 ^((~b0)& b1 );
3194 b1 = ROL64((a30^d0), 36);
3195 b2 = ROL64((a31^d1), 10);
3196 b3 = ROL64((a32^d2), 15);
3197 b4 = ROL64((a33^d3), 56);
3198 b0 = ROL64((a34^d4), 27);
3199 a30 = b0 ^((~b1)& b2 );
3200 a31 = b1 ^((~b2)& b3 );
3201 a32 = b2 ^((~b3)& b4 );
3202 a33 = b3 ^((~b4)& b0 );
3203 a34 = b4 ^((~b0)& b1 );
3205 b3 = ROL64((a40^d0), 41);
3206 b4 = ROL64((a41^d1), 2);
3207 b0 = ROL64((a42^d2), 62);
3208 b1 = ROL64((a43^d3), 55);
3209 b2 = ROL64((a44^d4), 39);
3210 a40 = b0 ^((~b1)& b2 );
3211 a41 = b1 ^((~b2)& b3 );
3212 a42 = b2 ^((~b3)& b4 );
3213 a43 = b3 ^((~b4)& b0 );
3214 a44 = b4 ^((~b0)& b1 );
3219 ** Initialize a new hash. iSize determines the size of the hash
3220 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
3221 ** can be zero to use the default hash size of 256 bits.
3223 static void SHA3Init(SHA3Context *p, int iSize){
3224 memset(p, 0, sizeof(*p));
3225 if( iSize>=128 && iSize<=512 ){
3226 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
3228 p->nRate = (1600 - 2*256)/8;
3230 #if SHA3_BYTEORDER==1234
3231 /* Known to be little-endian at compile-time. No-op */
3232 #elif SHA3_BYTEORDER==4321
3233 p->ixMask = 7; /* Big-endian */
3236 static unsigned int one = 1;
3237 if( 1==*(unsigned char*)&one ){
3238 /* Little endian. No byte swapping. */
3241 /* Big endian. Byte swap. */
3249 ** Make consecutive calls to the SHA3Update function to add new content
3252 static void SHA3Update(
3254 const unsigned char *aData,
3258 if( aData==0 ) return;
3259 #if SHA3_BYTEORDER==1234
3260 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
3261 for(; i+7<nData; i+=8){
3262 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
3264 if( p->nLoaded>=p->nRate ){
3271 for(; i<nData; i++){
3272 #if SHA3_BYTEORDER==1234
3273 p->u.x[p->nLoaded] ^= aData[i];
3274 #elif SHA3_BYTEORDER==4321
3275 p->u.x[p->nLoaded^0x07] ^= aData[i];
3277 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
3280 if( p->nLoaded==p->nRate ){
3288 ** After all content has been added, invoke SHA3Final() to compute
3289 ** the final hash. The function returns a pointer to the binary
3292 static unsigned char *SHA3Final(SHA3Context *p){
3294 if( p->nLoaded==p->nRate-1 ){
3295 const unsigned char c1 = 0x86;
3296 SHA3Update(p, &c1, 1);
3298 const unsigned char c2 = 0x06;
3299 const unsigned char c3 = 0x80;
3300 SHA3Update(p, &c2, 1);
3301 p->nLoaded = p->nRate - 1;
3302 SHA3Update(p, &c3, 1);
3304 for(i=0; i<p->nRate; i++){
3305 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
3307 return &p->u.x[p->nRate];
3309 /* End of the hashing logic
3310 *****************************************************************************/
3313 ** Implementation of the sha3(X,SIZE) function.
3315 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
3316 ** size is 256. If X is a BLOB, it is hashed as is.
3317 ** For all other non-NULL types of input, X is converted into a UTF-8 string
3318 ** and the string is hashed without the trailing 0x00 terminator. The hash
3319 ** of a NULL value is NULL.
3321 static void sha3Func(
3322 sqlite3_context *context,
3324 sqlite3_value **argv
3327 int eType = sqlite3_value_type(argv[0]);
3328 int nByte = sqlite3_value_bytes(argv[0]);
3333 iSize = sqlite3_value_int(argv[1]);
3334 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
3335 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
3340 if( eType==SQLITE_NULL ) return;
3341 SHA3Init(&cx, iSize);
3342 if( eType==SQLITE_BLOB ){
3343 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
3345 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
3347 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
3350 /* Compute a string using sqlite3_vsnprintf() with a maximum length
3351 ** of 50 bytes and add it to the hash.
3353 static void sha3_step_vformat(
3354 SHA3Context *p, /* Add content to this context */
3355 const char *zFormat,
3361 va_start(ap, zFormat);
3362 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
3364 n = (int)strlen(zBuf);
3365 SHA3Update(p, (unsigned char*)zBuf, n);
3369 ** Implementation of the sha3_query(SQL,SIZE) function.
3371 ** This function compiles and runs the SQL statement(s) given in the
3372 ** argument. The results are hashed using a SIZE-bit SHA3. The default
3375 ** The format of the byte stream that is hashed is summarized as follows:
3385 ** <sql> is the original SQL text for each statement run and <n> is
3386 ** the size of that text. The SQL text is UTF-8. A single R character
3387 ** occurs before the start of each row. N means a NULL value.
3388 ** I mean an 8-byte little-endian integer <int>. F is a floating point
3389 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
3390 ** B means blobs of <size> bytes. T means text rendered as <size>
3391 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
3394 ** For each SQL statement in the X input, there is one S segment. Each
3395 ** S segment is followed by zero or more R segments, one for each row in the
3396 ** result set. After each R, there are one or more N, I, F, B, or T segments,
3397 ** one for each column in the result set. Segments are concatentated directly
3398 ** with no delimiters of any kind.
3400 static void sha3QueryFunc(
3401 sqlite3_context *context,
3403 sqlite3_value **argv
3405 sqlite3 *db = sqlite3_context_db_handle(context);
3406 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
3407 sqlite3_stmt *pStmt = 0;
3408 int nCol; /* Number of columns in the result set */
3409 int i; /* Loop counter */
3419 iSize = sqlite3_value_int(argv[1]);
3420 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
3421 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
3426 if( zSql==0 ) return;
3427 SHA3Init(&cx, iSize);
3429 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
3431 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
3432 zSql, sqlite3_errmsg(db));
3433 sqlite3_finalize(pStmt);
3434 sqlite3_result_error(context, zMsg, -1);
3438 if( !sqlite3_stmt_readonly(pStmt) ){
3439 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
3440 sqlite3_finalize(pStmt);
3441 sqlite3_result_error(context, zMsg, -1);
3445 nCol = sqlite3_column_count(pStmt);
3446 z = sqlite3_sql(pStmt);
3449 sha3_step_vformat(&cx,"S%d:",n);
3450 SHA3Update(&cx,(unsigned char*)z,n);
3453 /* Compute a hash over the result of the query */
3454 while( SQLITE_ROW==sqlite3_step(pStmt) ){
3455 SHA3Update(&cx,(const unsigned char*)"R",1);
3456 for(i=0; i<nCol; i++){
3457 switch( sqlite3_column_type(pStmt,i) ){
3459 SHA3Update(&cx, (const unsigned char*)"N",1);
3462 case SQLITE_INTEGER: {
3466 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
3468 for(j=8; j>=1; j--){
3473 SHA3Update(&cx, x, 9);
3476 case SQLITE_FLOAT: {
3480 double r = sqlite3_column_double(pStmt,i);
3482 for(j=8; j>=1; j--){
3487 SHA3Update(&cx,x,9);
3491 int n2 = sqlite3_column_bytes(pStmt, i);
3492 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
3493 sha3_step_vformat(&cx,"T%d:",n2);
3494 SHA3Update(&cx, z2, n2);
3498 int n2 = sqlite3_column_bytes(pStmt, i);
3499 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
3500 sha3_step_vformat(&cx,"B%d:",n2);
3501 SHA3Update(&cx, z2, n2);
3507 sqlite3_finalize(pStmt);
3509 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
3516 int sqlite3_shathree_init(
3519 const sqlite3_api_routines *pApi
3522 SQLITE_EXTENSION_INIT2(pApi);
3523 (void)pzErrMsg; /* Unused parameter */
3524 rc = sqlite3_create_function(db, "sha3", 1,
3525 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
3527 if( rc==SQLITE_OK ){
3528 rc = sqlite3_create_function(db, "sha3", 2,
3529 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
3532 if( rc==SQLITE_OK ){
3533 rc = sqlite3_create_function(db, "sha3_query", 1,
3534 SQLITE_UTF8 | SQLITE_DIRECTONLY,
3535 0, sha3QueryFunc, 0, 0);
3537 if( rc==SQLITE_OK ){
3538 rc = sqlite3_create_function(db, "sha3_query", 2,
3539 SQLITE_UTF8 | SQLITE_DIRECTONLY,
3540 0, sha3QueryFunc, 0, 0);
3545 /************************* End ../ext/misc/shathree.c ********************/
3546 /************************* Begin ../ext/misc/uint.c ******************/
3550 ** The author disclaims copyright to this source code. In place of
3551 ** a legal notice, here is a blessing:
3553 ** May you do good and not evil.
3554 ** May you find forgiveness for yourself and forgive others.
3555 ** May you share freely, never taking more than you give.
3557 ******************************************************************************
3559 ** This SQLite extension implements the UINT collating sequence.
3561 ** UINT works like BINARY for text, except that embedded strings
3562 ** of digits compare in numeric order.
3564 ** * Leading zeros are handled properly, in the sense that
3565 ** they do not mess of the maginitude comparison of embedded
3566 ** strings of digits. "x00123y" is equal to "x123y".
3568 ** * Only unsigned integers are recognized. Plus and minus
3569 ** signs are ignored. Decimal points and exponential notation
3572 ** * Embedded integers can be of arbitrary length. Comparison
3573 ** is *not* limited integers that can be expressed as a
3574 ** 64-bit machine integer.
3576 /* #include "sqlite3ext.h" */
3577 SQLITE_EXTENSION_INIT1
3583 ** Compare text in lexicographic order, except strings of digits
3584 ** compare in numeric order.
3586 static int uintCollFunc(
3588 int nKey1, const void *pKey1,
3589 int nKey2, const void *pKey2
3591 const unsigned char *zA = (const unsigned char*)pKey1;
3592 const unsigned char *zB = (const unsigned char*)pKey2;
3595 while( i<nKey1 && j<nKey2 ){
3597 if( isdigit(zA[i]) ){
3599 if( !isdigit(zB[j]) ) return x;
3600 while( i<nKey1 && zA[i]=='0' ){ i++; }
3601 while( j<nKey2 && zB[j]=='0' ){ j++; }
3603 while( i+k<nKey1 && isdigit(zA[i+k])
3604 && j+k<nKey2 && isdigit(zB[j+k]) ){
3607 if( i+k<nKey1 && isdigit(zA[i+k]) ){
3609 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
3612 x = memcmp(zA+i, zB+j, k);
3624 return (nKey1 - i) - (nKey2 - j);
3630 int sqlite3_uint_init(
3633 const sqlite3_api_routines *pApi
3635 SQLITE_EXTENSION_INIT2(pApi);
3636 (void)pzErrMsg; /* Unused parameter */
3637 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
3640 /************************* End ../ext/misc/uint.c ********************/
3641 /************************* Begin ../ext/misc/decimal.c ******************/
3645 ** The author disclaims copyright to this source code. In place of
3646 ** a legal notice, here is a blessing:
3648 ** May you do good and not evil.
3649 ** May you find forgiveness for yourself and forgive others.
3650 ** May you share freely, never taking more than you give.
3652 ******************************************************************************
3654 ** Routines to implement arbitrary-precision decimal math.
3656 ** The focus here is on simplicity and correctness, not performance.
3658 /* #include "sqlite3ext.h" */
3659 SQLITE_EXTENSION_INIT1
3665 /* Mark a function parameter as unused, to suppress nuisance compiler
3667 #ifndef UNUSED_PARAMETER
3668 # define UNUSED_PARAMETER(X) (void)(X)
3672 /* A decimal object */
3673 typedef struct Decimal Decimal;
3675 char sign; /* 0 for positive, 1 for negative */
3676 char oom; /* True if an OOM is encountered */
3677 char isNull; /* True if holds a NULL rather than a number */
3678 char isInit; /* True upon initialization */
3679 int nDigit; /* Total number of digits */
3680 int nFrac; /* Number of digits to the right of the decimal point */
3681 signed char *a; /* Array of digits. Most significant first. */
3685 ** Release memory held by a Decimal, but do not free the object itself.
3687 static void decimal_clear(Decimal *p){
3692 ** Destroy a Decimal object
3694 static void decimal_free(Decimal *p){
3702 ** Allocate a new Decimal object initialized to the text in zIn[].
3703 ** Return NULL if any kind of error occurs.
3705 static Decimal *decimalNewFromText(const char *zIn, int n){
3710 p = sqlite3_malloc( sizeof(*p) );
3711 if( p==0 ) goto new_from_text_failed;
3718 p->a = sqlite3_malloc64( n+1 );
3719 if( p->a==0 ) goto new_from_text_failed;
3720 for(i=0; isspace(zIn[i]); i++){}
3724 }else if( zIn[i]=='+' ){
3727 while( i<n && zIn[i]=='0' ) i++;
3730 if( c>='0' && c<='9' ){
3731 p->a[p->nDigit++] = c - '0';
3733 p->nFrac = p->nDigit + 1;
3734 }else if( c=='e' || c=='E' ){
3741 }else if( zIn[j]=='+' ){
3744 while( j<n && iExp<1000000 ){
3745 if( zIn[j]>='0' && zIn[j]<='9' ){
3746 iExp = iExp*10 + zIn[j] - '0';
3750 if( neg ) iExp = -iExp;
3756 p->nFrac = p->nDigit - (p->nFrac - 1);
3760 if( iExp<=p->nFrac ){
3769 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3770 if( p->a==0 ) goto new_from_text_failed;
3771 memset(p->a+p->nDigit, 0, iExp);
3777 nExtra = p->nDigit - p->nFrac - 1;
3784 p->nFrac = p->nDigit - 1;
3788 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3789 if( p->a==0 ) goto new_from_text_failed;
3790 memmove(p->a+iExp, p->a, p->nDigit);
3791 memset(p->a, 0, iExp);
3798 new_from_text_failed:
3800 if( p->a ) sqlite3_free(p->a);
3806 /* Forward reference */
3807 static Decimal *decimalFromDouble(double);
3810 ** Allocate a new Decimal object from an sqlite3_value. Return a pointer
3811 ** to the new object, or NULL if there is an error. If the pCtx argument
3812 ** is not NULL, then errors are reported on it as well.
3814 ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
3815 ** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length
3816 ** 8 bytes, the resulting double value is expanded into its decimal equivalent.
3817 ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
3818 ** then NULL is returned.
3820 static Decimal *decimal_new(
3821 sqlite3_context *pCtx, /* Report error here, if not null */
3822 sqlite3_value *pIn, /* Construct the decimal object from this */
3823 int bTextOnly /* Always interpret pIn as text if true */
3826 int eType = sqlite3_value_type(pIn);
3827 if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
3828 eType = SQLITE_TEXT;
3832 case SQLITE_INTEGER: {
3833 const char *zIn = (const char*)sqlite3_value_text(pIn);
3834 int n = sqlite3_value_bytes(pIn);
3835 p = decimalNewFromText(zIn, n);
3836 if( p==0 ) goto new_failed;
3840 case SQLITE_FLOAT: {
3841 p = decimalFromDouble(sqlite3_value_double(pIn));
3846 const unsigned char *x;
3848 sqlite3_uint64 v = 0;
3851 if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
3852 x = sqlite3_value_blob(pIn);
3853 for(i=0; i<sizeof(r); i++){
3856 memcpy(&r, &v, sizeof(r));
3857 p = decimalFromDouble(r);
3868 if( pCtx ) sqlite3_result_error_nomem(pCtx);
3874 ** Make the given Decimal the result.
3876 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
3880 if( p==0 || p->oom ){
3881 sqlite3_result_error_nomem(pCtx);
3885 sqlite3_result_null(pCtx);
3888 z = sqlite3_malloc( p->nDigit+4 );
3890 sqlite3_result_error_nomem(pCtx);
3894 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
3901 n = p->nDigit - p->nFrac;
3906 while( n>1 && p->a[j]==0 ){
3911 z[i++] = p->a[j] + '0';
3918 z[i++] = p->a[j] + '0';
3920 }while( j<p->nDigit );
3923 sqlite3_result_text(pCtx, z, i, sqlite3_free);
3927 ** Make the given Decimal the result in an format similar to '%+#e'.
3928 ** In other words, show exponential notation with leading and trailing
3931 static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
3932 char *z; /* The output buffer */
3933 int i; /* Loop counter */
3934 int nZero; /* Number of leading zeros */
3935 int nDigit; /* Number of digits not counting trailing zeros */
3936 int nFrac; /* Digits to the right of the decimal point */
3937 int exp; /* Exponent value */
3938 signed char zero; /* Zero value */
3939 signed char *a; /* Array of digits */
3941 if( p==0 || p->oom ){
3942 sqlite3_result_error_nomem(pCtx);
3946 sqlite3_result_null(pCtx);
3949 for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3950 for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3951 nFrac = p->nFrac + (nDigit - p->nDigit);
3953 z = sqlite3_malloc( nDigit+20 );
3955 sqlite3_result_error_nomem(pCtx);
3966 if( p->sign && nDigit>0 ){
3977 for(i=1; i<nDigit; i++){
3982 exp = nDigit - nFrac - 1;
3983 sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
3984 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
3988 ** Compare to Decimal objects. Return negative, 0, or positive if the
3989 ** first object is less than, equal to, or greater than the second.
3991 ** Preconditions for this routine:
3998 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
3999 int nASig, nBSig, rc, n;
4000 if( pA->sign!=pB->sign ){
4001 return pA->sign ? -1 : +1;
4004 const Decimal *pTemp = pA;
4008 nASig = pA->nDigit - pA->nFrac;
4009 nBSig = pB->nDigit - pB->nFrac;
4011 return nASig - nBSig;
4014 if( n>pB->nDigit ) n = pB->nDigit;
4015 rc = memcmp(pA->a, pB->a, n);
4017 rc = pA->nDigit - pB->nDigit;
4023 ** SQL Function: decimal_cmp(X, Y)
4025 ** Return negative, zero, or positive if X is less then, equal to, or
4028 static void decimalCmpFunc(
4029 sqlite3_context *context,
4031 sqlite3_value **argv
4033 Decimal *pA = 0, *pB = 0;
4036 UNUSED_PARAMETER(argc);
4037 pA = decimal_new(context, argv[0], 1);
4038 if( pA==0 || pA->isNull ) goto cmp_done;
4039 pB = decimal_new(context, argv[1], 1);
4040 if( pB==0 || pB->isNull ) goto cmp_done;
4041 rc = decimal_cmp(pA, pB);
4043 else if( rc>0 ) rc = +1;
4044 sqlite3_result_int(context, rc);
4051 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4052 ** digits to the right of the decimal point.
4054 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4058 nAddFrac = nFrac - p->nFrac;
4059 nAddSig = (nDigit - p->nDigit) - nAddFrac;
4060 if( nAddFrac==0 && nAddSig==0 ) return;
4061 p->a = sqlite3_realloc64(p->a, nDigit+1);
4067 memmove(p->a+nAddSig, p->a, p->nDigit);
4068 memset(p->a, 0, nAddSig);
4069 p->nDigit += nAddSig;
4072 memset(p->a+p->nDigit, 0, nAddFrac);
4073 p->nDigit += nAddFrac;
4074 p->nFrac += nAddFrac;
4079 ** Add the value pB into pA. A := A + B.
4081 ** Both pA and pB might become denormalized by this routine.
4083 static void decimal_add(Decimal *pA, Decimal *pB){
4084 int nSig, nFrac, nDigit;
4089 if( pA->oom || pB==0 || pB->oom ){
4093 if( pA->isNull || pB->isNull ){
4097 nSig = pA->nDigit - pA->nFrac;
4098 if( nSig && pA->a[0]==0 ) nSig--;
4099 if( nSig<pB->nDigit-pB->nFrac ){
4100 nSig = pB->nDigit - pB->nFrac;
4103 if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4104 nDigit = nSig + nFrac + 1;
4105 decimal_expand(pA, nDigit, nFrac);
4106 decimal_expand(pB, nDigit, nFrac);
4107 if( pA->oom || pB->oom ){
4110 if( pA->sign==pB->sign ){
4112 for(i=nDigit-1; i>=0; i--){
4113 int x = pA->a[i] + pB->a[i] + carry;
4123 signed char *aA, *aB;
4125 rc = memcmp(pA->a, pB->a, nDigit);
4129 pA->sign = !pA->sign;
4134 for(i=nDigit-1; i>=0; i--){
4135 int x = aA[i] - aB[i] - borrow;
4149 ** Multiply A by B. A := A * B
4151 ** All significant digits after the decimal point are retained.
4152 ** Trailing zeros after the decimal point are omitted as long as
4153 ** the number of digits after the decimal point is no less than
4154 ** either the number of digits in either input.
4156 static void decimalMul(Decimal *pA, Decimal *pB){
4157 signed char *acc = 0;
4161 if( pA==0 || pA->oom || pA->isNull
4162 || pB==0 || pB->oom || pB->isNull
4166 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4171 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
4172 minFrac = pA->nFrac;
4173 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
4174 for(i=pA->nDigit-1; i>=0; i--){
4175 signed char f = pA->a[i];
4177 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
4178 x = acc[k] + f*pB->a[j] + carry;
4186 sqlite3_free(pA->a);
4189 pA->nDigit += pB->nDigit + 2;
4190 pA->nFrac += pB->nFrac;
4191 pA->sign ^= pB->sign;
4192 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
4202 ** Create a new Decimal object that contains an integer power of 2.
4204 static Decimal *decimalPow2(int N){
4205 Decimal *pA = 0; /* The result to be returned */
4206 Decimal *pX = 0; /* Multiplier */
4207 if( N<-20000 || N>20000 ) goto pow2_fault;
4208 pA = decimalNewFromText("1.0", 3);
4209 if( pA==0 || pA->oom ) goto pow2_fault;
4210 if( N==0 ) return pA;
4212 pX = decimalNewFromText("2.0", 3);
4215 pX = decimalNewFromText("0.5", 3);
4217 if( pX==0 || pX->oom ) goto pow2_fault;
4218 while( 1 /* Exit by break */ ){
4221 if( pA->oom ) goto pow2_fault;
4237 ** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
4239 static Decimal *decimalFromDouble(double r){
4252 memcpy(&a,&r,sizeof(a));
4258 m = a & ((((sqlite3_int64)1)<<52)-1);
4262 m |= ((sqlite3_int64)1)<<52;
4264 while( e<1075 && m>0 && (m&1)==0 ){
4271 return 0; /* A NaN or an Infinity */
4275 /* At this point m is the integer significand and e is the exponent */
4276 sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
4277 pA = decimalNewFromText(zNum, (int)strlen(zNum));
4278 pX = decimalPow2(e);
4285 ** SQL Function: decimal(X)
4286 ** OR: decimal_exp(X)
4288 ** Convert input X into decimal and then back into text.
4290 ** If X is originally a float, then a full decimal expansion of that floating
4291 ** point value is done. Or if X is an 8-byte blob, it is interpreted
4292 ** as a float and similarly expanded.
4294 ** The decimal_exp(X) function returns the result in exponential notation.
4295 ** decimal(X) returns a complete decimal, without the e+NNN at the end.
4297 static void decimalFunc(
4298 sqlite3_context *context,
4300 sqlite3_value **argv
4302 Decimal *p = decimal_new(context, argv[0], 0);
4303 UNUSED_PARAMETER(argc);
4305 if( sqlite3_user_data(context)!=0 ){
4306 decimal_result_sci(context, p);
4308 decimal_result(context, p);
4315 ** Compare text in decimal order.
4317 static int decimalCollFunc(
4319 int nKey1, const void *pKey1,
4320 int nKey2, const void *pKey2
4322 const unsigned char *zA = (const unsigned char*)pKey1;
4323 const unsigned char *zB = (const unsigned char*)pKey2;
4324 Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
4325 Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
4327 UNUSED_PARAMETER(notUsed);
4328 if( pA==0 || pB==0 ){
4331 rc = decimal_cmp(pA, pB);
4340 ** SQL Function: decimal_add(X, Y)
4341 ** decimal_sub(X, Y)
4343 ** Return the sum or difference of X and Y.
4345 static void decimalAddFunc(
4346 sqlite3_context *context,
4348 sqlite3_value **argv
4350 Decimal *pA = decimal_new(context, argv[0], 1);
4351 Decimal *pB = decimal_new(context, argv[1], 1);
4352 UNUSED_PARAMETER(argc);
4353 decimal_add(pA, pB);
4354 decimal_result(context, pA);
4358 static void decimalSubFunc(
4359 sqlite3_context *context,
4361 sqlite3_value **argv
4363 Decimal *pA = decimal_new(context, argv[0], 1);
4364 Decimal *pB = decimal_new(context, argv[1], 1);
4365 UNUSED_PARAMETER(argc);
4367 pB->sign = !pB->sign;
4368 decimal_add(pA, pB);
4369 decimal_result(context, pA);
4375 /* Aggregate funcion: decimal_sum(X)
4377 ** Works like sum() except that it uses decimal arithmetic for unlimited
4380 static void decimalSumStep(
4381 sqlite3_context *context,
4383 sqlite3_value **argv
4387 UNUSED_PARAMETER(argc);
4388 p = sqlite3_aggregate_context(context, sizeof(*p));
4392 p->a = sqlite3_malloc(2);
4401 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4402 pArg = decimal_new(context, argv[0], 1);
4403 decimal_add(p, pArg);
4406 static void decimalSumInverse(
4407 sqlite3_context *context,
4409 sqlite3_value **argv
4413 UNUSED_PARAMETER(argc);
4414 p = sqlite3_aggregate_context(context, sizeof(*p));
4416 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4417 pArg = decimal_new(context, argv[0], 1);
4418 if( pArg ) pArg->sign = !pArg->sign;
4419 decimal_add(p, pArg);
4422 static void decimalSumValue(sqlite3_context *context){
4423 Decimal *p = sqlite3_aggregate_context(context, 0);
4425 decimal_result(context, p);
4427 static void decimalSumFinalize(sqlite3_context *context){
4428 Decimal *p = sqlite3_aggregate_context(context, 0);
4430 decimal_result(context, p);
4435 ** SQL Function: decimal_mul(X, Y)
4437 ** Return the product of X and Y.
4439 static void decimalMulFunc(
4440 sqlite3_context *context,
4442 sqlite3_value **argv
4444 Decimal *pA = decimal_new(context, argv[0], 1);
4445 Decimal *pB = decimal_new(context, argv[1], 1);
4446 UNUSED_PARAMETER(argc);
4447 if( pA==0 || pA->oom || pA->isNull
4448 || pB==0 || pB->oom || pB->isNull
4456 decimal_result(context, pA);
4464 ** SQL Function: decimal_pow2(N)
4466 ** Return the N-th power of 2. N must be an integer.
4468 static void decimalPow2Func(
4469 sqlite3_context *context,
4471 sqlite3_value **argv
4473 UNUSED_PARAMETER(argc);
4474 if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
4475 Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
4476 decimal_result_sci(context, pA);
4484 int sqlite3_decimal_init(
4487 const sqlite3_api_routines *pApi
4490 static const struct {
4491 const char *zFuncName;
4494 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
4496 { "decimal", 1, 0, decimalFunc },
4497 { "decimal_exp", 1, 1, decimalFunc },
4498 { "decimal_cmp", 2, 0, decimalCmpFunc },
4499 { "decimal_add", 2, 0, decimalAddFunc },
4500 { "decimal_sub", 2, 0, decimalSubFunc },
4501 { "decimal_mul", 2, 0, decimalMulFunc },
4502 { "decimal_pow2", 1, 0, decimalPow2Func },
4505 (void)pzErrMsg; /* Unused parameter */
4507 SQLITE_EXTENSION_INIT2(pApi);
4509 for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
4510 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
4511 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4512 aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
4514 if( rc==SQLITE_OK ){
4515 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
4516 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
4517 decimalSumStep, decimalSumFinalize,
4518 decimalSumValue, decimalSumInverse, 0);
4520 if( rc==SQLITE_OK ){
4521 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
4522 0, decimalCollFunc);
4527 /************************* End ../ext/misc/decimal.c ********************/
4528 #undef sqlite3_base_init
4529 #define sqlite3_base_init sqlite3_base64_init
4530 /************************* Begin ../ext/misc/base64.c ******************/
4534 ** The author disclaims copyright to this source code. In place of
4535 ** a legal notice, here is a blessing:
4537 ** May you do good and not evil.
4538 ** May you find forgiveness for yourself and forgive others.
4539 ** May you share freely, never taking more than you give.
4541 *************************************************************************
4543 ** This is a SQLite extension for converting in either direction
4544 ** between a (binary) blob and base64 text. Base64 can transit a
4545 ** sane USASCII channel unmolested. It also plays nicely in CSV or
4546 ** written as TCL brace-enclosed literals or SQL string literals,
4547 ** and can be used unmodified in XML-like documents.
4549 ** This is an independent implementation of conversions specified in
4550 ** RFC 4648, done on the above date by the author (Larry Brasfield)
4551 ** who thereby has the right to put this into the public domain.
4553 ** The conversions meet RFC 4648 requirements, provided that this
4554 ** C source specifies that line-feeds are included in the encoded
4555 ** data to limit visible line lengths to 72 characters and to
4556 ** terminate any encoded blob having non-zero length.
4558 ** Length limitations are not imposed except that the runtime
4559 ** SQLite string or blob length limits are respected. Otherwise,
4560 ** any length binary sequence can be represented and recovered.
4561 ** Generated base64 sequences, with their line-feeds included,
4562 ** can be concatenated; the result converted back to binary will
4563 ** be the concatenation of the represented binary sequences.
4565 ** This SQLite3 extension creates a function, base64(x), which
4566 ** either: converts text x containing base64 to a returned blob;
4567 ** or converts a blob x to returned text containing base64. An
4568 ** error will be thrown for other input argument types.
4570 ** This code relies on UTF-8 encoding only with respect to the
4571 ** meaning of the first 128 (7-bit) codes matching that of USASCII.
4572 ** It will fail miserably if somehow made to try to convert EBCDIC.
4573 ** Because it is table-driven, it could be enhanced to handle that,
4574 ** but the world and SQLite have moved on from that anachronism.
4576 ** To build the extension:
4577 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
4578 ** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c
4579 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c
4580 ** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c
4581 ** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll
4586 /* #include "sqlite3ext.h" */
4588 #ifndef deliberate_fall_through
4589 /* Quiet some compilers about some of our intentional code. */
4590 # if GCC_VERSION>=7000000
4591 # define deliberate_fall_through __attribute__((fallthrough));
4593 # define deliberate_fall_through
4597 SQLITE_EXTENSION_INIT1;
4599 #define PC 0x80 /* pad character */
4600 #define WS 0x81 /* whitespace */
4601 #define ND 0x82 /* Not above or digit-value */
4602 #define PAD_CHAR '='
4605 /* typedef unsigned char u8; */
4609 /* Decoding table, ASCII (7-bit) value to base 64 digit value or other */
4610 static const u8 b64DigitValues[128] = {
4611 /* HT LF VT FF CR */
4612 ND,ND,ND,ND, ND,ND,ND,ND, ND,WS,WS,WS, WS,WS,ND,ND,
4614 ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND,
4616 WS,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,62, ND,ND,ND,63,
4618 52,53,54,55, 56,57,58,59, 60,61,ND,ND, ND,PC,ND,ND,
4620 ND, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
4622 15,16,17,18, 19,20,21,22, 23,24,25,ND, ND,ND,ND,ND,
4624 ND,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
4626 41,42,43,44, 45,46,47,48, 49,50,51,ND, ND,ND,ND,ND
4629 static const char b64Numerals[64+1]
4630 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4632 #define BX_DV_PROTO(c) \
4633 ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80)
4634 #define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80)
4635 #define IS_BX_WS(bdp) ((bdp)==WS)
4636 #define IS_BX_PAD(bdp) ((bdp)==PC)
4637 #define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)])
4638 /* Width of base64 lines. Should be an integer multiple of 4. */
4639 #define B64_DARK_MAX 72
4641 /* Encode a byte buffer into base64 text with linefeeds appended to limit
4642 ** encoded group lengths to B64_DARK_MAX or to terminate the last group.
4644 static char* toBase64( u8 *pIn, int nbIn, char *pOut ){
4647 /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */
4648 pOut[0] = BX_NUMERAL(pIn[0]>>2);
4649 pOut[1] = BX_NUMERAL(((pIn[0]<<4)|(pIn[1]>>4))&0x3f);
4650 pOut[2] = BX_NUMERAL(((pIn[1]&0xf)<<2)|(pIn[2]>>6));
4651 pOut[3] = BX_NUMERAL(pIn[2]&0x3f);
4655 if( (nCol += 4)>=B64_DARK_MAX || nbIn<=0 ){
4661 signed char nco = nbIn+1;
4663 unsigned long qv = *pIn++;
4664 for( nbe=1; nbe<3; ++nbe ){
4666 if( nbe<nbIn ) qv |= *pIn++;
4668 for( nbe=3; nbe>=0; --nbe ){
4669 char ce = (nbe<nco)? BX_NUMERAL((u8)(qv & 0x3f)) : PAD_CHAR;
4680 /* Skip over text which is not base64 numeral(s). */
4681 static char * skipNonB64( char *s, int nc ){
4683 while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s;
4687 /* Decode base64 text into a byte buffer. */
4688 static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){
4689 if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
4690 while( ncIn>0 && *pIn!=PAD_CHAR ){
4691 static signed char nboi[] = { 0, 0, 1, 2, 3 };
4692 char *pUse = skipNonB64(pIn, ncIn);
4693 unsigned long qv = 0L;
4695 ncIn -= (pUse - pIn);
4697 nti = (ncIn>4)? 4 : ncIn;
4701 for( nac=0; nac<4; ++nac ){
4702 char c = (nac<nti)? *pIn++ : b64Numerals[0];
4703 u8 bdp = BX_DV_PROTO(c);
4706 /* Treat dark non-digits as pad, but they terminate decode too. */
4708 deliberate_fall_through;
4710 /* Treat whitespace as pad and terminate this group.*/
4712 deliberate_fall_through;
4716 deliberate_fall_through;
4717 default: /* bdp is the digit value. */
4724 pOut[2] = (qv) & 0xff;
4726 pOut[1] = (qv>>8) & 0xff;
4728 pOut[0] = (qv>>16) & 0xff;
4735 /* This function does the work for the SQLite base64(x) UDF. */
4736 static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
4737 int nb, nc, nv = sqlite3_value_bytes(av[0]);
4738 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
4739 SQLITE_LIMIT_LENGTH, -1);
4743 switch( sqlite3_value_type(av[0]) ){
4746 nc = 4*(nv+2/3); /* quads needed */
4747 nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
4749 sqlite3_result_error(context, "blob expanded to base64 too big", -1);
4752 bBuf = (u8*)sqlite3_value_blob(av[0]);
4754 if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
4757 sqlite3_result_text(context,"",-1,SQLITE_STATIC);
4760 cBuf = sqlite3_malloc(nc);
4761 if( !cBuf ) goto memFail;
4762 nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf);
4763 sqlite3_result_text(context, cBuf, nc, sqlite3_free);
4767 nb = 3*((nv+3)/4); /* may overestimate due to LF and padding */
4769 sqlite3_result_error(context, "blob from base64 may be too big", -1);
4774 cBuf = (char *)sqlite3_value_text(av[0]);
4776 if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
4779 sqlite3_result_zeroblob(context, 0);
4782 bBuf = sqlite3_malloc(nb);
4783 if( !bBuf ) goto memFail;
4784 nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf);
4785 sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
4788 sqlite3_result_error(context, "base64 accepts only blob or text", -1);
4793 sqlite3_result_error(context, "base64 OOM", -1);
4797 ** Establish linkage to running SQLite library.
4799 #ifndef SQLITE_SHELL_EXTFUNCS
4803 int sqlite3_base_init
4805 static int sqlite3_base64_init
4807 (sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
4808 SQLITE_EXTENSION_INIT2(pApi);
4810 return sqlite3_create_function
4812 SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
4817 ** Define some macros to allow this extension to be built into the shell
4818 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
4819 ** allows shell.c, as distributed, to have this extension built in.
4821 #define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0)
4822 #define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
4824 /************************* End ../ext/misc/base64.c ********************/
4825 #undef sqlite3_base_init
4826 #define sqlite3_base_init sqlite3_base85_init
4827 #define OMIT_BASE85_CHECKER
4828 /************************* Begin ../ext/misc/base85.c ******************/
4832 ** The author disclaims copyright to this source code. In place of
4833 ** a legal notice, here is a blessing:
4835 ** May you do good and not evil.
4836 ** May you find forgiveness for yourself and forgive others.
4837 ** May you share freely, never taking more than you give.
4839 *************************************************************************
4841 ** This is a utility for converting binary to base85 or vice-versa.
4842 ** It can be built as a standalone program or an SQLite3 extension.
4844 ** Much like base64 representations, base85 can be sent through a
4845 ** sane USASCII channel unmolested. It also plays nicely in CSV or
4846 ** written as TCL brace-enclosed literals or SQL string literals.
4847 ** It is not suited for unmodified use in XML-like documents.
4849 ** The encoding used resembles Ascii85, but was devised by the author
4850 ** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85
4851 ** variant sources existed, in the 1984 timeframe on a VAX mainframe.
4852 ** Further, this is an independent implementation of a base85 system.
4853 ** Hence, the author has rightfully put this into the public domain.
4855 ** Base85 numerals are taken from the set of 7-bit USASCII codes,
4856 ** excluding control characters and Space ! " ' ( ) { | } ~ Del
4857 ** in code order representing digit values 0 to 84 (base 10.)
4859 ** Groups of 4 bytes, interpreted as big-endian 32-bit values,
4860 ** are represented as 5-digit base85 numbers with MS to LS digit
4861 ** order. Groups of 1-3 bytes are represented with 2-4 digits,
4862 ** still big-endian but 8-24 bit values. (Using big-endian yields
4863 ** the simplest transition to byte groups smaller than 4 bytes.
4864 ** These byte groups can also be considered base-256 numbers.)
4865 ** Groups of 0 bytes are represented with 0 digits and vice-versa.
4866 ** No pad characters are used; Encoded base85 numeral sequence
4867 ** (aka "group") length maps 1-to-1 to the decoded binary length.
4869 ** Any character not in the base85 numeral set delimits groups.
4870 ** When base85 is streamed or stored in containers of indefinite
4871 ** size, newline is used to separate it into sub-sequences of no
4872 ** more than 80 digits so that fgets() can be used to read it.
4874 ** Length limitations are not imposed except that the runtime
4875 ** SQLite string or blob length limits are respected. Otherwise,
4876 ** any length binary sequence can be represented and recovered.
4877 ** Base85 sequences can be concatenated by separating them with
4878 ** a non-base85 character; the conversion to binary will then
4879 ** be the concatenation of the represented binary sequences.
4881 ** The standalone program either converts base85 on stdin to create
4882 ** a binary file or converts a binary file to base85 on stdout.
4883 ** Read or make it blurt its help for invocation details.
4885 ** The SQLite3 extension creates a function, base85(x), which will
4886 ** either convert text base85 to a blob or a blob to text base85
4887 ** and return the result (or throw an error for other types.)
4888 ** Unless built with OMIT_BASE85_CHECKER defined, it also creates a
4889 ** function, is_base85(t), which returns 1 iff the text t contains
4890 ** nothing other than base85 numerals and whitespace, or 0 otherwise.
4892 ** To build the extension:
4893 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
4894 ** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted.
4895 ** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c
4896 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c
4897 ** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c
4898 ** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll
4900 ** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg.
4901 ** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85
4902 ** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c
4903 ** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c
4910 #ifndef OMIT_BASE85_CHECKER
4914 #ifndef BASE85_STANDALONE
4916 /* # include "sqlite3ext.h" */
4918 SQLITE_EXTENSION_INIT1;
4926 # define setmode(fd,m)
4929 static char *zHelp =
4930 "Usage: base85 <dirFlag> <binFile>\n"
4931 " <dirFlag> is either -r to read or -w to write <binFile>,\n"
4932 " content to be converted to/from base85 on stdout/stdin.\n"
4933 " <binFile> names a binary file to be rendered or created.\n"
4934 " Or, the name '-' refers to the stdin or stdout stream.\n"
4937 static void sayHelp(){
4938 printf("%s", zHelp);
4943 /* typedef unsigned char u8; */
4947 /* Classify c according to interval within USASCII set w.r.t. base85
4948 * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not.
4950 #define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z'))
4952 /* Provide digitValue to b85Numeral offset as a function of above class. */
4953 static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 };
4954 #define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)]
4956 /* Say whether c is a base85 numeral. */
4957 #define IS_B85( c ) (B85_CLASS(c) & 1)
4959 #if 0 /* Not used, */
4960 static u8 base85DigitValue( char c ){
4961 u8 dv = (u8)(c - '#');
4962 if( dv>87 ) return 0xff;
4963 return (dv > 3)? dv-3 : dv;
4967 /* Width of base64 lines. Should be an integer multiple of 5. */
4968 #define B85_DARK_MAX 80
4971 static char * skipNonB85( char *s, int nc ){
4973 while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s;
4977 /* Convert small integer, known to be in 0..84 inclusive, to base85 numeral.
4978 * Do not use the macro form with argument expression having a side-effect.*/
4980 static char base85Numeral( u8 b ){
4981 return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*');
4984 # define base85Numeral( dn )\
4985 ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*')))
4988 static char *putcs(char *pc, char *s){
4990 while( (c = *s++)!=0 ) *pc++ = c;
4994 /* Encode a byte buffer into base85 text. If pSep!=0, it's a C string
4995 ** to be appended to encoded groups to limit their length to B85_DARK_MAX
4996 ** or to terminate the last group (to aid concatenation.)
4998 static char* toBase85( u8 *pIn, int nbIn, char *pOut, char *pSep ){
5002 unsigned long qbv = (((unsigned long)pIn[0])<<24) |
5003 (pIn[1]<<16) | (pIn[2]<<8) | pIn[3];
5005 unsigned nqv = (unsigned)(qbv/85UL);
5006 unsigned char dv = qbv - 85UL*nqv;
5008 pOut[--nco] = base85Numeral(dv);
5013 if( pSep && (nCol += 5)>=B85_DARK_MAX ){
5014 pOut = putcs(pOut, pSep);
5020 unsigned long qv = *pIn++;
5022 while( nbe++ < nbIn ){
5023 qv = (qv<<8) | *pIn++;
5027 u8 dv = (u8)(qv % 85);
5029 pOut[--nco] = base85Numeral(dv);
5033 if( pSep && nCol>0 ) pOut = putcs(pOut, pSep);
5038 /* Decode base85 text into a byte buffer. */
5039 static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){
5040 if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
5042 static signed char nboi[] = { 0, 0, 1, 2, 3, 4 };
5043 char *pUse = skipNonB85(pIn, ncIn);
5044 unsigned long qv = 0L;
5046 ncIn -= (pUse - pIn);
5048 nti = (ncIn>5)? 5 : ncIn;
5053 u8 cdo = B85_DNOS(c);
5056 qv = 85 * qv + (c - cdo);
5059 nbo -= nti; /* Adjust for early (non-digit) end of group. */
5062 *pOut++ = (qv >> 24)&0xff;
5064 *pOut++ = (qv >> 16)&0xff;
5066 *pOut++ = (qv >> 8)&0xff;
5076 #ifndef OMIT_BASE85_CHECKER
5077 /* Say whether input char sequence is all (base85 and/or whitespace).*/
5078 static int allBase85( char *p, int len ){
5080 while( len-- > 0 && (c = *p++) != 0 ){
5081 if( !IS_B85(c) && !isspace(c) ) return 0;
5087 #ifndef BASE85_STANDALONE
5089 # ifndef OMIT_BASE85_CHECKER
5090 /* This function does the work for the SQLite is_base85(t) UDF. */
5091 static void is_base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5093 switch( sqlite3_value_type(av[0]) ){
5096 int rv = allBase85( (char *)sqlite3_value_text(av[0]),
5097 sqlite3_value_bytes(av[0]) );
5098 sqlite3_result_int(context, rv);
5102 sqlite3_result_null(context);
5105 sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1);
5111 /* This function does the work for the SQLite base85(x) UDF. */
5112 static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5113 int nb, nc, nv = sqlite3_value_bytes(av[0]);
5114 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
5115 SQLITE_LIMIT_LENGTH, -1);
5119 switch( sqlite3_value_type(av[0]) ){
5122 /* ulongs tail newlines tailenc+nul*/
5123 nc = 5*(nv/4) + nv%4 + nv/64+1 + 2;
5125 sqlite3_result_error(context, "blob expanded to base85 too big", -1);
5128 bBuf = (u8*)sqlite3_value_blob(av[0]);
5130 if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
5133 sqlite3_result_text(context,"",-1,SQLITE_STATIC);
5136 cBuf = sqlite3_malloc(nc);
5137 if( !cBuf ) goto memFail;
5138 nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf);
5139 sqlite3_result_text(context, cBuf, nc, sqlite3_free);
5143 nb = 4*(nv/5) + nv%5; /* may overestimate */
5145 sqlite3_result_error(context, "blob from base85 may be too big", -1);
5150 cBuf = (char *)sqlite3_value_text(av[0]);
5152 if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
5155 sqlite3_result_zeroblob(context, 0);
5158 bBuf = sqlite3_malloc(nb);
5159 if( !bBuf ) goto memFail;
5160 nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf);
5161 sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
5164 sqlite3_result_error(context, "base85 accepts only blob or text.", -1);
5169 sqlite3_result_error(context, "base85 OOM", -1);
5173 ** Establish linkage to running SQLite library.
5175 #ifndef SQLITE_SHELL_EXTFUNCS
5179 int sqlite3_base_init
5181 static int sqlite3_base85_init
5183 (sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
5184 SQLITE_EXTENSION_INIT2(pApi);
5186 # ifndef OMIT_BASE85_CHECKER
5188 int rc = sqlite3_create_function
5189 (db, "is_base85", 1,
5190 SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_UTF8,
5191 0, is_base85, 0, 0);
5192 if( rc!=SQLITE_OK ) return rc;
5195 return sqlite3_create_function
5197 SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
5202 ** Define some macros to allow this extension to be built into the shell
5203 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
5204 ** allows shell.c, as distributed, to have this extension built in.
5206 # define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0)
5207 # define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
5209 #else /* standalone program */
5211 int main(int na, char *av[]){
5214 u8 bBuf[4*(B85_DARK_MAX/5)];
5215 char cBuf[5*(sizeof(bBuf)/4)+2];
5217 # ifndef OMIT_BASE85_CHECKER
5221 FILE *fb = 0, *foc = 0;
5222 char fmode[3] = "xb";
5223 if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){
5228 if( av[2][0]=='-' && av[2][1]==0 ){
5232 setmode(fileno(stdin), O_BINARY);
5236 setmode(fileno(stdout), O_BINARY);
5240 fb = fopen(av[2], fmode);
5244 fprintf(stderr, "Cannot open %s for %c\n", av[2], rw);
5249 while( (nio = fread( bBuf, 1, sizeof(bBuf), fb))>0 ){
5250 toBase85( bBuf, (int)nio, cBuf, 0 );
5251 fprintf(stdout, "%s\n", cBuf);
5255 while( 0 != fgets(cBuf, sizeof(cBuf), stdin) ){
5256 int nc = strlen(cBuf);
5257 size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf;
5258 if( 1 != fwrite(bBuf, nbo, 1, fb) ) rc = 1;
5259 # ifndef OMIT_BASE85_CHECKER
5260 b85Clean &= allBase85( cBuf, nc );
5268 if( foc ) fclose(foc);
5270 # ifndef OMIT_BASE85_CHECKER
5272 fprintf(stderr, "Base85 input had non-base85 dark or control content.\n");
5280 /************************* End ../ext/misc/base85.c ********************/
5281 /************************* Begin ../ext/misc/ieee754.c ******************/
5285 ** The author disclaims copyright to this source code. In place of
5286 ** a legal notice, here is a blessing:
5288 ** May you do good and not evil.
5289 ** May you find forgiveness for yourself and forgive others.
5290 ** May you share freely, never taking more than you give.
5292 ******************************************************************************
5294 ** This SQLite extension implements functions for the exact display
5295 ** and input of IEEE754 Binary64 floating-point numbers.
5300 ** In the first form, the value X should be a floating-point number.
5301 ** The function will return a string of the form 'ieee754(Y,Z)' where
5302 ** Y and Z are integers such that X==Y*pow(2,Z).
5304 ** In the second form, Y and Z are integers which are the mantissa and
5305 ** base-2 exponent of a new floating point number. The function returns
5306 ** a floating-point value equal to Y*pow(2,Z).
5310 ** ieee754(2.0) -> 'ieee754(2,0)'
5311 ** ieee754(45.25) -> 'ieee754(181,-2)'
5312 ** ieee754(2, 0) -> 2.0
5313 ** ieee754(181, -2) -> 45.25
5315 ** Two additional functions break apart the one-argument ieee754()
5316 ** result into separate integer values:
5318 ** ieee754_mantissa(45.25) -> 181
5319 ** ieee754_exponent(45.25) -> -2
5321 ** These functions convert binary64 numbers into blobs and back again.
5323 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5324 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5326 ** In all single-argument functions, if the argument is an 8-byte blob
5327 ** then that blob is interpreted as a big-endian binary64 value.
5330 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5331 ** -----------------------------------------------
5333 ** This extension in combination with the separate 'decimal' extension
5334 ** can be used to compute the exact decimal representation of binary64
5335 ** values. To begin, first compute a table of exponent values:
5337 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5338 ** WITH RECURSIVE c(x,v) AS (
5341 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5342 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5343 ** WITH RECURSIVE c(x,v) AS (
5346 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5347 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5349 ** Then, to compute the exact decimal representation of a floating
5350 ** point value (the value 47.49 is used in the example) do:
5352 ** WITH c(n) AS (VALUES(47.49))
5353 ** ---------------^^^^^---- Replace with whatever you want
5354 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5355 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5357 ** Here is a query to show various boundry values for the binary64
5360 ** WITH c(name,bin) AS (VALUES
5361 ** ('minimum positive value', x'0000000000000001'),
5362 ** ('maximum subnormal value', x'000fffffffffffff'),
5363 ** ('mininum positive nornal value', x'0010000000000000'),
5364 ** ('maximum value', x'7fefffffffffffff'))
5365 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5366 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5369 /* #include "sqlite3ext.h" */
5370 SQLITE_EXTENSION_INIT1
5374 /* Mark a function parameter as unused, to suppress nuisance compiler
5376 #ifndef UNUSED_PARAMETER
5377 # define UNUSED_PARAMETER(X) (void)(X)
5381 ** Implementation of the ieee754() function
5383 static void ieee754func(
5384 sqlite3_context *context,
5386 sqlite3_value **argv
5394 assert( sizeof(m)==sizeof(r) );
5395 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5396 && sqlite3_value_bytes(argv[0])==sizeof(r)
5398 const unsigned char *x = sqlite3_value_blob(argv[0]);
5400 sqlite3_uint64 v = 0;
5401 for(i=0; i<sizeof(r); i++){
5404 memcpy(&r, &v, sizeof(r));
5406 r = sqlite3_value_double(argv[0]);
5414 memcpy(&a,&r,sizeof(a));
5420 m = a & ((((sqlite3_int64)1)<<52)-1);
5424 m |= ((sqlite3_int64)1)<<52;
5426 while( e<1075 && m>0 && (m&1)==0 ){
5432 switch( *(int*)sqlite3_user_data(context) ){
5434 sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5436 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5439 sqlite3_result_int64(context, m);
5442 sqlite3_result_int(context, e-1075);
5446 sqlite3_int64 m, e, a;
5449 m = sqlite3_value_int64(argv[0]);
5450 e = sqlite3_value_int64(argv[1]);
5452 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */
5455 }else if( e<-10000 ){
5463 }else if( m==0 && e>-1000 && e<1000 ){
5464 sqlite3_result_double(context, 0.0);
5467 while( (m>>32)&0xffe00000 ){
5471 while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5484 }else if( e>0x7ff ){
5487 a = m & ((((sqlite3_int64)1)<<52)-1);
5489 if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5490 memcpy(&r, &a, sizeof(r));
5491 sqlite3_result_double(context, r);
5496 ** Functions to convert between blobs and floats.
5498 static void ieee754func_from_blob(
5499 sqlite3_context *context,
5501 sqlite3_value **argv
5503 UNUSED_PARAMETER(argc);
5504 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5505 && sqlite3_value_bytes(argv[0])==sizeof(double)
5508 const unsigned char *x = sqlite3_value_blob(argv[0]);
5510 sqlite3_uint64 v = 0;
5511 for(i=0; i<sizeof(r); i++){
5514 memcpy(&r, &v, sizeof(r));
5515 sqlite3_result_double(context, r);
5518 static void ieee754func_to_blob(
5519 sqlite3_context *context,
5521 sqlite3_value **argv
5523 UNUSED_PARAMETER(argc);
5524 if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5525 || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5527 double r = sqlite3_value_double(argv[0]);
5529 unsigned char a[sizeof(r)];
5531 memcpy(&v, &r, sizeof(r));
5532 for(i=1; i<=sizeof(r); i++){
5533 a[sizeof(r)-i] = v&0xff;
5536 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5541 ** SQL Function: ieee754_inc(r,N)
5543 ** Move the floating point value r by N quantums and return the new
5546 ** Behind the scenes: this routine merely casts r into a 64-bit unsigned
5547 ** integer, adds N, then casts the value back into float.
5549 ** Example: To find the smallest positive number:
5551 ** SELECT ieee754_inc(0.0,+1);
5553 static void ieee754inc(
5554 sqlite3_context *context,
5556 sqlite3_value **argv
5560 sqlite3_uint64 m1, m2;
5562 UNUSED_PARAMETER(argc);
5563 r = sqlite3_value_double(argv[0]);
5564 N = sqlite3_value_int64(argv[1]);
5567 memcpy(&r2, &m2, 8);
5568 sqlite3_result_double(context, r2);
5575 int sqlite3_ieee_init(
5578 const sqlite3_api_routines *pApi
5580 static const struct {
5584 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5586 { "ieee754", 1, 0, ieee754func },
5587 { "ieee754", 2, 0, ieee754func },
5588 { "ieee754_mantissa", 1, 1, ieee754func },
5589 { "ieee754_exponent", 1, 2, ieee754func },
5590 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5591 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5592 { "ieee754_inc", 2, 0, ieee754inc },
5596 SQLITE_EXTENSION_INIT2(pApi);
5597 (void)pzErrMsg; /* Unused parameter */
5598 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5599 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5600 SQLITE_UTF8|SQLITE_INNOCUOUS,
5601 (void*)&aFunc[i].iAux,
5602 aFunc[i].xFunc, 0, 0);
5607 /************************* End ../ext/misc/ieee754.c ********************/
5608 /************************* Begin ../ext/misc/series.c ******************/
5610 ** 2015-08-18, 2023-04-28
5612 ** The author disclaims copyright to this source code. In place of
5613 ** a legal notice, here is a blessing:
5615 ** May you do good and not evil.
5616 ** May you find forgiveness for yourself and forgive others.
5617 ** May you share freely, never taking more than you give.
5619 *************************************************************************
5621 ** This file demonstrates how to create a table-valued-function using
5622 ** a virtual table. This demo implements the generate_series() function
5623 ** which gives the same results as the eponymous function in PostgreSQL,
5624 ** within the limitation that its arguments are signed 64-bit integers.
5626 ** Considering its equivalents to generate_series(start,stop,step): A
5627 ** value V[n] sequence is produced for integer n ascending from 0 where
5628 ** ( V[n] == start + n * step && sgn(V[n] - stop) * sgn(step) >= 0 )
5629 ** for each produced value (independent of production time ordering.)
5631 ** All parameters must be either integer or convertable to integer.
5632 ** The start parameter is required.
5633 ** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
5634 ** The step parameter defaults to 1 and 0 is treated as 1.
5638 ** SELECT * FROM generate_series(0,100,5);
5640 ** The query above returns integers from 0 through 100 counting by steps
5643 ** SELECT * FROM generate_series(0,100);
5645 ** Integers from 0 through 100 with a step size of 1.
5647 ** SELECT * FROM generate_series(20) LIMIT 10;
5649 ** Integers 20 through 29.
5651 ** SELECT * FROM generate_series(0,-100,-5);
5653 ** Integers 0 -5 -10 ... -100.
5655 ** SELECT * FROM generate_series(0,-1);
5661 ** The generate_series "function" is really a virtual table with the
5662 ** following schema:
5664 ** CREATE TABLE generate_series(
5671 ** The virtual table also has a rowid, logically equivalent to n+1 where
5672 ** "n" is the ascending integer in the aforesaid production definition.
5674 ** Function arguments in queries against this virtual table are translated
5675 ** into equality constraints against successive hidden columns. In other
5676 ** words, the following pairs of queries are equivalent to each other:
5678 ** SELECT * FROM generate_series(0,100,5);
5679 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5681 ** SELECT * FROM generate_series(0,100);
5682 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5684 ** SELECT * FROM generate_series(20) LIMIT 10;
5685 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5687 ** The generate_series virtual table implementation leaves the xCreate method
5688 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5689 ** TABLE command with "generate_series" as the USING argument. Instead, there
5690 ** is a single generate_series virtual table that is always available without
5691 ** having to be created first.
5693 ** The xBestIndex method looks for equality constraints against the hidden
5694 ** start, stop, and step columns, and if present, it uses those constraints
5695 ** to bound the sequence of generated values. If the equality constraints
5696 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5697 ** xBestIndex returns a small cost when both start and stop are available,
5698 ** and a very large cost if either start or stop are unavailable. This
5699 ** encourages the query planner to order joins such that the bounds of the
5700 ** series are well-defined.
5702 /* #include "sqlite3ext.h" */
5703 SQLITE_EXTENSION_INIT1
5708 #ifndef SQLITE_OMIT_VIRTUALTABLE
5710 ** Return that member of a generate_series(...) sequence whose 0-based
5711 ** index is ix. The 0th member is given by smBase. The sequence members
5712 ** progress per ix increment by smStep.
5714 static sqlite3_int64 genSeqMember(sqlite3_int64 smBase,
5715 sqlite3_int64 smStep,
5717 if( ix>=(sqlite3_uint64)LLONG_MAX ){
5718 /* Get ix into signed i64 range. */
5719 ix -= (sqlite3_uint64)LLONG_MAX;
5720 /* With 2's complement ALU, this next can be 1 step, but is split into
5721 * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
5722 smBase += (LLONG_MAX/2) * smStep;
5723 smBase += (LLONG_MAX - LLONG_MAX/2) * smStep;
5725 /* Under UBSAN (or on 1's complement machines), must do this last term
5726 * in steps to avoid the dreaded (and harmless) signed multiply overlow. */
5728 sqlite3_int64 ix2 = (sqlite3_int64)ix/2;
5729 smBase += ix2*smStep;
5732 return smBase + ((sqlite3_int64)ix)*smStep;
5735 /* typedef unsigned char u8; */
5737 typedef struct SequenceSpec {
5738 sqlite3_int64 iBase; /* Starting value ("start") */
5739 sqlite3_int64 iTerm; /* Given terminal value ("stop") */
5740 sqlite3_int64 iStep; /* Increment ("step") */
5741 sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
5742 sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
5743 sqlite3_int64 iValueNow; /* Current value during generation */
5744 u8 isNotEOF; /* Sequence generation not exhausted */
5745 u8 isReversing; /* Sequence is being reverse generated */
5749 ** Prepare a SequenceSpec for use in generating an integer series
5750 ** given initialized iBase, iTerm and iStep values. Sequence is
5751 ** initialized per given isReversing. Other members are computed.
5753 static void setupSequence( SequenceSpec *pss ){
5755 pss->uSeqIndexMax = 0;
5757 bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0);
5758 if( pss->iTerm < pss->iBase ){
5759 sqlite3_uint64 nuspan = 0;
5761 nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm);
5763 /* Under UBSAN (or on 1's complement machines), must do this in steps.
5764 * In this clause, iBase>=0 and iTerm<0 . */
5766 nuspan += pss->iBase;
5767 nuspan += -(pss->iTerm+1);
5771 if( nuspan==ULONG_MAX ){
5772 pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1;
5773 }else if( pss->iStep>LLONG_MIN ){
5774 pss->uSeqIndexMax = nuspan/-pss->iStep;
5777 }else if( pss->iTerm > pss->iBase ){
5778 sqlite3_uint64 puspan = 0;
5780 puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase);
5782 /* Under UBSAN (or on 1's complement machines), must do this in steps.
5783 * In this clause, iTerm>=0 and iBase<0 . */
5785 puspan += pss->iTerm;
5786 puspan += -(pss->iBase+1);
5790 pss->uSeqIndexMax = puspan/pss->iStep;
5792 }else if( pss->iTerm == pss->iBase ){
5794 pss->uSeqIndexMax = 0;
5796 pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0;
5797 pss->iValueNow = (pss->isReversing)
5798 ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax)
5803 ** Progress sequence generator to yield next value, if any.
5804 ** Leave its state to either yield next value or be at EOF.
5805 ** Return whether there is a next value, or 0 at EOF.
5807 static int progressSequence( SequenceSpec *pss ){
5808 if( !pss->isNotEOF ) return 0;
5809 if( pss->isReversing ){
5810 if( pss->uSeqIndexNow > 0 ){
5811 pss->uSeqIndexNow--;
5812 pss->iValueNow -= pss->iStep;
5817 if( pss->uSeqIndexNow < pss->uSeqIndexMax ){
5818 pss->uSeqIndexNow++;
5819 pss->iValueNow += pss->iStep;
5824 return pss->isNotEOF;
5827 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5828 ** serve as the underlying representation of a cursor that scans
5829 ** over rows of the result
5831 typedef struct series_cursor series_cursor;
5832 struct series_cursor {
5833 sqlite3_vtab_cursor base; /* Base class - must be first */
5834 SequenceSpec ss; /* (this) Derived class data */
5838 ** The seriesConnect() method is invoked to create a new
5839 ** series_vtab that describes the generate_series virtual table.
5841 ** Think of this routine as the constructor for series_vtab objects.
5843 ** All this routine needs to do is:
5845 ** (1) Allocate the series_vtab object and initialize all fields.
5847 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5848 ** result set of queries against generate_series will look like.
5850 static int seriesConnect(
5853 int argcUnused, const char *const*argvUnused,
5854 sqlite3_vtab **ppVtab,
5860 /* Column numbers */
5861 #define SERIES_COLUMN_VALUE 0
5862 #define SERIES_COLUMN_START 1
5863 #define SERIES_COLUMN_STOP 2
5864 #define SERIES_COLUMN_STEP 3
5870 rc = sqlite3_declare_vtab(db,
5871 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5872 if( rc==SQLITE_OK ){
5873 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5874 if( pNew==0 ) return SQLITE_NOMEM;
5875 memset(pNew, 0, sizeof(*pNew));
5876 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5882 ** This method is the destructor for series_cursor objects.
5884 static int seriesDisconnect(sqlite3_vtab *pVtab){
5885 sqlite3_free(pVtab);
5890 ** Constructor for a new series_cursor object.
5892 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5893 series_cursor *pCur;
5895 pCur = sqlite3_malloc( sizeof(*pCur) );
5896 if( pCur==0 ) return SQLITE_NOMEM;
5897 memset(pCur, 0, sizeof(*pCur));
5898 *ppCursor = &pCur->base;
5903 ** Destructor for a series_cursor.
5905 static int seriesClose(sqlite3_vtab_cursor *cur){
5912 ** Advance a series_cursor to its next row of output.
5914 static int seriesNext(sqlite3_vtab_cursor *cur){
5915 series_cursor *pCur = (series_cursor*)cur;
5916 progressSequence( & pCur->ss );
5921 ** Return values of columns for the row at which the series_cursor
5922 ** is currently pointing.
5924 static int seriesColumn(
5925 sqlite3_vtab_cursor *cur, /* The cursor */
5926 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5927 int i /* Which column to return */
5929 series_cursor *pCur = (series_cursor*)cur;
5930 sqlite3_int64 x = 0;
5932 case SERIES_COLUMN_START: x = pCur->ss.iBase; break;
5933 case SERIES_COLUMN_STOP: x = pCur->ss.iTerm; break;
5934 case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
5935 default: x = pCur->ss.iValueNow; break;
5937 sqlite3_result_int64(ctx, x);
5941 #ifndef LARGEST_UINT64
5942 #define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
5946 ** Return the rowid for the current row, logically equivalent to n+1 where
5947 ** "n" is the ascending integer in the aforesaid production definition.
5949 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5950 series_cursor *pCur = (series_cursor*)cur;
5951 sqlite3_uint64 n = pCur->ss.uSeqIndexNow;
5952 *pRowid = (sqlite3_int64)((n<LARGEST_UINT64)? n+1 : 0);
5957 ** Return TRUE if the cursor has been moved off of the last
5960 static int seriesEof(sqlite3_vtab_cursor *cur){
5961 series_cursor *pCur = (series_cursor*)cur;
5962 return !pCur->ss.isNotEOF;
5965 /* True to cause run-time checking of the start=, stop=, and/or step=
5966 ** parameters. The only reason to do this is for testing the
5967 ** constraint checking logic for virtual tables in the SQLite core.
5969 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5970 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5974 ** This method is called to "rewind" the series_cursor object back
5975 ** to the first row of output. This method is always called at least
5976 ** once prior to any call to seriesColumn() or seriesRowid() or
5979 ** The query plan selected by seriesBestIndex is passed in the idxNum
5980 ** parameter. (idxStr is not used in this implementation.) idxNum
5981 ** is a bitmask showing which constraints are available:
5987 ** Also, if bit 8 is set, that means that the series should be output
5988 ** in descending order rather than in ascending order. If bit 16 is
5989 ** set, then output must appear in ascending order.
5991 ** This routine should initialize the cursor and position it so that it
5992 ** is pointing at the first row, or pointing off the end of the table
5993 ** (so that seriesEof() will return true) if the table is empty.
5995 static int seriesFilter(
5996 sqlite3_vtab_cursor *pVtabCursor,
5997 int idxNum, const char *idxStrUnused,
5998 int argc, sqlite3_value **argv
6000 series_cursor *pCur = (series_cursor *)pVtabCursor;
6004 pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6009 pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6011 pCur->ss.iTerm = 0xffffffff;
6014 pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6015 if( pCur->ss.iStep==0 ){
6017 }else if( pCur->ss.iStep<0 ){
6018 if( (idxNum & 16)==0 ) idxNum |= 8;
6023 for(i=0; i<argc; i++){
6024 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6025 /* If any of the constraints have a NULL value, then return no rows.
6026 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
6034 pCur->ss.isReversing = pCur->ss.iStep > 0;
6036 pCur->ss.isReversing = pCur->ss.iStep < 0;
6038 setupSequence( &pCur->ss );
6043 ** SQLite will invoke this method one or more times while planning a query
6044 ** that uses the generate_series virtual table. This routine needs to create
6045 ** a query plan for each invocation and compute an estimated cost for that
6048 ** In this implementation idxNum is used to represent the
6049 ** query plan. idxStr is unused.
6051 ** The query plan is represented by bits in idxNum:
6053 ** (1) start = $value -- constraint exists
6054 ** (2) stop = $value -- constraint exists
6055 ** (4) step = $value -- constraint exists
6056 ** (8) output in descending order
6058 static int seriesBestIndex(
6059 sqlite3_vtab *pVTab,
6060 sqlite3_index_info *pIdxInfo
6062 int i, j; /* Loop over constraints */
6063 int idxNum = 0; /* The query plan bitmask */
6064 int bStartSeen = 0; /* EQ constraint seen on the START column */
6065 int unusableMask = 0; /* Mask of unusable constraints */
6066 int nArg = 0; /* Number of arguments that seriesFilter() expects */
6067 int aIdx[3]; /* Constraints on start, stop, and step */
6068 const struct sqlite3_index_constraint *pConstraint;
6070 /* This implementation assumes that the start, stop, and step columns
6071 ** are the last three columns in the virtual table. */
6072 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
6073 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
6075 aIdx[0] = aIdx[1] = aIdx[2] = -1;
6076 pConstraint = pIdxInfo->aConstraint;
6077 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
6078 int iCol; /* 0 for start, 1 for stop, 2 for step */
6079 int iMask; /* bitmask for those column */
6080 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
6081 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
6082 assert( iCol>=0 && iCol<=2 );
6084 if( iCol==0 ) bStartSeen = 1;
6085 if( pConstraint->usable==0 ){
6086 unusableMask |= iMask;
6088 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6094 if( (j = aIdx[i])>=0 ){
6095 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
6096 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
6099 /* The current generate_column() implementation requires at least one
6100 ** argument (the START value). Legacy versions assumed START=0 if the
6101 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
6102 ** to obtain the legacy behavior */
6103 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6105 sqlite3_free(pVTab->zErrMsg);
6106 pVTab->zErrMsg = sqlite3_mprintf(
6107 "first argument to \"generate_series()\" missing or unusable");
6108 return SQLITE_ERROR;
6111 if( (unusableMask & ~idxNum)!=0 ){
6112 /* The start, stop, and step columns are inputs. Therefore if there
6113 ** are unusable constraints on any of start, stop, or step then
6114 ** this plan is unusable */
6115 return SQLITE_CONSTRAINT;
6117 if( (idxNum & 3)==3 ){
6118 /* Both start= and stop= boundaries are available. This is the
6119 ** the preferred case */
6120 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
6121 pIdxInfo->estimatedRows = 1000;
6122 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
6123 if( pIdxInfo->aOrderBy[0].desc ){
6128 pIdxInfo->orderByConsumed = 1;
6131 /* If either boundary is missing, we have to generate a huge span
6132 ** of numbers. Make this case very expensive so that the query
6133 ** planner will work hard to avoid it. */
6134 pIdxInfo->estimatedRows = 2147483647;
6136 pIdxInfo->idxNum = idxNum;
6141 ** This following structure defines all the methods for the
6142 ** generate_series virtual table.
6144 static sqlite3_module seriesModule = {
6147 seriesConnect, /* xConnect */
6148 seriesBestIndex, /* xBestIndex */
6149 seriesDisconnect, /* xDisconnect */
6151 seriesOpen, /* xOpen - open a cursor */
6152 seriesClose, /* xClose - close a cursor */
6153 seriesFilter, /* xFilter - configure scan constraints */
6154 seriesNext, /* xNext - advance a cursor */
6155 seriesEof, /* xEof - check for end of scan */
6156 seriesColumn, /* xColumn - read data */
6157 seriesRowid, /* xRowid - read data */
6163 0, /* xFindMethod */
6167 0, /* xRollbackTo */
6168 0, /* xShadowName */
6172 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6177 int sqlite3_series_init(
6180 const sqlite3_api_routines *pApi
6183 SQLITE_EXTENSION_INIT2(pApi);
6184 #ifndef SQLITE_OMIT_VIRTUALTABLE
6185 if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
6186 *pzErrMsg = sqlite3_mprintf(
6187 "generate_series() requires SQLite 3.8.12 or later");
6188 return SQLITE_ERROR;
6190 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
6195 /************************* End ../ext/misc/series.c ********************/
6196 /************************* Begin ../ext/misc/regexp.c ******************/
6200 ** The author disclaims copyright to this source code. In place of
6201 ** a legal notice, here is a blessing:
6203 ** May you do good and not evil.
6204 ** May you find forgiveness for yourself and forgive others.
6205 ** May you share freely, never taking more than you give.
6207 ******************************************************************************
6209 ** The code in this file implements a compact but reasonably
6210 ** efficient regular-expression matcher for posix extended regular
6211 ** expressions against UTF8 text.
6213 ** This file is an SQLite extension. It registers a single function
6214 ** named "regexp(A,B)" where A is the regular expression and B is the
6215 ** string to be matched. By registering this function, SQLite will also
6216 ** then implement the "B regexp A" operator. Note that with the function
6217 ** the regular expression comes first, but with the operator it comes
6220 ** The following regular expression syntax is supported:
6222 ** X* zero or more occurrences of X
6223 ** X+ one or more occurrences of X
6224 ** X? zero or one occurrences of X
6225 ** X{p,q} between p and q occurrences of X
6228 ** ^X X occurring at the beginning of the string
6229 ** X$ X occurring at the end of the string
6230 ** . Match any single character
6231 ** \c Character c where c is one of \{}()[]|*+?.
6232 ** \c C-language escapes for c in afnrtv. ex: \t or \n
6233 ** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX
6234 ** \xXX Where XX is exactly 2 hex digits, unicode value XX
6235 ** [abc] Any single character from the set abc
6236 ** [^abc] Any single character not in the set abc
6237 ** [a-z] Any single character in the range a-z
6238 ** [^a-z] Any single character not in the range a-z
6240 ** \w Word character. [A-Za-z0-9_]
6241 ** \W Non-word character
6244 ** \s Whitespace character
6245 ** \S Non-whitespace character
6247 ** A nondeterministic finite automaton (NFA) is used for matching, so the
6248 ** performance is bounded by O(N*M) where N is the size of the regular
6249 ** expression and M is the size of the input string. The matcher never
6250 ** exhibits exponential behavior. Note that the X{p,q} operator expands
6251 ** to p copies of X following by q-p copies of X? and that the size of the
6252 ** regular expression in the O(N*M) performance bound is computed after
6257 /* #include "sqlite3ext.h" */
6258 SQLITE_EXTENSION_INIT1
6261 ** The following #defines change the names of some functions implemented in
6262 ** this file to prevent name collisions with C-library functions of the
6265 #define re_match sqlite3re_match
6266 #define re_compile sqlite3re_compile
6267 #define re_free sqlite3re_free
6269 /* The end-of-input character */
6270 #define RE_EOF 0 /* End of input */
6271 #define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
6273 /* The NFA is implemented as sequence of opcodes taken from the following
6274 ** set. Each opcode has a single integer argument.
6276 #define RE_OP_MATCH 1 /* Match the one character in the argument */
6277 #define RE_OP_ANY 2 /* Match any one character. (Implements ".") */
6278 #define RE_OP_ANYSTAR 3 /* Special optimized version of .* */
6279 #define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */
6280 #define RE_OP_GOTO 5 /* Jump to opcode at iArg */
6281 #define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */
6282 #define RE_OP_CC_INC 7 /* Beginning of a [...] character class */
6283 #define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */
6284 #define RE_OP_CC_VALUE 9 /* Single value in a character class */
6285 #define RE_OP_CC_RANGE 10 /* Range of values in a character class */
6286 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
6287 #define RE_OP_NOTWORD 12 /* Not a perl word character */
6288 #define RE_OP_DIGIT 13 /* digit: [0-9] */
6289 #define RE_OP_NOTDIGIT 14 /* Not a digit */
6290 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
6291 #define RE_OP_NOTSPACE 16 /* Not a digit */
6292 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6293 #define RE_OP_ATSTART 18 /* Currently at the start of the string */
6295 #if defined(SQLITE_DEBUG)
6296 /* Opcode names used for symbolic debugging */
6297 static const char *ReOpName[] = {
6318 #endif /* SQLITE_DEBUG */
6321 /* Each opcode is a "state" in the NFA */
6322 typedef unsigned short ReStateNumber;
6324 /* Because this is an NFA and not a DFA, multiple states can be active at
6325 ** once. An instance of the following object records all active states in
6326 ** the NFA. The implementation is optimized for the common case where the
6327 ** number of actives states is small.
6329 typedef struct ReStateSet {
6330 unsigned nState; /* Number of current states */
6331 ReStateNumber *aState; /* Current states */
6334 /* An input string read one character at a time.
6336 typedef struct ReInput ReInput;
6338 const unsigned char *z; /* All text */
6339 int i; /* Next byte to read */
6340 int mx; /* EOF when i>=mx */
6343 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6344 ** an instance of the following object.
6346 typedef struct ReCompiled ReCompiled;
6348 ReInput sIn; /* Regular expression text */
6349 const char *zErr; /* Error message to return */
6350 char *aOp; /* Operators for the virtual machine */
6351 int *aArg; /* Arguments to each operator */
6352 unsigned (*xNextChar)(ReInput*); /* Next character function */
6353 unsigned char zInit[12]; /* Initial text to match */
6354 int nInit; /* Number of bytes in zInit */
6355 unsigned nState; /* Number of entries in aOp[] and aArg[] */
6356 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
6359 /* Add a state to the given state set if it is not already there */
6360 static void re_add_state(ReStateSet *pSet, int newState){
6362 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6363 pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6366 /* Extract the next unicode character from *pzIn and return it. Advance
6367 ** *pzIn to the first byte past the end of the character returned. To
6368 ** be clear: this routine converts utf8 to unicode. This routine is
6369 ** optimized for the common case where the next character is a single byte.
6371 static unsigned re_next_char(ReInput *p){
6373 if( p->i>=p->mx ) return 0;
6376 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6377 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6378 if( c<0x80 ) c = 0xfffd;
6379 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6380 && (p->z[p->i+1]&0xc0)==0x80 ){
6381 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6383 if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6384 }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80
6385 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6386 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6387 | (p->z[p->i+2]&0x3f);
6389 if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6396 static unsigned re_next_char_nocase(ReInput *p){
6397 unsigned c = re_next_char(p);
6398 if( c>='A' && c<='Z' ) c += 'a' - 'A';
6402 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
6403 static int re_word_char(int c){
6404 return (c>='0' && c<='9') || (c>='a' && c<='z')
6405 || (c>='A' && c<='Z') || c=='_';
6408 /* Return true if c is a "digit" character: [0-9] */
6409 static int re_digit_char(int c){
6410 return (c>='0' && c<='9');
6413 /* Return true if c is a perl "space" character: [ \t\r\n\v\f] */
6414 static int re_space_char(int c){
6415 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6418 /* Run a compiled regular expression on the zero-terminated input
6419 ** string zIn[]. Return true on a match and false if there is no match.
6421 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6422 ReStateSet aStateSet[2], *pThis, *pNext;
6423 ReStateNumber aSpace[100];
6424 ReStateNumber *pToFree;
6426 unsigned int iSwap = 0;
6434 in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6436 /* Look for the initial prefix match, if there is one. */
6438 unsigned char x = pRe->zInit[0];
6439 while( in.i+pRe->nInit<=in.mx
6441 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6445 if( in.i+pRe->nInit>in.mx ) return 0;
6449 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6451 aStateSet[0].aState = aSpace;
6453 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6454 if( pToFree==0 ) return -1;
6455 aStateSet[0].aState = pToFree;
6457 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6458 pNext = &aStateSet[1];
6460 re_add_state(pNext, 0);
6461 while( c!=RE_EOF && pNext->nState>0 ){
6463 c = pRe->xNextChar(&in);
6465 pNext = &aStateSet[iSwap];
6468 for(i=0; i<pThis->nState; i++){
6469 int x = pThis->aState[i];
6470 switch( pRe->aOp[x] ){
6472 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6475 case RE_OP_ATSTART: {
6476 if( cPrev==RE_START ) re_add_state(pThis, x+1);
6480 if( c!=0 ) re_add_state(pNext, x+1);
6484 if( re_word_char(c) ) re_add_state(pNext, x+1);
6487 case RE_OP_NOTWORD: {
6488 if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6492 if( re_digit_char(c) ) re_add_state(pNext, x+1);
6495 case RE_OP_NOTDIGIT: {
6496 if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6500 if( re_space_char(c) ) re_add_state(pNext, x+1);
6503 case RE_OP_NOTSPACE: {
6504 if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6507 case RE_OP_BOUNDARY: {
6508 if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6511 case RE_OP_ANYSTAR: {
6512 re_add_state(pNext, x);
6513 re_add_state(pThis, x+1);
6517 re_add_state(pThis, x+pRe->aArg[x]);
6518 re_add_state(pThis, x+1);
6522 re_add_state(pThis, x+pRe->aArg[x]);
6525 case RE_OP_ACCEPT: {
6529 case RE_OP_CC_EXC: {
6531 /* fall-through */ goto re_op_cc_inc;
6533 case RE_OP_CC_INC: re_op_cc_inc: {
6535 int n = pRe->aArg[x];
6537 for(j=1; j>0 && j<n; j++){
6538 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6539 if( pRe->aArg[x+j]==c ){
6544 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6552 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6553 if( hit ) re_add_state(pNext, x+n);
6559 for(i=0; i<pNext->nState; i++){
6560 int x = pNext->aState[i];
6561 while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x];
6562 if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; }
6565 sqlite3_free(pToFree);
6569 /* Resize the opcode and argument arrays for an RE under construction.
6571 static int re_resize(ReCompiled *p, int N){
6574 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6575 if( aOp==0 ) return 1;
6577 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6578 if( aArg==0 ) return 1;
6584 /* Insert a new opcode and argument into an RE under construction. The
6585 ** insertion point is just prior to existing opcode iBefore.
6587 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6589 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6590 for(i=p->nState; i>iBefore; i--){
6591 p->aOp[i] = p->aOp[i-1];
6592 p->aArg[i] = p->aArg[i-1];
6595 p->aOp[iBefore] = (char)op;
6596 p->aArg[iBefore] = arg;
6600 /* Append a new opcode and argument to the end of the RE under construction.
6602 static int re_append(ReCompiled *p, int op, int arg){
6603 return re_insert(p, p->nState, op, arg);
6606 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6607 ** under construction.
6609 static void re_copy(ReCompiled *p, int iStart, int N){
6610 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6611 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6612 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6616 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
6617 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If
6618 ** c is not a hex digit *pV is unchanged.
6620 static int re_hex(int c, int *pV){
6621 if( c>='0' && c<='9' ){
6623 }else if( c>='a' && c<='f' ){
6625 }else if( c>='A' && c<='F' ){
6630 *pV = (*pV)*16 + (c & 0xff);
6634 /* A backslash character has been seen, read the next character and
6635 ** return its interpretation.
6637 static unsigned re_esc_char(ReCompiled *p){
6638 static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6639 static const char zTrans[] = "\a\f\n\r\t\v";
6642 if( p->sIn.i>=p->sIn.mx ) return 0;
6643 c = p->sIn.z[p->sIn.i];
6644 if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6645 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6646 if( re_hex(zIn[1],&v)
6647 && re_hex(zIn[2],&v)
6648 && re_hex(zIn[3],&v)
6649 && re_hex(zIn[4],&v)
6655 if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6656 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6657 if( re_hex(zIn[1],&v)
6658 && re_hex(zIn[2],&v)
6664 for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6666 if( i<6 ) c = zTrans[i];
6669 p->zErr = "unknown \\ escape";
6674 /* Forward declaration */
6675 static const char *re_subcompile_string(ReCompiled*);
6677 /* Peek at the next byte of input */
6678 static unsigned char rePeek(ReCompiled *p){
6679 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6682 /* Compile RE text into a sequence of opcodes. Continue up to the
6683 ** first unmatched ")" character, then return. If an error is found,
6684 ** return a pointer to the error message string.
6686 static const char *re_subcompile_re(ReCompiled *p){
6688 int iStart, iEnd, iGoto;
6690 zErr = re_subcompile_string(p);
6691 if( zErr ) return zErr;
6692 while( rePeek(p)=='|' ){
6694 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6695 iGoto = re_append(p, RE_OP_GOTO, 0);
6697 zErr = re_subcompile_string(p);
6698 if( zErr ) return zErr;
6699 p->aArg[iGoto] = p->nState - iGoto;
6704 /* Compile an element of regular expression text (anything that can be
6705 ** an operand to the "|" operator). Return NULL on success or a pointer
6706 ** to the error message if there is a problem.
6708 static const char *re_subcompile_string(ReCompiled *p){
6713 while( (c = p->xNextChar(&p->sIn))!=0 ){
6722 zErr = re_subcompile_re(p);
6723 if( zErr ) return zErr;
6724 if( rePeek(p)!=')' ) return "unmatched '('";
6729 if( rePeek(p)=='*' ){
6730 re_append(p, RE_OP_ANYSTAR, 0);
6733 re_append(p, RE_OP_ANY, 0);
6738 if( iPrev<0 ) return "'*' without operand";
6739 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6740 re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6744 if( iPrev<0 ) return "'+' without operand";
6745 re_append(p, RE_OP_FORK, iPrev - p->nState);
6749 if( iPrev<0 ) return "'?' without operand";
6750 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6754 re_append(p, RE_OP_MATCH, RE_EOF);
6758 re_append(p, RE_OP_ATSTART, 0);
6764 if( iPrev<0 ) return "'{m,n}' without operand";
6765 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6770 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6772 if( c!='}' ) return "unmatched '{'";
6773 if( n>0 && n<m ) return "n less than m in '{m,n}'";
6775 sz = p->nState - iPrev;
6777 if( n==0 ) return "both m and n are zero in '{m,n}'";
6778 re_insert(p, iPrev, RE_OP_FORK, sz+1);
6782 for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6785 re_append(p, RE_OP_FORK, sz+1);
6786 re_copy(p, iPrev, sz);
6789 re_append(p, RE_OP_FORK, -sz);
6794 unsigned int iFirst = p->nState;
6795 if( rePeek(p)=='^' ){
6796 re_append(p, RE_OP_CC_EXC, 0);
6799 re_append(p, RE_OP_CC_INC, 0);
6801 while( (c = p->xNextChar(&p->sIn))!=0 ){
6802 if( c=='[' && rePeek(p)==':' ){
6803 return "POSIX character classes not supported";
6805 if( c=='\\' ) c = re_esc_char(p);
6806 if( rePeek(p)=='-' ){
6807 re_append(p, RE_OP_CC_RANGE, c);
6809 c = p->xNextChar(&p->sIn);
6810 if( c=='\\' ) c = re_esc_char(p);
6811 re_append(p, RE_OP_CC_RANGE, c);
6813 re_append(p, RE_OP_CC_VALUE, c);
6815 if( rePeek(p)==']' ){ p->sIn.i++; break; }
6817 if( c==0 ) return "unclosed '['";
6818 if( p->nState>iFirst ) p->aArg[iFirst] = p->nState - iFirst;
6823 switch( rePeek(p) ){
6824 case 'b': specialOp = RE_OP_BOUNDARY; break;
6825 case 'd': specialOp = RE_OP_DIGIT; break;
6826 case 'D': specialOp = RE_OP_NOTDIGIT; break;
6827 case 's': specialOp = RE_OP_SPACE; break;
6828 case 'S': specialOp = RE_OP_NOTSPACE; break;
6829 case 'w': specialOp = RE_OP_WORD; break;
6830 case 'W': specialOp = RE_OP_NOTWORD; break;
6834 re_append(p, specialOp, 0);
6837 re_append(p, RE_OP_MATCH, c);
6842 re_append(p, RE_OP_MATCH, c);
6851 /* Free and reclaim all the memory used by a previously compiled
6852 ** regular expression. Applications should invoke this routine once
6853 ** for every call to re_compile() to avoid memory leaks.
6855 static void re_free(ReCompiled *pRe){
6857 sqlite3_free(pRe->aOp);
6858 sqlite3_free(pRe->aArg);
6864 ** Compile a textual regular expression in zIn[] into a compiled regular
6865 ** expression suitable for us by re_match() and return a pointer to the
6866 ** compiled regular expression in *ppRe. Return NULL on success or an
6867 ** error message if something goes wrong.
6869 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6875 pRe = sqlite3_malloc( sizeof(*pRe) );
6877 return "out of memory";
6879 memset(pRe, 0, sizeof(*pRe));
6880 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6881 if( re_resize(pRe, 30) ){
6883 return "out of memory";
6888 re_append(pRe, RE_OP_ANYSTAR, 0);
6890 pRe->sIn.z = (unsigned char*)zIn;
6892 pRe->sIn.mx = (int)strlen(zIn);
6893 zErr = re_subcompile_re(pRe);
6898 if( pRe->sIn.i>=pRe->sIn.mx ){
6899 re_append(pRe, RE_OP_ACCEPT, 0);
6903 return "unrecognized character";
6906 /* The following is a performance optimization. If the regex begins with
6907 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6908 ** one or more matching characters, enter those matching characters into
6909 ** zInit[]. The re_match() routine can then search ahead in the input
6910 ** string looking for the initial match without having to run the whole
6911 ** regex engine over the string. Do not worry about trying to match
6912 ** unicode characters beyond plane 0 - those are very rare and this is
6913 ** just an optimization. */
6914 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6915 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6916 unsigned x = pRe->aArg[i];
6918 pRe->zInit[j++] = (unsigned char)x;
6919 }else if( x<=0x7ff ){
6920 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6921 pRe->zInit[j++] = 0x80 | (x&0x3f);
6922 }else if( x<=0xffff ){
6923 pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
6924 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6925 pRe->zInit[j++] = 0x80 | (x&0x3f);
6930 if( j>0 && pRe->zInit[j-1]==0 ) j--;
6937 ** Implementation of the regexp() SQL function. This function implements
6938 ** the build-in REGEXP operator. The first argument to the function is the
6939 ** pattern and the second argument is the string. So, the SQL statements:
6943 ** is implemented as regexp(B,A).
6945 static void re_sql_func(
6946 sqlite3_context *context,
6948 sqlite3_value **argv
6950 ReCompiled *pRe; /* Compiled regular expression */
6951 const char *zPattern; /* The regular expression */
6952 const unsigned char *zStr;/* String being searched */
6953 const char *zErr; /* Compile error message */
6954 int setAux = 0; /* True to invoke sqlite3_set_auxdata() */
6956 (void)argc; /* Unused */
6957 pRe = sqlite3_get_auxdata(context, 0);
6959 zPattern = (const char*)sqlite3_value_text(argv[0]);
6960 if( zPattern==0 ) return;
6961 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6964 sqlite3_result_error(context, zErr, -1);
6968 sqlite3_result_error_nomem(context);
6973 zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6975 sqlite3_result_int(context, re_match(pRe, zStr, -1));
6978 sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6982 #if defined(SQLITE_DEBUG)
6984 ** This function is used for testing and debugging only. It is only available
6985 ** if the SQLITE_DEBUG compile-time option is used.
6987 ** Compile a regular expression and then convert the compiled expression into
6988 ** text and return that text.
6990 static void re_bytecode_func(
6991 sqlite3_context *context,
6993 sqlite3_value **argv
6995 const char *zPattern;
7004 zPattern = (const char*)sqlite3_value_text(argv[0]);
7005 if( zPattern==0 ) return;
7006 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
7009 sqlite3_result_error(context, zErr, -1);
7013 sqlite3_result_error_nomem(context);
7016 pStr = sqlite3_str_new(0);
7017 if( pStr==0 ) goto re_bytecode_func_err;
7019 sqlite3_str_appendf(pStr, "INIT ");
7020 for(i=0; i<pRe->nInit; i++){
7021 sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]);
7023 sqlite3_str_appendf(pStr, "\n");
7025 for(i=0; (unsigned)i<pRe->nState; i++){
7026 sqlite3_str_appendf(pStr, "%-8s %4d\n",
7027 ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]);
7029 n = sqlite3_str_length(pStr);
7030 z = sqlite3_str_finish(pStr);
7034 sqlite3_result_text(context, z, n-1, sqlite3_free);
7037 re_bytecode_func_err:
7041 #endif /* SQLITE_DEBUG */
7045 ** Invoke this routine to register the regexp() function with the
7046 ** SQLite database connection.
7051 int sqlite3_regexp_init(
7054 const sqlite3_api_routines *pApi
7057 SQLITE_EXTENSION_INIT2(pApi);
7058 (void)pzErrMsg; /* Unused */
7059 rc = sqlite3_create_function(db, "regexp", 2,
7060 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7061 0, re_sql_func, 0, 0);
7062 if( rc==SQLITE_OK ){
7063 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
7064 ** of regexp(PATTERN,STRING). */
7065 rc = sqlite3_create_function(db, "regexpi", 2,
7066 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7067 (void*)db, re_sql_func, 0, 0);
7068 #if defined(SQLITE_DEBUG)
7069 if( rc==SQLITE_OK ){
7070 rc = sqlite3_create_function(db, "regexp_bytecode", 1,
7071 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7072 0, re_bytecode_func, 0, 0);
7074 #endif /* SQLITE_DEBUG */
7079 /************************* End ../ext/misc/regexp.c ********************/
7080 #ifndef SQLITE_SHELL_FIDDLE
7081 /************************* Begin ../ext/misc/fileio.c ******************/
7085 ** The author disclaims copyright to this source code. In place of
7086 ** a legal notice, here is a blessing:
7088 ** May you do good and not evil.
7089 ** May you find forgiveness for yourself and forgive others.
7090 ** May you share freely, never taking more than you give.
7092 ******************************************************************************
7094 ** This SQLite extension implements SQL functions readfile() and
7095 ** writefile(), and eponymous virtual type "fsdir".
7097 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
7099 ** If neither of the optional arguments is present, then this UDF
7100 ** function writes blob DATA to file FILE. If successful, the number
7101 ** of bytes written is returned. If an error occurs, NULL is returned.
7103 ** If the first option argument - MODE - is present, then it must
7104 ** be passed an integer value that corresponds to a POSIX mode
7105 ** value (file type + permissions, as returned in the stat.st_mode
7106 ** field by the stat() system call). Three types of files may
7107 ** be written/created:
7109 ** regular files: (mode & 0170000)==0100000
7110 ** symbolic links: (mode & 0170000)==0120000
7111 ** directories: (mode & 0170000)==0040000
7113 ** For a directory, the DATA is ignored. For a symbolic link, it is
7114 ** interpreted as text and used as the target of the link. For a
7115 ** regular file, it is interpreted as a blob and written into the
7116 ** named file. Regardless of the type of file, its permissions are
7117 ** set to (mode & 0777) before returning.
7119 ** If the optional MTIME argument is present, then it is interpreted
7120 ** as an integer - the number of seconds since the unix epoch. The
7121 ** modification-time of the target file is set to this value before
7124 ** If three or more arguments are passed to this function and an
7125 ** error is encountered, an exception is raised.
7129 ** Read and return the contents of file FILE (type blob) from disk.
7135 ** SELECT * FROM fsdir($path [, $dir]);
7137 ** Parameter $path is an absolute or relative pathname. If the file that it
7138 ** refers to does not exist, it is an error. If the path refers to a regular
7139 ** file or symbolic link, it returns a single row. Or, if the path refers
7140 ** to a directory, it returns one row for the directory, and one row for each
7141 ** file within the hierarchy rooted at $path.
7143 ** Each row has the following columns:
7145 ** name: Path to file or directory (text value).
7146 ** mode: Value of stat.st_mode for directory entry (an integer).
7147 ** mtime: Value of stat.st_mtime for directory entry (an integer).
7148 ** data: For a regular file, a blob containing the file data. For a
7149 ** symlink, a text value containing the text of the link. For a
7152 ** If a non-NULL value is specified for the optional $dir parameter and
7153 ** $path is a relative path, then $path is interpreted relative to $dir.
7154 ** And the paths returned in the "name" column of the table are also
7155 ** relative to directory $dir.
7157 ** Notes on building this extension for Windows:
7158 ** Unless linked statically with the SQLite library, a preprocessor
7159 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
7160 ** DLL form of this extension for WIN32. See its use below for details.
7162 /* #include "sqlite3ext.h" */
7163 SQLITE_EXTENSION_INIT1
7168 #include <sys/types.h>
7169 #include <sys/stat.h>
7171 #if !defined(_WIN32) && !defined(WIN32)
7172 # include <unistd.h>
7173 # include <dirent.h>
7175 # include <sys/time.h>
7177 # include "windows.h"
7179 # include <direct.h>
7180 /* # include "test_windirent.h" */
7181 # define dirent DIRENT
7183 # define chmod _chmod
7188 # define mkdir(path,mode) _mkdir(path)
7189 # define lstat(path,buf) stat(path,buf)
7196 ** Structure of the fsdir() table-valued function
7199 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
7200 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
7201 #define FSDIR_COLUMN_MODE 1 /* Access mode */
7202 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
7203 #define FSDIR_COLUMN_DATA 3 /* File content */
7204 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
7205 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
7209 ** Set the result stored by context ctx to a blob containing the
7210 ** contents of file zName. Or, leave the result unchanged (NULL)
7211 ** if the file does not exist or is unreadable.
7213 ** If the file exceeds the SQLite blob size limit, through an
7214 ** SQLITE_TOOBIG error.
7216 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
7219 static void readFileContents(sqlite3_context *ctx, const char *zName){
7226 in = fopen(zName, "rb");
7228 /* File does not exist or is unreadable. Leave the result set to NULL. */
7231 fseek(in, 0, SEEK_END);
7234 db = sqlite3_context_db_handle(ctx);
7235 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
7237 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
7241 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
7243 sqlite3_result_error_nomem(ctx);
7247 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
7248 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
7250 sqlite3_result_error_code(ctx, SQLITE_IOERR);
7257 ** Implementation of the "readfile(X)" SQL function. The entire content
7258 ** of the file named X is read and returned as a BLOB. NULL is returned
7259 ** if the file does not exist or is unreadable.
7261 static void readfileFunc(
7262 sqlite3_context *context,
7264 sqlite3_value **argv
7267 (void)(argc); /* Unused parameter */
7268 zName = (const char*)sqlite3_value_text(argv[0]);
7269 if( zName==0 ) return;
7270 readFileContents(context, zName);
7274 ** Set the error message contained in context ctx to the results of
7275 ** vprintf(zFmt, ...).
7277 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
7281 zMsg = sqlite3_vmprintf(zFmt, ap);
7282 sqlite3_result_error(ctx, zMsg, -1);
7289 ** This function is designed to convert a Win32 FILETIME structure into the
7290 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
7292 static sqlite3_uint64 fileTimeToUnixTime(
7293 LPFILETIME pFileTime
7295 SYSTEMTIME epochSystemTime;
7296 ULARGE_INTEGER epochIntervals;
7297 FILETIME epochFileTime;
7298 ULARGE_INTEGER fileIntervals;
7300 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
7301 epochSystemTime.wYear = 1970;
7302 epochSystemTime.wMonth = 1;
7303 epochSystemTime.wDay = 1;
7304 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
7305 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
7306 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
7308 fileIntervals.LowPart = pFileTime->dwLowDateTime;
7309 fileIntervals.HighPart = pFileTime->dwHighDateTime;
7311 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
7315 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
7316 # /* To allow a standalone DLL, use this next replacement function: */
7317 # undef sqlite3_win32_utf8_to_unicode
7318 # define sqlite3_win32_utf8_to_unicode utf8_to_utf16
7320 LPWSTR utf8_to_utf16(const char *z){
7321 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
7322 LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
7323 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
7331 ** This function attempts to normalize the time values found in the stat()
7332 ** buffer to UTC. This is necessary on Win32, where the runtime library
7333 ** appears to return these values as local times.
7335 static void statTimesToUtc(
7337 struct stat *pStatBuf
7340 WIN32_FIND_DATAW fd;
7341 LPWSTR zUnicodeName;
7342 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
7343 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
7345 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
7346 hFindFile = FindFirstFileW(zUnicodeName, &fd);
7347 if( hFindFile!=NULL ){
7348 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
7349 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
7350 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
7351 FindClose(hFindFile);
7353 sqlite3_free(zUnicodeName);
7359 ** This function is used in place of stat(). On Windows, special handling
7360 ** is required in order for the included time to be returned as UTC. On all
7361 ** other systems, this function simply calls stat().
7363 static int fileStat(
7365 struct stat *pStatBuf
7368 int rc = stat(zPath, pStatBuf);
7369 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
7372 return stat(zPath, pStatBuf);
7377 ** This function is used in place of lstat(). On Windows, special handling
7378 ** is required in order for the included time to be returned as UTC. On all
7379 ** other systems, this function simply calls lstat().
7381 static int fileLinkStat(
7383 struct stat *pStatBuf
7386 int rc = lstat(zPath, pStatBuf);
7387 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
7390 return lstat(zPath, pStatBuf);
7395 ** Argument zFile is the name of a file that will be created and/or written
7396 ** by SQL function writefile(). This function ensures that the directory
7397 ** zFile will be written to exists, creating it if required. The permissions
7398 ** for any path components created by this function are set in accordance
7399 ** with the current umask.
7401 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
7402 ** SQLITE_OK is returned if the directory is successfully created, or
7403 ** SQLITE_ERROR otherwise.
7405 static int makeDirectory(
7408 char *zCopy = sqlite3_mprintf("%s", zFile);
7414 int nCopy = (int)strlen(zCopy);
7417 while( rc==SQLITE_OK ){
7421 for(; zCopy[i]!='/' && i<nCopy; i++);
7422 if( i==nCopy ) break;
7425 rc2 = fileStat(zCopy, &sStat);
7427 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
7429 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
7435 sqlite3_free(zCopy);
7442 ** This function does the work for the writefile() UDF. Refer to
7443 ** header comments at the top of this file for details.
7445 static int writeFile(
7446 sqlite3_context *pCtx, /* Context to return bytes written in */
7447 const char *zFile, /* File to write */
7448 sqlite3_value *pData, /* Data to write */
7449 mode_t mode, /* MODE parameter passed to writefile() */
7450 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
7452 if( zFile==0 ) return 1;
7453 #if !defined(_WIN32) && !defined(WIN32)
7454 if( S_ISLNK(mode) ){
7455 const char *zTo = (const char*)sqlite3_value_text(pData);
7456 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
7460 if( S_ISDIR(mode) ){
7461 if( mkdir(zFile, mode) ){
7462 /* The mkdir() call to create the directory failed. This might not
7463 ** be an error though - if there is already a directory at the same
7464 ** path and either the permissions already match or can be changed
7465 ** to do so using chmod(), it is not an error. */
7468 || 0!=fileStat(zFile, &sStat)
7469 || !S_ISDIR(sStat.st_mode)
7470 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
7476 sqlite3_int64 nWrite = 0;
7479 FILE *out = fopen(zFile, "wb");
7480 if( out==0 ) return 1;
7481 z = (const char*)sqlite3_value_blob(pData);
7483 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
7484 nWrite = sqlite3_value_bytes(pData);
7490 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
7494 sqlite3_result_int64(pCtx, nWrite);
7500 #if !SQLITE_OS_WINRT
7502 FILETIME lastAccess;
7504 SYSTEMTIME currentTime;
7507 LPWSTR zUnicodeName;
7508 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
7510 GetSystemTime(¤tTime);
7511 SystemTimeToFileTime(¤tTime, &lastAccess);
7512 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
7513 lastWrite.dwLowDateTime = (DWORD)intervals;
7514 lastWrite.dwHighDateTime = intervals >> 32;
7515 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
7516 if( zUnicodeName==0 ){
7519 hFile = CreateFileW(
7520 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
7521 FILE_FLAG_BACKUP_SEMANTICS, NULL
7523 sqlite3_free(zUnicodeName);
7524 if( hFile!=INVALID_HANDLE_VALUE ){
7525 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
7532 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
7534 struct timespec times[2];
7535 times[0].tv_nsec = times[1].tv_nsec = 0;
7536 times[0].tv_sec = time(0);
7537 times[1].tv_sec = mtime;
7538 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
7543 struct timeval times[2];
7544 times[0].tv_usec = times[1].tv_usec = 0;
7545 times[0].tv_sec = time(0);
7546 times[1].tv_sec = mtime;
7547 if( utimes(zFile, times) ){
7557 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
7558 ** Refer to header comments at the top of this file for details.
7560 static void writefileFunc(
7561 sqlite3_context *context,
7563 sqlite3_value **argv
7568 sqlite3_int64 mtime = -1;
7570 if( argc<2 || argc>4 ){
7571 sqlite3_result_error(context,
7572 "wrong number of arguments to function writefile()", -1
7577 zFile = (const char*)sqlite3_value_text(argv[0]);
7578 if( zFile==0 ) return;
7580 mode = (mode_t)sqlite3_value_int(argv[2]);
7583 mtime = sqlite3_value_int64(argv[3]);
7586 res = writeFile(context, zFile, argv[1], mode, mtime);
7587 if( res==1 && errno==ENOENT ){
7588 if( makeDirectory(zFile)==SQLITE_OK ){
7589 res = writeFile(context, zFile, argv[1], mode, mtime);
7593 if( argc>2 && res!=0 ){
7594 if( S_ISLNK(mode) ){
7595 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
7596 }else if( S_ISDIR(mode) ){
7597 ctxErrorMsg(context, "failed to create directory: %s", zFile);
7599 ctxErrorMsg(context, "failed to write file: %s", zFile);
7605 ** SQL function: lsmode(MODE)
7607 ** Given a numberic st_mode from stat(), convert it into a human-readable
7608 ** text string in the style of "ls -l".
7610 static void lsModeFunc(
7611 sqlite3_context *context,
7613 sqlite3_value **argv
7616 int iMode = sqlite3_value_int(argv[0]);
7619 if( S_ISLNK(iMode) ){
7621 }else if( S_ISREG(iMode) ){
7623 }else if( S_ISDIR(iMode) ){
7629 int m = (iMode >> ((2-i)*3));
7630 char *a = &z[1 + i*3];
7631 a[0] = (m & 0x4) ? 'r' : '-';
7632 a[1] = (m & 0x2) ? 'w' : '-';
7633 a[2] = (m & 0x1) ? 'x' : '-';
7636 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
7639 #ifndef SQLITE_OMIT_VIRTUALTABLE
7642 ** Cursor type for recursively iterating through a directory structure.
7644 typedef struct fsdir_cursor fsdir_cursor;
7645 typedef struct FsdirLevel FsdirLevel;
7648 DIR *pDir; /* From opendir() */
7649 char *zDir; /* Name of directory (nul-terminated) */
7652 struct fsdir_cursor {
7653 sqlite3_vtab_cursor base; /* Base class - must be first */
7655 int nLvl; /* Number of entries in aLvl[] array */
7656 int iLvl; /* Index of current entry */
7657 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
7662 struct stat sStat; /* Current lstat() results */
7663 char *zPath; /* Path to current entry */
7664 sqlite3_int64 iRowid; /* Current rowid */
7667 typedef struct fsdir_tab fsdir_tab;
7669 sqlite3_vtab base; /* Base class - must be first */
7673 ** Construct a new fsdir virtual table object.
7675 static int fsdirConnect(
7678 int argc, const char *const*argv,
7679 sqlite3_vtab **ppVtab,
7682 fsdir_tab *pNew = 0;
7688 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
7689 if( rc==SQLITE_OK ){
7690 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
7691 if( pNew==0 ) return SQLITE_NOMEM;
7692 memset(pNew, 0, sizeof(*pNew));
7693 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7695 *ppVtab = (sqlite3_vtab*)pNew;
7700 ** This method is the destructor for fsdir vtab objects.
7702 static int fsdirDisconnect(sqlite3_vtab *pVtab){
7703 sqlite3_free(pVtab);
7708 ** Constructor for a new fsdir_cursor object.
7710 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
7713 pCur = sqlite3_malloc( sizeof(*pCur) );
7714 if( pCur==0 ) return SQLITE_NOMEM;
7715 memset(pCur, 0, sizeof(*pCur));
7717 *ppCursor = &pCur->base;
7722 ** Reset a cursor back to the state it was in when first returned
7725 static void fsdirResetCursor(fsdir_cursor *pCur){
7727 for(i=0; i<=pCur->iLvl; i++){
7728 FsdirLevel *pLvl = &pCur->aLvl[i];
7729 if( pLvl->pDir ) closedir(pLvl->pDir);
7730 sqlite3_free(pLvl->zDir);
7732 sqlite3_free(pCur->zPath);
7733 sqlite3_free(pCur->aLvl);
7744 ** Destructor for an fsdir_cursor.
7746 static int fsdirClose(sqlite3_vtab_cursor *cur){
7747 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7749 fsdirResetCursor(pCur);
7755 ** Set the error message for the virtual table associated with cursor
7756 ** pCur to the results of vprintf(zFmt, ...).
7758 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
7761 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7767 ** Advance an fsdir_cursor to its next row of output.
7769 static int fsdirNext(sqlite3_vtab_cursor *cur){
7770 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7771 mode_t m = pCur->sStat.st_mode;
7775 /* Descend into this directory */
7776 int iNew = pCur->iLvl + 1;
7778 if( iNew>=pCur->nLvl ){
7780 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
7781 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
7782 if( aNew==0 ) return SQLITE_NOMEM;
7783 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
7788 pLvl = &pCur->aLvl[iNew];
7790 pLvl->zDir = pCur->zPath;
7792 pLvl->pDir = opendir(pLvl->zDir);
7793 if( pLvl->pDir==0 ){
7794 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
7795 return SQLITE_ERROR;
7799 while( pCur->iLvl>=0 ){
7800 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
7801 struct dirent *pEntry = readdir(pLvl->pDir);
7803 if( pEntry->d_name[0]=='.' ){
7804 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
7805 if( pEntry->d_name[1]=='\0' ) continue;
7807 sqlite3_free(pCur->zPath);
7808 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
7809 if( pCur->zPath==0 ) return SQLITE_NOMEM;
7810 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
7811 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
7812 return SQLITE_ERROR;
7816 closedir(pLvl->pDir);
7817 sqlite3_free(pLvl->zDir);
7824 sqlite3_free(pCur->zPath);
7830 ** Return values of columns for the row at which the series_cursor
7831 ** is currently pointing.
7833 static int fsdirColumn(
7834 sqlite3_vtab_cursor *cur, /* The cursor */
7835 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
7836 int i /* Which column to return */
7838 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7840 case FSDIR_COLUMN_NAME: {
7841 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
7845 case FSDIR_COLUMN_MODE:
7846 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
7849 case FSDIR_COLUMN_MTIME:
7850 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
7853 case FSDIR_COLUMN_DATA: {
7854 mode_t m = pCur->sStat.st_mode;
7856 sqlite3_result_null(ctx);
7857 #if !defined(_WIN32) && !defined(WIN32)
7858 }else if( S_ISLNK(m) ){
7860 char *aBuf = aStatic;
7861 sqlite3_int64 nBuf = 64;
7865 n = readlink(pCur->zPath, aBuf, nBuf);
7867 if( aBuf!=aStatic ) sqlite3_free(aBuf);
7869 aBuf = sqlite3_malloc64(nBuf);
7871 sqlite3_result_error_nomem(ctx);
7872 return SQLITE_NOMEM;
7876 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
7877 if( aBuf!=aStatic ) sqlite3_free(aBuf);
7880 readFileContents(ctx, pCur->zPath);
7883 case FSDIR_COLUMN_PATH:
7885 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
7886 ** always return their values as NULL */
7894 ** Return the rowid for the current row. In this implementation, the
7895 ** first row returned is assigned rowid value 1, and each subsequent
7896 ** row a value 1 more than that of the previous.
7898 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7899 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7900 *pRowid = pCur->iRowid;
7905 ** Return TRUE if the cursor has been moved off of the last
7908 static int fsdirEof(sqlite3_vtab_cursor *cur){
7909 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7910 return (pCur->zPath==0);
7914 ** xFilter callback.
7916 ** idxNum==1 PATH parameter only
7917 ** idxNum==2 Both PATH and DIR supplied
7919 static int fsdirFilter(
7920 sqlite3_vtab_cursor *cur,
7921 int idxNum, const char *idxStr,
7922 int argc, sqlite3_value **argv
7924 const char *zDir = 0;
7925 fsdir_cursor *pCur = (fsdir_cursor*)cur;
7927 fsdirResetCursor(pCur);
7930 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
7931 return SQLITE_ERROR;
7934 assert( argc==idxNum && (argc==1 || argc==2) );
7935 zDir = (const char*)sqlite3_value_text(argv[0]);
7937 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
7938 return SQLITE_ERROR;
7941 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
7944 pCur->nBase = (int)strlen(pCur->zBase)+1;
7945 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
7947 pCur->zPath = sqlite3_mprintf("%s", zDir);
7950 if( pCur->zPath==0 ){
7951 return SQLITE_NOMEM;
7953 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
7954 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
7955 return SQLITE_ERROR;
7962 ** SQLite will invoke this method one or more times while planning a query
7963 ** that uses the generate_series virtual table. This routine needs to create
7964 ** a query plan for each invocation and compute an estimated cost for that
7967 ** In this implementation idxNum is used to represent the
7968 ** query plan. idxStr is unused.
7970 ** The query plan is represented by values of idxNum:
7972 ** (1) The path value is supplied by argv[0]
7973 ** (2) Path is in argv[0] and dir is in argv[1]
7975 static int fsdirBestIndex(
7977 sqlite3_index_info *pIdxInfo
7979 int i; /* Loop over constraints */
7980 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
7981 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
7982 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
7983 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
7984 const struct sqlite3_index_constraint *pConstraint;
7987 pConstraint = pIdxInfo->aConstraint;
7988 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
7989 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
7990 switch( pConstraint->iColumn ){
7991 case FSDIR_COLUMN_PATH: {
7992 if( pConstraint->usable ){
7995 }else if( idxPath<0 ){
8000 case FSDIR_COLUMN_DIR: {
8001 if( pConstraint->usable ){
8004 }else if( idxDir<0 ){
8011 if( seenPath || seenDir ){
8012 /* If input parameters are unusable, disallow this plan */
8013 return SQLITE_CONSTRAINT;
8017 pIdxInfo->idxNum = 0;
8018 /* The pIdxInfo->estimatedCost should have been initialized to a huge
8019 ** number. Leave it unchanged. */
8020 pIdxInfo->estimatedRows = 0x7fffffff;
8022 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
8023 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
8025 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
8026 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
8027 pIdxInfo->idxNum = 2;
8028 pIdxInfo->estimatedCost = 10.0;
8030 pIdxInfo->idxNum = 1;
8031 pIdxInfo->estimatedCost = 100.0;
8039 ** Register the "fsdir" virtual table.
8041 static int fsdirRegister(sqlite3 *db){
8042 static sqlite3_module fsdirModule = {
8045 fsdirConnect, /* xConnect */
8046 fsdirBestIndex, /* xBestIndex */
8047 fsdirDisconnect, /* xDisconnect */
8049 fsdirOpen, /* xOpen - open a cursor */
8050 fsdirClose, /* xClose - close a cursor */
8051 fsdirFilter, /* xFilter - configure scan constraints */
8052 fsdirNext, /* xNext - advance a cursor */
8053 fsdirEof, /* xEof - check for end of scan */
8054 fsdirColumn, /* xColumn - read data */
8055 fsdirRowid, /* xRowid - read data */
8061 0, /* xFindMethod */
8065 0, /* xRollbackTo */
8066 0, /* xShadowName */
8070 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
8073 #else /* SQLITE_OMIT_VIRTUALTABLE */
8074 # define fsdirRegister(x) SQLITE_OK
8080 int sqlite3_fileio_init(
8083 const sqlite3_api_routines *pApi
8086 SQLITE_EXTENSION_INIT2(pApi);
8087 (void)pzErrMsg; /* Unused parameter */
8088 rc = sqlite3_create_function(db, "readfile", 1,
8089 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
8090 readfileFunc, 0, 0);
8091 if( rc==SQLITE_OK ){
8092 rc = sqlite3_create_function(db, "writefile", -1,
8093 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
8094 writefileFunc, 0, 0);
8096 if( rc==SQLITE_OK ){
8097 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
8100 if( rc==SQLITE_OK ){
8101 rc = fsdirRegister(db);
8106 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
8107 /* To allow a standalone DLL, make test_windirent.c use the same
8108 * redefined SQLite API calls as the above extension code does.
8109 * Just pull in this .c to accomplish this. As a beneficial side
8110 * effect, this extension becomes a single translation unit. */
8111 # include "test_windirent.c"
8114 /************************* End ../ext/misc/fileio.c ********************/
8115 /************************* Begin ../ext/misc/completion.c ******************/
8119 ** The author disclaims copyright to this source code. In place of
8120 ** a legal notice, here is a blessing:
8122 ** May you do good and not evil.
8123 ** May you find forgiveness for yourself and forgive others.
8124 ** May you share freely, never taking more than you give.
8126 *************************************************************************
8128 ** This file implements an eponymous virtual table that returns suggested
8129 ** completions for a partial SQL input.
8133 ** SELECT DISTINCT candidate COLLATE nocase
8134 ** FROM completion($prefix,$wholeline)
8137 ** The two query parameters are optional. $prefix is the text of the
8138 ** current word being typed and that is to be completed. $wholeline is
8139 ** the complete input line, used for context.
8141 ** The raw completion() table might return the same candidate multiple
8142 ** times, for example if the same column name is used to two or more
8143 ** tables. And the candidates are returned in an arbitrary order. Hence,
8144 ** the DISTINCT and ORDER BY are recommended.
8146 ** This virtual table operates at the speed of human typing, and so there
8147 ** is no attempt to make it fast. Even a slow implementation will be much
8148 ** faster than any human can type.
8151 /* #include "sqlite3ext.h" */
8152 SQLITE_EXTENSION_INIT1
8157 #ifndef SQLITE_OMIT_VIRTUALTABLE
8159 /* completion_vtab is a subclass of sqlite3_vtab which will
8160 ** serve as the underlying representation of a completion virtual table
8162 typedef struct completion_vtab completion_vtab;
8163 struct completion_vtab {
8164 sqlite3_vtab base; /* Base class - must be first */
8165 sqlite3 *db; /* Database connection for this completion vtab */
8168 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
8169 ** serve as the underlying representation of a cursor that scans
8170 ** over rows of the result
8172 typedef struct completion_cursor completion_cursor;
8173 struct completion_cursor {
8174 sqlite3_vtab_cursor base; /* Base class - must be first */
8175 sqlite3 *db; /* Database connection for this cursor */
8176 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
8177 char *zPrefix; /* The prefix for the word we want to complete */
8178 char *zLine; /* The whole that we want to complete */
8179 const char *zCurrentRow; /* Current output row */
8180 int szRow; /* Length of the zCurrentRow string */
8181 sqlite3_stmt *pStmt; /* Current statement */
8182 sqlite3_int64 iRowid; /* The rowid */
8183 int ePhase; /* Current phase */
8184 int j; /* inter-phase counter */
8187 /* Values for ePhase:
8189 #define COMPLETION_FIRST_PHASE 1
8190 #define COMPLETION_KEYWORDS 1
8191 #define COMPLETION_PRAGMAS 2
8192 #define COMPLETION_FUNCTIONS 3
8193 #define COMPLETION_COLLATIONS 4
8194 #define COMPLETION_INDEXES 5
8195 #define COMPLETION_TRIGGERS 6
8196 #define COMPLETION_DATABASES 7
8197 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
8198 #define COMPLETION_COLUMNS 9
8199 #define COMPLETION_MODULES 10
8200 #define COMPLETION_EOF 11
8203 ** The completionConnect() method is invoked to create a new
8204 ** completion_vtab that describes the completion virtual table.
8206 ** Think of this routine as the constructor for completion_vtab objects.
8208 ** All this routine needs to do is:
8210 ** (1) Allocate the completion_vtab object and initialize all fields.
8212 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
8213 ** result set of queries against completion will look like.
8215 static int completionConnect(
8218 int argc, const char *const*argv,
8219 sqlite3_vtab **ppVtab,
8222 completion_vtab *pNew;
8225 (void)(pAux); /* Unused parameter */
8226 (void)(argc); /* Unused parameter */
8227 (void)(argv); /* Unused parameter */
8228 (void)(pzErr); /* Unused parameter */
8230 /* Column numbers */
8231 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
8232 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
8233 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
8234 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
8236 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
8237 rc = sqlite3_declare_vtab(db,
8240 " prefix TEXT HIDDEN,"
8241 " wholeline TEXT HIDDEN,"
8242 " phase INT HIDDEN" /* Used for debugging only */
8244 if( rc==SQLITE_OK ){
8245 pNew = sqlite3_malloc( sizeof(*pNew) );
8246 *ppVtab = (sqlite3_vtab*)pNew;
8247 if( pNew==0 ) return SQLITE_NOMEM;
8248 memset(pNew, 0, sizeof(*pNew));
8255 ** This method is the destructor for completion_cursor objects.
8257 static int completionDisconnect(sqlite3_vtab *pVtab){
8258 sqlite3_free(pVtab);
8263 ** Constructor for a new completion_cursor object.
8265 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
8266 completion_cursor *pCur;
8267 pCur = sqlite3_malloc( sizeof(*pCur) );
8268 if( pCur==0 ) return SQLITE_NOMEM;
8269 memset(pCur, 0, sizeof(*pCur));
8270 pCur->db = ((completion_vtab*)p)->db;
8271 *ppCursor = &pCur->base;
8276 ** Reset the completion_cursor.
8278 static void completionCursorReset(completion_cursor *pCur){
8279 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
8280 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
8281 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
8286 ** Destructor for a completion_cursor.
8288 static int completionClose(sqlite3_vtab_cursor *cur){
8289 completionCursorReset((completion_cursor*)cur);
8295 ** Advance a completion_cursor to its next row of output.
8297 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
8298 ** record the current state of the scan. This routine sets ->zCurrentRow
8299 ** to the current row of output and then returns. If no more rows remain,
8300 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
8301 ** table that has reached the end of its scan.
8303 ** The current implementation just lists potential identifiers and
8304 ** keywords and filters them by zPrefix. Future enhancements should
8305 ** take zLine into account to try to restrict the set of identifiers and
8306 ** keywords based on what would be legal at the current point of input.
8308 static int completionNext(sqlite3_vtab_cursor *cur){
8309 completion_cursor *pCur = (completion_cursor*)cur;
8310 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
8311 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
8313 while( pCur->ePhase!=COMPLETION_EOF ){
8314 switch( pCur->ePhase ){
8315 case COMPLETION_KEYWORDS: {
8316 if( pCur->j >= sqlite3_keyword_count() ){
8317 pCur->zCurrentRow = 0;
8318 pCur->ePhase = COMPLETION_DATABASES;
8320 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
8325 case COMPLETION_DATABASES: {
8326 if( pCur->pStmt==0 ){
8327 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
8331 eNextPhase = COMPLETION_TABLES;
8334 case COMPLETION_TABLES: {
8335 if( pCur->pStmt==0 ){
8338 const char *zSep = "";
8339 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
8340 while( sqlite3_step(pS2)==SQLITE_ROW ){
8341 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8342 zSql = sqlite3_mprintf(
8344 "SELECT name FROM \"%w\".sqlite_schema",
8347 if( zSql==0 ) return SQLITE_NOMEM;
8350 sqlite3_finalize(pS2);
8351 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
8355 eNextPhase = COMPLETION_COLUMNS;
8358 case COMPLETION_COLUMNS: {
8359 if( pCur->pStmt==0 ){
8362 const char *zSep = "";
8363 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
8364 while( sqlite3_step(pS2)==SQLITE_ROW ){
8365 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8366 zSql = sqlite3_mprintf(
8368 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
8369 " JOIN pragma_table_info(sm.name,%Q) AS pti"
8370 " WHERE sm.type='table'",
8371 zSql, zSep, zDb, zDb
8373 if( zSql==0 ) return SQLITE_NOMEM;
8376 sqlite3_finalize(pS2);
8377 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
8381 eNextPhase = COMPLETION_EOF;
8386 /* This case is when the phase presets zCurrentRow */
8387 if( pCur->zCurrentRow==0 ) continue;
8389 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
8390 /* Extract the next row of content */
8391 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
8392 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
8394 /* When all rows are finished, advance to the next phase */
8395 sqlite3_finalize(pCur->pStmt);
8397 pCur->ePhase = eNextPhase;
8401 if( pCur->nPrefix==0 ) break;
8402 if( pCur->nPrefix<=pCur->szRow
8403 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
8413 ** Return values of columns for the row at which the completion_cursor
8414 ** is currently pointing.
8416 static int completionColumn(
8417 sqlite3_vtab_cursor *cur, /* The cursor */
8418 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
8419 int i /* Which column to return */
8421 completion_cursor *pCur = (completion_cursor*)cur;
8423 case COMPLETION_COLUMN_CANDIDATE: {
8424 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
8427 case COMPLETION_COLUMN_PREFIX: {
8428 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
8431 case COMPLETION_COLUMN_WHOLELINE: {
8432 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
8435 case COMPLETION_COLUMN_PHASE: {
8436 sqlite3_result_int(ctx, pCur->ePhase);
8444 ** Return the rowid for the current row. In this implementation, the
8445 ** rowid is the same as the output value.
8447 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8448 completion_cursor *pCur = (completion_cursor*)cur;
8449 *pRowid = pCur->iRowid;
8454 ** Return TRUE if the cursor has been moved off of the last
8457 static int completionEof(sqlite3_vtab_cursor *cur){
8458 completion_cursor *pCur = (completion_cursor*)cur;
8459 return pCur->ePhase >= COMPLETION_EOF;
8463 ** This method is called to "rewind" the completion_cursor object back
8464 ** to the first row of output. This method is always called at least
8465 ** once prior to any call to completionColumn() or completionRowid() or
8468 static int completionFilter(
8469 sqlite3_vtab_cursor *pVtabCursor,
8470 int idxNum, const char *idxStr,
8471 int argc, sqlite3_value **argv
8473 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
8475 (void)(idxStr); /* Unused parameter */
8476 (void)(argc); /* Unused parameter */
8477 completionCursorReset(pCur);
8479 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
8480 if( pCur->nPrefix>0 ){
8481 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8482 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8487 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
8488 if( pCur->nLine>0 ){
8489 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8490 if( pCur->zLine==0 ) return SQLITE_NOMEM;
8493 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
8494 int i = pCur->nLine;
8495 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
8498 pCur->nPrefix = pCur->nLine - i;
8499 if( pCur->nPrefix>0 ){
8500 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
8501 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8505 pCur->ePhase = COMPLETION_FIRST_PHASE;
8506 return completionNext(pVtabCursor);
8510 ** SQLite will invoke this method one or more times while planning a query
8511 ** that uses the completion virtual table. This routine needs to create
8512 ** a query plan for each invocation and compute an estimated cost for that
8515 ** There are two hidden parameters that act as arguments to the table-valued
8516 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
8517 ** is available and bit 1 is set if "wholeline" is available.
8519 static int completionBestIndex(
8521 sqlite3_index_info *pIdxInfo
8523 int i; /* Loop over constraints */
8524 int idxNum = 0; /* The query plan bitmask */
8525 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
8526 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
8527 int nArg = 0; /* Number of arguments that completeFilter() expects */
8528 const struct sqlite3_index_constraint *pConstraint;
8530 (void)(tab); /* Unused parameter */
8531 pConstraint = pIdxInfo->aConstraint;
8532 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
8533 if( pConstraint->usable==0 ) continue;
8534 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
8535 switch( pConstraint->iColumn ){
8536 case COMPLETION_COLUMN_PREFIX:
8540 case COMPLETION_COLUMN_WHOLELINE:
8547 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
8548 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
8550 if( wholelineIdx>=0 ){
8551 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
8552 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
8554 pIdxInfo->idxNum = idxNum;
8555 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
8556 pIdxInfo->estimatedRows = 500 - 100*nArg;
8561 ** This following structure defines all the methods for the
8562 ** completion virtual table.
8564 static sqlite3_module completionModule = {
8567 completionConnect, /* xConnect */
8568 completionBestIndex, /* xBestIndex */
8569 completionDisconnect, /* xDisconnect */
8571 completionOpen, /* xOpen - open a cursor */
8572 completionClose, /* xClose - close a cursor */
8573 completionFilter, /* xFilter - configure scan constraints */
8574 completionNext, /* xNext - advance a cursor */
8575 completionEof, /* xEof - check for end of scan */
8576 completionColumn, /* xColumn - read data */
8577 completionRowid, /* xRowid - read data */
8583 0, /* xFindMethod */
8587 0, /* xRollbackTo */
8588 0, /* xShadowName */
8592 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8594 int sqlite3CompletionVtabInit(sqlite3 *db){
8596 #ifndef SQLITE_OMIT_VIRTUALTABLE
8597 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
8605 int sqlite3_completion_init(
8608 const sqlite3_api_routines *pApi
8611 SQLITE_EXTENSION_INIT2(pApi);
8612 (void)(pzErrMsg); /* Unused parameter */
8613 #ifndef SQLITE_OMIT_VIRTUALTABLE
8614 rc = sqlite3CompletionVtabInit(db);
8619 /************************* End ../ext/misc/completion.c ********************/
8620 /************************* Begin ../ext/misc/appendvfs.c ******************/
8624 ** The author disclaims copyright to this source code. In place of
8625 ** a legal notice, here is a blessing:
8627 ** May you do good and not evil.
8628 ** May you find forgiveness for yourself and forgive others.
8629 ** May you share freely, never taking more than you give.
8631 ******************************************************************************
8633 ** This file implements a VFS shim that allows an SQLite database to be
8634 ** appended onto the end of some other file, such as an executable.
8636 ** A special record must appear at the end of the file that identifies the
8637 ** file as an appended database and provides the offset to the first page
8638 ** of the exposed content. (Or, it is the length of the content prefix.)
8639 ** For best performance page 1 should be located at a disk page boundary,
8640 ** though that is not required.
8642 ** When opening a database using this VFS, the connection might treat
8643 ** the file as an ordinary SQLite database, or it might treat it as a
8644 ** database appended onto some other file. The decision is made by
8645 ** applying the following rules in order:
8647 ** (1) An empty file is an ordinary database.
8649 ** (2) If the file ends with the appendvfs trailer string
8650 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
8652 ** (3) If the file begins with the standard SQLite prefix string
8653 ** "SQLite format 3", that file is an ordinary database.
8655 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
8656 ** set, then a new database is appended to the already existing file.
8658 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
8660 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
8661 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
8662 ** This VFS will not read or write past the 1GiB mark. This restriction
8663 ** might be lifted in future versions. For now, if you need a larger
8664 ** database, then keep it in a separate file.
8666 ** If the file being opened is a plain database (not an appended one), then
8667 ** this shim is a pass-through into the default underlying VFS. (rule 3)
8669 /* #include "sqlite3ext.h" */
8670 SQLITE_EXTENSION_INIT1
8674 /* The append mark at the end of the database is:
8676 ** Start-Of-SQLite3-NNNNNNNN
8677 ** 123456789 123456789 12345
8679 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
8680 ** the offset to page 1, and also the length of the prefix content.
8682 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
8683 #define APND_MARK_PREFIX_SZ 17
8684 #define APND_MARK_FOS_SZ 8
8685 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
8688 ** Maximum size of the combined prefix + database + append-mark. This
8689 ** must be less than 0x40000000 to avoid locking issues on Windows.
8691 #define APND_MAX_SIZE (0x40000000)
8694 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
8696 #ifndef APND_ROUNDUP
8697 #define APND_ROUNDUP 4096
8699 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
8700 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
8703 ** Forward declaration of objects used by this utility
8705 typedef struct sqlite3_vfs ApndVfs;
8706 typedef struct ApndFile ApndFile;
8708 /* Access to a lower-level VFS that (might) implement dynamic loading,
8709 ** access to randomness, etc.
8711 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
8712 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
8714 /* An open appendvfs file
8716 ** An instance of this structure describes the appended database file.
8717 ** A separate sqlite3_file object is always appended. The appended
8718 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
8719 ** the entire file, including the prefix, the database, and the
8722 ** The structure of an AppendVFS database is like this:
8724 ** +-------------+---------+----------+-------------+
8725 ** | prefix-file | padding | database | append-mark |
8726 ** +-------------+---------+----------+-------------+
8732 ** "prefix file" - file onto which the database has been appended.
8733 ** "padding" - zero or more bytes inserted so that "database"
8734 ** starts on an APND_ROUNDUP boundary
8735 ** "database" - The SQLite database file
8736 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
8737 ** the offset from the start of prefix-file to the start
8740 ** The size of the database is iMark - iPgOne.
8742 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
8743 ** of iPgOne stored as a big-ending 64-bit integer.
8745 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
8746 ** Or, iMark is -1 to indicate that it has not yet been written.
8749 sqlite3_file base; /* Subclass. MUST BE FIRST! */
8750 sqlite3_int64 iPgOne; /* Offset to the start of the database */
8751 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
8752 /* Always followed by another sqlite3_file that describes the whole file */
8756 ** Methods for ApndFile
8758 static int apndClose(sqlite3_file*);
8759 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
8760 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
8761 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
8762 static int apndSync(sqlite3_file*, int flags);
8763 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
8764 static int apndLock(sqlite3_file*, int);
8765 static int apndUnlock(sqlite3_file*, int);
8766 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
8767 static int apndFileControl(sqlite3_file*, int op, void *pArg);
8768 static int apndSectorSize(sqlite3_file*);
8769 static int apndDeviceCharacteristics(sqlite3_file*);
8770 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
8771 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
8772 static void apndShmBarrier(sqlite3_file*);
8773 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
8774 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
8775 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
8778 ** Methods for ApndVfs
8780 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
8781 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
8782 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
8783 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
8784 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
8785 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
8786 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
8787 static void apndDlClose(sqlite3_vfs*, void*);
8788 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
8789 static int apndSleep(sqlite3_vfs*, int microseconds);
8790 static int apndCurrentTime(sqlite3_vfs*, double*);
8791 static int apndGetLastError(sqlite3_vfs*, int, char *);
8792 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
8793 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
8794 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
8795 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
8797 static sqlite3_vfs apnd_vfs = {
8798 3, /* iVersion (set when registered) */
8799 0, /* szOsFile (set when registered) */
8800 1024, /* mxPathname */
8802 "apndvfs", /* zName */
8803 0, /* pAppData (set when registered) */
8804 apndOpen, /* xOpen */
8805 apndDelete, /* xDelete */
8806 apndAccess, /* xAccess */
8807 apndFullPathname, /* xFullPathname */
8808 apndDlOpen, /* xDlOpen */
8809 apndDlError, /* xDlError */
8810 apndDlSym, /* xDlSym */
8811 apndDlClose, /* xDlClose */
8812 apndRandomness, /* xRandomness */
8813 apndSleep, /* xSleep */
8814 apndCurrentTime, /* xCurrentTime */
8815 apndGetLastError, /* xGetLastError */
8816 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
8817 apndSetSystemCall, /* xSetSystemCall */
8818 apndGetSystemCall, /* xGetSystemCall */
8819 apndNextSystemCall /* xNextSystemCall */
8822 static const sqlite3_io_methods apnd_io_methods = {
8824 apndClose, /* xClose */
8825 apndRead, /* xRead */
8826 apndWrite, /* xWrite */
8827 apndTruncate, /* xTruncate */
8828 apndSync, /* xSync */
8829 apndFileSize, /* xFileSize */
8830 apndLock, /* xLock */
8831 apndUnlock, /* xUnlock */
8832 apndCheckReservedLock, /* xCheckReservedLock */
8833 apndFileControl, /* xFileControl */
8834 apndSectorSize, /* xSectorSize */
8835 apndDeviceCharacteristics, /* xDeviceCharacteristics */
8836 apndShmMap, /* xShmMap */
8837 apndShmLock, /* xShmLock */
8838 apndShmBarrier, /* xShmBarrier */
8839 apndShmUnmap, /* xShmUnmap */
8840 apndFetch, /* xFetch */
8841 apndUnfetch /* xUnfetch */
8845 ** Close an apnd-file.
8847 static int apndClose(sqlite3_file *pFile){
8848 pFile = ORIGFILE(pFile);
8849 return pFile->pMethods->xClose(pFile);
8853 ** Read data from an apnd-file.
8855 static int apndRead(
8856 sqlite3_file *pFile,
8861 ApndFile *paf = (ApndFile *)pFile;
8862 pFile = ORIGFILE(pFile);
8863 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
8867 ** Add the append-mark onto what should become the end of the file.
8868 * If and only if this succeeds, internal ApndFile.iMark is updated.
8869 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
8871 static int apndWriteMark(
8873 sqlite3_file *pFile,
8874 sqlite_int64 iWriteEnd
8876 sqlite_int64 iPgOne = paf->iPgOne;
8877 unsigned char a[APND_MARK_SIZE];
8878 int i = APND_MARK_FOS_SZ;
8880 assert(pFile == ORIGFILE(paf));
8881 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
8883 a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
8886 iWriteEnd += paf->iPgOne;
8887 if( SQLITE_OK==(rc = pFile->pMethods->xWrite
8888 (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
8889 paf->iMark = iWriteEnd;
8895 ** Write data to an apnd-file.
8897 static int apndWrite(
8898 sqlite3_file *pFile,
8903 ApndFile *paf = (ApndFile *)pFile;
8904 sqlite_int64 iWriteEnd = iOfst + iAmt;
8905 if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
8906 pFile = ORIGFILE(pFile);
8907 /* If append-mark is absent or will be overwritten, write it. */
8908 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
8909 int rc = apndWriteMark(paf, pFile, iWriteEnd);
8910 if( SQLITE_OK!=rc ) return rc;
8912 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
8916 ** Truncate an apnd-file.
8918 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
8919 ApndFile *paf = (ApndFile *)pFile;
8920 pFile = ORIGFILE(pFile);
8921 /* The append mark goes out first so truncate failure does not lose it. */
8922 if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
8923 /* Truncate underlying file just past append mark */
8924 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
8928 ** Sync an apnd-file.
8930 static int apndSync(sqlite3_file *pFile, int flags){
8931 pFile = ORIGFILE(pFile);
8932 return pFile->pMethods->xSync(pFile, flags);
8936 ** Return the current file-size of an apnd-file.
8937 ** If the append mark is not yet there, the file-size is 0.
8939 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
8940 ApndFile *paf = (ApndFile *)pFile;
8941 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
8946 ** Lock an apnd-file.
8948 static int apndLock(sqlite3_file *pFile, int eLock){
8949 pFile = ORIGFILE(pFile);
8950 return pFile->pMethods->xLock(pFile, eLock);
8954 ** Unlock an apnd-file.
8956 static int apndUnlock(sqlite3_file *pFile, int eLock){
8957 pFile = ORIGFILE(pFile);
8958 return pFile->pMethods->xUnlock(pFile, eLock);
8962 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
8964 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
8965 pFile = ORIGFILE(pFile);
8966 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
8970 ** File control method. For custom operations on an apnd-file.
8972 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
8973 ApndFile *paf = (ApndFile *)pFile;
8975 pFile = ORIGFILE(pFile);
8976 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
8977 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
8978 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
8979 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
8985 ** Return the sector-size in bytes for an apnd-file.
8987 static int apndSectorSize(sqlite3_file *pFile){
8988 pFile = ORIGFILE(pFile);
8989 return pFile->pMethods->xSectorSize(pFile);
8993 ** Return the device characteristic flags supported by an apnd-file.
8995 static int apndDeviceCharacteristics(sqlite3_file *pFile){
8996 pFile = ORIGFILE(pFile);
8997 return pFile->pMethods->xDeviceCharacteristics(pFile);
9000 /* Create a shared memory file mapping */
9001 static int apndShmMap(
9002 sqlite3_file *pFile,
9008 pFile = ORIGFILE(pFile);
9009 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
9012 /* Perform locking on a shared-memory segment */
9013 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
9014 pFile = ORIGFILE(pFile);
9015 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
9018 /* Memory barrier operation on shared memory */
9019 static void apndShmBarrier(sqlite3_file *pFile){
9020 pFile = ORIGFILE(pFile);
9021 pFile->pMethods->xShmBarrier(pFile);
9024 /* Unmap a shared memory segment */
9025 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
9026 pFile = ORIGFILE(pFile);
9027 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
9030 /* Fetch a page of a memory-mapped file */
9031 static int apndFetch(
9032 sqlite3_file *pFile,
9033 sqlite3_int64 iOfst,
9037 ApndFile *p = (ApndFile *)pFile;
9038 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
9039 return SQLITE_IOERR; /* Cannot read what is not yet there. */
9041 pFile = ORIGFILE(pFile);
9042 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
9045 /* Release a memory-mapped page */
9046 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
9047 ApndFile *p = (ApndFile *)pFile;
9048 pFile = ORIGFILE(pFile);
9049 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
9053 ** Try to read the append-mark off the end of a file. Return the
9054 ** start of the appended database if the append-mark is present.
9055 ** If there is no valid append-mark, return -1;
9057 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
9058 ** indicates that the appended database contains at least one page. The
9059 ** start-of-database value must be a multiple of 512.
9061 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
9063 sqlite3_int64 iMark;
9064 int msbs = 8 * (APND_MARK_FOS_SZ-1);
9065 unsigned char a[APND_MARK_SIZE];
9067 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
9068 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
9070 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
9071 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
9074 iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
9076 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
9077 if( iMark & 0x1ff ) return -1;
9081 static const char apvfsSqliteHdr[] = "SQLite format 3";
9083 ** Check to see if the file is an appendvfs SQLite database file.
9084 ** Return true iff it is such. Parameter sz is the file's size.
9086 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
9089 sqlite3_int64 iMark = apndReadMark(sz, pFile);
9091 /* If file has the correct end-marker, the expected odd size, and the
9092 ** SQLite DB type marker where the end-marker puts it, then it
9093 ** is an appendvfs database.
9095 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
9097 && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
9098 && (sz & 0x1ff) == APND_MARK_SIZE
9099 && sz>=512+APND_MARK_SIZE
9101 return 1; /* It's an appendvfs database */
9108 ** Check to see if the file is an ordinary SQLite database file.
9109 ** Return true iff so. Parameter sz is the file's size.
9111 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
9113 if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
9114 || (sz & 0x1ff) != 0
9115 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
9116 || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
9125 ** Open an apnd file handle.
9127 static int apndOpen(
9128 sqlite3_vfs *pApndVfs,
9130 sqlite3_file *pFile,
9134 ApndFile *pApndFile = (ApndFile*)pFile;
9135 sqlite3_file *pBaseFile = ORIGFILE(pFile);
9136 sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
9138 sqlite3_int64 sz = 0;
9139 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
9140 /* The appendvfs is not to be used for transient or temporary databases.
9141 ** Just use the base VFS open to initialize the given file object and
9142 ** open the underlying file. (Appendvfs is then unused for this file.)
9144 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
9146 memset(pApndFile, 0, sizeof(ApndFile));
9147 pFile->pMethods = &apnd_io_methods;
9148 pApndFile->iMark = -1; /* Append mark not yet written */
9150 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
9151 if( rc==SQLITE_OK ){
9152 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
9154 pBaseFile->pMethods->xClose(pBaseFile);
9158 pFile->pMethods = 0;
9161 if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
9162 /* The file being opened appears to be just an ordinary DB. Copy
9163 ** the base dispatch-table so this instance mimics the base VFS.
9165 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
9168 pApndFile->iPgOne = apndReadMark(sz, pFile);
9169 if( pApndFile->iPgOne>=0 ){
9170 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
9173 if( (flags & SQLITE_OPEN_CREATE)==0 ){
9174 pBaseFile->pMethods->xClose(pBaseFile);
9175 rc = SQLITE_CANTOPEN;
9176 pFile->pMethods = 0;
9178 /* Round newly added appendvfs location to #define'd page boundary.
9179 ** Note that nothing has yet been written to the underlying file.
9180 ** The append mark will be written along with first content write.
9181 ** Until then, paf->iMark value indicates it is not yet written.
9183 pApndFile->iPgOne = APND_START_ROUNDUP(sz);
9189 ** Delete an apnd file.
9190 ** For an appendvfs, this could mean delete the appendvfs portion,
9191 ** leaving the appendee as it was before it gained an appendvfs.
9192 ** For now, this code deletes the underlying file too.
9194 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
9195 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
9199 ** All other VFS methods are pass-thrus.
9201 static int apndAccess(
9207 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
9209 static int apndFullPathname(
9215 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
9217 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
9218 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
9220 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
9221 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
9223 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
9224 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
9226 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
9227 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
9229 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
9230 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
9232 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
9233 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
9235 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
9236 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
9238 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
9239 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
9241 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
9242 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
9244 static int apndSetSystemCall(
9247 sqlite3_syscall_ptr pCall
9249 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
9251 static sqlite3_syscall_ptr apndGetSystemCall(
9255 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
9257 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
9258 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
9266 ** This routine is called when the extension is loaded.
9267 ** Register the new VFS.
9269 int sqlite3_appendvfs_init(
9272 const sqlite3_api_routines *pApi
9276 SQLITE_EXTENSION_INIT2(pApi);
9279 pOrig = sqlite3_vfs_find(0);
9280 if( pOrig==0 ) return SQLITE_ERROR;
9281 apnd_vfs.iVersion = pOrig->iVersion;
9282 apnd_vfs.pAppData = pOrig;
9283 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
9284 rc = sqlite3_vfs_register(&apnd_vfs, 0);
9285 #ifdef APPENDVFS_TEST
9286 if( rc==SQLITE_OK ){
9287 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
9290 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
9294 /************************* End ../ext/misc/appendvfs.c ********************/
9296 #ifdef SQLITE_HAVE_ZLIB
9297 /************************* Begin ../ext/misc/zipfile.c ******************/
9301 ** The author disclaims copyright to this source code. In place of
9302 ** a legal notice, here is a blessing:
9304 ** May you do good and not evil.
9305 ** May you find forgiveness for yourself and forgive others.
9306 ** May you share freely, never taking more than you give.
9308 ******************************************************************************
9310 ** This file implements a virtual table for reading and writing ZIP archive
9315 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
9317 ** Current limitations:
9319 ** * No support for encryption
9320 ** * No support for ZIP archives spanning multiple files
9321 ** * No support for zip64 extensions
9322 ** * Only the "inflate/deflate" (zlib) compression method is supported
9324 /* #include "sqlite3ext.h" */
9325 SQLITE_EXTENSION_INIT1
9333 #ifndef SQLITE_OMIT_VIRTUALTABLE
9335 #ifndef SQLITE_AMALGAMATION
9338 # ifdef HAVE_UINT32_T
9339 # define UINT32_TYPE uint32_t
9341 # define UINT32_TYPE unsigned int
9345 # ifdef HAVE_UINT16_T
9346 # define UINT16_TYPE uint16_t
9348 # define UINT16_TYPE unsigned short int
9351 /* typedef sqlite3_int64 i64; */
9352 /* typedef unsigned char u8; */
9353 /* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
9354 /* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
9355 #define MIN(a,b) ((a)<(b) ? (a) : (b))
9357 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
9358 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
9360 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
9361 # define ALWAYS(X) (1)
9362 # define NEVER(X) (0)
9363 #elif !defined(NDEBUG)
9364 # define ALWAYS(X) ((X)?1:(assert(0),0))
9365 # define NEVER(X) ((X)?(assert(0),1):0)
9367 # define ALWAYS(X) (X)
9368 # define NEVER(X) (X)
9371 #endif /* SQLITE_AMALGAMATION */
9374 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
9376 ** In some ways it would be better to obtain these values from system
9377 ** header files. But, the dependency is undesirable and (a) these
9378 ** have been stable for decades, (b) the values are part of POSIX and
9379 ** are also made explicit in [man stat], and (c) are part of the
9380 ** file format for zip archives.
9383 # define S_IFDIR 0040000
9386 # define S_IFREG 0100000
9389 # define S_IFLNK 0120000
9392 static const char ZIPFILE_SCHEMA[] =
9394 "name PRIMARY KEY," /* 0: Name of file in zip archive */
9395 "mode," /* 1: POSIX mode for file */
9396 "mtime," /* 2: Last modification time (secs since 1970)*/
9397 "sz," /* 3: Size of object */
9398 "rawdata," /* 4: Raw data */
9399 "data," /* 5: Uncompressed data */
9400 "method," /* 6: Compression method (integer) */
9401 "z HIDDEN" /* 7: Name of zip file */
9404 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
9405 #define ZIPFILE_BUFFER_SIZE (64*1024)
9409 ** Magic numbers used to read and write zip files.
9411 ** ZIPFILE_NEWENTRY_MADEBY:
9412 ** Use this value for the "version-made-by" field in new zip file
9413 ** entries. The upper byte indicates "unix", and the lower byte
9414 ** indicates that the zip file matches pkzip specification 3.0.
9415 ** This is what info-zip seems to do.
9417 ** ZIPFILE_NEWENTRY_REQUIRED:
9418 ** Value for "version-required-to-extract" field of new entries.
9419 ** Version 2.0 is required to support folders and deflate compression.
9421 ** ZIPFILE_NEWENTRY_FLAGS:
9422 ** Value for "general-purpose-bit-flags" field of new entries. Bit
9423 ** 11 means "utf-8 filename and comment".
9425 ** ZIPFILE_SIGNATURE_CDS:
9426 ** First 4 bytes of a valid CDS record.
9428 ** ZIPFILE_SIGNATURE_LFH:
9429 ** First 4 bytes of a valid LFH record.
9431 ** ZIPFILE_SIGNATURE_EOCD
9432 ** First 4 bytes of a valid EOCD record.
9434 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
9435 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
9436 #define ZIPFILE_NEWENTRY_REQUIRED 20
9437 #define ZIPFILE_NEWENTRY_FLAGS 0x800
9438 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
9439 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
9440 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
9443 ** The sizes of the fixed-size part of each of the three main data
9444 ** structures in a zip archive.
9446 #define ZIPFILE_LFH_FIXED_SZ 30
9447 #define ZIPFILE_EOCD_FIXED_SZ 22
9448 #define ZIPFILE_CDS_FIXED_SZ 46
9451 *** 4.3.16 End of central directory record:
9453 *** end of central dir signature 4 bytes (0x06054b50)
9454 *** number of this disk 2 bytes
9455 *** number of the disk with the
9456 *** start of the central directory 2 bytes
9457 *** total number of entries in the
9458 *** central directory on this disk 2 bytes
9459 *** total number of entries in
9460 *** the central directory 2 bytes
9461 *** size of the central directory 4 bytes
9462 *** offset of start of central
9463 *** directory with respect to
9464 *** the starting disk number 4 bytes
9465 *** .ZIP file comment length 2 bytes
9466 *** .ZIP file comment (variable size)
9468 typedef struct ZipfileEOCD ZipfileEOCD;
9469 struct ZipfileEOCD {
9479 *** 4.3.12 Central directory structure:
9483 *** central file header signature 4 bytes (0x02014b50)
9484 *** version made by 2 bytes
9485 *** version needed to extract 2 bytes
9486 *** general purpose bit flag 2 bytes
9487 *** compression method 2 bytes
9488 *** last mod file time 2 bytes
9489 *** last mod file date 2 bytes
9491 *** compressed size 4 bytes
9492 *** uncompressed size 4 bytes
9493 *** file name length 2 bytes
9494 *** extra field length 2 bytes
9495 *** file comment length 2 bytes
9496 *** disk number start 2 bytes
9497 *** internal file attributes 2 bytes
9498 *** external file attributes 4 bytes
9499 *** relative offset of local header 4 bytes
9501 typedef struct ZipfileCDS ZipfileCDS;
9504 u16 iVersionExtract;
9519 char *zFile; /* Filename (sqlite3_malloc()) */
9523 *** 4.3.7 Local file header:
9525 *** local file header signature 4 bytes (0x04034b50)
9526 *** version needed to extract 2 bytes
9527 *** general purpose bit flag 2 bytes
9528 *** compression method 2 bytes
9529 *** last mod file time 2 bytes
9530 *** last mod file date 2 bytes
9532 *** compressed size 4 bytes
9533 *** uncompressed size 4 bytes
9534 *** file name length 2 bytes
9535 *** extra field length 2 bytes
9538 typedef struct ZipfileLFH ZipfileLFH;
9540 u16 iVersionExtract;
9552 typedef struct ZipfileEntry ZipfileEntry;
9553 struct ZipfileEntry {
9554 ZipfileCDS cds; /* Parsed CDS record */
9555 u32 mUnixTime; /* Modification time, in UNIX format */
9556 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
9557 i64 iDataOff; /* Offset to data in file (if aData==0) */
9558 u8 *aData; /* cds.szCompressed bytes of compressed data */
9559 ZipfileEntry *pNext; /* Next element in in-memory CDS */
9563 ** Cursor type for zipfile tables.
9565 typedef struct ZipfileCsr ZipfileCsr;
9567 sqlite3_vtab_cursor base; /* Base class - must be first */
9568 i64 iId; /* Cursor ID */
9569 u8 bEof; /* True when at EOF */
9570 u8 bNoop; /* If next xNext() call is no-op */
9572 /* Used outside of write transactions */
9573 FILE *pFile; /* Zip file */
9574 i64 iNextOff; /* Offset of next record in central directory */
9575 ZipfileEOCD eocd; /* Parse of central directory record */
9577 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
9578 ZipfileEntry *pCurrent; /* Current entry */
9579 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
9582 typedef struct ZipfileTab ZipfileTab;
9584 sqlite3_vtab base; /* Base class - must be first */
9585 char *zFile; /* Zip file this table accesses (may be NULL) */
9586 sqlite3 *db; /* Host database connection */
9587 u8 *aBuffer; /* Temporary buffer used for various tasks */
9589 ZipfileCsr *pCsrList; /* List of cursors */
9592 /* The following are used by write transactions only */
9593 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
9594 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
9595 FILE *pWriteFd; /* File handle open on zip archive */
9596 i64 szCurrent; /* Current size of zip archive */
9597 i64 szOrig; /* Size of archive at start of transaction */
9601 ** Set the error message contained in context ctx to the results of
9602 ** vprintf(zFmt, ...).
9604 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
9608 zMsg = sqlite3_vmprintf(zFmt, ap);
9609 sqlite3_result_error(ctx, zMsg, -1);
9615 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
9616 ** is not quoted, do nothing.
9618 static void zipfileDequote(char *zIn){
9620 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
9623 if( q=='[' ) q = ']';
9624 while( ALWAYS(zIn[iIn]) ){
9625 char c = zIn[iIn++];
9626 if( c==q && zIn[iIn++]!=q ) break;
9634 ** Construct a new ZipfileTab virtual table object.
9636 ** argv[0] -> module name ("zipfile")
9637 ** argv[1] -> database name
9638 ** argv[2] -> table name
9639 ** argv[...] -> "column name" and other module argument fields.
9641 static int zipfileConnect(
9644 int argc, const char *const*argv,
9645 sqlite3_vtab **ppVtab,
9648 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
9650 const char *zFile = 0;
9651 ZipfileTab *pNew = 0;
9655 /* If the table name is not "zipfile", require that the argument be
9656 ** specified. This stops zipfile tables from being created as:
9658 ** CREATE VIRTUAL TABLE zzz USING zipfile();
9660 ** It does not prevent:
9662 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
9664 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
9665 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
9666 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
9667 return SQLITE_ERROR;
9672 nFile = (int)strlen(zFile)+1;
9675 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
9676 if( rc==SQLITE_OK ){
9677 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
9678 if( pNew==0 ) return SQLITE_NOMEM;
9679 memset(pNew, 0, nByte+nFile);
9681 pNew->aBuffer = (u8*)&pNew[1];
9683 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
9684 memcpy(pNew->zFile, zFile, nFile);
9685 zipfileDequote(pNew->zFile);
9688 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
9689 *ppVtab = (sqlite3_vtab*)pNew;
9694 ** Free the ZipfileEntry structure indicated by the only argument.
9696 static void zipfileEntryFree(ZipfileEntry *p){
9698 sqlite3_free(p->cds.zFile);
9704 ** Release resources that should be freed at the end of a write
9707 static void zipfileCleanupTransaction(ZipfileTab *pTab){
9708 ZipfileEntry *pEntry;
9709 ZipfileEntry *pNext;
9711 if( pTab->pWriteFd ){
9712 fclose(pTab->pWriteFd);
9715 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
9716 pNext = pEntry->pNext;
9717 zipfileEntryFree(pEntry);
9719 pTab->pFirstEntry = 0;
9720 pTab->pLastEntry = 0;
9721 pTab->szCurrent = 0;
9726 ** This method is the destructor for zipfile vtab objects.
9728 static int zipfileDisconnect(sqlite3_vtab *pVtab){
9729 zipfileCleanupTransaction((ZipfileTab*)pVtab);
9730 sqlite3_free(pVtab);
9735 ** Constructor for a new ZipfileCsr object.
9737 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
9738 ZipfileTab *pTab = (ZipfileTab*)p;
9740 pCsr = sqlite3_malloc(sizeof(*pCsr));
9741 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
9743 return SQLITE_NOMEM;
9745 memset(pCsr, 0, sizeof(*pCsr));
9746 pCsr->iId = ++pTab->iNextCsrid;
9747 pCsr->pCsrNext = pTab->pCsrList;
9748 pTab->pCsrList = pCsr;
9753 ** Reset a cursor back to the state it was in when first returned
9754 ** by zipfileOpen().
9756 static void zipfileResetCursor(ZipfileCsr *pCsr){
9758 ZipfileEntry *pNext;
9762 fclose(pCsr->pFile);
9764 zipfileEntryFree(pCsr->pCurrent);
9768 for(p=pCsr->pFreeEntry; p; p=pNext){
9770 zipfileEntryFree(p);
9775 ** Destructor for an ZipfileCsr.
9777 static int zipfileClose(sqlite3_vtab_cursor *cur){
9778 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
9779 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
9781 zipfileResetCursor(pCsr);
9783 /* Remove this cursor from the ZipfileTab.pCsrList list. */
9784 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
9785 *pp = pCsr->pCsrNext;
9792 ** Set the error message for the virtual table associated with cursor
9793 ** pCsr to the results of vprintf(zFmt, ...).
9795 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
9798 sqlite3_free(pTab->base.zErrMsg);
9799 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
9802 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
9805 sqlite3_free(pCsr->base.pVtab->zErrMsg);
9806 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
9811 ** Read nRead bytes of data from offset iOff of file pFile into buffer
9812 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
9815 ** If an error does occur, output variable (*pzErrmsg) may be set to point
9816 ** to an English language error message. It is the responsibility of the
9817 ** caller to eventually free this buffer using
9820 static int zipfileReadData(
9821 FILE *pFile, /* Read from this file */
9822 u8 *aRead, /* Read into this buffer */
9823 int nRead, /* Number of bytes to read */
9824 i64 iOff, /* Offset to read from */
9825 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
9828 fseek(pFile, (long)iOff, SEEK_SET);
9829 n = fread(aRead, 1, nRead, pFile);
9830 if( (int)n!=nRead ){
9831 *pzErrmsg = sqlite3_mprintf("error in fread()");
9832 return SQLITE_ERROR;
9837 static int zipfileAppendData(
9844 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
9845 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
9846 if( (int)n!=nWrite ){
9847 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
9848 return SQLITE_ERROR;
9850 pTab->szCurrent += nWrite;
9856 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
9858 static u16 zipfileGetU16(const u8 *aBuf){
9859 return (aBuf[1] << 8) + aBuf[0];
9863 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
9865 static u32 zipfileGetU32(const u8 *aBuf){
9866 if( aBuf==0 ) return 0;
9867 return ((u32)(aBuf[3]) << 24)
9868 + ((u32)(aBuf[2]) << 16)
9869 + ((u32)(aBuf[1]) << 8)
9870 + ((u32)(aBuf[0]) << 0);
9874 ** Write a 16-bit little endiate integer into buffer aBuf.
9876 static void zipfilePutU16(u8 *aBuf, u16 val){
9877 aBuf[0] = val & 0xFF;
9878 aBuf[1] = (val>>8) & 0xFF;
9882 ** Write a 32-bit little endiate integer into buffer aBuf.
9884 static void zipfilePutU32(u8 *aBuf, u32 val){
9885 aBuf[0] = val & 0xFF;
9886 aBuf[1] = (val>>8) & 0xFF;
9887 aBuf[2] = (val>>16) & 0xFF;
9888 aBuf[3] = (val>>24) & 0xFF;
9891 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
9892 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
9894 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
9895 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
9898 ** Magic numbers used to read CDS records.
9900 #define ZIPFILE_CDS_NFILE_OFF 28
9901 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
9904 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
9905 ** if the record is not well-formed, or SQLITE_OK otherwise.
9907 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
9909 u32 sig = zipfileRead32(aRead);
9911 if( sig!=ZIPFILE_SIGNATURE_CDS ){
9914 pCDS->iVersionMadeBy = zipfileRead16(aRead);
9915 pCDS->iVersionExtract = zipfileRead16(aRead);
9916 pCDS->flags = zipfileRead16(aRead);
9917 pCDS->iCompression = zipfileRead16(aRead);
9918 pCDS->mTime = zipfileRead16(aRead);
9919 pCDS->mDate = zipfileRead16(aRead);
9920 pCDS->crc32 = zipfileRead32(aRead);
9921 pCDS->szCompressed = zipfileRead32(aRead);
9922 pCDS->szUncompressed = zipfileRead32(aRead);
9923 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
9924 pCDS->nFile = zipfileRead16(aRead);
9925 pCDS->nExtra = zipfileRead16(aRead);
9926 pCDS->nComment = zipfileRead16(aRead);
9927 pCDS->iDiskStart = zipfileRead16(aRead);
9928 pCDS->iInternalAttr = zipfileRead16(aRead);
9929 pCDS->iExternalAttr = zipfileRead32(aRead);
9930 pCDS->iOffset = zipfileRead32(aRead);
9931 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
9938 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
9939 ** if the record is not well-formed, or SQLITE_OK otherwise.
9941 static int zipfileReadLFH(
9945 u8 *aRead = aBuffer;
9948 u32 sig = zipfileRead32(aRead);
9949 if( sig!=ZIPFILE_SIGNATURE_LFH ){
9952 pLFH->iVersionExtract = zipfileRead16(aRead);
9953 pLFH->flags = zipfileRead16(aRead);
9954 pLFH->iCompression = zipfileRead16(aRead);
9955 pLFH->mTime = zipfileRead16(aRead);
9956 pLFH->mDate = zipfileRead16(aRead);
9957 pLFH->crc32 = zipfileRead32(aRead);
9958 pLFH->szCompressed = zipfileRead32(aRead);
9959 pLFH->szUncompressed = zipfileRead32(aRead);
9960 pLFH->nFile = zipfileRead16(aRead);
9961 pLFH->nExtra = zipfileRead16(aRead);
9968 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
9969 ** Scan through this buffer to find an "extra-timestamp" field. If one
9970 ** exists, extract the 32-bit modification-timestamp from it and store
9971 ** the value in output parameter *pmTime.
9973 ** Zero is returned if no extra-timestamp record could be found (and so
9974 ** *pmTime is left unchanged), or non-zero otherwise.
9976 ** The general format of an extra field is:
9978 ** Header ID 2 bytes
9979 ** Data Size 2 bytes
9982 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
9985 u8 *pEnd = &aExtra[nExtra];
9988 u16 id = zipfileRead16(p);
9989 u16 nByte = zipfileRead16(p);
9992 case ZIPFILE_EXTRA_TIMESTAMP: {
9994 if( b & 0x01 ){ /* 0x01 -> modtime is present */
9995 *pmTime = zipfileGetU32(&p[1]);
10008 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
10009 ** fields of the CDS structure passed as the only argument to a 32-bit
10010 ** UNIX seconds-since-the-epoch timestamp. Return the result.
10012 ** "Standard" MS-DOS time format:
10014 ** File modification time:
10015 ** Bits 00-04: seconds divided by 2
10016 ** Bits 05-10: minute
10017 ** Bits 11-15: hour
10018 ** File modification date:
10020 ** Bits 05-08: month (1-12)
10021 ** Bits 09-15: years from 1980
10023 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
10025 static u32 zipfileMtime(ZipfileCDS *pCDS){
10026 int Y,M,D,X1,X2,A,B,sec,min,hr;
10028 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
10029 M = ((pCDS->mDate >> 5) & 0x0F);
10030 D = (pCDS->mDate & 0x1F);
10031 sec = (pCDS->mTime & 0x1F)*2;
10032 min = (pCDS->mTime >> 5) & 0x3F;
10033 hr = (pCDS->mTime >> 11) & 0x1F;
10038 X1 = 36525*(Y+4716)/100;
10039 X2 = 306001*(M+1)/10000;
10042 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
10043 return (u32)(JDsec - (i64)24405875*(i64)8640);
10047 ** The opposite of zipfileMtime(). This function populates the mTime and
10048 ** mDate fields of the CDS structure passed as the first argument according
10049 ** to the UNIX timestamp value passed as the second.
10051 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
10052 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
10053 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
10059 A = (int)((JD - 1867216.25)/36524.25);
10060 A = (int)(JD + 1 + A - (A/4));
10062 C = (int)((B - 122.1)/365.25);
10063 D = (36525*(C&32767))/100;
10064 E = (int)((B-D)/30.6001);
10066 day = B - D - (int)(30.6001*E);
10067 mon = (E<14 ? E-1 : E-13);
10068 yr = mon>2 ? C-4716 : C-4715;
10070 hr = (mUnixTime % (24*60*60)) / (60*60);
10071 min = (mUnixTime % (60*60)) / 60;
10072 sec = (mUnixTime % 60);
10075 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
10076 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
10078 pCds->mDate = pCds->mTime = 0;
10081 assert( mUnixTime<315507600
10082 || mUnixTime==zipfileMtime(pCds)
10083 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
10084 /* || (mUnixTime % 2) */
10089 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
10090 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
10091 ** then pFile is a file-handle open on a zip file. In either case, this
10092 ** function creates a ZipfileEntry object based on the zip archive entry
10093 ** for which the CDS record is at offset iOff.
10095 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
10096 ** the new object. Otherwise, an SQLite error code is returned and the
10097 ** final value of (*ppEntry) undefined.
10099 static int zipfileGetEntry(
10100 ZipfileTab *pTab, /* Store any error message here */
10101 const u8 *aBlob, /* Pointer to in-memory file image */
10102 int nBlob, /* Size of aBlob[] in bytes */
10103 FILE *pFile, /* If aBlob==0, read from this file */
10104 i64 iOff, /* Offset of CDS record */
10105 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
10108 char **pzErr = &pTab->base.zErrMsg;
10109 int rc = SQLITE_OK;
10113 aRead = pTab->aBuffer;
10114 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
10116 aRead = (u8*)&aBlob[iOff];
10119 if( rc==SQLITE_OK ){
10120 sqlite3_int64 nAlloc;
10121 ZipfileEntry *pNew;
10123 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
10124 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
10125 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
10127 nAlloc = sizeof(ZipfileEntry) + nExtra;
10129 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
10132 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
10136 memset(pNew, 0, sizeof(ZipfileEntry));
10137 rc = zipfileReadCDS(aRead, &pNew->cds);
10138 if( rc!=SQLITE_OK ){
10139 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
10140 }else if( aBlob==0 ){
10141 rc = zipfileReadData(
10142 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
10145 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
10149 if( rc==SQLITE_OK ){
10150 u32 *pt = &pNew->mUnixTime;
10151 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
10152 pNew->aExtra = (u8*)&pNew[1];
10153 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
10154 if( pNew->cds.zFile==0 ){
10156 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
10157 pNew->mUnixTime = zipfileMtime(&pNew->cds);
10161 if( rc==SQLITE_OK ){
10162 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
10165 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
10167 aRead = (u8*)&aBlob[pNew->cds.iOffset];
10170 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
10171 if( rc==SQLITE_OK ){
10172 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
10173 pNew->iDataOff += lfh.nFile + lfh.nExtra;
10174 if( aBlob && pNew->cds.szCompressed ){
10175 pNew->aData = &pNew->aExtra[nExtra];
10176 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
10179 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
10180 (int)pNew->cds.iOffset
10185 if( rc!=SQLITE_OK ){
10186 zipfileEntryFree(pNew);
10196 ** Advance an ZipfileCsr to its next row of output.
10198 static int zipfileNext(sqlite3_vtab_cursor *cur){
10199 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10200 int rc = SQLITE_OK;
10203 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
10204 zipfileEntryFree(pCsr->pCurrent);
10205 pCsr->pCurrent = 0;
10206 if( pCsr->iNextOff>=iEof ){
10209 ZipfileEntry *p = 0;
10210 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
10211 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
10212 if( rc==SQLITE_OK ){
10213 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
10214 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
10216 pCsr->pCurrent = p;
10219 if( !pCsr->bNoop ){
10220 pCsr->pCurrent = pCsr->pCurrent->pNext;
10222 if( pCsr->pCurrent==0 ){
10231 static void zipfileFree(void *p) {
10236 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
10237 ** size is nOut bytes. This function uncompresses the data and sets the
10238 ** return value in context pCtx to the result (a blob).
10240 ** If an error occurs, an error code is left in pCtx instead.
10242 static void zipfileInflate(
10243 sqlite3_context *pCtx, /* Store result here */
10244 const u8 *aIn, /* Compressed data */
10245 int nIn, /* Size of buffer aIn[] in bytes */
10246 int nOut /* Expected output size */
10248 u8 *aRes = sqlite3_malloc(nOut);
10250 sqlite3_result_error_nomem(pCtx);
10254 memset(&str, 0, sizeof(str));
10256 str.next_in = (Byte*)aIn;
10257 str.avail_in = nIn;
10258 str.next_out = (Byte*)aRes;
10259 str.avail_out = nOut;
10261 err = inflateInit2(&str, -15);
10263 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
10265 err = inflate(&str, Z_NO_FLUSH);
10266 if( err!=Z_STREAM_END ){
10267 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
10269 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
10273 sqlite3_free(aRes);
10279 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
10280 ** compresses it and sets (*ppOut) to point to a buffer containing the
10281 ** compressed data. The caller is responsible for eventually calling
10282 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
10283 ** is set to the size of buffer (*ppOut) in bytes.
10285 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
10286 ** code is returned and an error message left in virtual-table handle
10287 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
10290 static int zipfileDeflate(
10291 const u8 *aIn, int nIn, /* Input */
10292 u8 **ppOut, int *pnOut, /* Output */
10293 char **pzErr /* OUT: Error message */
10295 int rc = SQLITE_OK;
10296 sqlite3_int64 nAlloc;
10300 memset(&str, 0, sizeof(str));
10301 str.next_in = (Bytef*)aIn;
10302 str.avail_in = nIn;
10303 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
10305 nAlloc = deflateBound(&str, nIn);
10306 aOut = (u8*)sqlite3_malloc64(nAlloc);
10311 str.next_out = aOut;
10312 str.avail_out = nAlloc;
10313 res = deflate(&str, Z_FINISH);
10314 if( res==Z_STREAM_END ){
10316 *pnOut = (int)str.total_out;
10318 sqlite3_free(aOut);
10319 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
10330 ** Return values of columns for the row at which the series_cursor
10331 ** is currently pointing.
10333 static int zipfileColumn(
10334 sqlite3_vtab_cursor *cur, /* The cursor */
10335 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
10336 int i /* Which column to return */
10338 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10339 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
10340 int rc = SQLITE_OK;
10343 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
10346 /* TODO: Whether or not the following is correct surely depends on
10347 ** the platform on which the archive was created. */
10348 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
10350 case 2: { /* mtime */
10351 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
10355 if( sqlite3_vtab_nochange(ctx)==0 ){
10356 sqlite3_result_int64(ctx, pCDS->szUncompressed);
10360 case 4: /* rawdata */
10361 if( sqlite3_vtab_nochange(ctx) ) break;
10362 case 5: { /* data */
10363 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
10364 int sz = pCDS->szCompressed;
10365 int szFinal = pCDS->szUncompressed;
10369 if( pCsr->pCurrent->aData ){
10370 aBuf = pCsr->pCurrent->aData;
10372 aBuf = aFree = sqlite3_malloc64(sz);
10376 FILE *pFile = pCsr->pFile;
10378 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
10380 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
10381 &pCsr->base.pVtab->zErrMsg
10385 if( rc==SQLITE_OK ){
10386 if( i==5 && pCDS->iCompression ){
10387 zipfileInflate(ctx, aBuf, sz, szFinal);
10389 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
10392 sqlite3_free(aFree);
10394 /* Figure out if this is a directory or a zero-sized file. Consider
10395 ** it to be a directory either if the mode suggests so, or if
10396 ** the final character in the name is '/'. */
10397 u32 mode = pCDS->iExternalAttr >> 16;
10398 if( !(mode & S_IFDIR)
10400 && pCDS->zFile[pCDS->nFile-1]!='/'
10402 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
10408 case 6: /* method */
10409 sqlite3_result_int(ctx, pCDS->iCompression);
10413 sqlite3_result_int64(ctx, pCsr->iId);
10421 ** Return TRUE if the cursor is at EOF.
10423 static int zipfileEof(sqlite3_vtab_cursor *cur){
10424 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10429 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
10430 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
10431 ** is guaranteed to be a file-handle open on a zip file.
10433 ** This function attempts to locate the EOCD record within the zip archive
10434 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
10435 ** returned if successful. Otherwise, an SQLite error code is returned and
10436 ** an English language error message may be left in virtual-table pTab.
10438 static int zipfileReadEOCD(
10439 ZipfileTab *pTab, /* Return errors here */
10440 const u8 *aBlob, /* Pointer to in-memory file image */
10441 int nBlob, /* Size of aBlob[] in bytes */
10442 FILE *pFile, /* Read from this file if aBlob==0 */
10443 ZipfileEOCD *pEOCD /* Object to populate */
10445 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
10446 int nRead; /* Bytes to read from file */
10447 int rc = SQLITE_OK;
10449 memset(pEOCD, 0, sizeof(ZipfileEOCD));
10451 i64 iOff; /* Offset to read from */
10452 i64 szFile; /* Total size of file in bytes */
10453 fseek(pFile, 0, SEEK_END);
10454 szFile = (i64)ftell(pFile);
10458 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
10459 iOff = szFile - nRead;
10460 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
10462 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
10463 aRead = (u8*)&aBlob[nBlob-nRead];
10466 if( rc==SQLITE_OK ){
10469 /* Scan backwards looking for the signature bytes */
10470 for(i=nRead-20; i>=0; i--){
10471 if( aRead[i]==0x50 && aRead[i+1]==0x4b
10472 && aRead[i+2]==0x05 && aRead[i+3]==0x06
10478 pTab->base.zErrMsg = sqlite3_mprintf(
10479 "cannot find end of central directory record"
10481 return SQLITE_ERROR;
10485 pEOCD->iDisk = zipfileRead16(aRead);
10486 pEOCD->iFirstDisk = zipfileRead16(aRead);
10487 pEOCD->nEntry = zipfileRead16(aRead);
10488 pEOCD->nEntryTotal = zipfileRead16(aRead);
10489 pEOCD->nSize = zipfileRead32(aRead);
10490 pEOCD->iOffset = zipfileRead32(aRead);
10497 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
10498 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
10499 ** to the end of the list. Otherwise, it is added to the list immediately
10500 ** before pBefore (which is guaranteed to be a part of said list).
10502 static void zipfileAddEntry(
10504 ZipfileEntry *pBefore,
10507 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
10508 assert( pNew->pNext==0 );
10510 if( pTab->pFirstEntry==0 ){
10511 pTab->pFirstEntry = pTab->pLastEntry = pNew;
10513 assert( pTab->pLastEntry->pNext==0 );
10514 pTab->pLastEntry->pNext = pNew;
10515 pTab->pLastEntry = pNew;
10519 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
10520 pNew->pNext = pBefore;
10525 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
10531 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
10532 iOff = eocd.iOffset;
10533 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
10534 ZipfileEntry *pNew = 0;
10535 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
10537 if( rc==SQLITE_OK ){
10538 zipfileAddEntry(pTab, 0, pNew);
10539 iOff += ZIPFILE_CDS_FIXED_SZ;
10540 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
10547 ** xFilter callback.
10549 static int zipfileFilter(
10550 sqlite3_vtab_cursor *cur,
10551 int idxNum, const char *idxStr,
10552 int argc, sqlite3_value **argv
10554 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
10555 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10556 const char *zFile = 0; /* Zip file to scan */
10557 int rc = SQLITE_OK; /* Return Code */
10558 int bInMemory = 0; /* True for an in-memory zipfile */
10563 zipfileResetCursor(pCsr);
10566 zFile = pTab->zFile;
10567 }else if( idxNum==0 ){
10568 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
10569 return SQLITE_ERROR;
10570 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
10571 static const u8 aEmptyBlob = 0;
10572 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
10573 int nBlob = sqlite3_value_bytes(argv[0]);
10574 assert( pTab->pFirstEntry==0 );
10576 aBlob = &aEmptyBlob;
10579 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
10580 pCsr->pFreeEntry = pTab->pFirstEntry;
10581 pTab->pFirstEntry = pTab->pLastEntry = 0;
10582 if( rc!=SQLITE_OK ) return rc;
10585 zFile = (const char*)sqlite3_value_text(argv[0]);
10588 if( 0==pTab->pWriteFd && 0==bInMemory ){
10589 pCsr->pFile = zFile ? fopen(zFile, "rb") : 0;
10590 if( pCsr->pFile==0 ){
10591 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
10594 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
10595 if( rc==SQLITE_OK ){
10596 if( pCsr->eocd.nEntry==0 ){
10599 pCsr->iNextOff = pCsr->eocd.iOffset;
10600 rc = zipfileNext(cur);
10606 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
10607 rc = zipfileNext(cur);
10614 ** xBestIndex callback.
10616 static int zipfileBestIndex(
10618 sqlite3_index_info *pIdxInfo
10625 for(i=0; i<pIdxInfo->nConstraint; i++){
10626 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
10627 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
10628 if( pCons->usable==0 ){
10630 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10634 pIdxInfo->estimatedCost = 1000.0;
10636 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
10637 pIdxInfo->aConstraintUsage[idx].omit = 1;
10638 pIdxInfo->idxNum = 1;
10639 }else if( unusable ){
10640 return SQLITE_CONSTRAINT;
10645 static ZipfileEntry *zipfileNewEntry(const char *zPath){
10646 ZipfileEntry *pNew;
10647 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
10649 memset(pNew, 0, sizeof(ZipfileEntry));
10650 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
10651 if( pNew->cds.zFile==0 ){
10652 sqlite3_free(pNew);
10659 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
10660 ZipfileCDS *pCds = &pEntry->cds;
10665 /* Write the LFH itself */
10666 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
10667 zipfileWrite16(a, pCds->iVersionExtract);
10668 zipfileWrite16(a, pCds->flags);
10669 zipfileWrite16(a, pCds->iCompression);
10670 zipfileWrite16(a, pCds->mTime);
10671 zipfileWrite16(a, pCds->mDate);
10672 zipfileWrite32(a, pCds->crc32);
10673 zipfileWrite32(a, pCds->szCompressed);
10674 zipfileWrite32(a, pCds->szUncompressed);
10675 zipfileWrite16(a, (u16)pCds->nFile);
10676 zipfileWrite16(a, pCds->nExtra);
10677 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
10679 /* Add the file name */
10680 memcpy(a, pCds->zFile, (int)pCds->nFile);
10681 a += (int)pCds->nFile;
10683 /* The "extra" data */
10684 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
10685 zipfileWrite16(a, 5);
10687 zipfileWrite32(a, pEntry->mUnixTime);
10692 static int zipfileAppendEntry(
10694 ZipfileEntry *pEntry,
10698 u8 *aBuf = pTab->aBuffer;
10702 nBuf = zipfileSerializeLFH(pEntry, aBuf);
10703 rc = zipfileAppendData(pTab, aBuf, nBuf);
10704 if( rc==SQLITE_OK ){
10705 pEntry->iDataOff = pTab->szCurrent;
10706 rc = zipfileAppendData(pTab, pData, nData);
10712 static int zipfileGetMode(
10713 sqlite3_value *pVal,
10714 int bIsDir, /* If true, default to directory */
10715 u32 *pMode, /* OUT: Mode value */
10716 char **pzErr /* OUT: Error message */
10718 const char *z = (const char*)sqlite3_value_text(pVal);
10721 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
10722 }else if( z[0]>='0' && z[0]<='9' ){
10723 mode = (unsigned int)sqlite3_value_int(pVal);
10725 const char zTemplate[11] = "-rwxrwxrwx";
10727 if( strlen(z)!=10 ) goto parse_error;
10729 case '-': mode |= S_IFREG; break;
10730 case 'd': mode |= S_IFDIR; break;
10731 case 'l': mode |= S_IFLNK; break;
10732 default: goto parse_error;
10734 for(i=1; i<10; i++){
10735 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
10736 else if( z[i]!='-' ) goto parse_error;
10739 if( ((mode & S_IFDIR)==0)==bIsDir ){
10740 /* The "mode" attribute is a directory, but data has been specified.
10741 ** Or vice-versa - no data but "mode" is a file or symlink. */
10742 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
10743 return SQLITE_CONSTRAINT;
10749 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
10750 return SQLITE_ERROR;
10754 ** Both (const char*) arguments point to nul-terminated strings. Argument
10755 ** nB is the value of strlen(zB). This function returns 0 if the strings are
10756 ** identical, ignoring any trailing '/' character in either path. */
10757 static int zipfileComparePath(const char *zA, const char *zB, int nB){
10758 int nA = (int)strlen(zA);
10759 if( nA>0 && zA[nA-1]=='/' ) nA--;
10760 if( nB>0 && zB[nB-1]=='/' ) nB--;
10761 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
10765 static int zipfileBegin(sqlite3_vtab *pVtab){
10766 ZipfileTab *pTab = (ZipfileTab*)pVtab;
10767 int rc = SQLITE_OK;
10769 assert( pTab->pWriteFd==0 );
10770 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
10771 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
10772 return SQLITE_ERROR;
10775 /* Open a write fd on the file. Also load the entire central directory
10776 ** structure into memory. During the transaction any new file data is
10777 ** appended to the archive file, but the central directory is accumulated
10778 ** in main-memory until the transaction is committed. */
10779 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
10780 if( pTab->pWriteFd==0 ){
10781 pTab->base.zErrMsg = sqlite3_mprintf(
10782 "zipfile: failed to open file %s for writing", pTab->zFile
10786 fseek(pTab->pWriteFd, 0, SEEK_END);
10787 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
10788 rc = zipfileLoadDirectory(pTab, 0, 0);
10791 if( rc!=SQLITE_OK ){
10792 zipfileCleanupTransaction(pTab);
10799 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
10802 static u32 zipfileTime(void){
10803 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
10805 if( pVfs==0 ) return 0;
10806 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
10808 pVfs->xCurrentTimeInt64(pVfs, &ms);
10809 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
10812 pVfs->xCurrentTime(pVfs, &day);
10813 ret = (u32)((day - 2440587.5) * 86400);
10819 ** Return a 32-bit timestamp in UNIX epoch format.
10821 ** If the value passed as the only argument is either NULL or an SQL NULL,
10822 ** return the current time. Otherwise, return the value stored in (*pVal)
10823 ** cast to a 32-bit unsigned integer.
10825 static u32 zipfileGetTime(sqlite3_value *pVal){
10826 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
10827 return zipfileTime();
10829 return (u32)sqlite3_value_int64(pVal);
10833 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
10834 ** linked list. Remove it from the list and free the object.
10836 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
10838 if( pTab->pFirstEntry==pOld ){
10839 pTab->pFirstEntry = pOld->pNext;
10840 if( pTab->pLastEntry==pOld ) pTab->pLastEntry = 0;
10843 for(p=pTab->pFirstEntry; p; p=p->pNext){
10844 if( p->pNext==pOld ){
10845 p->pNext = pOld->pNext;
10846 if( pTab->pLastEntry==pOld ) pTab->pLastEntry = p;
10851 zipfileEntryFree(pOld);
10858 static int zipfileUpdate(
10859 sqlite3_vtab *pVtab,
10861 sqlite3_value **apVal,
10862 sqlite_int64 *pRowid
10864 ZipfileTab *pTab = (ZipfileTab*)pVtab;
10865 int rc = SQLITE_OK; /* Return Code */
10866 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
10868 u32 mode = 0; /* Mode for new entry */
10869 u32 mTime = 0; /* Modification time for new entry */
10870 i64 sz = 0; /* Uncompressed size */
10871 const char *zPath = 0; /* Path for new entry */
10872 int nPath = 0; /* strlen(zPath) */
10873 const u8 *pData = 0; /* Pointer to buffer containing content */
10874 int nData = 0; /* Size of pData buffer in bytes */
10875 int iMethod = 0; /* Compression method for new entry */
10876 u8 *pFree = 0; /* Free this */
10877 char *zFree = 0; /* Also free this */
10878 ZipfileEntry *pOld = 0;
10879 ZipfileEntry *pOld2 = 0;
10880 int bUpdate = 0; /* True for an update that modifies "name" */
10886 if( pTab->pWriteFd==0 ){
10887 rc = zipfileBegin(pVtab);
10888 if( rc!=SQLITE_OK ) return rc;
10891 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
10892 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
10893 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
10894 int nDelete = (int)strlen(zDelete);
10896 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
10897 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
10901 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
10902 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
10905 assert( pOld->pNext );
10910 /* Check that "sz" and "rawdata" are both NULL: */
10911 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
10912 zipfileTableErr(pTab, "sz must be NULL");
10913 rc = SQLITE_CONSTRAINT;
10915 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
10916 zipfileTableErr(pTab, "rawdata must be NULL");
10917 rc = SQLITE_CONSTRAINT;
10920 if( rc==SQLITE_OK ){
10921 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
10922 /* data=NULL. A directory */
10925 /* Value specified for "data", and possibly "method". This must be
10926 ** a regular file or a symlink. */
10927 const u8 *aIn = sqlite3_value_blob(apVal[7]);
10928 int nIn = sqlite3_value_bytes(apVal[7]);
10929 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
10931 iMethod = sqlite3_value_int(apVal[8]);
10935 if( iMethod!=0 && iMethod!=8 ){
10936 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
10937 rc = SQLITE_CONSTRAINT;
10939 if( bAuto || iMethod ){
10941 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
10942 if( rc==SQLITE_OK ){
10943 if( iMethod || nCmp<nIn ){
10950 iCrc32 = crc32(0, aIn, nIn);
10955 if( rc==SQLITE_OK ){
10956 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
10959 if( rc==SQLITE_OK ){
10960 zPath = (const char*)sqlite3_value_text(apVal[2]);
10961 if( zPath==0 ) zPath = "";
10962 nPath = (int)strlen(zPath);
10963 mTime = zipfileGetTime(apVal[4]);
10966 if( rc==SQLITE_OK && bIsDir ){
10967 /* For a directory, check that the last character in the path is a
10968 ** '/'. This appears to be required for compatibility with info-zip
10969 ** (the unzip command on unix). It does not create directories
10971 if( nPath<=0 || zPath[nPath-1]!='/' ){
10972 zFree = sqlite3_mprintf("%s/", zPath);
10973 zPath = (const char*)zFree;
10978 nPath = (int)strlen(zPath);
10983 /* Check that we're not inserting a duplicate entry -OR- updating an
10984 ** entry with a path, thereby making it into a duplicate. */
10985 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
10987 for(p=pTab->pFirstEntry; p; p=p->pNext){
10988 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
10989 switch( sqlite3_vtab_on_conflict(pTab->db) ){
10990 case SQLITE_IGNORE: {
10991 goto zipfile_update_done;
10993 case SQLITE_REPLACE: {
10998 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
10999 rc = SQLITE_CONSTRAINT;
11008 if( rc==SQLITE_OK ){
11009 /* Create the new CDS record. */
11010 pNew = zipfileNewEntry(zPath);
11014 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
11015 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
11016 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
11017 pNew->cds.iCompression = (u16)iMethod;
11018 zipfileMtimeToDos(&pNew->cds, mTime);
11019 pNew->cds.crc32 = iCrc32;
11020 pNew->cds.szCompressed = nData;
11021 pNew->cds.szUncompressed = (u32)sz;
11022 pNew->cds.iExternalAttr = (mode<<16);
11023 pNew->cds.iOffset = (u32)pTab->szCurrent;
11024 pNew->cds.nFile = (u16)nPath;
11025 pNew->mUnixTime = (u32)mTime;
11026 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
11027 zipfileAddEntry(pTab, pOld, pNew);
11032 if( rc==SQLITE_OK && (pOld || pOld2) ){
11034 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
11035 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
11036 pCsr->pCurrent = pCsr->pCurrent->pNext;
11041 zipfileRemoveEntryFromList(pTab, pOld);
11042 zipfileRemoveEntryFromList(pTab, pOld2);
11045 zipfile_update_done:
11046 sqlite3_free(pFree);
11047 sqlite3_free(zFree);
11051 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
11053 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
11054 zipfileWrite16(a, p->iDisk);
11055 zipfileWrite16(a, p->iFirstDisk);
11056 zipfileWrite16(a, p->nEntry);
11057 zipfileWrite16(a, p->nEntryTotal);
11058 zipfileWrite32(a, p->nSize);
11059 zipfileWrite32(a, p->iOffset);
11060 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
11065 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
11066 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
11067 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
11068 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
11072 ** Serialize the CDS structure into buffer aBuf[]. Return the number
11073 ** of bytes written.
11075 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
11077 ZipfileCDS *pCDS = &pEntry->cds;
11079 if( pEntry->aExtra==0 ){
11083 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
11084 zipfileWrite16(a, pCDS->iVersionMadeBy);
11085 zipfileWrite16(a, pCDS->iVersionExtract);
11086 zipfileWrite16(a, pCDS->flags);
11087 zipfileWrite16(a, pCDS->iCompression);
11088 zipfileWrite16(a, pCDS->mTime);
11089 zipfileWrite16(a, pCDS->mDate);
11090 zipfileWrite32(a, pCDS->crc32);
11091 zipfileWrite32(a, pCDS->szCompressed);
11092 zipfileWrite32(a, pCDS->szUncompressed);
11093 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
11094 zipfileWrite16(a, pCDS->nFile);
11095 zipfileWrite16(a, pCDS->nExtra);
11096 zipfileWrite16(a, pCDS->nComment);
11097 zipfileWrite16(a, pCDS->iDiskStart);
11098 zipfileWrite16(a, pCDS->iInternalAttr);
11099 zipfileWrite32(a, pCDS->iExternalAttr);
11100 zipfileWrite32(a, pCDS->iOffset);
11102 memcpy(a, pCDS->zFile, pCDS->nFile);
11105 if( pEntry->aExtra ){
11106 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
11107 memcpy(a, pEntry->aExtra, n);
11110 assert( pCDS->nExtra==9 );
11111 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
11112 zipfileWrite16(a, 5);
11114 zipfileWrite32(a, pEntry->mUnixTime);
11120 static int zipfileCommit(sqlite3_vtab *pVtab){
11121 ZipfileTab *pTab = (ZipfileTab*)pVtab;
11122 int rc = SQLITE_OK;
11123 if( pTab->pWriteFd ){
11124 i64 iOffset = pTab->szCurrent;
11129 /* Write out all entries */
11130 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
11131 int n = zipfileSerializeCDS(p, pTab->aBuffer);
11132 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
11136 /* Write out the EOCD record */
11138 eocd.iFirstDisk = 0;
11139 eocd.nEntry = (u16)nEntry;
11140 eocd.nEntryTotal = (u16)nEntry;
11141 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
11142 eocd.iOffset = (u32)iOffset;
11143 rc = zipfileAppendEOCD(pTab, &eocd);
11145 zipfileCleanupTransaction(pTab);
11150 static int zipfileRollback(sqlite3_vtab *pVtab){
11151 return zipfileCommit(pVtab);
11154 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
11156 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
11157 if( iId==pCsr->iId ) break;
11162 static void zipfileFunctionCds(
11163 sqlite3_context *context,
11165 sqlite3_value **argv
11168 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
11171 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
11173 ZipfileCDS *p = &pCsr->pCurrent->cds;
11174 char *zRes = sqlite3_mprintf("{"
11175 "\"version-made-by\" : %u, "
11176 "\"version-to-extract\" : %u, "
11178 "\"compression\" : %u, "
11182 "\"compressed-size\" : %u, "
11183 "\"uncompressed-size\" : %u, "
11184 "\"file-name-length\" : %u, "
11185 "\"extra-field-length\" : %u, "
11186 "\"file-comment-length\" : %u, "
11187 "\"disk-number-start\" : %u, "
11188 "\"internal-attr\" : %u, "
11189 "\"external-attr\" : %u, "
11190 "\"offset\" : %u }",
11191 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
11192 (u32)p->flags, (u32)p->iCompression,
11193 (u32)p->mTime, (u32)p->mDate,
11194 (u32)p->crc32, (u32)p->szCompressed,
11195 (u32)p->szUncompressed, (u32)p->nFile,
11196 (u32)p->nExtra, (u32)p->nComment,
11197 (u32)p->iDiskStart, (u32)p->iInternalAttr,
11198 (u32)p->iExternalAttr, (u32)p->iOffset
11202 sqlite3_result_error_nomem(context);
11204 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
11205 sqlite3_free(zRes);
11211 ** xFindFunction method.
11213 static int zipfileFindFunction(
11214 sqlite3_vtab *pVtab, /* Virtual table handle */
11215 int nArg, /* Number of SQL function arguments */
11216 const char *zName, /* Name of SQL function */
11217 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
11218 void **ppArg /* OUT: User data for *pxFunc */
11221 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
11222 *pxFunc = zipfileFunctionCds;
11223 *ppArg = (void*)pVtab;
11229 typedef struct ZipfileBuffer ZipfileBuffer;
11230 struct ZipfileBuffer {
11231 u8 *a; /* Pointer to buffer */
11232 int n; /* Size of buffer in bytes */
11233 int nAlloc; /* Byte allocated at a[] */
11236 typedef struct ZipfileCtx ZipfileCtx;
11237 struct ZipfileCtx {
11239 ZipfileBuffer body;
11243 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
11244 if( pBuf->n+nByte>pBuf->nAlloc ){
11246 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
11247 int nReq = pBuf->n + nByte;
11249 while( nNew<nReq ) nNew = nNew*2;
11250 aNew = sqlite3_realloc64(pBuf->a, nNew);
11251 if( aNew==0 ) return SQLITE_NOMEM;
11253 pBuf->nAlloc = (int)nNew;
11259 ** xStep() callback for the zipfile() aggregate. This can be called in
11260 ** any of the following ways:
11262 ** SELECT zipfile(name,data) ...
11263 ** SELECT zipfile(name,mode,mtime,data) ...
11264 ** SELECT zipfile(name,mode,mtime,data,method) ...
11266 static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
11267 ZipfileCtx *p; /* Aggregate function context */
11268 ZipfileEntry e; /* New entry to add to zip archive */
11270 sqlite3_value *pName = 0;
11271 sqlite3_value *pMode = 0;
11272 sqlite3_value *pMtime = 0;
11273 sqlite3_value *pData = 0;
11274 sqlite3_value *pMethod = 0;
11278 int rc = SQLITE_OK;
11281 int iMethod = -1; /* Compression method to use (0 or 8) */
11283 const u8 *aData = 0; /* Possibly compressed data for new entry */
11284 int nData = 0; /* Size of aData[] in bytes */
11285 int szUncompressed = 0; /* Size of data before compression */
11286 u8 *aFree = 0; /* Free this before returning */
11287 u32 iCrc32 = 0; /* crc32 of uncompressed data */
11289 char *zName = 0; /* Path (name) of new entry */
11290 int nName = 0; /* Size of zName in bytes */
11291 char *zFree = 0; /* Free this before returning */
11294 memset(&e, 0, sizeof(e));
11295 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
11298 /* Martial the arguments into stack variables */
11299 if( nVal!=2 && nVal!=4 && nVal!=5 ){
11300 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
11302 goto zipfile_step_out;
11312 pMethod = apVal[4];
11316 /* Check that the 'name' parameter looks ok. */
11317 zName = (char*)sqlite3_value_text(pName);
11318 nName = sqlite3_value_bytes(pName);
11320 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
11322 goto zipfile_step_out;
11325 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
11326 ** deflate compression) or NULL (choose automatically). */
11327 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
11328 iMethod = (int)sqlite3_value_int64(pMethod);
11329 if( iMethod!=0 && iMethod!=8 ){
11330 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
11332 goto zipfile_step_out;
11336 /* Now inspect the data. If this is NULL, then the new entry must be a
11337 ** directory. Otherwise, figure out whether or not the data should
11338 ** be deflated or simply stored in the zip archive. */
11339 if( sqlite3_value_type(pData)==SQLITE_NULL ){
11343 aData = sqlite3_value_blob(pData);
11344 szUncompressed = nData = sqlite3_value_bytes(pData);
11345 iCrc32 = crc32(0, aData, nData);
11346 if( iMethod<0 || iMethod==8 ){
11348 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
11349 if( rc!=SQLITE_OK ){
11350 goto zipfile_step_out;
11352 if( iMethod==8 || nOut<nData ){
11362 /* Decode the "mode" argument. */
11363 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
11364 if( rc ) goto zipfile_step_out;
11366 /* Decode the "mtime" argument. */
11367 e.mUnixTime = zipfileGetTime(pMtime);
11369 /* If this is a directory entry, ensure that there is exactly one '/'
11370 ** at the end of the path. Or, if this is not a directory and the path
11371 ** ends in '/' it is an error. */
11373 if( nName>0 && zName[nName-1]=='/' ){
11374 zErr = sqlite3_mprintf("non-directory name must not end with /");
11376 goto zipfile_step_out;
11379 if( nName==0 || zName[nName-1]!='/' ){
11380 zName = zFree = sqlite3_mprintf("%s/", zName);
11383 goto zipfile_step_out;
11385 nName = (int)strlen(zName);
11387 while( nName>1 && zName[nName-2]=='/' ) nName--;
11391 /* Assemble the ZipfileEntry object for the new zip archive entry */
11392 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
11393 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
11394 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
11395 e.cds.iCompression = (u16)iMethod;
11396 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
11397 e.cds.crc32 = iCrc32;
11398 e.cds.szCompressed = nData;
11399 e.cds.szUncompressed = szUncompressed;
11400 e.cds.iExternalAttr = (mode<<16);
11401 e.cds.iOffset = p->body.n;
11402 e.cds.nFile = (u16)nName;
11403 e.cds.zFile = zName;
11405 /* Append the LFH to the body of the new archive */
11406 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
11407 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
11408 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
11410 /* Append the data to the body of the new archive */
11412 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
11413 memcpy(&p->body.a[p->body.n], aData, nData);
11414 p->body.n += nData;
11417 /* Append the CDS record to the directory of the new archive */
11418 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
11419 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
11420 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
11422 /* Increment the count of entries in the archive */
11426 sqlite3_free(aFree);
11427 sqlite3_free(zFree);
11430 sqlite3_result_error(pCtx, zErr, -1);
11432 sqlite3_result_error_code(pCtx, rc);
11435 sqlite3_free(zErr);
11439 ** xFinalize() callback for zipfile aggregate function.
11441 static void zipfileFinal(sqlite3_context *pCtx){
11444 sqlite3_int64 nZip;
11447 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
11450 memset(&eocd, 0, sizeof(eocd));
11451 eocd.nEntry = (u16)p->nEntry;
11452 eocd.nEntryTotal = (u16)p->nEntry;
11453 eocd.nSize = p->cds.n;
11454 eocd.iOffset = p->body.n;
11456 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
11457 aZip = (u8*)sqlite3_malloc64(nZip);
11459 sqlite3_result_error_nomem(pCtx);
11461 memcpy(aZip, p->body.a, p->body.n);
11462 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
11463 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
11464 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
11468 sqlite3_free(p->body.a);
11469 sqlite3_free(p->cds.a);
11474 ** Register the "zipfile" virtual table.
11476 static int zipfileRegister(sqlite3 *db){
11477 static sqlite3_module zipfileModule = {
11479 zipfileConnect, /* xCreate */
11480 zipfileConnect, /* xConnect */
11481 zipfileBestIndex, /* xBestIndex */
11482 zipfileDisconnect, /* xDisconnect */
11483 zipfileDisconnect, /* xDestroy */
11484 zipfileOpen, /* xOpen - open a cursor */
11485 zipfileClose, /* xClose - close a cursor */
11486 zipfileFilter, /* xFilter - configure scan constraints */
11487 zipfileNext, /* xNext - advance a cursor */
11488 zipfileEof, /* xEof - check for end of scan */
11489 zipfileColumn, /* xColumn - read data */
11490 0, /* xRowid - read data */
11491 zipfileUpdate, /* xUpdate */
11492 zipfileBegin, /* xBegin */
11494 zipfileCommit, /* xCommit */
11495 zipfileRollback, /* xRollback */
11496 zipfileFindFunction, /* xFindMethod */
11498 0, /* xSavepoint */
11501 0, /* xShadowName */
11505 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
11506 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
11507 if( rc==SQLITE_OK ){
11508 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
11509 zipfileStep, zipfileFinal
11512 assert( sizeof(i64)==8 );
11513 assert( sizeof(u32)==4 );
11514 assert( sizeof(u16)==2 );
11515 assert( sizeof(u8)==1 );
11518 #else /* SQLITE_OMIT_VIRTUALTABLE */
11519 # define zipfileRegister(x) SQLITE_OK
11525 int sqlite3_zipfile_init(
11528 const sqlite3_api_routines *pApi
11530 SQLITE_EXTENSION_INIT2(pApi);
11531 (void)pzErrMsg; /* Unused parameter */
11532 return zipfileRegister(db);
11535 /************************* End ../ext/misc/zipfile.c ********************/
11536 /************************* Begin ../ext/misc/sqlar.c ******************/
11540 ** The author disclaims copyright to this source code. In place of
11541 ** a legal notice, here is a blessing:
11543 ** May you do good and not evil.
11544 ** May you find forgiveness for yourself and forgive others.
11545 ** May you share freely, never taking more than you give.
11547 ******************************************************************************
11549 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
11550 ** for working with sqlar archives and used by the shell tool's built-in
11553 /* #include "sqlite3ext.h" */
11554 SQLITE_EXTENSION_INIT1
11556 #include <assert.h>
11559 ** Implementation of the "sqlar_compress(X)" SQL function.
11561 ** If the type of X is SQLITE_BLOB, and compressing that blob using
11562 ** zlib utility function compress() yields a smaller blob, return the
11563 ** compressed blob. Otherwise, return a copy of X.
11565 ** SQLar uses the "zlib format" for compressed content. The zlib format
11566 ** contains a two-byte identification header and a four-byte checksum at
11567 ** the end. This is different from ZIP which uses the raw deflate format.
11569 ** Future enhancements to SQLar might add support for new compression formats.
11570 ** If so, those new formats will be identified by alternative headers in the
11571 ** compressed data.
11573 static void sqlarCompressFunc(
11574 sqlite3_context *context,
11576 sqlite3_value **argv
11579 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
11580 const Bytef *pData = sqlite3_value_blob(argv[0]);
11581 uLong nData = sqlite3_value_bytes(argv[0]);
11582 uLongf nOut = compressBound(nData);
11585 pOut = (Bytef*)sqlite3_malloc(nOut);
11587 sqlite3_result_error_nomem(context);
11590 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
11591 sqlite3_result_error(context, "error in compress()", -1);
11592 }else if( nOut<nData ){
11593 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
11595 sqlite3_result_value(context, argv[0]);
11597 sqlite3_free(pOut);
11600 sqlite3_result_value(context, argv[0]);
11605 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
11607 ** Parameter SZ is interpreted as an integer. If it is less than or
11608 ** equal to zero, then this function returns a copy of X. Or, if
11609 ** SZ is equal to the size of X when interpreted as a blob, also
11610 ** return a copy of X. Otherwise, decompress blob X using zlib
11611 ** utility function uncompress() and return the results (another
11614 static void sqlarUncompressFunc(
11615 sqlite3_context *context,
11617 sqlite3_value **argv
11623 sz = sqlite3_value_int(argv[1]);
11625 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
11626 sqlite3_result_value(context, argv[0]);
11628 const Bytef *pData= sqlite3_value_blob(argv[0]);
11629 Bytef *pOut = sqlite3_malloc(sz);
11631 sqlite3_result_error_nomem(context);
11632 }else if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
11633 sqlite3_result_error(context, "error in uncompress()", -1);
11635 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
11637 sqlite3_free(pOut);
11644 int sqlite3_sqlar_init(
11647 const sqlite3_api_routines *pApi
11649 int rc = SQLITE_OK;
11650 SQLITE_EXTENSION_INIT2(pApi);
11651 (void)pzErrMsg; /* Unused parameter */
11652 rc = sqlite3_create_function(db, "sqlar_compress", 1,
11653 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
11654 sqlarCompressFunc, 0, 0);
11655 if( rc==SQLITE_OK ){
11656 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
11657 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
11658 sqlarUncompressFunc, 0, 0);
11663 /************************* End ../ext/misc/sqlar.c ********************/
11665 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
11669 ** The author disclaims copyright to this source code. In place of
11670 ** a legal notice, here is a blessing:
11672 ** May you do good and not evil.
11673 ** May you find forgiveness for yourself and forgive others.
11674 ** May you share freely, never taking more than you give.
11676 *************************************************************************
11678 #if !defined(SQLITEEXPERT_H)
11679 #define SQLITEEXPERT_H 1
11680 /* #include "sqlite3.h" */
11682 typedef struct sqlite3expert sqlite3expert;
11685 ** Create a new sqlite3expert object.
11687 ** If successful, a pointer to the new object is returned and (*pzErr) set
11688 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
11689 ** an English-language error message. In this case it is the responsibility
11690 ** of the caller to eventually free the error message buffer using
11693 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
11696 ** Configure an sqlite3expert object.
11698 ** EXPERT_CONFIG_SAMPLE:
11699 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
11700 ** each candidate index. This involves scanning and sorting the entire
11701 ** contents of each user database table once for each candidate index
11702 ** associated with the table. For large databases, this can be
11703 ** prohibitively slow. This option allows the sqlite3expert object to
11704 ** be configured so that sqlite_stat1 data is instead generated based on a
11705 ** subset of each table, or so that no sqlite_stat1 data is used at all.
11707 ** A single integer argument is passed to this option. If the value is less
11708 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
11709 ** the analysis - indexes are recommended based on the database schema only.
11710 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
11711 ** generated for each candidate index (this is the default). Finally, if the
11712 ** value falls between 0 and 100, then it represents the percentage of user
11713 ** table rows that should be considered when generating sqlite_stat1 data.
11717 ** // Do not generate any sqlite_stat1 data
11718 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
11720 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
11721 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
11723 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
11725 #define EXPERT_CONFIG_SAMPLE 1 /* int */
11728 ** Specify zero or more SQL statements to be included in the analysis.
11730 ** Buffer zSql must contain zero or more complete SQL statements. This
11731 ** function parses all statements contained in the buffer and adds them
11732 ** to the internal list of statements to analyze. If successful, SQLITE_OK
11733 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
11734 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
11735 ** may be set to point to an English language error message. In this case
11736 ** the caller is responsible for eventually freeing the error message buffer
11737 ** using sqlite3_free().
11739 ** If an error does occur while processing one of the statements in the
11740 ** buffer passed as the second argument, none of the statements in the
11741 ** buffer are added to the analysis.
11743 ** This function must be called before sqlite3_expert_analyze(). If a call
11744 ** to this function is made on an sqlite3expert object that has already
11745 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
11746 ** immediately and no statements are added to the analysis.
11748 int sqlite3_expert_sql(
11749 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
11750 const char *zSql, /* SQL statement(s) to add */
11751 char **pzErr /* OUT: Error message (if any) */
11756 ** This function is called after the sqlite3expert object has been configured
11757 ** with all SQL statements using sqlite3_expert_sql() to actually perform
11758 ** the analysis. Once this function has been called, it is not possible to
11759 ** add further SQL statements to the analysis.
11761 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
11762 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
11763 ** point to a buffer containing an English language error message. In this
11764 ** case it is the responsibility of the caller to eventually free the buffer
11765 ** using sqlite3_free().
11767 ** If an error does occur within this function, the sqlite3expert object
11768 ** is no longer useful for any purpose. At that point it is no longer
11769 ** possible to add further SQL statements to the object or to re-attempt
11770 ** the analysis. The sqlite3expert object must still be freed using a call
11771 ** sqlite3_expert_destroy().
11773 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
11776 ** Return the total number of statements loaded using sqlite3_expert_sql().
11777 ** The total number of SQL statements may be different from the total number
11778 ** to calls to sqlite3_expert_sql().
11780 int sqlite3_expert_count(sqlite3expert*);
11783 ** Return a component of the report.
11785 ** This function is called after sqlite3_expert_analyze() to extract the
11786 ** results of the analysis. Each call to this function returns either a
11787 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
11788 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
11789 ** #define constants defined below.
11791 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
11792 ** information relating to a specific SQL statement. In these cases that
11793 ** SQL statement is identified by the value passed as the second argument.
11794 ** SQL statements are numbered from 0 in the order in which they are parsed.
11795 ** If an out-of-range value (less than zero or equal to or greater than the
11796 ** value returned by sqlite3_expert_count()) is passed as the second argument
11797 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
11799 ** EXPERT_REPORT_SQL:
11800 ** Return the text of SQL statement iStmt.
11802 ** EXPERT_REPORT_INDEXES:
11803 ** Return a buffer containing the CREATE INDEX statements for all recommended
11804 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
11807 ** EXPERT_REPORT_PLAN:
11808 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
11809 ** iStmt after the proposed indexes have been added to the database schema.
11811 ** EXPERT_REPORT_CANDIDATES:
11812 ** Return a pointer to a buffer containing the CREATE INDEX statements
11813 ** for all indexes that were tested (for all SQL statements). The iStmt
11814 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
11816 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
11819 ** Values for the third argument passed to sqlite3_expert_report().
11821 #define EXPERT_REPORT_SQL 1
11822 #define EXPERT_REPORT_INDEXES 2
11823 #define EXPERT_REPORT_PLAN 3
11824 #define EXPERT_REPORT_CANDIDATES 4
11827 ** Free an (sqlite3expert*) handle and all associated resources. There
11828 ** should be one call to this function for each successful call to
11829 ** sqlite3-expert_new().
11831 void sqlite3_expert_destroy(sqlite3expert*);
11833 #endif /* !defined(SQLITEEXPERT_H) */
11835 /************************* End ../ext/expert/sqlite3expert.h ********************/
11836 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
11840 ** The author disclaims copyright to this source code. In place of
11841 ** a legal notice, here is a blessing:
11843 ** May you do good and not evil.
11844 ** May you find forgiveness for yourself and forgive others.
11845 ** May you share freely, never taking more than you give.
11847 *************************************************************************
11849 /* #include "sqlite3expert.h" */
11850 #include <assert.h>
11851 #include <string.h>
11854 #if !defined(SQLITE_AMALGAMATION)
11855 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
11856 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
11858 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
11859 # define ALWAYS(X) (1)
11860 # define NEVER(X) (0)
11861 #elif !defined(NDEBUG)
11862 # define ALWAYS(X) ((X)?1:(assert(0),0))
11863 # define NEVER(X) ((X)?(assert(0),1):0)
11865 # define ALWAYS(X) (X)
11866 # define NEVER(X) (X)
11868 #endif /* !defined(SQLITE_AMALGAMATION) */
11871 #ifndef SQLITE_OMIT_VIRTUALTABLE
11873 /* typedef sqlite3_int64 i64; */
11874 /* typedef sqlite3_uint64 u64; */
11876 typedef struct IdxColumn IdxColumn;
11877 typedef struct IdxConstraint IdxConstraint;
11878 typedef struct IdxScan IdxScan;
11879 typedef struct IdxStatement IdxStatement;
11880 typedef struct IdxTable IdxTable;
11881 typedef struct IdxWrite IdxWrite;
11883 #define STRLEN (int)strlen
11886 ** A temp table name that we assume no user database will actually use.
11887 ** If this assumption proves incorrect triggers on the table with the
11888 ** conflicting name will be ignored.
11890 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
11893 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
11894 ** any other type of single-ended range constraint on a column).
11897 ** Used to temporarily link IdxConstraint objects into lists while
11898 ** creating candidate indexes.
11900 struct IdxConstraint {
11901 char *zColl; /* Collation sequence */
11902 int bRange; /* True for range, false for eq */
11903 int iCol; /* Constrained table column */
11904 int bFlag; /* Used by idxFindCompatible() */
11905 int bDesc; /* True if ORDER BY <expr> DESC */
11906 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
11907 IdxConstraint *pLink; /* See above */
11911 ** A single scan of a single table.
11914 IdxTable *pTab; /* Associated table object */
11915 int iDb; /* Database containing table zTable */
11916 i64 covering; /* Mask of columns required for cov. index */
11917 IdxConstraint *pOrder; /* ORDER BY columns */
11918 IdxConstraint *pEq; /* List of == constraints */
11919 IdxConstraint *pRange; /* List of < constraints */
11920 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
11924 ** Information regarding a single database table. Extracted from
11925 ** "PRAGMA table_info" by function idxGetTableInfo().
11934 char *zName; /* Table name */
11936 IdxTable *pNext; /* Next table in linked list of all tables */
11940 ** An object of the following type is created for each unique table/write-op
11941 ** seen. The objects are stored in a singly-linked list beginning at
11942 ** sqlite3expert.pWrite.
11946 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
11951 ** Each statement being analyzed is represented by an instance of this
11954 struct IdxStatement {
11955 int iId; /* Statement number */
11956 char *zSql; /* SQL statement */
11957 char *zIdx; /* Indexes */
11958 char *zEQP; /* Plan */
11959 IdxStatement *pNext;
11964 ** A hash table for storing strings. With space for a payload string
11965 ** with each entry. Methods are:
11972 #define IDX_HASH_SIZE 1023
11973 typedef struct IdxHashEntry IdxHashEntry;
11974 typedef struct IdxHash IdxHash;
11975 struct IdxHashEntry {
11976 char *zKey; /* nul-terminated key */
11977 char *zVal; /* nul-terminated value string */
11978 char *zVal2; /* nul-terminated value string 2 */
11979 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
11980 IdxHashEntry *pNext; /* Next entry in hash */
11983 IdxHashEntry *pFirst;
11984 IdxHashEntry *aHash[IDX_HASH_SIZE];
11988 ** sqlite3expert object.
11990 struct sqlite3expert {
11991 int iSample; /* Percentage of tables to sample for stat1 */
11992 sqlite3 *db; /* User database */
11993 sqlite3 *dbm; /* In-memory db for this analysis */
11994 sqlite3 *dbv; /* Vtab schema for this analysis */
11995 IdxTable *pTable; /* List of all IdxTable objects */
11996 IdxScan *pScan; /* List of scan objects */
11997 IdxWrite *pWrite; /* List of write objects */
11998 IdxStatement *pStatement; /* List of IdxStatement objects */
11999 int bRun; /* True once analysis has run */
12001 int rc; /* Error code from whereinfo hook */
12002 IdxHash hIdx; /* Hash containing all candidate indexes */
12003 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
12008 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
12009 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
12011 static void *idxMalloc(int *pRc, int nByte){
12013 assert( *pRc==SQLITE_OK );
12015 pRet = sqlite3_malloc(nByte);
12017 memset(pRet, 0, nByte);
12019 *pRc = SQLITE_NOMEM;
12025 ** Initialize an IdxHash hash table.
12027 static void idxHashInit(IdxHash *pHash){
12028 memset(pHash, 0, sizeof(IdxHash));
12032 ** Reset an IdxHash hash table.
12034 static void idxHashClear(IdxHash *pHash){
12036 for(i=0; i<IDX_HASH_SIZE; i++){
12037 IdxHashEntry *pEntry;
12038 IdxHashEntry *pNext;
12039 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
12040 pNext = pEntry->pHashNext;
12041 sqlite3_free(pEntry->zVal2);
12042 sqlite3_free(pEntry);
12045 memset(pHash, 0, sizeof(IdxHash));
12049 ** Return the index of the hash bucket that the string specified by the
12050 ** arguments to this function belongs.
12052 static int idxHashString(const char *z, int n){
12053 unsigned int ret = 0;
12055 for(i=0; i<n; i++){
12056 ret += (ret<<3) + (unsigned char)(z[i]);
12058 return (int)(ret % IDX_HASH_SIZE);
12062 ** If zKey is already present in the hash table, return non-zero and do
12063 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
12064 ** the hash table passed as the second argument.
12066 static int idxHashAdd(
12072 int nKey = STRLEN(zKey);
12073 int iHash = idxHashString(zKey, nKey);
12074 int nVal = (zVal ? STRLEN(zVal) : 0);
12075 IdxHashEntry *pEntry;
12076 assert( iHash>=0 );
12077 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
12078 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
12082 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
12084 pEntry->zKey = (char*)&pEntry[1];
12085 memcpy(pEntry->zKey, zKey, nKey);
12087 pEntry->zVal = &pEntry->zKey[nKey+1];
12088 memcpy(pEntry->zVal, zVal, nVal);
12090 pEntry->pHashNext = pHash->aHash[iHash];
12091 pHash->aHash[iHash] = pEntry;
12093 pEntry->pNext = pHash->pFirst;
12094 pHash->pFirst = pEntry;
12100 ** If zKey/nKey is present in the hash table, return a pointer to the
12101 ** hash-entry object.
12103 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
12105 IdxHashEntry *pEntry;
12106 if( nKey<0 ) nKey = STRLEN(zKey);
12107 iHash = idxHashString(zKey, nKey);
12108 assert( iHash>=0 );
12109 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
12110 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
12118 ** If the hash table contains an entry with a key equal to the string
12119 ** passed as the final two arguments to this function, return a pointer
12120 ** to the payload string. Otherwise, if zKey/nKey is not present in the
12121 ** hash table, return NULL.
12123 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
12124 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
12125 if( pEntry ) return pEntry->zVal;
12130 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
12131 ** variable to point to a copy of nul-terminated string zColl.
12133 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
12134 IdxConstraint *pNew;
12135 int nColl = STRLEN(zColl);
12137 assert( *pRc==SQLITE_OK );
12138 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
12140 pNew->zColl = (char*)&pNew[1];
12141 memcpy(pNew->zColl, zColl, nColl+1);
12147 ** An error associated with database handle db has just occurred. Pass
12148 ** the error message to callback function xOut.
12150 static void idxDatabaseError(
12151 sqlite3 *db, /* Database handle */
12152 char **pzErrmsg /* Write error here */
12154 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
12158 ** Prepare an SQL statement.
12160 static int idxPrepareStmt(
12161 sqlite3 *db, /* Database handle to compile against */
12162 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
12163 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
12164 const char *zSql /* SQL statement to compile */
12166 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12167 if( rc!=SQLITE_OK ){
12169 idxDatabaseError(db, pzErrmsg);
12175 ** Prepare an SQL statement using the results of a printf() formatting.
12177 static int idxPrintfPrepareStmt(
12178 sqlite3 *db, /* Database handle to compile against */
12179 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
12180 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
12181 const char *zFmt, /* printf() format of SQL statement */
12182 ... /* Trailing printf() arguments */
12187 va_start(ap, zFmt);
12188 zSql = sqlite3_vmprintf(zFmt, ap);
12192 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
12193 sqlite3_free(zSql);
12200 /*************************************************************************
12201 ** Beginning of virtual table implementation.
12203 typedef struct ExpertVtab ExpertVtab;
12204 struct ExpertVtab {
12207 sqlite3expert *pExpert;
12210 typedef struct ExpertCsr ExpertCsr;
12212 sqlite3_vtab_cursor base;
12213 sqlite3_stmt *pData;
12216 static char *expertDequote(const char *zIn){
12217 int n = STRLEN(zIn);
12218 char *zRet = sqlite3_malloc(n);
12220 assert( zIn[0]=='\'' );
12221 assert( zIn[n-1]=='\'' );
12226 for(iIn=1; iIn<(n-1); iIn++){
12227 if( zIn[iIn]=='\'' ){
12228 assert( zIn[iIn+1]=='\'' );
12231 zRet[iOut++] = zIn[iIn];
12240 ** This function is the implementation of both the xConnect and xCreate
12241 ** methods of the r-tree virtual table.
12243 ** argv[0] -> module name
12244 ** argv[1] -> database name
12245 ** argv[2] -> table name
12246 ** argv[...] -> column names...
12248 static int expertConnect(
12251 int argc, const char *const*argv,
12252 sqlite3_vtab **ppVtab,
12255 sqlite3expert *pExpert = (sqlite3expert*)pAux;
12260 *pzErr = sqlite3_mprintf("internal error!");
12263 char *zCreateTable = expertDequote(argv[3]);
12264 if( zCreateTable ){
12265 rc = sqlite3_declare_vtab(db, zCreateTable);
12266 if( rc==SQLITE_OK ){
12267 p = idxMalloc(&rc, sizeof(ExpertVtab));
12269 if( rc==SQLITE_OK ){
12270 p->pExpert = pExpert;
12271 p->pTab = pExpert->pTable;
12272 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
12274 sqlite3_free(zCreateTable);
12280 *ppVtab = (sqlite3_vtab*)p;
12284 static int expertDisconnect(sqlite3_vtab *pVtab){
12285 ExpertVtab *p = (ExpertVtab*)pVtab;
12290 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
12291 ExpertVtab *p = (ExpertVtab*)pVtab;
12292 int rc = SQLITE_OK;
12296 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
12297 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
12298 SQLITE_INDEX_CONSTRAINT_LE;
12300 pScan = idxMalloc(&rc, sizeof(IdxScan));
12304 /* Link the new scan object into the list */
12305 pScan->pTab = p->pTab;
12306 pScan->pNextScan = p->pExpert->pScan;
12307 p->pExpert->pScan = pScan;
12309 /* Add the constraints to the IdxScan object */
12310 for(i=0; i<pIdxInfo->nConstraint; i++){
12311 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
12313 && pCons->iColumn>=0
12314 && p->pTab->aCol[pCons->iColumn].iPk==0
12315 && (pCons->op & opmask)
12317 IdxConstraint *pNew;
12318 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
12319 pNew = idxNewConstraint(&rc, zColl);
12321 pNew->iCol = pCons->iColumn;
12322 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
12323 pNew->pNext = pScan->pEq;
12327 pNew->pNext = pScan->pRange;
12328 pScan->pRange = pNew;
12332 pIdxInfo->aConstraintUsage[i].argvIndex = n;
12336 /* Add the ORDER BY to the IdxScan object */
12337 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
12338 int iCol = pIdxInfo->aOrderBy[i].iColumn;
12340 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
12343 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
12344 pNew->pNext = pScan->pOrder;
12345 pNew->pLink = pScan->pOrder;
12346 pScan->pOrder = pNew;
12353 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
12357 static int expertUpdate(
12358 sqlite3_vtab *pVtab,
12360 sqlite3_value **azData,
12361 sqlite_int64 *pRowid
12371 ** Virtual table module xOpen method.
12373 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
12374 int rc = SQLITE_OK;
12377 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
12378 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
12383 ** Virtual table module xClose method.
12385 static int expertClose(sqlite3_vtab_cursor *cur){
12386 ExpertCsr *pCsr = (ExpertCsr*)cur;
12387 sqlite3_finalize(pCsr->pData);
12388 sqlite3_free(pCsr);
12393 ** Virtual table module xEof method.
12395 ** Return non-zero if the cursor does not currently point to a valid
12396 ** record (i.e if the scan has finished), or zero otherwise.
12398 static int expertEof(sqlite3_vtab_cursor *cur){
12399 ExpertCsr *pCsr = (ExpertCsr*)cur;
12400 return pCsr->pData==0;
12404 ** Virtual table module xNext method.
12406 static int expertNext(sqlite3_vtab_cursor *cur){
12407 ExpertCsr *pCsr = (ExpertCsr*)cur;
12408 int rc = SQLITE_OK;
12410 assert( pCsr->pData );
12411 rc = sqlite3_step(pCsr->pData);
12412 if( rc!=SQLITE_ROW ){
12413 rc = sqlite3_finalize(pCsr->pData);
12423 ** Virtual table module xRowid method.
12425 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
12432 ** Virtual table module xColumn method.
12434 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
12435 ExpertCsr *pCsr = (ExpertCsr*)cur;
12436 sqlite3_value *pVal;
12437 pVal = sqlite3_column_value(pCsr->pData, i);
12439 sqlite3_result_value(ctx, pVal);
12445 ** Virtual table module xFilter method.
12447 static int expertFilter(
12448 sqlite3_vtab_cursor *cur,
12449 int idxNum, const char *idxStr,
12450 int argc, sqlite3_value **argv
12452 ExpertCsr *pCsr = (ExpertCsr*)cur;
12453 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
12454 sqlite3expert *pExpert = pVtab->pExpert;
12461 rc = sqlite3_finalize(pCsr->pData);
12463 if( rc==SQLITE_OK ){
12464 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
12465 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
12469 if( rc==SQLITE_OK ){
12470 rc = expertNext(cur);
12475 static int idxRegisterVtab(sqlite3expert *p){
12476 static sqlite3_module expertModule = {
12478 expertConnect, /* xCreate - create a table */
12479 expertConnect, /* xConnect - connect to an existing table */
12480 expertBestIndex, /* xBestIndex - Determine search strategy */
12481 expertDisconnect, /* xDisconnect - Disconnect from a table */
12482 expertDisconnect, /* xDestroy - Drop a table */
12483 expertOpen, /* xOpen - open a cursor */
12484 expertClose, /* xClose - close a cursor */
12485 expertFilter, /* xFilter - configure scan constraints */
12486 expertNext, /* xNext - advance a cursor */
12487 expertEof, /* xEof */
12488 expertColumn, /* xColumn - read data */
12489 expertRowid, /* xRowid - read data */
12490 expertUpdate, /* xUpdate - write data */
12491 0, /* xBegin - begin transaction */
12492 0, /* xSync - sync transaction */
12493 0, /* xCommit - commit transaction */
12494 0, /* xRollback - rollback transaction */
12495 0, /* xFindFunction - function overloading */
12496 0, /* xRename - rename the table */
12497 0, /* xSavepoint */
12499 0, /* xRollbackTo */
12500 0, /* xShadowName */
12501 0, /* xIntegrity */
12504 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
12507 ** End of virtual table implementation.
12508 *************************************************************************/
12510 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
12511 ** is called, set it to the return value of sqlite3_finalize() before
12512 ** returning. Otherwise, discard the sqlite3_finalize() return value.
12514 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
12515 int rc = sqlite3_finalize(pStmt);
12516 if( *pRc==SQLITE_OK ) *pRc = rc;
12520 ** Attempt to allocate an IdxTable structure corresponding to table zTab
12521 ** in the main database of connection db. If successful, set (*ppOut) to
12522 ** point to the new object and return SQLITE_OK. Otherwise, return an
12523 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
12524 ** set to point to an error string.
12526 ** It is the responsibility of the caller to eventually free either the
12527 ** IdxTable object or error message using sqlite3_free().
12529 static int idxGetTableInfo(
12530 sqlite3 *db, /* Database connection to read details from */
12531 const char *zTab, /* Table name */
12532 IdxTable **ppOut, /* OUT: New object (if successful) */
12533 char **pzErrmsg /* OUT: Error message (if not) */
12535 sqlite3_stmt *p1 = 0;
12539 IdxTable *pNew = 0;
12545 if( zTab==0 ) return SQLITE_ERROR;
12546 nTab = STRLEN(zTab);
12547 nByte = sizeof(IdxTable) + nTab + 1;
12548 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
12549 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
12550 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
12551 const char *zColSeq = 0;
12556 nByte += 1 + STRLEN(zCol);
12557 rc = sqlite3_table_column_metadata(
12558 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
12560 if( zColSeq==0 ) zColSeq = "binary";
12561 nByte += 1 + STRLEN(zColSeq);
12563 nPk += (sqlite3_column_int(p1, 5)>0);
12565 rc2 = sqlite3_reset(p1);
12566 if( rc==SQLITE_OK ) rc = rc2;
12568 nByte += sizeof(IdxColumn) * nCol;
12569 if( rc==SQLITE_OK ){
12570 pNew = idxMalloc(&rc, nByte);
12572 if( rc==SQLITE_OK ){
12573 pNew->aCol = (IdxColumn*)&pNew[1];
12575 pCsr = (char*)&pNew->aCol[nCol];
12579 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
12580 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
12581 const char *zColSeq = 0;
12583 if( zCol==0 ) continue;
12584 nCopy = STRLEN(zCol) + 1;
12585 pNew->aCol[nCol].zName = pCsr;
12586 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
12587 memcpy(pCsr, zCol, nCopy);
12590 rc = sqlite3_table_column_metadata(
12591 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
12593 if( rc==SQLITE_OK ){
12594 if( zColSeq==0 ) zColSeq = "binary";
12595 nCopy = STRLEN(zColSeq) + 1;
12596 pNew->aCol[nCol].zColl = pCsr;
12597 memcpy(pCsr, zColSeq, nCopy);
12603 idxFinalize(&rc, p1);
12605 if( rc!=SQLITE_OK ){
12606 sqlite3_free(pNew);
12608 }else if( ALWAYS(pNew!=0) ){
12609 pNew->zName = pCsr;
12610 if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
12618 ** This function is a no-op if *pRc is set to anything other than
12619 ** SQLITE_OK when it is called.
12621 ** If *pRc is initially set to SQLITE_OK, then the text specified by
12622 ** the printf() style arguments is appended to zIn and the result returned
12623 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
12624 ** zIn before returning.
12626 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
12630 int nIn = zIn ? STRLEN(zIn) : 0;
12632 va_start(ap, zFmt);
12633 if( *pRc==SQLITE_OK ){
12634 zAppend = sqlite3_vmprintf(zFmt, ap);
12636 nAppend = STRLEN(zAppend);
12637 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
12639 if( zAppend && zRet ){
12640 if( nIn ) memcpy(zRet, zIn, nIn);
12641 memcpy(&zRet[nIn], zAppend, nAppend+1);
12643 sqlite3_free(zRet);
12645 *pRc = SQLITE_NOMEM;
12647 sqlite3_free(zAppend);
12655 ** Return true if zId must be quoted in order to use it as an SQL
12656 ** identifier, or false otherwise.
12658 static int idxIdentifierRequiresQuotes(const char *zId){
12660 int nId = STRLEN(zId);
12662 if( sqlite3_keyword_check(zId, nId) ) return 1;
12664 for(i=0; zId[i]; i++){
12666 && !(zId[i]>='0' && zId[i]<='9')
12667 && !(zId[i]>='a' && zId[i]<='z')
12668 && !(zId[i]>='A' && zId[i]<='Z')
12677 ** This function appends an index column definition suitable for constraint
12678 ** pCons to the string passed as zIn and returns the result.
12680 static char *idxAppendColDefn(
12681 int *pRc, /* IN/OUT: Error code */
12682 char *zIn, /* Column defn accumulated so far */
12683 IdxTable *pTab, /* Table index will be created on */
12684 IdxConstraint *pCons
12687 IdxColumn *p = &pTab->aCol[pCons->iCol];
12688 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
12690 if( idxIdentifierRequiresQuotes(p->zName) ){
12691 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
12693 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
12696 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
12697 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
12698 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
12700 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
12704 if( pCons->bDesc ){
12705 zRet = idxAppendText(pRc, zRet, " DESC");
12711 ** Search database dbm for an index compatible with the one idxCreateFromCons()
12712 ** would create from arguments pScan, pEq and pTail. If no error occurs and
12713 ** such an index is found, return non-zero. Or, if no such index is found,
12716 ** If an error occurs, set *pRc to an SQLite error code and return zero.
12718 static int idxFindCompatible(
12719 int *pRc, /* OUT: Error code */
12720 sqlite3* dbm, /* Database to search */
12721 IdxScan *pScan, /* Scan for table to search for index on */
12722 IdxConstraint *pEq, /* List of == constraints */
12723 IdxConstraint *pTail /* List of range constraints */
12725 const char *zTbl = pScan->pTab->zName;
12726 sqlite3_stmt *pIdxList = 0;
12727 IdxConstraint *pIter;
12728 int nEq = 0; /* Number of elements in pEq */
12731 /* Count the elements in list pEq */
12732 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
12734 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
12735 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
12737 IdxConstraint *pT = pTail;
12738 sqlite3_stmt *pInfo = 0;
12739 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
12740 if( zIdx==0 ) continue;
12742 /* Zero the IdxConstraint.bFlag values in the pEq list */
12743 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
12745 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
12746 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
12747 int iIdx = sqlite3_column_int(pInfo, 0);
12748 int iCol = sqlite3_column_int(pInfo, 1);
12749 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
12752 for(pIter=pEq; pIter; pIter=pIter->pLink){
12753 if( pIter->bFlag ) continue;
12754 if( pIter->iCol!=iCol ) continue;
12755 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
12765 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
12773 idxFinalize(&rc, pInfo);
12775 if( rc==SQLITE_OK && bMatch ){
12776 sqlite3_finalize(pIdxList);
12780 idxFinalize(&rc, pIdxList);
12786 /* Callback for sqlite3_exec() with query with leading count(*) column.
12787 * The first argument is expected to be an int*, referent to be incremented
12788 * if that leading column is not exactly '0'.
12790 static int countNonzeros(void* pCount, int nc,
12791 char* azResults[], char* azColumns[]){
12792 (void)azColumns; /* Suppress unused parameter warning */
12793 if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
12794 *((int *)pCount) += 1;
12799 static int idxCreateFromCons(
12802 IdxConstraint *pEq,
12803 IdxConstraint *pTail
12805 sqlite3 *dbm = p->dbm;
12806 int rc = SQLITE_OK;
12807 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
12808 IdxTable *pTab = pScan->pTab;
12811 IdxConstraint *pCons;
12812 unsigned int h = 0;
12815 for(pCons=pEq; pCons; pCons=pCons->pLink){
12816 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
12818 for(pCons=pTail; pCons; pCons=pCons->pLink){
12819 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
12822 if( rc==SQLITE_OK ){
12823 /* Hash the list of columns to come up with a name for the index */
12824 const char *zTable = pScan->pTab->zName;
12825 int quoteTable = idxIdentifierRequiresQuotes(zTable);
12826 char *zName = 0; /* Index name */
12827 int collisions = 0;
12831 for(i=0; zCols[i]; i++){
12832 h += ((h<<3) + zCols[i]);
12834 sqlite3_free(zName);
12835 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
12836 if( zName==0 ) break;
12837 /* Is is unique among table, view and index names? */
12838 zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
12839 " AND type in ('index','table','view')";
12840 zFind = sqlite3_mprintf(zFmt, zName);
12842 rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
12843 assert(rc==SQLITE_OK);
12844 sqlite3_free(zFind);
12850 }while( collisions<50 && zName!=0 );
12852 /* This return means "Gave up trying to find a unique index name." */
12853 rc = SQLITE_BUSY_TIMEOUT;
12854 }else if( zName==0 ){
12858 zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
12860 zFmt = "CREATE INDEX %s ON %s(%s)";
12862 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
12866 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
12867 if( rc!=SQLITE_OK ){
12868 rc = SQLITE_BUSY_TIMEOUT;
12870 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
12873 sqlite3_free(zName);
12874 sqlite3_free(zIdx);
12878 sqlite3_free(zCols);
12884 ** Return true if list pList (linked by IdxConstraint.pLink) contains
12885 ** a constraint compatible with *p. Otherwise return false.
12887 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
12888 IdxConstraint *pCmp;
12889 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
12890 if( p->iCol==pCmp->iCol ) return 1;
12895 static int idxCreateFromWhere(
12897 IdxScan *pScan, /* Create indexes for this scan */
12898 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
12900 IdxConstraint *p1 = 0;
12901 IdxConstraint *pCon;
12904 /* Gather up all the == constraints. */
12905 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
12906 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
12912 /* Create an index using the == constraints collected above. And the
12913 ** range constraint/ORDER BY terms passed in by the caller, if any. */
12914 rc = idxCreateFromCons(p, pScan, p1, pTail);
12916 /* If no range/ORDER BY passed by the caller, create a version of the
12917 ** index for each range constraint. */
12919 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
12920 assert( pCon->pLink==0 );
12921 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
12922 rc = idxCreateFromCons(p, pScan, p1, pCon);
12931 ** Create candidate indexes in database [dbm] based on the data in
12932 ** linked-list pScan.
12934 static int idxCreateCandidates(sqlite3expert *p){
12935 int rc = SQLITE_OK;
12938 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
12939 rc = idxCreateFromWhere(p, pIter, 0);
12940 if( rc==SQLITE_OK && pIter->pOrder ){
12941 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
12949 ** Free all elements of the linked list starting at pConstraint.
12951 static void idxConstraintFree(IdxConstraint *pConstraint){
12952 IdxConstraint *pNext;
12955 for(p=pConstraint; p; p=pNext){
12962 ** Free all elements of the linked list starting from pScan up until pLast
12963 ** (pLast is not freed).
12965 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
12968 for(p=pScan; p!=pLast; p=pNext){
12969 pNext = p->pNextScan;
12970 idxConstraintFree(p->pOrder);
12971 idxConstraintFree(p->pEq);
12972 idxConstraintFree(p->pRange);
12978 ** Free all elements of the linked list starting from pStatement up
12979 ** until pLast (pLast is not freed).
12981 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
12983 IdxStatement *pNext;
12984 for(p=pStatement; p!=pLast; p=pNext){
12986 sqlite3_free(p->zEQP);
12987 sqlite3_free(p->zIdx);
12993 ** Free the linked list of IdxTable objects starting at pTab.
12995 static void idxTableFree(IdxTable *pTab){
12998 for(pIter=pTab; pIter; pIter=pNext){
12999 pNext = pIter->pNext;
13000 sqlite3_free(pIter);
13005 ** Free the linked list of IdxWrite objects starting at pTab.
13007 static void idxWriteFree(IdxWrite *pTab){
13010 for(pIter=pTab; pIter; pIter=pNext){
13011 pNext = pIter->pNext;
13012 sqlite3_free(pIter);
13019 ** This function is called after candidate indexes have been created. It
13020 ** runs all the queries to see which indexes they prefer, and populates
13021 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
13023 static int idxFindIndexes(
13025 char **pzErr /* OUT: Error message (sqlite3_malloc) */
13027 IdxStatement *pStmt;
13028 sqlite3 *dbm = p->dbm;
13029 int rc = SQLITE_OK;
13032 idxHashInit(&hIdx);
13034 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
13035 IdxHashEntry *pEntry;
13036 sqlite3_stmt *pExplain = 0;
13037 idxHashClear(&hIdx);
13038 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
13039 "EXPLAIN QUERY PLAN %s", pStmt->zSql
13041 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
13042 /* int iId = sqlite3_column_int(pExplain, 0); */
13043 /* int iParent = sqlite3_column_int(pExplain, 1); */
13044 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
13045 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
13049 if( !zDetail ) continue;
13050 nDetail = STRLEN(zDetail);
13052 for(i=0; i<nDetail; i++){
13053 const char *zIdx = 0;
13054 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
13055 zIdx = &zDetail[i+13];
13056 }else if( i+22<nDetail
13057 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
13059 zIdx = &zDetail[i+22];
13064 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
13067 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
13069 idxHashAdd(&rc, &hIdx, zSql, 0);
13070 if( rc ) goto find_indexes_out;
13076 if( zDetail[0]!='-' ){
13077 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
13081 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
13082 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
13085 idxFinalize(&rc, pExplain);
13089 idxHashClear(&hIdx);
13093 static int idxAuthCallback(
13099 const char *zTrigger
13101 int rc = SQLITE_OK;
13104 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
13105 if( sqlite3_stricmp(zDb, "main")==0 ){
13106 sqlite3expert *p = (sqlite3expert*)pCtx;
13108 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
13109 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
13113 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
13114 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
13117 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
13118 if( rc==SQLITE_OK ){
13119 pWrite->pTab = pTab;
13121 pWrite->pNext = p->pWrite;
13122 p->pWrite = pWrite;
13131 static int idxProcessOneTrigger(
13136 static const char *zInt = UNIQUE_TABLE_NAME;
13137 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
13138 IdxTable *pTab = pWrite->pTab;
13139 const char *zTab = pTab->zName;
13141 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
13142 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
13144 sqlite3_stmt *pSelect = 0;
13145 int rc = SQLITE_OK;
13148 /* Create the table and its triggers in the temp schema */
13149 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
13150 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
13151 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
13152 if( zCreate==0 ) continue;
13153 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
13155 idxFinalize(&rc, pSelect);
13157 /* Rename the table in the temp schema to zInt */
13158 if( rc==SQLITE_OK ){
13159 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
13163 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
13168 switch( pWrite->eOp ){
13169 case SQLITE_INSERT: {
13171 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
13172 for(i=0; i<pTab->nCol; i++){
13173 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
13175 zWrite = idxAppendText(&rc, zWrite, ")");
13178 case SQLITE_UPDATE: {
13180 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
13181 for(i=0; i<pTab->nCol; i++){
13182 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
13183 pTab->aCol[i].zName
13189 assert( pWrite->eOp==SQLITE_DELETE );
13190 if( rc==SQLITE_OK ){
13191 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
13192 if( zWrite==0 ) rc = SQLITE_NOMEM;
13197 if( rc==SQLITE_OK ){
13198 sqlite3_stmt *pX = 0;
13199 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
13200 idxFinalize(&rc, pX);
13201 if( rc!=SQLITE_OK ){
13202 idxDatabaseError(p->dbv, pzErr);
13205 sqlite3_free(zWrite);
13207 if( rc==SQLITE_OK ){
13208 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
13214 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
13215 int rc = SQLITE_OK;
13216 IdxWrite *pEnd = 0;
13217 IdxWrite *pFirst = p->pWrite;
13219 while( rc==SQLITE_OK && pFirst!=pEnd ){
13221 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
13222 rc = idxProcessOneTrigger(p, pIter, pzErr);
13225 pFirst = p->pWrite;
13232 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
13233 int rc = idxRegisterVtab(p);
13234 sqlite3_stmt *pSchema = 0;
13236 /* For each table in the main db schema:
13238 ** 1) Add an entry to the p->pTable list, and
13239 ** 2) Create the equivalent virtual table in dbv.
13241 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
13242 "SELECT type, name, sql, 1 FROM sqlite_schema "
13243 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
13245 "SELECT type, name, sql, 2 FROM sqlite_schema "
13246 "WHERE type = 'trigger'"
13247 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
13250 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
13251 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
13252 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
13253 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
13255 if( zType==0 || zName==0 ) continue;
13256 if( zType[0]=='v' || zType[1]=='r' ){
13257 if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
13260 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
13261 if( rc==SQLITE_OK ){
13265 pTab->pNext = p->pTable;
13268 /* The statement the vtab will pass to sqlite3_declare_vtab() */
13269 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
13270 for(i=0; i<pTab->nCol; i++){
13271 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
13272 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
13275 zInner = idxAppendText(&rc, zInner, ")");
13277 /* The CVT statement to create the vtab */
13278 zOuter = idxAppendText(&rc, 0,
13279 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
13281 if( rc==SQLITE_OK ){
13282 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
13284 sqlite3_free(zInner);
13285 sqlite3_free(zOuter);
13289 idxFinalize(&rc, pSchema);
13293 struct IdxSampleCtx {
13295 double target; /* Target nRet/nRow value */
13296 double nRow; /* Number of rows seen */
13297 double nRet; /* Number of rows returned */
13300 static void idxSampleFunc(
13301 sqlite3_context *pCtx,
13303 sqlite3_value **argv
13305 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
13310 if( p->nRow==0.0 ){
13313 bRet = (p->nRet / p->nRow) <= p->target;
13315 unsigned short rnd;
13316 sqlite3_randomness(2, (void*)&rnd);
13317 bRet = ((int)rnd % 100) <= p->iTarget;
13321 sqlite3_result_int(pCtx, bRet);
13323 p->nRet += (double)bRet;
13328 struct IdxRemSlot {
13329 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
13330 i64 iVal; /* SQLITE_INTEGER value */
13331 double rVal; /* SQLITE_FLOAT value */
13332 int nByte; /* Bytes of space allocated at z */
13333 int n; /* Size of buffer z */
13334 char *z; /* SQLITE_TEXT/BLOB value */
13339 ** Implementation of scalar function rem().
13341 static void idxRemFunc(
13342 sqlite3_context *pCtx,
13344 sqlite3_value **argv
13346 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
13347 struct IdxRemSlot *pSlot;
13351 iSlot = sqlite3_value_int(argv[0]);
13352 assert( iSlot<=p->nSlot );
13353 pSlot = &p->aSlot[iSlot];
13355 switch( pSlot->eType ){
13360 case SQLITE_INTEGER:
13361 sqlite3_result_int64(pCtx, pSlot->iVal);
13365 sqlite3_result_double(pCtx, pSlot->rVal);
13369 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
13373 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
13377 pSlot->eType = sqlite3_value_type(argv[1]);
13378 switch( pSlot->eType ){
13383 case SQLITE_INTEGER:
13384 pSlot->iVal = sqlite3_value_int64(argv[1]);
13388 pSlot->rVal = sqlite3_value_double(argv[1]);
13392 case SQLITE_TEXT: {
13393 int nByte = sqlite3_value_bytes(argv[1]);
13394 const void *pData = 0;
13395 if( nByte>pSlot->nByte ){
13396 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
13398 sqlite3_result_error_nomem(pCtx);
13401 pSlot->nByte = nByte*2;
13405 if( pSlot->eType==SQLITE_BLOB ){
13406 pData = sqlite3_value_blob(argv[1]);
13407 if( pData ) memcpy(pSlot->z, pData, nByte);
13409 pData = sqlite3_value_text(argv[1]);
13410 memcpy(pSlot->z, pData, nByte);
13417 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
13418 int rc = SQLITE_OK;
13420 "SELECT max(i.seqno) FROM "
13421 " sqlite_schema AS s, "
13422 " pragma_index_list(s.name) AS l, "
13423 " pragma_index_info(l.name) AS i "
13424 "WHERE s.type = 'table'";
13425 sqlite3_stmt *pMax = 0;
13428 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
13429 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
13430 *pnMax = sqlite3_column_int(pMax, 0) + 1;
13432 idxFinalize(&rc, pMax);
13437 static int idxPopulateOneStat1(
13439 sqlite3_stmt *pIndexXInfo,
13440 sqlite3_stmt *pWriteStat,
13450 sqlite3_stmt *pQuery = 0;
13452 int rc = SQLITE_OK;
13454 assert( p->iSample>0 );
13456 /* Formulate the query text */
13457 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
13458 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
13459 const char *zComma = zCols==0 ? "" : ", ";
13460 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
13461 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
13462 zCols = idxAppendText(&rc, zCols,
13463 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
13465 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
13467 sqlite3_reset(pIndexXInfo);
13468 if( rc==SQLITE_OK ){
13469 if( p->iSample==100 ){
13470 zQuery = sqlite3_mprintf(
13471 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
13474 zQuery = sqlite3_mprintf(
13475 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
13479 sqlite3_free(zCols);
13480 sqlite3_free(zOrder);
13482 /* Formulate the query text */
13483 if( rc==SQLITE_OK ){
13484 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
13485 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
13487 sqlite3_free(zQuery);
13489 if( rc==SQLITE_OK ){
13490 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
13492 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
13493 IdxHashEntry *pEntry;
13495 for(i=0; i<=nCol; i++) aStat[i] = 1;
13496 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
13498 for(i=0; i<nCol; i++){
13499 if( sqlite3_column_int(pQuery, i)==0 ) break;
13501 for(/*no-op*/; i<nCol; i++){
13506 if( rc==SQLITE_OK ){
13508 zStat = sqlite3_mprintf("%d", s0);
13509 if( zStat==0 ) rc = SQLITE_NOMEM;
13510 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
13511 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
13515 if( rc==SQLITE_OK ){
13516 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
13517 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
13518 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
13519 sqlite3_step(pWriteStat);
13520 rc = sqlite3_reset(pWriteStat);
13523 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
13525 assert( pEntry->zVal2==0 );
13526 pEntry->zVal2 = zStat;
13528 sqlite3_free(zStat);
13531 sqlite3_free(aStat);
13532 idxFinalize(&rc, pQuery);
13537 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
13541 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
13542 if( rc!=SQLITE_OK ) return rc;
13544 zSql = sqlite3_mprintf(
13545 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
13547 if( zSql==0 ) return SQLITE_NOMEM;
13548 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
13549 sqlite3_free(zSql);
13555 ** This function is called as part of sqlite3_expert_analyze(). Candidate
13556 ** indexes have already been created in database sqlite3expert.dbm, this
13557 ** function populates sqlite_stat1 table in the same database.
13559 ** The stat1 data is generated by querying the
13561 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
13562 int rc = SQLITE_OK;
13564 struct IdxRemCtx *pCtx = 0;
13565 struct IdxSampleCtx samplectx;
13567 i64 iPrev = -100000;
13568 sqlite3_stmt *pAllIndex = 0;
13569 sqlite3_stmt *pIndexXInfo = 0;
13570 sqlite3_stmt *pWrite = 0;
13572 const char *zAllIndex =
13573 "SELECT s.rowid, s.name, l.name FROM "
13574 " sqlite_schema AS s, "
13575 " pragma_index_list(s.name) AS l "
13576 "WHERE s.type = 'table'";
13577 const char *zIndexXInfo =
13578 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
13579 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
13581 /* If iSample==0, no sqlite_stat1 data is required. */
13582 if( p->iSample==0 ) return SQLITE_OK;
13584 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
13585 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
13587 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
13589 if( rc==SQLITE_OK ){
13590 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
13591 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
13594 if( rc==SQLITE_OK ){
13595 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
13596 rc = sqlite3_create_function(
13597 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
13600 if( rc==SQLITE_OK ){
13601 rc = sqlite3_create_function(
13602 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
13606 if( rc==SQLITE_OK ){
13607 pCtx->nSlot = nMax+1;
13608 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
13610 if( rc==SQLITE_OK ){
13611 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
13613 if( rc==SQLITE_OK ){
13614 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
13617 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
13618 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
13619 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
13620 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
13621 if( zTab==0 || zIdx==0 ) continue;
13622 if( p->iSample<100 && iPrev!=iRowid ){
13623 samplectx.target = (double)p->iSample / 100.0;
13624 samplectx.iTarget = p->iSample;
13625 samplectx.nRow = 0.0;
13626 samplectx.nRet = 0.0;
13627 rc = idxBuildSampleTable(p, zTab);
13628 if( rc!=SQLITE_OK ) break;
13630 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
13633 if( rc==SQLITE_OK && p->iSample<100 ){
13634 rc = sqlite3_exec(p->dbv,
13635 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
13639 idxFinalize(&rc, pAllIndex);
13640 idxFinalize(&rc, pIndexXInfo);
13641 idxFinalize(&rc, pWrite);
13644 for(i=0; i<pCtx->nSlot; i++){
13645 sqlite3_free(pCtx->aSlot[i].z);
13647 sqlite3_free(pCtx);
13650 if( rc==SQLITE_OK ){
13651 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
13654 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
13659 ** Define and possibly pretend to use a useless collation sequence.
13660 ** This pretense allows expert to accept SQL using custom collations.
13662 int dummyCompare(void *up1, int up2, const void *up3, int up4, const void *up5){
13668 assert(0); /* VDBE should never be run. */
13671 /* And a callback to register above upon actual need */
13672 void useDummyCS(void *up1, sqlite3 *db, int etr, const char *zName){
13674 sqlite3_create_collation_v2(db, zName, etr, 0, dummyCompare, 0);
13677 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
13678 && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
13680 ** dummy functions for no-op implementation of UDFs during expert's work
13682 void dummyUDF(sqlite3_context *up1, int up2, sqlite3_value **up3){
13686 assert(0); /* VDBE should never be run. */
13688 void dummyUDFvalue(sqlite3_context *up1){
13690 assert(0); /* VDBE should never be run. */
13694 ** Register UDFs from user database with another.
13696 int registerUDFs(sqlite3 *dbSrc, sqlite3 *dbDst){
13697 sqlite3_stmt *pStmt;
13698 int rc = sqlite3_prepare_v2(dbSrc,
13699 "SELECT name,type,enc,narg,flags "
13700 "FROM pragma_function_list() "
13701 "WHERE builtin==0", -1, &pStmt, 0);
13702 if( rc==SQLITE_OK ){
13703 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
13704 int nargs = sqlite3_column_int(pStmt,3);
13705 int flags = sqlite3_column_int(pStmt,4);
13706 const char *name = (char*)sqlite3_column_text(pStmt,0);
13707 const char *type = (char*)sqlite3_column_text(pStmt,1);
13708 const char *enc = (char*)sqlite3_column_text(pStmt,2);
13709 if( name==0 || type==0 || enc==0 ){
13710 /* no-op. Only happens on OOM */
13712 int ienc = SQLITE_UTF8;
13713 int rcf = SQLITE_ERROR;
13714 if( strcmp(enc,"utf16le")==0 ) ienc = SQLITE_UTF16LE;
13715 else if( strcmp(enc,"utf16be")==0 ) ienc = SQLITE_UTF16BE;
13716 ienc |= (flags & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY));
13717 if( strcmp(type,"w")==0 ){
13718 rcf = sqlite3_create_window_function(dbDst,name,nargs,ienc,0,
13719 dummyUDF,dummyUDFvalue,0,0,0);
13720 }else if( strcmp(type,"a")==0 ){
13721 rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
13722 0,dummyUDF,dummyUDFvalue);
13723 }else if( strcmp(type,"s")==0 ){
13724 rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
13727 if( rcf!=SQLITE_OK ){
13733 sqlite3_finalize(pStmt);
13734 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
13741 ** Allocate a new sqlite3expert object.
13743 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
13744 int rc = SQLITE_OK;
13745 sqlite3expert *pNew;
13747 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
13749 /* Open two in-memory databases to work with. The "vtab database" (dbv)
13750 ** will contain a virtual table corresponding to each real table in
13751 ** the user database schema, and a copy of each view. It is used to
13752 ** collect information regarding the WHERE, ORDER BY and other clauses
13753 ** of the user's query.
13755 if( rc==SQLITE_OK ){
13757 pNew->iSample = 100;
13758 rc = sqlite3_open(":memory:", &pNew->dbv);
13760 if( rc==SQLITE_OK ){
13761 rc = sqlite3_open(":memory:", &pNew->dbm);
13762 if( rc==SQLITE_OK ){
13763 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
13767 /* Allow custom collations to be dealt with through prepare. */
13768 if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS);
13769 if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS);
13771 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
13772 && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
13773 /* Register UDFs from database [db] with [dbm] and [dbv]. */
13774 if( rc==SQLITE_OK ){
13775 rc = registerUDFs(pNew->db, pNew->dbm);
13777 if( rc==SQLITE_OK ){
13778 rc = registerUDFs(pNew->db, pNew->dbv);
13782 /* Copy the entire schema of database [db] into [dbm]. */
13783 if( rc==SQLITE_OK ){
13784 sqlite3_stmt *pSql = 0;
13785 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
13786 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
13787 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
13789 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13790 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
13791 if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
13793 idxFinalize(&rc, pSql);
13796 /* Create the vtab schema */
13797 if( rc==SQLITE_OK ){
13798 rc = idxCreateVtabSchema(pNew, pzErrmsg);
13801 /* Register the auth callback with dbv */
13802 if( rc==SQLITE_OK ){
13803 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
13806 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
13807 ** return the new sqlite3expert handle. */
13808 if( rc!=SQLITE_OK ){
13809 sqlite3_expert_destroy(pNew);
13816 ** Configure an sqlite3expert object.
13818 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
13819 int rc = SQLITE_OK;
13823 case EXPERT_CONFIG_SAMPLE: {
13824 int iVal = va_arg(ap, int);
13825 if( iVal<0 ) iVal = 0;
13826 if( iVal>100 ) iVal = 100;
13831 rc = SQLITE_NOTFOUND;
13840 ** Add an SQL statement to the analysis.
13842 int sqlite3_expert_sql(
13843 sqlite3expert *p, /* From sqlite3_expert_new() */
13844 const char *zSql, /* SQL statement to add */
13845 char **pzErr /* OUT: Error message (if any) */
13847 IdxScan *pScanOrig = p->pScan;
13848 IdxStatement *pStmtOrig = p->pStatement;
13849 int rc = SQLITE_OK;
13850 const char *zStmt = zSql;
13852 if( p->bRun ) return SQLITE_MISUSE;
13854 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
13855 sqlite3_stmt *pStmt = 0;
13856 /* Ensure that the provided statement compiles against user's DB. */
13857 rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt);
13858 if( rc!=SQLITE_OK ) break;
13859 sqlite3_finalize(pStmt);
13860 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
13861 if( rc==SQLITE_OK ){
13863 IdxStatement *pNew;
13864 const char *z = sqlite3_sql(pStmt);
13866 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
13867 if( rc==SQLITE_OK ){
13868 pNew->zSql = (char*)&pNew[1];
13869 memcpy(pNew->zSql, z, n+1);
13870 pNew->pNext = p->pStatement;
13871 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
13872 p->pStatement = pNew;
13874 sqlite3_finalize(pStmt);
13877 idxDatabaseError(p->dbv, pzErr);
13881 if( rc!=SQLITE_OK ){
13882 idxScanFree(p->pScan, pScanOrig);
13883 idxStatementFree(p->pStatement, pStmtOrig);
13884 p->pScan = pScanOrig;
13885 p->pStatement = pStmtOrig;
13891 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
13893 IdxHashEntry *pEntry;
13895 /* Do trigger processing to collect any extra IdxScan structures */
13896 rc = idxProcessTriggers(p, pzErr);
13898 /* Create candidate indexes within the in-memory database file */
13899 if( rc==SQLITE_OK ){
13900 rc = idxCreateCandidates(p);
13901 }else if ( rc==SQLITE_BUSY_TIMEOUT ){
13903 *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
13907 /* Generate the stat1 data */
13908 if( rc==SQLITE_OK ){
13909 rc = idxPopulateStat1(p, pzErr);
13912 /* Formulate the EXPERT_REPORT_CANDIDATES text */
13913 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
13914 p->zCandidates = idxAppendText(&rc, p->zCandidates,
13915 "%s;%s%s\n", pEntry->zVal,
13916 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
13920 /* Figure out which of the candidate indexes are preferred by the query
13921 ** planner and report the results to the user. */
13922 if( rc==SQLITE_OK ){
13923 rc = idxFindIndexes(p, pzErr);
13926 if( rc==SQLITE_OK ){
13933 ** Return the total number of statements that have been added to this
13934 ** sqlite3expert using sqlite3_expert_sql().
13936 int sqlite3_expert_count(sqlite3expert *p){
13938 if( p->pStatement ) nRet = p->pStatement->iId+1;
13943 ** Return a component of the report.
13945 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
13946 const char *zRet = 0;
13947 IdxStatement *pStmt;
13949 if( p->bRun==0 ) return 0;
13950 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
13952 case EXPERT_REPORT_SQL:
13953 if( pStmt ) zRet = pStmt->zSql;
13955 case EXPERT_REPORT_INDEXES:
13956 if( pStmt ) zRet = pStmt->zIdx;
13958 case EXPERT_REPORT_PLAN:
13959 if( pStmt ) zRet = pStmt->zEQP;
13961 case EXPERT_REPORT_CANDIDATES:
13962 zRet = p->zCandidates;
13969 ** Free an sqlite3expert object.
13971 void sqlite3_expert_destroy(sqlite3expert *p){
13973 sqlite3_close(p->dbm);
13974 sqlite3_close(p->dbv);
13975 idxScanFree(p->pScan, 0);
13976 idxStatementFree(p->pStatement, 0);
13977 idxTableFree(p->pTable);
13978 idxWriteFree(p->pWrite);
13979 idxHashClear(&p->hIdx);
13980 sqlite3_free(p->zCandidates);
13985 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13987 /************************* End ../ext/expert/sqlite3expert.c ********************/
13989 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
13990 #define SQLITE_SHELL_HAVE_RECOVER 1
13992 #define SQLITE_SHELL_HAVE_RECOVER 0
13994 #if SQLITE_SHELL_HAVE_RECOVER
13995 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
13999 ** The author disclaims copyright to this source code. In place of
14000 ** a legal notice, here is a blessing:
14002 ** May you do good and not evil.
14003 ** May you find forgiveness for yourself and forgive others.
14004 ** May you share freely, never taking more than you give.
14006 *************************************************************************
14008 ** This file contains the public interface to the "recover" extension -
14009 ** an SQLite extension designed to recover data from corrupted database
14016 ** To use the API to recover data from a corrupted database, an
14019 ** 1) Creates an sqlite3_recover handle by calling either
14020 ** sqlite3_recover_init() or sqlite3_recover_init_sql().
14022 ** 2) Configures the new handle using one or more calls to
14023 ** sqlite3_recover_config().
14025 ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
14026 ** the handle until it returns something other than SQLITE_OK. If it
14027 ** returns SQLITE_DONE, then the recovery operation completed without
14028 ** error. If it returns some other non-SQLITE_OK value, then an error
14031 ** 4) Retrieves any error code and English language error message using the
14032 ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
14035 ** 5) Destroys the sqlite3_recover handle and frees all resources
14036 ** using sqlite3_recover_finish().
14038 ** The application may abandon the recovery operation at any point
14039 ** before it is finished by passing the sqlite3_recover handle to
14040 ** sqlite3_recover_finish(). This is not an error, but the final state
14041 ** of the output database, or the results of running the partial script
14042 ** delivered to the SQL callback, are undefined.
14045 #ifndef _SQLITE_RECOVER_H
14046 #define _SQLITE_RECOVER_H
14048 /* #include "sqlite3.h" */
14055 ** An instance of the sqlite3_recover object represents a recovery
14056 ** operation in progress.
14060 ** sqlite3_recover_init()
14061 ** sqlite3_recover_init_sql()
14065 ** sqlite3_recover_finish()
14069 ** sqlite3_recover_config()
14070 ** sqlite3_recover_errcode()
14071 ** sqlite3_recover_errmsg()
14072 ** sqlite3_recover_run()
14073 ** sqlite3_recover_step()
14075 typedef struct sqlite3_recover sqlite3_recover;
14078 ** These two APIs attempt to create and return a new sqlite3_recover object.
14079 ** In both cases the first two arguments identify the (possibly
14080 ** corrupt) database to recover data from. The first argument is an open
14081 ** database handle and the second the name of a database attached to that
14082 ** handle (i.e. "main", "temp" or the name of an attached database).
14084 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
14085 ** handle, then data is recovered into a new database, identified by
14086 ** string parameter zUri. zUri may be an absolute or relative file path,
14087 ** or may be an SQLite URI. If the identified database file already exists,
14088 ** it is overwritten.
14090 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
14091 ** be returned to the user as a series of SQL statements. Executing these
14092 ** SQL statements results in the same database as would have been created
14093 ** had sqlite3_recover_init() been used. For each SQL statement in the
14094 ** output, the callback function passed as the third argument (xSql) is
14095 ** invoked once. The first parameter is a passed a copy of the fourth argument
14096 ** to this function (pCtx) as its first parameter, and a pointer to a
14097 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
14098 ** the second. If the xSql callback returns any value other than SQLITE_OK,
14099 ** then processing is immediately abandoned and the value returned used as
14100 ** the recover handle error code (see below).
14102 ** If an out-of-memory error occurs, NULL may be returned instead of
14103 ** a valid handle. In all other cases, it is the responsibility of the
14104 ** application to avoid resource leaks by ensuring that
14105 ** sqlite3_recover_finish() is called on all allocated handles.
14107 sqlite3_recover *sqlite3_recover_init(
14112 sqlite3_recover *sqlite3_recover_init_sql(
14115 int (*xSql)(void*, const char*),
14120 ** Configure an sqlite3_recover object that has just been created using
14121 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
14122 ** may only be called before the first call to sqlite3_recover_step()
14123 ** or sqlite3_recover_run() on the object.
14125 ** The second argument passed to this function must be one of the
14126 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
14127 ** depend on the specific SQLITE_RECOVER_* symbol in use.
14129 ** SQLITE_OK is returned if the configuration operation was successful,
14130 ** or an SQLite error code otherwise.
14132 int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
14135 ** SQLITE_RECOVER_LOST_AND_FOUND:
14136 ** The pArg argument points to a string buffer containing the name
14137 ** of a "lost-and-found" table in the output database, or NULL. If
14138 ** the argument is non-NULL and the database contains seemingly
14139 ** valid pages that cannot be associated with any table in the
14140 ** recovered part of the schema, data is extracted from these
14141 ** pages to add to the lost-and-found table.
14143 ** SQLITE_RECOVER_FREELIST_CORRUPT:
14144 ** The pArg value must actually be a pointer to a value of type
14145 ** int containing value 0 or 1 cast as a (void*). If this option is set
14146 ** (argument is 1) and a lost-and-found table has been configured using
14147 ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
14148 ** corrupt and an attempt is made to recover records from pages that
14149 ** appear to be linked into the freelist. Otherwise, pages on the freelist
14150 ** are ignored. Setting this option can recover more data from the
14151 ** database, but often ends up "recovering" deleted records. The default
14152 ** value is 0 (clear).
14154 ** SQLITE_RECOVER_ROWIDS:
14155 ** The pArg value must actually be a pointer to a value of type
14156 ** int containing value 0 or 1 cast as a (void*). If this option is set
14157 ** (argument is 1), then an attempt is made to recover rowid values
14158 ** that are not also INTEGER PRIMARY KEY values. If this option is
14159 ** clear, then new rowids are assigned to all recovered rows. The
14160 ** default value is 1 (set).
14162 ** SQLITE_RECOVER_SLOWINDEXES:
14163 ** The pArg value must actually be a pointer to a value of type
14164 ** int containing value 0 or 1 cast as a (void*). If this option is clear
14165 ** (argument is 0), then when creating an output database, the recover
14166 ** module creates and populates non-UNIQUE indexes right at the end of the
14167 ** recovery operation - after all recoverable data has been inserted
14168 ** into the new database. This is faster overall, but means that the
14169 ** final call to sqlite3_recover_step() for a recovery operation may
14170 ** be need to create a large number of indexes, which may be very slow.
14172 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
14173 ** are created in the output database before it is populated with
14174 ** recovered data. This is slower overall, but avoids the slow call
14175 ** to sqlite3_recover_step() at the end of the recovery operation.
14177 ** The default option value is 0.
14179 #define SQLITE_RECOVER_LOST_AND_FOUND 1
14180 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
14181 #define SQLITE_RECOVER_ROWIDS 3
14182 #define SQLITE_RECOVER_SLOWINDEXES 4
14185 ** Perform a unit of work towards the recovery operation. This function
14186 ** must normally be called multiple times to complete database recovery.
14188 ** If no error occurs but the recovery operation is not completed, this
14189 ** function returns SQLITE_OK. If recovery has been completed successfully
14190 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
14191 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
14192 ** considered an error if some or all of the data cannot be recovered
14193 ** due to database corruption.
14195 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
14196 ** all further such calls on the same recover handle are no-ops that return
14197 ** the same non-SQLITE_OK value.
14199 int sqlite3_recover_step(sqlite3_recover*);
14202 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
14203 ** or an SQLite error code otherwise. Calling this function is the same
14206 ** while( SQLITE_OK==sqlite3_recover_step(p) );
14207 ** return sqlite3_recover_errcode(p);
14209 int sqlite3_recover_run(sqlite3_recover*);
14212 ** If an error has been encountered during a prior call to
14213 ** sqlite3_recover_step(), then this function attempts to return a
14214 ** pointer to a buffer containing an English language explanation of
14215 ** the error. If no error message is available, or if an out-of memory
14216 ** error occurs while attempting to allocate a buffer in which to format
14217 ** the error message, NULL is returned.
14219 ** The returned buffer remains valid until the sqlite3_recover handle is
14220 ** destroyed using sqlite3_recover_finish().
14222 const char *sqlite3_recover_errmsg(sqlite3_recover*);
14225 ** If this function is called on an sqlite3_recover handle after
14226 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
14228 int sqlite3_recover_errcode(sqlite3_recover*);
14231 ** Clean up a recovery object created by a call to sqlite3_recover_init().
14232 ** The results of using a recovery object with any API after it has been
14233 ** passed to this function are undefined.
14235 ** This function returns the same value as sqlite3_recover_errcode().
14237 int sqlite3_recover_finish(sqlite3_recover*);
14241 } /* end of the 'extern "C"' block */
14244 #endif /* ifndef _SQLITE_RECOVER_H */
14246 /************************* End ../ext/recover/sqlite3recover.h ********************/
14247 # ifndef SQLITE_HAVE_SQLITE3R
14248 /************************* Begin ../ext/recover/dbdata.c ******************/
14252 ** The author disclaims copyright to this source code. In place of
14253 ** a legal notice, here is a blessing:
14255 ** May you do good and not evil.
14256 ** May you find forgiveness for yourself and forgive others.
14257 ** May you share freely, never taking more than you give.
14259 ******************************************************************************
14261 ** This file contains an implementation of two eponymous virtual tables,
14262 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
14263 ** "sqlite_dbpage" eponymous virtual table be available.
14266 ** sqlite_dbdata is used to extract data directly from a database b-tree
14267 ** page and its associated overflow pages, bypassing the b-tree layer.
14268 ** The table schema is equivalent to:
14270 ** CREATE TABLE sqlite_dbdata(
14275 ** schema TEXT HIDDEN
14278 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
14279 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
14282 ** Each page of the database is inspected. If it cannot be interpreted as
14283 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
14284 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
14285 ** table contains one row for each field in the record associated with
14286 ** each cell on the page. For intkey b-trees, the key value is stored in
14289 ** For example, for the database:
14291 ** CREATE TABLE t1(a, b); -- root page is page 2
14292 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
14293 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
14295 ** the sqlite_dbdata table contains, as well as from entries related to
14296 ** page 1, content equivalent to:
14298 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
14300 ** (2, 0, 0, 'v' ),
14301 ** (2, 0, 1, 'five'),
14302 ** (2, 1, -1, 10 ),
14303 ** (2, 1, 0, 'x' ),
14304 ** (2, 1, 1, 'ten' );
14306 ** If database corruption is encountered, this module does not report an
14307 ** error. Instead, it attempts to extract as much data as possible and
14308 ** ignores the corruption.
14311 ** The sqlite_dbptr table has the following schema:
14313 ** CREATE TABLE sqlite_dbptr(
14316 ** schema TEXT HIDDEN
14319 ** It contains one entry for each b-tree pointer between a parent and
14320 ** child page in the database.
14323 #if !defined(SQLITEINT_H)
14324 /* #include "sqlite3.h" */
14326 /* typedef unsigned char u8; */
14327 /* typedef unsigned int u32; */
14330 #include <string.h>
14331 #include <assert.h>
14333 #ifndef SQLITE_OMIT_VIRTUALTABLE
14335 #define DBDATA_PADDING_BYTES 100
14337 typedef struct DbdataTable DbdataTable;
14338 typedef struct DbdataCursor DbdataCursor;
14340 /* Cursor object */
14341 struct DbdataCursor {
14342 sqlite3_vtab_cursor base; /* Base class. Must be first */
14343 sqlite3_stmt *pStmt; /* For fetching database pages */
14345 int iPgno; /* Current page number */
14346 u8 *aPage; /* Buffer containing page */
14347 int nPage; /* Size of aPage[] in bytes */
14348 int nCell; /* Number of cells on aPage[] */
14349 int iCell; /* Current cell number */
14350 int bOnePage; /* True to stop after one page */
14352 sqlite3_int64 iRowid;
14354 /* Only for the sqlite_dbdata table */
14355 u8 *pRec; /* Buffer containing current record */
14356 sqlite3_int64 nRec; /* Size of pRec[] in bytes */
14357 sqlite3_int64 nHdr; /* Size of header in bytes */
14358 int iField; /* Current field number */
14361 u32 enc; /* Text encoding */
14363 sqlite3_int64 iIntkey; /* Integer key value */
14367 struct DbdataTable {
14368 sqlite3_vtab base; /* Base class. Must be first */
14369 sqlite3 *db; /* The database connection */
14370 sqlite3_stmt *pStmt; /* For fetching database pages */
14371 int bPtr; /* True for sqlite3_dbptr table */
14374 /* Column and schema definitions for sqlite_dbdata */
14375 #define DBDATA_COLUMN_PGNO 0
14376 #define DBDATA_COLUMN_CELL 1
14377 #define DBDATA_COLUMN_FIELD 2
14378 #define DBDATA_COLUMN_VALUE 3
14379 #define DBDATA_COLUMN_SCHEMA 4
14380 #define DBDATA_SCHEMA \
14381 "CREATE TABLE x(" \
14384 " field INTEGER," \
14386 " schema TEXT HIDDEN" \
14389 /* Column and schema definitions for sqlite_dbptr */
14390 #define DBPTR_COLUMN_PGNO 0
14391 #define DBPTR_COLUMN_CHILD 1
14392 #define DBPTR_COLUMN_SCHEMA 2
14393 #define DBPTR_SCHEMA \
14394 "CREATE TABLE x(" \
14396 " child INTEGER," \
14397 " schema TEXT HIDDEN" \
14401 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
14404 static int dbdataConnect(
14407 int argc, const char *const*argv,
14408 sqlite3_vtab **ppVtab,
14411 DbdataTable *pTab = 0;
14412 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
14417 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS);
14418 if( rc==SQLITE_OK ){
14419 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
14423 memset(pTab, 0, sizeof(DbdataTable));
14425 pTab->bPtr = (pAux!=0);
14429 *ppVtab = (sqlite3_vtab*)pTab;
14434 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
14436 static int dbdataDisconnect(sqlite3_vtab *pVtab){
14437 DbdataTable *pTab = (DbdataTable*)pVtab;
14439 sqlite3_finalize(pTab->pStmt);
14440 sqlite3_free(pVtab);
14446 ** This function interprets two types of constraints:
14451 ** If neither are present, idxNum is set to 0. If schema=? is present,
14452 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
14453 ** in idxNum is set.
14455 ** If both parameters are present, schema is in position 0 and pgno in
14458 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
14459 DbdataTable *pTab = (DbdataTable*)tab;
14463 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
14465 for(i=0; i<pIdx->nConstraint; i++){
14466 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
14467 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
14468 if( p->iColumn==colSchema ){
14469 if( p->usable==0 ) return SQLITE_CONSTRAINT;
14472 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
14479 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
14480 pIdx->aConstraintUsage[iSchema].omit = 1;
14483 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
14484 pIdx->aConstraintUsage[iPgno].omit = 1;
14485 pIdx->estimatedCost = 100;
14486 pIdx->estimatedRows = 50;
14488 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
14489 int iCol = pIdx->aOrderBy[0].iColumn;
14490 if( pIdx->nOrderBy==1 ){
14491 pIdx->orderByConsumed = (iCol==0 || iCol==1);
14492 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
14493 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
14498 pIdx->estimatedCost = 100000000;
14499 pIdx->estimatedRows = 1000000000;
14501 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
14506 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
14508 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
14509 DbdataCursor *pCsr;
14511 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
14513 return SQLITE_NOMEM;
14515 memset(pCsr, 0, sizeof(DbdataCursor));
14516 pCsr->base.pVtab = pVTab;
14519 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
14524 ** Restore a cursor object to the state it was in when first allocated
14525 ** by dbdataOpen().
14527 static void dbdataResetCursor(DbdataCursor *pCsr){
14528 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
14529 if( pTab->pStmt==0 ){
14530 pTab->pStmt = pCsr->pStmt;
14532 sqlite3_finalize(pCsr->pStmt);
14538 pCsr->bOnePage = 0;
14539 sqlite3_free(pCsr->aPage);
14540 sqlite3_free(pCsr->pRec);
14546 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
14548 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
14549 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14550 dbdataResetCursor(pCsr);
14551 sqlite3_free(pCsr);
14556 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
14558 static u32 get_uint16(unsigned char *a){
14559 return (a[0]<<8)|a[1];
14561 static u32 get_uint32(unsigned char *a){
14562 return ((u32)a[0]<<24)
14569 ** Load page pgno from the database via the sqlite_dbpage virtual table.
14570 ** If successful, set (*ppPage) to point to a buffer containing the page
14571 ** data, (*pnPage) to the size of that buffer in bytes and return
14572 ** SQLITE_OK. In this case it is the responsibility of the caller to
14573 ** eventually free the buffer using sqlite3_free().
14575 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
14576 ** return an SQLite error code.
14578 static int dbdataLoadPage(
14579 DbdataCursor *pCsr, /* Cursor object */
14580 u32 pgno, /* Page number of page to load */
14581 u8 **ppPage, /* OUT: pointer to page buffer */
14582 int *pnPage /* OUT: Size of (*ppPage) in bytes */
14585 int rc = SQLITE_OK;
14586 sqlite3_stmt *pStmt = pCsr->pStmt;
14591 sqlite3_bind_int64(pStmt, 2, pgno);
14592 if( SQLITE_ROW==sqlite3_step(pStmt) ){
14593 int nCopy = sqlite3_column_bytes(pStmt, 0);
14596 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
14600 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
14601 memcpy(pPage, pCopy, nCopy);
14602 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
14608 rc2 = sqlite3_reset(pStmt);
14609 if( rc==SQLITE_OK ) rc = rc2;
14616 ** Read a varint. Put the value in *pVal and return the number of bytes.
14618 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
14619 sqlite3_uint64 u = 0;
14621 for(i=0; i<8; i++){
14622 u = (u<<7) + (z[i]&0x7f);
14623 if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
14625 u = (u<<8) + (z[i]&0xff);
14626 *pVal = (sqlite3_int64)u;
14631 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
14632 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
14633 ** SQLite database except for key values in intkey tables.
14635 static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
14637 int nRet = dbdataGetVarint(z, &val);
14638 if( val<0 || val>0xFFFFFFFF ) val = 0;
14644 ** Return the number of bytes of space used by an SQLite value of type
14647 static int dbdataValueBytes(int eType){
14649 case 0: case 8: case 9:
14667 return ((eType-12) / 2);
14674 ** Load a value of type eType from buffer pData and use it to set the
14675 ** result of context object pCtx.
14677 static void dbdataValue(
14678 sqlite3_context *pCtx,
14682 sqlite3_int64 nData
14684 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
14689 sqlite3_result_null(pCtx);
14693 sqlite3_result_int(pCtx, 0);
14696 sqlite3_result_int(pCtx, 1);
14699 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
14700 sqlite3_uint64 v = (signed char)pData[0];
14704 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
14705 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
14706 case 4: v = (v<<8) + pData[0]; pData++;
14707 case 3: v = (v<<8) + pData[0]; pData++;
14708 case 2: v = (v<<8) + pData[0]; pData++;
14713 memcpy(&r, &v, sizeof(r));
14714 sqlite3_result_double(pCtx, r);
14716 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
14722 int n = ((eType-12) / 2);
14725 #ifndef SQLITE_OMIT_UTF16
14726 case SQLITE_UTF16BE:
14727 sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
14729 case SQLITE_UTF16LE:
14730 sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
14734 sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
14738 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
14746 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
14748 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
14749 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14750 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
14755 int iOff = (pCsr->iPgno==1 ? 100 : 0);
14758 if( pCsr->aPage==0 ){
14760 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
14761 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
14762 if( rc!=SQLITE_OK ) return rc;
14763 if( pCsr->aPage && pCsr->nPage>=256 ) break;
14764 sqlite3_free(pCsr->aPage);
14766 if( pCsr->bOnePage ) return SQLITE_OK;
14770 assert( iOff+3+2<=pCsr->nPage );
14771 pCsr->iCell = pTab->bPtr ? -2 : 0;
14772 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
14776 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
14777 pCsr->iCell = pCsr->nCell;
14780 if( pCsr->iCell>=pCsr->nCell ){
14781 sqlite3_free(pCsr->aPage);
14783 if( pCsr->bOnePage ) return SQLITE_OK;
14789 /* If there is no record loaded, load it now. */
14790 if( pCsr->pRec==0 ){
14793 sqlite3_int64 nPayload = 0;
14794 sqlite3_int64 nHdr = 0;
14799 switch( pCsr->aPage[iOff] ){
14809 /* This is not a b-tree page with records on it. Continue. */
14810 pCsr->iCell = pCsr->nCell;
14814 if( pCsr->iCell>=pCsr->nCell ){
14818 iOff += 8 + nPointer + pCsr->iCell*2;
14819 if( iOff>pCsr->nPage ){
14822 iOff = get_uint16(&pCsr->aPage[iOff]);
14825 /* For an interior node cell, skip past the child-page number */
14828 /* Load the "byte of payload including overflow" field */
14829 if( bNextPage || iOff>pCsr->nPage ){
14832 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
14833 if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
14836 /* If this is a leaf intkey cell, load the rowid */
14837 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
14838 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
14841 /* Figure out how much data to read from the local page */
14846 X = ((U-12)*64/255)-23;
14852 M = ((U-12)*32/255)-23;
14853 K = M+((nPayload-M)%(U-4));
14861 if( bNextPage || nLocal+iOff>pCsr->nPage ){
14865 /* Allocate space for payload. And a bit more to catch small buffer
14866 ** overruns caused by attempting to read a varint or similar from
14867 ** near the end of a corrupt record. */
14868 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
14869 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
14870 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
14871 pCsr->nRec = nPayload;
14873 /* Load the nLocal bytes of payload */
14874 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
14877 /* Load content from overflow pages */
14878 if( nPayload>nLocal ){
14879 sqlite3_int64 nRem = nPayload - nLocal;
14880 u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
14885 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
14886 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
14887 if( rc!=SQLITE_OK ) return rc;
14888 if( aOvfl==0 ) break;
14891 if( nCopy>nRem ) nCopy = nRem;
14892 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
14895 pgnoOvfl = get_uint32(aOvfl);
14896 sqlite3_free(aOvfl);
14900 iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
14901 if( nHdr>nPayload ) nHdr = 0;
14903 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
14904 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
14905 pCsr->iField = (bHasRowid ? -1 : 0);
14910 if( pCsr->iField>0 ){
14911 sqlite3_int64 iType;
14912 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
14916 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
14917 szField = dbdataValueBytes(iType);
14918 if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){
14919 pCsr->pPtr = &pCsr->pRec[pCsr->nRec];
14921 pCsr->pPtr += szField;
14928 sqlite3_free(pCsr->aPage);
14929 sqlite3_free(pCsr->pRec);
14932 if( pCsr->bOnePage ) return SQLITE_OK;
14935 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
14939 /* Advance to the next cell. The next iteration of the loop will load
14940 ** the record and so on. */
14941 sqlite3_free(pCsr->pRec);
14948 assert( !"can't get here" );
14953 ** Return true if the cursor is at EOF.
14955 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
14956 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14957 return pCsr->aPage==0;
14961 ** Return true if nul-terminated string zSchema ends in "()". Or false
14964 static int dbdataIsFunction(const char *zSchema){
14965 size_t n = strlen(zSchema);
14966 if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
14973 ** Determine the size in pages of database zSchema (where zSchema is
14974 ** "main", "temp" or the name of an attached database) and set
14975 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
14976 ** an SQLite error code.
14978 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
14979 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
14983 sqlite3_stmt *pStmt = 0;
14985 if( (nFunc = dbdataIsFunction(zSchema))>0 ){
14986 zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
14988 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
14990 if( zSql==0 ) return SQLITE_NOMEM;
14992 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
14993 sqlite3_free(zSql);
14994 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14995 pCsr->szDb = sqlite3_column_int(pStmt, 0);
14997 rc2 = sqlite3_finalize(pStmt);
14998 if( rc==SQLITE_OK ) rc = rc2;
15003 ** Attempt to figure out the encoding of the database by retrieving page 1
15004 ** and inspecting the header field. If successful, set the pCsr->enc variable
15005 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
15007 static int dbdataGetEncoding(DbdataCursor *pCsr){
15008 int rc = SQLITE_OK;
15011 rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
15012 if( rc==SQLITE_OK && nPg1>=(56+4) ){
15013 pCsr->enc = get_uint32(&aPg1[56]);
15015 sqlite3_free(aPg1);
15021 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
15023 static int dbdataFilter(
15024 sqlite3_vtab_cursor *pCursor,
15025 int idxNum, const char *idxStr,
15026 int argc, sqlite3_value **argv
15028 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15029 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
15030 int rc = SQLITE_OK;
15031 const char *zSchema = "main";
15035 dbdataResetCursor(pCsr);
15036 assert( pCsr->iPgno==1 );
15037 if( idxNum & 0x01 ){
15038 zSchema = (const char*)sqlite3_value_text(argv[0]);
15039 if( zSchema==0 ) zSchema = "";
15041 if( idxNum & 0x02 ){
15042 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
15043 pCsr->bOnePage = 1;
15045 rc = dbdataDbsize(pCsr, zSchema);
15048 if( rc==SQLITE_OK ){
15051 pCsr->pStmt = pTab->pStmt;
15053 }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
15054 char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
15058 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
15059 sqlite3_free(zSql);
15062 rc = sqlite3_prepare_v2(pTab->db,
15063 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
15068 if( rc==SQLITE_OK ){
15069 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
15072 /* Try to determine the encoding of the db by inspecting the header
15073 ** field on page 1. */
15074 if( rc==SQLITE_OK ){
15075 rc = dbdataGetEncoding(pCsr);
15078 if( rc!=SQLITE_OK ){
15079 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
15082 if( rc==SQLITE_OK ){
15083 rc = dbdataNext(pCursor);
15089 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
15091 static int dbdataColumn(
15092 sqlite3_vtab_cursor *pCursor,
15093 sqlite3_context *ctx,
15096 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15097 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
15100 case DBPTR_COLUMN_PGNO:
15101 sqlite3_result_int64(ctx, pCsr->iPgno);
15103 case DBPTR_COLUMN_CHILD: {
15104 int iOff = pCsr->iPgno==1 ? 100 : 0;
15105 if( pCsr->iCell<0 ){
15108 iOff += 12 + pCsr->iCell*2;
15109 if( iOff>pCsr->nPage ) return SQLITE_OK;
15110 iOff = get_uint16(&pCsr->aPage[iOff]);
15112 if( iOff<=pCsr->nPage ){
15113 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
15120 case DBDATA_COLUMN_PGNO:
15121 sqlite3_result_int64(ctx, pCsr->iPgno);
15123 case DBDATA_COLUMN_CELL:
15124 sqlite3_result_int(ctx, pCsr->iCell);
15126 case DBDATA_COLUMN_FIELD:
15127 sqlite3_result_int(ctx, pCsr->iField);
15129 case DBDATA_COLUMN_VALUE: {
15130 if( pCsr->iField<0 ){
15131 sqlite3_result_int64(ctx, pCsr->iIntkey);
15132 }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){
15133 sqlite3_int64 iType;
15134 dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
15136 ctx, pCsr->enc, iType, pCsr->pPtr,
15137 &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
15148 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
15150 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
15151 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15152 *pRowid = pCsr->iRowid;
15158 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
15160 static int sqlite3DbdataRegister(sqlite3 *db){
15161 static sqlite3_module dbdata_module = {
15164 dbdataConnect, /* xConnect */
15165 dbdataBestIndex, /* xBestIndex */
15166 dbdataDisconnect, /* xDisconnect */
15168 dbdataOpen, /* xOpen - open a cursor */
15169 dbdataClose, /* xClose - close a cursor */
15170 dbdataFilter, /* xFilter - configure scan constraints */
15171 dbdataNext, /* xNext - advance a cursor */
15172 dbdataEof, /* xEof - check for end of scan */
15173 dbdataColumn, /* xColumn - read data */
15174 dbdataRowid, /* xRowid - read data */
15180 0, /* xFindMethod */
15182 0, /* xSavepoint */
15184 0, /* xRollbackTo */
15185 0, /* xShadowName */
15189 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
15190 if( rc==SQLITE_OK ){
15191 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
15196 int sqlite3_dbdata_init(
15199 const sqlite3_api_routines *pApi
15202 return sqlite3DbdataRegister(db);
15205 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15207 /************************* End ../ext/recover/dbdata.c ********************/
15208 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
15212 ** The author disclaims copyright to this source code. In place of
15213 ** a legal notice, here is a blessing:
15215 ** May you do good and not evil.
15216 ** May you find forgiveness for yourself and forgive others.
15217 ** May you share freely, never taking more than you give.
15219 *************************************************************************
15224 /* #include "sqlite3recover.h" */
15225 #include <assert.h>
15226 #include <string.h>
15228 #ifndef SQLITE_OMIT_VIRTUALTABLE
15231 ** Declaration for public API function in file dbdata.c. This may be called
15232 ** with NULL as the final two arguments to register the sqlite_dbptr and
15233 ** sqlite_dbdata virtual tables with a database handle.
15238 int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
15240 /* typedef unsigned int u32; */
15241 /* typedef unsigned char u8; */
15242 /* typedef sqlite3_int64 i64; */
15244 typedef struct RecoverTable RecoverTable;
15245 typedef struct RecoverColumn RecoverColumn;
15248 ** When recovering rows of data that can be associated with table
15249 ** definitions recovered from the sqlite_schema table, each table is
15250 ** represented by an instance of the following object.
15253 ** The root page in the original database. Not necessarily (and usually
15254 ** not) the same in the recovered database.
15257 ** Name of the table.
15260 ** aCol[] is an array of nCol columns. In the order in which they appear
15264 ** Set to true for intkey tables, false for WITHOUT ROWID.
15267 ** Each column in the aCol[] array has associated with it the index of
15268 ** the bind parameter its values will be bound to in the INSERT statement
15269 ** used to construct the output database. If the table does has a rowid
15270 ** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
15271 ** index of the bind paramater to which the rowid value should be bound.
15272 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
15273 ** KEY column, then the rowid value should be bound to the index associated
15274 ** with the column.
15277 ** All RecoverTable objects used by the recovery operation are allocated
15278 ** and populated as part of creating the recovered database schema in
15279 ** the output database, before any non-schema data are recovered. They
15280 ** are then stored in a singly-linked list linked by this variable beginning
15281 ** at sqlite3_recover.pTblList.
15283 struct RecoverTable {
15284 u32 iRoot; /* Root page in original database */
15285 char *zTab; /* Name of table */
15286 int nCol; /* Number of columns in table */
15287 RecoverColumn *aCol; /* Array of columns */
15288 int bIntkey; /* True for intkey, false for without rowid */
15289 int iRowidBind; /* If >0, bind rowid to INSERT here */
15290 RecoverTable *pNext;
15294 ** Each database column is represented by an instance of the following object
15295 ** stored in the RecoverTable.aCol[] array of the associated table.
15298 ** The index of the associated field within database records. Or -1 if
15299 ** there is no associated field (e.g. for virtual generated columns).
15302 ** The bind index of the INSERT statement to bind this columns values
15303 ** to. Or 0 if there is no such index (iff (iField<0)).
15306 ** True if this is the INTEGER PRIMARY KEY column.
15312 ** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
15314 struct RecoverColumn {
15315 int iField; /* Field in record on disk */
15316 int iBind; /* Binding to use in INSERT */
15317 int bIPK; /* True for IPK column */
15322 #define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
15323 #define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
15324 #define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
15325 #define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
15328 ** Bitmap object used to track pages in the input database. Allocated
15329 ** and manipulated only by the following functions:
15331 ** recoverBitmapAlloc()
15332 ** recoverBitmapFree()
15333 ** recoverBitmapSet()
15334 ** recoverBitmapQuery()
15337 ** Largest page number that may be stored in the bitmap. The range
15338 ** of valid keys is 1 to nPg, inclusive.
15341 ** Array large enough to contain a bit for each key. For key value
15342 ** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
15343 ** In other words, the following is true if bit iKey is set, or
15344 ** false if it is clear:
15346 ** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
15348 typedef struct RecoverBitmap RecoverBitmap;
15349 struct RecoverBitmap {
15350 i64 nPg; /* Size of bitmap */
15351 u32 aElem[1]; /* Array of 32-bit bitmasks */
15355 ** State variables (part of the sqlite3_recover structure) used while
15356 ** recovering data for tables identified in the recovered schema (state
15357 ** RECOVER_STATE_WRITING).
15359 typedef struct RecoverStateW1 RecoverStateW1;
15360 struct RecoverStateW1 {
15361 sqlite3_stmt *pTbls;
15362 sqlite3_stmt *pSel;
15363 sqlite3_stmt *pInsert;
15366 RecoverTable *pTab; /* Table currently being written */
15367 int nMax; /* Max column count in any schema table */
15368 sqlite3_value **apVal; /* Array of nMax values */
15369 int nVal; /* Number of valid entries in apVal[] */
15377 ** State variables (part of the sqlite3_recover structure) used while
15378 ** recovering data destined for the lost and found table (states
15379 ** RECOVER_STATE_LOSTANDFOUND[123]).
15381 typedef struct RecoverStateLAF RecoverStateLAF;
15382 struct RecoverStateLAF {
15383 RecoverBitmap *pUsed;
15384 i64 nPg; /* Size of db in pages */
15385 sqlite3_stmt *pAllAndParent;
15386 sqlite3_stmt *pMapInsert;
15387 sqlite3_stmt *pMaxField;
15388 sqlite3_stmt *pUsedPages;
15389 sqlite3_stmt *pFindRoot;
15390 sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */
15391 sqlite3_stmt *pAllPage;
15392 sqlite3_stmt *pPageData;
15393 sqlite3_value **apVal;
15398 ** Main recover handle structure.
15400 struct sqlite3_recover {
15401 /* Copies of sqlite3_recover_init[_sql]() parameters */
15402 sqlite3 *dbIn; /* Input database */
15403 char *zDb; /* Name of input db ("main" etc.) */
15404 char *zUri; /* URI for output database */
15405 void *pSqlCtx; /* SQL callback context */
15406 int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
15408 /* Values configured by sqlite3_recover_config() */
15409 char *zStateDb; /* State database to use (or NULL) */
15410 char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
15411 int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
15412 int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */
15413 int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
15421 /* Error code and error message */
15422 int errCode; /* For sqlite3_recover_errcode() */
15423 char *zErrMsg; /* For sqlite3_recover_errmsg() */
15426 int bCloseTransaction;
15428 /* Variables used with eState==RECOVER_STATE_WRITING */
15431 /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
15432 RecoverStateLAF laf;
15434 /* Fields used within sqlite3_recover_run() */
15435 sqlite3 *dbOut; /* Output database */
15436 sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */
15437 RecoverTable *pTblList; /* List of tables recovered from schema */
15441 ** The various states in which an sqlite3_recover object may exist:
15443 ** RECOVER_STATE_INIT:
15444 ** The object is initially created in this state. sqlite3_recover_step()
15445 ** has yet to be called. This is the only state in which it is permitted
15446 ** to call sqlite3_recover_config().
15448 ** RECOVER_STATE_WRITING:
15450 ** RECOVER_STATE_LOSTANDFOUND1:
15451 ** State to populate the bitmap of pages used by other tables or the
15452 ** database freelist.
15454 ** RECOVER_STATE_LOSTANDFOUND2:
15455 ** Populate the recovery.map table - used to figure out a "root" page
15456 ** for each lost page from in the database from which records are
15459 ** RECOVER_STATE_LOSTANDFOUND3:
15460 ** Populate the lost-and-found table itself.
15462 #define RECOVER_STATE_INIT 0
15463 #define RECOVER_STATE_WRITING 1
15464 #define RECOVER_STATE_LOSTANDFOUND1 2
15465 #define RECOVER_STATE_LOSTANDFOUND2 3
15466 #define RECOVER_STATE_LOSTANDFOUND3 4
15467 #define RECOVER_STATE_SCHEMA2 5
15468 #define RECOVER_STATE_DONE 6
15472 ** Global variables used by this extension.
15474 typedef struct RecoverGlobal RecoverGlobal;
15475 struct RecoverGlobal {
15476 const sqlite3_io_methods *pMethods;
15477 sqlite3_recover *p;
15479 static RecoverGlobal recover_g;
15482 ** Use this static SQLite mutex to protect the globals during the
15483 ** first call to sqlite3_recover_step().
15485 #define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
15489 ** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
15491 #define RECOVER_ROWID_DEFAULT 1
15496 ** recoverEnterMutex() - Enter the recovery mutex
15497 ** recoverLeaveMutex() - Leave the recovery mutex
15498 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
15500 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
15501 # define recoverEnterMutex()
15502 # define recoverLeaveMutex()
15504 static void recoverEnterMutex(void){
15505 sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
15507 static void recoverLeaveMutex(void){
15508 sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
15511 #if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
15512 static void recoverAssertMutexHeld(void){
15513 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
15516 # define recoverAssertMutexHeld()
15521 ** Like strlen(). But handles NULL pointer arguments.
15523 static int recoverStrlen(const char *zStr){
15524 if( zStr==0 ) return 0;
15525 return (int)(strlen(zStr)&0x7fffffff);
15529 ** This function is a no-op if the recover handle passed as the first
15530 ** argument already contains an error (if p->errCode!=SQLITE_OK).
15532 ** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
15533 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
15534 ** if an OOM error occurs, NULL is returned and the handle error code
15535 ** (p->errCode) set to SQLITE_NOMEM.
15537 static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
15540 if( p->errCode==SQLITE_OK ){
15541 pRet = sqlite3_malloc64(nByte);
15543 memset(pRet, 0, nByte);
15545 p->errCode = SQLITE_NOMEM;
15552 ** Set the error code and error message for the recover handle passed as
15553 ** the first argument. The error code is set to the value of parameter
15556 ** Parameter zFmt must be a printf() style formatting string. The handle
15557 ** error message is set to the result of using any trailing arguments for
15558 ** parameter substitutions in the formatting string.
15562 ** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
15564 static int recoverError(
15565 sqlite3_recover *p,
15567 const char *zFmt, ...
15571 va_start(ap, zFmt);
15573 z = sqlite3_vmprintf(zFmt, ap);
15576 sqlite3_free(p->zErrMsg);
15578 p->errCode = errCode;
15584 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
15585 ** In this case it returns NULL.
15587 ** Otherwise, an attempt is made to allocate and return a bitmap object
15588 ** large enough to store a bit for all page numbers between 1 and nPg,
15589 ** inclusive. The bitmap is initially zeroed.
15591 static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
15592 int nElem = (nPg+1+31) / 32;
15593 int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
15594 RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
15603 ** Free a bitmap object allocated by recoverBitmapAlloc().
15605 static void recoverBitmapFree(RecoverBitmap *pMap){
15606 sqlite3_free(pMap);
15610 ** Set the bit associated with page iPg in bitvec pMap.
15612 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
15613 if( iPg<=pMap->nPg ){
15614 int iElem = (iPg / 32);
15615 int iBit = (iPg % 32);
15616 pMap->aElem[iElem] |= (((u32)1) << iBit);
15621 ** Query bitmap object pMap for the state of the bit associated with page
15622 ** iPg. Return 1 if it is set, or 0 otherwise.
15624 static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
15626 if( iPg<=pMap->nPg && iPg>0 ){
15627 int iElem = (iPg / 32);
15628 int iBit = (iPg % 32);
15629 ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
15635 ** Set the recover handle error to the error code and message returned by
15636 ** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
15639 static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
15640 return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
15644 ** This function is a no-op if recover handle p already contains an error
15645 ** (if p->errCode!=SQLITE_OK).
15647 ** Otherwise, it attempts to prepare the SQL statement in zSql against
15648 ** database handle db. If successful, the statement handle is returned.
15649 ** Or, if an error occurs, NULL is returned and an error left in the
15652 static sqlite3_stmt *recoverPrepare(
15653 sqlite3_recover *p,
15657 sqlite3_stmt *pStmt = 0;
15658 if( p->errCode==SQLITE_OK ){
15659 if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
15660 recoverDbError(p, db);
15667 ** This function is a no-op if recover handle p already contains an error
15668 ** (if p->errCode!=SQLITE_OK).
15670 ** Otherwise, argument zFmt is used as a printf() style format string,
15671 ** along with any trailing arguments, to create an SQL statement. This
15672 ** SQL statement is prepared against database handle db and, if successful,
15673 ** the statment handle returned. Or, if an error occurs - either during
15674 ** the printf() formatting or when preparing the resulting SQL - an
15675 ** error code and message are left in the recover handle.
15677 static sqlite3_stmt *recoverPreparePrintf(
15678 sqlite3_recover *p,
15680 const char *zFmt, ...
15682 sqlite3_stmt *pStmt = 0;
15683 if( p->errCode==SQLITE_OK ){
15686 va_start(ap, zFmt);
15687 z = sqlite3_vmprintf(zFmt, ap);
15690 p->errCode = SQLITE_NOMEM;
15692 pStmt = recoverPrepare(p, db, z);
15700 ** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
15701 ** indicates that an error occurred, and there is not already an error
15702 ** in the recover handle passed as the first argument, set the error
15703 ** code and error message appropriately.
15705 ** This function returns a copy of the statement handle pointer passed
15706 ** as the second argument.
15708 static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
15709 int rc = sqlite3_reset(pStmt);
15710 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
15711 recoverDbError(p, sqlite3_db_handle(pStmt));
15717 ** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
15718 ** indicates that an error occurred, and there is not already an error
15719 ** in the recover handle passed as the first argument, set the error
15720 ** code and error message appropriately.
15722 static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
15723 sqlite3 *db = sqlite3_db_handle(pStmt);
15724 int rc = sqlite3_finalize(pStmt);
15725 if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
15726 recoverDbError(p, db);
15731 ** This function is a no-op if recover handle p already contains an error
15732 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
15735 ** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
15736 ** Or, if an error occurs, leave an error code and message in the recover
15737 ** handle and return a copy of the error code.
15739 static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
15740 if( p->errCode==SQLITE_OK ){
15741 int rc = sqlite3_exec(db, zSql, 0, 0, 0);
15743 recoverDbError(p, db);
15750 ** Bind the value pVal to parameter iBind of statement pStmt. Leave an
15751 ** error in the recover handle passed as the first argument if an error
15752 ** (e.g. an OOM) occurs.
15754 static void recoverBindValue(
15755 sqlite3_recover *p,
15756 sqlite3_stmt *pStmt,
15758 sqlite3_value *pVal
15760 if( p->errCode==SQLITE_OK ){
15761 int rc = sqlite3_bind_value(pStmt, iBind, pVal);
15762 if( rc ) recoverError(p, rc, 0);
15767 ** This function is a no-op if recover handle p already contains an error
15768 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
15770 ** Otherwise, an attempt is made to interpret zFmt as a printf() style
15771 ** formatting string and the result of using the trailing arguments for
15772 ** parameter substitution with it written into a buffer obtained from
15773 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
15774 ** It is the responsibility of the caller to eventually free the buffer
15775 ** using sqlite3_free().
15777 ** Or, if an error occurs, an error code and message is left in the recover
15778 ** handle and NULL returned.
15780 static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
15783 va_start(ap, zFmt);
15784 z = sqlite3_vmprintf(zFmt, ap);
15786 if( p->errCode==SQLITE_OK ){
15787 if( z==0 ) p->errCode = SQLITE_NOMEM;
15796 ** This function is a no-op if recover handle p already contains an error
15797 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
15799 ** Otherwise, execute "PRAGMA page_count" against the input database. If
15800 ** successful, return the integer result. Or, if an error occurs, leave an
15801 ** error code and error message in the sqlite3_recover handle and return
15804 static i64 recoverPageCount(sqlite3_recover *p){
15806 if( p->errCode==SQLITE_OK ){
15807 sqlite3_stmt *pStmt = 0;
15808 pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
15810 sqlite3_step(pStmt);
15811 nPg = sqlite3_column_int64(pStmt, 0);
15813 recoverFinalize(p, pStmt);
15819 ** Implementation of SQL scalar function "read_i32". The first argument to
15820 ** this function must be a blob. The second a non-negative integer. This
15821 ** function reads and returns a 32-bit big-endian integer from byte
15822 ** offset (4*<arg2>) of the blob.
15824 ** SELECT read_i32(<blob>, <idx>)
15826 static void recoverReadI32(
15827 sqlite3_context *context,
15829 sqlite3_value **argv
15831 const unsigned char *pBlob;
15836 nBlob = sqlite3_value_bytes(argv[0]);
15837 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15838 iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
15840 if( (iInt+1)*4<=nBlob ){
15841 const unsigned char *a = &pBlob[iInt*4];
15842 i64 iVal = ((i64)a[0]<<24)
15846 sqlite3_result_int64(context, iVal);
15851 ** Implementation of SQL scalar function "page_is_used". This function
15852 ** is used as part of the procedure for locating orphan rows for the
15853 ** lost-and-found table, and it depends on those routines having populated
15854 ** the sqlite3_recover.laf.pUsed variable.
15856 ** The only argument to this function is a page-number. It returns true
15857 ** if the page has already been used somehow during data recovery, or false
15860 ** SELECT page_is_used(<pgno>);
15862 static void recoverPageIsUsed(
15863 sqlite3_context *pCtx,
15865 sqlite3_value **apArg
15867 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
15868 i64 pgno = sqlite3_value_int64(apArg[0]);
15870 sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
15874 ** The implementation of a user-defined SQL function invoked by the
15875 ** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
15876 ** of the database being recovered.
15878 ** This function always takes a single integer argument. If the argument
15879 ** is zero, then the value returned is the number of pages in the db being
15880 ** recovered. If the argument is greater than zero, it is a page number.
15881 ** The value returned in this case is an SQL blob containing the data for
15882 ** the identified page of the db being recovered. e.g.
15884 ** SELECT getpage(0); -- return number of pages in db
15885 ** SELECT getpage(4); -- return page 4 of db as a blob of data
15887 static void recoverGetPage(
15888 sqlite3_context *pCtx,
15890 sqlite3_value **apArg
15892 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
15893 i64 pgno = sqlite3_value_int64(apArg[0]);
15894 sqlite3_stmt *pStmt = 0;
15898 i64 nPg = recoverPageCount(p);
15899 sqlite3_result_int64(pCtx, nPg);
15902 if( p->pGetPage==0 ){
15903 pStmt = p->pGetPage = recoverPreparePrintf(
15904 p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
15906 }else if( p->errCode==SQLITE_OK ){
15907 pStmt = p->pGetPage;
15911 sqlite3_bind_int64(pStmt, 1, pgno);
15912 if( SQLITE_ROW==sqlite3_step(pStmt) ){
15915 assert( p->errCode==SQLITE_OK );
15916 aPg = sqlite3_column_blob(pStmt, 0);
15917 nPg = sqlite3_column_bytes(pStmt, 0);
15918 if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
15919 aPg = p->pPage1Disk;
15921 sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
15923 recoverReset(p, pStmt);
15928 if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
15929 sqlite3_result_error_code(pCtx, p->errCode);
15934 ** Find a string that is not found anywhere in z[]. Return a pointer
15937 ** Try to use zA and zB first. If both of those are already found in z[]
15938 ** then make up some string and store it in the buffer zBuf.
15940 static const char *recoverUnusedString(
15941 const char *z, /* Result must not appear anywhere in z */
15942 const char *zA, const char *zB, /* Try these first */
15943 char *zBuf /* Space to store a generated string */
15946 if( strstr(z, zA)==0 ) return zA;
15947 if( strstr(z, zB)==0 ) return zB;
15949 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
15950 }while( strstr(z,zBuf)!=0 );
15955 ** Implementation of scalar SQL function "escape_crnl". The argument passed to
15956 ** this function is the output of built-in function quote(). If the first
15957 ** character of the input is "'", indicating that the value passed to quote()
15958 ** was a text value, then this function searches the input for "\n" and "\r"
15959 ** characters and adds a wrapper similar to the following:
15961 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
15963 ** Or, if the first character of the input is not "'", then a copy of the input
15966 static void recoverEscapeCrnl(
15967 sqlite3_context *context,
15969 sqlite3_value **argv
15971 const char *zText = (const char*)sqlite3_value_text(argv[0]);
15973 if( zText && zText[0]=='\'' ){
15974 int nText = sqlite3_value_bytes(argv[0]);
15978 const char *zNL = 0;
15979 const char *zCR = 0;
15983 for(i=0; zText[i]; i++){
15984 if( zNL==0 && zText[i]=='\n' ){
15985 zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
15986 nNL = (int)strlen(zNL);
15988 if( zCR==0 && zText[i]=='\r' ){
15989 zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
15990 nCR = (int)strlen(zCR);
15996 i64 nMax = (nNL > nCR) ? nNL : nCR;
15997 i64 nAlloc = nMax * nText + (nMax+64)*2;
15998 char *zOut = (char*)sqlite3_malloc64(nAlloc);
16000 sqlite3_result_error_nomem(context);
16005 memcpy(&zOut[iOut], "replace(replace(", 16);
16008 memcpy(&zOut[iOut], "replace(", 8);
16011 for(i=0; zText[i]; i++){
16012 if( zText[i]=='\n' ){
16013 memcpy(&zOut[iOut], zNL, nNL);
16015 }else if( zText[i]=='\r' ){
16016 memcpy(&zOut[iOut], zCR, nCR);
16019 zOut[iOut] = zText[i];
16025 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
16026 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
16027 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
16030 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
16031 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
16032 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
16035 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
16036 sqlite3_free(zOut);
16041 sqlite3_result_value(context, argv[0]);
16045 ** This function is a no-op if recover handle p already contains an error
16046 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16049 ** Otherwise, attempt to populate temporary table "recovery.schema" with the
16050 ** parts of the database schema that can be extracted from the input database.
16052 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
16053 ** and error message are left in the recover handle and a copy of the
16054 ** error code returned. It is not considered an error if part of all of
16055 ** the database schema cannot be recovered due to corruption.
16057 static int recoverCacheSchema(sqlite3_recover *p){
16058 return recoverExec(p, p->dbOut,
16059 "WITH RECURSIVE pages(p) AS ("
16062 " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
16064 "INSERT INTO recovery.schema SELECT"
16065 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
16066 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
16067 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
16068 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
16069 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
16070 "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
16071 " SELECT p FROM pages"
16072 ") GROUP BY pgno, cell"
16077 ** If this recover handle is not in SQL callback mode (i.e. was not created
16078 ** using sqlite3_recover_init_sql()) of if an error has already occurred,
16079 ** this function is a no-op. Otherwise, issue a callback with SQL statement
16080 ** zSql as the parameter.
16082 ** If the callback returns non-zero, set the recover handle error code to
16083 ** the value returned (so that the caller will abandon processing).
16085 static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
16086 if( p->errCode==SQLITE_OK && p->xSql ){
16087 int res = p->xSql(p->pSqlCtx, zSql);
16089 recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
16095 ** Transfer the following settings from the input database to the output
16099 ** + auto-vacuum settings,
16100 ** + database encoding,
16101 ** + user-version (PRAGMA user_version), and
16102 ** + application-id (PRAGMA application_id), and
16104 static void recoverTransferSettings(sqlite3_recover *p){
16105 const char *aPragma[] = {
16114 /* Truncate the output database to 0 pages in size. This is done by
16115 ** opening a new, empty, temp db, then using the backup API to clobber
16116 ** any existing output db with a copy of it. */
16117 if( p->errCode==SQLITE_OK ){
16119 int rc = sqlite3_open("", &db2);
16120 if( rc!=SQLITE_OK ){
16121 recoverDbError(p, db2);
16125 for(ii=0; ii<(int)(sizeof(aPragma)/sizeof(aPragma[0])); ii++){
16126 const char *zPrag = aPragma[ii];
16127 sqlite3_stmt *p1 = 0;
16128 p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
16129 if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
16130 const char *zArg = (const char*)sqlite3_column_text(p1, 0);
16131 char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
16132 recoverSqlCallback(p, z2);
16133 recoverExec(p, db2, z2);
16136 recoverError(p, SQLITE_NOMEM, 0);
16139 recoverFinalize(p, p1);
16141 recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;");
16143 if( p->errCode==SQLITE_OK ){
16144 sqlite3 *db = p->dbOut;
16145 sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
16147 sqlite3_backup_step(pBackup, -1);
16148 p->errCode = sqlite3_backup_finish(pBackup);
16150 recoverDbError(p, db);
16154 sqlite3_close(db2);
16159 ** This function is a no-op if recover handle p already contains an error
16160 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16163 ** Otherwise, an attempt is made to open the output database, attach
16164 ** and create the schema of the temporary database used to store
16165 ** intermediate data, and to register all required user functions and
16166 ** virtual table modules with the output handle.
16168 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
16169 ** and error message are left in the recover handle and a copy of the
16170 ** error code returned.
16172 static int recoverOpenOutput(sqlite3_recover *p){
16176 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
16178 { "getpage", 1, recoverGetPage },
16179 { "page_is_used", 1, recoverPageIsUsed },
16180 { "read_i32", 2, recoverReadI32 },
16181 { "escape_crnl", 1, recoverEscapeCrnl },
16184 const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
16185 sqlite3 *db = 0; /* New database handle */
16186 int ii; /* For iterating through aFunc[] */
16188 assert( p->dbOut==0 );
16190 if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
16191 recoverDbError(p, db);
16194 /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
16195 ** These two are registered with the output database handle - this
16196 ** module depends on the input handle supporting the sqlite_dbpage
16197 ** virtual table only. */
16198 if( p->errCode==SQLITE_OK ){
16199 p->errCode = sqlite3_dbdata_init(db, 0, 0);
16202 /* Register the custom user-functions with the output handle. */
16204 p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0]));
16206 p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
16207 aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
16216 ** Attach the auxiliary database 'recovery' to the output database handle.
16217 ** This temporary database is used during the recovery process and then
16220 static void recoverOpenRecovery(sqlite3_recover *p){
16221 char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
16222 recoverExec(p, p->dbOut, zSql);
16223 recoverExec(p, p->dbOut,
16224 "PRAGMA writable_schema = 1;"
16225 "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
16226 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
16228 sqlite3_free(zSql);
16233 ** This function is a no-op if recover handle p already contains an error
16234 ** (if p->errCode!=SQLITE_OK).
16236 ** Otherwise, argument zName must be the name of a table that has just been
16237 ** created in the output database. This function queries the output db
16238 ** for the schema of said table, and creates a RecoverTable object to
16239 ** store the schema in memory. The new RecoverTable object is linked into
16240 ** the list at sqlite3_recover.pTblList.
16242 ** Parameter iRoot must be the root page of table zName in the INPUT
16245 static void recoverAddTable(
16246 sqlite3_recover *p,
16247 const char *zName, /* Name of table created in output db */
16248 i64 iRoot /* Root page of same table in INPUT db */
16250 sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
16251 "PRAGMA table_xinfo(%Q)", zName
16257 RecoverTable *pNew = 0;
16259 int nName = recoverStrlen(zName);
16261 while( sqlite3_step(pStmt)==SQLITE_ROW ){
16263 nByte += (sqlite3_column_bytes(pStmt, 1)+1);
16265 nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
16266 recoverReset(p, pStmt);
16268 pNew = recoverMalloc(p, nByte);
16273 pNew->aCol = (RecoverColumn*)&pNew[1];
16274 pNew->zTab = csr = (char*)&pNew->aCol[nCol];
16276 pNew->iRoot = iRoot;
16277 memcpy(csr, zName, nName);
16280 for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
16281 int iPKF = sqlite3_column_int(pStmt, 5);
16282 int n = sqlite3_column_bytes(pStmt, 1);
16283 const char *z = (const char*)sqlite3_column_text(pStmt, 1);
16284 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
16285 int eHidden = sqlite3_column_int(pStmt, 6);
16287 if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
16288 if( iPKF>1 ) iPk = -2;
16289 pNew->aCol[i].zCol = csr;
16290 pNew->aCol[i].eHidden = eHidden;
16291 if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
16292 pNew->aCol[i].iField = -1;
16294 pNew->aCol[i].iField = iField++;
16296 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
16297 && eHidden!=RECOVER_EHIDDEN_STORED
16299 pNew->aCol[i].iBind = iBind++;
16305 pNew->pNext = p->pTblList;
16306 p->pTblList = pNew;
16310 recoverFinalize(p, pStmt);
16312 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
16313 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16314 int iField = sqlite3_column_int(pStmt, 0);
16315 int iCol = sqlite3_column_int(pStmt, 1);
16317 assert( iCol<pNew->nCol );
16318 pNew->aCol[iCol].iField = iField;
16323 recoverFinalize(p, pStmt);
16325 if( p->errCode==SQLITE_OK ){
16327 pNew->aCol[iPk].bIPK = 1;
16328 }else if( pNew->bIntkey ){
16329 pNew->iRowidBind = iBind++;
16336 ** This function is called after recoverCacheSchema() has cached those parts
16337 ** of the input database schema that could be recovered in temporary table
16338 ** "recovery.schema". This function creates in the output database copies
16339 ** of all parts of that schema that must be created before the tables can
16340 ** be populated. Specifically, this means:
16342 ** * all tables that are not VIRTUAL, and
16343 ** * UNIQUE indexes.
16345 ** If the recovery handle uses SQL callbacks, then callbacks containing
16346 ** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
16348 ** Additionally, records are added to the sqlite_schema table of the
16349 ** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
16350 ** records are written directly to sqlite_schema, not actually executed.
16351 ** If the handle is in SQL callback mode, then callbacks are invoked
16352 ** with equivalent SQL statements.
16354 static int recoverWriteSchema1(sqlite3_recover *p){
16355 sqlite3_stmt *pSelect = 0;
16356 sqlite3_stmt *pTblname = 0;
16358 pSelect = recoverPrepare(p, p->dbOut,
16359 "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
16360 " SELECT rootpage, name, sql, "
16362 " sql LIKE 'create virtual%',"
16363 " (type='index' AND (sql LIKE '%unique%' OR ?1))"
16364 " FROM recovery.schema"
16366 "SELECT rootpage, tbl, isVirtual, name, sql"
16368 " WHERE tbl OR isIndex"
16369 " ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
16372 pTblname = recoverPrepare(p, p->dbOut,
16373 "SELECT name FROM sqlite_schema "
16374 "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
16378 sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
16379 while( sqlite3_step(pSelect)==SQLITE_ROW ){
16380 i64 iRoot = sqlite3_column_int64(pSelect, 0);
16381 int bTable = sqlite3_column_int(pSelect, 1);
16382 int bVirtual = sqlite3_column_int(pSelect, 2);
16383 const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
16384 const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
16386 int rc = SQLITE_OK;
16389 zSql = (const char*)(zFree = recoverMPrintf(p,
16390 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
16394 rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
16395 if( rc==SQLITE_OK ){
16396 recoverSqlCallback(p, zSql);
16397 if( bTable && !bVirtual ){
16398 if( SQLITE_ROW==sqlite3_step(pTblname) ){
16399 const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
16400 recoverAddTable(p, zTbl, iRoot);
16402 recoverReset(p, pTblname);
16404 }else if( rc!=SQLITE_ERROR ){
16405 recoverDbError(p, p->dbOut);
16407 sqlite3_free(zFree);
16410 recoverFinalize(p, pSelect);
16411 recoverFinalize(p, pTblname);
16417 ** This function is called after the output database has been populated. It
16418 ** adds all recovered schema elements that were not created in the output
16419 ** database by recoverWriteSchema1() - everything except for tables and
16420 ** UNIQUE indexes. Specifically:
16424 ** * non-UNIQUE indexes.
16426 ** If the recover handle is in SQL callback mode, then equivalent callbacks
16427 ** are issued to create the schema elements.
16429 static int recoverWriteSchema2(sqlite3_recover *p){
16430 sqlite3_stmt *pSelect = 0;
16432 pSelect = recoverPrepare(p, p->dbOut,
16434 "SELECT rootpage, sql FROM recovery.schema "
16435 " WHERE type!='table' AND type!='index'"
16437 "SELECT rootpage, sql FROM recovery.schema "
16438 " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
16442 while( sqlite3_step(pSelect)==SQLITE_ROW ){
16443 const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
16444 int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
16445 if( rc==SQLITE_OK ){
16446 recoverSqlCallback(p, zSql);
16447 }else if( rc!=SQLITE_ERROR ){
16448 recoverDbError(p, p->dbOut);
16452 recoverFinalize(p, pSelect);
16458 ** This function is a no-op if recover handle p already contains an error
16459 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
16461 ** Otherwise, if the recover handle is configured to create an output
16462 ** database (was created by sqlite3_recover_init()), then this function
16463 ** prepares and returns an SQL statement to INSERT a new record into table
16464 ** pTab, assuming the first nField fields of a record extracted from disk
16467 ** For example, if table pTab is:
16469 ** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
16471 ** And nField is 4, then the SQL statement prepared and returned is:
16473 ** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
16475 ** In this case even though 4 values were extracted from the input db,
16476 ** only 3 are written to the output, as the generated STORED column
16477 ** cannot be written.
16479 ** If the recover handle is in SQL callback mode, then the SQL statement
16480 ** prepared is such that evaluating it returns a single row containing
16481 ** a single text value - itself an SQL statement similar to the above,
16482 ** except with SQL literals in place of the variables. For example:
16484 ** SELECT 'INSERT INTO (a, c, d) VALUES ('
16485 ** || quote(?1) || ', '
16486 ** || quote(?2) || ', '
16487 ** || quote(?3) || ')';
16489 ** In either case, it is the responsibility of the caller to eventually
16490 ** free the statement handle using sqlite3_finalize().
16492 static sqlite3_stmt *recoverInsertStmt(
16493 sqlite3_recover *p,
16494 RecoverTable *pTab,
16497 sqlite3_stmt *pRet = 0;
16498 const char *zSep = "";
16499 const char *zSqlSep = "";
16504 int bSql = p->xSql ? 1 : 0;
16506 if( nField<=0 ) return 0;
16508 assert( nField<=pTab->nCol );
16510 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
16512 if( pTab->iRowidBind ){
16513 assert( pTab->bIntkey );
16514 zSql = recoverMPrintf(p, "%z_rowid_", zSql);
16516 zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
16518 zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
16520 zSqlSep = "||', '||";
16524 for(ii=0; ii<nField; ii++){
16525 int eHidden = pTab->aCol[ii].eHidden;
16526 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
16527 && eHidden!=RECOVER_EHIDDEN_STORED
16529 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
16530 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
16533 zBind = recoverMPrintf(p,
16534 "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
16536 zSqlSep = "||', '||";
16538 zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
16545 zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
16549 zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
16552 pRet = recoverPrepare(p, p->dbOut, zFinal);
16553 sqlite3_free(zSql);
16554 sqlite3_free(zBind);
16555 sqlite3_free(zFinal);
16562 ** Search the list of RecoverTable objects at p->pTblList for one that
16563 ** has root page iRoot in the input database. If such an object is found,
16564 ** return a pointer to it. Otherwise, return NULL.
16566 static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
16567 RecoverTable *pRet = 0;
16568 for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
16573 ** This function attempts to create a lost and found table within the
16574 ** output db. If successful, it returns a pointer to a buffer containing
16575 ** the name of the new table. It is the responsibility of the caller to
16576 ** eventually free this buffer using sqlite3_free().
16578 ** If an error occurs, NULL is returned and an error code and error
16579 ** message left in the recover handle.
16581 static char *recoverLostAndFoundCreate(
16582 sqlite3_recover *p, /* Recover object */
16583 int nField /* Number of column fields in new table */
16586 sqlite3_stmt *pProbe = 0;
16589 pProbe = recoverPrepare(p, p->dbOut,
16590 "SELECT 1 FROM sqlite_schema WHERE name=?"
16592 for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
16595 zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
16597 zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
16600 if( p->errCode==SQLITE_OK ){
16601 sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
16602 if( SQLITE_ROW==sqlite3_step(pProbe) ){
16605 recoverReset(p, pProbe);
16609 sqlite3_clear_bindings(pProbe);
16610 sqlite3_free(zTbl);
16614 recoverFinalize(p, pProbe);
16617 const char *zSep = 0;
16621 zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
16622 for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
16623 zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
16627 zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
16628 sqlite3_free(zField);
16630 recoverExec(p, p->dbOut, zSql);
16631 recoverSqlCallback(p, zSql);
16632 sqlite3_free(zSql);
16633 }else if( p->errCode==SQLITE_OK ){
16635 p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
16643 ** Synthesize and prepare an INSERT statement to write to the lost_and_found
16644 ** table in the output database. The name of the table is zTab, and it has
16645 ** nField c* fields.
16647 static sqlite3_stmt *recoverLostAndFoundInsert(
16648 sqlite3_recover *p,
16652 int nTotal = nField + 4;
16655 sqlite3_stmt *pRet = 0;
16658 for(ii=0; ii<nTotal; ii++){
16659 zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
16661 pRet = recoverPreparePrintf(
16662 p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
16665 const char *zSep = "";
16666 for(ii=0; ii<nTotal; ii++){
16667 zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
16668 zSep = "|| ', ' ||";
16670 pRet = recoverPreparePrintf(
16671 p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
16675 sqlite3_free(zBind);
16680 ** Input database page iPg contains data that will be written to the
16681 ** lost-and-found table of the output database. This function attempts
16682 ** to identify the root page of the tree that page iPg belonged to.
16683 ** If successful, it sets output variable (*piRoot) to the page number
16684 ** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
16685 ** an SQLite error code is returned and the final value of *piRoot
16688 static int recoverLostAndFoundFindRoot(
16689 sqlite3_recover *p,
16693 RecoverStateLAF *pLaf = &p->laf;
16695 if( pLaf->pFindRoot==0 ){
16696 pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
16697 "WITH RECURSIVE p(pgno) AS ("
16700 " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
16702 "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
16703 " AND m.parent IS NULL"
16706 if( p->errCode==SQLITE_OK ){
16707 sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
16708 if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
16709 *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
16713 recoverReset(p, pLaf->pFindRoot);
16719 ** Recover data from page iPage of the input database and write it to
16720 ** the lost-and-found table in the output database.
16722 static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
16723 RecoverStateLAF *pLaf = &p->laf;
16724 sqlite3_value **apVal = pLaf->apVal;
16725 sqlite3_stmt *pPageData = pLaf->pPageData;
16726 sqlite3_stmt *pInsert = pLaf->pInsert;
16731 int bHaveRowid = 0;
16735 if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
16736 sqlite3_bind_int64(pPageData, 1, iPage);
16737 while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
16738 int iCell = sqlite3_column_int64(pPageData, 0);
16739 int iField = sqlite3_column_int64(pPageData, 1);
16741 if( iPrevCell!=iCell && nVal>=0 ){
16742 /* Insert the new row */
16743 sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */
16744 sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */
16745 sqlite3_bind_int(pInsert, 3, nVal); /* nfield */
16747 sqlite3_bind_int64(pInsert, 4, iRowid); /* id */
16749 for(ii=0; ii<nVal; ii++){
16750 recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
16752 if( sqlite3_step(pInsert)==SQLITE_ROW ){
16753 recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
16755 recoverReset(p, pInsert);
16757 /* Discard the accumulated row data */
16758 for(ii=0; ii<nVal; ii++){
16759 sqlite3_value_free(apVal[ii]);
16762 sqlite3_clear_bindings(pInsert);
16767 if( iCell<0 ) break;
16770 assert( nVal==-1 );
16771 iRowid = sqlite3_column_int64(pPageData, 2);
16774 }else if( iField<pLaf->nMaxField ){
16775 sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
16776 apVal[iField] = sqlite3_value_dup(pVal);
16777 assert( iField==nVal || (nVal==-1 && iField==0) );
16779 if( apVal[iField]==0 ){
16780 recoverError(p, SQLITE_NOMEM, 0);
16786 recoverReset(p, pPageData);
16788 for(ii=0; ii<nVal; ii++){
16789 sqlite3_value_free(apVal[ii]);
16795 ** Perform one step (sqlite3_recover_step()) of work for the connection
16796 ** passed as the only argument, which is guaranteed to be in
16797 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
16798 ** table of the output database is populated with recovered data that can
16799 ** not be assigned to any recovered schema object.
16801 static int recoverLostAndFound3Step(sqlite3_recover *p){
16802 RecoverStateLAF *pLaf = &p->laf;
16803 if( p->errCode==SQLITE_OK ){
16804 if( pLaf->pInsert==0 ){
16805 return SQLITE_DONE;
16807 if( p->errCode==SQLITE_OK ){
16808 int res = sqlite3_step(pLaf->pAllPage);
16809 if( res==SQLITE_ROW ){
16810 i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
16811 if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
16812 recoverLostAndFoundOnePage(p, iPage);
16815 recoverReset(p, pLaf->pAllPage);
16816 return SQLITE_DONE;
16825 ** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
16826 ** state - during which the lost-and-found table of the output database
16827 ** is populated with recovered data that can not be assigned to any
16828 ** recovered schema object.
16830 static void recoverLostAndFound3Init(sqlite3_recover *p){
16831 RecoverStateLAF *pLaf = &p->laf;
16833 if( pLaf->nMaxField>0 ){
16834 char *zTab = 0; /* Name of lost_and_found table */
16836 zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
16837 pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
16838 sqlite3_free(zTab);
16840 pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
16841 "WITH RECURSIVE seq(ii) AS ("
16842 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
16844 "SELECT ii FROM seq" , p->laf.nPg
16846 pLaf->pPageData = recoverPrepare(p, p->dbOut,
16847 "SELECT cell, field, value "
16848 "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
16850 "SELECT -1, -1, -1"
16853 pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
16854 pLaf->nMaxField*sizeof(sqlite3_value*)
16860 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
16861 ** tables recovered from the schema of the input database are populated with
16864 static int recoverWriteDataInit(sqlite3_recover *p){
16865 RecoverStateW1 *p1 = &p->w1;
16866 RecoverTable *pTbl = 0;
16869 /* Figure out the maximum number of columns for any table in the schema */
16870 assert( p1->nMax==0 );
16871 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
16872 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
16875 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
16876 ** that will be written to the output database in a single row. */
16877 nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
16878 p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
16879 if( p1->apVal==0 ) return p->errCode;
16881 /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
16882 ** to loop through cells that appear to belong to a single table (pSel). */
16883 p1->pTbls = recoverPrepare(p, p->dbOut,
16884 "SELECT rootpage FROM recovery.schema "
16885 " WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
16886 " ORDER BY (tbl_name='sqlite_sequence') ASC"
16888 p1->pSel = recoverPrepare(p, p->dbOut,
16889 "WITH RECURSIVE pages(page) AS ("
16892 " SELECT child FROM sqlite_dbptr('getpage()'), pages "
16895 "SELECT page, cell, field, value "
16896 "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
16898 "SELECT 0, 0, 0, 0"
16905 ** Clean up resources allocated by recoverWriteDataInit() (stuff in
16906 ** sqlite3_recover.w1).
16908 static void recoverWriteDataCleanup(sqlite3_recover *p){
16909 RecoverStateW1 *p1 = &p->w1;
16911 for(ii=0; ii<p1->nVal; ii++){
16912 sqlite3_value_free(p1->apVal[ii]);
16914 sqlite3_free(p1->apVal);
16915 recoverFinalize(p, p1->pInsert);
16916 recoverFinalize(p, p1->pTbls);
16917 recoverFinalize(p, p1->pSel);
16918 memset(p1, 0, sizeof(*p1));
16922 ** Perform one step (sqlite3_recover_step()) of work for the connection
16923 ** passed as the only argument, which is guaranteed to be in
16924 ** RECOVER_STATE_WRITING state - during which tables recovered from the
16925 ** schema of the input database are populated with recovered data.
16927 static int recoverWriteDataStep(sqlite3_recover *p){
16928 RecoverStateW1 *p1 = &p->w1;
16929 sqlite3_stmt *pSel = p1->pSel;
16930 sqlite3_value **apVal = p1->apVal;
16932 if( p->errCode==SQLITE_OK && p1->pTab==0 ){
16933 if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
16934 i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
16935 p1->pTab = recoverFindTable(p, iRoot);
16937 recoverFinalize(p, p1->pInsert);
16940 /* If this table is unknown, return early. The caller will invoke this
16941 ** function again and it will move on to the next table. */
16942 if( p1->pTab==0 ) return p->errCode;
16944 /* If this is the sqlite_sequence table, delete any rows added by
16945 ** earlier INSERT statements on tables with AUTOINCREMENT primary
16946 ** keys before recovering its contents. The p1->pTbls SELECT statement
16947 ** is rigged to deliver "sqlite_sequence" last of all, so we don't
16948 ** worry about it being modified after it is recovered. */
16949 if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
16950 recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
16951 recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
16954 /* Bind the root page of this table within the original database to
16955 ** SELECT statement p1->pSel. The SELECT statement will then iterate
16956 ** through cells that look like they belong to table pTab. */
16957 sqlite3_bind_int64(pSel, 1, iRoot);
16960 p1->bHaveRowid = 0;
16961 p1->iPrevPage = -1;
16962 p1->iPrevCell = -1;
16964 return SQLITE_DONE;
16967 assert( p->errCode!=SQLITE_OK || p1->pTab );
16969 if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
16970 RecoverTable *pTab = p1->pTab;
16972 i64 iPage = sqlite3_column_int64(pSel, 0);
16973 int iCell = sqlite3_column_int(pSel, 1);
16974 int iField = sqlite3_column_int(pSel, 2);
16975 sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
16976 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
16978 assert( bNewCell==0 || (iField==-1 || iField==0) );
16979 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
16984 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
16985 recoverFinalize(p, p1->pInsert);
16986 p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
16987 p1->nInsert = p1->nVal;
16990 sqlite3_stmt *pInsert = p1->pInsert;
16991 for(ii=0; ii<pTab->nCol; ii++){
16992 RecoverColumn *pCol = &pTab->aCol[ii];
16993 int iBind = pCol->iBind;
16996 sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
16997 }else if( pCol->iField<p1->nVal ){
16998 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
17002 if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
17003 sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
17005 if( SQLITE_ROW==sqlite3_step(pInsert) ){
17006 const char *z = (const char*)sqlite3_column_text(pInsert, 0);
17007 recoverSqlCallback(p, z);
17009 recoverReset(p, pInsert);
17010 assert( p->errCode || pInsert );
17011 if( pInsert ) sqlite3_clear_bindings(pInsert);
17015 for(ii=0; ii<p1->nVal; ii++){
17016 sqlite3_value_free(apVal[ii]);
17020 p1->bHaveRowid = 0;
17025 p1->iRowid = sqlite3_column_int64(pSel, 3);
17026 assert( p1->nVal==-1 );
17028 p1->bHaveRowid = 1;
17029 }else if( iField<pTab->nCol ){
17030 assert( apVal[iField]==0 );
17031 apVal[iField] = sqlite3_value_dup( pVal );
17032 if( apVal[iField]==0 ){
17033 recoverError(p, SQLITE_NOMEM, 0);
17035 p1->nVal = iField+1;
17037 p1->iPrevCell = iCell;
17038 p1->iPrevPage = iPage;
17041 recoverReset(p, pSel);
17049 ** Initialize resources required by sqlite3_recover_step() in
17050 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17051 ** already allocated to a recovered schema element is determined.
17053 static void recoverLostAndFound1Init(sqlite3_recover *p){
17054 RecoverStateLAF *pLaf = &p->laf;
17055 sqlite3_stmt *pStmt = 0;
17057 assert( p->laf.pUsed==0 );
17058 pLaf->nPg = recoverPageCount(p);
17059 pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
17061 /* Prepare a statement to iterate through all pages that are part of any tree
17062 ** in the recoverable part of the input database schema to the bitmap. And,
17063 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
17065 pStmt = recoverPrepare(
17067 "WITH trunk(pgno) AS ("
17068 " SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
17070 " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
17072 "trunkdata(pgno, data) AS ("
17073 " SELECT pgno, getpage(pgno) FROM trunk"
17075 "freelist(data, n, freepgno) AS ("
17076 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
17078 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
17082 " SELECT 1 UNION ALL"
17083 " SELECT rootpage FROM recovery.schema WHERE rootpage>0"
17086 " SELECT r FROM roots"
17088 " SELECT child FROM sqlite_dbptr('getpage()'), used "
17091 "SELECT page FROM used"
17093 "SELECT freepgno FROM freelist WHERE NOT ?"
17095 if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
17096 pLaf->pUsedPages = pStmt;
17100 ** Perform one step (sqlite3_recover_step()) of work for the connection
17101 ** passed as the only argument, which is guaranteed to be in
17102 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17103 ** already allocated to a recovered schema element is determined.
17105 static int recoverLostAndFound1Step(sqlite3_recover *p){
17106 RecoverStateLAF *pLaf = &p->laf;
17107 int rc = p->errCode;
17108 if( rc==SQLITE_OK ){
17109 rc = sqlite3_step(pLaf->pUsedPages);
17110 if( rc==SQLITE_ROW ){
17111 i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
17112 recoverBitmapSet(pLaf->pUsed, iPg);
17115 recoverFinalize(p, pLaf->pUsedPages);
17116 pLaf->pUsedPages = 0;
17123 ** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
17124 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
17125 ** are sorted into sets that likely belonged to the same database tree.
17127 static void recoverLostAndFound2Init(sqlite3_recover *p){
17128 RecoverStateLAF *pLaf = &p->laf;
17130 assert( p->laf.pAllAndParent==0 );
17131 assert( p->laf.pMapInsert==0 );
17132 assert( p->laf.pMaxField==0 );
17133 assert( p->laf.nMaxField==0 );
17135 pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
17136 "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
17138 pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
17139 "WITH RECURSIVE seq(ii) AS ("
17140 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
17142 "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
17144 "SELECT NULL, ii FROM seq", p->laf.nPg
17146 pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
17147 "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
17152 ** Perform one step (sqlite3_recover_step()) of work for the connection
17153 ** passed as the only argument, which is guaranteed to be in
17154 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
17155 ** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
17156 ** to the same database tree.
17158 static int recoverLostAndFound2Step(sqlite3_recover *p){
17159 RecoverStateLAF *pLaf = &p->laf;
17160 if( p->errCode==SQLITE_OK ){
17161 int res = sqlite3_step(pLaf->pAllAndParent);
17162 if( res==SQLITE_ROW ){
17163 i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
17164 if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
17165 sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
17166 sqlite3_bind_value(pLaf->pMapInsert, 2,
17167 sqlite3_column_value(pLaf->pAllAndParent, 0)
17169 sqlite3_step(pLaf->pMapInsert);
17170 recoverReset(p, pLaf->pMapInsert);
17171 sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
17172 if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
17173 int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
17174 if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
17176 recoverReset(p, pLaf->pMaxField);
17179 recoverFinalize(p, pLaf->pAllAndParent);
17180 pLaf->pAllAndParent =0;
17181 return SQLITE_DONE;
17188 ** Free all resources allocated as part of sqlite3_recover_step() calls
17189 ** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
17191 static void recoverLostAndFoundCleanup(sqlite3_recover *p){
17192 recoverBitmapFree(p->laf.pUsed);
17194 sqlite3_finalize(p->laf.pUsedPages);
17195 sqlite3_finalize(p->laf.pAllAndParent);
17196 sqlite3_finalize(p->laf.pMapInsert);
17197 sqlite3_finalize(p->laf.pMaxField);
17198 sqlite3_finalize(p->laf.pFindRoot);
17199 sqlite3_finalize(p->laf.pInsert);
17200 sqlite3_finalize(p->laf.pAllPage);
17201 sqlite3_finalize(p->laf.pPageData);
17202 p->laf.pUsedPages = 0;
17203 p->laf.pAllAndParent = 0;
17204 p->laf.pMapInsert = 0;
17205 p->laf.pMaxField = 0;
17206 p->laf.pFindRoot = 0;
17207 p->laf.pInsert = 0;
17208 p->laf.pAllPage = 0;
17209 p->laf.pPageData = 0;
17210 sqlite3_free(p->laf.apVal);
17215 ** Free all resources allocated as part of sqlite3_recover_step() calls.
17217 static void recoverFinalCleanup(sqlite3_recover *p){
17218 RecoverTable *pTab = 0;
17219 RecoverTable *pNext = 0;
17221 recoverWriteDataCleanup(p);
17222 recoverLostAndFoundCleanup(p);
17224 for(pTab=p->pTblList; pTab; pTab=pNext){
17225 pNext = pTab->pNext;
17226 sqlite3_free(pTab);
17229 sqlite3_finalize(p->pGetPage);
17231 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
17237 sqlite3_close(p->dbOut);
17238 assert( res==SQLITE_OK );
17244 ** Decode and return an unsigned 16-bit big-endian integer value from
17247 static u32 recoverGetU16(const u8 *a){
17248 return (((u32)a[0])<<8) + ((u32)a[1]);
17252 ** Decode and return an unsigned 32-bit big-endian integer value from
17255 static u32 recoverGetU32(const u8 *a){
17256 return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
17260 ** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
17261 ** and return the number of bytes consumed.
17263 static int recoverGetVarint(const u8 *a, i64 *pVal){
17264 sqlite3_uint64 u = 0;
17266 for(i=0; i<8; i++){
17267 u = (u<<7) + (a[i]&0x7f);
17268 if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
17270 u = (u<<8) + (a[i]&0xff);
17271 *pVal = (sqlite3_int64)u;
17276 ** The second argument points to a buffer n bytes in size. If this buffer
17277 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
17278 ** return the page-size in bytes. Otherwise, if the buffer does not
17279 ** appear to contain a well-formed b-tree page, return 0.
17281 static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
17286 int nCell = 0; /* Number of cells on page */
17287 int iCellOff = 0; /* Offset of cell array in page */
17293 if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
17295 iFree = (int)recoverGetU16(&a[1]);
17296 nCell = (int)recoverGetU16(&a[3]);
17297 iContent = (int)recoverGetU16(&a[5]);
17298 if( iContent==0 ) iContent = 65536;
17301 if( iContent>n ) return 0;
17303 memset(aUsed, 0, n);
17304 memset(aUsed, 0xFF, iContent);
17306 /* Follow the free-list. This is the same format for all b-tree pages. */
17307 if( iFree && iFree<=iContent ) return 0;
17311 if( iFree>(n-4) ) return 0;
17312 iNext = recoverGetU16(&a[iFree]);
17313 nByte = recoverGetU16(&a[iFree+2]);
17314 if( iFree+nByte>n || nByte<4 ) return 0;
17315 if( iNext && iNext<iFree+nByte ) return 0;
17316 memset(&aUsed[iFree], 0xFF, nByte);
17320 /* Run through the cells */
17321 if( eType==0x02 || eType==0x05 ){
17326 if( (iCellOff + 2*nCell)>iContent ) return 0;
17327 for(ii=0; ii<nCell; ii++){
17331 int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
17332 if( iOff<iContent || iOff>n ){
17335 if( eType==0x05 || eType==0x02 ) nByte += 4;
17336 nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
17339 nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
17342 int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
17343 int M = ((n-12)*32/255)-23;
17344 int K = M+((nPayload-M)%(n-4));
17355 if( iOff+nByte>n ){
17358 for(iByte=iOff; iByte<(iOff+nByte); iByte++){
17359 if( aUsed[iByte]!=0 ){
17362 aUsed[iByte] = 0xFF;
17367 for(ii=0; ii<n; ii++){
17368 if( aUsed[ii]==0 ) nActual++;
17370 return (nActual==nFrag);
17374 static int recoverVfsClose(sqlite3_file*);
17375 static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
17376 static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
17377 static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
17378 static int recoverVfsSync(sqlite3_file*, int flags);
17379 static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
17380 static int recoverVfsLock(sqlite3_file*, int);
17381 static int recoverVfsUnlock(sqlite3_file*, int);
17382 static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
17383 static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
17384 static int recoverVfsSectorSize(sqlite3_file*);
17385 static int recoverVfsDeviceCharacteristics(sqlite3_file*);
17386 static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
17387 static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
17388 static void recoverVfsShmBarrier(sqlite3_file*);
17389 static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
17390 static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
17391 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
17393 static sqlite3_io_methods recover_methods = {
17398 recoverVfsTruncate,
17400 recoverVfsFileSize,
17403 recoverVfsCheckReservedLock,
17404 recoverVfsFileControl,
17405 recoverVfsSectorSize,
17406 recoverVfsDeviceCharacteristics,
17409 recoverVfsShmBarrier,
17410 recoverVfsShmUnmap,
17415 static int recoverVfsClose(sqlite3_file *pFd){
17416 assert( pFd->pMethods!=&recover_methods );
17417 return pFd->pMethods->xClose(pFd);
17421 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
17423 static void recoverPutU16(u8 *a, u32 v){
17424 a[0] = (v>>8) & 0x00FF;
17425 a[1] = (v>>0) & 0x00FF;
17429 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
17431 static void recoverPutU32(u8 *a, u32 v){
17432 a[0] = (v>>24) & 0x00FF;
17433 a[1] = (v>>16) & 0x00FF;
17434 a[2] = (v>>8) & 0x00FF;
17435 a[3] = (v>>0) & 0x00FF;
17439 ** Detect the page-size of the database opened by file-handle pFd by
17440 ** searching the first part of the file for a well-formed SQLite b-tree
17441 ** page. If parameter nReserve is non-zero, then as well as searching for
17442 ** a b-tree page with zero reserved bytes, this function searches for one
17443 ** with nReserve reserved bytes at the end of it.
17445 ** If successful, set variable p->detected_pgsz to the detected page-size
17446 ** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
17447 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
17448 ** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
17449 ** is returned. The final value of p->detected_pgsz is undefined in this
17452 static int recoverVfsDetectPagesize(
17453 sqlite3_recover *p, /* Recover handle */
17454 sqlite3_file *pFd, /* File-handle open on input database */
17455 u32 nReserve, /* Possible nReserve value */
17456 i64 nSz /* Size of database file in bytes */
17458 int rc = SQLITE_OK;
17459 const int nMin = 512;
17460 const int nMax = 65536;
17461 const int nMaxBlk = 4;
17468 aPg = (u8*)sqlite3_malloc(2*nMax);
17469 if( aPg==0 ) return SQLITE_NOMEM;
17472 nBlk = (nSz+nMax-1)/nMax;
17473 if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
17476 for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
17477 int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
17478 memset(aPg, 0, nMax);
17479 rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
17480 if( rc==SQLITE_OK ){
17482 for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
17484 for(iOff=0; iOff<nMax; iOff+=pgsz2){
17485 if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
17493 if( pgsz>(u32)p->detected_pgsz ){
17494 p->detected_pgsz = pgsz;
17495 p->nReserve = nReserve;
17497 if( nReserve==0 ) break;
17501 p->detected_pgsz = pgsz;
17507 ** The xRead() method of the wrapper VFS. This is used to intercept calls
17508 ** to read page 1 of the input database.
17510 static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
17511 int rc = SQLITE_OK;
17512 if( pFd->pMethods==&recover_methods ){
17513 pFd->pMethods = recover_g.pMethods;
17514 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
17516 sqlite3_randomness(16, aBuf);
17518 if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
17519 /* Ensure that the database has a valid header file. The only fields
17520 ** that really matter to recovery are:
17522 ** + Database page size (16-bits at offset 16)
17523 ** + Size of db in pages (32-bits at offset 28)
17524 ** + Database encoding (32-bits at offset 56)
17526 ** Also preserved are:
17528 ** + first freelist page (32-bits at offset 32)
17529 ** + size of freelist (32-bits at offset 36)
17530 ** + the wal-mode flags (16-bits at offset 18)
17532 ** We also try to preserve the auto-vacuum, incr-value, user-version
17533 ** and application-id fields - all 32 bit quantities at offsets
17534 ** 52, 60, 64 and 68. All other fields are set to known good values.
17536 ** Byte offset 105 should also contain the page-size as a 16-bit
17539 const int aPreserve[] = {32, 36, 52, 60, 64, 68};
17541 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
17542 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
17543 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
17544 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
17545 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17546 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
17547 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
17548 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17549 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17550 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17551 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17552 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17553 0x00, 0x2e, 0x5b, 0x30,
17555 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
17559 u32 pgsz = recoverGetU16(&a[16]);
17560 u32 nReserve = a[20];
17561 u32 enc = recoverGetU32(&a[56]);
17563 i64 dbFileSize = 0;
17565 sqlite3_recover *p = recover_g.p;
17567 if( pgsz==0x01 ) pgsz = 65536;
17568 rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
17570 if( rc==SQLITE_OK && p->detected_pgsz==0 ){
17571 rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
17573 if( p->detected_pgsz ){
17574 pgsz = p->detected_pgsz;
17575 nReserve = p->nReserve;
17579 dbsz = dbFileSize / pgsz;
17581 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
17585 sqlite3_free(p->pPage1Cache);
17586 p->pPage1Cache = 0;
17590 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
17591 if( p->pPage1Cache ){
17592 p->pPage1Disk = &p->pPage1Cache[nByte];
17593 memcpy(p->pPage1Disk, aBuf, nByte);
17596 recoverPutU32(&aHdr[28], dbsz);
17597 recoverPutU32(&aHdr[56], enc);
17598 recoverPutU16(&aHdr[105], pgsz-nReserve);
17599 if( pgsz==65536 ) pgsz = 1;
17600 recoverPutU16(&aHdr[16], pgsz);
17601 aHdr[20] = nReserve;
17602 for(ii=0; ii<(int)(sizeof(aPreserve)/sizeof(aPreserve[0])); ii++){
17603 memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
17605 memcpy(aBuf, aHdr, sizeof(aHdr));
17606 memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
17608 memcpy(p->pPage1Cache, aBuf, nByte);
17614 pFd->pMethods = &recover_methods;
17616 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
17622 ** Used to make sqlite3_io_methods wrapper methods less verbose.
17624 #define RECOVER_VFS_WRAPPER(code) \
17625 int rc = SQLITE_OK; \
17626 if( pFd->pMethods==&recover_methods ){ \
17627 pFd->pMethods = recover_g.pMethods; \
17629 pFd->pMethods = &recover_methods; \
17636 ** Methods of the wrapper VFS. All methods except for xRead() and xClose()
17637 ** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
17638 ** method on the lower level VFS, then reinstall the wrapper before returning.
17639 ** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
17641 static int recoverVfsWrite(
17642 sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
17644 RECOVER_VFS_WRAPPER (
17645 pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
17648 static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
17649 RECOVER_VFS_WRAPPER (
17650 pFd->pMethods->xTruncate(pFd, size)
17653 static int recoverVfsSync(sqlite3_file *pFd, int flags){
17654 RECOVER_VFS_WRAPPER (
17655 pFd->pMethods->xSync(pFd, flags)
17658 static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
17659 RECOVER_VFS_WRAPPER (
17660 pFd->pMethods->xFileSize(pFd, pSize)
17663 static int recoverVfsLock(sqlite3_file *pFd, int eLock){
17664 RECOVER_VFS_WRAPPER (
17665 pFd->pMethods->xLock(pFd, eLock)
17668 static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
17669 RECOVER_VFS_WRAPPER (
17670 pFd->pMethods->xUnlock(pFd, eLock)
17673 static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
17674 RECOVER_VFS_WRAPPER (
17675 pFd->pMethods->xCheckReservedLock(pFd, pResOut)
17678 static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
17679 RECOVER_VFS_WRAPPER (
17680 (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
17683 static int recoverVfsSectorSize(sqlite3_file *pFd){
17684 RECOVER_VFS_WRAPPER (
17685 pFd->pMethods->xSectorSize(pFd)
17688 static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
17689 RECOVER_VFS_WRAPPER (
17690 pFd->pMethods->xDeviceCharacteristics(pFd)
17693 static int recoverVfsShmMap(
17694 sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
17696 RECOVER_VFS_WRAPPER (
17697 pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
17700 static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
17701 RECOVER_VFS_WRAPPER (
17702 pFd->pMethods->xShmLock(pFd, offset, n, flags)
17705 static void recoverVfsShmBarrier(sqlite3_file *pFd){
17706 if( pFd->pMethods==&recover_methods ){
17707 pFd->pMethods = recover_g.pMethods;
17708 pFd->pMethods->xShmBarrier(pFd);
17709 pFd->pMethods = &recover_methods;
17711 pFd->pMethods->xShmBarrier(pFd);
17714 static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
17715 RECOVER_VFS_WRAPPER (
17716 pFd->pMethods->xShmUnmap(pFd, deleteFlag)
17720 static int recoverVfsFetch(
17722 sqlite3_int64 iOff,
17732 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
17740 ** Install the VFS wrapper around the file-descriptor open on the input
17741 ** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
17742 ** when this function is called.
17744 static void recoverInstallWrapper(sqlite3_recover *p){
17745 sqlite3_file *pFd = 0;
17746 assert( recover_g.pMethods==0 );
17747 recoverAssertMutexHeld();
17748 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
17749 assert( pFd==0 || pFd->pMethods!=&recover_methods );
17750 if( pFd && pFd->pMethods ){
17751 int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
17752 recover_g.pMethods = pFd->pMethods;
17754 recover_methods.iVersion = iVersion;
17755 pFd->pMethods = &recover_methods;
17760 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
17761 ** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
17762 ** held when this function is called.
17764 static void recoverUninstallWrapper(sqlite3_recover *p){
17765 sqlite3_file *pFd = 0;
17766 recoverAssertMutexHeld();
17767 sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
17768 if( pFd && pFd->pMethods ){
17769 pFd->pMethods = recover_g.pMethods;
17770 recover_g.pMethods = 0;
17776 ** This function does the work of a single sqlite3_recover_step() call. It
17777 ** is guaranteed that the handle is not in an error state when this
17778 ** function is called.
17780 static void recoverStep(sqlite3_recover *p){
17781 assert( p && p->errCode==SQLITE_OK );
17782 switch( p->eState ){
17783 case RECOVER_STATE_INIT:
17784 /* This is the very first call to sqlite3_recover_step() on this object.
17786 recoverSqlCallback(p, "BEGIN");
17787 recoverSqlCallback(p, "PRAGMA writable_schema = on");
17789 recoverEnterMutex();
17790 recoverInstallWrapper(p);
17792 /* Open the output database. And register required virtual tables and
17793 ** user functions with the new handle. */
17794 recoverOpenOutput(p);
17796 /* Open transactions on both the input and output databases. */
17797 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
17798 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
17799 recoverExec(p, p->dbIn, "BEGIN");
17800 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
17801 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
17802 recoverTransferSettings(p);
17803 recoverOpenRecovery(p);
17804 recoverCacheSchema(p);
17806 recoverUninstallWrapper(p);
17807 recoverLeaveMutex();
17809 recoverExec(p, p->dbOut, "BEGIN");
17811 recoverWriteSchema1(p);
17812 p->eState = RECOVER_STATE_WRITING;
17815 case RECOVER_STATE_WRITING: {
17816 if( p->w1.pTbls==0 ){
17817 recoverWriteDataInit(p);
17819 if( SQLITE_DONE==recoverWriteDataStep(p) ){
17820 recoverWriteDataCleanup(p);
17821 if( p->zLostAndFound ){
17822 p->eState = RECOVER_STATE_LOSTANDFOUND1;
17824 p->eState = RECOVER_STATE_SCHEMA2;
17830 case RECOVER_STATE_LOSTANDFOUND1: {
17831 if( p->laf.pUsed==0 ){
17832 recoverLostAndFound1Init(p);
17834 if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
17835 p->eState = RECOVER_STATE_LOSTANDFOUND2;
17839 case RECOVER_STATE_LOSTANDFOUND2: {
17840 if( p->laf.pAllAndParent==0 ){
17841 recoverLostAndFound2Init(p);
17843 if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
17844 p->eState = RECOVER_STATE_LOSTANDFOUND3;
17849 case RECOVER_STATE_LOSTANDFOUND3: {
17850 if( p->laf.pInsert==0 ){
17851 recoverLostAndFound3Init(p);
17853 if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
17854 p->eState = RECOVER_STATE_SCHEMA2;
17859 case RECOVER_STATE_SCHEMA2: {
17860 int rc = SQLITE_OK;
17862 recoverWriteSchema2(p);
17863 p->eState = RECOVER_STATE_DONE;
17865 /* If no error has occurred, commit the write transaction on the output
17866 ** database. Regardless of whether or not an error has occurred, make
17867 ** an attempt to end the read transaction on the input database. */
17868 recoverExec(p, p->dbOut, "COMMIT");
17869 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
17870 if( p->errCode==SQLITE_OK ) p->errCode = rc;
17872 recoverSqlCallback(p, "PRAGMA writable_schema = off");
17873 recoverSqlCallback(p, "COMMIT");
17874 p->eState = RECOVER_STATE_DONE;
17875 recoverFinalCleanup(p);
17879 case RECOVER_STATE_DONE: {
17888 ** This is a worker function that does the heavy lifting for both init
17891 ** sqlite3_recover_init()
17892 ** sqlite3_recover_init_sql()
17894 ** All this function does is allocate space for the recover handle and
17895 ** take copies of the input parameters. All the real work is done within
17896 ** sqlite3_recover_run().
17898 sqlite3_recover *recoverInit(
17901 const char *zUri, /* Output URI for _recover_init() */
17902 int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
17903 void *pSqlCtx /* Context arg for _recover_init_sql() */
17905 sqlite3_recover *pRet = 0;
17910 if( zDb==0 ){ zDb = "main"; }
17912 nDb = recoverStrlen(zDb);
17913 nUri = recoverStrlen(zUri);
17915 nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
17916 pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
17918 memset(pRet, 0, nByte);
17920 pRet->zDb = (char*)&pRet[1];
17921 pRet->zUri = &pRet->zDb[nDb+1];
17922 memcpy(pRet->zDb, zDb, nDb);
17923 if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
17925 pRet->pSqlCtx = pSqlCtx;
17926 pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
17933 ** Initialize a recovery handle that creates a new database containing
17934 ** the recovered data.
17936 sqlite3_recover *sqlite3_recover_init(
17941 return recoverInit(db, zDb, zUri, 0, 0);
17945 ** Initialize a recovery handle that returns recovered data in the
17946 ** form of SQL statements via a callback.
17948 sqlite3_recover *sqlite3_recover_init_sql(
17951 int (*xSql)(void*, const char*),
17954 return recoverInit(db, zDb, 0, xSql, pSqlCtx);
17958 ** Return the handle error message, if any.
17960 const char *sqlite3_recover_errmsg(sqlite3_recover *p){
17961 return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
17965 ** Return the handle error code.
17967 int sqlite3_recover_errcode(sqlite3_recover *p){
17968 return p ? p->errCode : SQLITE_NOMEM;
17972 ** Configure the handle.
17974 int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
17975 int rc = SQLITE_OK;
17978 }else if( p->eState!=RECOVER_STATE_INIT ){
17979 rc = SQLITE_MISUSE;
17983 /* This undocumented magic configuration option is used to set the
17984 ** name of the auxiliary database that is ATTACH-ed to the database
17985 ** connection and used to hold state information during the
17986 ** recovery process. This option is for debugging use only and
17987 ** is subject to change or removal at any time. */
17988 sqlite3_free(p->zStateDb);
17989 p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
17992 case SQLITE_RECOVER_LOST_AND_FOUND: {
17993 const char *zArg = (const char*)pArg;
17994 sqlite3_free(p->zLostAndFound);
17996 p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
17998 p->zLostAndFound = 0;
18003 case SQLITE_RECOVER_FREELIST_CORRUPT:
18004 p->bFreelistCorrupt = *(int*)pArg;
18007 case SQLITE_RECOVER_ROWIDS:
18008 p->bRecoverRowid = *(int*)pArg;
18011 case SQLITE_RECOVER_SLOWINDEXES:
18012 p->bSlowIndexes = *(int*)pArg;
18016 rc = SQLITE_NOTFOUND;
18025 ** Do a unit of work towards the recovery job. Return SQLITE_OK if
18026 ** no error has occurred but database recovery is not finished, SQLITE_DONE
18027 ** if database recovery has been successfully completed, or an SQLite
18028 ** error code if an error has occurred.
18030 int sqlite3_recover_step(sqlite3_recover *p){
18031 if( p==0 ) return SQLITE_NOMEM;
18032 if( p->errCode==SQLITE_OK ) recoverStep(p);
18033 if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
18034 return SQLITE_DONE;
18040 ** Do the configured recovery operation. Return SQLITE_OK if successful, or
18041 ** else an SQLite error code.
18043 int sqlite3_recover_run(sqlite3_recover *p){
18044 while( SQLITE_OK==sqlite3_recover_step(p) );
18045 return sqlite3_recover_errcode(p);
18050 ** Free all resources associated with the recover handle passed as the only
18051 ** argument. The results of using a handle with any sqlite3_recover_**
18052 ** API function after it has been passed to this function are undefined.
18054 ** A copy of the value returned by the first call made to sqlite3_recover_run()
18055 ** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
18056 ** not been called on this handle.
18058 int sqlite3_recover_finish(sqlite3_recover *p){
18063 recoverFinalCleanup(p);
18064 if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
18065 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
18066 if( p->errCode==SQLITE_OK ) p->errCode = rc;
18069 sqlite3_free(p->zErrMsg);
18070 sqlite3_free(p->zStateDb);
18071 sqlite3_free(p->zLostAndFound);
18072 sqlite3_free(p->pPage1Cache);
18078 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
18080 /************************* End ../ext/recover/sqlite3recover.c ********************/
18081 # endif /* SQLITE_HAVE_SQLITE3R */
18083 #ifdef SQLITE_SHELL_EXTSRC
18084 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
18087 #if defined(SQLITE_ENABLE_SESSION)
18089 ** State information for a single open session
18091 typedef struct OpenSession OpenSession;
18092 struct OpenSession {
18093 char *zName; /* Symbolic name for this session */
18094 int nFilter; /* Number of xFilter rejection GLOB patterns */
18095 char **azFilter; /* Array of xFilter rejection GLOB patterns */
18096 sqlite3_session *p; /* The open session */
18100 typedef struct ExpertInfo ExpertInfo;
18101 struct ExpertInfo {
18102 sqlite3expert *pExpert;
18106 /* A single line in the EQP output */
18107 typedef struct EQPGraphRow EQPGraphRow;
18108 struct EQPGraphRow {
18109 int iEqpId; /* ID for this row */
18110 int iParentId; /* ID of the parent row */
18111 EQPGraphRow *pNext; /* Next row in sequence */
18112 char zText[1]; /* Text to display for this row */
18115 /* All EQP output is collected into an instance of the following */
18116 typedef struct EQPGraph EQPGraph;
18118 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
18119 EQPGraphRow *pLast; /* Last element of the pRow list */
18120 char zPrefix[100]; /* Graph prefix */
18123 /* Parameters affecting columnar mode result display (defaulting together) */
18124 typedef struct ColModeOpts {
18125 int iWrap; /* In columnar modes, wrap lines reaching this limit */
18126 u8 bQuote; /* Quote results for .mode box and table */
18127 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */
18129 #define ColModeOpts_default { 60, 0, 0 }
18130 #define ColModeOpts_default_qbox { 60, 1, 0 }
18133 ** State information about the database connection is contained in an
18134 ** instance of the following structure.
18136 typedef struct ShellState ShellState;
18137 struct ShellState {
18138 sqlite3 *db; /* The database */
18139 u8 autoExplain; /* Automatically turn on .explain mode */
18140 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
18141 u8 autoEQPtest; /* autoEQP is in test mode */
18142 u8 autoEQPtrace; /* autoEQP is in trace mode */
18143 u8 scanstatsOn; /* True to display scan stats before each finalize */
18144 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
18145 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
18146 u8 nEqpLevel; /* Depth of the EQP output graph */
18147 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
18148 u8 bSafeMode; /* True to prohibit unsafe operations */
18149 u8 bSafeModePersist; /* The long-term value of bSafeMode */
18150 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
18151 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
18152 unsigned statsOn; /* True to display memory stats before each finalize */
18153 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
18154 int inputNesting; /* Track nesting level of .read and other redirects */
18155 int outCount; /* Revert to stdout when reaching zero */
18156 int cnt; /* Number of records displayed so far */
18157 int lineno; /* Line number of last line read from in */
18158 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
18159 FILE *in; /* Read commands from this stream */
18160 FILE *out; /* Write results here */
18161 FILE *traceOut; /* Output for sqlite3_trace() */
18162 int nErr; /* Number of errors seen */
18163 int mode; /* An output mode setting */
18164 int modePrior; /* Saved mode */
18165 int cMode; /* temporary output mode for the current query */
18166 int normalMode; /* Output mode before ".explain on" */
18167 int writableSchema; /* True if PRAGMA writable_schema=ON */
18168 int showHeader; /* True to show column names in List or Column mode */
18169 int nCheck; /* Number of ".check" commands run */
18170 unsigned nProgress; /* Number of progress callbacks encountered */
18171 unsigned mxProgress; /* Maximum progress callbacks before failing */
18172 unsigned flgProgress; /* Flags for the progress callback */
18173 unsigned shellFlgs; /* Various flags */
18174 unsigned priorShFlgs; /* Saved copy of flags */
18175 sqlite3_int64 szMax; /* --maxsize argument to .open */
18176 char *zDestTable; /* Name of destination table when MODE_Insert */
18177 char *zTempFile; /* Temporary file that might need deleting */
18178 char zTestcase[30]; /* Name of current test case */
18179 char colSeparator[20]; /* Column separator character for several modes */
18180 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
18181 char colSepPrior[20]; /* Saved column separator */
18182 char rowSepPrior[20]; /* Saved row separator */
18183 int *colWidth; /* Requested width of each column in columnar modes */
18184 int *actualWidth; /* Actual width of each column */
18185 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
18186 char nullValue[20]; /* The text to print when a NULL comes back from
18188 char outfile[FILENAME_MAX]; /* Filename for *out */
18189 sqlite3_stmt *pStmt; /* Current statement if any. */
18190 FILE *pLog; /* Write log output here */
18191 struct AuxDb { /* Storage space for auxiliary database connections */
18192 sqlite3 *db; /* Connection pointer */
18193 const char *zDbFilename; /* Filename used to open the connection */
18194 char *zFreeOnClose; /* Free this memory allocation on close */
18195 #if defined(SQLITE_ENABLE_SESSION)
18196 int nSession; /* Number of active sessions */
18197 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
18199 } aAuxDb[5], /* Array of all database connections */
18200 *pAuxDb; /* Currently active database connection */
18201 int *aiIndent; /* Array of indents used in MODE_Explain */
18202 int nIndent; /* Size of array aiIndent[] */
18203 int iIndent; /* Index of current op in aiIndent[] */
18204 char *zNonce; /* Nonce for temporary safe-mode escapes */
18205 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
18206 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
18207 #ifdef SQLITE_SHELL_FIDDLE
18209 const char * zInput; /* Input string from wasm/JS proxy */
18210 const char * zPos; /* Cursor pos into zInput */
18211 const char * zDefaultDbName; /* Default name for db file */
18216 #ifdef SQLITE_SHELL_FIDDLE
18217 static ShellState shellState;
18221 /* Allowed values for ShellState.autoEQP
18223 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
18224 #define AUTOEQP_on 1 /* Automatic EQP is on */
18225 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
18226 #define AUTOEQP_full 3 /* Show full EXPLAIN */
18228 /* Allowed values for ShellState.openMode
18230 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
18231 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
18232 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
18233 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
18234 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
18235 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
18236 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
18238 /* Allowed values for ShellState.eTraceType
18240 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
18241 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
18242 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
18244 /* Bits in the ShellState.flgProgress variable */
18245 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
18246 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
18247 ** callback limit is reached, and for each
18248 ** top-level SQL statement */
18249 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
18252 ** These are the allowed shellFlgs values
18254 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
18255 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
18256 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
18257 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
18258 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
18259 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
18260 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
18261 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
18262 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
18263 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
18264 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
18267 ** Macros for testing and setting shellFlgs
18269 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
18270 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
18271 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
18274 ** These are the allowed modes.
18276 #define MODE_Line 0 /* One column per line. Blank line between records */
18277 #define MODE_Column 1 /* One record per line in neat columns */
18278 #define MODE_List 2 /* One record per line with a separator */
18279 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
18280 #define MODE_Html 4 /* Generate an XHTML table */
18281 #define MODE_Insert 5 /* Generate SQL "insert" statements */
18282 #define MODE_Quote 6 /* Quote values as for SQL */
18283 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
18284 #define MODE_Csv 8 /* Quote strings, numbers are plain */
18285 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
18286 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
18287 #define MODE_Pretty 11 /* Pretty-print schemas */
18288 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
18289 #define MODE_Json 13 /* Output JSON */
18290 #define MODE_Markdown 14 /* Markdown formatting */
18291 #define MODE_Table 15 /* MySQL-style table formatting */
18292 #define MODE_Box 16 /* Unicode box-drawing characters */
18293 #define MODE_Count 17 /* Output only a count of the rows of output */
18294 #define MODE_Off 18 /* No query output shown */
18295 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
18297 static const char *modeDescr[] = {
18320 ** These are the column/row/line separators used by the various
18321 ** import/export modes.
18323 #define SEP_Column "|"
18324 #define SEP_Row "\n"
18325 #define SEP_Tab "\t"
18326 #define SEP_Space " "
18327 #define SEP_Comma ","
18328 #define SEP_CrLf "\r\n"
18329 #define SEP_Unit "\x1F"
18330 #define SEP_Record "\x1E"
18333 ** Limit input nesting via .read or any other input redirect.
18334 ** It's not too expensive, so a generous allowance can be made.
18336 #define MAX_INPUT_NESTING 25
18339 ** A callback for the sqlite3_log() interface.
18341 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
18342 ShellState *p = (ShellState*)pArg;
18343 if( p->pLog==0 ) return;
18344 sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
18349 ** SQL function: shell_putsnl(X)
18351 ** Write the text X to the screen (or whatever output is being directed)
18352 ** adding a newline at the end, and then return X.
18354 static void shellPutsFunc(
18355 sqlite3_context *pCtx,
18357 sqlite3_value **apVal
18359 /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
18361 oputf("%s\n", sqlite3_value_text(apVal[0]));
18362 sqlite3_result_value(pCtx, apVal[0]);
18366 ** If in safe mode, print an error message described by the arguments
18367 ** and exit immediately.
18369 static void failIfSafeMode(
18371 const char *zErrMsg,
18374 if( p->bSafeMode ){
18377 va_start(ap, zErrMsg);
18378 zMsg = sqlite3_vmprintf(zErrMsg, ap);
18380 eputf("line %d: %s\n", p->lineno, zMsg);
18386 ** SQL function: edit(VALUE)
18387 ** edit(VALUE,EDITOR)
18391 ** (1) Write VALUE into a temporary file.
18392 ** (2) Run program EDITOR on that temporary file.
18393 ** (3) Read the temporary file back and return its content as the result.
18394 ** (4) Delete the temporary file
18396 ** If the EDITOR argument is omitted, use the value in the VISUAL
18397 ** environment variable. If still there is no EDITOR, through an error.
18399 ** Also throw an error if the EDITOR program returns a non-zero exit code.
18401 #ifndef SQLITE_NOHAVE_SYSTEM
18402 static void editFunc(
18403 sqlite3_context *context,
18405 sqlite3_value **argv
18407 const char *zEditor;
18408 char *zTempFile = 0;
18417 unsigned char *p = 0;
18420 zEditor = (const char*)sqlite3_value_text(argv[1]);
18422 zEditor = getenv("VISUAL");
18425 sqlite3_result_error(context, "no editor for edit()", -1);
18428 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
18429 sqlite3_result_error(context, "NULL input to edit()", -1);
18432 db = sqlite3_context_db_handle(context);
18434 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
18435 if( zTempFile==0 ){
18436 sqlite3_uint64 r = 0;
18437 sqlite3_randomness(sizeof(r), &r);
18438 zTempFile = sqlite3_mprintf("temp%llx", r);
18439 if( zTempFile==0 ){
18440 sqlite3_result_error_nomem(context);
18444 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
18445 /* When writing the file to be edited, do \n to \r\n conversions on systems
18446 ** that want \r\n line endings */
18447 f = fopen(zTempFile, bBin ? "wb" : "w");
18449 sqlite3_result_error(context, "edit() cannot open temp file", -1);
18450 goto edit_func_end;
18452 sz = sqlite3_value_bytes(argv[0]);
18454 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
18456 const char *z = (const char*)sqlite3_value_text(argv[0]);
18457 /* Remember whether or not the value originally contained \r\n */
18458 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
18459 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
18464 sqlite3_result_error(context, "edit() could not write the whole file", -1);
18465 goto edit_func_end;
18467 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
18469 sqlite3_result_error_nomem(context);
18470 goto edit_func_end;
18473 sqlite3_free(zCmd);
18475 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
18476 goto edit_func_end;
18478 f = fopen(zTempFile, "rb");
18480 sqlite3_result_error(context,
18481 "edit() cannot reopen temp file after edit", -1);
18482 goto edit_func_end;
18484 fseek(f, 0, SEEK_END);
18487 p = sqlite3_malloc64( sz+1 );
18489 sqlite3_result_error_nomem(context);
18490 goto edit_func_end;
18492 x = fread(p, 1, (size_t)sz, f);
18496 sqlite3_result_error(context, "could not read back the whole file", -1);
18497 goto edit_func_end;
18500 sqlite3_result_blob64(context, p, sz, sqlite3_free);
18502 sqlite3_int64 i, j;
18504 /* If the original contains \r\n then do no conversions back to \n */
18506 /* If the file did not originally contain \r\n then convert any new
18507 ** \r\n back into \n */
18509 for(i=j=0; i<sz; i++){
18510 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
18516 sqlite3_result_text64(context, (const char*)p, sz,
18517 sqlite3_free, SQLITE_UTF8);
18524 sqlite3_free(zTempFile);
18527 #endif /* SQLITE_NOHAVE_SYSTEM */
18530 ** Save or restore the current output mode
18532 static void outputModePush(ShellState *p){
18533 p->modePrior = p->mode;
18534 p->priorShFlgs = p->shellFlgs;
18535 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
18536 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
18538 static void outputModePop(ShellState *p){
18539 p->mode = p->modePrior;
18540 p->shellFlgs = p->priorShFlgs;
18541 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
18542 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
18546 ** Output the given string as a hex-encoded blob (eg. X'1234' )
18548 static void output_hex_blob(const void *pBlob, int nBlob){
18550 unsigned char *aBlob = (unsigned char*)pBlob;
18552 char *zStr = sqlite3_malloc(nBlob*2 + 1);
18553 shell_check_oom(zStr);
18555 for(i=0; i<nBlob; i++){
18556 static const char aHex[] = {
18557 '0', '1', '2', '3', '4', '5', '6', '7',
18558 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
18560 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
18561 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
18565 oputf("X'%s'", zStr);
18566 sqlite3_free(zStr);
18570 ** Find a string that is not found anywhere in z[]. Return a pointer
18573 ** Try to use zA and zB first. If both of those are already found in z[]
18574 ** then make up some string and store it in the buffer zBuf.
18576 static const char *unused_string(
18577 const char *z, /* Result must not appear anywhere in z */
18578 const char *zA, const char *zB, /* Try these first */
18579 char *zBuf /* Space to store a generated string */
18582 if( strstr(z, zA)==0 ) return zA;
18583 if( strstr(z, zB)==0 ) return zB;
18585 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
18586 }while( strstr(z,zBuf)!=0 );
18591 ** Output the given string as a quoted string using SQL quoting conventions.
18593 ** See also: output_quoted_escaped_string()
18595 static void output_quoted_string(const char *z){
18598 #ifndef SQLITE_SHELL_FIDDLE
18599 FILE *pfO = setOutputStream(invalidFileStream);
18600 setBinaryMode(pfO, 1);
18603 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18609 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18612 oputf("%.*s", i, z);
18626 #ifndef SQLITE_SHELL_FIDDLE
18627 setTextMode(pfO, 1);
18629 setTextMode(stdout, 1);
18634 ** Output the given string as a quoted string using SQL quoting conventions.
18635 ** Additionallly , escape the "\n" and "\r" characters so that they do not
18636 ** get corrupted by end-of-line translation facilities in some operating
18639 ** This is like output_quoted_string() but with the addition of the \r\n
18640 ** escape mechanism.
18642 static void output_quoted_escaped_string(const char *z){
18645 #ifndef SQLITE_SHELL_FIDDLE
18646 FILE *pfO = setOutputStream(invalidFileStream);
18647 setBinaryMode(pfO, 1);
18649 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
18653 const char *zNL = 0;
18654 const char *zCR = 0;
18657 char zBuf1[20], zBuf2[20];
18658 for(i=0; z[i]; i++){
18659 if( z[i]=='\n' ) nNL++;
18660 if( z[i]=='\r' ) nCR++;
18664 zNL = unused_string(z, "\\n", "\\012", zBuf1);
18668 zCR = unused_string(z, "\\r", "\\015", zBuf2);
18672 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
18675 oputf("%.*s", i, z);
18694 oputf(",'%s',char(13))", zCR);
18697 oputf(",'%s',char(10))", zNL);
18700 #ifndef SQLITE_SHELL_FIDDLE
18701 setTextMode(pfO, 1);
18703 setTextMode(stdout, 1);
18708 ** Find earliest of chars within s specified in zAny.
18709 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
18711 static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
18712 const char *pcFirst = 0;
18713 if( ns == ~(size_t)0 ) ns = strlen(s);
18715 const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
18725 ** Output the given string as a quoted according to C or TCL quoting rules.
18727 static void output_c_string(const char *z){
18729 static const char *zq = "\"";
18730 static long ctrlMask = ~0L;
18731 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
18732 char ace[3] = "\\?";
18736 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
18737 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
18738 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
18739 if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
18740 if( (c = *pcEnd)==0 ) break;
18743 case '\\': case '"':
18746 case '\t': cbsSay = 't'; break;
18747 case '\n': cbsSay = 'n'; break;
18748 case '\r': cbsSay = 'r'; break;
18749 case '\f': cbsSay = 'f'; break;
18750 default: cbsSay = 0; break;
18755 }else if( !isprint(c&0xff) ){
18756 oputf("\\%03o", c&0xff);
18767 ** Output the given string as a quoted according to JSON quoting rules.
18769 static void output_json_string(const char *z, i64 n){
18771 static const char *zq = "\"";
18772 static long ctrlMask = ~0L;
18773 static const char *zDQBS = "\"\\";
18774 const char *pcLimit;
18775 char ace[3] = "\\?";
18779 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
18781 while( z < pcLimit ){
18782 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
18783 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
18784 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
18786 oputb(z, (int)(pcEnd-z));
18789 if( z >= pcLimit ) break;
18792 case '"': case '\\':
18795 case '\b': cbsSay = 'b'; break;
18796 case '\f': cbsSay = 'f'; break;
18797 case '\n': cbsSay = 'n'; break;
18798 case '\r': cbsSay = 'r'; break;
18799 case '\t': cbsSay = 't'; break;
18800 default: cbsSay = 0; break;
18805 }else if( c<=0x1f ){
18816 ** Output the given string with characters that are special to
18819 static void output_html_string(const char *z){
18835 }else if( z[i]=='&' ){
18837 }else if( z[i]=='>' ){
18839 }else if( z[i]=='\"' ){
18841 }else if( z[i]=='\'' ){
18851 ** If a field contains any character identified by a 1 in the following
18852 ** array, then the string must be quoted for CSV.
18854 static const char needCsvQuote[] = {
18855 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18856 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18857 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
18858 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18859 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18860 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18861 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18862 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
18863 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18864 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18865 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18866 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18867 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18868 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18869 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18870 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18874 ** Output a single term of CSV. Actually, p->colSeparator is used for
18875 ** the separator, which may or may not be a comma. p->nullValue is
18876 ** the null value. Strings are quoted if necessary. The separator
18877 ** is only issued if bSep is true.
18879 static void output_csv(ShellState *p, const char *z, int bSep){
18881 oputf("%s",p->nullValue);
18884 for(i=0; z[i]; i++){
18885 if( needCsvQuote[((unsigned char*)z)[i]] ){
18890 if( i==0 || strstr(z, p->colSeparator)!=0 ){
18891 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
18892 shell_check_oom(zQuoted);
18894 sqlite3_free(zQuoted);
18900 oputz(p->colSeparator);
18905 ** This routine runs when the user presses Ctrl-C
18907 static void interrupt_handler(int NotUsed){
18908 UNUSED_PARAMETER(NotUsed);
18909 if( ++seenInterrupt>1 ) exit(1);
18910 if( globalDb ) sqlite3_interrupt(globalDb);
18913 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
18915 ** This routine runs for console events (e.g. Ctrl-C) on Win32
18917 static BOOL WINAPI ConsoleCtrlHandler(
18918 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
18920 if( dwCtrlType==CTRL_C_EVENT ){
18921 interrupt_handler(0);
18928 #ifndef SQLITE_OMIT_AUTHORIZATION
18930 ** This authorizer runs in safe mode.
18932 static int safeModeAuth(
18940 ShellState *p = (ShellState*)pClientData;
18941 static const char *azProhibitedFunctions[] = {
18950 UNUSED_PARAMETER(zA1);
18951 UNUSED_PARAMETER(zA3);
18952 UNUSED_PARAMETER(zA4);
18954 case SQLITE_ATTACH: {
18955 #ifndef SQLITE_SHELL_FIDDLE
18956 /* In WASM builds the filesystem is a virtual sandbox, so
18957 ** there's no harm in using ATTACH. */
18958 failIfSafeMode(p, "cannot run ATTACH in safe mode");
18962 case SQLITE_FUNCTION: {
18964 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
18965 if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
18966 failIfSafeMode(p, "cannot use the %s() function in safe mode",
18967 azProhibitedFunctions[i]);
18977 ** When the ".auth ON" is set, the following authorizer callback is
18978 ** invoked. It always returns SQLITE_OK.
18980 static int shellAuth(
18988 ShellState *p = (ShellState*)pClientData;
18989 static const char *azAction[] = { 0,
18990 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
18991 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
18992 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
18993 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
18994 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
18995 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
18996 "PRAGMA", "READ", "SELECT",
18997 "TRANSACTION", "UPDATE", "ATTACH",
18998 "DETACH", "ALTER_TABLE", "REINDEX",
18999 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
19000 "FUNCTION", "SAVEPOINT", "RECURSIVE"
19008 oputf("authorizer: %s", azAction[op]);
19009 for(i=0; i<4; i++){
19012 output_c_string(az[i]);
19018 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
19024 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
19026 ** This routine converts some CREATE TABLE statements for shadow tables
19027 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
19029 ** If the schema statement in z[] contains a start-of-comment and if
19030 ** sqlite3_complete() returns false, try to terminate the comment before
19031 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
19033 static void printSchemaLine(const char *z, const char *zTail){
19036 if( zTail==0 ) return;
19037 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
19038 const char *zOrig = z;
19039 static const char *azTerm[] = { "", "*/", "\n" };
19041 for(i=0; i<ArraySize(azTerm); i++){
19042 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
19043 shell_check_oom(zNew);
19044 if( sqlite3_complete(zNew) ){
19045 size_t n = strlen(zNew);
19051 sqlite3_free(zNew);
19054 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
19055 oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
19057 oputf("%s%s", z, zTail);
19059 sqlite3_free(zToFree);
19061 static void printSchemaLineN(char *z, int n, const char *zTail){
19064 printSchemaLine(z, zTail);
19069 ** Return true if string z[] has nothing but whitespace and comments to the
19070 ** end of the first line.
19072 static int wsToEol(const char *z){
19074 for(i=0; z[i]; i++){
19075 if( z[i]=='\n' ) return 1;
19076 if( IsSpace(z[i]) ) continue;
19077 if( z[i]=='-' && z[i+1]=='-' ) return 1;
19084 ** Add a new entry to the EXPLAIN QUERY PLAN data
19086 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
19089 if( zText==0 ) return;
19090 nText = strlen(zText);
19091 if( p->autoEQPtest ){
19092 oputf("%d,%d,%s\n", iEqpId, p2, zText);
19094 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
19095 shell_check_oom(pNew);
19096 pNew->iEqpId = iEqpId;
19097 pNew->iParentId = p2;
19098 memcpy(pNew->zText, zText, nText+1);
19100 if( p->sGraph.pLast ){
19101 p->sGraph.pLast->pNext = pNew;
19103 p->sGraph.pRow = pNew;
19105 p->sGraph.pLast = pNew;
19109 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
19112 static void eqp_reset(ShellState *p){
19113 EQPGraphRow *pRow, *pNext;
19114 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
19115 pNext = pRow->pNext;
19116 sqlite3_free(pRow);
19118 memset(&p->sGraph, 0, sizeof(p->sGraph));
19121 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
19122 ** pOld, or return the first such line if pOld is NULL
19124 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
19125 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
19126 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
19130 /* Render a single level of the graph that has iEqpId as its parent. Called
19131 ** recursively to render sublevels.
19133 static void eqp_render_level(ShellState *p, int iEqpId){
19134 EQPGraphRow *pRow, *pNext;
19135 i64 n = strlen(p->sGraph.zPrefix);
19137 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
19138 pNext = eqp_next_row(p, iEqpId, pRow);
19140 oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
19141 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
19142 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
19143 eqp_render_level(p, pRow->iEqpId);
19144 p->sGraph.zPrefix[n] = 0;
19150 ** Display and reset the EXPLAIN QUERY PLAN data
19152 static void eqp_render(ShellState *p, i64 nCycle){
19153 EQPGraphRow *pRow = p->sGraph.pRow;
19155 if( pRow->zText[0]=='-' ){
19156 if( pRow->pNext==0 ){
19160 oputf("%s\n", pRow->zText+3);
19161 p->sGraph.pRow = pRow->pNext;
19162 sqlite3_free(pRow);
19163 }else if( nCycle>0 ){
19164 oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
19166 oputz("QUERY PLAN\n");
19168 p->sGraph.zPrefix[0] = 0;
19169 eqp_render_level(p, 0);
19174 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19176 ** Progress handler callback.
19178 static int progress_handler(void *pClientData) {
19179 ShellState *p = (ShellState*)pClientData;
19181 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
19182 oputf("Progress limit reached (%u)\n", p->nProgress);
19183 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
19184 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
19187 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
19188 oputf("Progress %u\n", p->nProgress);
19192 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19197 static void print_dashes(int N){
19198 const char zDash[] = "--------------------------------------------------";
19199 const int nDash = sizeof(zDash) - 1;
19204 oputf("%.*s", N, zDash);
19208 ** Print a markdown or table-style row separator using ascii-art
19210 static void print_row_separator(
19218 print_dashes(p->actualWidth[0]+2);
19219 for(i=1; i<nArg; i++){
19221 print_dashes(p->actualWidth[i]+2);
19229 ** This is the callback routine that the shell
19230 ** invokes for each row of a query result.
19232 static int shell_callback(
19234 int nArg, /* Number of result columns */
19235 char **azArg, /* Text of each result column */
19236 char **azCol, /* Column names */
19237 int *aiType /* Column types. Might be NULL */
19240 ShellState *p = (ShellState*)pArg;
19242 if( azArg==0 ) return 0;
19243 switch( p->cMode ){
19250 if( azArg==0 ) break;
19251 for(i=0; i<nArg; i++){
19252 int len = strlen30(azCol[i] ? azCol[i] : "");
19253 if( len>w ) w = len;
19255 if( p->cnt++>0 ) oputz(p->rowSeparator);
19256 for(i=0; i<nArg; i++){
19257 oputf("%*s = %s%s", w, azCol[i],
19258 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
19263 case MODE_Explain: {
19264 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
19265 static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 };
19266 static const int aScanExpWidth[] = {4, 6, 6, 13, 4, 4, 4, 13, 2, 13};
19267 static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
19269 const int *aWidth = aExplainWidth;
19270 const int *aMap = aExplainMap;
19271 int nWidth = ArraySize(aExplainWidth);
19274 if( p->cMode==MODE_ScanExp ){
19275 aWidth = aScanExpWidth;
19276 aMap = aScanExpMap;
19277 nWidth = ArraySize(aScanExpWidth);
19280 if( nArg>nWidth ) nArg = nWidth;
19282 /* If this is the first row seen, print out the headers */
19284 for(i=0; i<nArg; i++){
19285 utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
19286 oputz(i==nArg-1 ? "\n" : " ");
19288 for(i=0; i<nArg; i++){
19289 print_dashes(aWidth[i]);
19290 oputz(i==nArg-1 ? "\n" : " ");
19294 /* If there is no data, exit early. */
19295 if( azArg==0 ) break;
19297 for(i=0; i<nArg; i++){
19298 const char *zSep = " ";
19300 const char *zVal = azArg[ aMap[i] ];
19301 if( i==nArg-1 ) w = 0;
19302 if( zVal && strlenChar(zVal)>w ){
19303 w = strlenChar(zVal);
19306 if( i==iIndent && p->aiIndent && p->pStmt ){
19307 if( p->iIndent<p->nIndent ){
19308 oputf("%*.s", p->aiIndent[p->iIndent], "");
19312 utf8_width_print(w, zVal ? zVal : p->nullValue);
19313 oputz(i==nArg-1 ? "\n" : zSep);
19317 case MODE_Semi: { /* .schema and .fullschema output */
19318 printSchemaLine(azArg[0], ";\n");
19321 case MODE_Pretty: { /* .schema and .fullschema with --indent */
19329 if( azArg[0]==0 ) break;
19330 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
19331 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
19333 oputf("%s;\n", azArg[0]);
19336 z = sqlite3_mprintf("%s", azArg[0]);
19337 shell_check_oom(z);
19339 for(i=0; IsSpace(z[i]); i++){}
19340 for(; (c = z[i])!=0; i++){
19342 if( z[j-1]=='\r' ) z[j-1] = '\n';
19343 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
19344 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
19349 while( j>0 && IsSpace(z[j-1]) ){ j--; }
19351 if( strlen30(z)>=79 ){
19352 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
19355 }else if( c=='"' || c=='\'' || c=='`' ){
19357 }else if( c=='[' ){
19359 }else if( c=='-' && z[i+1]=='-' ){
19361 }else if( c=='(' ){
19363 }else if( c==')' ){
19365 if( nLine>0 && nParen==0 && j>0 ){
19366 printSchemaLineN(z, j, "\n");
19371 if( nParen==1 && cEnd==0
19372 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
19375 printSchemaLineN(z, j, "\n ");
19378 while( IsSpace(z[i+1]) ){ i++; }
19383 printSchemaLine(z, ";\n");
19388 if( p->cnt++==0 && p->showHeader ){
19389 for(i=0; i<nArg; i++){
19390 oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
19393 if( azArg==0 ) break;
19394 for(i=0; i<nArg; i++){
19395 char *z = azArg[i];
19396 if( z==0 ) z = p->nullValue;
19398 oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
19403 if( p->cnt++==0 && p->showHeader ){
19405 for(i=0; i<nArg; i++){
19407 output_html_string(azCol[i]);
19412 if( azArg==0 ) break;
19414 for(i=0; i<nArg; i++){
19416 output_html_string(azArg[i] ? azArg[i] : p->nullValue);
19423 if( p->cnt++==0 && p->showHeader ){
19424 for(i=0; i<nArg; i++){
19425 output_c_string(azCol[i] ? azCol[i] : "");
19426 if(i<nArg-1) oputz(p->colSeparator);
19428 oputz(p->rowSeparator);
19430 if( azArg==0 ) break;
19431 for(i=0; i<nArg; i++){
19432 output_c_string(azArg[i] ? azArg[i] : p->nullValue);
19433 if(i<nArg-1) oputz(p->colSeparator);
19435 oputz(p->rowSeparator);
19439 setBinaryMode(p->out, 1);
19440 if( p->cnt++==0 && p->showHeader ){
19441 for(i=0; i<nArg; i++){
19442 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
19444 oputz(p->rowSeparator);
19447 for(i=0; i<nArg; i++){
19448 output_csv(p, azArg[i], i<nArg-1);
19450 oputz(p->rowSeparator);
19452 setTextMode(p->out, 1);
19455 case MODE_Insert: {
19456 if( azArg==0 ) break;
19457 oputf("INSERT INTO %s",p->zDestTable);
19458 if( p->showHeader ){
19460 for(i=0; i<nArg; i++){
19461 if( i>0 ) oputz(",");
19462 if( quoteChar(azCol[i]) ){
19463 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
19464 shell_check_oom(z);
19468 oputf("%s", azCol[i]);
19474 for(i=0; i<nArg; i++){
19475 oputz(i>0 ? "," : " VALUES(");
19476 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19478 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19479 if( ShellHasFlag(p, SHFLG_Newlines) ){
19480 output_quoted_string(azArg[i]);
19482 output_quoted_escaped_string(azArg[i]);
19484 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19486 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19488 double r = sqlite3_column_double(p->pStmt, i);
19490 memcpy(&ur,&r,sizeof(r));
19491 if( ur==0x7ff0000000000000LL ){
19493 }else if( ur==0xfff0000000000000LL ){
19494 oputz("-9.0e+999");
19496 sqlite3_int64 ir = (sqlite3_int64)r;
19497 if( r==(double)ir ){
19498 sqlite3_snprintf(50,z,"%lld.0", ir);
19500 sqlite3_snprintf(50,z,"%!.20g", r);
19504 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19505 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19506 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19507 output_hex_blob(pBlob, nBlob);
19508 }else if( isNumber(azArg[i], 0) ){
19510 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
19511 output_quoted_string(azArg[i]);
19513 output_quoted_escaped_string(azArg[i]);
19520 if( azArg==0 ) break;
19522 fputs("[{", p->out);
19524 fputs(",\n{", p->out);
19527 for(i=0; i<nArg; i++){
19528 output_json_string(azCol[i], -1);
19530 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19532 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19534 double r = sqlite3_column_double(p->pStmt, i);
19536 memcpy(&ur,&r,sizeof(r));
19537 if( ur==0x7ff0000000000000LL ){
19539 }else if( ur==0xfff0000000000000LL ){
19540 oputz("-9.0e+999");
19542 sqlite3_snprintf(50,z,"%!.20g", r);
19545 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19546 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19547 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19548 output_json_string(pBlob, nBlob);
19549 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19550 output_json_string(azArg[i], -1);
19562 if( azArg==0 ) break;
19563 if( p->cnt==0 && p->showHeader ){
19564 for(i=0; i<nArg; i++){
19565 if( i>0 ) fputs(p->colSeparator, p->out);
19566 output_quoted_string(azCol[i]);
19568 fputs(p->rowSeparator, p->out);
19571 for(i=0; i<nArg; i++){
19572 if( i>0 ) fputs(p->colSeparator, p->out);
19573 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19575 }else if( aiType && aiType[i]==SQLITE_TEXT ){
19576 output_quoted_string(azArg[i]);
19577 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19579 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19581 double r = sqlite3_column_double(p->pStmt, i);
19582 sqlite3_snprintf(50,z,"%!.20g", r);
19584 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19585 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19586 int nBlob = sqlite3_column_bytes(p->pStmt, i);
19587 output_hex_blob(pBlob, nBlob);
19588 }else if( isNumber(azArg[i], 0) ){
19591 output_quoted_string(azArg[i]);
19594 fputs(p->rowSeparator, p->out);
19598 if( p->cnt++==0 && p->showHeader ){
19599 for(i=0; i<nArg; i++){
19600 if( i>0 ) oputz(p->colSeparator);
19601 oputz(azCol[i] ? azCol[i] : "");
19603 oputz(p->rowSeparator);
19605 if( azArg==0 ) break;
19606 for(i=0; i<nArg; i++){
19607 if( i>0 ) oputz(p->colSeparator);
19608 oputz(azArg[i] ? azArg[i] : p->nullValue);
19610 oputz(p->rowSeparator);
19614 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
19622 ** This is the callback routine that the SQLite library
19623 ** invokes for each row of a query result.
19625 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
19626 /* since we don't have type info, call the shell_callback with a NULL value */
19627 return shell_callback(pArg, nArg, azArg, azCol, NULL);
19631 ** This is the callback routine from sqlite3_exec() that appends all
19632 ** output onto the end of a ShellText object.
19634 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
19635 ShellText *p = (ShellText*)pArg;
19637 UNUSED_PARAMETER(az);
19638 if( azArg==0 ) return 0;
19639 if( p->n ) appendText(p, "|", 0);
19640 for(i=0; i<nArg; i++){
19641 if( i ) appendText(p, ",", 0);
19642 if( azArg[i] ) appendText(p, azArg[i], 0);
19648 ** Generate an appropriate SELFTEST table in the main database.
19650 static void createSelftestTable(ShellState *p){
19652 sqlite3_exec(p->db,
19653 "SAVEPOINT selftest_init;\n"
19654 "CREATE TABLE IF NOT EXISTS selftest(\n"
19655 " tno INTEGER PRIMARY KEY,\n" /* Test number */
19656 " op TEXT,\n" /* Operator: memo run */
19657 " cmd TEXT,\n" /* Command text */
19658 " ans TEXT\n" /* Desired answer */
19660 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
19661 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
19662 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
19663 " 'memo','Tests generated by --init');\n"
19664 "INSERT INTO [_shell$self]\n"
19666 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
19667 "FROM sqlite_schema ORDER BY 2'',224))',\n"
19668 " hex(sha3_query('SELECT type,name,tbl_name,sql "
19669 "FROM sqlite_schema ORDER BY 2',224));\n"
19670 "INSERT INTO [_shell$self]\n"
19672 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
19673 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
19674 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
19676 " SELECT name FROM sqlite_schema\n"
19677 " WHERE type='table'\n"
19678 " AND name<>'selftest'\n"
19679 " AND coalesce(rootpage,0)>0\n"
19681 " ORDER BY name;\n"
19682 "INSERT INTO [_shell$self]\n"
19683 " VALUES('run','PRAGMA integrity_check','ok');\n"
19684 "INSERT INTO selftest(tno,op,cmd,ans)"
19685 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
19686 "DROP TABLE [_shell$self];"
19689 eputf("SELFTEST initialization failure: %s\n", zErrMsg);
19690 sqlite3_free(zErrMsg);
19692 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
19697 ** Set the destination table field of the ShellState structure to
19698 ** the name of the table given. Escape any quote characters in the
19701 static void set_table_name(ShellState *p, const char *zName){
19706 if( p->zDestTable ){
19707 free(p->zDestTable);
19710 if( zName==0 ) return;
19711 cQuote = quoteChar(zName);
19712 n = strlen30(zName);
19713 if( cQuote ) n += n+2;
19714 z = p->zDestTable = malloc( n+1 );
19715 shell_check_oom(z);
19717 if( cQuote ) z[n++] = cQuote;
19718 for(i=0; zName[i]; i++){
19720 if( zName[i]==cQuote ) z[n++] = cQuote;
19722 if( cQuote ) z[n++] = cQuote;
19727 ** Maybe construct two lines of text that point out the position of a
19728 ** syntax error. Return a pointer to the text, in memory obtained from
19729 ** sqlite3_malloc(). Or, if the most recent error does not involve a
19730 ** specific token that we can point to, return an empty string.
19732 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
19733 ** and should be released by the caller invoking sqlite3_free().
19735 static char *shell_error_context(const char *zSql, sqlite3 *db){
19743 || (iOffset = sqlite3_error_offset(db))<0
19744 || iOffset>=(int)strlen(zSql)
19746 return sqlite3_mprintf("");
19748 while( iOffset>50 ){
19751 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
19753 len = strlen(zSql);
19756 while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
19758 zCode = sqlite3_mprintf("%.*s", len, zSql);
19759 shell_check_oom(zCode);
19760 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
19762 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,"");
19764 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,"");
19771 ** Execute a query statement that will generate SQL output. Print
19772 ** the result columns, comma-separated, on a line and then add a
19773 ** semicolon terminator to the end of that line.
19775 ** If the number of columns is 1 and that column contains text "--"
19776 ** then write the semicolon on a separate line. That way, if a
19777 ** "--" comment occurs at the end of the statement, the comment
19778 ** won't consume the semicolon terminator.
19780 static int run_table_dump_query(
19781 ShellState *p, /* Query context */
19782 const char *zSelect /* SELECT statement to extract content */
19784 sqlite3_stmt *pSelect;
19789 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
19790 if( rc!=SQLITE_OK || !pSelect ){
19791 char *zContext = shell_error_context(zSelect, p->db);
19792 oputf("/**** ERROR: (%d) %s *****/\n%s",
19793 rc, sqlite3_errmsg(p->db), zContext);
19794 sqlite3_free(zContext);
19795 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19798 rc = sqlite3_step(pSelect);
19799 nResult = sqlite3_column_count(pSelect);
19800 while( rc==SQLITE_ROW ){
19801 z = (const char*)sqlite3_column_text(pSelect, 0);
19803 for(i=1; i<nResult; i++){
19804 oputf(",%s", sqlite3_column_text(pSelect, i));
19807 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
19813 rc = sqlite3_step(pSelect);
19815 rc = sqlite3_finalize(pSelect);
19816 if( rc!=SQLITE_OK ){
19817 oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
19818 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19824 ** Allocate space and save off string indicating current error.
19826 static char *save_err_msg(
19827 sqlite3 *db, /* Database to query */
19828 const char *zPhase, /* When the error occurs */
19829 int rc, /* Error code returned from API */
19830 const char *zSql /* SQL string, or NULL */
19834 sqlite3_str *pStr = sqlite3_str_new(0);
19835 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
19837 sqlite3_str_appendf(pStr, " (%d)", rc);
19839 zContext = shell_error_context(zSql, db);
19841 sqlite3_str_appendall(pStr, zContext);
19842 sqlite3_free(zContext);
19844 zErr = sqlite3_str_finish(pStr);
19845 shell_check_oom(zErr);
19851 ** Attempt to display I/O stats on Linux using /proc/PID/io
19853 static void displayLinuxIoStats(void){
19856 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
19857 in = fopen(z, "rb");
19858 if( in==0 ) return;
19859 while( fgets(z, sizeof(z), in)!=0 ){
19860 static const struct {
19861 const char *zPattern;
19864 { "rchar: ", "Bytes received by read():" },
19865 { "wchar: ", "Bytes sent to write():" },
19866 { "syscr: ", "Read() system calls:" },
19867 { "syscw: ", "Write() system calls:" },
19868 { "read_bytes: ", "Bytes read from storage:" },
19869 { "write_bytes: ", "Bytes written to storage:" },
19870 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
19873 for(i=0; i<ArraySize(aTrans); i++){
19874 int n = strlen30(aTrans[i].zPattern);
19875 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
19876 oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
19886 ** Display a single line of status using 64-bit values.
19888 static void displayStatLine(
19889 char *zLabel, /* Label for this one line */
19890 char *zFormat, /* Format for the result */
19891 int iStatusCtrl, /* Which status to display */
19892 int bReset /* True to reset the stats */
19894 sqlite3_int64 iCur = -1;
19895 sqlite3_int64 iHiwtr = -1;
19898 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
19899 for(i=0, nPercent=0; zFormat[i]; i++){
19900 if( zFormat[i]=='%' ) nPercent++;
19903 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
19905 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
19907 oputf("%-36s %s\n", zLabel, zLine);
19911 ** Display memory stats.
19913 static int display_stats(
19914 sqlite3 *db, /* Database to query */
19915 ShellState *pArg, /* Pointer to ShellState */
19916 int bReset /* True to reset the stats */
19920 if( pArg==0 || pArg->out==0 ) return 0;
19922 if( pArg->pStmt && pArg->statsOn==2 ){
19924 sqlite3_stmt *pStmt = pArg->pStmt;
19926 nCol = sqlite3_column_count(pStmt);
19927 oputf("%-36s %d\n", "Number of output columns:", nCol);
19928 for(i=0; i<nCol; i++){
19929 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
19930 oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
19931 #ifndef SQLITE_OMIT_DECLTYPE
19932 sqlite3_snprintf(30, z+x, "declared type:");
19933 oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
19935 #ifdef SQLITE_ENABLE_COLUMN_METADATA
19936 sqlite3_snprintf(30, z+x, "database name:");
19937 oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
19938 sqlite3_snprintf(30, z+x, "table name:");
19939 oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
19940 sqlite3_snprintf(30, z+x, "origin name:");
19941 oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
19946 if( pArg->statsOn==3 ){
19948 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
19949 oputf("VM-steps: %d\n", iCur);
19954 displayStatLine("Memory Used:",
19955 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
19956 displayStatLine("Number of Outstanding Allocations:",
19957 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
19958 if( pArg->shellFlgs & SHFLG_Pagecache ){
19959 displayStatLine("Number of Pcache Pages Used:",
19960 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
19962 displayStatLine("Number of Pcache Overflow Bytes:",
19963 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
19964 displayStatLine("Largest Allocation:",
19965 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
19966 displayStatLine("Largest Pcache Allocation:",
19967 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
19968 #ifdef YYTRACKMAXSTACKDEPTH
19969 displayStatLine("Deepest Parser Stack:",
19970 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
19974 if( pArg->shellFlgs & SHFLG_Lookaside ){
19975 iHiwtr = iCur = -1;
19976 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
19977 &iCur, &iHiwtr, bReset);
19978 oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
19979 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
19980 &iCur, &iHiwtr, bReset);
19981 oputf("Successful lookaside attempts: %d\n", iHiwtr);
19982 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
19983 &iCur, &iHiwtr, bReset);
19984 oputf("Lookaside failures due to size: %d\n", iHiwtr);
19985 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
19986 &iCur, &iHiwtr, bReset);
19987 oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
19989 iHiwtr = iCur = -1;
19990 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
19991 oputf("Pager Heap Usage: %d bytes\n", iCur);
19992 iHiwtr = iCur = -1;
19993 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
19994 oputf("Page cache hits: %d\n", iCur);
19995 iHiwtr = iCur = -1;
19996 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
19997 oputf("Page cache misses: %d\n", iCur);
19998 iHiwtr = iCur = -1;
19999 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
20000 oputf("Page cache writes: %d\n", iCur);
20001 iHiwtr = iCur = -1;
20002 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
20003 oputf("Page cache spills: %d\n", iCur);
20004 iHiwtr = iCur = -1;
20005 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
20006 oputf("Schema Heap Usage: %d bytes\n", iCur);
20007 iHiwtr = iCur = -1;
20008 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
20009 oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
20014 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
20016 oputf("Fullscan Steps: %d\n", iCur);
20017 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
20018 oputf("Sort Operations: %d\n", iCur);
20019 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
20020 oputf("Autoindex Inserts: %d\n", iCur);
20021 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
20023 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
20025 if( iHit || iMiss ){
20026 oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
20028 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
20029 oputf("Virtual Machine Steps: %d\n", iCur);
20030 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
20031 oputf("Reprepare operations: %d\n", iCur);
20032 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
20033 oputf("Number of times run: %d\n", iCur);
20034 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
20035 oputf("Memory used by prepared stmt: %d\n", iCur);
20039 displayLinuxIoStats();
20042 /* Do not remove this machine readable comment: extra-stats-output-here */
20048 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20049 static int scanStatsHeight(sqlite3_stmt *p, int iEntry){
20052 sqlite3_stmt_scanstatus_v2(p, iEntry,
20053 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
20057 for(ii=0; 1; ii++){
20060 res = sqlite3_stmt_scanstatus_v2(p, ii,
20061 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
20065 sqlite3_stmt_scanstatus_v2(p, ii,
20066 SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
20076 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20077 static void display_explain_scanstats(
20078 sqlite3 *db, /* Database to query */
20079 ShellState *pArg /* Pointer to ShellState */
20081 static const int f = SQLITE_SCANSTAT_COMPLEX;
20082 sqlite3_stmt *p = pArg->pStmt;
20088 for(ii=0; 1; ii++){
20091 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
20094 n = (int)strlen(z) + scanStatsHeight(p, ii)*3;
20095 if( n>nWidth ) nWidth = n;
20099 sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
20100 for(ii=0; 1; ii++){
20106 const char *zo = 0;
20107 const char *zName = 0;
20111 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
20114 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
20115 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
20116 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
20117 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
20118 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
20119 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
20120 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
20122 zText = sqlite3_mprintf("%s", zo);
20123 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
20125 if( nCycle>=0 && nTotal>0 ){
20126 z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
20127 nCycle, ((nCycle*100)+nTotal/2) / nTotal
20131 z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop);
20134 z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow);
20137 if( zName && pArg->scanstatsOn>1 ){
20138 double rpl = (double)nRow / (double)nLoop;
20139 z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst);
20142 zText = sqlite3_mprintf(
20143 "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z
20147 eqp_append(pArg, iId, iPid, zText);
20148 sqlite3_free(zText);
20151 eqp_render(pArg, nTotal);
20157 ** Parameter azArray points to a zero-terminated array of strings. zStr
20158 ** points to a single nul-terminated string. Return non-zero if zStr
20159 ** is equal, according to strcmp(), to any of the strings in the array.
20160 ** Otherwise, return zero.
20162 static int str_in_array(const char *zStr, const char **azArray){
20164 for(i=0; azArray[i]; i++){
20165 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
20171 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
20172 ** and populate the ShellState.aiIndent[] array with the number of
20173 ** spaces each opcode should be indented before it is output.
20175 ** The indenting rules are:
20177 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
20178 ** all opcodes that occur between the p2 jump destination and the opcode
20179 ** itself by 2 spaces.
20181 ** * Do the previous for "Return" instructions for when P2 is positive.
20182 ** See tag-20220407a in wherecode.c and vdbe.c.
20184 ** * For each "Goto", if the jump destination is earlier in the program
20185 ** and ends on one of:
20186 ** Yield SeekGt SeekLt RowSetRead Rewind
20187 ** or if the P1 parameter is one instead of zero,
20188 ** then indent all opcodes between the earlier instruction
20189 ** and "Goto" by 2 spaces.
20191 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
20192 int *abYield = 0; /* True if op is an OP_Yield */
20193 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
20194 int iOp; /* Index of operation in p->aiIndent[] */
20196 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
20198 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
20200 const char *azGoto[] = { "Goto", 0 };
20202 /* The caller guarantees that the leftmost 4 columns of the statement
20203 ** passed to this function are equivalent to the leftmost 4 columns
20204 ** of EXPLAIN statement output. In practice the statement may be
20205 ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
20206 assert( sqlite3_column_count(pSql)>=4 );
20207 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) );
20208 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) );
20209 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) );
20210 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) );
20212 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
20214 int iAddr = sqlite3_column_int(pSql, 0);
20215 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
20216 int p1 = sqlite3_column_int(pSql, 2);
20217 int p2 = sqlite3_column_int(pSql, 3);
20219 /* Assuming that p2 is an instruction address, set variable p2op to the
20220 ** index of that instruction in the aiIndent[] array. p2 and p2op may be
20221 ** different if the current instruction is part of a sub-program generated
20222 ** by an SQL trigger or foreign key. */
20223 int p2op = (p2 + (iOp-iAddr));
20225 /* Grow the p->aiIndent array as required */
20228 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
20229 shell_check_oom(p->aiIndent);
20230 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
20231 shell_check_oom(abYield);
20234 abYield[iOp] = str_in_array(zOp, azYield);
20235 p->aiIndent[iOp] = 0;
20236 p->nIndent = iOp+1;
20237 if( str_in_array(zOp, azNext) && p2op>0 ){
20238 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
20240 if( str_in_array(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
20241 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
20246 sqlite3_free(abYield);
20247 sqlite3_reset(pSql);
20251 ** Free the array allocated by explain_data_prepare().
20253 static void explain_data_delete(ShellState *p){
20254 sqlite3_free(p->aiIndent);
20260 static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
20263 ** Display scan stats.
20265 static void display_scanstats(
20266 sqlite3 *db, /* Database to query */
20267 ShellState *pArg /* Pointer to ShellState */
20269 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20270 UNUSED_PARAMETER(db);
20271 UNUSED_PARAMETER(pArg);
20273 if( pArg->scanstatsOn==3 ){
20275 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
20276 " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
20277 " FROM bytecode(?)";
20279 int rc = SQLITE_OK;
20280 sqlite3_stmt *pStmt = 0;
20281 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
20282 if( rc==SQLITE_OK ){
20283 sqlite3_stmt *pSave = pArg->pStmt;
20284 pArg->pStmt = pStmt;
20285 sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
20288 pArg->cMode = MODE_ScanExp;
20289 explain_data_prepare(pArg, pStmt);
20290 exec_prepared_stmt(pArg, pStmt);
20291 explain_data_delete(pArg);
20293 sqlite3_finalize(pStmt);
20294 pArg->pStmt = pSave;
20297 display_explain_scanstats(db, pArg);
20303 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
20305 static unsigned int savedSelectTrace;
20306 static unsigned int savedWhereTrace;
20307 static void disable_debug_trace_modes(void){
20308 unsigned int zero = 0;
20309 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
20310 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
20311 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
20312 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
20314 static void restore_debug_trace_modes(void){
20315 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
20316 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
20319 /* Create the TEMP table used to store parameter bindings */
20320 static void bind_table_init(ShellState *p){
20322 int defensiveMode = 0;
20323 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
20324 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
20325 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
20326 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
20327 sqlite3_exec(p->db,
20328 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
20329 " key TEXT PRIMARY KEY,\n"
20331 ") WITHOUT ROWID;",
20333 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
20334 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
20338 ** Bind parameters on a prepared statement.
20340 ** Parameter bindings are taken from a TEMP table of the form:
20342 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
20345 ** No bindings occur if this table does not exist. The name of the table
20346 ** begins with "sqlite_" so that it will not collide with ordinary application
20347 ** tables. The table must be in the TEMP schema.
20349 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
20353 sqlite3_stmt *pQ = 0;
20355 nVar = sqlite3_bind_parameter_count(pStmt);
20356 if( nVar==0 ) return; /* Nothing to do */
20357 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
20358 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
20359 rc = SQLITE_NOTFOUND;
20362 rc = sqlite3_prepare_v2(pArg->db,
20363 "SELECT value FROM temp.sqlite_parameters"
20364 " WHERE key=?1", -1, &pQ, 0);
20366 for(i=1; i<=nVar; i++){
20368 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
20370 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
20373 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
20374 if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
20375 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
20377 }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
20378 sqlite3_bind_double(pStmt, i, NAN);
20381 }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
20382 sqlite3_bind_double(pStmt, i, INFINITY);
20385 sqlite3_bind_null(pStmt, i);
20389 sqlite3_finalize(pQ);
20393 ** UTF8 box-drawing characters. Imagine box lines like this:
20401 ** Each box characters has between 2 and 4 of the lines leading from
20402 ** the center. The characters are here identified by the numbers of
20403 ** their corresponding lines.
20405 #define BOX_24 "\342\224\200" /* U+2500 --- */
20406 #define BOX_13 "\342\224\202" /* U+2502 | */
20407 #define BOX_23 "\342\224\214" /* U+250c ,- */
20408 #define BOX_34 "\342\224\220" /* U+2510 -, */
20409 #define BOX_12 "\342\224\224" /* U+2514 '- */
20410 #define BOX_14 "\342\224\230" /* U+2518 -' */
20411 #define BOX_123 "\342\224\234" /* U+251c |- */
20412 #define BOX_134 "\342\224\244" /* U+2524 -| */
20413 #define BOX_234 "\342\224\254" /* U+252c -,- */
20414 #define BOX_124 "\342\224\264" /* U+2534 -'- */
20415 #define BOX_1234 "\342\224\274" /* U+253c -|- */
20417 /* Draw horizontal line N characters long using unicode box
20420 static void print_box_line(int N){
20421 const char zDash[] =
20422 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
20423 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
20424 const int nDash = sizeof(zDash) - 1;
20430 oputf("%.*s", N, zDash);
20434 ** Draw a horizontal separator for a MODE_Box table.
20436 static void print_box_row_separator(
20446 print_box_line(p->actualWidth[0]+2);
20447 for(i=1; i<nArg; i++){
20449 print_box_line(p->actualWidth[i]+2);
20457 ** z[] is a line of text that is to be displayed the .mode box or table or
20458 ** similar tabular formats. z[] might contain control characters such
20459 ** as \n, \t, \f, or \r.
20461 ** Compute characters to display on the first line of z[]. Stop at the
20462 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
20463 ** from malloc()) of that first line, which caller should free sometime.
20464 ** Write anything to display on the next line into *pzTail. If this is
20465 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
20467 static char *translateForDisplayAndDup(
20468 const unsigned char *z, /* Input text to be transformed */
20469 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
20470 int mxWidth, /* Max width. 0 means no limit */
20471 u8 bWordWrap /* If true, avoid breaking mid-word */
20473 int i; /* Input bytes consumed */
20474 int j; /* Output bytes generated */
20475 int k; /* Input bytes to be displayed */
20476 int n; /* Output column number */
20477 unsigned char *zOut; /* Output text */
20483 if( mxWidth<0 ) mxWidth = -mxWidth;
20484 if( mxWidth==0 ) mxWidth = 1000000;
20486 while( n<mxWidth ){
20489 do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
20496 }while( (n&7)!=0 && n<mxWidth );
20502 if( n>=mxWidth && bWordWrap ){
20503 /* Perhaps try to back up to a better place to break the line */
20504 for(k=i; k>i/2; k--){
20505 if( isspace(z[k-1]) ) break;
20508 for(k=i; k>i/2; k--){
20509 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
20516 while( z[i]==' ' ) i++;
20521 if( n>=mxWidth && z[i]>=' ' ){
20523 }else if( z[i]=='\r' && z[i+1]=='\n' ){
20524 *pzTail = z[i+2] ? &z[i+2] : 0;
20525 }else if( z[i]==0 || z[i+1]==0 ){
20530 zOut = malloc( j+1 );
20531 shell_check_oom(zOut);
20536 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
20543 }while( (n&7)!=0 && n<mxWidth );
20550 return (char*)zOut;
20553 /* Extract the value of the i-th current column for pStmt as an SQL literal
20554 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
20557 static char *quoted_column(sqlite3_stmt *pStmt, int i){
20558 switch( sqlite3_column_type(pStmt, i) ){
20559 case SQLITE_NULL: {
20560 return sqlite3_mprintf("NULL");
20562 case SQLITE_INTEGER:
20563 case SQLITE_FLOAT: {
20564 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
20566 case SQLITE_TEXT: {
20567 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
20569 case SQLITE_BLOB: {
20571 sqlite3_str *pStr = sqlite3_str_new(0);
20572 const unsigned char *a = sqlite3_column_blob(pStmt,i);
20573 int n = sqlite3_column_bytes(pStmt,i);
20574 sqlite3_str_append(pStr, "x'", 2);
20575 for(j=0; j<n; j++){
20576 sqlite3_str_appendf(pStr, "%02x", a[j]);
20578 sqlite3_str_append(pStr, "'", 1);
20579 return sqlite3_str_finish(pStr);
20582 return 0; /* Not reached */
20586 ** Run a prepared statement and output the result in one of the
20587 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
20590 ** This is different from ordinary exec_prepared_stmt() in that
20591 ** it has to run the entire query and gather the results into memory
20592 ** first, in order to determine column widths, before providing
20595 static void exec_prepared_stmt_columnar(
20596 ShellState *p, /* Pointer to ShellState */
20597 sqlite3_stmt *pStmt /* Statement to run */
20599 sqlite3_int64 nRow = 0;
20602 sqlite3_int64 nAlloc = 0;
20603 char *abRowDiv = 0;
20604 const unsigned char *uz;
20606 char **azQuoted = 0;
20608 sqlite3_int64 i, nData;
20609 int j, nTotal, w, n;
20610 const char *colSep = 0;
20611 const char *rowSep = 0;
20612 const unsigned char **azNextLine = 0;
20614 int bMultiLineRowExists = 0;
20615 int bw = p->cmOpts.bWordWrap;
20616 const char *zEmpty = "";
20617 const char *zShowNull = p->nullValue;
20619 rc = sqlite3_step(pStmt);
20620 if( rc!=SQLITE_ROW ) return;
20621 nColumn = sqlite3_column_count(pStmt);
20622 nAlloc = nColumn*4;
20623 if( nAlloc<=0 ) nAlloc = 1;
20624 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
20625 shell_check_oom(azData);
20626 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
20627 shell_check_oom(azNextLine);
20628 memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
20629 if( p->cmOpts.bQuote ){
20630 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
20631 shell_check_oom(azQuoted);
20632 memset(azQuoted, 0, nColumn*sizeof(char*) );
20634 abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
20635 shell_check_oom(abRowDiv);
20636 if( nColumn>p->nWidth ){
20637 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
20638 shell_check_oom(p->colWidth);
20639 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
20640 p->nWidth = nColumn;
20641 p->actualWidth = &p->colWidth[nColumn];
20643 memset(p->actualWidth, 0, nColumn*sizeof(int));
20644 for(i=0; i<nColumn; i++){
20645 w = p->colWidth[i];
20647 p->actualWidth[i] = w;
20649 for(i=0; i<nColumn; i++){
20650 const unsigned char *zNotUsed;
20651 int wx = p->colWidth[i];
20653 wx = p->cmOpts.iWrap;
20655 if( wx<0 ) wx = -wx;
20656 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
20657 if( uz==0 ) uz = (u8*)"";
20658 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
20661 int useNextLine = bNextLine;
20663 if( (nRow+2)*nColumn >= nAlloc ){
20665 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
20666 shell_check_oom(azData);
20667 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
20668 shell_check_oom(abRowDiv);
20670 abRowDiv[nRow] = 1;
20672 for(i=0; i<nColumn; i++){
20673 int wx = p->colWidth[i];
20675 wx = p->cmOpts.iWrap;
20677 if( wx<0 ) wx = -wx;
20679 uz = azNextLine[i];
20680 if( uz==0 ) uz = (u8*)zEmpty;
20681 }else if( p->cmOpts.bQuote ){
20682 sqlite3_free(azQuoted[i]);
20683 azQuoted[i] = quoted_column(pStmt,i);
20684 uz = (const unsigned char*)azQuoted[i];
20686 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
20687 if( uz==0 ) uz = (u8*)zShowNull;
20689 azData[nRow*nColumn + i]
20690 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
20691 if( azNextLine[i] ){
20693 abRowDiv[nRow-1] = 0;
20694 bMultiLineRowExists = 1;
20697 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
20698 nTotal = nColumn*(nRow+1);
20699 for(i=0; i<nTotal; i++){
20701 if( z==0 ) z = (char*)zEmpty;
20704 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
20706 if( seenInterrupt ) goto columnar_end;
20707 if( nColumn==0 ) goto columnar_end;
20708 switch( p->cMode ){
20709 case MODE_Column: {
20712 if( p->showHeader ){
20713 for(i=0; i<nColumn; i++){
20714 w = p->actualWidth[i];
20715 if( p->colWidth[i]<0 ) w = -w;
20716 utf8_width_print(w, azData[i]);
20717 fputs(i==nColumn-1?"\n":" ", p->out);
20719 for(i=0; i<nColumn; i++){
20720 print_dashes(p->actualWidth[i]);
20721 fputs(i==nColumn-1?"\n":" ", p->out);
20729 print_row_separator(p, nColumn, "+");
20730 fputs("| ", p->out);
20731 for(i=0; i<nColumn; i++){
20732 w = p->actualWidth[i];
20733 n = strlenChar(azData[i]);
20734 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20735 oputz(i==nColumn-1?" |\n":" | ");
20737 print_row_separator(p, nColumn, "+");
20740 case MODE_Markdown: {
20743 fputs("| ", p->out);
20744 for(i=0; i<nColumn; i++){
20745 w = p->actualWidth[i];
20746 n = strlenChar(azData[i]);
20747 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20748 oputz(i==nColumn-1?" |\n":" | ");
20750 print_row_separator(p, nColumn, "|");
20754 colSep = " " BOX_13 " ";
20755 rowSep = " " BOX_13 "\n";
20756 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
20758 for(i=0; i<nColumn; i++){
20759 w = p->actualWidth[i];
20760 n = strlenChar(azData[i]);
20761 oputf("%*s%s%*s%s",
20762 (w-n)/2, "", azData[i], (w-n+1)/2, "",
20763 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
20765 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20769 for(i=nColumn, j=0; i<nTotal; i++, j++){
20770 if( j==0 && p->cMode!=MODE_Column ){
20771 oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
20774 if( z==0 ) z = p->nullValue;
20775 w = p->actualWidth[j];
20776 if( p->colWidth[j]<0 ) w = -w;
20777 utf8_width_print(w, z);
20778 if( j==nColumn-1 ){
20780 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
20781 if( p->cMode==MODE_Table ){
20782 print_row_separator(p, nColumn, "+");
20783 }else if( p->cMode==MODE_Box ){
20784 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20785 }else if( p->cMode==MODE_Column ){
20790 if( seenInterrupt ) goto columnar_end;
20795 if( p->cMode==MODE_Table ){
20796 print_row_separator(p, nColumn, "+");
20797 }else if( p->cMode==MODE_Box ){
20798 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
20801 if( seenInterrupt ){
20802 oputz("Interrupt\n");
20804 nData = (nRow+1)*nColumn;
20805 for(i=0; i<nData; i++){
20807 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
20809 sqlite3_free(azData);
20810 sqlite3_free((void*)azNextLine);
20811 sqlite3_free(abRowDiv);
20813 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
20814 sqlite3_free(azQuoted);
20819 ** Run a prepared statement
20821 static void exec_prepared_stmt(
20822 ShellState *pArg, /* Pointer to ShellState */
20823 sqlite3_stmt *pStmt /* Statement to run */
20826 sqlite3_uint64 nRow = 0;
20828 if( pArg->cMode==MODE_Column
20829 || pArg->cMode==MODE_Table
20830 || pArg->cMode==MODE_Box
20831 || pArg->cMode==MODE_Markdown
20833 exec_prepared_stmt_columnar(pArg, pStmt);
20837 /* perform the first step. this will tell us if we
20838 ** have a result set or not and how wide it is.
20840 rc = sqlite3_step(pStmt);
20841 /* if we have a result set... */
20842 if( SQLITE_ROW == rc ){
20843 /* allocate space for col name ptr, value ptr, and type */
20844 int nCol = sqlite3_column_count(pStmt);
20845 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
20847 shell_out_of_memory();
20849 char **azCols = (char **)pData; /* Names of result columns */
20850 char **azVals = &azCols[nCol]; /* Results */
20851 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
20853 assert(sizeof(int) <= sizeof(char *));
20854 /* save off ptrs to column names */
20855 for(i=0; i<nCol; i++){
20856 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
20860 /* extract the data and data types */
20861 for(i=0; i<nCol; i++){
20862 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
20865 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
20869 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
20871 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
20873 break; /* from for */
20877 /* if data and types extracted successfully... */
20878 if( SQLITE_ROW == rc ){
20879 /* call the supplied callback with the result row data */
20880 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
20883 rc = sqlite3_step(pStmt);
20886 } while( SQLITE_ROW == rc );
20887 sqlite3_free(pData);
20888 if( pArg->cMode==MODE_Json ){
20889 fputs("]\n", pArg->out);
20890 }else if( pArg->cMode==MODE_Count ){
20892 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
20893 nRow, nRow!=1 ? "s" : "");
20894 printf("%s", zBuf);
20900 #ifndef SQLITE_OMIT_VIRTUALTABLE
20902 ** This function is called to process SQL if the previous shell command
20903 ** was ".expert". It passes the SQL in the second argument directly to
20904 ** the sqlite3expert object.
20906 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
20907 ** code. In this case, (*pzErr) may be set to point to a buffer containing
20908 ** an English language error message. It is the responsibility of the
20909 ** caller to eventually free this buffer using sqlite3_free().
20911 static int expertHandleSQL(
20912 ShellState *pState,
20916 assert( pState->expert.pExpert );
20917 assert( pzErr==0 || *pzErr==0 );
20918 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
20922 ** This function is called either to silently clean up the object
20923 ** created by the ".expert" command (if bCancel==1), or to generate a
20924 ** report from it and then clean it up (if bCancel==0).
20926 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
20927 ** code. In this case, (*pzErr) may be set to point to a buffer containing
20928 ** an English language error message. It is the responsibility of the
20929 ** caller to eventually free this buffer using sqlite3_free().
20931 static int expertFinish(
20932 ShellState *pState,
20936 int rc = SQLITE_OK;
20937 sqlite3expert *p = pState->expert.pExpert;
20939 assert( bCancel || pzErr==0 || *pzErr==0 );
20941 int bVerbose = pState->expert.bVerbose;
20943 rc = sqlite3_expert_analyze(p, pzErr);
20944 if( rc==SQLITE_OK ){
20945 int nQuery = sqlite3_expert_count(p);
20949 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
20950 oputz("-- Candidates -----------------------------\n");
20951 oputf("%s\n", zCand);
20953 for(i=0; i<nQuery; i++){
20954 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
20955 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
20956 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
20957 if( zIdx==0 ) zIdx = "(no new indexes)\n";
20959 oputf("-- Query %d --------------------------------\n",i+1);
20960 oputf("%s\n\n", zSql);
20962 oputf("%s\n", zIdx);
20963 oputf("%s\n", zEQP);
20967 sqlite3_expert_destroy(p);
20968 pState->expert.pExpert = 0;
20973 ** Implementation of ".expert" dot command.
20975 static int expertDotCommand(
20976 ShellState *pState, /* Current shell tool state */
20977 char **azArg, /* Array of arguments passed to dot command */
20978 int nArg /* Number of entries in azArg[] */
20980 int rc = SQLITE_OK;
20985 assert( pState->expert.pExpert==0 );
20986 memset(&pState->expert, 0, sizeof(ExpertInfo));
20988 for(i=1; rc==SQLITE_OK && i<nArg; i++){
20989 char *z = azArg[i];
20991 if( z[0]=='-' && z[1]=='-' ) z++;
20993 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
20994 pState->expert.bVerbose = 1;
20996 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
20998 eputf("option requires an argument: %s\n", z);
21001 iSample = (int)integerValue(azArg[++i]);
21002 if( iSample<0 || iSample>100 ){
21003 eputf("value out of range: %s\n", azArg[i]);
21009 eputf("unknown option: %s\n", z);
21014 if( rc==SQLITE_OK ){
21015 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
21016 if( pState->expert.pExpert==0 ){
21017 eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
21020 sqlite3_expert_config(
21021 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
21025 sqlite3_free(zErr);
21029 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
21032 ** Execute a statement or set of statements. Print
21033 ** any result rows/columns depending on the current mode
21034 ** set via the supplied callback.
21036 ** This is very similar to SQLite's built-in sqlite3_exec()
21037 ** function except it takes a slightly different callback
21038 ** and callback data argument.
21040 static int shell_exec(
21041 ShellState *pArg, /* Pointer to ShellState */
21042 const char *zSql, /* SQL to be evaluated */
21043 char **pzErrMsg /* Error msg written here */
21045 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
21046 int rc = SQLITE_OK; /* Return Code */
21048 const char *zLeftover; /* Tail of unprocessed SQL */
21049 sqlite3 *db = pArg->db;
21055 #ifndef SQLITE_OMIT_VIRTUALTABLE
21056 if( pArg->expert.pExpert ){
21057 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
21058 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
21062 while( zSql[0] && (SQLITE_OK == rc) ){
21063 static const char *zStmtSql;
21064 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
21065 if( SQLITE_OK != rc ){
21067 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
21071 /* this happens for a comment or white-space */
21073 while( IsSpace(zSql[0]) ) zSql++;
21076 zStmtSql = sqlite3_sql(pStmt);
21077 if( zStmtSql==0 ) zStmtSql = "";
21078 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
21080 /* save off the prepared statement handle and reset row count */
21082 pArg->pStmt = pStmt;
21086 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
21087 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
21088 sqlite3_stmt *pExplain;
21089 int triggerEQP = 0;
21090 disable_debug_trace_modes();
21091 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
21092 if( pArg->autoEQP>=AUTOEQP_trigger ){
21093 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
21096 sqlite3_reset(pExplain);
21097 rc = sqlite3_stmt_explain(pExplain, 2);
21098 if( rc==SQLITE_OK ){
21099 while( sqlite3_step(pExplain)==SQLITE_ROW ){
21100 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
21101 int iEqpId = sqlite3_column_int(pExplain, 0);
21102 int iParentId = sqlite3_column_int(pExplain, 1);
21103 if( zEQPLine==0 ) zEQPLine = "";
21104 if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
21105 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
21107 eqp_render(pArg, 0);
21109 if( pArg->autoEQP>=AUTOEQP_full ){
21110 /* Also do an EXPLAIN for ".eqp full" mode */
21111 sqlite3_reset(pExplain);
21112 rc = sqlite3_stmt_explain(pExplain, 1);
21113 if( rc==SQLITE_OK ){
21114 pArg->cMode = MODE_Explain;
21115 assert( sqlite3_stmt_isexplain(pExplain)==1 );
21116 explain_data_prepare(pArg, pExplain);
21117 exec_prepared_stmt(pArg, pExplain);
21118 explain_data_delete(pArg);
21121 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
21122 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
21124 sqlite3_reset(pStmt);
21125 sqlite3_stmt_explain(pStmt, 0);
21126 restore_debug_trace_modes();
21130 int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
21131 pArg->cMode = pArg->mode;
21132 if( pArg->autoExplain ){
21134 pArg->cMode = MODE_Explain;
21136 if( sqlite3_stmt_isexplain(pStmt)==2 ){
21137 pArg->cMode = MODE_EQP;
21141 /* If the shell is currently in ".explain" mode, gather the extra
21142 ** data required to add indents to the output.*/
21143 if( pArg->cMode==MODE_Explain && bIsExplain ){
21144 explain_data_prepare(pArg, pStmt);
21148 bind_prepared_stmt(pArg, pStmt);
21149 exec_prepared_stmt(pArg, pStmt);
21150 explain_data_delete(pArg);
21151 eqp_render(pArg, 0);
21153 /* print usage stats if stats on */
21154 if( pArg && pArg->statsOn ){
21155 display_stats(db, pArg, 0);
21158 /* print loop-counters if required */
21159 if( pArg && pArg->scanstatsOn ){
21160 display_scanstats(db, pArg);
21163 /* Finalize the statement just executed. If this fails, save a
21164 ** copy of the error message. Otherwise, set zSql to point to the
21165 ** next statement to execute. */
21166 rc2 = sqlite3_finalize(pStmt);
21167 if( rc!=SQLITE_NOMEM ) rc = rc2;
21168 if( rc==SQLITE_OK ){
21170 while( IsSpace(zSql[0]) ) zSql++;
21171 }else if( pzErrMsg ){
21172 *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
21175 /* clear saved stmt handle */
21177 pArg->pStmt = NULL;
21186 ** Release memory previously allocated by tableColumnList().
21188 static void freeColumnList(char **azCol){
21190 for(i=1; azCol[i]; i++){
21191 sqlite3_free(azCol[i]);
21193 /* azCol[0] is a static string */
21194 sqlite3_free(azCol);
21198 ** Return a list of pointers to strings which are the names of all
21199 ** columns in table zTab. The memory to hold the names is dynamically
21200 ** allocated and must be released by the caller using a subsequent call
21201 ** to freeColumnList().
21203 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
21204 ** value that needs to be preserved, then azCol[0] is filled in with the
21205 ** name of the rowid column.
21207 ** The first regular column in the table is azCol[1]. The list is terminated
21208 ** by an entry with azCol[i]==0.
21210 static char **tableColumnList(ShellState *p, const char *zTab){
21212 sqlite3_stmt *pStmt;
21216 int nPK = 0; /* Number of PRIMARY KEY columns seen */
21217 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
21218 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
21221 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
21222 shell_check_oom(zSql);
21223 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21224 sqlite3_free(zSql);
21226 while( sqlite3_step(pStmt)==SQLITE_ROW ){
21227 if( nCol>=nAlloc-2 ){
21228 nAlloc = nAlloc*2 + nCol + 10;
21229 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
21230 shell_check_oom(azCol);
21232 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
21233 shell_check_oom(azCol[nCol]);
21234 if( sqlite3_column_int(pStmt, 5) ){
21237 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
21246 sqlite3_finalize(pStmt);
21247 if( azCol==0 ) return 0;
21251 /* The decision of whether or not a rowid really needs to be preserved
21252 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
21253 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
21254 ** rowids on tables where the rowid is inaccessible because there are other
21255 ** columns in the table named "rowid", "_rowid_", and "oid".
21257 if( preserveRowid && isIPK ){
21258 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
21259 ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID
21260 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
21261 ** ROWID aliases. To distinguish these cases, check to see if
21262 ** there is a "pk" entry in "PRAGMA index_list". There will be
21263 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
21265 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
21266 " WHERE origin='pk'", zTab);
21267 shell_check_oom(zSql);
21268 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21269 sqlite3_free(zSql);
21271 freeColumnList(azCol);
21274 rc = sqlite3_step(pStmt);
21275 sqlite3_finalize(pStmt);
21276 preserveRowid = rc==SQLITE_ROW;
21278 if( preserveRowid ){
21279 /* Only preserve the rowid if we can find a name to use for the
21281 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
21283 for(j=0; j<3; j++){
21284 for(i=1; i<=nCol; i++){
21285 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
21288 /* At this point, we know that azRowid[j] is not the name of any
21289 ** ordinary column in the table. Verify that azRowid[j] is a valid
21290 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
21291 ** tables will fail this last check */
21292 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
21293 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
21302 ** Toggle the reverse_unordered_selects setting.
21304 static void toggleSelectOrder(sqlite3 *db){
21305 sqlite3_stmt *pStmt = 0;
21308 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
21309 if( sqlite3_step(pStmt)==SQLITE_ROW ){
21310 iSetting = sqlite3_column_int(pStmt, 0);
21312 sqlite3_finalize(pStmt);
21313 sqlite3_snprintf(sizeof(zStmt), zStmt,
21314 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
21315 sqlite3_exec(db, zStmt, 0, 0, 0);
21319 ** This is a different callback routine used for dumping the database.
21320 ** Each row received by this callback consists of a table name,
21321 ** the table type ("index" or "table") and SQL to create the table.
21322 ** This routine should print text sufficient to recreate the table.
21324 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
21326 const char *zTable;
21329 ShellState *p = (ShellState *)pArg;
21333 UNUSED_PARAMETER(azNotUsed);
21334 if( nArg!=3 || azArg==0 ) return 0;
21338 if( zTable==0 ) return 0;
21339 if( zType==0 ) return 0;
21340 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
21341 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
21343 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
21344 if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
21345 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
21346 if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
21347 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
21349 }else if( dataOnly ){
21351 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
21353 if( !p->writableSchema ){
21354 oputz("PRAGMA writable_schema=ON;\n");
21355 p->writableSchema = 1;
21357 zIns = sqlite3_mprintf(
21358 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
21359 "VALUES('table','%q','%q',0,'%q');",
21360 zTable, zTable, zSql);
21361 shell_check_oom(zIns);
21362 oputf("%s\n", zIns);
21363 sqlite3_free(zIns);
21366 printSchemaLine(zSql, ";\n");
21369 if( cli_strcmp(zType, "table")==0 ){
21374 char *savedDestTable;
21377 azCol = tableColumnList(p, zTable);
21383 /* Always quote the table name, even if it appears to be pure ascii,
21384 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
21386 appendText(&sTable, zTable, quoteChar(zTable));
21387 /* If preserving the rowid, add a column list after the table name.
21388 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
21389 ** instead of the usual "INSERT INTO tab VALUES(...)".
21392 appendText(&sTable, "(", 0);
21393 appendText(&sTable, azCol[0], 0);
21394 for(i=1; azCol[i]; i++){
21395 appendText(&sTable, ",", 0);
21396 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
21398 appendText(&sTable, ")", 0);
21401 /* Build an appropriate SELECT statement */
21402 initText(&sSelect);
21403 appendText(&sSelect, "SELECT ", 0);
21405 appendText(&sSelect, azCol[0], 0);
21406 appendText(&sSelect, ",", 0);
21408 for(i=1; azCol[i]; i++){
21409 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
21411 appendText(&sSelect, ",", 0);
21414 freeColumnList(azCol);
21415 appendText(&sSelect, " FROM ", 0);
21416 appendText(&sSelect, zTable, quoteChar(zTable));
21418 savedDestTable = p->zDestTable;
21419 savedMode = p->mode;
21420 p->zDestTable = sTable.z;
21421 p->mode = p->cMode = MODE_Insert;
21422 rc = shell_exec(p, sSelect.z, 0);
21423 if( (rc&0xff)==SQLITE_CORRUPT ){
21424 oputz("/****** CORRUPTION ERROR *******/\n");
21425 toggleSelectOrder(p->db);
21426 shell_exec(p, sSelect.z, 0);
21427 toggleSelectOrder(p->db);
21429 p->zDestTable = savedDestTable;
21430 p->mode = savedMode;
21432 freeText(&sSelect);
21433 if( rc ) p->nErr++;
21439 ** Run zQuery. Use dump_callback() as the callback routine so that
21440 ** the contents of the query are output as SQL statements.
21442 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
21443 ** "ORDER BY rowid DESC" to the end.
21445 static int run_schema_dump_query(
21451 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
21452 if( rc==SQLITE_CORRUPT ){
21454 int len = strlen30(zQuery);
21455 oputz("/****** CORRUPTION ERROR *******/\n");
21457 oputf("/****** %s ******/\n", zErr);
21458 sqlite3_free(zErr);
21461 zQ2 = malloc( len+100 );
21462 if( zQ2==0 ) return rc;
21463 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
21464 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
21466 oputf("/****** ERROR: %s ******/\n", zErr);
21468 rc = SQLITE_CORRUPT;
21470 sqlite3_free(zErr);
21477 ** Text of help messages.
21479 ** The help text for each individual command begins with a line that starts
21480 ** with ".". Subsequent lines are supplemental information.
21482 ** There must be two or more spaces between the end of the command and the
21483 ** start of the description of what that command does.
21485 static const char *(azHelp[]) = {
21486 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
21487 && !defined(SQLITE_SHELL_FIDDLE)
21488 ".archive ... Manage SQL archives",
21489 " Each command must have exactly one of the following options:",
21490 " -c, --create Create a new archive",
21491 " -u, --update Add or update files with changed mtime",
21492 " -i, --insert Like -u but always add even if unchanged",
21493 " -r, --remove Remove files from archive",
21494 " -t, --list List contents of archive",
21495 " -x, --extract Extract files from archive",
21496 " Optional arguments:",
21497 " -v, --verbose Print each filename as it is processed",
21498 " -f FILE, --file FILE Use archive FILE (default is current db)",
21499 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
21500 " -C DIR, --directory DIR Read/extract files from directory DIR",
21501 " -g, --glob Use glob matching for names in archive",
21502 " -n, --dryrun Show the SQL that would have occurred",
21504 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
21505 " .ar -tf ARCHIVE # List members of ARCHIVE",
21506 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
21508 " http://sqlite.org/cli.html#sqlite_archive_support",
21510 #ifndef SQLITE_OMIT_AUTHORIZATION
21511 ".auth ON|OFF Show authorizer callbacks",
21513 #ifndef SQLITE_SHELL_FIDDLE
21514 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
21516 " --append Use the appendvfs",
21517 " --async Write to FILE without journal and fsync()",
21519 ".bail on|off Stop after hitting an error. Default OFF",
21520 #ifndef SQLITE_SHELL_FIDDLE
21521 ".cd DIRECTORY Change the working directory to DIRECTORY",
21523 ".changes on|off Show number of rows changed by SQL",
21524 #ifndef SQLITE_SHELL_FIDDLE
21525 ".check GLOB Fail if output since .testcase does not match",
21526 ".clone NEWDB Clone data into NEWDB from the existing database",
21528 ".connection [close] [#] Open or close an auxiliary database connection",
21529 #if defined(_WIN32) || defined(WIN32)
21530 ".crnl on|off Translate \\n to \\r\\n. Default ON",
21532 ".databases List names and files of attached databases",
21533 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
21534 #if SQLITE_SHELL_HAVE_RECOVER
21535 ".dbinfo ?DB? Show status information about the database",
21537 ".dump ?OBJECTS? Render database content as SQL",
21539 " --data-only Output only INSERT statements",
21540 " --newlines Allow unescaped newline characters in output",
21541 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
21542 " --preserve-rowids Include ROWID values in the output",
21543 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
21544 " Additional LIKE patterns can be given in subsequent arguments",
21545 ".echo on|off Turn command echo on or off",
21546 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
21548 #ifdef SQLITE_DEBUG
21549 " test Show raw EXPLAIN QUERY PLAN output",
21550 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
21552 " trigger Like \"full\" but also show trigger bytecode",
21553 #ifndef SQLITE_SHELL_FIDDLE
21554 ".excel Display the output of next command in spreadsheet",
21555 " --bom Put a UTF8 byte-order mark on intermediate file",
21557 #ifndef SQLITE_SHELL_FIDDLE
21558 ".exit ?CODE? Exit this program with return-code CODE",
21560 ".expert EXPERIMENTAL. Suggest indexes for queries",
21561 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
21562 ".filectrl CMD ... Run various sqlite3_file_control() operations",
21563 " --schema SCHEMA Use SCHEMA instead of \"main\"",
21564 " --help Show CMD details",
21565 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
21566 ".headers on|off Turn display of headers on or off",
21567 ".help ?-all? ?PATTERN? Show help text for PATTERN",
21568 #ifndef SQLITE_SHELL_FIDDLE
21569 ".import FILE TABLE Import data from FILE into TABLE",
21571 " --ascii Use \\037 and \\036 as column and row separators",
21572 " --csv Use , and \\n as column and row separators",
21573 " --skip N Skip the first N rows of input",
21574 " --schema S Target table to be S.TABLE",
21575 " -v \"Verbose\" - increase auxiliary output",
21577 " * If TABLE does not exist, it is created. The first row of input",
21578 " determines the column names.",
21579 " * If neither --csv or --ascii are used, the input mode is derived",
21580 " from the \".mode\" output mode",
21581 " * If FILE begins with \"|\" then it is a command that generates the",
21584 #ifndef SQLITE_OMIT_TEST_CONTROL
21585 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
21587 ".indexes ?TABLE? Show names of indexes",
21588 " If TABLE is specified, only show indexes for",
21589 " tables matching TABLE using the LIKE operator.",
21590 #ifdef SQLITE_ENABLE_IOTRACE
21591 ",iotrace FILE Enable I/O diagnostic logging to FILE",
21593 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
21594 ".lint OPTIONS Report potential schema issues.",
21596 " fkey-indexes Find missing foreign key indexes",
21597 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
21598 ".load FILE ?ENTRY? Load an extension library",
21600 #if !defined(SQLITE_SHELL_FIDDLE)
21601 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
21603 ".log on|off Turn logging on or off.",
21605 ".mode MODE ?OPTIONS? Set output mode",
21606 " MODE is one of:",
21607 " ascii Columns/rows delimited by 0x1F and 0x1E",
21608 " box Tables using unicode box-drawing characters",
21609 " csv Comma-separated values",
21610 " column Output in columns. (See .width)",
21611 " html HTML <table> code",
21612 " insert SQL insert statements for TABLE",
21613 " json Results in a JSON array",
21614 " line One value per line",
21615 " list Values delimited by \"|\"",
21616 " markdown Markdown table format",
21617 " qbox Shorthand for \"box --wrap 60 --quote\"",
21618 " quote Escape answers as for SQL",
21619 " table ASCII-art table",
21620 " tabs Tab-separated values",
21621 " tcl TCL list elements",
21622 " OPTIONS: (for columnar modes or insert mode):",
21623 " --wrap N Wrap output lines to no longer than N characters",
21624 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
21625 " --ww Shorthand for \"--wordwrap 1\"",
21626 " --quote Quote output text as SQL literals",
21627 " --noquote Do not quote output text",
21628 " TABLE The name of SQL table used for \"insert\" mode",
21629 #ifndef SQLITE_SHELL_FIDDLE
21630 ".nonce STRING Suspend safe mode for one command if nonce matches",
21632 ".nullvalue STRING Use STRING in place of NULL values",
21633 #ifndef SQLITE_SHELL_FIDDLE
21634 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
21635 " If FILE begins with '|' then open as a pipe",
21636 " --bom Put a UTF8 byte-order mark at the beginning",
21637 " -e Send output to the system text editor",
21638 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
21639 /* Note that .open is (partially) available in WASM builds but is
21640 ** currently only intended to be used by the fiddle tool, not
21641 ** end users, so is "undocumented." */
21642 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
21644 " --append Use appendvfs to append database to the end of FILE",
21646 #ifndef SQLITE_OMIT_DESERIALIZE
21647 " --deserialize Load into memory using sqlite3_deserialize()",
21648 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
21649 " --maxsize N Maximum size for --hexdb or --deserialized database",
21651 " --new Initialize FILE to an empty database",
21652 " --nofollow Do not follow symbolic links",
21653 " --readonly Open FILE readonly",
21654 " --zip FILE is a ZIP archive",
21655 #ifndef SQLITE_SHELL_FIDDLE
21656 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
21657 " If FILE begins with '|' then open it as a pipe.",
21659 " --bom Prefix output with a UTF8 byte-order mark",
21660 " -e Send output to the system text editor",
21661 " -x Send output as CSV to a spreadsheet",
21663 ".parameter CMD ... Manage SQL parameter bindings",
21664 " clear Erase all bindings",
21665 " init Initialize the TEMP table that holds bindings",
21666 " list List the current parameter bindings",
21667 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
21668 " PARAMETER should start with one of: $ : @ ?",
21669 " unset PARAMETER Remove PARAMETER from the binding table",
21670 ".print STRING... Print literal STRING",
21671 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
21672 ".progress N Invoke progress handler after every N opcodes",
21673 " --limit N Interrupt after N progress callbacks",
21674 " --once Do no more than one progress interrupt",
21675 " --quiet|-q No output except at interrupts",
21676 " --reset Reset the count for each input and interrupt",
21678 ".prompt MAIN CONTINUE Replace the standard prompts",
21679 #ifndef SQLITE_SHELL_FIDDLE
21680 ".quit Stop interpreting input stream, exit if primary.",
21681 ".read FILE Read input from FILE or command output",
21682 " If FILE begins with \"|\", it is a command that generates the input.",
21684 #if SQLITE_SHELL_HAVE_RECOVER
21685 ".recover Recover as much data as possible from corrupt db.",
21686 " --ignore-freelist Ignore pages that appear to be on db freelist",
21687 " --lost-and-found TABLE Alternative name for the lost-and-found table",
21688 " --no-rowids Do not attempt to recover rowid values",
21689 " that are not also INTEGER PRIMARY KEYs",
21691 #ifndef SQLITE_SHELL_FIDDLE
21692 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
21693 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
21695 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
21696 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
21698 " --indent Try to pretty-print the schema",
21699 " --nosys Omit objects whose names start with \"sqlite_\"",
21700 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
21702 " --init Create a new SELFTEST table",
21703 " -v Verbose output",
21704 ".separator COL ?ROW? Change the column and row separators",
21705 #if defined(SQLITE_ENABLE_SESSION)
21706 ".session ?NAME? CMD ... Create or control sessions",
21708 " attach TABLE Attach TABLE",
21709 " changeset FILE Write a changeset into FILE",
21710 " close Close one session",
21711 " enable ?BOOLEAN? Set or query the enable bit",
21712 " filter GLOB... Reject tables matching GLOBs",
21713 " indirect ?BOOLEAN? Mark or query the indirect status",
21714 " isempty Query whether the session is empty",
21715 " list List currently open session names",
21716 " open DB NAME Open a new session on DB",
21717 " patchset FILE Write a patchset into FILE",
21718 " If ?NAME? is omitted, the first defined session is used.",
21720 ".sha3sum ... Compute a SHA3 hash of database content",
21722 " --schema Also hash the sqlite_schema table",
21723 " --sha3-224 Use the sha3-224 algorithm",
21724 " --sha3-256 Use the sha3-256 algorithm (default)",
21725 " --sha3-384 Use the sha3-384 algorithm",
21726 " --sha3-512 Use the sha3-512 algorithm",
21727 " Any other argument is a LIKE pattern for tables to hash",
21728 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21729 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
21731 ".show Show the current values for various settings",
21732 ".stats ?ARG? Show stats or turn stats on or off",
21733 " off Turn off automatic stat display",
21734 " on Turn on automatic stat display",
21735 " stmt Show statement stats",
21736 " vmstep Show the virtual machine step count only",
21737 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21738 ".system CMD ARGS... Run CMD ARGS... in a system shell",
21740 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
21741 #ifndef SQLITE_SHELL_FIDDLE
21742 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
21744 ",testctrl CMD ... Run various sqlite3_test_control() operations",
21745 " Run \".testctrl\" with no arguments for details",
21746 ".timeout MS Try opening locked tables for MS milliseconds",
21747 ".timer on|off Turn SQL timer on or off",
21748 #ifndef SQLITE_OMIT_TRACE
21749 ".trace ?OPTIONS? Output each SQL statement as it is run",
21750 " FILE Send output to FILE",
21751 " stdout Send output to stdout",
21752 " stderr Send output to stderr",
21753 " off Disable tracing",
21754 " --expanded Expand query parameters",
21755 #ifdef SQLITE_ENABLE_NORMALIZE
21756 " --normalized Normal the SQL statements",
21758 " --plain Show SQL as it is input",
21759 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
21760 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
21761 " --row Trace each row (SQLITE_TRACE_ROW)",
21762 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
21763 #endif /* SQLITE_OMIT_TRACE */
21764 #ifdef SQLITE_DEBUG
21765 ".unmodule NAME ... Unregister virtual table modules",
21766 " --allexcept Unregister everything except those named",
21768 ".version Show source, library and compiler versions",
21769 ".vfsinfo ?AUX? Information about the top-level VFS",
21770 ".vfslist List all available VFSes",
21771 ".vfsname ?AUX? Print the name of the VFS stack",
21772 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
21773 " Negative values right-justify",
21777 ** Output help text.
21779 ** zPattern describes the set of commands for which help text is provided.
21780 ** If zPattern is NULL, then show all commands, but only give a one-line
21781 ** description of each.
21783 ** Return the number of matches.
21785 static int showHelp(FILE *out, const char *zPattern){
21791 || zPattern[0]=='0'
21792 || cli_strcmp(zPattern,"-a")==0
21793 || cli_strcmp(zPattern,"-all")==0
21794 || cli_strcmp(zPattern,"--all")==0
21796 enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
21797 enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
21798 /* Show all or most commands
21799 ** *zPattern==0 => summary of documented commands only
21800 ** *zPattern=='0' => whole help for undocumented commands
21801 ** Otherwise => whole help for documented commands
21803 enum HelpWanted hw = HW_SummaryOnly;
21804 enum HelpHave hh = HH_More;
21806 hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
21808 for(i=0; i<ArraySize(azHelp); i++){
21809 switch( azHelp[i][0] ){
21811 hh = HH_Summary|HH_Undoc;
21820 if( ((hw^hh)&HH_Undoc)==0 ){
21821 if( (hh&HH_Summary)!=0 ){
21822 sputf(out, ".%s\n", azHelp[i]+1);
21824 }else if( (hw&HW_SummaryOnly)==0 ){
21825 sputf(out, "%s\n", azHelp[i]);
21830 /* Seek documented commands for which zPattern is an exact prefix */
21831 zPat = sqlite3_mprintf(".%s*", zPattern);
21832 shell_check_oom(zPat);
21833 for(i=0; i<ArraySize(azHelp); i++){
21834 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
21835 sputf(out, "%s\n", azHelp[i]);
21840 sqlite3_free(zPat);
21843 /* when zPattern is a prefix of exactly one command, then include
21844 ** the details of that command, which should begin at offset j */
21845 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
21846 sputf(out, "%s\n", azHelp[j]);
21852 /* Look for documented commands that contain zPattern anywhere.
21853 ** Show complete text of all documented commands that match. */
21854 zPat = sqlite3_mprintf("%%%s%%", zPattern);
21855 shell_check_oom(zPat);
21856 for(i=0; i<ArraySize(azHelp); i++){
21857 if( azHelp[i][0]==',' ){
21858 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
21861 if( azHelp[i][0]=='.' ) j = i;
21862 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
21863 sputf(out, "%s\n", azHelp[j]);
21864 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
21866 sputf(out, "%s\n", azHelp[j]);
21872 sqlite3_free(zPat);
21877 /* Forward reference */
21878 static int process_input(ShellState *p);
21881 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
21882 ** and return a pointer to the buffer. The caller is responsible for freeing
21885 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
21888 ** For convenience, a nul-terminator byte is always appended to the data read
21889 ** from the file before the buffer is returned. This byte is not included in
21890 ** the final value of (*pnByte), if applicable.
21892 ** NULL is returned if any error is encountered. The final value of *pnByte
21893 ** is undefined in this case.
21895 static char *readFile(const char *zName, int *pnByte){
21896 FILE *in = fopen(zName, "rb");
21901 if( in==0 ) return 0;
21902 rc = fseek(in, 0, SEEK_END);
21904 eputf("Error: '%s' not seekable\n", zName);
21910 pBuf = sqlite3_malloc64( nIn+1 );
21912 eputz("Error: out of memory\n");
21916 nRead = fread(pBuf, nIn, 1, in);
21919 sqlite3_free(pBuf);
21920 eputf("Error: cannot read '%s'\n", zName);
21924 if( pnByte ) *pnByte = nIn;
21928 #if defined(SQLITE_ENABLE_SESSION)
21930 ** Close a single OpenSession object and release all of its associated
21933 static void session_close(OpenSession *pSession){
21935 sqlite3session_delete(pSession->p);
21936 sqlite3_free(pSession->zName);
21937 for(i=0; i<pSession->nFilter; i++){
21938 sqlite3_free(pSession->azFilter[i]);
21940 sqlite3_free(pSession->azFilter);
21941 memset(pSession, 0, sizeof(OpenSession));
21946 ** Close all OpenSession objects and release all associated resources.
21948 #if defined(SQLITE_ENABLE_SESSION)
21949 static void session_close_all(ShellState *p, int i){
21951 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
21952 for(j=0; j<pAuxDb->nSession; j++){
21953 session_close(&pAuxDb->aSession[j]);
21955 pAuxDb->nSession = 0;
21958 # define session_close_all(X,Y)
21962 ** Implementation of the xFilter function for an open session. Omit
21963 ** any tables named by ".session filter" but let all other table through.
21965 #if defined(SQLITE_ENABLE_SESSION)
21966 static int session_filter(void *pCtx, const char *zTab){
21967 OpenSession *pSession = (OpenSession*)pCtx;
21969 for(i=0; i<pSession->nFilter; i++){
21970 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
21977 ** Try to deduce the type of file for zName based on its content. Return
21978 ** one of the SHELL_OPEN_* constants.
21980 ** If the file does not exist or is empty but its name looks like a ZIP
21981 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
21982 ** Otherwise, assume an ordinary database regardless of the filename if
21983 ** the type cannot be determined from content.
21985 int deduceDatabaseType(const char *zName, int dfltZip){
21986 FILE *f = fopen(zName, "rb");
21988 int rc = SHELL_OPEN_UNSPEC;
21991 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
21992 return SHELL_OPEN_ZIPFILE;
21994 return SHELL_OPEN_NORMAL;
21997 n = fread(zBuf, 16, 1, f);
21998 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
22000 return SHELL_OPEN_NORMAL;
22002 fseek(f, -25, SEEK_END);
22003 n = fread(zBuf, 25, 1, f);
22004 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
22005 rc = SHELL_OPEN_APPENDVFS;
22007 fseek(f, -22, SEEK_END);
22008 n = fread(zBuf, 22, 1, f);
22009 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
22010 && zBuf[3]==0x06 ){
22011 rc = SHELL_OPEN_ZIPFILE;
22012 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
22013 rc = SHELL_OPEN_ZIPFILE;
22020 #ifndef SQLITE_OMIT_DESERIALIZE
22022 ** Reconstruct an in-memory database using the output from the "dbtotxt"
22023 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
22024 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
22026 static unsigned char *readHexDb(ShellState *p, int *pnData){
22027 unsigned char *a = 0;
22035 const char *zDbFilename = p->pAuxDb->zDbFilename;
22036 unsigned int x[16];
22039 in = fopen(zDbFilename, "r");
22041 eputf("cannot open \"%s\" for reading\n", zDbFilename);
22048 if( in==0 ) in = stdin;
22052 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
22053 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
22054 if( rc!=2 ) goto readHexDb_error;
22055 if( n<0 ) goto readHexDb_error;
22056 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
22057 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
22058 a = sqlite3_malloc( n ? n : 1 );
22059 shell_check_oom(a);
22061 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
22062 eputz("invalid pagesize\n");
22063 goto readHexDb_error;
22065 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
22066 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
22071 if( cli_strncmp(zLine, "| end ", 6)==0 ){
22074 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
22075 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
22076 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
22079 if( k+16<=n && k>=0 ){
22081 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
22097 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
22099 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
22104 eputf("Error on line %d of --hexdb input\n", nLine);
22107 #endif /* SQLITE_OMIT_DESERIALIZE */
22110 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
22112 static void shellUSleepFunc(
22113 sqlite3_context *context,
22115 sqlite3_value **argv
22117 int sleep = sqlite3_value_int(argv[0]);
22119 sqlite3_sleep(sleep/1000);
22120 sqlite3_result_int(context, sleep);
22123 /* Flags for open_db().
22125 ** The default behavior of open_db() is to exit(1) if the database fails to
22126 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
22127 ** but still returns without calling exit.
22129 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
22130 ** ZIP archive if the file does not exist or is empty and its name matches
22131 ** the *.zip pattern.
22133 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
22134 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
22137 ** Make sure the database is open. If it is not, then open it. If
22138 ** the database fails to open, print an error message and exit.
22140 static void open_db(ShellState *p, int openFlags){
22142 const char *zDbFilename = p->pAuxDb->zDbFilename;
22143 if( p->openMode==SHELL_OPEN_UNSPEC ){
22144 if( zDbFilename==0 || zDbFilename[0]==0 ){
22145 p->openMode = SHELL_OPEN_NORMAL;
22147 p->openMode = (u8)deduceDatabaseType(zDbFilename,
22148 (openFlags & OPEN_DB_ZIPFILE)!=0);
22151 switch( p->openMode ){
22152 case SHELL_OPEN_APPENDVFS: {
22153 sqlite3_open_v2(zDbFilename, &p->db,
22154 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
22157 case SHELL_OPEN_HEXDB:
22158 case SHELL_OPEN_DESERIALIZE: {
22159 sqlite3_open(0, &p->db);
22162 case SHELL_OPEN_ZIPFILE: {
22163 sqlite3_open(":memory:", &p->db);
22166 case SHELL_OPEN_READONLY: {
22167 sqlite3_open_v2(zDbFilename, &p->db,
22168 SQLITE_OPEN_READONLY|p->openFlags, 0);
22171 case SHELL_OPEN_UNSPEC:
22172 case SHELL_OPEN_NORMAL: {
22173 sqlite3_open_v2(zDbFilename, &p->db,
22174 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
22178 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22179 eputf("Error: unable to open database \"%s\": %s\n",
22180 zDbFilename, sqlite3_errmsg(p->db));
22181 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
22184 sqlite3_close(p->db);
22185 sqlite3_open(":memory:", &p->db);
22186 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22187 eputz("Also: unable to open substitute in-memory database.\n");
22190 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22195 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
22197 /* Reflect the use or absence of --unsafe-testing invocation. */
22199 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
22200 sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
22201 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
22204 #ifndef SQLITE_OMIT_LOAD_EXTENSION
22205 sqlite3_enable_load_extension(p->db, 1);
22207 sqlite3_shathree_init(p->db, 0, 0);
22208 sqlite3_uint_init(p->db, 0, 0);
22209 sqlite3_decimal_init(p->db, 0, 0);
22210 sqlite3_base64_init(p->db, 0, 0);
22211 sqlite3_base85_init(p->db, 0, 0);
22212 sqlite3_regexp_init(p->db, 0, 0);
22213 sqlite3_ieee_init(p->db, 0, 0);
22214 sqlite3_series_init(p->db, 0, 0);
22215 #ifndef SQLITE_SHELL_FIDDLE
22216 sqlite3_fileio_init(p->db, 0, 0);
22217 sqlite3_completion_init(p->db, 0, 0);
22219 #ifdef SQLITE_HAVE_ZLIB
22220 if( !p->bSafeModePersist ){
22221 sqlite3_zipfile_init(p->db, 0, 0);
22222 sqlite3_sqlar_init(p->db, 0, 0);
22225 #ifdef SQLITE_SHELL_EXTFUNCS
22226 /* Create a preprocessing mechanism for extensions to make
22227 * their own provisions for being built into the shell.
22228 * This is a short-span macro. See further below for usage.
22230 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
22231 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
22232 /* Let custom-included extensions get their ..._init() called.
22233 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
22234 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
22235 * initialization routine to be called.
22238 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
22239 /* Let custom-included extensions expose their functionality.
22240 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
22241 * the SQL functions, virtual tables, collating sequences or
22242 * VFS's implemented by the extension to be registered.
22245 || irc==SQLITE_OK_LOAD_PERMANENTLY ){
22246 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
22248 #undef SHELL_SUB_MACRO
22249 #undef SHELL_SUBMACRO
22253 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
22254 shellStrtod, 0, 0);
22255 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
22256 shellDtostr, 0, 0);
22257 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
22258 shellDtostr, 0, 0);
22259 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
22260 shellAddSchemaName, 0, 0);
22261 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
22262 shellModuleSchema, 0, 0);
22263 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
22264 shellPutsFunc, 0, 0);
22265 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
22266 shellUSleepFunc, 0, 0);
22267 #ifndef SQLITE_NOHAVE_SYSTEM
22268 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
22270 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
22274 if( p->openMode==SHELL_OPEN_ZIPFILE ){
22275 char *zSql = sqlite3_mprintf(
22276 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
22277 shell_check_oom(zSql);
22278 sqlite3_exec(p->db, zSql, 0, 0, 0);
22279 sqlite3_free(zSql);
22281 #ifndef SQLITE_OMIT_DESERIALIZE
22283 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
22286 unsigned char *aData;
22287 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
22288 aData = (unsigned char*)readFile(zDbFilename, &nData);
22290 aData = readHexDb(p, &nData);
22295 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
22296 SQLITE_DESERIALIZE_RESIZEABLE |
22297 SQLITE_DESERIALIZE_FREEONCLOSE);
22299 eputf("Error: sqlite3_deserialize() returns %d\n", rc);
22302 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
22308 if( p->bSafeModePersist ){
22309 sqlite3_set_authorizer(p->db, safeModeAuth, p);
22312 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
22318 ** Attempt to close the database connection. Report errors.
22320 void close_db(sqlite3 *db){
22321 int rc = sqlite3_close(db);
22323 eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
22327 #if HAVE_READLINE || HAVE_EDITLINE
22329 ** Readline completion callbacks
22331 static char *readline_completion_generator(const char *text, int state){
22332 static sqlite3_stmt *pStmt = 0;
22336 sqlite3_finalize(pStmt);
22337 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
22338 " FROM completion(%Q) ORDER BY 1", text);
22339 shell_check_oom(zSql);
22340 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
22341 sqlite3_free(zSql);
22343 if( sqlite3_step(pStmt)==SQLITE_ROW ){
22344 const char *z = (const char*)sqlite3_column_text(pStmt,0);
22345 zRet = z ? strdup(z) : 0;
22347 sqlite3_finalize(pStmt);
22353 static char **readline_completion(const char *zText, int iStart, int iEnd){
22356 rl_attempted_completion_over = 1;
22357 return rl_completion_matches(zText, readline_completion_generator);
22360 #elif HAVE_LINENOISE
22362 ** Linenoise completion callback
22364 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
22365 i64 nLine = strlen(zLine);
22367 sqlite3_stmt *pStmt = 0;
22371 if( nLine>(i64)sizeof(zBuf)-30 ) return;
22372 if( zLine[0]=='.' || zLine[0]=='#') return;
22373 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
22374 if( i==nLine-1 ) return;
22376 memcpy(zBuf, zLine, iStart);
22377 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
22378 " FROM completion(%Q,%Q) ORDER BY 1",
22379 &zLine[iStart], zLine);
22380 shell_check_oom(zSql);
22381 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
22382 sqlite3_free(zSql);
22383 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
22384 while( sqlite3_step(pStmt)==SQLITE_ROW ){
22385 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
22386 int nCompletion = sqlite3_column_bytes(pStmt, 0);
22387 if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
22388 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
22389 linenoiseAddCompletion(lc, zBuf);
22392 sqlite3_finalize(pStmt);
22397 ** Do C-language style dequoting.
22403 ** \v -> vertical tab
22405 ** \r -> carriage return
22410 ** \NNN -> ascii character NNN in octal
22411 ** \xHH -> ascii character HH in hexadecimal
22413 static void resolve_backslashes(char *z){
22416 while( *z && *z!='\\' ) z++;
22417 for(i=j=0; (c = z[i])!=0; i++, j++){
22418 if( c=='\\' && z[i+1]!=0 ){
22422 }else if( c=='b' ){
22424 }else if( c=='t' ){
22426 }else if( c=='n' ){
22428 }else if( c=='v' ){
22430 }else if( c=='f' ){
22432 }else if( c=='r' ){
22434 }else if( c=='"' ){
22436 }else if( c=='\'' ){
22438 }else if( c=='\\' ){
22440 }else if( c=='x' ){
22443 while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
22444 hv = (u8)((hv<<4)|hdv);
22449 }else if( c>='0' && c<='7' ){
22451 if( z[i+1]>='0' && z[i+1]<='7' ){
22453 c = (c<<3) + z[i] - '0';
22454 if( z[i+1]>='0' && z[i+1]<='7' ){
22456 c = (c<<3) + z[i] - '0';
22463 if( j<i ) z[j] = 0;
22467 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
22468 ** for TRUE and FALSE. Return the integer value if appropriate.
22470 static int booleanValue(const char *zArg){
22472 if( zArg[0]=='0' && zArg[1]=='x' ){
22473 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
22475 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
22477 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
22478 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
22481 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
22484 eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
22489 ** Set or clear a shell flag according to a boolean value.
22491 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
22492 if( booleanValue(zArg) ){
22493 ShellSetFlag(p, mFlag);
22495 ShellClearFlag(p, mFlag);
22500 ** Close an output file, assuming it is not stderr or stdout
22502 static void output_file_close(FILE *f){
22503 if( f && f!=stdout && f!=stderr ) fclose(f);
22507 ** Try to open an output file. The names "stdout" and "stderr" are
22508 ** recognized and do the right thing. NULL is returned if the output
22509 ** filename is "off".
22511 static FILE *output_file_open(const char *zFile, int bTextMode){
22513 if( cli_strcmp(zFile,"stdout")==0 ){
22515 }else if( cli_strcmp(zFile, "stderr")==0 ){
22517 }else if( cli_strcmp(zFile, "off")==0 ){
22520 f = fopen(zFile, bTextMode ? "w" : "wb");
22522 eputf("Error: cannot open \"%s\"\n", zFile);
22528 #ifndef SQLITE_OMIT_TRACE
22530 ** A routine for handling output from sqlite3_trace().
22532 static int sql_trace_callback(
22533 unsigned mType, /* The trace type */
22534 void *pArg, /* The ShellState pointer */
22535 void *pP, /* Usually a pointer to sqlite_stmt */
22536 void *pX /* Auxiliary output */
22538 ShellState *p = (ShellState*)pArg;
22539 sqlite3_stmt *pStmt;
22542 if( p->traceOut==0 ) return 0;
22543 if( mType==SQLITE_TRACE_CLOSE ){
22544 sputz(p->traceOut, "-- closing database connection\n");
22547 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
22548 zSql = (const char*)pX;
22550 pStmt = (sqlite3_stmt*)pP;
22551 switch( p->eTraceType ){
22552 case SHELL_TRACE_EXPANDED: {
22553 zSql = sqlite3_expanded_sql(pStmt);
22556 #ifdef SQLITE_ENABLE_NORMALIZE
22557 case SHELL_TRACE_NORMALIZED: {
22558 zSql = sqlite3_normalized_sql(pStmt);
22563 zSql = sqlite3_sql(pStmt);
22568 if( zSql==0 ) return 0;
22569 nSql = strlen(zSql);
22570 if( nSql>1000000000 ) nSql = 1000000000;
22571 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
22573 case SQLITE_TRACE_ROW:
22574 case SQLITE_TRACE_STMT: {
22575 sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
22578 case SQLITE_TRACE_PROFILE: {
22579 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
22580 sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
22589 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
22590 ** a useful spot to set a debugger breakpoint.
22592 ** This routine does not do anything practical. The code are there simply
22593 ** to prevent the compiler from optimizing this routine out.
22595 static void test_breakpoint(void){
22596 static unsigned int nCall = 0;
22597 if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
22601 ** An object used to read a CSV and other files for import.
22603 typedef struct ImportCtx ImportCtx;
22605 const char *zFile; /* Name of the input file */
22606 FILE *in; /* Read the CSV text from this input stream */
22607 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
22608 char *z; /* Accumulated text for a field */
22609 int n; /* Number of bytes in z */
22610 int nAlloc; /* Space allocated for z[] */
22611 int nLine; /* Current line number */
22612 int nRow; /* Number of rows imported */
22613 int nErr; /* Number of errors encountered */
22614 int bNotFirst; /* True if one or more bytes already read */
22615 int cTerm; /* Character that terminated the most recent field */
22616 int cColSep; /* The column separator character. (Usually ",") */
22617 int cRowSep; /* The row separator character. (Usually "\n") */
22620 /* Clean up resourced used by an ImportCtx */
22621 static void import_cleanup(ImportCtx *p){
22622 if( p->in!=0 && p->xCloser!=0 ){
22626 sqlite3_free(p->z);
22630 /* Append a single byte to z[] */
22631 static void import_append_char(ImportCtx *p, int c){
22632 if( p->n+1>=p->nAlloc ){
22633 p->nAlloc += p->nAlloc + 100;
22634 p->z = sqlite3_realloc64(p->z, p->nAlloc);
22635 shell_check_oom(p->z);
22637 p->z[p->n++] = (char)c;
22640 /* Read a single field of CSV text. Compatible with rfc4180 and extended
22641 ** with the option of having a separator other than ",".
22643 ** + Input comes from p->in.
22644 ** + Store results in p->z of length p->n. Space to hold p->z comes
22645 ** from sqlite3_malloc64().
22646 ** + Use p->cSep as the column separator. The default is ",".
22647 ** + Use p->rSep as the row separator. The default is "\n".
22648 ** + Keep track of the line number in p->nLine.
22649 ** + Store the character that terminates the field in p->cTerm. Store
22650 ** EOF on end-of-file.
22651 ** + Report syntax errors on stderr
22653 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
22655 int cSep = (u8)p->cColSep;
22656 int rSep = (u8)p->cRowSep;
22659 if( c==EOF || seenInterrupt ){
22665 int startLine = p->nLine;
22670 if( c==rSep ) p->nLine++;
22677 if( (c==cSep && pc==cQuote)
22678 || (c==rSep && pc==cQuote)
22679 || (c==rSep && pc=='\r' && ppc==cQuote)
22680 || (c==EOF && pc==cQuote)
22682 do{ p->n--; }while( p->z[p->n]!=cQuote );
22686 if( pc==cQuote && c!='\r' ){
22687 eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
22690 eputf("%s:%d: unterminated %c-quoted field\n",
22691 p->zFile, startLine, cQuote);
22695 import_append_char(p, c);
22700 /* If this is the first field being parsed and it begins with the
22701 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
22702 if( (c&0xff)==0xef && p->bNotFirst==0 ){
22703 import_append_char(p, c);
22705 if( (c&0xff)==0xbb ){
22706 import_append_char(p, c);
22708 if( (c&0xff)==0xbf ){
22711 return csv_read_one_field(p);
22715 while( c!=EOF && c!=cSep && c!=rSep ){
22716 import_append_char(p, c);
22721 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
22725 if( p->z ) p->z[p->n] = 0;
22730 /* Read a single field of ASCII delimited text.
22732 ** + Input comes from p->in.
22733 ** + Store results in p->z of length p->n. Space to hold p->z comes
22734 ** from sqlite3_malloc64().
22735 ** + Use p->cSep as the column separator. The default is "\x1F".
22736 ** + Use p->rSep as the row separator. The default is "\x1E".
22737 ** + Keep track of the row number in p->nLine.
22738 ** + Store the character that terminates the field in p->cTerm. Store
22739 ** EOF on end-of-file.
22740 ** + Report syntax errors on stderr
22742 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
22744 int cSep = (u8)p->cColSep;
22745 int rSep = (u8)p->cRowSep;
22748 if( c==EOF || seenInterrupt ){
22752 while( c!=EOF && c!=cSep && c!=rSep ){
22753 import_append_char(p, c);
22760 if( p->z ) p->z[p->n] = 0;
22765 ** Try to transfer data for table zTable. If an error is seen while
22766 ** moving forward, try to go backwards. The backwards movement won't
22767 ** work for WITHOUT ROWID tables.
22769 static void tryToCloneData(
22774 sqlite3_stmt *pQuery = 0;
22775 sqlite3_stmt *pInsert = 0;
22780 int nTable = strlen30(zTable);
22783 const int spinRate = 10000;
22785 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
22786 shell_check_oom(zQuery);
22787 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22789 eputf("Error %d: %s on [%s]\n",
22790 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
22791 goto end_data_xfer;
22793 n = sqlite3_column_count(pQuery);
22794 zInsert = sqlite3_malloc64(200 + nTable + n*3);
22795 shell_check_oom(zInsert);
22796 sqlite3_snprintf(200+nTable,zInsert,
22797 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
22798 i = strlen30(zInsert);
22799 for(j=1; j<n; j++){
22800 memcpy(zInsert+i, ",?", 2);
22803 memcpy(zInsert+i, ");", 3);
22804 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
22806 eputf("Error %d: %s on [%s]\n",
22807 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
22808 goto end_data_xfer;
22810 for(k=0; k<2; k++){
22811 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22812 for(i=0; i<n; i++){
22813 switch( sqlite3_column_type(pQuery, i) ){
22814 case SQLITE_NULL: {
22815 sqlite3_bind_null(pInsert, i+1);
22818 case SQLITE_INTEGER: {
22819 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
22822 case SQLITE_FLOAT: {
22823 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
22826 case SQLITE_TEXT: {
22827 sqlite3_bind_text(pInsert, i+1,
22828 (const char*)sqlite3_column_text(pQuery,i),
22829 -1, SQLITE_STATIC);
22832 case SQLITE_BLOB: {
22833 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
22834 sqlite3_column_bytes(pQuery,i),
22840 rc = sqlite3_step(pInsert);
22841 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
22842 eputf("Error %d: %s\n",
22843 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
22845 sqlite3_reset(pInsert);
22847 if( (cnt%spinRate)==0 ){
22848 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
22852 if( rc==SQLITE_DONE ) break;
22853 sqlite3_finalize(pQuery);
22854 sqlite3_free(zQuery);
22855 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
22857 shell_check_oom(zQuery);
22858 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22860 eputf("Warning: cannot step \"%s\" backwards", zTable);
22863 } /* End for(k=0...) */
22866 sqlite3_finalize(pQuery);
22867 sqlite3_finalize(pInsert);
22868 sqlite3_free(zQuery);
22869 sqlite3_free(zInsert);
22874 ** Try to transfer all rows of the schema that match zWhere. For
22875 ** each row, invoke xForEach() on the object defined by that row.
22876 ** If an error is encountered while moving forward through the
22877 ** sqlite_schema table, try again moving backwards.
22879 static void tryToCloneSchema(
22882 const char *zWhere,
22883 void (*xForEach)(ShellState*,sqlite3*,const char*)
22885 sqlite3_stmt *pQuery = 0;
22888 const unsigned char *zName;
22889 const unsigned char *zSql;
22892 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22893 " WHERE %s ORDER BY rowid ASC", zWhere);
22894 shell_check_oom(zQuery);
22895 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22897 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
22898 sqlite3_errmsg(p->db), zQuery);
22899 goto end_schema_xfer;
22901 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22902 zName = sqlite3_column_text(pQuery, 0);
22903 zSql = sqlite3_column_text(pQuery, 1);
22904 if( zName==0 || zSql==0 ) continue;
22905 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
22906 sputf(stdout, "%s... ", zName); fflush(stdout);
22907 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22909 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22910 sqlite3_free(zErrMsg);
22915 xForEach(p, newDb, (const char*)zName);
22917 sputz(stdout, "done\n");
22919 if( rc!=SQLITE_DONE ){
22920 sqlite3_finalize(pQuery);
22921 sqlite3_free(zQuery);
22922 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22923 " WHERE %s ORDER BY rowid DESC", zWhere);
22924 shell_check_oom(zQuery);
22925 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22927 eputf("Error: (%d) %s on [%s]\n",
22928 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
22929 goto end_schema_xfer;
22931 while( sqlite3_step(pQuery)==SQLITE_ROW ){
22932 zName = sqlite3_column_text(pQuery, 0);
22933 zSql = sqlite3_column_text(pQuery, 1);
22934 if( zName==0 || zSql==0 ) continue;
22935 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
22936 sputf(stdout, "%s... ", zName); fflush(stdout);
22937 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22939 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22940 sqlite3_free(zErrMsg);
22944 xForEach(p, newDb, (const char*)zName);
22946 sputz(stdout, "done\n");
22950 sqlite3_finalize(pQuery);
22951 sqlite3_free(zQuery);
22955 ** Open a new database file named "zNewDb". Try to recover as much information
22956 ** as possible out of the main database (which might be corrupt) and write it
22959 static void tryToClone(ShellState *p, const char *zNewDb){
22961 sqlite3 *newDb = 0;
22962 if( access(zNewDb,0)==0 ){
22963 eputf("File \"%s\" already exists.\n", zNewDb);
22966 rc = sqlite3_open(zNewDb, &newDb);
22968 eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
22970 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
22971 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
22972 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
22973 tryToCloneSchema(p, newDb, "type!='table'", 0);
22974 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
22975 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
22980 #ifndef SQLITE_SHELL_FIDDLE
22982 ** Change the output stream (file or pipe or console) to something else.
22984 static void output_redir(ShellState *p, FILE *pfNew){
22985 if( p->out != stdout ) eputz("Output already redirected.\n");
22988 setOutputStream(pfNew);
22993 ** Change the output file back to stdout.
22995 ** If the p->doXdgOpen flag is set, that means the output was being
22996 ** redirected to a temporary file named by p->zTempFile. In that case,
22997 ** launch start/open/xdg-open on that temporary file.
22999 static void output_reset(ShellState *p){
23000 if( p->outfile[0]=='|' ){
23001 #ifndef SQLITE_OMIT_POPEN
23005 output_file_close(p->out);
23006 #ifndef SQLITE_NOHAVE_SYSTEM
23007 if( p->doXdgOpen ){
23008 const char *zXdgOpenCmd =
23009 #if defined(_WIN32)
23011 #elif defined(__APPLE__)
23017 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
23018 if( system(zCmd) ){
23019 eputf("Failed: [%s]\n", zCmd);
23021 /* Give the start/open/xdg-open command some time to get
23022 ** going before we continue, and potential delete the
23023 ** p->zTempFile data file out from under it */
23024 sqlite3_sleep(2000);
23026 sqlite3_free(zCmd);
23030 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
23034 setOutputStream(stdout);
23037 # define output_redir(SS,pfO)
23038 # define output_reset(SS)
23042 ** Run an SQL command and return the single integer result.
23044 static int db_int(sqlite3 *db, const char *zSql){
23045 sqlite3_stmt *pStmt;
23047 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
23048 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
23049 res = sqlite3_column_int(pStmt,0);
23051 sqlite3_finalize(pStmt);
23055 #if SQLITE_SHELL_HAVE_RECOVER
23057 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
23059 static unsigned int get2byteInt(unsigned char *a){
23060 return (a[0]<<8) + a[1];
23062 static unsigned int get4byteInt(unsigned char *a){
23063 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
23067 ** Implementation of the ".dbinfo" command.
23069 ** Return 1 on error, 2 to exit, and 0 otherwise.
23071 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
23072 static const struct { const char *zName; int ofst; } aField[] = {
23073 { "file change counter:", 24 },
23074 { "database page count:", 28 },
23075 { "freelist page count:", 36 },
23076 { "schema cookie:", 40 },
23077 { "schema format:", 44 },
23078 { "default cache size:", 48 },
23079 { "autovacuum top root:", 52 },
23080 { "incremental vacuum:", 64 },
23081 { "text encoding:", 56 },
23082 { "user version:", 60 },
23083 { "application id:", 68 },
23084 { "software version:", 96 },
23086 static const struct { const char *zName; const char *zSql; } aQuery[] = {
23087 { "number of tables:",
23088 "SELECT count(*) FROM %s WHERE type='table'" },
23089 { "number of indexes:",
23090 "SELECT count(*) FROM %s WHERE type='index'" },
23091 { "number of triggers:",
23092 "SELECT count(*) FROM %s WHERE type='trigger'" },
23093 { "number of views:",
23094 "SELECT count(*) FROM %s WHERE type='view'" },
23096 "SELECT total(length(sql)) FROM %s" },
23099 unsigned iDataVersion;
23101 char *zDb = nArg>=2 ? azArg[1] : "main";
23102 sqlite3_stmt *pStmt = 0;
23103 unsigned char aHdr[100];
23105 if( p->db==0 ) return 1;
23106 rc = sqlite3_prepare_v2(p->db,
23107 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
23110 eputf("error: %s\n", sqlite3_errmsg(p->db));
23111 sqlite3_finalize(pStmt);
23114 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
23115 if( sqlite3_step(pStmt)==SQLITE_ROW
23116 && sqlite3_column_bytes(pStmt,0)>100
23118 const u8 *pb = sqlite3_column_blob(pStmt,0);
23119 shell_check_oom(pb);
23120 memcpy(aHdr, pb, 100);
23121 sqlite3_finalize(pStmt);
23123 eputz("unable to read database header\n");
23124 sqlite3_finalize(pStmt);
23127 i = get2byteInt(aHdr+16);
23128 if( i==1 ) i = 65536;
23129 oputf("%-20s %d\n", "database page size:", i);
23130 oputf("%-20s %d\n", "write format:", aHdr[18]);
23131 oputf("%-20s %d\n", "read format:", aHdr[19]);
23132 oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
23133 for(i=0; i<ArraySize(aField); i++){
23134 int ofst = aField[i].ofst;
23135 unsigned int val = get4byteInt(aHdr + ofst);
23136 oputf("%-20s %u", aField[i].zName, val);
23139 if( val==1 ) oputz(" (utf8)");
23140 if( val==2 ) oputz(" (utf16le)");
23141 if( val==3 ) oputz(" (utf16be)");
23147 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
23148 }else if( cli_strcmp(zDb,"temp")==0 ){
23149 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
23151 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
23153 for(i=0; i<ArraySize(aQuery); i++){
23154 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
23155 int val = db_int(p->db, zSql);
23156 sqlite3_free(zSql);
23157 oputf("%-20s %d\n", aQuery[i].zName, val);
23159 sqlite3_free(zSchemaTab);
23160 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
23161 oputf("%-20s %u\n", "data version", iDataVersion);
23164 #endif /* SQLITE_SHELL_HAVE_RECOVER */
23167 ** Print the current sqlite3_errmsg() value to stderr and return 1.
23169 static int shellDatabaseError(sqlite3 *db){
23170 const char *zErr = sqlite3_errmsg(db);
23171 eputf("Error: %s\n", zErr);
23176 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
23177 ** if they match and FALSE (0) if they do not match.
23181 ** '*' Matches any sequence of zero or more characters.
23183 ** '?' Matches exactly one character.
23185 ** [...] Matches one character from the enclosed list of
23188 ** [^...] Matches one character not in the enclosed list.
23190 ** '#' Matches any sequence of one or more digits with an
23191 ** optional + or - sign in front
23193 ** ' ' Any span of whitespace matches any other span of
23196 ** Extra whitespace at the end of z[] is ignored.
23198 static int testcase_glob(const char *zGlob, const char *z){
23203 while( (c = (*(zGlob++)))!=0 ){
23205 if( !IsSpace(*z) ) return 0;
23206 while( IsSpace(*zGlob) ) zGlob++;
23207 while( IsSpace(*z) ) z++;
23208 }else if( c=='*' ){
23209 while( (c=(*(zGlob++))) == '*' || c=='?' ){
23210 if( c=='?' && (*(z++))==0 ) return 0;
23214 }else if( c=='[' ){
23215 while( *z && testcase_glob(zGlob-1,z)==0 ){
23220 while( (c2 = (*(z++)))!=0 ){
23223 if( c2==0 ) return 0;
23225 if( testcase_glob(zGlob,z) ) return 1;
23228 }else if( c=='?' ){
23229 if( (*(z++))==0 ) return 0;
23230 }else if( c=='[' ){
23235 if( c==0 ) return 0;
23242 if( c==']' ) seen = 1;
23245 while( c2 && c2!=']' ){
23246 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
23248 if( c>=prior_c && c<=c2 ) seen = 1;
23258 if( c2==0 || (seen ^ invert)==0 ) return 0;
23259 }else if( c=='#' ){
23260 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
23261 if( !IsDigit(z[0]) ) return 0;
23263 while( IsDigit(z[0]) ){ z++; }
23265 if( c!=(*(z++)) ) return 0;
23268 while( IsSpace(*z) ){ z++; }
23274 ** Compare the string as a command-line option with either one or two
23275 ** initial "-" characters.
23277 static int optionMatch(const char *zStr, const char *zOpt){
23278 if( zStr[0]!='-' ) return 0;
23280 if( zStr[0]=='-' ) zStr++;
23281 return cli_strcmp(zStr, zOpt)==0;
23287 int shellDeleteFile(const char *zFilename){
23290 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
23294 rc = unlink(zFilename);
23300 ** Try to delete the temporary file (if there is one) and free the
23301 ** memory used to hold the name of the temp file.
23303 static void clearTempFile(ShellState *p){
23304 if( p->zTempFile==0 ) return;
23305 if( p->doXdgOpen ) return;
23306 if( shellDeleteFile(p->zTempFile) ) return;
23307 sqlite3_free(p->zTempFile);
23312 ** Create a new temp file name with the given suffix.
23314 static void newTempFile(ShellState *p, const char *zSuffix){
23316 sqlite3_free(p->zTempFile);
23319 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
23321 if( p->zTempFile==0 ){
23322 /* If p->db is an in-memory database then the TEMPFILENAME file-control
23323 ** will not work and we will need to fallback to guessing */
23326 sqlite3_randomness(sizeof(r), &r);
23327 zTemp = getenv("TEMP");
23328 if( zTemp==0 ) zTemp = getenv("TMP");
23336 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
23338 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
23340 shell_check_oom(p->zTempFile);
23345 ** The implementation of SQL scalar function fkey_collate_clause(), used
23346 ** by the ".lint fkey-indexes" command. This scalar function is always
23347 ** called with four arguments - the parent table name, the parent column name,
23348 ** the child table name and the child column name.
23350 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
23352 ** If either of the named tables or columns do not exist, this function
23353 ** returns an empty string. An empty string is also returned if both tables
23354 ** and columns exist but have the same default collation sequence. Or,
23355 ** if both exist but the default collation sequences are different, this
23356 ** function returns the string " COLLATE <parent-collation>", where
23357 ** <parent-collation> is the default collation sequence of the parent column.
23359 static void shellFkeyCollateClause(
23360 sqlite3_context *pCtx,
23362 sqlite3_value **apVal
23364 sqlite3 *db = sqlite3_context_db_handle(pCtx);
23365 const char *zParent;
23366 const char *zParentCol;
23367 const char *zParentSeq;
23368 const char *zChild;
23369 const char *zChildCol;
23370 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
23374 zParent = (const char*)sqlite3_value_text(apVal[0]);
23375 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
23376 zChild = (const char*)sqlite3_value_text(apVal[2]);
23377 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
23379 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
23380 rc = sqlite3_table_column_metadata(
23381 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
23383 if( rc==SQLITE_OK ){
23384 rc = sqlite3_table_column_metadata(
23385 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
23389 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
23390 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
23391 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
23398 ** The implementation of dot-command ".lint fkey-indexes".
23400 static int lintFkeyIndexes(
23401 ShellState *pState, /* Current shell tool state */
23402 char **azArg, /* Array of arguments passed to dot command */
23403 int nArg /* Number of entries in azArg[] */
23405 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
23406 int bVerbose = 0; /* If -verbose is present */
23407 int bGroupByParent = 0; /* If -groupbyparent is present */
23408 int i; /* To iterate through azArg[] */
23409 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
23410 int rc; /* Return code */
23411 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
23414 ** This SELECT statement returns one row for each foreign key constraint
23415 ** in the schema of the main database. The column values are:
23417 ** 0. The text of an SQL statement similar to:
23419 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
23421 ** This SELECT is similar to the one that the foreign keys implementation
23422 ** needs to run internally on child tables. If there is an index that can
23423 ** be used to optimize this query, then it can also be used by the FK
23424 ** implementation to optimize DELETE or UPDATE statements on the parent
23427 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
23428 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
23429 ** contains an index that can be used to optimize the query.
23431 ** 2. Human readable text that describes the child table and columns. e.g.
23433 ** "child_table(child_key1, child_key2)"
23435 ** 3. Human readable text that describes the parent table and columns. e.g.
23437 ** "parent_table(parent_key1, parent_key2)"
23439 ** 4. A full CREATE INDEX statement for an index that could be used to
23440 ** optimize DELETE or UPDATE statements on the parent table. e.g.
23442 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
23444 ** 5. The name of the parent table.
23446 ** These six values are used by the C logic below to generate the report.
23450 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
23451 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
23452 " || fkey_collate_clause("
23453 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
23455 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
23456 " || group_concat('*=?', ' AND ') || ')'"
23458 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
23460 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
23462 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
23463 " || ' ON ' || quote(s.name) || '('"
23464 " || group_concat(quote(f.[from]) ||"
23465 " fkey_collate_clause("
23466 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
23470 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
23471 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
23472 "GROUP BY s.name, f.id "
23473 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
23475 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
23477 for(i=2; i<nArg; i++){
23478 int n = strlen30(azArg[i]);
23479 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
23482 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
23483 bGroupByParent = 1;
23487 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
23488 return SQLITE_ERROR;
23492 /* Register the fkey_collate_clause() SQL function */
23493 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
23494 0, shellFkeyCollateClause, 0, 0
23498 if( rc==SQLITE_OK ){
23499 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
23501 if( rc==SQLITE_OK ){
23502 sqlite3_bind_int(pSql, 1, bGroupByParent);
23505 if( rc==SQLITE_OK ){
23508 while( SQLITE_ROW==sqlite3_step(pSql) ){
23510 sqlite3_stmt *pExplain = 0;
23511 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
23512 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
23513 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
23514 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
23515 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
23516 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
23518 if( zEQP==0 ) continue;
23519 if( zGlob==0 ) continue;
23520 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
23521 if( rc!=SQLITE_OK ) break;
23522 if( SQLITE_ROW==sqlite3_step(pExplain) ){
23523 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
23524 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
23525 || 0==sqlite3_strglob(zGlobIPK, zPlan));
23527 rc = sqlite3_finalize(pExplain);
23528 if( rc!=SQLITE_OK ) break;
23531 eputz("Error: internal error");
23535 && (bVerbose || res==0)
23536 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
23538 oputf("-- Parent table %s\n", zParent);
23539 sqlite3_free(zPrev);
23540 zPrev = sqlite3_mprintf("%s", zParent);
23544 oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
23545 }else if( bVerbose ){
23546 oputf("%s/* no extra indexes required for %s -> %s */\n",
23547 zIndent, zFrom, zTarget
23552 sqlite3_free(zPrev);
23554 if( rc!=SQLITE_OK ){
23555 eputf("%s\n", sqlite3_errmsg(db));
23558 rc2 = sqlite3_finalize(pSql);
23559 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
23561 eputf("%s\n", sqlite3_errmsg(db));
23564 eputf("%s\n", sqlite3_errmsg(db));
23571 ** Implementation of ".lint" dot command.
23573 static int lintDotCommand(
23574 ShellState *pState, /* Current shell tool state */
23575 char **azArg, /* Array of arguments passed to dot command */
23576 int nArg /* Number of entries in azArg[] */
23579 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
23580 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
23581 return lintFkeyIndexes(pState, azArg, nArg);
23584 eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
23585 eputz("Where sub-commands are:\n");
23586 eputz(" fkey-indexes\n");
23587 return SQLITE_ERROR;
23590 static void shellPrepare(
23594 sqlite3_stmt **ppStmt
23597 if( *pRc==SQLITE_OK ){
23598 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
23599 if( rc!=SQLITE_OK ){
23600 eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
23607 ** Create a prepared statement using printf-style arguments for the SQL.
23609 static void shellPreparePrintf(
23612 sqlite3_stmt **ppStmt,
23617 if( *pRc==SQLITE_OK ){
23620 va_start(ap, zFmt);
23621 z = sqlite3_vmprintf(zFmt, ap);
23624 *pRc = SQLITE_NOMEM;
23626 shellPrepare(db, pRc, z, ppStmt);
23633 ** Finalize the prepared statement created using shellPreparePrintf().
23635 static void shellFinalize(
23637 sqlite3_stmt *pStmt
23640 sqlite3 *db = sqlite3_db_handle(pStmt);
23641 int rc = sqlite3_finalize(pStmt);
23642 if( *pRc==SQLITE_OK ){
23643 if( rc!=SQLITE_OK ){
23644 eputf("SQL error: %s\n", sqlite3_errmsg(db));
23651 #if !defined SQLITE_OMIT_VIRTUALTABLE
23652 /* Reset the prepared statement created using shellPreparePrintf().
23654 ** This routine is could be marked "static". But it is not always used,
23655 ** depending on compile-time options. By omitting the "static", we avoid
23656 ** nuisance compiler warnings about "defined but not used".
23660 sqlite3_stmt *pStmt
23662 int rc = sqlite3_reset(pStmt);
23663 if( *pRc==SQLITE_OK ){
23664 if( rc!=SQLITE_OK ){
23665 sqlite3 *db = sqlite3_db_handle(pStmt);
23666 eputf("SQL error: %s\n", sqlite3_errmsg(db));
23671 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
23673 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23674 /******************************************************************************
23675 ** The ".archive" or ".ar" command.
23678 ** Structure representing a single ".ar" command.
23680 typedef struct ArCommand ArCommand;
23682 u8 eCmd; /* An AR_CMD_* value */
23683 u8 bVerbose; /* True if --verbose */
23684 u8 bZip; /* True if the archive is a ZIP */
23685 u8 bDryRun; /* True if --dry-run */
23686 u8 bAppend; /* True if --append */
23687 u8 bGlob; /* True if --glob */
23688 u8 fromCmdLine; /* Run from -A instead of .archive */
23689 int nArg; /* Number of command arguments */
23690 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
23691 const char *zFile; /* --file argument, or NULL */
23692 const char *zDir; /* --directory argument, or NULL */
23693 char **azArg; /* Array of command arguments */
23694 ShellState *p; /* Shell state */
23695 sqlite3 *db; /* Database containing the archive */
23699 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
23701 static int arUsage(FILE *f){
23702 showHelp(f,"archive");
23703 return SQLITE_ERROR;
23707 ** Print an error message for the .ar command to stderr and return
23710 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
23713 va_start(ap, zFmt);
23714 z = sqlite3_vmprintf(zFmt, ap);
23716 eputf("Error: %s\n", z);
23717 if( pAr->fromCmdLine ){
23718 eputz("Use \"-A\" for more help\n");
23720 eputz("Use \".archive --help\" for more help\n");
23723 return SQLITE_ERROR;
23727 ** Values for ArCommand.eCmd.
23729 #define AR_CMD_CREATE 1
23730 #define AR_CMD_UPDATE 2
23731 #define AR_CMD_INSERT 3
23732 #define AR_CMD_EXTRACT 4
23733 #define AR_CMD_LIST 5
23734 #define AR_CMD_HELP 6
23735 #define AR_CMD_REMOVE 7
23738 ** Other (non-command) switches.
23740 #define AR_SWITCH_VERBOSE 8
23741 #define AR_SWITCH_FILE 9
23742 #define AR_SWITCH_DIRECTORY 10
23743 #define AR_SWITCH_APPEND 11
23744 #define AR_SWITCH_DRYRUN 12
23745 #define AR_SWITCH_GLOB 13
23747 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
23749 case AR_CMD_CREATE:
23750 case AR_CMD_EXTRACT:
23752 case AR_CMD_REMOVE:
23753 case AR_CMD_UPDATE:
23754 case AR_CMD_INSERT:
23757 return arErrorMsg(pAr, "multiple command options");
23759 pAr->eCmd = eSwitch;
23762 case AR_SWITCH_DRYRUN:
23765 case AR_SWITCH_GLOB:
23768 case AR_SWITCH_VERBOSE:
23771 case AR_SWITCH_APPEND:
23773 deliberate_fall_through;
23774 case AR_SWITCH_FILE:
23777 case AR_SWITCH_DIRECTORY:
23786 ** Parse the command line for an ".ar" command. The results are written into
23787 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
23788 ** successfully, otherwise an error message is written to stderr and
23789 ** SQLITE_ERROR returned.
23791 static int arParseCommand(
23792 char **azArg, /* Array of arguments passed to dot command */
23793 int nArg, /* Number of entries in azArg[] */
23794 ArCommand *pAr /* Populate this object */
23802 { "create", 'c', AR_CMD_CREATE, 0 },
23803 { "extract", 'x', AR_CMD_EXTRACT, 0 },
23804 { "insert", 'i', AR_CMD_INSERT, 0 },
23805 { "list", 't', AR_CMD_LIST, 0 },
23806 { "remove", 'r', AR_CMD_REMOVE, 0 },
23807 { "update", 'u', AR_CMD_UPDATE, 0 },
23808 { "help", 'h', AR_CMD_HELP, 0 },
23809 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
23810 { "file", 'f', AR_SWITCH_FILE, 1 },
23811 { "append", 'a', AR_SWITCH_APPEND, 1 },
23812 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
23813 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
23814 { "glob", 'g', AR_SWITCH_GLOB, 0 },
23816 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
23817 struct ArSwitch *pEnd = &aSwitch[nSwitch];
23820 eputz("Wrong number of arguments. Usage:\n");
23821 return arUsage(stderr);
23823 char *z = azArg[1];
23825 /* Traditional style [tar] invocation */
23828 for(i=0; z[i]; i++){
23829 const char *zArg = 0;
23830 struct ArSwitch *pOpt;
23831 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23832 if( z[i]==pOpt->cShort ) break;
23835 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
23839 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
23841 zArg = azArg[iArg++];
23843 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
23845 pAr->nArg = nArg-iArg;
23847 pAr->azArg = &azArg[iArg];
23850 /* Non-traditional invocation */
23852 for(iArg=1; iArg<nArg; iArg++){
23856 /* All remaining command line words are command arguments. */
23857 pAr->azArg = &azArg[iArg];
23858 pAr->nArg = nArg-iArg;
23865 /* One or more short options */
23866 for(i=1; i<n; i++){
23867 const char *zArg = 0;
23868 struct ArSwitch *pOpt;
23869 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23870 if( z[i]==pOpt->cShort ) break;
23873 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
23880 if( iArg>=(nArg-1) ){
23881 return arErrorMsg(pAr, "option requires an argument: %c",
23884 zArg = azArg[++iArg];
23887 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
23889 }else if( z[2]=='\0' ){
23890 /* A -- option, indicating that all remaining command line words
23891 ** are command arguments. */
23892 pAr->azArg = &azArg[iArg+1];
23893 pAr->nArg = nArg-iArg-1;
23896 /* A long option */
23897 const char *zArg = 0; /* Argument for option, if any */
23898 struct ArSwitch *pMatch = 0; /* Matching option */
23899 struct ArSwitch *pOpt; /* Iterator */
23900 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23901 const char *zLong = pOpt->zLong;
23902 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
23904 return arErrorMsg(pAr, "ambiguous option: %s",z);
23912 return arErrorMsg(pAr, "unrecognized option: %s", z);
23914 if( pMatch->bArg ){
23915 if( iArg>=(nArg-1) ){
23916 return arErrorMsg(pAr, "option requires an argument: %s", z);
23918 zArg = azArg[++iArg];
23920 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
23925 if( pAr->eCmd==0 ){
23926 eputz("Required argument missing. Usage:\n");
23927 return arUsage(stderr);
23933 ** This function assumes that all arguments within the ArCommand.azArg[]
23934 ** array refer to archive members, as for the --extract, --list or --remove
23935 ** commands. It checks that each of them are "present". If any specified
23936 ** file is not present in the archive, an error is printed to stderr and an
23937 ** error code returned. Otherwise, if all specified arguments are present
23938 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
23939 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
23940 ** when pAr->bGlob is true.
23942 ** This function strips any trailing '/' characters from each argument.
23943 ** This is consistent with the way the [tar] command seems to work on
23946 static int arCheckEntries(ArCommand *pAr){
23947 int rc = SQLITE_OK;
23950 sqlite3_stmt *pTest = 0;
23951 const char *zSel = (pAr->bGlob)
23952 ? "SELECT name FROM %s WHERE glob($name,name)"
23953 : "SELECT name FROM %s WHERE name=$name";
23955 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
23956 j = sqlite3_bind_parameter_index(pTest, "$name");
23957 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
23958 char *z = pAr->azArg[i];
23959 int n = strlen30(z);
23961 while( n>0 && z[n-1]=='/' ) n--;
23963 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
23964 if( SQLITE_ROW==sqlite3_step(pTest) ){
23967 shellReset(&rc, pTest);
23968 if( rc==SQLITE_OK && bOk==0 ){
23969 eputf("not found in archive: %s\n", z);
23973 shellFinalize(&rc, pTest);
23979 ** Format a WHERE clause that can be used against the "sqlar" table to
23980 ** identify all archive members that match the command arguments held
23981 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
23982 ** The caller is responsible for eventually calling sqlite3_free() on
23983 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
23984 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
23986 static void arWhereClause(
23989 char **pzWhere /* OUT: New WHERE clause */
23992 const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
23993 if( *pRc==SQLITE_OK ){
23994 if( pAr->nArg==0 ){
23995 zWhere = sqlite3_mprintf("1");
23998 const char *zSep = "";
23999 for(i=0; i<pAr->nArg; i++){
24000 const char *z = pAr->azArg[i];
24001 zWhere = sqlite3_mprintf(
24002 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
24003 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
24006 *pRc = SQLITE_NOMEM;
24017 ** Implementation of .ar "lisT" command.
24019 static int arListCommand(ArCommand *pAr){
24020 const char *zSql = "SELECT %s FROM %s WHERE %s";
24021 const char *azCols[] = {
24023 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
24027 sqlite3_stmt *pSql = 0;
24030 rc = arCheckEntries(pAr);
24031 arWhereClause(&rc, pAr, &zWhere);
24033 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
24034 pAr->zSrcTable, zWhere);
24035 if( pAr->bDryRun ){
24036 oputf("%s\n", sqlite3_sql(pSql));
24038 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24039 if( pAr->bVerbose ){
24040 oputf("%s % 10d %s %s\n",
24041 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
24042 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
24044 oputf("%s\n", sqlite3_column_text(pSql, 0));
24048 shellFinalize(&rc, pSql);
24049 sqlite3_free(zWhere);
24054 ** Implementation of .ar "Remove" command.
24056 static int arRemoveCommand(ArCommand *pAr){
24062 /* Verify that args actually exist within the archive before proceeding.
24063 ** And formulate a WHERE clause to match them. */
24064 rc = arCheckEntries(pAr);
24065 arWhereClause(&rc, pAr, &zWhere);
24067 if( rc==SQLITE_OK ){
24068 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
24069 pAr->zSrcTable, zWhere);
24070 if( pAr->bDryRun ){
24071 oputf("%s\n", zSql);
24074 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
24075 if( rc==SQLITE_OK ){
24076 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
24077 if( rc!=SQLITE_OK ){
24078 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
24080 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
24084 sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
24085 sqlite3_free(zErr);
24089 sqlite3_free(zWhere);
24090 sqlite3_free(zSql);
24095 ** Implementation of .ar "eXtract" command.
24097 static int arExtractCommand(ArCommand *pAr){
24098 const char *zSql1 =
24101 " writefile(($dir || name), %s, mode, mtime) "
24102 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
24103 " AND name NOT GLOB '*..[/\\]*'";
24105 const char *azExtraArg[] = {
24106 "sqlar_uncompress(data, sz)",
24110 sqlite3_stmt *pSql = 0;
24111 int rc = SQLITE_OK;
24116 /* If arguments are specified, check that they actually exist within
24117 ** the archive before proceeding. And formulate a WHERE clause to
24119 rc = arCheckEntries(pAr);
24120 arWhereClause(&rc, pAr, &zWhere);
24122 if( rc==SQLITE_OK ){
24124 zDir = sqlite3_mprintf("%s/", pAr->zDir);
24126 zDir = sqlite3_mprintf("");
24128 if( zDir==0 ) rc = SQLITE_NOMEM;
24131 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
24132 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
24135 if( rc==SQLITE_OK ){
24136 j = sqlite3_bind_parameter_index(pSql, "$dir");
24137 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
24139 /* Run the SELECT statement twice. The first time, writefile() is called
24140 ** for all archive members that should be extracted. The second time,
24141 ** only for the directories. This is because the timestamps for
24142 ** extracted directories must be reset after they are populated (as
24143 ** populating them changes the timestamp). */
24144 for(i=0; i<2; i++){
24145 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
24146 sqlite3_bind_int(pSql, j, i);
24147 if( pAr->bDryRun ){
24148 oputf("%s\n", sqlite3_sql(pSql));
24150 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24151 if( i==0 && pAr->bVerbose ){
24152 oputf("%s\n", sqlite3_column_text(pSql, 0));
24156 shellReset(&rc, pSql);
24158 shellFinalize(&rc, pSql);
24161 sqlite3_free(zDir);
24162 sqlite3_free(zWhere);
24167 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
24169 static int arExecSql(ArCommand *pAr, const char *zSql){
24171 if( pAr->bDryRun ){
24172 oputf("%s\n", zSql);
24176 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
24178 sputf(stdout, "ERROR: %s\n", zErr);
24179 sqlite3_free(zErr);
24187 ** Implementation of .ar "create", "insert", and "update" commands.
24189 ** create -> Create a new SQL archive
24190 ** insert -> Insert or reinsert all files listed
24191 ** update -> Insert files that have changed or that were not
24192 ** previously in the archive
24194 ** Create the "sqlar" table in the database if it does not already exist.
24195 ** Then add each file in the azFile[] array to the archive. Directories
24196 ** are added recursively. If argument bVerbose is non-zero, a message is
24197 ** printed on stdout for each file archived.
24199 ** The create command is the same as update, except that it drops
24200 ** any existing "sqlar" table before beginning. The "insert" command
24201 ** always overwrites every file named on the command-line, where as
24202 ** "update" only overwrites if the size or mtime or mode has changed.
24204 static int arCreateOrUpdateCommand(
24205 ArCommand *pAr, /* Command arguments and options */
24206 int bUpdate, /* true for a --create. */
24207 int bOnlyIfChanged /* Only update if file has changed */
24209 const char *zCreate =
24210 "CREATE TABLE IF NOT EXISTS sqlar(\n"
24211 " name TEXT PRIMARY KEY, -- name of the file\n"
24212 " mode INT, -- access permissions\n"
24213 " mtime INT, -- last modification time\n"
24214 " sz INT, -- original file size\n"
24215 " data BLOB -- compressed content\n"
24217 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
24218 const char *zInsertFmt[2] = {
24219 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
24224 " CASE substr(lsmode(mode),1,1)\n"
24225 " WHEN '-' THEN length(data)\n"
24226 " WHEN 'd' THEN 0\n"
24228 " sqlar_compress(data)\n"
24229 " FROM fsdir(%Q,%Q) AS disk\n"
24230 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
24232 "REPLACE INTO %s(name,mode,mtime,data)\n"
24238 " FROM fsdir(%Q,%Q) AS disk\n"
24239 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
24241 int i; /* For iterating through azFile[] */
24242 int rc; /* Return code */
24243 const char *zTab = 0; /* SQL table into which to insert */
24248 arExecSql(pAr, "PRAGMA page_size=512");
24249 rc = arExecSql(pAr, "SAVEPOINT ar;");
24250 if( rc!=SQLITE_OK ) return rc;
24253 /* Initialize the zipfile virtual table, if necessary */
24256 sqlite3_randomness(sizeof(r),&r);
24257 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
24259 zSql = sqlite3_mprintf(
24260 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
24263 rc = arExecSql(pAr, zSql);
24264 sqlite3_free(zSql);
24269 /* Initialize the table for an SQLAR */
24272 rc = arExecSql(pAr, zDrop);
24273 if( rc!=SQLITE_OK ) goto end_ar_transaction;
24275 rc = arExecSql(pAr, zCreate);
24277 if( bOnlyIfChanged ){
24278 zExists = sqlite3_mprintf(
24280 "SELECT 1 FROM %s AS mem"
24281 " WHERE mem.name=disk.name"
24282 " AND mem.mtime=disk.mtime"
24283 " AND mem.mode=disk.mode)", zTab);
24285 zExists = sqlite3_mprintf("");
24287 if( zExists==0 ) rc = SQLITE_NOMEM;
24288 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
24289 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
24290 pAr->bVerbose ? "shell_putsnl(name)" : "name",
24291 pAr->azArg[i], pAr->zDir, zExists);
24292 rc = arExecSql(pAr, zSql2);
24293 sqlite3_free(zSql2);
24295 end_ar_transaction:
24296 if( rc!=SQLITE_OK ){
24297 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
24299 rc = arExecSql(pAr, "RELEASE ar;");
24300 if( pAr->bZip && pAr->zFile ){
24301 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
24302 arExecSql(pAr, zSql);
24303 sqlite3_free(zSql);
24306 sqlite3_free(zExists);
24311 ** Implementation of ".ar" dot command.
24313 static int arDotCommand(
24314 ShellState *pState, /* Current shell tool state */
24315 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
24316 char **azArg, /* Array of arguments passed to dot command */
24317 int nArg /* Number of entries in azArg[] */
24321 memset(&cmd, 0, sizeof(cmd));
24322 cmd.fromCmdLine = fromCmdLine;
24323 rc = arParseCommand(azArg, nArg, &cmd);
24324 if( rc==SQLITE_OK ){
24325 int eDbType = SHELL_OPEN_UNSPEC;
24327 cmd.db = pState->db;
24329 eDbType = deduceDatabaseType(cmd.zFile, 1);
24331 eDbType = pState->openMode;
24333 if( eDbType==SHELL_OPEN_ZIPFILE ){
24334 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
24335 if( cmd.zFile==0 ){
24336 cmd.zSrcTable = sqlite3_mprintf("zip");
24338 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
24342 }else if( cmd.zFile ){
24344 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
24345 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
24346 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
24347 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
24349 flags = SQLITE_OPEN_READONLY;
24353 oputf("-- open database '%s'%s\n", cmd.zFile,
24354 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
24356 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
24357 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
24358 if( rc!=SQLITE_OK ){
24359 eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
24360 goto end_ar_command;
24362 sqlite3_fileio_init(cmd.db, 0, 0);
24363 sqlite3_sqlar_init(cmd.db, 0, 0);
24364 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
24365 shellPutsFunc, 0, 0);
24368 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
24369 if( cmd.eCmd!=AR_CMD_CREATE
24370 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
24372 eputz("database does not contain an 'sqlar' table\n");
24374 goto end_ar_command;
24376 cmd.zSrcTable = sqlite3_mprintf("sqlar");
24379 switch( cmd.eCmd ){
24380 case AR_CMD_CREATE:
24381 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
24384 case AR_CMD_EXTRACT:
24385 rc = arExtractCommand(&cmd);
24389 rc = arListCommand(&cmd);
24393 arUsage(pState->out);
24396 case AR_CMD_INSERT:
24397 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
24400 case AR_CMD_REMOVE:
24401 rc = arRemoveCommand(&cmd);
24405 assert( cmd.eCmd==AR_CMD_UPDATE );
24406 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
24411 if( cmd.db!=pState->db ){
24414 sqlite3_free(cmd.zSrcTable);
24418 /* End of the ".archive" or ".ar" command logic
24419 *******************************************************************************/
24420 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
24422 #if SQLITE_SHELL_HAVE_RECOVER
24425 ** This function is used as a callback by the recover extension. Simply
24426 ** print the supplied SQL statement to stdout.
24428 static int recoverSqlCb(void *pCtx, const char *zSql){
24429 ShellState *pState = (ShellState*)pCtx;
24430 sputf(pState->out, "%s;\n", zSql);
24435 ** This function is called to recover data from the database. A script
24436 ** to construct a new database containing all recovered data is output
24437 ** on stream pState->out.
24439 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
24440 int rc = SQLITE_OK;
24441 const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
24442 const char *zLAF = "lost_and_found";
24443 int bFreelist = 1; /* 0 if --ignore-freelist is specified */
24444 int bRowids = 1; /* 0 if --no-rowids */
24445 sqlite3_recover *p = 0;
24448 for(i=1; i<nArg; i++){
24449 char *z = azArg[i];
24451 if( z[0]=='-' && z[1]=='-' ) z++;
24453 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
24456 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
24457 /* This option determines the name of the ATTACH-ed database used
24458 ** internally by the recovery extension. The default is "" which
24459 ** means to use a temporary database that is automatically deleted
24460 ** when closed. This option is undocumented and might disappear at
24463 zRecoveryDb = azArg[i];
24465 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
24469 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
24473 eputf("unexpected option: %s\n", azArg[i]);
24474 showHelp(pState->out, azArg[0]);
24479 p = sqlite3_recover_init_sql(
24480 pState->db, "main", recoverSqlCb, (void*)pState
24483 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
24484 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
24485 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
24486 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
24488 sqlite3_recover_run(p);
24489 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
24490 const char *zErr = sqlite3_recover_errmsg(p);
24491 int errCode = sqlite3_recover_errcode(p);
24492 eputf("sql error: %s (%d)\n", zErr, errCode);
24494 rc = sqlite3_recover_finish(p);
24497 #endif /* SQLITE_SHELL_HAVE_RECOVER */
24501 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
24502 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
24503 * close db and set it to 0, and return the columns spec, to later
24504 * be sqlite3_free()'ed by the caller.
24505 * The return is 0 when either:
24506 * (a) The db was not initialized and zCol==0 (There are no columns.)
24507 * (b) zCol!=0 (Column was added, db initialized as needed.)
24508 * The 3rd argument, pRenamed, references an out parameter. If the
24509 * pointer is non-zero, its referent will be set to a summary of renames
24510 * done if renaming was necessary, or set to 0 if none was done. The out
24511 * string (if any) must be sqlite3_free()'ed by the caller.
24514 #define rc_err_oom_die(rc) \
24515 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
24516 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
24517 eputf("E:%d\n",rc), assert(0)
24519 static void rc_err_oom_die(int rc){
24520 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
24521 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
24525 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
24526 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
24527 #else /* Otherwise, memory is faster/better for the transient DB. */
24528 static const char *zCOL_DB = ":memory:";
24531 /* Define character (as C string) to separate generated column ordinal
24532 * from protected part of incoming column names. This defaults to "_"
24533 * so that incoming column identifiers that did not need not be quoted
24534 * remain usable without being quoted. It must be one character.
24536 #ifndef SHELL_AUTOCOLUMN_SEP
24537 # define AUTOCOLUMN_SEP "_"
24539 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
24542 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
24543 /* Queries and D{D,M}L used here */
24544 static const char * const zTabMake = "\
24545 CREATE TABLE ColNames(\
24546 cpos INTEGER PRIMARY KEY,\
24547 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
24548 CREATE VIEW RepeatedNames AS \
24549 SELECT DISTINCT t.name FROM ColNames t \
24550 WHERE t.name COLLATE NOCASE IN (\
24551 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
24554 static const char * const zTabFill = "\
24555 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
24556 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
24558 static const char * const zHasDupes = "\
24559 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
24560 <count(name) FROM ColNames\
24562 #ifdef SHELL_COLUMN_RENAME_CLEAN
24563 static const char * const zDedoctor = "\
24564 UPDATE ColNames SET chop=iif(\
24565 (substring(name,nlen,1) BETWEEN '0' AND '9')\
24566 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
24567 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
24572 static const char * const zSetReps = "\
24573 UPDATE ColNames AS t SET reps=\
24574 (SELECT count(*) FROM ColNames d \
24575 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
24579 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
24580 static const char * const zColDigits = "\
24581 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
24584 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
24585 static const char * const zColDigits = "\
24586 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
24587 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
24588 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
24591 static const char * const zRenameRank =
24592 #ifdef SHELL_COLUMN_RENAME_CLEAN
24593 "UPDATE ColNames AS t SET suff="
24594 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
24595 #else /* ...RENAME_MINIMAL_ONE_PASS */
24596 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
24599 " SELECT nlz+1 AS nlz FROM Lzn"
24602 " FROM ColNames t, ColNames o"
24604 " iif(t.name IN (SELECT * FROM RepeatedNames),"
24605 " printf('%s"AUTOCOLUMN_SEP"%s',"
24606 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
24610 " iif(o.name IN (SELECT * FROM RepeatedNames),"
24611 " printf('%s"AUTOCOLUMN_SEP"%s',"
24612 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
24616 " AND o.cpos<>t.cpos"
24619 ") UPDATE Colnames AS t SET"
24620 " chop = 0," /* No chopping, never touch incoming names. */
24621 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
24622 " printf('"AUTOCOLUMN_SEP"%s', substring("
24623 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
24628 static const char * const zCollectVar = "\
24633 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
24634 ||')' AS ColsSpec \
24636 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
24637 FROM ColNames ORDER BY cpos\
24639 static const char * const zRenamesDone =
24640 "SELECT group_concat("
24641 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
24643 "FROM ColNames WHERE suff<>'' OR chop!=0"
24646 sqlite3_stmt *pStmt = 0;
24649 /* Add initial or additional column. Init db if necessary. */
24651 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
24652 #ifdef SHELL_COLFIX_DB
24654 sqlite3_exec(*pDb,"drop table if exists ColNames;"
24655 "drop view if exists RepeatedNames;",0,0,0);
24657 #undef SHELL_COLFIX_DB
24658 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
24659 rc_err_oom_die(rc);
24662 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
24663 rc_err_oom_die(rc);
24664 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
24665 rc_err_oom_die(rc);
24666 rc = sqlite3_step(pStmt);
24667 rc_err_oom_die(rc);
24668 sqlite3_finalize(pStmt);
24670 }else if( *pDb==0 ){
24673 /* Formulate the columns spec, close the DB, zero *pDb. */
24674 char *zColsSpec = 0;
24675 int hasDupes = db_int(*pDb, zHasDupes);
24676 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
24678 #ifdef SHELL_COLUMN_RENAME_CLEAN
24679 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
24680 rc_err_oom_die(rc);
24682 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
24683 rc_err_oom_die(rc);
24684 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
24685 rc_err_oom_die(rc);
24686 sqlite3_bind_int(pStmt, 1, nDigits);
24687 rc = sqlite3_step(pStmt);
24688 sqlite3_finalize(pStmt);
24689 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
24691 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
24692 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
24693 rc_err_oom_die(rc);
24694 rc = sqlite3_step(pStmt);
24695 if( rc==SQLITE_ROW ){
24696 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
24700 if( pzRenamed!=0 ){
24701 if( !hasDupes ) *pzRenamed = 0;
24703 sqlite3_finalize(pStmt);
24704 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
24705 && SQLITE_ROW==sqlite3_step(pStmt) ){
24706 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
24711 sqlite3_finalize(pStmt);
24712 sqlite3_close(*pDb);
24719 ** Check if the sqlite_schema table contains one or more virtual tables. If
24720 ** parameter zLike is not NULL, then it is an SQL expression that the
24721 ** sqlite_schema row must also match. If one or more such rows are found,
24722 ** print the following warning to the output:
24724 ** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
24726 static int outputDumpWarning(ShellState *p, const char *zLike){
24727 int rc = SQLITE_OK;
24728 sqlite3_stmt *pStmt = 0;
24729 shellPreparePrintf(p->db, &rc, &pStmt,
24730 "SELECT 1 FROM sqlite_schema o WHERE "
24731 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
24733 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
24734 oputz("/* WARNING: "
24735 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
24738 shellFinalize(&rc, pStmt);
24743 ** If an input line begins with "." then invoke this routine to
24744 ** process that line.
24746 ** Return 1 on error, 2 to exit, and 0 otherwise.
24748 static int do_meta_command(char *zLine, ShellState *p){
24755 #ifndef SQLITE_OMIT_VIRTUALTABLE
24756 if( p->expert.pExpert ){
24757 expertFinish(p, 1, 0);
24761 /* Parse the input line into tokens.
24763 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
24764 while( IsSpace(zLine[h]) ){ h++; }
24765 if( zLine[h]==0 ) break;
24766 if( zLine[h]=='\'' || zLine[h]=='"' ){
24767 int delim = zLine[h++];
24768 azArg[nArg++] = &zLine[h];
24769 while( zLine[h] && zLine[h]!=delim ){
24770 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
24773 if( zLine[h]==delim ){
24776 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
24778 azArg[nArg++] = &zLine[h];
24779 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
24780 if( zLine[h] ) zLine[h++] = 0;
24785 /* Process the input line.
24787 if( nArg==0 ) return 0; /* no tokens, no error */
24788 n = strlen30(azArg[0]);
24792 #ifndef SQLITE_OMIT_AUTHORIZATION
24793 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
24795 eputz("Usage: .auth ON|OFF\n");
24797 goto meta_command_exit;
24800 if( booleanValue(azArg[1]) ){
24801 sqlite3_set_authorizer(p->db, shellAuth, p);
24802 }else if( p->bSafeModePersist ){
24803 sqlite3_set_authorizer(p->db, safeModeAuth, p);
24805 sqlite3_set_authorizer(p->db, 0, 0);
24810 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
24811 && !defined(SQLITE_SHELL_FIDDLE)
24812 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
24814 failIfSafeMode(p, "cannot run .archive in safe mode");
24815 rc = arDotCommand(p, 0, azArg, nArg);
24819 #ifndef SQLITE_SHELL_FIDDLE
24820 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
24821 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
24823 const char *zDestFile = 0;
24824 const char *zDb = 0;
24826 sqlite3_backup *pBackup;
24829 const char *zVfs = 0;
24830 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
24831 for(j=1; j<nArg; j++){
24832 const char *z = azArg[j];
24834 if( z[1]=='-' ) z++;
24835 if( cli_strcmp(z, "-append")==0 ){
24838 if( cli_strcmp(z, "-async")==0 ){
24842 eputf("unknown option: %s\n", azArg[j]);
24845 }else if( zDestFile==0 ){
24846 zDestFile = azArg[j];
24847 }else if( zDb==0 ){
24849 zDestFile = azArg[j];
24851 eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
24855 if( zDestFile==0 ){
24856 eputz("missing FILENAME argument on .backup\n");
24859 if( zDb==0 ) zDb = "main";
24860 rc = sqlite3_open_v2(zDestFile, &pDest,
24861 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
24862 if( rc!=SQLITE_OK ){
24863 eputf("Error: cannot open \"%s\"\n", zDestFile);
24868 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
24872 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
24874 eputf("Error: %s\n", sqlite3_errmsg(pDest));
24878 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
24879 sqlite3_backup_finish(pBackup);
24880 if( rc==SQLITE_DONE ){
24883 eputf("Error: %s\n", sqlite3_errmsg(pDest));
24888 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24890 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
24892 bail_on_error = booleanValue(azArg[1]);
24894 eputz("Usage: .bail on|off\n");
24899 /* Undocumented. Legacy only. See "crnl" below */
24900 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
24902 if( booleanValue(azArg[1]) ){
24903 setBinaryMode(p->out, 1);
24905 setTextMode(p->out, 1);
24908 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
24909 "Usage: .binary on|off\n");
24914 /* The undocumented ".breakpoint" command causes a call to the no-op
24915 ** routine named test_breakpoint().
24917 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
24921 #ifndef SQLITE_SHELL_FIDDLE
24922 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
24923 failIfSafeMode(p, "cannot run .cd in safe mode");
24925 #if defined(_WIN32) || defined(WIN32)
24926 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
24927 rc = !SetCurrentDirectoryW(z);
24930 rc = chdir(azArg[1]);
24933 eputf("Cannot change to directory \"%s\"\n", azArg[1]);
24937 eputz("Usage: .cd DIRECTORY\n");
24941 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24943 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
24945 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
24947 eputz("Usage: .changes on|off\n");
24952 #ifndef SQLITE_SHELL_FIDDLE
24953 /* Cancel output redirection, if it is currently set (by .testcase)
24954 ** Then read the content of the testcase-out.txt file and compare against
24955 ** azArg[1]. If there are differences, report an error and exit.
24957 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
24961 eputz("Usage: .check GLOB-PATTERN\n");
24963 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
24965 }else if( testcase_glob(azArg[1],zRes)==0 ){
24966 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
24967 p->zTestcase, azArg[1], zRes);
24970 oputf("testcase-%s ok\n", p->zTestcase);
24973 sqlite3_free(zRes);
24975 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24977 #ifndef SQLITE_SHELL_FIDDLE
24978 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
24979 failIfSafeMode(p, "cannot run .clone in safe mode");
24981 tryToClone(p, azArg[1]);
24983 eputz("Usage: .clone FILENAME\n");
24987 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24989 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
24991 /* List available connections */
24993 for(i=0; i<ArraySize(p->aAuxDb); i++){
24994 const char *zFile = p->aAuxDb[i].zDbFilename;
24995 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
24996 zFile = "(not open)";
24997 }else if( zFile==0 ){
24998 zFile = "(memory)";
24999 }else if( zFile[0]==0 ){
25000 zFile = "(temporary-file)";
25002 if( p->pAuxDb == &p->aAuxDb[i] ){
25003 sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
25004 }else if( p->aAuxDb[i].db!=0 ){
25005 sputf(stdout, " %d: %s\n", i, zFile);
25008 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
25009 int i = azArg[1][0] - '0';
25010 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
25011 p->pAuxDb->db = p->db;
25012 p->pAuxDb = &p->aAuxDb[i];
25013 globalDb = p->db = p->pAuxDb->db;
25016 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
25017 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
25018 int i = azArg[2][0] - '0';
25019 if( i<0 || i>=ArraySize(p->aAuxDb) ){
25021 }else if( p->pAuxDb == &p->aAuxDb[i] ){
25022 eputz("cannot close the active database connection\n");
25024 }else if( p->aAuxDb[i].db ){
25025 session_close_all(p, i);
25026 close_db(p->aAuxDb[i].db);
25027 p->aAuxDb[i].db = 0;
25030 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
25035 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
25037 if( booleanValue(azArg[1]) ){
25038 setTextMode(p->out, 1);
25040 setBinaryMode(p->out, 1);
25043 #if !defined(_WIN32) && !defined(WIN32)
25044 eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
25046 eputz("Usage: .crnl on|off\n");
25051 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
25054 sqlite3_stmt *pStmt;
25057 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
25059 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25062 while( sqlite3_step(pStmt)==SQLITE_ROW ){
25063 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
25064 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
25065 if( zSchema==0 || zFile==0 ) continue;
25066 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
25067 shell_check_oom(azName);
25068 azName[nName*2] = strdup(zSchema);
25069 azName[nName*2+1] = strdup(zFile);
25073 sqlite3_finalize(pStmt);
25074 for(i=0; i<nName; i++){
25075 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
25076 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
25077 const char *z = azName[i*2+1];
25078 oputf("%s: %s %s%s\n",
25079 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
25080 eTxn==SQLITE_TXN_NONE ? "" :
25081 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
25083 free(azName[i*2+1]);
25085 sqlite3_free(azName);
25088 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
25089 static const struct DbConfigChoices {
25093 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
25094 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
25095 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
25096 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
25097 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
25098 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
25099 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
25100 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
25101 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
25102 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
25103 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
25104 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
25105 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
25106 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER },
25107 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS },
25108 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
25109 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
25110 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
25114 for(ii=0; ii<ArraySize(aDbConfig); ii++){
25115 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
25117 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
25119 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
25120 oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
25121 if( nArg>1 ) break;
25123 if( nArg>1 && ii==ArraySize(aDbConfig) ){
25124 eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
25125 eputz("Enter \".dbconfig\" with no arguments for a list\n");
25129 #if SQLITE_SHELL_HAVE_RECOVER
25130 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
25131 rc = shell_dbinfo_command(p, nArg, azArg);
25134 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
25136 rc = recoverDatabaseCmd(p, nArg, azArg);
25138 #endif /* SQLITE_SHELL_HAVE_RECOVER */
25140 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
25144 int savedShowHeader = p->showHeader;
25145 int savedShellFlags = p->shellFlgs;
25147 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
25148 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
25149 for(i=1; i<nArg; i++){
25150 if( azArg[i][0]=='-' ){
25151 const char *z = azArg[i]+1;
25152 if( z[0]=='-' ) z++;
25153 if( cli_strcmp(z,"preserve-rowids")==0 ){
25154 #ifdef SQLITE_OMIT_VIRTUALTABLE
25155 eputz("The --preserve-rowids option is not compatible"
25156 " with SQLITE_OMIT_VIRTUALTABLE\n");
25158 sqlite3_free(zLike);
25159 goto meta_command_exit;
25161 ShellSetFlag(p, SHFLG_PreserveRowid);
25164 if( cli_strcmp(z,"newlines")==0 ){
25165 ShellSetFlag(p, SHFLG_Newlines);
25167 if( cli_strcmp(z,"data-only")==0 ){
25168 ShellSetFlag(p, SHFLG_DumpDataOnly);
25170 if( cli_strcmp(z,"nosys")==0 ){
25171 ShellSetFlag(p, SHFLG_DumpNoSys);
25174 eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
25176 sqlite3_free(zLike);
25177 goto meta_command_exit;
25180 /* azArg[i] contains a LIKE pattern. This ".dump" request should
25181 ** only dump data for tables for which either the table name matches
25182 ** the LIKE pattern, or the table appears to be a shadow table of
25183 ** a virtual table for which the name matches the LIKE pattern.
25185 char *zExpr = sqlite3_mprintf(
25186 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
25187 " SELECT 1 FROM sqlite_schema WHERE "
25188 " name LIKE %Q ESCAPE '\\' AND"
25189 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
25190 " substr(o.name, 1, length(name)+1) == (name||'_')"
25191 ")", azArg[i], azArg[i]
25195 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
25204 outputDumpWarning(p, zLike);
25205 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25206 /* When playing back a "dump", the content might appear in an order
25207 ** which causes immediate foreign key constraints to be violated.
25208 ** So disable foreign-key constraint enforcement to prevent problems. */
25209 oputz("PRAGMA foreign_keys=OFF;\n");
25210 oputz("BEGIN TRANSACTION;\n");
25212 p->writableSchema = 0;
25214 /* Set writable_schema=ON since doing so forces SQLite to initialize
25215 ** as much of the schema as it can even if the sqlite_schema table is
25217 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
25219 if( zLike==0 ) zLike = sqlite3_mprintf("true");
25220 zSql = sqlite3_mprintf(
25221 "SELECT name, type, sql FROM sqlite_schema AS o "
25222 "WHERE (%s) AND type=='table'"
25223 " AND sql NOT NULL"
25224 " ORDER BY tbl_name='sqlite_sequence', rowid",
25227 run_schema_dump_query(p,zSql);
25228 sqlite3_free(zSql);
25229 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25230 zSql = sqlite3_mprintf(
25231 "SELECT sql FROM sqlite_schema AS o "
25232 "WHERE (%s) AND sql NOT NULL"
25233 " AND type IN ('index','trigger','view')",
25236 run_table_dump_query(p, zSql);
25237 sqlite3_free(zSql);
25239 sqlite3_free(zLike);
25240 if( p->writableSchema ){
25241 oputz("PRAGMA writable_schema=OFF;\n");
25242 p->writableSchema = 0;
25244 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
25245 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
25246 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25247 oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
25249 p->showHeader = savedShowHeader;
25250 p->shellFlgs = savedShellFlags;
25253 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
25255 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
25257 eputz("Usage: .echo on|off\n");
25262 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
25264 p->autoEQPtest = 0;
25265 if( p->autoEQPtrace ){
25266 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
25267 p->autoEQPtrace = 0;
25269 if( cli_strcmp(azArg[1],"full")==0 ){
25270 p->autoEQP = AUTOEQP_full;
25271 }else if( cli_strcmp(azArg[1],"trigger")==0 ){
25272 p->autoEQP = AUTOEQP_trigger;
25273 #ifdef SQLITE_DEBUG
25274 }else if( cli_strcmp(azArg[1],"test")==0 ){
25275 p->autoEQP = AUTOEQP_on;
25276 p->autoEQPtest = 1;
25277 }else if( cli_strcmp(azArg[1],"trace")==0 ){
25278 p->autoEQP = AUTOEQP_full;
25279 p->autoEQPtrace = 1;
25281 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
25282 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
25285 p->autoEQP = (u8)booleanValue(azArg[1]);
25288 eputz("Usage: .eqp off|on|trace|trigger|full\n");
25293 #ifndef SQLITE_SHELL_FIDDLE
25294 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
25295 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
25300 /* The ".explain" command is automatic now. It is largely pointless. It
25301 ** retained purely for backwards compatibility */
25302 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
25305 if( cli_strcmp(azArg[1],"auto")==0 ){
25308 val = booleanValue(azArg[1]);
25311 if( val==1 && p->mode!=MODE_Explain ){
25312 p->normalMode = p->mode;
25313 p->mode = MODE_Explain;
25314 p->autoExplain = 0;
25315 }else if( val==0 ){
25316 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
25317 p->autoExplain = 0;
25318 }else if( val==99 ){
25319 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
25320 p->autoExplain = 1;
25324 #ifndef SQLITE_OMIT_VIRTUALTABLE
25325 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
25326 if( p->bSafeMode ){
25327 eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
25332 expertDotCommand(p, azArg, nArg);
25337 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
25338 static const struct {
25339 const char *zCtrlName; /* Name of a test-control option */
25340 int ctrlCode; /* Integer code for that option */
25341 const char *zUsage; /* Usage notes */
25343 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
25344 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
25345 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
25346 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
25347 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
25348 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
25349 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
25350 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
25351 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
25352 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
25353 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
25357 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
25358 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
25360 const char *zCmd = 0;
25361 const char *zSchema = 0;
25364 zCmd = nArg>=2 ? azArg[1] : "help";
25367 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
25370 zSchema = azArg[2];
25371 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
25376 /* The argument can optionally begin with "-" or "--" */
25377 if( zCmd[0]=='-' && zCmd[1] ){
25379 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
25382 /* --help lists all file-controls */
25383 if( cli_strcmp(zCmd,"help")==0 ){
25384 oputz("Available file-controls:\n");
25385 for(i=0; i<ArraySize(aCtrl); i++){
25386 oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
25389 goto meta_command_exit;
25392 /* convert filectrl text option to value. allow any unique prefix
25393 ** of the option name, or a numerical value. */
25394 n2 = strlen30(zCmd);
25395 for(i=0; i<ArraySize(aCtrl); i++){
25396 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
25398 filectrl = aCtrl[i].ctrlCode;
25401 eputf("Error: ambiguous file-control: \"%s\"\n"
25402 "Use \".filectrl --help\" for help\n", zCmd);
25404 goto meta_command_exit;
25409 eputf("Error: unknown file-control: %s\n"
25410 "Use \".filectrl --help\" for help\n", zCmd);
25413 case SQLITE_FCNTL_SIZE_LIMIT: {
25414 if( nArg!=2 && nArg!=3 ) break;
25415 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
25416 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
25420 case SQLITE_FCNTL_LOCK_TIMEOUT:
25421 case SQLITE_FCNTL_CHUNK_SIZE: {
25423 if( nArg!=3 ) break;
25424 x = (int)integerValue(azArg[2]);
25425 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25429 case SQLITE_FCNTL_PERSIST_WAL:
25430 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
25432 if( nArg!=2 && nArg!=3 ) break;
25433 x = nArg==3 ? booleanValue(azArg[2]) : -1;
25434 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25439 case SQLITE_FCNTL_DATA_VERSION:
25440 case SQLITE_FCNTL_HAS_MOVED: {
25442 if( nArg!=2 ) break;
25443 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25448 case SQLITE_FCNTL_TEMPFILENAME: {
25450 if( nArg!=2 ) break;
25451 sqlite3_file_control(p->db, zSchema, filectrl, &z);
25459 case SQLITE_FCNTL_RESERVE_BYTES: {
25462 x = atoi(azArg[2]);
25463 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25466 sqlite3_file_control(p->db, zSchema, filectrl, &x);
25473 if( isOk==0 && iCtrl>=0 ){
25474 oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
25476 }else if( isOk==1 ){
25478 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
25479 oputf("%s\n", zBuf);
25483 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
25486 memcpy(&data, p, sizeof(data));
25487 data.showHeader = 0;
25488 data.cMode = data.mode = MODE_Semi;
25489 if( nArg==2 && optionMatch(azArg[1], "indent") ){
25490 data.cMode = data.mode = MODE_Pretty;
25494 eputz("Usage: .fullschema ?--indent?\n");
25496 goto meta_command_exit;
25499 rc = sqlite3_exec(p->db,
25501 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
25502 " FROM sqlite_schema UNION ALL"
25503 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
25504 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
25508 if( rc==SQLITE_OK ){
25509 sqlite3_stmt *pStmt;
25510 rc = sqlite3_prepare_v2(p->db,
25511 "SELECT rowid FROM sqlite_schema"
25512 " WHERE name GLOB 'sqlite_stat[134]'",
25514 if( rc==SQLITE_OK ){
25515 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
25516 sqlite3_finalize(pStmt);
25520 oputz("/* No STAT tables available */\n");
25522 oputz("ANALYZE sqlite_schema;\n");
25523 data.cMode = data.mode = MODE_Insert;
25524 data.zDestTable = "sqlite_stat1";
25525 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
25526 data.zDestTable = "sqlite_stat4";
25527 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
25528 oputz("ANALYZE sqlite_schema;\n");
25532 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
25534 p->showHeader = booleanValue(azArg[1]);
25535 p->shellFlgs |= SHFLG_HeaderSet;
25537 eputz("Usage: .headers on|off\n");
25542 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
25544 n = showHelp(p->out, azArg[1]);
25546 oputf("Nothing matches '%s'\n", azArg[1]);
25549 showHelp(p->out, 0);
25553 #ifndef SQLITE_SHELL_FIDDLE
25554 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
25555 char *zTable = 0; /* Insert data into this table */
25556 char *zSchema = 0; /* within this schema (may default to "main") */
25557 char *zFile = 0; /* Name of file to extra content from */
25558 sqlite3_stmt *pStmt = NULL; /* A statement */
25559 int nCol; /* Number of columns in the table */
25560 int nByte; /* Number of bytes in an SQL string */
25561 int i, j; /* Loop counters */
25562 int needCommit; /* True to COMMIT or ROLLBACK at end */
25563 int nSep; /* Number of bytes in p->colSeparator[] */
25564 char *zSql; /* An SQL statement */
25565 char *zFullTabName; /* Table name with schema if applicable */
25566 ImportCtx sCtx; /* Reader context */
25567 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
25568 int eVerbose = 0; /* Larger for more console output */
25569 int nSkip = 0; /* Initial lines to skip */
25570 int useOutputMode = 1; /* Use output mode to determine separators */
25571 char *zCreate = 0; /* CREATE TABLE statement text */
25573 failIfSafeMode(p, "cannot run .import in safe mode");
25574 memset(&sCtx, 0, sizeof(sCtx));
25575 if( p->mode==MODE_Ascii ){
25576 xRead = ascii_read_one_field;
25578 xRead = csv_read_one_field;
25581 for(i=1; i<nArg; i++){
25582 char *z = azArg[i];
25583 if( z[0]=='-' && z[1]=='-' ) z++;
25587 }else if( zTable==0 ){
25590 oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
25591 showHelp(p->out, "import");
25592 goto meta_command_exit;
25594 }else if( cli_strcmp(z,"-v")==0 ){
25596 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
25597 zSchema = azArg[++i];
25598 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
25599 nSkip = integerValue(azArg[++i]);
25600 }else if( cli_strcmp(z,"-ascii")==0 ){
25601 sCtx.cColSep = SEP_Unit[0];
25602 sCtx.cRowSep = SEP_Record[0];
25603 xRead = ascii_read_one_field;
25605 }else if( cli_strcmp(z,"-csv")==0 ){
25606 sCtx.cColSep = ',';
25607 sCtx.cRowSep = '\n';
25608 xRead = csv_read_one_field;
25611 oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
25612 showHelp(p->out, "import");
25613 goto meta_command_exit;
25617 oputf("ERROR: missing %s argument. Usage:\n",
25618 zFile==0 ? "FILE" : "TABLE");
25619 showHelp(p->out, "import");
25620 goto meta_command_exit;
25624 if( useOutputMode ){
25625 /* If neither the --csv or --ascii options are specified, then set
25626 ** the column and row separator characters from the output mode. */
25627 nSep = strlen30(p->colSeparator);
25629 eputz("Error: non-null column separator required for import\n");
25630 goto meta_command_exit;
25633 eputz("Error: multi-character column separators not allowed"
25635 goto meta_command_exit;
25637 nSep = strlen30(p->rowSeparator);
25639 eputz("Error: non-null row separator required for import\n");
25640 goto meta_command_exit;
25642 if( nSep==2 && p->mode==MODE_Csv
25643 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
25645 /* When importing CSV (only), if the row separator is set to the
25646 ** default output row separator, change it to the default input
25647 ** row separator. This avoids having to maintain different input
25648 ** and output row separators. */
25649 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
25650 nSep = strlen30(p->rowSeparator);
25653 eputz("Error: multi-character row separators not allowed"
25655 goto meta_command_exit;
25657 sCtx.cColSep = (u8)p->colSeparator[0];
25658 sCtx.cRowSep = (u8)p->rowSeparator[0];
25660 sCtx.zFile = zFile;
25662 if( sCtx.zFile[0]=='|' ){
25663 #ifdef SQLITE_OMIT_POPEN
25664 eputz("Error: pipes are not supported in this OS\n");
25665 goto meta_command_exit;
25667 sCtx.in = popen(sCtx.zFile+1, "r");
25668 sCtx.zFile = "<pipe>";
25669 sCtx.xCloser = pclose;
25672 sCtx.in = fopen(sCtx.zFile, "rb");
25673 sCtx.xCloser = fclose;
25676 eputf("Error: cannot open \"%s\"\n", zFile);
25677 goto meta_command_exit;
25679 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
25682 zSep[0] = sCtx.cColSep;
25683 oputz("Column separator ");
25684 output_c_string(zSep);
25685 oputz(", row separator ");
25686 zSep[0] = sCtx.cRowSep;
25687 output_c_string(zSep);
25690 sCtx.z = sqlite3_malloc64(120);
25692 import_cleanup(&sCtx);
25693 shell_out_of_memory();
25695 /* Below, resources must be freed before exit. */
25696 while( (nSkip--)>0 ){
25697 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
25700 zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
25702 zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
25704 zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
25705 if( zSql==0 || zFullTabName==0 ){
25706 import_cleanup(&sCtx);
25707 shell_out_of_memory();
25709 nByte = strlen30(zSql);
25710 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25711 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
25712 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
25713 sqlite3 *dbCols = 0;
25714 char *zRenames = 0;
25716 zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
25717 while( xRead(&sCtx) ){
25718 zAutoColumn(sCtx.z, &dbCols, 0);
25719 if( sCtx.cTerm!=sCtx.cColSep ) break;
25721 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
25723 sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
25724 "Columns renamed during .import %s due to duplicates:\n"
25725 "%s\n", sCtx.zFile, zRenames);
25726 sqlite3_free(zRenames);
25730 eputf("%s: empty file\n", sCtx.zFile);
25732 sqlite3_free(zCreate);
25733 sqlite3_free(zSql);
25734 sqlite3_free(zFullTabName);
25735 import_cleanup(&sCtx);
25737 goto meta_command_exit;
25739 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
25741 oputf("%s\n", zCreate);
25743 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
25745 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
25748 sqlite3_free(zCreate);
25750 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25753 if (pStmt) sqlite3_finalize(pStmt);
25754 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25757 sqlite3_free(zSql);
25758 nCol = sqlite3_column_count(pStmt);
25759 sqlite3_finalize(pStmt);
25761 if( nCol==0 ) return 0; /* no columns, no error */
25762 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
25764 import_cleanup(&sCtx);
25765 shell_out_of_memory();
25767 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
25768 j = strlen30(zSql);
25769 for(i=1; i<nCol; i++){
25776 oputf("Insert using: %s\n", zSql);
25778 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25780 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25781 if (pStmt) sqlite3_finalize(pStmt);
25784 sqlite3_free(zSql);
25785 sqlite3_free(zFullTabName);
25786 needCommit = sqlite3_get_autocommit(p->db);
25787 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
25789 int startLine = sCtx.nLine;
25790 for(i=0; i<nCol; i++){
25791 char *z = xRead(&sCtx);
25793 ** Did we reach end-of-file before finding any columns?
25794 ** If so, stop instead of NULL filling the remaining columns.
25796 if( z==0 && i==0 ) break;
25798 ** Did we reach end-of-file OR end-of-line before finding any
25799 ** columns in ASCII mode? If so, stop instead of NULL filling
25800 ** the remaining columns.
25802 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
25804 ** For CSV mode, per RFC 4180, accept EOF in lieu of final
25805 ** record terminator but only for last field of multi-field row.
25806 ** (If there are too few fields, it's not valid CSV anyway.)
25808 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
25811 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
25812 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
25813 eputf("%s:%d: expected %d columns but found %d"
25814 " - filling the rest with NULL\n",
25815 sCtx.zFile, startLine, nCol, i+1);
25817 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
25820 if( sCtx.cTerm==sCtx.cColSep ){
25824 }while( sCtx.cTerm==sCtx.cColSep );
25825 eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
25826 sCtx.zFile, startLine, nCol, i);
25829 sqlite3_step(pStmt);
25830 rc = sqlite3_reset(pStmt);
25831 if( rc!=SQLITE_OK ){
25832 eputf("%s:%d: INSERT failed: %s\n",
25833 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
25839 }while( sCtx.cTerm!=EOF );
25841 import_cleanup(&sCtx);
25842 sqlite3_finalize(pStmt);
25843 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
25845 oputf("Added %d rows with %d errors using %d lines of input\n",
25846 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
25849 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25851 #ifndef SQLITE_UNTESTABLE
25852 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
25854 char *zCollist = 0;
25855 sqlite3_stmt *pStmt;
25857 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
25858 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
25860 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
25861 eputf(".%s unavailable without --unsafe-testing\n",
25864 goto meta_command_exit;
25866 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
25867 eputz("Usage: .imposter INDEX IMPOSTER\n"
25868 " .imposter off\n");
25869 /* Also allowed, but not documented:
25871 ** .imposter TABLE IMPOSTER
25873 ** where TABLE is a WITHOUT ROWID table. In that case, the
25874 ** imposter is another WITHOUT ROWID table with the columns in
25875 ** storage order. */
25877 goto meta_command_exit;
25881 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
25882 goto meta_command_exit;
25884 zSql = sqlite3_mprintf(
25885 "SELECT rootpage, 0 FROM sqlite_schema"
25886 " WHERE name='%q' AND type='index'"
25888 "SELECT rootpage, 1 FROM sqlite_schema"
25889 " WHERE name='%q' AND type='table'"
25890 " AND sql LIKE '%%without%%rowid%%'",
25893 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25894 sqlite3_free(zSql);
25895 if( sqlite3_step(pStmt)==SQLITE_ROW ){
25896 tnum = sqlite3_column_int(pStmt, 0);
25897 isWO = sqlite3_column_int(pStmt, 1);
25899 sqlite3_finalize(pStmt);
25900 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
25901 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25902 sqlite3_free(zSql);
25904 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
25906 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
25909 if( sqlite3_column_int(pStmt,1)==-1 ){
25912 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
25916 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
25917 lenPK = (int)strlen(zCollist);
25920 zCollist = sqlite3_mprintf("\"%w\"", zCol);
25922 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
25925 sqlite3_finalize(pStmt);
25926 if( i==0 || tnum==0 ){
25927 eputf("no such index: \"%s\"\n", azArg[1]);
25929 sqlite3_free(zCollist);
25930 goto meta_command_exit;
25932 if( lenPK==0 ) lenPK = 100000;
25933 zSql = sqlite3_mprintf(
25934 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
25935 azArg[2], zCollist, lenPK, zCollist);
25936 sqlite3_free(zCollist);
25937 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
25938 if( rc==SQLITE_OK ){
25939 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
25940 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
25942 eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
25944 sputf(stdout, "%s;\n", zSql);
25945 sputf(stdout, "WARNING: writing to an imposter table will corrupt"
25946 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
25949 eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
25952 sqlite3_free(zSql);
25954 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
25956 #ifdef SQLITE_ENABLE_IOTRACE
25957 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
25958 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
25959 if( iotrace && iotrace!=stdout ) fclose(iotrace);
25962 sqlite3IoTrace = 0;
25963 }else if( cli_strcmp(azArg[1], "-")==0 ){
25964 sqlite3IoTrace = iotracePrintf;
25967 iotrace = fopen(azArg[1], "w");
25969 eputf("Error: cannot open \"%s\"\n", azArg[1]);
25970 sqlite3IoTrace = 0;
25973 sqlite3IoTrace = iotracePrintf;
25979 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
25980 static const struct {
25981 const char *zLimitName; /* Name of a limit */
25982 int limitCode; /* Integer code for that limit */
25984 { "length", SQLITE_LIMIT_LENGTH },
25985 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
25986 { "column", SQLITE_LIMIT_COLUMN },
25987 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
25988 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
25989 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
25990 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
25991 { "attached", SQLITE_LIMIT_ATTACHED },
25992 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
25993 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
25994 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
25995 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
26000 for(i=0; i<ArraySize(aLimit); i++){
26001 sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
26002 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
26004 }else if( nArg>3 ){
26005 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
26007 goto meta_command_exit;
26010 n2 = strlen30(azArg[1]);
26011 for(i=0; i<ArraySize(aLimit); i++){
26012 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
26016 eputf("ambiguous limit: \"%s\"\n", azArg[1]);
26018 goto meta_command_exit;
26023 eputf("unknown limit: \"%s\"\n"
26024 "enter \".limits\" with no arguments for a list.\n",
26027 goto meta_command_exit;
26030 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
26031 (int)integerValue(azArg[2]));
26033 sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
26034 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
26038 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
26040 lintDotCommand(p, azArg, nArg);
26043 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
26044 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
26045 const char *zFile, *zProc;
26047 failIfSafeMode(p, "cannot run .load in safe mode");
26048 if( nArg<2 || azArg[1][0]==0 ){
26049 /* Must have a non-empty FILE. (Will not load self.) */
26050 eputz("Usage: .load FILE ?ENTRYPOINT?\n");
26052 goto meta_command_exit;
26055 zProc = nArg>=3 ? azArg[2] : 0;
26057 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
26058 if( rc!=SQLITE_OK ){
26059 eputf("Error: %s\n", zErrMsg);
26060 sqlite3_free(zErrMsg);
26066 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
26068 eputz("Usage: .log FILENAME\n");
26071 const char *zFile = azArg[1];
26073 && cli_strcmp(zFile,"on")!=0
26074 && cli_strcmp(zFile,"off")!=0
26076 sputz(stdout, "cannot set .log to anything other"
26077 " than \"on\" or \"off\"\n");
26080 output_file_close(p->pLog);
26081 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
26082 p->pLog = output_file_open(zFile, 0);
26086 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
26087 const char *zMode = 0;
26088 const char *zTabname = 0;
26090 ColModeOpts cmOpts = ColModeOpts_default;
26091 for(i=1; i<nArg; i++){
26092 const char *z = azArg[i];
26093 if( optionMatch(z,"wrap") && i+1<nArg ){
26094 cmOpts.iWrap = integerValue(azArg[++i]);
26095 }else if( optionMatch(z,"ww") ){
26096 cmOpts.bWordWrap = 1;
26097 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
26098 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
26099 }else if( optionMatch(z,"quote") ){
26101 }else if( optionMatch(z,"noquote") ){
26103 }else if( zMode==0 ){
26105 /* Apply defaults for qbox pseudo-mode. If that
26106 * overwrites already-set values, user was informed of this.
26108 if( cli_strcmp(z, "qbox")==0 ){
26109 ColModeOpts cmo = ColModeOpts_default_qbox;
26113 }else if( zTabname==0 ){
26115 }else if( z[0]=='-' ){
26116 eputf("unknown option: %s\n", z);
26120 " --wordwrap on/off\n"
26124 goto meta_command_exit;
26126 eputf("extra argument: \"%s\"\n", z);
26128 goto meta_command_exit;
26132 if( p->mode==MODE_Column
26133 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
26135 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
26136 modeDescr[p->mode], p->cmOpts.iWrap,
26137 p->cmOpts.bWordWrap ? "on" : "off",
26138 p->cmOpts.bQuote ? "" : "no");
26140 oputf("current output mode: %s\n", modeDescr[p->mode]);
26142 zMode = modeDescr[p->mode];
26144 n2 = strlen30(zMode);
26145 if( cli_strncmp(zMode,"lines",n2)==0 ){
26146 p->mode = MODE_Line;
26147 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26148 }else if( cli_strncmp(zMode,"columns",n2)==0 ){
26149 p->mode = MODE_Column;
26150 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
26153 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26154 p->cmOpts = cmOpts;
26155 }else if( cli_strncmp(zMode,"list",n2)==0 ){
26156 p->mode = MODE_List;
26157 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
26158 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26159 }else if( cli_strncmp(zMode,"html",n2)==0 ){
26160 p->mode = MODE_Html;
26161 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
26162 p->mode = MODE_Tcl;
26163 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
26164 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26165 }else if( cli_strncmp(zMode,"csv",n2)==0 ){
26166 p->mode = MODE_Csv;
26167 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26168 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
26169 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
26170 p->mode = MODE_List;
26171 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
26172 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
26173 p->mode = MODE_Insert;
26174 set_table_name(p, zTabname ? zTabname : "table");
26175 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
26176 p->mode = MODE_Quote;
26177 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26178 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26179 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
26180 p->mode = MODE_Ascii;
26181 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
26182 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
26183 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
26184 p->mode = MODE_Markdown;
26185 p->cmOpts = cmOpts;
26186 }else if( cli_strncmp(zMode,"table",n2)==0 ){
26187 p->mode = MODE_Table;
26188 p->cmOpts = cmOpts;
26189 }else if( cli_strncmp(zMode,"box",n2)==0 ){
26190 p->mode = MODE_Box;
26191 p->cmOpts = cmOpts;
26192 }else if( cli_strncmp(zMode,"count",n2)==0 ){
26193 p->mode = MODE_Count;
26194 }else if( cli_strncmp(zMode,"off",n2)==0 ){
26195 p->mode = MODE_Off;
26196 }else if( cli_strncmp(zMode,"json",n2)==0 ){
26197 p->mode = MODE_Json;
26199 eputz("Error: mode should be one of: "
26200 "ascii box column csv html insert json line list markdown "
26201 "qbox quote table tabs tcl\n");
26204 p->cMode = p->mode;
26207 #ifndef SQLITE_SHELL_FIDDLE
26208 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
26210 eputz("Usage: .nonce NONCE\n");
26212 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
26213 eputf("line %d: incorrect nonce: \"%s\"\n",
26214 p->lineno, azArg[1]);
26218 return 0; /* Return immediately to bypass the safe mode reset
26219 ** at the end of this procedure */
26222 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26224 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
26226 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
26227 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
26229 eputz("Usage: .nullvalue STRING\n");
26234 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
26235 const char *zFN = 0; /* Pointer to constant filename */
26236 char *zNewFilename = 0; /* Name of the database file to open */
26237 int iName = 1; /* Index in azArg[] of the filename */
26238 int newFlag = 0; /* True to delete file before opening */
26239 int openMode = SHELL_OPEN_UNSPEC;
26241 /* Check for command-line arguments */
26242 for(iName=1; iName<nArg; iName++){
26243 const char *z = azArg[iName];
26244 #ifndef SQLITE_SHELL_FIDDLE
26245 if( optionMatch(z,"new") ){
26247 #ifdef SQLITE_HAVE_ZLIB
26248 }else if( optionMatch(z, "zip") ){
26249 openMode = SHELL_OPEN_ZIPFILE;
26251 }else if( optionMatch(z, "append") ){
26252 openMode = SHELL_OPEN_APPENDVFS;
26253 }else if( optionMatch(z, "readonly") ){
26254 openMode = SHELL_OPEN_READONLY;
26255 }else if( optionMatch(z, "nofollow") ){
26256 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
26257 #ifndef SQLITE_OMIT_DESERIALIZE
26258 }else if( optionMatch(z, "deserialize") ){
26259 openMode = SHELL_OPEN_DESERIALIZE;
26260 }else if( optionMatch(z, "hexdb") ){
26261 openMode = SHELL_OPEN_HEXDB;
26262 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
26263 p->szMax = integerValue(azArg[++iName]);
26264 #endif /* SQLITE_OMIT_DESERIALIZE */
26266 #endif /* !SQLITE_SHELL_FIDDLE */
26268 eputf("unknown option: %s\n", z);
26270 goto meta_command_exit;
26272 eputf("extra argument: \"%s\"\n", z);
26274 goto meta_command_exit;
26280 /* Close the existing database */
26281 session_close_all(p, -1);
26284 p->pAuxDb->zDbFilename = 0;
26285 sqlite3_free(p->pAuxDb->zFreeOnClose);
26286 p->pAuxDb->zFreeOnClose = 0;
26287 p->openMode = openMode;
26291 /* If a filename is specified, try to open it first */
26292 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
26293 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
26294 #ifndef SQLITE_SHELL_FIDDLE
26296 && p->openMode!=SHELL_OPEN_HEXDB
26298 && cli_strcmp(zFN,":memory:")!=0
26300 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
26303 /* WASM mode has its own sandboxed pseudo-filesystem. */
26306 zNewFilename = sqlite3_mprintf("%s", zFN);
26307 shell_check_oom(zNewFilename);
26311 p->pAuxDb->zDbFilename = zNewFilename;
26312 open_db(p, OPEN_DB_KEEPALIVE);
26314 eputf("Error: cannot open '%s'\n", zNewFilename);
26315 sqlite3_free(zNewFilename);
26317 p->pAuxDb->zFreeOnClose = zNewFilename;
26321 /* As a fall-back open a TEMP database */
26322 p->pAuxDb->zDbFilename = 0;
26327 #ifndef SQLITE_SHELL_FIDDLE
26329 && (cli_strncmp(azArg[0], "output", n)==0
26330 || cli_strncmp(azArg[0], "once", n)==0))
26331 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
26337 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
26338 static const char *zBomUtf8 = "\xef\xbb\xbf";
26339 const char *zBom = 0;
26341 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
26345 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
26348 for(i=1; i<nArg; i++){
26349 char *z = azArg[i];
26351 if( z[1]=='-' ) z++;
26352 if( cli_strcmp(z,"-bom")==0 ){
26354 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
26355 eMode = 'x'; /* spreadsheet */
26356 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
26357 eMode = 'e'; /* text editor */
26359 oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
26360 showHelp(p->out, azArg[0]);
26362 goto meta_command_exit;
26364 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
26365 zFile = sqlite3_mprintf("%s", z);
26366 if( zFile && zFile[0]=='|' ){
26367 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
26371 oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
26372 showHelp(p->out, azArg[0]);
26374 sqlite3_free(zFile);
26375 goto meta_command_exit;
26379 zFile = sqlite3_mprintf("stdout");
26387 #ifndef SQLITE_NOHAVE_SYSTEM
26388 if( eMode=='e' || eMode=='x' ){
26392 /* spreadsheet mode. Output as CSV. */
26393 newTempFile(p, "csv");
26394 ShellClearFlag(p, SHFLG_Echo);
26395 p->mode = MODE_Csv;
26396 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26397 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
26399 /* text editor mode */
26400 newTempFile(p, "txt");
26403 sqlite3_free(zFile);
26404 zFile = sqlite3_mprintf("%s", p->zTempFile);
26406 #endif /* SQLITE_NOHAVE_SYSTEM */
26407 shell_check_oom(zFile);
26408 if( zFile[0]=='|' ){
26409 #ifdef SQLITE_OMIT_POPEN
26410 eputz("Error: pipes are not supported in this OS\n");
26412 output_redir(p, stdout);
26414 FILE *pfPipe = popen(zFile + 1, "w");
26416 eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
26419 output_redir(p, pfPipe);
26420 if( zBom ) oputz(zBom);
26421 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26425 FILE *pfFile = output_file_open(zFile, bTxtMode);
26427 if( cli_strcmp(zFile,"off")!=0 ){
26428 eputf("Error: cannot write to \"%s\"\n", zFile);
26432 output_redir(p, pfFile);
26433 if( zBom ) oputz(zBom);
26434 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26437 sqlite3_free(zFile);
26439 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26441 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
26443 if( nArg<=1 ) goto parameter_syntax_error;
26445 /* .parameter clear
26446 ** Clear all bind parameters by dropping the TEMP table that holds them.
26448 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
26449 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
26454 ** List all bind parameters.
26456 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
26457 sqlite3_stmt *pStmt = 0;
26460 rx = sqlite3_prepare_v2(p->db,
26461 "SELECT max(length(key)) "
26462 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
26463 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
26464 len = sqlite3_column_int(pStmt, 0);
26465 if( len>40 ) len = 40;
26467 sqlite3_finalize(pStmt);
26470 rx = sqlite3_prepare_v2(p->db,
26471 "SELECT key, quote(value) "
26472 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
26473 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
26474 oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
26475 sqlite3_column_text(pStmt,1));
26477 sqlite3_finalize(pStmt);
26482 ** Make sure the TEMP table used to hold bind parameters exists.
26483 ** Create it if necessary.
26485 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
26486 bind_table_init(p);
26489 /* .parameter set NAME VALUE
26490 ** Set or reset a bind parameter. NAME should be the full parameter
26491 ** name exactly as it appears in the query. (ex: $abc, @def). The
26492 ** VALUE can be in either SQL literal notation, or if not it will be
26493 ** understood to be a text string.
26495 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
26498 sqlite3_stmt *pStmt;
26499 const char *zKey = azArg[2];
26500 const char *zValue = azArg[3];
26501 bind_table_init(p);
26502 zSql = sqlite3_mprintf(
26503 "REPLACE INTO temp.sqlite_parameters(key,value)"
26504 "VALUES(%Q,%s);", zKey, zValue);
26505 shell_check_oom(zSql);
26507 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26508 sqlite3_free(zSql);
26509 if( rx!=SQLITE_OK ){
26510 sqlite3_finalize(pStmt);
26512 zSql = sqlite3_mprintf(
26513 "REPLACE INTO temp.sqlite_parameters(key,value)"
26514 "VALUES(%Q,%Q);", zKey, zValue);
26515 shell_check_oom(zSql);
26516 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26517 sqlite3_free(zSql);
26518 if( rx!=SQLITE_OK ){
26519 oputf("Error: %s\n", sqlite3_errmsg(p->db));
26520 sqlite3_finalize(pStmt);
26525 sqlite3_step(pStmt);
26526 sqlite3_finalize(pStmt);
26529 /* .parameter unset NAME
26530 ** Remove the NAME binding from the parameter binding table, if it
26533 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
26534 char *zSql = sqlite3_mprintf(
26535 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
26536 shell_check_oom(zSql);
26537 sqlite3_exec(p->db, zSql, 0, 0, 0);
26538 sqlite3_free(zSql);
26540 /* If no command name matches, show a syntax error */
26541 parameter_syntax_error:
26542 showHelp(p->out, "parameter");
26545 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
26547 for(i=1; i<nArg; i++){
26548 if( i>1 ) oputz(" ");
26554 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
26555 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
26558 p->flgProgress = 0;
26561 for(i=1; i<nArg; i++){
26562 const char *z = azArg[i];
26565 if( z[0]=='-' ) z++;
26566 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
26567 p->flgProgress |= SHELL_PROGRESS_QUIET;
26570 if( cli_strcmp(z,"reset")==0 ){
26571 p->flgProgress |= SHELL_PROGRESS_RESET;
26574 if( cli_strcmp(z,"once")==0 ){
26575 p->flgProgress |= SHELL_PROGRESS_ONCE;
26578 if( cli_strcmp(z,"limit")==0 ){
26580 eputz("Error: missing argument on --limit\n");
26582 goto meta_command_exit;
26584 p->mxProgress = (int)integerValue(azArg[++i]);
26588 eputf("Error: unknown option: \"%s\"\n", azArg[i]);
26590 goto meta_command_exit;
26592 nn = (int)integerValue(z);
26596 sqlite3_progress_handler(p->db, nn, progress_handler, p);
26598 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
26600 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
26602 shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
26605 shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
26609 #ifndef SQLITE_SHELL_FIDDLE
26610 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
26615 #ifndef SQLITE_SHELL_FIDDLE
26616 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
26617 FILE *inSaved = p->in;
26618 int savedLineno = p->lineno;
26619 failIfSafeMode(p, "cannot run .read in safe mode");
26621 eputz("Usage: .read FILE\n");
26623 goto meta_command_exit;
26625 if( azArg[1][0]=='|' ){
26626 #ifdef SQLITE_OMIT_POPEN
26627 eputz("Error: pipes are not supported in this OS\n");
26631 p->in = popen(azArg[1]+1, "r");
26633 eputf("Error: cannot open \"%s\"\n", azArg[1]);
26636 rc = process_input(p);
26640 }else if( (p->in = openChrSource(azArg[1]))==0 ){
26641 eputf("Error: cannot open \"%s\"\n", azArg[1]);
26644 rc = process_input(p);
26648 p->lineno = savedLineno;
26650 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26652 #ifndef SQLITE_SHELL_FIDDLE
26653 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
26654 const char *zSrcFile;
26657 sqlite3_backup *pBackup;
26660 failIfSafeMode(p, "cannot run .restore in safe mode");
26662 zSrcFile = azArg[1];
26664 }else if( nArg==3 ){
26665 zSrcFile = azArg[2];
26668 eputz("Usage: .restore ?DB? FILE\n");
26670 goto meta_command_exit;
26672 rc = sqlite3_open(zSrcFile, &pSrc);
26673 if( rc!=SQLITE_OK ){
26674 eputf("Error: cannot open \"%s\"\n", zSrcFile);
26679 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
26681 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26685 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
26686 || rc==SQLITE_BUSY ){
26687 if( rc==SQLITE_BUSY ){
26688 if( nTimeout++ >= 3 ) break;
26689 sqlite3_sleep(100);
26692 sqlite3_backup_finish(pBackup);
26693 if( rc==SQLITE_DONE ){
26695 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
26696 eputz("Error: source database is busy\n");
26699 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26704 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26706 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
26708 if( cli_strcmp(azArg[1], "vm")==0 ){
26709 p->scanstatsOn = 3;
26711 if( cli_strcmp(azArg[1], "est")==0 ){
26712 p->scanstatsOn = 2;
26714 p->scanstatsOn = (u8)booleanValue(azArg[1]);
26718 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
26720 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
26721 eputz("Warning: .scanstats not available in this build.\n");
26722 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
26723 if( p->scanstatsOn==3 ){
26724 eputz("Warning: \".scanstats vm\" not available in this build.\n");
26728 eputz("Usage: .scanstats on|off|est\n");
26733 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
26737 const char *zDiv = "(";
26738 const char *zName = 0;
26741 int bNoSystemTabs = 0;
26745 memcpy(&data, p, sizeof(data));
26746 data.showHeader = 0;
26747 data.cMode = data.mode = MODE_Semi;
26748 initText(&sSelect);
26749 for(ii=1; ii<nArg; ii++){
26750 if( optionMatch(azArg[ii],"indent") ){
26751 data.cMode = data.mode = MODE_Pretty;
26752 }else if( optionMatch(azArg[ii],"debug") ){
26754 }else if( optionMatch(azArg[ii],"nosys") ){
26756 }else if( azArg[ii][0]=='-' ){
26757 eputf("Unknown option: \"%s\"\n", azArg[ii]);
26759 goto meta_command_exit;
26760 }else if( zName==0 ){
26763 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
26765 goto meta_command_exit;
26769 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
26770 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
26771 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
26772 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
26774 char *new_argv[2], *new_colv[2];
26775 new_argv[0] = sqlite3_mprintf(
26776 "CREATE TABLE %s (\n"
26779 " tbl_name text,\n"
26780 " rootpage integer,\n"
26783 shell_check_oom(new_argv[0]);
26785 new_colv[0] = "sql";
26787 callback(&data, 1, new_argv, new_colv);
26788 sqlite3_free(new_argv[0]);
26792 sqlite3_stmt *pStmt = 0;
26793 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
26796 eputf("Error: %s\n", sqlite3_errmsg(p->db));
26797 sqlite3_finalize(pStmt);
26799 goto meta_command_exit;
26801 appendText(&sSelect, "SELECT sql FROM", 0);
26803 while( sqlite3_step(pStmt)==SQLITE_ROW ){
26804 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
26806 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
26807 appendText(&sSelect, zDiv, 0);
26808 zDiv = " UNION ALL ";
26809 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
26810 if( sqlite3_stricmp(zDb, "main")!=0 ){
26811 appendText(&sSelect, zDb, '\'');
26813 appendText(&sSelect, "NULL", 0);
26815 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
26816 appendText(&sSelect, zScNum, 0);
26817 appendText(&sSelect, " AS snum, ", 0);
26818 appendText(&sSelect, zDb, '\'');
26819 appendText(&sSelect, " AS sname FROM ", 0);
26820 appendText(&sSelect, zDb, quoteChar(zDb));
26821 appendText(&sSelect, ".sqlite_schema", 0);
26823 sqlite3_finalize(pStmt);
26824 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
26826 appendText(&sSelect,
26827 " UNION ALL SELECT shell_module_schema(name),"
26828 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
26832 appendText(&sSelect, ") WHERE ", 0);
26834 char *zQarg = sqlite3_mprintf("%Q", zName);
26836 shell_check_oom(zQarg);
26837 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
26838 strchr(zName, '[') != 0;
26839 if( strchr(zName, '.') ){
26840 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
26842 appendText(&sSelect, "lower(tbl_name)", 0);
26844 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
26845 appendText(&sSelect, zQarg, 0);
26847 appendText(&sSelect, " ESCAPE '\\' ", 0);
26849 appendText(&sSelect, " AND ", 0);
26850 sqlite3_free(zQarg);
26852 if( bNoSystemTabs ){
26853 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
26855 appendText(&sSelect, "sql IS NOT NULL"
26856 " ORDER BY snum, rowid", 0);
26858 oputf("SQL: %s;\n", sSelect.z);
26860 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
26862 freeText(&sSelect);
26865 eputf("Error: %s\n", zErrMsg);
26866 sqlite3_free(zErrMsg);
26868 }else if( rc != SQLITE_OK ){
26869 eputz("Error: querying schema information\n");
26876 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
26877 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
26879 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
26880 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
26883 #if defined(SQLITE_ENABLE_SESSION)
26884 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
26885 struct AuxDb *pAuxDb = p->pAuxDb;
26886 OpenSession *pSession = &pAuxDb->aSession[0];
26887 char **azCmd = &azArg[1];
26889 int nCmd = nArg - 1;
26891 if( nArg<=1 ) goto session_syntax_error;
26894 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
26895 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
26897 if( iSes<pAuxDb->nSession ){
26898 pSession = &pAuxDb->aSession[iSes];
26902 pSession = &pAuxDb->aSession[0];
26907 /* .session attach TABLE
26908 ** Invoke the sqlite3session_attach() interface to attach a particular
26909 ** table so that it is never filtered.
26911 if( cli_strcmp(azCmd[0],"attach")==0 ){
26912 if( nCmd!=2 ) goto session_syntax_error;
26913 if( pSession->p==0 ){
26915 eputz("ERROR: No sessions are open\n");
26917 rc = sqlite3session_attach(pSession->p, azCmd[1]);
26919 eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
26925 /* .session changeset FILE
26926 ** .session patchset FILE
26927 ** Write a changeset or patchset into a file. The file is overwritten.
26929 if( cli_strcmp(azCmd[0],"changeset")==0
26930 || cli_strcmp(azCmd[0],"patchset")==0
26933 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
26934 if( nCmd!=2 ) goto session_syntax_error;
26935 if( pSession->p==0 ) goto session_not_open;
26936 out = fopen(azCmd[1], "wb");
26938 eputf("ERROR: cannot open \"%s\" for writing\n",
26943 if( azCmd[0][0]=='c' ){
26944 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
26946 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
26949 sputf(stdout, "Error: error code %d\n", rc);
26953 && fwrite(pChng, szChng, 1, out)!=1 ){
26954 eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
26956 sqlite3_free(pChng);
26962 ** Close the identified session
26964 if( cli_strcmp(azCmd[0], "close")==0 ){
26965 if( nCmd!=1 ) goto session_syntax_error;
26966 if( pAuxDb->nSession ){
26967 session_close(pSession);
26968 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
26972 /* .session enable ?BOOLEAN?
26973 ** Query or set the enable flag
26975 if( cli_strcmp(azCmd[0], "enable")==0 ){
26977 if( nCmd>2 ) goto session_syntax_error;
26978 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26979 if( pAuxDb->nSession ){
26980 ii = sqlite3session_enable(pSession->p, ii);
26981 oputf("session %s enable flag = %d\n", pSession->zName, ii);
26985 /* .session filter GLOB ....
26986 ** Set a list of GLOB patterns of table names to be excluded.
26988 if( cli_strcmp(azCmd[0], "filter")==0 ){
26990 if( nCmd<2 ) goto session_syntax_error;
26991 if( pAuxDb->nSession ){
26992 for(ii=0; ii<pSession->nFilter; ii++){
26993 sqlite3_free(pSession->azFilter[ii]);
26995 sqlite3_free(pSession->azFilter);
26996 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
26997 pSession->azFilter = sqlite3_malloc( nByte );
26998 shell_check_oom( pSession->azFilter );
26999 for(ii=1; ii<nCmd; ii++){
27000 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
27001 shell_check_oom(x);
27003 pSession->nFilter = ii-1;
27007 /* .session indirect ?BOOLEAN?
27008 ** Query or set the indirect flag
27010 if( cli_strcmp(azCmd[0], "indirect")==0 ){
27012 if( nCmd>2 ) goto session_syntax_error;
27013 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
27014 if( pAuxDb->nSession ){
27015 ii = sqlite3session_indirect(pSession->p, ii);
27016 oputf("session %s indirect flag = %d\n", pSession->zName, ii);
27020 /* .session isempty
27021 ** Determine if the session is empty
27023 if( cli_strcmp(azCmd[0], "isempty")==0 ){
27025 if( nCmd!=1 ) goto session_syntax_error;
27026 if( pAuxDb->nSession ){
27027 ii = sqlite3session_isempty(pSession->p);
27028 oputf("session %s isempty flag = %d\n", pSession->zName, ii);
27033 ** List all currently open sessions
27035 if( cli_strcmp(azCmd[0],"list")==0 ){
27036 for(i=0; i<pAuxDb->nSession; i++){
27037 oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
27041 /* .session open DB NAME
27042 ** Open a new session called NAME on the attached database DB.
27043 ** DB is normally "main".
27045 if( cli_strcmp(azCmd[0],"open")==0 ){
27047 if( nCmd!=3 ) goto session_syntax_error;
27049 if( zName[0]==0 ) goto session_syntax_error;
27050 for(i=0; i<pAuxDb->nSession; i++){
27051 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
27052 eputf("Session \"%s\" already exists\n", zName);
27053 goto meta_command_exit;
27056 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
27057 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
27058 goto meta_command_exit;
27060 pSession = &pAuxDb->aSession[pAuxDb->nSession];
27061 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
27063 eputf("Cannot open session: error code=%d\n", rc);
27065 goto meta_command_exit;
27067 pSession->nFilter = 0;
27068 sqlite3session_table_filter(pSession->p, session_filter, pSession);
27069 pAuxDb->nSession++;
27070 pSession->zName = sqlite3_mprintf("%s", zName);
27071 shell_check_oom(pSession->zName);
27073 /* If no command name matches, show a syntax error */
27074 session_syntax_error:
27075 showHelp(p->out, "session");
27079 #ifdef SQLITE_DEBUG
27080 /* Undocumented commands for internal testing. Subject to change
27081 ** without notice. */
27082 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
27083 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
27085 for(i=1; i<nArg; i++){
27086 v = booleanValue(azArg[i]);
27087 oputf("%s: %d 0x%x\n", azArg[i], v, v);
27090 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
27091 int i; sqlite3_int64 v;
27092 for(i=1; i<nArg; i++){
27094 v = integerValue(azArg[i]);
27095 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
27102 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
27103 int bIsInit = 0; /* True to initialize the SELFTEST table */
27104 int bVerbose = 0; /* Verbose output */
27105 int bSelftestExists; /* True if SELFTEST already exists */
27106 int i, k; /* Loop counters */
27107 int nTest = 0; /* Number of tests runs */
27108 int nErr = 0; /* Number of errors seen */
27109 ShellText str; /* Answer for a query */
27110 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
27113 for(i=1; i<nArg; i++){
27114 const char *z = azArg[i];
27115 if( z[0]=='-' && z[1]=='-' ) z++;
27116 if( cli_strcmp(z,"-init")==0 ){
27119 if( cli_strcmp(z,"-v")==0 ){
27123 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27124 eputz("Should be one of: --init -v\n");
27126 goto meta_command_exit;
27129 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
27131 bSelftestExists = 0;
27133 bSelftestExists = 1;
27136 createSelftestTable(p);
27137 bSelftestExists = 1;
27140 appendText(&str, "x", 0);
27141 for(k=bSelftestExists; k>=0; k--){
27143 rc = sqlite3_prepare_v2(p->db,
27144 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
27147 rc = sqlite3_prepare_v2(p->db,
27148 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
27149 " (1,'run','PRAGMA integrity_check','ok')",
27153 eputz("Error querying the selftest table\n");
27155 sqlite3_finalize(pStmt);
27156 goto meta_command_exit;
27158 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
27159 int tno = sqlite3_column_int(pStmt, 0);
27160 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
27161 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
27162 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
27164 if( zOp==0 ) continue;
27165 if( zSql==0 ) continue;
27166 if( zAns==0 ) continue;
27169 sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
27171 if( cli_strcmp(zOp,"memo")==0 ){
27172 oputf("%s\n", zSql);
27174 if( cli_strcmp(zOp,"run")==0 ){
27178 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
27181 oputf("Result: %s\n", str.z);
27183 if( rc || zErrMsg ){
27186 oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
27187 sqlite3_free(zErrMsg);
27188 }else if( cli_strcmp(zAns,str.z)!=0 ){
27191 oputf("%d: Expected: [%s]\n", tno, zAns);
27192 oputf("%d: Got: [%s]\n", tno, str.z);
27196 eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
27200 } /* End loop over rows of content from SELFTEST */
27201 sqlite3_finalize(pStmt);
27202 } /* End loop over k */
27204 oputf("%d errors out of %d tests\n", nErr, nTest);
27207 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
27208 if( nArg<2 || nArg>3 ){
27209 eputz("Usage: .separator COL ?ROW?\n");
27213 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
27214 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
27217 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
27218 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
27222 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
27223 const char *zLike = 0; /* Which table to checksum. 0 means everything */
27224 int i; /* Loop counter */
27225 int bSchema = 0; /* Also hash the schema */
27226 int bSeparate = 0; /* Hash each table separately */
27227 int iSize = 224; /* Hash algorithm to use */
27228 int bDebug = 0; /* Only show the query that would have run */
27229 sqlite3_stmt *pStmt; /* For querying tables names */
27230 char *zSql; /* SQL to be run */
27231 char *zSep; /* Separator */
27232 ShellText sSql; /* Complete SQL for the query to run the hash */
27233 ShellText sQuery; /* Set of queries used to read all content */
27235 for(i=1; i<nArg; i++){
27236 const char *z = azArg[i];
27239 if( z[0]=='-' ) z++;
27240 if( cli_strcmp(z,"schema")==0 ){
27243 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
27244 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
27246 iSize = atoi(&z[5]);
27248 if( cli_strcmp(z,"debug")==0 ){
27252 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27253 showHelp(p->out, azArg[0]);
27255 goto meta_command_exit;
27258 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
27260 goto meta_command_exit;
27264 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
27268 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
27269 " WHERE type='table' AND coalesce(rootpage,0)>1"
27270 " UNION ALL SELECT 'sqlite_schema'"
27271 " ORDER BY 1 collate nocase";
27273 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
27274 " WHERE type='table' AND coalesce(rootpage,0)>1"
27275 " AND name NOT LIKE 'sqlite_%'"
27276 " ORDER BY 1 collate nocase";
27278 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
27281 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
27283 while( SQLITE_ROW==sqlite3_step(pStmt) ){
27284 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
27285 if( zTab==0 ) continue;
27286 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
27287 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
27288 appendText(&sQuery,"SELECT * FROM ", 0);
27289 appendText(&sQuery,zTab,'"');
27290 appendText(&sQuery," NOT INDEXED;", 0);
27291 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
27292 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
27293 " ORDER BY name;", 0);
27294 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
27295 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
27296 " ORDER BY name;", 0);
27297 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
27298 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
27299 " ORDER BY tbl,idx;", 0);
27300 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
27301 appendText(&sQuery, "SELECT * FROM ", 0);
27302 appendText(&sQuery, zTab, 0);
27303 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
27305 appendText(&sSql, zSep, 0);
27306 appendText(&sSql, sQuery.z, '\'');
27308 appendText(&sSql, ",", 0);
27309 appendText(&sSql, zTab, '\'');
27312 sqlite3_finalize(pStmt);
27314 zSql = sqlite3_mprintf(
27316 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
27317 " FROM [sha3sum$query]",
27320 zSql = sqlite3_mprintf(
27322 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
27323 " FROM [sha3sum$query]",
27326 shell_check_oom(zSql);
27330 oputf("%s\n", zSql);
27332 shell_exec(p, zSql, 0);
27334 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27337 char *zRevText = /* Query for reversible to-blob-to-text check */
27338 "SELECT lower(name) as tname FROM sqlite_schema\n"
27339 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
27340 "AND name NOT LIKE 'sqlite_%%'%s\n"
27341 "ORDER BY 1 collate nocase";
27342 zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
27343 zRevText = sqlite3_mprintf(
27344 /* lower-case query is first run, producing upper-case query. */
27345 "with tabcols as materialized(\n"
27346 "select tname, cname\n"
27348 " select printf('\"%%w\"',ss.tname) as tname,"
27349 " printf('\"%%w\"',ti.name) as cname\n"
27350 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
27351 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
27352 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
27353 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
27354 "FROM '||tname||' WHERE '\n"
27355 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
27356 "|| ' AND typeof('||cname||')=''text'' ',\n"
27357 "' OR ') as query, tname from tabcols group by tname)"
27359 shell_check_oom(zRevText);
27360 if( bDebug ) oputf("%s\n", zRevText);
27361 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
27362 if( lrc!=SQLITE_OK ){
27363 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
27364 ** user does cruel and unnatural things like ".limit expr_depth 0". */
27367 if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
27368 lrc = SQLITE_ROW==sqlite3_step(pStmt);
27370 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
27371 sqlite3_stmt *pCheckStmt;
27372 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
27373 if( bDebug ) oputf("%s\n", zGenQuery);
27374 if( lrc!=SQLITE_OK ){
27377 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
27378 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
27379 if( countIrreversible>0 ){
27380 int sz = (int)(countIrreversible + 0.5);
27381 eputf("Digest includes %d invalidly encoded text field%s.\n",
27382 sz, (sz>1)? "s": "");
27385 sqlite3_finalize(pCheckStmt);
27387 sqlite3_finalize(pStmt);
27390 if( rc ) eputz(".sha3sum failed.\n");
27391 sqlite3_free(zRevText);
27393 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
27394 sqlite3_free(zSql);
27397 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
27399 && (cli_strncmp(azArg[0], "shell", n)==0
27400 || cli_strncmp(azArg[0],"system",n)==0)
27404 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
27406 eputz("Usage: .system COMMAND\n");
27408 goto meta_command_exit;
27410 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
27411 for(i=2; i<nArg && zCmd!=0; i++){
27412 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
27416 x = zCmd!=0 ? system(zCmd) : 1;
27417 consoleRenewSetup();
27418 sqlite3_free(zCmd);
27419 if( x ) eputf("System command returns %d\n", x);
27421 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
27423 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
27424 static const char *azBool[] = { "off", "on", "trigger", "full"};
27428 eputz("Usage: .show\n");
27430 goto meta_command_exit;
27432 oputf("%12.12s: %s\n","echo",
27433 azBool[ShellHasFlag(p, SHFLG_Echo)]);
27434 oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
27435 oputf("%12.12s: %s\n","explain",
27436 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
27437 oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
27438 if( p->mode==MODE_Column
27439 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
27441 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
27442 modeDescr[p->mode], p->cmOpts.iWrap,
27443 p->cmOpts.bWordWrap ? "on" : "off",
27444 p->cmOpts.bQuote ? "" : "no");
27446 oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
27448 oputf("%12.12s: ", "nullvalue");
27449 output_c_string(p->nullValue);
27451 oputf("%12.12s: %s\n","output",
27452 strlen30(p->outfile) ? p->outfile : "stdout");
27453 oputf("%12.12s: ", "colseparator");
27454 output_c_string(p->colSeparator);
27456 oputf("%12.12s: ", "rowseparator");
27457 output_c_string(p->rowSeparator);
27459 switch( p->statsOn ){
27460 case 0: zOut = "off"; break;
27461 default: zOut = "on"; break;
27462 case 2: zOut = "stmt"; break;
27463 case 3: zOut = "vmstep"; break;
27465 oputf("%12.12s: %s\n","stats", zOut);
27466 oputf("%12.12s: ", "width");
27467 for (i=0;i<p->nWidth;i++) {
27468 oputf("%d ", p->colWidth[i]);
27471 oputf("%12.12s: %s\n", "filename",
27472 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
27475 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
27477 if( cli_strcmp(azArg[1],"stmt")==0 ){
27479 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
27482 p->statsOn = (u8)booleanValue(azArg[1]);
27484 }else if( nArg==1 ){
27485 display_stats(p->db, p, 0);
27487 eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
27492 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
27493 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
27494 || cli_strncmp(azArg[0], "indexes", n)==0) )
27496 sqlite3_stmt *pStmt;
27503 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
27505 sqlite3_finalize(pStmt);
27506 return shellDatabaseError(p->db);
27509 if( nArg>2 && c=='i' ){
27510 /* It is an historical accident that the .indexes command shows an error
27511 ** when called with the wrong number of arguments whereas the .tables
27512 ** command does not. */
27513 eputz("Usage: .indexes ?LIKE-PATTERN?\n");
27515 sqlite3_finalize(pStmt);
27516 goto meta_command_exit;
27518 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
27519 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
27520 if( zDbName==0 ) continue;
27521 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
27522 if( sqlite3_stricmp(zDbName, "main")==0 ){
27523 appendText(&s, "SELECT name FROM ", 0);
27525 appendText(&s, "SELECT ", 0);
27526 appendText(&s, zDbName, '\'');
27527 appendText(&s, "||'.'||name FROM ", 0);
27529 appendText(&s, zDbName, '"');
27530 appendText(&s, ".sqlite_schema ", 0);
27532 appendText(&s," WHERE type IN ('table','view')"
27533 " AND name NOT LIKE 'sqlite_%'"
27534 " AND name LIKE ?1", 0);
27536 appendText(&s," WHERE type='index'"
27537 " AND tbl_name LIKE ?1", 0);
27540 rc = sqlite3_finalize(pStmt);
27541 if( rc==SQLITE_OK ){
27542 appendText(&s, " ORDER BY 1", 0);
27543 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
27546 if( rc ) return shellDatabaseError(p->db);
27548 /* Run the SQL statement prepared by the above block. Store the results
27549 ** as an array of nul-terminated strings in azResult[]. */
27553 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
27555 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
27557 while( sqlite3_step(pStmt)==SQLITE_ROW ){
27558 if( nRow>=nAlloc ){
27560 int n2 = nAlloc*2 + 10;
27561 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
27562 shell_check_oom(azNew);
27566 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
27567 shell_check_oom(azResult[nRow]);
27570 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
27571 rc = shellDatabaseError(p->db);
27574 /* Pretty-print the contents of array azResult[] to the output */
27575 if( rc==0 && nRow>0 ){
27576 int len, maxlen = 0;
27578 int nPrintCol, nPrintRow;
27579 for(i=0; i<nRow; i++){
27580 len = strlen30(azResult[i]);
27581 if( len>maxlen ) maxlen = len;
27583 nPrintCol = 80/(maxlen+2);
27584 if( nPrintCol<1 ) nPrintCol = 1;
27585 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
27586 for(i=0; i<nPrintRow; i++){
27587 for(j=i; j<nRow; j+=nPrintRow){
27588 char *zSp = j<nPrintRow ? "" : " ";
27589 oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
27595 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
27596 sqlite3_free(azResult);
27599 #ifndef SQLITE_SHELL_FIDDLE
27600 /* Begin redirecting output to the file "testcase-out.txt" */
27601 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
27603 p->out = output_file_open("testcase-out.txt", 0);
27605 eputz("Error: cannot open 'testcase-out.txt'\n");
27608 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
27610 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
27613 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
27615 #ifndef SQLITE_UNTESTABLE
27616 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
27617 static const struct {
27618 const char *zCtrlName; /* Name of a test-control option */
27619 int ctrlCode; /* Integer code for that option */
27620 int unSafe; /* Not valid unless --unsafe-testing */
27621 const char *zUsage; /* Usage notes */
27623 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
27624 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
27625 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
27626 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
27627 {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" },
27628 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" },
27629 /*{"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/
27630 {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN" },
27631 {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
27632 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" },
27633 {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" },
27634 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" },
27635 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" },
27636 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" },
27638 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" },
27640 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " },
27641 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" },
27642 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
27643 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
27644 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
27645 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
27646 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
27647 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
27651 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
27654 const char *zCmd = 0;
27657 zCmd = nArg>=2 ? azArg[1] : "help";
27659 /* The argument can optionally begin with "-" or "--" */
27660 if( zCmd[0]=='-' && zCmd[1] ){
27662 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
27665 /* --help lists all test-controls */
27666 if( cli_strcmp(zCmd,"help")==0 ){
27667 oputz("Available test-controls:\n");
27668 for(i=0; i<ArraySize(aCtrl); i++){
27669 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
27670 oputf(" .testctrl %s %s\n",
27671 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
27674 goto meta_command_exit;
27677 /* convert testctrl text option to value. allow any unique prefix
27678 ** of the option name, or a numerical value. */
27679 n2 = strlen30(zCmd);
27680 for(i=0; i<ArraySize(aCtrl); i++){
27681 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
27682 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
27684 testctrl = aCtrl[i].ctrlCode;
27687 eputf("Error: ambiguous test-control: \"%s\"\n"
27688 "Use \".testctrl --help\" for help\n", zCmd);
27690 goto meta_command_exit;
27695 eputf("Error: unknown test-control: %s\n"
27696 "Use \".testctrl --help\" for help\n", zCmd);
27700 /* sqlite3_test_control(int, db, int) */
27701 case SQLITE_TESTCTRL_OPTIMIZATIONS:
27702 case SQLITE_TESTCTRL_FK_NO_ACTION:
27704 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
27705 rc2 = sqlite3_test_control(testctrl, p->db, opt);
27710 /* sqlite3_test_control(int) */
27711 case SQLITE_TESTCTRL_PRNG_SAVE:
27712 case SQLITE_TESTCTRL_PRNG_RESTORE:
27713 case SQLITE_TESTCTRL_BYTEORDER:
27715 rc2 = sqlite3_test_control(testctrl);
27716 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
27720 /* sqlite3_test_control(int, uint) */
27721 case SQLITE_TESTCTRL_PENDING_BYTE:
27723 unsigned int opt = (unsigned int)integerValue(azArg[2]);
27724 rc2 = sqlite3_test_control(testctrl, opt);
27729 /* sqlite3_test_control(int, int, sqlite3*) */
27730 case SQLITE_TESTCTRL_PRNG_SEED:
27731 if( nArg==3 || nArg==4 ){
27732 int ii = (int)integerValue(azArg[2]);
27734 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
27735 sqlite3_randomness(sizeof(ii),&ii);
27736 sputf(stdout, "-- random seed: %d\n", ii);
27742 /* Make sure the schema has been loaded */
27743 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
27745 rc2 = sqlite3_test_control(testctrl, ii, db);
27750 /* sqlite3_test_control(int, int) */
27751 case SQLITE_TESTCTRL_ASSERT:
27752 case SQLITE_TESTCTRL_ALWAYS:
27754 int opt = booleanValue(azArg[2]);
27755 rc2 = sqlite3_test_control(testctrl, opt);
27760 /* sqlite3_test_control(int, int) */
27761 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
27762 case SQLITE_TESTCTRL_NEVER_CORRUPT:
27764 int opt = booleanValue(azArg[2]);
27765 rc2 = sqlite3_test_control(testctrl, opt);
27770 /* sqlite3_test_control(int, int) */
27771 case SQLITE_TESTCTRL_USELONGDOUBLE: {
27774 if( cli_strcmp(azArg[2],"default")==0 ){
27777 opt = booleanValue(azArg[2]);
27780 rc2 = sqlite3_test_control(testctrl, opt);
27785 /* sqlite3_test_control(sqlite3*) */
27786 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
27787 rc2 = sqlite3_test_control(testctrl, p->db);
27791 case SQLITE_TESTCTRL_IMPOSTER:
27793 rc2 = sqlite3_test_control(testctrl, p->db,
27795 integerValue(azArg[3]),
27796 integerValue(azArg[4]));
27801 case SQLITE_TESTCTRL_SEEK_COUNT: {
27803 rc2 = sqlite3_test_control(testctrl, p->db, &x);
27804 oputf("%llu\n", x);
27810 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
27812 sqlite3_test_control(testctrl, p->out);
27818 #ifdef SQLITE_DEBUG
27819 case SQLITE_TESTCTRL_TUNE: {
27821 int id = (int)integerValue(azArg[2]);
27822 int val = (int)integerValue(azArg[3]);
27823 sqlite3_test_control(testctrl, id, &val);
27825 }else if( nArg==3 ){
27826 int id = (int)integerValue(azArg[2]);
27827 sqlite3_test_control(testctrl, -id, &rc2);
27829 }else if( nArg==2 ){
27833 rc2 = sqlite3_test_control(testctrl, -id, &val);
27834 if( rc2!=SQLITE_OK ) break;
27835 if( id>1 ) oputz(" ");
27836 oputf("%d: %d", id, val);
27839 if( id>1 ) oputz("\n");
27845 case SQLITE_TESTCTRL_SORTER_MMAP:
27847 int opt = (unsigned int)integerValue(azArg[2]);
27848 rc2 = sqlite3_test_control(testctrl, p->db, opt);
27852 case SQLITE_TESTCTRL_JSON_SELFCHECK:
27857 rc2 = booleanValue(azArg[2]);
27860 sqlite3_test_control(testctrl, &rc2);
27864 if( isOk==0 && iCtrl>=0 ){
27865 oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
27867 }else if( isOk==1 ){
27868 oputf("%d\n", rc2);
27869 }else if( isOk==2 ){
27870 oputf("0x%08x\n", rc2);
27873 #endif /* !defined(SQLITE_UNTESTABLE) */
27875 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
27877 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
27880 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
27882 enableTimer = booleanValue(azArg[1]);
27883 if( enableTimer && !HAS_TIMER ){
27884 eputz("Error: timer not available on this system.\n");
27888 eputz("Usage: .timer on|off\n");
27893 #ifndef SQLITE_OMIT_TRACE
27894 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
27898 for(jj=1; jj<nArg; jj++){
27899 const char *z = azArg[jj];
27901 if( optionMatch(z, "expanded") ){
27902 p->eTraceType = SHELL_TRACE_EXPANDED;
27904 #ifdef SQLITE_ENABLE_NORMALIZE
27905 else if( optionMatch(z, "normalized") ){
27906 p->eTraceType = SHELL_TRACE_NORMALIZED;
27909 else if( optionMatch(z, "plain") ){
27910 p->eTraceType = SHELL_TRACE_PLAIN;
27912 else if( optionMatch(z, "profile") ){
27913 mType |= SQLITE_TRACE_PROFILE;
27915 else if( optionMatch(z, "row") ){
27916 mType |= SQLITE_TRACE_ROW;
27918 else if( optionMatch(z, "stmt") ){
27919 mType |= SQLITE_TRACE_STMT;
27921 else if( optionMatch(z, "close") ){
27922 mType |= SQLITE_TRACE_CLOSE;
27925 eputf("Unknown option \"%s\" on \".trace\"\n", z);
27927 goto meta_command_exit;
27930 output_file_close(p->traceOut);
27931 p->traceOut = output_file_open(z, 0);
27934 if( p->traceOut==0 ){
27935 sqlite3_trace_v2(p->db, 0, 0, 0);
27937 if( mType==0 ) mType = SQLITE_TRACE_STMT;
27938 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
27941 #endif /* !defined(SQLITE_OMIT_TRACE) */
27943 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27944 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
27949 eputz("Usage: .unmodule [--allexcept] NAME ...\n");
27951 goto meta_command_exit;
27955 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
27956 lenOpt = (int)strlen(zOpt);
27957 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
27958 assert( azArg[nArg]==0 );
27959 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
27961 for(ii=1; ii<nArg; ii++){
27962 sqlite3_create_module(p->db, azArg[ii], 0, 0);
27968 #if SQLITE_USER_AUTHENTICATION
27969 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
27971 eputz("Usage: .user SUBCOMMAND ...\n");
27973 goto meta_command_exit;
27976 if( cli_strcmp(azArg[1],"login")==0 ){
27978 eputz("Usage: .user login USER PASSWORD\n");
27980 goto meta_command_exit;
27982 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
27983 strlen30(azArg[3]));
27985 eputf("Authentication failed for user %s\n", azArg[2]);
27988 }else if( cli_strcmp(azArg[1],"add")==0 ){
27990 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
27992 goto meta_command_exit;
27994 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27995 booleanValue(azArg[4]));
27997 eputf("User-Add failed: %d\n", rc);
28000 }else if( cli_strcmp(azArg[1],"edit")==0 ){
28002 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
28004 goto meta_command_exit;
28006 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
28007 booleanValue(azArg[4]));
28009 eputf("User-Edit failed: %d\n", rc);
28012 }else if( cli_strcmp(azArg[1],"delete")==0 ){
28014 eputz("Usage: .user delete USER\n");
28016 goto meta_command_exit;
28018 rc = sqlite3_user_delete(p->db, azArg[2]);
28020 eputf("User-Delete failed: %d\n", rc);
28024 eputz("Usage: .user login|add|edit|delete ...\n");
28026 goto meta_command_exit;
28029 #endif /* SQLITE_USER_AUTHENTICATION */
28031 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
28032 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
28033 oputf("SQLite %s %s\n" /*extra-version-info*/,
28034 sqlite3_libversion(), sqlite3_sourceid());
28035 #if SQLITE_HAVE_ZLIB
28036 oputf("zlib version %s\n", zlibVersion());
28038 #define CTIMEOPT_VAL_(opt) #opt
28039 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
28040 #if defined(__clang__) && defined(__clang_major__)
28041 oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
28042 CTIMEOPT_VAL(__clang_minor__) "."
28043 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
28044 #elif defined(_MSC_VER)
28045 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
28046 #elif defined(__GNUC__) && defined(__VERSION__)
28047 oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
28051 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
28052 const char *zDbName = nArg==2 ? azArg[1] : "main";
28053 sqlite3_vfs *pVfs = 0;
28055 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
28057 oputf("vfs.zName = \"%s\"\n", pVfs->zName);
28058 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28059 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28060 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28065 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
28067 sqlite3_vfs *pCurrent = 0;
28069 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
28071 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
28072 oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
28073 pVfs==pCurrent ? " <--- CURRENT" : "");
28074 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
28075 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
28076 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28078 oputz("-----------------------------------\n");
28083 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
28084 const char *zDbName = nArg==2 ? azArg[1] : "main";
28085 char *zVfsName = 0;
28087 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
28089 oputf("%s\n", zVfsName);
28090 sqlite3_free(zVfsName);
28095 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
28096 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
28097 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
28100 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
28102 assert( nArg<=ArraySize(azArg) );
28103 p->nWidth = nArg-1;
28104 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
28105 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
28106 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
28107 for(j=1; j<nArg; j++){
28108 p->colWidth[j-1] = (int)integerValue(azArg[j]);
28113 eputf("Error: unknown command or invalid arguments: "
28114 " \"%s\". Enter \".help\" for help\n", azArg[0]);
28121 if( p->outCount==0 ) output_reset(p);
28123 p->bSafeMode = p->bSafeModePersist;
28127 /* Line scan result and intermediate states (supporting scan resumption)
28130 # define CHAR_BIT 8
28133 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
28134 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
28137 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
28138 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
28139 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
28140 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
28141 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
28144 ** Scan line for classification to guide shell's handling.
28145 ** The scan is resumable for subsequent lines when prior
28146 ** return values are passed as the 2nd argument.
28148 static QuickScanState quickscan(char *zLine, QuickScanState qss,
28149 SCAN_TRACKER_REFTYPE pst){
28151 char cWait = (char)qss; /* intentional narrowing loss */
28154 assert( cWait==0 );
28155 while( (cin = *zLine++)!=0 ){
28162 while((cin = *++zLine)!=0 )
28167 qss |= QSS_EndingSemi;
28173 CONTINUE_PROMPT_AWAITS(pst, "/*");
28174 qss = QSS_SETV(qss, cWait);
28180 deliberate_fall_through;
28181 case '`': case '\'': case '"':
28183 qss = QSS_HasDark | cWait;
28184 CONTINUE_PROMPT_AWAITC(pst, cin);
28187 CONTINUE_PAREN_INCR(pst, 1);
28190 CONTINUE_PAREN_INCR(pst, -1);
28195 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
28199 while( (cin = *zLine++)!=0 ){
28203 if( *zLine != '/' )
28207 CONTINUE_PROMPT_AWAITC(pst, 0);
28208 qss = QSS_SETV(qss, 0);
28210 case '`': case '\'': case '"':
28212 /* Swallow doubled end-delimiter.*/
28216 deliberate_fall_through;
28219 CONTINUE_PROMPT_AWAITC(pst, 0);
28220 qss = QSS_SETV(qss, 0);
28222 default: assert(0);
28231 ** Return TRUE if the line typed in is an SQL command terminator other
28232 ** than a semi-colon. The SQL Server style "go" command is understood
28233 ** as is the Oracle "/".
28235 static int line_is_command_terminator(char *zLine){
28236 while( IsSpace(zLine[0]) ){ zLine++; };
28237 if( zLine[0]=='/' )
28238 zLine += 1; /* Oracle */
28239 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
28240 zLine += 2; /* SQL Server */
28243 return quickscan(zLine, QSS_Start, 0)==QSS_Start;
28247 ** The CLI needs a working sqlite3_complete() to work properly. So error
28248 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
28250 #ifdef SQLITE_OMIT_COMPLETE
28251 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
28255 ** Return true if zSql is a complete SQL statement. Return false if it
28256 ** ends in the middle of a string literal or C-style comment.
28258 static int line_is_complete(char *zSql, int nSql){
28260 if( zSql==0 ) return 1;
28263 rc = sqlite3_complete(zSql);
28269 ** This function is called after processing each line of SQL in the
28270 ** runOneSqlLine() function. Its purpose is to detect scenarios where
28271 ** defensive mode should be automatically turned off. Specifically, when
28273 ** 1. The first line of input is "PRAGMA foreign_keys=OFF;",
28274 ** 2. The second line of input is "BEGIN TRANSACTION;",
28275 ** 3. The database is empty, and
28276 ** 4. The shell is not running in --safe mode.
28278 ** The implementation uses the ShellState.eRestoreState to maintain state:
28280 ** 0: Have not seen any SQL.
28281 ** 1: Have seen "PRAGMA foreign_keys=OFF;".
28282 ** 2-6: Currently running .dump transaction. If the "2" bit is set,
28283 ** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
28284 ** 7: Nothing left to do. This function becomes a no-op.
28286 static int doAutoDetectRestore(ShellState *p, const char *zSql){
28287 int rc = SQLITE_OK;
28289 if( p->eRestoreState<7 ){
28290 switch( p->eRestoreState ){
28292 const char *zExpect = "PRAGMA foreign_keys=OFF;";
28293 assert( strlen(zExpect)==24 );
28294 if( p->bSafeMode==0 && memcmp(zSql, zExpect, 25)==0 ){
28295 p->eRestoreState = 1;
28297 p->eRestoreState = 7;
28304 const char *zExpect = "BEGIN TRANSACTION;";
28305 assert( strlen(zExpect)==18 );
28306 if( memcmp(zSql, zExpect, 19)==0 ){
28307 /* Now check if the database is empty. */
28308 const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
28309 sqlite3_stmt *pStmt = 0;
28312 shellPrepare(p->db, &rc, zQuery, &pStmt);
28313 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
28316 shellFinalize(&rc, pStmt);
28318 if( bIsDump && rc==SQLITE_OK ){
28321 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
28322 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
28323 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
28324 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
28325 p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
28327 p->eRestoreState = 7;
28333 if( sqlite3_get_autocommit(p->db) ){
28334 if( (p->eRestoreState & 2) ){
28335 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
28337 if( (p->eRestoreState & 4) ){
28338 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
28340 p->eRestoreState = 7;
28351 ** Run a single line of SQL. Return the number of errors.
28353 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
28358 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
28359 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
28361 rc = shell_exec(p, zSql, &zErrMsg);
28363 if( rc || zErrMsg ){
28365 const char *zErrorTail;
28366 const char *zErrorType;
28368 zErrorType = "Error";
28369 zErrorTail = sqlite3_errmsg(p->db);
28370 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
28371 zErrorType = "Parse error";
28372 zErrorTail = &zErrMsg[12];
28373 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
28374 zErrorType = "Runtime error";
28375 zErrorTail = &zErrMsg[10];
28377 zErrorType = "Error";
28378 zErrorTail = zErrMsg;
28380 if( in!=0 || !stdin_is_interactive ){
28381 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
28382 "%s near line %d:", zErrorType, startline);
28384 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
28386 eputf("%s %s\n", zPrefix, zErrorTail);
28387 sqlite3_free(zErrMsg);
28390 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
28391 char zLineBuf[2000];
28392 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
28393 "changes: %lld total_changes: %lld",
28394 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
28395 oputf("%s\n", zLineBuf);
28398 if( doAutoDetectRestore(p, zSql) ) return 1;
28402 static void echo_group_input(ShellState *p, const char *zDo){
28403 if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
28406 #ifdef SQLITE_SHELL_FIDDLE
28408 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
28409 ** impl because we need the global shellState and cannot access it from that
28410 ** function without moving lots of code around (creating a larger/messier diff).
28412 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
28413 /* Parse the next line from shellState.wasm.zInput. */
28414 const char *zBegin = shellState.wasm.zPos;
28415 const char *z = zBegin;
28419 UNUSED_PARAMETER(in);
28420 UNUSED_PARAMETER(isContinuation);
28424 while(*z && isspace(*z)) ++z;
28426 for(; *z && '\n'!=*z; ++nZ, ++z){}
28427 if(nZ>0 && '\r'==zBegin[nZ-1]){
28430 shellState.wasm.zPos = z;
28431 zLine = realloc(zPrior, nZ+1);
28432 shell_check_oom(zLine);
28433 memcpy(zLine, zBegin, nZ);
28437 #endif /* SQLITE_SHELL_FIDDLE */
28440 ** Read input from *in and process it. If *in==0 then input
28441 ** is interactive - the user is typing it it. Otherwise, input
28442 ** is coming from a file or device. A prompt is issued and history
28443 ** is saved only if input is interactive. An interrupt signal will
28444 ** cause this routine to exit immediately, unless input is interactive.
28446 ** Return the number of errors.
28448 static int process_input(ShellState *p){
28449 char *zLine = 0; /* A single input line */
28450 char *zSql = 0; /* Accumulated SQL text */
28451 i64 nLine; /* Length of current line */
28452 i64 nSql = 0; /* Bytes of zSql[] used */
28453 i64 nAlloc = 0; /* Allocated zSql[] space */
28454 int rc; /* Error code */
28455 int errCnt = 0; /* Number of errors seen */
28456 i64 startline = 0; /* Line number for start of current input */
28457 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
28459 if( p->inputNesting==MAX_INPUT_NESTING ){
28460 /* This will be more informative in a later version. */
28461 eputf("Input nesting limit (%d) reached at line %d."
28462 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
28467 CONTINUE_PROMPT_RESET;
28468 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
28470 zLine = one_input_line(p->in, zLine, nSql>0);
28473 if( p->in==0 && stdin_is_interactive ) oputz("\n");
28476 if( seenInterrupt ){
28477 if( p->in!=0 ) break;
28481 if( QSS_INPLAIN(qss)
28482 && line_is_command_terminator(zLine)
28483 && line_is_complete(zSql, nSql) ){
28484 memcpy(zLine,";",2);
28486 qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
28487 if( QSS_PLAINWHITE(qss) && nSql==0 ){
28488 /* Just swallow single-line whitespace */
28489 echo_group_input(p, zLine);
28493 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
28494 CONTINUE_PROMPT_RESET;
28495 echo_group_input(p, zLine);
28496 if( zLine[0]=='.' ){
28497 rc = do_meta_command(zLine, p);
28498 if( rc==2 ){ /* exit requested */
28507 /* No single-line dispositions remain; accumulate line(s). */
28508 nLine = strlen(zLine);
28509 if( nSql+nLine+2>=nAlloc ){
28510 /* Grow buffer by half-again increments when big. */
28511 nAlloc = nSql+(nSql>>1)+nLine+100;
28512 zSql = realloc(zSql, nAlloc);
28513 shell_check_oom(zSql);
28517 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
28518 assert( nAlloc>0 && zSql!=0 );
28519 memcpy(zSql, zLine+i, nLine+1-i);
28520 startline = p->lineno;
28523 zSql[nSql++] = '\n';
28524 memcpy(zSql+nSql, zLine, nLine+1);
28527 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
28528 echo_group_input(p, zSql);
28529 errCnt += runOneSqlLine(p, zSql, p->in, startline);
28530 CONTINUE_PROMPT_RESET;
28538 p->bSafeMode = p->bSafeModePersist;
28540 }else if( nSql && QSS_PLAINWHITE(qss) ){
28541 echo_group_input(p, zSql);
28547 /* This may be incomplete. Let the SQL parser deal with that. */
28548 echo_group_input(p, zSql);
28549 errCnt += runOneSqlLine(p, zSql, p->in, startline);
28550 CONTINUE_PROMPT_RESET;
28559 ** Return a pathname which is the user's home directory. A
28560 ** 0 return indicates an error of some kind.
28562 static char *find_home_dir(int clearFlag){
28563 static char *home_dir = NULL;
28569 if( home_dir ) return home_dir;
28571 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
28572 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
28574 struct passwd *pwent;
28575 uid_t uid = getuid();
28576 if( (pwent=getpwuid(uid)) != NULL) {
28577 home_dir = pwent->pw_dir;
28582 #if defined(_WIN32_WCE)
28583 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
28588 #if defined(_WIN32) || defined(WIN32)
28590 home_dir = getenv("USERPROFILE");
28595 home_dir = getenv("HOME");
28598 #if defined(_WIN32) || defined(WIN32)
28600 char *zDrive, *zPath;
28602 zDrive = getenv("HOMEDRIVE");
28603 zPath = getenv("HOMEPATH");
28604 if( zDrive && zPath ){
28605 n = strlen30(zDrive) + strlen30(zPath) + 1;
28606 home_dir = malloc( n );
28607 if( home_dir==0 ) return 0;
28608 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
28615 #endif /* !_WIN32_WCE */
28618 i64 n = strlen(home_dir) + 1;
28619 char *z = malloc( n );
28620 if( z ) memcpy(z, home_dir, n);
28628 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
28629 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
28630 ** the path to it, else return 0. The result is cached for
28631 ** subsequent calls.
28633 static const char *find_xdg_config(void){
28634 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
28635 || defined(__RTP__) || defined(_WRS_KERNEL)
28638 static int alreadyTried = 0;
28639 static char *zConfig = 0;
28640 const char *zXdgHome;
28642 if( alreadyTried!=0 ){
28646 zXdgHome = getenv("XDG_CONFIG_HOME");
28650 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
28651 shell_check_oom(zConfig);
28652 if( access(zConfig,0)!=0 ){
28653 sqlite3_free(zConfig);
28661 ** Read input from the file given by sqliterc_override. Or if that
28662 ** parameter is NULL, take input from the first of find_xdg_config()
28663 ** or ~/.sqliterc which is found.
28665 ** Returns the number of errors.
28667 static void process_sqliterc(
28668 ShellState *p, /* Configuration data */
28669 const char *sqliterc_override /* Name of config file. NULL to use default */
28671 char *home_dir = NULL;
28672 const char *sqliterc = sqliterc_override;
28674 FILE *inSaved = p->in;
28675 int savedLineno = p->lineno;
28677 if( sqliterc == NULL ){
28678 sqliterc = find_xdg_config();
28680 if( sqliterc == NULL ){
28681 home_dir = find_home_dir(0);
28683 eputz("-- warning: cannot find home directory;"
28684 " cannot read ~/.sqliterc\n");
28687 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
28688 shell_check_oom(zBuf);
28691 p->in = fopen(sqliterc,"rb");
28693 if( stdin_is_interactive ){
28694 eputf("-- Loading resources from %s\n", sqliterc);
28696 if( process_input(p) && bail_on_error ) exit(1);
28698 }else if( sqliterc_override!=0 ){
28699 eputf("cannot open: \"%s\"\n", sqliterc);
28700 if( bail_on_error ) exit(1);
28703 p->lineno = savedLineno;
28704 sqlite3_free(zBuf);
28708 ** Show available command line options
28710 static const char zOptions[] =
28711 " -- treat no subsequent arguments as options\n"
28712 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
28713 " -A ARGS... run \".archive ARGS\" and exit\n"
28715 " -append append the database to the end of the file\n"
28716 " -ascii set output mode to 'ascii'\n"
28717 " -bail stop after hitting an error\n"
28718 " -batch force batch I/O\n"
28719 " -box set output mode to 'box'\n"
28720 " -column set output mode to 'column'\n"
28721 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
28722 " -csv set output mode to 'csv'\n"
28723 #if !defined(SQLITE_OMIT_DESERIALIZE)
28724 " -deserialize open the database using sqlite3_deserialize()\n"
28726 " -echo print inputs before execution\n"
28727 " -init FILENAME read/process named file\n"
28728 " -[no]header turn headers on or off\n"
28729 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
28730 " -heap SIZE Size of heap for memsys3 or memsys5\n"
28732 " -help show this message\n"
28733 " -html set output mode to HTML\n"
28734 " -interactive force interactive I/O\n"
28735 " -json set output mode to 'json'\n"
28736 " -line set output mode to 'line'\n"
28737 " -list set output mode to 'list'\n"
28738 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
28739 " -markdown set output mode to 'markdown'\n"
28740 #if !defined(SQLITE_OMIT_DESERIALIZE)
28741 " -maxsize N maximum size for a --deserialize database\n"
28743 " -memtrace trace all memory allocations and deallocations\n"
28744 " -mmap N default mmap size set to N\n"
28745 #ifdef SQLITE_ENABLE_MULTIPLEX
28746 " -multiplex enable the multiplexor VFS\n"
28748 " -newline SEP set output row separator. Default: '\\n'\n"
28749 " -nofollow refuse to open symbolic links to database files\n"
28750 " -nonce STRING set the safe-mode escape nonce\n"
28751 " -nullvalue TEXT set text string for NULL values. Default ''\n"
28752 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
28753 " -pcachetrace trace all page cache operations\n"
28754 " -quote set output mode to 'quote'\n"
28755 " -readonly open the database read-only\n"
28756 " -safe enable safe-mode\n"
28757 " -separator SEP set output column separator. Default: '|'\n"
28758 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
28759 " -sorterref SIZE sorter references threshold size\n"
28761 " -stats print memory stats before each finalize\n"
28762 " -table set output mode to 'table'\n"
28763 " -tabs set output mode to 'tabs'\n"
28764 " -unsafe-testing allow unsafe commands and modes for testing\n"
28765 " -version show SQLite version\n"
28766 " -vfs NAME use NAME as the default VFS\n"
28767 #ifdef SQLITE_ENABLE_VFSTRACE
28768 " -vfstrace enable tracing of all VFS calls\n"
28770 #ifdef SQLITE_HAVE_ZLIB
28771 " -zip open the file as a ZIP Archive\n"
28774 static void usage(int showDetail){
28775 eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
28776 "FILENAME is the name of an SQLite database. A new database is created\n"
28777 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
28779 eputf("OPTIONS include:\n%s", zOptions);
28781 eputz("Use the -help option for additional information\n");
28787 ** Internal check: Verify that the SQLite is uninitialized. Print a
28788 ** error message if it is initialized.
28790 static void verify_uninitialized(void){
28791 if( sqlite3_config(-1)==SQLITE_MISUSE ){
28792 sputz(stdout, "WARNING: attempt to configure SQLite after"
28793 " initialization.\n");
28798 ** Initialize the state information in data
28800 static void main_init(ShellState *data) {
28801 memset(data, 0, sizeof(*data));
28802 data->normalMode = data->cMode = data->mode = MODE_List;
28803 data->autoExplain = 1;
28804 data->pAuxDb = &data->aAuxDb[0];
28805 memcpy(data->colSeparator,SEP_Column, 2);
28806 memcpy(data->rowSeparator,SEP_Row, 2);
28807 data->showHeader = 0;
28808 data->shellFlgs = SHFLG_Lookaside;
28809 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
28810 #if !defined(SQLITE_SHELL_FIDDLE)
28811 verify_uninitialized();
28813 sqlite3_config(SQLITE_CONFIG_URI, 1);
28814 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
28815 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
28816 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
28820 ** Output text to the console in a font that attracts extra attention.
28822 #if defined(_WIN32) || defined(WIN32)
28823 static void printBold(const char *zText){
28824 #if !SQLITE_OS_WINRT
28825 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
28826 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
28827 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
28828 SetConsoleTextAttribute(out,
28829 FOREGROUND_RED|FOREGROUND_INTENSITY
28832 sputz(stdout, zText);
28833 #if !SQLITE_OS_WINRT
28834 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
28838 static void printBold(const char *zText){
28839 sputf(stdout, "\033[1m%s\033[0m", zText);
28844 ** Get the argument to an --option. Throw an error and die if no argument
28847 static char *cmdline_option_value(int argc, char **argv, int i){
28849 eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
28855 static void sayAbnormalExit(void){
28856 if( seenInterrupt ) eputz("Program interrupted.\n");
28859 #ifndef SQLITE_SHELL_IS_UTF8
28860 # if (defined(_WIN32) || defined(WIN32)) \
28861 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
28862 # define SQLITE_SHELL_IS_UTF8 (0)
28864 # define SQLITE_SHELL_IS_UTF8 (1)
28868 #ifdef SQLITE_SHELL_FIDDLE
28869 # define main fiddle_main
28872 #if SQLITE_SHELL_IS_UTF8
28873 int SQLITE_CDECL main(int argc, char **argv){
28875 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
28878 #ifdef SQLITE_DEBUG
28879 sqlite3_int64 mem_main_enter = 0;
28882 #ifdef SQLITE_SHELL_FIDDLE
28883 # define data shellState
28886 StreamsAreConsole consStreams = SAC_NoConsole;
28888 const char *zInitFile = 0;
28891 int warnInmemoryDb = 0;
28894 int nOptsEnd = argc;
28896 const char *zVfs = 0; /* Value of -vfs command-line option */
28897 #if !SQLITE_SHELL_IS_UTF8
28898 char **argvToFree = 0;
28899 int argcToFree = 0;
28901 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
28903 #ifdef SQLITE_SHELL_FIDDLE
28904 stdin_is_interactive = 0;
28905 stdout_is_console = 1;
28906 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
28908 consStreams = consoleClassifySetup(stdin, stdout, stderr);
28909 stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
28910 stdout_is_console = (consStreams & SAC_OutConsole)!=0;
28911 atexit(consoleRestore);
28913 atexit(sayAbnormalExit);
28914 #ifdef SQLITE_DEBUG
28915 mem_main_enter = sqlite3_memory_used();
28917 #if !defined(_WIN32_WCE)
28918 if( getenv("SQLITE_DEBUG_BREAK") ){
28919 if( isatty(0) && isatty(2) ){
28920 eputf("attach debugger to process %d and press any key to continue.\n",
28924 #if defined(_WIN32) || defined(WIN32)
28925 #if SQLITE_OS_WINRT
28930 #elif defined(SIGTRAP)
28936 /* Register a valid signal handler early, before much else is done. */
28938 signal(SIGINT, interrupt_handler);
28939 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
28940 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
28941 eputz("No ^C handler.\n");
28945 #if USE_SYSTEM_SQLITE+0!=1
28946 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
28947 eputf("SQLite header and source version mismatch\n%s\n%s\n",
28948 sqlite3_sourceid(), SQLITE_SOURCE_ID);
28954 /* On Windows, we must translate command-line arguments into UTF-8.
28955 ** The SQLite memory allocator subsystem has to be enabled in order to
28956 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
28957 ** subsequent sqlite3_config() calls will work. So copy all results into
28958 ** memory that does not come from the SQLite memory allocator.
28960 #if !SQLITE_SHELL_IS_UTF8
28961 sqlite3_initialize();
28962 argvToFree = malloc(sizeof(argv[0])*argc*2);
28963 shell_check_oom(argvToFree);
28965 argv = argvToFree + argc;
28966 for(i=0; i<argc; i++){
28967 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
28969 shell_check_oom(z);
28971 argv[i] = malloc( n+1 );
28972 shell_check_oom(argv[i]);
28973 memcpy(argv[i], z, n+1);
28974 argvToFree[i] = argv[i];
28977 sqlite3_shutdown();
28980 assert( argc>=1 && argv && argv[0] );
28983 #ifdef SQLITE_SHELL_DBNAME_PROC
28985 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
28986 ** of a C-function that will provide the name of the database file. Use
28987 ** this compile-time option to embed this shell program in larger
28988 ** applications. */
28989 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
28990 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
28991 warnInmemoryDb = 0;
28995 /* Do an initial pass through the command-line argument to locate
28996 ** the name of the database file, the name of the initialization file,
28997 ** the size of the alternative malloc heap, options affecting commands
28998 ** or SQL run from the command line, and the first command to execute.
29000 #ifndef SQLITE_SHELL_FIDDLE
29001 verify_uninitialized();
29003 for(i=1; i<argc; i++){
29006 if( z[0]!='-' || i>nOptsEnd ){
29007 if( data.aAuxDb->zDbFilename==0 ){
29008 data.aAuxDb->zDbFilename = z;
29010 /* Excess arguments are interpreted as SQL (or dot-commands) and
29011 ** mean that nothing is read from stdin */
29014 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
29015 shell_check_oom(azCmd);
29020 if( z[1]=='-' ) z++;
29021 if( cli_strcmp(z, "-")==0 ){
29024 }else if( cli_strcmp(z,"-separator")==0
29025 || cli_strcmp(z,"-nullvalue")==0
29026 || cli_strcmp(z,"-newline")==0
29027 || cli_strcmp(z,"-cmd")==0
29029 (void)cmdline_option_value(argc, argv, ++i);
29030 }else if( cli_strcmp(z,"-init")==0 ){
29031 zInitFile = cmdline_option_value(argc, argv, ++i);
29032 }else if( cli_strcmp(z,"-interactive")==0 ){
29033 }else if( cli_strcmp(z,"-batch")==0 ){
29034 /* Need to check for batch mode here to so we can avoid printing
29035 ** informational messages (like from process_sqliterc) before
29036 ** we do the actual processing of arguments later in a second pass.
29038 stdin_is_interactive = 0;
29039 }else if( cli_strcmp(z,"-utf8")==0 ){
29040 }else if( cli_strcmp(z,"-no-utf8")==0 ){
29041 }else if( cli_strcmp(z,"-heap")==0 ){
29042 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
29044 sqlite3_int64 szHeap;
29046 zSize = cmdline_option_value(argc, argv, ++i);
29047 szHeap = integerValue(zSize);
29048 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
29049 verify_uninitialized();
29050 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
29052 (void)cmdline_option_value(argc, argv, ++i);
29054 }else if( cli_strcmp(z,"-pagecache")==0 ){
29055 sqlite3_int64 n, sz;
29056 sz = integerValue(cmdline_option_value(argc,argv,++i));
29057 if( sz>70000 ) sz = 70000;
29059 n = integerValue(cmdline_option_value(argc,argv,++i));
29060 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
29061 n = 0xffffffffffffLL/sz;
29063 verify_uninitialized();
29064 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
29065 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
29066 data.shellFlgs |= SHFLG_Pagecache;
29067 }else if( cli_strcmp(z,"-lookaside")==0 ){
29069 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
29071 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
29073 verify_uninitialized();
29074 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
29075 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
29076 }else if( cli_strcmp(z,"-threadsafe")==0 ){
29078 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
29079 verify_uninitialized();
29081 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
29082 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
29083 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
29085 #ifdef SQLITE_ENABLE_VFSTRACE
29086 }else if( cli_strcmp(z,"-vfstrace")==0 ){
29087 extern int vfstrace_register(
29088 const char *zTraceName,
29089 const char *zOldVfsName,
29090 int (*xOut)(const char*,void*),
29094 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
29096 #ifdef SQLITE_ENABLE_MULTIPLEX
29097 }else if( cli_strcmp(z,"-multiplex")==0 ){
29098 extern int sqlite3_multiplex_initialize(const char*,int);
29099 sqlite3_multiplex_initialize(0, 1);
29101 }else if( cli_strcmp(z,"-mmap")==0 ){
29102 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
29103 verify_uninitialized();
29104 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
29105 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
29106 }else if( cli_strcmp(z,"-sorterref")==0 ){
29107 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
29108 verify_uninitialized();
29109 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
29111 }else if( cli_strcmp(z,"-vfs")==0 ){
29112 zVfs = cmdline_option_value(argc, argv, ++i);
29113 #ifdef SQLITE_HAVE_ZLIB
29114 }else if( cli_strcmp(z,"-zip")==0 ){
29115 data.openMode = SHELL_OPEN_ZIPFILE;
29117 }else if( cli_strcmp(z,"-append")==0 ){
29118 data.openMode = SHELL_OPEN_APPENDVFS;
29119 #ifndef SQLITE_OMIT_DESERIALIZE
29120 }else if( cli_strcmp(z,"-deserialize")==0 ){
29121 data.openMode = SHELL_OPEN_DESERIALIZE;
29122 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29123 data.szMax = integerValue(argv[++i]);
29125 }else if( cli_strcmp(z,"-readonly")==0 ){
29126 data.openMode = SHELL_OPEN_READONLY;
29127 }else if( cli_strcmp(z,"-nofollow")==0 ){
29128 data.openFlags = SQLITE_OPEN_NOFOLLOW;
29129 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29130 }else if( cli_strncmp(z, "-A",2)==0 ){
29131 /* All remaining command-line arguments are passed to the ".archive"
29132 ** command, so ignore them */
29135 }else if( cli_strcmp(z, "-memtrace")==0 ){
29136 sqlite3MemTraceActivate(stderr);
29137 }else if( cli_strcmp(z, "-pcachetrace")==0 ){
29138 sqlite3PcacheTraceActivate(stderr);
29139 }else if( cli_strcmp(z,"-bail")==0 ){
29141 }else if( cli_strcmp(z,"-nonce")==0 ){
29143 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
29144 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29145 ShellSetFlag(&data,SHFLG_TestingMode);
29146 }else if( cli_strcmp(z,"-safe")==0 ){
29147 /* no-op - catch this on the second pass */
29150 #ifndef SQLITE_SHELL_FIDDLE
29151 verify_uninitialized();
29155 #ifdef SQLITE_SHELL_INIT_PROC
29157 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
29158 ** of a C-function that will perform initialization actions on SQLite that
29159 ** occur just before or after sqlite3_initialize(). Use this compile-time
29160 ** option to embed this shell program in larger applications. */
29161 extern void SQLITE_SHELL_INIT_PROC(void);
29162 SQLITE_SHELL_INIT_PROC();
29165 /* All the sqlite3_config() calls have now been made. So it is safe
29166 ** to call sqlite3_initialize() and process any command line -vfs option. */
29167 sqlite3_initialize();
29171 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
29173 sqlite3_vfs_register(pVfs, 1);
29175 eputf("no such VFS: \"%s\"\n", zVfs);
29180 if( data.pAuxDb->zDbFilename==0 ){
29181 #ifndef SQLITE_OMIT_MEMORYDB
29182 data.pAuxDb->zDbFilename = ":memory:";
29183 warnInmemoryDb = argc==1;
29185 eputf("%s: Error: no database filename specified\n", Argv0);
29190 #ifndef SQLITE_SHELL_FIDDLE
29191 sqlite3_appendvfs_init(0,0,0);
29194 /* Go ahead and open the database file if it already exists. If the
29195 ** file does not exist, delay opening it. This prevents empty database
29196 ** files from being created if a user mistypes the database name argument
29197 ** to the sqlite command-line tool.
29199 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
29203 /* Process the initialization file if there is one. If no -init option
29204 ** is given on the command line, look for a file named ~/.sqliterc and
29205 ** try to process it.
29207 process_sqliterc(&data,zInitFile);
29209 /* Make a second pass through the command-line argument and set
29210 ** options. This second pass is delayed until after the initialization
29211 ** file is processed so that the command-line arguments will override
29212 ** settings in the initialization file.
29214 for(i=1; i<argc; i++){
29216 if( z[0]!='-' || i>=nOptsEnd ) continue;
29217 if( z[1]=='-' ){ z++; }
29218 if( cli_strcmp(z,"-init")==0 ){
29220 }else if( cli_strcmp(z,"-html")==0 ){
29221 data.mode = MODE_Html;
29222 }else if( cli_strcmp(z,"-list")==0 ){
29223 data.mode = MODE_List;
29224 }else if( cli_strcmp(z,"-quote")==0 ){
29225 data.mode = MODE_Quote;
29226 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
29227 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
29228 }else if( cli_strcmp(z,"-line")==0 ){
29229 data.mode = MODE_Line;
29230 }else if( cli_strcmp(z,"-column")==0 ){
29231 data.mode = MODE_Column;
29232 }else if( cli_strcmp(z,"-json")==0 ){
29233 data.mode = MODE_Json;
29234 }else if( cli_strcmp(z,"-markdown")==0 ){
29235 data.mode = MODE_Markdown;
29236 }else if( cli_strcmp(z,"-table")==0 ){
29237 data.mode = MODE_Table;
29238 }else if( cli_strcmp(z,"-box")==0 ){
29239 data.mode = MODE_Box;
29240 }else if( cli_strcmp(z,"-csv")==0 ){
29241 data.mode = MODE_Csv;
29242 memcpy(data.colSeparator,",",2);
29243 #ifdef SQLITE_HAVE_ZLIB
29244 }else if( cli_strcmp(z,"-zip")==0 ){
29245 data.openMode = SHELL_OPEN_ZIPFILE;
29247 }else if( cli_strcmp(z,"-append")==0 ){
29248 data.openMode = SHELL_OPEN_APPENDVFS;
29249 #ifndef SQLITE_OMIT_DESERIALIZE
29250 }else if( cli_strcmp(z,"-deserialize")==0 ){
29251 data.openMode = SHELL_OPEN_DESERIALIZE;
29252 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29253 data.szMax = integerValue(argv[++i]);
29255 }else if( cli_strcmp(z,"-readonly")==0 ){
29256 data.openMode = SHELL_OPEN_READONLY;
29257 }else if( cli_strcmp(z,"-nofollow")==0 ){
29258 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
29259 }else if( cli_strcmp(z,"-ascii")==0 ){
29260 data.mode = MODE_Ascii;
29261 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
29262 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
29263 }else if( cli_strcmp(z,"-tabs")==0 ){
29264 data.mode = MODE_List;
29265 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab);
29266 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row);
29267 }else if( cli_strcmp(z,"-separator")==0 ){
29268 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
29269 "%s",cmdline_option_value(argc,argv,++i));
29270 }else if( cli_strcmp(z,"-newline")==0 ){
29271 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
29272 "%s",cmdline_option_value(argc,argv,++i));
29273 }else if( cli_strcmp(z,"-nullvalue")==0 ){
29274 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
29275 "%s",cmdline_option_value(argc,argv,++i));
29276 }else if( cli_strcmp(z,"-header")==0 ){
29277 data.showHeader = 1;
29278 ShellSetFlag(&data, SHFLG_HeaderSet);
29279 }else if( cli_strcmp(z,"-noheader")==0 ){
29280 data.showHeader = 0;
29281 ShellSetFlag(&data, SHFLG_HeaderSet);
29282 }else if( cli_strcmp(z,"-echo")==0 ){
29283 ShellSetFlag(&data, SHFLG_Echo);
29284 }else if( cli_strcmp(z,"-eqp")==0 ){
29285 data.autoEQP = AUTOEQP_on;
29286 }else if( cli_strcmp(z,"-eqpfull")==0 ){
29287 data.autoEQP = AUTOEQP_full;
29288 }else if( cli_strcmp(z,"-stats")==0 ){
29290 }else if( cli_strcmp(z,"-scanstats")==0 ){
29291 data.scanstatsOn = 1;
29292 }else if( cli_strcmp(z,"-backslash")==0 ){
29293 /* Undocumented command-line option: -backslash
29294 ** Causes C-style backslash escapes to be evaluated in SQL statements
29295 ** prior to sending the SQL into SQLite. Useful for injecting
29296 ** crazy bytes in the middle of SQL statements for testing and debugging.
29298 ShellSetFlag(&data, SHFLG_Backslash);
29299 }else if( cli_strcmp(z,"-bail")==0 ){
29300 /* No-op. The bail_on_error flag should already be set. */
29301 }else if( cli_strcmp(z,"-version")==0 ){
29302 sputf(stdout, "%s %s (%d-bit)\n",
29303 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
29305 }else if( cli_strcmp(z,"-interactive")==0 ){
29306 /* Need to check for interactive override here to so that it can
29307 ** affect console setup (for Windows only) and testing thereof.
29309 stdin_is_interactive = 1;
29310 }else if( cli_strcmp(z,"-batch")==0 ){
29311 /* already handled */
29312 }else if( cli_strcmp(z,"-utf8")==0 ){
29313 /* already handled */
29314 }else if( cli_strcmp(z,"-no-utf8")==0 ){
29315 /* already handled */
29316 }else if( cli_strcmp(z,"-heap")==0 ){
29318 }else if( cli_strcmp(z,"-pagecache")==0 ){
29320 }else if( cli_strcmp(z,"-lookaside")==0 ){
29322 }else if( cli_strcmp(z,"-threadsafe")==0 ){
29324 }else if( cli_strcmp(z,"-nonce")==0 ){
29326 }else if( cli_strcmp(z,"-mmap")==0 ){
29328 }else if( cli_strcmp(z,"-memtrace")==0 ){
29330 }else if( cli_strcmp(z,"-pcachetrace")==0 ){
29332 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
29333 }else if( cli_strcmp(z,"-sorterref")==0 ){
29336 }else if( cli_strcmp(z,"-vfs")==0 ){
29338 #ifdef SQLITE_ENABLE_VFSTRACE
29339 }else if( cli_strcmp(z,"-vfstrace")==0 ){
29342 #ifdef SQLITE_ENABLE_MULTIPLEX
29343 }else if( cli_strcmp(z,"-multiplex")==0 ){
29346 }else if( cli_strcmp(z,"-help")==0 ){
29348 }else if( cli_strcmp(z,"-cmd")==0 ){
29349 /* Run commands that follow -cmd first and separately from commands
29350 ** that simply appear on the command-line. This seems goofy. It would
29351 ** be better if all commands ran in the order that they appear. But
29352 ** we retain the goofy behavior for historical compatibility. */
29353 if( i==argc-1 ) break;
29354 z = cmdline_option_value(argc,argv,++i);
29356 rc = do_meta_command(z, &data);
29357 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
29360 rc = shell_exec(&data, z, &zErrMsg);
29362 eputf("Error: %s\n", zErrMsg);
29363 if( bail_on_error ) return rc!=0 ? rc : 1;
29365 eputf("Error: unable to process SQL \"%s\"\n", z);
29366 if( bail_on_error ) return rc;
29369 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29370 }else if( cli_strncmp(z, "-A", 2)==0 ){
29372 eputf("Error: cannot mix regular SQL or dot-commands"
29373 " with \"%s\"\n", z);
29376 open_db(&data, OPEN_DB_ZIPFILE);
29379 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
29381 arDotCommand(&data, 1, argv+i, argc-i);
29386 }else if( cli_strcmp(z,"-safe")==0 ){
29387 data.bSafeMode = data.bSafeModePersist = 1;
29388 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29389 /* Acted upon in first pass. */
29391 eputf("%s: Error: unknown option: %s\n", Argv0, z);
29392 eputz("Use -help for a list of options.\n");
29395 data.cMode = data.mode;
29399 /* Run all arguments that do not begin with '-' as if they were separate
29400 ** command-line inputs, except for the argToSkip argument which contains
29401 ** the database filename.
29403 for(i=0; i<nCmd; i++){
29404 if( azCmd[i][0]=='.' ){
29405 rc = do_meta_command(azCmd[i], &data);
29408 return rc==2 ? 0 : rc;
29412 echo_group_input(&data, azCmd[i]);
29413 rc = shell_exec(&data, azCmd[i], &zErrMsg);
29414 if( zErrMsg || rc ){
29416 eputf("Error: %s\n", zErrMsg);
29418 eputf("Error: unable to process SQL: %s\n", azCmd[i]);
29420 sqlite3_free(zErrMsg);
29422 return rc!=0 ? rc : 1;
29427 /* Run commands received from standard input
29429 if( stdin_is_interactive ){
29433 #if CIO_WIN_WC_XLATE
29434 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
29436 # define SHELL_CIO_CHAR_SET ""
29438 sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
29439 "Enter \".help\" for usage hints.\n",
29440 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
29441 if( warnInmemoryDb ){
29442 sputz(stdout, "Connected to a ");
29443 printBold("transient in-memory database");
29444 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
29445 " persistent database.\n");
29447 zHistory = getenv("SQLITE_HISTORY");
29449 zHistory = strdup(zHistory);
29450 }else if( (zHome = find_home_dir(0))!=0 ){
29451 nHistory = strlen30(zHome) + 20;
29452 if( (zHistory = malloc(nHistory))!=0 ){
29453 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
29456 if( zHistory ){ shell_read_history(zHistory); }
29457 #if HAVE_READLINE || HAVE_EDITLINE
29458 rl_attempted_completion_function = readline_completion;
29459 #elif HAVE_LINENOISE
29460 linenoiseSetCompletionCallback(linenoise_completion);
29463 rc = process_input(&data);
29465 shell_stifle_history(2000);
29466 shell_write_history(zHistory);
29471 rc = process_input(&data);
29474 #ifndef SQLITE_SHELL_FIDDLE
29475 /* In WASM mode we have to leave the db state in place so that
29476 ** client code can "push" SQL into it after this call returns. */
29478 set_table_name(&data, 0);
29480 session_close_all(&data, -1);
29483 for(i=0; i<ArraySize(data.aAuxDb); i++){
29484 sqlite3_free(data.aAuxDb[i].zFreeOnClose);
29485 if( data.aAuxDb[i].db ){
29486 session_close_all(&data, i);
29487 close_db(data.aAuxDb[i].db);
29491 output_reset(&data);
29492 data.doXdgOpen = 0;
29493 clearTempFile(&data);
29494 #if !SQLITE_SHELL_IS_UTF8
29495 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
29498 free(data.colWidth);
29500 /* Clear the global data structure so that valgrind will detect memory
29502 memset(&data, 0, sizeof(data));
29503 #ifdef SQLITE_DEBUG
29504 if( sqlite3_memory_used()>mem_main_enter ){
29505 eputf("Memory leaked: %u bytes\n",
29506 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
29509 #endif /* !SQLITE_SHELL_FIDDLE */
29514 #ifdef SQLITE_SHELL_FIDDLE
29515 /* Only for emcc experimentation purposes. */
29516 int fiddle_experiment(int a,int b){
29521 ** Returns a pointer to the current DB handle.
29523 sqlite3 * fiddle_db_handle(){
29528 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
29529 ** "main" is assumed. Returns 0 if no db with the given name is
29532 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
29533 sqlite3_vfs * pVfs = 0;
29535 sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
29536 SQLITE_FCNTL_VFS_POINTER, &pVfs);
29541 /* Only for emcc experimentation purposes. */
29542 sqlite3 * fiddle_db_arg(sqlite3 *arg){
29543 printf("fiddle_db_arg(%p)\n", (const void*)arg);
29548 ** Intended to be called via a SharedWorker() while a separate
29549 ** SharedWorker() (which manages the wasm module) is performing work
29550 ** which should be interrupted. Unfortunately, SharedWorker is not
29551 ** portable enough to make real use of.
29553 void fiddle_interrupt(void){
29554 if( globalDb ) sqlite3_interrupt(globalDb);
29558 ** Returns the filename of the given db name, assuming "main" if
29559 ** zDbName is NULL. Returns NULL if globalDb is not opened.
29561 const char * fiddle_db_filename(const char * zDbName){
29563 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
29568 ** Completely wipes out the contents of the currently-opened database
29569 ** but leaves its storage intact for reuse.
29571 void fiddle_reset_db(void){
29573 int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
29574 if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
29575 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
29580 ** Uses the current database's VFS xRead to stream the db file's
29581 ** contents out to the given callback. The callback gets a single
29582 ** chunk of size n (its 2nd argument) on each call and must return 0
29583 ** on success, non-0 on error. This function returns 0 on success,
29584 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
29585 ** code from the callback. Note that this is not thread-friendly: it
29586 ** expects that it will be the only thread reading the db file and
29587 ** takes no measures to ensure that is the case.
29589 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
29590 sqlite3_int64 nSize = 0;
29591 sqlite3_int64 nPos = 0;
29592 sqlite3_file * pFile = 0;
29593 unsigned char buf[1024 * 8];
29594 int nBuf = (int)sizeof(buf);
29595 int rc = shellState.db
29596 ? sqlite3_file_control(shellState.db, "main",
29597 SQLITE_FCNTL_FILE_POINTER, &pFile)
29599 if( rc ) return rc;
29600 rc = pFile->pMethods->xFileSize(pFile, &nSize);
29601 if( rc ) return rc;
29603 /* DB size is not an even multiple of the buffer size. Reduce
29604 ** buffer size so that we do not unduly inflate the db size when
29606 if(0 == nSize % 4096) nBuf = 4096;
29607 else if(0 == nSize % 2048) nBuf = 2048;
29608 else if(0 == nSize % 1024) nBuf = 1024;
29611 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
29612 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
29613 if(SQLITE_IOERR_SHORT_READ == rc){
29614 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
29616 if( 0==rc ) rc = xCallback(buf, nBuf);
29622 ** Trivial exportable function for emscripten. It processes zSql as if
29623 ** it were input to the sqlite3 shell and redirects all output to the
29624 ** wasm binding. fiddle_main() must have been called before this
29625 ** is called, or results are undefined.
29627 void fiddle_exec(const char * zSql){
29629 if('.'==*zSql) puts(zSql);
29630 shellState.wasm.zInput = zSql;
29631 shellState.wasm.zPos = zSql;
29632 process_input(&shellState);
29633 shellState.wasm.zInput = shellState.wasm.zPos = 0;
29636 #endif /* SQLITE_SHELL_FIDDLE */