1/* 2** 2001 September 15 3** 4** The author disclaims copyright to this source code. In place of 5** a legal notice, here is a blessing: 6** 7** May you do good and not evil. 8** May you find forgiveness for yourself and forgive others. 9** May you share freely, never taking more than you give. 10** 11************************************************************************* 12** This file contains code to implement the "sqlite" command line 13** utility for accessing SQLite databases. 14*/ 15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) 16/* This needs to come before any includes for MSVC compiler */ 17#define _CRT_SECURE_NO_WARNINGS 18#endif 19 20/* 21** Optionally #include a user-defined header, whereby compilation options 22** may be set prior to where they take effect, but after platform setup. 23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include 24** file. Note that this macro has a like effect on sqlite3.c compilation. 25*/ 26# define SHELL_STRINGIFY_(f) #f 27# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f) 28#ifdef SQLITE_CUSTOM_INCLUDE 29# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE) 30#endif 31 32/* 33** Determine if we are dealing with WinRT, which provides only a subset of 34** the full Win32 API. 35*/ 36#if !defined(SQLITE_OS_WINRT) 37# define SQLITE_OS_WINRT 0 38#endif 39 40/* 41** If SQLITE_SHELL_FIDDLE is defined then the shell is modified 42** somewhat for use as a WASM module in a web browser. This flag 43** should only be used when building the "fiddle" web application, as 44** the browser-mode build has much different user input requirements 45** and this build mode rewires the user input subsystem to account for 46** that. 47*/ 48 49/* 50** Warning pragmas copied from msvc.h in the core. 51*/ 52#if defined(_MSC_VER) 53#pragma warning(disable : 4054) 54#pragma warning(disable : 4055) 55#pragma warning(disable : 4100) 56#pragma warning(disable : 4127) 57#pragma warning(disable : 4130) 58#pragma warning(disable : 4152) 59#pragma warning(disable : 4189) 60#pragma warning(disable : 4206) 61#pragma warning(disable : 4210) 62#pragma warning(disable : 4232) 63#pragma warning(disable : 4244) 64#pragma warning(disable : 4305) 65#pragma warning(disable : 4306) 66#pragma warning(disable : 4702) 67#pragma warning(disable : 4706) 68#endif /* defined(_MSC_VER) */ 69 70/* 71** No support for loadable extensions in VxWorks. 72*/ 73#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION 74# define SQLITE_OMIT_LOAD_EXTENSION 1 75#endif 76 77/* 78** Enable large-file support for fopen() and friends on unix. 79*/ 80#ifndef SQLITE_DISABLE_LFS 81# define _LARGE_FILE 1 82# ifndef _FILE_OFFSET_BITS 83# define _FILE_OFFSET_BITS 64 84# endif 85# define _LARGEFILE_SOURCE 1 86#endif 87 88#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE) 89/* 90** emcc requires _POSIX_SOURCE (or one of several similar defines) 91** to expose strdup(). 92*/ 93# define _POSIX_SOURCE 94#endif 95 96#include <stdlib.h> 97#include <string.h> 98#include <stdio.h> 99#include <assert.h> 100#include "sqlite3.h" 101typedef sqlite3_int64 i64; 102typedef sqlite3_uint64 u64; 103typedef unsigned char u8; 104#if SQLITE_USER_AUTHENTICATION 105# include "sqlite3userauth.h" 106#endif 107#include <ctype.h> 108#include <stdarg.h> 109 110#if !defined(_WIN32) && !defined(WIN32) 111# include <signal.h> 112# if !defined(__RTP__) && !defined(_WRS_KERNEL) 113# include <pwd.h> 114# endif 115#endif 116#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) 117# include <unistd.h> 118# include <dirent.h> 119# define GETPID getpid 120# if defined(__MINGW32__) 121# define DIRENT dirent 122# ifndef S_ISLNK 123# define S_ISLNK(mode) (0) 124# endif 125# endif 126#else 127# define GETPID (int)GetCurrentProcessId 128#endif 129#include <sys/types.h> 130#include <sys/stat.h> 131 132#if HAVE_READLINE 133# include <readline/readline.h> 134# include <readline/history.h> 135#endif 136 137#if HAVE_EDITLINE 138# include <editline/readline.h> 139#endif 140 141#if HAVE_EDITLINE || HAVE_READLINE 142 143# define shell_add_history(X) add_history(X) 144# define shell_read_history(X) read_history(X) 145# define shell_write_history(X) write_history(X) 146# define shell_stifle_history(X) stifle_history(X) 147# define shell_readline(X) readline(X) 148 149#elif HAVE_LINENOISE 150 151# include "linenoise.h" 152# define shell_add_history(X) linenoiseHistoryAdd(X) 153# define shell_read_history(X) linenoiseHistoryLoad(X) 154# define shell_write_history(X) linenoiseHistorySave(X) 155# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) 156# define shell_readline(X) linenoise(X) 157 158#else 159 160# define shell_read_history(X) 161# define shell_write_history(X) 162# define shell_stifle_history(X) 163 164# define SHELL_USE_LOCAL_GETLINE 1 165#endif 166 167 168#if defined(_WIN32) || defined(WIN32) 169# if SQLITE_OS_WINRT 170# define SQLITE_OMIT_POPEN 1 171# else 172# include <io.h> 173# include <fcntl.h> 174# define isatty(h) _isatty(h) 175# ifndef access 176# define access(f,m) _access((f),(m)) 177# endif 178# ifndef unlink 179# define unlink _unlink 180# endif 181# ifndef strdup 182# define strdup _strdup 183# endif 184# undef popen 185# define popen _popen 186# undef pclose 187# define pclose _pclose 188# endif 189#else 190 /* Make sure isatty() has a prototype. */ 191 extern int isatty(int); 192 193# if !defined(__RTP__) && !defined(_WRS_KERNEL) 194 /* popen and pclose are not C89 functions and so are 195 ** sometimes omitted from the <stdio.h> header */ 196 extern FILE *popen(const char*,const char*); 197 extern int pclose(FILE*); 198# else 199# define SQLITE_OMIT_POPEN 1 200# endif 201#endif 202 203#if defined(_WIN32_WCE) 204/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() 205 * thus we always assume that we have a console. That can be 206 * overridden with the -batch command line option. 207 */ 208#define isatty(x) 1 209#endif 210 211/* ctype macros that work with signed characters */ 212#define IsSpace(X) isspace((unsigned char)X) 213#define IsDigit(X) isdigit((unsigned char)X) 214#define ToLower(X) (char)tolower((unsigned char)X) 215 216#if defined(_WIN32) || defined(WIN32) 217#if SQLITE_OS_WINRT 218#include <intrin.h> 219#endif 220#include <windows.h> 221 222/* string conversion routines only needed on Win32 */ 223extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); 224extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); 225extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); 226extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); 227#endif 228 229/* On Windows, we normally run with output mode of TEXT so that \n characters 230** are automatically translated into \r\n. However, this behavior needs 231** to be disabled in some cases (ex: when generating CSV output and when 232** rendering quoted strings that contain \n characters). The following 233** routines take care of that. 234*/ 235#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT 236static void setBinaryMode(FILE *file, int isOutput){ 237 if( isOutput ) fflush(file); 238 _setmode(_fileno(file), _O_BINARY); 239} 240static void setTextMode(FILE *file, int isOutput){ 241 if( isOutput ) fflush(file); 242 _setmode(_fileno(file), _O_TEXT); 243} 244#else 245# define setBinaryMode(X,Y) 246# define setTextMode(X,Y) 247#endif 248 249/* True if the timer is enabled */ 250static int enableTimer = 0; 251 252/* Return the current wall-clock time */ 253static sqlite3_int64 timeOfDay(void){ 254 static sqlite3_vfs *clockVfs = 0; 255 sqlite3_int64 t; 256 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 257 if( clockVfs==0 ) return 0; /* Never actually happens */ 258 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 259 clockVfs->xCurrentTimeInt64(clockVfs, &t); 260 }else{ 261 double r; 262 clockVfs->xCurrentTime(clockVfs, &r); 263 t = (sqlite3_int64)(r*86400000.0); 264 } 265 return t; 266} 267 268#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) 269#include <sys/time.h> 270#include <sys/resource.h> 271 272/* VxWorks does not support getrusage() as far as we can determine */ 273#if defined(_WRS_KERNEL) || defined(__RTP__) 274struct rusage { 275 struct timeval ru_utime; /* user CPU time used */ 276 struct timeval ru_stime; /* system CPU time used */ 277}; 278#define getrusage(A,B) memset(B,0,sizeof(*B)) 279#endif 280 281/* Saved resource information for the beginning of an operation */ 282static struct rusage sBegin; /* CPU time at start */ 283static sqlite3_int64 iBegin; /* Wall-clock time at start */ 284 285/* 286** Begin timing an operation 287*/ 288static void beginTimer(void){ 289 if( enableTimer ){ 290 getrusage(RUSAGE_SELF, &sBegin); 291 iBegin = timeOfDay(); 292 } 293} 294 295/* Return the difference of two time_structs in seconds */ 296static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ 297 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 298 (double)(pEnd->tv_sec - pStart->tv_sec); 299} 300 301/* 302** Print the timing results. 303*/ 304static void endTimer(void){ 305 if( enableTimer ){ 306 sqlite3_int64 iEnd = timeOfDay(); 307 struct rusage sEnd; 308 getrusage(RUSAGE_SELF, &sEnd); 309 printf("Run Time: real %.3f user %f sys %f\n", 310 (iEnd - iBegin)*0.001, 311 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), 312 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); 313 } 314} 315 316#define BEGIN_TIMER beginTimer() 317#define END_TIMER endTimer() 318#define HAS_TIMER 1 319 320#elif (defined(_WIN32) || defined(WIN32)) 321 322/* Saved resource information for the beginning of an operation */ 323static HANDLE hProcess; 324static FILETIME ftKernelBegin; 325static FILETIME ftUserBegin; 326static sqlite3_int64 ftWallBegin; 327typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, 328 LPFILETIME, LPFILETIME); 329static GETPROCTIMES getProcessTimesAddr = NULL; 330 331/* 332** Check to see if we have timer support. Return 1 if necessary 333** support found (or found previously). 334*/ 335static int hasTimer(void){ 336 if( getProcessTimesAddr ){ 337 return 1; 338 } else { 339#if !SQLITE_OS_WINRT 340 /* GetProcessTimes() isn't supported in WIN95 and some other Windows 341 ** versions. See if the version we are running on has it, and if it 342 ** does, save off a pointer to it and the current process handle. 343 */ 344 hProcess = GetCurrentProcess(); 345 if( hProcess ){ 346 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); 347 if( NULL != hinstLib ){ 348 getProcessTimesAddr = 349 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); 350 if( NULL != getProcessTimesAddr ){ 351 return 1; 352 } 353 FreeLibrary(hinstLib); 354 } 355 } 356#endif 357 } 358 return 0; 359} 360 361/* 362** Begin timing an operation 363*/ 364static void beginTimer(void){ 365 if( enableTimer && getProcessTimesAddr ){ 366 FILETIME ftCreation, ftExit; 367 getProcessTimesAddr(hProcess,&ftCreation,&ftExit, 368 &ftKernelBegin,&ftUserBegin); 369 ftWallBegin = timeOfDay(); 370 } 371} 372 373/* Return the difference of two FILETIME structs in seconds */ 374static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ 375 sqlite_int64 i64Start = *((sqlite_int64 *) pStart); 376 sqlite_int64 i64End = *((sqlite_int64 *) pEnd); 377 return (double) ((i64End - i64Start) / 10000000.0); 378} 379 380/* 381** Print the timing results. 382*/ 383static void endTimer(void){ 384 if( enableTimer && getProcessTimesAddr){ 385 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; 386 sqlite3_int64 ftWallEnd = timeOfDay(); 387 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); 388 printf("Run Time: real %.3f user %f sys %f\n", 389 (ftWallEnd - ftWallBegin)*0.001, 390 timeDiff(&ftUserBegin, &ftUserEnd), 391 timeDiff(&ftKernelBegin, &ftKernelEnd)); 392 } 393} 394 395#define BEGIN_TIMER beginTimer() 396#define END_TIMER endTimer() 397#define HAS_TIMER hasTimer() 398 399#else 400#define BEGIN_TIMER 401#define END_TIMER 402#define HAS_TIMER 0 403#endif 404 405/* 406** Used to prevent warnings about unused parameters 407*/ 408#define UNUSED_PARAMETER(x) (void)(x) 409 410/* 411** Number of elements in an array 412*/ 413#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) 414 415/* 416** If the following flag is set, then command execution stops 417** at an error if we are not interactive. 418*/ 419static int bail_on_error = 0; 420 421/* 422** Threat stdin as an interactive input if the following variable 423** is true. Otherwise, assume stdin is connected to a file or pipe. 424*/ 425static int stdin_is_interactive = 1; 426 427/* 428** On Windows systems we have to know if standard output is a console 429** in order to translate UTF-8 into MBCS. The following variable is 430** true if translation is required. 431*/ 432static int stdout_is_console = 1; 433 434/* 435** The following is the open SQLite database. We make a pointer 436** to this database a static variable so that it can be accessed 437** by the SIGINT handler to interrupt database processing. 438*/ 439static sqlite3 *globalDb = 0; 440 441/* 442** True if an interrupt (Control-C) has been received. 443*/ 444static volatile int seenInterrupt = 0; 445 446/* 447** This is the name of our program. It is set in main(), used 448** in a number of other places, mostly for error messages. 449*/ 450static char *Argv0; 451 452/* 453** Prompt strings. Initialized in main. Settable with 454** .prompt main continue 455*/ 456static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ 457static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ 458 459/* 460** Render output like fprintf(). Except, if the output is going to the 461** console and if this is running on a Windows machine, translate the 462** output from UTF-8 into MBCS. 463*/ 464#if defined(_WIN32) || defined(WIN32) 465void utf8_printf(FILE *out, const char *zFormat, ...){ 466 va_list ap; 467 va_start(ap, zFormat); 468 if( stdout_is_console && (out==stdout || out==stderr) ){ 469 char *z1 = sqlite3_vmprintf(zFormat, ap); 470 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); 471 sqlite3_free(z1); 472 fputs(z2, out); 473 sqlite3_free(z2); 474 }else{ 475 vfprintf(out, zFormat, ap); 476 } 477 va_end(ap); 478} 479#elif !defined(utf8_printf) 480# define utf8_printf fprintf 481#endif 482 483/* 484** Render output like fprintf(). This should not be used on anything that 485** includes string formatting (e.g. "%s"). 486*/ 487#if !defined(raw_printf) 488# define raw_printf fprintf 489#endif 490 491/* Indicate out-of-memory and exit. */ 492static void shell_out_of_memory(void){ 493 raw_printf(stderr,"Error: out of memory\n"); 494 exit(1); 495} 496 497/* Check a pointer to see if it is NULL. If it is NULL, exit with an 498** out-of-memory error. 499*/ 500static void shell_check_oom(void *p){ 501 if( p==0 ) shell_out_of_memory(); 502} 503 504/* 505** Write I/O traces to the following stream. 506*/ 507#ifdef SQLITE_ENABLE_IOTRACE 508static FILE *iotrace = 0; 509#endif 510 511/* 512** This routine works like printf in that its first argument is a 513** format string and subsequent arguments are values to be substituted 514** in place of % fields. The result of formatting this string 515** is written to iotrace. 516*/ 517#ifdef SQLITE_ENABLE_IOTRACE 518static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ 519 va_list ap; 520 char *z; 521 if( iotrace==0 ) return; 522 va_start(ap, zFormat); 523 z = sqlite3_vmprintf(zFormat, ap); 524 va_end(ap); 525 utf8_printf(iotrace, "%s", z); 526 sqlite3_free(z); 527} 528#endif 529 530/* 531** Output string zUtf to stream pOut as w characters. If w is negative, 532** then right-justify the text. W is the width in UTF-8 characters, not 533** in bytes. This is different from the %*.*s specification in printf 534** since with %*.*s the width is measured in bytes, not characters. 535*/ 536static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ 537 int i; 538 int n; 539 int aw = w<0 ? -w : w; 540 for(i=n=0; zUtf[i]; i++){ 541 if( (zUtf[i]&0xc0)!=0x80 ){ 542 n++; 543 if( n==aw ){ 544 do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); 545 break; 546 } 547 } 548 } 549 if( n>=aw ){ 550 utf8_printf(pOut, "%.*s", i, zUtf); 551 }else if( w<0 ){ 552 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); 553 }else{ 554 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); 555 } 556} 557 558 559/* 560** Determines if a string is a number of not. 561*/ 562static int isNumber(const char *z, int *realnum){ 563 if( *z=='-' || *z=='+' ) z++; 564 if( !IsDigit(*z) ){ 565 return 0; 566 } 567 z++; 568 if( realnum ) *realnum = 0; 569 while( IsDigit(*z) ){ z++; } 570 if( *z=='.' ){ 571 z++; 572 if( !IsDigit(*z) ) return 0; 573 while( IsDigit(*z) ){ z++; } 574 if( realnum ) *realnum = 1; 575 } 576 if( *z=='e' || *z=='E' ){ 577 z++; 578 if( *z=='+' || *z=='-' ) z++; 579 if( !IsDigit(*z) ) return 0; 580 while( IsDigit(*z) ){ z++; } 581 if( realnum ) *realnum = 1; 582 } 583 return *z==0; 584} 585 586/* 587** Compute a string length that is limited to what can be stored in 588** lower 30 bits of a 32-bit signed integer. 589*/ 590static int strlen30(const char *z){ 591 const char *z2 = z; 592 while( *z2 ){ z2++; } 593 return 0x3fffffff & (int)(z2 - z); 594} 595 596/* 597** Return the length of a string in characters. Multibyte UTF8 characters 598** count as a single character. 599*/ 600static int strlenChar(const char *z){ 601 int n = 0; 602 while( *z ){ 603 if( (0xc0&*(z++))!=0x80 ) n++; 604 } 605 return n; 606} 607 608/* 609** Return open FILE * if zFile exists, can be opened for read 610** and is an ordinary file or a character stream source. 611** Otherwise return 0. 612*/ 613static FILE * openChrSource(const char *zFile){ 614#ifdef _WIN32 615 struct _stat x = {0}; 616# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) 617 /* On Windows, open first, then check the stream nature. This order 618 ** is necessary because _stat() and sibs, when checking a named pipe, 619 ** effectively break the pipe as its supplier sees it. */ 620 FILE *rv = fopen(zFile, "rb"); 621 if( rv==0 ) return 0; 622 if( _fstat(_fileno(rv), &x) != 0 623 || !STAT_CHR_SRC(x.st_mode)){ 624 fclose(rv); 625 rv = 0; 626 } 627 return rv; 628#else 629 struct stat x = {0}; 630 int rc = stat(zFile, &x); 631# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) 632 if( rc!=0 ) return 0; 633 if( STAT_CHR_SRC(x.st_mode) ){ 634 return fopen(zFile, "rb"); 635 }else{ 636 return 0; 637 } 638#endif 639#undef STAT_CHR_SRC 640} 641 642/* 643** This routine reads a line of text from FILE in, stores 644** the text in memory obtained from malloc() and returns a pointer 645** to the text. NULL is returned at end of file, or if malloc() 646** fails. 647** 648** If zLine is not NULL then it is a malloced buffer returned from 649** a previous call to this routine that may be reused. 650*/ 651static char *local_getline(char *zLine, FILE *in){ 652 int nLine = zLine==0 ? 0 : 100; 653 int n = 0; 654 655 while( 1 ){ 656 if( n+100>nLine ){ 657 nLine = nLine*2 + 100; 658 zLine = realloc(zLine, nLine); 659 shell_check_oom(zLine); 660 } 661 if( fgets(&zLine[n], nLine - n, in)==0 ){ 662 if( n==0 ){ 663 free(zLine); 664 return 0; 665 } 666 zLine[n] = 0; 667 break; 668 } 669 while( zLine[n] ) n++; 670 if( n>0 && zLine[n-1]=='\n' ){ 671 n--; 672 if( n>0 && zLine[n-1]=='\r' ) n--; 673 zLine[n] = 0; 674 break; 675 } 676 } 677#if defined(_WIN32) || defined(WIN32) 678 /* For interactive input on Windows systems, translate the 679 ** multi-byte characterset characters into UTF-8. */ 680 if( stdin_is_interactive && in==stdin ){ 681 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); 682 if( zTrans ){ 683 i64 nTrans = strlen(zTrans)+1; 684 if( nTrans>nLine ){ 685 zLine = realloc(zLine, nTrans); 686 shell_check_oom(zLine); 687 } 688 memcpy(zLine, zTrans, nTrans); 689 sqlite3_free(zTrans); 690 } 691 } 692#endif /* defined(_WIN32) || defined(WIN32) */ 693 return zLine; 694} 695 696/* 697** Retrieve a single line of input text. 698** 699** If in==0 then read from standard input and prompt before each line. 700** If isContinuation is true, then a continuation prompt is appropriate. 701** If isContinuation is zero, then the main prompt should be used. 702** 703** If zPrior is not NULL then it is a buffer from a prior call to this 704** routine that can be reused. 705** 706** The result is stored in space obtained from malloc() and must either 707** be freed by the caller or else passed back into this routine via the 708** zPrior argument for reuse. 709*/ 710#ifndef SQLITE_SHELL_FIDDLE 711static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 712 char *zPrompt; 713 char *zResult; 714 if( in!=0 ){ 715 zResult = local_getline(zPrior, in); 716 }else{ 717 zPrompt = isContinuation ? continuePrompt : mainPrompt; 718#if SHELL_USE_LOCAL_GETLINE 719 printf("%s", zPrompt); 720 fflush(stdout); 721 zResult = local_getline(zPrior, stdin); 722#else 723 free(zPrior); 724 zResult = shell_readline(zPrompt); 725 if( zResult && *zResult ) shell_add_history(zResult); 726#endif 727 } 728 return zResult; 729} 730#endif /* !SQLITE_SHELL_FIDDLE */ 731 732/* 733** Return the value of a hexadecimal digit. Return -1 if the input 734** is not a hex digit. 735*/ 736static int hexDigitValue(char c){ 737 if( c>='0' && c<='9' ) return c - '0'; 738 if( c>='a' && c<='f' ) return c - 'a' + 10; 739 if( c>='A' && c<='F' ) return c - 'A' + 10; 740 return -1; 741} 742 743/* 744** Interpret zArg as an integer value, possibly with suffixes. 745*/ 746static sqlite3_int64 integerValue(const char *zArg){ 747 sqlite3_int64 v = 0; 748 static const struct { char *zSuffix; int iMult; } aMult[] = { 749 { "KiB", 1024 }, 750 { "MiB", 1024*1024 }, 751 { "GiB", 1024*1024*1024 }, 752 { "KB", 1000 }, 753 { "MB", 1000000 }, 754 { "GB", 1000000000 }, 755 { "K", 1000 }, 756 { "M", 1000000 }, 757 { "G", 1000000000 }, 758 }; 759 int i; 760 int isNeg = 0; 761 if( zArg[0]=='-' ){ 762 isNeg = 1; 763 zArg++; 764 }else if( zArg[0]=='+' ){ 765 zArg++; 766 } 767 if( zArg[0]=='0' && zArg[1]=='x' ){ 768 int x; 769 zArg += 2; 770 while( (x = hexDigitValue(zArg[0]))>=0 ){ 771 v = (v<<4) + x; 772 zArg++; 773 } 774 }else{ 775 while( IsDigit(zArg[0]) ){ 776 v = v*10 + zArg[0] - '0'; 777 zArg++; 778 } 779 } 780 for(i=0; i<ArraySize(aMult); i++){ 781 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 782 v *= aMult[i].iMult; 783 break; 784 } 785 } 786 return isNeg? -v : v; 787} 788 789/* 790** A variable length string to which one can append text. 791*/ 792typedef struct ShellText ShellText; 793struct ShellText { 794 char *z; 795 int n; 796 int nAlloc; 797}; 798 799/* 800** Initialize and destroy a ShellText object 801*/ 802static void initText(ShellText *p){ 803 memset(p, 0, sizeof(*p)); 804} 805static void freeText(ShellText *p){ 806 free(p->z); 807 initText(p); 808} 809 810/* zIn is either a pointer to a NULL-terminated string in memory obtained 811** from malloc(), or a NULL pointer. The string pointed to by zAppend is 812** added to zIn, and the result returned in memory obtained from malloc(). 813** zIn, if it was not NULL, is freed. 814** 815** If the third argument, quote, is not '\0', then it is used as a 816** quote character for zAppend. 817*/ 818static void appendText(ShellText *p, const char *zAppend, char quote){ 819 i64 len; 820 i64 i; 821 i64 nAppend = strlen30(zAppend); 822 823 len = nAppend+p->n+1; 824 if( quote ){ 825 len += 2; 826 for(i=0; i<nAppend; i++){ 827 if( zAppend[i]==quote ) len++; 828 } 829 } 830 831 if( p->z==0 || p->n+len>=p->nAlloc ){ 832 p->nAlloc = p->nAlloc*2 + len + 20; 833 p->z = realloc(p->z, p->nAlloc); 834 shell_check_oom(p->z); 835 } 836 837 if( quote ){ 838 char *zCsr = p->z+p->n; 839 *zCsr++ = quote; 840 for(i=0; i<nAppend; i++){ 841 *zCsr++ = zAppend[i]; 842 if( zAppend[i]==quote ) *zCsr++ = quote; 843 } 844 *zCsr++ = quote; 845 p->n = (int)(zCsr - p->z); 846 *zCsr = '\0'; 847 }else{ 848 memcpy(p->z+p->n, zAppend, nAppend); 849 p->n += nAppend; 850 p->z[p->n] = '\0'; 851 } 852} 853 854/* 855** Attempt to determine if identifier zName needs to be quoted, either 856** because it contains non-alphanumeric characters, or because it is an 857** SQLite keyword. Be conservative in this estimate: When in doubt assume 858** that quoting is required. 859** 860** Return '"' if quoting is required. Return 0 if no quoting is required. 861*/ 862static char quoteChar(const char *zName){ 863 int i; 864 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; 865 for(i=0; zName[i]; i++){ 866 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; 867 } 868 return sqlite3_keyword_check(zName, i) ? '"' : 0; 869} 870 871/* 872** Construct a fake object name and column list to describe the structure 873** of the view, virtual table, or table valued function zSchema.zName. 874*/ 875static char *shellFakeSchema( 876 sqlite3 *db, /* The database connection containing the vtab */ 877 const char *zSchema, /* Schema of the database holding the vtab */ 878 const char *zName /* The name of the virtual table */ 879){ 880 sqlite3_stmt *pStmt = 0; 881 char *zSql; 882 ShellText s; 883 char cQuote; 884 char *zDiv = "("; 885 int nRow = 0; 886 887 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", 888 zSchema ? zSchema : "main", zName); 889 shell_check_oom(zSql); 890 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 891 sqlite3_free(zSql); 892 initText(&s); 893 if( zSchema ){ 894 cQuote = quoteChar(zSchema); 895 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; 896 appendText(&s, zSchema, cQuote); 897 appendText(&s, ".", 0); 898 } 899 cQuote = quoteChar(zName); 900 appendText(&s, zName, cQuote); 901 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 902 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); 903 nRow++; 904 appendText(&s, zDiv, 0); 905 zDiv = ","; 906 if( zCol==0 ) zCol = ""; 907 cQuote = quoteChar(zCol); 908 appendText(&s, zCol, cQuote); 909 } 910 appendText(&s, ")", 0); 911 sqlite3_finalize(pStmt); 912 if( nRow==0 ){ 913 freeText(&s); 914 s.z = 0; 915 } 916 return s.z; 917} 918 919/* 920** SQL function: shell_module_schema(X) 921** 922** Return a fake schema for the table-valued function or eponymous virtual 923** table X. 924*/ 925static void shellModuleSchema( 926 sqlite3_context *pCtx, 927 int nVal, 928 sqlite3_value **apVal 929){ 930 const char *zName; 931 char *zFake; 932 UNUSED_PARAMETER(nVal); 933 zName = (const char*)sqlite3_value_text(apVal[0]); 934 zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0; 935 if( zFake ){ 936 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), 937 -1, sqlite3_free); 938 free(zFake); 939 } 940} 941 942/* 943** SQL function: shell_add_schema(S,X) 944** 945** Add the schema name X to the CREATE statement in S and return the result. 946** Examples: 947** 948** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); 949** 950** Also works on 951** 952** CREATE INDEX 953** CREATE UNIQUE INDEX 954** CREATE VIEW 955** CREATE TRIGGER 956** CREATE VIRTUAL TABLE 957** 958** This UDF is used by the .schema command to insert the schema name of 959** attached databases into the middle of the sqlite_schema.sql field. 960*/ 961static void shellAddSchemaName( 962 sqlite3_context *pCtx, 963 int nVal, 964 sqlite3_value **apVal 965){ 966 static const char *aPrefix[] = { 967 "TABLE", 968 "INDEX", 969 "UNIQUE INDEX", 970 "VIEW", 971 "TRIGGER", 972 "VIRTUAL TABLE" 973 }; 974 int i = 0; 975 const char *zIn = (const char*)sqlite3_value_text(apVal[0]); 976 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); 977 const char *zName = (const char*)sqlite3_value_text(apVal[2]); 978 sqlite3 *db = sqlite3_context_db_handle(pCtx); 979 UNUSED_PARAMETER(nVal); 980 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ 981 for(i=0; i<ArraySize(aPrefix); i++){ 982 int n = strlen30(aPrefix[i]); 983 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ 984 char *z = 0; 985 char *zFake = 0; 986 if( zSchema ){ 987 char cQuote = quoteChar(zSchema); 988 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ 989 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); 990 }else{ 991 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); 992 } 993 } 994 if( zName 995 && aPrefix[i][0]=='V' 996 && (zFake = shellFakeSchema(db, zSchema, zName))!=0 997 ){ 998 if( z==0 ){ 999 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); 1000 }else{ 1001 z = sqlite3_mprintf("%z\n/* %s */", z, zFake); 1002 } 1003 free(zFake); 1004 } 1005 if( z ){ 1006 sqlite3_result_text(pCtx, z, -1, sqlite3_free); 1007 return; 1008 } 1009 } 1010 } 1011 } 1012 sqlite3_result_value(pCtx, apVal[0]); 1013} 1014 1015/* 1016** The source code for several run-time loadable extensions is inserted 1017** below by the ../tool/mkshellc.tcl script. Before processing that included 1018** code, we need to override some macros to make the included program code 1019** work here in the middle of this regular program. 1020*/ 1021#define SQLITE_EXTENSION_INIT1 1022#define SQLITE_EXTENSION_INIT2(X) (void)(X) 1023 1024#if defined(_WIN32) && defined(_MSC_VER) 1025INCLUDE test_windirent.h 1026INCLUDE test_windirent.c 1027#define dirent DIRENT 1028#endif 1029INCLUDE ../ext/misc/memtrace.c 1030INCLUDE ../ext/misc/shathree.c 1031INCLUDE ../ext/misc/uint.c 1032INCLUDE ../ext/misc/decimal.c 1033INCLUDE ../ext/misc/ieee754.c 1034INCLUDE ../ext/misc/series.c 1035INCLUDE ../ext/misc/regexp.c 1036#ifndef SQLITE_SHELL_FIDDLE 1037INCLUDE ../ext/misc/fileio.c 1038INCLUDE ../ext/misc/completion.c 1039INCLUDE ../ext/misc/appendvfs.c 1040#endif 1041#ifdef SQLITE_HAVE_ZLIB 1042INCLUDE ../ext/misc/zipfile.c 1043INCLUDE ../ext/misc/sqlar.c 1044#endif 1045INCLUDE ../ext/expert/sqlite3expert.h 1046INCLUDE ../ext/expert/sqlite3expert.c 1047 1048#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 1049INCLUDE ../ext/recover/dbdata.c 1050INCLUDE ../ext/recover/sqlite3recover.h 1051INCLUDE ../ext/recover/sqlite3recover.c 1052#endif 1053 1054#if defined(SQLITE_ENABLE_SESSION) 1055/* 1056** State information for a single open session 1057*/ 1058typedef struct OpenSession OpenSession; 1059struct OpenSession { 1060 char *zName; /* Symbolic name for this session */ 1061 int nFilter; /* Number of xFilter rejection GLOB patterns */ 1062 char **azFilter; /* Array of xFilter rejection GLOB patterns */ 1063 sqlite3_session *p; /* The open session */ 1064}; 1065#endif 1066 1067typedef struct ExpertInfo ExpertInfo; 1068struct ExpertInfo { 1069 sqlite3expert *pExpert; 1070 int bVerbose; 1071}; 1072 1073/* A single line in the EQP output */ 1074typedef struct EQPGraphRow EQPGraphRow; 1075struct EQPGraphRow { 1076 int iEqpId; /* ID for this row */ 1077 int iParentId; /* ID of the parent row */ 1078 EQPGraphRow *pNext; /* Next row in sequence */ 1079 char zText[1]; /* Text to display for this row */ 1080}; 1081 1082/* All EQP output is collected into an instance of the following */ 1083typedef struct EQPGraph EQPGraph; 1084struct EQPGraph { 1085 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ 1086 EQPGraphRow *pLast; /* Last element of the pRow list */ 1087 char zPrefix[100]; /* Graph prefix */ 1088}; 1089 1090/* Parameters affecting columnar mode result display (defaulting together) */ 1091typedef struct ColModeOpts { 1092 int iWrap; /* In columnar modes, wrap lines reaching this limit */ 1093 u8 bQuote; /* Quote results for .mode box and table */ 1094 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */ 1095} ColModeOpts; 1096#define ColModeOpts_default { 60, 0, 0 } 1097#define ColModeOpts_default_qbox { 60, 1, 0 } 1098 1099/* 1100** State information about the database connection is contained in an 1101** instance of the following structure. 1102*/ 1103typedef struct ShellState ShellState; 1104struct ShellState { 1105 sqlite3 *db; /* The database */ 1106 u8 autoExplain; /* Automatically turn on .explain mode */ 1107 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ 1108 u8 autoEQPtest; /* autoEQP is in test mode */ 1109 u8 autoEQPtrace; /* autoEQP is in trace mode */ 1110 u8 scanstatsOn; /* True to display scan stats before each finalize */ 1111 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ 1112 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ 1113 u8 nEqpLevel; /* Depth of the EQP output graph */ 1114 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ 1115 u8 bSafeMode; /* True to prohibit unsafe operations */ 1116 u8 bSafeModePersist; /* The long-term value of bSafeMode */ 1117 ColModeOpts cmOpts; /* Option values affecting columnar mode output */ 1118 unsigned statsOn; /* True to display memory stats before each finalize */ 1119 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ 1120 int inputNesting; /* Track nesting level of .read and other redirects */ 1121 int outCount; /* Revert to stdout when reaching zero */ 1122 int cnt; /* Number of records displayed so far */ 1123 int lineno; /* Line number of last line read from in */ 1124 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ 1125 FILE *in; /* Read commands from this stream */ 1126 FILE *out; /* Write results here */ 1127 FILE *traceOut; /* Output for sqlite3_trace() */ 1128 int nErr; /* Number of errors seen */ 1129 int mode; /* An output mode setting */ 1130 int modePrior; /* Saved mode */ 1131 int cMode; /* temporary output mode for the current query */ 1132 int normalMode; /* Output mode before ".explain on" */ 1133 int writableSchema; /* True if PRAGMA writable_schema=ON */ 1134 int showHeader; /* True to show column names in List or Column mode */ 1135 int nCheck; /* Number of ".check" commands run */ 1136 unsigned nProgress; /* Number of progress callbacks encountered */ 1137 unsigned mxProgress; /* Maximum progress callbacks before failing */ 1138 unsigned flgProgress; /* Flags for the progress callback */ 1139 unsigned shellFlgs; /* Various flags */ 1140 unsigned priorShFlgs; /* Saved copy of flags */ 1141 sqlite3_int64 szMax; /* --maxsize argument to .open */ 1142 char *zDestTable; /* Name of destination table when MODE_Insert */ 1143 char *zTempFile; /* Temporary file that might need deleting */ 1144 char zTestcase[30]; /* Name of current test case */ 1145 char colSeparator[20]; /* Column separator character for several modes */ 1146 char rowSeparator[20]; /* Row separator character for MODE_Ascii */ 1147 char colSepPrior[20]; /* Saved column separator */ 1148 char rowSepPrior[20]; /* Saved row separator */ 1149 int *colWidth; /* Requested width of each column in columnar modes */ 1150 int *actualWidth; /* Actual width of each column */ 1151 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ 1152 char nullValue[20]; /* The text to print when a NULL comes back from 1153 ** the database */ 1154 char outfile[FILENAME_MAX]; /* Filename for *out */ 1155 sqlite3_stmt *pStmt; /* Current statement if any. */ 1156 FILE *pLog; /* Write log output here */ 1157 struct AuxDb { /* Storage space for auxiliary database connections */ 1158 sqlite3 *db; /* Connection pointer */ 1159 const char *zDbFilename; /* Filename used to open the connection */ 1160 char *zFreeOnClose; /* Free this memory allocation on close */ 1161#if defined(SQLITE_ENABLE_SESSION) 1162 int nSession; /* Number of active sessions */ 1163 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ 1164#endif 1165 } aAuxDb[5], /* Array of all database connections */ 1166 *pAuxDb; /* Currently active database connection */ 1167 int *aiIndent; /* Array of indents used in MODE_Explain */ 1168 int nIndent; /* Size of array aiIndent[] */ 1169 int iIndent; /* Index of current op in aiIndent[] */ 1170 char *zNonce; /* Nonce for temporary safe-mode excapes */ 1171 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ 1172 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ 1173#ifdef SQLITE_SHELL_FIDDLE 1174 struct { 1175 const char * zInput; /* Input string from wasm/JS proxy */ 1176 const char * zPos; /* Cursor pos into zInput */ 1177 const char * zDefaultDbName; /* Default name for db file */ 1178 } wasm; 1179#endif 1180}; 1181 1182#ifdef SQLITE_SHELL_FIDDLE 1183static ShellState shellState; 1184#endif 1185 1186 1187/* Allowed values for ShellState.autoEQP 1188*/ 1189#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ 1190#define AUTOEQP_on 1 /* Automatic EQP is on */ 1191#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ 1192#define AUTOEQP_full 3 /* Show full EXPLAIN */ 1193 1194/* Allowed values for ShellState.openMode 1195*/ 1196#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ 1197#define SHELL_OPEN_NORMAL 1 /* Normal database file */ 1198#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ 1199#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ 1200#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ 1201#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ 1202#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ 1203 1204/* Allowed values for ShellState.eTraceType 1205*/ 1206#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ 1207#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ 1208#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ 1209 1210/* Bits in the ShellState.flgProgress variable */ 1211#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ 1212#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres 1213 ** callback limit is reached, and for each 1214 ** top-level SQL statement */ 1215#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ 1216 1217/* 1218** These are the allowed shellFlgs values 1219*/ 1220#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ 1221#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ 1222#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ 1223#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ 1224#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ 1225#define SHFLG_CountChanges 0x00000020 /* .changes setting */ 1226#define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */ 1227#define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */ 1228#define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ 1229#define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ 1230 1231/* 1232** Macros for testing and setting shellFlgs 1233*/ 1234#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) 1235#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) 1236#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) 1237 1238/* 1239** These are the allowed modes. 1240*/ 1241#define MODE_Line 0 /* One column per line. Blank line between records */ 1242#define MODE_Column 1 /* One record per line in neat columns */ 1243#define MODE_List 2 /* One record per line with a separator */ 1244#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ 1245#define MODE_Html 4 /* Generate an XHTML table */ 1246#define MODE_Insert 5 /* Generate SQL "insert" statements */ 1247#define MODE_Quote 6 /* Quote values as for SQL */ 1248#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ 1249#define MODE_Csv 8 /* Quote strings, numbers are plain */ 1250#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ 1251#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ 1252#define MODE_Pretty 11 /* Pretty-print schemas */ 1253#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ 1254#define MODE_Json 13 /* Output JSON */ 1255#define MODE_Markdown 14 /* Markdown formatting */ 1256#define MODE_Table 15 /* MySQL-style table formatting */ 1257#define MODE_Box 16 /* Unicode box-drawing characters */ 1258#define MODE_Count 17 /* Output only a count of the rows of output */ 1259#define MODE_Off 18 /* No query output shown */ 1260 1261static const char *modeDescr[] = { 1262 "line", 1263 "column", 1264 "list", 1265 "semi", 1266 "html", 1267 "insert", 1268 "quote", 1269 "tcl", 1270 "csv", 1271 "explain", 1272 "ascii", 1273 "prettyprint", 1274 "eqp", 1275 "json", 1276 "markdown", 1277 "table", 1278 "box", 1279 "count", 1280 "off" 1281}; 1282 1283/* 1284** These are the column/row/line separators used by the various 1285** import/export modes. 1286*/ 1287#define SEP_Column "|" 1288#define SEP_Row "\n" 1289#define SEP_Tab "\t" 1290#define SEP_Space " " 1291#define SEP_Comma "," 1292#define SEP_CrLf "\r\n" 1293#define SEP_Unit "\x1F" 1294#define SEP_Record "\x1E" 1295 1296/* 1297** Limit input nesting via .read or any other input redirect. 1298** It's not too expensive, so a generous allowance can be made. 1299*/ 1300#define MAX_INPUT_NESTING 25 1301 1302/* 1303** A callback for the sqlite3_log() interface. 1304*/ 1305static void shellLog(void *pArg, int iErrCode, const char *zMsg){ 1306 ShellState *p = (ShellState*)pArg; 1307 if( p->pLog==0 ) return; 1308 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); 1309 fflush(p->pLog); 1310} 1311 1312/* 1313** SQL function: shell_putsnl(X) 1314** 1315** Write the text X to the screen (or whatever output is being directed) 1316** adding a newline at the end, and then return X. 1317*/ 1318static void shellPutsFunc( 1319 sqlite3_context *pCtx, 1320 int nVal, 1321 sqlite3_value **apVal 1322){ 1323 ShellState *p = (ShellState*)sqlite3_user_data(pCtx); 1324 (void)nVal; 1325 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); 1326 sqlite3_result_value(pCtx, apVal[0]); 1327} 1328 1329/* 1330** If in safe mode, print an error message described by the arguments 1331** and exit immediately. 1332*/ 1333static void failIfSafeMode( 1334 ShellState *p, 1335 const char *zErrMsg, 1336 ... 1337){ 1338 if( p->bSafeMode ){ 1339 va_list ap; 1340 char *zMsg; 1341 va_start(ap, zErrMsg); 1342 zMsg = sqlite3_vmprintf(zErrMsg, ap); 1343 va_end(ap); 1344 raw_printf(stderr, "line %d: ", p->lineno); 1345 utf8_printf(stderr, "%s\n", zMsg); 1346 exit(1); 1347 } 1348} 1349 1350/* 1351** SQL function: edit(VALUE) 1352** edit(VALUE,EDITOR) 1353** 1354** These steps: 1355** 1356** (1) Write VALUE into a temporary file. 1357** (2) Run program EDITOR on that temporary file. 1358** (3) Read the temporary file back and return its content as the result. 1359** (4) Delete the temporary file 1360** 1361** If the EDITOR argument is omitted, use the value in the VISUAL 1362** environment variable. If still there is no EDITOR, through an error. 1363** 1364** Also throw an error if the EDITOR program returns a non-zero exit code. 1365*/ 1366#ifndef SQLITE_NOHAVE_SYSTEM 1367static void editFunc( 1368 sqlite3_context *context, 1369 int argc, 1370 sqlite3_value **argv 1371){ 1372 const char *zEditor; 1373 char *zTempFile = 0; 1374 sqlite3 *db; 1375 char *zCmd = 0; 1376 int bBin; 1377 int rc; 1378 int hasCRNL = 0; 1379 FILE *f = 0; 1380 sqlite3_int64 sz; 1381 sqlite3_int64 x; 1382 unsigned char *p = 0; 1383 1384 if( argc==2 ){ 1385 zEditor = (const char*)sqlite3_value_text(argv[1]); 1386 }else{ 1387 zEditor = getenv("VISUAL"); 1388 } 1389 if( zEditor==0 ){ 1390 sqlite3_result_error(context, "no editor for edit()", -1); 1391 return; 1392 } 1393 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1394 sqlite3_result_error(context, "NULL input to edit()", -1); 1395 return; 1396 } 1397 db = sqlite3_context_db_handle(context); 1398 zTempFile = 0; 1399 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); 1400 if( zTempFile==0 ){ 1401 sqlite3_uint64 r = 0; 1402 sqlite3_randomness(sizeof(r), &r); 1403 zTempFile = sqlite3_mprintf("temp%llx", r); 1404 if( zTempFile==0 ){ 1405 sqlite3_result_error_nomem(context); 1406 return; 1407 } 1408 } 1409 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; 1410 /* When writing the file to be edited, do \n to \r\n conversions on systems 1411 ** that want \r\n line endings */ 1412 f = fopen(zTempFile, bBin ? "wb" : "w"); 1413 if( f==0 ){ 1414 sqlite3_result_error(context, "edit() cannot open temp file", -1); 1415 goto edit_func_end; 1416 } 1417 sz = sqlite3_value_bytes(argv[0]); 1418 if( bBin ){ 1419 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); 1420 }else{ 1421 const char *z = (const char*)sqlite3_value_text(argv[0]); 1422 /* Remember whether or not the value originally contained \r\n */ 1423 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; 1424 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); 1425 } 1426 fclose(f); 1427 f = 0; 1428 if( x!=sz ){ 1429 sqlite3_result_error(context, "edit() could not write the whole file", -1); 1430 goto edit_func_end; 1431 } 1432 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); 1433 if( zCmd==0 ){ 1434 sqlite3_result_error_nomem(context); 1435 goto edit_func_end; 1436 } 1437 rc = system(zCmd); 1438 sqlite3_free(zCmd); 1439 if( rc ){ 1440 sqlite3_result_error(context, "EDITOR returned non-zero", -1); 1441 goto edit_func_end; 1442 } 1443 f = fopen(zTempFile, "rb"); 1444 if( f==0 ){ 1445 sqlite3_result_error(context, 1446 "edit() cannot reopen temp file after edit", -1); 1447 goto edit_func_end; 1448 } 1449 fseek(f, 0, SEEK_END); 1450 sz = ftell(f); 1451 rewind(f); 1452 p = sqlite3_malloc64( sz+1 ); 1453 if( p==0 ){ 1454 sqlite3_result_error_nomem(context); 1455 goto edit_func_end; 1456 } 1457 x = fread(p, 1, (size_t)sz, f); 1458 fclose(f); 1459 f = 0; 1460 if( x!=sz ){ 1461 sqlite3_result_error(context, "could not read back the whole file", -1); 1462 goto edit_func_end; 1463 } 1464 if( bBin ){ 1465 sqlite3_result_blob64(context, p, sz, sqlite3_free); 1466 }else{ 1467 sqlite3_int64 i, j; 1468 if( hasCRNL ){ 1469 /* If the original contains \r\n then do no conversions back to \n */ 1470 }else{ 1471 /* If the file did not originally contain \r\n then convert any new 1472 ** \r\n back into \n */ 1473 for(i=j=0; i<sz; i++){ 1474 if( p[i]=='\r' && p[i+1]=='\n' ) i++; 1475 p[j++] = p[i]; 1476 } 1477 sz = j; 1478 p[sz] = 0; 1479 } 1480 sqlite3_result_text64(context, (const char*)p, sz, 1481 sqlite3_free, SQLITE_UTF8); 1482 } 1483 p = 0; 1484 1485edit_func_end: 1486 if( f ) fclose(f); 1487 unlink(zTempFile); 1488 sqlite3_free(zTempFile); 1489 sqlite3_free(p); 1490} 1491#endif /* SQLITE_NOHAVE_SYSTEM */ 1492 1493/* 1494** Save or restore the current output mode 1495*/ 1496static void outputModePush(ShellState *p){ 1497 p->modePrior = p->mode; 1498 p->priorShFlgs = p->shellFlgs; 1499 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); 1500 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); 1501} 1502static void outputModePop(ShellState *p){ 1503 p->mode = p->modePrior; 1504 p->shellFlgs = p->priorShFlgs; 1505 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); 1506 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); 1507} 1508 1509/* 1510** Output the given string as a hex-encoded blob (eg. X'1234' ) 1511*/ 1512static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ 1513 int i; 1514 unsigned char *aBlob = (unsigned char*)pBlob; 1515 1516 char *zStr = sqlite3_malloc(nBlob*2 + 1); 1517 shell_check_oom(zStr); 1518 1519 for(i=0; i<nBlob; i++){ 1520 static const char aHex[] = { 1521 '0', '1', '2', '3', '4', '5', '6', '7', 1522 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 1523 }; 1524 zStr[i*2] = aHex[ (aBlob[i] >> 4) ]; 1525 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; 1526 } 1527 zStr[i*2] = '\0'; 1528 1529 raw_printf(out,"X'%s'", zStr); 1530 sqlite3_free(zStr); 1531} 1532 1533/* 1534** Find a string that is not found anywhere in z[]. Return a pointer 1535** to that string. 1536** 1537** Try to use zA and zB first. If both of those are already found in z[] 1538** then make up some string and store it in the buffer zBuf. 1539*/ 1540static const char *unused_string( 1541 const char *z, /* Result must not appear anywhere in z */ 1542 const char *zA, const char *zB, /* Try these first */ 1543 char *zBuf /* Space to store a generated string */ 1544){ 1545 unsigned i = 0; 1546 if( strstr(z, zA)==0 ) return zA; 1547 if( strstr(z, zB)==0 ) return zB; 1548 do{ 1549 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); 1550 }while( strstr(z,zBuf)!=0 ); 1551 return zBuf; 1552} 1553 1554/* 1555** Output the given string as a quoted string using SQL quoting conventions. 1556** 1557** See also: output_quoted_escaped_string() 1558*/ 1559static void output_quoted_string(FILE *out, const char *z){ 1560 int i; 1561 char c; 1562 setBinaryMode(out, 1); 1563 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1564 if( c==0 ){ 1565 utf8_printf(out,"'%s'",z); 1566 }else{ 1567 raw_printf(out, "'"); 1568 while( *z ){ 1569 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1570 if( c=='\'' ) i++; 1571 if( i ){ 1572 utf8_printf(out, "%.*s", i, z); 1573 z += i; 1574 } 1575 if( c=='\'' ){ 1576 raw_printf(out, "'"); 1577 continue; 1578 } 1579 if( c==0 ){ 1580 break; 1581 } 1582 z++; 1583 } 1584 raw_printf(out, "'"); 1585 } 1586 setTextMode(out, 1); 1587} 1588 1589/* 1590** Output the given string as a quoted string using SQL quoting conventions. 1591** Additionallly , escape the "\n" and "\r" characters so that they do not 1592** get corrupted by end-of-line translation facilities in some operating 1593** systems. 1594** 1595** This is like output_quoted_string() but with the addition of the \r\n 1596** escape mechanism. 1597*/ 1598static void output_quoted_escaped_string(FILE *out, const char *z){ 1599 int i; 1600 char c; 1601 setBinaryMode(out, 1); 1602 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} 1603 if( c==0 ){ 1604 utf8_printf(out,"'%s'",z); 1605 }else{ 1606 const char *zNL = 0; 1607 const char *zCR = 0; 1608 int nNL = 0; 1609 int nCR = 0; 1610 char zBuf1[20], zBuf2[20]; 1611 for(i=0; z[i]; i++){ 1612 if( z[i]=='\n' ) nNL++; 1613 if( z[i]=='\r' ) nCR++; 1614 } 1615 if( nNL ){ 1616 raw_printf(out, "replace("); 1617 zNL = unused_string(z, "\\n", "\\012", zBuf1); 1618 } 1619 if( nCR ){ 1620 raw_printf(out, "replace("); 1621 zCR = unused_string(z, "\\r", "\\015", zBuf2); 1622 } 1623 raw_printf(out, "'"); 1624 while( *z ){ 1625 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} 1626 if( c=='\'' ) i++; 1627 if( i ){ 1628 utf8_printf(out, "%.*s", i, z); 1629 z += i; 1630 } 1631 if( c=='\'' ){ 1632 raw_printf(out, "'"); 1633 continue; 1634 } 1635 if( c==0 ){ 1636 break; 1637 } 1638 z++; 1639 if( c=='\n' ){ 1640 raw_printf(out, "%s", zNL); 1641 continue; 1642 } 1643 raw_printf(out, "%s", zCR); 1644 } 1645 raw_printf(out, "'"); 1646 if( nCR ){ 1647 raw_printf(out, ",'%s',char(13))", zCR); 1648 } 1649 if( nNL ){ 1650 raw_printf(out, ",'%s',char(10))", zNL); 1651 } 1652 } 1653 setTextMode(out, 1); 1654} 1655 1656/* 1657** Output the given string as a quoted according to C or TCL quoting rules. 1658*/ 1659static void output_c_string(FILE *out, const char *z){ 1660 unsigned int c; 1661 fputc('"', out); 1662 while( (c = *(z++))!=0 ){ 1663 if( c=='\\' ){ 1664 fputc(c, out); 1665 fputc(c, out); 1666 }else if( c=='"' ){ 1667 fputc('\\', out); 1668 fputc('"', out); 1669 }else if( c=='\t' ){ 1670 fputc('\\', out); 1671 fputc('t', out); 1672 }else if( c=='\n' ){ 1673 fputc('\\', out); 1674 fputc('n', out); 1675 }else if( c=='\r' ){ 1676 fputc('\\', out); 1677 fputc('r', out); 1678 }else if( !isprint(c&0xff) ){ 1679 raw_printf(out, "\\%03o", c&0xff); 1680 }else{ 1681 fputc(c, out); 1682 } 1683 } 1684 fputc('"', out); 1685} 1686 1687/* 1688** Output the given string as a quoted according to JSON quoting rules. 1689*/ 1690static void output_json_string(FILE *out, const char *z, i64 n){ 1691 unsigned int c; 1692 if( n<0 ) n = strlen(z); 1693 fputc('"', out); 1694 while( n-- ){ 1695 c = *(z++); 1696 if( c=='\\' || c=='"' ){ 1697 fputc('\\', out); 1698 fputc(c, out); 1699 }else if( c<=0x1f ){ 1700 fputc('\\', out); 1701 if( c=='\b' ){ 1702 fputc('b', out); 1703 }else if( c=='\f' ){ 1704 fputc('f', out); 1705 }else if( c=='\n' ){ 1706 fputc('n', out); 1707 }else if( c=='\r' ){ 1708 fputc('r', out); 1709 }else if( c=='\t' ){ 1710 fputc('t', out); 1711 }else{ 1712 raw_printf(out, "u%04x",c); 1713 } 1714 }else{ 1715 fputc(c, out); 1716 } 1717 } 1718 fputc('"', out); 1719} 1720 1721/* 1722** Output the given string with characters that are special to 1723** HTML escaped. 1724*/ 1725static void output_html_string(FILE *out, const char *z){ 1726 int i; 1727 if( z==0 ) z = ""; 1728 while( *z ){ 1729 for(i=0; z[i] 1730 && z[i]!='<' 1731 && z[i]!='&' 1732 && z[i]!='>' 1733 && z[i]!='\"' 1734 && z[i]!='\''; 1735 i++){} 1736 if( i>0 ){ 1737 utf8_printf(out,"%.*s",i,z); 1738 } 1739 if( z[i]=='<' ){ 1740 raw_printf(out,"<"); 1741 }else if( z[i]=='&' ){ 1742 raw_printf(out,"&"); 1743 }else if( z[i]=='>' ){ 1744 raw_printf(out,">"); 1745 }else if( z[i]=='\"' ){ 1746 raw_printf(out,"""); 1747 }else if( z[i]=='\'' ){ 1748 raw_printf(out,"'"); 1749 }else{ 1750 break; 1751 } 1752 z += i + 1; 1753 } 1754} 1755 1756/* 1757** If a field contains any character identified by a 1 in the following 1758** array, then the string must be quoted for CSV. 1759*/ 1760static const char needCsvQuote[] = { 1761 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1762 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1763 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1764 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1765 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1766 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1767 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1768 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1769 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1770 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1771 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1772 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1773 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1774 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1775 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1776 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1777}; 1778 1779/* 1780** Output a single term of CSV. Actually, p->colSeparator is used for 1781** the separator, which may or may not be a comma. p->nullValue is 1782** the null value. Strings are quoted if necessary. The separator 1783** is only issued if bSep is true. 1784*/ 1785static void output_csv(ShellState *p, const char *z, int bSep){ 1786 FILE *out = p->out; 1787 if( z==0 ){ 1788 utf8_printf(out,"%s",p->nullValue); 1789 }else{ 1790 unsigned i; 1791 for(i=0; z[i]; i++){ 1792 if( needCsvQuote[((unsigned char*)z)[i]] ){ 1793 i = 0; 1794 break; 1795 } 1796 } 1797 if( i==0 || strstr(z, p->colSeparator)!=0 ){ 1798 char *zQuoted = sqlite3_mprintf("\"%w\"", z); 1799 shell_check_oom(zQuoted); 1800 utf8_printf(out, "%s", zQuoted); 1801 sqlite3_free(zQuoted); 1802 }else{ 1803 utf8_printf(out, "%s", z); 1804 } 1805 } 1806 if( bSep ){ 1807 utf8_printf(p->out, "%s", p->colSeparator); 1808 } 1809} 1810 1811/* 1812** This routine runs when the user presses Ctrl-C 1813*/ 1814static void interrupt_handler(int NotUsed){ 1815 UNUSED_PARAMETER(NotUsed); 1816 seenInterrupt++; 1817 if( seenInterrupt>2 ) exit(1); 1818 if( globalDb ) sqlite3_interrupt(globalDb); 1819} 1820 1821#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 1822/* 1823** This routine runs for console events (e.g. Ctrl-C) on Win32 1824*/ 1825static BOOL WINAPI ConsoleCtrlHandler( 1826 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ 1827){ 1828 if( dwCtrlType==CTRL_C_EVENT ){ 1829 interrupt_handler(0); 1830 return TRUE; 1831 } 1832 return FALSE; 1833} 1834#endif 1835 1836#ifndef SQLITE_OMIT_AUTHORIZATION 1837/* 1838** This authorizer runs in safe mode. 1839*/ 1840static int safeModeAuth( 1841 void *pClientData, 1842 int op, 1843 const char *zA1, 1844 const char *zA2, 1845 const char *zA3, 1846 const char *zA4 1847){ 1848 ShellState *p = (ShellState*)pClientData; 1849 static const char *azProhibitedFunctions[] = { 1850 "edit", 1851 "fts3_tokenizer", 1852 "load_extension", 1853 "readfile", 1854 "writefile", 1855 "zipfile", 1856 "zipfile_cds", 1857 }; 1858 UNUSED_PARAMETER(zA2); 1859 UNUSED_PARAMETER(zA3); 1860 UNUSED_PARAMETER(zA4); 1861 switch( op ){ 1862 case SQLITE_ATTACH: { 1863#ifndef SQLITE_SHELL_FIDDLE 1864 /* In WASM builds the filesystem is a virtual sandbox, so 1865 ** there's no harm in using ATTACH. */ 1866 failIfSafeMode(p, "cannot run ATTACH in safe mode"); 1867#endif 1868 break; 1869 } 1870 case SQLITE_FUNCTION: { 1871 int i; 1872 for(i=0; i<ArraySize(azProhibitedFunctions); i++){ 1873 if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){ 1874 failIfSafeMode(p, "cannot use the %s() function in safe mode", 1875 azProhibitedFunctions[i]); 1876 } 1877 } 1878 break; 1879 } 1880 } 1881 return SQLITE_OK; 1882} 1883 1884/* 1885** When the ".auth ON" is set, the following authorizer callback is 1886** invoked. It always returns SQLITE_OK. 1887*/ 1888static int shellAuth( 1889 void *pClientData, 1890 int op, 1891 const char *zA1, 1892 const char *zA2, 1893 const char *zA3, 1894 const char *zA4 1895){ 1896 ShellState *p = (ShellState*)pClientData; 1897 static const char *azAction[] = { 0, 1898 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", 1899 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", 1900 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", 1901 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", 1902 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", 1903 "DROP_TRIGGER", "DROP_VIEW", "INSERT", 1904 "PRAGMA", "READ", "SELECT", 1905 "TRANSACTION", "UPDATE", "ATTACH", 1906 "DETACH", "ALTER_TABLE", "REINDEX", 1907 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", 1908 "FUNCTION", "SAVEPOINT", "RECURSIVE" 1909 }; 1910 int i; 1911 const char *az[4]; 1912 az[0] = zA1; 1913 az[1] = zA2; 1914 az[2] = zA3; 1915 az[3] = zA4; 1916 utf8_printf(p->out, "authorizer: %s", azAction[op]); 1917 for(i=0; i<4; i++){ 1918 raw_printf(p->out, " "); 1919 if( az[i] ){ 1920 output_c_string(p->out, az[i]); 1921 }else{ 1922 raw_printf(p->out, "NULL"); 1923 } 1924 } 1925 raw_printf(p->out, "\n"); 1926 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); 1927 return SQLITE_OK; 1928} 1929#endif 1930 1931/* 1932** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. 1933** 1934** This routine converts some CREATE TABLE statements for shadow tables 1935** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. 1936** 1937** If the schema statement in z[] contains a start-of-comment and if 1938** sqlite3_complete() returns false, try to terminate the comment before 1939** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c 1940*/ 1941static void printSchemaLine(FILE *out, const char *z, const char *zTail){ 1942 char *zToFree = 0; 1943 if( z==0 ) return; 1944 if( zTail==0 ) return; 1945 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ 1946 const char *zOrig = z; 1947 static const char *azTerm[] = { "", "*/", "\n" }; 1948 int i; 1949 for(i=0; i<ArraySize(azTerm); i++){ 1950 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]); 1951 if( sqlite3_complete(zNew) ){ 1952 size_t n = strlen(zNew); 1953 zNew[n-1] = 0; 1954 zToFree = zNew; 1955 z = zNew; 1956 break; 1957 } 1958 sqlite3_free(zNew); 1959 } 1960 } 1961 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ 1962 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); 1963 }else{ 1964 utf8_printf(out, "%s%s", z, zTail); 1965 } 1966 sqlite3_free(zToFree); 1967} 1968static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ 1969 char c = z[n]; 1970 z[n] = 0; 1971 printSchemaLine(out, z, zTail); 1972 z[n] = c; 1973} 1974 1975/* 1976** Return true if string z[] has nothing but whitespace and comments to the 1977** end of the first line. 1978*/ 1979static int wsToEol(const char *z){ 1980 int i; 1981 for(i=0; z[i]; i++){ 1982 if( z[i]=='\n' ) return 1; 1983 if( IsSpace(z[i]) ) continue; 1984 if( z[i]=='-' && z[i+1]=='-' ) return 1; 1985 return 0; 1986 } 1987 return 1; 1988} 1989 1990/* 1991** Add a new entry to the EXPLAIN QUERY PLAN data 1992*/ 1993static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ 1994 EQPGraphRow *pNew; 1995 i64 nText = strlen(zText); 1996 if( p->autoEQPtest ){ 1997 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); 1998 } 1999 pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); 2000 shell_check_oom(pNew); 2001 pNew->iEqpId = iEqpId; 2002 pNew->iParentId = p2; 2003 memcpy(pNew->zText, zText, nText+1); 2004 pNew->pNext = 0; 2005 if( p->sGraph.pLast ){ 2006 p->sGraph.pLast->pNext = pNew; 2007 }else{ 2008 p->sGraph.pRow = pNew; 2009 } 2010 p->sGraph.pLast = pNew; 2011} 2012 2013/* 2014** Free and reset the EXPLAIN QUERY PLAN data that has been collected 2015** in p->sGraph. 2016*/ 2017static void eqp_reset(ShellState *p){ 2018 EQPGraphRow *pRow, *pNext; 2019 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ 2020 pNext = pRow->pNext; 2021 sqlite3_free(pRow); 2022 } 2023 memset(&p->sGraph, 0, sizeof(p->sGraph)); 2024} 2025 2026/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after 2027** pOld, or return the first such line if pOld is NULL 2028*/ 2029static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ 2030 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; 2031 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; 2032 return pRow; 2033} 2034 2035/* Render a single level of the graph that has iEqpId as its parent. Called 2036** recursively to render sublevels. 2037*/ 2038static void eqp_render_level(ShellState *p, int iEqpId){ 2039 EQPGraphRow *pRow, *pNext; 2040 i64 n = strlen(p->sGraph.zPrefix); 2041 char *z; 2042 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ 2043 pNext = eqp_next_row(p, iEqpId, pRow); 2044 z = pRow->zText; 2045 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, 2046 pNext ? "|--" : "`--", z); 2047 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ 2048 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); 2049 eqp_render_level(p, pRow->iEqpId); 2050 p->sGraph.zPrefix[n] = 0; 2051 } 2052 } 2053} 2054 2055/* 2056** Display and reset the EXPLAIN QUERY PLAN data 2057*/ 2058static void eqp_render(ShellState *p){ 2059 EQPGraphRow *pRow = p->sGraph.pRow; 2060 if( pRow ){ 2061 if( pRow->zText[0]=='-' ){ 2062 if( pRow->pNext==0 ){ 2063 eqp_reset(p); 2064 return; 2065 } 2066 utf8_printf(p->out, "%s\n", pRow->zText+3); 2067 p->sGraph.pRow = pRow->pNext; 2068 sqlite3_free(pRow); 2069 }else{ 2070 utf8_printf(p->out, "QUERY PLAN\n"); 2071 } 2072 p->sGraph.zPrefix[0] = 0; 2073 eqp_render_level(p, 0); 2074 eqp_reset(p); 2075 } 2076} 2077 2078#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 2079/* 2080** Progress handler callback. 2081*/ 2082static int progress_handler(void *pClientData) { 2083 ShellState *p = (ShellState*)pClientData; 2084 p->nProgress++; 2085 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ 2086 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); 2087 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 2088 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; 2089 return 1; 2090 } 2091 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ 2092 raw_printf(p->out, "Progress %u\n", p->nProgress); 2093 } 2094 return 0; 2095} 2096#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 2097 2098/* 2099** Print N dashes 2100*/ 2101static void print_dashes(FILE *out, int N){ 2102 const char zDash[] = "--------------------------------------------------"; 2103 const int nDash = sizeof(zDash) - 1; 2104 while( N>nDash ){ 2105 fputs(zDash, out); 2106 N -= nDash; 2107 } 2108 raw_printf(out, "%.*s", N, zDash); 2109} 2110 2111/* 2112** Print a markdown or table-style row separator using ascii-art 2113*/ 2114static void print_row_separator( 2115 ShellState *p, 2116 int nArg, 2117 const char *zSep 2118){ 2119 int i; 2120 if( nArg>0 ){ 2121 fputs(zSep, p->out); 2122 print_dashes(p->out, p->actualWidth[0]+2); 2123 for(i=1; i<nArg; i++){ 2124 fputs(zSep, p->out); 2125 print_dashes(p->out, p->actualWidth[i]+2); 2126 } 2127 fputs(zSep, p->out); 2128 } 2129 fputs("\n", p->out); 2130} 2131 2132/* 2133** This is the callback routine that the shell 2134** invokes for each row of a query result. 2135*/ 2136static int shell_callback( 2137 void *pArg, 2138 int nArg, /* Number of result columns */ 2139 char **azArg, /* Text of each result column */ 2140 char **azCol, /* Column names */ 2141 int *aiType /* Column types. Might be NULL */ 2142){ 2143 int i; 2144 ShellState *p = (ShellState*)pArg; 2145 2146 if( azArg==0 ) return 0; 2147 switch( p->cMode ){ 2148 case MODE_Count: 2149 case MODE_Off: { 2150 break; 2151 } 2152 case MODE_Line: { 2153 int w = 5; 2154 if( azArg==0 ) break; 2155 for(i=0; i<nArg; i++){ 2156 int len = strlen30(azCol[i] ? azCol[i] : ""); 2157 if( len>w ) w = len; 2158 } 2159 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); 2160 for(i=0; i<nArg; i++){ 2161 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], 2162 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); 2163 } 2164 break; 2165 } 2166 case MODE_Explain: { 2167 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; 2168 if( nArg>ArraySize(aExplainWidth) ){ 2169 nArg = ArraySize(aExplainWidth); 2170 } 2171 if( p->cnt++==0 ){ 2172 for(i=0; i<nArg; i++){ 2173 int w = aExplainWidth[i]; 2174 utf8_width_print(p->out, w, azCol[i]); 2175 fputs(i==nArg-1 ? "\n" : " ", p->out); 2176 } 2177 for(i=0; i<nArg; i++){ 2178 int w = aExplainWidth[i]; 2179 print_dashes(p->out, w); 2180 fputs(i==nArg-1 ? "\n" : " ", p->out); 2181 } 2182 } 2183 if( azArg==0 ) break; 2184 for(i=0; i<nArg; i++){ 2185 int w = aExplainWidth[i]; 2186 if( i==nArg-1 ) w = 0; 2187 if( azArg[i] && strlenChar(azArg[i])>w ){ 2188 w = strlenChar(azArg[i]); 2189 } 2190 if( i==1 && p->aiIndent && p->pStmt ){ 2191 if( p->iIndent<p->nIndent ){ 2192 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); 2193 } 2194 p->iIndent++; 2195 } 2196 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); 2197 fputs(i==nArg-1 ? "\n" : " ", p->out); 2198 } 2199 break; 2200 } 2201 case MODE_Semi: { /* .schema and .fullschema output */ 2202 printSchemaLine(p->out, azArg[0], ";\n"); 2203 break; 2204 } 2205 case MODE_Pretty: { /* .schema and .fullschema with --indent */ 2206 char *z; 2207 int j; 2208 int nParen = 0; 2209 char cEnd = 0; 2210 char c; 2211 int nLine = 0; 2212 assert( nArg==1 ); 2213 if( azArg[0]==0 ) break; 2214 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 2215 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 2216 ){ 2217 utf8_printf(p->out, "%s;\n", azArg[0]); 2218 break; 2219 } 2220 z = sqlite3_mprintf("%s", azArg[0]); 2221 shell_check_oom(z); 2222 j = 0; 2223 for(i=0; IsSpace(z[i]); i++){} 2224 for(; (c = z[i])!=0; i++){ 2225 if( IsSpace(c) ){ 2226 if( z[j-1]=='\r' ) z[j-1] = '\n'; 2227 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; 2228 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ 2229 j--; 2230 } 2231 z[j++] = c; 2232 } 2233 while( j>0 && IsSpace(z[j-1]) ){ j--; } 2234 z[j] = 0; 2235 if( strlen30(z)>=79 ){ 2236 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ 2237 if( c==cEnd ){ 2238 cEnd = 0; 2239 }else if( c=='"' || c=='\'' || c=='`' ){ 2240 cEnd = c; 2241 }else if( c=='[' ){ 2242 cEnd = ']'; 2243 }else if( c=='-' && z[i+1]=='-' ){ 2244 cEnd = '\n'; 2245 }else if( c=='(' ){ 2246 nParen++; 2247 }else if( c==')' ){ 2248 nParen--; 2249 if( nLine>0 && nParen==0 && j>0 ){ 2250 printSchemaLineN(p->out, z, j, "\n"); 2251 j = 0; 2252 } 2253 } 2254 z[j++] = c; 2255 if( nParen==1 && cEnd==0 2256 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) 2257 ){ 2258 if( c=='\n' ) j--; 2259 printSchemaLineN(p->out, z, j, "\n "); 2260 j = 0; 2261 nLine++; 2262 while( IsSpace(z[i+1]) ){ i++; } 2263 } 2264 } 2265 z[j] = 0; 2266 } 2267 printSchemaLine(p->out, z, ";\n"); 2268 sqlite3_free(z); 2269 break; 2270 } 2271 case MODE_List: { 2272 if( p->cnt++==0 && p->showHeader ){ 2273 for(i=0; i<nArg; i++){ 2274 utf8_printf(p->out,"%s%s",azCol[i], 2275 i==nArg-1 ? p->rowSeparator : p->colSeparator); 2276 } 2277 } 2278 if( azArg==0 ) break; 2279 for(i=0; i<nArg; i++){ 2280 char *z = azArg[i]; 2281 if( z==0 ) z = p->nullValue; 2282 utf8_printf(p->out, "%s", z); 2283 if( i<nArg-1 ){ 2284 utf8_printf(p->out, "%s", p->colSeparator); 2285 }else{ 2286 utf8_printf(p->out, "%s", p->rowSeparator); 2287 } 2288 } 2289 break; 2290 } 2291 case MODE_Html: { 2292 if( p->cnt++==0 && p->showHeader ){ 2293 raw_printf(p->out,"<TR>"); 2294 for(i=0; i<nArg; i++){ 2295 raw_printf(p->out,"<TH>"); 2296 output_html_string(p->out, azCol[i]); 2297 raw_printf(p->out,"</TH>\n"); 2298 } 2299 raw_printf(p->out,"</TR>\n"); 2300 } 2301 if( azArg==0 ) break; 2302 raw_printf(p->out,"<TR>"); 2303 for(i=0; i<nArg; i++){ 2304 raw_printf(p->out,"<TD>"); 2305 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2306 raw_printf(p->out,"</TD>\n"); 2307 } 2308 raw_printf(p->out,"</TR>\n"); 2309 break; 2310 } 2311 case MODE_Tcl: { 2312 if( p->cnt++==0 && p->showHeader ){ 2313 for(i=0; i<nArg; i++){ 2314 output_c_string(p->out,azCol[i] ? azCol[i] : ""); 2315 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2316 } 2317 utf8_printf(p->out, "%s", p->rowSeparator); 2318 } 2319 if( azArg==0 ) break; 2320 for(i=0; i<nArg; i++){ 2321 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2322 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2323 } 2324 utf8_printf(p->out, "%s", p->rowSeparator); 2325 break; 2326 } 2327 case MODE_Csv: { 2328 setBinaryMode(p->out, 1); 2329 if( p->cnt++==0 && p->showHeader ){ 2330 for(i=0; i<nArg; i++){ 2331 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); 2332 } 2333 utf8_printf(p->out, "%s", p->rowSeparator); 2334 } 2335 if( nArg>0 ){ 2336 for(i=0; i<nArg; i++){ 2337 output_csv(p, azArg[i], i<nArg-1); 2338 } 2339 utf8_printf(p->out, "%s", p->rowSeparator); 2340 } 2341 setTextMode(p->out, 1); 2342 break; 2343 } 2344 case MODE_Insert: { 2345 if( azArg==0 ) break; 2346 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); 2347 if( p->showHeader ){ 2348 raw_printf(p->out,"("); 2349 for(i=0; i<nArg; i++){ 2350 if( i>0 ) raw_printf(p->out, ","); 2351 if( quoteChar(azCol[i]) ){ 2352 char *z = sqlite3_mprintf("\"%w\"", azCol[i]); 2353 shell_check_oom(z); 2354 utf8_printf(p->out, "%s", z); 2355 sqlite3_free(z); 2356 }else{ 2357 raw_printf(p->out, "%s", azCol[i]); 2358 } 2359 } 2360 raw_printf(p->out,")"); 2361 } 2362 p->cnt++; 2363 for(i=0; i<nArg; i++){ 2364 raw_printf(p->out, i>0 ? "," : " VALUES("); 2365 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2366 utf8_printf(p->out,"NULL"); 2367 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2368 if( ShellHasFlag(p, SHFLG_Newlines) ){ 2369 output_quoted_string(p->out, azArg[i]); 2370 }else{ 2371 output_quoted_escaped_string(p->out, azArg[i]); 2372 } 2373 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2374 utf8_printf(p->out,"%s", azArg[i]); 2375 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2376 char z[50]; 2377 double r = sqlite3_column_double(p->pStmt, i); 2378 sqlite3_uint64 ur; 2379 memcpy(&ur,&r,sizeof(r)); 2380 if( ur==0x7ff0000000000000LL ){ 2381 raw_printf(p->out, "1e999"); 2382 }else if( ur==0xfff0000000000000LL ){ 2383 raw_printf(p->out, "-1e999"); 2384 }else{ 2385 sqlite3_int64 ir = (sqlite3_int64)r; 2386 if( r==(double)ir ){ 2387 sqlite3_snprintf(50,z,"%lld.0", ir); 2388 }else{ 2389 sqlite3_snprintf(50,z,"%!.20g", r); 2390 } 2391 raw_printf(p->out, "%s", z); 2392 } 2393 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2394 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2395 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2396 output_hex_blob(p->out, pBlob, nBlob); 2397 }else if( isNumber(azArg[i], 0) ){ 2398 utf8_printf(p->out,"%s", azArg[i]); 2399 }else if( ShellHasFlag(p, SHFLG_Newlines) ){ 2400 output_quoted_string(p->out, azArg[i]); 2401 }else{ 2402 output_quoted_escaped_string(p->out, azArg[i]); 2403 } 2404 } 2405 raw_printf(p->out,");\n"); 2406 break; 2407 } 2408 case MODE_Json: { 2409 if( azArg==0 ) break; 2410 if( p->cnt==0 ){ 2411 fputs("[{", p->out); 2412 }else{ 2413 fputs(",\n{", p->out); 2414 } 2415 p->cnt++; 2416 for(i=0; i<nArg; i++){ 2417 output_json_string(p->out, azCol[i], -1); 2418 putc(':', p->out); 2419 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2420 fputs("null",p->out); 2421 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2422 char z[50]; 2423 double r = sqlite3_column_double(p->pStmt, i); 2424 sqlite3_uint64 ur; 2425 memcpy(&ur,&r,sizeof(r)); 2426 if( ur==0x7ff0000000000000LL ){ 2427 raw_printf(p->out, "1e999"); 2428 }else if( ur==0xfff0000000000000LL ){ 2429 raw_printf(p->out, "-1e999"); 2430 }else{ 2431 sqlite3_snprintf(50,z,"%!.20g", r); 2432 raw_printf(p->out, "%s", z); 2433 } 2434 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2435 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2436 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2437 output_json_string(p->out, pBlob, nBlob); 2438 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2439 output_json_string(p->out, azArg[i], -1); 2440 }else{ 2441 utf8_printf(p->out,"%s", azArg[i]); 2442 } 2443 if( i<nArg-1 ){ 2444 putc(',', p->out); 2445 } 2446 } 2447 putc('}', p->out); 2448 break; 2449 } 2450 case MODE_Quote: { 2451 if( azArg==0 ) break; 2452 if( p->cnt==0 && p->showHeader ){ 2453 for(i=0; i<nArg; i++){ 2454 if( i>0 ) fputs(p->colSeparator, p->out); 2455 output_quoted_string(p->out, azCol[i]); 2456 } 2457 fputs(p->rowSeparator, p->out); 2458 } 2459 p->cnt++; 2460 for(i=0; i<nArg; i++){ 2461 if( i>0 ) fputs(p->colSeparator, p->out); 2462 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2463 utf8_printf(p->out,"NULL"); 2464 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2465 output_quoted_string(p->out, azArg[i]); 2466 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2467 utf8_printf(p->out,"%s", azArg[i]); 2468 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2469 char z[50]; 2470 double r = sqlite3_column_double(p->pStmt, i); 2471 sqlite3_snprintf(50,z,"%!.20g", r); 2472 raw_printf(p->out, "%s", z); 2473 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2474 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2475 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2476 output_hex_blob(p->out, pBlob, nBlob); 2477 }else if( isNumber(azArg[i], 0) ){ 2478 utf8_printf(p->out,"%s", azArg[i]); 2479 }else{ 2480 output_quoted_string(p->out, azArg[i]); 2481 } 2482 } 2483 fputs(p->rowSeparator, p->out); 2484 break; 2485 } 2486 case MODE_Ascii: { 2487 if( p->cnt++==0 && p->showHeader ){ 2488 for(i=0; i<nArg; i++){ 2489 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2490 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); 2491 } 2492 utf8_printf(p->out, "%s", p->rowSeparator); 2493 } 2494 if( azArg==0 ) break; 2495 for(i=0; i<nArg; i++){ 2496 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2497 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); 2498 } 2499 utf8_printf(p->out, "%s", p->rowSeparator); 2500 break; 2501 } 2502 case MODE_EQP: { 2503 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); 2504 break; 2505 } 2506 } 2507 return 0; 2508} 2509 2510/* 2511** This is the callback routine that the SQLite library 2512** invokes for each row of a query result. 2513*/ 2514static int callback(void *pArg, int nArg, char **azArg, char **azCol){ 2515 /* since we don't have type info, call the shell_callback with a NULL value */ 2516 return shell_callback(pArg, nArg, azArg, azCol, NULL); 2517} 2518 2519/* 2520** This is the callback routine from sqlite3_exec() that appends all 2521** output onto the end of a ShellText object. 2522*/ 2523static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ 2524 ShellText *p = (ShellText*)pArg; 2525 int i; 2526 UNUSED_PARAMETER(az); 2527 if( azArg==0 ) return 0; 2528 if( p->n ) appendText(p, "|", 0); 2529 for(i=0; i<nArg; i++){ 2530 if( i ) appendText(p, ",", 0); 2531 if( azArg[i] ) appendText(p, azArg[i], 0); 2532 } 2533 return 0; 2534} 2535 2536/* 2537** Generate an appropriate SELFTEST table in the main database. 2538*/ 2539static void createSelftestTable(ShellState *p){ 2540 char *zErrMsg = 0; 2541 sqlite3_exec(p->db, 2542 "SAVEPOINT selftest_init;\n" 2543 "CREATE TABLE IF NOT EXISTS selftest(\n" 2544 " tno INTEGER PRIMARY KEY,\n" /* Test number */ 2545 " op TEXT,\n" /* Operator: memo run */ 2546 " cmd TEXT,\n" /* Command text */ 2547 " ans TEXT\n" /* Desired answer */ 2548 ");" 2549 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" 2550 "INSERT INTO [_shell$self](rowid,op,cmd)\n" 2551 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" 2552 " 'memo','Tests generated by --init');\n" 2553 "INSERT INTO [_shell$self]\n" 2554 " SELECT 'run',\n" 2555 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " 2556 "FROM sqlite_schema ORDER BY 2'',224))',\n" 2557 " hex(sha3_query('SELECT type,name,tbl_name,sql " 2558 "FROM sqlite_schema ORDER BY 2',224));\n" 2559 "INSERT INTO [_shell$self]\n" 2560 " SELECT 'run'," 2561 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" 2562 " printf('%w',name) || '\" NOT INDEXED'',224))',\n" 2563 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" 2564 " FROM (\n" 2565 " SELECT name FROM sqlite_schema\n" 2566 " WHERE type='table'\n" 2567 " AND name<>'selftest'\n" 2568 " AND coalesce(rootpage,0)>0\n" 2569 " )\n" 2570 " ORDER BY name;\n" 2571 "INSERT INTO [_shell$self]\n" 2572 " VALUES('run','PRAGMA integrity_check','ok');\n" 2573 "INSERT INTO selftest(tno,op,cmd,ans)" 2574 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" 2575 "DROP TABLE [_shell$self];" 2576 ,0,0,&zErrMsg); 2577 if( zErrMsg ){ 2578 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); 2579 sqlite3_free(zErrMsg); 2580 } 2581 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); 2582} 2583 2584 2585/* 2586** Set the destination table field of the ShellState structure to 2587** the name of the table given. Escape any quote characters in the 2588** table name. 2589*/ 2590static void set_table_name(ShellState *p, const char *zName){ 2591 int i, n; 2592 char cQuote; 2593 char *z; 2594 2595 if( p->zDestTable ){ 2596 free(p->zDestTable); 2597 p->zDestTable = 0; 2598 } 2599 if( zName==0 ) return; 2600 cQuote = quoteChar(zName); 2601 n = strlen30(zName); 2602 if( cQuote ) n += n+2; 2603 z = p->zDestTable = malloc( n+1 ); 2604 shell_check_oom(z); 2605 n = 0; 2606 if( cQuote ) z[n++] = cQuote; 2607 for(i=0; zName[i]; i++){ 2608 z[n++] = zName[i]; 2609 if( zName[i]==cQuote ) z[n++] = cQuote; 2610 } 2611 if( cQuote ) z[n++] = cQuote; 2612 z[n] = 0; 2613} 2614 2615/* 2616** Maybe construct two lines of text that point out the position of a 2617** syntax error. Return a pointer to the text, in memory obtained from 2618** sqlite3_malloc(). Or, if the most recent error does not involve a 2619** specific token that we can point to, return an empty string. 2620** 2621** In all cases, the memory returned is obtained from sqlite3_malloc64() 2622** and should be released by the caller invoking sqlite3_free(). 2623*/ 2624static char *shell_error_context(const char *zSql, sqlite3 *db){ 2625 int iOffset; 2626 size_t len; 2627 char *zCode; 2628 char *zMsg; 2629 int i; 2630 if( db==0 2631 || zSql==0 2632 || (iOffset = sqlite3_error_offset(db))<0 2633 ){ 2634 return sqlite3_mprintf(""); 2635 } 2636 while( iOffset>50 ){ 2637 iOffset--; 2638 zSql++; 2639 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } 2640 } 2641 len = strlen(zSql); 2642 if( len>78 ){ 2643 len = 78; 2644 while( (zSql[len]&0xc0)==0x80 ) len--; 2645 } 2646 zCode = sqlite3_mprintf("%.*s", len, zSql); 2647 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } 2648 if( iOffset<25 ){ 2649 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, ""); 2650 }else{ 2651 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, ""); 2652 } 2653 return zMsg; 2654} 2655 2656 2657/* 2658** Execute a query statement that will generate SQL output. Print 2659** the result columns, comma-separated, on a line and then add a 2660** semicolon terminator to the end of that line. 2661** 2662** If the number of columns is 1 and that column contains text "--" 2663** then write the semicolon on a separate line. That way, if a 2664** "--" comment occurs at the end of the statement, the comment 2665** won't consume the semicolon terminator. 2666*/ 2667static int run_table_dump_query( 2668 ShellState *p, /* Query context */ 2669 const char *zSelect /* SELECT statement to extract content */ 2670){ 2671 sqlite3_stmt *pSelect; 2672 int rc; 2673 int nResult; 2674 int i; 2675 const char *z; 2676 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); 2677 if( rc!=SQLITE_OK || !pSelect ){ 2678 char *zContext = shell_error_context(zSelect, p->db); 2679 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc, 2680 sqlite3_errmsg(p->db), zContext); 2681 sqlite3_free(zContext); 2682 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2683 return rc; 2684 } 2685 rc = sqlite3_step(pSelect); 2686 nResult = sqlite3_column_count(pSelect); 2687 while( rc==SQLITE_ROW ){ 2688 z = (const char*)sqlite3_column_text(pSelect, 0); 2689 utf8_printf(p->out, "%s", z); 2690 for(i=1; i<nResult; i++){ 2691 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); 2692 } 2693 if( z==0 ) z = ""; 2694 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; 2695 if( z[0] ){ 2696 raw_printf(p->out, "\n;\n"); 2697 }else{ 2698 raw_printf(p->out, ";\n"); 2699 } 2700 rc = sqlite3_step(pSelect); 2701 } 2702 rc = sqlite3_finalize(pSelect); 2703 if( rc!=SQLITE_OK ){ 2704 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2705 sqlite3_errmsg(p->db)); 2706 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2707 } 2708 return rc; 2709} 2710 2711/* 2712** Allocate space and save off string indicating current error. 2713*/ 2714static char *save_err_msg( 2715 sqlite3 *db, /* Database to query */ 2716 const char *zPhase, /* When the error occcurs */ 2717 int rc, /* Error code returned from API */ 2718 const char *zSql /* SQL string, or NULL */ 2719){ 2720 char *zErr; 2721 char *zContext; 2722 sqlite3_str *pStr = sqlite3_str_new(0); 2723 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); 2724 if( rc>1 ){ 2725 sqlite3_str_appendf(pStr, " (%d)", rc); 2726 } 2727 zContext = shell_error_context(zSql, db); 2728 if( zContext ){ 2729 sqlite3_str_appendall(pStr, zContext); 2730 sqlite3_free(zContext); 2731 } 2732 zErr = sqlite3_str_finish(pStr); 2733 shell_check_oom(zErr); 2734 return zErr; 2735} 2736 2737#ifdef __linux__ 2738/* 2739** Attempt to display I/O stats on Linux using /proc/PID/io 2740*/ 2741static void displayLinuxIoStats(FILE *out){ 2742 FILE *in; 2743 char z[200]; 2744 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 2745 in = fopen(z, "rb"); 2746 if( in==0 ) return; 2747 while( fgets(z, sizeof(z), in)!=0 ){ 2748 static const struct { 2749 const char *zPattern; 2750 const char *zDesc; 2751 } aTrans[] = { 2752 { "rchar: ", "Bytes received by read():" }, 2753 { "wchar: ", "Bytes sent to write():" }, 2754 { "syscr: ", "Read() system calls:" }, 2755 { "syscw: ", "Write() system calls:" }, 2756 { "read_bytes: ", "Bytes read from storage:" }, 2757 { "write_bytes: ", "Bytes written to storage:" }, 2758 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 2759 }; 2760 int i; 2761 for(i=0; i<ArraySize(aTrans); i++){ 2762 int n = strlen30(aTrans[i].zPattern); 2763 if( strncmp(aTrans[i].zPattern, z, n)==0 ){ 2764 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); 2765 break; 2766 } 2767 } 2768 } 2769 fclose(in); 2770} 2771#endif 2772 2773/* 2774** Display a single line of status using 64-bit values. 2775*/ 2776static void displayStatLine( 2777 ShellState *p, /* The shell context */ 2778 char *zLabel, /* Label for this one line */ 2779 char *zFormat, /* Format for the result */ 2780 int iStatusCtrl, /* Which status to display */ 2781 int bReset /* True to reset the stats */ 2782){ 2783 sqlite3_int64 iCur = -1; 2784 sqlite3_int64 iHiwtr = -1; 2785 int i, nPercent; 2786 char zLine[200]; 2787 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); 2788 for(i=0, nPercent=0; zFormat[i]; i++){ 2789 if( zFormat[i]=='%' ) nPercent++; 2790 } 2791 if( nPercent>1 ){ 2792 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); 2793 }else{ 2794 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); 2795 } 2796 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); 2797} 2798 2799/* 2800** Display memory stats. 2801*/ 2802static int display_stats( 2803 sqlite3 *db, /* Database to query */ 2804 ShellState *pArg, /* Pointer to ShellState */ 2805 int bReset /* True to reset the stats */ 2806){ 2807 int iCur; 2808 int iHiwtr; 2809 FILE *out; 2810 if( pArg==0 || pArg->out==0 ) return 0; 2811 out = pArg->out; 2812 2813 if( pArg->pStmt && pArg->statsOn==2 ){ 2814 int nCol, i, x; 2815 sqlite3_stmt *pStmt = pArg->pStmt; 2816 char z[100]; 2817 nCol = sqlite3_column_count(pStmt); 2818 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); 2819 for(i=0; i<nCol; i++){ 2820 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); 2821 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); 2822#ifndef SQLITE_OMIT_DECLTYPE 2823 sqlite3_snprintf(30, z+x, "declared type:"); 2824 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); 2825#endif 2826#ifdef SQLITE_ENABLE_COLUMN_METADATA 2827 sqlite3_snprintf(30, z+x, "database name:"); 2828 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); 2829 sqlite3_snprintf(30, z+x, "table name:"); 2830 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); 2831 sqlite3_snprintf(30, z+x, "origin name:"); 2832 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); 2833#endif 2834 } 2835 } 2836 2837 if( pArg->statsOn==3 ){ 2838 if( pArg->pStmt ){ 2839 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2840 raw_printf(pArg->out, "VM-steps: %d\n", iCur); 2841 } 2842 return 0; 2843 } 2844 2845 displayStatLine(pArg, "Memory Used:", 2846 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); 2847 displayStatLine(pArg, "Number of Outstanding Allocations:", 2848 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); 2849 if( pArg->shellFlgs & SHFLG_Pagecache ){ 2850 displayStatLine(pArg, "Number of Pcache Pages Used:", 2851 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); 2852 } 2853 displayStatLine(pArg, "Number of Pcache Overflow Bytes:", 2854 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); 2855 displayStatLine(pArg, "Largest Allocation:", 2856 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); 2857 displayStatLine(pArg, "Largest Pcache Allocation:", 2858 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); 2859#ifdef YYTRACKMAXSTACKDEPTH 2860 displayStatLine(pArg, "Deepest Parser Stack:", 2861 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); 2862#endif 2863 2864 if( db ){ 2865 if( pArg->shellFlgs & SHFLG_Lookaside ){ 2866 iHiwtr = iCur = -1; 2867 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, 2868 &iCur, &iHiwtr, bReset); 2869 raw_printf(pArg->out, 2870 "Lookaside Slots Used: %d (max %d)\n", 2871 iCur, iHiwtr); 2872 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, 2873 &iCur, &iHiwtr, bReset); 2874 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", 2875 iHiwtr); 2876 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, 2877 &iCur, &iHiwtr, bReset); 2878 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", 2879 iHiwtr); 2880 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, 2881 &iCur, &iHiwtr, bReset); 2882 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", 2883 iHiwtr); 2884 } 2885 iHiwtr = iCur = -1; 2886 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); 2887 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", 2888 iCur); 2889 iHiwtr = iCur = -1; 2890 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); 2891 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); 2892 iHiwtr = iCur = -1; 2893 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); 2894 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); 2895 iHiwtr = iCur = -1; 2896 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); 2897 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); 2898 iHiwtr = iCur = -1; 2899 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); 2900 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); 2901 iHiwtr = iCur = -1; 2902 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); 2903 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", 2904 iCur); 2905 iHiwtr = iCur = -1; 2906 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); 2907 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", 2908 iCur); 2909 } 2910 2911 if( pArg->pStmt ){ 2912 int iHit, iMiss; 2913 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, 2914 bReset); 2915 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); 2916 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); 2917 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); 2918 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); 2919 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); 2920 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset); 2921 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset); 2922 if( iHit || iMiss ){ 2923 raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n", 2924 iHit, iHit+iMiss); 2925 } 2926 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2927 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); 2928 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); 2929 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); 2930 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); 2931 raw_printf(pArg->out, "Number of times run: %d\n", iCur); 2932 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); 2933 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); 2934 } 2935 2936#ifdef __linux__ 2937 displayLinuxIoStats(pArg->out); 2938#endif 2939 2940 /* Do not remove this machine readable comment: extra-stats-output-here */ 2941 2942 return 0; 2943} 2944 2945/* 2946** Display scan stats. 2947*/ 2948static void display_scanstats( 2949 sqlite3 *db, /* Database to query */ 2950 ShellState *pArg /* Pointer to ShellState */ 2951){ 2952#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 2953 UNUSED_PARAMETER(db); 2954 UNUSED_PARAMETER(pArg); 2955#else 2956 int i, k, n, mx; 2957 raw_printf(pArg->out, "-------- scanstats --------\n"); 2958 mx = 0; 2959 for(k=0; k<=mx; k++){ 2960 double rEstLoop = 1.0; 2961 for(i=n=0; 1; i++){ 2962 sqlite3_stmt *p = pArg->pStmt; 2963 sqlite3_int64 nLoop, nVisit; 2964 double rEst; 2965 int iSid; 2966 const char *zExplain; 2967 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ 2968 break; 2969 } 2970 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); 2971 if( iSid>mx ) mx = iSid; 2972 if( iSid!=k ) continue; 2973 if( n==0 ){ 2974 rEstLoop = (double)nLoop; 2975 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); 2976 } 2977 n++; 2978 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2979 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); 2980 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 2981 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); 2982 rEstLoop *= rEst; 2983 raw_printf(pArg->out, 2984 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", 2985 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst 2986 ); 2987 } 2988 } 2989 raw_printf(pArg->out, "---------------------------\n"); 2990#endif 2991} 2992 2993/* 2994** Parameter azArray points to a zero-terminated array of strings. zStr 2995** points to a single nul-terminated string. Return non-zero if zStr 2996** is equal, according to strcmp(), to any of the strings in the array. 2997** Otherwise, return zero. 2998*/ 2999static int str_in_array(const char *zStr, const char **azArray){ 3000 int i; 3001 for(i=0; azArray[i]; i++){ 3002 if( 0==strcmp(zStr, azArray[i]) ) return 1; 3003 } 3004 return 0; 3005} 3006 3007/* 3008** If compiled statement pSql appears to be an EXPLAIN statement, allocate 3009** and populate the ShellState.aiIndent[] array with the number of 3010** spaces each opcode should be indented before it is output. 3011** 3012** The indenting rules are: 3013** 3014** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent 3015** all opcodes that occur between the p2 jump destination and the opcode 3016** itself by 2 spaces. 3017** 3018** * Do the previous for "Return" instructions for when P2 is positive. 3019** See tag-20220407a in wherecode.c and vdbe.c. 3020** 3021** * For each "Goto", if the jump destination is earlier in the program 3022** and ends on one of: 3023** Yield SeekGt SeekLt RowSetRead Rewind 3024** or if the P1 parameter is one instead of zero, 3025** then indent all opcodes between the earlier instruction 3026** and "Goto" by 2 spaces. 3027*/ 3028static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ 3029 const char *zSql; /* The text of the SQL statement */ 3030 const char *z; /* Used to check if this is an EXPLAIN */ 3031 int *abYield = 0; /* True if op is an OP_Yield */ 3032 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ 3033 int iOp; /* Index of operation in p->aiIndent[] */ 3034 3035 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 3036 "Return", 0 }; 3037 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", 3038 "Rewind", 0 }; 3039 const char *azGoto[] = { "Goto", 0 }; 3040 3041 /* Try to figure out if this is really an EXPLAIN statement. If this 3042 ** cannot be verified, return early. */ 3043 if( sqlite3_column_count(pSql)!=8 ){ 3044 p->cMode = p->mode; 3045 return; 3046 } 3047 zSql = sqlite3_sql(pSql); 3048 if( zSql==0 ) return; 3049 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); 3050 if( sqlite3_strnicmp(z, "explain", 7) ){ 3051 p->cMode = p->mode; 3052 return; 3053 } 3054 3055 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ 3056 int i; 3057 int iAddr = sqlite3_column_int(pSql, 0); 3058 const char *zOp = (const char*)sqlite3_column_text(pSql, 1); 3059 3060 /* Set p2 to the P2 field of the current opcode. Then, assuming that 3061 ** p2 is an instruction address, set variable p2op to the index of that 3062 ** instruction in the aiIndent[] array. p2 and p2op may be different if 3063 ** the current instruction is part of a sub-program generated by an 3064 ** SQL trigger or foreign key. */ 3065 int p2 = sqlite3_column_int(pSql, 3); 3066 int p2op = (p2 + (iOp-iAddr)); 3067 3068 /* Grow the p->aiIndent array as required */ 3069 if( iOp>=nAlloc ){ 3070 if( iOp==0 ){ 3071 /* Do further verfication that this is explain output. Abort if 3072 ** it is not */ 3073 static const char *explainCols[] = { 3074 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; 3075 int jj; 3076 for(jj=0; jj<ArraySize(explainCols); jj++){ 3077 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ 3078 p->cMode = p->mode; 3079 sqlite3_reset(pSql); 3080 return; 3081 } 3082 } 3083 } 3084 nAlloc += 100; 3085 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); 3086 shell_check_oom(p->aiIndent); 3087 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); 3088 shell_check_oom(abYield); 3089 } 3090 abYield[iOp] = str_in_array(zOp, azYield); 3091 p->aiIndent[iOp] = 0; 3092 p->nIndent = iOp+1; 3093 3094 if( str_in_array(zOp, azNext) && p2op>0 ){ 3095 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 3096 } 3097 if( str_in_array(zOp, azGoto) && p2op<p->nIndent 3098 && (abYield[p2op] || sqlite3_column_int(pSql, 2)) 3099 ){ 3100 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 3101 } 3102 } 3103 3104 p->iIndent = 0; 3105 sqlite3_free(abYield); 3106 sqlite3_reset(pSql); 3107} 3108 3109/* 3110** Free the array allocated by explain_data_prepare(). 3111*/ 3112static void explain_data_delete(ShellState *p){ 3113 sqlite3_free(p->aiIndent); 3114 p->aiIndent = 0; 3115 p->nIndent = 0; 3116 p->iIndent = 0; 3117} 3118 3119/* 3120** Disable and restore .wheretrace and .treetrace/.selecttrace settings. 3121*/ 3122static unsigned int savedSelectTrace; 3123static unsigned int savedWhereTrace; 3124static void disable_debug_trace_modes(void){ 3125 unsigned int zero = 0; 3126 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); 3127 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); 3128 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); 3129 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); 3130} 3131static void restore_debug_trace_modes(void){ 3132 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); 3133 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); 3134} 3135 3136/* Create the TEMP table used to store parameter bindings */ 3137static void bind_table_init(ShellState *p){ 3138 int wrSchema = 0; 3139 int defensiveMode = 0; 3140 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); 3141 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); 3142 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); 3143 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); 3144 sqlite3_exec(p->db, 3145 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" 3146 " key TEXT PRIMARY KEY,\n" 3147 " value\n" 3148 ") WITHOUT ROWID;", 3149 0, 0, 0); 3150 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); 3151 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); 3152} 3153 3154/* 3155** Bind parameters on a prepared statement. 3156** 3157** Parameter bindings are taken from a TEMP table of the form: 3158** 3159** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) 3160** WITHOUT ROWID; 3161** 3162** No bindings occur if this table does not exist. The name of the table 3163** begins with "sqlite_" so that it will not collide with ordinary application 3164** tables. The table must be in the TEMP schema. 3165*/ 3166static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ 3167 int nVar; 3168 int i; 3169 int rc; 3170 sqlite3_stmt *pQ = 0; 3171 3172 nVar = sqlite3_bind_parameter_count(pStmt); 3173 if( nVar==0 ) return; /* Nothing to do */ 3174 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", 3175 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ 3176 return; /* Parameter table does not exist */ 3177 } 3178 rc = sqlite3_prepare_v2(pArg->db, 3179 "SELECT value FROM temp.sqlite_parameters" 3180 " WHERE key=?1", -1, &pQ, 0); 3181 if( rc || pQ==0 ) return; 3182 for(i=1; i<=nVar; i++){ 3183 char zNum[30]; 3184 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 3185 if( zVar==0 ){ 3186 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); 3187 zVar = zNum; 3188 } 3189 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); 3190 if( sqlite3_step(pQ)==SQLITE_ROW ){ 3191 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); 3192 }else{ 3193 sqlite3_bind_null(pStmt, i); 3194 } 3195 sqlite3_reset(pQ); 3196 } 3197 sqlite3_finalize(pQ); 3198} 3199 3200/* 3201** UTF8 box-drawing characters. Imagine box lines like this: 3202** 3203** 1 3204** | 3205** 4 --+-- 2 3206** | 3207** 3 3208** 3209** Each box characters has between 2 and 4 of the lines leading from 3210** the center. The characters are here identified by the numbers of 3211** their corresponding lines. 3212*/ 3213#define BOX_24 "\342\224\200" /* U+2500 --- */ 3214#define BOX_13 "\342\224\202" /* U+2502 | */ 3215#define BOX_23 "\342\224\214" /* U+250c ,- */ 3216#define BOX_34 "\342\224\220" /* U+2510 -, */ 3217#define BOX_12 "\342\224\224" /* U+2514 '- */ 3218#define BOX_14 "\342\224\230" /* U+2518 -' */ 3219#define BOX_123 "\342\224\234" /* U+251c |- */ 3220#define BOX_134 "\342\224\244" /* U+2524 -| */ 3221#define BOX_234 "\342\224\254" /* U+252c -,- */ 3222#define BOX_124 "\342\224\264" /* U+2534 -'- */ 3223#define BOX_1234 "\342\224\274" /* U+253c -|- */ 3224 3225/* Draw horizontal line N characters long using unicode box 3226** characters 3227*/ 3228static void print_box_line(FILE *out, int N){ 3229 const char zDash[] = 3230 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 3231 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; 3232 const int nDash = sizeof(zDash) - 1; 3233 N *= 3; 3234 while( N>nDash ){ 3235 utf8_printf(out, zDash); 3236 N -= nDash; 3237 } 3238 utf8_printf(out, "%.*s", N, zDash); 3239} 3240 3241/* 3242** Draw a horizontal separator for a MODE_Box table. 3243*/ 3244static void print_box_row_separator( 3245 ShellState *p, 3246 int nArg, 3247 const char *zSep1, 3248 const char *zSep2, 3249 const char *zSep3 3250){ 3251 int i; 3252 if( nArg>0 ){ 3253 utf8_printf(p->out, "%s", zSep1); 3254 print_box_line(p->out, p->actualWidth[0]+2); 3255 for(i=1; i<nArg; i++){ 3256 utf8_printf(p->out, "%s", zSep2); 3257 print_box_line(p->out, p->actualWidth[i]+2); 3258 } 3259 utf8_printf(p->out, "%s", zSep3); 3260 } 3261 fputs("\n", p->out); 3262} 3263 3264/* 3265** z[] is a line of text that is to be displayed the .mode box or table or 3266** similar tabular formats. z[] might contain control characters such 3267** as \n, \t, \f, or \r. 3268** 3269** Compute characters to display on the first line of z[]. Stop at the 3270** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained 3271** from malloc()) of that first line, which caller should free sometime. 3272** Write anything to display on the next line into *pzTail. If this is 3273** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) 3274*/ 3275static char *translateForDisplayAndDup( 3276 const unsigned char *z, /* Input text to be transformed */ 3277 const unsigned char **pzTail, /* OUT: Tail of the input for next line */ 3278 int mxWidth, /* Max width. 0 means no limit */ 3279 u8 bWordWrap /* If true, avoid breaking mid-word */ 3280){ 3281 int i; /* Input bytes consumed */ 3282 int j; /* Output bytes generated */ 3283 int k; /* Input bytes to be displayed */ 3284 int n; /* Output column number */ 3285 unsigned char *zOut; /* Output text */ 3286 3287 if( z==0 ){ 3288 *pzTail = 0; 3289 return 0; 3290 } 3291 if( mxWidth<0 ) mxWidth = -mxWidth; 3292 if( mxWidth==0 ) mxWidth = 1000000; 3293 i = j = n = 0; 3294 while( n<mxWidth ){ 3295 if( z[i]>=' ' ){ 3296 n++; 3297 do{ i++; j++; }while( (z[i]&0xc0)==0x80 ); 3298 continue; 3299 } 3300 if( z[i]=='\t' ){ 3301 do{ 3302 n++; 3303 j++; 3304 }while( (n&7)!=0 && n<mxWidth ); 3305 i++; 3306 continue; 3307 } 3308 break; 3309 } 3310 if( n>=mxWidth && bWordWrap ){ 3311 /* Perhaps try to back up to a better place to break the line */ 3312 for(k=i; k>i/2; k--){ 3313 if( isspace(z[k-1]) ) break; 3314 } 3315 if( k<=i/2 ){ 3316 for(k=i; k>i/2; k--){ 3317 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; 3318 } 3319 } 3320 if( k<=i/2 ){ 3321 k = i; 3322 }else{ 3323 i = k; 3324 while( z[i]==' ' ) i++; 3325 } 3326 }else{ 3327 k = i; 3328 } 3329 if( n>=mxWidth && z[i]>=' ' ){ 3330 *pzTail = &z[i]; 3331 }else if( z[i]=='\r' && z[i+1]=='\n' ){ 3332 *pzTail = z[i+2] ? &z[i+2] : 0; 3333 }else if( z[i]==0 || z[i+1]==0 ){ 3334 *pzTail = 0; 3335 }else{ 3336 *pzTail = &z[i+1]; 3337 } 3338 zOut = malloc( j+1 ); 3339 shell_check_oom(zOut); 3340 i = j = n = 0; 3341 while( i<k ){ 3342 if( z[i]>=' ' ){ 3343 n++; 3344 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 ); 3345 continue; 3346 } 3347 if( z[i]=='\t' ){ 3348 do{ 3349 n++; 3350 zOut[j++] = ' '; 3351 }while( (n&7)!=0 && n<mxWidth ); 3352 i++; 3353 continue; 3354 } 3355 break; 3356 } 3357 zOut[j] = 0; 3358 return (char*)zOut; 3359} 3360 3361/* Extract the value of the i-th current column for pStmt as an SQL literal 3362** value. Memory is obtained from sqlite3_malloc64() and must be freed by 3363** the caller. 3364*/ 3365static char *quoted_column(sqlite3_stmt *pStmt, int i){ 3366 switch( sqlite3_column_type(pStmt, i) ){ 3367 case SQLITE_NULL: { 3368 return sqlite3_mprintf("NULL"); 3369 } 3370 case SQLITE_INTEGER: 3371 case SQLITE_FLOAT: { 3372 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); 3373 } 3374 case SQLITE_TEXT: { 3375 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i)); 3376 } 3377 case SQLITE_BLOB: { 3378 int j; 3379 sqlite3_str *pStr = sqlite3_str_new(0); 3380 const unsigned char *a = sqlite3_column_blob(pStmt,i); 3381 int n = sqlite3_column_bytes(pStmt,i); 3382 sqlite3_str_append(pStr, "x'", 2); 3383 for(j=0; j<n; j++){ 3384 sqlite3_str_appendf(pStr, "%02x", a[j]); 3385 } 3386 sqlite3_str_append(pStr, "'", 1); 3387 return sqlite3_str_finish(pStr); 3388 } 3389 } 3390 return 0; /* Not reached */ 3391} 3392 3393/* 3394** Run a prepared statement and output the result in one of the 3395** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, 3396** or MODE_Box. 3397** 3398** This is different from ordinary exec_prepared_stmt() in that 3399** it has to run the entire query and gather the results into memory 3400** first, in order to determine column widths, before providing 3401** any output. 3402*/ 3403static void exec_prepared_stmt_columnar( 3404 ShellState *p, /* Pointer to ShellState */ 3405 sqlite3_stmt *pStmt /* Statment to run */ 3406){ 3407 sqlite3_int64 nRow = 0; 3408 int nColumn = 0; 3409 char **azData = 0; 3410 sqlite3_int64 nAlloc = 0; 3411 char *abRowDiv = 0; 3412 const unsigned char *uz; 3413 const char *z; 3414 char **azQuoted = 0; 3415 int rc; 3416 sqlite3_int64 i, nData; 3417 int j, nTotal, w, n; 3418 const char *colSep = 0; 3419 const char *rowSep = 0; 3420 const unsigned char **azNextLine = 0; 3421 int bNextLine = 0; 3422 int bMultiLineRowExists = 0; 3423 int bw = p->cmOpts.bWordWrap; 3424 const char *zEmpty = ""; 3425 const char *zShowNull = p->nullValue; 3426 3427 rc = sqlite3_step(pStmt); 3428 if( rc!=SQLITE_ROW ) return; 3429 nColumn = sqlite3_column_count(pStmt); 3430 nAlloc = nColumn*4; 3431 if( nAlloc<=0 ) nAlloc = 1; 3432 azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); 3433 shell_check_oom(azData); 3434 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); 3435 shell_check_oom((void*)azNextLine); 3436 memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); 3437 if( p->cmOpts.bQuote ){ 3438 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); 3439 shell_check_oom(azQuoted); 3440 memset(azQuoted, 0, nColumn*sizeof(char*) ); 3441 } 3442 abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); 3443 shell_check_oom(abRowDiv); 3444 if( nColumn>p->nWidth ){ 3445 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); 3446 shell_check_oom(p->colWidth); 3447 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; 3448 p->nWidth = nColumn; 3449 p->actualWidth = &p->colWidth[nColumn]; 3450 } 3451 memset(p->actualWidth, 0, nColumn*sizeof(int)); 3452 for(i=0; i<nColumn; i++){ 3453 w = p->colWidth[i]; 3454 if( w<0 ) w = -w; 3455 p->actualWidth[i] = w; 3456 } 3457 for(i=0; i<nColumn; i++){ 3458 const unsigned char *zNotUsed; 3459 int wx = p->colWidth[i]; 3460 if( wx==0 ){ 3461 wx = p->cmOpts.iWrap; 3462 } 3463 if( wx<0 ) wx = -wx; 3464 uz = (const unsigned char*)sqlite3_column_name(pStmt,i); 3465 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw); 3466 } 3467 do{ 3468 int useNextLine = bNextLine; 3469 bNextLine = 0; 3470 if( (nRow+2)*nColumn >= nAlloc ){ 3471 nAlloc *= 2; 3472 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); 3473 shell_check_oom(azData); 3474 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); 3475 shell_check_oom(abRowDiv); 3476 } 3477 abRowDiv[nRow] = 1; 3478 nRow++; 3479 for(i=0; i<nColumn; i++){ 3480 int wx = p->colWidth[i]; 3481 if( wx==0 ){ 3482 wx = p->cmOpts.iWrap; 3483 } 3484 if( wx<0 ) wx = -wx; 3485 if( useNextLine ){ 3486 uz = azNextLine[i]; 3487 if( uz==0 ) uz = (u8*)zEmpty; 3488 }else if( p->cmOpts.bQuote ){ 3489 sqlite3_free(azQuoted[i]); 3490 azQuoted[i] = quoted_column(pStmt,i); 3491 uz = (const unsigned char*)azQuoted[i]; 3492 }else{ 3493 uz = (const unsigned char*)sqlite3_column_text(pStmt,i); 3494 if( uz==0 ) uz = (u8*)zShowNull; 3495 } 3496 azData[nRow*nColumn + i] 3497 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw); 3498 if( azNextLine[i] ){ 3499 bNextLine = 1; 3500 abRowDiv[nRow-1] = 0; 3501 bMultiLineRowExists = 1; 3502 } 3503 } 3504 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); 3505 nTotal = nColumn*(nRow+1); 3506 for(i=0; i<nTotal; i++){ 3507 z = azData[i]; 3508 if( z==0 ) z = (char*)zEmpty; 3509 n = strlenChar(z); 3510 j = i%nColumn; 3511 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; 3512 } 3513 if( seenInterrupt ) goto columnar_end; 3514 if( nColumn==0 ) goto columnar_end; 3515 switch( p->cMode ){ 3516 case MODE_Column: { 3517 colSep = " "; 3518 rowSep = "\n"; 3519 if( p->showHeader ){ 3520 for(i=0; i<nColumn; i++){ 3521 w = p->actualWidth[i]; 3522 if( p->colWidth[i]<0 ) w = -w; 3523 utf8_width_print(p->out, w, azData[i]); 3524 fputs(i==nColumn-1?"\n":" ", p->out); 3525 } 3526 for(i=0; i<nColumn; i++){ 3527 print_dashes(p->out, p->actualWidth[i]); 3528 fputs(i==nColumn-1?"\n":" ", p->out); 3529 } 3530 } 3531 break; 3532 } 3533 case MODE_Table: { 3534 colSep = " | "; 3535 rowSep = " |\n"; 3536 print_row_separator(p, nColumn, "+"); 3537 fputs("| ", p->out); 3538 for(i=0; i<nColumn; i++){ 3539 w = p->actualWidth[i]; 3540 n = strlenChar(azData[i]); 3541 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3542 fputs(i==nColumn-1?" |\n":" | ", p->out); 3543 } 3544 print_row_separator(p, nColumn, "+"); 3545 break; 3546 } 3547 case MODE_Markdown: { 3548 colSep = " | "; 3549 rowSep = " |\n"; 3550 fputs("| ", p->out); 3551 for(i=0; i<nColumn; i++){ 3552 w = p->actualWidth[i]; 3553 n = strlenChar(azData[i]); 3554 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3555 fputs(i==nColumn-1?" |\n":" | ", p->out); 3556 } 3557 print_row_separator(p, nColumn, "|"); 3558 break; 3559 } 3560 case MODE_Box: { 3561 colSep = " " BOX_13 " "; 3562 rowSep = " " BOX_13 "\n"; 3563 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); 3564 utf8_printf(p->out, BOX_13 " "); 3565 for(i=0; i<nColumn; i++){ 3566 w = p->actualWidth[i]; 3567 n = strlenChar(azData[i]); 3568 utf8_printf(p->out, "%*s%s%*s%s", 3569 (w-n)/2, "", azData[i], (w-n+1)/2, "", 3570 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); 3571 } 3572 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); 3573 break; 3574 } 3575 } 3576 for(i=nColumn, j=0; i<nTotal; i++, j++){ 3577 if( j==0 && p->cMode!=MODE_Column ){ 3578 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); 3579 } 3580 z = azData[i]; 3581 if( z==0 ) z = p->nullValue; 3582 w = p->actualWidth[j]; 3583 if( p->colWidth[j]<0 ) w = -w; 3584 utf8_width_print(p->out, w, z); 3585 if( j==nColumn-1 ){ 3586 utf8_printf(p->out, "%s", rowSep); 3587 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ 3588 if( p->cMode==MODE_Table ){ 3589 print_row_separator(p, nColumn, "+"); 3590 }else if( p->cMode==MODE_Box ){ 3591 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); 3592 }else if( p->cMode==MODE_Column ){ 3593 raw_printf(p->out, "\n"); 3594 } 3595 } 3596 j = -1; 3597 if( seenInterrupt ) goto columnar_end; 3598 }else{ 3599 utf8_printf(p->out, "%s", colSep); 3600 } 3601 } 3602 if( p->cMode==MODE_Table ){ 3603 print_row_separator(p, nColumn, "+"); 3604 }else if( p->cMode==MODE_Box ){ 3605 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); 3606 } 3607columnar_end: 3608 if( seenInterrupt ){ 3609 utf8_printf(p->out, "Interrupt\n"); 3610 } 3611 nData = (nRow+1)*nColumn; 3612 for(i=0; i<nData; i++){ 3613 z = azData[i]; 3614 if( z!=zEmpty && z!=zShowNull ) free(azData[i]); 3615 } 3616 sqlite3_free(azData); 3617 sqlite3_free((void*)azNextLine); 3618 sqlite3_free(abRowDiv); 3619 if( azQuoted ){ 3620 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]); 3621 sqlite3_free(azQuoted); 3622 } 3623} 3624 3625/* 3626** Run a prepared statement 3627*/ 3628static void exec_prepared_stmt( 3629 ShellState *pArg, /* Pointer to ShellState */ 3630 sqlite3_stmt *pStmt /* Statment to run */ 3631){ 3632 int rc; 3633 sqlite3_uint64 nRow = 0; 3634 3635 if( pArg->cMode==MODE_Column 3636 || pArg->cMode==MODE_Table 3637 || pArg->cMode==MODE_Box 3638 || pArg->cMode==MODE_Markdown 3639 ){ 3640 exec_prepared_stmt_columnar(pArg, pStmt); 3641 return; 3642 } 3643 3644 /* perform the first step. this will tell us if we 3645 ** have a result set or not and how wide it is. 3646 */ 3647 rc = sqlite3_step(pStmt); 3648 /* if we have a result set... */ 3649 if( SQLITE_ROW == rc ){ 3650 /* allocate space for col name ptr, value ptr, and type */ 3651 int nCol = sqlite3_column_count(pStmt); 3652 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); 3653 if( !pData ){ 3654 shell_out_of_memory(); 3655 }else{ 3656 char **azCols = (char **)pData; /* Names of result columns */ 3657 char **azVals = &azCols[nCol]; /* Results */ 3658 int *aiTypes = (int *)&azVals[nCol]; /* Result types */ 3659 int i, x; 3660 assert(sizeof(int) <= sizeof(char *)); 3661 /* save off ptrs to column names */ 3662 for(i=0; i<nCol; i++){ 3663 azCols[i] = (char *)sqlite3_column_name(pStmt, i); 3664 } 3665 do{ 3666 nRow++; 3667 /* extract the data and data types */ 3668 for(i=0; i<nCol; i++){ 3669 aiTypes[i] = x = sqlite3_column_type(pStmt, i); 3670 if( x==SQLITE_BLOB 3671 && pArg 3672 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) 3673 ){ 3674 azVals[i] = ""; 3675 }else{ 3676 azVals[i] = (char*)sqlite3_column_text(pStmt, i); 3677 } 3678 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ 3679 rc = SQLITE_NOMEM; 3680 break; /* from for */ 3681 } 3682 } /* end for */ 3683 3684 /* if data and types extracted successfully... */ 3685 if( SQLITE_ROW == rc ){ 3686 /* call the supplied callback with the result row data */ 3687 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ 3688 rc = SQLITE_ABORT; 3689 }else{ 3690 rc = sqlite3_step(pStmt); 3691 } 3692 } 3693 } while( SQLITE_ROW == rc ); 3694 sqlite3_free(pData); 3695 if( pArg->cMode==MODE_Json ){ 3696 fputs("]\n", pArg->out); 3697 }else if( pArg->cMode==MODE_Count ){ 3698 char zBuf[200]; 3699 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n", 3700 nRow, nRow!=1 ? "s" : ""); 3701 printf("%s", zBuf); 3702 } 3703 } 3704 } 3705} 3706 3707#ifndef SQLITE_OMIT_VIRTUALTABLE 3708/* 3709** This function is called to process SQL if the previous shell command 3710** was ".expert". It passes the SQL in the second argument directly to 3711** the sqlite3expert object. 3712** 3713** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3714** code. In this case, (*pzErr) may be set to point to a buffer containing 3715** an English language error message. It is the responsibility of the 3716** caller to eventually free this buffer using sqlite3_free(). 3717*/ 3718static int expertHandleSQL( 3719 ShellState *pState, 3720 const char *zSql, 3721 char **pzErr 3722){ 3723 assert( pState->expert.pExpert ); 3724 assert( pzErr==0 || *pzErr==0 ); 3725 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); 3726} 3727 3728/* 3729** This function is called either to silently clean up the object 3730** created by the ".expert" command (if bCancel==1), or to generate a 3731** report from it and then clean it up (if bCancel==0). 3732** 3733** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3734** code. In this case, (*pzErr) may be set to point to a buffer containing 3735** an English language error message. It is the responsibility of the 3736** caller to eventually free this buffer using sqlite3_free(). 3737*/ 3738static int expertFinish( 3739 ShellState *pState, 3740 int bCancel, 3741 char **pzErr 3742){ 3743 int rc = SQLITE_OK; 3744 sqlite3expert *p = pState->expert.pExpert; 3745 assert( p ); 3746 assert( bCancel || pzErr==0 || *pzErr==0 ); 3747 if( bCancel==0 ){ 3748 FILE *out = pState->out; 3749 int bVerbose = pState->expert.bVerbose; 3750 3751 rc = sqlite3_expert_analyze(p, pzErr); 3752 if( rc==SQLITE_OK ){ 3753 int nQuery = sqlite3_expert_count(p); 3754 int i; 3755 3756 if( bVerbose ){ 3757 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); 3758 raw_printf(out, "-- Candidates -----------------------------\n"); 3759 raw_printf(out, "%s\n", zCand); 3760 } 3761 for(i=0; i<nQuery; i++){ 3762 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); 3763 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); 3764 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); 3765 if( zIdx==0 ) zIdx = "(no new indexes)\n"; 3766 if( bVerbose ){ 3767 raw_printf(out, "-- Query %d --------------------------------\n",i+1); 3768 raw_printf(out, "%s\n\n", zSql); 3769 } 3770 raw_printf(out, "%s\n", zIdx); 3771 raw_printf(out, "%s\n", zEQP); 3772 } 3773 } 3774 } 3775 sqlite3_expert_destroy(p); 3776 pState->expert.pExpert = 0; 3777 return rc; 3778} 3779 3780/* 3781** Implementation of ".expert" dot command. 3782*/ 3783static int expertDotCommand( 3784 ShellState *pState, /* Current shell tool state */ 3785 char **azArg, /* Array of arguments passed to dot command */ 3786 int nArg /* Number of entries in azArg[] */ 3787){ 3788 int rc = SQLITE_OK; 3789 char *zErr = 0; 3790 int i; 3791 int iSample = 0; 3792 3793 assert( pState->expert.pExpert==0 ); 3794 memset(&pState->expert, 0, sizeof(ExpertInfo)); 3795 3796 for(i=1; rc==SQLITE_OK && i<nArg; i++){ 3797 char *z = azArg[i]; 3798 int n; 3799 if( z[0]=='-' && z[1]=='-' ) z++; 3800 n = strlen30(z); 3801 if( n>=2 && 0==strncmp(z, "-verbose", n) ){ 3802 pState->expert.bVerbose = 1; 3803 } 3804 else if( n>=2 && 0==strncmp(z, "-sample", n) ){ 3805 if( i==(nArg-1) ){ 3806 raw_printf(stderr, "option requires an argument: %s\n", z); 3807 rc = SQLITE_ERROR; 3808 }else{ 3809 iSample = (int)integerValue(azArg[++i]); 3810 if( iSample<0 || iSample>100 ){ 3811 raw_printf(stderr, "value out of range: %s\n", azArg[i]); 3812 rc = SQLITE_ERROR; 3813 } 3814 } 3815 } 3816 else{ 3817 raw_printf(stderr, "unknown option: %s\n", z); 3818 rc = SQLITE_ERROR; 3819 } 3820 } 3821 3822 if( rc==SQLITE_OK ){ 3823 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); 3824 if( pState->expert.pExpert==0 ){ 3825 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory"); 3826 rc = SQLITE_ERROR; 3827 }else{ 3828 sqlite3_expert_config( 3829 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample 3830 ); 3831 } 3832 } 3833 sqlite3_free(zErr); 3834 3835 return rc; 3836} 3837#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 3838 3839/* 3840** Execute a statement or set of statements. Print 3841** any result rows/columns depending on the current mode 3842** set via the supplied callback. 3843** 3844** This is very similar to SQLite's built-in sqlite3_exec() 3845** function except it takes a slightly different callback 3846** and callback data argument. 3847*/ 3848static int shell_exec( 3849 ShellState *pArg, /* Pointer to ShellState */ 3850 const char *zSql, /* SQL to be evaluated */ 3851 char **pzErrMsg /* Error msg written here */ 3852){ 3853 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ 3854 int rc = SQLITE_OK; /* Return Code */ 3855 int rc2; 3856 const char *zLeftover; /* Tail of unprocessed SQL */ 3857 sqlite3 *db = pArg->db; 3858 3859 if( pzErrMsg ){ 3860 *pzErrMsg = NULL; 3861 } 3862 3863#ifndef SQLITE_OMIT_VIRTUALTABLE 3864 if( pArg->expert.pExpert ){ 3865 rc = expertHandleSQL(pArg, zSql, pzErrMsg); 3866 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); 3867 } 3868#endif 3869 3870 while( zSql[0] && (SQLITE_OK == rc) ){ 3871 static const char *zStmtSql; 3872 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); 3873 if( SQLITE_OK != rc ){ 3874 if( pzErrMsg ){ 3875 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql); 3876 } 3877 }else{ 3878 if( !pStmt ){ 3879 /* this happens for a comment or white-space */ 3880 zSql = zLeftover; 3881 while( IsSpace(zSql[0]) ) zSql++; 3882 continue; 3883 } 3884 zStmtSql = sqlite3_sql(pStmt); 3885 if( zStmtSql==0 ) zStmtSql = ""; 3886 while( IsSpace(zStmtSql[0]) ) zStmtSql++; 3887 3888 /* save off the prepared statment handle and reset row count */ 3889 if( pArg ){ 3890 pArg->pStmt = pStmt; 3891 pArg->cnt = 0; 3892 } 3893 3894 /* Show the EXPLAIN QUERY PLAN if .eqp is on */ 3895 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ 3896 sqlite3_stmt *pExplain; 3897 char *zEQP; 3898 int triggerEQP = 0; 3899 disable_debug_trace_modes(); 3900 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); 3901 if( pArg->autoEQP>=AUTOEQP_trigger ){ 3902 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); 3903 } 3904 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); 3905 shell_check_oom(zEQP); 3906 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3907 if( rc==SQLITE_OK ){ 3908 while( sqlite3_step(pExplain)==SQLITE_ROW ){ 3909 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); 3910 int iEqpId = sqlite3_column_int(pExplain, 0); 3911 int iParentId = sqlite3_column_int(pExplain, 1); 3912 if( zEQPLine==0 ) zEQPLine = ""; 3913 if( zEQPLine[0]=='-' ) eqp_render(pArg); 3914 eqp_append(pArg, iEqpId, iParentId, zEQPLine); 3915 } 3916 eqp_render(pArg); 3917 } 3918 sqlite3_finalize(pExplain); 3919 sqlite3_free(zEQP); 3920 if( pArg->autoEQP>=AUTOEQP_full ){ 3921 /* Also do an EXPLAIN for ".eqp full" mode */ 3922 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); 3923 shell_check_oom(zEQP); 3924 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3925 if( rc==SQLITE_OK ){ 3926 pArg->cMode = MODE_Explain; 3927 explain_data_prepare(pArg, pExplain); 3928 exec_prepared_stmt(pArg, pExplain); 3929 explain_data_delete(pArg); 3930 } 3931 sqlite3_finalize(pExplain); 3932 sqlite3_free(zEQP); 3933 } 3934 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ 3935 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); 3936 /* Reprepare pStmt before reactiving trace modes */ 3937 sqlite3_finalize(pStmt); 3938 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 3939 if( pArg ) pArg->pStmt = pStmt; 3940 } 3941 restore_debug_trace_modes(); 3942 } 3943 3944 if( pArg ){ 3945 pArg->cMode = pArg->mode; 3946 if( pArg->autoExplain ){ 3947 if( sqlite3_stmt_isexplain(pStmt)==1 ){ 3948 pArg->cMode = MODE_Explain; 3949 } 3950 if( sqlite3_stmt_isexplain(pStmt)==2 ){ 3951 pArg->cMode = MODE_EQP; 3952 } 3953 } 3954 3955 /* If the shell is currently in ".explain" mode, gather the extra 3956 ** data required to add indents to the output.*/ 3957 if( pArg->cMode==MODE_Explain ){ 3958 explain_data_prepare(pArg, pStmt); 3959 } 3960 } 3961 3962 bind_prepared_stmt(pArg, pStmt); 3963 exec_prepared_stmt(pArg, pStmt); 3964 explain_data_delete(pArg); 3965 eqp_render(pArg); 3966 3967 /* print usage stats if stats on */ 3968 if( pArg && pArg->statsOn ){ 3969 display_stats(db, pArg, 0); 3970 } 3971 3972 /* print loop-counters if required */ 3973 if( pArg && pArg->scanstatsOn ){ 3974 display_scanstats(db, pArg); 3975 } 3976 3977 /* Finalize the statement just executed. If this fails, save a 3978 ** copy of the error message. Otherwise, set zSql to point to the 3979 ** next statement to execute. */ 3980 rc2 = sqlite3_finalize(pStmt); 3981 if( rc!=SQLITE_NOMEM ) rc = rc2; 3982 if( rc==SQLITE_OK ){ 3983 zSql = zLeftover; 3984 while( IsSpace(zSql[0]) ) zSql++; 3985 }else if( pzErrMsg ){ 3986 *pzErrMsg = save_err_msg(db, "stepping", rc, 0); 3987 } 3988 3989 /* clear saved stmt handle */ 3990 if( pArg ){ 3991 pArg->pStmt = NULL; 3992 } 3993 } 3994 } /* end while */ 3995 3996 return rc; 3997} 3998 3999/* 4000** Release memory previously allocated by tableColumnList(). 4001*/ 4002static void freeColumnList(char **azCol){ 4003 int i; 4004 for(i=1; azCol[i]; i++){ 4005 sqlite3_free(azCol[i]); 4006 } 4007 /* azCol[0] is a static string */ 4008 sqlite3_free(azCol); 4009} 4010 4011/* 4012** Return a list of pointers to strings which are the names of all 4013** columns in table zTab. The memory to hold the names is dynamically 4014** allocated and must be released by the caller using a subsequent call 4015** to freeColumnList(). 4016** 4017** The azCol[0] entry is usually NULL. However, if zTab contains a rowid 4018** value that needs to be preserved, then azCol[0] is filled in with the 4019** name of the rowid column. 4020** 4021** The first regular column in the table is azCol[1]. The list is terminated 4022** by an entry with azCol[i]==0. 4023*/ 4024static char **tableColumnList(ShellState *p, const char *zTab){ 4025 char **azCol = 0; 4026 sqlite3_stmt *pStmt; 4027 char *zSql; 4028 int nCol = 0; 4029 int nAlloc = 0; 4030 int nPK = 0; /* Number of PRIMARY KEY columns seen */ 4031 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ 4032 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); 4033 int rc; 4034 4035 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); 4036 shell_check_oom(zSql); 4037 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 4038 sqlite3_free(zSql); 4039 if( rc ) return 0; 4040 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 4041 if( nCol>=nAlloc-2 ){ 4042 nAlloc = nAlloc*2 + nCol + 10; 4043 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); 4044 shell_check_oom(azCol); 4045 } 4046 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); 4047 shell_check_oom(azCol[nCol]); 4048 if( sqlite3_column_int(pStmt, 5) ){ 4049 nPK++; 4050 if( nPK==1 4051 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), 4052 "INTEGER")==0 4053 ){ 4054 isIPK = 1; 4055 }else{ 4056 isIPK = 0; 4057 } 4058 } 4059 } 4060 sqlite3_finalize(pStmt); 4061 if( azCol==0 ) return 0; 4062 azCol[0] = 0; 4063 azCol[nCol+1] = 0; 4064 4065 /* The decision of whether or not a rowid really needs to be preserved 4066 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table 4067 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve 4068 ** rowids on tables where the rowid is inaccessible because there are other 4069 ** columns in the table named "rowid", "_rowid_", and "oid". 4070 */ 4071 if( preserveRowid && isIPK ){ 4072 /* If a single PRIMARY KEY column with type INTEGER was seen, then it 4073 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID 4074 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are 4075 ** ROWID aliases. To distinguish these cases, check to see if 4076 ** there is a "pk" entry in "PRAGMA index_list". There will be 4077 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. 4078 */ 4079 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" 4080 " WHERE origin='pk'", zTab); 4081 shell_check_oom(zSql); 4082 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 4083 sqlite3_free(zSql); 4084 if( rc ){ 4085 freeColumnList(azCol); 4086 return 0; 4087 } 4088 rc = sqlite3_step(pStmt); 4089 sqlite3_finalize(pStmt); 4090 preserveRowid = rc==SQLITE_ROW; 4091 } 4092 if( preserveRowid ){ 4093 /* Only preserve the rowid if we can find a name to use for the 4094 ** rowid */ 4095 static char *azRowid[] = { "rowid", "_rowid_", "oid" }; 4096 int i, j; 4097 for(j=0; j<3; j++){ 4098 for(i=1; i<=nCol; i++){ 4099 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; 4100 } 4101 if( i>nCol ){ 4102 /* At this point, we know that azRowid[j] is not the name of any 4103 ** ordinary column in the table. Verify that azRowid[j] is a valid 4104 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID 4105 ** tables will fail this last check */ 4106 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); 4107 if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; 4108 break; 4109 } 4110 } 4111 } 4112 return azCol; 4113} 4114 4115/* 4116** Toggle the reverse_unordered_selects setting. 4117*/ 4118static void toggleSelectOrder(sqlite3 *db){ 4119 sqlite3_stmt *pStmt = 0; 4120 int iSetting = 0; 4121 char zStmt[100]; 4122 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); 4123 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 4124 iSetting = sqlite3_column_int(pStmt, 0); 4125 } 4126 sqlite3_finalize(pStmt); 4127 sqlite3_snprintf(sizeof(zStmt), zStmt, 4128 "PRAGMA reverse_unordered_selects(%d)", !iSetting); 4129 sqlite3_exec(db, zStmt, 0, 0, 0); 4130} 4131 4132/* 4133** This is a different callback routine used for dumping the database. 4134** Each row received by this callback consists of a table name, 4135** the table type ("index" or "table") and SQL to create the table. 4136** This routine should print text sufficient to recreate the table. 4137*/ 4138static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ 4139 int rc; 4140 const char *zTable; 4141 const char *zType; 4142 const char *zSql; 4143 ShellState *p = (ShellState *)pArg; 4144 int dataOnly; 4145 int noSys; 4146 4147 UNUSED_PARAMETER(azNotUsed); 4148 if( nArg!=3 || azArg==0 ) return 0; 4149 zTable = azArg[0]; 4150 zType = azArg[1]; 4151 zSql = azArg[2]; 4152 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; 4153 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; 4154 4155 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){ 4156 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); 4157 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){ 4158 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 4159 }else if( strncmp(zTable, "sqlite_", 7)==0 ){ 4160 return 0; 4161 }else if( dataOnly ){ 4162 /* no-op */ 4163 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ 4164 char *zIns; 4165 if( !p->writableSchema ){ 4166 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); 4167 p->writableSchema = 1; 4168 } 4169 zIns = sqlite3_mprintf( 4170 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" 4171 "VALUES('table','%q','%q',0,'%q');", 4172 zTable, zTable, zSql); 4173 shell_check_oom(zIns); 4174 utf8_printf(p->out, "%s\n", zIns); 4175 sqlite3_free(zIns); 4176 return 0; 4177 }else{ 4178 printSchemaLine(p->out, zSql, ";\n"); 4179 } 4180 4181 if( strcmp(zType, "table")==0 ){ 4182 ShellText sSelect; 4183 ShellText sTable; 4184 char **azCol; 4185 int i; 4186 char *savedDestTable; 4187 int savedMode; 4188 4189 azCol = tableColumnList(p, zTable); 4190 if( azCol==0 ){ 4191 p->nErr++; 4192 return 0; 4193 } 4194 4195 /* Always quote the table name, even if it appears to be pure ascii, 4196 ** in case it is a keyword. Ex: INSERT INTO "table" ... */ 4197 initText(&sTable); 4198 appendText(&sTable, zTable, quoteChar(zTable)); 4199 /* If preserving the rowid, add a column list after the table name. 4200 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" 4201 ** instead of the usual "INSERT INTO tab VALUES(...)". 4202 */ 4203 if( azCol[0] ){ 4204 appendText(&sTable, "(", 0); 4205 appendText(&sTable, azCol[0], 0); 4206 for(i=1; azCol[i]; i++){ 4207 appendText(&sTable, ",", 0); 4208 appendText(&sTable, azCol[i], quoteChar(azCol[i])); 4209 } 4210 appendText(&sTable, ")", 0); 4211 } 4212 4213 /* Build an appropriate SELECT statement */ 4214 initText(&sSelect); 4215 appendText(&sSelect, "SELECT ", 0); 4216 if( azCol[0] ){ 4217 appendText(&sSelect, azCol[0], 0); 4218 appendText(&sSelect, ",", 0); 4219 } 4220 for(i=1; azCol[i]; i++){ 4221 appendText(&sSelect, azCol[i], quoteChar(azCol[i])); 4222 if( azCol[i+1] ){ 4223 appendText(&sSelect, ",", 0); 4224 } 4225 } 4226 freeColumnList(azCol); 4227 appendText(&sSelect, " FROM ", 0); 4228 appendText(&sSelect, zTable, quoteChar(zTable)); 4229 4230 savedDestTable = p->zDestTable; 4231 savedMode = p->mode; 4232 p->zDestTable = sTable.z; 4233 p->mode = p->cMode = MODE_Insert; 4234 rc = shell_exec(p, sSelect.z, 0); 4235 if( (rc&0xff)==SQLITE_CORRUPT ){ 4236 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 4237 toggleSelectOrder(p->db); 4238 shell_exec(p, sSelect.z, 0); 4239 toggleSelectOrder(p->db); 4240 } 4241 p->zDestTable = savedDestTable; 4242 p->mode = savedMode; 4243 freeText(&sTable); 4244 freeText(&sSelect); 4245 if( rc ) p->nErr++; 4246 } 4247 return 0; 4248} 4249 4250/* 4251** Run zQuery. Use dump_callback() as the callback routine so that 4252** the contents of the query are output as SQL statements. 4253** 4254** If we get a SQLITE_CORRUPT error, rerun the query after appending 4255** "ORDER BY rowid DESC" to the end. 4256*/ 4257static int run_schema_dump_query( 4258 ShellState *p, 4259 const char *zQuery 4260){ 4261 int rc; 4262 char *zErr = 0; 4263 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); 4264 if( rc==SQLITE_CORRUPT ){ 4265 char *zQ2; 4266 int len = strlen30(zQuery); 4267 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 4268 if( zErr ){ 4269 utf8_printf(p->out, "/****** %s ******/\n", zErr); 4270 sqlite3_free(zErr); 4271 zErr = 0; 4272 } 4273 zQ2 = malloc( len+100 ); 4274 if( zQ2==0 ) return rc; 4275 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); 4276 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); 4277 if( rc ){ 4278 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); 4279 }else{ 4280 rc = SQLITE_CORRUPT; 4281 } 4282 sqlite3_free(zErr); 4283 free(zQ2); 4284 } 4285 return rc; 4286} 4287 4288/* 4289** Text of help messages. 4290** 4291** The help text for each individual command begins with a line that starts 4292** with ".". Subsequent lines are supplemental information. 4293** 4294** There must be two or more spaces between the end of the command and the 4295** start of the description of what that command does. 4296*/ 4297static const char *(azHelp[]) = { 4298#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \ 4299 && !defined(SQLITE_SHELL_FIDDLE) 4300 ".archive ... Manage SQL archives", 4301 " Each command must have exactly one of the following options:", 4302 " -c, --create Create a new archive", 4303 " -u, --update Add or update files with changed mtime", 4304 " -i, --insert Like -u but always add even if unchanged", 4305 " -r, --remove Remove files from archive", 4306 " -t, --list List contents of archive", 4307 " -x, --extract Extract files from archive", 4308 " Optional arguments:", 4309 " -v, --verbose Print each filename as it is processed", 4310 " -f FILE, --file FILE Use archive FILE (default is current db)", 4311 " -a FILE, --append FILE Open FILE using the apndvfs VFS", 4312 " -C DIR, --directory DIR Read/extract files from directory DIR", 4313 " -g, --glob Use glob matching for names in archive", 4314 " -n, --dryrun Show the SQL that would have occurred", 4315 " Examples:", 4316 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", 4317 " .ar -tf ARCHIVE # List members of ARCHIVE", 4318 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", 4319 " See also:", 4320 " http://sqlite.org/cli.html#sqlite_archive_support", 4321#endif 4322#ifndef SQLITE_OMIT_AUTHORIZATION 4323 ".auth ON|OFF Show authorizer callbacks", 4324#endif 4325#ifndef SQLITE_SHELL_FIDDLE 4326 ".backup ?DB? FILE Backup DB (default \"main\") to FILE", 4327 " Options:", 4328 " --append Use the appendvfs", 4329 " --async Write to FILE without journal and fsync()", 4330#endif 4331 ".bail on|off Stop after hitting an error. Default OFF", 4332 ".binary on|off Turn binary output on or off. Default OFF", 4333#ifndef SQLITE_SHELL_FIDDLE 4334 ".cd DIRECTORY Change the working directory to DIRECTORY", 4335#endif 4336 ".changes on|off Show number of rows changed by SQL", 4337#ifndef SQLITE_SHELL_FIDDLE 4338 ".check GLOB Fail if output since .testcase does not match", 4339 ".clone NEWDB Clone data into NEWDB from the existing database", 4340#endif 4341 ".connection [close] [#] Open or close an auxiliary database connection", 4342 ".databases List names and files of attached databases", 4343 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", 4344#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 4345 ".dbinfo ?DB? Show status information about the database", 4346#endif 4347 ".dump ?OBJECTS? Render database content as SQL", 4348 " Options:", 4349 " --data-only Output only INSERT statements", 4350 " --newlines Allow unescaped newline characters in output", 4351 " --nosys Omit system tables (ex: \"sqlite_stat1\")", 4352 " --preserve-rowids Include ROWID values in the output", 4353 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump", 4354 " Additional LIKE patterns can be given in subsequent arguments", 4355 ".echo on|off Turn command echo on or off", 4356 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", 4357 " Other Modes:", 4358#ifdef SQLITE_DEBUG 4359 " test Show raw EXPLAIN QUERY PLAN output", 4360 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", 4361#endif 4362 " trigger Like \"full\" but also show trigger bytecode", 4363#ifndef SQLITE_SHELL_FIDDLE 4364 ".excel Display the output of next command in spreadsheet", 4365 " --bom Put a UTF8 byte-order mark on intermediate file", 4366#endif 4367#ifndef SQLITE_SHELL_FIDDLE 4368 ".exit ?CODE? Exit this program with return-code CODE", 4369#endif 4370 ".expert EXPERIMENTAL. Suggest indexes for queries", 4371 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", 4372 ".filectrl CMD ... Run various sqlite3_file_control() operations", 4373 " --schema SCHEMA Use SCHEMA instead of \"main\"", 4374 " --help Show CMD details", 4375 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", 4376 ".headers on|off Turn display of headers on or off", 4377 ".help ?-all? ?PATTERN? Show help text for PATTERN", 4378#ifndef SQLITE_SHELL_FIDDLE 4379 ".import FILE TABLE Import data from FILE into TABLE", 4380 " Options:", 4381 " --ascii Use \\037 and \\036 as column and row separators", 4382 " --csv Use , and \\n as column and row separators", 4383 " --skip N Skip the first N rows of input", 4384 " --schema S Target table to be S.TABLE", 4385 " -v \"Verbose\" - increase auxiliary output", 4386 " Notes:", 4387 " * If TABLE does not exist, it is created. The first row of input", 4388 " determines the column names.", 4389 " * If neither --csv or --ascii are used, the input mode is derived", 4390 " from the \".mode\" output mode", 4391 " * If FILE begins with \"|\" then it is a command that generates the", 4392 " input text.", 4393#endif 4394#ifndef SQLITE_OMIT_TEST_CONTROL 4395 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", 4396#endif 4397 ".indexes ?TABLE? Show names of indexes", 4398 " If TABLE is specified, only show indexes for", 4399 " tables matching TABLE using the LIKE operator.", 4400#ifdef SQLITE_ENABLE_IOTRACE 4401 ".iotrace FILE Enable I/O diagnostic logging to FILE", 4402#endif 4403 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", 4404 ".lint OPTIONS Report potential schema issues.", 4405 " Options:", 4406 " fkey-indexes Find missing foreign key indexes", 4407#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) 4408 ".load FILE ?ENTRY? Load an extension library", 4409#endif 4410#ifndef SQLITE_SHELL_FIDDLE 4411 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", 4412#endif 4413 ".mode MODE ?OPTIONS? Set output mode", 4414 " MODE is one of:", 4415 " ascii Columns/rows delimited by 0x1F and 0x1E", 4416 " box Tables using unicode box-drawing characters", 4417 " csv Comma-separated values", 4418 " column Output in columns. (See .width)", 4419 " html HTML <table> code", 4420 " insert SQL insert statements for TABLE", 4421 " json Results in a JSON array", 4422 " line One value per line", 4423 " list Values delimited by \"|\"", 4424 " markdown Markdown table format", 4425 " qbox Shorthand for \"box --width 60 --quote\"", 4426 " quote Escape answers as for SQL", 4427 " table ASCII-art table", 4428 " tabs Tab-separated values", 4429 " tcl TCL list elements", 4430 " OPTIONS: (for columnar modes or insert mode):", 4431 " --wrap N Wrap output lines to no longer than N characters", 4432 " --wordwrap B Wrap or not at word boundaries per B (on/off)", 4433 " --ww Shorthand for \"--wordwrap 1\"", 4434 " --quote Quote output text as SQL literals", 4435 " --noquote Do not quote output text", 4436 " TABLE The name of SQL table used for \"insert\" mode", 4437#ifndef SQLITE_SHELL_FIDDLE 4438 ".nonce STRING Suspend safe mode for one command if nonce matches", 4439#endif 4440 ".nullvalue STRING Use STRING in place of NULL values", 4441#ifndef SQLITE_SHELL_FIDDLE 4442 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", 4443 " If FILE begins with '|' then open as a pipe", 4444 " --bom Put a UTF8 byte-order mark at the beginning", 4445 " -e Send output to the system text editor", 4446 " -x Send output as CSV to a spreadsheet (same as \".excel\")", 4447 /* Note that .open is (partially) available in WASM builds but is 4448 ** currently only intended to be used by the fiddle tool, not 4449 ** end users, so is "undocumented." */ 4450 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", 4451 " Options:", 4452 " --append Use appendvfs to append database to the end of FILE", 4453#endif 4454#ifndef SQLITE_OMIT_DESERIALIZE 4455 " --deserialize Load into memory using sqlite3_deserialize()", 4456 " --hexdb Load the output of \"dbtotxt\" as an in-memory db", 4457 " --maxsize N Maximum size for --hexdb or --deserialized database", 4458#endif 4459 " --new Initialize FILE to an empty database", 4460 " --nofollow Do not follow symbolic links", 4461 " --readonly Open FILE readonly", 4462 " --zip FILE is a ZIP archive", 4463#ifndef SQLITE_SHELL_FIDDLE 4464 ".output ?FILE? Send output to FILE or stdout if FILE is omitted", 4465 " If FILE begins with '|' then open it as a pipe.", 4466 " Options:", 4467 " --bom Prefix output with a UTF8 byte-order mark", 4468 " -e Send output to the system text editor", 4469 " -x Send output as CSV to a spreadsheet", 4470#endif 4471 ".parameter CMD ... Manage SQL parameter bindings", 4472 " clear Erase all bindings", 4473 " init Initialize the TEMP table that holds bindings", 4474 " list List the current parameter bindings", 4475 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", 4476 " PARAMETER should start with one of: $ : @ ?", 4477 " unset PARAMETER Remove PARAMETER from the binding table", 4478 ".print STRING... Print literal STRING", 4479#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 4480 ".progress N Invoke progress handler after every N opcodes", 4481 " --limit N Interrupt after N progress callbacks", 4482 " --once Do no more than one progress interrupt", 4483 " --quiet|-q No output except at interrupts", 4484 " --reset Reset the count for each input and interrupt", 4485#endif 4486 ".prompt MAIN CONTINUE Replace the standard prompts", 4487#ifndef SQLITE_SHELL_FIDDLE 4488 ".quit Exit this program", 4489 ".read FILE Read input from FILE or command output", 4490 " If FILE begins with \"|\", it is a command that generates the input.", 4491#endif 4492#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 4493 ".recover Recover as much data as possible from corrupt db.", 4494 " --freelist-corrupt Assume the freelist is corrupt", 4495 " --recovery-db NAME Store recovery metadata in database file NAME", 4496 " --lost-and-found TABLE Alternative name for the lost-and-found table", 4497 " --no-rowids Do not attempt to recover rowid values", 4498 " that are not also INTEGER PRIMARY KEYs", 4499#endif 4500#ifndef SQLITE_SHELL_FIDDLE 4501 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", 4502 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", 4503#endif 4504 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", 4505 ".schema ?PATTERN? Show the CREATE statements matching PATTERN", 4506 " Options:", 4507 " --indent Try to pretty-print the schema", 4508 " --nosys Omit objects whose names start with \"sqlite_\"", 4509 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", 4510 " Options:", 4511 " --init Create a new SELFTEST table", 4512 " -v Verbose output", 4513 ".separator COL ?ROW? Change the column and row separators", 4514#if defined(SQLITE_ENABLE_SESSION) 4515 ".session ?NAME? CMD ... Create or control sessions", 4516 " Subcommands:", 4517 " attach TABLE Attach TABLE", 4518 " changeset FILE Write a changeset into FILE", 4519 " close Close one session", 4520 " enable ?BOOLEAN? Set or query the enable bit", 4521 " filter GLOB... Reject tables matching GLOBs", 4522 " indirect ?BOOLEAN? Mark or query the indirect status", 4523 " isempty Query whether the session is empty", 4524 " list List currently open session names", 4525 " open DB NAME Open a new session on DB", 4526 " patchset FILE Write a patchset into FILE", 4527 " If ?NAME? is omitted, the first defined session is used.", 4528#endif 4529 ".sha3sum ... Compute a SHA3 hash of database content", 4530 " Options:", 4531 " --schema Also hash the sqlite_schema table", 4532 " --sha3-224 Use the sha3-224 algorithm", 4533 " --sha3-256 Use the sha3-256 algorithm (default)", 4534 " --sha3-384 Use the sha3-384 algorithm", 4535 " --sha3-512 Use the sha3-512 algorithm", 4536 " Any other argument is a LIKE pattern for tables to hash", 4537#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 4538 ".shell CMD ARGS... Run CMD ARGS... in a system shell", 4539#endif 4540 ".show Show the current values for various settings", 4541 ".stats ?ARG? Show stats or turn stats on or off", 4542 " off Turn off automatic stat display", 4543 " on Turn on automatic stat display", 4544 " stmt Show statement stats", 4545 " vmstep Show the virtual machine step count only", 4546#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 4547 ".system CMD ARGS... Run CMD ARGS... in a system shell", 4548#endif 4549 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", 4550#ifndef SQLITE_SHELL_FIDDLE 4551 ".testcase NAME Begin redirecting output to 'testcase-out.txt'", 4552#endif 4553 ".testctrl CMD ... Run various sqlite3_test_control() operations", 4554 " Run \".testctrl\" with no arguments for details", 4555 ".timeout MS Try opening locked tables for MS milliseconds", 4556 ".timer on|off Turn SQL timer on or off", 4557#ifndef SQLITE_OMIT_TRACE 4558 ".trace ?OPTIONS? Output each SQL statement as it is run", 4559 " FILE Send output to FILE", 4560 " stdout Send output to stdout", 4561 " stderr Send output to stderr", 4562 " off Disable tracing", 4563 " --expanded Expand query parameters", 4564#ifdef SQLITE_ENABLE_NORMALIZE 4565 " --normalized Normal the SQL statements", 4566#endif 4567 " --plain Show SQL as it is input", 4568 " --stmt Trace statement execution (SQLITE_TRACE_STMT)", 4569 " --profile Profile statements (SQLITE_TRACE_PROFILE)", 4570 " --row Trace each row (SQLITE_TRACE_ROW)", 4571 " --close Trace connection close (SQLITE_TRACE_CLOSE)", 4572#endif /* SQLITE_OMIT_TRACE */ 4573#ifdef SQLITE_DEBUG 4574 ".unmodule NAME ... Unregister virtual table modules", 4575 " --allexcept Unregister everything except those named", 4576#endif 4577 ".vfsinfo ?AUX? Information about the top-level VFS", 4578 ".vfslist List all available VFSes", 4579 ".vfsname ?AUX? Print the name of the VFS stack", 4580 ".width NUM1 NUM2 ... Set minimum column widths for columnar output", 4581 " Negative values right-justify", 4582}; 4583 4584/* 4585** Output help text. 4586** 4587** zPattern describes the set of commands for which help text is provided. 4588** If zPattern is NULL, then show all commands, but only give a one-line 4589** description of each. 4590** 4591** Return the number of matches. 4592*/ 4593static int showHelp(FILE *out, const char *zPattern){ 4594 int i = 0; 4595 int j = 0; 4596 int n = 0; 4597 char *zPat; 4598 if( zPattern==0 4599 || zPattern[0]=='0' 4600 || strcmp(zPattern,"-a")==0 4601 || strcmp(zPattern,"-all")==0 4602 || strcmp(zPattern,"--all")==0 4603 ){ 4604 /* Show all commands, but only one line per command */ 4605 if( zPattern==0 ) zPattern = ""; 4606 for(i=0; i<ArraySize(azHelp); i++){ 4607 if( azHelp[i][0]=='.' || zPattern[0] ){ 4608 utf8_printf(out, "%s\n", azHelp[i]); 4609 n++; 4610 } 4611 } 4612 }else{ 4613 /* Look for commands that for which zPattern is an exact prefix */ 4614 zPat = sqlite3_mprintf(".%s*", zPattern); 4615 shell_check_oom(zPat); 4616 for(i=0; i<ArraySize(azHelp); i++){ 4617 if( sqlite3_strglob(zPat, azHelp[i])==0 ){ 4618 utf8_printf(out, "%s\n", azHelp[i]); 4619 j = i+1; 4620 n++; 4621 } 4622 } 4623 sqlite3_free(zPat); 4624 if( n ){ 4625 if( n==1 ){ 4626 /* when zPattern is a prefix of exactly one command, then include the 4627 ** details of that command, which should begin at offset j */ 4628 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ 4629 utf8_printf(out, "%s\n", azHelp[j]); 4630 j++; 4631 } 4632 } 4633 return n; 4634 } 4635 /* Look for commands that contain zPattern anywhere. Show the complete 4636 ** text of all commands that match. */ 4637 zPat = sqlite3_mprintf("%%%s%%", zPattern); 4638 shell_check_oom(zPat); 4639 for(i=0; i<ArraySize(azHelp); i++){ 4640 if( azHelp[i][0]=='.' ) j = i; 4641 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ 4642 utf8_printf(out, "%s\n", azHelp[j]); 4643 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ 4644 j++; 4645 utf8_printf(out, "%s\n", azHelp[j]); 4646 } 4647 i = j; 4648 n++; 4649 } 4650 } 4651 sqlite3_free(zPat); 4652 } 4653 return n; 4654} 4655 4656/* Forward reference */ 4657static int process_input(ShellState *p); 4658 4659/* 4660** Read the content of file zName into memory obtained from sqlite3_malloc64() 4661** and return a pointer to the buffer. The caller is responsible for freeing 4662** the memory. 4663** 4664** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes 4665** read. 4666** 4667** For convenience, a nul-terminator byte is always appended to the data read 4668** from the file before the buffer is returned. This byte is not included in 4669** the final value of (*pnByte), if applicable. 4670** 4671** NULL is returned if any error is encountered. The final value of *pnByte 4672** is undefined in this case. 4673*/ 4674static char *readFile(const char *zName, int *pnByte){ 4675 FILE *in = fopen(zName, "rb"); 4676 long nIn; 4677 size_t nRead; 4678 char *pBuf; 4679 if( in==0 ) return 0; 4680 fseek(in, 0, SEEK_END); 4681 nIn = ftell(in); 4682 rewind(in); 4683 pBuf = sqlite3_malloc64( nIn+1 ); 4684 if( pBuf==0 ){ fclose(in); return 0; } 4685 nRead = fread(pBuf, nIn, 1, in); 4686 fclose(in); 4687 if( nRead!=1 ){ 4688 sqlite3_free(pBuf); 4689 return 0; 4690 } 4691 pBuf[nIn] = 0; 4692 if( pnByte ) *pnByte = nIn; 4693 return pBuf; 4694} 4695 4696#if defined(SQLITE_ENABLE_SESSION) 4697/* 4698** Close a single OpenSession object and release all of its associated 4699** resources. 4700*/ 4701static void session_close(OpenSession *pSession){ 4702 int i; 4703 sqlite3session_delete(pSession->p); 4704 sqlite3_free(pSession->zName); 4705 for(i=0; i<pSession->nFilter; i++){ 4706 sqlite3_free(pSession->azFilter[i]); 4707 } 4708 sqlite3_free(pSession->azFilter); 4709 memset(pSession, 0, sizeof(OpenSession)); 4710} 4711#endif 4712 4713/* 4714** Close all OpenSession objects and release all associated resources. 4715*/ 4716#if defined(SQLITE_ENABLE_SESSION) 4717static void session_close_all(ShellState *p, int i){ 4718 int j; 4719 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; 4720 for(j=0; j<pAuxDb->nSession; j++){ 4721 session_close(&pAuxDb->aSession[j]); 4722 } 4723 pAuxDb->nSession = 0; 4724} 4725#else 4726# define session_close_all(X,Y) 4727#endif 4728 4729/* 4730** Implementation of the xFilter function for an open session. Omit 4731** any tables named by ".session filter" but let all other table through. 4732*/ 4733#if defined(SQLITE_ENABLE_SESSION) 4734static int session_filter(void *pCtx, const char *zTab){ 4735 OpenSession *pSession = (OpenSession*)pCtx; 4736 int i; 4737 for(i=0; i<pSession->nFilter; i++){ 4738 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; 4739 } 4740 return 1; 4741} 4742#endif 4743 4744/* 4745** Try to deduce the type of file for zName based on its content. Return 4746** one of the SHELL_OPEN_* constants. 4747** 4748** If the file does not exist or is empty but its name looks like a ZIP 4749** archive and the dfltZip flag is true, then assume it is a ZIP archive. 4750** Otherwise, assume an ordinary database regardless of the filename if 4751** the type cannot be determined from content. 4752*/ 4753int deduceDatabaseType(const char *zName, int dfltZip){ 4754 FILE *f = fopen(zName, "rb"); 4755 size_t n; 4756 int rc = SHELL_OPEN_UNSPEC; 4757 char zBuf[100]; 4758 if( f==0 ){ 4759 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4760 return SHELL_OPEN_ZIPFILE; 4761 }else{ 4762 return SHELL_OPEN_NORMAL; 4763 } 4764 } 4765 n = fread(zBuf, 16, 1, f); 4766 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ 4767 fclose(f); 4768 return SHELL_OPEN_NORMAL; 4769 } 4770 fseek(f, -25, SEEK_END); 4771 n = fread(zBuf, 25, 1, f); 4772 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ 4773 rc = SHELL_OPEN_APPENDVFS; 4774 }else{ 4775 fseek(f, -22, SEEK_END); 4776 n = fread(zBuf, 22, 1, f); 4777 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 4778 && zBuf[3]==0x06 ){ 4779 rc = SHELL_OPEN_ZIPFILE; 4780 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4781 rc = SHELL_OPEN_ZIPFILE; 4782 } 4783 } 4784 fclose(f); 4785 return rc; 4786} 4787 4788#ifndef SQLITE_OMIT_DESERIALIZE 4789/* 4790** Reconstruct an in-memory database using the output from the "dbtotxt" 4791** program. Read content from the file in p->aAuxDb[].zDbFilename. 4792** If p->aAuxDb[].zDbFilename is 0, then read from standard input. 4793*/ 4794static unsigned char *readHexDb(ShellState *p, int *pnData){ 4795 unsigned char *a = 0; 4796 int nLine; 4797 int n = 0; 4798 int pgsz = 0; 4799 int iOffset = 0; 4800 int j, k; 4801 int rc; 4802 FILE *in; 4803 const char *zDbFilename = p->pAuxDb->zDbFilename; 4804 unsigned int x[16]; 4805 char zLine[1000]; 4806 if( zDbFilename ){ 4807 in = fopen(zDbFilename, "r"); 4808 if( in==0 ){ 4809 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename); 4810 return 0; 4811 } 4812 nLine = 0; 4813 }else{ 4814 in = p->in; 4815 nLine = p->lineno; 4816 if( in==0 ) in = stdin; 4817 } 4818 *pnData = 0; 4819 nLine++; 4820 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; 4821 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); 4822 if( rc!=2 ) goto readHexDb_error; 4823 if( n<0 ) goto readHexDb_error; 4824 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; 4825 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ 4826 a = sqlite3_malloc( n ? n : 1 ); 4827 shell_check_oom(a); 4828 memset(a, 0, n); 4829 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ 4830 utf8_printf(stderr, "invalid pagesize\n"); 4831 goto readHexDb_error; 4832 } 4833 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ 4834 rc = sscanf(zLine, "| page %d offset %d", &j, &k); 4835 if( rc==2 ){ 4836 iOffset = k; 4837 continue; 4838 } 4839 if( strncmp(zLine, "| end ", 6)==0 ){ 4840 break; 4841 } 4842 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 4843 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], 4844 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); 4845 if( rc==17 ){ 4846 k = iOffset+j; 4847 if( k+16<=n && k>=0 ){ 4848 int ii; 4849 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; 4850 } 4851 } 4852 } 4853 *pnData = n; 4854 if( in!=p->in ){ 4855 fclose(in); 4856 }else{ 4857 p->lineno = nLine; 4858 } 4859 return a; 4860 4861readHexDb_error: 4862 if( in!=p->in ){ 4863 fclose(in); 4864 }else{ 4865 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ 4866 nLine++; 4867 if(strncmp(zLine, "| end ", 6)==0 ) break; 4868 } 4869 p->lineno = nLine; 4870 } 4871 sqlite3_free(a); 4872 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); 4873 return 0; 4874} 4875#endif /* SQLITE_OMIT_DESERIALIZE */ 4876 4877/* 4878** Scalar function "shell_int32". The first argument to this function 4879** must be a blob. The second a non-negative integer. This function 4880** reads and returns a 32-bit big-endian integer from byte 4881** offset (4*<arg2>) of the blob. 4882*/ 4883static void shellInt32( 4884 sqlite3_context *context, 4885 int argc, 4886 sqlite3_value **argv 4887){ 4888 const unsigned char *pBlob; 4889 int nBlob; 4890 int iInt; 4891 4892 UNUSED_PARAMETER(argc); 4893 nBlob = sqlite3_value_bytes(argv[0]); 4894 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); 4895 iInt = sqlite3_value_int(argv[1]); 4896 4897 if( iInt>=0 && (iInt+1)*4<=nBlob ){ 4898 const unsigned char *a = &pBlob[iInt*4]; 4899 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) 4900 + ((sqlite3_int64)a[1]<<16) 4901 + ((sqlite3_int64)a[2]<< 8) 4902 + ((sqlite3_int64)a[3]<< 0); 4903 sqlite3_result_int64(context, iVal); 4904 } 4905} 4906 4907/* 4908** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, 4909** using "..." with internal double-quote characters doubled. 4910*/ 4911static void shellIdQuote( 4912 sqlite3_context *context, 4913 int argc, 4914 sqlite3_value **argv 4915){ 4916 const char *zName = (const char*)sqlite3_value_text(argv[0]); 4917 UNUSED_PARAMETER(argc); 4918 if( zName ){ 4919 char *z = sqlite3_mprintf("\"%w\"", zName); 4920 sqlite3_result_text(context, z, -1, sqlite3_free); 4921 } 4922} 4923 4924/* 4925** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X. 4926*/ 4927static void shellUSleepFunc( 4928 sqlite3_context *context, 4929 int argcUnused, 4930 sqlite3_value **argv 4931){ 4932 int sleep = sqlite3_value_int(argv[0]); 4933 (void)argcUnused; 4934 sqlite3_sleep(sleep/1000); 4935 sqlite3_result_int(context, sleep); 4936} 4937 4938/* 4939** Scalar function "shell_escape_crnl" used by the .recover command. 4940** The argument passed to this function is the output of built-in 4941** function quote(). If the first character of the input is "'", 4942** indicating that the value passed to quote() was a text value, 4943** then this function searches the input for "\n" and "\r" characters 4944** and adds a wrapper similar to the following: 4945** 4946** replace(replace(<input>, '\n', char(10), '\r', char(13)); 4947** 4948** Or, if the first character of the input is not "'", then a copy 4949** of the input is returned. 4950*/ 4951static void shellEscapeCrnl( 4952 sqlite3_context *context, 4953 int argc, 4954 sqlite3_value **argv 4955){ 4956 const char *zText = (const char*)sqlite3_value_text(argv[0]); 4957 UNUSED_PARAMETER(argc); 4958 if( zText && zText[0]=='\'' ){ 4959 i64 nText = sqlite3_value_bytes(argv[0]); 4960 i64 i; 4961 char zBuf1[20]; 4962 char zBuf2[20]; 4963 const char *zNL = 0; 4964 const char *zCR = 0; 4965 i64 nCR = 0; 4966 i64 nNL = 0; 4967 4968 for(i=0; zText[i]; i++){ 4969 if( zNL==0 && zText[i]=='\n' ){ 4970 zNL = unused_string(zText, "\\n", "\\012", zBuf1); 4971 nNL = strlen(zNL); 4972 } 4973 if( zCR==0 && zText[i]=='\r' ){ 4974 zCR = unused_string(zText, "\\r", "\\015", zBuf2); 4975 nCR = strlen(zCR); 4976 } 4977 } 4978 4979 if( zNL || zCR ){ 4980 i64 iOut = 0; 4981 i64 nMax = (nNL > nCR) ? nNL : nCR; 4982 i64 nAlloc = nMax * nText + (nMax+64)*2; 4983 char *zOut = (char*)sqlite3_malloc64(nAlloc); 4984 if( zOut==0 ){ 4985 sqlite3_result_error_nomem(context); 4986 return; 4987 } 4988 4989 if( zNL && zCR ){ 4990 memcpy(&zOut[iOut], "replace(replace(", 16); 4991 iOut += 16; 4992 }else{ 4993 memcpy(&zOut[iOut], "replace(", 8); 4994 iOut += 8; 4995 } 4996 for(i=0; zText[i]; i++){ 4997 if( zText[i]=='\n' ){ 4998 memcpy(&zOut[iOut], zNL, nNL); 4999 iOut += nNL; 5000 }else if( zText[i]=='\r' ){ 5001 memcpy(&zOut[iOut], zCR, nCR); 5002 iOut += nCR; 5003 }else{ 5004 zOut[iOut] = zText[i]; 5005 iOut++; 5006 } 5007 } 5008 5009 if( zNL ){ 5010 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 5011 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; 5012 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; 5013 } 5014 if( zCR ){ 5015 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 5016 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; 5017 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; 5018 } 5019 5020 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); 5021 sqlite3_free(zOut); 5022 return; 5023 } 5024 } 5025 5026 sqlite3_result_value(context, argv[0]); 5027} 5028 5029/* Flags for open_db(). 5030** 5031** The default behavior of open_db() is to exit(1) if the database fails to 5032** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error 5033** but still returns without calling exit. 5034** 5035** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a 5036** ZIP archive if the file does not exist or is empty and its name matches 5037** the *.zip pattern. 5038*/ 5039#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ 5040#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ 5041 5042/* 5043** Make sure the database is open. If it is not, then open it. If 5044** the database fails to open, print an error message and exit. 5045*/ 5046static void open_db(ShellState *p, int openFlags){ 5047 if( p->db==0 ){ 5048 const char *zDbFilename = p->pAuxDb->zDbFilename; 5049 if( p->openMode==SHELL_OPEN_UNSPEC ){ 5050 if( zDbFilename==0 || zDbFilename[0]==0 ){ 5051 p->openMode = SHELL_OPEN_NORMAL; 5052 }else{ 5053 p->openMode = (u8)deduceDatabaseType(zDbFilename, 5054 (openFlags & OPEN_DB_ZIPFILE)!=0); 5055 } 5056 } 5057 switch( p->openMode ){ 5058 case SHELL_OPEN_APPENDVFS: { 5059 sqlite3_open_v2(zDbFilename, &p->db, 5060 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); 5061 break; 5062 } 5063 case SHELL_OPEN_HEXDB: 5064 case SHELL_OPEN_DESERIALIZE: { 5065 sqlite3_open(0, &p->db); 5066 break; 5067 } 5068 case SHELL_OPEN_ZIPFILE: { 5069 sqlite3_open(":memory:", &p->db); 5070 break; 5071 } 5072 case SHELL_OPEN_READONLY: { 5073 sqlite3_open_v2(zDbFilename, &p->db, 5074 SQLITE_OPEN_READONLY|p->openFlags, 0); 5075 break; 5076 } 5077 case SHELL_OPEN_UNSPEC: 5078 case SHELL_OPEN_NORMAL: { 5079 sqlite3_open_v2(zDbFilename, &p->db, 5080 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); 5081 break; 5082 } 5083 } 5084 globalDb = p->db; 5085 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ 5086 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", 5087 zDbFilename, sqlite3_errmsg(p->db)); 5088 if( openFlags & OPEN_DB_KEEPALIVE ){ 5089 sqlite3_open(":memory:", &p->db); 5090 return; 5091 } 5092 exit(1); 5093 } 5094#ifndef SQLITE_OMIT_LOAD_EXTENSION 5095 sqlite3_enable_load_extension(p->db, 1); 5096#endif 5097 sqlite3_shathree_init(p->db, 0, 0); 5098 sqlite3_uint_init(p->db, 0, 0); 5099 sqlite3_decimal_init(p->db, 0, 0); 5100 sqlite3_regexp_init(p->db, 0, 0); 5101 sqlite3_ieee_init(p->db, 0, 0); 5102 sqlite3_series_init(p->db, 0, 0); 5103#ifndef SQLITE_SHELL_FIDDLE 5104 sqlite3_fileio_init(p->db, 0, 0); 5105 sqlite3_completion_init(p->db, 0, 0); 5106#endif 5107#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 5108 sqlite3_dbdata_init(p->db, 0, 0); 5109#endif 5110#ifdef SQLITE_HAVE_ZLIB 5111 if( !p->bSafeModePersist ){ 5112 sqlite3_zipfile_init(p->db, 0, 0); 5113 sqlite3_sqlar_init(p->db, 0, 0); 5114 } 5115#endif 5116 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, 5117 shellAddSchemaName, 0, 0); 5118 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, 5119 shellModuleSchema, 0, 0); 5120 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, 5121 shellPutsFunc, 0, 0); 5122 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, 5123 shellEscapeCrnl, 0, 0); 5124 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, 5125 shellInt32, 0, 0); 5126 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, 5127 shellIdQuote, 0, 0); 5128 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, 5129 shellUSleepFunc, 0, 0); 5130#ifndef SQLITE_NOHAVE_SYSTEM 5131 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, 5132 editFunc, 0, 0); 5133 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, 5134 editFunc, 0, 0); 5135#endif 5136 if( p->openMode==SHELL_OPEN_ZIPFILE ){ 5137 char *zSql = sqlite3_mprintf( 5138 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename); 5139 shell_check_oom(zSql); 5140 sqlite3_exec(p->db, zSql, 0, 0, 0); 5141 sqlite3_free(zSql); 5142 } 5143#ifndef SQLITE_OMIT_DESERIALIZE 5144 else 5145 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ 5146 int rc; 5147 int nData = 0; 5148 unsigned char *aData; 5149 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ 5150 aData = (unsigned char*)readFile(zDbFilename, &nData); 5151 }else{ 5152 aData = readHexDb(p, &nData); 5153 if( aData==0 ){ 5154 return; 5155 } 5156 } 5157 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, 5158 SQLITE_DESERIALIZE_RESIZEABLE | 5159 SQLITE_DESERIALIZE_FREEONCLOSE); 5160 if( rc ){ 5161 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); 5162 } 5163 if( p->szMax>0 ){ 5164 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); 5165 } 5166 } 5167#endif 5168 } 5169 if( p->bSafeModePersist && p->db!=0 ){ 5170 sqlite3_set_authorizer(p->db, safeModeAuth, p); 5171 } 5172} 5173 5174/* 5175** Attempt to close the databaes connection. Report errors. 5176*/ 5177void close_db(sqlite3 *db){ 5178 int rc = sqlite3_close(db); 5179 if( rc ){ 5180 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", 5181 rc, sqlite3_errmsg(db)); 5182 } 5183} 5184 5185#if HAVE_READLINE || HAVE_EDITLINE 5186/* 5187** Readline completion callbacks 5188*/ 5189static char *readline_completion_generator(const char *text, int state){ 5190 static sqlite3_stmt *pStmt = 0; 5191 char *zRet; 5192 if( state==0 ){ 5193 char *zSql; 5194 sqlite3_finalize(pStmt); 5195 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 5196 " FROM completion(%Q) ORDER BY 1", text); 5197 shell_check_oom(zSql); 5198 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 5199 sqlite3_free(zSql); 5200 } 5201 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 5202 const char *z = (const char*)sqlite3_column_text(pStmt,0); 5203 zRet = z ? strdup(z) : 0; 5204 }else{ 5205 sqlite3_finalize(pStmt); 5206 pStmt = 0; 5207 zRet = 0; 5208 } 5209 return zRet; 5210} 5211static char **readline_completion(const char *zText, int iStart, int iEnd){ 5212 rl_attempted_completion_over = 1; 5213 return rl_completion_matches(zText, readline_completion_generator); 5214} 5215 5216#elif HAVE_LINENOISE 5217/* 5218** Linenoise completion callback 5219*/ 5220static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ 5221 i64 nLine = strlen(zLine); 5222 i64 i, iStart; 5223 sqlite3_stmt *pStmt = 0; 5224 char *zSql; 5225 char zBuf[1000]; 5226 5227 if( nLine>sizeof(zBuf)-30 ) return; 5228 if( zLine[0]=='.' || zLine[0]=='#') return; 5229 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} 5230 if( i==nLine-1 ) return; 5231 iStart = i+1; 5232 memcpy(zBuf, zLine, iStart); 5233 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 5234 " FROM completion(%Q,%Q) ORDER BY 1", 5235 &zLine[iStart], zLine); 5236 shell_check_oom(zSql); 5237 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 5238 sqlite3_free(zSql); 5239 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ 5240 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 5241 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); 5242 int nCompletion = sqlite3_column_bytes(pStmt, 0); 5243 if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){ 5244 memcpy(zBuf+iStart, zCompletion, nCompletion+1); 5245 linenoiseAddCompletion(lc, zBuf); 5246 } 5247 } 5248 sqlite3_finalize(pStmt); 5249} 5250#endif 5251 5252/* 5253** Do C-language style dequoting. 5254** 5255** \a -> alarm 5256** \b -> backspace 5257** \t -> tab 5258** \n -> newline 5259** \v -> vertical tab 5260** \f -> form feed 5261** \r -> carriage return 5262** \s -> space 5263** \" -> " 5264** \' -> ' 5265** \\ -> backslash 5266** \NNN -> ascii character NNN in octal 5267*/ 5268static void resolve_backslashes(char *z){ 5269 int i, j; 5270 char c; 5271 while( *z && *z!='\\' ) z++; 5272 for(i=j=0; (c = z[i])!=0; i++, j++){ 5273 if( c=='\\' && z[i+1]!=0 ){ 5274 c = z[++i]; 5275 if( c=='a' ){ 5276 c = '\a'; 5277 }else if( c=='b' ){ 5278 c = '\b'; 5279 }else if( c=='t' ){ 5280 c = '\t'; 5281 }else if( c=='n' ){ 5282 c = '\n'; 5283 }else if( c=='v' ){ 5284 c = '\v'; 5285 }else if( c=='f' ){ 5286 c = '\f'; 5287 }else if( c=='r' ){ 5288 c = '\r'; 5289 }else if( c=='"' ){ 5290 c = '"'; 5291 }else if( c=='\'' ){ 5292 c = '\''; 5293 }else if( c=='\\' ){ 5294 c = '\\'; 5295 }else if( c>='0' && c<='7' ){ 5296 c -= '0'; 5297 if( z[i+1]>='0' && z[i+1]<='7' ){ 5298 i++; 5299 c = (c<<3) + z[i] - '0'; 5300 if( z[i+1]>='0' && z[i+1]<='7' ){ 5301 i++; 5302 c = (c<<3) + z[i] - '0'; 5303 } 5304 } 5305 } 5306 } 5307 z[j] = c; 5308 } 5309 if( j<i ) z[j] = 0; 5310} 5311 5312/* 5313** Interpret zArg as either an integer or a boolean value. Return 1 or 0 5314** for TRUE and FALSE. Return the integer value if appropriate. 5315*/ 5316static int booleanValue(const char *zArg){ 5317 int i; 5318 if( zArg[0]=='0' && zArg[1]=='x' ){ 5319 for(i=2; hexDigitValue(zArg[i])>=0; i++){} 5320 }else{ 5321 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} 5322 } 5323 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); 5324 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ 5325 return 1; 5326 } 5327 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ 5328 return 0; 5329 } 5330 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", 5331 zArg); 5332 return 0; 5333} 5334 5335/* 5336** Set or clear a shell flag according to a boolean value. 5337*/ 5338static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ 5339 if( booleanValue(zArg) ){ 5340 ShellSetFlag(p, mFlag); 5341 }else{ 5342 ShellClearFlag(p, mFlag); 5343 } 5344} 5345 5346/* 5347** Close an output file, assuming it is not stderr or stdout 5348*/ 5349static void output_file_close(FILE *f){ 5350 if( f && f!=stdout && f!=stderr ) fclose(f); 5351} 5352 5353/* 5354** Try to open an output file. The names "stdout" and "stderr" are 5355** recognized and do the right thing. NULL is returned if the output 5356** filename is "off". 5357*/ 5358static FILE *output_file_open(const char *zFile, int bTextMode){ 5359 FILE *f; 5360 if( strcmp(zFile,"stdout")==0 ){ 5361 f = stdout; 5362 }else if( strcmp(zFile, "stderr")==0 ){ 5363 f = stderr; 5364 }else if( strcmp(zFile, "off")==0 ){ 5365 f = 0; 5366 }else{ 5367 f = fopen(zFile, bTextMode ? "w" : "wb"); 5368 if( f==0 ){ 5369 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 5370 } 5371 } 5372 return f; 5373} 5374 5375#ifndef SQLITE_OMIT_TRACE 5376/* 5377** A routine for handling output from sqlite3_trace(). 5378*/ 5379static int sql_trace_callback( 5380 unsigned mType, /* The trace type */ 5381 void *pArg, /* The ShellState pointer */ 5382 void *pP, /* Usually a pointer to sqlite_stmt */ 5383 void *pX /* Auxiliary output */ 5384){ 5385 ShellState *p = (ShellState*)pArg; 5386 sqlite3_stmt *pStmt; 5387 const char *zSql; 5388 i64 nSql; 5389 if( p->traceOut==0 ) return 0; 5390 if( mType==SQLITE_TRACE_CLOSE ){ 5391 utf8_printf(p->traceOut, "-- closing database connection\n"); 5392 return 0; 5393 } 5394 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ 5395 zSql = (const char*)pX; 5396 }else{ 5397 pStmt = (sqlite3_stmt*)pP; 5398 switch( p->eTraceType ){ 5399 case SHELL_TRACE_EXPANDED: { 5400 zSql = sqlite3_expanded_sql(pStmt); 5401 break; 5402 } 5403#ifdef SQLITE_ENABLE_NORMALIZE 5404 case SHELL_TRACE_NORMALIZED: { 5405 zSql = sqlite3_normalized_sql(pStmt); 5406 break; 5407 } 5408#endif 5409 default: { 5410 zSql = sqlite3_sql(pStmt); 5411 break; 5412 } 5413 } 5414 } 5415 if( zSql==0 ) return 0; 5416 nSql = strlen(zSql); 5417 if( nSql>1000000000 ) nSql = 1000000000; 5418 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } 5419 switch( mType ){ 5420 case SQLITE_TRACE_ROW: 5421 case SQLITE_TRACE_STMT: { 5422 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql); 5423 break; 5424 } 5425 case SQLITE_TRACE_PROFILE: { 5426 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; 5427 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec); 5428 break; 5429 } 5430 } 5431 return 0; 5432} 5433#endif 5434 5435/* 5436** A no-op routine that runs with the ".breakpoint" doc-command. This is 5437** a useful spot to set a debugger breakpoint. 5438*/ 5439static void test_breakpoint(void){ 5440 static int nCall = 0; 5441 nCall++; 5442} 5443 5444/* 5445** An object used to read a CSV and other files for import. 5446*/ 5447typedef struct ImportCtx ImportCtx; 5448struct ImportCtx { 5449 const char *zFile; /* Name of the input file */ 5450 FILE *in; /* Read the CSV text from this input stream */ 5451 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ 5452 char *z; /* Accumulated text for a field */ 5453 int n; /* Number of bytes in z */ 5454 int nAlloc; /* Space allocated for z[] */ 5455 int nLine; /* Current line number */ 5456 int nRow; /* Number of rows imported */ 5457 int nErr; /* Number of errors encountered */ 5458 int bNotFirst; /* True if one or more bytes already read */ 5459 int cTerm; /* Character that terminated the most recent field */ 5460 int cColSep; /* The column separator character. (Usually ",") */ 5461 int cRowSep; /* The row separator character. (Usually "\n") */ 5462}; 5463 5464/* Clean up resourced used by an ImportCtx */ 5465static void import_cleanup(ImportCtx *p){ 5466 if( p->in!=0 && p->xCloser!=0 ){ 5467 p->xCloser(p->in); 5468 p->in = 0; 5469 } 5470 sqlite3_free(p->z); 5471 p->z = 0; 5472} 5473 5474/* Append a single byte to z[] */ 5475static void import_append_char(ImportCtx *p, int c){ 5476 if( p->n+1>=p->nAlloc ){ 5477 p->nAlloc += p->nAlloc + 100; 5478 p->z = sqlite3_realloc64(p->z, p->nAlloc); 5479 shell_check_oom(p->z); 5480 } 5481 p->z[p->n++] = (char)c; 5482} 5483 5484/* Read a single field of CSV text. Compatible with rfc4180 and extended 5485** with the option of having a separator other than ",". 5486** 5487** + Input comes from p->in. 5488** + Store results in p->z of length p->n. Space to hold p->z comes 5489** from sqlite3_malloc64(). 5490** + Use p->cSep as the column separator. The default is ",". 5491** + Use p->rSep as the row separator. The default is "\n". 5492** + Keep track of the line number in p->nLine. 5493** + Store the character that terminates the field in p->cTerm. Store 5494** EOF on end-of-file. 5495** + Report syntax errors on stderr 5496*/ 5497static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ 5498 int c; 5499 int cSep = p->cColSep; 5500 int rSep = p->cRowSep; 5501 p->n = 0; 5502 c = fgetc(p->in); 5503 if( c==EOF || seenInterrupt ){ 5504 p->cTerm = EOF; 5505 return 0; 5506 } 5507 if( c=='"' ){ 5508 int pc, ppc; 5509 int startLine = p->nLine; 5510 int cQuote = c; 5511 pc = ppc = 0; 5512 while( 1 ){ 5513 c = fgetc(p->in); 5514 if( c==rSep ) p->nLine++; 5515 if( c==cQuote ){ 5516 if( pc==cQuote ){ 5517 pc = 0; 5518 continue; 5519 } 5520 } 5521 if( (c==cSep && pc==cQuote) 5522 || (c==rSep && pc==cQuote) 5523 || (c==rSep && pc=='\r' && ppc==cQuote) 5524 || (c==EOF && pc==cQuote) 5525 ){ 5526 do{ p->n--; }while( p->z[p->n]!=cQuote ); 5527 p->cTerm = c; 5528 break; 5529 } 5530 if( pc==cQuote && c!='\r' ){ 5531 utf8_printf(stderr, "%s:%d: unescaped %c character\n", 5532 p->zFile, p->nLine, cQuote); 5533 } 5534 if( c==EOF ){ 5535 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", 5536 p->zFile, startLine, cQuote); 5537 p->cTerm = c; 5538 break; 5539 } 5540 import_append_char(p, c); 5541 ppc = pc; 5542 pc = c; 5543 } 5544 }else{ 5545 /* If this is the first field being parsed and it begins with the 5546 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ 5547 if( (c&0xff)==0xef && p->bNotFirst==0 ){ 5548 import_append_char(p, c); 5549 c = fgetc(p->in); 5550 if( (c&0xff)==0xbb ){ 5551 import_append_char(p, c); 5552 c = fgetc(p->in); 5553 if( (c&0xff)==0xbf ){ 5554 p->bNotFirst = 1; 5555 p->n = 0; 5556 return csv_read_one_field(p); 5557 } 5558 } 5559 } 5560 while( c!=EOF && c!=cSep && c!=rSep ){ 5561 import_append_char(p, c); 5562 c = fgetc(p->in); 5563 } 5564 if( c==rSep ){ 5565 p->nLine++; 5566 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; 5567 } 5568 p->cTerm = c; 5569 } 5570 if( p->z ) p->z[p->n] = 0; 5571 p->bNotFirst = 1; 5572 return p->z; 5573} 5574 5575/* Read a single field of ASCII delimited text. 5576** 5577** + Input comes from p->in. 5578** + Store results in p->z of length p->n. Space to hold p->z comes 5579** from sqlite3_malloc64(). 5580** + Use p->cSep as the column separator. The default is "\x1F". 5581** + Use p->rSep as the row separator. The default is "\x1E". 5582** + Keep track of the row number in p->nLine. 5583** + Store the character that terminates the field in p->cTerm. Store 5584** EOF on end-of-file. 5585** + Report syntax errors on stderr 5586*/ 5587static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ 5588 int c; 5589 int cSep = p->cColSep; 5590 int rSep = p->cRowSep; 5591 p->n = 0; 5592 c = fgetc(p->in); 5593 if( c==EOF || seenInterrupt ){ 5594 p->cTerm = EOF; 5595 return 0; 5596 } 5597 while( c!=EOF && c!=cSep && c!=rSep ){ 5598 import_append_char(p, c); 5599 c = fgetc(p->in); 5600 } 5601 if( c==rSep ){ 5602 p->nLine++; 5603 } 5604 p->cTerm = c; 5605 if( p->z ) p->z[p->n] = 0; 5606 return p->z; 5607} 5608 5609/* 5610** Try to transfer data for table zTable. If an error is seen while 5611** moving forward, try to go backwards. The backwards movement won't 5612** work for WITHOUT ROWID tables. 5613*/ 5614static void tryToCloneData( 5615 ShellState *p, 5616 sqlite3 *newDb, 5617 const char *zTable 5618){ 5619 sqlite3_stmt *pQuery = 0; 5620 sqlite3_stmt *pInsert = 0; 5621 char *zQuery = 0; 5622 char *zInsert = 0; 5623 int rc; 5624 int i, j, n; 5625 int nTable = strlen30(zTable); 5626 int k = 0; 5627 int cnt = 0; 5628 const int spinRate = 10000; 5629 5630 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); 5631 shell_check_oom(zQuery); 5632 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5633 if( rc ){ 5634 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5635 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5636 zQuery); 5637 goto end_data_xfer; 5638 } 5639 n = sqlite3_column_count(pQuery); 5640 zInsert = sqlite3_malloc64(200 + nTable + n*3); 5641 shell_check_oom(zInsert); 5642 sqlite3_snprintf(200+nTable,zInsert, 5643 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); 5644 i = strlen30(zInsert); 5645 for(j=1; j<n; j++){ 5646 memcpy(zInsert+i, ",?", 2); 5647 i += 2; 5648 } 5649 memcpy(zInsert+i, ");", 3); 5650 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); 5651 if( rc ){ 5652 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5653 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), 5654 zQuery); 5655 goto end_data_xfer; 5656 } 5657 for(k=0; k<2; k++){ 5658 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5659 for(i=0; i<n; i++){ 5660 switch( sqlite3_column_type(pQuery, i) ){ 5661 case SQLITE_NULL: { 5662 sqlite3_bind_null(pInsert, i+1); 5663 break; 5664 } 5665 case SQLITE_INTEGER: { 5666 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); 5667 break; 5668 } 5669 case SQLITE_FLOAT: { 5670 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); 5671 break; 5672 } 5673 case SQLITE_TEXT: { 5674 sqlite3_bind_text(pInsert, i+1, 5675 (const char*)sqlite3_column_text(pQuery,i), 5676 -1, SQLITE_STATIC); 5677 break; 5678 } 5679 case SQLITE_BLOB: { 5680 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), 5681 sqlite3_column_bytes(pQuery,i), 5682 SQLITE_STATIC); 5683 break; 5684 } 5685 } 5686 } /* End for */ 5687 rc = sqlite3_step(pInsert); 5688 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 5689 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), 5690 sqlite3_errmsg(newDb)); 5691 } 5692 sqlite3_reset(pInsert); 5693 cnt++; 5694 if( (cnt%spinRate)==0 ){ 5695 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); 5696 fflush(stdout); 5697 } 5698 } /* End while */ 5699 if( rc==SQLITE_DONE ) break; 5700 sqlite3_finalize(pQuery); 5701 sqlite3_free(zQuery); 5702 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", 5703 zTable); 5704 shell_check_oom(zQuery); 5705 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5706 if( rc ){ 5707 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); 5708 break; 5709 } 5710 } /* End for(k=0...) */ 5711 5712end_data_xfer: 5713 sqlite3_finalize(pQuery); 5714 sqlite3_finalize(pInsert); 5715 sqlite3_free(zQuery); 5716 sqlite3_free(zInsert); 5717} 5718 5719 5720/* 5721** Try to transfer all rows of the schema that match zWhere. For 5722** each row, invoke xForEach() on the object defined by that row. 5723** If an error is encountered while moving forward through the 5724** sqlite_schema table, try again moving backwards. 5725*/ 5726static void tryToCloneSchema( 5727 ShellState *p, 5728 sqlite3 *newDb, 5729 const char *zWhere, 5730 void (*xForEach)(ShellState*,sqlite3*,const char*) 5731){ 5732 sqlite3_stmt *pQuery = 0; 5733 char *zQuery = 0; 5734 int rc; 5735 const unsigned char *zName; 5736 const unsigned char *zSql; 5737 char *zErrMsg = 0; 5738 5739 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" 5740 " WHERE %s", zWhere); 5741 shell_check_oom(zQuery); 5742 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5743 if( rc ){ 5744 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5745 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5746 zQuery); 5747 goto end_schema_xfer; 5748 } 5749 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5750 zName = sqlite3_column_text(pQuery, 0); 5751 zSql = sqlite3_column_text(pQuery, 1); 5752 if( zName==0 || zSql==0 ) continue; 5753 printf("%s... ", zName); fflush(stdout); 5754 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5755 if( zErrMsg ){ 5756 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5757 sqlite3_free(zErrMsg); 5758 zErrMsg = 0; 5759 } 5760 if( xForEach ){ 5761 xForEach(p, newDb, (const char*)zName); 5762 } 5763 printf("done\n"); 5764 } 5765 if( rc!=SQLITE_DONE ){ 5766 sqlite3_finalize(pQuery); 5767 sqlite3_free(zQuery); 5768 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" 5769 " WHERE %s ORDER BY rowid DESC", zWhere); 5770 shell_check_oom(zQuery); 5771 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5772 if( rc ){ 5773 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5774 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5775 zQuery); 5776 goto end_schema_xfer; 5777 } 5778 while( sqlite3_step(pQuery)==SQLITE_ROW ){ 5779 zName = sqlite3_column_text(pQuery, 0); 5780 zSql = sqlite3_column_text(pQuery, 1); 5781 if( zName==0 || zSql==0 ) continue; 5782 printf("%s... ", zName); fflush(stdout); 5783 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5784 if( zErrMsg ){ 5785 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5786 sqlite3_free(zErrMsg); 5787 zErrMsg = 0; 5788 } 5789 if( xForEach ){ 5790 xForEach(p, newDb, (const char*)zName); 5791 } 5792 printf("done\n"); 5793 } 5794 } 5795end_schema_xfer: 5796 sqlite3_finalize(pQuery); 5797 sqlite3_free(zQuery); 5798} 5799 5800/* 5801** Open a new database file named "zNewDb". Try to recover as much information 5802** as possible out of the main database (which might be corrupt) and write it 5803** into zNewDb. 5804*/ 5805static void tryToClone(ShellState *p, const char *zNewDb){ 5806 int rc; 5807 sqlite3 *newDb = 0; 5808 if( access(zNewDb,0)==0 ){ 5809 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); 5810 return; 5811 } 5812 rc = sqlite3_open(zNewDb, &newDb); 5813 if( rc ){ 5814 utf8_printf(stderr, "Cannot create output database: %s\n", 5815 sqlite3_errmsg(newDb)); 5816 }else{ 5817 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); 5818 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); 5819 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); 5820 tryToCloneSchema(p, newDb, "type!='table'", 0); 5821 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); 5822 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 5823 } 5824 close_db(newDb); 5825} 5826 5827/* 5828** Change the output file back to stdout. 5829** 5830** If the p->doXdgOpen flag is set, that means the output was being 5831** redirected to a temporary file named by p->zTempFile. In that case, 5832** launch start/open/xdg-open on that temporary file. 5833*/ 5834static void output_reset(ShellState *p){ 5835 if( p->outfile[0]=='|' ){ 5836#ifndef SQLITE_OMIT_POPEN 5837 pclose(p->out); 5838#endif 5839 }else{ 5840 output_file_close(p->out); 5841#ifndef SQLITE_NOHAVE_SYSTEM 5842 if( p->doXdgOpen ){ 5843 const char *zXdgOpenCmd = 5844#if defined(_WIN32) 5845 "start"; 5846#elif defined(__APPLE__) 5847 "open"; 5848#else 5849 "xdg-open"; 5850#endif 5851 char *zCmd; 5852 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); 5853 if( system(zCmd) ){ 5854 utf8_printf(stderr, "Failed: [%s]\n", zCmd); 5855 }else{ 5856 /* Give the start/open/xdg-open command some time to get 5857 ** going before we continue, and potential delete the 5858 ** p->zTempFile data file out from under it */ 5859 sqlite3_sleep(2000); 5860 } 5861 sqlite3_free(zCmd); 5862 outputModePop(p); 5863 p->doXdgOpen = 0; 5864 } 5865#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 5866 } 5867 p->outfile[0] = 0; 5868 p->out = stdout; 5869} 5870 5871/* 5872** Run an SQL command and return the single integer result. 5873*/ 5874static int db_int(sqlite3 *db, const char *zSql){ 5875 sqlite3_stmt *pStmt; 5876 int res = 0; 5877 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 5878 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 5879 res = sqlite3_column_int(pStmt,0); 5880 } 5881 sqlite3_finalize(pStmt); 5882 return res; 5883} 5884 5885#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 5886/* 5887** Convert a 2-byte or 4-byte big-endian integer into a native integer 5888*/ 5889static unsigned int get2byteInt(unsigned char *a){ 5890 return (a[0]<<8) + a[1]; 5891} 5892static unsigned int get4byteInt(unsigned char *a){ 5893 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; 5894} 5895 5896/* 5897** Implementation of the ".dbinfo" command. 5898** 5899** Return 1 on error, 2 to exit, and 0 otherwise. 5900*/ 5901static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ 5902 static const struct { const char *zName; int ofst; } aField[] = { 5903 { "file change counter:", 24 }, 5904 { "database page count:", 28 }, 5905 { "freelist page count:", 36 }, 5906 { "schema cookie:", 40 }, 5907 { "schema format:", 44 }, 5908 { "default cache size:", 48 }, 5909 { "autovacuum top root:", 52 }, 5910 { "incremental vacuum:", 64 }, 5911 { "text encoding:", 56 }, 5912 { "user version:", 60 }, 5913 { "application id:", 68 }, 5914 { "software version:", 96 }, 5915 }; 5916 static const struct { const char *zName; const char *zSql; } aQuery[] = { 5917 { "number of tables:", 5918 "SELECT count(*) FROM %s WHERE type='table'" }, 5919 { "number of indexes:", 5920 "SELECT count(*) FROM %s WHERE type='index'" }, 5921 { "number of triggers:", 5922 "SELECT count(*) FROM %s WHERE type='trigger'" }, 5923 { "number of views:", 5924 "SELECT count(*) FROM %s WHERE type='view'" }, 5925 { "schema size:", 5926 "SELECT total(length(sql)) FROM %s" }, 5927 }; 5928 int i, rc; 5929 unsigned iDataVersion; 5930 char *zSchemaTab; 5931 char *zDb = nArg>=2 ? azArg[1] : "main"; 5932 sqlite3_stmt *pStmt = 0; 5933 unsigned char aHdr[100]; 5934 open_db(p, 0); 5935 if( p->db==0 ) return 1; 5936 rc = sqlite3_prepare_v2(p->db, 5937 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", 5938 -1, &pStmt, 0); 5939 if( rc ){ 5940 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); 5941 sqlite3_finalize(pStmt); 5942 return 1; 5943 } 5944 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); 5945 if( sqlite3_step(pStmt)==SQLITE_ROW 5946 && sqlite3_column_bytes(pStmt,0)>100 5947 ){ 5948 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); 5949 sqlite3_finalize(pStmt); 5950 }else{ 5951 raw_printf(stderr, "unable to read database header\n"); 5952 sqlite3_finalize(pStmt); 5953 return 1; 5954 } 5955 i = get2byteInt(aHdr+16); 5956 if( i==1 ) i = 65536; 5957 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); 5958 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); 5959 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); 5960 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); 5961 for(i=0; i<ArraySize(aField); i++){ 5962 int ofst = aField[i].ofst; 5963 unsigned int val = get4byteInt(aHdr + ofst); 5964 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); 5965 switch( ofst ){ 5966 case 56: { 5967 if( val==1 ) raw_printf(p->out, " (utf8)"); 5968 if( val==2 ) raw_printf(p->out, " (utf16le)"); 5969 if( val==3 ) raw_printf(p->out, " (utf16be)"); 5970 } 5971 } 5972 raw_printf(p->out, "\n"); 5973 } 5974 if( zDb==0 ){ 5975 zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); 5976 }else if( strcmp(zDb,"temp")==0 ){ 5977 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); 5978 }else{ 5979 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); 5980 } 5981 for(i=0; i<ArraySize(aQuery); i++){ 5982 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); 5983 int val = db_int(p->db, zSql); 5984 sqlite3_free(zSql); 5985 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); 5986 } 5987 sqlite3_free(zSchemaTab); 5988 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); 5989 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); 5990 return 0; 5991} 5992#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) 5993 && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 5994 5995/* 5996** Print the current sqlite3_errmsg() value to stderr and return 1. 5997*/ 5998static int shellDatabaseError(sqlite3 *db){ 5999 const char *zErr = sqlite3_errmsg(db); 6000 utf8_printf(stderr, "Error: %s\n", zErr); 6001 return 1; 6002} 6003 6004/* 6005** Compare the pattern in zGlob[] against the text in z[]. Return TRUE 6006** if they match and FALSE (0) if they do not match. 6007** 6008** Globbing rules: 6009** 6010** '*' Matches any sequence of zero or more characters. 6011** 6012** '?' Matches exactly one character. 6013** 6014** [...] Matches one character from the enclosed list of 6015** characters. 6016** 6017** [^...] Matches one character not in the enclosed list. 6018** 6019** '#' Matches any sequence of one or more digits with an 6020** optional + or - sign in front 6021** 6022** ' ' Any span of whitespace matches any other span of 6023** whitespace. 6024** 6025** Extra whitespace at the end of z[] is ignored. 6026*/ 6027static int testcase_glob(const char *zGlob, const char *z){ 6028 int c, c2; 6029 int invert; 6030 int seen; 6031 6032 while( (c = (*(zGlob++)))!=0 ){ 6033 if( IsSpace(c) ){ 6034 if( !IsSpace(*z) ) return 0; 6035 while( IsSpace(*zGlob) ) zGlob++; 6036 while( IsSpace(*z) ) z++; 6037 }else if( c=='*' ){ 6038 while( (c=(*(zGlob++))) == '*' || c=='?' ){ 6039 if( c=='?' && (*(z++))==0 ) return 0; 6040 } 6041 if( c==0 ){ 6042 return 1; 6043 }else if( c=='[' ){ 6044 while( *z && testcase_glob(zGlob-1,z)==0 ){ 6045 z++; 6046 } 6047 return (*z)!=0; 6048 } 6049 while( (c2 = (*(z++)))!=0 ){ 6050 while( c2!=c ){ 6051 c2 = *(z++); 6052 if( c2==0 ) return 0; 6053 } 6054 if( testcase_glob(zGlob,z) ) return 1; 6055 } 6056 return 0; 6057 }else if( c=='?' ){ 6058 if( (*(z++))==0 ) return 0; 6059 }else if( c=='[' ){ 6060 int prior_c = 0; 6061 seen = 0; 6062 invert = 0; 6063 c = *(z++); 6064 if( c==0 ) return 0; 6065 c2 = *(zGlob++); 6066 if( c2=='^' ){ 6067 invert = 1; 6068 c2 = *(zGlob++); 6069 } 6070 if( c2==']' ){ 6071 if( c==']' ) seen = 1; 6072 c2 = *(zGlob++); 6073 } 6074 while( c2 && c2!=']' ){ 6075 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 6076 c2 = *(zGlob++); 6077 if( c>=prior_c && c<=c2 ) seen = 1; 6078 prior_c = 0; 6079 }else{ 6080 if( c==c2 ){ 6081 seen = 1; 6082 } 6083 prior_c = c2; 6084 } 6085 c2 = *(zGlob++); 6086 } 6087 if( c2==0 || (seen ^ invert)==0 ) return 0; 6088 }else if( c=='#' ){ 6089 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; 6090 if( !IsDigit(z[0]) ) return 0; 6091 z++; 6092 while( IsDigit(z[0]) ){ z++; } 6093 }else{ 6094 if( c!=(*(z++)) ) return 0; 6095 } 6096 } 6097 while( IsSpace(*z) ){ z++; } 6098 return *z==0; 6099} 6100 6101 6102/* 6103** Compare the string as a command-line option with either one or two 6104** initial "-" characters. 6105*/ 6106static int optionMatch(const char *zStr, const char *zOpt){ 6107 if( zStr[0]!='-' ) return 0; 6108 zStr++; 6109 if( zStr[0]=='-' ) zStr++; 6110 return strcmp(zStr, zOpt)==0; 6111} 6112 6113/* 6114** Delete a file. 6115*/ 6116int shellDeleteFile(const char *zFilename){ 6117 int rc; 6118#ifdef _WIN32 6119 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); 6120 rc = _wunlink(z); 6121 sqlite3_free(z); 6122#else 6123 rc = unlink(zFilename); 6124#endif 6125 return rc; 6126} 6127 6128/* 6129** Try to delete the temporary file (if there is one) and free the 6130** memory used to hold the name of the temp file. 6131*/ 6132static void clearTempFile(ShellState *p){ 6133 if( p->zTempFile==0 ) return; 6134 if( p->doXdgOpen ) return; 6135 if( shellDeleteFile(p->zTempFile) ) return; 6136 sqlite3_free(p->zTempFile); 6137 p->zTempFile = 0; 6138} 6139 6140/* 6141** Create a new temp file name with the given suffix. 6142*/ 6143static void newTempFile(ShellState *p, const char *zSuffix){ 6144 clearTempFile(p); 6145 sqlite3_free(p->zTempFile); 6146 p->zTempFile = 0; 6147 if( p->db ){ 6148 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); 6149 } 6150 if( p->zTempFile==0 ){ 6151 /* If p->db is an in-memory database then the TEMPFILENAME file-control 6152 ** will not work and we will need to fallback to guessing */ 6153 char *zTemp; 6154 sqlite3_uint64 r; 6155 sqlite3_randomness(sizeof(r), &r); 6156 zTemp = getenv("TEMP"); 6157 if( zTemp==0 ) zTemp = getenv("TMP"); 6158 if( zTemp==0 ){ 6159#ifdef _WIN32 6160 zTemp = "\\tmp"; 6161#else 6162 zTemp = "/tmp"; 6163#endif 6164 } 6165 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); 6166 }else{ 6167 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); 6168 } 6169 shell_check_oom(p->zTempFile); 6170} 6171 6172 6173/* 6174** The implementation of SQL scalar function fkey_collate_clause(), used 6175** by the ".lint fkey-indexes" command. This scalar function is always 6176** called with four arguments - the parent table name, the parent column name, 6177** the child table name and the child column name. 6178** 6179** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') 6180** 6181** If either of the named tables or columns do not exist, this function 6182** returns an empty string. An empty string is also returned if both tables 6183** and columns exist but have the same default collation sequence. Or, 6184** if both exist but the default collation sequences are different, this 6185** function returns the string " COLLATE <parent-collation>", where 6186** <parent-collation> is the default collation sequence of the parent column. 6187*/ 6188static void shellFkeyCollateClause( 6189 sqlite3_context *pCtx, 6190 int nVal, 6191 sqlite3_value **apVal 6192){ 6193 sqlite3 *db = sqlite3_context_db_handle(pCtx); 6194 const char *zParent; 6195 const char *zParentCol; 6196 const char *zParentSeq; 6197 const char *zChild; 6198 const char *zChildCol; 6199 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ 6200 int rc; 6201 6202 assert( nVal==4 ); 6203 zParent = (const char*)sqlite3_value_text(apVal[0]); 6204 zParentCol = (const char*)sqlite3_value_text(apVal[1]); 6205 zChild = (const char*)sqlite3_value_text(apVal[2]); 6206 zChildCol = (const char*)sqlite3_value_text(apVal[3]); 6207 6208 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); 6209 rc = sqlite3_table_column_metadata( 6210 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 6211 ); 6212 if( rc==SQLITE_OK ){ 6213 rc = sqlite3_table_column_metadata( 6214 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 6215 ); 6216 } 6217 6218 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ 6219 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); 6220 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); 6221 sqlite3_free(z); 6222 } 6223} 6224 6225 6226/* 6227** The implementation of dot-command ".lint fkey-indexes". 6228*/ 6229static int lintFkeyIndexes( 6230 ShellState *pState, /* Current shell tool state */ 6231 char **azArg, /* Array of arguments passed to dot command */ 6232 int nArg /* Number of entries in azArg[] */ 6233){ 6234 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ 6235 FILE *out = pState->out; /* Stream to write non-error output to */ 6236 int bVerbose = 0; /* If -verbose is present */ 6237 int bGroupByParent = 0; /* If -groupbyparent is present */ 6238 int i; /* To iterate through azArg[] */ 6239 const char *zIndent = ""; /* How much to indent CREATE INDEX by */ 6240 int rc; /* Return code */ 6241 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ 6242 6243 /* 6244 ** This SELECT statement returns one row for each foreign key constraint 6245 ** in the schema of the main database. The column values are: 6246 ** 6247 ** 0. The text of an SQL statement similar to: 6248 ** 6249 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" 6250 ** 6251 ** This SELECT is similar to the one that the foreign keys implementation 6252 ** needs to run internally on child tables. If there is an index that can 6253 ** be used to optimize this query, then it can also be used by the FK 6254 ** implementation to optimize DELETE or UPDATE statements on the parent 6255 ** table. 6256 ** 6257 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by 6258 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema 6259 ** contains an index that can be used to optimize the query. 6260 ** 6261 ** 2. Human readable text that describes the child table and columns. e.g. 6262 ** 6263 ** "child_table(child_key1, child_key2)" 6264 ** 6265 ** 3. Human readable text that describes the parent table and columns. e.g. 6266 ** 6267 ** "parent_table(parent_key1, parent_key2)" 6268 ** 6269 ** 4. A full CREATE INDEX statement for an index that could be used to 6270 ** optimize DELETE or UPDATE statements on the parent table. e.g. 6271 ** 6272 ** "CREATE INDEX child_table_child_key ON child_table(child_key)" 6273 ** 6274 ** 5. The name of the parent table. 6275 ** 6276 ** These six values are used by the C logic below to generate the report. 6277 */ 6278 const char *zSql = 6279 "SELECT " 6280 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" 6281 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " 6282 " || fkey_collate_clause(" 6283 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" 6284 ", " 6285 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" 6286 " || group_concat('*=?', ' AND ') || ')'" 6287 ", " 6288 " s.name || '(' || group_concat(f.[from], ', ') || ')'" 6289 ", " 6290 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" 6291 ", " 6292 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" 6293 " || ' ON ' || quote(s.name) || '('" 6294 " || group_concat(quote(f.[from]) ||" 6295 " fkey_collate_clause(" 6296 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" 6297 " || ');'" 6298 ", " 6299 " f.[table] " 6300 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " 6301 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " 6302 "GROUP BY s.name, f.id " 6303 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" 6304 ; 6305 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; 6306 6307 for(i=2; i<nArg; i++){ 6308 int n = strlen30(azArg[i]); 6309 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ 6310 bVerbose = 1; 6311 } 6312 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ 6313 bGroupByParent = 1; 6314 zIndent = " "; 6315 } 6316 else{ 6317 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", 6318 azArg[0], azArg[1] 6319 ); 6320 return SQLITE_ERROR; 6321 } 6322 } 6323 6324 /* Register the fkey_collate_clause() SQL function */ 6325 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, 6326 0, shellFkeyCollateClause, 0, 0 6327 ); 6328 6329 6330 if( rc==SQLITE_OK ){ 6331 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); 6332 } 6333 if( rc==SQLITE_OK ){ 6334 sqlite3_bind_int(pSql, 1, bGroupByParent); 6335 } 6336 6337 if( rc==SQLITE_OK ){ 6338 int rc2; 6339 char *zPrev = 0; 6340 while( SQLITE_ROW==sqlite3_step(pSql) ){ 6341 int res = -1; 6342 sqlite3_stmt *pExplain = 0; 6343 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); 6344 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); 6345 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); 6346 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); 6347 const char *zCI = (const char*)sqlite3_column_text(pSql, 4); 6348 const char *zParent = (const char*)sqlite3_column_text(pSql, 5); 6349 6350 if( zEQP==0 ) continue; 6351 if( zGlob==0 ) continue; 6352 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 6353 if( rc!=SQLITE_OK ) break; 6354 if( SQLITE_ROW==sqlite3_step(pExplain) ){ 6355 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); 6356 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) 6357 || 0==sqlite3_strglob(zGlobIPK, zPlan)); 6358 } 6359 rc = sqlite3_finalize(pExplain); 6360 if( rc!=SQLITE_OK ) break; 6361 6362 if( res<0 ){ 6363 raw_printf(stderr, "Error: internal error"); 6364 break; 6365 }else{ 6366 if( bGroupByParent 6367 && (bVerbose || res==0) 6368 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) 6369 ){ 6370 raw_printf(out, "-- Parent table %s\n", zParent); 6371 sqlite3_free(zPrev); 6372 zPrev = sqlite3_mprintf("%s", zParent); 6373 } 6374 6375 if( res==0 ){ 6376 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); 6377 }else if( bVerbose ){ 6378 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", 6379 zIndent, zFrom, zTarget 6380 ); 6381 } 6382 } 6383 } 6384 sqlite3_free(zPrev); 6385 6386 if( rc!=SQLITE_OK ){ 6387 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6388 } 6389 6390 rc2 = sqlite3_finalize(pSql); 6391 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ 6392 rc = rc2; 6393 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6394 } 6395 }else{ 6396 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6397 } 6398 6399 return rc; 6400} 6401 6402/* 6403** Implementation of ".lint" dot command. 6404*/ 6405static int lintDotCommand( 6406 ShellState *pState, /* Current shell tool state */ 6407 char **azArg, /* Array of arguments passed to dot command */ 6408 int nArg /* Number of entries in azArg[] */ 6409){ 6410 int n; 6411 n = (nArg>=2 ? strlen30(azArg[1]) : 0); 6412 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; 6413 return lintFkeyIndexes(pState, azArg, nArg); 6414 6415 usage: 6416 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); 6417 raw_printf(stderr, "Where sub-commands are:\n"); 6418 raw_printf(stderr, " fkey-indexes\n"); 6419 return SQLITE_ERROR; 6420} 6421 6422#if !defined SQLITE_OMIT_VIRTUALTABLE 6423static void shellPrepare( 6424 sqlite3 *db, 6425 int *pRc, 6426 const char *zSql, 6427 sqlite3_stmt **ppStmt 6428){ 6429 *ppStmt = 0; 6430 if( *pRc==SQLITE_OK ){ 6431 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); 6432 if( rc!=SQLITE_OK ){ 6433 raw_printf(stderr, "sql error: %s (%d)\n", 6434 sqlite3_errmsg(db), sqlite3_errcode(db) 6435 ); 6436 *pRc = rc; 6437 } 6438 } 6439} 6440 6441/* 6442** Create a prepared statement using printf-style arguments for the SQL. 6443** 6444** This routine is could be marked "static". But it is not always used, 6445** depending on compile-time options. By omitting the "static", we avoid 6446** nuisance compiler warnings about "defined but not used". 6447*/ 6448void shellPreparePrintf( 6449 sqlite3 *db, 6450 int *pRc, 6451 sqlite3_stmt **ppStmt, 6452 const char *zFmt, 6453 ... 6454){ 6455 *ppStmt = 0; 6456 if( *pRc==SQLITE_OK ){ 6457 va_list ap; 6458 char *z; 6459 va_start(ap, zFmt); 6460 z = sqlite3_vmprintf(zFmt, ap); 6461 va_end(ap); 6462 if( z==0 ){ 6463 *pRc = SQLITE_NOMEM; 6464 }else{ 6465 shellPrepare(db, pRc, z, ppStmt); 6466 sqlite3_free(z); 6467 } 6468 } 6469} 6470 6471/* Finalize the prepared statement created using shellPreparePrintf(). 6472** 6473** This routine is could be marked "static". But it is not always used, 6474** depending on compile-time options. By omitting the "static", we avoid 6475** nuisance compiler warnings about "defined but not used". 6476*/ 6477void shellFinalize( 6478 int *pRc, 6479 sqlite3_stmt *pStmt 6480){ 6481 if( pStmt ){ 6482 sqlite3 *db = sqlite3_db_handle(pStmt); 6483 int rc = sqlite3_finalize(pStmt); 6484 if( *pRc==SQLITE_OK ){ 6485 if( rc!=SQLITE_OK ){ 6486 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 6487 } 6488 *pRc = rc; 6489 } 6490 } 6491} 6492 6493/* Reset the prepared statement created using shellPreparePrintf(). 6494** 6495** This routine is could be marked "static". But it is not always used, 6496** depending on compile-time options. By omitting the "static", we avoid 6497** nuisance compiler warnings about "defined but not used". 6498*/ 6499void shellReset( 6500 int *pRc, 6501 sqlite3_stmt *pStmt 6502){ 6503 int rc = sqlite3_reset(pStmt); 6504 if( *pRc==SQLITE_OK ){ 6505 if( rc!=SQLITE_OK ){ 6506 sqlite3 *db = sqlite3_db_handle(pStmt); 6507 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 6508 } 6509 *pRc = rc; 6510 } 6511} 6512#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ 6513 6514#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 6515/****************************************************************************** 6516** The ".archive" or ".ar" command. 6517*/ 6518/* 6519** Structure representing a single ".ar" command. 6520*/ 6521typedef struct ArCommand ArCommand; 6522struct ArCommand { 6523 u8 eCmd; /* An AR_CMD_* value */ 6524 u8 bVerbose; /* True if --verbose */ 6525 u8 bZip; /* True if the archive is a ZIP */ 6526 u8 bDryRun; /* True if --dry-run */ 6527 u8 bAppend; /* True if --append */ 6528 u8 bGlob; /* True if --glob */ 6529 u8 fromCmdLine; /* Run from -A instead of .archive */ 6530 int nArg; /* Number of command arguments */ 6531 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ 6532 const char *zFile; /* --file argument, or NULL */ 6533 const char *zDir; /* --directory argument, or NULL */ 6534 char **azArg; /* Array of command arguments */ 6535 ShellState *p; /* Shell state */ 6536 sqlite3 *db; /* Database containing the archive */ 6537}; 6538 6539/* 6540** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. 6541*/ 6542static int arUsage(FILE *f){ 6543 showHelp(f,"archive"); 6544 return SQLITE_ERROR; 6545} 6546 6547/* 6548** Print an error message for the .ar command to stderr and return 6549** SQLITE_ERROR. 6550*/ 6551static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ 6552 va_list ap; 6553 char *z; 6554 va_start(ap, zFmt); 6555 z = sqlite3_vmprintf(zFmt, ap); 6556 va_end(ap); 6557 utf8_printf(stderr, "Error: %s\n", z); 6558 if( pAr->fromCmdLine ){ 6559 utf8_printf(stderr, "Use \"-A\" for more help\n"); 6560 }else{ 6561 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); 6562 } 6563 sqlite3_free(z); 6564 return SQLITE_ERROR; 6565} 6566 6567/* 6568** Values for ArCommand.eCmd. 6569*/ 6570#define AR_CMD_CREATE 1 6571#define AR_CMD_UPDATE 2 6572#define AR_CMD_INSERT 3 6573#define AR_CMD_EXTRACT 4 6574#define AR_CMD_LIST 5 6575#define AR_CMD_HELP 6 6576#define AR_CMD_REMOVE 7 6577 6578/* 6579** Other (non-command) switches. 6580*/ 6581#define AR_SWITCH_VERBOSE 8 6582#define AR_SWITCH_FILE 9 6583#define AR_SWITCH_DIRECTORY 10 6584#define AR_SWITCH_APPEND 11 6585#define AR_SWITCH_DRYRUN 12 6586#define AR_SWITCH_GLOB 13 6587 6588static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ 6589 switch( eSwitch ){ 6590 case AR_CMD_CREATE: 6591 case AR_CMD_EXTRACT: 6592 case AR_CMD_LIST: 6593 case AR_CMD_REMOVE: 6594 case AR_CMD_UPDATE: 6595 case AR_CMD_INSERT: 6596 case AR_CMD_HELP: 6597 if( pAr->eCmd ){ 6598 return arErrorMsg(pAr, "multiple command options"); 6599 } 6600 pAr->eCmd = eSwitch; 6601 break; 6602 6603 case AR_SWITCH_DRYRUN: 6604 pAr->bDryRun = 1; 6605 break; 6606 case AR_SWITCH_GLOB: 6607 pAr->bGlob = 1; 6608 break; 6609 case AR_SWITCH_VERBOSE: 6610 pAr->bVerbose = 1; 6611 break; 6612 case AR_SWITCH_APPEND: 6613 pAr->bAppend = 1; 6614 /* Fall thru into --file */ 6615 case AR_SWITCH_FILE: 6616 pAr->zFile = zArg; 6617 break; 6618 case AR_SWITCH_DIRECTORY: 6619 pAr->zDir = zArg; 6620 break; 6621 } 6622 6623 return SQLITE_OK; 6624} 6625 6626/* 6627** Parse the command line for an ".ar" command. The results are written into 6628** structure (*pAr). SQLITE_OK is returned if the command line is parsed 6629** successfully, otherwise an error message is written to stderr and 6630** SQLITE_ERROR returned. 6631*/ 6632static int arParseCommand( 6633 char **azArg, /* Array of arguments passed to dot command */ 6634 int nArg, /* Number of entries in azArg[] */ 6635 ArCommand *pAr /* Populate this object */ 6636){ 6637 struct ArSwitch { 6638 const char *zLong; 6639 char cShort; 6640 u8 eSwitch; 6641 u8 bArg; 6642 } aSwitch[] = { 6643 { "create", 'c', AR_CMD_CREATE, 0 }, 6644 { "extract", 'x', AR_CMD_EXTRACT, 0 }, 6645 { "insert", 'i', AR_CMD_INSERT, 0 }, 6646 { "list", 't', AR_CMD_LIST, 0 }, 6647 { "remove", 'r', AR_CMD_REMOVE, 0 }, 6648 { "update", 'u', AR_CMD_UPDATE, 0 }, 6649 { "help", 'h', AR_CMD_HELP, 0 }, 6650 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, 6651 { "file", 'f', AR_SWITCH_FILE, 1 }, 6652 { "append", 'a', AR_SWITCH_APPEND, 1 }, 6653 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, 6654 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, 6655 { "glob", 'g', AR_SWITCH_GLOB, 0 }, 6656 }; 6657 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); 6658 struct ArSwitch *pEnd = &aSwitch[nSwitch]; 6659 6660 if( nArg<=1 ){ 6661 utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); 6662 return arUsage(stderr); 6663 }else{ 6664 char *z = azArg[1]; 6665 if( z[0]!='-' ){ 6666 /* Traditional style [tar] invocation */ 6667 int i; 6668 int iArg = 2; 6669 for(i=0; z[i]; i++){ 6670 const char *zArg = 0; 6671 struct ArSwitch *pOpt; 6672 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6673 if( z[i]==pOpt->cShort ) break; 6674 } 6675 if( pOpt==pEnd ){ 6676 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6677 } 6678 if( pOpt->bArg ){ 6679 if( iArg>=nArg ){ 6680 return arErrorMsg(pAr, "option requires an argument: %c",z[i]); 6681 } 6682 zArg = azArg[iArg++]; 6683 } 6684 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6685 } 6686 pAr->nArg = nArg-iArg; 6687 if( pAr->nArg>0 ){ 6688 pAr->azArg = &azArg[iArg]; 6689 } 6690 }else{ 6691 /* Non-traditional invocation */ 6692 int iArg; 6693 for(iArg=1; iArg<nArg; iArg++){ 6694 int n; 6695 z = azArg[iArg]; 6696 if( z[0]!='-' ){ 6697 /* All remaining command line words are command arguments. */ 6698 pAr->azArg = &azArg[iArg]; 6699 pAr->nArg = nArg-iArg; 6700 break; 6701 } 6702 n = strlen30(z); 6703 6704 if( z[1]!='-' ){ 6705 int i; 6706 /* One or more short options */ 6707 for(i=1; i<n; i++){ 6708 const char *zArg = 0; 6709 struct ArSwitch *pOpt; 6710 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6711 if( z[i]==pOpt->cShort ) break; 6712 } 6713 if( pOpt==pEnd ){ 6714 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6715 } 6716 if( pOpt->bArg ){ 6717 if( i<(n-1) ){ 6718 zArg = &z[i+1]; 6719 i = n; 6720 }else{ 6721 if( iArg>=(nArg-1) ){ 6722 return arErrorMsg(pAr, "option requires an argument: %c", 6723 z[i]); 6724 } 6725 zArg = azArg[++iArg]; 6726 } 6727 } 6728 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6729 } 6730 }else if( z[2]=='\0' ){ 6731 /* A -- option, indicating that all remaining command line words 6732 ** are command arguments. */ 6733 pAr->azArg = &azArg[iArg+1]; 6734 pAr->nArg = nArg-iArg-1; 6735 break; 6736 }else{ 6737 /* A long option */ 6738 const char *zArg = 0; /* Argument for option, if any */ 6739 struct ArSwitch *pMatch = 0; /* Matching option */ 6740 struct ArSwitch *pOpt; /* Iterator */ 6741 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6742 const char *zLong = pOpt->zLong; 6743 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ 6744 if( pMatch ){ 6745 return arErrorMsg(pAr, "ambiguous option: %s",z); 6746 }else{ 6747 pMatch = pOpt; 6748 } 6749 } 6750 } 6751 6752 if( pMatch==0 ){ 6753 return arErrorMsg(pAr, "unrecognized option: %s", z); 6754 } 6755 if( pMatch->bArg ){ 6756 if( iArg>=(nArg-1) ){ 6757 return arErrorMsg(pAr, "option requires an argument: %s", z); 6758 } 6759 zArg = azArg[++iArg]; 6760 } 6761 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; 6762 } 6763 } 6764 } 6765 } 6766 6767 return SQLITE_OK; 6768} 6769 6770/* 6771** This function assumes that all arguments within the ArCommand.azArg[] 6772** array refer to archive members, as for the --extract, --list or --remove 6773** commands. It checks that each of them are "present". If any specified 6774** file is not present in the archive, an error is printed to stderr and an 6775** error code returned. Otherwise, if all specified arguments are present 6776** in the archive, SQLITE_OK is returned. Here, "present" means either an 6777** exact equality when pAr->bGlob is false or a "name GLOB pattern" match 6778** when pAr->bGlob is true. 6779** 6780** This function strips any trailing '/' characters from each argument. 6781** This is consistent with the way the [tar] command seems to work on 6782** Linux. 6783*/ 6784static int arCheckEntries(ArCommand *pAr){ 6785 int rc = SQLITE_OK; 6786 if( pAr->nArg ){ 6787 int i, j; 6788 sqlite3_stmt *pTest = 0; 6789 const char *zSel = (pAr->bGlob) 6790 ? "SELECT name FROM %s WHERE glob($name,name)" 6791 : "SELECT name FROM %s WHERE name=$name"; 6792 6793 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); 6794 j = sqlite3_bind_parameter_index(pTest, "$name"); 6795 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 6796 char *z = pAr->azArg[i]; 6797 int n = strlen30(z); 6798 int bOk = 0; 6799 while( n>0 && z[n-1]=='/' ) n--; 6800 z[n] = '\0'; 6801 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); 6802 if( SQLITE_ROW==sqlite3_step(pTest) ){ 6803 bOk = 1; 6804 } 6805 shellReset(&rc, pTest); 6806 if( rc==SQLITE_OK && bOk==0 ){ 6807 utf8_printf(stderr, "not found in archive: %s\n", z); 6808 rc = SQLITE_ERROR; 6809 } 6810 } 6811 shellFinalize(&rc, pTest); 6812 } 6813 return rc; 6814} 6815 6816/* 6817** Format a WHERE clause that can be used against the "sqlar" table to 6818** identify all archive members that match the command arguments held 6819** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. 6820** The caller is responsible for eventually calling sqlite3_free() on 6821** any non-NULL (*pzWhere) value. Here, "match" means strict equality 6822** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. 6823*/ 6824static void arWhereClause( 6825 int *pRc, 6826 ArCommand *pAr, 6827 char **pzWhere /* OUT: New WHERE clause */ 6828){ 6829 char *zWhere = 0; 6830 const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; 6831 if( *pRc==SQLITE_OK ){ 6832 if( pAr->nArg==0 ){ 6833 zWhere = sqlite3_mprintf("1"); 6834 }else{ 6835 int i; 6836 const char *zSep = ""; 6837 for(i=0; i<pAr->nArg; i++){ 6838 const char *z = pAr->azArg[i]; 6839 zWhere = sqlite3_mprintf( 6840 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", 6841 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z 6842 ); 6843 if( zWhere==0 ){ 6844 *pRc = SQLITE_NOMEM; 6845 break; 6846 } 6847 zSep = " OR "; 6848 } 6849 } 6850 } 6851 *pzWhere = zWhere; 6852} 6853 6854/* 6855** Implementation of .ar "lisT" command. 6856*/ 6857static int arListCommand(ArCommand *pAr){ 6858 const char *zSql = "SELECT %s FROM %s WHERE %s"; 6859 const char *azCols[] = { 6860 "name", 6861 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" 6862 }; 6863 6864 char *zWhere = 0; 6865 sqlite3_stmt *pSql = 0; 6866 int rc; 6867 6868 rc = arCheckEntries(pAr); 6869 arWhereClause(&rc, pAr, &zWhere); 6870 6871 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], 6872 pAr->zSrcTable, zWhere); 6873 if( pAr->bDryRun ){ 6874 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 6875 }else{ 6876 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 6877 if( pAr->bVerbose ){ 6878 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", 6879 sqlite3_column_text(pSql, 0), 6880 sqlite3_column_int(pSql, 1), 6881 sqlite3_column_text(pSql, 2), 6882 sqlite3_column_text(pSql, 3) 6883 ); 6884 }else{ 6885 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 6886 } 6887 } 6888 } 6889 shellFinalize(&rc, pSql); 6890 sqlite3_free(zWhere); 6891 return rc; 6892} 6893 6894 6895/* 6896** Implementation of .ar "Remove" command. 6897*/ 6898static int arRemoveCommand(ArCommand *pAr){ 6899 int rc = 0; 6900 char *zSql = 0; 6901 char *zWhere = 0; 6902 6903 if( pAr->nArg ){ 6904 /* Verify that args actually exist within the archive before proceeding. 6905 ** And formulate a WHERE clause to match them. */ 6906 rc = arCheckEntries(pAr); 6907 arWhereClause(&rc, pAr, &zWhere); 6908 } 6909 if( rc==SQLITE_OK ){ 6910 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", 6911 pAr->zSrcTable, zWhere); 6912 if( pAr->bDryRun ){ 6913 utf8_printf(pAr->p->out, "%s\n", zSql); 6914 }else{ 6915 char *zErr = 0; 6916 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); 6917 if( rc==SQLITE_OK ){ 6918 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 6919 if( rc!=SQLITE_OK ){ 6920 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 6921 }else{ 6922 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); 6923 } 6924 } 6925 if( zErr ){ 6926 utf8_printf(stdout, "ERROR: %s\n", zErr); 6927 sqlite3_free(zErr); 6928 } 6929 } 6930 } 6931 sqlite3_free(zWhere); 6932 sqlite3_free(zSql); 6933 return rc; 6934} 6935 6936/* 6937** Implementation of .ar "eXtract" command. 6938*/ 6939static int arExtractCommand(ArCommand *pAr){ 6940 const char *zSql1 = 6941 "SELECT " 6942 " ($dir || name)," 6943 " writefile(($dir || name), %s, mode, mtime) " 6944 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" 6945 " AND name NOT GLOB '*..[/\\]*'"; 6946 6947 const char *azExtraArg[] = { 6948 "sqlar_uncompress(data, sz)", 6949 "data" 6950 }; 6951 6952 sqlite3_stmt *pSql = 0; 6953 int rc = SQLITE_OK; 6954 char *zDir = 0; 6955 char *zWhere = 0; 6956 int i, j; 6957 6958 /* If arguments are specified, check that they actually exist within 6959 ** the archive before proceeding. And formulate a WHERE clause to 6960 ** match them. */ 6961 rc = arCheckEntries(pAr); 6962 arWhereClause(&rc, pAr, &zWhere); 6963 6964 if( rc==SQLITE_OK ){ 6965 if( pAr->zDir ){ 6966 zDir = sqlite3_mprintf("%s/", pAr->zDir); 6967 }else{ 6968 zDir = sqlite3_mprintf(""); 6969 } 6970 if( zDir==0 ) rc = SQLITE_NOMEM; 6971 } 6972 6973 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 6974 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere 6975 ); 6976 6977 if( rc==SQLITE_OK ){ 6978 j = sqlite3_bind_parameter_index(pSql, "$dir"); 6979 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); 6980 6981 /* Run the SELECT statement twice. The first time, writefile() is called 6982 ** for all archive members that should be extracted. The second time, 6983 ** only for the directories. This is because the timestamps for 6984 ** extracted directories must be reset after they are populated (as 6985 ** populating them changes the timestamp). */ 6986 for(i=0; i<2; i++){ 6987 j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); 6988 sqlite3_bind_int(pSql, j, i); 6989 if( pAr->bDryRun ){ 6990 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 6991 }else{ 6992 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 6993 if( i==0 && pAr->bVerbose ){ 6994 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 6995 } 6996 } 6997 } 6998 shellReset(&rc, pSql); 6999 } 7000 shellFinalize(&rc, pSql); 7001 } 7002 7003 sqlite3_free(zDir); 7004 sqlite3_free(zWhere); 7005 return rc; 7006} 7007 7008/* 7009** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. 7010*/ 7011static int arExecSql(ArCommand *pAr, const char *zSql){ 7012 int rc; 7013 if( pAr->bDryRun ){ 7014 utf8_printf(pAr->p->out, "%s\n", zSql); 7015 rc = SQLITE_OK; 7016 }else{ 7017 char *zErr = 0; 7018 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 7019 if( zErr ){ 7020 utf8_printf(stdout, "ERROR: %s\n", zErr); 7021 sqlite3_free(zErr); 7022 } 7023 } 7024 return rc; 7025} 7026 7027 7028/* 7029** Implementation of .ar "create", "insert", and "update" commands. 7030** 7031** create -> Create a new SQL archive 7032** insert -> Insert or reinsert all files listed 7033** update -> Insert files that have changed or that were not 7034** previously in the archive 7035** 7036** Create the "sqlar" table in the database if it does not already exist. 7037** Then add each file in the azFile[] array to the archive. Directories 7038** are added recursively. If argument bVerbose is non-zero, a message is 7039** printed on stdout for each file archived. 7040** 7041** The create command is the same as update, except that it drops 7042** any existing "sqlar" table before beginning. The "insert" command 7043** always overwrites every file named on the command-line, where as 7044** "update" only overwrites if the size or mtime or mode has changed. 7045*/ 7046static int arCreateOrUpdateCommand( 7047 ArCommand *pAr, /* Command arguments and options */ 7048 int bUpdate, /* true for a --create. */ 7049 int bOnlyIfChanged /* Only update if file has changed */ 7050){ 7051 const char *zCreate = 7052 "CREATE TABLE IF NOT EXISTS sqlar(\n" 7053 " name TEXT PRIMARY KEY, -- name of the file\n" 7054 " mode INT, -- access permissions\n" 7055 " mtime INT, -- last modification time\n" 7056 " sz INT, -- original file size\n" 7057 " data BLOB -- compressed content\n" 7058 ")"; 7059 const char *zDrop = "DROP TABLE IF EXISTS sqlar"; 7060 const char *zInsertFmt[2] = { 7061 "REPLACE INTO %s(name,mode,mtime,sz,data)\n" 7062 " SELECT\n" 7063 " %s,\n" 7064 " mode,\n" 7065 " mtime,\n" 7066 " CASE substr(lsmode(mode),1,1)\n" 7067 " WHEN '-' THEN length(data)\n" 7068 " WHEN 'd' THEN 0\n" 7069 " ELSE -1 END,\n" 7070 " sqlar_compress(data)\n" 7071 " FROM fsdir(%Q,%Q) AS disk\n" 7072 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 7073 , 7074 "REPLACE INTO %s(name,mode,mtime,data)\n" 7075 " SELECT\n" 7076 " %s,\n" 7077 " mode,\n" 7078 " mtime,\n" 7079 " data\n" 7080 " FROM fsdir(%Q,%Q) AS disk\n" 7081 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 7082 }; 7083 int i; /* For iterating through azFile[] */ 7084 int rc; /* Return code */ 7085 const char *zTab = 0; /* SQL table into which to insert */ 7086 char *zSql; 7087 char zTemp[50]; 7088 char *zExists = 0; 7089 7090 arExecSql(pAr, "PRAGMA page_size=512"); 7091 rc = arExecSql(pAr, "SAVEPOINT ar;"); 7092 if( rc!=SQLITE_OK ) return rc; 7093 zTemp[0] = 0; 7094 if( pAr->bZip ){ 7095 /* Initialize the zipfile virtual table, if necessary */ 7096 if( pAr->zFile ){ 7097 sqlite3_uint64 r; 7098 sqlite3_randomness(sizeof(r),&r); 7099 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); 7100 zTab = zTemp; 7101 zSql = sqlite3_mprintf( 7102 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", 7103 zTab, pAr->zFile 7104 ); 7105 rc = arExecSql(pAr, zSql); 7106 sqlite3_free(zSql); 7107 }else{ 7108 zTab = "zip"; 7109 } 7110 }else{ 7111 /* Initialize the table for an SQLAR */ 7112 zTab = "sqlar"; 7113 if( bUpdate==0 ){ 7114 rc = arExecSql(pAr, zDrop); 7115 if( rc!=SQLITE_OK ) goto end_ar_transaction; 7116 } 7117 rc = arExecSql(pAr, zCreate); 7118 } 7119 if( bOnlyIfChanged ){ 7120 zExists = sqlite3_mprintf( 7121 " AND NOT EXISTS(" 7122 "SELECT 1 FROM %s AS mem" 7123 " WHERE mem.name=disk.name" 7124 " AND mem.mtime=disk.mtime" 7125 " AND mem.mode=disk.mode)", zTab); 7126 }else{ 7127 zExists = sqlite3_mprintf(""); 7128 } 7129 if( zExists==0 ) rc = SQLITE_NOMEM; 7130 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 7131 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, 7132 pAr->bVerbose ? "shell_putsnl(name)" : "name", 7133 pAr->azArg[i], pAr->zDir, zExists); 7134 rc = arExecSql(pAr, zSql2); 7135 sqlite3_free(zSql2); 7136 } 7137end_ar_transaction: 7138 if( rc!=SQLITE_OK ){ 7139 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 7140 }else{ 7141 rc = arExecSql(pAr, "RELEASE ar;"); 7142 if( pAr->bZip && pAr->zFile ){ 7143 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); 7144 arExecSql(pAr, zSql); 7145 sqlite3_free(zSql); 7146 } 7147 } 7148 sqlite3_free(zExists); 7149 return rc; 7150} 7151 7152/* 7153** Implementation of ".ar" dot command. 7154*/ 7155static int arDotCommand( 7156 ShellState *pState, /* Current shell tool state */ 7157 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ 7158 char **azArg, /* Array of arguments passed to dot command */ 7159 int nArg /* Number of entries in azArg[] */ 7160){ 7161 ArCommand cmd; 7162 int rc; 7163 memset(&cmd, 0, sizeof(cmd)); 7164 cmd.fromCmdLine = fromCmdLine; 7165 rc = arParseCommand(azArg, nArg, &cmd); 7166 if( rc==SQLITE_OK ){ 7167 int eDbType = SHELL_OPEN_UNSPEC; 7168 cmd.p = pState; 7169 cmd.db = pState->db; 7170 if( cmd.zFile ){ 7171 eDbType = deduceDatabaseType(cmd.zFile, 1); 7172 }else{ 7173 eDbType = pState->openMode; 7174 } 7175 if( eDbType==SHELL_OPEN_ZIPFILE ){ 7176 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ 7177 if( cmd.zFile==0 ){ 7178 cmd.zSrcTable = sqlite3_mprintf("zip"); 7179 }else{ 7180 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); 7181 } 7182 } 7183 cmd.bZip = 1; 7184 }else if( cmd.zFile ){ 7185 int flags; 7186 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; 7187 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 7188 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ 7189 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 7190 }else{ 7191 flags = SQLITE_OPEN_READONLY; 7192 } 7193 cmd.db = 0; 7194 if( cmd.bDryRun ){ 7195 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, 7196 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); 7197 } 7198 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 7199 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); 7200 if( rc!=SQLITE_OK ){ 7201 utf8_printf(stderr, "cannot open file: %s (%s)\n", 7202 cmd.zFile, sqlite3_errmsg(cmd.db) 7203 ); 7204 goto end_ar_command; 7205 } 7206 sqlite3_fileio_init(cmd.db, 0, 0); 7207 sqlite3_sqlar_init(cmd.db, 0, 0); 7208 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, 7209 shellPutsFunc, 0, 0); 7210 7211 } 7212 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ 7213 if( cmd.eCmd!=AR_CMD_CREATE 7214 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) 7215 ){ 7216 utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); 7217 rc = SQLITE_ERROR; 7218 goto end_ar_command; 7219 } 7220 cmd.zSrcTable = sqlite3_mprintf("sqlar"); 7221 } 7222 7223 switch( cmd.eCmd ){ 7224 case AR_CMD_CREATE: 7225 rc = arCreateOrUpdateCommand(&cmd, 0, 0); 7226 break; 7227 7228 case AR_CMD_EXTRACT: 7229 rc = arExtractCommand(&cmd); 7230 break; 7231 7232 case AR_CMD_LIST: 7233 rc = arListCommand(&cmd); 7234 break; 7235 7236 case AR_CMD_HELP: 7237 arUsage(pState->out); 7238 break; 7239 7240 case AR_CMD_INSERT: 7241 rc = arCreateOrUpdateCommand(&cmd, 1, 0); 7242 break; 7243 7244 case AR_CMD_REMOVE: 7245 rc = arRemoveCommand(&cmd); 7246 break; 7247 7248 default: 7249 assert( cmd.eCmd==AR_CMD_UPDATE ); 7250 rc = arCreateOrUpdateCommand(&cmd, 1, 1); 7251 break; 7252 } 7253 } 7254end_ar_command: 7255 if( cmd.db!=pState->db ){ 7256 close_db(cmd.db); 7257 } 7258 sqlite3_free(cmd.zSrcTable); 7259 7260 return rc; 7261} 7262/* End of the ".archive" or ".ar" command logic 7263*******************************************************************************/ 7264#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ 7265 7266#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 7267 7268/* 7269** This function is used as a callback by the recover extension. Simply 7270** print the supplied SQL statement to stdout. 7271*/ 7272static int recoverSqlCb(void *pCtx, const char *zSql){ 7273 ShellState *pState = (ShellState*)pCtx; 7274 utf8_printf(pState->out, "%s;\n", zSql); 7275 return SQLITE_OK; 7276} 7277 7278/* 7279** This function is called to recover data from the database. A script 7280** to construct a new database containing all recovered data is output 7281** on stream pState->out. 7282*/ 7283static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ 7284 int rc = SQLITE_OK; 7285 const char *zRecoveryDb = ""; /* Name of "recovery" database */ 7286 const char *zLAF = "lost_and_found"; 7287 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ 7288 int bRowids = 1; /* 0 if --no-rowids */ 7289 sqlite3_recover *p = 0; 7290 int i = 0; 7291 7292 for(i=1; i<nArg; i++){ 7293 char *z = azArg[i]; 7294 int n; 7295 if( z[0]=='-' && z[1]=='-' ) z++; 7296 n = strlen30(z); 7297 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ 7298 bFreelist = 0; 7299 }else 7300 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ 7301 i++; 7302 zRecoveryDb = azArg[i]; 7303 }else 7304 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ 7305 i++; 7306 zLAF = azArg[i]; 7307 }else 7308 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ 7309 bRowids = 0; 7310 } 7311 else{ 7312 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 7313 showHelp(pState->out, azArg[0]); 7314 return 1; 7315 } 7316 } 7317 7318 p = sqlite3_recover_init_sql( 7319 pState->db, "main", recoverSqlCb, (void*)pState 7320 ); 7321 7322 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); 7323 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); 7324 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); 7325 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); 7326 7327 sqlite3_recover_run(p); 7328 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ 7329 const char *zErr = sqlite3_recover_errmsg(p); 7330 int errCode = sqlite3_recover_errcode(p); 7331 raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode); 7332 } 7333 rc = sqlite3_recover_finish(p); 7334 return rc; 7335} 7336#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7337 7338 7339/* 7340 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. 7341 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, 7342 * close db and set it to 0, and return the columns spec, to later 7343 * be sqlite3_free()'ed by the caller. 7344 * The return is 0 when either: 7345 * (a) The db was not initialized and zCol==0 (There are no columns.) 7346 * (b) zCol!=0 (Column was added, db initialized as needed.) 7347 * The 3rd argument, pRenamed, references an out parameter. If the 7348 * pointer is non-zero, its referent will be set to a summary of renames 7349 * done if renaming was necessary, or set to 0 if none was done. The out 7350 * string (if any) must be sqlite3_free()'ed by the caller. 7351 */ 7352#ifdef SHELL_DEBUG 7353#define rc_err_oom_die(rc) \ 7354 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ 7355 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ 7356 fprintf(stderr,"E:%d\n",rc), assert(0) 7357#else 7358static void rc_err_oom_die(int rc){ 7359 if( rc==SQLITE_NOMEM ) shell_check_oom(0); 7360 assert(rc==SQLITE_OK||rc==SQLITE_DONE); 7361} 7362#endif 7363 7364#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ 7365static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); 7366#else /* Otherwise, memory is faster/better for the transient DB. */ 7367static const char *zCOL_DB = ":memory:"; 7368#endif 7369 7370/* Define character (as C string) to separate generated column ordinal 7371 * from protected part of incoming column names. This defaults to "_" 7372 * so that incoming column identifiers that did not need not be quoted 7373 * remain usable without being quoted. It must be one character. 7374 */ 7375#ifndef SHELL_AUTOCOLUMN_SEP 7376# define AUTOCOLUMN_SEP "_" 7377#else 7378# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) 7379#endif 7380 7381static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ 7382 /* Queries and D{D,M}L used here */ 7383 static const char * const zTabMake = "\ 7384CREATE TABLE ColNames(\ 7385 cpos INTEGER PRIMARY KEY,\ 7386 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ 7387CREATE VIEW RepeatedNames AS \ 7388SELECT DISTINCT t.name FROM ColNames t \ 7389WHERE t.name COLLATE NOCASE IN (\ 7390 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ 7391);\ 7392"; 7393 static const char * const zTabFill = "\ 7394INSERT INTO ColNames(name,nlen,chop,reps,suff)\ 7395 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ 7396"; 7397 static const char * const zHasDupes = "\ 7398SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ 7399 <count(name) FROM ColNames\ 7400"; 7401#ifdef SHELL_COLUMN_RENAME_CLEAN 7402 static const char * const zDedoctor = "\ 7403UPDATE ColNames SET chop=iif(\ 7404 (substring(name,nlen,1) BETWEEN '0' AND '9')\ 7405 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\ 7406 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ 7407 0\ 7408)\ 7409"; 7410#endif 7411 static const char * const zSetReps = "\ 7412UPDATE ColNames AS t SET reps=\ 7413(SELECT count(*) FROM ColNames d \ 7414 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ 7415 COLLATE NOCASE\ 7416)\ 7417"; 7418#ifdef SQLITE_ENABLE_MATH_FUNCTIONS 7419 static const char * const zColDigits = "\ 7420SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \ 7421"; 7422#else 7423 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */ 7424 static const char * const zColDigits = "\ 7425SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \ 7426 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \ 7427 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \ 7428"; 7429#endif 7430 static const char * const zRenameRank = 7431#ifdef SHELL_COLUMN_RENAME_CLEAN 7432 "UPDATE ColNames AS t SET suff=" 7433 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" 7434#else /* ...RENAME_MINIMAL_ONE_PASS */ 7435"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ 7436" SELECT 0 AS nlz" 7437" UNION" 7438" SELECT nlz+1 AS nlz FROM Lzn" 7439" WHERE EXISTS(" 7440" SELECT 1" 7441" FROM ColNames t, ColNames o" 7442" WHERE" 7443" iif(t.name IN (SELECT * FROM RepeatedNames)," 7444" printf('%s"AUTOCOLUMN_SEP"%s'," 7445" t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," 7446" t.name" 7447" )" 7448" =" 7449" iif(o.name IN (SELECT * FROM RepeatedNames)," 7450" printf('%s"AUTOCOLUMN_SEP"%s'," 7451" o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," 7452" o.name" 7453" )" 7454" COLLATE NOCASE" 7455" AND o.cpos<>t.cpos" 7456" GROUP BY t.cpos" 7457" )" 7458") UPDATE Colnames AS t SET" 7459" chop = 0," /* No chopping, never touch incoming names. */ 7460" suff = iif(name IN (SELECT * FROM RepeatedNames)," 7461" printf('"AUTOCOLUMN_SEP"%s', substring(" 7462" printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," 7463" ''" 7464" )" 7465#endif 7466 ; 7467 static const char * const zCollectVar = "\ 7468SELECT\ 7469 '('||x'0a'\ 7470 || group_concat(\ 7471 cname||' TEXT',\ 7472 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ 7473 ||')' AS ColsSpec \ 7474FROM (\ 7475 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ 7476 FROM ColNames ORDER BY cpos\ 7477)"; 7478 static const char * const zRenamesDone = 7479 "SELECT group_concat(" 7480 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," 7481 " ','||x'0a')" 7482 "FROM ColNames WHERE suff<>'' OR chop!=0" 7483 ; 7484 int rc; 7485 sqlite3_stmt *pStmt = 0; 7486 assert(pDb!=0); 7487 if( zColNew ){ 7488 /* Add initial or additional column. Init db if necessary. */ 7489 if( *pDb==0 ){ 7490 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; 7491#ifdef SHELL_COLFIX_DB 7492 if(*zCOL_DB!=':') 7493 sqlite3_exec(*pDb,"drop table if exists ColNames;" 7494 "drop view if exists RepeatedNames;",0,0,0); 7495#endif 7496 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); 7497 rc_err_oom_die(rc); 7498 } 7499 assert(*pDb!=0); 7500 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); 7501 rc_err_oom_die(rc); 7502 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); 7503 rc_err_oom_die(rc); 7504 rc = sqlite3_step(pStmt); 7505 rc_err_oom_die(rc); 7506 sqlite3_finalize(pStmt); 7507 return 0; 7508 }else if( *pDb==0 ){ 7509 return 0; 7510 }else{ 7511 /* Formulate the columns spec, close the DB, zero *pDb. */ 7512 char *zColsSpec = 0; 7513 int hasDupes = db_int(*pDb, zHasDupes); 7514 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0; 7515 if( hasDupes ){ 7516#ifdef SHELL_COLUMN_RENAME_CLEAN 7517 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); 7518 rc_err_oom_die(rc); 7519#endif 7520 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); 7521 rc_err_oom_die(rc); 7522 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); 7523 rc_err_oom_die(rc); 7524 sqlite3_bind_int(pStmt, 1, nDigits); 7525 rc = sqlite3_step(pStmt); 7526 sqlite3_finalize(pStmt); 7527 assert(rc==SQLITE_DONE); 7528 } 7529 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */ 7530 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); 7531 rc_err_oom_die(rc); 7532 rc = sqlite3_step(pStmt); 7533 if( rc==SQLITE_ROW ){ 7534 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 7535 }else{ 7536 zColsSpec = 0; 7537 } 7538 if( pzRenamed!=0 ){ 7539 if( !hasDupes ) *pzRenamed = 0; 7540 else{ 7541 sqlite3_finalize(pStmt); 7542 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) 7543 && SQLITE_ROW==sqlite3_step(pStmt) ){ 7544 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 7545 }else 7546 *pzRenamed = 0; 7547 } 7548 } 7549 sqlite3_finalize(pStmt); 7550 sqlite3_close(*pDb); 7551 *pDb = 0; 7552 return zColsSpec; 7553 } 7554} 7555 7556/* 7557** If an input line begins with "." then invoke this routine to 7558** process that line. 7559** 7560** Return 1 on error, 2 to exit, and 0 otherwise. 7561*/ 7562static int do_meta_command(char *zLine, ShellState *p){ 7563 int h = 1; 7564 int nArg = 0; 7565 int n, c; 7566 int rc = 0; 7567 char *azArg[52]; 7568 7569#ifndef SQLITE_OMIT_VIRTUALTABLE 7570 if( p->expert.pExpert ){ 7571 expertFinish(p, 1, 0); 7572 } 7573#endif 7574 7575 /* Parse the input line into tokens. 7576 */ 7577 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ 7578 while( IsSpace(zLine[h]) ){ h++; } 7579 if( zLine[h]==0 ) break; 7580 if( zLine[h]=='\'' || zLine[h]=='"' ){ 7581 int delim = zLine[h++]; 7582 azArg[nArg++] = &zLine[h]; 7583 while( zLine[h] && zLine[h]!=delim ){ 7584 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; 7585 h++; 7586 } 7587 if( zLine[h]==delim ){ 7588 zLine[h++] = 0; 7589 } 7590 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); 7591 }else{ 7592 azArg[nArg++] = &zLine[h]; 7593 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } 7594 if( zLine[h] ) zLine[h++] = 0; 7595 resolve_backslashes(azArg[nArg-1]); 7596 } 7597 } 7598 azArg[nArg] = 0; 7599 7600 /* Process the input line. 7601 */ 7602 if( nArg==0 ) return 0; /* no tokens, no error */ 7603 n = strlen30(azArg[0]); 7604 c = azArg[0][0]; 7605 clearTempFile(p); 7606 7607#ifndef SQLITE_OMIT_AUTHORIZATION 7608 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ 7609 if( nArg!=2 ){ 7610 raw_printf(stderr, "Usage: .auth ON|OFF\n"); 7611 rc = 1; 7612 goto meta_command_exit; 7613 } 7614 open_db(p, 0); 7615 if( booleanValue(azArg[1]) ){ 7616 sqlite3_set_authorizer(p->db, shellAuth, p); 7617 }else if( p->bSafeModePersist ){ 7618 sqlite3_set_authorizer(p->db, safeModeAuth, p); 7619 }else{ 7620 sqlite3_set_authorizer(p->db, 0, 0); 7621 } 7622 }else 7623#endif 7624 7625#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ 7626 && !defined(SQLITE_SHELL_FIDDLE) 7627 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ 7628 open_db(p, 0); 7629 failIfSafeMode(p, "cannot run .archive in safe mode"); 7630 rc = arDotCommand(p, 0, azArg, nArg); 7631 }else 7632#endif 7633 7634#ifndef SQLITE_SHELL_FIDDLE 7635 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) 7636 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) 7637 ){ 7638 const char *zDestFile = 0; 7639 const char *zDb = 0; 7640 sqlite3 *pDest; 7641 sqlite3_backup *pBackup; 7642 int j; 7643 int bAsync = 0; 7644 const char *zVfs = 0; 7645 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 7646 for(j=1; j<nArg; j++){ 7647 const char *z = azArg[j]; 7648 if( z[0]=='-' ){ 7649 if( z[1]=='-' ) z++; 7650 if( strcmp(z, "-append")==0 ){ 7651 zVfs = "apndvfs"; 7652 }else 7653 if( strcmp(z, "-async")==0 ){ 7654 bAsync = 1; 7655 }else 7656 { 7657 utf8_printf(stderr, "unknown option: %s\n", azArg[j]); 7658 return 1; 7659 } 7660 }else if( zDestFile==0 ){ 7661 zDestFile = azArg[j]; 7662 }else if( zDb==0 ){ 7663 zDb = zDestFile; 7664 zDestFile = azArg[j]; 7665 }else{ 7666 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); 7667 return 1; 7668 } 7669 } 7670 if( zDestFile==0 ){ 7671 raw_printf(stderr, "missing FILENAME argument on .backup\n"); 7672 return 1; 7673 } 7674 if( zDb==0 ) zDb = "main"; 7675 rc = sqlite3_open_v2(zDestFile, &pDest, 7676 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); 7677 if( rc!=SQLITE_OK ){ 7678 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); 7679 close_db(pDest); 7680 return 1; 7681 } 7682 if( bAsync ){ 7683 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", 7684 0, 0, 0); 7685 } 7686 open_db(p, 0); 7687 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); 7688 if( pBackup==0 ){ 7689 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7690 close_db(pDest); 7691 return 1; 7692 } 7693 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 7694 sqlite3_backup_finish(pBackup); 7695 if( rc==SQLITE_DONE ){ 7696 rc = 0; 7697 }else{ 7698 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7699 rc = 1; 7700 } 7701 close_db(pDest); 7702 }else 7703#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7704 7705 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ 7706 if( nArg==2 ){ 7707 bail_on_error = booleanValue(azArg[1]); 7708 }else{ 7709 raw_printf(stderr, "Usage: .bail on|off\n"); 7710 rc = 1; 7711 } 7712 }else 7713 7714 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ 7715 if( nArg==2 ){ 7716 if( booleanValue(azArg[1]) ){ 7717 setBinaryMode(p->out, 1); 7718 }else{ 7719 setTextMode(p->out, 1); 7720 } 7721 }else{ 7722 raw_printf(stderr, "Usage: .binary on|off\n"); 7723 rc = 1; 7724 } 7725 }else 7726 7727 /* The undocumented ".breakpoint" command causes a call to the no-op 7728 ** routine named test_breakpoint(). 7729 */ 7730 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ 7731 test_breakpoint(); 7732 }else 7733 7734#ifndef SQLITE_SHELL_FIDDLE 7735 if( c=='c' && strcmp(azArg[0],"cd")==0 ){ 7736 failIfSafeMode(p, "cannot run .cd in safe mode"); 7737 if( nArg==2 ){ 7738#if defined(_WIN32) || defined(WIN32) 7739 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); 7740 rc = !SetCurrentDirectoryW(z); 7741 sqlite3_free(z); 7742#else 7743 rc = chdir(azArg[1]); 7744#endif 7745 if( rc ){ 7746 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); 7747 rc = 1; 7748 } 7749 }else{ 7750 raw_printf(stderr, "Usage: .cd DIRECTORY\n"); 7751 rc = 1; 7752 } 7753 }else 7754#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7755 7756 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ 7757 if( nArg==2 ){ 7758 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); 7759 }else{ 7760 raw_printf(stderr, "Usage: .changes on|off\n"); 7761 rc = 1; 7762 } 7763 }else 7764 7765#ifndef SQLITE_SHELL_FIDDLE 7766 /* Cancel output redirection, if it is currently set (by .testcase) 7767 ** Then read the content of the testcase-out.txt file and compare against 7768 ** azArg[1]. If there are differences, report an error and exit. 7769 */ 7770 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ 7771 char *zRes = 0; 7772 output_reset(p); 7773 if( nArg!=2 ){ 7774 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); 7775 rc = 2; 7776 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ 7777 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); 7778 rc = 2; 7779 }else if( testcase_glob(azArg[1],zRes)==0 ){ 7780 utf8_printf(stderr, 7781 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", 7782 p->zTestcase, azArg[1], zRes); 7783 rc = 1; 7784 }else{ 7785 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); 7786 p->nCheck++; 7787 } 7788 sqlite3_free(zRes); 7789 }else 7790#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7791 7792#ifndef SQLITE_SHELL_FIDDLE 7793 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ 7794 failIfSafeMode(p, "cannot run .clone in safe mode"); 7795 if( nArg==2 ){ 7796 tryToClone(p, azArg[1]); 7797 }else{ 7798 raw_printf(stderr, "Usage: .clone FILENAME\n"); 7799 rc = 1; 7800 } 7801 }else 7802#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7803 7804 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){ 7805 if( nArg==1 ){ 7806 /* List available connections */ 7807 int i; 7808 for(i=0; i<ArraySize(p->aAuxDb); i++){ 7809 const char *zFile = p->aAuxDb[i].zDbFilename; 7810 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ 7811 zFile = "(not open)"; 7812 }else if( zFile==0 ){ 7813 zFile = "(memory)"; 7814 }else if( zFile[0]==0 ){ 7815 zFile = "(temporary-file)"; 7816 } 7817 if( p->pAuxDb == &p->aAuxDb[i] ){ 7818 utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile); 7819 }else if( p->aAuxDb[i].db!=0 ){ 7820 utf8_printf(stdout, " %d: %s\n", i, zFile); 7821 } 7822 } 7823 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ 7824 int i = azArg[1][0] - '0'; 7825 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ 7826 p->pAuxDb->db = p->db; 7827 p->pAuxDb = &p->aAuxDb[i]; 7828 globalDb = p->db = p->pAuxDb->db; 7829 p->pAuxDb->db = 0; 7830 } 7831 }else if( nArg==3 && strcmp(azArg[1], "close")==0 7832 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ 7833 int i = azArg[2][0] - '0'; 7834 if( i<0 || i>=ArraySize(p->aAuxDb) ){ 7835 /* No-op */ 7836 }else if( p->pAuxDb == &p->aAuxDb[i] ){ 7837 raw_printf(stderr, "cannot close the active database connection\n"); 7838 rc = 1; 7839 }else if( p->aAuxDb[i].db ){ 7840 session_close_all(p, i); 7841 close_db(p->aAuxDb[i].db); 7842 p->aAuxDb[i].db = 0; 7843 } 7844 }else{ 7845 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); 7846 rc = 1; 7847 } 7848 }else 7849 7850 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ 7851 char **azName = 0; 7852 int nName = 0; 7853 sqlite3_stmt *pStmt; 7854 int i; 7855 open_db(p, 0); 7856 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 7857 if( rc ){ 7858 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 7859 rc = 1; 7860 }else{ 7861 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 7862 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); 7863 const char *zFile = (const char*)sqlite3_column_text(pStmt,2); 7864 if( zSchema==0 || zFile==0 ) continue; 7865 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); 7866 shell_check_oom(azName); 7867 azName[nName*2] = strdup(zSchema); 7868 azName[nName*2+1] = strdup(zFile); 7869 nName++; 7870 } 7871 } 7872 sqlite3_finalize(pStmt); 7873 for(i=0; i<nName; i++){ 7874 int eTxn = sqlite3_txn_state(p->db, azName[i*2]); 7875 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); 7876 const char *z = azName[i*2+1]; 7877 utf8_printf(p->out, "%s: %s %s%s\n", 7878 azName[i*2], 7879 z && z[0] ? z : "\"\"", 7880 bRdonly ? "r/o" : "r/w", 7881 eTxn==SQLITE_TXN_NONE ? "" : 7882 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); 7883 free(azName[i*2]); 7884 free(azName[i*2+1]); 7885 } 7886 sqlite3_free(azName); 7887 }else 7888 7889 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ 7890 static const struct DbConfigChoices { 7891 const char *zName; 7892 int op; 7893 } aDbConfig[] = { 7894 { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, 7895 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, 7896 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, 7897 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, 7898 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, 7899 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 7900 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, 7901 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 7902 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, 7903 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, 7904 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, 7905 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, 7906 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, 7907 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, 7908 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, 7909 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 7910 }; 7911 int ii, v; 7912 open_db(p, 0); 7913 for(ii=0; ii<ArraySize(aDbConfig); ii++){ 7914 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; 7915 if( nArg>=3 ){ 7916 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); 7917 } 7918 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); 7919 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); 7920 if( nArg>1 ) break; 7921 } 7922 if( nArg>1 && ii==ArraySize(aDbConfig) ){ 7923 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); 7924 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); 7925 } 7926 }else 7927 7928#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 7929 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ 7930 rc = shell_dbinfo_command(p, nArg, azArg); 7931 }else 7932 7933 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ 7934 open_db(p, 0); 7935 rc = recoverDatabaseCmd(p, nArg, azArg); 7936 }else 7937#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7938 7939 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ 7940 char *zLike = 0; 7941 char *zSql; 7942 int i; 7943 int savedShowHeader = p->showHeader; 7944 int savedShellFlags = p->shellFlgs; 7945 ShellClearFlag(p, 7946 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo 7947 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); 7948 for(i=1; i<nArg; i++){ 7949 if( azArg[i][0]=='-' ){ 7950 const char *z = azArg[i]+1; 7951 if( z[0]=='-' ) z++; 7952 if( strcmp(z,"preserve-rowids")==0 ){ 7953#ifdef SQLITE_OMIT_VIRTUALTABLE 7954 raw_printf(stderr, "The --preserve-rowids option is not compatible" 7955 " with SQLITE_OMIT_VIRTUALTABLE\n"); 7956 rc = 1; 7957 sqlite3_free(zLike); 7958 goto meta_command_exit; 7959#else 7960 ShellSetFlag(p, SHFLG_PreserveRowid); 7961#endif 7962 }else 7963 if( strcmp(z,"newlines")==0 ){ 7964 ShellSetFlag(p, SHFLG_Newlines); 7965 }else 7966 if( strcmp(z,"data-only")==0 ){ 7967 ShellSetFlag(p, SHFLG_DumpDataOnly); 7968 }else 7969 if( strcmp(z,"nosys")==0 ){ 7970 ShellSetFlag(p, SHFLG_DumpNoSys); 7971 }else 7972 { 7973 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); 7974 rc = 1; 7975 sqlite3_free(zLike); 7976 goto meta_command_exit; 7977 } 7978 }else{ 7979 /* azArg[i] contains a LIKE pattern. This ".dump" request should 7980 ** only dump data for tables for which either the table name matches 7981 ** the LIKE pattern, or the table appears to be a shadow table of 7982 ** a virtual table for which the name matches the LIKE pattern. 7983 */ 7984 char *zExpr = sqlite3_mprintf( 7985 "name LIKE %Q ESCAPE '\\' OR EXISTS (" 7986 " SELECT 1 FROM sqlite_schema WHERE " 7987 " name LIKE %Q ESCAPE '\\' AND" 7988 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND" 7989 " substr(o.name, 1, length(name)+1) == (name||'_')" 7990 ")", azArg[i], azArg[i] 7991 ); 7992 7993 if( zLike ){ 7994 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr); 7995 }else{ 7996 zLike = zExpr; 7997 } 7998 } 7999 } 8000 8001 open_db(p, 0); 8002 8003 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8004 /* When playing back a "dump", the content might appear in an order 8005 ** which causes immediate foreign key constraints to be violated. 8006 ** So disable foreign-key constraint enforcement to prevent problems. */ 8007 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); 8008 raw_printf(p->out, "BEGIN TRANSACTION;\n"); 8009 } 8010 p->writableSchema = 0; 8011 p->showHeader = 0; 8012 /* Set writable_schema=ON since doing so forces SQLite to initialize 8013 ** as much of the schema as it can even if the sqlite_schema table is 8014 ** corrupt. */ 8015 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); 8016 p->nErr = 0; 8017 if( zLike==0 ) zLike = sqlite3_mprintf("true"); 8018 zSql = sqlite3_mprintf( 8019 "SELECT name, type, sql FROM sqlite_schema AS o " 8020 "WHERE (%s) AND type=='table'" 8021 " AND sql NOT NULL" 8022 " ORDER BY tbl_name='sqlite_sequence', rowid", 8023 zLike 8024 ); 8025 run_schema_dump_query(p,zSql); 8026 sqlite3_free(zSql); 8027 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8028 zSql = sqlite3_mprintf( 8029 "SELECT sql FROM sqlite_schema AS o " 8030 "WHERE (%s) AND sql NOT NULL" 8031 " AND type IN ('index','trigger','view')", 8032 zLike 8033 ); 8034 run_table_dump_query(p, zSql); 8035 sqlite3_free(zSql); 8036 } 8037 sqlite3_free(zLike); 8038 if( p->writableSchema ){ 8039 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); 8040 p->writableSchema = 0; 8041 } 8042 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 8043 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); 8044 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8045 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); 8046 } 8047 p->showHeader = savedShowHeader; 8048 p->shellFlgs = savedShellFlags; 8049 }else 8050 8051 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ 8052 if( nArg==2 ){ 8053 setOrClearFlag(p, SHFLG_Echo, azArg[1]); 8054 }else{ 8055 raw_printf(stderr, "Usage: .echo on|off\n"); 8056 rc = 1; 8057 } 8058 }else 8059 8060 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ 8061 if( nArg==2 ){ 8062 p->autoEQPtest = 0; 8063 if( p->autoEQPtrace ){ 8064 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); 8065 p->autoEQPtrace = 0; 8066 } 8067 if( strcmp(azArg[1],"full")==0 ){ 8068 p->autoEQP = AUTOEQP_full; 8069 }else if( strcmp(azArg[1],"trigger")==0 ){ 8070 p->autoEQP = AUTOEQP_trigger; 8071#ifdef SQLITE_DEBUG 8072 }else if( strcmp(azArg[1],"test")==0 ){ 8073 p->autoEQP = AUTOEQP_on; 8074 p->autoEQPtest = 1; 8075 }else if( strcmp(azArg[1],"trace")==0 ){ 8076 p->autoEQP = AUTOEQP_full; 8077 p->autoEQPtrace = 1; 8078 open_db(p, 0); 8079 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); 8080 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); 8081#endif 8082 }else{ 8083 p->autoEQP = (u8)booleanValue(azArg[1]); 8084 } 8085 }else{ 8086 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); 8087 rc = 1; 8088 } 8089 }else 8090 8091#ifndef SQLITE_SHELL_FIDDLE 8092 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ 8093 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); 8094 rc = 2; 8095 }else 8096#endif 8097 8098 /* The ".explain" command is automatic now. It is largely pointless. It 8099 ** retained purely for backwards compatibility */ 8100 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ 8101 int val = 1; 8102 if( nArg>=2 ){ 8103 if( strcmp(azArg[1],"auto")==0 ){ 8104 val = 99; 8105 }else{ 8106 val = booleanValue(azArg[1]); 8107 } 8108 } 8109 if( val==1 && p->mode!=MODE_Explain ){ 8110 p->normalMode = p->mode; 8111 p->mode = MODE_Explain; 8112 p->autoExplain = 0; 8113 }else if( val==0 ){ 8114 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 8115 p->autoExplain = 0; 8116 }else if( val==99 ){ 8117 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 8118 p->autoExplain = 1; 8119 } 8120 }else 8121 8122#ifndef SQLITE_OMIT_VIRTUALTABLE 8123 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ 8124 if( p->bSafeMode ){ 8125 raw_printf(stderr, 8126 "Cannot run experimental commands such as \"%s\" in safe mode\n", 8127 azArg[0]); 8128 rc = 1; 8129 }else{ 8130 open_db(p, 0); 8131 expertDotCommand(p, azArg, nArg); 8132 } 8133 }else 8134#endif 8135 8136 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ 8137 static const struct { 8138 const char *zCtrlName; /* Name of a test-control option */ 8139 int ctrlCode; /* Integer code for that option */ 8140 const char *zUsage; /* Usage notes */ 8141 } aCtrl[] = { 8142 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, 8143 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, 8144 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, 8145 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, 8146 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, 8147 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ 8148 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, 8149 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, 8150 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, 8151 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, 8152 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ 8153 }; 8154 int filectrl = -1; 8155 int iCtrl = -1; 8156 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ 8157 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ 8158 int n2, i; 8159 const char *zCmd = 0; 8160 const char *zSchema = 0; 8161 8162 open_db(p, 0); 8163 zCmd = nArg>=2 ? azArg[1] : "help"; 8164 8165 if( zCmd[0]=='-' 8166 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0) 8167 && nArg>=4 8168 ){ 8169 zSchema = azArg[2]; 8170 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; 8171 nArg -= 2; 8172 zCmd = azArg[1]; 8173 } 8174 8175 /* The argument can optionally begin with "-" or "--" */ 8176 if( zCmd[0]=='-' && zCmd[1] ){ 8177 zCmd++; 8178 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 8179 } 8180 8181 /* --help lists all file-controls */ 8182 if( strcmp(zCmd,"help")==0 ){ 8183 utf8_printf(p->out, "Available file-controls:\n"); 8184 for(i=0; i<ArraySize(aCtrl); i++){ 8185 utf8_printf(p->out, " .filectrl %s %s\n", 8186 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 8187 } 8188 rc = 1; 8189 goto meta_command_exit; 8190 } 8191 8192 /* convert filectrl text option to value. allow any unique prefix 8193 ** of the option name, or a numerical value. */ 8194 n2 = strlen30(zCmd); 8195 for(i=0; i<ArraySize(aCtrl); i++){ 8196 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 8197 if( filectrl<0 ){ 8198 filectrl = aCtrl[i].ctrlCode; 8199 iCtrl = i; 8200 }else{ 8201 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" 8202 "Use \".filectrl --help\" for help\n", zCmd); 8203 rc = 1; 8204 goto meta_command_exit; 8205 } 8206 } 8207 } 8208 if( filectrl<0 ){ 8209 utf8_printf(stderr,"Error: unknown file-control: %s\n" 8210 "Use \".filectrl --help\" for help\n", zCmd); 8211 }else{ 8212 switch(filectrl){ 8213 case SQLITE_FCNTL_SIZE_LIMIT: { 8214 if( nArg!=2 && nArg!=3 ) break; 8215 iRes = nArg==3 ? integerValue(azArg[2]) : -1; 8216 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); 8217 isOk = 1; 8218 break; 8219 } 8220 case SQLITE_FCNTL_LOCK_TIMEOUT: 8221 case SQLITE_FCNTL_CHUNK_SIZE: { 8222 int x; 8223 if( nArg!=3 ) break; 8224 x = (int)integerValue(azArg[2]); 8225 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8226 isOk = 2; 8227 break; 8228 } 8229 case SQLITE_FCNTL_PERSIST_WAL: 8230 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { 8231 int x; 8232 if( nArg!=2 && nArg!=3 ) break; 8233 x = nArg==3 ? booleanValue(azArg[2]) : -1; 8234 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8235 iRes = x; 8236 isOk = 1; 8237 break; 8238 } 8239 case SQLITE_FCNTL_DATA_VERSION: 8240 case SQLITE_FCNTL_HAS_MOVED: { 8241 int x; 8242 if( nArg!=2 ) break; 8243 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8244 iRes = x; 8245 isOk = 1; 8246 break; 8247 } 8248 case SQLITE_FCNTL_TEMPFILENAME: { 8249 char *z = 0; 8250 if( nArg!=2 ) break; 8251 sqlite3_file_control(p->db, zSchema, filectrl, &z); 8252 if( z ){ 8253 utf8_printf(p->out, "%s\n", z); 8254 sqlite3_free(z); 8255 } 8256 isOk = 2; 8257 break; 8258 } 8259 case SQLITE_FCNTL_RESERVE_BYTES: { 8260 int x; 8261 if( nArg>=3 ){ 8262 x = atoi(azArg[2]); 8263 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8264 } 8265 x = -1; 8266 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8267 utf8_printf(p->out,"%d\n", x); 8268 isOk = 2; 8269 break; 8270 } 8271 } 8272 } 8273 if( isOk==0 && iCtrl>=0 ){ 8274 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 8275 rc = 1; 8276 }else if( isOk==1 ){ 8277 char zBuf[100]; 8278 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); 8279 raw_printf(p->out, "%s\n", zBuf); 8280 } 8281 }else 8282 8283 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ 8284 ShellState data; 8285 int doStats = 0; 8286 memcpy(&data, p, sizeof(data)); 8287 data.showHeader = 0; 8288 data.cMode = data.mode = MODE_Semi; 8289 if( nArg==2 && optionMatch(azArg[1], "indent") ){ 8290 data.cMode = data.mode = MODE_Pretty; 8291 nArg = 1; 8292 } 8293 if( nArg!=1 ){ 8294 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); 8295 rc = 1; 8296 goto meta_command_exit; 8297 } 8298 open_db(p, 0); 8299 rc = sqlite3_exec(p->db, 8300 "SELECT sql FROM" 8301 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 8302 " FROM sqlite_schema UNION ALL" 8303 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " 8304 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 8305 "ORDER BY x", 8306 callback, &data, 0 8307 ); 8308 if( rc==SQLITE_OK ){ 8309 sqlite3_stmt *pStmt; 8310 rc = sqlite3_prepare_v2(p->db, 8311 "SELECT rowid FROM sqlite_schema" 8312 " WHERE name GLOB 'sqlite_stat[134]'", 8313 -1, &pStmt, 0); 8314 doStats = sqlite3_step(pStmt)==SQLITE_ROW; 8315 sqlite3_finalize(pStmt); 8316 } 8317 if( doStats==0 ){ 8318 raw_printf(p->out, "/* No STAT tables available */\n"); 8319 }else{ 8320 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 8321 data.cMode = data.mode = MODE_Insert; 8322 data.zDestTable = "sqlite_stat1"; 8323 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); 8324 data.zDestTable = "sqlite_stat4"; 8325 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); 8326 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 8327 } 8328 }else 8329 8330 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ 8331 if( nArg==2 ){ 8332 p->showHeader = booleanValue(azArg[1]); 8333 p->shellFlgs |= SHFLG_HeaderSet; 8334 }else{ 8335 raw_printf(stderr, "Usage: .headers on|off\n"); 8336 rc = 1; 8337 } 8338 }else 8339 8340 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ 8341 if( nArg>=2 ){ 8342 n = showHelp(p->out, azArg[1]); 8343 if( n==0 ){ 8344 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); 8345 } 8346 }else{ 8347 showHelp(p->out, 0); 8348 } 8349 }else 8350 8351#ifndef SQLITE_SHELL_FIDDLE 8352 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ 8353 char *zTable = 0; /* Insert data into this table */ 8354 char *zSchema = 0; /* within this schema (may default to "main") */ 8355 char *zFile = 0; /* Name of file to extra content from */ 8356 sqlite3_stmt *pStmt = NULL; /* A statement */ 8357 int nCol; /* Number of columns in the table */ 8358 int nByte; /* Number of bytes in an SQL string */ 8359 int i, j; /* Loop counters */ 8360 int needCommit; /* True to COMMIT or ROLLBACK at end */ 8361 int nSep; /* Number of bytes in p->colSeparator[] */ 8362 char *zSql; /* An SQL statement */ 8363 char *zFullTabName; /* Table name with schema if applicable */ 8364 ImportCtx sCtx; /* Reader context */ 8365 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ 8366 int eVerbose = 0; /* Larger for more console output */ 8367 int nSkip = 0; /* Initial lines to skip */ 8368 int useOutputMode = 1; /* Use output mode to determine separators */ 8369 char *zCreate = 0; /* CREATE TABLE statement text */ 8370 8371 failIfSafeMode(p, "cannot run .import in safe mode"); 8372 memset(&sCtx, 0, sizeof(sCtx)); 8373 if( p->mode==MODE_Ascii ){ 8374 xRead = ascii_read_one_field; 8375 }else{ 8376 xRead = csv_read_one_field; 8377 } 8378 rc = 1; 8379 for(i=1; i<nArg; i++){ 8380 char *z = azArg[i]; 8381 if( z[0]=='-' && z[1]=='-' ) z++; 8382 if( z[0]!='-' ){ 8383 if( zFile==0 ){ 8384 zFile = z; 8385 }else if( zTable==0 ){ 8386 zTable = z; 8387 }else{ 8388 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); 8389 showHelp(p->out, "import"); 8390 goto meta_command_exit; 8391 } 8392 }else if( strcmp(z,"-v")==0 ){ 8393 eVerbose++; 8394 }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){ 8395 zSchema = azArg[++i]; 8396 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){ 8397 nSkip = integerValue(azArg[++i]); 8398 }else if( strcmp(z,"-ascii")==0 ){ 8399 sCtx.cColSep = SEP_Unit[0]; 8400 sCtx.cRowSep = SEP_Record[0]; 8401 xRead = ascii_read_one_field; 8402 useOutputMode = 0; 8403 }else if( strcmp(z,"-csv")==0 ){ 8404 sCtx.cColSep = ','; 8405 sCtx.cRowSep = '\n'; 8406 xRead = csv_read_one_field; 8407 useOutputMode = 0; 8408 }else{ 8409 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); 8410 showHelp(p->out, "import"); 8411 goto meta_command_exit; 8412 } 8413 } 8414 if( zTable==0 ){ 8415 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", 8416 zFile==0 ? "FILE" : "TABLE"); 8417 showHelp(p->out, "import"); 8418 goto meta_command_exit; 8419 } 8420 seenInterrupt = 0; 8421 open_db(p, 0); 8422 if( useOutputMode ){ 8423 /* If neither the --csv or --ascii options are specified, then set 8424 ** the column and row separator characters from the output mode. */ 8425 nSep = strlen30(p->colSeparator); 8426 if( nSep==0 ){ 8427 raw_printf(stderr, 8428 "Error: non-null column separator required for import\n"); 8429 goto meta_command_exit; 8430 } 8431 if( nSep>1 ){ 8432 raw_printf(stderr, 8433 "Error: multi-character column separators not allowed" 8434 " for import\n"); 8435 goto meta_command_exit; 8436 } 8437 nSep = strlen30(p->rowSeparator); 8438 if( nSep==0 ){ 8439 raw_printf(stderr, 8440 "Error: non-null row separator required for import\n"); 8441 goto meta_command_exit; 8442 } 8443 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){ 8444 /* When importing CSV (only), if the row separator is set to the 8445 ** default output row separator, change it to the default input 8446 ** row separator. This avoids having to maintain different input 8447 ** and output row separators. */ 8448 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8449 nSep = strlen30(p->rowSeparator); 8450 } 8451 if( nSep>1 ){ 8452 raw_printf(stderr, "Error: multi-character row separators not allowed" 8453 " for import\n"); 8454 goto meta_command_exit; 8455 } 8456 sCtx.cColSep = p->colSeparator[0]; 8457 sCtx.cRowSep = p->rowSeparator[0]; 8458 } 8459 sCtx.zFile = zFile; 8460 sCtx.nLine = 1; 8461 if( sCtx.zFile[0]=='|' ){ 8462#ifdef SQLITE_OMIT_POPEN 8463 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8464 goto meta_command_exit; 8465#else 8466 sCtx.in = popen(sCtx.zFile+1, "r"); 8467 sCtx.zFile = "<pipe>"; 8468 sCtx.xCloser = pclose; 8469#endif 8470 }else{ 8471 sCtx.in = fopen(sCtx.zFile, "rb"); 8472 sCtx.xCloser = fclose; 8473 } 8474 if( sCtx.in==0 ){ 8475 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 8476 goto meta_command_exit; 8477 } 8478 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ 8479 char zSep[2]; 8480 zSep[1] = 0; 8481 zSep[0] = sCtx.cColSep; 8482 utf8_printf(p->out, "Column separator "); 8483 output_c_string(p->out, zSep); 8484 utf8_printf(p->out, ", row separator "); 8485 zSep[0] = sCtx.cRowSep; 8486 output_c_string(p->out, zSep); 8487 utf8_printf(p->out, "\n"); 8488 } 8489 sCtx.z = sqlite3_malloc64(120); 8490 if( sCtx.z==0 ){ 8491 import_cleanup(&sCtx); 8492 shell_out_of_memory(); 8493 } 8494 /* Below, resources must be freed before exit. */ 8495 while( (nSkip--)>0 ){ 8496 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} 8497 } 8498 if( zSchema!=0 ){ 8499 zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable); 8500 }else{ 8501 zFullTabName = sqlite3_mprintf("\"%w\"", zTable); 8502 } 8503 zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName); 8504 if( zSql==0 || zFullTabName==0 ){ 8505 import_cleanup(&sCtx); 8506 shell_out_of_memory(); 8507 } 8508 nByte = strlen30(zSql); 8509 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8510 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ 8511 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ 8512 sqlite3 *dbCols = 0; 8513 char *zRenames = 0; 8514 char *zColDefs; 8515 zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName); 8516 while( xRead(&sCtx) ){ 8517 zAutoColumn(sCtx.z, &dbCols, 0); 8518 if( sCtx.cTerm!=sCtx.cColSep ) break; 8519 } 8520 zColDefs = zAutoColumn(0, &dbCols, &zRenames); 8521 if( zRenames!=0 ){ 8522 utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr, 8523 "Columns renamed during .import %s due to duplicates:\n" 8524 "%s\n", sCtx.zFile, zRenames); 8525 sqlite3_free(zRenames); 8526 } 8527 assert(dbCols==0); 8528 if( zColDefs==0 ){ 8529 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); 8530 import_fail: 8531 sqlite3_free(zCreate); 8532 sqlite3_free(zSql); 8533 sqlite3_free(zFullTabName); 8534 import_cleanup(&sCtx); 8535 rc = 1; 8536 goto meta_command_exit; 8537 } 8538 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); 8539 if( eVerbose>=1 ){ 8540 utf8_printf(p->out, "%s\n", zCreate); 8541 } 8542 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); 8543 if( rc ){ 8544 utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); 8545 goto import_fail; 8546 } 8547 sqlite3_free(zCreate); 8548 zCreate = 0; 8549 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8550 } 8551 if( rc ){ 8552 if (pStmt) sqlite3_finalize(pStmt); 8553 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); 8554 goto import_fail; 8555 } 8556 sqlite3_free(zSql); 8557 nCol = sqlite3_column_count(pStmt); 8558 sqlite3_finalize(pStmt); 8559 pStmt = 0; 8560 if( nCol==0 ) return 0; /* no columns, no error */ 8561 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); 8562 if( zSql==0 ){ 8563 import_cleanup(&sCtx); 8564 shell_out_of_memory(); 8565 } 8566 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName); 8567 j = strlen30(zSql); 8568 for(i=1; i<nCol; i++){ 8569 zSql[j++] = ','; 8570 zSql[j++] = '?'; 8571 } 8572 zSql[j++] = ')'; 8573 zSql[j] = 0; 8574 if( eVerbose>=2 ){ 8575 utf8_printf(p->out, "Insert using: %s\n", zSql); 8576 } 8577 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8578 if( rc ){ 8579 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8580 if (pStmt) sqlite3_finalize(pStmt); 8581 goto import_fail; 8582 } 8583 sqlite3_free(zSql); 8584 sqlite3_free(zFullTabName); 8585 needCommit = sqlite3_get_autocommit(p->db); 8586 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); 8587 do{ 8588 int startLine = sCtx.nLine; 8589 for(i=0; i<nCol; i++){ 8590 char *z = xRead(&sCtx); 8591 /* 8592 ** Did we reach end-of-file before finding any columns? 8593 ** If so, stop instead of NULL filling the remaining columns. 8594 */ 8595 if( z==0 && i==0 ) break; 8596 /* 8597 ** Did we reach end-of-file OR end-of-line before finding any 8598 ** columns in ASCII mode? If so, stop instead of NULL filling 8599 ** the remaining columns. 8600 */ 8601 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; 8602 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); 8603 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ 8604 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8605 "filling the rest with NULL\n", 8606 sCtx.zFile, startLine, nCol, i+1); 8607 i += 2; 8608 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } 8609 } 8610 } 8611 if( sCtx.cTerm==sCtx.cColSep ){ 8612 do{ 8613 xRead(&sCtx); 8614 i++; 8615 }while( sCtx.cTerm==sCtx.cColSep ); 8616 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8617 "extras ignored\n", 8618 sCtx.zFile, startLine, nCol, i); 8619 } 8620 if( i>=nCol ){ 8621 sqlite3_step(pStmt); 8622 rc = sqlite3_reset(pStmt); 8623 if( rc!=SQLITE_OK ){ 8624 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, 8625 startLine, sqlite3_errmsg(p->db)); 8626 sCtx.nErr++; 8627 }else{ 8628 sCtx.nRow++; 8629 } 8630 } 8631 }while( sCtx.cTerm!=EOF ); 8632 8633 import_cleanup(&sCtx); 8634 sqlite3_finalize(pStmt); 8635 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); 8636 if( eVerbose>0 ){ 8637 utf8_printf(p->out, 8638 "Added %d rows with %d errors using %d lines of input\n", 8639 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); 8640 } 8641 }else 8642#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 8643 8644#ifndef SQLITE_UNTESTABLE 8645 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ 8646 char *zSql; 8647 char *zCollist = 0; 8648 sqlite3_stmt *pStmt; 8649 int tnum = 0; 8650 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ 8651 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ 8652 int i; 8653 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ 8654 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" 8655 " .imposter off\n"); 8656 /* Also allowed, but not documented: 8657 ** 8658 ** .imposter TABLE IMPOSTER 8659 ** 8660 ** where TABLE is a WITHOUT ROWID table. In that case, the 8661 ** imposter is another WITHOUT ROWID table with the columns in 8662 ** storage order. */ 8663 rc = 1; 8664 goto meta_command_exit; 8665 } 8666 open_db(p, 0); 8667 if( nArg==2 ){ 8668 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); 8669 goto meta_command_exit; 8670 } 8671 zSql = sqlite3_mprintf( 8672 "SELECT rootpage, 0 FROM sqlite_schema" 8673 " WHERE name='%q' AND type='index'" 8674 "UNION ALL " 8675 "SELECT rootpage, 1 FROM sqlite_schema" 8676 " WHERE name='%q' AND type='table'" 8677 " AND sql LIKE '%%without%%rowid%%'", 8678 azArg[1], azArg[1] 8679 ); 8680 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8681 sqlite3_free(zSql); 8682 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 8683 tnum = sqlite3_column_int(pStmt, 0); 8684 isWO = sqlite3_column_int(pStmt, 1); 8685 } 8686 sqlite3_finalize(pStmt); 8687 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); 8688 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8689 sqlite3_free(zSql); 8690 i = 0; 8691 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 8692 char zLabel[20]; 8693 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); 8694 i++; 8695 if( zCol==0 ){ 8696 if( sqlite3_column_int(pStmt,1)==-1 ){ 8697 zCol = "_ROWID_"; 8698 }else{ 8699 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); 8700 zCol = zLabel; 8701 } 8702 } 8703 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ 8704 lenPK = (int)strlen(zCollist); 8705 } 8706 if( zCollist==0 ){ 8707 zCollist = sqlite3_mprintf("\"%w\"", zCol); 8708 }else{ 8709 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); 8710 } 8711 } 8712 sqlite3_finalize(pStmt); 8713 if( i==0 || tnum==0 ){ 8714 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); 8715 rc = 1; 8716 sqlite3_free(zCollist); 8717 goto meta_command_exit; 8718 } 8719 if( lenPK==0 ) lenPK = 100000; 8720 zSql = sqlite3_mprintf( 8721 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", 8722 azArg[2], zCollist, lenPK, zCollist); 8723 sqlite3_free(zCollist); 8724 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); 8725 if( rc==SQLITE_OK ){ 8726 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 8727 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); 8728 if( rc ){ 8729 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); 8730 }else{ 8731 utf8_printf(stdout, "%s;\n", zSql); 8732 raw_printf(stdout, 8733 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", 8734 azArg[1], isWO ? "table" : "index" 8735 ); 8736 } 8737 }else{ 8738 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); 8739 rc = 1; 8740 } 8741 sqlite3_free(zSql); 8742 }else 8743#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ 8744 8745#ifdef SQLITE_ENABLE_IOTRACE 8746 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ 8747 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); 8748 if( iotrace && iotrace!=stdout ) fclose(iotrace); 8749 iotrace = 0; 8750 if( nArg<2 ){ 8751 sqlite3IoTrace = 0; 8752 }else if( strcmp(azArg[1], "-")==0 ){ 8753 sqlite3IoTrace = iotracePrintf; 8754 iotrace = stdout; 8755 }else{ 8756 iotrace = fopen(azArg[1], "w"); 8757 if( iotrace==0 ){ 8758 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 8759 sqlite3IoTrace = 0; 8760 rc = 1; 8761 }else{ 8762 sqlite3IoTrace = iotracePrintf; 8763 } 8764 } 8765 }else 8766#endif 8767 8768 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ 8769 static const struct { 8770 const char *zLimitName; /* Name of a limit */ 8771 int limitCode; /* Integer code for that limit */ 8772 } aLimit[] = { 8773 { "length", SQLITE_LIMIT_LENGTH }, 8774 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, 8775 { "column", SQLITE_LIMIT_COLUMN }, 8776 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, 8777 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, 8778 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, 8779 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, 8780 { "attached", SQLITE_LIMIT_ATTACHED }, 8781 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 8782 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, 8783 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, 8784 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, 8785 }; 8786 int i, n2; 8787 open_db(p, 0); 8788 if( nArg==1 ){ 8789 for(i=0; i<ArraySize(aLimit); i++){ 8790 printf("%20s %d\n", aLimit[i].zLimitName, 8791 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); 8792 } 8793 }else if( nArg>3 ){ 8794 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); 8795 rc = 1; 8796 goto meta_command_exit; 8797 }else{ 8798 int iLimit = -1; 8799 n2 = strlen30(azArg[1]); 8800 for(i=0; i<ArraySize(aLimit); i++){ 8801 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ 8802 if( iLimit<0 ){ 8803 iLimit = i; 8804 }else{ 8805 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); 8806 rc = 1; 8807 goto meta_command_exit; 8808 } 8809 } 8810 } 8811 if( iLimit<0 ){ 8812 utf8_printf(stderr, "unknown limit: \"%s\"\n" 8813 "enter \".limits\" with no arguments for a list.\n", 8814 azArg[1]); 8815 rc = 1; 8816 goto meta_command_exit; 8817 } 8818 if( nArg==3 ){ 8819 sqlite3_limit(p->db, aLimit[iLimit].limitCode, 8820 (int)integerValue(azArg[2])); 8821 } 8822 printf("%20s %d\n", aLimit[iLimit].zLimitName, 8823 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); 8824 } 8825 }else 8826 8827 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ 8828 open_db(p, 0); 8829 lintDotCommand(p, azArg, nArg); 8830 }else 8831 8832#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) 8833 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ 8834 const char *zFile, *zProc; 8835 char *zErrMsg = 0; 8836 failIfSafeMode(p, "cannot run .load in safe mode"); 8837 if( nArg<2 ){ 8838 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); 8839 rc = 1; 8840 goto meta_command_exit; 8841 } 8842 zFile = azArg[1]; 8843 zProc = nArg>=3 ? azArg[2] : 0; 8844 open_db(p, 0); 8845 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); 8846 if( rc!=SQLITE_OK ){ 8847 utf8_printf(stderr, "Error: %s\n", zErrMsg); 8848 sqlite3_free(zErrMsg); 8849 rc = 1; 8850 } 8851 }else 8852#endif 8853 8854#ifndef SQLITE_SHELL_FIDDLE 8855 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ 8856 failIfSafeMode(p, "cannot run .log in safe mode"); 8857 if( nArg!=2 ){ 8858 raw_printf(stderr, "Usage: .log FILENAME\n"); 8859 rc = 1; 8860 }else{ 8861 const char *zFile = azArg[1]; 8862 output_file_close(p->pLog); 8863 p->pLog = output_file_open(zFile, 0); 8864 } 8865 }else 8866#endif 8867 8868 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ 8869 const char *zMode = 0; 8870 const char *zTabname = 0; 8871 int i, n2; 8872 ColModeOpts cmOpts = ColModeOpts_default; 8873 for(i=1; i<nArg; i++){ 8874 const char *z = azArg[i]; 8875 if( optionMatch(z,"wrap") && i+1<nArg ){ 8876 cmOpts.iWrap = integerValue(azArg[++i]); 8877 }else if( optionMatch(z,"ww") ){ 8878 cmOpts.bWordWrap = 1; 8879 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){ 8880 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]); 8881 }else if( optionMatch(z,"quote") ){ 8882 cmOpts.bQuote = 1; 8883 }else if( optionMatch(z,"noquote") ){ 8884 cmOpts.bQuote = 0; 8885 }else if( zMode==0 ){ 8886 zMode = z; 8887 /* Apply defaults for qbox pseudo-mods. If that 8888 * overwrites already-set values, user was informed of this. 8889 */ 8890 if( strcmp(z, "qbox")==0 ){ 8891 ColModeOpts cmo = ColModeOpts_default_qbox; 8892 zMode = "box"; 8893 cmOpts = cmo; 8894 } 8895 }else if( zTabname==0 ){ 8896 zTabname = z; 8897 }else if( z[0]=='-' ){ 8898 utf8_printf(stderr, "unknown option: %s\n", z); 8899 utf8_printf(stderr, "options:\n" 8900 " --noquote\n" 8901 " --quote\n" 8902 " --wordwrap on/off\n" 8903 " --wrap N\n" 8904 " --ww\n"); 8905 rc = 1; 8906 goto meta_command_exit; 8907 }else{ 8908 utf8_printf(stderr, "extra argument: \"%s\"\n", z); 8909 rc = 1; 8910 goto meta_command_exit; 8911 } 8912 } 8913 if( zMode==0 ){ 8914 if( p->mode==MODE_Column 8915 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) 8916 ){ 8917 raw_printf 8918 (p->out, 8919 "current output mode: %s --wrap %d --wordwrap %s --%squote\n", 8920 modeDescr[p->mode], p->cmOpts.iWrap, 8921 p->cmOpts.bWordWrap ? "on" : "off", 8922 p->cmOpts.bQuote ? "" : "no"); 8923 }else{ 8924 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); 8925 } 8926 zMode = modeDescr[p->mode]; 8927 } 8928 n2 = strlen30(zMode); 8929 if( strncmp(zMode,"lines",n2)==0 ){ 8930 p->mode = MODE_Line; 8931 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8932 }else if( strncmp(zMode,"columns",n2)==0 ){ 8933 p->mode = MODE_Column; 8934 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ 8935 p->showHeader = 1; 8936 } 8937 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8938 p->cmOpts = cmOpts; 8939 }else if( strncmp(zMode,"list",n2)==0 ){ 8940 p->mode = MODE_List; 8941 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); 8942 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8943 }else if( strncmp(zMode,"html",n2)==0 ){ 8944 p->mode = MODE_Html; 8945 }else if( strncmp(zMode,"tcl",n2)==0 ){ 8946 p->mode = MODE_Tcl; 8947 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); 8948 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8949 }else if( strncmp(zMode,"csv",n2)==0 ){ 8950 p->mode = MODE_Csv; 8951 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8952 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8953 }else if( strncmp(zMode,"tabs",n2)==0 ){ 8954 p->mode = MODE_List; 8955 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); 8956 }else if( strncmp(zMode,"insert",n2)==0 ){ 8957 p->mode = MODE_Insert; 8958 set_table_name(p, zTabname ? zTabname : "table"); 8959 }else if( strncmp(zMode,"quote",n2)==0 ){ 8960 p->mode = MODE_Quote; 8961 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8962 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8963 }else if( strncmp(zMode,"ascii",n2)==0 ){ 8964 p->mode = MODE_Ascii; 8965 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); 8966 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); 8967 }else if( strncmp(zMode,"markdown",n2)==0 ){ 8968 p->mode = MODE_Markdown; 8969 p->cmOpts = cmOpts; 8970 }else if( strncmp(zMode,"table",n2)==0 ){ 8971 p->mode = MODE_Table; 8972 p->cmOpts = cmOpts; 8973 }else if( strncmp(zMode,"box",n2)==0 ){ 8974 p->mode = MODE_Box; 8975 p->cmOpts = cmOpts; 8976 }else if( strncmp(zMode,"count",n2)==0 ){ 8977 p->mode = MODE_Count; 8978 }else if( strncmp(zMode,"off",n2)==0 ){ 8979 p->mode = MODE_Off; 8980 }else if( strncmp(zMode,"json",n2)==0 ){ 8981 p->mode = MODE_Json; 8982 }else{ 8983 raw_printf(stderr, "Error: mode should be one of: " 8984 "ascii box column csv html insert json line list markdown " 8985 "qbox quote table tabs tcl\n"); 8986 rc = 1; 8987 } 8988 p->cMode = p->mode; 8989 }else 8990 8991#ifndef SQLITE_SHELL_FIDDLE 8992 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){ 8993 if( nArg!=2 ){ 8994 raw_printf(stderr, "Usage: .nonce NONCE\n"); 8995 rc = 1; 8996 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){ 8997 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", 8998 p->lineno, azArg[1]); 8999 exit(1); 9000 }else{ 9001 p->bSafeMode = 0; 9002 return 0; /* Return immediately to bypass the safe mode reset 9003 ** at the end of this procedure */ 9004 } 9005 }else 9006#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9007 9008 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ 9009 if( nArg==2 ){ 9010 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, 9011 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); 9012 }else{ 9013 raw_printf(stderr, "Usage: .nullvalue STRING\n"); 9014 rc = 1; 9015 } 9016 }else 9017 9018 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ 9019 const char *zFN = 0; /* Pointer to constant filename */ 9020 char *zNewFilename = 0; /* Name of the database file to open */ 9021 int iName = 1; /* Index in azArg[] of the filename */ 9022 int newFlag = 0; /* True to delete file before opening */ 9023 int openMode = SHELL_OPEN_UNSPEC; 9024 9025 /* Check for command-line arguments */ 9026 for(iName=1; iName<nArg; iName++){ 9027 const char *z = azArg[iName]; 9028#ifndef SQLITE_SHELL_FIDDLE 9029 if( optionMatch(z,"new") ){ 9030 newFlag = 1; 9031#ifdef SQLITE_HAVE_ZLIB 9032 }else if( optionMatch(z, "zip") ){ 9033 openMode = SHELL_OPEN_ZIPFILE; 9034#endif 9035 }else if( optionMatch(z, "append") ){ 9036 openMode = SHELL_OPEN_APPENDVFS; 9037 }else if( optionMatch(z, "readonly") ){ 9038 openMode = SHELL_OPEN_READONLY; 9039 }else if( optionMatch(z, "nofollow") ){ 9040 p->openFlags |= SQLITE_OPEN_NOFOLLOW; 9041#ifndef SQLITE_OMIT_DESERIALIZE 9042 }else if( optionMatch(z, "deserialize") ){ 9043 openMode = SHELL_OPEN_DESERIALIZE; 9044 }else if( optionMatch(z, "hexdb") ){ 9045 openMode = SHELL_OPEN_HEXDB; 9046 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ 9047 p->szMax = integerValue(azArg[++iName]); 9048#endif /* SQLITE_OMIT_DESERIALIZE */ 9049 }else 9050#endif /* !SQLITE_SHELL_FIDDLE */ 9051 if( z[0]=='-' ){ 9052 utf8_printf(stderr, "unknown option: %s\n", z); 9053 rc = 1; 9054 goto meta_command_exit; 9055 }else if( zFN ){ 9056 utf8_printf(stderr, "extra argument: \"%s\"\n", z); 9057 rc = 1; 9058 goto meta_command_exit; 9059 }else{ 9060 zFN = z; 9061 } 9062 } 9063 9064 /* Close the existing database */ 9065 session_close_all(p, -1); 9066 close_db(p->db); 9067 p->db = 0; 9068 p->pAuxDb->zDbFilename = 0; 9069 sqlite3_free(p->pAuxDb->zFreeOnClose); 9070 p->pAuxDb->zFreeOnClose = 0; 9071 p->openMode = openMode; 9072 p->openFlags = 0; 9073 p->szMax = 0; 9074 9075 /* If a filename is specified, try to open it first */ 9076 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ 9077 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); 9078#ifndef SQLITE_SHELL_FIDDLE 9079 if( p->bSafeMode 9080 && p->openMode!=SHELL_OPEN_HEXDB 9081 && zFN 9082 && strcmp(zFN,":memory:")!=0 9083 ){ 9084 failIfSafeMode(p, "cannot open disk-based database files in safe mode"); 9085 } 9086#else 9087 /* WASM mode has its own sandboxed pseudo-filesystem. */ 9088#endif 9089 if( zFN ){ 9090 zNewFilename = sqlite3_mprintf("%s", zFN); 9091 shell_check_oom(zNewFilename); 9092 }else{ 9093 zNewFilename = 0; 9094 } 9095 p->pAuxDb->zDbFilename = zNewFilename; 9096 open_db(p, OPEN_DB_KEEPALIVE); 9097 if( p->db==0 ){ 9098 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); 9099 sqlite3_free(zNewFilename); 9100 }else{ 9101 p->pAuxDb->zFreeOnClose = zNewFilename; 9102 } 9103 } 9104 if( p->db==0 ){ 9105 /* As a fall-back open a TEMP database */ 9106 p->pAuxDb->zDbFilename = 0; 9107 open_db(p, 0); 9108 } 9109 }else 9110 9111#ifndef SQLITE_SHELL_FIDDLE 9112 if( (c=='o' 9113 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) 9114 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) 9115 ){ 9116 char *zFile = 0; 9117 int bTxtMode = 0; 9118 int i; 9119 int eMode = 0; 9120 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ 9121 unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */ 9122 9123 zBOM[0] = 0; 9124 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 9125 if( c=='e' ){ 9126 eMode = 'x'; 9127 bOnce = 2; 9128 }else if( strncmp(azArg[0],"once",n)==0 ){ 9129 bOnce = 1; 9130 } 9131 for(i=1; i<nArg; i++){ 9132 char *z = azArg[i]; 9133 if( z[0]=='-' ){ 9134 if( z[1]=='-' ) z++; 9135 if( strcmp(z,"-bom")==0 ){ 9136 zBOM[0] = 0xef; 9137 zBOM[1] = 0xbb; 9138 zBOM[2] = 0xbf; 9139 zBOM[3] = 0; 9140 }else if( c!='e' && strcmp(z,"-x")==0 ){ 9141 eMode = 'x'; /* spreadsheet */ 9142 }else if( c!='e' && strcmp(z,"-e")==0 ){ 9143 eMode = 'e'; /* text editor */ 9144 }else{ 9145 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", 9146 azArg[i]); 9147 showHelp(p->out, azArg[0]); 9148 rc = 1; 9149 goto meta_command_exit; 9150 } 9151 }else if( zFile==0 && eMode!='e' && eMode!='x' ){ 9152 zFile = sqlite3_mprintf("%s", z); 9153 if( zFile && zFile[0]=='|' ){ 9154 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]); 9155 break; 9156 } 9157 }else{ 9158 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", 9159 azArg[i]); 9160 showHelp(p->out, azArg[0]); 9161 rc = 1; 9162 sqlite3_free(zFile); 9163 goto meta_command_exit; 9164 } 9165 } 9166 if( zFile==0 ){ 9167 zFile = sqlite3_mprintf("stdout"); 9168 } 9169 if( bOnce ){ 9170 p->outCount = 2; 9171 }else{ 9172 p->outCount = 0; 9173 } 9174 output_reset(p); 9175#ifndef SQLITE_NOHAVE_SYSTEM 9176 if( eMode=='e' || eMode=='x' ){ 9177 p->doXdgOpen = 1; 9178 outputModePush(p); 9179 if( eMode=='x' ){ 9180 /* spreadsheet mode. Output as CSV. */ 9181 newTempFile(p, "csv"); 9182 ShellClearFlag(p, SHFLG_Echo); 9183 p->mode = MODE_Csv; 9184 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 9185 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 9186 }else{ 9187 /* text editor mode */ 9188 newTempFile(p, "txt"); 9189 bTxtMode = 1; 9190 } 9191 sqlite3_free(zFile); 9192 zFile = sqlite3_mprintf("%s", p->zTempFile); 9193 } 9194#endif /* SQLITE_NOHAVE_SYSTEM */ 9195 shell_check_oom(zFile); 9196 if( zFile[0]=='|' ){ 9197#ifdef SQLITE_OMIT_POPEN 9198 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 9199 rc = 1; 9200 p->out = stdout; 9201#else 9202 p->out = popen(zFile + 1, "w"); 9203 if( p->out==0 ){ 9204 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); 9205 p->out = stdout; 9206 rc = 1; 9207 }else{ 9208 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); 9209 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 9210 } 9211#endif 9212 }else{ 9213 p->out = output_file_open(zFile, bTxtMode); 9214 if( p->out==0 ){ 9215 if( strcmp(zFile,"off")!=0 ){ 9216 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); 9217 } 9218 p->out = stdout; 9219 rc = 1; 9220 } else { 9221 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); 9222 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 9223 } 9224 } 9225 sqlite3_free(zFile); 9226 }else 9227#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9228 9229 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ 9230 open_db(p,0); 9231 if( nArg<=1 ) goto parameter_syntax_error; 9232 9233 /* .parameter clear 9234 ** Clear all bind parameters by dropping the TEMP table that holds them. 9235 */ 9236 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ 9237 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", 9238 0, 0, 0); 9239 }else 9240 9241 /* .parameter list 9242 ** List all bind parameters. 9243 */ 9244 if( nArg==2 && strcmp(azArg[1],"list")==0 ){ 9245 sqlite3_stmt *pStmt = 0; 9246 int rx; 9247 int len = 0; 9248 rx = sqlite3_prepare_v2(p->db, 9249 "SELECT max(length(key)) " 9250 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 9251 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 9252 len = sqlite3_column_int(pStmt, 0); 9253 if( len>40 ) len = 40; 9254 } 9255 sqlite3_finalize(pStmt); 9256 pStmt = 0; 9257 if( len ){ 9258 rx = sqlite3_prepare_v2(p->db, 9259 "SELECT key, quote(value) " 9260 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 9261 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 9262 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), 9263 sqlite3_column_text(pStmt,1)); 9264 } 9265 sqlite3_finalize(pStmt); 9266 } 9267 }else 9268 9269 /* .parameter init 9270 ** Make sure the TEMP table used to hold bind parameters exists. 9271 ** Create it if necessary. 9272 */ 9273 if( nArg==2 && strcmp(azArg[1],"init")==0 ){ 9274 bind_table_init(p); 9275 }else 9276 9277 /* .parameter set NAME VALUE 9278 ** Set or reset a bind parameter. NAME should be the full parameter 9279 ** name exactly as it appears in the query. (ex: $abc, @def). The 9280 ** VALUE can be in either SQL literal notation, or if not it will be 9281 ** understood to be a text string. 9282 */ 9283 if( nArg==4 && strcmp(azArg[1],"set")==0 ){ 9284 int rx; 9285 char *zSql; 9286 sqlite3_stmt *pStmt; 9287 const char *zKey = azArg[2]; 9288 const char *zValue = azArg[3]; 9289 bind_table_init(p); 9290 zSql = sqlite3_mprintf( 9291 "REPLACE INTO temp.sqlite_parameters(key,value)" 9292 "VALUES(%Q,%s);", zKey, zValue); 9293 shell_check_oom(zSql); 9294 pStmt = 0; 9295 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 9296 sqlite3_free(zSql); 9297 if( rx!=SQLITE_OK ){ 9298 sqlite3_finalize(pStmt); 9299 pStmt = 0; 9300 zSql = sqlite3_mprintf( 9301 "REPLACE INTO temp.sqlite_parameters(key,value)" 9302 "VALUES(%Q,%Q);", zKey, zValue); 9303 shell_check_oom(zSql); 9304 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 9305 sqlite3_free(zSql); 9306 if( rx!=SQLITE_OK ){ 9307 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); 9308 sqlite3_finalize(pStmt); 9309 pStmt = 0; 9310 rc = 1; 9311 } 9312 } 9313 sqlite3_step(pStmt); 9314 sqlite3_finalize(pStmt); 9315 }else 9316 9317 /* .parameter unset NAME 9318 ** Remove the NAME binding from the parameter binding table, if it 9319 ** exists. 9320 */ 9321 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ 9322 char *zSql = sqlite3_mprintf( 9323 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); 9324 shell_check_oom(zSql); 9325 sqlite3_exec(p->db, zSql, 0, 0, 0); 9326 sqlite3_free(zSql); 9327 }else 9328 /* If no command name matches, show a syntax error */ 9329 parameter_syntax_error: 9330 showHelp(p->out, "parameter"); 9331 }else 9332 9333 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ 9334 int i; 9335 for(i=1; i<nArg; i++){ 9336 if( i>1 ) raw_printf(p->out, " "); 9337 utf8_printf(p->out, "%s", azArg[i]); 9338 } 9339 raw_printf(p->out, "\n"); 9340 }else 9341 9342#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 9343 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ 9344 int i; 9345 int nn = 0; 9346 p->flgProgress = 0; 9347 p->mxProgress = 0; 9348 p->nProgress = 0; 9349 for(i=1; i<nArg; i++){ 9350 const char *z = azArg[i]; 9351 if( z[0]=='-' ){ 9352 z++; 9353 if( z[0]=='-' ) z++; 9354 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 9355 p->flgProgress |= SHELL_PROGRESS_QUIET; 9356 continue; 9357 } 9358 if( strcmp(z,"reset")==0 ){ 9359 p->flgProgress |= SHELL_PROGRESS_RESET; 9360 continue; 9361 } 9362 if( strcmp(z,"once")==0 ){ 9363 p->flgProgress |= SHELL_PROGRESS_ONCE; 9364 continue; 9365 } 9366 if( strcmp(z,"limit")==0 ){ 9367 if( i+1>=nArg ){ 9368 utf8_printf(stderr, "Error: missing argument on --limit\n"); 9369 rc = 1; 9370 goto meta_command_exit; 9371 }else{ 9372 p->mxProgress = (int)integerValue(azArg[++i]); 9373 } 9374 continue; 9375 } 9376 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); 9377 rc = 1; 9378 goto meta_command_exit; 9379 }else{ 9380 nn = (int)integerValue(z); 9381 } 9382 } 9383 open_db(p, 0); 9384 sqlite3_progress_handler(p->db, nn, progress_handler, p); 9385 }else 9386#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 9387 9388 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ 9389 if( nArg >= 2) { 9390 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); 9391 } 9392 if( nArg >= 3) { 9393 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); 9394 } 9395 }else 9396 9397#ifndef SQLITE_SHELL_FIDDLE 9398 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ 9399 rc = 2; 9400 }else 9401#endif 9402 9403#ifndef SQLITE_SHELL_FIDDLE 9404 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ 9405 FILE *inSaved = p->in; 9406 int savedLineno = p->lineno; 9407 failIfSafeMode(p, "cannot run .read in safe mode"); 9408 if( nArg!=2 ){ 9409 raw_printf(stderr, "Usage: .read FILE\n"); 9410 rc = 1; 9411 goto meta_command_exit; 9412 } 9413 if( azArg[1][0]=='|' ){ 9414#ifdef SQLITE_OMIT_POPEN 9415 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 9416 rc = 1; 9417 p->out = stdout; 9418#else 9419 p->in = popen(azArg[1]+1, "r"); 9420 if( p->in==0 ){ 9421 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 9422 rc = 1; 9423 }else{ 9424 rc = process_input(p); 9425 pclose(p->in); 9426 } 9427#endif 9428 }else if( (p->in = openChrSource(azArg[1]))==0 ){ 9429 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); 9430 rc = 1; 9431 }else{ 9432 rc = process_input(p); 9433 fclose(p->in); 9434 } 9435 p->in = inSaved; 9436 p->lineno = savedLineno; 9437 }else 9438#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9439 9440#ifndef SQLITE_SHELL_FIDDLE 9441 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ 9442 const char *zSrcFile; 9443 const char *zDb; 9444 sqlite3 *pSrc; 9445 sqlite3_backup *pBackup; 9446 int nTimeout = 0; 9447 9448 failIfSafeMode(p, "cannot run .restore in safe mode"); 9449 if( nArg==2 ){ 9450 zSrcFile = azArg[1]; 9451 zDb = "main"; 9452 }else if( nArg==3 ){ 9453 zSrcFile = azArg[2]; 9454 zDb = azArg[1]; 9455 }else{ 9456 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); 9457 rc = 1; 9458 goto meta_command_exit; 9459 } 9460 rc = sqlite3_open(zSrcFile, &pSrc); 9461 if( rc!=SQLITE_OK ){ 9462 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); 9463 close_db(pSrc); 9464 return 1; 9465 } 9466 open_db(p, 0); 9467 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); 9468 if( pBackup==0 ){ 9469 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9470 close_db(pSrc); 9471 return 1; 9472 } 9473 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 9474 || rc==SQLITE_BUSY ){ 9475 if( rc==SQLITE_BUSY ){ 9476 if( nTimeout++ >= 3 ) break; 9477 sqlite3_sleep(100); 9478 } 9479 } 9480 sqlite3_backup_finish(pBackup); 9481 if( rc==SQLITE_DONE ){ 9482 rc = 0; 9483 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 9484 raw_printf(stderr, "Error: source database is busy\n"); 9485 rc = 1; 9486 }else{ 9487 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9488 rc = 1; 9489 } 9490 close_db(pSrc); 9491 }else 9492#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9493 9494 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ 9495 if( nArg==2 ){ 9496 p->scanstatsOn = (u8)booleanValue(azArg[1]); 9497#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 9498 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); 9499#endif 9500 }else{ 9501 raw_printf(stderr, "Usage: .scanstats on|off\n"); 9502 rc = 1; 9503 } 9504 }else 9505 9506 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ 9507 ShellText sSelect; 9508 ShellState data; 9509 char *zErrMsg = 0; 9510 const char *zDiv = "("; 9511 const char *zName = 0; 9512 int iSchema = 0; 9513 int bDebug = 0; 9514 int bNoSystemTabs = 0; 9515 int ii; 9516 9517 open_db(p, 0); 9518 memcpy(&data, p, sizeof(data)); 9519 data.showHeader = 0; 9520 data.cMode = data.mode = MODE_Semi; 9521 initText(&sSelect); 9522 for(ii=1; ii<nArg; ii++){ 9523 if( optionMatch(azArg[ii],"indent") ){ 9524 data.cMode = data.mode = MODE_Pretty; 9525 }else if( optionMatch(azArg[ii],"debug") ){ 9526 bDebug = 1; 9527 }else if( optionMatch(azArg[ii],"nosys") ){ 9528 bNoSystemTabs = 1; 9529 }else if( azArg[ii][0]=='-' ){ 9530 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]); 9531 rc = 1; 9532 goto meta_command_exit; 9533 }else if( zName==0 ){ 9534 zName = azArg[ii]; 9535 }else{ 9536 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); 9537 rc = 1; 9538 goto meta_command_exit; 9539 } 9540 } 9541 if( zName!=0 ){ 9542 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 9543 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 9544 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 9545 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; 9546 if( isSchema ){ 9547 char *new_argv[2], *new_colv[2]; 9548 new_argv[0] = sqlite3_mprintf( 9549 "CREATE TABLE %s (\n" 9550 " type text,\n" 9551 " name text,\n" 9552 " tbl_name text,\n" 9553 " rootpage integer,\n" 9554 " sql text\n" 9555 ")", zName); 9556 shell_check_oom(new_argv[0]); 9557 new_argv[1] = 0; 9558 new_colv[0] = "sql"; 9559 new_colv[1] = 0; 9560 callback(&data, 1, new_argv, new_colv); 9561 sqlite3_free(new_argv[0]); 9562 } 9563 } 9564 if( zDiv ){ 9565 sqlite3_stmt *pStmt = 0; 9566 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", 9567 -1, &pStmt, 0); 9568 if( rc ){ 9569 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9570 sqlite3_finalize(pStmt); 9571 rc = 1; 9572 goto meta_command_exit; 9573 } 9574 appendText(&sSelect, "SELECT sql FROM", 0); 9575 iSchema = 0; 9576 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9577 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); 9578 char zScNum[30]; 9579 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); 9580 appendText(&sSelect, zDiv, 0); 9581 zDiv = " UNION ALL "; 9582 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); 9583 if( sqlite3_stricmp(zDb, "main")!=0 ){ 9584 appendText(&sSelect, zDb, '\''); 9585 }else{ 9586 appendText(&sSelect, "NULL", 0); 9587 } 9588 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); 9589 appendText(&sSelect, zScNum, 0); 9590 appendText(&sSelect, " AS snum, ", 0); 9591 appendText(&sSelect, zDb, '\''); 9592 appendText(&sSelect, " AS sname FROM ", 0); 9593 appendText(&sSelect, zDb, quoteChar(zDb)); 9594 appendText(&sSelect, ".sqlite_schema", 0); 9595 } 9596 sqlite3_finalize(pStmt); 9597#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 9598 if( zName ){ 9599 appendText(&sSelect, 9600 " UNION ALL SELECT shell_module_schema(name)," 9601 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 9602 0); 9603 } 9604#endif 9605 appendText(&sSelect, ") WHERE ", 0); 9606 if( zName ){ 9607 char *zQarg = sqlite3_mprintf("%Q", zName); 9608 int bGlob; 9609 shell_check_oom(zQarg); 9610 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || 9611 strchr(zName, '[') != 0; 9612 if( strchr(zName, '.') ){ 9613 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); 9614 }else{ 9615 appendText(&sSelect, "lower(tbl_name)", 0); 9616 } 9617 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); 9618 appendText(&sSelect, zQarg, 0); 9619 if( !bGlob ){ 9620 appendText(&sSelect, " ESCAPE '\\' ", 0); 9621 } 9622 appendText(&sSelect, " AND ", 0); 9623 sqlite3_free(zQarg); 9624 } 9625 if( bNoSystemTabs ){ 9626 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); 9627 } 9628 appendText(&sSelect, "sql IS NOT NULL" 9629 " ORDER BY snum, rowid", 0); 9630 if( bDebug ){ 9631 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); 9632 }else{ 9633 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); 9634 } 9635 freeText(&sSelect); 9636 } 9637 if( zErrMsg ){ 9638 utf8_printf(stderr,"Error: %s\n", zErrMsg); 9639 sqlite3_free(zErrMsg); 9640 rc = 1; 9641 }else if( rc != SQLITE_OK ){ 9642 raw_printf(stderr,"Error: querying schema information\n"); 9643 rc = 1; 9644 }else{ 9645 rc = 0; 9646 } 9647 }else 9648 9649 if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0) 9650 || (c=='t' && n==9 && strncmp(azArg[0], "treetrace", n)==0) 9651 ){ 9652 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; 9653 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); 9654 }else 9655 9656#if defined(SQLITE_ENABLE_SESSION) 9657 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ 9658 struct AuxDb *pAuxDb = p->pAuxDb; 9659 OpenSession *pSession = &pAuxDb->aSession[0]; 9660 char **azCmd = &azArg[1]; 9661 int iSes = 0; 9662 int nCmd = nArg - 1; 9663 int i; 9664 if( nArg<=1 ) goto session_syntax_error; 9665 open_db(p, 0); 9666 if( nArg>=3 ){ 9667 for(iSes=0; iSes<pAuxDb->nSession; iSes++){ 9668 if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; 9669 } 9670 if( iSes<pAuxDb->nSession ){ 9671 pSession = &pAuxDb->aSession[iSes]; 9672 azCmd++; 9673 nCmd--; 9674 }else{ 9675 pSession = &pAuxDb->aSession[0]; 9676 iSes = 0; 9677 } 9678 } 9679 9680 /* .session attach TABLE 9681 ** Invoke the sqlite3session_attach() interface to attach a particular 9682 ** table so that it is never filtered. 9683 */ 9684 if( strcmp(azCmd[0],"attach")==0 ){ 9685 if( nCmd!=2 ) goto session_syntax_error; 9686 if( pSession->p==0 ){ 9687 session_not_open: 9688 raw_printf(stderr, "ERROR: No sessions are open\n"); 9689 }else{ 9690 rc = sqlite3session_attach(pSession->p, azCmd[1]); 9691 if( rc ){ 9692 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); 9693 rc = 0; 9694 } 9695 } 9696 }else 9697 9698 /* .session changeset FILE 9699 ** .session patchset FILE 9700 ** Write a changeset or patchset into a file. The file is overwritten. 9701 */ 9702 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ 9703 FILE *out = 0; 9704 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); 9705 if( nCmd!=2 ) goto session_syntax_error; 9706 if( pSession->p==0 ) goto session_not_open; 9707 out = fopen(azCmd[1], "wb"); 9708 if( out==0 ){ 9709 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", 9710 azCmd[1]); 9711 }else{ 9712 int szChng; 9713 void *pChng; 9714 if( azCmd[0][0]=='c' ){ 9715 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); 9716 }else{ 9717 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); 9718 } 9719 if( rc ){ 9720 printf("Error: error code %d\n", rc); 9721 rc = 0; 9722 } 9723 if( pChng 9724 && fwrite(pChng, szChng, 1, out)!=1 ){ 9725 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", 9726 szChng); 9727 } 9728 sqlite3_free(pChng); 9729 fclose(out); 9730 } 9731 }else 9732 9733 /* .session close 9734 ** Close the identified session 9735 */ 9736 if( strcmp(azCmd[0], "close")==0 ){ 9737 if( nCmd!=1 ) goto session_syntax_error; 9738 if( pAuxDb->nSession ){ 9739 session_close(pSession); 9740 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; 9741 } 9742 }else 9743 9744 /* .session enable ?BOOLEAN? 9745 ** Query or set the enable flag 9746 */ 9747 if( strcmp(azCmd[0], "enable")==0 ){ 9748 int ii; 9749 if( nCmd>2 ) goto session_syntax_error; 9750 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9751 if( pAuxDb->nSession ){ 9752 ii = sqlite3session_enable(pSession->p, ii); 9753 utf8_printf(p->out, "session %s enable flag = %d\n", 9754 pSession->zName, ii); 9755 } 9756 }else 9757 9758 /* .session filter GLOB .... 9759 ** Set a list of GLOB patterns of table names to be excluded. 9760 */ 9761 if( strcmp(azCmd[0], "filter")==0 ){ 9762 int ii, nByte; 9763 if( nCmd<2 ) goto session_syntax_error; 9764 if( pAuxDb->nSession ){ 9765 for(ii=0; ii<pSession->nFilter; ii++){ 9766 sqlite3_free(pSession->azFilter[ii]); 9767 } 9768 sqlite3_free(pSession->azFilter); 9769 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); 9770 pSession->azFilter = sqlite3_malloc( nByte ); 9771 if( pSession->azFilter==0 ){ 9772 raw_printf(stderr, "Error: out or memory\n"); 9773 exit(1); 9774 } 9775 for(ii=1; ii<nCmd; ii++){ 9776 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); 9777 shell_check_oom(x); 9778 } 9779 pSession->nFilter = ii-1; 9780 } 9781 }else 9782 9783 /* .session indirect ?BOOLEAN? 9784 ** Query or set the indirect flag 9785 */ 9786 if( strcmp(azCmd[0], "indirect")==0 ){ 9787 int ii; 9788 if( nCmd>2 ) goto session_syntax_error; 9789 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9790 if( pAuxDb->nSession ){ 9791 ii = sqlite3session_indirect(pSession->p, ii); 9792 utf8_printf(p->out, "session %s indirect flag = %d\n", 9793 pSession->zName, ii); 9794 } 9795 }else 9796 9797 /* .session isempty 9798 ** Determine if the session is empty 9799 */ 9800 if( strcmp(azCmd[0], "isempty")==0 ){ 9801 int ii; 9802 if( nCmd!=1 ) goto session_syntax_error; 9803 if( pAuxDb->nSession ){ 9804 ii = sqlite3session_isempty(pSession->p); 9805 utf8_printf(p->out, "session %s isempty flag = %d\n", 9806 pSession->zName, ii); 9807 } 9808 }else 9809 9810 /* .session list 9811 ** List all currently open sessions 9812 */ 9813 if( strcmp(azCmd[0],"list")==0 ){ 9814 for(i=0; i<pAuxDb->nSession; i++){ 9815 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); 9816 } 9817 }else 9818 9819 /* .session open DB NAME 9820 ** Open a new session called NAME on the attached database DB. 9821 ** DB is normally "main". 9822 */ 9823 if( strcmp(azCmd[0],"open")==0 ){ 9824 char *zName; 9825 if( nCmd!=3 ) goto session_syntax_error; 9826 zName = azCmd[2]; 9827 if( zName[0]==0 ) goto session_syntax_error; 9828 for(i=0; i<pAuxDb->nSession; i++){ 9829 if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ 9830 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); 9831 goto meta_command_exit; 9832 } 9833 } 9834 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ 9835 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); 9836 goto meta_command_exit; 9837 } 9838 pSession = &pAuxDb->aSession[pAuxDb->nSession]; 9839 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); 9840 if( rc ){ 9841 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); 9842 rc = 0; 9843 goto meta_command_exit; 9844 } 9845 pSession->nFilter = 0; 9846 sqlite3session_table_filter(pSession->p, session_filter, pSession); 9847 pAuxDb->nSession++; 9848 pSession->zName = sqlite3_mprintf("%s", zName); 9849 shell_check_oom(pSession->zName); 9850 }else 9851 /* If no command name matches, show a syntax error */ 9852 session_syntax_error: 9853 showHelp(p->out, "session"); 9854 }else 9855#endif 9856 9857#ifdef SQLITE_DEBUG 9858 /* Undocumented commands for internal testing. Subject to change 9859 ** without notice. */ 9860 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ 9861 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ 9862 int i, v; 9863 for(i=1; i<nArg; i++){ 9864 v = booleanValue(azArg[i]); 9865 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); 9866 } 9867 } 9868 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ 9869 int i; sqlite3_int64 v; 9870 for(i=1; i<nArg; i++){ 9871 char zBuf[200]; 9872 v = integerValue(azArg[i]); 9873 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); 9874 utf8_printf(p->out, "%s", zBuf); 9875 } 9876 } 9877 }else 9878#endif 9879 9880 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ 9881 int bIsInit = 0; /* True to initialize the SELFTEST table */ 9882 int bVerbose = 0; /* Verbose output */ 9883 int bSelftestExists; /* True if SELFTEST already exists */ 9884 int i, k; /* Loop counters */ 9885 int nTest = 0; /* Number of tests runs */ 9886 int nErr = 0; /* Number of errors seen */ 9887 ShellText str; /* Answer for a query */ 9888 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ 9889 9890 open_db(p,0); 9891 for(i=1; i<nArg; i++){ 9892 const char *z = azArg[i]; 9893 if( z[0]=='-' && z[1]=='-' ) z++; 9894 if( strcmp(z,"-init")==0 ){ 9895 bIsInit = 1; 9896 }else 9897 if( strcmp(z,"-v")==0 ){ 9898 bVerbose++; 9899 }else 9900 { 9901 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 9902 azArg[i], azArg[0]); 9903 raw_printf(stderr, "Should be one of: --init -v\n"); 9904 rc = 1; 9905 goto meta_command_exit; 9906 } 9907 } 9908 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) 9909 != SQLITE_OK ){ 9910 bSelftestExists = 0; 9911 }else{ 9912 bSelftestExists = 1; 9913 } 9914 if( bIsInit ){ 9915 createSelftestTable(p); 9916 bSelftestExists = 1; 9917 } 9918 initText(&str); 9919 appendText(&str, "x", 0); 9920 for(k=bSelftestExists; k>=0; k--){ 9921 if( k==1 ){ 9922 rc = sqlite3_prepare_v2(p->db, 9923 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", 9924 -1, &pStmt, 0); 9925 }else{ 9926 rc = sqlite3_prepare_v2(p->db, 9927 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," 9928 " (1,'run','PRAGMA integrity_check','ok')", 9929 -1, &pStmt, 0); 9930 } 9931 if( rc ){ 9932 raw_printf(stderr, "Error querying the selftest table\n"); 9933 rc = 1; 9934 sqlite3_finalize(pStmt); 9935 goto meta_command_exit; 9936 } 9937 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ 9938 int tno = sqlite3_column_int(pStmt, 0); 9939 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); 9940 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); 9941 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); 9942 9943 if( zOp==0 ) continue; 9944 if( zSql==0 ) continue; 9945 if( zAns==0 ) continue; 9946 k = 0; 9947 if( bVerbose>0 ){ 9948 printf("%d: %s %s\n", tno, zOp, zSql); 9949 } 9950 if( strcmp(zOp,"memo")==0 ){ 9951 utf8_printf(p->out, "%s\n", zSql); 9952 }else 9953 if( strcmp(zOp,"run")==0 ){ 9954 char *zErrMsg = 0; 9955 str.n = 0; 9956 str.z[0] = 0; 9957 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); 9958 nTest++; 9959 if( bVerbose ){ 9960 utf8_printf(p->out, "Result: %s\n", str.z); 9961 } 9962 if( rc || zErrMsg ){ 9963 nErr++; 9964 rc = 1; 9965 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); 9966 sqlite3_free(zErrMsg); 9967 }else if( strcmp(zAns,str.z)!=0 ){ 9968 nErr++; 9969 rc = 1; 9970 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); 9971 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); 9972 } 9973 }else 9974 { 9975 utf8_printf(stderr, 9976 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); 9977 rc = 1; 9978 break; 9979 } 9980 } /* End loop over rows of content from SELFTEST */ 9981 sqlite3_finalize(pStmt); 9982 } /* End loop over k */ 9983 freeText(&str); 9984 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); 9985 }else 9986 9987 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ 9988 if( nArg<2 || nArg>3 ){ 9989 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); 9990 rc = 1; 9991 } 9992 if( nArg>=2 ){ 9993 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, 9994 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); 9995 } 9996 if( nArg>=3 ){ 9997 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, 9998 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); 9999 } 10000 }else 10001 10002 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ 10003 const char *zLike = 0; /* Which table to checksum. 0 means everything */ 10004 int i; /* Loop counter */ 10005 int bSchema = 0; /* Also hash the schema */ 10006 int bSeparate = 0; /* Hash each table separately */ 10007 int iSize = 224; /* Hash algorithm to use */ 10008 int bDebug = 0; /* Only show the query that would have run */ 10009 sqlite3_stmt *pStmt; /* For querying tables names */ 10010 char *zSql; /* SQL to be run */ 10011 char *zSep; /* Separator */ 10012 ShellText sSql; /* Complete SQL for the query to run the hash */ 10013 ShellText sQuery; /* Set of queries used to read all content */ 10014 open_db(p, 0); 10015 for(i=1; i<nArg; i++){ 10016 const char *z = azArg[i]; 10017 if( z[0]=='-' ){ 10018 z++; 10019 if( z[0]=='-' ) z++; 10020 if( strcmp(z,"schema")==0 ){ 10021 bSchema = 1; 10022 }else 10023 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 10024 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 10025 ){ 10026 iSize = atoi(&z[5]); 10027 }else 10028 if( strcmp(z,"debug")==0 ){ 10029 bDebug = 1; 10030 }else 10031 { 10032 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 10033 azArg[i], azArg[0]); 10034 showHelp(p->out, azArg[0]); 10035 rc = 1; 10036 goto meta_command_exit; 10037 } 10038 }else if( zLike ){ 10039 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); 10040 rc = 1; 10041 goto meta_command_exit; 10042 }else{ 10043 zLike = z; 10044 bSeparate = 1; 10045 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; 10046 } 10047 } 10048 if( bSchema ){ 10049 zSql = "SELECT lower(name) FROM sqlite_schema" 10050 " WHERE type='table' AND coalesce(rootpage,0)>1" 10051 " UNION ALL SELECT 'sqlite_schema'" 10052 " ORDER BY 1 collate nocase"; 10053 }else{ 10054 zSql = "SELECT lower(name) FROM sqlite_schema" 10055 " WHERE type='table' AND coalesce(rootpage,0)>1" 10056 " AND name NOT LIKE 'sqlite_%'" 10057 " ORDER BY 1 collate nocase"; 10058 } 10059 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 10060 initText(&sQuery); 10061 initText(&sSql); 10062 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); 10063 zSep = "VALUES("; 10064 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 10065 const char *zTab = (const char*)sqlite3_column_text(pStmt,0); 10066 if( zTab==0 ) continue; 10067 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; 10068 if( strncmp(zTab, "sqlite_",7)!=0 ){ 10069 appendText(&sQuery,"SELECT * FROM ", 0); 10070 appendText(&sQuery,zTab,'"'); 10071 appendText(&sQuery," NOT INDEXED;", 0); 10072 }else if( strcmp(zTab, "sqlite_schema")==0 ){ 10073 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" 10074 " ORDER BY name;", 0); 10075 }else if( strcmp(zTab, "sqlite_sequence")==0 ){ 10076 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" 10077 " ORDER BY name;", 0); 10078 }else if( strcmp(zTab, "sqlite_stat1")==0 ){ 10079 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" 10080 " ORDER BY tbl,idx;", 0); 10081 }else if( strcmp(zTab, "sqlite_stat4")==0 ){ 10082 appendText(&sQuery, "SELECT * FROM ", 0); 10083 appendText(&sQuery, zTab, 0); 10084 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); 10085 } 10086 appendText(&sSql, zSep, 0); 10087 appendText(&sSql, sQuery.z, '\''); 10088 sQuery.n = 0; 10089 appendText(&sSql, ",", 0); 10090 appendText(&sSql, zTab, '\''); 10091 zSep = "),("; 10092 } 10093 sqlite3_finalize(pStmt); 10094 if( bSeparate ){ 10095 zSql = sqlite3_mprintf( 10096 "%s))" 10097 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" 10098 " FROM [sha3sum$query]", 10099 sSql.z, iSize); 10100 }else{ 10101 zSql = sqlite3_mprintf( 10102 "%s))" 10103 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" 10104 " FROM [sha3sum$query]", 10105 sSql.z, iSize); 10106 } 10107 shell_check_oom(zSql); 10108 freeText(&sQuery); 10109 freeText(&sSql); 10110 if( bDebug ){ 10111 utf8_printf(p->out, "%s\n", zSql); 10112 }else{ 10113 shell_exec(p, zSql, 0); 10114 } 10115 sqlite3_free(zSql); 10116 }else 10117 10118#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 10119 if( c=='s' 10120 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) 10121 ){ 10122 char *zCmd; 10123 int i, x; 10124 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 10125 if( nArg<2 ){ 10126 raw_printf(stderr, "Usage: .system COMMAND\n"); 10127 rc = 1; 10128 goto meta_command_exit; 10129 } 10130 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); 10131 for(i=2; i<nArg && zCmd!=0; i++){ 10132 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", 10133 zCmd, azArg[i]); 10134 } 10135 x = zCmd!=0 ? system(zCmd) : 1; 10136 sqlite3_free(zCmd); 10137 if( x ) raw_printf(stderr, "System command returns %d\n", x); 10138 }else 10139#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */ 10140 10141 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ 10142 static const char *azBool[] = { "off", "on", "trigger", "full"}; 10143 const char *zOut; 10144 int i; 10145 if( nArg!=1 ){ 10146 raw_printf(stderr, "Usage: .show\n"); 10147 rc = 1; 10148 goto meta_command_exit; 10149 } 10150 utf8_printf(p->out, "%12.12s: %s\n","echo", 10151 azBool[ShellHasFlag(p, SHFLG_Echo)]); 10152 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); 10153 utf8_printf(p->out, "%12.12s: %s\n","explain", 10154 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); 10155 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); 10156 if( p->mode==MODE_Column 10157 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) 10158 ){ 10159 utf8_printf 10160 (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", 10161 modeDescr[p->mode], p->cmOpts.iWrap, 10162 p->cmOpts.bWordWrap ? "on" : "off", 10163 p->cmOpts.bQuote ? "" : "no"); 10164 }else{ 10165 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); 10166 } 10167 utf8_printf(p->out, "%12.12s: ", "nullvalue"); 10168 output_c_string(p->out, p->nullValue); 10169 raw_printf(p->out, "\n"); 10170 utf8_printf(p->out,"%12.12s: %s\n","output", 10171 strlen30(p->outfile) ? p->outfile : "stdout"); 10172 utf8_printf(p->out,"%12.12s: ", "colseparator"); 10173 output_c_string(p->out, p->colSeparator); 10174 raw_printf(p->out, "\n"); 10175 utf8_printf(p->out,"%12.12s: ", "rowseparator"); 10176 output_c_string(p->out, p->rowSeparator); 10177 raw_printf(p->out, "\n"); 10178 switch( p->statsOn ){ 10179 case 0: zOut = "off"; break; 10180 default: zOut = "on"; break; 10181 case 2: zOut = "stmt"; break; 10182 case 3: zOut = "vmstep"; break; 10183 } 10184 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); 10185 utf8_printf(p->out, "%12.12s: ", "width"); 10186 for (i=0;i<p->nWidth;i++) { 10187 raw_printf(p->out, "%d ", p->colWidth[i]); 10188 } 10189 raw_printf(p->out, "\n"); 10190 utf8_printf(p->out, "%12.12s: %s\n", "filename", 10191 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); 10192 }else 10193 10194 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ 10195 if( nArg==2 ){ 10196 if( strcmp(azArg[1],"stmt")==0 ){ 10197 p->statsOn = 2; 10198 }else if( strcmp(azArg[1],"vmstep")==0 ){ 10199 p->statsOn = 3; 10200 }else{ 10201 p->statsOn = (u8)booleanValue(azArg[1]); 10202 } 10203 }else if( nArg==1 ){ 10204 display_stats(p->db, p, 0); 10205 }else{ 10206 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n"); 10207 rc = 1; 10208 } 10209 }else 10210 10211 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) 10212 || (c=='i' && (strncmp(azArg[0], "indices", n)==0 10213 || strncmp(azArg[0], "indexes", n)==0) ) 10214 ){ 10215 sqlite3_stmt *pStmt; 10216 char **azResult; 10217 int nRow, nAlloc; 10218 int ii; 10219 ShellText s; 10220 initText(&s); 10221 open_db(p, 0); 10222 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 10223 if( rc ){ 10224 sqlite3_finalize(pStmt); 10225 return shellDatabaseError(p->db); 10226 } 10227 10228 if( nArg>2 && c=='i' ){ 10229 /* It is an historical accident that the .indexes command shows an error 10230 ** when called with the wrong number of arguments whereas the .tables 10231 ** command does not. */ 10232 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); 10233 rc = 1; 10234 sqlite3_finalize(pStmt); 10235 goto meta_command_exit; 10236 } 10237 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ 10238 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); 10239 if( zDbName==0 ) continue; 10240 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); 10241 if( sqlite3_stricmp(zDbName, "main")==0 ){ 10242 appendText(&s, "SELECT name FROM ", 0); 10243 }else{ 10244 appendText(&s, "SELECT ", 0); 10245 appendText(&s, zDbName, '\''); 10246 appendText(&s, "||'.'||name FROM ", 0); 10247 } 10248 appendText(&s, zDbName, '"'); 10249 appendText(&s, ".sqlite_schema ", 0); 10250 if( c=='t' ){ 10251 appendText(&s," WHERE type IN ('table','view')" 10252 " AND name NOT LIKE 'sqlite_%'" 10253 " AND name LIKE ?1", 0); 10254 }else{ 10255 appendText(&s," WHERE type='index'" 10256 " AND tbl_name LIKE ?1", 0); 10257 } 10258 } 10259 rc = sqlite3_finalize(pStmt); 10260 if( rc==SQLITE_OK ){ 10261 appendText(&s, " ORDER BY 1", 0); 10262 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); 10263 } 10264 freeText(&s); 10265 if( rc ) return shellDatabaseError(p->db); 10266 10267 /* Run the SQL statement prepared by the above block. Store the results 10268 ** as an array of nul-terminated strings in azResult[]. */ 10269 nRow = nAlloc = 0; 10270 azResult = 0; 10271 if( nArg>1 ){ 10272 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); 10273 }else{ 10274 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); 10275 } 10276 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 10277 if( nRow>=nAlloc ){ 10278 char **azNew; 10279 int n2 = nAlloc*2 + 10; 10280 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); 10281 shell_check_oom(azNew); 10282 nAlloc = n2; 10283 azResult = azNew; 10284 } 10285 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 10286 shell_check_oom(azResult[nRow]); 10287 nRow++; 10288 } 10289 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ 10290 rc = shellDatabaseError(p->db); 10291 } 10292 10293 /* Pretty-print the contents of array azResult[] to the output */ 10294 if( rc==0 && nRow>0 ){ 10295 int len, maxlen = 0; 10296 int i, j; 10297 int nPrintCol, nPrintRow; 10298 for(i=0; i<nRow; i++){ 10299 len = strlen30(azResult[i]); 10300 if( len>maxlen ) maxlen = len; 10301 } 10302 nPrintCol = 80/(maxlen+2); 10303 if( nPrintCol<1 ) nPrintCol = 1; 10304 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; 10305 for(i=0; i<nPrintRow; i++){ 10306 for(j=i; j<nRow; j+=nPrintRow){ 10307 char *zSp = j<nPrintRow ? "" : " "; 10308 utf8_printf(p->out, "%s%-*s", zSp, maxlen, 10309 azResult[j] ? azResult[j]:""); 10310 } 10311 raw_printf(p->out, "\n"); 10312 } 10313 } 10314 10315 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); 10316 sqlite3_free(azResult); 10317 }else 10318 10319#ifndef SQLITE_SHELL_FIDDLE 10320 /* Begin redirecting output to the file "testcase-out.txt" */ 10321 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ 10322 output_reset(p); 10323 p->out = output_file_open("testcase-out.txt", 0); 10324 if( p->out==0 ){ 10325 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); 10326 } 10327 if( nArg>=2 ){ 10328 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); 10329 }else{ 10330 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); 10331 } 10332 }else 10333#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 10334 10335#ifndef SQLITE_UNTESTABLE 10336 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ 10337 static const struct { 10338 const char *zCtrlName; /* Name of a test-control option */ 10339 int ctrlCode; /* Integer code for that option */ 10340 int unSafe; /* Not valid for --safe mode */ 10341 const char *zUsage; /* Usage notes */ 10342 } aCtrl[] = { 10343 { "always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, 10344 { "assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, 10345 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ 10346 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/ 10347 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, 10348 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, 10349 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/ 10350 { "imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, 10351 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, 10352 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, 10353 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, 10354 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, 10355#ifdef YYCOVERAGE 10356 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, 10357#endif 10358 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " }, 10359 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, 10360 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, 10361 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, 10362 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, 10363 { "sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, 10364 { "tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, 10365 }; 10366 int testctrl = -1; 10367 int iCtrl = -1; 10368 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ 10369 int isOk = 0; 10370 int i, n2; 10371 const char *zCmd = 0; 10372 10373 open_db(p, 0); 10374 zCmd = nArg>=2 ? azArg[1] : "help"; 10375 10376 /* The argument can optionally begin with "-" or "--" */ 10377 if( zCmd[0]=='-' && zCmd[1] ){ 10378 zCmd++; 10379 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 10380 } 10381 10382 /* --help lists all test-controls */ 10383 if( strcmp(zCmd,"help")==0 ){ 10384 utf8_printf(p->out, "Available test-controls:\n"); 10385 for(i=0; i<ArraySize(aCtrl); i++){ 10386 utf8_printf(p->out, " .testctrl %s %s\n", 10387 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 10388 } 10389 rc = 1; 10390 goto meta_command_exit; 10391 } 10392 10393 /* convert testctrl text option to value. allow any unique prefix 10394 ** of the option name, or a numerical value. */ 10395 n2 = strlen30(zCmd); 10396 for(i=0; i<ArraySize(aCtrl); i++){ 10397 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 10398 if( testctrl<0 ){ 10399 testctrl = aCtrl[i].ctrlCode; 10400 iCtrl = i; 10401 }else{ 10402 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" 10403 "Use \".testctrl --help\" for help\n", zCmd); 10404 rc = 1; 10405 goto meta_command_exit; 10406 } 10407 } 10408 } 10409 if( testctrl<0 ){ 10410 utf8_printf(stderr,"Error: unknown test-control: %s\n" 10411 "Use \".testctrl --help\" for help\n", zCmd); 10412 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ 10413 utf8_printf(stderr, 10414 "line %d: \".testctrl %s\" may not be used in safe mode\n", 10415 p->lineno, aCtrl[iCtrl].zCtrlName); 10416 exit(1); 10417 }else{ 10418 switch(testctrl){ 10419 10420 /* sqlite3_test_control(int, db, int) */ 10421 case SQLITE_TESTCTRL_OPTIMIZATIONS: 10422 if( nArg==3 ){ 10423 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); 10424 rc2 = sqlite3_test_control(testctrl, p->db, opt); 10425 isOk = 3; 10426 } 10427 break; 10428 10429 /* sqlite3_test_control(int) */ 10430 case SQLITE_TESTCTRL_PRNG_SAVE: 10431 case SQLITE_TESTCTRL_PRNG_RESTORE: 10432 case SQLITE_TESTCTRL_BYTEORDER: 10433 if( nArg==2 ){ 10434 rc2 = sqlite3_test_control(testctrl); 10435 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; 10436 } 10437 break; 10438 10439 /* sqlite3_test_control(int, uint) */ 10440 case SQLITE_TESTCTRL_PENDING_BYTE: 10441 if( nArg==3 ){ 10442 unsigned int opt = (unsigned int)integerValue(azArg[2]); 10443 rc2 = sqlite3_test_control(testctrl, opt); 10444 isOk = 3; 10445 } 10446 break; 10447 10448 /* sqlite3_test_control(int, int, sqlite3*) */ 10449 case SQLITE_TESTCTRL_PRNG_SEED: 10450 if( nArg==3 || nArg==4 ){ 10451 int ii = (int)integerValue(azArg[2]); 10452 sqlite3 *db; 10453 if( ii==0 && strcmp(azArg[2],"random")==0 ){ 10454 sqlite3_randomness(sizeof(ii),&ii); 10455 printf("-- random seed: %d\n", ii); 10456 } 10457 if( nArg==3 ){ 10458 db = 0; 10459 }else{ 10460 db = p->db; 10461 /* Make sure the schema has been loaded */ 10462 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); 10463 } 10464 rc2 = sqlite3_test_control(testctrl, ii, db); 10465 isOk = 3; 10466 } 10467 break; 10468 10469 /* sqlite3_test_control(int, int) */ 10470 case SQLITE_TESTCTRL_ASSERT: 10471 case SQLITE_TESTCTRL_ALWAYS: 10472 if( nArg==3 ){ 10473 int opt = booleanValue(azArg[2]); 10474 rc2 = sqlite3_test_control(testctrl, opt); 10475 isOk = 1; 10476 } 10477 break; 10478 10479 /* sqlite3_test_control(int, int) */ 10480 case SQLITE_TESTCTRL_LOCALTIME_FAULT: 10481 case SQLITE_TESTCTRL_NEVER_CORRUPT: 10482 if( nArg==3 ){ 10483 int opt = booleanValue(azArg[2]); 10484 rc2 = sqlite3_test_control(testctrl, opt); 10485 isOk = 3; 10486 } 10487 break; 10488 10489 /* sqlite3_test_control(sqlite3*) */ 10490 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: 10491 rc2 = sqlite3_test_control(testctrl, p->db); 10492 isOk = 3; 10493 break; 10494 10495 case SQLITE_TESTCTRL_IMPOSTER: 10496 if( nArg==5 ){ 10497 rc2 = sqlite3_test_control(testctrl, p->db, 10498 azArg[2], 10499 integerValue(azArg[3]), 10500 integerValue(azArg[4])); 10501 isOk = 3; 10502 } 10503 break; 10504 10505 case SQLITE_TESTCTRL_SEEK_COUNT: { 10506 u64 x = 0; 10507 rc2 = sqlite3_test_control(testctrl, p->db, &x); 10508 utf8_printf(p->out, "%llu\n", x); 10509 isOk = 3; 10510 break; 10511 } 10512 10513#ifdef YYCOVERAGE 10514 case SQLITE_TESTCTRL_PARSER_COVERAGE: { 10515 if( nArg==2 ){ 10516 sqlite3_test_control(testctrl, p->out); 10517 isOk = 3; 10518 } 10519 break; 10520 } 10521#endif 10522#ifdef SQLITE_DEBUG 10523 case SQLITE_TESTCTRL_TUNE: { 10524 if( nArg==4 ){ 10525 int id = (int)integerValue(azArg[2]); 10526 int val = (int)integerValue(azArg[3]); 10527 sqlite3_test_control(testctrl, id, &val); 10528 isOk = 3; 10529 }else if( nArg==3 ){ 10530 int id = (int)integerValue(azArg[2]); 10531 sqlite3_test_control(testctrl, -id, &rc2); 10532 isOk = 1; 10533 }else if( nArg==2 ){ 10534 int id = 1; 10535 while(1){ 10536 int val = 0; 10537 rc2 = sqlite3_test_control(testctrl, -id, &val); 10538 if( rc2!=SQLITE_OK ) break; 10539 if( id>1 ) utf8_printf(p->out, " "); 10540 utf8_printf(p->out, "%d: %d", id, val); 10541 id++; 10542 } 10543 if( id>1 ) utf8_printf(p->out, "\n"); 10544 isOk = 3; 10545 } 10546 break; 10547 } 10548#endif 10549 case SQLITE_TESTCTRL_SORTER_MMAP: 10550 if( nArg==3 ){ 10551 int opt = (unsigned int)integerValue(azArg[2]); 10552 rc2 = sqlite3_test_control(testctrl, p->db, opt); 10553 isOk = 3; 10554 } 10555 break; 10556 } 10557 } 10558 if( isOk==0 && iCtrl>=0 ){ 10559 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 10560 rc = 1; 10561 }else if( isOk==1 ){ 10562 raw_printf(p->out, "%d\n", rc2); 10563 }else if( isOk==2 ){ 10564 raw_printf(p->out, "0x%08x\n", rc2); 10565 } 10566 }else 10567#endif /* !defined(SQLITE_UNTESTABLE) */ 10568 10569 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ 10570 open_db(p, 0); 10571 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); 10572 }else 10573 10574 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ 10575 if( nArg==2 ){ 10576 enableTimer = booleanValue(azArg[1]); 10577 if( enableTimer && !HAS_TIMER ){ 10578 raw_printf(stderr, "Error: timer not available on this system.\n"); 10579 enableTimer = 0; 10580 } 10581 }else{ 10582 raw_printf(stderr, "Usage: .timer on|off\n"); 10583 rc = 1; 10584 } 10585 }else 10586 10587#ifndef SQLITE_OMIT_TRACE 10588 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ 10589 int mType = 0; 10590 int jj; 10591 open_db(p, 0); 10592 for(jj=1; jj<nArg; jj++){ 10593 const char *z = azArg[jj]; 10594 if( z[0]=='-' ){ 10595 if( optionMatch(z, "expanded") ){ 10596 p->eTraceType = SHELL_TRACE_EXPANDED; 10597 } 10598#ifdef SQLITE_ENABLE_NORMALIZE 10599 else if( optionMatch(z, "normalized") ){ 10600 p->eTraceType = SHELL_TRACE_NORMALIZED; 10601 } 10602#endif 10603 else if( optionMatch(z, "plain") ){ 10604 p->eTraceType = SHELL_TRACE_PLAIN; 10605 } 10606 else if( optionMatch(z, "profile") ){ 10607 mType |= SQLITE_TRACE_PROFILE; 10608 } 10609 else if( optionMatch(z, "row") ){ 10610 mType |= SQLITE_TRACE_ROW; 10611 } 10612 else if( optionMatch(z, "stmt") ){ 10613 mType |= SQLITE_TRACE_STMT; 10614 } 10615 else if( optionMatch(z, "close") ){ 10616 mType |= SQLITE_TRACE_CLOSE; 10617 } 10618 else { 10619 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); 10620 rc = 1; 10621 goto meta_command_exit; 10622 } 10623 }else{ 10624 output_file_close(p->traceOut); 10625 p->traceOut = output_file_open(azArg[1], 0); 10626 } 10627 } 10628 if( p->traceOut==0 ){ 10629 sqlite3_trace_v2(p->db, 0, 0, 0); 10630 }else{ 10631 if( mType==0 ) mType = SQLITE_TRACE_STMT; 10632 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); 10633 } 10634 }else 10635#endif /* !defined(SQLITE_OMIT_TRACE) */ 10636 10637#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) 10638 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ 10639 int ii; 10640 int lenOpt; 10641 char *zOpt; 10642 if( nArg<2 ){ 10643 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); 10644 rc = 1; 10645 goto meta_command_exit; 10646 } 10647 open_db(p, 0); 10648 zOpt = azArg[1]; 10649 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; 10650 lenOpt = (int)strlen(zOpt); 10651 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ 10652 assert( azArg[nArg]==0 ); 10653 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); 10654 }else{ 10655 for(ii=1; ii<nArg; ii++){ 10656 sqlite3_create_module(p->db, azArg[ii], 0, 0); 10657 } 10658 } 10659 }else 10660#endif 10661 10662#if SQLITE_USER_AUTHENTICATION 10663 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ 10664 if( nArg<2 ){ 10665 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); 10666 rc = 1; 10667 goto meta_command_exit; 10668 } 10669 open_db(p, 0); 10670 if( strcmp(azArg[1],"login")==0 ){ 10671 if( nArg!=4 ){ 10672 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); 10673 rc = 1; 10674 goto meta_command_exit; 10675 } 10676 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], 10677 strlen30(azArg[3])); 10678 if( rc ){ 10679 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); 10680 rc = 1; 10681 } 10682 }else if( strcmp(azArg[1],"add")==0 ){ 10683 if( nArg!=5 ){ 10684 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); 10685 rc = 1; 10686 goto meta_command_exit; 10687 } 10688 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10689 booleanValue(azArg[4])); 10690 if( rc ){ 10691 raw_printf(stderr, "User-Add failed: %d\n", rc); 10692 rc = 1; 10693 } 10694 }else if( strcmp(azArg[1],"edit")==0 ){ 10695 if( nArg!=5 ){ 10696 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); 10697 rc = 1; 10698 goto meta_command_exit; 10699 } 10700 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10701 booleanValue(azArg[4])); 10702 if( rc ){ 10703 raw_printf(stderr, "User-Edit failed: %d\n", rc); 10704 rc = 1; 10705 } 10706 }else if( strcmp(azArg[1],"delete")==0 ){ 10707 if( nArg!=3 ){ 10708 raw_printf(stderr, "Usage: .user delete USER\n"); 10709 rc = 1; 10710 goto meta_command_exit; 10711 } 10712 rc = sqlite3_user_delete(p->db, azArg[2]); 10713 if( rc ){ 10714 raw_printf(stderr, "User-Delete failed: %d\n", rc); 10715 rc = 1; 10716 } 10717 }else{ 10718 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); 10719 rc = 1; 10720 goto meta_command_exit; 10721 } 10722 }else 10723#endif /* SQLITE_USER_AUTHENTICATION */ 10724 10725 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ 10726 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, 10727 sqlite3_libversion(), sqlite3_sourceid()); 10728#if SQLITE_HAVE_ZLIB 10729 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); 10730#endif 10731#define CTIMEOPT_VAL_(opt) #opt 10732#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) 10733#if defined(__clang__) && defined(__clang_major__) 10734 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." 10735 CTIMEOPT_VAL(__clang_minor__) "." 10736 CTIMEOPT_VAL(__clang_patchlevel__) "\n"); 10737#elif defined(_MSC_VER) 10738 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); 10739#elif defined(__GNUC__) && defined(__VERSION__) 10740 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); 10741#endif 10742 }else 10743 10744 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ 10745 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10746 sqlite3_vfs *pVfs = 0; 10747 if( p->db ){ 10748 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); 10749 if( pVfs ){ 10750 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); 10751 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10752 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10753 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10754 } 10755 } 10756 }else 10757 10758 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ 10759 sqlite3_vfs *pVfs; 10760 sqlite3_vfs *pCurrent = 0; 10761 if( p->db ){ 10762 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); 10763 } 10764 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 10765 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, 10766 pVfs==pCurrent ? " <--- CURRENT" : ""); 10767 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10768 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10769 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10770 if( pVfs->pNext ){ 10771 raw_printf(p->out, "-----------------------------------\n"); 10772 } 10773 } 10774 }else 10775 10776 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ 10777 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10778 char *zVfsName = 0; 10779 if( p->db ){ 10780 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); 10781 if( zVfsName ){ 10782 utf8_printf(p->out, "%s\n", zVfsName); 10783 sqlite3_free(zVfsName); 10784 } 10785 } 10786 }else 10787 10788 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ 10789 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; 10790 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); 10791 }else 10792 10793 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ 10794 int j; 10795 assert( nArg<=ArraySize(azArg) ); 10796 p->nWidth = nArg-1; 10797 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); 10798 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); 10799 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; 10800 for(j=1; j<nArg; j++){ 10801 p->colWidth[j-1] = (int)integerValue(azArg[j]); 10802 } 10803 }else 10804 10805 { 10806 utf8_printf(stderr, "Error: unknown command or invalid arguments: " 10807 " \"%s\". Enter \".help\" for help\n", azArg[0]); 10808 rc = 1; 10809 } 10810 10811meta_command_exit: 10812 if( p->outCount ){ 10813 p->outCount--; 10814 if( p->outCount==0 ) output_reset(p); 10815 } 10816 p->bSafeMode = p->bSafeModePersist; 10817 return rc; 10818} 10819 10820/* Line scan result and intermediate states (supporting scan resumption) 10821*/ 10822#ifndef CHAR_BIT 10823# define CHAR_BIT 8 10824#endif 10825typedef enum { 10826 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, 10827 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, 10828 QSS_Start = 0 10829} QuickScanState; 10830#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) 10831#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) 10832#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) 10833#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) 10834#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) 10835 10836/* 10837** Scan line for classification to guide shell's handling. 10838** The scan is resumable for subsequent lines when prior 10839** return values are passed as the 2nd argument. 10840*/ 10841static QuickScanState quickscan(char *zLine, QuickScanState qss){ 10842 char cin; 10843 char cWait = (char)qss; /* intentional narrowing loss */ 10844 if( cWait==0 ){ 10845 PlainScan: 10846 assert( cWait==0 ); 10847 while( (cin = *zLine++)!=0 ){ 10848 if( IsSpace(cin) ) 10849 continue; 10850 switch (cin){ 10851 case '-': 10852 if( *zLine!='-' ) 10853 break; 10854 while((cin = *++zLine)!=0 ) 10855 if( cin=='\n') 10856 goto PlainScan; 10857 return qss; 10858 case ';': 10859 qss |= QSS_EndingSemi; 10860 continue; 10861 case '/': 10862 if( *zLine=='*' ){ 10863 ++zLine; 10864 cWait = '*'; 10865 qss = QSS_SETV(qss, cWait); 10866 goto TermScan; 10867 } 10868 break; 10869 case '[': 10870 cin = ']'; 10871 /* fall thru */ 10872 case '`': case '\'': case '"': 10873 cWait = cin; 10874 qss = QSS_HasDark | cWait; 10875 goto TermScan; 10876 default: 10877 break; 10878 } 10879 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; 10880 } 10881 }else{ 10882 TermScan: 10883 while( (cin = *zLine++)!=0 ){ 10884 if( cin==cWait ){ 10885 switch( cWait ){ 10886 case '*': 10887 if( *zLine != '/' ) 10888 continue; 10889 ++zLine; 10890 cWait = 0; 10891 qss = QSS_SETV(qss, 0); 10892 goto PlainScan; 10893 case '`': case '\'': case '"': 10894 if(*zLine==cWait){ 10895 ++zLine; 10896 continue; 10897 } 10898 /* fall thru */ 10899 case ']': 10900 cWait = 0; 10901 qss = QSS_SETV(qss, 0); 10902 goto PlainScan; 10903 default: assert(0); 10904 } 10905 } 10906 } 10907 } 10908 return qss; 10909} 10910 10911/* 10912** Return TRUE if the line typed in is an SQL command terminator other 10913** than a semi-colon. The SQL Server style "go" command is understood 10914** as is the Oracle "/". 10915*/ 10916static int line_is_command_terminator(char *zLine){ 10917 while( IsSpace(zLine[0]) ){ zLine++; }; 10918 if( zLine[0]=='/' ) 10919 zLine += 1; /* Oracle */ 10920 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) 10921 zLine += 2; /* SQL Server */ 10922 else 10923 return 0; 10924 return quickscan(zLine, QSS_Start)==QSS_Start; 10925} 10926 10927/* 10928** We need a default sqlite3_complete() implementation to use in case 10929** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes 10930** any arbitrary text is a complete SQL statement. This is not very 10931** user-friendly, but it does seem to work. 10932*/ 10933#ifdef SQLITE_OMIT_COMPLETE 10934#define sqlite3_complete(x) 1 10935#endif 10936 10937/* 10938** Return true if zSql is a complete SQL statement. Return false if it 10939** ends in the middle of a string literal or C-style comment. 10940*/ 10941static int line_is_complete(char *zSql, int nSql){ 10942 int rc; 10943 if( zSql==0 ) return 1; 10944 zSql[nSql] = ';'; 10945 zSql[nSql+1] = 0; 10946 rc = sqlite3_complete(zSql); 10947 zSql[nSql] = 0; 10948 return rc; 10949} 10950 10951/* 10952** Run a single line of SQL. Return the number of errors. 10953*/ 10954static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ 10955 int rc; 10956 char *zErrMsg = 0; 10957 10958 open_db(p, 0); 10959 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); 10960 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 10961 BEGIN_TIMER; 10962 rc = shell_exec(p, zSql, &zErrMsg); 10963 END_TIMER; 10964 if( rc || zErrMsg ){ 10965 char zPrefix[100]; 10966 const char *zErrorTail; 10967 const char *zErrorType; 10968 if( zErrMsg==0 ){ 10969 zErrorType = "Error"; 10970 zErrorTail = sqlite3_errmsg(p->db); 10971 }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){ 10972 zErrorType = "Parse error"; 10973 zErrorTail = &zErrMsg[12]; 10974 }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){ 10975 zErrorType = "Runtime error"; 10976 zErrorTail = &zErrMsg[10]; 10977 }else{ 10978 zErrorType = "Error"; 10979 zErrorTail = zErrMsg; 10980 } 10981 if( in!=0 || !stdin_is_interactive ){ 10982 sqlite3_snprintf(sizeof(zPrefix), zPrefix, 10983 "%s near line %d:", zErrorType, startline); 10984 }else{ 10985 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); 10986 } 10987 utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail); 10988 sqlite3_free(zErrMsg); 10989 zErrMsg = 0; 10990 return 1; 10991 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ 10992 char zLineBuf[2000]; 10993 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, 10994 "changes: %lld total_changes: %lld", 10995 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); 10996 raw_printf(p->out, "%s\n", zLineBuf); 10997 } 10998 return 0; 10999} 11000 11001static void echo_group_input(ShellState *p, const char *zDo){ 11002 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); 11003} 11004 11005#ifdef SQLITE_SHELL_FIDDLE 11006/* 11007** Alternate one_input_line() impl for wasm mode. This is not in the primary impl 11008** because we need the global shellState and cannot access it from that function 11009** without moving lots of code around (creating a larger/messier diff). 11010*/ 11011static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 11012 /* Parse the next line from shellState.wasm.zInput. */ 11013 const char *zBegin = shellState.wasm.zPos; 11014 const char *z = zBegin; 11015 char *zLine = 0; 11016 i64 nZ = 0; 11017 11018 UNUSED_PARAMETER(in); 11019 UNUSED_PARAMETER(isContinuation); 11020 if(!z || !*z){ 11021 return 0; 11022 } 11023 while(*z && isspace(*z)) ++z; 11024 zBegin = z; 11025 for(; *z && '\n'!=*z; ++nZ, ++z){} 11026 if(nZ>0 && '\r'==zBegin[nZ-1]){ 11027 --nZ; 11028 } 11029 shellState.wasm.zPos = z; 11030 zLine = realloc(zPrior, nZ+1); 11031 shell_check_oom(zLine); 11032 memcpy(zLine, zBegin, nZ); 11033 zLine[nZ] = 0; 11034 return zLine; 11035} 11036#endif /* SQLITE_SHELL_FIDDLE */ 11037 11038/* 11039** Read input from *in and process it. If *in==0 then input 11040** is interactive - the user is typing it it. Otherwise, input 11041** is coming from a file or device. A prompt is issued and history 11042** is saved only if input is interactive. An interrupt signal will 11043** cause this routine to exit immediately, unless input is interactive. 11044** 11045** Return the number of errors. 11046*/ 11047static int process_input(ShellState *p){ 11048 char *zLine = 0; /* A single input line */ 11049 char *zSql = 0; /* Accumulated SQL text */ 11050 i64 nLine; /* Length of current line */ 11051 i64 nSql = 0; /* Bytes of zSql[] used */ 11052 i64 nAlloc = 0; /* Allocated zSql[] space */ 11053 int rc; /* Error code */ 11054 int errCnt = 0; /* Number of errors seen */ 11055 i64 startline = 0; /* Line number for start of current input */ 11056 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ 11057 11058 if( p->inputNesting==MAX_INPUT_NESTING ){ 11059 /* This will be more informative in a later version. */ 11060 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d." 11061 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); 11062 return 1; 11063 } 11064 ++p->inputNesting; 11065 p->lineno = 0; 11066 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ 11067 fflush(p->out); 11068 zLine = one_input_line(p->in, zLine, nSql>0); 11069 if( zLine==0 ){ 11070 /* End of input */ 11071 if( p->in==0 && stdin_is_interactive ) printf("\n"); 11072 break; 11073 } 11074 if( seenInterrupt ){ 11075 if( p->in!=0 ) break; 11076 seenInterrupt = 0; 11077 } 11078 p->lineno++; 11079 if( QSS_INPLAIN(qss) 11080 && line_is_command_terminator(zLine) 11081 && line_is_complete(zSql, nSql) ){ 11082 memcpy(zLine,";",2); 11083 } 11084 qss = quickscan(zLine, qss); 11085 if( QSS_PLAINWHITE(qss) && nSql==0 ){ 11086 /* Just swallow single-line whitespace */ 11087 echo_group_input(p, zLine); 11088 qss = QSS_Start; 11089 continue; 11090 } 11091 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ 11092 echo_group_input(p, zLine); 11093 if( zLine[0]=='.' ){ 11094 rc = do_meta_command(zLine, p); 11095 if( rc==2 ){ /* exit requested */ 11096 break; 11097 }else if( rc ){ 11098 errCnt++; 11099 } 11100 } 11101 qss = QSS_Start; 11102 continue; 11103 } 11104 /* No single-line dispositions remain; accumulate line(s). */ 11105 nLine = strlen(zLine); 11106 if( nSql+nLine+2>=nAlloc ){ 11107 /* Grow buffer by half-again increments when big. */ 11108 nAlloc = nSql+(nSql>>1)+nLine+100; 11109 zSql = realloc(zSql, nAlloc); 11110 shell_check_oom(zSql); 11111 } 11112 if( nSql==0 ){ 11113 i64 i; 11114 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} 11115 assert( nAlloc>0 && zSql!=0 ); 11116 memcpy(zSql, zLine+i, nLine+1-i); 11117 startline = p->lineno; 11118 nSql = nLine-i; 11119 }else{ 11120 zSql[nSql++] = '\n'; 11121 memcpy(zSql+nSql, zLine, nLine+1); 11122 nSql += nLine; 11123 } 11124 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ 11125 echo_group_input(p, zSql); 11126 errCnt += runOneSqlLine(p, zSql, p->in, startline); 11127 nSql = 0; 11128 if( p->outCount ){ 11129 output_reset(p); 11130 p->outCount = 0; 11131 }else{ 11132 clearTempFile(p); 11133 } 11134 p->bSafeMode = p->bSafeModePersist; 11135 qss = QSS_Start; 11136 }else if( nSql && QSS_PLAINWHITE(qss) ){ 11137 echo_group_input(p, zSql); 11138 nSql = 0; 11139 qss = QSS_Start; 11140 } 11141 } 11142 if( nSql ){ 11143 /* This may be incomplete. Let the SQL parser deal with that. */ 11144 echo_group_input(p, zSql); 11145 errCnt += runOneSqlLine(p, zSql, p->in, startline); 11146 } 11147 free(zSql); 11148 free(zLine); 11149 --p->inputNesting; 11150 return errCnt>0; 11151} 11152 11153/* 11154** Return a pathname which is the user's home directory. A 11155** 0 return indicates an error of some kind. 11156*/ 11157static char *find_home_dir(int clearFlag){ 11158 static char *home_dir = NULL; 11159 if( clearFlag ){ 11160 free(home_dir); 11161 home_dir = 0; 11162 return 0; 11163 } 11164 if( home_dir ) return home_dir; 11165 11166#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 11167 && !defined(__RTP__) && !defined(_WRS_KERNEL) 11168 { 11169 struct passwd *pwent; 11170 uid_t uid = getuid(); 11171 if( (pwent=getpwuid(uid)) != NULL) { 11172 home_dir = pwent->pw_dir; 11173 } 11174 } 11175#endif 11176 11177#if defined(_WIN32_WCE) 11178 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() 11179 */ 11180 home_dir = "/"; 11181#else 11182 11183#if defined(_WIN32) || defined(WIN32) 11184 if (!home_dir) { 11185 home_dir = getenv("USERPROFILE"); 11186 } 11187#endif 11188 11189 if (!home_dir) { 11190 home_dir = getenv("HOME"); 11191 } 11192 11193#if defined(_WIN32) || defined(WIN32) 11194 if (!home_dir) { 11195 char *zDrive, *zPath; 11196 int n; 11197 zDrive = getenv("HOMEDRIVE"); 11198 zPath = getenv("HOMEPATH"); 11199 if( zDrive && zPath ){ 11200 n = strlen30(zDrive) + strlen30(zPath) + 1; 11201 home_dir = malloc( n ); 11202 if( home_dir==0 ) return 0; 11203 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); 11204 return home_dir; 11205 } 11206 home_dir = "c:\\"; 11207 } 11208#endif 11209 11210#endif /* !_WIN32_WCE */ 11211 11212 if( home_dir ){ 11213 i64 n = strlen(home_dir) + 1; 11214 char *z = malloc( n ); 11215 if( z ) memcpy(z, home_dir, n); 11216 home_dir = z; 11217 } 11218 11219 return home_dir; 11220} 11221 11222/* 11223** Read input from the file given by sqliterc_override. Or if that 11224** parameter is NULL, take input from ~/.sqliterc 11225** 11226** Returns the number of errors. 11227*/ 11228static void process_sqliterc( 11229 ShellState *p, /* Configuration data */ 11230 const char *sqliterc_override /* Name of config file. NULL to use default */ 11231){ 11232 char *home_dir = NULL; 11233 const char *sqliterc = sqliterc_override; 11234 char *zBuf = 0; 11235 FILE *inSaved = p->in; 11236 int savedLineno = p->lineno; 11237 11238 if (sqliterc == NULL) { 11239 home_dir = find_home_dir(0); 11240 if( home_dir==0 ){ 11241 raw_printf(stderr, "-- warning: cannot find home directory;" 11242 " cannot read ~/.sqliterc\n"); 11243 return; 11244 } 11245 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); 11246 shell_check_oom(zBuf); 11247 sqliterc = zBuf; 11248 } 11249 p->in = fopen(sqliterc,"rb"); 11250 if( p->in ){ 11251 if( stdin_is_interactive ){ 11252 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); 11253 } 11254 if( process_input(p) && bail_on_error ) exit(1); 11255 fclose(p->in); 11256 }else if( sqliterc_override!=0 ){ 11257 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc); 11258 if( bail_on_error ) exit(1); 11259 } 11260 p->in = inSaved; 11261 p->lineno = savedLineno; 11262 sqlite3_free(zBuf); 11263} 11264 11265/* 11266** Show available command line options 11267*/ 11268static const char zOptions[] = 11269#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 11270 " -A ARGS... run \".archive ARGS\" and exit\n" 11271#endif 11272 " -append append the database to the end of the file\n" 11273 " -ascii set output mode to 'ascii'\n" 11274 " -bail stop after hitting an error\n" 11275 " -batch force batch I/O\n" 11276 " -box set output mode to 'box'\n" 11277 " -column set output mode to 'column'\n" 11278 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 11279 " -csv set output mode to 'csv'\n" 11280#if !defined(SQLITE_OMIT_DESERIALIZE) 11281 " -deserialize open the database using sqlite3_deserialize()\n" 11282#endif 11283 " -echo print inputs before execution\n" 11284 " -init FILENAME read/process named file\n" 11285 " -[no]header turn headers on or off\n" 11286#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 11287 " -heap SIZE Size of heap for memsys3 or memsys5\n" 11288#endif 11289 " -help show this message\n" 11290 " -html set output mode to HTML\n" 11291 " -interactive force interactive I/O\n" 11292 " -json set output mode to 'json'\n" 11293 " -line set output mode to 'line'\n" 11294 " -list set output mode to 'list'\n" 11295 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 11296 " -markdown set output mode to 'markdown'\n" 11297#if !defined(SQLITE_OMIT_DESERIALIZE) 11298 " -maxsize N maximum size for a --deserialize database\n" 11299#endif 11300 " -memtrace trace all memory allocations and deallocations\n" 11301 " -mmap N default mmap size set to N\n" 11302#ifdef SQLITE_ENABLE_MULTIPLEX 11303 " -multiplex enable the multiplexor VFS\n" 11304#endif 11305 " -newline SEP set output row separator. Default: '\\n'\n" 11306 " -nofollow refuse to open symbolic links to database files\n" 11307 " -nonce STRING set the safe-mode escape nonce\n" 11308 " -nullvalue TEXT set text string for NULL values. Default ''\n" 11309 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 11310 " -quote set output mode to 'quote'\n" 11311 " -readonly open the database read-only\n" 11312 " -safe enable safe-mode\n" 11313 " -separator SEP set output column separator. Default: '|'\n" 11314#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11315 " -sorterref SIZE sorter references threshold size\n" 11316#endif 11317 " -stats print memory stats before each finalize\n" 11318 " -table set output mode to 'table'\n" 11319 " -tabs set output mode to 'tabs'\n" 11320 " -version show SQLite version\n" 11321 " -vfs NAME use NAME as the default VFS\n" 11322#ifdef SQLITE_ENABLE_VFSTRACE 11323 " -vfstrace enable tracing of all VFS calls\n" 11324#endif 11325#ifdef SQLITE_HAVE_ZLIB 11326 " -zip open the file as a ZIP Archive\n" 11327#endif 11328; 11329static void usage(int showDetail){ 11330 utf8_printf(stderr, 11331 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 11332 "FILENAME is the name of an SQLite database. A new database is created\n" 11333 "if the file does not previously exist.\n", Argv0); 11334 if( showDetail ){ 11335 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); 11336 }else{ 11337 raw_printf(stderr, "Use the -help option for additional information\n"); 11338 } 11339 exit(1); 11340} 11341 11342/* 11343** Internal check: Verify that the SQLite is uninitialized. Print a 11344** error message if it is initialized. 11345*/ 11346static void verify_uninitialized(void){ 11347 if( sqlite3_config(-1)==SQLITE_MISUSE ){ 11348 utf8_printf(stdout, "WARNING: attempt to configure SQLite after" 11349 " initialization.\n"); 11350 } 11351} 11352 11353/* 11354** Initialize the state information in data 11355*/ 11356static void main_init(ShellState *data) { 11357 memset(data, 0, sizeof(*data)); 11358 data->normalMode = data->cMode = data->mode = MODE_List; 11359 data->autoExplain = 1; 11360 data->pAuxDb = &data->aAuxDb[0]; 11361 memcpy(data->colSeparator,SEP_Column, 2); 11362 memcpy(data->rowSeparator,SEP_Row, 2); 11363 data->showHeader = 0; 11364 data->shellFlgs = SHFLG_Lookaside; 11365 verify_uninitialized(); 11366 sqlite3_config(SQLITE_CONFIG_URI, 1); 11367 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); 11368 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 11369 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); 11370 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); 11371} 11372 11373/* 11374** Output text to the console in a font that attracts extra attention. 11375*/ 11376#ifdef _WIN32 11377static void printBold(const char *zText){ 11378#if !SQLITE_OS_WINRT 11379 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 11380 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; 11381 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); 11382 SetConsoleTextAttribute(out, 11383 FOREGROUND_RED|FOREGROUND_INTENSITY 11384 ); 11385#endif 11386 printf("%s", zText); 11387#if !SQLITE_OS_WINRT 11388 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); 11389#endif 11390} 11391#else 11392static void printBold(const char *zText){ 11393 printf("\033[1m%s\033[0m", zText); 11394} 11395#endif 11396 11397/* 11398** Get the argument to an --option. Throw an error and die if no argument 11399** is available. 11400*/ 11401static char *cmdline_option_value(int argc, char **argv, int i){ 11402 if( i==argc ){ 11403 utf8_printf(stderr, "%s: Error: missing argument to %s\n", 11404 argv[0], argv[argc-1]); 11405 exit(1); 11406 } 11407 return argv[i]; 11408} 11409 11410#ifndef SQLITE_SHELL_IS_UTF8 11411# if (defined(_WIN32) || defined(WIN32)) \ 11412 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) 11413# define SQLITE_SHELL_IS_UTF8 (0) 11414# else 11415# define SQLITE_SHELL_IS_UTF8 (1) 11416# endif 11417#endif 11418 11419#ifdef SQLITE_SHELL_FIDDLE 11420# define main fiddle_main 11421#endif 11422 11423#if SQLITE_SHELL_IS_UTF8 11424int SQLITE_CDECL main(int argc, char **argv){ 11425#else 11426int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ 11427 char **argv; 11428#endif 11429#ifdef SQLITE_DEBUG 11430 sqlite3_int64 mem_main_enter = sqlite3_memory_used(); 11431#endif 11432 char *zErrMsg = 0; 11433#ifdef SQLITE_SHELL_FIDDLE 11434# define data shellState 11435#else 11436 ShellState data; 11437#endif 11438 const char *zInitFile = 0; 11439 int i; 11440 int rc = 0; 11441 int warnInmemoryDb = 0; 11442 int readStdin = 1; 11443 int nCmd = 0; 11444 char **azCmd = 0; 11445 const char *zVfs = 0; /* Value of -vfs command-line option */ 11446#if !SQLITE_SHELL_IS_UTF8 11447 char **argvToFree = 0; 11448 int argcToFree = 0; 11449#endif 11450 11451 setBinaryMode(stdin, 0); 11452 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ 11453#ifdef SQLITE_SHELL_FIDDLE 11454 stdin_is_interactive = 0; 11455 stdout_is_console = 1; 11456 data.wasm.zDefaultDbName = "/fiddle.sqlite3"; 11457#else 11458 stdin_is_interactive = isatty(0); 11459 stdout_is_console = isatty(1); 11460#endif 11461 11462#if !defined(_WIN32_WCE) 11463 if( getenv("SQLITE_DEBUG_BREAK") ){ 11464 if( isatty(0) && isatty(2) ){ 11465 fprintf(stderr, 11466 "attach debugger to process %d and press any key to continue.\n", 11467 GETPID()); 11468 fgetc(stdin); 11469 }else{ 11470#if defined(_WIN32) || defined(WIN32) 11471#if SQLITE_OS_WINRT 11472 __debugbreak(); 11473#else 11474 DebugBreak(); 11475#endif 11476#elif defined(SIGTRAP) 11477 raise(SIGTRAP); 11478#endif 11479 } 11480 } 11481#endif 11482 11483#if USE_SYSTEM_SQLITE+0!=1 11484 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ 11485 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", 11486 sqlite3_sourceid(), SQLITE_SOURCE_ID); 11487 exit(1); 11488 } 11489#endif 11490 main_init(&data); 11491 11492 /* On Windows, we must translate command-line arguments into UTF-8. 11493 ** The SQLite memory allocator subsystem has to be enabled in order to 11494 ** do this. But we want to run an sqlite3_shutdown() afterwards so that 11495 ** subsequent sqlite3_config() calls will work. So copy all results into 11496 ** memory that does not come from the SQLite memory allocator. 11497 */ 11498#if !SQLITE_SHELL_IS_UTF8 11499 sqlite3_initialize(); 11500 argvToFree = malloc(sizeof(argv[0])*argc*2); 11501 shell_check_oom(argvToFree); 11502 argcToFree = argc; 11503 argv = argvToFree + argc; 11504 for(i=0; i<argc; i++){ 11505 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); 11506 i64 n; 11507 shell_check_oom(z); 11508 n = strlen(z); 11509 argv[i] = malloc( n+1 ); 11510 shell_check_oom(argv[i]); 11511 memcpy(argv[i], z, n+1); 11512 argvToFree[i] = argv[i]; 11513 sqlite3_free(z); 11514 } 11515 sqlite3_shutdown(); 11516#endif 11517 11518 assert( argc>=1 && argv && argv[0] ); 11519 Argv0 = argv[0]; 11520 11521 /* Make sure we have a valid signal handler early, before anything 11522 ** else is done. 11523 */ 11524#ifdef SIGINT 11525 signal(SIGINT, interrupt_handler); 11526#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 11527 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); 11528#endif 11529 11530#ifdef SQLITE_SHELL_DBNAME_PROC 11531 { 11532 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name 11533 ** of a C-function that will provide the name of the database file. Use 11534 ** this compile-time option to embed this shell program in larger 11535 ** applications. */ 11536 extern void SQLITE_SHELL_DBNAME_PROC(const char**); 11537 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); 11538 warnInmemoryDb = 0; 11539 } 11540#endif 11541 11542 /* Do an initial pass through the command-line argument to locate 11543 ** the name of the database file, the name of the initialization file, 11544 ** the size of the alternative malloc heap, 11545 ** and the first command to execute. 11546 */ 11547 verify_uninitialized(); 11548 for(i=1; i<argc; i++){ 11549 char *z; 11550 z = argv[i]; 11551 if( z[0]!='-' ){ 11552 if( data.aAuxDb->zDbFilename==0 ){ 11553 data.aAuxDb->zDbFilename = z; 11554 }else{ 11555 /* Excesss arguments are interpreted as SQL (or dot-commands) and 11556 ** mean that nothing is read from stdin */ 11557 readStdin = 0; 11558 nCmd++; 11559 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); 11560 shell_check_oom(azCmd); 11561 azCmd[nCmd-1] = z; 11562 } 11563 } 11564 if( z[1]=='-' ) z++; 11565 if( strcmp(z,"-separator")==0 11566 || strcmp(z,"-nullvalue")==0 11567 || strcmp(z,"-newline")==0 11568 || strcmp(z,"-cmd")==0 11569 ){ 11570 (void)cmdline_option_value(argc, argv, ++i); 11571 }else if( strcmp(z,"-init")==0 ){ 11572 zInitFile = cmdline_option_value(argc, argv, ++i); 11573 }else if( strcmp(z,"-batch")==0 ){ 11574 /* Need to check for batch mode here to so we can avoid printing 11575 ** informational messages (like from process_sqliterc) before 11576 ** we do the actual processing of arguments later in a second pass. 11577 */ 11578 stdin_is_interactive = 0; 11579 }else if( strcmp(z,"-heap")==0 ){ 11580#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 11581 const char *zSize; 11582 sqlite3_int64 szHeap; 11583 11584 zSize = cmdline_option_value(argc, argv, ++i); 11585 szHeap = integerValue(zSize); 11586 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; 11587 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); 11588#else 11589 (void)cmdline_option_value(argc, argv, ++i); 11590#endif 11591 }else if( strcmp(z,"-pagecache")==0 ){ 11592 sqlite3_int64 n, sz; 11593 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11594 if( sz>70000 ) sz = 70000; 11595 if( sz<0 ) sz = 0; 11596 n = integerValue(cmdline_option_value(argc,argv,++i)); 11597 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ 11598 n = 0xffffffffffffLL/sz; 11599 } 11600 sqlite3_config(SQLITE_CONFIG_PAGECACHE, 11601 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); 11602 data.shellFlgs |= SHFLG_Pagecache; 11603 }else if( strcmp(z,"-lookaside")==0 ){ 11604 int n, sz; 11605 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11606 if( sz<0 ) sz = 0; 11607 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11608 if( n<0 ) n = 0; 11609 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); 11610 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; 11611 }else if( strcmp(z,"-threadsafe")==0 ){ 11612 int n; 11613 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11614 switch( n ){ 11615 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; 11616 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; 11617 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; 11618 } 11619#ifdef SQLITE_ENABLE_VFSTRACE 11620 }else if( strcmp(z,"-vfstrace")==0 ){ 11621 extern int vfstrace_register( 11622 const char *zTraceName, 11623 const char *zOldVfsName, 11624 int (*xOut)(const char*,void*), 11625 void *pOutArg, 11626 int makeDefault 11627 ); 11628 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); 11629#endif 11630#ifdef SQLITE_ENABLE_MULTIPLEX 11631 }else if( strcmp(z,"-multiplex")==0 ){ 11632 extern int sqlite3_multiple_initialize(const char*,int); 11633 sqlite3_multiplex_initialize(0, 1); 11634#endif 11635 }else if( strcmp(z,"-mmap")==0 ){ 11636 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11637 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); 11638#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11639 }else if( strcmp(z,"-sorterref")==0 ){ 11640 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11641 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); 11642#endif 11643 }else if( strcmp(z,"-vfs")==0 ){ 11644 zVfs = cmdline_option_value(argc, argv, ++i); 11645#ifdef SQLITE_HAVE_ZLIB 11646 }else if( strcmp(z,"-zip")==0 ){ 11647 data.openMode = SHELL_OPEN_ZIPFILE; 11648#endif 11649 }else if( strcmp(z,"-append")==0 ){ 11650 data.openMode = SHELL_OPEN_APPENDVFS; 11651#ifndef SQLITE_OMIT_DESERIALIZE 11652 }else if( strcmp(z,"-deserialize")==0 ){ 11653 data.openMode = SHELL_OPEN_DESERIALIZE; 11654 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 11655 data.szMax = integerValue(argv[++i]); 11656#endif 11657 }else if( strcmp(z,"-readonly")==0 ){ 11658 data.openMode = SHELL_OPEN_READONLY; 11659 }else if( strcmp(z,"-nofollow")==0 ){ 11660 data.openFlags = SQLITE_OPEN_NOFOLLOW; 11661#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 11662 }else if( strncmp(z, "-A",2)==0 ){ 11663 /* All remaining command-line arguments are passed to the ".archive" 11664 ** command, so ignore them */ 11665 break; 11666#endif 11667 }else if( strcmp(z, "-memtrace")==0 ){ 11668 sqlite3MemTraceActivate(stderr); 11669 }else if( strcmp(z,"-bail")==0 ){ 11670 bail_on_error = 1; 11671 }else if( strcmp(z,"-nonce")==0 ){ 11672 free(data.zNonce); 11673 data.zNonce = strdup(argv[++i]); 11674 }else if( strcmp(z,"-safe")==0 ){ 11675 /* no-op - catch this on the second pass */ 11676 } 11677 } 11678 verify_uninitialized(); 11679 11680 11681#ifdef SQLITE_SHELL_INIT_PROC 11682 { 11683 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name 11684 ** of a C-function that will perform initialization actions on SQLite that 11685 ** occur just before or after sqlite3_initialize(). Use this compile-time 11686 ** option to embed this shell program in larger applications. */ 11687 extern void SQLITE_SHELL_INIT_PROC(void); 11688 SQLITE_SHELL_INIT_PROC(); 11689 } 11690#else 11691 /* All the sqlite3_config() calls have now been made. So it is safe 11692 ** to call sqlite3_initialize() and process any command line -vfs option. */ 11693 sqlite3_initialize(); 11694#endif 11695 11696 if( zVfs ){ 11697 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); 11698 if( pVfs ){ 11699 sqlite3_vfs_register(pVfs, 1); 11700 }else{ 11701 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); 11702 exit(1); 11703 } 11704 } 11705 11706 if( data.pAuxDb->zDbFilename==0 ){ 11707#ifndef SQLITE_OMIT_MEMORYDB 11708 data.pAuxDb->zDbFilename = ":memory:"; 11709 warnInmemoryDb = argc==1; 11710#else 11711 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); 11712 return 1; 11713#endif 11714 } 11715 data.out = stdout; 11716#ifndef SQLITE_SHELL_FIDDLE 11717 sqlite3_appendvfs_init(0,0,0); 11718#endif 11719 11720 /* Go ahead and open the database file if it already exists. If the 11721 ** file does not exist, delay opening it. This prevents empty database 11722 ** files from being created if a user mistypes the database name argument 11723 ** to the sqlite command-line tool. 11724 */ 11725 if( access(data.pAuxDb->zDbFilename, 0)==0 ){ 11726 open_db(&data, 0); 11727 } 11728 11729 /* Process the initialization file if there is one. If no -init option 11730 ** is given on the command line, look for a file named ~/.sqliterc and 11731 ** try to process it. 11732 */ 11733 process_sqliterc(&data,zInitFile); 11734 11735 /* Make a second pass through the command-line argument and set 11736 ** options. This second pass is delayed until after the initialization 11737 ** file is processed so that the command-line arguments will override 11738 ** settings in the initialization file. 11739 */ 11740 for(i=1; i<argc; i++){ 11741 char *z = argv[i]; 11742 if( z[0]!='-' ) continue; 11743 if( z[1]=='-' ){ z++; } 11744 if( strcmp(z,"-init")==0 ){ 11745 i++; 11746 }else if( strcmp(z,"-html")==0 ){ 11747 data.mode = MODE_Html; 11748 }else if( strcmp(z,"-list")==0 ){ 11749 data.mode = MODE_List; 11750 }else if( strcmp(z,"-quote")==0 ){ 11751 data.mode = MODE_Quote; 11752 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); 11753 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); 11754 }else if( strcmp(z,"-line")==0 ){ 11755 data.mode = MODE_Line; 11756 }else if( strcmp(z,"-column")==0 ){ 11757 data.mode = MODE_Column; 11758 }else if( strcmp(z,"-json")==0 ){ 11759 data.mode = MODE_Json; 11760 }else if( strcmp(z,"-markdown")==0 ){ 11761 data.mode = MODE_Markdown; 11762 }else if( strcmp(z,"-table")==0 ){ 11763 data.mode = MODE_Table; 11764 }else if( strcmp(z,"-box")==0 ){ 11765 data.mode = MODE_Box; 11766 }else if( strcmp(z,"-csv")==0 ){ 11767 data.mode = MODE_Csv; 11768 memcpy(data.colSeparator,",",2); 11769#ifdef SQLITE_HAVE_ZLIB 11770 }else if( strcmp(z,"-zip")==0 ){ 11771 data.openMode = SHELL_OPEN_ZIPFILE; 11772#endif 11773 }else if( strcmp(z,"-append")==0 ){ 11774 data.openMode = SHELL_OPEN_APPENDVFS; 11775#ifndef SQLITE_OMIT_DESERIALIZE 11776 }else if( strcmp(z,"-deserialize")==0 ){ 11777 data.openMode = SHELL_OPEN_DESERIALIZE; 11778 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 11779 data.szMax = integerValue(argv[++i]); 11780#endif 11781 }else if( strcmp(z,"-readonly")==0 ){ 11782 data.openMode = SHELL_OPEN_READONLY; 11783 }else if( strcmp(z,"-nofollow")==0 ){ 11784 data.openFlags |= SQLITE_OPEN_NOFOLLOW; 11785 }else if( strcmp(z,"-ascii")==0 ){ 11786 data.mode = MODE_Ascii; 11787 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit); 11788 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record); 11789 }else if( strcmp(z,"-tabs")==0 ){ 11790 data.mode = MODE_List; 11791 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab); 11792 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); 11793 }else if( strcmp(z,"-separator")==0 ){ 11794 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 11795 "%s",cmdline_option_value(argc,argv,++i)); 11796 }else if( strcmp(z,"-newline")==0 ){ 11797 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 11798 "%s",cmdline_option_value(argc,argv,++i)); 11799 }else if( strcmp(z,"-nullvalue")==0 ){ 11800 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, 11801 "%s",cmdline_option_value(argc,argv,++i)); 11802 }else if( strcmp(z,"-header")==0 ){ 11803 data.showHeader = 1; 11804 ShellSetFlag(&data, SHFLG_HeaderSet); 11805 }else if( strcmp(z,"-noheader")==0 ){ 11806 data.showHeader = 0; 11807 ShellSetFlag(&data, SHFLG_HeaderSet); 11808 }else if( strcmp(z,"-echo")==0 ){ 11809 ShellSetFlag(&data, SHFLG_Echo); 11810 }else if( strcmp(z,"-eqp")==0 ){ 11811 data.autoEQP = AUTOEQP_on; 11812 }else if( strcmp(z,"-eqpfull")==0 ){ 11813 data.autoEQP = AUTOEQP_full; 11814 }else if( strcmp(z,"-stats")==0 ){ 11815 data.statsOn = 1; 11816 }else if( strcmp(z,"-scanstats")==0 ){ 11817 data.scanstatsOn = 1; 11818 }else if( strcmp(z,"-backslash")==0 ){ 11819 /* Undocumented command-line option: -backslash 11820 ** Causes C-style backslash escapes to be evaluated in SQL statements 11821 ** prior to sending the SQL into SQLite. Useful for injecting 11822 ** crazy bytes in the middle of SQL statements for testing and debugging. 11823 */ 11824 ShellSetFlag(&data, SHFLG_Backslash); 11825 }else if( strcmp(z,"-bail")==0 ){ 11826 /* No-op. The bail_on_error flag should already be set. */ 11827 }else if( strcmp(z,"-version")==0 ){ 11828 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 11829 return 0; 11830 }else if( strcmp(z,"-interactive")==0 ){ 11831 stdin_is_interactive = 1; 11832 }else if( strcmp(z,"-batch")==0 ){ 11833 stdin_is_interactive = 0; 11834 }else if( strcmp(z,"-heap")==0 ){ 11835 i++; 11836 }else if( strcmp(z,"-pagecache")==0 ){ 11837 i+=2; 11838 }else if( strcmp(z,"-lookaside")==0 ){ 11839 i+=2; 11840 }else if( strcmp(z,"-threadsafe")==0 ){ 11841 i+=2; 11842 }else if( strcmp(z,"-nonce")==0 ){ 11843 i += 2; 11844 }else if( strcmp(z,"-mmap")==0 ){ 11845 i++; 11846 }else if( strcmp(z,"-memtrace")==0 ){ 11847 i++; 11848#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11849 }else if( strcmp(z,"-sorterref")==0 ){ 11850 i++; 11851#endif 11852 }else if( strcmp(z,"-vfs")==0 ){ 11853 i++; 11854#ifdef SQLITE_ENABLE_VFSTRACE 11855 }else if( strcmp(z,"-vfstrace")==0 ){ 11856 i++; 11857#endif 11858#ifdef SQLITE_ENABLE_MULTIPLEX 11859 }else if( strcmp(z,"-multiplex")==0 ){ 11860 i++; 11861#endif 11862 }else if( strcmp(z,"-help")==0 ){ 11863 usage(1); 11864 }else if( strcmp(z,"-cmd")==0 ){ 11865 /* Run commands that follow -cmd first and separately from commands 11866 ** that simply appear on the command-line. This seems goofy. It would 11867 ** be better if all commands ran in the order that they appear. But 11868 ** we retain the goofy behavior for historical compatibility. */ 11869 if( i==argc-1 ) break; 11870 z = cmdline_option_value(argc,argv,++i); 11871 if( z[0]=='.' ){ 11872 rc = do_meta_command(z, &data); 11873 if( rc && bail_on_error ) return rc==2 ? 0 : rc; 11874 }else{ 11875 open_db(&data, 0); 11876 rc = shell_exec(&data, z, &zErrMsg); 11877 if( zErrMsg!=0 ){ 11878 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11879 if( bail_on_error ) return rc!=0 ? rc : 1; 11880 }else if( rc!=0 ){ 11881 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); 11882 if( bail_on_error ) return rc; 11883 } 11884 } 11885#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 11886 }else if( strncmp(z, "-A", 2)==0 ){ 11887 if( nCmd>0 ){ 11888 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" 11889 " with \"%s\"\n", z); 11890 return 1; 11891 } 11892 open_db(&data, OPEN_DB_ZIPFILE); 11893 if( z[2] ){ 11894 argv[i] = &z[2]; 11895 arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); 11896 }else{ 11897 arDotCommand(&data, 1, argv+i, argc-i); 11898 } 11899 readStdin = 0; 11900 break; 11901#endif 11902 }else if( strcmp(z,"-safe")==0 ){ 11903 data.bSafeMode = data.bSafeModePersist = 1; 11904 }else{ 11905 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); 11906 raw_printf(stderr,"Use -help for a list of options.\n"); 11907 return 1; 11908 } 11909 data.cMode = data.mode; 11910 } 11911 11912 if( !readStdin ){ 11913 /* Run all arguments that do not begin with '-' as if they were separate 11914 ** command-line inputs, except for the argToSkip argument which contains 11915 ** the database filename. 11916 */ 11917 for(i=0; i<nCmd; i++){ 11918 if( azCmd[i][0]=='.' ){ 11919 rc = do_meta_command(azCmd[i], &data); 11920 if( rc ){ 11921 free(azCmd); 11922 return rc==2 ? 0 : rc; 11923 } 11924 }else{ 11925 open_db(&data, 0); 11926 rc = shell_exec(&data, azCmd[i], &zErrMsg); 11927 if( zErrMsg || rc ){ 11928 if( zErrMsg!=0 ){ 11929 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11930 }else{ 11931 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); 11932 } 11933 sqlite3_free(zErrMsg); 11934 free(azCmd); 11935 return rc!=0 ? rc : 1; 11936 } 11937 } 11938 } 11939 }else{ 11940 /* Run commands received from standard input 11941 */ 11942 if( stdin_is_interactive ){ 11943 char *zHome; 11944 char *zHistory; 11945 int nHistory; 11946 printf( 11947 "SQLite version %s %.19s\n" /*extra-version-info*/ 11948 "Enter \".help\" for usage hints.\n", 11949 sqlite3_libversion(), sqlite3_sourceid() 11950 ); 11951 if( warnInmemoryDb ){ 11952 printf("Connected to a "); 11953 printBold("transient in-memory database"); 11954 printf(".\nUse \".open FILENAME\" to reopen on a " 11955 "persistent database.\n"); 11956 } 11957 zHistory = getenv("SQLITE_HISTORY"); 11958 if( zHistory ){ 11959 zHistory = strdup(zHistory); 11960 }else if( (zHome = find_home_dir(0))!=0 ){ 11961 nHistory = strlen30(zHome) + 20; 11962 if( (zHistory = malloc(nHistory))!=0 ){ 11963 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); 11964 } 11965 } 11966 if( zHistory ){ shell_read_history(zHistory); } 11967#if HAVE_READLINE || HAVE_EDITLINE 11968 rl_attempted_completion_function = readline_completion; 11969#elif HAVE_LINENOISE 11970 linenoiseSetCompletionCallback(linenoise_completion); 11971#endif 11972 data.in = 0; 11973 rc = process_input(&data); 11974 if( zHistory ){ 11975 shell_stifle_history(2000); 11976 shell_write_history(zHistory); 11977 free(zHistory); 11978 } 11979 }else{ 11980 data.in = stdin; 11981 rc = process_input(&data); 11982 } 11983 } 11984#ifndef SQLITE_SHELL_FIDDLE 11985 /* In WASM mode we have to leave the db state in place so that 11986 ** client code can "push" SQL into it after this call returns. */ 11987 free(azCmd); 11988 set_table_name(&data, 0); 11989 if( data.db ){ 11990 session_close_all(&data, -1); 11991 close_db(data.db); 11992 } 11993 for(i=0; i<ArraySize(data.aAuxDb); i++){ 11994 sqlite3_free(data.aAuxDb[i].zFreeOnClose); 11995 if( data.aAuxDb[i].db ){ 11996 session_close_all(&data, i); 11997 close_db(data.aAuxDb[i].db); 11998 } 11999 } 12000 find_home_dir(1); 12001 output_reset(&data); 12002 data.doXdgOpen = 0; 12003 clearTempFile(&data); 12004#if !SQLITE_SHELL_IS_UTF8 12005 for(i=0; i<argcToFree; i++) free(argvToFree[i]); 12006 free(argvToFree); 12007#endif 12008 free(data.colWidth); 12009 free(data.zNonce); 12010 /* Clear the global data structure so that valgrind will detect memory 12011 ** leaks */ 12012 memset(&data, 0, sizeof(data)); 12013#ifdef SQLITE_DEBUG 12014 if( sqlite3_memory_used()>mem_main_enter ){ 12015 utf8_printf(stderr, "Memory leaked: %u bytes\n", 12016 (unsigned int)(sqlite3_memory_used()-mem_main_enter)); 12017 } 12018#endif 12019#endif /* !SQLITE_SHELL_FIDDLE */ 12020 return rc; 12021} 12022 12023 12024#ifdef SQLITE_SHELL_FIDDLE 12025/* Only for emcc experimentation purposes. */ 12026int fiddle_experiment(int a,int b){ 12027 return a + b; 12028} 12029 12030/* 12031** Returns a pointer to the current DB handle. 12032*/ 12033sqlite3 * fiddle_db_handle(){ 12034 return globalDb; 12035} 12036 12037/* 12038** Returns a pointer to the given DB name's VFS. If zDbName is 0 then 12039** "main" is assumed. Returns 0 if no db with the given name is 12040** open. 12041*/ 12042sqlite3_vfs * fiddle_db_vfs(const char *zDbName){ 12043 sqlite3_vfs * pVfs = 0; 12044 if(globalDb){ 12045 sqlite3_file_control(globalDb, zDbName ? zDbName : "main", 12046 SQLITE_FCNTL_VFS_POINTER, &pVfs); 12047 } 12048 return pVfs; 12049} 12050 12051/* Only for emcc experimentation purposes. */ 12052sqlite3 * fiddle_db_arg(sqlite3 *arg){ 12053 printf("fiddle_db_arg(%p)\n", (const void*)arg); 12054 return arg; 12055} 12056 12057/* 12058** Intended to be called via a SharedWorker() while a separate 12059** SharedWorker() (which manages the wasm module) is performing work 12060** which should be interrupted. Unfortunately, SharedWorker is not 12061** portable enough to make real use of. 12062*/ 12063void fiddle_interrupt(void){ 12064 if( globalDb ) sqlite3_interrupt(globalDb); 12065} 12066 12067/* 12068** Returns the filename of the given db name, assuming "main" if 12069** zDbName is NULL. Returns NULL if globalDb is not opened. 12070*/ 12071const char * fiddle_db_filename(const char * zDbName){ 12072 return globalDb 12073 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") 12074 : NULL; 12075} 12076 12077/* 12078** Completely wipes out the contents of the currently-opened database 12079** but leaves its storage intact for reuse. 12080*/ 12081void fiddle_reset_db(void){ 12082 if( globalDb ){ 12083 int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); 12084 if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0); 12085 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); 12086 } 12087} 12088 12089/* 12090** Uses the current database's VFS xRead to stream the db file's 12091** contents out to the given callback. The callback gets a single 12092** chunk of size n (its 2nd argument) on each call and must return 0 12093** on success, non-0 on error. This function returns 0 on success, 12094** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 12095** code from the callback. Note that this is not thread-friendly: it 12096** expects that it will be the only thread reading the db file and 12097** takes no measures to ensure that is the case. 12098*/ 12099int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){ 12100 sqlite3_int64 nSize = 0; 12101 sqlite3_int64 nPos = 0; 12102 sqlite3_file * pFile = 0; 12103 unsigned char buf[1024 * 8]; 12104 int nBuf = (int)sizeof(buf); 12105 int rc = shellState.db 12106 ? sqlite3_file_control(shellState.db, "main", 12107 SQLITE_FCNTL_FILE_POINTER, &pFile) 12108 : SQLITE_NOTFOUND; 12109 if( rc ) return rc; 12110 rc = pFile->pMethods->xFileSize(pFile, &nSize); 12111 if( rc ) return rc; 12112 if(nSize % nBuf){ 12113 /* DB size is not an even multiple of the buffer size. Reduce 12114 ** buffer size so that we do not unduly inflate the db size when 12115 ** exporting. */ 12116 if(0 == nSize % 4096) nBuf = 4096; 12117 else if(0 == nSize % 2048) nBuf = 2048; 12118 else if(0 == nSize % 1024) nBuf = 1024; 12119 else nBuf = 512; 12120 } 12121 for( ; 0==rc && nPos<nSize; nPos += nBuf ){ 12122 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); 12123 if(SQLITE_IOERR_SHORT_READ == rc){ 12124 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; 12125 } 12126 if( 0==rc ) rc = xCallback(buf, nBuf); 12127 } 12128 return rc; 12129} 12130 12131/* 12132** Trivial exportable function for emscripten. It processes zSql as if 12133** it were input to the sqlite3 shell and redirects all output to the 12134** wasm binding. If fiddle_main() has not been called by the time this 12135** is called, this function calls it with a conservative set of 12136** flags. 12137*/ 12138void fiddle_exec(const char * zSql){ 12139 if(zSql && *zSql){ 12140 if('.'==*zSql) puts(zSql); 12141 shellState.wasm.zInput = zSql; 12142 shellState.wasm.zPos = zSql; 12143 process_input(&shellState); 12144 shellState.wasm.zInput = shellState.wasm.zPos = 0; 12145 } 12146} 12147#endif /* SQLITE_SHELL_FIDDLE */ 12148