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** Determine if we are dealing with WinRT, which provides only a subset of 22** the full Win32 API. 23*/ 24#if !defined(SQLITE_OS_WINRT) 25# define SQLITE_OS_WINRT 0 26#endif 27 28/* 29** Warning pragmas copied from msvc.h in the core. 30*/ 31#if defined(_MSC_VER) 32#pragma warning(disable : 4054) 33#pragma warning(disable : 4055) 34#pragma warning(disable : 4100) 35#pragma warning(disable : 4127) 36#pragma warning(disable : 4130) 37#pragma warning(disable : 4152) 38#pragma warning(disable : 4189) 39#pragma warning(disable : 4206) 40#pragma warning(disable : 4210) 41#pragma warning(disable : 4232) 42#pragma warning(disable : 4244) 43#pragma warning(disable : 4305) 44#pragma warning(disable : 4306) 45#pragma warning(disable : 4702) 46#pragma warning(disable : 4706) 47#endif /* defined(_MSC_VER) */ 48 49/* 50** No support for loadable extensions in VxWorks. 51*/ 52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION 53# define SQLITE_OMIT_LOAD_EXTENSION 1 54#endif 55 56/* 57** Enable large-file support for fopen() and friends on unix. 58*/ 59#ifndef SQLITE_DISABLE_LFS 60# define _LARGE_FILE 1 61# ifndef _FILE_OFFSET_BITS 62# define _FILE_OFFSET_BITS 64 63# endif 64# define _LARGEFILE_SOURCE 1 65#endif 66 67#include <stdlib.h> 68#include <string.h> 69#include <stdio.h> 70#include <assert.h> 71#include "sqlite3.h" 72typedef sqlite3_int64 i64; 73typedef sqlite3_uint64 u64; 74typedef unsigned char u8; 75#if SQLITE_USER_AUTHENTICATION 76# include "sqlite3userauth.h" 77#endif 78#include <ctype.h> 79#include <stdarg.h> 80 81#if !defined(_WIN32) && !defined(WIN32) 82# include <signal.h> 83# if !defined(__RTP__) && !defined(_WRS_KERNEL) 84# include <pwd.h> 85# endif 86#endif 87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) 88# include <unistd.h> 89# include <dirent.h> 90# define GETPID getpid 91# if defined(__MINGW32__) 92# define DIRENT dirent 93# ifndef S_ISLNK 94# define S_ISLNK(mode) (0) 95# endif 96# endif 97#else 98# define GETPID (int)GetCurrentProcessId 99#endif 100#include <sys/types.h> 101#include <sys/stat.h> 102 103#if HAVE_READLINE 104# include <readline/readline.h> 105# include <readline/history.h> 106#endif 107 108#if HAVE_EDITLINE 109# include <editline/readline.h> 110#endif 111 112#if HAVE_EDITLINE || HAVE_READLINE 113 114# define shell_add_history(X) add_history(X) 115# define shell_read_history(X) read_history(X) 116# define shell_write_history(X) write_history(X) 117# define shell_stifle_history(X) stifle_history(X) 118# define shell_readline(X) readline(X) 119 120#elif HAVE_LINENOISE 121 122# include "linenoise.h" 123# define shell_add_history(X) linenoiseHistoryAdd(X) 124# define shell_read_history(X) linenoiseHistoryLoad(X) 125# define shell_write_history(X) linenoiseHistorySave(X) 126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) 127# define shell_readline(X) linenoise(X) 128 129#else 130 131# define shell_read_history(X) 132# define shell_write_history(X) 133# define shell_stifle_history(X) 134 135# define SHELL_USE_LOCAL_GETLINE 1 136#endif 137 138 139#if defined(_WIN32) || defined(WIN32) 140# if SQLITE_OS_WINRT 141# define SQLITE_OMIT_POPEN 1 142# else 143# include <io.h> 144# include <fcntl.h> 145# define isatty(h) _isatty(h) 146# ifndef access 147# define access(f,m) _access((f),(m)) 148# endif 149# ifndef unlink 150# define unlink _unlink 151# endif 152# ifndef strdup 153# define strdup _strdup 154# endif 155# undef popen 156# define popen _popen 157# undef pclose 158# define pclose _pclose 159# endif 160#else 161 /* Make sure isatty() has a prototype. */ 162 extern int isatty(int); 163 164# if !defined(__RTP__) && !defined(_WRS_KERNEL) 165 /* popen and pclose are not C89 functions and so are 166 ** sometimes omitted from the <stdio.h> header */ 167 extern FILE *popen(const char*,const char*); 168 extern int pclose(FILE*); 169# else 170# define SQLITE_OMIT_POPEN 1 171# endif 172#endif 173 174#if defined(_WIN32_WCE) 175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() 176 * thus we always assume that we have a console. That can be 177 * overridden with the -batch command line option. 178 */ 179#define isatty(x) 1 180#endif 181 182/* ctype macros that work with signed characters */ 183#define IsSpace(X) isspace((unsigned char)X) 184#define IsDigit(X) isdigit((unsigned char)X) 185#define ToLower(X) (char)tolower((unsigned char)X) 186 187#if defined(_WIN32) || defined(WIN32) 188#if SQLITE_OS_WINRT 189#include <intrin.h> 190#endif 191#include <windows.h> 192 193/* string conversion routines only needed on Win32 */ 194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); 195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); 196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); 197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); 198#endif 199 200/* On Windows, we normally run with output mode of TEXT so that \n characters 201** are automatically translated into \r\n. However, this behavior needs 202** to be disabled in some cases (ex: when generating CSV output and when 203** rendering quoted strings that contain \n characters). The following 204** routines take care of that. 205*/ 206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT 207static void setBinaryMode(FILE *file, int isOutput){ 208 if( isOutput ) fflush(file); 209 _setmode(_fileno(file), _O_BINARY); 210} 211static void setTextMode(FILE *file, int isOutput){ 212 if( isOutput ) fflush(file); 213 _setmode(_fileno(file), _O_TEXT); 214} 215#else 216# define setBinaryMode(X,Y) 217# define setTextMode(X,Y) 218#endif 219 220 221/* True if the timer is enabled */ 222static int enableTimer = 0; 223 224/* Return the current wall-clock time */ 225static sqlite3_int64 timeOfDay(void){ 226 static sqlite3_vfs *clockVfs = 0; 227 sqlite3_int64 t; 228 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 229 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 230 clockVfs->xCurrentTimeInt64(clockVfs, &t); 231 }else{ 232 double r; 233 clockVfs->xCurrentTime(clockVfs, &r); 234 t = (sqlite3_int64)(r*86400000.0); 235 } 236 return t; 237} 238 239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) 240#include <sys/time.h> 241#include <sys/resource.h> 242 243/* VxWorks does not support getrusage() as far as we can determine */ 244#if defined(_WRS_KERNEL) || defined(__RTP__) 245struct rusage { 246 struct timeval ru_utime; /* user CPU time used */ 247 struct timeval ru_stime; /* system CPU time used */ 248}; 249#define getrusage(A,B) memset(B,0,sizeof(*B)) 250#endif 251 252/* Saved resource information for the beginning of an operation */ 253static struct rusage sBegin; /* CPU time at start */ 254static sqlite3_int64 iBegin; /* Wall-clock time at start */ 255 256/* 257** Begin timing an operation 258*/ 259static void beginTimer(void){ 260 if( enableTimer ){ 261 getrusage(RUSAGE_SELF, &sBegin); 262 iBegin = timeOfDay(); 263 } 264} 265 266/* Return the difference of two time_structs in seconds */ 267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ 268 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 269 (double)(pEnd->tv_sec - pStart->tv_sec); 270} 271 272/* 273** Print the timing results. 274*/ 275static void endTimer(void){ 276 if( enableTimer ){ 277 sqlite3_int64 iEnd = timeOfDay(); 278 struct rusage sEnd; 279 getrusage(RUSAGE_SELF, &sEnd); 280 printf("Run Time: real %.3f user %f sys %f\n", 281 (iEnd - iBegin)*0.001, 282 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), 283 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); 284 } 285} 286 287#define BEGIN_TIMER beginTimer() 288#define END_TIMER endTimer() 289#define HAS_TIMER 1 290 291#elif (defined(_WIN32) || defined(WIN32)) 292 293/* Saved resource information for the beginning of an operation */ 294static HANDLE hProcess; 295static FILETIME ftKernelBegin; 296static FILETIME ftUserBegin; 297static sqlite3_int64 ftWallBegin; 298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, 299 LPFILETIME, LPFILETIME); 300static GETPROCTIMES getProcessTimesAddr = NULL; 301 302/* 303** Check to see if we have timer support. Return 1 if necessary 304** support found (or found previously). 305*/ 306static int hasTimer(void){ 307 if( getProcessTimesAddr ){ 308 return 1; 309 } else { 310#if !SQLITE_OS_WINRT 311 /* GetProcessTimes() isn't supported in WIN95 and some other Windows 312 ** versions. See if the version we are running on has it, and if it 313 ** does, save off a pointer to it and the current process handle. 314 */ 315 hProcess = GetCurrentProcess(); 316 if( hProcess ){ 317 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); 318 if( NULL != hinstLib ){ 319 getProcessTimesAddr = 320 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); 321 if( NULL != getProcessTimesAddr ){ 322 return 1; 323 } 324 FreeLibrary(hinstLib); 325 } 326 } 327#endif 328 } 329 return 0; 330} 331 332/* 333** Begin timing an operation 334*/ 335static void beginTimer(void){ 336 if( enableTimer && getProcessTimesAddr ){ 337 FILETIME ftCreation, ftExit; 338 getProcessTimesAddr(hProcess,&ftCreation,&ftExit, 339 &ftKernelBegin,&ftUserBegin); 340 ftWallBegin = timeOfDay(); 341 } 342} 343 344/* Return the difference of two FILETIME structs in seconds */ 345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ 346 sqlite_int64 i64Start = *((sqlite_int64 *) pStart); 347 sqlite_int64 i64End = *((sqlite_int64 *) pEnd); 348 return (double) ((i64End - i64Start) / 10000000.0); 349} 350 351/* 352** Print the timing results. 353*/ 354static void endTimer(void){ 355 if( enableTimer && getProcessTimesAddr){ 356 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; 357 sqlite3_int64 ftWallEnd = timeOfDay(); 358 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); 359 printf("Run Time: real %.3f user %f sys %f\n", 360 (ftWallEnd - ftWallBegin)*0.001, 361 timeDiff(&ftUserBegin, &ftUserEnd), 362 timeDiff(&ftKernelBegin, &ftKernelEnd)); 363 } 364} 365 366#define BEGIN_TIMER beginTimer() 367#define END_TIMER endTimer() 368#define HAS_TIMER hasTimer() 369 370#else 371#define BEGIN_TIMER 372#define END_TIMER 373#define HAS_TIMER 0 374#endif 375 376/* 377** Used to prevent warnings about unused parameters 378*/ 379#define UNUSED_PARAMETER(x) (void)(x) 380 381/* 382** Number of elements in an array 383*/ 384#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) 385 386/* 387** If the following flag is set, then command execution stops 388** at an error if we are not interactive. 389*/ 390static int bail_on_error = 0; 391 392/* 393** Threat stdin as an interactive input if the following variable 394** is true. Otherwise, assume stdin is connected to a file or pipe. 395*/ 396static int stdin_is_interactive = 1; 397 398/* 399** On Windows systems we have to know if standard output is a console 400** in order to translate UTF-8 into MBCS. The following variable is 401** true if translation is required. 402*/ 403static int stdout_is_console = 1; 404 405/* 406** The following is the open SQLite database. We make a pointer 407** to this database a static variable so that it can be accessed 408** by the SIGINT handler to interrupt database processing. 409*/ 410static sqlite3 *globalDb = 0; 411 412/* 413** True if an interrupt (Control-C) has been received. 414*/ 415static volatile int seenInterrupt = 0; 416 417#ifdef SQLITE_DEBUG 418/* 419** Out-of-memory simulator variables 420*/ 421static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ 422static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ 423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ 424#endif /* SQLITE_DEBUG */ 425 426/* 427** This is the name of our program. It is set in main(), used 428** in a number of other places, mostly for error messages. 429*/ 430static char *Argv0; 431 432/* 433** Prompt strings. Initialized in main. Settable with 434** .prompt main continue 435*/ 436static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ 437static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ 438 439/* 440** Render output like fprintf(). Except, if the output is going to the 441** console and if this is running on a Windows machine, translate the 442** output from UTF-8 into MBCS. 443*/ 444#if defined(_WIN32) || defined(WIN32) 445void utf8_printf(FILE *out, const char *zFormat, ...){ 446 va_list ap; 447 va_start(ap, zFormat); 448 if( stdout_is_console && (out==stdout || out==stderr) ){ 449 char *z1 = sqlite3_vmprintf(zFormat, ap); 450 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); 451 sqlite3_free(z1); 452 fputs(z2, out); 453 sqlite3_free(z2); 454 }else{ 455 vfprintf(out, zFormat, ap); 456 } 457 va_end(ap); 458} 459#elif !defined(utf8_printf) 460# define utf8_printf fprintf 461#endif 462 463/* 464** Render output like fprintf(). This should not be used on anything that 465** includes string formatting (e.g. "%s"). 466*/ 467#if !defined(raw_printf) 468# define raw_printf fprintf 469#endif 470 471/* Indicate out-of-memory and exit. */ 472static void shell_out_of_memory(void){ 473 raw_printf(stderr,"Error: out of memory\n"); 474 exit(1); 475} 476 477#ifdef SQLITE_DEBUG 478/* This routine is called when a simulated OOM occurs. It is broken 479** out as a separate routine to make it easy to set a breakpoint on 480** the OOM 481*/ 482void shellOomFault(void){ 483 if( oomRepeat>0 ){ 484 oomRepeat--; 485 }else{ 486 oomCounter--; 487 } 488} 489#endif /* SQLITE_DEBUG */ 490 491#ifdef SQLITE_DEBUG 492/* This routine is a replacement malloc() that is used to simulate 493** Out-Of-Memory (OOM) errors for testing purposes. 494*/ 495static void *oomMalloc(int nByte){ 496 if( oomCounter ){ 497 if( oomCounter==1 ){ 498 shellOomFault(); 499 return 0; 500 }else{ 501 oomCounter--; 502 } 503 } 504 return defaultMalloc(nByte); 505} 506#endif /* SQLITE_DEBUG */ 507 508#ifdef SQLITE_DEBUG 509/* Register the OOM simulator. This must occur before any memory 510** allocations */ 511static void registerOomSimulator(void){ 512 sqlite3_mem_methods mem; 513 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); 514 defaultMalloc = mem.xMalloc; 515 mem.xMalloc = oomMalloc; 516 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); 517} 518#endif 519 520/* 521** Write I/O traces to the following stream. 522*/ 523#ifdef SQLITE_ENABLE_IOTRACE 524static FILE *iotrace = 0; 525#endif 526 527/* 528** This routine works like printf in that its first argument is a 529** format string and subsequent arguments are values to be substituted 530** in place of % fields. The result of formatting this string 531** is written to iotrace. 532*/ 533#ifdef SQLITE_ENABLE_IOTRACE 534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ 535 va_list ap; 536 char *z; 537 if( iotrace==0 ) return; 538 va_start(ap, zFormat); 539 z = sqlite3_vmprintf(zFormat, ap); 540 va_end(ap); 541 utf8_printf(iotrace, "%s", z); 542 sqlite3_free(z); 543} 544#endif 545 546/* 547** Output string zUtf to stream pOut as w characters. If w is negative, 548** then right-justify the text. W is the width in UTF-8 characters, not 549** in bytes. This is different from the %*.*s specification in printf 550** since with %*.*s the width is measured in bytes, not characters. 551*/ 552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ 553 int i; 554 int n; 555 int aw = w<0 ? -w : w; 556 char zBuf[1000]; 557 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; 558 for(i=n=0; zUtf[i]; i++){ 559 if( (zUtf[i]&0xc0)!=0x80 ){ 560 n++; 561 if( n==aw ){ 562 do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); 563 break; 564 } 565 } 566 } 567 if( n>=aw ){ 568 utf8_printf(pOut, "%.*s", i, zUtf); 569 }else if( w<0 ){ 570 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); 571 }else{ 572 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); 573 } 574} 575 576 577/* 578** Determines if a string is a number of not. 579*/ 580static int isNumber(const char *z, int *realnum){ 581 if( *z=='-' || *z=='+' ) z++; 582 if( !IsDigit(*z) ){ 583 return 0; 584 } 585 z++; 586 if( realnum ) *realnum = 0; 587 while( IsDigit(*z) ){ z++; } 588 if( *z=='.' ){ 589 z++; 590 if( !IsDigit(*z) ) return 0; 591 while( IsDigit(*z) ){ z++; } 592 if( realnum ) *realnum = 1; 593 } 594 if( *z=='e' || *z=='E' ){ 595 z++; 596 if( *z=='+' || *z=='-' ) z++; 597 if( !IsDigit(*z) ) return 0; 598 while( IsDigit(*z) ){ z++; } 599 if( realnum ) *realnum = 1; 600 } 601 return *z==0; 602} 603 604/* 605** Compute a string length that is limited to what can be stored in 606** lower 30 bits of a 32-bit signed integer. 607*/ 608static int strlen30(const char *z){ 609 const char *z2 = z; 610 while( *z2 ){ z2++; } 611 return 0x3fffffff & (int)(z2 - z); 612} 613 614/* 615** Return the length of a string in characters. Multibyte UTF8 characters 616** count as a single character. 617*/ 618static int strlenChar(const char *z){ 619 int n = 0; 620 while( *z ){ 621 if( (0xc0&*(z++))!=0x80 ) n++; 622 } 623 return n; 624} 625 626/* 627** This routine reads a line of text from FILE in, stores 628** the text in memory obtained from malloc() and returns a pointer 629** to the text. NULL is returned at end of file, or if malloc() 630** fails. 631** 632** If zLine is not NULL then it is a malloced buffer returned from 633** a previous call to this routine that may be reused. 634*/ 635static char *local_getline(char *zLine, FILE *in){ 636 int nLine = zLine==0 ? 0 : 100; 637 int n = 0; 638 639 while( 1 ){ 640 if( n+100>nLine ){ 641 nLine = nLine*2 + 100; 642 zLine = realloc(zLine, nLine); 643 if( zLine==0 ) shell_out_of_memory(); 644 } 645 if( fgets(&zLine[n], nLine - n, in)==0 ){ 646 if( n==0 ){ 647 free(zLine); 648 return 0; 649 } 650 zLine[n] = 0; 651 break; 652 } 653 while( zLine[n] ) n++; 654 if( n>0 && zLine[n-1]=='\n' ){ 655 n--; 656 if( n>0 && zLine[n-1]=='\r' ) n--; 657 zLine[n] = 0; 658 break; 659 } 660 } 661#if defined(_WIN32) || defined(WIN32) 662 /* For interactive input on Windows systems, translate the 663 ** multi-byte characterset characters into UTF-8. */ 664 if( stdin_is_interactive && in==stdin ){ 665 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); 666 if( zTrans ){ 667 int nTrans = strlen30(zTrans)+1; 668 if( nTrans>nLine ){ 669 zLine = realloc(zLine, nTrans); 670 if( zLine==0 ) shell_out_of_memory(); 671 } 672 memcpy(zLine, zTrans, nTrans); 673 sqlite3_free(zTrans); 674 } 675 } 676#endif /* defined(_WIN32) || defined(WIN32) */ 677 return zLine; 678} 679 680/* 681** Retrieve a single line of input text. 682** 683** If in==0 then read from standard input and prompt before each line. 684** If isContinuation is true, then a continuation prompt is appropriate. 685** If isContinuation is zero, then the main prompt should be used. 686** 687** If zPrior is not NULL then it is a buffer from a prior call to this 688** routine that can be reused. 689** 690** The result is stored in space obtained from malloc() and must either 691** be freed by the caller or else passed back into this routine via the 692** zPrior argument for reuse. 693*/ 694static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 695 char *zPrompt; 696 char *zResult; 697 if( in!=0 ){ 698 zResult = local_getline(zPrior, in); 699 }else{ 700 zPrompt = isContinuation ? continuePrompt : mainPrompt; 701#if SHELL_USE_LOCAL_GETLINE 702 printf("%s", zPrompt); 703 fflush(stdout); 704 zResult = local_getline(zPrior, stdin); 705#else 706 free(zPrior); 707 zResult = shell_readline(zPrompt); 708 if( zResult && *zResult ) shell_add_history(zResult); 709#endif 710 } 711 return zResult; 712} 713 714 715/* 716** Return the value of a hexadecimal digit. Return -1 if the input 717** is not a hex digit. 718*/ 719static int hexDigitValue(char c){ 720 if( c>='0' && c<='9' ) return c - '0'; 721 if( c>='a' && c<='f' ) return c - 'a' + 10; 722 if( c>='A' && c<='F' ) return c - 'A' + 10; 723 return -1; 724} 725 726/* 727** Interpret zArg as an integer value, possibly with suffixes. 728*/ 729static sqlite3_int64 integerValue(const char *zArg){ 730 sqlite3_int64 v = 0; 731 static const struct { char *zSuffix; int iMult; } aMult[] = { 732 { "KiB", 1024 }, 733 { "MiB", 1024*1024 }, 734 { "GiB", 1024*1024*1024 }, 735 { "KB", 1000 }, 736 { "MB", 1000000 }, 737 { "GB", 1000000000 }, 738 { "K", 1000 }, 739 { "M", 1000000 }, 740 { "G", 1000000000 }, 741 }; 742 int i; 743 int isNeg = 0; 744 if( zArg[0]=='-' ){ 745 isNeg = 1; 746 zArg++; 747 }else if( zArg[0]=='+' ){ 748 zArg++; 749 } 750 if( zArg[0]=='0' && zArg[1]=='x' ){ 751 int x; 752 zArg += 2; 753 while( (x = hexDigitValue(zArg[0]))>=0 ){ 754 v = (v<<4) + x; 755 zArg++; 756 } 757 }else{ 758 while( IsDigit(zArg[0]) ){ 759 v = v*10 + zArg[0] - '0'; 760 zArg++; 761 } 762 } 763 for(i=0; i<ArraySize(aMult); i++){ 764 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 765 v *= aMult[i].iMult; 766 break; 767 } 768 } 769 return isNeg? -v : v; 770} 771 772/* 773** A variable length string to which one can append text. 774*/ 775typedef struct ShellText ShellText; 776struct ShellText { 777 char *z; 778 int n; 779 int nAlloc; 780}; 781 782/* 783** Initialize and destroy a ShellText object 784*/ 785static void initText(ShellText *p){ 786 memset(p, 0, sizeof(*p)); 787} 788static void freeText(ShellText *p){ 789 free(p->z); 790 initText(p); 791} 792 793/* zIn is either a pointer to a NULL-terminated string in memory obtained 794** from malloc(), or a NULL pointer. The string pointed to by zAppend is 795** added to zIn, and the result returned in memory obtained from malloc(). 796** zIn, if it was not NULL, is freed. 797** 798** If the third argument, quote, is not '\0', then it is used as a 799** quote character for zAppend. 800*/ 801static void appendText(ShellText *p, char const *zAppend, char quote){ 802 int len; 803 int i; 804 int nAppend = strlen30(zAppend); 805 806 len = nAppend+p->n+1; 807 if( quote ){ 808 len += 2; 809 for(i=0; i<nAppend; i++){ 810 if( zAppend[i]==quote ) len++; 811 } 812 } 813 814 if( p->n+len>=p->nAlloc ){ 815 p->nAlloc = p->nAlloc*2 + len + 20; 816 p->z = realloc(p->z, p->nAlloc); 817 if( p->z==0 ) shell_out_of_memory(); 818 } 819 820 if( quote ){ 821 char *zCsr = p->z+p->n; 822 *zCsr++ = quote; 823 for(i=0; i<nAppend; i++){ 824 *zCsr++ = zAppend[i]; 825 if( zAppend[i]==quote ) *zCsr++ = quote; 826 } 827 *zCsr++ = quote; 828 p->n = (int)(zCsr - p->z); 829 *zCsr = '\0'; 830 }else{ 831 memcpy(p->z+p->n, zAppend, nAppend); 832 p->n += nAppend; 833 p->z[p->n] = '\0'; 834 } 835} 836 837/* 838** Attempt to determine if identifier zName needs to be quoted, either 839** because it contains non-alphanumeric characters, or because it is an 840** SQLite keyword. Be conservative in this estimate: When in doubt assume 841** that quoting is required. 842** 843** Return '"' if quoting is required. Return 0 if no quoting is required. 844*/ 845static char quoteChar(const char *zName){ 846 int i; 847 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; 848 for(i=0; zName[i]; i++){ 849 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; 850 } 851 return sqlite3_keyword_check(zName, i) ? '"' : 0; 852} 853 854/* 855** Construct a fake object name and column list to describe the structure 856** of the view, virtual table, or table valued function zSchema.zName. 857*/ 858static char *shellFakeSchema( 859 sqlite3 *db, /* The database connection containing the vtab */ 860 const char *zSchema, /* Schema of the database holding the vtab */ 861 const char *zName /* The name of the virtual table */ 862){ 863 sqlite3_stmt *pStmt = 0; 864 char *zSql; 865 ShellText s; 866 char cQuote; 867 char *zDiv = "("; 868 int nRow = 0; 869 870 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", 871 zSchema ? zSchema : "main", zName); 872 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 873 sqlite3_free(zSql); 874 initText(&s); 875 if( zSchema ){ 876 cQuote = quoteChar(zSchema); 877 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; 878 appendText(&s, zSchema, cQuote); 879 appendText(&s, ".", 0); 880 } 881 cQuote = quoteChar(zName); 882 appendText(&s, zName, cQuote); 883 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 884 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); 885 nRow++; 886 appendText(&s, zDiv, 0); 887 zDiv = ","; 888 cQuote = quoteChar(zCol); 889 appendText(&s, zCol, cQuote); 890 } 891 appendText(&s, ")", 0); 892 sqlite3_finalize(pStmt); 893 if( nRow==0 ){ 894 freeText(&s); 895 s.z = 0; 896 } 897 return s.z; 898} 899 900/* 901** SQL function: shell_module_schema(X) 902** 903** Return a fake schema for the table-valued function or eponymous virtual 904** table X. 905*/ 906static void shellModuleSchema( 907 sqlite3_context *pCtx, 908 int nVal, 909 sqlite3_value **apVal 910){ 911 const char *zName = (const char*)sqlite3_value_text(apVal[0]); 912 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName); 913 UNUSED_PARAMETER(nVal); 914 if( zFake ){ 915 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), 916 -1, sqlite3_free); 917 free(zFake); 918 } 919} 920 921/* 922** SQL function: shell_add_schema(S,X) 923** 924** Add the schema name X to the CREATE statement in S and return the result. 925** Examples: 926** 927** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); 928** 929** Also works on 930** 931** CREATE INDEX 932** CREATE UNIQUE INDEX 933** CREATE VIEW 934** CREATE TRIGGER 935** CREATE VIRTUAL TABLE 936** 937** This UDF is used by the .schema command to insert the schema name of 938** attached databases into the middle of the sqlite_master.sql field. 939*/ 940static void shellAddSchemaName( 941 sqlite3_context *pCtx, 942 int nVal, 943 sqlite3_value **apVal 944){ 945 static const char *aPrefix[] = { 946 "TABLE", 947 "INDEX", 948 "UNIQUE INDEX", 949 "VIEW", 950 "TRIGGER", 951 "VIRTUAL TABLE" 952 }; 953 int i = 0; 954 const char *zIn = (const char*)sqlite3_value_text(apVal[0]); 955 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); 956 const char *zName = (const char*)sqlite3_value_text(apVal[2]); 957 sqlite3 *db = sqlite3_context_db_handle(pCtx); 958 UNUSED_PARAMETER(nVal); 959 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ 960 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ 961 int n = strlen30(aPrefix[i]); 962 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ 963 char *z = 0; 964 char *zFake = 0; 965 if( zSchema ){ 966 char cQuote = quoteChar(zSchema); 967 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ 968 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); 969 }else{ 970 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); 971 } 972 } 973 if( zName 974 && aPrefix[i][0]=='V' 975 && (zFake = shellFakeSchema(db, zSchema, zName))!=0 976 ){ 977 if( z==0 ){ 978 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); 979 }else{ 980 z = sqlite3_mprintf("%z\n/* %s */", z, zFake); 981 } 982 free(zFake); 983 } 984 if( z ){ 985 sqlite3_result_text(pCtx, z, -1, sqlite3_free); 986 return; 987 } 988 } 989 } 990 } 991 sqlite3_result_value(pCtx, apVal[0]); 992} 993 994/* 995** The source code for several run-time loadable extensions is inserted 996** below by the ../tool/mkshellc.tcl script. Before processing that included 997** code, we need to override some macros to make the included program code 998** work here in the middle of this regular program. 999*/ 1000#define SQLITE_EXTENSION_INIT1 1001#define SQLITE_EXTENSION_INIT2(X) (void)(X) 1002 1003#if defined(_WIN32) && defined(_MSC_VER) 1004INCLUDE test_windirent.h 1005INCLUDE test_windirent.c 1006#define dirent DIRENT 1007#endif 1008INCLUDE ../ext/misc/shathree.c 1009INCLUDE ../ext/misc/fileio.c 1010INCLUDE ../ext/misc/completion.c 1011INCLUDE ../ext/misc/appendvfs.c 1012INCLUDE ../ext/misc/memtrace.c 1013INCLUDE ../ext/misc/uint.c 1014#ifdef SQLITE_HAVE_ZLIB 1015INCLUDE ../ext/misc/zipfile.c 1016INCLUDE ../ext/misc/sqlar.c 1017#endif 1018INCLUDE ../ext/expert/sqlite3expert.h 1019INCLUDE ../ext/expert/sqlite3expert.c 1020 1021#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 1022INCLUDE ../ext/misc/dbdata.c 1023#endif 1024 1025#if defined(SQLITE_ENABLE_SESSION) 1026/* 1027** State information for a single open session 1028*/ 1029typedef struct OpenSession OpenSession; 1030struct OpenSession { 1031 char *zName; /* Symbolic name for this session */ 1032 int nFilter; /* Number of xFilter rejection GLOB patterns */ 1033 char **azFilter; /* Array of xFilter rejection GLOB patterns */ 1034 sqlite3_session *p; /* The open session */ 1035}; 1036#endif 1037 1038typedef struct ExpertInfo ExpertInfo; 1039struct ExpertInfo { 1040 sqlite3expert *pExpert; 1041 int bVerbose; 1042}; 1043 1044/* A single line in the EQP output */ 1045typedef struct EQPGraphRow EQPGraphRow; 1046struct EQPGraphRow { 1047 int iEqpId; /* ID for this row */ 1048 int iParentId; /* ID of the parent row */ 1049 EQPGraphRow *pNext; /* Next row in sequence */ 1050 char zText[1]; /* Text to display for this row */ 1051}; 1052 1053/* All EQP output is collected into an instance of the following */ 1054typedef struct EQPGraph EQPGraph; 1055struct EQPGraph { 1056 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ 1057 EQPGraphRow *pLast; /* Last element of the pRow list */ 1058 char zPrefix[100]; /* Graph prefix */ 1059}; 1060 1061/* 1062** State information about the database connection is contained in an 1063** instance of the following structure. 1064*/ 1065typedef struct ShellState ShellState; 1066struct ShellState { 1067 sqlite3 *db; /* The database */ 1068 u8 autoExplain; /* Automatically turn on .explain mode */ 1069 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ 1070 u8 autoEQPtest; /* autoEQP is in test mode */ 1071 u8 autoEQPtrace; /* autoEQP is in trace mode */ 1072 u8 statsOn; /* True to display memory stats before each finalize */ 1073 u8 scanstatsOn; /* True to display scan stats before each finalize */ 1074 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ 1075 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ 1076 u8 nEqpLevel; /* Depth of the EQP output graph */ 1077 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ 1078 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ 1079 int outCount; /* Revert to stdout when reaching zero */ 1080 int cnt; /* Number of records displayed so far */ 1081 int lineno; /* Line number of last line read from in */ 1082 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ 1083 FILE *in; /* Read commands from this stream */ 1084 FILE *out; /* Write results here */ 1085 FILE *traceOut; /* Output for sqlite3_trace() */ 1086 int nErr; /* Number of errors seen */ 1087 int mode; /* An output mode setting */ 1088 int modePrior; /* Saved mode */ 1089 int cMode; /* temporary output mode for the current query */ 1090 int normalMode; /* Output mode before ".explain on" */ 1091 int writableSchema; /* True if PRAGMA writable_schema=ON */ 1092 int showHeader; /* True to show column names in List or Column mode */ 1093 int nCheck; /* Number of ".check" commands run */ 1094 unsigned nProgress; /* Number of progress callbacks encountered */ 1095 unsigned mxProgress; /* Maximum progress callbacks before failing */ 1096 unsigned flgProgress; /* Flags for the progress callback */ 1097 unsigned shellFlgs; /* Various flags */ 1098 unsigned priorShFlgs; /* Saved copy of flags */ 1099 sqlite3_int64 szMax; /* --maxsize argument to .open */ 1100 char *zDestTable; /* Name of destination table when MODE_Insert */ 1101 char *zTempFile; /* Temporary file that might need deleting */ 1102 char zTestcase[30]; /* Name of current test case */ 1103 char colSeparator[20]; /* Column separator character for several modes */ 1104 char rowSeparator[20]; /* Row separator character for MODE_Ascii */ 1105 char colSepPrior[20]; /* Saved column separator */ 1106 char rowSepPrior[20]; /* Saved row separator */ 1107 int *colWidth; /* Requested width of each column in columnar modes */ 1108 int *actualWidth; /* Actual width of each column */ 1109 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ 1110 char nullValue[20]; /* The text to print when a NULL comes back from 1111 ** the database */ 1112 char outfile[FILENAME_MAX]; /* Filename for *out */ 1113 const char *zDbFilename; /* name of the database file */ 1114 char *zFreeOnClose; /* Filename to free when closing */ 1115 const char *zVfs; /* Name of VFS to use */ 1116 sqlite3_stmt *pStmt; /* Current statement if any. */ 1117 FILE *pLog; /* Write log output here */ 1118 int *aiIndent; /* Array of indents used in MODE_Explain */ 1119 int nIndent; /* Size of array aiIndent[] */ 1120 int iIndent; /* Index of current op in aiIndent[] */ 1121 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ 1122#if defined(SQLITE_ENABLE_SESSION) 1123 int nSession; /* Number of active sessions */ 1124 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ 1125#endif 1126 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ 1127}; 1128 1129 1130/* Allowed values for ShellState.autoEQP 1131*/ 1132#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ 1133#define AUTOEQP_on 1 /* Automatic EQP is on */ 1134#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ 1135#define AUTOEQP_full 3 /* Show full EXPLAIN */ 1136 1137/* Allowed values for ShellState.openMode 1138*/ 1139#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ 1140#define SHELL_OPEN_NORMAL 1 /* Normal database file */ 1141#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ 1142#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ 1143#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ 1144#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ 1145#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ 1146 1147/* Allowed values for ShellState.eTraceType 1148*/ 1149#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ 1150#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ 1151#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ 1152 1153/* Bits in the ShellState.flgProgress variable */ 1154#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ 1155#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres 1156 ** callback limit is reached, and for each 1157 ** top-level SQL statement */ 1158#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ 1159 1160/* 1161** These are the allowed shellFlgs values 1162*/ 1163#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ 1164#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ 1165#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ 1166#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ 1167#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ 1168#define SHFLG_CountChanges 0x00000020 /* .changes setting */ 1169#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ 1170#define SHFLG_HeaderSet 0x00000080 /* .header has been used */ 1171 1172/* 1173** Macros for testing and setting shellFlgs 1174*/ 1175#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) 1176#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) 1177#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) 1178 1179/* 1180** These are the allowed modes. 1181*/ 1182#define MODE_Line 0 /* One column per line. Blank line between records */ 1183#define MODE_Column 1 /* One record per line in neat columns */ 1184#define MODE_List 2 /* One record per line with a separator */ 1185#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ 1186#define MODE_Html 4 /* Generate an XHTML table */ 1187#define MODE_Insert 5 /* Generate SQL "insert" statements */ 1188#define MODE_Quote 6 /* Quote values as for SQL */ 1189#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ 1190#define MODE_Csv 8 /* Quote strings, numbers are plain */ 1191#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ 1192#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ 1193#define MODE_Pretty 11 /* Pretty-print schemas */ 1194#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ 1195#define MODE_Json 13 /* Output JSON */ 1196#define MODE_Markdown 14 /* Markdown formatting */ 1197#define MODE_Table 15 /* MySQL-style table formatting */ 1198#define MODE_Box 16 /* Unicode box-drawing characters */ 1199 1200static const char *modeDescr[] = { 1201 "line", 1202 "column", 1203 "list", 1204 "semi", 1205 "html", 1206 "insert", 1207 "quote", 1208 "tcl", 1209 "csv", 1210 "explain", 1211 "ascii", 1212 "prettyprint", 1213 "eqp", 1214 "json", 1215 "markdown", 1216 "table", 1217 "box" 1218}; 1219 1220/* 1221** These are the column/row/line separators used by the various 1222** import/export modes. 1223*/ 1224#define SEP_Column "|" 1225#define SEP_Row "\n" 1226#define SEP_Tab "\t" 1227#define SEP_Space " " 1228#define SEP_Comma "," 1229#define SEP_CrLf "\r\n" 1230#define SEP_Unit "\x1F" 1231#define SEP_Record "\x1E" 1232 1233/* 1234** A callback for the sqlite3_log() interface. 1235*/ 1236static void shellLog(void *pArg, int iErrCode, const char *zMsg){ 1237 ShellState *p = (ShellState*)pArg; 1238 if( p->pLog==0 ) return; 1239 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); 1240 fflush(p->pLog); 1241} 1242 1243/* 1244** SQL function: shell_putsnl(X) 1245** 1246** Write the text X to the screen (or whatever output is being directed) 1247** adding a newline at the end, and then return X. 1248*/ 1249static void shellPutsFunc( 1250 sqlite3_context *pCtx, 1251 int nVal, 1252 sqlite3_value **apVal 1253){ 1254 ShellState *p = (ShellState*)sqlite3_user_data(pCtx); 1255 (void)nVal; 1256 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); 1257 sqlite3_result_value(pCtx, apVal[0]); 1258} 1259 1260/* 1261** SQL function: edit(VALUE) 1262** edit(VALUE,EDITOR) 1263** 1264** These steps: 1265** 1266** (1) Write VALUE into a temporary file. 1267** (2) Run program EDITOR on that temporary file. 1268** (3) Read the temporary file back and return its content as the result. 1269** (4) Delete the temporary file 1270** 1271** If the EDITOR argument is omitted, use the value in the VISUAL 1272** environment variable. If still there is no EDITOR, through an error. 1273** 1274** Also throw an error if the EDITOR program returns a non-zero exit code. 1275*/ 1276#ifndef SQLITE_NOHAVE_SYSTEM 1277static void editFunc( 1278 sqlite3_context *context, 1279 int argc, 1280 sqlite3_value **argv 1281){ 1282 const char *zEditor; 1283 char *zTempFile = 0; 1284 sqlite3 *db; 1285 char *zCmd = 0; 1286 int bBin; 1287 int rc; 1288 int hasCRNL = 0; 1289 FILE *f = 0; 1290 sqlite3_int64 sz; 1291 sqlite3_int64 x; 1292 unsigned char *p = 0; 1293 1294 if( argc==2 ){ 1295 zEditor = (const char*)sqlite3_value_text(argv[1]); 1296 }else{ 1297 zEditor = getenv("VISUAL"); 1298 } 1299 if( zEditor==0 ){ 1300 sqlite3_result_error(context, "no editor for edit()", -1); 1301 return; 1302 } 1303 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1304 sqlite3_result_error(context, "NULL input to edit()", -1); 1305 return; 1306 } 1307 db = sqlite3_context_db_handle(context); 1308 zTempFile = 0; 1309 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); 1310 if( zTempFile==0 ){ 1311 sqlite3_uint64 r = 0; 1312 sqlite3_randomness(sizeof(r), &r); 1313 zTempFile = sqlite3_mprintf("temp%llx", r); 1314 if( zTempFile==0 ){ 1315 sqlite3_result_error_nomem(context); 1316 return; 1317 } 1318 } 1319 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; 1320 /* When writing the file to be edited, do \n to \r\n conversions on systems 1321 ** that want \r\n line endings */ 1322 f = fopen(zTempFile, bBin ? "wb" : "w"); 1323 if( f==0 ){ 1324 sqlite3_result_error(context, "edit() cannot open temp file", -1); 1325 goto edit_func_end; 1326 } 1327 sz = sqlite3_value_bytes(argv[0]); 1328 if( bBin ){ 1329 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); 1330 }else{ 1331 const char *z = (const char*)sqlite3_value_text(argv[0]); 1332 /* Remember whether or not the value originally contained \r\n */ 1333 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; 1334 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); 1335 } 1336 fclose(f); 1337 f = 0; 1338 if( x!=sz ){ 1339 sqlite3_result_error(context, "edit() could not write the whole file", -1); 1340 goto edit_func_end; 1341 } 1342 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); 1343 if( zCmd==0 ){ 1344 sqlite3_result_error_nomem(context); 1345 goto edit_func_end; 1346 } 1347 rc = system(zCmd); 1348 sqlite3_free(zCmd); 1349 if( rc ){ 1350 sqlite3_result_error(context, "EDITOR returned non-zero", -1); 1351 goto edit_func_end; 1352 } 1353 f = fopen(zTempFile, "rb"); 1354 if( f==0 ){ 1355 sqlite3_result_error(context, 1356 "edit() cannot reopen temp file after edit", -1); 1357 goto edit_func_end; 1358 } 1359 fseek(f, 0, SEEK_END); 1360 sz = ftell(f); 1361 rewind(f); 1362 p = sqlite3_malloc64( sz+1 ); 1363 if( p==0 ){ 1364 sqlite3_result_error_nomem(context); 1365 goto edit_func_end; 1366 } 1367 x = fread(p, 1, (size_t)sz, f); 1368 fclose(f); 1369 f = 0; 1370 if( x!=sz ){ 1371 sqlite3_result_error(context, "could not read back the whole file", -1); 1372 goto edit_func_end; 1373 } 1374 if( bBin ){ 1375 sqlite3_result_blob64(context, p, sz, sqlite3_free); 1376 }else{ 1377 sqlite3_int64 i, j; 1378 if( hasCRNL ){ 1379 /* If the original contains \r\n then do no conversions back to \n */ 1380 j = sz; 1381 }else{ 1382 /* If the file did not originally contain \r\n then convert any new 1383 ** \r\n back into \n */ 1384 for(i=j=0; i<sz; i++){ 1385 if( p[i]=='\r' && p[i+1]=='\n' ) i++; 1386 p[j++] = p[i]; 1387 } 1388 sz = j; 1389 p[sz] = 0; 1390 } 1391 sqlite3_result_text64(context, (const char*)p, sz, 1392 sqlite3_free, SQLITE_UTF8); 1393 } 1394 p = 0; 1395 1396edit_func_end: 1397 if( f ) fclose(f); 1398 unlink(zTempFile); 1399 sqlite3_free(zTempFile); 1400 sqlite3_free(p); 1401} 1402#endif /* SQLITE_NOHAVE_SYSTEM */ 1403 1404/* 1405** Save or restore the current output mode 1406*/ 1407static void outputModePush(ShellState *p){ 1408 p->modePrior = p->mode; 1409 p->priorShFlgs = p->shellFlgs; 1410 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); 1411 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); 1412} 1413static void outputModePop(ShellState *p){ 1414 p->mode = p->modePrior; 1415 p->shellFlgs = p->priorShFlgs; 1416 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); 1417 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); 1418} 1419 1420/* 1421** Output the given string as a hex-encoded blob (eg. X'1234' ) 1422*/ 1423static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ 1424 int i; 1425 char *zBlob = (char *)pBlob; 1426 raw_printf(out,"X'"); 1427 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } 1428 raw_printf(out,"'"); 1429} 1430 1431/* 1432** Find a string that is not found anywhere in z[]. Return a pointer 1433** to that string. 1434** 1435** Try to use zA and zB first. If both of those are already found in z[] 1436** then make up some string and store it in the buffer zBuf. 1437*/ 1438static const char *unused_string( 1439 const char *z, /* Result must not appear anywhere in z */ 1440 const char *zA, const char *zB, /* Try these first */ 1441 char *zBuf /* Space to store a generated string */ 1442){ 1443 unsigned i = 0; 1444 if( strstr(z, zA)==0 ) return zA; 1445 if( strstr(z, zB)==0 ) return zB; 1446 do{ 1447 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); 1448 }while( strstr(z,zBuf)!=0 ); 1449 return zBuf; 1450} 1451 1452/* 1453** Output the given string as a quoted string using SQL quoting conventions. 1454** 1455** See also: output_quoted_escaped_string() 1456*/ 1457static void output_quoted_string(FILE *out, const char *z){ 1458 int i; 1459 char c; 1460 setBinaryMode(out, 1); 1461 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1462 if( c==0 ){ 1463 utf8_printf(out,"'%s'",z); 1464 }else{ 1465 raw_printf(out, "'"); 1466 while( *z ){ 1467 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1468 if( c=='\'' ) i++; 1469 if( i ){ 1470 utf8_printf(out, "%.*s", i, z); 1471 z += i; 1472 } 1473 if( c=='\'' ){ 1474 raw_printf(out, "'"); 1475 continue; 1476 } 1477 if( c==0 ){ 1478 break; 1479 } 1480 z++; 1481 } 1482 raw_printf(out, "'"); 1483 } 1484 setTextMode(out, 1); 1485} 1486 1487/* 1488** Output the given string as a quoted string using SQL quoting conventions. 1489** Additionallly , escape the "\n" and "\r" characters so that they do not 1490** get corrupted by end-of-line translation facilities in some operating 1491** systems. 1492** 1493** This is like output_quoted_string() but with the addition of the \r\n 1494** escape mechanism. 1495*/ 1496static void output_quoted_escaped_string(FILE *out, const char *z){ 1497 int i; 1498 char c; 1499 setBinaryMode(out, 1); 1500 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} 1501 if( c==0 ){ 1502 utf8_printf(out,"'%s'",z); 1503 }else{ 1504 const char *zNL = 0; 1505 const char *zCR = 0; 1506 int nNL = 0; 1507 int nCR = 0; 1508 char zBuf1[20], zBuf2[20]; 1509 for(i=0; z[i]; i++){ 1510 if( z[i]=='\n' ) nNL++; 1511 if( z[i]=='\r' ) nCR++; 1512 } 1513 if( nNL ){ 1514 raw_printf(out, "replace("); 1515 zNL = unused_string(z, "\\n", "\\012", zBuf1); 1516 } 1517 if( nCR ){ 1518 raw_printf(out, "replace("); 1519 zCR = unused_string(z, "\\r", "\\015", zBuf2); 1520 } 1521 raw_printf(out, "'"); 1522 while( *z ){ 1523 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} 1524 if( c=='\'' ) i++; 1525 if( i ){ 1526 utf8_printf(out, "%.*s", i, z); 1527 z += i; 1528 } 1529 if( c=='\'' ){ 1530 raw_printf(out, "'"); 1531 continue; 1532 } 1533 if( c==0 ){ 1534 break; 1535 } 1536 z++; 1537 if( c=='\n' ){ 1538 raw_printf(out, "%s", zNL); 1539 continue; 1540 } 1541 raw_printf(out, "%s", zCR); 1542 } 1543 raw_printf(out, "'"); 1544 if( nCR ){ 1545 raw_printf(out, ",'%s',char(13))", zCR); 1546 } 1547 if( nNL ){ 1548 raw_printf(out, ",'%s',char(10))", zNL); 1549 } 1550 } 1551 setTextMode(out, 1); 1552} 1553 1554/* 1555** Output the given string as a quoted according to C or TCL quoting rules. 1556*/ 1557static void output_c_string(FILE *out, const char *z){ 1558 unsigned int c; 1559 fputc('"', out); 1560 while( (c = *(z++))!=0 ){ 1561 if( c=='\\' ){ 1562 fputc(c, out); 1563 fputc(c, out); 1564 }else if( c=='"' ){ 1565 fputc('\\', out); 1566 fputc('"', out); 1567 }else if( c=='\t' ){ 1568 fputc('\\', out); 1569 fputc('t', out); 1570 }else if( c=='\n' ){ 1571 fputc('\\', out); 1572 fputc('n', out); 1573 }else if( c=='\r' ){ 1574 fputc('\\', out); 1575 fputc('r', out); 1576 }else if( !isprint(c&0xff) ){ 1577 raw_printf(out, "\\%03o", c&0xff); 1578 }else{ 1579 fputc(c, out); 1580 } 1581 } 1582 fputc('"', out); 1583} 1584 1585/* 1586** Output the given string as a quoted according to JSON quoting rules. 1587*/ 1588static void output_json_string(FILE *out, const char *z, int n){ 1589 unsigned int c; 1590 if( n<0 ) n = (int)strlen(z); 1591 fputc('"', out); 1592 while( n-- ){ 1593 c = *(z++); 1594 if( c=='\\' || c=='"' ){ 1595 fputc('\\', out); 1596 fputc(c, out); 1597 }else if( c<=0x1f ){ 1598 fputc('\\', out); 1599 if( c=='\b' ){ 1600 fputc('b', out); 1601 }else if( c=='\f' ){ 1602 fputc('f', out); 1603 }else if( c=='\n' ){ 1604 fputc('n', out); 1605 }else if( c=='\r' ){ 1606 fputc('r', out); 1607 }else if( c=='\t' ){ 1608 fputc('t', out); 1609 }else{ 1610 raw_printf(out, "u%04x",c); 1611 } 1612 }else{ 1613 fputc(c, out); 1614 } 1615 } 1616 fputc('"', out); 1617} 1618 1619/* 1620** Output the given string with characters that are special to 1621** HTML escaped. 1622*/ 1623static void output_html_string(FILE *out, const char *z){ 1624 int i; 1625 if( z==0 ) z = ""; 1626 while( *z ){ 1627 for(i=0; z[i] 1628 && z[i]!='<' 1629 && z[i]!='&' 1630 && z[i]!='>' 1631 && z[i]!='\"' 1632 && z[i]!='\''; 1633 i++){} 1634 if( i>0 ){ 1635 utf8_printf(out,"%.*s",i,z); 1636 } 1637 if( z[i]=='<' ){ 1638 raw_printf(out,"<"); 1639 }else if( z[i]=='&' ){ 1640 raw_printf(out,"&"); 1641 }else if( z[i]=='>' ){ 1642 raw_printf(out,">"); 1643 }else if( z[i]=='\"' ){ 1644 raw_printf(out,"""); 1645 }else if( z[i]=='\'' ){ 1646 raw_printf(out,"'"); 1647 }else{ 1648 break; 1649 } 1650 z += i + 1; 1651 } 1652} 1653 1654/* 1655** If a field contains any character identified by a 1 in the following 1656** array, then the string must be quoted for CSV. 1657*/ 1658static const char needCsvQuote[] = { 1659 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1660 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1661 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1663 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1665 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1666 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1667 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1668 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1669 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1670 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1671 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1672 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1673 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1674 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1675}; 1676 1677/* 1678** Output a single term of CSV. Actually, p->colSeparator is used for 1679** the separator, which may or may not be a comma. p->nullValue is 1680** the null value. Strings are quoted if necessary. The separator 1681** is only issued if bSep is true. 1682*/ 1683static void output_csv(ShellState *p, const char *z, int bSep){ 1684 FILE *out = p->out; 1685 if( z==0 ){ 1686 utf8_printf(out,"%s",p->nullValue); 1687 }else{ 1688 int i; 1689 int nSep = strlen30(p->colSeparator); 1690 for(i=0; z[i]; i++){ 1691 if( needCsvQuote[((unsigned char*)z)[i]] 1692 || (z[i]==p->colSeparator[0] && 1693 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ 1694 i = 0; 1695 break; 1696 } 1697 } 1698 if( i==0 ){ 1699 char *zQuoted = sqlite3_mprintf("\"%w\"", z); 1700 utf8_printf(out, "%s", zQuoted); 1701 sqlite3_free(zQuoted); 1702 }else{ 1703 utf8_printf(out, "%s", z); 1704 } 1705 } 1706 if( bSep ){ 1707 utf8_printf(p->out, "%s", p->colSeparator); 1708 } 1709} 1710 1711/* 1712** This routine runs when the user presses Ctrl-C 1713*/ 1714static void interrupt_handler(int NotUsed){ 1715 UNUSED_PARAMETER(NotUsed); 1716 seenInterrupt++; 1717 if( seenInterrupt>2 ) exit(1); 1718 if( globalDb ) sqlite3_interrupt(globalDb); 1719} 1720 1721#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 1722/* 1723** This routine runs for console events (e.g. Ctrl-C) on Win32 1724*/ 1725static BOOL WINAPI ConsoleCtrlHandler( 1726 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ 1727){ 1728 if( dwCtrlType==CTRL_C_EVENT ){ 1729 interrupt_handler(0); 1730 return TRUE; 1731 } 1732 return FALSE; 1733} 1734#endif 1735 1736#ifndef SQLITE_OMIT_AUTHORIZATION 1737/* 1738** When the ".auth ON" is set, the following authorizer callback is 1739** invoked. It always returns SQLITE_OK. 1740*/ 1741static int shellAuth( 1742 void *pClientData, 1743 int op, 1744 const char *zA1, 1745 const char *zA2, 1746 const char *zA3, 1747 const char *zA4 1748){ 1749 ShellState *p = (ShellState*)pClientData; 1750 static const char *azAction[] = { 0, 1751 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", 1752 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", 1753 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", 1754 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", 1755 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", 1756 "DROP_TRIGGER", "DROP_VIEW", "INSERT", 1757 "PRAGMA", "READ", "SELECT", 1758 "TRANSACTION", "UPDATE", "ATTACH", 1759 "DETACH", "ALTER_TABLE", "REINDEX", 1760 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", 1761 "FUNCTION", "SAVEPOINT", "RECURSIVE" 1762 }; 1763 int i; 1764 const char *az[4]; 1765 az[0] = zA1; 1766 az[1] = zA2; 1767 az[2] = zA3; 1768 az[3] = zA4; 1769 utf8_printf(p->out, "authorizer: %s", azAction[op]); 1770 for(i=0; i<4; i++){ 1771 raw_printf(p->out, " "); 1772 if( az[i] ){ 1773 output_c_string(p->out, az[i]); 1774 }else{ 1775 raw_printf(p->out, "NULL"); 1776 } 1777 } 1778 raw_printf(p->out, "\n"); 1779 return SQLITE_OK; 1780} 1781#endif 1782 1783/* 1784** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. 1785** 1786** This routine converts some CREATE TABLE statements for shadow tables 1787** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. 1788*/ 1789static void printSchemaLine(FILE *out, const char *z, const char *zTail){ 1790 if( z==0 ) return; 1791 if( zTail==0 ) return; 1792 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ 1793 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); 1794 }else{ 1795 utf8_printf(out, "%s%s", z, zTail); 1796 } 1797} 1798static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ 1799 char c = z[n]; 1800 z[n] = 0; 1801 printSchemaLine(out, z, zTail); 1802 z[n] = c; 1803} 1804 1805/* 1806** Return true if string z[] has nothing but whitespace and comments to the 1807** end of the first line. 1808*/ 1809static int wsToEol(const char *z){ 1810 int i; 1811 for(i=0; z[i]; i++){ 1812 if( z[i]=='\n' ) return 1; 1813 if( IsSpace(z[i]) ) continue; 1814 if( z[i]=='-' && z[i+1]=='-' ) return 1; 1815 return 0; 1816 } 1817 return 1; 1818} 1819 1820/* 1821** Add a new entry to the EXPLAIN QUERY PLAN data 1822*/ 1823static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ 1824 EQPGraphRow *pNew; 1825 int nText = strlen30(zText); 1826 if( p->autoEQPtest ){ 1827 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); 1828 } 1829 pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); 1830 if( pNew==0 ) shell_out_of_memory(); 1831 pNew->iEqpId = iEqpId; 1832 pNew->iParentId = p2; 1833 memcpy(pNew->zText, zText, nText+1); 1834 pNew->pNext = 0; 1835 if( p->sGraph.pLast ){ 1836 p->sGraph.pLast->pNext = pNew; 1837 }else{ 1838 p->sGraph.pRow = pNew; 1839 } 1840 p->sGraph.pLast = pNew; 1841} 1842 1843/* 1844** Free and reset the EXPLAIN QUERY PLAN data that has been collected 1845** in p->sGraph. 1846*/ 1847static void eqp_reset(ShellState *p){ 1848 EQPGraphRow *pRow, *pNext; 1849 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ 1850 pNext = pRow->pNext; 1851 sqlite3_free(pRow); 1852 } 1853 memset(&p->sGraph, 0, sizeof(p->sGraph)); 1854} 1855 1856/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after 1857** pOld, or return the first such line if pOld is NULL 1858*/ 1859static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ 1860 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; 1861 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; 1862 return pRow; 1863} 1864 1865/* Render a single level of the graph that has iEqpId as its parent. Called 1866** recursively to render sublevels. 1867*/ 1868static void eqp_render_level(ShellState *p, int iEqpId){ 1869 EQPGraphRow *pRow, *pNext; 1870 int n = strlen30(p->sGraph.zPrefix); 1871 char *z; 1872 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ 1873 pNext = eqp_next_row(p, iEqpId, pRow); 1874 z = pRow->zText; 1875 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, 1876 pNext ? "|--" : "`--", z); 1877 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ 1878 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); 1879 eqp_render_level(p, pRow->iEqpId); 1880 p->sGraph.zPrefix[n] = 0; 1881 } 1882 } 1883} 1884 1885/* 1886** Display and reset the EXPLAIN QUERY PLAN data 1887*/ 1888static void eqp_render(ShellState *p){ 1889 EQPGraphRow *pRow = p->sGraph.pRow; 1890 if( pRow ){ 1891 if( pRow->zText[0]=='-' ){ 1892 if( pRow->pNext==0 ){ 1893 eqp_reset(p); 1894 return; 1895 } 1896 utf8_printf(p->out, "%s\n", pRow->zText+3); 1897 p->sGraph.pRow = pRow->pNext; 1898 sqlite3_free(pRow); 1899 }else{ 1900 utf8_printf(p->out, "QUERY PLAN\n"); 1901 } 1902 p->sGraph.zPrefix[0] = 0; 1903 eqp_render_level(p, 0); 1904 eqp_reset(p); 1905 } 1906} 1907 1908#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1909/* 1910** Progress handler callback. 1911*/ 1912static int progress_handler(void *pClientData) { 1913 ShellState *p = (ShellState*)pClientData; 1914 p->nProgress++; 1915 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ 1916 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); 1917 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 1918 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; 1919 return 1; 1920 } 1921 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ 1922 raw_printf(p->out, "Progress %u\n", p->nProgress); 1923 } 1924 return 0; 1925} 1926#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 1927 1928/* 1929** Print N dashes 1930*/ 1931static void print_dashes(FILE *out, int N){ 1932 const char zDash[] = "--------------------------------------------------"; 1933 const int nDash = sizeof(zDash) - 1; 1934 while( N>nDash ){ 1935 fputs(zDash, out); 1936 N -= nDash; 1937 } 1938 raw_printf(out, "%.*s", N, zDash); 1939} 1940 1941/* 1942** Print a markdown or table-style row separator using ascii-art 1943*/ 1944static void print_row_separator( 1945 ShellState *p, 1946 int nArg, 1947 const char *zSep 1948){ 1949 int i; 1950 if( nArg>0 ){ 1951 fputs(zSep, p->out); 1952 print_dashes(p->out, p->actualWidth[0]+2); 1953 for(i=1; i<nArg; i++){ 1954 fputs(zSep, p->out); 1955 print_dashes(p->out, p->actualWidth[i]+2); 1956 } 1957 fputs(zSep, p->out); 1958 } 1959 fputs("\n", p->out); 1960} 1961 1962/* 1963** This is the callback routine that the shell 1964** invokes for each row of a query result. 1965*/ 1966static int shell_callback( 1967 void *pArg, 1968 int nArg, /* Number of result columns */ 1969 char **azArg, /* Text of each result column */ 1970 char **azCol, /* Column names */ 1971 int *aiType /* Column types. Might be NULL */ 1972){ 1973 int i; 1974 ShellState *p = (ShellState*)pArg; 1975 1976 if( azArg==0 ) return 0; 1977 switch( p->cMode ){ 1978 case MODE_Line: { 1979 int w = 5; 1980 if( azArg==0 ) break; 1981 for(i=0; i<nArg; i++){ 1982 int len = strlen30(azCol[i] ? azCol[i] : ""); 1983 if( len>w ) w = len; 1984 } 1985 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); 1986 for(i=0; i<nArg; i++){ 1987 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], 1988 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); 1989 } 1990 break; 1991 } 1992 case MODE_Explain: { 1993 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; 1994 if( nArg>ArraySize(aExplainWidth) ){ 1995 nArg = ArraySize(aExplainWidth); 1996 } 1997 if( p->cnt++==0 ){ 1998 for(i=0; i<nArg; i++){ 1999 int w = aExplainWidth[i]; 2000 utf8_width_print(p->out, w, azCol[i]); 2001 fputs(i==nArg-1 ? "\n" : " ", p->out); 2002 } 2003 for(i=0; i<nArg; i++){ 2004 int w = aExplainWidth[i]; 2005 print_dashes(p->out, w); 2006 fputs(i==nArg-1 ? "\n" : " ", p->out); 2007 } 2008 } 2009 if( azArg==0 ) break; 2010 for(i=0; i<nArg; i++){ 2011 int w = aExplainWidth[i]; 2012 if( azArg[i] && strlenChar(azArg[i])>w ){ 2013 w = strlenChar(azArg[i]); 2014 } 2015 if( i==1 && p->aiIndent && p->pStmt ){ 2016 if( p->iIndent<p->nIndent ){ 2017 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); 2018 } 2019 p->iIndent++; 2020 } 2021 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); 2022 fputs(i==nArg-1 ? "\n" : " ", p->out); 2023 } 2024 break; 2025 } 2026 case MODE_Semi: { /* .schema and .fullschema output */ 2027 printSchemaLine(p->out, azArg[0], ";\n"); 2028 break; 2029 } 2030 case MODE_Pretty: { /* .schema and .fullschema with --indent */ 2031 char *z; 2032 int j; 2033 int nParen = 0; 2034 char cEnd = 0; 2035 char c; 2036 int nLine = 0; 2037 assert( nArg==1 ); 2038 if( azArg[0]==0 ) break; 2039 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 2040 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 2041 ){ 2042 utf8_printf(p->out, "%s;\n", azArg[0]); 2043 break; 2044 } 2045 z = sqlite3_mprintf("%s", azArg[0]); 2046 j = 0; 2047 for(i=0; IsSpace(z[i]); i++){} 2048 for(; (c = z[i])!=0; i++){ 2049 if( IsSpace(c) ){ 2050 if( z[j-1]=='\r' ) z[j-1] = '\n'; 2051 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; 2052 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ 2053 j--; 2054 } 2055 z[j++] = c; 2056 } 2057 while( j>0 && IsSpace(z[j-1]) ){ j--; } 2058 z[j] = 0; 2059 if( strlen30(z)>=79 ){ 2060 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ 2061 if( c==cEnd ){ 2062 cEnd = 0; 2063 }else if( c=='"' || c=='\'' || c=='`' ){ 2064 cEnd = c; 2065 }else if( c=='[' ){ 2066 cEnd = ']'; 2067 }else if( c=='-' && z[i+1]=='-' ){ 2068 cEnd = '\n'; 2069 }else if( c=='(' ){ 2070 nParen++; 2071 }else if( c==')' ){ 2072 nParen--; 2073 if( nLine>0 && nParen==0 && j>0 ){ 2074 printSchemaLineN(p->out, z, j, "\n"); 2075 j = 0; 2076 } 2077 } 2078 z[j++] = c; 2079 if( nParen==1 && cEnd==0 2080 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) 2081 ){ 2082 if( c=='\n' ) j--; 2083 printSchemaLineN(p->out, z, j, "\n "); 2084 j = 0; 2085 nLine++; 2086 while( IsSpace(z[i+1]) ){ i++; } 2087 } 2088 } 2089 z[j] = 0; 2090 } 2091 printSchemaLine(p->out, z, ";\n"); 2092 sqlite3_free(z); 2093 break; 2094 } 2095 case MODE_List: { 2096 if( p->cnt++==0 && p->showHeader ){ 2097 for(i=0; i<nArg; i++){ 2098 utf8_printf(p->out,"%s%s",azCol[i], 2099 i==nArg-1 ? p->rowSeparator : p->colSeparator); 2100 } 2101 } 2102 if( azArg==0 ) break; 2103 for(i=0; i<nArg; i++){ 2104 char *z = azArg[i]; 2105 if( z==0 ) z = p->nullValue; 2106 utf8_printf(p->out, "%s", z); 2107 if( i<nArg-1 ){ 2108 utf8_printf(p->out, "%s", p->colSeparator); 2109 }else{ 2110 utf8_printf(p->out, "%s", p->rowSeparator); 2111 } 2112 } 2113 break; 2114 } 2115 case MODE_Html: { 2116 if( p->cnt++==0 && p->showHeader ){ 2117 raw_printf(p->out,"<TR>"); 2118 for(i=0; i<nArg; i++){ 2119 raw_printf(p->out,"<TH>"); 2120 output_html_string(p->out, azCol[i]); 2121 raw_printf(p->out,"</TH>\n"); 2122 } 2123 raw_printf(p->out,"</TR>\n"); 2124 } 2125 if( azArg==0 ) break; 2126 raw_printf(p->out,"<TR>"); 2127 for(i=0; i<nArg; i++){ 2128 raw_printf(p->out,"<TD>"); 2129 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2130 raw_printf(p->out,"</TD>\n"); 2131 } 2132 raw_printf(p->out,"</TR>\n"); 2133 break; 2134 } 2135 case MODE_Tcl: { 2136 if( p->cnt++==0 && p->showHeader ){ 2137 for(i=0; i<nArg; i++){ 2138 output_c_string(p->out,azCol[i] ? azCol[i] : ""); 2139 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2140 } 2141 utf8_printf(p->out, "%s", p->rowSeparator); 2142 } 2143 if( azArg==0 ) break; 2144 for(i=0; i<nArg; i++){ 2145 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2146 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2147 } 2148 utf8_printf(p->out, "%s", p->rowSeparator); 2149 break; 2150 } 2151 case MODE_Csv: { 2152 setBinaryMode(p->out, 1); 2153 if( p->cnt++==0 && p->showHeader ){ 2154 for(i=0; i<nArg; i++){ 2155 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); 2156 } 2157 utf8_printf(p->out, "%s", p->rowSeparator); 2158 } 2159 if( nArg>0 ){ 2160 for(i=0; i<nArg; i++){ 2161 output_csv(p, azArg[i], i<nArg-1); 2162 } 2163 utf8_printf(p->out, "%s", p->rowSeparator); 2164 } 2165 setTextMode(p->out, 1); 2166 break; 2167 } 2168 case MODE_Insert: { 2169 if( azArg==0 ) break; 2170 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); 2171 if( p->showHeader ){ 2172 raw_printf(p->out,"("); 2173 for(i=0; i<nArg; i++){ 2174 if( i>0 ) raw_printf(p->out, ","); 2175 if( quoteChar(azCol[i]) ){ 2176 char *z = sqlite3_mprintf("\"%w\"", azCol[i]); 2177 utf8_printf(p->out, "%s", z); 2178 sqlite3_free(z); 2179 }else{ 2180 raw_printf(p->out, "%s", azCol[i]); 2181 } 2182 } 2183 raw_printf(p->out,")"); 2184 } 2185 p->cnt++; 2186 for(i=0; i<nArg; i++){ 2187 raw_printf(p->out, i>0 ? "," : " VALUES("); 2188 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2189 utf8_printf(p->out,"NULL"); 2190 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2191 if( ShellHasFlag(p, SHFLG_Newlines) ){ 2192 output_quoted_string(p->out, azArg[i]); 2193 }else{ 2194 output_quoted_escaped_string(p->out, azArg[i]); 2195 } 2196 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2197 utf8_printf(p->out,"%s", azArg[i]); 2198 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2199 char z[50]; 2200 double r = sqlite3_column_double(p->pStmt, i); 2201 sqlite3_uint64 ur; 2202 memcpy(&ur,&r,sizeof(r)); 2203 if( ur==0x7ff0000000000000LL ){ 2204 raw_printf(p->out, "1e999"); 2205 }else if( ur==0xfff0000000000000LL ){ 2206 raw_printf(p->out, "-1e999"); 2207 }else{ 2208 sqlite3_snprintf(50,z,"%!.20g", r); 2209 raw_printf(p->out, "%s", z); 2210 } 2211 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2212 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2213 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2214 output_hex_blob(p->out, pBlob, nBlob); 2215 }else if( isNumber(azArg[i], 0) ){ 2216 utf8_printf(p->out,"%s", azArg[i]); 2217 }else if( ShellHasFlag(p, SHFLG_Newlines) ){ 2218 output_quoted_string(p->out, azArg[i]); 2219 }else{ 2220 output_quoted_escaped_string(p->out, azArg[i]); 2221 } 2222 } 2223 raw_printf(p->out,");\n"); 2224 break; 2225 } 2226 case MODE_Json: { 2227 if( azArg==0 ) break; 2228 if( p->cnt==0 ){ 2229 fputs("[{", p->out); 2230 }else{ 2231 fputs(",\n{", p->out); 2232 } 2233 p->cnt++; 2234 for(i=0; i<nArg; i++){ 2235 output_json_string(p->out, azCol[i], -1); 2236 putc(':', p->out); 2237 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2238 fputs("null",p->out); 2239 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2240 char z[50]; 2241 double r = sqlite3_column_double(p->pStmt, i); 2242 sqlite3_uint64 ur; 2243 memcpy(&ur,&r,sizeof(r)); 2244 if( ur==0x7ff0000000000000LL ){ 2245 raw_printf(p->out, "1e999"); 2246 }else if( ur==0xfff0000000000000LL ){ 2247 raw_printf(p->out, "-1e999"); 2248 }else{ 2249 sqlite3_snprintf(50,z,"%!.20g", r); 2250 raw_printf(p->out, "%s", z); 2251 } 2252 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2253 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2254 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2255 output_json_string(p->out, pBlob, nBlob); 2256 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2257 output_json_string(p->out, azArg[i], -1); 2258 }else{ 2259 utf8_printf(p->out,"%s", azArg[i]); 2260 } 2261 if( i<nArg-1 ){ 2262 putc(',', p->out); 2263 } 2264 } 2265 putc('}', p->out); 2266 break; 2267 } 2268 case MODE_Quote: { 2269 if( azArg==0 ) break; 2270 if( p->cnt==0 && p->showHeader ){ 2271 for(i=0; i<nArg; i++){ 2272 if( i>0 ) fputs(p->colSeparator, p->out); 2273 output_quoted_string(p->out, azCol[i]); 2274 } 2275 fputs(p->rowSeparator, p->out); 2276 } 2277 p->cnt++; 2278 for(i=0; i<nArg; i++){ 2279 if( i>0 ) fputs(p->colSeparator, p->out); 2280 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2281 utf8_printf(p->out,"NULL"); 2282 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2283 output_quoted_string(p->out, azArg[i]); 2284 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2285 utf8_printf(p->out,"%s", azArg[i]); 2286 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2287 char z[50]; 2288 double r = sqlite3_column_double(p->pStmt, i); 2289 sqlite3_snprintf(50,z,"%!.20g", r); 2290 raw_printf(p->out, "%s", z); 2291 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2292 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2293 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2294 output_hex_blob(p->out, pBlob, nBlob); 2295 }else if( isNumber(azArg[i], 0) ){ 2296 utf8_printf(p->out,"%s", azArg[i]); 2297 }else{ 2298 output_quoted_string(p->out, azArg[i]); 2299 } 2300 } 2301 fputs(p->rowSeparator, p->out); 2302 break; 2303 } 2304 case MODE_Ascii: { 2305 if( p->cnt++==0 && p->showHeader ){ 2306 for(i=0; i<nArg; i++){ 2307 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2308 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); 2309 } 2310 utf8_printf(p->out, "%s", p->rowSeparator); 2311 } 2312 if( azArg==0 ) break; 2313 for(i=0; i<nArg; i++){ 2314 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2315 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); 2316 } 2317 utf8_printf(p->out, "%s", p->rowSeparator); 2318 break; 2319 } 2320 case MODE_EQP: { 2321 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); 2322 break; 2323 } 2324 } 2325 return 0; 2326} 2327 2328/* 2329** This is the callback routine that the SQLite library 2330** invokes for each row of a query result. 2331*/ 2332static int callback(void *pArg, int nArg, char **azArg, char **azCol){ 2333 /* since we don't have type info, call the shell_callback with a NULL value */ 2334 return shell_callback(pArg, nArg, azArg, azCol, NULL); 2335} 2336 2337/* 2338** This is the callback routine from sqlite3_exec() that appends all 2339** output onto the end of a ShellText object. 2340*/ 2341static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ 2342 ShellText *p = (ShellText*)pArg; 2343 int i; 2344 UNUSED_PARAMETER(az); 2345 if( azArg==0 ) return 0; 2346 if( p->n ) appendText(p, "|", 0); 2347 for(i=0; i<nArg; i++){ 2348 if( i ) appendText(p, ",", 0); 2349 if( azArg[i] ) appendText(p, azArg[i], 0); 2350 } 2351 return 0; 2352} 2353 2354/* 2355** Generate an appropriate SELFTEST table in the main database. 2356*/ 2357static void createSelftestTable(ShellState *p){ 2358 char *zErrMsg = 0; 2359 sqlite3_exec(p->db, 2360 "SAVEPOINT selftest_init;\n" 2361 "CREATE TABLE IF NOT EXISTS selftest(\n" 2362 " tno INTEGER PRIMARY KEY,\n" /* Test number */ 2363 " op TEXT,\n" /* Operator: memo run */ 2364 " cmd TEXT,\n" /* Command text */ 2365 " ans TEXT\n" /* Desired answer */ 2366 ");" 2367 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" 2368 "INSERT INTO [_shell$self](rowid,op,cmd)\n" 2369 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" 2370 " 'memo','Tests generated by --init');\n" 2371 "INSERT INTO [_shell$self]\n" 2372 " SELECT 'run',\n" 2373 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " 2374 "FROM sqlite_master ORDER BY 2'',224))',\n" 2375 " hex(sha3_query('SELECT type,name,tbl_name,sql " 2376 "FROM sqlite_master ORDER BY 2',224));\n" 2377 "INSERT INTO [_shell$self]\n" 2378 " SELECT 'run'," 2379 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" 2380 " printf('%w',name) || '\" NOT INDEXED'',224))',\n" 2381 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" 2382 " FROM (\n" 2383 " SELECT name FROM sqlite_master\n" 2384 " WHERE type='table'\n" 2385 " AND name<>'selftest'\n" 2386 " AND coalesce(rootpage,0)>0\n" 2387 " )\n" 2388 " ORDER BY name;\n" 2389 "INSERT INTO [_shell$self]\n" 2390 " VALUES('run','PRAGMA integrity_check','ok');\n" 2391 "INSERT INTO selftest(tno,op,cmd,ans)" 2392 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" 2393 "DROP TABLE [_shell$self];" 2394 ,0,0,&zErrMsg); 2395 if( zErrMsg ){ 2396 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); 2397 sqlite3_free(zErrMsg); 2398 } 2399 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); 2400} 2401 2402 2403/* 2404** Set the destination table field of the ShellState structure to 2405** the name of the table given. Escape any quote characters in the 2406** table name. 2407*/ 2408static void set_table_name(ShellState *p, const char *zName){ 2409 int i, n; 2410 char cQuote; 2411 char *z; 2412 2413 if( p->zDestTable ){ 2414 free(p->zDestTable); 2415 p->zDestTable = 0; 2416 } 2417 if( zName==0 ) return; 2418 cQuote = quoteChar(zName); 2419 n = strlen30(zName); 2420 if( cQuote ) n += n+2; 2421 z = p->zDestTable = malloc( n+1 ); 2422 if( z==0 ) shell_out_of_memory(); 2423 n = 0; 2424 if( cQuote ) z[n++] = cQuote; 2425 for(i=0; zName[i]; i++){ 2426 z[n++] = zName[i]; 2427 if( zName[i]==cQuote ) z[n++] = cQuote; 2428 } 2429 if( cQuote ) z[n++] = cQuote; 2430 z[n] = 0; 2431} 2432 2433 2434/* 2435** Execute a query statement that will generate SQL output. Print 2436** the result columns, comma-separated, on a line and then add a 2437** semicolon terminator to the end of that line. 2438** 2439** If the number of columns is 1 and that column contains text "--" 2440** then write the semicolon on a separate line. That way, if a 2441** "--" comment occurs at the end of the statement, the comment 2442** won't consume the semicolon terminator. 2443*/ 2444static int run_table_dump_query( 2445 ShellState *p, /* Query context */ 2446 const char *zSelect /* SELECT statement to extract content */ 2447){ 2448 sqlite3_stmt *pSelect; 2449 int rc; 2450 int nResult; 2451 int i; 2452 const char *z; 2453 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); 2454 if( rc!=SQLITE_OK || !pSelect ){ 2455 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2456 sqlite3_errmsg(p->db)); 2457 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2458 return rc; 2459 } 2460 rc = sqlite3_step(pSelect); 2461 nResult = sqlite3_column_count(pSelect); 2462 while( rc==SQLITE_ROW ){ 2463 z = (const char*)sqlite3_column_text(pSelect, 0); 2464 utf8_printf(p->out, "%s", z); 2465 for(i=1; i<nResult; i++){ 2466 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); 2467 } 2468 if( z==0 ) z = ""; 2469 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; 2470 if( z[0] ){ 2471 raw_printf(p->out, "\n;\n"); 2472 }else{ 2473 raw_printf(p->out, ";\n"); 2474 } 2475 rc = sqlite3_step(pSelect); 2476 } 2477 rc = sqlite3_finalize(pSelect); 2478 if( rc!=SQLITE_OK ){ 2479 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2480 sqlite3_errmsg(p->db)); 2481 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2482 } 2483 return rc; 2484} 2485 2486/* 2487** Allocate space and save off current error string. 2488*/ 2489static char *save_err_msg( 2490 sqlite3 *db /* Database to query */ 2491){ 2492 int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); 2493 char *zErrMsg = sqlite3_malloc64(nErrMsg); 2494 if( zErrMsg ){ 2495 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); 2496 } 2497 return zErrMsg; 2498} 2499 2500#ifdef __linux__ 2501/* 2502** Attempt to display I/O stats on Linux using /proc/PID/io 2503*/ 2504static void displayLinuxIoStats(FILE *out){ 2505 FILE *in; 2506 char z[200]; 2507 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 2508 in = fopen(z, "rb"); 2509 if( in==0 ) return; 2510 while( fgets(z, sizeof(z), in)!=0 ){ 2511 static const struct { 2512 const char *zPattern; 2513 const char *zDesc; 2514 } aTrans[] = { 2515 { "rchar: ", "Bytes received by read():" }, 2516 { "wchar: ", "Bytes sent to write():" }, 2517 { "syscr: ", "Read() system calls:" }, 2518 { "syscw: ", "Write() system calls:" }, 2519 { "read_bytes: ", "Bytes read from storage:" }, 2520 { "write_bytes: ", "Bytes written to storage:" }, 2521 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 2522 }; 2523 int i; 2524 for(i=0; i<ArraySize(aTrans); i++){ 2525 int n = strlen30(aTrans[i].zPattern); 2526 if( strncmp(aTrans[i].zPattern, z, n)==0 ){ 2527 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); 2528 break; 2529 } 2530 } 2531 } 2532 fclose(in); 2533} 2534#endif 2535 2536/* 2537** Display a single line of status using 64-bit values. 2538*/ 2539static void displayStatLine( 2540 ShellState *p, /* The shell context */ 2541 char *zLabel, /* Label for this one line */ 2542 char *zFormat, /* Format for the result */ 2543 int iStatusCtrl, /* Which status to display */ 2544 int bReset /* True to reset the stats */ 2545){ 2546 sqlite3_int64 iCur = -1; 2547 sqlite3_int64 iHiwtr = -1; 2548 int i, nPercent; 2549 char zLine[200]; 2550 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); 2551 for(i=0, nPercent=0; zFormat[i]; i++){ 2552 if( zFormat[i]=='%' ) nPercent++; 2553 } 2554 if( nPercent>1 ){ 2555 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); 2556 }else{ 2557 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); 2558 } 2559 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); 2560} 2561 2562/* 2563** Display memory stats. 2564*/ 2565static int display_stats( 2566 sqlite3 *db, /* Database to query */ 2567 ShellState *pArg, /* Pointer to ShellState */ 2568 int bReset /* True to reset the stats */ 2569){ 2570 int iCur; 2571 int iHiwtr; 2572 FILE *out; 2573 if( pArg==0 || pArg->out==0 ) return 0; 2574 out = pArg->out; 2575 2576 if( pArg->pStmt && (pArg->statsOn & 2) ){ 2577 int nCol, i, x; 2578 sqlite3_stmt *pStmt = pArg->pStmt; 2579 char z[100]; 2580 nCol = sqlite3_column_count(pStmt); 2581 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); 2582 for(i=0; i<nCol; i++){ 2583 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); 2584 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); 2585#ifndef SQLITE_OMIT_DECLTYPE 2586 sqlite3_snprintf(30, z+x, "declared type:"); 2587 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); 2588#endif 2589#ifdef SQLITE_ENABLE_COLUMN_METADATA 2590 sqlite3_snprintf(30, z+x, "database name:"); 2591 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); 2592 sqlite3_snprintf(30, z+x, "table name:"); 2593 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); 2594 sqlite3_snprintf(30, z+x, "origin name:"); 2595 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); 2596#endif 2597 } 2598 } 2599 2600 displayStatLine(pArg, "Memory Used:", 2601 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); 2602 displayStatLine(pArg, "Number of Outstanding Allocations:", 2603 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); 2604 if( pArg->shellFlgs & SHFLG_Pagecache ){ 2605 displayStatLine(pArg, "Number of Pcache Pages Used:", 2606 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); 2607 } 2608 displayStatLine(pArg, "Number of Pcache Overflow Bytes:", 2609 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); 2610 displayStatLine(pArg, "Largest Allocation:", 2611 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); 2612 displayStatLine(pArg, "Largest Pcache Allocation:", 2613 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); 2614#ifdef YYTRACKMAXSTACKDEPTH 2615 displayStatLine(pArg, "Deepest Parser Stack:", 2616 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); 2617#endif 2618 2619 if( db ){ 2620 if( pArg->shellFlgs & SHFLG_Lookaside ){ 2621 iHiwtr = iCur = -1; 2622 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, 2623 &iCur, &iHiwtr, bReset); 2624 raw_printf(pArg->out, 2625 "Lookaside Slots Used: %d (max %d)\n", 2626 iCur, iHiwtr); 2627 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, 2628 &iCur, &iHiwtr, bReset); 2629 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", 2630 iHiwtr); 2631 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, 2632 &iCur, &iHiwtr, bReset); 2633 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", 2634 iHiwtr); 2635 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, 2636 &iCur, &iHiwtr, bReset); 2637 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", 2638 iHiwtr); 2639 } 2640 iHiwtr = iCur = -1; 2641 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); 2642 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", 2643 iCur); 2644 iHiwtr = iCur = -1; 2645 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); 2646 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); 2647 iHiwtr = iCur = -1; 2648 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); 2649 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); 2650 iHiwtr = iCur = -1; 2651 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); 2652 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); 2653 iHiwtr = iCur = -1; 2654 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); 2655 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); 2656 iHiwtr = iCur = -1; 2657 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); 2658 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", 2659 iCur); 2660 iHiwtr = iCur = -1; 2661 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); 2662 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", 2663 iCur); 2664 } 2665 2666 if( pArg->pStmt ){ 2667 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, 2668 bReset); 2669 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); 2670 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); 2671 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); 2672 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); 2673 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); 2674 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2675 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); 2676 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); 2677 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); 2678 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); 2679 raw_printf(pArg->out, "Number of times run: %d\n", iCur); 2680 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); 2681 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); 2682 } 2683 2684#ifdef __linux__ 2685 displayLinuxIoStats(pArg->out); 2686#endif 2687 2688 /* Do not remove this machine readable comment: extra-stats-output-here */ 2689 2690 return 0; 2691} 2692 2693/* 2694** Display scan stats. 2695*/ 2696static void display_scanstats( 2697 sqlite3 *db, /* Database to query */ 2698 ShellState *pArg /* Pointer to ShellState */ 2699){ 2700#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 2701 UNUSED_PARAMETER(db); 2702 UNUSED_PARAMETER(pArg); 2703#else 2704 int i, k, n, mx; 2705 raw_printf(pArg->out, "-------- scanstats --------\n"); 2706 mx = 0; 2707 for(k=0; k<=mx; k++){ 2708 double rEstLoop = 1.0; 2709 for(i=n=0; 1; i++){ 2710 sqlite3_stmt *p = pArg->pStmt; 2711 sqlite3_int64 nLoop, nVisit; 2712 double rEst; 2713 int iSid; 2714 const char *zExplain; 2715 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ 2716 break; 2717 } 2718 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); 2719 if( iSid>mx ) mx = iSid; 2720 if( iSid!=k ) continue; 2721 if( n==0 ){ 2722 rEstLoop = (double)nLoop; 2723 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); 2724 } 2725 n++; 2726 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2727 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); 2728 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 2729 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); 2730 rEstLoop *= rEst; 2731 raw_printf(pArg->out, 2732 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", 2733 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst 2734 ); 2735 } 2736 } 2737 raw_printf(pArg->out, "---------------------------\n"); 2738#endif 2739} 2740 2741/* 2742** Parameter azArray points to a zero-terminated array of strings. zStr 2743** points to a single nul-terminated string. Return non-zero if zStr 2744** is equal, according to strcmp(), to any of the strings in the array. 2745** Otherwise, return zero. 2746*/ 2747static int str_in_array(const char *zStr, const char **azArray){ 2748 int i; 2749 for(i=0; azArray[i]; i++){ 2750 if( 0==strcmp(zStr, azArray[i]) ) return 1; 2751 } 2752 return 0; 2753} 2754 2755/* 2756** If compiled statement pSql appears to be an EXPLAIN statement, allocate 2757** and populate the ShellState.aiIndent[] array with the number of 2758** spaces each opcode should be indented before it is output. 2759** 2760** The indenting rules are: 2761** 2762** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent 2763** all opcodes that occur between the p2 jump destination and the opcode 2764** itself by 2 spaces. 2765** 2766** * For each "Goto", if the jump destination is earlier in the program 2767** and ends on one of: 2768** Yield SeekGt SeekLt RowSetRead Rewind 2769** or if the P1 parameter is one instead of zero, 2770** then indent all opcodes between the earlier instruction 2771** and "Goto" by 2 spaces. 2772*/ 2773static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ 2774 const char *zSql; /* The text of the SQL statement */ 2775 const char *z; /* Used to check if this is an EXPLAIN */ 2776 int *abYield = 0; /* True if op is an OP_Yield */ 2777 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ 2778 int iOp; /* Index of operation in p->aiIndent[] */ 2779 2780 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 }; 2781 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", 2782 "Rewind", 0 }; 2783 const char *azGoto[] = { "Goto", 0 }; 2784 2785 /* Try to figure out if this is really an EXPLAIN statement. If this 2786 ** cannot be verified, return early. */ 2787 if( sqlite3_column_count(pSql)!=8 ){ 2788 p->cMode = p->mode; 2789 return; 2790 } 2791 zSql = sqlite3_sql(pSql); 2792 if( zSql==0 ) return; 2793 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); 2794 if( sqlite3_strnicmp(z, "explain", 7) ){ 2795 p->cMode = p->mode; 2796 return; 2797 } 2798 2799 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ 2800 int i; 2801 int iAddr = sqlite3_column_int(pSql, 0); 2802 const char *zOp = (const char*)sqlite3_column_text(pSql, 1); 2803 2804 /* Set p2 to the P2 field of the current opcode. Then, assuming that 2805 ** p2 is an instruction address, set variable p2op to the index of that 2806 ** instruction in the aiIndent[] array. p2 and p2op may be different if 2807 ** the current instruction is part of a sub-program generated by an 2808 ** SQL trigger or foreign key. */ 2809 int p2 = sqlite3_column_int(pSql, 3); 2810 int p2op = (p2 + (iOp-iAddr)); 2811 2812 /* Grow the p->aiIndent array as required */ 2813 if( iOp>=nAlloc ){ 2814 if( iOp==0 ){ 2815 /* Do further verfication that this is explain output. Abort if 2816 ** it is not */ 2817 static const char *explainCols[] = { 2818 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; 2819 int jj; 2820 for(jj=0; jj<ArraySize(explainCols); jj++){ 2821 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ 2822 p->cMode = p->mode; 2823 sqlite3_reset(pSql); 2824 return; 2825 } 2826 } 2827 } 2828 nAlloc += 100; 2829 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); 2830 if( p->aiIndent==0 ) shell_out_of_memory(); 2831 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); 2832 if( abYield==0 ) shell_out_of_memory(); 2833 } 2834 abYield[iOp] = str_in_array(zOp, azYield); 2835 p->aiIndent[iOp] = 0; 2836 p->nIndent = iOp+1; 2837 2838 if( str_in_array(zOp, azNext) ){ 2839 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 2840 } 2841 if( str_in_array(zOp, azGoto) && p2op<p->nIndent 2842 && (abYield[p2op] || sqlite3_column_int(pSql, 2)) 2843 ){ 2844 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 2845 } 2846 } 2847 2848 p->iIndent = 0; 2849 sqlite3_free(abYield); 2850 sqlite3_reset(pSql); 2851} 2852 2853/* 2854** Free the array allocated by explain_data_prepare(). 2855*/ 2856static void explain_data_delete(ShellState *p){ 2857 sqlite3_free(p->aiIndent); 2858 p->aiIndent = 0; 2859 p->nIndent = 0; 2860 p->iIndent = 0; 2861} 2862 2863/* 2864** Disable and restore .wheretrace and .selecttrace settings. 2865*/ 2866#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2867extern int sqlite3SelectTrace; 2868static int savedSelectTrace; 2869#endif 2870#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2871extern int sqlite3WhereTrace; 2872static int savedWhereTrace; 2873#endif 2874static void disable_debug_trace_modes(void){ 2875#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2876 savedSelectTrace = sqlite3SelectTrace; 2877 sqlite3SelectTrace = 0; 2878#endif 2879#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2880 savedWhereTrace = sqlite3WhereTrace; 2881 sqlite3WhereTrace = 0; 2882#endif 2883} 2884static void restore_debug_trace_modes(void){ 2885#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2886 sqlite3SelectTrace = savedSelectTrace; 2887#endif 2888#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2889 sqlite3WhereTrace = savedWhereTrace; 2890#endif 2891} 2892 2893/* Create the TEMP table used to store parameter bindings */ 2894static void bind_table_init(ShellState *p){ 2895 int wrSchema = 0; 2896 int defensiveMode = 0; 2897 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); 2898 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); 2899 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); 2900 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); 2901 sqlite3_exec(p->db, 2902 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" 2903 " key TEXT PRIMARY KEY,\n" 2904 " value ANY\n" 2905 ") WITHOUT ROWID;", 2906 0, 0, 0); 2907 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); 2908 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); 2909} 2910 2911/* 2912** Bind parameters on a prepared statement. 2913** 2914** Parameter bindings are taken from a TEMP table of the form: 2915** 2916** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) 2917** WITHOUT ROWID; 2918** 2919** No bindings occur if this table does not exist. The name of the table 2920** begins with "sqlite_" so that it will not collide with ordinary application 2921** tables. The table must be in the TEMP schema. 2922*/ 2923static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ 2924 int nVar; 2925 int i; 2926 int rc; 2927 sqlite3_stmt *pQ = 0; 2928 2929 nVar = sqlite3_bind_parameter_count(pStmt); 2930 if( nVar==0 ) return; /* Nothing to do */ 2931 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", 2932 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ 2933 return; /* Parameter table does not exist */ 2934 } 2935 rc = sqlite3_prepare_v2(pArg->db, 2936 "SELECT value FROM temp.sqlite_parameters" 2937 " WHERE key=?1", -1, &pQ, 0); 2938 if( rc || pQ==0 ) return; 2939 for(i=1; i<=nVar; i++){ 2940 char zNum[30]; 2941 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 2942 if( zVar==0 ){ 2943 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); 2944 zVar = zNum; 2945 } 2946 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); 2947 if( sqlite3_step(pQ)==SQLITE_ROW ){ 2948 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); 2949 }else{ 2950 sqlite3_bind_null(pStmt, i); 2951 } 2952 sqlite3_reset(pQ); 2953 } 2954 sqlite3_finalize(pQ); 2955} 2956 2957/* 2958** UTF8 box-drawing characters. Imagine box lines like this: 2959** 2960** 1 2961** | 2962** 4 --+-- 2 2963** | 2964** 3 2965** 2966** Each box characters has between 2 and 4 of the lines leading from 2967** the center. The characters are here identified by the numbers of 2968** their corresponding lines. 2969*/ 2970#define BOX_24 "\342\224\200" /* U+2500 --- */ 2971#define BOX_13 "\342\224\202" /* U+2502 | */ 2972#define BOX_23 "\342\224\214" /* U+250c ,- */ 2973#define BOX_34 "\342\224\220" /* U+2510 -, */ 2974#define BOX_12 "\342\224\224" /* U+2514 '- */ 2975#define BOX_14 "\342\224\230" /* U+2518 -' */ 2976#define BOX_123 "\342\224\234" /* U+251c |- */ 2977#define BOX_134 "\342\224\244" /* U+2524 -| */ 2978#define BOX_234 "\342\224\254" /* U+252c -,- */ 2979#define BOX_124 "\342\224\264" /* U+2534 -'- */ 2980#define BOX_1234 "\342\224\274" /* U+253c -|- */ 2981 2982/* Draw horizontal line N characters long using unicode box 2983** characters 2984*/ 2985static void print_box_line(FILE *out, int N){ 2986 const char zDash[] = 2987 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 2988 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; 2989 const int nDash = sizeof(zDash) - 1; 2990 N *= 3; 2991 while( N>nDash ){ 2992 utf8_printf(out, zDash); 2993 N -= nDash; 2994 } 2995 utf8_printf(out, "%.*s", N, zDash); 2996} 2997 2998/* 2999** Draw a horizontal separator for a MODE_Box table. 3000*/ 3001static void print_box_row_separator( 3002 ShellState *p, 3003 int nArg, 3004 const char *zSep1, 3005 const char *zSep2, 3006 const char *zSep3 3007){ 3008 int i; 3009 if( nArg>0 ){ 3010 utf8_printf(p->out, "%s", zSep1); 3011 print_box_line(p->out, p->actualWidth[0]+2); 3012 for(i=1; i<nArg; i++){ 3013 utf8_printf(p->out, "%s", zSep2); 3014 print_box_line(p->out, p->actualWidth[i]+2); 3015 } 3016 utf8_printf(p->out, "%s", zSep3); 3017 } 3018 fputs("\n", p->out); 3019} 3020 3021 3022 3023/* 3024** Run a prepared statement and output the result in one of the 3025** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, 3026** or MODE_Box. 3027** 3028** This is different from ordinary exec_prepared_stmt() in that 3029** it has to run the entire query and gather the results into memory 3030** first, in order to determine column widths, before providing 3031** any output. 3032*/ 3033static void exec_prepared_stmt_columnar( 3034 ShellState *p, /* Pointer to ShellState */ 3035 sqlite3_stmt *pStmt /* Statment to run */ 3036){ 3037 int nRow = 0; 3038 int nColumn = 0; 3039 char **azData = 0; 3040 char *zMsg = 0; 3041 const char *z; 3042 int rc; 3043 int i, j, nTotal, w, n; 3044 const char *colSep = 0; 3045 const char *rowSep = 0; 3046 3047 rc = sqlite3_get_table(p->db, sqlite3_sql(pStmt), 3048 &azData, &nRow, &nColumn, &zMsg); 3049 if( rc ){ 3050 utf8_printf(p->out, "ERROR: %s\n", zMsg); 3051 sqlite3_free(zMsg); 3052 sqlite3_free_table(azData); 3053 return; 3054 } 3055 if( nRow==0 || nColumn==0 ) goto columnar_end; 3056 if( nColumn>p->nWidth ){ 3057 p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int)); 3058 if( p->colWidth==0 ) shell_out_of_memory(); 3059 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; 3060 p->nWidth = nColumn; 3061 p->actualWidth = &p->colWidth[nColumn]; 3062 } 3063 memset(p->actualWidth, 0, nColumn*sizeof(int)); 3064 for(i=0; i<nColumn; i++){ 3065 w = p->colWidth[i]; 3066 if( w<0 ) w = -w; 3067 p->actualWidth[i] = w; 3068 } 3069 nTotal = nColumn*(nRow+1); 3070 for(i=0; i<nTotal; i++){ 3071 z = azData[i]; 3072 if( z==0 ) z = p->nullValue; 3073 n = strlenChar(z); 3074 j = i%nColumn; 3075 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; 3076 } 3077 if( seenInterrupt ) goto columnar_end; 3078 switch( p->cMode ){ 3079 case MODE_Column: { 3080 colSep = " "; 3081 rowSep = "\n"; 3082 if( p->showHeader ){ 3083 for(i=0; i<nColumn; i++){ 3084 w = p->actualWidth[i]; 3085 if( p->colWidth[i]<0 ) w = -w; 3086 utf8_width_print(p->out, w, azData[i]); 3087 fputs(i==nColumn-1?"\n":" ", p->out); 3088 } 3089 for(i=0; i<nColumn; i++){ 3090 print_dashes(p->out, p->actualWidth[i]); 3091 fputs(i==nColumn-1?"\n":" ", p->out); 3092 } 3093 } 3094 break; 3095 } 3096 case MODE_Table: { 3097 colSep = " | "; 3098 rowSep = " |\n"; 3099 print_row_separator(p, nColumn, "+"); 3100 fputs("| ", p->out); 3101 for(i=0; i<nColumn; i++){ 3102 w = p->actualWidth[i]; 3103 n = strlenChar(azData[i]); 3104 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3105 fputs(i==nColumn-1?" |\n":" | ", p->out); 3106 } 3107 print_row_separator(p, nColumn, "+"); 3108 break; 3109 } 3110 case MODE_Markdown: { 3111 colSep = " | "; 3112 rowSep = " |\n"; 3113 fputs("| ", p->out); 3114 for(i=0; i<nColumn; i++){ 3115 w = p->actualWidth[i]; 3116 n = strlenChar(azData[i]); 3117 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3118 fputs(i==nColumn-1?" |\n":" | ", p->out); 3119 } 3120 print_row_separator(p, nColumn, "|"); 3121 break; 3122 } 3123 case MODE_Box: { 3124 colSep = " " BOX_13 " "; 3125 rowSep = " " BOX_13 "\n"; 3126 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); 3127 utf8_printf(p->out, BOX_13 " "); 3128 for(i=0; i<nColumn; i++){ 3129 w = p->actualWidth[i]; 3130 n = strlenChar(azData[i]); 3131 utf8_printf(p->out, "%*s%s%*s%s", 3132 (w-n)/2, "", azData[i], (w-n+1)/2, "", 3133 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); 3134 } 3135 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); 3136 break; 3137 } 3138 } 3139 for(i=nColumn, j=0; i<nTotal; i++, j++){ 3140 if( j==0 && p->cMode!=MODE_Column ){ 3141 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); 3142 } 3143 z = azData[i]; 3144 if( z==0 ) z = p->nullValue; 3145 w = p->actualWidth[j]; 3146 if( p->colWidth[j]<0 ) w = -w; 3147 utf8_width_print(p->out, w, z); 3148 if( j==nColumn-1 ){ 3149 utf8_printf(p->out, "%s", rowSep); 3150 j = -1; 3151 if( seenInterrupt ) goto columnar_end; 3152 }else{ 3153 utf8_printf(p->out, "%s", colSep); 3154 } 3155 } 3156 if( p->cMode==MODE_Table ){ 3157 print_row_separator(p, nColumn, "+"); 3158 }else if( p->cMode==MODE_Box ){ 3159 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); 3160 } 3161columnar_end: 3162 if( seenInterrupt ){ 3163 utf8_printf(p->out, "Interrupt\n"); 3164 } 3165 sqlite3_free_table(azData); 3166} 3167 3168/* 3169** Run a prepared statement 3170*/ 3171static void exec_prepared_stmt( 3172 ShellState *pArg, /* Pointer to ShellState */ 3173 sqlite3_stmt *pStmt /* Statment to run */ 3174){ 3175 int rc; 3176 3177 if( pArg->cMode==MODE_Column 3178 || pArg->cMode==MODE_Table 3179 || pArg->cMode==MODE_Box 3180 || pArg->cMode==MODE_Markdown 3181 ){ 3182 exec_prepared_stmt_columnar(pArg, pStmt); 3183 return; 3184 } 3185 3186 /* perform the first step. this will tell us if we 3187 ** have a result set or not and how wide it is. 3188 */ 3189 rc = sqlite3_step(pStmt); 3190 /* if we have a result set... */ 3191 if( SQLITE_ROW == rc ){ 3192 /* allocate space for col name ptr, value ptr, and type */ 3193 int nCol = sqlite3_column_count(pStmt); 3194 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); 3195 if( !pData ){ 3196 rc = SQLITE_NOMEM; 3197 }else{ 3198 char **azCols = (char **)pData; /* Names of result columns */ 3199 char **azVals = &azCols[nCol]; /* Results */ 3200 int *aiTypes = (int *)&azVals[nCol]; /* Result types */ 3201 int i, x; 3202 assert(sizeof(int) <= sizeof(char *)); 3203 /* save off ptrs to column names */ 3204 for(i=0; i<nCol; i++){ 3205 azCols[i] = (char *)sqlite3_column_name(pStmt, i); 3206 } 3207 do{ 3208 /* extract the data and data types */ 3209 for(i=0; i<nCol; i++){ 3210 aiTypes[i] = x = sqlite3_column_type(pStmt, i); 3211 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ 3212 azVals[i] = ""; 3213 }else{ 3214 azVals[i] = (char*)sqlite3_column_text(pStmt, i); 3215 } 3216 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ 3217 rc = SQLITE_NOMEM; 3218 break; /* from for */ 3219 } 3220 } /* end for */ 3221 3222 /* if data and types extracted successfully... */ 3223 if( SQLITE_ROW == rc ){ 3224 /* call the supplied callback with the result row data */ 3225 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ 3226 rc = SQLITE_ABORT; 3227 }else{ 3228 rc = sqlite3_step(pStmt); 3229 } 3230 } 3231 } while( SQLITE_ROW == rc ); 3232 sqlite3_free(pData); 3233 if( pArg->cMode==MODE_Json ){ 3234 fputs("]\n", pArg->out); 3235 } 3236 } 3237 } 3238} 3239 3240#ifndef SQLITE_OMIT_VIRTUALTABLE 3241/* 3242** This function is called to process SQL if the previous shell command 3243** was ".expert". It passes the SQL in the second argument directly to 3244** the sqlite3expert object. 3245** 3246** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3247** code. In this case, (*pzErr) may be set to point to a buffer containing 3248** an English language error message. It is the responsibility of the 3249** caller to eventually free this buffer using sqlite3_free(). 3250*/ 3251static int expertHandleSQL( 3252 ShellState *pState, 3253 const char *zSql, 3254 char **pzErr 3255){ 3256 assert( pState->expert.pExpert ); 3257 assert( pzErr==0 || *pzErr==0 ); 3258 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); 3259} 3260 3261/* 3262** This function is called either to silently clean up the object 3263** created by the ".expert" command (if bCancel==1), or to generate a 3264** report from it and then clean it up (if bCancel==0). 3265** 3266** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3267** code. In this case, (*pzErr) may be set to point to a buffer containing 3268** an English language error message. It is the responsibility of the 3269** caller to eventually free this buffer using sqlite3_free(). 3270*/ 3271static int expertFinish( 3272 ShellState *pState, 3273 int bCancel, 3274 char **pzErr 3275){ 3276 int rc = SQLITE_OK; 3277 sqlite3expert *p = pState->expert.pExpert; 3278 assert( p ); 3279 assert( bCancel || pzErr==0 || *pzErr==0 ); 3280 if( bCancel==0 ){ 3281 FILE *out = pState->out; 3282 int bVerbose = pState->expert.bVerbose; 3283 3284 rc = sqlite3_expert_analyze(p, pzErr); 3285 if( rc==SQLITE_OK ){ 3286 int nQuery = sqlite3_expert_count(p); 3287 int i; 3288 3289 if( bVerbose ){ 3290 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); 3291 raw_printf(out, "-- Candidates -----------------------------\n"); 3292 raw_printf(out, "%s\n", zCand); 3293 } 3294 for(i=0; i<nQuery; i++){ 3295 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); 3296 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); 3297 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); 3298 if( zIdx==0 ) zIdx = "(no new indexes)\n"; 3299 if( bVerbose ){ 3300 raw_printf(out, "-- Query %d --------------------------------\n",i+1); 3301 raw_printf(out, "%s\n\n", zSql); 3302 } 3303 raw_printf(out, "%s\n", zIdx); 3304 raw_printf(out, "%s\n", zEQP); 3305 } 3306 } 3307 } 3308 sqlite3_expert_destroy(p); 3309 pState->expert.pExpert = 0; 3310 return rc; 3311} 3312 3313/* 3314** Implementation of ".expert" dot command. 3315*/ 3316static int expertDotCommand( 3317 ShellState *pState, /* Current shell tool state */ 3318 char **azArg, /* Array of arguments passed to dot command */ 3319 int nArg /* Number of entries in azArg[] */ 3320){ 3321 int rc = SQLITE_OK; 3322 char *zErr = 0; 3323 int i; 3324 int iSample = 0; 3325 3326 assert( pState->expert.pExpert==0 ); 3327 memset(&pState->expert, 0, sizeof(ExpertInfo)); 3328 3329 for(i=1; rc==SQLITE_OK && i<nArg; i++){ 3330 char *z = azArg[i]; 3331 int n; 3332 if( z[0]=='-' && z[1]=='-' ) z++; 3333 n = strlen30(z); 3334 if( n>=2 && 0==strncmp(z, "-verbose", n) ){ 3335 pState->expert.bVerbose = 1; 3336 } 3337 else if( n>=2 && 0==strncmp(z, "-sample", n) ){ 3338 if( i==(nArg-1) ){ 3339 raw_printf(stderr, "option requires an argument: %s\n", z); 3340 rc = SQLITE_ERROR; 3341 }else{ 3342 iSample = (int)integerValue(azArg[++i]); 3343 if( iSample<0 || iSample>100 ){ 3344 raw_printf(stderr, "value out of range: %s\n", azArg[i]); 3345 rc = SQLITE_ERROR; 3346 } 3347 } 3348 } 3349 else{ 3350 raw_printf(stderr, "unknown option: %s\n", z); 3351 rc = SQLITE_ERROR; 3352 } 3353 } 3354 3355 if( rc==SQLITE_OK ){ 3356 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); 3357 if( pState->expert.pExpert==0 ){ 3358 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr); 3359 rc = SQLITE_ERROR; 3360 }else{ 3361 sqlite3_expert_config( 3362 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample 3363 ); 3364 } 3365 } 3366 3367 return rc; 3368} 3369#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 3370 3371/* 3372** Execute a statement or set of statements. Print 3373** any result rows/columns depending on the current mode 3374** set via the supplied callback. 3375** 3376** This is very similar to SQLite's built-in sqlite3_exec() 3377** function except it takes a slightly different callback 3378** and callback data argument. 3379*/ 3380static int shell_exec( 3381 ShellState *pArg, /* Pointer to ShellState */ 3382 const char *zSql, /* SQL to be evaluated */ 3383 char **pzErrMsg /* Error msg written here */ 3384){ 3385 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ 3386 int rc = SQLITE_OK; /* Return Code */ 3387 int rc2; 3388 const char *zLeftover; /* Tail of unprocessed SQL */ 3389 sqlite3 *db = pArg->db; 3390 3391 if( pzErrMsg ){ 3392 *pzErrMsg = NULL; 3393 } 3394 3395#ifndef SQLITE_OMIT_VIRTUALTABLE 3396 if( pArg->expert.pExpert ){ 3397 rc = expertHandleSQL(pArg, zSql, pzErrMsg); 3398 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); 3399 } 3400#endif 3401 3402 while( zSql[0] && (SQLITE_OK == rc) ){ 3403 static const char *zStmtSql; 3404 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); 3405 if( SQLITE_OK != rc ){ 3406 if( pzErrMsg ){ 3407 *pzErrMsg = save_err_msg(db); 3408 } 3409 }else{ 3410 if( !pStmt ){ 3411 /* this happens for a comment or white-space */ 3412 zSql = zLeftover; 3413 while( IsSpace(zSql[0]) ) zSql++; 3414 continue; 3415 } 3416 zStmtSql = sqlite3_sql(pStmt); 3417 if( zStmtSql==0 ) zStmtSql = ""; 3418 while( IsSpace(zStmtSql[0]) ) zStmtSql++; 3419 3420 /* save off the prepared statment handle and reset row count */ 3421 if( pArg ){ 3422 pArg->pStmt = pStmt; 3423 pArg->cnt = 0; 3424 } 3425 3426 /* echo the sql statement if echo on */ 3427 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ 3428 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); 3429 } 3430 3431 /* Show the EXPLAIN QUERY PLAN if .eqp is on */ 3432 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ 3433 sqlite3_stmt *pExplain; 3434 char *zEQP; 3435 int triggerEQP = 0; 3436 disable_debug_trace_modes(); 3437 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); 3438 if( pArg->autoEQP>=AUTOEQP_trigger ){ 3439 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); 3440 } 3441 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); 3442 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3443 if( rc==SQLITE_OK ){ 3444 while( sqlite3_step(pExplain)==SQLITE_ROW ){ 3445 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); 3446 int iEqpId = sqlite3_column_int(pExplain, 0); 3447 int iParentId = sqlite3_column_int(pExplain, 1); 3448 if( zEQPLine==0 ) zEQPLine = ""; 3449 if( zEQPLine[0]=='-' ) eqp_render(pArg); 3450 eqp_append(pArg, iEqpId, iParentId, zEQPLine); 3451 } 3452 eqp_render(pArg); 3453 } 3454 sqlite3_finalize(pExplain); 3455 sqlite3_free(zEQP); 3456 if( pArg->autoEQP>=AUTOEQP_full ){ 3457 /* Also do an EXPLAIN for ".eqp full" mode */ 3458 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); 3459 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3460 if( rc==SQLITE_OK ){ 3461 pArg->cMode = MODE_Explain; 3462 explain_data_prepare(pArg, pExplain); 3463 exec_prepared_stmt(pArg, pExplain); 3464 explain_data_delete(pArg); 3465 } 3466 sqlite3_finalize(pExplain); 3467 sqlite3_free(zEQP); 3468 } 3469 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ 3470 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); 3471 /* Reprepare pStmt before reactiving trace modes */ 3472 sqlite3_finalize(pStmt); 3473 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 3474 if( pArg ) pArg->pStmt = pStmt; 3475 } 3476 restore_debug_trace_modes(); 3477 } 3478 3479 if( pArg ){ 3480 pArg->cMode = pArg->mode; 3481 if( pArg->autoExplain ){ 3482 if( sqlite3_stmt_isexplain(pStmt)==1 ){ 3483 pArg->cMode = MODE_Explain; 3484 } 3485 if( sqlite3_stmt_isexplain(pStmt)==2 ){ 3486 pArg->cMode = MODE_EQP; 3487 } 3488 } 3489 3490 /* If the shell is currently in ".explain" mode, gather the extra 3491 ** data required to add indents to the output.*/ 3492 if( pArg->cMode==MODE_Explain ){ 3493 explain_data_prepare(pArg, pStmt); 3494 } 3495 } 3496 3497 bind_prepared_stmt(pArg, pStmt); 3498 exec_prepared_stmt(pArg, pStmt); 3499 explain_data_delete(pArg); 3500 eqp_render(pArg); 3501 3502 /* print usage stats if stats on */ 3503 if( pArg && pArg->statsOn ){ 3504 display_stats(db, pArg, 0); 3505 } 3506 3507 /* print loop-counters if required */ 3508 if( pArg && pArg->scanstatsOn ){ 3509 display_scanstats(db, pArg); 3510 } 3511 3512 /* Finalize the statement just executed. If this fails, save a 3513 ** copy of the error message. Otherwise, set zSql to point to the 3514 ** next statement to execute. */ 3515 rc2 = sqlite3_finalize(pStmt); 3516 if( rc!=SQLITE_NOMEM ) rc = rc2; 3517 if( rc==SQLITE_OK ){ 3518 zSql = zLeftover; 3519 while( IsSpace(zSql[0]) ) zSql++; 3520 }else if( pzErrMsg ){ 3521 *pzErrMsg = save_err_msg(db); 3522 } 3523 3524 /* clear saved stmt handle */ 3525 if( pArg ){ 3526 pArg->pStmt = NULL; 3527 } 3528 } 3529 } /* end while */ 3530 3531 return rc; 3532} 3533 3534/* 3535** Release memory previously allocated by tableColumnList(). 3536*/ 3537static void freeColumnList(char **azCol){ 3538 int i; 3539 for(i=1; azCol[i]; i++){ 3540 sqlite3_free(azCol[i]); 3541 } 3542 /* azCol[0] is a static string */ 3543 sqlite3_free(azCol); 3544} 3545 3546/* 3547** Return a list of pointers to strings which are the names of all 3548** columns in table zTab. The memory to hold the names is dynamically 3549** allocated and must be released by the caller using a subsequent call 3550** to freeColumnList(). 3551** 3552** The azCol[0] entry is usually NULL. However, if zTab contains a rowid 3553** value that needs to be preserved, then azCol[0] is filled in with the 3554** name of the rowid column. 3555** 3556** The first regular column in the table is azCol[1]. The list is terminated 3557** by an entry with azCol[i]==0. 3558*/ 3559static char **tableColumnList(ShellState *p, const char *zTab){ 3560 char **azCol = 0; 3561 sqlite3_stmt *pStmt; 3562 char *zSql; 3563 int nCol = 0; 3564 int nAlloc = 0; 3565 int nPK = 0; /* Number of PRIMARY KEY columns seen */ 3566 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ 3567 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); 3568 int rc; 3569 3570 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); 3571 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 3572 sqlite3_free(zSql); 3573 if( rc ) return 0; 3574 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 3575 if( nCol>=nAlloc-2 ){ 3576 nAlloc = nAlloc*2 + nCol + 10; 3577 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); 3578 if( azCol==0 ) shell_out_of_memory(); 3579 } 3580 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); 3581 if( sqlite3_column_int(pStmt, 5) ){ 3582 nPK++; 3583 if( nPK==1 3584 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), 3585 "INTEGER")==0 3586 ){ 3587 isIPK = 1; 3588 }else{ 3589 isIPK = 0; 3590 } 3591 } 3592 } 3593 sqlite3_finalize(pStmt); 3594 if( azCol==0 ) return 0; 3595 azCol[0] = 0; 3596 azCol[nCol+1] = 0; 3597 3598 /* The decision of whether or not a rowid really needs to be preserved 3599 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table 3600 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve 3601 ** rowids on tables where the rowid is inaccessible because there are other 3602 ** columns in the table named "rowid", "_rowid_", and "oid". 3603 */ 3604 if( preserveRowid && isIPK ){ 3605 /* If a single PRIMARY KEY column with type INTEGER was seen, then it 3606 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID 3607 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are 3608 ** ROWID aliases. To distinguish these cases, check to see if 3609 ** there is a "pk" entry in "PRAGMA index_list". There will be 3610 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. 3611 */ 3612 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" 3613 " WHERE origin='pk'", zTab); 3614 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 3615 sqlite3_free(zSql); 3616 if( rc ){ 3617 freeColumnList(azCol); 3618 return 0; 3619 } 3620 rc = sqlite3_step(pStmt); 3621 sqlite3_finalize(pStmt); 3622 preserveRowid = rc==SQLITE_ROW; 3623 } 3624 if( preserveRowid ){ 3625 /* Only preserve the rowid if we can find a name to use for the 3626 ** rowid */ 3627 static char *azRowid[] = { "rowid", "_rowid_", "oid" }; 3628 int i, j; 3629 for(j=0; j<3; j++){ 3630 for(i=1; i<=nCol; i++){ 3631 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; 3632 } 3633 if( i>nCol ){ 3634 /* At this point, we know that azRowid[j] is not the name of any 3635 ** ordinary column in the table. Verify that azRowid[j] is a valid 3636 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID 3637 ** tables will fail this last check */ 3638 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); 3639 if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; 3640 break; 3641 } 3642 } 3643 } 3644 return azCol; 3645} 3646 3647/* 3648** Toggle the reverse_unordered_selects setting. 3649*/ 3650static void toggleSelectOrder(sqlite3 *db){ 3651 sqlite3_stmt *pStmt = 0; 3652 int iSetting = 0; 3653 char zStmt[100]; 3654 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); 3655 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 3656 iSetting = sqlite3_column_int(pStmt, 0); 3657 } 3658 sqlite3_finalize(pStmt); 3659 sqlite3_snprintf(sizeof(zStmt), zStmt, 3660 "PRAGMA reverse_unordered_selects(%d)", !iSetting); 3661 sqlite3_exec(db, zStmt, 0, 0, 0); 3662} 3663 3664/* 3665** This is a different callback routine used for dumping the database. 3666** Each row received by this callback consists of a table name, 3667** the table type ("index" or "table") and SQL to create the table. 3668** This routine should print text sufficient to recreate the table. 3669*/ 3670static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ 3671 int rc; 3672 const char *zTable; 3673 const char *zType; 3674 const char *zSql; 3675 ShellState *p = (ShellState *)pArg; 3676 3677 UNUSED_PARAMETER(azNotUsed); 3678 if( nArg!=3 || azArg==0 ) return 0; 3679 zTable = azArg[0]; 3680 zType = azArg[1]; 3681 zSql = azArg[2]; 3682 3683 if( strcmp(zTable, "sqlite_sequence")==0 ){ 3684 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); 3685 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ 3686 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 3687 }else if( strncmp(zTable, "sqlite_", 7)==0 ){ 3688 return 0; 3689 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ 3690 char *zIns; 3691 if( !p->writableSchema ){ 3692 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); 3693 p->writableSchema = 1; 3694 } 3695 zIns = sqlite3_mprintf( 3696 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" 3697 "VALUES('table','%q','%q',0,'%q');", 3698 zTable, zTable, zSql); 3699 utf8_printf(p->out, "%s\n", zIns); 3700 sqlite3_free(zIns); 3701 return 0; 3702 }else{ 3703 printSchemaLine(p->out, zSql, ";\n"); 3704 } 3705 3706 if( strcmp(zType, "table")==0 ){ 3707 ShellText sSelect; 3708 ShellText sTable; 3709 char **azCol; 3710 int i; 3711 char *savedDestTable; 3712 int savedMode; 3713 3714 azCol = tableColumnList(p, zTable); 3715 if( azCol==0 ){ 3716 p->nErr++; 3717 return 0; 3718 } 3719 3720 /* Always quote the table name, even if it appears to be pure ascii, 3721 ** in case it is a keyword. Ex: INSERT INTO "table" ... */ 3722 initText(&sTable); 3723 appendText(&sTable, zTable, quoteChar(zTable)); 3724 /* If preserving the rowid, add a column list after the table name. 3725 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" 3726 ** instead of the usual "INSERT INTO tab VALUES(...)". 3727 */ 3728 if( azCol[0] ){ 3729 appendText(&sTable, "(", 0); 3730 appendText(&sTable, azCol[0], 0); 3731 for(i=1; azCol[i]; i++){ 3732 appendText(&sTable, ",", 0); 3733 appendText(&sTable, azCol[i], quoteChar(azCol[i])); 3734 } 3735 appendText(&sTable, ")", 0); 3736 } 3737 3738 /* Build an appropriate SELECT statement */ 3739 initText(&sSelect); 3740 appendText(&sSelect, "SELECT ", 0); 3741 if( azCol[0] ){ 3742 appendText(&sSelect, azCol[0], 0); 3743 appendText(&sSelect, ",", 0); 3744 } 3745 for(i=1; azCol[i]; i++){ 3746 appendText(&sSelect, azCol[i], quoteChar(azCol[i])); 3747 if( azCol[i+1] ){ 3748 appendText(&sSelect, ",", 0); 3749 } 3750 } 3751 freeColumnList(azCol); 3752 appendText(&sSelect, " FROM ", 0); 3753 appendText(&sSelect, zTable, quoteChar(zTable)); 3754 3755 savedDestTable = p->zDestTable; 3756 savedMode = p->mode; 3757 p->zDestTable = sTable.z; 3758 p->mode = p->cMode = MODE_Insert; 3759 rc = shell_exec(p, sSelect.z, 0); 3760 if( (rc&0xff)==SQLITE_CORRUPT ){ 3761 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 3762 toggleSelectOrder(p->db); 3763 shell_exec(p, sSelect.z, 0); 3764 toggleSelectOrder(p->db); 3765 } 3766 p->zDestTable = savedDestTable; 3767 p->mode = savedMode; 3768 freeText(&sTable); 3769 freeText(&sSelect); 3770 if( rc ) p->nErr++; 3771 } 3772 return 0; 3773} 3774 3775/* 3776** Run zQuery. Use dump_callback() as the callback routine so that 3777** the contents of the query are output as SQL statements. 3778** 3779** If we get a SQLITE_CORRUPT error, rerun the query after appending 3780** "ORDER BY rowid DESC" to the end. 3781*/ 3782static int run_schema_dump_query( 3783 ShellState *p, 3784 const char *zQuery 3785){ 3786 int rc; 3787 char *zErr = 0; 3788 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); 3789 if( rc==SQLITE_CORRUPT ){ 3790 char *zQ2; 3791 int len = strlen30(zQuery); 3792 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 3793 if( zErr ){ 3794 utf8_printf(p->out, "/****** %s ******/\n", zErr); 3795 sqlite3_free(zErr); 3796 zErr = 0; 3797 } 3798 zQ2 = malloc( len+100 ); 3799 if( zQ2==0 ) return rc; 3800 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); 3801 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); 3802 if( rc ){ 3803 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); 3804 }else{ 3805 rc = SQLITE_CORRUPT; 3806 } 3807 sqlite3_free(zErr); 3808 free(zQ2); 3809 } 3810 return rc; 3811} 3812 3813/* 3814** Text of help messages. 3815** 3816** The help text for each individual command begins with a line that starts 3817** with ".". Subsequent lines are supplimental information. 3818** 3819** There must be two or more spaces between the end of the command and the 3820** start of the description of what that command does. 3821*/ 3822static const char *(azHelp[]) = { 3823#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 3824 ".archive ... Manage SQL archives", 3825 " Each command must have exactly one of the following options:", 3826 " -c, --create Create a new archive", 3827 " -u, --update Add or update files with changed mtime", 3828 " -i, --insert Like -u but always add even if unchanged", 3829 " -t, --list List contents of archive", 3830 " -x, --extract Extract files from archive", 3831 " Optional arguments:", 3832 " -v, --verbose Print each filename as it is processed", 3833 " -f FILE, --file FILE Use archive FILE (default is current db)", 3834 " -a FILE, --append FILE Open FILE using the apndvfs VFS", 3835 " -C DIR, --directory DIR Read/extract files from directory DIR", 3836 " -n, --dryrun Show the SQL that would have occurred", 3837 " Examples:", 3838 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", 3839 " .ar -tf ARCHIVE # List members of ARCHIVE", 3840 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", 3841 " See also:", 3842 " http://sqlite.org/cli.html#sqlar_archive_support", 3843#endif 3844#ifndef SQLITE_OMIT_AUTHORIZATION 3845 ".auth ON|OFF Show authorizer callbacks", 3846#endif 3847 ".backup ?DB? FILE Backup DB (default \"main\") to FILE", 3848 " --append Use the appendvfs", 3849 " --async Write to FILE without journal and fsync()", 3850 ".bail on|off Stop after hitting an error. Default OFF", 3851 ".binary on|off Turn binary output on or off. Default OFF", 3852 ".cd DIRECTORY Change the working directory to DIRECTORY", 3853 ".changes on|off Show number of rows changed by SQL", 3854 ".check GLOB Fail if output since .testcase does not match", 3855 ".clone NEWDB Clone data into NEWDB from the existing database", 3856 ".databases List names and files of attached databases", 3857 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", 3858 ".dbinfo ?DB? Show status information about the database", 3859 ".dump ?TABLE? Render database content as SQL", 3860 " Options:", 3861 " --preserve-rowids Include ROWID values in the output", 3862 " --newlines Allow unescaped newline characters in output", 3863 " TABLE is a LIKE pattern for the tables to dump", 3864 " Additional LIKE patterns can be given in subsequent arguments", 3865 ".echo on|off Turn command echo on or off", 3866 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", 3867 " Other Modes:", 3868#ifdef SQLITE_DEBUG 3869 " test Show raw EXPLAIN QUERY PLAN output", 3870 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", 3871#endif 3872 " trigger Like \"full\" but also show trigger bytecode", 3873 ".excel Display the output of next command in spreadsheet", 3874 " --bom Put a UTF8 byte-order mark on intermediate file", 3875 ".exit ?CODE? Exit this program with return-code CODE", 3876 ".expert EXPERIMENTAL. Suggest indexes for queries", 3877 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", 3878 ".filectrl CMD ... Run various sqlite3_file_control() operations", 3879 " --schema SCHEMA Use SCHEMA instead of \"main\"", 3880 " --help Show CMD details", 3881 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", 3882 ".headers on|off Turn display of headers on or off", 3883 ".help ?-all? ?PATTERN? Show help text for PATTERN", 3884 ".import FILE TABLE Import data from FILE into TABLE", 3885 " Options:", 3886 " --ascii Use \\037 and \\036 as column and row separators", 3887 " --csv Use , and \\n as column and row separators", 3888 " --skip N Skip the first N rows of input", 3889 " -v \"Verbose\" - increase auxiliary output", 3890 " Notes:", 3891 " * If TABLE does not exist, it is created. The first row of input", 3892 " determines the column names.", 3893 " * If neither --csv or --ascii are used, the input mode is derived", 3894 " from the \".mode\" output mode", 3895 " * If FILE begins with \"|\" then it is a command that generates the", 3896 " input text.", 3897#ifndef SQLITE_OMIT_TEST_CONTROL 3898 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", 3899#endif 3900 ".indexes ?TABLE? Show names of indexes", 3901 " If TABLE is specified, only show indexes for", 3902 " tables matching TABLE using the LIKE operator.", 3903#ifdef SQLITE_ENABLE_IOTRACE 3904 ".iotrace FILE Enable I/O diagnostic logging to FILE", 3905#endif 3906 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", 3907 ".lint OPTIONS Report potential schema issues.", 3908 " Options:", 3909 " fkey-indexes Find missing foreign key indexes", 3910#ifndef SQLITE_OMIT_LOAD_EXTENSION 3911 ".load FILE ?ENTRY? Load an extension library", 3912#endif 3913 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", 3914 ".mode MODE ?TABLE? Set output mode", 3915 " MODE is one of:", 3916 " ascii Columns/rows delimited by 0x1F and 0x1E", 3917 " box Tables using unicode box-drawing characters", 3918 " csv Comma-separated values", 3919 " column Output in columns. (See .width)", 3920 " html HTML <table> code", 3921 " insert SQL insert statements for TABLE", 3922 " json Results in a JSON array", 3923 " line One value per line", 3924 " list Values delimited by \"|\"", 3925 " markdown Markdown table format", 3926 " quote Escape answers as for SQL", 3927 " table ASCII-art table", 3928 " tabs Tab-separated values", 3929 " tcl TCL list elements", 3930 ".nullvalue STRING Use STRING in place of NULL values", 3931 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", 3932 " If FILE begins with '|' then open as a pipe", 3933 " --bom Put a UTF8 byte-order mark at the beginning", 3934 " -e Send output to the system text editor", 3935 " -x Send output as CSV to a spreadsheet (same as \".excel\")", 3936#ifdef SQLITE_DEBUG 3937 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation", 3938#endif 3939 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", 3940 " Options:", 3941 " --append Use appendvfs to append database to the end of FILE", 3942#ifdef SQLITE_ENABLE_DESERIALIZE 3943 " --deserialize Load into memory useing sqlite3_deserialize()", 3944 " --hexdb Load the output of \"dbtotxt\" as an in-memory db", 3945 " --maxsize N Maximum size for --hexdb or --deserialized database", 3946#endif 3947 " --new Initialize FILE to an empty database", 3948 " --nofollow Do not follow symbolic links", 3949 " --readonly Open FILE readonly", 3950 " --zip FILE is a ZIP archive", 3951 ".output ?FILE? Send output to FILE or stdout if FILE is omitted", 3952 " If FILE begins with '|' then open it as a pipe.", 3953 " Options:", 3954 " --bom Prefix output with a UTF8 byte-order mark", 3955 " -e Send output to the system text editor", 3956 " -x Send output as CSV to a spreadsheet", 3957 ".parameter CMD ... Manage SQL parameter bindings", 3958 " clear Erase all bindings", 3959 " init Initialize the TEMP table that holds bindings", 3960 " list List the current parameter bindings", 3961 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", 3962 " PARAMETER should start with one of: $ : @ ?", 3963 " unset PARAMETER Remove PARAMETER from the binding table", 3964 ".print STRING... Print literal STRING", 3965#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 3966 ".progress N Invoke progress handler after every N opcodes", 3967 " --limit N Interrupt after N progress callbacks", 3968 " --once Do no more than one progress interrupt", 3969 " --quiet|-q No output except at interrupts", 3970 " --reset Reset the count for each input and interrupt", 3971#endif 3972 ".prompt MAIN CONTINUE Replace the standard prompts", 3973 ".quit Exit this program", 3974 ".read FILE Read input from FILE", 3975#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 3976 ".recover Recover as much data as possible from corrupt db.", 3977 " --freelist-corrupt Assume the freelist is corrupt", 3978 " --recovery-db NAME Store recovery metadata in database file NAME", 3979 " --lost-and-found TABLE Alternative name for the lost-and-found table", 3980 " --no-rowids Do not attempt to recover rowid values", 3981 " that are not also INTEGER PRIMARY KEYs", 3982#endif 3983 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", 3984 ".save FILE Write in-memory database into FILE", 3985 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", 3986 ".schema ?PATTERN? Show the CREATE statements matching PATTERN", 3987 " Options:", 3988 " --indent Try to pretty-print the schema", 3989 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", 3990 " Options:", 3991 " --init Create a new SELFTEST table", 3992 " -v Verbose output", 3993 ".separator COL ?ROW? Change the column and row separators", 3994#if defined(SQLITE_ENABLE_SESSION) 3995 ".session ?NAME? CMD ... Create or control sessions", 3996 " Subcommands:", 3997 " attach TABLE Attach TABLE", 3998 " changeset FILE Write a changeset into FILE", 3999 " close Close one session", 4000 " enable ?BOOLEAN? Set or query the enable bit", 4001 " filter GLOB... Reject tables matching GLOBs", 4002 " indirect ?BOOLEAN? Mark or query the indirect status", 4003 " isempty Query whether the session is empty", 4004 " list List currently open session names", 4005 " open DB NAME Open a new session on DB", 4006 " patchset FILE Write a patchset into FILE", 4007 " If ?NAME? is omitted, the first defined session is used.", 4008#endif 4009 ".sha3sum ... Compute a SHA3 hash of database content", 4010 " Options:", 4011 " --schema Also hash the sqlite_master table", 4012 " --sha3-224 Use the sha3-224 algorithm", 4013 " --sha3-256 Use the sha3-256 algorithm (default)", 4014 " --sha3-384 Use the sha3-384 algorithm", 4015 " --sha3-512 Use the sha3-512 algorithm", 4016 " Any other argument is a LIKE pattern for tables to hash", 4017#ifndef SQLITE_NOHAVE_SYSTEM 4018 ".shell CMD ARGS... Run CMD ARGS... in a system shell", 4019#endif 4020 ".show Show the current values for various settings", 4021 ".stats ?on|off? Show stats or turn stats on or off", 4022#ifndef SQLITE_NOHAVE_SYSTEM 4023 ".system CMD ARGS... Run CMD ARGS... in a system shell", 4024#endif 4025 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", 4026 ".testcase NAME Begin redirecting output to 'testcase-out.txt'", 4027 ".testctrl CMD ... Run various sqlite3_test_control() operations", 4028 " Run \".testctrl\" with no arguments for details", 4029 ".timeout MS Try opening locked tables for MS milliseconds", 4030 ".timer on|off Turn SQL timer on or off", 4031#ifndef SQLITE_OMIT_TRACE 4032 ".trace ?OPTIONS? Output each SQL statement as it is run", 4033 " FILE Send output to FILE", 4034 " stdout Send output to stdout", 4035 " stderr Send output to stderr", 4036 " off Disable tracing", 4037 " --expanded Expand query parameters", 4038#ifdef SQLITE_ENABLE_NORMALIZE 4039 " --normalized Normal the SQL statements", 4040#endif 4041 " --plain Show SQL as it is input", 4042 " --stmt Trace statement execution (SQLITE_TRACE_STMT)", 4043 " --profile Profile statements (SQLITE_TRACE_PROFILE)", 4044 " --row Trace each row (SQLITE_TRACE_ROW)", 4045 " --close Trace connection close (SQLITE_TRACE_CLOSE)", 4046#endif /* SQLITE_OMIT_TRACE */ 4047#ifdef SQLITE_DEBUG 4048 ".unmodule NAME ... Unregister virtual table modules", 4049 " --allexcept Unregister everything except those named", 4050#endif 4051 ".vfsinfo ?AUX? Information about the top-level VFS", 4052 ".vfslist List all available VFSes", 4053 ".vfsname ?AUX? Print the name of the VFS stack", 4054 ".width NUM1 NUM2 ... Set minimum column widths for columnar output", 4055 " Negative values right-justify", 4056}; 4057 4058/* 4059** Output help text. 4060** 4061** zPattern describes the set of commands for which help text is provided. 4062** If zPattern is NULL, then show all commands, but only give a one-line 4063** description of each. 4064** 4065** Return the number of matches. 4066*/ 4067static int showHelp(FILE *out, const char *zPattern){ 4068 int i = 0; 4069 int j = 0; 4070 int n = 0; 4071 char *zPat; 4072 if( zPattern==0 4073 || zPattern[0]=='0' 4074 || strcmp(zPattern,"-a")==0 4075 || strcmp(zPattern,"-all")==0 4076 || strcmp(zPattern,"--all")==0 4077 ){ 4078 /* Show all commands, but only one line per command */ 4079 if( zPattern==0 ) zPattern = ""; 4080 for(i=0; i<ArraySize(azHelp); i++){ 4081 if( azHelp[i][0]=='.' || zPattern[0] ){ 4082 utf8_printf(out, "%s\n", azHelp[i]); 4083 n++; 4084 } 4085 } 4086 }else{ 4087 /* Look for commands that for which zPattern is an exact prefix */ 4088 zPat = sqlite3_mprintf(".%s*", zPattern); 4089 for(i=0; i<ArraySize(azHelp); i++){ 4090 if( sqlite3_strglob(zPat, azHelp[i])==0 ){ 4091 utf8_printf(out, "%s\n", azHelp[i]); 4092 j = i+1; 4093 n++; 4094 } 4095 } 4096 sqlite3_free(zPat); 4097 if( n ){ 4098 if( n==1 ){ 4099 /* when zPattern is a prefix of exactly one command, then include the 4100 ** details of that command, which should begin at offset j */ 4101 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ 4102 utf8_printf(out, "%s\n", azHelp[j]); 4103 j++; 4104 } 4105 } 4106 return n; 4107 } 4108 /* Look for commands that contain zPattern anywhere. Show the complete 4109 ** text of all commands that match. */ 4110 zPat = sqlite3_mprintf("%%%s%%", zPattern); 4111 for(i=0; i<ArraySize(azHelp); i++){ 4112 if( azHelp[i][0]=='.' ) j = i; 4113 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ 4114 utf8_printf(out, "%s\n", azHelp[j]); 4115 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ 4116 j++; 4117 utf8_printf(out, "%s\n", azHelp[j]); 4118 } 4119 i = j; 4120 n++; 4121 } 4122 } 4123 sqlite3_free(zPat); 4124 } 4125 return n; 4126} 4127 4128/* Forward reference */ 4129static int process_input(ShellState *p); 4130 4131/* 4132** Read the content of file zName into memory obtained from sqlite3_malloc64() 4133** and return a pointer to the buffer. The caller is responsible for freeing 4134** the memory. 4135** 4136** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes 4137** read. 4138** 4139** For convenience, a nul-terminator byte is always appended to the data read 4140** from the file before the buffer is returned. This byte is not included in 4141** the final value of (*pnByte), if applicable. 4142** 4143** NULL is returned if any error is encountered. The final value of *pnByte 4144** is undefined in this case. 4145*/ 4146static char *readFile(const char *zName, int *pnByte){ 4147 FILE *in = fopen(zName, "rb"); 4148 long nIn; 4149 size_t nRead; 4150 char *pBuf; 4151 if( in==0 ) return 0; 4152 fseek(in, 0, SEEK_END); 4153 nIn = ftell(in); 4154 rewind(in); 4155 pBuf = sqlite3_malloc64( nIn+1 ); 4156 if( pBuf==0 ){ fclose(in); return 0; } 4157 nRead = fread(pBuf, nIn, 1, in); 4158 fclose(in); 4159 if( nRead!=1 ){ 4160 sqlite3_free(pBuf); 4161 return 0; 4162 } 4163 pBuf[nIn] = 0; 4164 if( pnByte ) *pnByte = nIn; 4165 return pBuf; 4166} 4167 4168#if defined(SQLITE_ENABLE_SESSION) 4169/* 4170** Close a single OpenSession object and release all of its associated 4171** resources. 4172*/ 4173static void session_close(OpenSession *pSession){ 4174 int i; 4175 sqlite3session_delete(pSession->p); 4176 sqlite3_free(pSession->zName); 4177 for(i=0; i<pSession->nFilter; i++){ 4178 sqlite3_free(pSession->azFilter[i]); 4179 } 4180 sqlite3_free(pSession->azFilter); 4181 memset(pSession, 0, sizeof(OpenSession)); 4182} 4183#endif 4184 4185/* 4186** Close all OpenSession objects and release all associated resources. 4187*/ 4188#if defined(SQLITE_ENABLE_SESSION) 4189static void session_close_all(ShellState *p){ 4190 int i; 4191 for(i=0; i<p->nSession; i++){ 4192 session_close(&p->aSession[i]); 4193 } 4194 p->nSession = 0; 4195} 4196#else 4197# define session_close_all(X) 4198#endif 4199 4200/* 4201** Implementation of the xFilter function for an open session. Omit 4202** any tables named by ".session filter" but let all other table through. 4203*/ 4204#if defined(SQLITE_ENABLE_SESSION) 4205static int session_filter(void *pCtx, const char *zTab){ 4206 OpenSession *pSession = (OpenSession*)pCtx; 4207 int i; 4208 for(i=0; i<pSession->nFilter; i++){ 4209 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; 4210 } 4211 return 1; 4212} 4213#endif 4214 4215/* 4216** Try to deduce the type of file for zName based on its content. Return 4217** one of the SHELL_OPEN_* constants. 4218** 4219** If the file does not exist or is empty but its name looks like a ZIP 4220** archive and the dfltZip flag is true, then assume it is a ZIP archive. 4221** Otherwise, assume an ordinary database regardless of the filename if 4222** the type cannot be determined from content. 4223*/ 4224int deduceDatabaseType(const char *zName, int dfltZip){ 4225 FILE *f = fopen(zName, "rb"); 4226 size_t n; 4227 int rc = SHELL_OPEN_UNSPEC; 4228 char zBuf[100]; 4229 if( f==0 ){ 4230 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4231 return SHELL_OPEN_ZIPFILE; 4232 }else{ 4233 return SHELL_OPEN_NORMAL; 4234 } 4235 } 4236 n = fread(zBuf, 16, 1, f); 4237 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ 4238 fclose(f); 4239 return SHELL_OPEN_NORMAL; 4240 } 4241 fseek(f, -25, SEEK_END); 4242 n = fread(zBuf, 25, 1, f); 4243 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ 4244 rc = SHELL_OPEN_APPENDVFS; 4245 }else{ 4246 fseek(f, -22, SEEK_END); 4247 n = fread(zBuf, 22, 1, f); 4248 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 4249 && zBuf[3]==0x06 ){ 4250 rc = SHELL_OPEN_ZIPFILE; 4251 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4252 rc = SHELL_OPEN_ZIPFILE; 4253 } 4254 } 4255 fclose(f); 4256 return rc; 4257} 4258 4259#ifdef SQLITE_ENABLE_DESERIALIZE 4260/* 4261** Reconstruct an in-memory database using the output from the "dbtotxt" 4262** program. Read content from the file in p->zDbFilename. If p->zDbFilename 4263** is 0, then read from standard input. 4264*/ 4265static unsigned char *readHexDb(ShellState *p, int *pnData){ 4266 unsigned char *a = 0; 4267 int nLine; 4268 int n = 0; 4269 int pgsz = 0; 4270 int iOffset = 0; 4271 int j, k; 4272 int rc; 4273 FILE *in; 4274 unsigned int x[16]; 4275 char zLine[1000]; 4276 if( p->zDbFilename ){ 4277 in = fopen(p->zDbFilename, "r"); 4278 if( in==0 ){ 4279 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename); 4280 return 0; 4281 } 4282 nLine = 0; 4283 }else{ 4284 in = p->in; 4285 nLine = p->lineno; 4286 if( in==0 ) in = stdin; 4287 } 4288 *pnData = 0; 4289 nLine++; 4290 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; 4291 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); 4292 if( rc!=2 ) goto readHexDb_error; 4293 if( n<0 ) goto readHexDb_error; 4294 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; 4295 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ 4296 a = sqlite3_malloc( n ? n : 1 ); 4297 if( a==0 ){ 4298 utf8_printf(stderr, "Out of memory!\n"); 4299 goto readHexDb_error; 4300 } 4301 memset(a, 0, n); 4302 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ 4303 utf8_printf(stderr, "invalid pagesize\n"); 4304 goto readHexDb_error; 4305 } 4306 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ 4307 rc = sscanf(zLine, "| page %d offset %d", &j, &k); 4308 if( rc==2 ){ 4309 iOffset = k; 4310 continue; 4311 } 4312 if( strncmp(zLine, "| end ", 6)==0 ){ 4313 break; 4314 } 4315 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 4316 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], 4317 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); 4318 if( rc==17 ){ 4319 k = iOffset+j; 4320 if( k+16<=n ){ 4321 int ii; 4322 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; 4323 } 4324 } 4325 } 4326 *pnData = n; 4327 if( in!=p->in ){ 4328 fclose(in); 4329 }else{ 4330 p->lineno = nLine; 4331 } 4332 return a; 4333 4334readHexDb_error: 4335 if( in!=p->in ){ 4336 fclose(in); 4337 }else{ 4338 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ 4339 nLine++; 4340 if(strncmp(zLine, "| end ", 6)==0 ) break; 4341 } 4342 p->lineno = nLine; 4343 } 4344 sqlite3_free(a); 4345 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); 4346 return 0; 4347} 4348#endif /* SQLITE_ENABLE_DESERIALIZE */ 4349 4350/* 4351** Scalar function "shell_int32". The first argument to this function 4352** must be a blob. The second a non-negative integer. This function 4353** reads and returns a 32-bit big-endian integer from byte 4354** offset (4*<arg2>) of the blob. 4355*/ 4356static void shellInt32( 4357 sqlite3_context *context, 4358 int argc, 4359 sqlite3_value **argv 4360){ 4361 const unsigned char *pBlob; 4362 int nBlob; 4363 int iInt; 4364 4365 UNUSED_PARAMETER(argc); 4366 nBlob = sqlite3_value_bytes(argv[0]); 4367 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); 4368 iInt = sqlite3_value_int(argv[1]); 4369 4370 if( iInt>=0 && (iInt+1)*4<=nBlob ){ 4371 const unsigned char *a = &pBlob[iInt*4]; 4372 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) 4373 + ((sqlite3_int64)a[1]<<16) 4374 + ((sqlite3_int64)a[2]<< 8) 4375 + ((sqlite3_int64)a[3]<< 0); 4376 sqlite3_result_int64(context, iVal); 4377 } 4378} 4379 4380/* 4381** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, 4382** using "..." with internal double-quote characters doubled. 4383*/ 4384static void shellIdQuote( 4385 sqlite3_context *context, 4386 int argc, 4387 sqlite3_value **argv 4388){ 4389 const char *zName = (const char*)sqlite3_value_text(argv[0]); 4390 UNUSED_PARAMETER(argc); 4391 if( zName ){ 4392 char *z = sqlite3_mprintf("\"%w\"", zName); 4393 sqlite3_result_text(context, z, -1, sqlite3_free); 4394 } 4395} 4396 4397/* 4398** Scalar function "shell_escape_crnl" used by the .recover command. 4399** The argument passed to this function is the output of built-in 4400** function quote(). If the first character of the input is "'", 4401** indicating that the value passed to quote() was a text value, 4402** then this function searches the input for "\n" and "\r" characters 4403** and adds a wrapper similar to the following: 4404** 4405** replace(replace(<input>, '\n', char(10), '\r', char(13)); 4406** 4407** Or, if the first character of the input is not "'", then a copy 4408** of the input is returned. 4409*/ 4410static void shellEscapeCrnl( 4411 sqlite3_context *context, 4412 int argc, 4413 sqlite3_value **argv 4414){ 4415 const char *zText = (const char*)sqlite3_value_text(argv[0]); 4416 UNUSED_PARAMETER(argc); 4417 if( zText[0]=='\'' ){ 4418 int nText = sqlite3_value_bytes(argv[0]); 4419 int i; 4420 char zBuf1[20]; 4421 char zBuf2[20]; 4422 const char *zNL = 0; 4423 const char *zCR = 0; 4424 int nCR = 0; 4425 int nNL = 0; 4426 4427 for(i=0; zText[i]; i++){ 4428 if( zNL==0 && zText[i]=='\n' ){ 4429 zNL = unused_string(zText, "\\n", "\\012", zBuf1); 4430 nNL = (int)strlen(zNL); 4431 } 4432 if( zCR==0 && zText[i]=='\r' ){ 4433 zCR = unused_string(zText, "\\r", "\\015", zBuf2); 4434 nCR = (int)strlen(zCR); 4435 } 4436 } 4437 4438 if( zNL || zCR ){ 4439 int iOut = 0; 4440 i64 nMax = (nNL > nCR) ? nNL : nCR; 4441 i64 nAlloc = nMax * nText + (nMax+64)*2; 4442 char *zOut = (char*)sqlite3_malloc64(nAlloc); 4443 if( zOut==0 ){ 4444 sqlite3_result_error_nomem(context); 4445 return; 4446 } 4447 4448 if( zNL && zCR ){ 4449 memcpy(&zOut[iOut], "replace(replace(", 16); 4450 iOut += 16; 4451 }else{ 4452 memcpy(&zOut[iOut], "replace(", 8); 4453 iOut += 8; 4454 } 4455 for(i=0; zText[i]; i++){ 4456 if( zText[i]=='\n' ){ 4457 memcpy(&zOut[iOut], zNL, nNL); 4458 iOut += nNL; 4459 }else if( zText[i]=='\r' ){ 4460 memcpy(&zOut[iOut], zCR, nCR); 4461 iOut += nCR; 4462 }else{ 4463 zOut[iOut] = zText[i]; 4464 iOut++; 4465 } 4466 } 4467 4468 if( zNL ){ 4469 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 4470 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; 4471 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; 4472 } 4473 if( zCR ){ 4474 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 4475 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; 4476 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; 4477 } 4478 4479 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); 4480 sqlite3_free(zOut); 4481 return; 4482 } 4483 } 4484 4485 sqlite3_result_value(context, argv[0]); 4486} 4487 4488/* Flags for open_db(). 4489** 4490** The default behavior of open_db() is to exit(1) if the database fails to 4491** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error 4492** but still returns without calling exit. 4493** 4494** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a 4495** ZIP archive if the file does not exist or is empty and its name matches 4496** the *.zip pattern. 4497*/ 4498#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ 4499#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ 4500 4501/* 4502** Make sure the database is open. If it is not, then open it. If 4503** the database fails to open, print an error message and exit. 4504*/ 4505static void open_db(ShellState *p, int openFlags){ 4506 if( p->db==0 ){ 4507 if( p->openMode==SHELL_OPEN_UNSPEC ){ 4508 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){ 4509 p->openMode = SHELL_OPEN_NORMAL; 4510 }else{ 4511 p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 4512 (openFlags & OPEN_DB_ZIPFILE)!=0); 4513 } 4514 } 4515 switch( p->openMode ){ 4516 case SHELL_OPEN_APPENDVFS: { 4517 sqlite3_open_v2(p->zDbFilename, &p->db, 4518 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); 4519 break; 4520 } 4521 case SHELL_OPEN_HEXDB: 4522 case SHELL_OPEN_DESERIALIZE: { 4523 sqlite3_open(0, &p->db); 4524 break; 4525 } 4526 case SHELL_OPEN_ZIPFILE: { 4527 sqlite3_open(":memory:", &p->db); 4528 break; 4529 } 4530 case SHELL_OPEN_READONLY: { 4531 sqlite3_open_v2(p->zDbFilename, &p->db, 4532 SQLITE_OPEN_READONLY|p->openFlags, 0); 4533 break; 4534 } 4535 case SHELL_OPEN_UNSPEC: 4536 case SHELL_OPEN_NORMAL: { 4537 sqlite3_open_v2(p->zDbFilename, &p->db, 4538 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); 4539 break; 4540 } 4541 } 4542 globalDb = p->db; 4543 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ 4544 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", 4545 p->zDbFilename, sqlite3_errmsg(p->db)); 4546 if( openFlags & OPEN_DB_KEEPALIVE ){ 4547 sqlite3_open(":memory:", &p->db); 4548 return; 4549 } 4550 exit(1); 4551 } 4552#ifndef SQLITE_OMIT_LOAD_EXTENSION 4553 sqlite3_enable_load_extension(p->db, 1); 4554#endif 4555 sqlite3_fileio_init(p->db, 0, 0); 4556 sqlite3_shathree_init(p->db, 0, 0); 4557 sqlite3_completion_init(p->db, 0, 0); 4558 sqlite3_uint_init(p->db, 0, 0); 4559#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 4560 sqlite3_dbdata_init(p->db, 0, 0); 4561#endif 4562#ifdef SQLITE_HAVE_ZLIB 4563 sqlite3_zipfile_init(p->db, 0, 0); 4564 sqlite3_sqlar_init(p->db, 0, 0); 4565#endif 4566 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, 4567 shellAddSchemaName, 0, 0); 4568 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, 4569 shellModuleSchema, 0, 0); 4570 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, 4571 shellPutsFunc, 0, 0); 4572 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, 4573 shellEscapeCrnl, 0, 0); 4574 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, 4575 shellInt32, 0, 0); 4576 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, 4577 shellIdQuote, 0, 0); 4578#ifndef SQLITE_NOHAVE_SYSTEM 4579 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, 4580 editFunc, 0, 0); 4581 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, 4582 editFunc, 0, 0); 4583#endif 4584 if( p->openMode==SHELL_OPEN_ZIPFILE ){ 4585 char *zSql = sqlite3_mprintf( 4586 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); 4587 sqlite3_exec(p->db, zSql, 0, 0, 0); 4588 sqlite3_free(zSql); 4589 } 4590#ifdef SQLITE_ENABLE_DESERIALIZE 4591 else 4592 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ 4593 int rc; 4594 int nData = 0; 4595 unsigned char *aData; 4596 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ 4597 aData = (unsigned char*)readFile(p->zDbFilename, &nData); 4598 }else{ 4599 aData = readHexDb(p, &nData); 4600 if( aData==0 ){ 4601 return; 4602 } 4603 } 4604 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, 4605 SQLITE_DESERIALIZE_RESIZEABLE | 4606 SQLITE_DESERIALIZE_FREEONCLOSE); 4607 if( rc ){ 4608 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); 4609 } 4610 if( p->szMax>0 ){ 4611 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); 4612 } 4613 } 4614#endif 4615 } 4616} 4617 4618/* 4619** Attempt to close the databaes connection. Report errors. 4620*/ 4621void close_db(sqlite3 *db){ 4622 int rc = sqlite3_close(db); 4623 if( rc ){ 4624 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", 4625 rc, sqlite3_errmsg(db)); 4626 } 4627} 4628 4629#if HAVE_READLINE || HAVE_EDITLINE 4630/* 4631** Readline completion callbacks 4632*/ 4633static char *readline_completion_generator(const char *text, int state){ 4634 static sqlite3_stmt *pStmt = 0; 4635 char *zRet; 4636 if( state==0 ){ 4637 char *zSql; 4638 sqlite3_finalize(pStmt); 4639 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 4640 " FROM completion(%Q) ORDER BY 1", text); 4641 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 4642 sqlite3_free(zSql); 4643 } 4644 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 4645 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0)); 4646 }else{ 4647 sqlite3_finalize(pStmt); 4648 pStmt = 0; 4649 zRet = 0; 4650 } 4651 return zRet; 4652} 4653static char **readline_completion(const char *zText, int iStart, int iEnd){ 4654 rl_attempted_completion_over = 1; 4655 return rl_completion_matches(zText, readline_completion_generator); 4656} 4657 4658#elif HAVE_LINENOISE 4659/* 4660** Linenoise completion callback 4661*/ 4662static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ 4663 int nLine = strlen30(zLine); 4664 int i, iStart; 4665 sqlite3_stmt *pStmt = 0; 4666 char *zSql; 4667 char zBuf[1000]; 4668 4669 if( nLine>sizeof(zBuf)-30 ) return; 4670 if( zLine[0]=='.' || zLine[0]=='#') return; 4671 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} 4672 if( i==nLine-1 ) return; 4673 iStart = i+1; 4674 memcpy(zBuf, zLine, iStart); 4675 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 4676 " FROM completion(%Q,%Q) ORDER BY 1", 4677 &zLine[iStart], zLine); 4678 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 4679 sqlite3_free(zSql); 4680 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ 4681 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 4682 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); 4683 int nCompletion = sqlite3_column_bytes(pStmt, 0); 4684 if( iStart+nCompletion < sizeof(zBuf)-1 ){ 4685 memcpy(zBuf+iStart, zCompletion, nCompletion+1); 4686 linenoiseAddCompletion(lc, zBuf); 4687 } 4688 } 4689 sqlite3_finalize(pStmt); 4690} 4691#endif 4692 4693/* 4694** Do C-language style dequoting. 4695** 4696** \a -> alarm 4697** \b -> backspace 4698** \t -> tab 4699** \n -> newline 4700** \v -> vertical tab 4701** \f -> form feed 4702** \r -> carriage return 4703** \s -> space 4704** \" -> " 4705** \' -> ' 4706** \\ -> backslash 4707** \NNN -> ascii character NNN in octal 4708*/ 4709static void resolve_backslashes(char *z){ 4710 int i, j; 4711 char c; 4712 while( *z && *z!='\\' ) z++; 4713 for(i=j=0; (c = z[i])!=0; i++, j++){ 4714 if( c=='\\' && z[i+1]!=0 ){ 4715 c = z[++i]; 4716 if( c=='a' ){ 4717 c = '\a'; 4718 }else if( c=='b' ){ 4719 c = '\b'; 4720 }else if( c=='t' ){ 4721 c = '\t'; 4722 }else if( c=='n' ){ 4723 c = '\n'; 4724 }else if( c=='v' ){ 4725 c = '\v'; 4726 }else if( c=='f' ){ 4727 c = '\f'; 4728 }else if( c=='r' ){ 4729 c = '\r'; 4730 }else if( c=='"' ){ 4731 c = '"'; 4732 }else if( c=='\'' ){ 4733 c = '\''; 4734 }else if( c=='\\' ){ 4735 c = '\\'; 4736 }else if( c>='0' && c<='7' ){ 4737 c -= '0'; 4738 if( z[i+1]>='0' && z[i+1]<='7' ){ 4739 i++; 4740 c = (c<<3) + z[i] - '0'; 4741 if( z[i+1]>='0' && z[i+1]<='7' ){ 4742 i++; 4743 c = (c<<3) + z[i] - '0'; 4744 } 4745 } 4746 } 4747 } 4748 z[j] = c; 4749 } 4750 if( j<i ) z[j] = 0; 4751} 4752 4753/* 4754** Interpret zArg as either an integer or a boolean value. Return 1 or 0 4755** for TRUE and FALSE. Return the integer value if appropriate. 4756*/ 4757static int booleanValue(const char *zArg){ 4758 int i; 4759 if( zArg[0]=='0' && zArg[1]=='x' ){ 4760 for(i=2; hexDigitValue(zArg[i])>=0; i++){} 4761 }else{ 4762 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} 4763 } 4764 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); 4765 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ 4766 return 1; 4767 } 4768 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ 4769 return 0; 4770 } 4771 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", 4772 zArg); 4773 return 0; 4774} 4775 4776/* 4777** Set or clear a shell flag according to a boolean value. 4778*/ 4779static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ 4780 if( booleanValue(zArg) ){ 4781 ShellSetFlag(p, mFlag); 4782 }else{ 4783 ShellClearFlag(p, mFlag); 4784 } 4785} 4786 4787/* 4788** Close an output file, assuming it is not stderr or stdout 4789*/ 4790static void output_file_close(FILE *f){ 4791 if( f && f!=stdout && f!=stderr ) fclose(f); 4792} 4793 4794/* 4795** Try to open an output file. The names "stdout" and "stderr" are 4796** recognized and do the right thing. NULL is returned if the output 4797** filename is "off". 4798*/ 4799static FILE *output_file_open(const char *zFile, int bTextMode){ 4800 FILE *f; 4801 if( strcmp(zFile,"stdout")==0 ){ 4802 f = stdout; 4803 }else if( strcmp(zFile, "stderr")==0 ){ 4804 f = stderr; 4805 }else if( strcmp(zFile, "off")==0 ){ 4806 f = 0; 4807 }else{ 4808 f = fopen(zFile, bTextMode ? "w" : "wb"); 4809 if( f==0 ){ 4810 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 4811 } 4812 } 4813 return f; 4814} 4815 4816#ifndef SQLITE_OMIT_TRACE 4817/* 4818** A routine for handling output from sqlite3_trace(). 4819*/ 4820static int sql_trace_callback( 4821 unsigned mType, /* The trace type */ 4822 void *pArg, /* The ShellState pointer */ 4823 void *pP, /* Usually a pointer to sqlite_stmt */ 4824 void *pX /* Auxiliary output */ 4825){ 4826 ShellState *p = (ShellState*)pArg; 4827 sqlite3_stmt *pStmt; 4828 const char *zSql; 4829 int nSql; 4830 if( p->traceOut==0 ) return 0; 4831 if( mType==SQLITE_TRACE_CLOSE ){ 4832 utf8_printf(p->traceOut, "-- closing database connection\n"); 4833 return 0; 4834 } 4835 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ 4836 zSql = (const char*)pX; 4837 }else{ 4838 pStmt = (sqlite3_stmt*)pP; 4839 switch( p->eTraceType ){ 4840 case SHELL_TRACE_EXPANDED: { 4841 zSql = sqlite3_expanded_sql(pStmt); 4842 break; 4843 } 4844#ifdef SQLITE_ENABLE_NORMALIZE 4845 case SHELL_TRACE_NORMALIZED: { 4846 zSql = sqlite3_normalized_sql(pStmt); 4847 break; 4848 } 4849#endif 4850 default: { 4851 zSql = sqlite3_sql(pStmt); 4852 break; 4853 } 4854 } 4855 } 4856 if( zSql==0 ) return 0; 4857 nSql = strlen30(zSql); 4858 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } 4859 switch( mType ){ 4860 case SQLITE_TRACE_ROW: 4861 case SQLITE_TRACE_STMT: { 4862 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql); 4863 break; 4864 } 4865 case SQLITE_TRACE_PROFILE: { 4866 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; 4867 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec); 4868 break; 4869 } 4870 } 4871 return 0; 4872} 4873#endif 4874 4875/* 4876** A no-op routine that runs with the ".breakpoint" doc-command. This is 4877** a useful spot to set a debugger breakpoint. 4878*/ 4879static void test_breakpoint(void){ 4880 static int nCall = 0; 4881 nCall++; 4882} 4883 4884/* 4885** An object used to read a CSV and other files for import. 4886*/ 4887typedef struct ImportCtx ImportCtx; 4888struct ImportCtx { 4889 const char *zFile; /* Name of the input file */ 4890 FILE *in; /* Read the CSV text from this input stream */ 4891 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ 4892 char *z; /* Accumulated text for a field */ 4893 int n; /* Number of bytes in z */ 4894 int nAlloc; /* Space allocated for z[] */ 4895 int nLine; /* Current line number */ 4896 int nRow; /* Number of rows imported */ 4897 int nErr; /* Number of errors encountered */ 4898 int bNotFirst; /* True if one or more bytes already read */ 4899 int cTerm; /* Character that terminated the most recent field */ 4900 int cColSep; /* The column separator character. (Usually ",") */ 4901 int cRowSep; /* The row separator character. (Usually "\n") */ 4902}; 4903 4904/* Clean up resourced used by an ImportCtx */ 4905static void import_cleanup(ImportCtx *p){ 4906 if( p->in!=0 && p->xCloser!=0 ){ 4907 p->xCloser(p->in); 4908 p->in = 0; 4909 } 4910 sqlite3_free(p->z); 4911 p->z = 0; 4912} 4913 4914/* Append a single byte to z[] */ 4915static void import_append_char(ImportCtx *p, int c){ 4916 if( p->n+1>=p->nAlloc ){ 4917 p->nAlloc += p->nAlloc + 100; 4918 p->z = sqlite3_realloc64(p->z, p->nAlloc); 4919 if( p->z==0 ) shell_out_of_memory(); 4920 } 4921 p->z[p->n++] = (char)c; 4922} 4923 4924/* Read a single field of CSV text. Compatible with rfc4180 and extended 4925** with the option of having a separator other than ",". 4926** 4927** + Input comes from p->in. 4928** + Store results in p->z of length p->n. Space to hold p->z comes 4929** from sqlite3_malloc64(). 4930** + Use p->cSep as the column separator. The default is ",". 4931** + Use p->rSep as the row separator. The default is "\n". 4932** + Keep track of the line number in p->nLine. 4933** + Store the character that terminates the field in p->cTerm. Store 4934** EOF on end-of-file. 4935** + Report syntax errors on stderr 4936*/ 4937static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ 4938 int c; 4939 int cSep = p->cColSep; 4940 int rSep = p->cRowSep; 4941 p->n = 0; 4942 c = fgetc(p->in); 4943 if( c==EOF || seenInterrupt ){ 4944 p->cTerm = EOF; 4945 return 0; 4946 } 4947 if( c=='"' ){ 4948 int pc, ppc; 4949 int startLine = p->nLine; 4950 int cQuote = c; 4951 pc = ppc = 0; 4952 while( 1 ){ 4953 c = fgetc(p->in); 4954 if( c==rSep ) p->nLine++; 4955 if( c==cQuote ){ 4956 if( pc==cQuote ){ 4957 pc = 0; 4958 continue; 4959 } 4960 } 4961 if( (c==cSep && pc==cQuote) 4962 || (c==rSep && pc==cQuote) 4963 || (c==rSep && pc=='\r' && ppc==cQuote) 4964 || (c==EOF && pc==cQuote) 4965 ){ 4966 do{ p->n--; }while( p->z[p->n]!=cQuote ); 4967 p->cTerm = c; 4968 break; 4969 } 4970 if( pc==cQuote && c!='\r' ){ 4971 utf8_printf(stderr, "%s:%d: unescaped %c character\n", 4972 p->zFile, p->nLine, cQuote); 4973 } 4974 if( c==EOF ){ 4975 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", 4976 p->zFile, startLine, cQuote); 4977 p->cTerm = c; 4978 break; 4979 } 4980 import_append_char(p, c); 4981 ppc = pc; 4982 pc = c; 4983 } 4984 }else{ 4985 /* If this is the first field being parsed and it begins with the 4986 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ 4987 if( (c&0xff)==0xef && p->bNotFirst==0 ){ 4988 import_append_char(p, c); 4989 c = fgetc(p->in); 4990 if( (c&0xff)==0xbb ){ 4991 import_append_char(p, c); 4992 c = fgetc(p->in); 4993 if( (c&0xff)==0xbf ){ 4994 p->bNotFirst = 1; 4995 p->n = 0; 4996 return csv_read_one_field(p); 4997 } 4998 } 4999 } 5000 while( c!=EOF && c!=cSep && c!=rSep ){ 5001 import_append_char(p, c); 5002 c = fgetc(p->in); 5003 } 5004 if( c==rSep ){ 5005 p->nLine++; 5006 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; 5007 } 5008 p->cTerm = c; 5009 } 5010 if( p->z ) p->z[p->n] = 0; 5011 p->bNotFirst = 1; 5012 return p->z; 5013} 5014 5015/* Read a single field of ASCII delimited text. 5016** 5017** + Input comes from p->in. 5018** + Store results in p->z of length p->n. Space to hold p->z comes 5019** from sqlite3_malloc64(). 5020** + Use p->cSep as the column separator. The default is "\x1F". 5021** + Use p->rSep as the row separator. The default is "\x1E". 5022** + Keep track of the row number in p->nLine. 5023** + Store the character that terminates the field in p->cTerm. Store 5024** EOF on end-of-file. 5025** + Report syntax errors on stderr 5026*/ 5027static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ 5028 int c; 5029 int cSep = p->cColSep; 5030 int rSep = p->cRowSep; 5031 p->n = 0; 5032 c = fgetc(p->in); 5033 if( c==EOF || seenInterrupt ){ 5034 p->cTerm = EOF; 5035 return 0; 5036 } 5037 while( c!=EOF && c!=cSep && c!=rSep ){ 5038 import_append_char(p, c); 5039 c = fgetc(p->in); 5040 } 5041 if( c==rSep ){ 5042 p->nLine++; 5043 } 5044 p->cTerm = c; 5045 if( p->z ) p->z[p->n] = 0; 5046 return p->z; 5047} 5048 5049/* 5050** Try to transfer data for table zTable. If an error is seen while 5051** moving forward, try to go backwards. The backwards movement won't 5052** work for WITHOUT ROWID tables. 5053*/ 5054static void tryToCloneData( 5055 ShellState *p, 5056 sqlite3 *newDb, 5057 const char *zTable 5058){ 5059 sqlite3_stmt *pQuery = 0; 5060 sqlite3_stmt *pInsert = 0; 5061 char *zQuery = 0; 5062 char *zInsert = 0; 5063 int rc; 5064 int i, j, n; 5065 int nTable = strlen30(zTable); 5066 int k = 0; 5067 int cnt = 0; 5068 const int spinRate = 10000; 5069 5070 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); 5071 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5072 if( rc ){ 5073 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5074 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5075 zQuery); 5076 goto end_data_xfer; 5077 } 5078 n = sqlite3_column_count(pQuery); 5079 zInsert = sqlite3_malloc64(200 + nTable + n*3); 5080 if( zInsert==0 ) shell_out_of_memory(); 5081 sqlite3_snprintf(200+nTable,zInsert, 5082 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); 5083 i = strlen30(zInsert); 5084 for(j=1; j<n; j++){ 5085 memcpy(zInsert+i, ",?", 2); 5086 i += 2; 5087 } 5088 memcpy(zInsert+i, ");", 3); 5089 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); 5090 if( rc ){ 5091 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5092 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), 5093 zQuery); 5094 goto end_data_xfer; 5095 } 5096 for(k=0; k<2; k++){ 5097 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5098 for(i=0; i<n; i++){ 5099 switch( sqlite3_column_type(pQuery, i) ){ 5100 case SQLITE_NULL: { 5101 sqlite3_bind_null(pInsert, i+1); 5102 break; 5103 } 5104 case SQLITE_INTEGER: { 5105 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); 5106 break; 5107 } 5108 case SQLITE_FLOAT: { 5109 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); 5110 break; 5111 } 5112 case SQLITE_TEXT: { 5113 sqlite3_bind_text(pInsert, i+1, 5114 (const char*)sqlite3_column_text(pQuery,i), 5115 -1, SQLITE_STATIC); 5116 break; 5117 } 5118 case SQLITE_BLOB: { 5119 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), 5120 sqlite3_column_bytes(pQuery,i), 5121 SQLITE_STATIC); 5122 break; 5123 } 5124 } 5125 } /* End for */ 5126 rc = sqlite3_step(pInsert); 5127 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 5128 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), 5129 sqlite3_errmsg(newDb)); 5130 } 5131 sqlite3_reset(pInsert); 5132 cnt++; 5133 if( (cnt%spinRate)==0 ){ 5134 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); 5135 fflush(stdout); 5136 } 5137 } /* End while */ 5138 if( rc==SQLITE_DONE ) break; 5139 sqlite3_finalize(pQuery); 5140 sqlite3_free(zQuery); 5141 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", 5142 zTable); 5143 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5144 if( rc ){ 5145 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); 5146 break; 5147 } 5148 } /* End for(k=0...) */ 5149 5150end_data_xfer: 5151 sqlite3_finalize(pQuery); 5152 sqlite3_finalize(pInsert); 5153 sqlite3_free(zQuery); 5154 sqlite3_free(zInsert); 5155} 5156 5157 5158/* 5159** Try to transfer all rows of the schema that match zWhere. For 5160** each row, invoke xForEach() on the object defined by that row. 5161** If an error is encountered while moving forward through the 5162** sqlite_master table, try again moving backwards. 5163*/ 5164static void tryToCloneSchema( 5165 ShellState *p, 5166 sqlite3 *newDb, 5167 const char *zWhere, 5168 void (*xForEach)(ShellState*,sqlite3*,const char*) 5169){ 5170 sqlite3_stmt *pQuery = 0; 5171 char *zQuery = 0; 5172 int rc; 5173 const unsigned char *zName; 5174 const unsigned char *zSql; 5175 char *zErrMsg = 0; 5176 5177 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" 5178 " WHERE %s", zWhere); 5179 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5180 if( rc ){ 5181 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5182 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5183 zQuery); 5184 goto end_schema_xfer; 5185 } 5186 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5187 zName = sqlite3_column_text(pQuery, 0); 5188 zSql = sqlite3_column_text(pQuery, 1); 5189 printf("%s... ", zName); fflush(stdout); 5190 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5191 if( zErrMsg ){ 5192 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5193 sqlite3_free(zErrMsg); 5194 zErrMsg = 0; 5195 } 5196 if( xForEach ){ 5197 xForEach(p, newDb, (const char*)zName); 5198 } 5199 printf("done\n"); 5200 } 5201 if( rc!=SQLITE_DONE ){ 5202 sqlite3_finalize(pQuery); 5203 sqlite3_free(zQuery); 5204 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" 5205 " WHERE %s ORDER BY rowid DESC", zWhere); 5206 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5207 if( rc ){ 5208 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5209 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5210 zQuery); 5211 goto end_schema_xfer; 5212 } 5213 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5214 zName = sqlite3_column_text(pQuery, 0); 5215 zSql = sqlite3_column_text(pQuery, 1); 5216 printf("%s... ", zName); fflush(stdout); 5217 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5218 if( zErrMsg ){ 5219 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5220 sqlite3_free(zErrMsg); 5221 zErrMsg = 0; 5222 } 5223 if( xForEach ){ 5224 xForEach(p, newDb, (const char*)zName); 5225 } 5226 printf("done\n"); 5227 } 5228 } 5229end_schema_xfer: 5230 sqlite3_finalize(pQuery); 5231 sqlite3_free(zQuery); 5232} 5233 5234/* 5235** Open a new database file named "zNewDb". Try to recover as much information 5236** as possible out of the main database (which might be corrupt) and write it 5237** into zNewDb. 5238*/ 5239static void tryToClone(ShellState *p, const char *zNewDb){ 5240 int rc; 5241 sqlite3 *newDb = 0; 5242 if( access(zNewDb,0)==0 ){ 5243 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); 5244 return; 5245 } 5246 rc = sqlite3_open(zNewDb, &newDb); 5247 if( rc ){ 5248 utf8_printf(stderr, "Cannot create output database: %s\n", 5249 sqlite3_errmsg(newDb)); 5250 }else{ 5251 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); 5252 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); 5253 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); 5254 tryToCloneSchema(p, newDb, "type!='table'", 0); 5255 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); 5256 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 5257 } 5258 close_db(newDb); 5259} 5260 5261/* 5262** Change the output file back to stdout. 5263** 5264** If the p->doXdgOpen flag is set, that means the output was being 5265** redirected to a temporary file named by p->zTempFile. In that case, 5266** launch start/open/xdg-open on that temporary file. 5267*/ 5268static void output_reset(ShellState *p){ 5269 if( p->outfile[0]=='|' ){ 5270#ifndef SQLITE_OMIT_POPEN 5271 pclose(p->out); 5272#endif 5273 }else{ 5274 output_file_close(p->out); 5275#ifndef SQLITE_NOHAVE_SYSTEM 5276 if( p->doXdgOpen ){ 5277 const char *zXdgOpenCmd = 5278#if defined(_WIN32) 5279 "start"; 5280#elif defined(__APPLE__) 5281 "open"; 5282#else 5283 "xdg-open"; 5284#endif 5285 char *zCmd; 5286 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); 5287 if( system(zCmd) ){ 5288 utf8_printf(stderr, "Failed: [%s]\n", zCmd); 5289 }else{ 5290 /* Give the start/open/xdg-open command some time to get 5291 ** going before we continue, and potential delete the 5292 ** p->zTempFile data file out from under it */ 5293 sqlite3_sleep(2000); 5294 } 5295 sqlite3_free(zCmd); 5296 outputModePop(p); 5297 p->doXdgOpen = 0; 5298 } 5299#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 5300 } 5301 p->outfile[0] = 0; 5302 p->out = stdout; 5303} 5304 5305/* 5306** Run an SQL command and return the single integer result. 5307*/ 5308static int db_int(ShellState *p, const char *zSql){ 5309 sqlite3_stmt *pStmt; 5310 int res = 0; 5311 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 5312 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 5313 res = sqlite3_column_int(pStmt,0); 5314 } 5315 sqlite3_finalize(pStmt); 5316 return res; 5317} 5318 5319/* 5320** Convert a 2-byte or 4-byte big-endian integer into a native integer 5321*/ 5322static unsigned int get2byteInt(unsigned char *a){ 5323 return (a[0]<<8) + a[1]; 5324} 5325static unsigned int get4byteInt(unsigned char *a){ 5326 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; 5327} 5328 5329/* 5330** Implementation of the ".dbinfo" command. 5331** 5332** Return 1 on error, 2 to exit, and 0 otherwise. 5333*/ 5334static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ 5335 static const struct { const char *zName; int ofst; } aField[] = { 5336 { "file change counter:", 24 }, 5337 { "database page count:", 28 }, 5338 { "freelist page count:", 36 }, 5339 { "schema cookie:", 40 }, 5340 { "schema format:", 44 }, 5341 { "default cache size:", 48 }, 5342 { "autovacuum top root:", 52 }, 5343 { "incremental vacuum:", 64 }, 5344 { "text encoding:", 56 }, 5345 { "user version:", 60 }, 5346 { "application id:", 68 }, 5347 { "software version:", 96 }, 5348 }; 5349 static const struct { const char *zName; const char *zSql; } aQuery[] = { 5350 { "number of tables:", 5351 "SELECT count(*) FROM %s WHERE type='table'" }, 5352 { "number of indexes:", 5353 "SELECT count(*) FROM %s WHERE type='index'" }, 5354 { "number of triggers:", 5355 "SELECT count(*) FROM %s WHERE type='trigger'" }, 5356 { "number of views:", 5357 "SELECT count(*) FROM %s WHERE type='view'" }, 5358 { "schema size:", 5359 "SELECT total(length(sql)) FROM %s" }, 5360 }; 5361 int i, rc; 5362 unsigned iDataVersion; 5363 char *zSchemaTab; 5364 char *zDb = nArg>=2 ? azArg[1] : "main"; 5365 sqlite3_stmt *pStmt = 0; 5366 unsigned char aHdr[100]; 5367 open_db(p, 0); 5368 if( p->db==0 ) return 1; 5369 rc = sqlite3_prepare_v2(p->db, 5370 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", 5371 -1, &pStmt, 0); 5372 if( rc ){ 5373 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); 5374 sqlite3_finalize(pStmt); 5375 return 1; 5376 } 5377 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); 5378 if( sqlite3_step(pStmt)==SQLITE_ROW 5379 && sqlite3_column_bytes(pStmt,0)>100 5380 ){ 5381 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); 5382 sqlite3_finalize(pStmt); 5383 }else{ 5384 raw_printf(stderr, "unable to read database header\n"); 5385 sqlite3_finalize(pStmt); 5386 return 1; 5387 } 5388 i = get2byteInt(aHdr+16); 5389 if( i==1 ) i = 65536; 5390 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); 5391 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); 5392 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); 5393 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); 5394 for(i=0; i<ArraySize(aField); i++){ 5395 int ofst = aField[i].ofst; 5396 unsigned int val = get4byteInt(aHdr + ofst); 5397 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); 5398 switch( ofst ){ 5399 case 56: { 5400 if( val==1 ) raw_printf(p->out, " (utf8)"); 5401 if( val==2 ) raw_printf(p->out, " (utf16le)"); 5402 if( val==3 ) raw_printf(p->out, " (utf16be)"); 5403 } 5404 } 5405 raw_printf(p->out, "\n"); 5406 } 5407 if( zDb==0 ){ 5408 zSchemaTab = sqlite3_mprintf("main.sqlite_master"); 5409 }else if( strcmp(zDb,"temp")==0 ){ 5410 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); 5411 }else{ 5412 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); 5413 } 5414 for(i=0; i<ArraySize(aQuery); i++){ 5415 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); 5416 int val = db_int(p, zSql); 5417 sqlite3_free(zSql); 5418 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); 5419 } 5420 sqlite3_free(zSchemaTab); 5421 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); 5422 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); 5423 return 0; 5424} 5425 5426/* 5427** Print the current sqlite3_errmsg() value to stderr and return 1. 5428*/ 5429static int shellDatabaseError(sqlite3 *db){ 5430 const char *zErr = sqlite3_errmsg(db); 5431 utf8_printf(stderr, "Error: %s\n", zErr); 5432 return 1; 5433} 5434 5435/* 5436** Compare the pattern in zGlob[] against the text in z[]. Return TRUE 5437** if they match and FALSE (0) if they do not match. 5438** 5439** Globbing rules: 5440** 5441** '*' Matches any sequence of zero or more characters. 5442** 5443** '?' Matches exactly one character. 5444** 5445** [...] Matches one character from the enclosed list of 5446** characters. 5447** 5448** [^...] Matches one character not in the enclosed list. 5449** 5450** '#' Matches any sequence of one or more digits with an 5451** optional + or - sign in front 5452** 5453** ' ' Any span of whitespace matches any other span of 5454** whitespace. 5455** 5456** Extra whitespace at the end of z[] is ignored. 5457*/ 5458static int testcase_glob(const char *zGlob, const char *z){ 5459 int c, c2; 5460 int invert; 5461 int seen; 5462 5463 while( (c = (*(zGlob++)))!=0 ){ 5464 if( IsSpace(c) ){ 5465 if( !IsSpace(*z) ) return 0; 5466 while( IsSpace(*zGlob) ) zGlob++; 5467 while( IsSpace(*z) ) z++; 5468 }else if( c=='*' ){ 5469 while( (c=(*(zGlob++))) == '*' || c=='?' ){ 5470 if( c=='?' && (*(z++))==0 ) return 0; 5471 } 5472 if( c==0 ){ 5473 return 1; 5474 }else if( c=='[' ){ 5475 while( *z && testcase_glob(zGlob-1,z)==0 ){ 5476 z++; 5477 } 5478 return (*z)!=0; 5479 } 5480 while( (c2 = (*(z++)))!=0 ){ 5481 while( c2!=c ){ 5482 c2 = *(z++); 5483 if( c2==0 ) return 0; 5484 } 5485 if( testcase_glob(zGlob,z) ) return 1; 5486 } 5487 return 0; 5488 }else if( c=='?' ){ 5489 if( (*(z++))==0 ) return 0; 5490 }else if( c=='[' ){ 5491 int prior_c = 0; 5492 seen = 0; 5493 invert = 0; 5494 c = *(z++); 5495 if( c==0 ) return 0; 5496 c2 = *(zGlob++); 5497 if( c2=='^' ){ 5498 invert = 1; 5499 c2 = *(zGlob++); 5500 } 5501 if( c2==']' ){ 5502 if( c==']' ) seen = 1; 5503 c2 = *(zGlob++); 5504 } 5505 while( c2 && c2!=']' ){ 5506 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 5507 c2 = *(zGlob++); 5508 if( c>=prior_c && c<=c2 ) seen = 1; 5509 prior_c = 0; 5510 }else{ 5511 if( c==c2 ){ 5512 seen = 1; 5513 } 5514 prior_c = c2; 5515 } 5516 c2 = *(zGlob++); 5517 } 5518 if( c2==0 || (seen ^ invert)==0 ) return 0; 5519 }else if( c=='#' ){ 5520 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; 5521 if( !IsDigit(z[0]) ) return 0; 5522 z++; 5523 while( IsDigit(z[0]) ){ z++; } 5524 }else{ 5525 if( c!=(*(z++)) ) return 0; 5526 } 5527 } 5528 while( IsSpace(*z) ){ z++; } 5529 return *z==0; 5530} 5531 5532 5533/* 5534** Compare the string as a command-line option with either one or two 5535** initial "-" characters. 5536*/ 5537static int optionMatch(const char *zStr, const char *zOpt){ 5538 if( zStr[0]!='-' ) return 0; 5539 zStr++; 5540 if( zStr[0]=='-' ) zStr++; 5541 return strcmp(zStr, zOpt)==0; 5542} 5543 5544/* 5545** Delete a file. 5546*/ 5547int shellDeleteFile(const char *zFilename){ 5548 int rc; 5549#ifdef _WIN32 5550 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); 5551 rc = _wunlink(z); 5552 sqlite3_free(z); 5553#else 5554 rc = unlink(zFilename); 5555#endif 5556 return rc; 5557} 5558 5559/* 5560** Try to delete the temporary file (if there is one) and free the 5561** memory used to hold the name of the temp file. 5562*/ 5563static void clearTempFile(ShellState *p){ 5564 if( p->zTempFile==0 ) return; 5565 if( p->doXdgOpen ) return; 5566 if( shellDeleteFile(p->zTempFile) ) return; 5567 sqlite3_free(p->zTempFile); 5568 p->zTempFile = 0; 5569} 5570 5571/* 5572** Create a new temp file name with the given suffix. 5573*/ 5574static void newTempFile(ShellState *p, const char *zSuffix){ 5575 clearTempFile(p); 5576 sqlite3_free(p->zTempFile); 5577 p->zTempFile = 0; 5578 if( p->db ){ 5579 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); 5580 } 5581 if( p->zTempFile==0 ){ 5582 /* If p->db is an in-memory database then the TEMPFILENAME file-control 5583 ** will not work and we will need to fallback to guessing */ 5584 char *zTemp; 5585 sqlite3_uint64 r; 5586 sqlite3_randomness(sizeof(r), &r); 5587 zTemp = getenv("TEMP"); 5588 if( zTemp==0 ) zTemp = getenv("TMP"); 5589 if( zTemp==0 ){ 5590#ifdef _WIN32 5591 zTemp = "\\tmp"; 5592#else 5593 zTemp = "/tmp"; 5594#endif 5595 } 5596 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); 5597 }else{ 5598 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); 5599 } 5600 if( p->zTempFile==0 ){ 5601 raw_printf(stderr, "out of memory\n"); 5602 exit(1); 5603 } 5604} 5605 5606 5607/* 5608** The implementation of SQL scalar function fkey_collate_clause(), used 5609** by the ".lint fkey-indexes" command. This scalar function is always 5610** called with four arguments - the parent table name, the parent column name, 5611** the child table name and the child column name. 5612** 5613** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') 5614** 5615** If either of the named tables or columns do not exist, this function 5616** returns an empty string. An empty string is also returned if both tables 5617** and columns exist but have the same default collation sequence. Or, 5618** if both exist but the default collation sequences are different, this 5619** function returns the string " COLLATE <parent-collation>", where 5620** <parent-collation> is the default collation sequence of the parent column. 5621*/ 5622static void shellFkeyCollateClause( 5623 sqlite3_context *pCtx, 5624 int nVal, 5625 sqlite3_value **apVal 5626){ 5627 sqlite3 *db = sqlite3_context_db_handle(pCtx); 5628 const char *zParent; 5629 const char *zParentCol; 5630 const char *zParentSeq; 5631 const char *zChild; 5632 const char *zChildCol; 5633 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ 5634 int rc; 5635 5636 assert( nVal==4 ); 5637 zParent = (const char*)sqlite3_value_text(apVal[0]); 5638 zParentCol = (const char*)sqlite3_value_text(apVal[1]); 5639 zChild = (const char*)sqlite3_value_text(apVal[2]); 5640 zChildCol = (const char*)sqlite3_value_text(apVal[3]); 5641 5642 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); 5643 rc = sqlite3_table_column_metadata( 5644 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 5645 ); 5646 if( rc==SQLITE_OK ){ 5647 rc = sqlite3_table_column_metadata( 5648 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 5649 ); 5650 } 5651 5652 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ 5653 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); 5654 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); 5655 sqlite3_free(z); 5656 } 5657} 5658 5659 5660/* 5661** The implementation of dot-command ".lint fkey-indexes". 5662*/ 5663static int lintFkeyIndexes( 5664 ShellState *pState, /* Current shell tool state */ 5665 char **azArg, /* Array of arguments passed to dot command */ 5666 int nArg /* Number of entries in azArg[] */ 5667){ 5668 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ 5669 FILE *out = pState->out; /* Stream to write non-error output to */ 5670 int bVerbose = 0; /* If -verbose is present */ 5671 int bGroupByParent = 0; /* If -groupbyparent is present */ 5672 int i; /* To iterate through azArg[] */ 5673 const char *zIndent = ""; /* How much to indent CREATE INDEX by */ 5674 int rc; /* Return code */ 5675 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ 5676 5677 /* 5678 ** This SELECT statement returns one row for each foreign key constraint 5679 ** in the schema of the main database. The column values are: 5680 ** 5681 ** 0. The text of an SQL statement similar to: 5682 ** 5683 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" 5684 ** 5685 ** This SELECT is similar to the one that the foreign keys implementation 5686 ** needs to run internally on child tables. If there is an index that can 5687 ** be used to optimize this query, then it can also be used by the FK 5688 ** implementation to optimize DELETE or UPDATE statements on the parent 5689 ** table. 5690 ** 5691 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by 5692 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema 5693 ** contains an index that can be used to optimize the query. 5694 ** 5695 ** 2. Human readable text that describes the child table and columns. e.g. 5696 ** 5697 ** "child_table(child_key1, child_key2)" 5698 ** 5699 ** 3. Human readable text that describes the parent table and columns. e.g. 5700 ** 5701 ** "parent_table(parent_key1, parent_key2)" 5702 ** 5703 ** 4. A full CREATE INDEX statement for an index that could be used to 5704 ** optimize DELETE or UPDATE statements on the parent table. e.g. 5705 ** 5706 ** "CREATE INDEX child_table_child_key ON child_table(child_key)" 5707 ** 5708 ** 5. The name of the parent table. 5709 ** 5710 ** These six values are used by the C logic below to generate the report. 5711 */ 5712 const char *zSql = 5713 "SELECT " 5714 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" 5715 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " 5716 " || fkey_collate_clause(" 5717 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" 5718 ", " 5719 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" 5720 " || group_concat('*=?', ' AND ') || ')'" 5721 ", " 5722 " s.name || '(' || group_concat(f.[from], ', ') || ')'" 5723 ", " 5724 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" 5725 ", " 5726 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" 5727 " || ' ON ' || quote(s.name) || '('" 5728 " || group_concat(quote(f.[from]) ||" 5729 " fkey_collate_clause(" 5730 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" 5731 " || ');'" 5732 ", " 5733 " f.[table] " 5734 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " 5735 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " 5736 "GROUP BY s.name, f.id " 5737 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" 5738 ; 5739 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; 5740 5741 for(i=2; i<nArg; i++){ 5742 int n = strlen30(azArg[i]); 5743 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ 5744 bVerbose = 1; 5745 } 5746 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ 5747 bGroupByParent = 1; 5748 zIndent = " "; 5749 } 5750 else{ 5751 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", 5752 azArg[0], azArg[1] 5753 ); 5754 return SQLITE_ERROR; 5755 } 5756 } 5757 5758 /* Register the fkey_collate_clause() SQL function */ 5759 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, 5760 0, shellFkeyCollateClause, 0, 0 5761 ); 5762 5763 5764 if( rc==SQLITE_OK ){ 5765 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); 5766 } 5767 if( rc==SQLITE_OK ){ 5768 sqlite3_bind_int(pSql, 1, bGroupByParent); 5769 } 5770 5771 if( rc==SQLITE_OK ){ 5772 int rc2; 5773 char *zPrev = 0; 5774 while( SQLITE_ROW==sqlite3_step(pSql) ){ 5775 int res = -1; 5776 sqlite3_stmt *pExplain = 0; 5777 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); 5778 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); 5779 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); 5780 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); 5781 const char *zCI = (const char*)sqlite3_column_text(pSql, 4); 5782 const char *zParent = (const char*)sqlite3_column_text(pSql, 5); 5783 5784 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 5785 if( rc!=SQLITE_OK ) break; 5786 if( SQLITE_ROW==sqlite3_step(pExplain) ){ 5787 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); 5788 res = ( 5789 0==sqlite3_strglob(zGlob, zPlan) 5790 || 0==sqlite3_strglob(zGlobIPK, zPlan) 5791 ); 5792 } 5793 rc = sqlite3_finalize(pExplain); 5794 if( rc!=SQLITE_OK ) break; 5795 5796 if( res<0 ){ 5797 raw_printf(stderr, "Error: internal error"); 5798 break; 5799 }else{ 5800 if( bGroupByParent 5801 && (bVerbose || res==0) 5802 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) 5803 ){ 5804 raw_printf(out, "-- Parent table %s\n", zParent); 5805 sqlite3_free(zPrev); 5806 zPrev = sqlite3_mprintf("%s", zParent); 5807 } 5808 5809 if( res==0 ){ 5810 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); 5811 }else if( bVerbose ){ 5812 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", 5813 zIndent, zFrom, zTarget 5814 ); 5815 } 5816 } 5817 } 5818 sqlite3_free(zPrev); 5819 5820 if( rc!=SQLITE_OK ){ 5821 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5822 } 5823 5824 rc2 = sqlite3_finalize(pSql); 5825 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ 5826 rc = rc2; 5827 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5828 } 5829 }else{ 5830 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5831 } 5832 5833 return rc; 5834} 5835 5836/* 5837** Implementation of ".lint" dot command. 5838*/ 5839static int lintDotCommand( 5840 ShellState *pState, /* Current shell tool state */ 5841 char **azArg, /* Array of arguments passed to dot command */ 5842 int nArg /* Number of entries in azArg[] */ 5843){ 5844 int n; 5845 n = (nArg>=2 ? strlen30(azArg[1]) : 0); 5846 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; 5847 return lintFkeyIndexes(pState, azArg, nArg); 5848 5849 usage: 5850 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); 5851 raw_printf(stderr, "Where sub-commands are:\n"); 5852 raw_printf(stderr, " fkey-indexes\n"); 5853 return SQLITE_ERROR; 5854} 5855 5856#if !defined SQLITE_OMIT_VIRTUALTABLE 5857static void shellPrepare( 5858 sqlite3 *db, 5859 int *pRc, 5860 const char *zSql, 5861 sqlite3_stmt **ppStmt 5862){ 5863 *ppStmt = 0; 5864 if( *pRc==SQLITE_OK ){ 5865 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); 5866 if( rc!=SQLITE_OK ){ 5867 raw_printf(stderr, "sql error: %s (%d)\n", 5868 sqlite3_errmsg(db), sqlite3_errcode(db) 5869 ); 5870 *pRc = rc; 5871 } 5872 } 5873} 5874 5875/* 5876** Create a prepared statement using printf-style arguments for the SQL. 5877** 5878** This routine is could be marked "static". But it is not always used, 5879** depending on compile-time options. By omitting the "static", we avoid 5880** nuisance compiler warnings about "defined but not used". 5881*/ 5882void shellPreparePrintf( 5883 sqlite3 *db, 5884 int *pRc, 5885 sqlite3_stmt **ppStmt, 5886 const char *zFmt, 5887 ... 5888){ 5889 *ppStmt = 0; 5890 if( *pRc==SQLITE_OK ){ 5891 va_list ap; 5892 char *z; 5893 va_start(ap, zFmt); 5894 z = sqlite3_vmprintf(zFmt, ap); 5895 va_end(ap); 5896 if( z==0 ){ 5897 *pRc = SQLITE_NOMEM; 5898 }else{ 5899 shellPrepare(db, pRc, z, ppStmt); 5900 sqlite3_free(z); 5901 } 5902 } 5903} 5904 5905/* Finalize the prepared statement created using shellPreparePrintf(). 5906** 5907** This routine is could be marked "static". But it is not always used, 5908** depending on compile-time options. By omitting the "static", we avoid 5909** nuisance compiler warnings about "defined but not used". 5910*/ 5911void shellFinalize( 5912 int *pRc, 5913 sqlite3_stmt *pStmt 5914){ 5915 if( pStmt ){ 5916 sqlite3 *db = sqlite3_db_handle(pStmt); 5917 int rc = sqlite3_finalize(pStmt); 5918 if( *pRc==SQLITE_OK ){ 5919 if( rc!=SQLITE_OK ){ 5920 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 5921 } 5922 *pRc = rc; 5923 } 5924 } 5925} 5926 5927/* Reset the prepared statement created using shellPreparePrintf(). 5928** 5929** This routine is could be marked "static". But it is not always used, 5930** depending on compile-time options. By omitting the "static", we avoid 5931** nuisance compiler warnings about "defined but not used". 5932*/ 5933void shellReset( 5934 int *pRc, 5935 sqlite3_stmt *pStmt 5936){ 5937 int rc = sqlite3_reset(pStmt); 5938 if( *pRc==SQLITE_OK ){ 5939 if( rc!=SQLITE_OK ){ 5940 sqlite3 *db = sqlite3_db_handle(pStmt); 5941 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 5942 } 5943 *pRc = rc; 5944 } 5945} 5946#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ 5947 5948#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 5949/****************************************************************************** 5950** The ".archive" or ".ar" command. 5951*/ 5952/* 5953** Structure representing a single ".ar" command. 5954*/ 5955typedef struct ArCommand ArCommand; 5956struct ArCommand { 5957 u8 eCmd; /* An AR_CMD_* value */ 5958 u8 bVerbose; /* True if --verbose */ 5959 u8 bZip; /* True if the archive is a ZIP */ 5960 u8 bDryRun; /* True if --dry-run */ 5961 u8 bAppend; /* True if --append */ 5962 u8 fromCmdLine; /* Run from -A instead of .archive */ 5963 int nArg; /* Number of command arguments */ 5964 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ 5965 const char *zFile; /* --file argument, or NULL */ 5966 const char *zDir; /* --directory argument, or NULL */ 5967 char **azArg; /* Array of command arguments */ 5968 ShellState *p; /* Shell state */ 5969 sqlite3 *db; /* Database containing the archive */ 5970}; 5971 5972/* 5973** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. 5974*/ 5975static int arUsage(FILE *f){ 5976 showHelp(f,"archive"); 5977 return SQLITE_ERROR; 5978} 5979 5980/* 5981** Print an error message for the .ar command to stderr and return 5982** SQLITE_ERROR. 5983*/ 5984static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ 5985 va_list ap; 5986 char *z; 5987 va_start(ap, zFmt); 5988 z = sqlite3_vmprintf(zFmt, ap); 5989 va_end(ap); 5990 utf8_printf(stderr, "Error: %s\n", z); 5991 if( pAr->fromCmdLine ){ 5992 utf8_printf(stderr, "Use \"-A\" for more help\n"); 5993 }else{ 5994 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); 5995 } 5996 sqlite3_free(z); 5997 return SQLITE_ERROR; 5998} 5999 6000/* 6001** Values for ArCommand.eCmd. 6002*/ 6003#define AR_CMD_CREATE 1 6004#define AR_CMD_UPDATE 2 6005#define AR_CMD_INSERT 3 6006#define AR_CMD_EXTRACT 4 6007#define AR_CMD_LIST 5 6008#define AR_CMD_HELP 6 6009 6010/* 6011** Other (non-command) switches. 6012*/ 6013#define AR_SWITCH_VERBOSE 7 6014#define AR_SWITCH_FILE 8 6015#define AR_SWITCH_DIRECTORY 9 6016#define AR_SWITCH_APPEND 10 6017#define AR_SWITCH_DRYRUN 11 6018 6019static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ 6020 switch( eSwitch ){ 6021 case AR_CMD_CREATE: 6022 case AR_CMD_EXTRACT: 6023 case AR_CMD_LIST: 6024 case AR_CMD_UPDATE: 6025 case AR_CMD_INSERT: 6026 case AR_CMD_HELP: 6027 if( pAr->eCmd ){ 6028 return arErrorMsg(pAr, "multiple command options"); 6029 } 6030 pAr->eCmd = eSwitch; 6031 break; 6032 6033 case AR_SWITCH_DRYRUN: 6034 pAr->bDryRun = 1; 6035 break; 6036 case AR_SWITCH_VERBOSE: 6037 pAr->bVerbose = 1; 6038 break; 6039 case AR_SWITCH_APPEND: 6040 pAr->bAppend = 1; 6041 /* Fall thru into --file */ 6042 case AR_SWITCH_FILE: 6043 pAr->zFile = zArg; 6044 break; 6045 case AR_SWITCH_DIRECTORY: 6046 pAr->zDir = zArg; 6047 break; 6048 } 6049 6050 return SQLITE_OK; 6051} 6052 6053/* 6054** Parse the command line for an ".ar" command. The results are written into 6055** structure (*pAr). SQLITE_OK is returned if the command line is parsed 6056** successfully, otherwise an error message is written to stderr and 6057** SQLITE_ERROR returned. 6058*/ 6059static int arParseCommand( 6060 char **azArg, /* Array of arguments passed to dot command */ 6061 int nArg, /* Number of entries in azArg[] */ 6062 ArCommand *pAr /* Populate this object */ 6063){ 6064 struct ArSwitch { 6065 const char *zLong; 6066 char cShort; 6067 u8 eSwitch; 6068 u8 bArg; 6069 } aSwitch[] = { 6070 { "create", 'c', AR_CMD_CREATE, 0 }, 6071 { "extract", 'x', AR_CMD_EXTRACT, 0 }, 6072 { "insert", 'i', AR_CMD_INSERT, 0 }, 6073 { "list", 't', AR_CMD_LIST, 0 }, 6074 { "update", 'u', AR_CMD_UPDATE, 0 }, 6075 { "help", 'h', AR_CMD_HELP, 0 }, 6076 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, 6077 { "file", 'f', AR_SWITCH_FILE, 1 }, 6078 { "append", 'a', AR_SWITCH_APPEND, 1 }, 6079 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, 6080 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, 6081 }; 6082 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); 6083 struct ArSwitch *pEnd = &aSwitch[nSwitch]; 6084 6085 if( nArg<=1 ){ 6086 utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); 6087 return arUsage(stderr); 6088 }else{ 6089 char *z = azArg[1]; 6090 if( z[0]!='-' ){ 6091 /* Traditional style [tar] invocation */ 6092 int i; 6093 int iArg = 2; 6094 for(i=0; z[i]; i++){ 6095 const char *zArg = 0; 6096 struct ArSwitch *pOpt; 6097 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6098 if( z[i]==pOpt->cShort ) break; 6099 } 6100 if( pOpt==pEnd ){ 6101 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6102 } 6103 if( pOpt->bArg ){ 6104 if( iArg>=nArg ){ 6105 return arErrorMsg(pAr, "option requires an argument: %c",z[i]); 6106 } 6107 zArg = azArg[iArg++]; 6108 } 6109 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6110 } 6111 pAr->nArg = nArg-iArg; 6112 if( pAr->nArg>0 ){ 6113 pAr->azArg = &azArg[iArg]; 6114 } 6115 }else{ 6116 /* Non-traditional invocation */ 6117 int iArg; 6118 for(iArg=1; iArg<nArg; iArg++){ 6119 int n; 6120 z = azArg[iArg]; 6121 if( z[0]!='-' ){ 6122 /* All remaining command line words are command arguments. */ 6123 pAr->azArg = &azArg[iArg]; 6124 pAr->nArg = nArg-iArg; 6125 break; 6126 } 6127 n = strlen30(z); 6128 6129 if( z[1]!='-' ){ 6130 int i; 6131 /* One or more short options */ 6132 for(i=1; i<n; i++){ 6133 const char *zArg = 0; 6134 struct ArSwitch *pOpt; 6135 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6136 if( z[i]==pOpt->cShort ) break; 6137 } 6138 if( pOpt==pEnd ){ 6139 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6140 } 6141 if( pOpt->bArg ){ 6142 if( i<(n-1) ){ 6143 zArg = &z[i+1]; 6144 i = n; 6145 }else{ 6146 if( iArg>=(nArg-1) ){ 6147 return arErrorMsg(pAr, "option requires an argument: %c", 6148 z[i]); 6149 } 6150 zArg = azArg[++iArg]; 6151 } 6152 } 6153 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6154 } 6155 }else if( z[2]=='\0' ){ 6156 /* A -- option, indicating that all remaining command line words 6157 ** are command arguments. */ 6158 pAr->azArg = &azArg[iArg+1]; 6159 pAr->nArg = nArg-iArg-1; 6160 break; 6161 }else{ 6162 /* A long option */ 6163 const char *zArg = 0; /* Argument for option, if any */ 6164 struct ArSwitch *pMatch = 0; /* Matching option */ 6165 struct ArSwitch *pOpt; /* Iterator */ 6166 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6167 const char *zLong = pOpt->zLong; 6168 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ 6169 if( pMatch ){ 6170 return arErrorMsg(pAr, "ambiguous option: %s",z); 6171 }else{ 6172 pMatch = pOpt; 6173 } 6174 } 6175 } 6176 6177 if( pMatch==0 ){ 6178 return arErrorMsg(pAr, "unrecognized option: %s", z); 6179 } 6180 if( pMatch->bArg ){ 6181 if( iArg>=(nArg-1) ){ 6182 return arErrorMsg(pAr, "option requires an argument: %s", z); 6183 } 6184 zArg = azArg[++iArg]; 6185 } 6186 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; 6187 } 6188 } 6189 } 6190 } 6191 6192 return SQLITE_OK; 6193} 6194 6195/* 6196** This function assumes that all arguments within the ArCommand.azArg[] 6197** array refer to archive members, as for the --extract or --list commands. 6198** It checks that each of them are present. If any specified file is not 6199** present in the archive, an error is printed to stderr and an error 6200** code returned. Otherwise, if all specified arguments are present in 6201** the archive, SQLITE_OK is returned. 6202** 6203** This function strips any trailing '/' characters from each argument. 6204** This is consistent with the way the [tar] command seems to work on 6205** Linux. 6206*/ 6207static int arCheckEntries(ArCommand *pAr){ 6208 int rc = SQLITE_OK; 6209 if( pAr->nArg ){ 6210 int i, j; 6211 sqlite3_stmt *pTest = 0; 6212 6213 shellPreparePrintf(pAr->db, &rc, &pTest, 6214 "SELECT name FROM %s WHERE name=$name", 6215 pAr->zSrcTable 6216 ); 6217 j = sqlite3_bind_parameter_index(pTest, "$name"); 6218 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 6219 char *z = pAr->azArg[i]; 6220 int n = strlen30(z); 6221 int bOk = 0; 6222 while( n>0 && z[n-1]=='/' ) n--; 6223 z[n] = '\0'; 6224 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); 6225 if( SQLITE_ROW==sqlite3_step(pTest) ){ 6226 bOk = 1; 6227 } 6228 shellReset(&rc, pTest); 6229 if( rc==SQLITE_OK && bOk==0 ){ 6230 utf8_printf(stderr, "not found in archive: %s\n", z); 6231 rc = SQLITE_ERROR; 6232 } 6233 } 6234 shellFinalize(&rc, pTest); 6235 } 6236 return rc; 6237} 6238 6239/* 6240** Format a WHERE clause that can be used against the "sqlar" table to 6241** identify all archive members that match the command arguments held 6242** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. 6243** The caller is responsible for eventually calling sqlite3_free() on 6244** any non-NULL (*pzWhere) value. 6245*/ 6246static void arWhereClause( 6247 int *pRc, 6248 ArCommand *pAr, 6249 char **pzWhere /* OUT: New WHERE clause */ 6250){ 6251 char *zWhere = 0; 6252 if( *pRc==SQLITE_OK ){ 6253 if( pAr->nArg==0 ){ 6254 zWhere = sqlite3_mprintf("1"); 6255 }else{ 6256 int i; 6257 const char *zSep = ""; 6258 for(i=0; i<pAr->nArg; i++){ 6259 const char *z = pAr->azArg[i]; 6260 zWhere = sqlite3_mprintf( 6261 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 6262 zWhere, zSep, z, strlen30(z)+1, z 6263 ); 6264 if( zWhere==0 ){ 6265 *pRc = SQLITE_NOMEM; 6266 break; 6267 } 6268 zSep = " OR "; 6269 } 6270 } 6271 } 6272 *pzWhere = zWhere; 6273} 6274 6275/* 6276** Implementation of .ar "lisT" command. 6277*/ 6278static int arListCommand(ArCommand *pAr){ 6279 const char *zSql = "SELECT %s FROM %s WHERE %s"; 6280 const char *azCols[] = { 6281 "name", 6282 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" 6283 }; 6284 6285 char *zWhere = 0; 6286 sqlite3_stmt *pSql = 0; 6287 int rc; 6288 6289 rc = arCheckEntries(pAr); 6290 arWhereClause(&rc, pAr, &zWhere); 6291 6292 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], 6293 pAr->zSrcTable, zWhere); 6294 if( pAr->bDryRun ){ 6295 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 6296 }else{ 6297 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 6298 if( pAr->bVerbose ){ 6299 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", 6300 sqlite3_column_text(pSql, 0), 6301 sqlite3_column_int(pSql, 1), 6302 sqlite3_column_text(pSql, 2), 6303 sqlite3_column_text(pSql, 3) 6304 ); 6305 }else{ 6306 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 6307 } 6308 } 6309 } 6310 shellFinalize(&rc, pSql); 6311 sqlite3_free(zWhere); 6312 return rc; 6313} 6314 6315 6316/* 6317** Implementation of .ar "eXtract" command. 6318*/ 6319static int arExtractCommand(ArCommand *pAr){ 6320 const char *zSql1 = 6321 "SELECT " 6322 " ($dir || name)," 6323 " writefile(($dir || name), %s, mode, mtime) " 6324 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" 6325 " AND name NOT GLOB '*..[/\\]*'"; 6326 6327 const char *azExtraArg[] = { 6328 "sqlar_uncompress(data, sz)", 6329 "data" 6330 }; 6331 6332 sqlite3_stmt *pSql = 0; 6333 int rc = SQLITE_OK; 6334 char *zDir = 0; 6335 char *zWhere = 0; 6336 int i, j; 6337 6338 /* If arguments are specified, check that they actually exist within 6339 ** the archive before proceeding. And formulate a WHERE clause to 6340 ** match them. */ 6341 rc = arCheckEntries(pAr); 6342 arWhereClause(&rc, pAr, &zWhere); 6343 6344 if( rc==SQLITE_OK ){ 6345 if( pAr->zDir ){ 6346 zDir = sqlite3_mprintf("%s/", pAr->zDir); 6347 }else{ 6348 zDir = sqlite3_mprintf(""); 6349 } 6350 if( zDir==0 ) rc = SQLITE_NOMEM; 6351 } 6352 6353 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 6354 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere 6355 ); 6356 6357 if( rc==SQLITE_OK ){ 6358 j = sqlite3_bind_parameter_index(pSql, "$dir"); 6359 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); 6360 6361 /* Run the SELECT statement twice. The first time, writefile() is called 6362 ** for all archive members that should be extracted. The second time, 6363 ** only for the directories. This is because the timestamps for 6364 ** extracted directories must be reset after they are populated (as 6365 ** populating them changes the timestamp). */ 6366 for(i=0; i<2; i++){ 6367 j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); 6368 sqlite3_bind_int(pSql, j, i); 6369 if( pAr->bDryRun ){ 6370 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 6371 }else{ 6372 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 6373 if( i==0 && pAr->bVerbose ){ 6374 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 6375 } 6376 } 6377 } 6378 shellReset(&rc, pSql); 6379 } 6380 shellFinalize(&rc, pSql); 6381 } 6382 6383 sqlite3_free(zDir); 6384 sqlite3_free(zWhere); 6385 return rc; 6386} 6387 6388/* 6389** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. 6390*/ 6391static int arExecSql(ArCommand *pAr, const char *zSql){ 6392 int rc; 6393 if( pAr->bDryRun ){ 6394 utf8_printf(pAr->p->out, "%s\n", zSql); 6395 rc = SQLITE_OK; 6396 }else{ 6397 char *zErr = 0; 6398 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 6399 if( zErr ){ 6400 utf8_printf(stdout, "ERROR: %s\n", zErr); 6401 sqlite3_free(zErr); 6402 } 6403 } 6404 return rc; 6405} 6406 6407 6408/* 6409** Implementation of .ar "create", "insert", and "update" commands. 6410** 6411** create -> Create a new SQL archive 6412** insert -> Insert or reinsert all files listed 6413** update -> Insert files that have changed or that were not 6414** previously in the archive 6415** 6416** Create the "sqlar" table in the database if it does not already exist. 6417** Then add each file in the azFile[] array to the archive. Directories 6418** are added recursively. If argument bVerbose is non-zero, a message is 6419** printed on stdout for each file archived. 6420** 6421** The create command is the same as update, except that it drops 6422** any existing "sqlar" table before beginning. The "insert" command 6423** always overwrites every file named on the command-line, where as 6424** "update" only overwrites if the size or mtime or mode has changed. 6425*/ 6426static int arCreateOrUpdateCommand( 6427 ArCommand *pAr, /* Command arguments and options */ 6428 int bUpdate, /* true for a --create. */ 6429 int bOnlyIfChanged /* Only update if file has changed */ 6430){ 6431 const char *zCreate = 6432 "CREATE TABLE IF NOT EXISTS sqlar(\n" 6433 " name TEXT PRIMARY KEY, -- name of the file\n" 6434 " mode INT, -- access permissions\n" 6435 " mtime INT, -- last modification time\n" 6436 " sz INT, -- original file size\n" 6437 " data BLOB -- compressed content\n" 6438 ")"; 6439 const char *zDrop = "DROP TABLE IF EXISTS sqlar"; 6440 const char *zInsertFmt[2] = { 6441 "REPLACE INTO %s(name,mode,mtime,sz,data)\n" 6442 " SELECT\n" 6443 " %s,\n" 6444 " mode,\n" 6445 " mtime,\n" 6446 " CASE substr(lsmode(mode),1,1)\n" 6447 " WHEN '-' THEN length(data)\n" 6448 " WHEN 'd' THEN 0\n" 6449 " ELSE -1 END,\n" 6450 " sqlar_compress(data)\n" 6451 " FROM fsdir(%Q,%Q) AS disk\n" 6452 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 6453 , 6454 "REPLACE INTO %s(name,mode,mtime,data)\n" 6455 " SELECT\n" 6456 " %s,\n" 6457 " mode,\n" 6458 " mtime,\n" 6459 " data\n" 6460 " FROM fsdir(%Q,%Q) AS disk\n" 6461 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 6462 }; 6463 int i; /* For iterating through azFile[] */ 6464 int rc; /* Return code */ 6465 const char *zTab = 0; /* SQL table into which to insert */ 6466 char *zSql; 6467 char zTemp[50]; 6468 char *zExists = 0; 6469 6470 arExecSql(pAr, "PRAGMA page_size=512"); 6471 rc = arExecSql(pAr, "SAVEPOINT ar;"); 6472 if( rc!=SQLITE_OK ) return rc; 6473 zTemp[0] = 0; 6474 if( pAr->bZip ){ 6475 /* Initialize the zipfile virtual table, if necessary */ 6476 if( pAr->zFile ){ 6477 sqlite3_uint64 r; 6478 sqlite3_randomness(sizeof(r),&r); 6479 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); 6480 zTab = zTemp; 6481 zSql = sqlite3_mprintf( 6482 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", 6483 zTab, pAr->zFile 6484 ); 6485 rc = arExecSql(pAr, zSql); 6486 sqlite3_free(zSql); 6487 }else{ 6488 zTab = "zip"; 6489 } 6490 }else{ 6491 /* Initialize the table for an SQLAR */ 6492 zTab = "sqlar"; 6493 if( bUpdate==0 ){ 6494 rc = arExecSql(pAr, zDrop); 6495 if( rc!=SQLITE_OK ) goto end_ar_transaction; 6496 } 6497 rc = arExecSql(pAr, zCreate); 6498 } 6499 if( bOnlyIfChanged ){ 6500 zExists = sqlite3_mprintf( 6501 " AND NOT EXISTS(" 6502 "SELECT 1 FROM %s AS mem" 6503 " WHERE mem.name=disk.name" 6504 " AND mem.mtime=disk.mtime" 6505 " AND mem.mode=disk.mode)", zTab); 6506 }else{ 6507 zExists = sqlite3_mprintf(""); 6508 } 6509 if( zExists==0 ) rc = SQLITE_NOMEM; 6510 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 6511 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, 6512 pAr->bVerbose ? "shell_putsnl(name)" : "name", 6513 pAr->azArg[i], pAr->zDir, zExists); 6514 rc = arExecSql(pAr, zSql2); 6515 sqlite3_free(zSql2); 6516 } 6517end_ar_transaction: 6518 if( rc!=SQLITE_OK ){ 6519 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 6520 }else{ 6521 rc = arExecSql(pAr, "RELEASE ar;"); 6522 if( pAr->bZip && pAr->zFile ){ 6523 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); 6524 arExecSql(pAr, zSql); 6525 sqlite3_free(zSql); 6526 } 6527 } 6528 sqlite3_free(zExists); 6529 return rc; 6530} 6531 6532/* 6533** Implementation of ".ar" dot command. 6534*/ 6535static int arDotCommand( 6536 ShellState *pState, /* Current shell tool state */ 6537 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ 6538 char **azArg, /* Array of arguments passed to dot command */ 6539 int nArg /* Number of entries in azArg[] */ 6540){ 6541 ArCommand cmd; 6542 int rc; 6543 memset(&cmd, 0, sizeof(cmd)); 6544 cmd.fromCmdLine = fromCmdLine; 6545 rc = arParseCommand(azArg, nArg, &cmd); 6546 if( rc==SQLITE_OK ){ 6547 int eDbType = SHELL_OPEN_UNSPEC; 6548 cmd.p = pState; 6549 cmd.db = pState->db; 6550 if( cmd.zFile ){ 6551 eDbType = deduceDatabaseType(cmd.zFile, 1); 6552 }else{ 6553 eDbType = pState->openMode; 6554 } 6555 if( eDbType==SHELL_OPEN_ZIPFILE ){ 6556 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ 6557 if( cmd.zFile==0 ){ 6558 cmd.zSrcTable = sqlite3_mprintf("zip"); 6559 }else{ 6560 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); 6561 } 6562 } 6563 cmd.bZip = 1; 6564 }else if( cmd.zFile ){ 6565 int flags; 6566 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; 6567 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 6568 || cmd.eCmd==AR_CMD_UPDATE ){ 6569 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 6570 }else{ 6571 flags = SQLITE_OPEN_READONLY; 6572 } 6573 cmd.db = 0; 6574 if( cmd.bDryRun ){ 6575 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, 6576 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); 6577 } 6578 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 6579 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); 6580 if( rc!=SQLITE_OK ){ 6581 utf8_printf(stderr, "cannot open file: %s (%s)\n", 6582 cmd.zFile, sqlite3_errmsg(cmd.db) 6583 ); 6584 goto end_ar_command; 6585 } 6586 sqlite3_fileio_init(cmd.db, 0, 0); 6587 sqlite3_sqlar_init(cmd.db, 0, 0); 6588 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, 6589 shellPutsFunc, 0, 0); 6590 6591 } 6592 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ 6593 if( cmd.eCmd!=AR_CMD_CREATE 6594 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) 6595 ){ 6596 utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); 6597 rc = SQLITE_ERROR; 6598 goto end_ar_command; 6599 } 6600 cmd.zSrcTable = sqlite3_mprintf("sqlar"); 6601 } 6602 6603 switch( cmd.eCmd ){ 6604 case AR_CMD_CREATE: 6605 rc = arCreateOrUpdateCommand(&cmd, 0, 0); 6606 break; 6607 6608 case AR_CMD_EXTRACT: 6609 rc = arExtractCommand(&cmd); 6610 break; 6611 6612 case AR_CMD_LIST: 6613 rc = arListCommand(&cmd); 6614 break; 6615 6616 case AR_CMD_HELP: 6617 arUsage(pState->out); 6618 break; 6619 6620 case AR_CMD_INSERT: 6621 rc = arCreateOrUpdateCommand(&cmd, 1, 0); 6622 break; 6623 6624 default: 6625 assert( cmd.eCmd==AR_CMD_UPDATE ); 6626 rc = arCreateOrUpdateCommand(&cmd, 1, 1); 6627 break; 6628 } 6629 } 6630end_ar_command: 6631 if( cmd.db!=pState->db ){ 6632 close_db(cmd.db); 6633 } 6634 sqlite3_free(cmd.zSrcTable); 6635 6636 return rc; 6637} 6638/* End of the ".archive" or ".ar" command logic 6639*******************************************************************************/ 6640#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ 6641 6642#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 6643/* 6644** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op. 6645** Otherwise, the SQL statement or statements in zSql are executed using 6646** database connection db and the error code written to *pRc before 6647** this function returns. 6648*/ 6649static void shellExec(sqlite3 *db, int *pRc, const char *zSql){ 6650 int rc = *pRc; 6651 if( rc==SQLITE_OK ){ 6652 char *zErr = 0; 6653 rc = sqlite3_exec(db, zSql, 0, 0, &zErr); 6654 if( rc!=SQLITE_OK ){ 6655 raw_printf(stderr, "SQL error: %s\n", zErr); 6656 } 6657 *pRc = rc; 6658 } 6659} 6660 6661/* 6662** Like shellExec(), except that zFmt is a printf() style format string. 6663*/ 6664static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){ 6665 char *z = 0; 6666 if( *pRc==SQLITE_OK ){ 6667 va_list ap; 6668 va_start(ap, zFmt); 6669 z = sqlite3_vmprintf(zFmt, ap); 6670 va_end(ap); 6671 if( z==0 ){ 6672 *pRc = SQLITE_NOMEM; 6673 }else{ 6674 shellExec(db, pRc, z); 6675 } 6676 sqlite3_free(z); 6677 } 6678} 6679 6680/* 6681** If *pRc is not SQLITE_OK when this function is called, it is a no-op. 6682** Otherwise, an attempt is made to allocate, zero and return a pointer 6683** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set 6684** to SQLITE_NOMEM and NULL returned. 6685*/ 6686static void *shellMalloc(int *pRc, sqlite3_int64 nByte){ 6687 void *pRet = 0; 6688 if( *pRc==SQLITE_OK ){ 6689 pRet = sqlite3_malloc64(nByte); 6690 if( pRet==0 ){ 6691 *pRc = SQLITE_NOMEM; 6692 }else{ 6693 memset(pRet, 0, nByte); 6694 } 6695 } 6696 return pRet; 6697} 6698 6699/* 6700** If *pRc is not SQLITE_OK when this function is called, it is a no-op. 6701** Otherwise, zFmt is treated as a printf() style string. The result of 6702** formatting it along with any trailing arguments is written into a 6703** buffer obtained from sqlite3_malloc(), and pointer to which is returned. 6704** It is the responsibility of the caller to eventually free this buffer 6705** using a call to sqlite3_free(). 6706** 6707** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 6708** pointer returned. 6709*/ 6710static char *shellMPrintf(int *pRc, const char *zFmt, ...){ 6711 char *z = 0; 6712 if( *pRc==SQLITE_OK ){ 6713 va_list ap; 6714 va_start(ap, zFmt); 6715 z = sqlite3_vmprintf(zFmt, ap); 6716 va_end(ap); 6717 if( z==0 ){ 6718 *pRc = SQLITE_NOMEM; 6719 } 6720 } 6721 return z; 6722} 6723 6724/* 6725** When running the ".recover" command, each output table, and the special 6726** orphaned row table if it is required, is represented by an instance 6727** of the following struct. 6728*/ 6729typedef struct RecoverTable RecoverTable; 6730struct RecoverTable { 6731 char *zQuoted; /* Quoted version of table name */ 6732 int nCol; /* Number of columns in table */ 6733 char **azlCol; /* Array of column lists */ 6734 int iPk; /* Index of IPK column */ 6735}; 6736 6737/* 6738** Free a RecoverTable object allocated by recoverFindTable() or 6739** recoverOrphanTable(). 6740*/ 6741static void recoverFreeTable(RecoverTable *pTab){ 6742 if( pTab ){ 6743 sqlite3_free(pTab->zQuoted); 6744 if( pTab->azlCol ){ 6745 int i; 6746 for(i=0; i<=pTab->nCol; i++){ 6747 sqlite3_free(pTab->azlCol[i]); 6748 } 6749 sqlite3_free(pTab->azlCol); 6750 } 6751 sqlite3_free(pTab); 6752 } 6753} 6754 6755/* 6756** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. 6757** Otherwise, it allocates and returns a RecoverTable object based on the 6758** final four arguments passed to this function. It is the responsibility 6759** of the caller to eventually free the returned object using 6760** recoverFreeTable(). 6761*/ 6762static RecoverTable *recoverNewTable( 6763 int *pRc, /* IN/OUT: Error code */ 6764 const char *zName, /* Name of table */ 6765 const char *zSql, /* CREATE TABLE statement */ 6766 int bIntkey, 6767 int nCol 6768){ 6769 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */ 6770 int rc = *pRc; 6771 RecoverTable *pTab = 0; 6772 6773 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable)); 6774 if( rc==SQLITE_OK ){ 6775 int nSqlCol = 0; 6776 int bSqlIntkey = 0; 6777 sqlite3_stmt *pStmt = 0; 6778 6779 rc = sqlite3_open("", &dbtmp); 6780 if( rc==SQLITE_OK ){ 6781 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0, 6782 shellIdQuote, 0, 0); 6783 } 6784 if( rc==SQLITE_OK ){ 6785 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0); 6786 } 6787 if( rc==SQLITE_OK ){ 6788 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0); 6789 if( rc==SQLITE_ERROR ){ 6790 rc = SQLITE_OK; 6791 goto finished; 6792 } 6793 } 6794 shellPreparePrintf(dbtmp, &rc, &pStmt, 6795 "SELECT count(*) FROM pragma_table_info(%Q)", zName 6796 ); 6797 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6798 nSqlCol = sqlite3_column_int(pStmt, 0); 6799 } 6800 shellFinalize(&rc, pStmt); 6801 6802 if( rc!=SQLITE_OK || nSqlCol<nCol ){ 6803 goto finished; 6804 } 6805 6806 shellPreparePrintf(dbtmp, &rc, &pStmt, 6807 "SELECT (" 6808 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage" 6809 ") FROM sqlite_master WHERE name = %Q", zName 6810 ); 6811 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6812 bSqlIntkey = sqlite3_column_int(pStmt, 0); 6813 } 6814 shellFinalize(&rc, pStmt); 6815 6816 if( bIntkey==bSqlIntkey ){ 6817 int i; 6818 const char *zPk = "_rowid_"; 6819 sqlite3_stmt *pPkFinder = 0; 6820 6821 /* If this is an intkey table and there is an INTEGER PRIMARY KEY, 6822 ** set zPk to the name of the PK column, and pTab->iPk to the index 6823 ** of the column, where columns are 0-numbered from left to right. 6824 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column, 6825 ** leave zPk as "_rowid_" and pTab->iPk at -2. */ 6826 pTab->iPk = -2; 6827 if( bIntkey ){ 6828 shellPreparePrintf(dbtmp, &rc, &pPkFinder, 6829 "SELECT cid, name FROM pragma_table_info(%Q) " 6830 " WHERE pk=1 AND type='integer' COLLATE nocase" 6831 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)" 6832 , zName, zName 6833 ); 6834 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){ 6835 pTab->iPk = sqlite3_column_int(pPkFinder, 0); 6836 zPk = (const char*)sqlite3_column_text(pPkFinder, 1); 6837 } 6838 } 6839 6840 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName); 6841 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1)); 6842 pTab->nCol = nSqlCol; 6843 6844 if( bIntkey ){ 6845 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk); 6846 }else{ 6847 pTab->azlCol[0] = shellMPrintf(&rc, ""); 6848 } 6849 i = 1; 6850 shellPreparePrintf(dbtmp, &rc, &pStmt, 6851 "SELECT %Q || group_concat(shell_idquote(name), ', ') " 6852 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) " 6853 "FROM pragma_table_info(%Q)", 6854 bIntkey ? ", " : "", pTab->iPk, 6855 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ", 6856 zName 6857 ); 6858 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6859 const char *zText = (const char*)sqlite3_column_text(pStmt, 0); 6860 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText); 6861 i++; 6862 } 6863 shellFinalize(&rc, pStmt); 6864 6865 shellFinalize(&rc, pPkFinder); 6866 } 6867 } 6868 6869 finished: 6870 sqlite3_close(dbtmp); 6871 *pRc = rc; 6872 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){ 6873 recoverFreeTable(pTab); 6874 pTab = 0; 6875 } 6876 return pTab; 6877} 6878 6879/* 6880** This function is called to search the schema recovered from the 6881** sqlite_master table of the (possibly) corrupt database as part 6882** of a ".recover" command. Specifically, for a table with root page 6883** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the 6884** table must be a WITHOUT ROWID table, or if non-zero, not one of 6885** those. 6886** 6887** If a table is found, a (RecoverTable*) object is returned. Or, if 6888** no such table is found, but bIntkey is false and iRoot is the 6889** root page of an index in the recovered schema, then (*pbNoop) is 6890** set to true and NULL returned. Or, if there is no such table or 6891** index, NULL is returned and (*pbNoop) set to 0, indicating that 6892** the caller should write data to the orphans table. 6893*/ 6894static RecoverTable *recoverFindTable( 6895 ShellState *pState, /* Shell state object */ 6896 int *pRc, /* IN/OUT: Error code */ 6897 int iRoot, /* Root page of table */ 6898 int bIntkey, /* True for an intkey table */ 6899 int nCol, /* Number of columns in table */ 6900 int *pbNoop /* OUT: True if iRoot is root of index */ 6901){ 6902 sqlite3_stmt *pStmt = 0; 6903 RecoverTable *pRet = 0; 6904 int bNoop = 0; 6905 const char *zSql = 0; 6906 const char *zName = 0; 6907 6908 /* Search the recovered schema for an object with root page iRoot. */ 6909 shellPreparePrintf(pState->db, pRc, &pStmt, 6910 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot 6911 ); 6912 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6913 const char *zType = (const char*)sqlite3_column_text(pStmt, 0); 6914 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){ 6915 bNoop = 1; 6916 break; 6917 } 6918 if( sqlite3_stricmp(zType, "table")==0 ){ 6919 zName = (const char*)sqlite3_column_text(pStmt, 1); 6920 zSql = (const char*)sqlite3_column_text(pStmt, 2); 6921 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol); 6922 break; 6923 } 6924 } 6925 6926 shellFinalize(pRc, pStmt); 6927 *pbNoop = bNoop; 6928 return pRet; 6929} 6930 6931/* 6932** Return a RecoverTable object representing the orphans table. 6933*/ 6934static RecoverTable *recoverOrphanTable( 6935 ShellState *pState, /* Shell state object */ 6936 int *pRc, /* IN/OUT: Error code */ 6937 const char *zLostAndFound, /* Base name for orphans table */ 6938 int nCol /* Number of user data columns */ 6939){ 6940 RecoverTable *pTab = 0; 6941 if( nCol>=0 && *pRc==SQLITE_OK ){ 6942 int i; 6943 6944 /* This block determines the name of the orphan table. The prefered 6945 ** name is zLostAndFound. But if that clashes with another name 6946 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1 6947 ** and so on until a non-clashing name is found. */ 6948 int iTab = 0; 6949 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound); 6950 sqlite3_stmt *pTest = 0; 6951 shellPrepare(pState->db, pRc, 6952 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest 6953 ); 6954 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); 6955 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){ 6956 shellReset(pRc, pTest); 6957 sqlite3_free(zTab); 6958 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++); 6959 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); 6960 } 6961 shellFinalize(pRc, pTest); 6962 6963 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable)); 6964 if( pTab ){ 6965 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab); 6966 pTab->nCol = nCol; 6967 pTab->iPk = -2; 6968 if( nCol>0 ){ 6969 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1)); 6970 if( pTab->azlCol ){ 6971 pTab->azlCol[nCol] = shellMPrintf(pRc, ""); 6972 for(i=nCol-1; i>=0; i--){ 6973 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]); 6974 } 6975 } 6976 } 6977 6978 if( *pRc!=SQLITE_OK ){ 6979 recoverFreeTable(pTab); 6980 pTab = 0; 6981 }else{ 6982 raw_printf(pState->out, 6983 "CREATE TABLE %s(rootpgno INTEGER, " 6984 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted 6985 ); 6986 for(i=0; i<nCol; i++){ 6987 raw_printf(pState->out, ", c%d", i); 6988 } 6989 raw_printf(pState->out, ");\n"); 6990 } 6991 } 6992 sqlite3_free(zTab); 6993 } 6994 return pTab; 6995} 6996 6997/* 6998** This function is called to recover data from the database. A script 6999** to construct a new database containing all recovered data is output 7000** on stream pState->out. 7001*/ 7002static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ 7003 int rc = SQLITE_OK; 7004 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */ 7005 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */ 7006 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */ 7007 const char *zRecoveryDb = ""; /* Name of "recovery" database */ 7008 const char *zLostAndFound = "lost_and_found"; 7009 int i; 7010 int nOrphan = -1; 7011 RecoverTable *pOrphan = 0; 7012 7013 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ 7014 int bRowids = 1; /* 0 if --no-rowids */ 7015 for(i=1; i<nArg; i++){ 7016 char *z = azArg[i]; 7017 int n; 7018 if( z[0]=='-' && z[1]=='-' ) z++; 7019 n = strlen30(z); 7020 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ 7021 bFreelist = 0; 7022 }else 7023 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ 7024 i++; 7025 zRecoveryDb = azArg[i]; 7026 }else 7027 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ 7028 i++; 7029 zLostAndFound = azArg[i]; 7030 }else 7031 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ 7032 bRowids = 0; 7033 } 7034 else{ 7035 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 7036 showHelp(pState->out, azArg[0]); 7037 return 1; 7038 } 7039 } 7040 7041 shellExecPrintf(pState->db, &rc, 7042 /* Attach an in-memory database named 'recovery'. Create an indexed 7043 ** cache of the sqlite_dbptr virtual table. */ 7044 "PRAGMA writable_schema = on;" 7045 "ATTACH %Q AS recovery;" 7046 "DROP TABLE IF EXISTS recovery.dbptr;" 7047 "DROP TABLE IF EXISTS recovery.freelist;" 7048 "DROP TABLE IF EXISTS recovery.map;" 7049 "DROP TABLE IF EXISTS recovery.schema;" 7050 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb 7051 ); 7052 7053 if( bFreelist ){ 7054 shellExec(pState->db, &rc, 7055 "WITH trunk(pgno) AS (" 7056 " SELECT shell_int32(" 7057 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x " 7058 " WHERE x>0" 7059 " UNION" 7060 " SELECT shell_int32(" 7061 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x " 7062 " FROM trunk WHERE x>0" 7063 ")," 7064 "freelist(data, n, freepgno) AS (" 7065 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno " 7066 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno" 7067 " UNION ALL" 7068 " SELECT data, n-1, shell_int32(data, 2+n) " 7069 " FROM freelist WHERE n>=0" 7070 ")" 7071 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;" 7072 ); 7073 } 7074 7075 /* If this is an auto-vacuum database, add all pointer-map pages to 7076 ** the freelist table. Do this regardless of whether or not 7077 ** --freelist-corrupt was specified. */ 7078 shellExec(pState->db, &rc, 7079 "WITH ptrmap(pgno) AS (" 7080 " SELECT 2 WHERE shell_int32(" 7081 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13" 7082 " )" 7083 " UNION ALL " 7084 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp " 7085 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)" 7086 ")" 7087 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap" 7088 ); 7089 7090 shellExec(pState->db, &rc, 7091 "CREATE TABLE recovery.dbptr(" 7092 " pgno, child, PRIMARY KEY(child, pgno)" 7093 ") WITHOUT ROWID;" 7094 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) " 7095 " SELECT * FROM sqlite_dbptr" 7096 " WHERE pgno NOT IN freelist AND child NOT IN freelist;" 7097 7098 /* Delete any pointer to page 1. This ensures that page 1 is considered 7099 ** a root page, regardless of how corrupt the db is. */ 7100 "DELETE FROM recovery.dbptr WHERE child = 1;" 7101 7102 /* Delete all pointers to any pages that have more than one pointer 7103 ** to them. Such pages will be treated as root pages when recovering 7104 ** data. */ 7105 "DELETE FROM recovery.dbptr WHERE child IN (" 7106 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1" 7107 ");" 7108 7109 /* Create the "map" table that will (eventually) contain instructions 7110 ** for dealing with each page in the db that contains one or more 7111 ** records. */ 7112 "CREATE TABLE recovery.map(" 7113 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT" 7114 ");" 7115 7116 /* Populate table [map]. If there are circular loops of pages in the 7117 ** database, the following adds all pages in such a loop to the map 7118 ** as individual root pages. This could be handled better. */ 7119 "WITH pages(i, maxlen) AS (" 7120 " SELECT page_count, (" 7121 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count" 7122 " ) FROM pragma_page_count WHERE page_count>0" 7123 " UNION ALL" 7124 " SELECT i-1, (" 7125 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1" 7126 " ) FROM pages WHERE i>=2" 7127 ")" 7128 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) " 7129 " SELECT i, maxlen, NULL, (" 7130 " WITH p(orig, pgno, parent) AS (" 7131 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)" 7132 " UNION " 7133 " SELECT i, p.parent, " 7134 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p" 7135 " )" 7136 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)" 7137 ") " 7138 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;" 7139 "UPDATE recovery.map AS o SET intkey = (" 7140 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno" 7141 ");" 7142 7143 /* Extract data from page 1 and any linked pages into table 7144 ** recovery.schema. With the same schema as an sqlite_master table. */ 7145 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);" 7146 "INSERT INTO recovery.schema SELECT " 7147 " max(CASE WHEN field=0 THEN value ELSE NULL END)," 7148 " max(CASE WHEN field=1 THEN value ELSE NULL END)," 7149 " max(CASE WHEN field=2 THEN value ELSE NULL END)," 7150 " max(CASE WHEN field=3 THEN value ELSE NULL END)," 7151 " max(CASE WHEN field=4 THEN value ELSE NULL END)" 7152 "FROM sqlite_dbdata WHERE pgno IN (" 7153 " SELECT pgno FROM recovery.map WHERE root=1" 7154 ")" 7155 "GROUP BY pgno, cell;" 7156 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);" 7157 ); 7158 7159 /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 7160 ** CREATE TABLE statements that extracted from the existing schema. */ 7161 if( rc==SQLITE_OK ){ 7162 sqlite3_stmt *pStmt = 0; 7163 /* ".recover" might output content in an order which causes immediate 7164 ** foreign key constraints to be violated. So disable foreign-key 7165 ** constraint enforcement to prevent problems when running the output 7166 ** script. */ 7167 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n"); 7168 raw_printf(pState->out, "BEGIN;\n"); 7169 raw_printf(pState->out, "PRAGMA writable_schema = on;\n"); 7170 shellPrepare(pState->db, &rc, 7171 "SELECT sql FROM recovery.schema " 7172 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt 7173 ); 7174 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 7175 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0); 7176 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 7177 &zCreateTable[12] 7178 ); 7179 } 7180 shellFinalize(&rc, pStmt); 7181 } 7182 7183 /* Figure out if an orphan table will be required. And if so, how many 7184 ** user columns it should contain */ 7185 shellPrepare(pState->db, &rc, 7186 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1" 7187 , &pLoop 7188 ); 7189 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ 7190 nOrphan = sqlite3_column_int(pLoop, 0); 7191 } 7192 shellFinalize(&rc, pLoop); 7193 pLoop = 0; 7194 7195 shellPrepare(pState->db, &rc, 7196 "SELECT pgno FROM recovery.map WHERE root=?", &pPages 7197 ); 7198 7199 shellPrepare(pState->db, &rc, 7200 "SELECT max(field), group_concat(shell_escape_crnl(quote" 7201 "(case when (? AND field<0) then NULL else value end)" 7202 "), ', ')" 7203 ", min(field) " 7204 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?" 7205 "GROUP BY cell", &pCells 7206 ); 7207 7208 /* Loop through each root page. */ 7209 shellPrepare(pState->db, &rc, 7210 "SELECT root, intkey, max(maxlen) FROM recovery.map" 7211 " WHERE root>1 GROUP BY root, intkey ORDER BY root=(" 7212 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'" 7213 ")", &pLoop 7214 ); 7215 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ 7216 int iRoot = sqlite3_column_int(pLoop, 0); 7217 int bIntkey = sqlite3_column_int(pLoop, 1); 7218 int nCol = sqlite3_column_int(pLoop, 2); 7219 int bNoop = 0; 7220 RecoverTable *pTab; 7221 7222 assert( bIntkey==0 || bIntkey==1 ); 7223 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop); 7224 if( bNoop || rc ) continue; 7225 if( pTab==0 ){ 7226 if( pOrphan==0 ){ 7227 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); 7228 } 7229 pTab = pOrphan; 7230 if( pTab==0 ) break; 7231 } 7232 7233 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){ 7234 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); 7235 } 7236 sqlite3_bind_int(pPages, 1, iRoot); 7237 if( bRowids==0 && pTab->iPk<0 ){ 7238 sqlite3_bind_int(pCells, 1, 1); 7239 }else{ 7240 sqlite3_bind_int(pCells, 1, 0); 7241 } 7242 sqlite3_bind_int(pCells, 3, pTab->iPk); 7243 7244 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){ 7245 int iPgno = sqlite3_column_int(pPages, 0); 7246 sqlite3_bind_int(pCells, 2, iPgno); 7247 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){ 7248 int nField = sqlite3_column_int(pCells, 0); 7249 int iMin = sqlite3_column_int(pCells, 2); 7250 const char *zVal = (const char*)sqlite3_column_text(pCells, 1); 7251 7252 RecoverTable *pTab2 = pTab; 7253 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){ 7254 if( pOrphan==0 ){ 7255 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); 7256 } 7257 pTab2 = pOrphan; 7258 if( pTab2==0 ) break; 7259 } 7260 7261 nField = nField+1; 7262 if( pTab2==pOrphan ){ 7263 raw_printf(pState->out, 7264 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n", 7265 pTab2->zQuoted, iRoot, iPgno, nField, 7266 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField] 7267 ); 7268 }else{ 7269 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 7270 pTab2->zQuoted, pTab2->azlCol[nField], zVal 7271 ); 7272 } 7273 } 7274 shellReset(&rc, pCells); 7275 } 7276 shellReset(&rc, pPages); 7277 if( pTab!=pOrphan ) recoverFreeTable(pTab); 7278 } 7279 shellFinalize(&rc, pLoop); 7280 shellFinalize(&rc, pPages); 7281 shellFinalize(&rc, pCells); 7282 recoverFreeTable(pOrphan); 7283 7284 /* The rest of the schema */ 7285 if( rc==SQLITE_OK ){ 7286 sqlite3_stmt *pStmt = 0; 7287 shellPrepare(pState->db, &rc, 7288 "SELECT sql, name FROM recovery.schema " 7289 "WHERE sql NOT LIKE 'create table%'", &pStmt 7290 ); 7291 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 7292 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0); 7293 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){ 7294 const char *zName = (const char*)sqlite3_column_text(pStmt, 1); 7295 char *zPrint = shellMPrintf(&rc, 7296 "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)", 7297 zName, zName, zSql 7298 ); 7299 raw_printf(pState->out, "%s;\n", zPrint); 7300 sqlite3_free(zPrint); 7301 }else{ 7302 raw_printf(pState->out, "%s;\n", zSql); 7303 } 7304 } 7305 shellFinalize(&rc, pStmt); 7306 } 7307 7308 if( rc==SQLITE_OK ){ 7309 raw_printf(pState->out, "PRAGMA writable_schema = off;\n"); 7310 raw_printf(pState->out, "COMMIT;\n"); 7311 } 7312 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0); 7313 return rc; 7314} 7315#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7316 7317 7318/* 7319** If an input line begins with "." then invoke this routine to 7320** process that line. 7321** 7322** Return 1 on error, 2 to exit, and 0 otherwise. 7323*/ 7324static int do_meta_command(char *zLine, ShellState *p){ 7325 int h = 1; 7326 int nArg = 0; 7327 int n, c; 7328 int rc = 0; 7329 char *azArg[52]; 7330 7331#ifndef SQLITE_OMIT_VIRTUALTABLE 7332 if( p->expert.pExpert ){ 7333 expertFinish(p, 1, 0); 7334 } 7335#endif 7336 7337 /* Parse the input line into tokens. 7338 */ 7339 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ 7340 while( IsSpace(zLine[h]) ){ h++; } 7341 if( zLine[h]==0 ) break; 7342 if( zLine[h]=='\'' || zLine[h]=='"' ){ 7343 int delim = zLine[h++]; 7344 azArg[nArg++] = &zLine[h]; 7345 while( zLine[h] && zLine[h]!=delim ){ 7346 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; 7347 h++; 7348 } 7349 if( zLine[h]==delim ){ 7350 zLine[h++] = 0; 7351 } 7352 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); 7353 }else{ 7354 azArg[nArg++] = &zLine[h]; 7355 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } 7356 if( zLine[h] ) zLine[h++] = 0; 7357 resolve_backslashes(azArg[nArg-1]); 7358 } 7359 } 7360 azArg[nArg] = 0; 7361 7362 /* Process the input line. 7363 */ 7364 if( nArg==0 ) return 0; /* no tokens, no error */ 7365 n = strlen30(azArg[0]); 7366 c = azArg[0][0]; 7367 clearTempFile(p); 7368 7369#ifndef SQLITE_OMIT_AUTHORIZATION 7370 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ 7371 if( nArg!=2 ){ 7372 raw_printf(stderr, "Usage: .auth ON|OFF\n"); 7373 rc = 1; 7374 goto meta_command_exit; 7375 } 7376 open_db(p, 0); 7377 if( booleanValue(azArg[1]) ){ 7378 sqlite3_set_authorizer(p->db, shellAuth, p); 7379 }else{ 7380 sqlite3_set_authorizer(p->db, 0, 0); 7381 } 7382 }else 7383#endif 7384 7385#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 7386 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ 7387 open_db(p, 0); 7388 rc = arDotCommand(p, 0, azArg, nArg); 7389 }else 7390#endif 7391 7392 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) 7393 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) 7394 ){ 7395 const char *zDestFile = 0; 7396 const char *zDb = 0; 7397 sqlite3 *pDest; 7398 sqlite3_backup *pBackup; 7399 int j; 7400 int bAsync = 0; 7401 const char *zVfs = 0; 7402 for(j=1; j<nArg; j++){ 7403 const char *z = azArg[j]; 7404 if( z[0]=='-' ){ 7405 if( z[1]=='-' ) z++; 7406 if( strcmp(z, "-append")==0 ){ 7407 zVfs = "apndvfs"; 7408 }else 7409 if( strcmp(z, "-async")==0 ){ 7410 bAsync = 1; 7411 }else 7412 { 7413 utf8_printf(stderr, "unknown option: %s\n", azArg[j]); 7414 return 1; 7415 } 7416 }else if( zDestFile==0 ){ 7417 zDestFile = azArg[j]; 7418 }else if( zDb==0 ){ 7419 zDb = zDestFile; 7420 zDestFile = azArg[j]; 7421 }else{ 7422 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); 7423 return 1; 7424 } 7425 } 7426 if( zDestFile==0 ){ 7427 raw_printf(stderr, "missing FILENAME argument on .backup\n"); 7428 return 1; 7429 } 7430 if( zDb==0 ) zDb = "main"; 7431 rc = sqlite3_open_v2(zDestFile, &pDest, 7432 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); 7433 if( rc!=SQLITE_OK ){ 7434 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); 7435 close_db(pDest); 7436 return 1; 7437 } 7438 if( bAsync ){ 7439 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", 7440 0, 0, 0); 7441 } 7442 open_db(p, 0); 7443 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); 7444 if( pBackup==0 ){ 7445 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7446 close_db(pDest); 7447 return 1; 7448 } 7449 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 7450 sqlite3_backup_finish(pBackup); 7451 if( rc==SQLITE_DONE ){ 7452 rc = 0; 7453 }else{ 7454 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7455 rc = 1; 7456 } 7457 close_db(pDest); 7458 }else 7459 7460 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ 7461 if( nArg==2 ){ 7462 bail_on_error = booleanValue(azArg[1]); 7463 }else{ 7464 raw_printf(stderr, "Usage: .bail on|off\n"); 7465 rc = 1; 7466 } 7467 }else 7468 7469 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ 7470 if( nArg==2 ){ 7471 if( booleanValue(azArg[1]) ){ 7472 setBinaryMode(p->out, 1); 7473 }else{ 7474 setTextMode(p->out, 1); 7475 } 7476 }else{ 7477 raw_printf(stderr, "Usage: .binary on|off\n"); 7478 rc = 1; 7479 } 7480 }else 7481 7482 if( c=='c' && strcmp(azArg[0],"cd")==0 ){ 7483 if( nArg==2 ){ 7484#if defined(_WIN32) || defined(WIN32) 7485 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); 7486 rc = !SetCurrentDirectoryW(z); 7487 sqlite3_free(z); 7488#else 7489 rc = chdir(azArg[1]); 7490#endif 7491 if( rc ){ 7492 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); 7493 rc = 1; 7494 } 7495 }else{ 7496 raw_printf(stderr, "Usage: .cd DIRECTORY\n"); 7497 rc = 1; 7498 } 7499 }else 7500 7501 /* The undocumented ".breakpoint" command causes a call to the no-op 7502 ** routine named test_breakpoint(). 7503 */ 7504 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ 7505 test_breakpoint(); 7506 }else 7507 7508 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ 7509 if( nArg==2 ){ 7510 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); 7511 }else{ 7512 raw_printf(stderr, "Usage: .changes on|off\n"); 7513 rc = 1; 7514 } 7515 }else 7516 7517 /* Cancel output redirection, if it is currently set (by .testcase) 7518 ** Then read the content of the testcase-out.txt file and compare against 7519 ** azArg[1]. If there are differences, report an error and exit. 7520 */ 7521 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ 7522 char *zRes = 0; 7523 output_reset(p); 7524 if( nArg!=2 ){ 7525 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); 7526 rc = 2; 7527 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ 7528 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); 7529 rc = 2; 7530 }else if( testcase_glob(azArg[1],zRes)==0 ){ 7531 utf8_printf(stderr, 7532 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", 7533 p->zTestcase, azArg[1], zRes); 7534 rc = 1; 7535 }else{ 7536 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); 7537 p->nCheck++; 7538 } 7539 sqlite3_free(zRes); 7540 }else 7541 7542 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ 7543 if( nArg==2 ){ 7544 tryToClone(p, azArg[1]); 7545 }else{ 7546 raw_printf(stderr, "Usage: .clone FILENAME\n"); 7547 rc = 1; 7548 } 7549 }else 7550 7551 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ 7552 ShellState data; 7553 char *zErrMsg = 0; 7554 open_db(p, 0); 7555 memcpy(&data, p, sizeof(data)); 7556 data.showHeader = 0; 7557 data.cMode = data.mode = MODE_List; 7558 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); 7559 data.cnt = 0; 7560 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", 7561 callback, &data, &zErrMsg); 7562 if( zErrMsg ){ 7563 utf8_printf(stderr,"Error: %s\n", zErrMsg); 7564 sqlite3_free(zErrMsg); 7565 rc = 1; 7566 } 7567 }else 7568 7569 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ 7570 static const struct DbConfigChoices { 7571 const char *zName; 7572 int op; 7573 } aDbConfig[] = { 7574 { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, 7575 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, 7576 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, 7577 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, 7578 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, 7579 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 7580 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, 7581 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 7582 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, 7583 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, 7584 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, 7585 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, 7586 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, 7587 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, 7588 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, 7589 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 7590 }; 7591 int ii, v; 7592 open_db(p, 0); 7593 for(ii=0; ii<ArraySize(aDbConfig); ii++){ 7594 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; 7595 if( nArg>=3 ){ 7596 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); 7597 } 7598 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); 7599 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); 7600 if( nArg>1 ) break; 7601 } 7602 if( nArg>1 && ii==ArraySize(aDbConfig) ){ 7603 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); 7604 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); 7605 } 7606 }else 7607 7608 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ 7609 rc = shell_dbinfo_command(p, nArg, azArg); 7610 }else 7611 7612#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 7613 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ 7614 open_db(p, 0); 7615 rc = recoverDatabaseCmd(p, nArg, azArg); 7616 }else 7617#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7618 7619 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ 7620 char *zLike = 0; 7621 char *zSql; 7622 int i; 7623 int savedShowHeader = p->showHeader; 7624 int savedShellFlags = p->shellFlgs; 7625 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo); 7626 for(i=1; i<nArg; i++){ 7627 if( azArg[i][0]=='-' ){ 7628 const char *z = azArg[i]+1; 7629 if( z[0]=='-' ) z++; 7630 if( strcmp(z,"preserve-rowids")==0 ){ 7631#ifdef SQLITE_OMIT_VIRTUALTABLE 7632 raw_printf(stderr, "The --preserve-rowids option is not compatible" 7633 " with SQLITE_OMIT_VIRTUALTABLE\n"); 7634 rc = 1; 7635 sqlite3_free(zLike); 7636 goto meta_command_exit; 7637#else 7638 ShellSetFlag(p, SHFLG_PreserveRowid); 7639#endif 7640 }else 7641 if( strcmp(z,"newlines")==0 ){ 7642 ShellSetFlag(p, SHFLG_Newlines); 7643 }else 7644 { 7645 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); 7646 rc = 1; 7647 sqlite3_free(zLike); 7648 goto meta_command_exit; 7649 } 7650 }else if( zLike ){ 7651 zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'", 7652 zLike, azArg[i]); 7653 }else{ 7654 zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]); 7655 } 7656 } 7657 7658 open_db(p, 0); 7659 7660 /* When playing back a "dump", the content might appear in an order 7661 ** which causes immediate foreign key constraints to be violated. 7662 ** So disable foreign-key constraint enforcement to prevent problems. */ 7663 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); 7664 raw_printf(p->out, "BEGIN TRANSACTION;\n"); 7665 p->writableSchema = 0; 7666 p->showHeader = 0; 7667 /* Set writable_schema=ON since doing so forces SQLite to initialize 7668 ** as much of the schema as it can even if the sqlite_master table is 7669 ** corrupt. */ 7670 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); 7671 p->nErr = 0; 7672 if( zLike==0 ) zLike = sqlite3_mprintf("true"); 7673 zSql = sqlite3_mprintf( 7674 "SELECT name, type, sql FROM sqlite_master " 7675 "WHERE (%s) AND type=='table'" 7676 " AND sql NOT NULL" 7677 " ORDER BY tbl_name='sqlite_sequence', rowid", 7678 zLike 7679 ); 7680 run_schema_dump_query(p,zSql); 7681 sqlite3_free(zSql); 7682 zSql = sqlite3_mprintf( 7683 "SELECT sql FROM sqlite_master " 7684 "WHERE (%s) AND sql NOT NULL" 7685 " AND type IN ('index','trigger','view')", 7686 zLike 7687 ); 7688 run_table_dump_query(p, zSql); 7689 sqlite3_free(zSql); 7690 sqlite3_free(zLike); 7691 if( p->writableSchema ){ 7692 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); 7693 p->writableSchema = 0; 7694 } 7695 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 7696 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); 7697 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); 7698 p->showHeader = savedShowHeader; 7699 p->shellFlgs = savedShellFlags; 7700 }else 7701 7702 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ 7703 if( nArg==2 ){ 7704 setOrClearFlag(p, SHFLG_Echo, azArg[1]); 7705 }else{ 7706 raw_printf(stderr, "Usage: .echo on|off\n"); 7707 rc = 1; 7708 } 7709 }else 7710 7711 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ 7712 if( nArg==2 ){ 7713 p->autoEQPtest = 0; 7714 if( p->autoEQPtrace ){ 7715 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); 7716 p->autoEQPtrace = 0; 7717 } 7718 if( strcmp(azArg[1],"full")==0 ){ 7719 p->autoEQP = AUTOEQP_full; 7720 }else if( strcmp(azArg[1],"trigger")==0 ){ 7721 p->autoEQP = AUTOEQP_trigger; 7722#ifdef SQLITE_DEBUG 7723 }else if( strcmp(azArg[1],"test")==0 ){ 7724 p->autoEQP = AUTOEQP_on; 7725 p->autoEQPtest = 1; 7726 }else if( strcmp(azArg[1],"trace")==0 ){ 7727 p->autoEQP = AUTOEQP_full; 7728 p->autoEQPtrace = 1; 7729 open_db(p, 0); 7730 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0); 7731 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); 7732#endif 7733 }else{ 7734 p->autoEQP = (u8)booleanValue(azArg[1]); 7735 } 7736 }else{ 7737 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); 7738 rc = 1; 7739 } 7740 }else 7741 7742 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ 7743 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); 7744 rc = 2; 7745 }else 7746 7747 /* The ".explain" command is automatic now. It is largely pointless. It 7748 ** retained purely for backwards compatibility */ 7749 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ 7750 int val = 1; 7751 if( nArg>=2 ){ 7752 if( strcmp(azArg[1],"auto")==0 ){ 7753 val = 99; 7754 }else{ 7755 val = booleanValue(azArg[1]); 7756 } 7757 } 7758 if( val==1 && p->mode!=MODE_Explain ){ 7759 p->normalMode = p->mode; 7760 p->mode = MODE_Explain; 7761 p->autoExplain = 0; 7762 }else if( val==0 ){ 7763 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7764 p->autoExplain = 0; 7765 }else if( val==99 ){ 7766 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7767 p->autoExplain = 1; 7768 } 7769 }else 7770 7771#ifndef SQLITE_OMIT_VIRTUALTABLE 7772 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ 7773 open_db(p, 0); 7774 expertDotCommand(p, azArg, nArg); 7775 }else 7776#endif 7777 7778 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ 7779 static const struct { 7780 const char *zCtrlName; /* Name of a test-control option */ 7781 int ctrlCode; /* Integer code for that option */ 7782 const char *zUsage; /* Usage notes */ 7783 } aCtrl[] = { 7784 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, 7785 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, 7786 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ 7787 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, 7788 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, 7789 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ 7790 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, 7791 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, 7792 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, 7793 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, 7794 }; 7795 int filectrl = -1; 7796 int iCtrl = -1; 7797 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ 7798 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ 7799 int n2, i; 7800 const char *zCmd = 0; 7801 const char *zSchema = 0; 7802 7803 open_db(p, 0); 7804 zCmd = nArg>=2 ? azArg[1] : "help"; 7805 7806 if( zCmd[0]=='-' 7807 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0) 7808 && nArg>=4 7809 ){ 7810 zSchema = azArg[2]; 7811 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; 7812 nArg -= 2; 7813 zCmd = azArg[1]; 7814 } 7815 7816 /* The argument can optionally begin with "-" or "--" */ 7817 if( zCmd[0]=='-' && zCmd[1] ){ 7818 zCmd++; 7819 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 7820 } 7821 7822 /* --help lists all file-controls */ 7823 if( strcmp(zCmd,"help")==0 ){ 7824 utf8_printf(p->out, "Available file-controls:\n"); 7825 for(i=0; i<ArraySize(aCtrl); i++){ 7826 utf8_printf(p->out, " .filectrl %s %s\n", 7827 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 7828 } 7829 rc = 1; 7830 goto meta_command_exit; 7831 } 7832 7833 /* convert filectrl text option to value. allow any unique prefix 7834 ** of the option name, or a numerical value. */ 7835 n2 = strlen30(zCmd); 7836 for(i=0; i<ArraySize(aCtrl); i++){ 7837 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 7838 if( filectrl<0 ){ 7839 filectrl = aCtrl[i].ctrlCode; 7840 iCtrl = i; 7841 }else{ 7842 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" 7843 "Use \".filectrl --help\" for help\n", zCmd); 7844 rc = 1; 7845 goto meta_command_exit; 7846 } 7847 } 7848 } 7849 if( filectrl<0 ){ 7850 utf8_printf(stderr,"Error: unknown file-control: %s\n" 7851 "Use \".filectrl --help\" for help\n", zCmd); 7852 }else{ 7853 switch(filectrl){ 7854 case SQLITE_FCNTL_SIZE_LIMIT: { 7855 if( nArg!=2 && nArg!=3 ) break; 7856 iRes = nArg==3 ? integerValue(azArg[2]) : -1; 7857 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); 7858 isOk = 1; 7859 break; 7860 } 7861 case SQLITE_FCNTL_LOCK_TIMEOUT: 7862 case SQLITE_FCNTL_CHUNK_SIZE: { 7863 int x; 7864 if( nArg!=3 ) break; 7865 x = (int)integerValue(azArg[2]); 7866 sqlite3_file_control(p->db, zSchema, filectrl, &x); 7867 isOk = 2; 7868 break; 7869 } 7870 case SQLITE_FCNTL_PERSIST_WAL: 7871 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { 7872 int x; 7873 if( nArg!=2 && nArg!=3 ) break; 7874 x = nArg==3 ? booleanValue(azArg[2]) : -1; 7875 sqlite3_file_control(p->db, zSchema, filectrl, &x); 7876 iRes = x; 7877 isOk = 1; 7878 break; 7879 } 7880 case SQLITE_FCNTL_HAS_MOVED: { 7881 int x; 7882 if( nArg!=2 ) break; 7883 sqlite3_file_control(p->db, zSchema, filectrl, &x); 7884 iRes = x; 7885 isOk = 1; 7886 break; 7887 } 7888 case SQLITE_FCNTL_TEMPFILENAME: { 7889 char *z = 0; 7890 if( nArg!=2 ) break; 7891 sqlite3_file_control(p->db, zSchema, filectrl, &z); 7892 if( z ){ 7893 utf8_printf(p->out, "%s\n", z); 7894 sqlite3_free(z); 7895 } 7896 isOk = 2; 7897 break; 7898 } 7899 case SQLITE_FCNTL_RESERVE_BYTES: { 7900 int x; 7901 if( nArg>=3 ){ 7902 x = atoi(azArg[2]); 7903 sqlite3_file_control(p->db, zSchema, filectrl, &x); 7904 } 7905 x = -1; 7906 sqlite3_file_control(p->db, zSchema, filectrl, &x); 7907 utf8_printf(p->out,"%d\n", x); 7908 isOk = 2; 7909 break; 7910 } 7911 } 7912 } 7913 if( isOk==0 && iCtrl>=0 ){ 7914 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 7915 rc = 1; 7916 }else if( isOk==1 ){ 7917 char zBuf[100]; 7918 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); 7919 raw_printf(p->out, "%s\n", zBuf); 7920 } 7921 }else 7922 7923 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ 7924 ShellState data; 7925 char *zErrMsg = 0; 7926 int doStats = 0; 7927 memcpy(&data, p, sizeof(data)); 7928 data.showHeader = 0; 7929 data.cMode = data.mode = MODE_Semi; 7930 if( nArg==2 && optionMatch(azArg[1], "indent") ){ 7931 data.cMode = data.mode = MODE_Pretty; 7932 nArg = 1; 7933 } 7934 if( nArg!=1 ){ 7935 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); 7936 rc = 1; 7937 goto meta_command_exit; 7938 } 7939 open_db(p, 0); 7940 rc = sqlite3_exec(p->db, 7941 "SELECT sql FROM" 7942 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 7943 " FROM sqlite_master UNION ALL" 7944 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 7945 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 7946 "ORDER BY rowid", 7947 callback, &data, &zErrMsg 7948 ); 7949 if( rc==SQLITE_OK ){ 7950 sqlite3_stmt *pStmt; 7951 rc = sqlite3_prepare_v2(p->db, 7952 "SELECT rowid FROM sqlite_master" 7953 " WHERE name GLOB 'sqlite_stat[134]'", 7954 -1, &pStmt, 0); 7955 doStats = sqlite3_step(pStmt)==SQLITE_ROW; 7956 sqlite3_finalize(pStmt); 7957 } 7958 if( doStats==0 ){ 7959 raw_printf(p->out, "/* No STAT tables available */\n"); 7960 }else{ 7961 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7962 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", 7963 callback, &data, &zErrMsg); 7964 data.cMode = data.mode = MODE_Insert; 7965 data.zDestTable = "sqlite_stat1"; 7966 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg); 7967 data.zDestTable = "sqlite_stat4"; 7968 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg); 7969 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7970 } 7971 }else 7972 7973 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ 7974 if( nArg==2 ){ 7975 p->showHeader = booleanValue(azArg[1]); 7976 p->shellFlgs |= SHFLG_HeaderSet; 7977 }else{ 7978 raw_printf(stderr, "Usage: .headers on|off\n"); 7979 rc = 1; 7980 } 7981 }else 7982 7983 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ 7984 if( nArg>=2 ){ 7985 n = showHelp(p->out, azArg[1]); 7986 if( n==0 ){ 7987 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); 7988 } 7989 }else{ 7990 showHelp(p->out, 0); 7991 } 7992 }else 7993 7994 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ 7995 char *zTable = 0; /* Insert data into this table */ 7996 char *zFile = 0; /* Name of file to extra content from */ 7997 sqlite3_stmt *pStmt = NULL; /* A statement */ 7998 int nCol; /* Number of columns in the table */ 7999 int nByte; /* Number of bytes in an SQL string */ 8000 int i, j; /* Loop counters */ 8001 int needCommit; /* True to COMMIT or ROLLBACK at end */ 8002 int nSep; /* Number of bytes in p->colSeparator[] */ 8003 char *zSql; /* An SQL statement */ 8004 ImportCtx sCtx; /* Reader context */ 8005 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ 8006 int eVerbose = 0; /* Larger for more console output */ 8007 int nSkip = 0; /* Initial lines to skip */ 8008 int useOutputMode = 1; /* Use output mode to determine separators */ 8009 8010 memset(&sCtx, 0, sizeof(sCtx)); 8011 if( p->mode==MODE_Ascii ){ 8012 xRead = ascii_read_one_field; 8013 }else{ 8014 xRead = csv_read_one_field; 8015 } 8016 for(i=1; i<nArg; i++){ 8017 char *z = azArg[i]; 8018 if( z[0]=='-' && z[1]=='-' ) z++; 8019 if( z[0]!='-' ){ 8020 if( zFile==0 ){ 8021 zFile = z; 8022 }else if( zTable==0 ){ 8023 zTable = z; 8024 }else{ 8025 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); 8026 showHelp(p->out, "import"); 8027 rc = 1; 8028 goto meta_command_exit; 8029 } 8030 }else if( strcmp(z,"-v")==0 ){ 8031 eVerbose++; 8032 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){ 8033 nSkip = integerValue(azArg[++i]); 8034 }else if( strcmp(z,"-ascii")==0 ){ 8035 sCtx.cColSep = SEP_Unit[0]; 8036 sCtx.cRowSep = SEP_Record[0]; 8037 xRead = ascii_read_one_field; 8038 useOutputMode = 0; 8039 }else if( strcmp(z,"-csv")==0 ){ 8040 sCtx.cColSep = ','; 8041 sCtx.cRowSep = '\n'; 8042 xRead = csv_read_one_field; 8043 useOutputMode = 0; 8044 }else{ 8045 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); 8046 showHelp(p->out, "import"); 8047 rc = 1; 8048 goto meta_command_exit; 8049 } 8050 } 8051 if( zTable==0 ){ 8052 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", 8053 zFile==0 ? "FILE" : "TABLE"); 8054 showHelp(p->out, "import"); 8055 rc = 1; 8056 goto meta_command_exit; 8057 } 8058 seenInterrupt = 0; 8059 open_db(p, 0); 8060 if( useOutputMode ){ 8061 /* If neither the --csv or --ascii options are specified, then set 8062 ** the column and row separator characters from the output mode. */ 8063 nSep = strlen30(p->colSeparator); 8064 if( nSep==0 ){ 8065 raw_printf(stderr, 8066 "Error: non-null column separator required for import\n"); 8067 rc = 1; 8068 goto meta_command_exit; 8069 } 8070 if( nSep>1 ){ 8071 raw_printf(stderr, 8072 "Error: multi-character column separators not allowed" 8073 " for import\n"); 8074 rc = 1; 8075 goto meta_command_exit; 8076 } 8077 nSep = strlen30(p->rowSeparator); 8078 if( nSep==0 ){ 8079 raw_printf(stderr, 8080 "Error: non-null row separator required for import\n"); 8081 rc = 1; 8082 goto meta_command_exit; 8083 } 8084 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){ 8085 /* When importing CSV (only), if the row separator is set to the 8086 ** default output row separator, change it to the default input 8087 ** row separator. This avoids having to maintain different input 8088 ** and output row separators. */ 8089 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8090 nSep = strlen30(p->rowSeparator); 8091 } 8092 if( nSep>1 ){ 8093 raw_printf(stderr, "Error: multi-character row separators not allowed" 8094 " for import\n"); 8095 rc = 1; 8096 goto meta_command_exit; 8097 } 8098 sCtx.cColSep = p->colSeparator[0]; 8099 sCtx.cRowSep = p->rowSeparator[0]; 8100 } 8101 sCtx.zFile = zFile; 8102 sCtx.nLine = 1; 8103 if( sCtx.zFile[0]=='|' ){ 8104#ifdef SQLITE_OMIT_POPEN 8105 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8106 rc = 1; 8107 goto meta_command_exit; 8108#else 8109 sCtx.in = popen(sCtx.zFile+1, "r"); 8110 sCtx.zFile = "<pipe>"; 8111 sCtx.xCloser = pclose; 8112#endif 8113 }else{ 8114 sCtx.in = fopen(sCtx.zFile, "rb"); 8115 sCtx.xCloser = fclose; 8116 } 8117 if( sCtx.in==0 ){ 8118 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 8119 rc = 1; 8120 goto meta_command_exit; 8121 } 8122 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ 8123 char zSep[2]; 8124 zSep[1] = 0; 8125 zSep[0] = sCtx.cColSep; 8126 utf8_printf(p->out, "Column separator "); 8127 output_c_string(p->out, zSep); 8128 utf8_printf(p->out, ", row separator "); 8129 zSep[0] = sCtx.cRowSep; 8130 output_c_string(p->out, zSep); 8131 utf8_printf(p->out, "\n"); 8132 } 8133 while( (nSkip--)>0 ){ 8134 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} 8135 sCtx.nLine++; 8136 } 8137 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); 8138 if( zSql==0 ){ 8139 import_cleanup(&sCtx); 8140 shell_out_of_memory(); 8141 } 8142 nByte = strlen30(zSql); 8143 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8144 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ 8145 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ 8146 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); 8147 char cSep = '('; 8148 while( xRead(&sCtx) ){ 8149 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); 8150 cSep = ','; 8151 if( sCtx.cTerm!=sCtx.cColSep ) break; 8152 } 8153 if( cSep=='(' ){ 8154 sqlite3_free(zCreate); 8155 import_cleanup(&sCtx); 8156 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); 8157 rc = 1; 8158 goto meta_command_exit; 8159 } 8160 zCreate = sqlite3_mprintf("%z\n)", zCreate); 8161 if( eVerbose>=1 ){ 8162 utf8_printf(p->out, "%s\n", zCreate); 8163 } 8164 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); 8165 sqlite3_free(zCreate); 8166 if( rc ){ 8167 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, 8168 sqlite3_errmsg(p->db)); 8169 import_cleanup(&sCtx); 8170 rc = 1; 8171 goto meta_command_exit; 8172 } 8173 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8174 } 8175 sqlite3_free(zSql); 8176 if( rc ){ 8177 if (pStmt) sqlite3_finalize(pStmt); 8178 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); 8179 import_cleanup(&sCtx); 8180 rc = 1; 8181 goto meta_command_exit; 8182 } 8183 nCol = sqlite3_column_count(pStmt); 8184 sqlite3_finalize(pStmt); 8185 pStmt = 0; 8186 if( nCol==0 ) return 0; /* no columns, no error */ 8187 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); 8188 if( zSql==0 ){ 8189 import_cleanup(&sCtx); 8190 shell_out_of_memory(); 8191 } 8192 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); 8193 j = strlen30(zSql); 8194 for(i=1; i<nCol; i++){ 8195 zSql[j++] = ','; 8196 zSql[j++] = '?'; 8197 } 8198 zSql[j++] = ')'; 8199 zSql[j] = 0; 8200 if( eVerbose>=2 ){ 8201 utf8_printf(p->out, "Insert using: %s\n", zSql); 8202 } 8203 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8204 sqlite3_free(zSql); 8205 if( rc ){ 8206 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8207 if (pStmt) sqlite3_finalize(pStmt); 8208 import_cleanup(&sCtx); 8209 rc = 1; 8210 goto meta_command_exit; 8211 } 8212 needCommit = sqlite3_get_autocommit(p->db); 8213 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); 8214 do{ 8215 int startLine = sCtx.nLine; 8216 for(i=0; i<nCol; i++){ 8217 char *z = xRead(&sCtx); 8218 /* 8219 ** Did we reach end-of-file before finding any columns? 8220 ** If so, stop instead of NULL filling the remaining columns. 8221 */ 8222 if( z==0 && i==0 ) break; 8223 /* 8224 ** Did we reach end-of-file OR end-of-line before finding any 8225 ** columns in ASCII mode? If so, stop instead of NULL filling 8226 ** the remaining columns. 8227 */ 8228 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; 8229 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); 8230 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ 8231 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8232 "filling the rest with NULL\n", 8233 sCtx.zFile, startLine, nCol, i+1); 8234 i += 2; 8235 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } 8236 } 8237 } 8238 if( sCtx.cTerm==sCtx.cColSep ){ 8239 do{ 8240 xRead(&sCtx); 8241 i++; 8242 }while( sCtx.cTerm==sCtx.cColSep ); 8243 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8244 "extras ignored\n", 8245 sCtx.zFile, startLine, nCol, i); 8246 } 8247 if( i>=nCol ){ 8248 sqlite3_step(pStmt); 8249 rc = sqlite3_reset(pStmt); 8250 if( rc!=SQLITE_OK ){ 8251 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, 8252 startLine, sqlite3_errmsg(p->db)); 8253 sCtx.nErr++; 8254 }else{ 8255 sCtx.nRow++; 8256 } 8257 } 8258 }while( sCtx.cTerm!=EOF ); 8259 8260 import_cleanup(&sCtx); 8261 sqlite3_finalize(pStmt); 8262 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); 8263 if( eVerbose>0 ){ 8264 utf8_printf(p->out, 8265 "Added %d rows with %d errors using %d lines of input\n", 8266 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); 8267 } 8268 }else 8269 8270#ifndef SQLITE_UNTESTABLE 8271 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ 8272 char *zSql; 8273 char *zCollist = 0; 8274 sqlite3_stmt *pStmt; 8275 int tnum = 0; 8276 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ 8277 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ 8278 int i; 8279 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ 8280 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" 8281 " .imposter off\n"); 8282 /* Also allowed, but not documented: 8283 ** 8284 ** .imposter TABLE IMPOSTER 8285 ** 8286 ** where TABLE is a WITHOUT ROWID table. In that case, the 8287 ** imposter is another WITHOUT ROWID table with the columns in 8288 ** storage order. */ 8289 rc = 1; 8290 goto meta_command_exit; 8291 } 8292 open_db(p, 0); 8293 if( nArg==2 ){ 8294 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); 8295 goto meta_command_exit; 8296 } 8297 zSql = sqlite3_mprintf( 8298 "SELECT rootpage, 0 FROM sqlite_master" 8299 " WHERE name='%q' AND type='index'" 8300 "UNION ALL " 8301 "SELECT rootpage, 1 FROM sqlite_master" 8302 " WHERE name='%q' AND type='table'" 8303 " AND sql LIKE '%%without%%rowid%%'", 8304 azArg[1], azArg[1] 8305 ); 8306 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8307 sqlite3_free(zSql); 8308 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 8309 tnum = sqlite3_column_int(pStmt, 0); 8310 isWO = sqlite3_column_int(pStmt, 1); 8311 } 8312 sqlite3_finalize(pStmt); 8313 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); 8314 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8315 sqlite3_free(zSql); 8316 i = 0; 8317 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8318 char zLabel[20]; 8319 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); 8320 i++; 8321 if( zCol==0 ){ 8322 if( sqlite3_column_int(pStmt,1)==-1 ){ 8323 zCol = "_ROWID_"; 8324 }else{ 8325 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); 8326 zCol = zLabel; 8327 } 8328 } 8329 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ 8330 lenPK = (int)strlen(zCollist); 8331 } 8332 if( zCollist==0 ){ 8333 zCollist = sqlite3_mprintf("\"%w\"", zCol); 8334 }else{ 8335 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); 8336 } 8337 } 8338 sqlite3_finalize(pStmt); 8339 if( i==0 || tnum==0 ){ 8340 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); 8341 rc = 1; 8342 sqlite3_free(zCollist); 8343 goto meta_command_exit; 8344 } 8345 if( lenPK==0 ) lenPK = 100000; 8346 zSql = sqlite3_mprintf( 8347 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", 8348 azArg[2], zCollist, lenPK, zCollist); 8349 sqlite3_free(zCollist); 8350 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); 8351 if( rc==SQLITE_OK ){ 8352 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 8353 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); 8354 if( rc ){ 8355 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); 8356 }else{ 8357 utf8_printf(stdout, "%s;\n", zSql); 8358 raw_printf(stdout, 8359 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", 8360 azArg[1], isWO ? "table" : "index" 8361 ); 8362 } 8363 }else{ 8364 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); 8365 rc = 1; 8366 } 8367 sqlite3_free(zSql); 8368 }else 8369#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ 8370 8371#ifdef SQLITE_ENABLE_IOTRACE 8372 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ 8373 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); 8374 if( iotrace && iotrace!=stdout ) fclose(iotrace); 8375 iotrace = 0; 8376 if( nArg<2 ){ 8377 sqlite3IoTrace = 0; 8378 }else if( strcmp(azArg[1], "-")==0 ){ 8379 sqlite3IoTrace = iotracePrintf; 8380 iotrace = stdout; 8381 }else{ 8382 iotrace = fopen(azArg[1], "w"); 8383 if( iotrace==0 ){ 8384 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 8385 sqlite3IoTrace = 0; 8386 rc = 1; 8387 }else{ 8388 sqlite3IoTrace = iotracePrintf; 8389 } 8390 } 8391 }else 8392#endif 8393 8394 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ 8395 static const struct { 8396 const char *zLimitName; /* Name of a limit */ 8397 int limitCode; /* Integer code for that limit */ 8398 } aLimit[] = { 8399 { "length", SQLITE_LIMIT_LENGTH }, 8400 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, 8401 { "column", SQLITE_LIMIT_COLUMN }, 8402 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, 8403 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, 8404 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, 8405 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, 8406 { "attached", SQLITE_LIMIT_ATTACHED }, 8407 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 8408 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, 8409 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, 8410 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, 8411 }; 8412 int i, n2; 8413 open_db(p, 0); 8414 if( nArg==1 ){ 8415 for(i=0; i<ArraySize(aLimit); i++){ 8416 printf("%20s %d\n", aLimit[i].zLimitName, 8417 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); 8418 } 8419 }else if( nArg>3 ){ 8420 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); 8421 rc = 1; 8422 goto meta_command_exit; 8423 }else{ 8424 int iLimit = -1; 8425 n2 = strlen30(azArg[1]); 8426 for(i=0; i<ArraySize(aLimit); i++){ 8427 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ 8428 if( iLimit<0 ){ 8429 iLimit = i; 8430 }else{ 8431 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); 8432 rc = 1; 8433 goto meta_command_exit; 8434 } 8435 } 8436 } 8437 if( iLimit<0 ){ 8438 utf8_printf(stderr, "unknown limit: \"%s\"\n" 8439 "enter \".limits\" with no arguments for a list.\n", 8440 azArg[1]); 8441 rc = 1; 8442 goto meta_command_exit; 8443 } 8444 if( nArg==3 ){ 8445 sqlite3_limit(p->db, aLimit[iLimit].limitCode, 8446 (int)integerValue(azArg[2])); 8447 } 8448 printf("%20s %d\n", aLimit[iLimit].zLimitName, 8449 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); 8450 } 8451 }else 8452 8453 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ 8454 open_db(p, 0); 8455 lintDotCommand(p, azArg, nArg); 8456 }else 8457 8458#ifndef SQLITE_OMIT_LOAD_EXTENSION 8459 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ 8460 const char *zFile, *zProc; 8461 char *zErrMsg = 0; 8462 if( nArg<2 ){ 8463 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); 8464 rc = 1; 8465 goto meta_command_exit; 8466 } 8467 zFile = azArg[1]; 8468 zProc = nArg>=3 ? azArg[2] : 0; 8469 open_db(p, 0); 8470 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); 8471 if( rc!=SQLITE_OK ){ 8472 utf8_printf(stderr, "Error: %s\n", zErrMsg); 8473 sqlite3_free(zErrMsg); 8474 rc = 1; 8475 } 8476 }else 8477#endif 8478 8479 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ 8480 if( nArg!=2 ){ 8481 raw_printf(stderr, "Usage: .log FILENAME\n"); 8482 rc = 1; 8483 }else{ 8484 const char *zFile = azArg[1]; 8485 output_file_close(p->pLog); 8486 p->pLog = output_file_open(zFile, 0); 8487 } 8488 }else 8489 8490 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ 8491 const char *zMode = nArg>=2 ? azArg[1] : ""; 8492 int n2 = strlen30(zMode); 8493 int c2 = zMode[0]; 8494 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ 8495 p->mode = MODE_Line; 8496 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8497 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ 8498 p->mode = MODE_Column; 8499 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ 8500 p->showHeader = 1; 8501 } 8502 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8503 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ 8504 p->mode = MODE_List; 8505 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); 8506 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8507 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ 8508 p->mode = MODE_Html; 8509 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ 8510 p->mode = MODE_Tcl; 8511 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); 8512 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8513 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ 8514 p->mode = MODE_Csv; 8515 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8516 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8517 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ 8518 p->mode = MODE_List; 8519 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); 8520 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ 8521 p->mode = MODE_Insert; 8522 set_table_name(p, nArg>=3 ? azArg[2] : "table"); 8523 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ 8524 p->mode = MODE_Quote; 8525 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8526 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8527 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ 8528 p->mode = MODE_Ascii; 8529 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); 8530 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); 8531 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){ 8532 p->mode = MODE_Markdown; 8533 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){ 8534 p->mode = MODE_Table; 8535 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){ 8536 p->mode = MODE_Box; 8537 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){ 8538 p->mode = MODE_Json; 8539 }else if( nArg==1 ){ 8540 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); 8541 }else{ 8542 raw_printf(stderr, "Error: mode should be one of: " 8543 "ascii box column csv html insert json line list markdown " 8544 "quote table tabs tcl\n"); 8545 rc = 1; 8546 } 8547 p->cMode = p->mode; 8548 }else 8549 8550 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ 8551 if( nArg==2 ){ 8552 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, 8553 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); 8554 }else{ 8555 raw_printf(stderr, "Usage: .nullvalue STRING\n"); 8556 rc = 1; 8557 } 8558 }else 8559 8560#ifdef SQLITE_DEBUG 8561 if( c=='o' && strcmp(azArg[0],"oom")==0 ){ 8562 int i; 8563 for(i=1; i<nArg; i++){ 8564 const char *z = azArg[i]; 8565 if( z[0]=='-' && z[1]=='-' ) z++; 8566 if( strcmp(z,"-repeat")==0 ){ 8567 if( i==nArg-1 ){ 8568 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]); 8569 rc = 1; 8570 }else{ 8571 oomRepeat = (int)integerValue(azArg[++i]); 8572 } 8573 }else if( IsDigit(z[0]) ){ 8574 oomCounter = (int)integerValue(azArg[i]); 8575 }else{ 8576 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]); 8577 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n"); 8578 rc = 1; 8579 } 8580 } 8581 if( rc==0 ){ 8582 raw_printf(p->out, "oomCounter = %d\n", oomCounter); 8583 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat); 8584 } 8585 }else 8586#endif /* SQLITE_DEBUG */ 8587 8588 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ 8589 char *zNewFilename; /* Name of the database file to open */ 8590 int iName = 1; /* Index in azArg[] of the filename */ 8591 int newFlag = 0; /* True to delete file before opening */ 8592 /* Close the existing database */ 8593 session_close_all(p); 8594 close_db(p->db); 8595 p->db = 0; 8596 p->zDbFilename = 0; 8597 sqlite3_free(p->zFreeOnClose); 8598 p->zFreeOnClose = 0; 8599 p->openMode = SHELL_OPEN_UNSPEC; 8600 p->openFlags = 0; 8601 p->szMax = 0; 8602 /* Check for command-line arguments */ 8603 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ 8604 const char *z = azArg[iName]; 8605 if( optionMatch(z,"new") ){ 8606 newFlag = 1; 8607#ifdef SQLITE_HAVE_ZLIB 8608 }else if( optionMatch(z, "zip") ){ 8609 p->openMode = SHELL_OPEN_ZIPFILE; 8610#endif 8611 }else if( optionMatch(z, "append") ){ 8612 p->openMode = SHELL_OPEN_APPENDVFS; 8613 }else if( optionMatch(z, "readonly") ){ 8614 p->openMode = SHELL_OPEN_READONLY; 8615 }else if( optionMatch(z, "nofollow") ){ 8616 p->openFlags |= SQLITE_OPEN_NOFOLLOW; 8617#ifdef SQLITE_ENABLE_DESERIALIZE 8618 }else if( optionMatch(z, "deserialize") ){ 8619 p->openMode = SHELL_OPEN_DESERIALIZE; 8620 }else if( optionMatch(z, "hexdb") ){ 8621 p->openMode = SHELL_OPEN_HEXDB; 8622 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ 8623 p->szMax = integerValue(azArg[++iName]); 8624#endif /* SQLITE_ENABLE_DESERIALIZE */ 8625 }else if( z[0]=='-' ){ 8626 utf8_printf(stderr, "unknown option: %s\n", z); 8627 rc = 1; 8628 goto meta_command_exit; 8629 } 8630 } 8631 /* If a filename is specified, try to open it first */ 8632 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; 8633 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){ 8634 if( newFlag ) shellDeleteFile(zNewFilename); 8635 p->zDbFilename = zNewFilename; 8636 open_db(p, OPEN_DB_KEEPALIVE); 8637 if( p->db==0 ){ 8638 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); 8639 sqlite3_free(zNewFilename); 8640 }else{ 8641 p->zFreeOnClose = zNewFilename; 8642 } 8643 } 8644 if( p->db==0 ){ 8645 /* As a fall-back open a TEMP database */ 8646 p->zDbFilename = 0; 8647 open_db(p, 0); 8648 } 8649 }else 8650 8651 if( (c=='o' 8652 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) 8653 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) 8654 ){ 8655 const char *zFile = 0; 8656 int bTxtMode = 0; 8657 int i; 8658 int eMode = 0; 8659 int bBOM = 0; 8660 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ 8661 8662 if( c=='e' ){ 8663 eMode = 'x'; 8664 bOnce = 2; 8665 }else if( strncmp(azArg[0],"once",n)==0 ){ 8666 bOnce = 1; 8667 } 8668 for(i=1; i<nArg; i++){ 8669 char *z = azArg[i]; 8670 if( z[0]=='-' ){ 8671 if( z[1]=='-' ) z++; 8672 if( strcmp(z,"-bom")==0 ){ 8673 bBOM = 1; 8674 }else if( c!='e' && strcmp(z,"-x")==0 ){ 8675 eMode = 'x'; /* spreadsheet */ 8676 }else if( c!='e' && strcmp(z,"-e")==0 ){ 8677 eMode = 'e'; /* text editor */ 8678 }else{ 8679 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", 8680 azArg[i]); 8681 showHelp(p->out, azArg[0]); 8682 rc = 1; 8683 goto meta_command_exit; 8684 } 8685 }else if( zFile==0 ){ 8686 zFile = z; 8687 }else{ 8688 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", 8689 azArg[i]); 8690 showHelp(p->out, azArg[0]); 8691 rc = 1; 8692 goto meta_command_exit; 8693 } 8694 } 8695 if( zFile==0 ) zFile = "stdout"; 8696 if( bOnce ){ 8697 p->outCount = 2; 8698 }else{ 8699 p->outCount = 0; 8700 } 8701 output_reset(p); 8702#ifndef SQLITE_NOHAVE_SYSTEM 8703 if( eMode=='e' || eMode=='x' ){ 8704 p->doXdgOpen = 1; 8705 outputModePush(p); 8706 if( eMode=='x' ){ 8707 /* spreadsheet mode. Output as CSV. */ 8708 newTempFile(p, "csv"); 8709 ShellClearFlag(p, SHFLG_Echo); 8710 p->mode = MODE_Csv; 8711 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8712 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8713 }else{ 8714 /* text editor mode */ 8715 newTempFile(p, "txt"); 8716 bTxtMode = 1; 8717 } 8718 zFile = p->zTempFile; 8719 } 8720#endif /* SQLITE_NOHAVE_SYSTEM */ 8721 if( zFile[0]=='|' ){ 8722#ifdef SQLITE_OMIT_POPEN 8723 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8724 rc = 1; 8725 p->out = stdout; 8726#else 8727 p->out = popen(zFile + 1, "w"); 8728 if( p->out==0 ){ 8729 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); 8730 p->out = stdout; 8731 rc = 1; 8732 }else{ 8733 if( bBOM ) fprintf(p->out,"\357\273\277"); 8734 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8735 } 8736#endif 8737 }else{ 8738 p->out = output_file_open(zFile, bTxtMode); 8739 if( p->out==0 ){ 8740 if( strcmp(zFile,"off")!=0 ){ 8741 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); 8742 } 8743 p->out = stdout; 8744 rc = 1; 8745 } else { 8746 if( bBOM ) fprintf(p->out,"\357\273\277"); 8747 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8748 } 8749 } 8750 }else 8751 8752 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ 8753 open_db(p,0); 8754 if( nArg<=1 ) goto parameter_syntax_error; 8755 8756 /* .parameter clear 8757 ** Clear all bind parameters by dropping the TEMP table that holds them. 8758 */ 8759 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ 8760 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", 8761 0, 0, 0); 8762 }else 8763 8764 /* .parameter list 8765 ** List all bind parameters. 8766 */ 8767 if( nArg==2 && strcmp(azArg[1],"list")==0 ){ 8768 sqlite3_stmt *pStmt = 0; 8769 int rx; 8770 int len = 0; 8771 rx = sqlite3_prepare_v2(p->db, 8772 "SELECT max(length(key)) " 8773 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8774 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 8775 len = sqlite3_column_int(pStmt, 0); 8776 if( len>40 ) len = 40; 8777 } 8778 sqlite3_finalize(pStmt); 8779 pStmt = 0; 8780 if( len ){ 8781 rx = sqlite3_prepare_v2(p->db, 8782 "SELECT key, quote(value) " 8783 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8784 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8785 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), 8786 sqlite3_column_text(pStmt,1)); 8787 } 8788 sqlite3_finalize(pStmt); 8789 } 8790 }else 8791 8792 /* .parameter init 8793 ** Make sure the TEMP table used to hold bind parameters exists. 8794 ** Create it if necessary. 8795 */ 8796 if( nArg==2 && strcmp(azArg[1],"init")==0 ){ 8797 bind_table_init(p); 8798 }else 8799 8800 /* .parameter set NAME VALUE 8801 ** Set or reset a bind parameter. NAME should be the full parameter 8802 ** name exactly as it appears in the query. (ex: $abc, @def). The 8803 ** VALUE can be in either SQL literal notation, or if not it will be 8804 ** understood to be a text string. 8805 */ 8806 if( nArg==4 && strcmp(azArg[1],"set")==0 ){ 8807 int rx; 8808 char *zSql; 8809 sqlite3_stmt *pStmt; 8810 const char *zKey = azArg[2]; 8811 const char *zValue = azArg[3]; 8812 bind_table_init(p); 8813 zSql = sqlite3_mprintf( 8814 "REPLACE INTO temp.sqlite_parameters(key,value)" 8815 "VALUES(%Q,%s);", zKey, zValue); 8816 if( zSql==0 ) shell_out_of_memory(); 8817 pStmt = 0; 8818 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8819 sqlite3_free(zSql); 8820 if( rx!=SQLITE_OK ){ 8821 sqlite3_finalize(pStmt); 8822 pStmt = 0; 8823 zSql = sqlite3_mprintf( 8824 "REPLACE INTO temp.sqlite_parameters(key,value)" 8825 "VALUES(%Q,%Q);", zKey, zValue); 8826 if( zSql==0 ) shell_out_of_memory(); 8827 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8828 sqlite3_free(zSql); 8829 if( rx!=SQLITE_OK ){ 8830 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); 8831 sqlite3_finalize(pStmt); 8832 pStmt = 0; 8833 rc = 1; 8834 } 8835 } 8836 sqlite3_step(pStmt); 8837 sqlite3_finalize(pStmt); 8838 }else 8839 8840 /* .parameter unset NAME 8841 ** Remove the NAME binding from the parameter binding table, if it 8842 ** exists. 8843 */ 8844 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ 8845 char *zSql = sqlite3_mprintf( 8846 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); 8847 if( zSql==0 ) shell_out_of_memory(); 8848 sqlite3_exec(p->db, zSql, 0, 0, 0); 8849 sqlite3_free(zSql); 8850 }else 8851 /* If no command name matches, show a syntax error */ 8852 parameter_syntax_error: 8853 showHelp(p->out, "parameter"); 8854 }else 8855 8856 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ 8857 int i; 8858 for(i=1; i<nArg; i++){ 8859 if( i>1 ) raw_printf(p->out, " "); 8860 utf8_printf(p->out, "%s", azArg[i]); 8861 } 8862 raw_printf(p->out, "\n"); 8863 }else 8864 8865#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 8866 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ 8867 int i; 8868 int nn = 0; 8869 p->flgProgress = 0; 8870 p->mxProgress = 0; 8871 p->nProgress = 0; 8872 for(i=1; i<nArg; i++){ 8873 const char *z = azArg[i]; 8874 if( z[0]=='-' ){ 8875 z++; 8876 if( z[0]=='-' ) z++; 8877 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 8878 p->flgProgress |= SHELL_PROGRESS_QUIET; 8879 continue; 8880 } 8881 if( strcmp(z,"reset")==0 ){ 8882 p->flgProgress |= SHELL_PROGRESS_RESET; 8883 continue; 8884 } 8885 if( strcmp(z,"once")==0 ){ 8886 p->flgProgress |= SHELL_PROGRESS_ONCE; 8887 continue; 8888 } 8889 if( strcmp(z,"limit")==0 ){ 8890 if( i+1>=nArg ){ 8891 utf8_printf(stderr, "Error: missing argument on --limit\n"); 8892 rc = 1; 8893 goto meta_command_exit; 8894 }else{ 8895 p->mxProgress = (int)integerValue(azArg[++i]); 8896 } 8897 continue; 8898 } 8899 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); 8900 rc = 1; 8901 goto meta_command_exit; 8902 }else{ 8903 nn = (int)integerValue(z); 8904 } 8905 } 8906 open_db(p, 0); 8907 sqlite3_progress_handler(p->db, nn, progress_handler, p); 8908 }else 8909#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 8910 8911 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ 8912 if( nArg >= 2) { 8913 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); 8914 } 8915 if( nArg >= 3) { 8916 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); 8917 } 8918 }else 8919 8920 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ 8921 rc = 2; 8922 }else 8923 8924 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ 8925 FILE *inSaved = p->in; 8926 int savedLineno = p->lineno; 8927 if( nArg!=2 ){ 8928 raw_printf(stderr, "Usage: .read FILE\n"); 8929 rc = 1; 8930 goto meta_command_exit; 8931 } 8932 p->in = fopen(azArg[1], "rb"); 8933 if( p->in==0 ){ 8934 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); 8935 rc = 1; 8936 }else{ 8937 rc = process_input(p); 8938 fclose(p->in); 8939 } 8940 p->in = inSaved; 8941 p->lineno = savedLineno; 8942 }else 8943 8944 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ 8945 const char *zSrcFile; 8946 const char *zDb; 8947 sqlite3 *pSrc; 8948 sqlite3_backup *pBackup; 8949 int nTimeout = 0; 8950 8951 if( nArg==2 ){ 8952 zSrcFile = azArg[1]; 8953 zDb = "main"; 8954 }else if( nArg==3 ){ 8955 zSrcFile = azArg[2]; 8956 zDb = azArg[1]; 8957 }else{ 8958 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); 8959 rc = 1; 8960 goto meta_command_exit; 8961 } 8962 rc = sqlite3_open(zSrcFile, &pSrc); 8963 if( rc!=SQLITE_OK ){ 8964 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); 8965 close_db(pSrc); 8966 return 1; 8967 } 8968 open_db(p, 0); 8969 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); 8970 if( pBackup==0 ){ 8971 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8972 close_db(pSrc); 8973 return 1; 8974 } 8975 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 8976 || rc==SQLITE_BUSY ){ 8977 if( rc==SQLITE_BUSY ){ 8978 if( nTimeout++ >= 3 ) break; 8979 sqlite3_sleep(100); 8980 } 8981 } 8982 sqlite3_backup_finish(pBackup); 8983 if( rc==SQLITE_DONE ){ 8984 rc = 0; 8985 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 8986 raw_printf(stderr, "Error: source database is busy\n"); 8987 rc = 1; 8988 }else{ 8989 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8990 rc = 1; 8991 } 8992 close_db(pSrc); 8993 }else 8994 8995 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ 8996 if( nArg==2 ){ 8997 p->scanstatsOn = (u8)booleanValue(azArg[1]); 8998#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 8999 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); 9000#endif 9001 }else{ 9002 raw_printf(stderr, "Usage: .scanstats on|off\n"); 9003 rc = 1; 9004 } 9005 }else 9006 9007 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ 9008 ShellText sSelect; 9009 ShellState data; 9010 char *zErrMsg = 0; 9011 const char *zDiv = "("; 9012 const char *zName = 0; 9013 int iSchema = 0; 9014 int bDebug = 0; 9015 int ii; 9016 9017 open_db(p, 0); 9018 memcpy(&data, p, sizeof(data)); 9019 data.showHeader = 0; 9020 data.cMode = data.mode = MODE_Semi; 9021 initText(&sSelect); 9022 for(ii=1; ii<nArg; ii++){ 9023 if( optionMatch(azArg[ii],"indent") ){ 9024 data.cMode = data.mode = MODE_Pretty; 9025 }else if( optionMatch(azArg[ii],"debug") ){ 9026 bDebug = 1; 9027 }else if( zName==0 ){ 9028 zName = azArg[ii]; 9029 }else{ 9030 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); 9031 rc = 1; 9032 goto meta_command_exit; 9033 } 9034 } 9035 if( zName!=0 ){ 9036 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0 9037 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 9038 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 9039 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; 9040 if( isMaster ){ 9041 char *new_argv[2], *new_colv[2]; 9042 new_argv[0] = sqlite3_mprintf( 9043 "CREATE TABLE %s (\n" 9044 " type text,\n" 9045 " name text,\n" 9046 " tbl_name text,\n" 9047 " rootpage integer,\n" 9048 " sql text\n" 9049 ")", zName); 9050 new_argv[1] = 0; 9051 new_colv[0] = "sql"; 9052 new_colv[1] = 0; 9053 callback(&data, 1, new_argv, new_colv); 9054 sqlite3_free(new_argv[0]); 9055 } 9056 } 9057 if( zDiv ){ 9058 sqlite3_stmt *pStmt = 0; 9059 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", 9060 -1, &pStmt, 0); 9061 if( rc ){ 9062 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9063 sqlite3_finalize(pStmt); 9064 rc = 1; 9065 goto meta_command_exit; 9066 } 9067 appendText(&sSelect, "SELECT sql FROM", 0); 9068 iSchema = 0; 9069 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9070 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); 9071 char zScNum[30]; 9072 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); 9073 appendText(&sSelect, zDiv, 0); 9074 zDiv = " UNION ALL "; 9075 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); 9076 if( sqlite3_stricmp(zDb, "main")!=0 ){ 9077 appendText(&sSelect, zDb, '\''); 9078 }else{ 9079 appendText(&sSelect, "NULL", 0); 9080 } 9081 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); 9082 appendText(&sSelect, zScNum, 0); 9083 appendText(&sSelect, " AS snum, ", 0); 9084 appendText(&sSelect, zDb, '\''); 9085 appendText(&sSelect, " AS sname FROM ", 0); 9086 appendText(&sSelect, zDb, quoteChar(zDb)); 9087 appendText(&sSelect, ".sqlite_master", 0); 9088 } 9089 sqlite3_finalize(pStmt); 9090#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 9091 if( zName ){ 9092 appendText(&sSelect, 9093 " UNION ALL SELECT shell_module_schema(name)," 9094 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 9095 0); 9096 } 9097#endif 9098 appendText(&sSelect, ") WHERE ", 0); 9099 if( zName ){ 9100 char *zQarg = sqlite3_mprintf("%Q", zName); 9101 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || 9102 strchr(zName, '[') != 0; 9103 if( strchr(zName, '.') ){ 9104 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); 9105 }else{ 9106 appendText(&sSelect, "lower(tbl_name)", 0); 9107 } 9108 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); 9109 appendText(&sSelect, zQarg, 0); 9110 if( !bGlob ){ 9111 appendText(&sSelect, " ESCAPE '\\' ", 0); 9112 } 9113 appendText(&sSelect, " AND ", 0); 9114 sqlite3_free(zQarg); 9115 } 9116 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" 9117 " ORDER BY snum, rowid", 0); 9118 if( bDebug ){ 9119 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); 9120 }else{ 9121 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); 9122 } 9123 freeText(&sSelect); 9124 } 9125 if( zErrMsg ){ 9126 utf8_printf(stderr,"Error: %s\n", zErrMsg); 9127 sqlite3_free(zErrMsg); 9128 rc = 1; 9129 }else if( rc != SQLITE_OK ){ 9130 raw_printf(stderr,"Error: querying schema information\n"); 9131 rc = 1; 9132 }else{ 9133 rc = 0; 9134 } 9135 }else 9136 9137#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 9138 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ 9139 sqlite3SelectTrace = (int)integerValue(azArg[1]); 9140 }else 9141#endif 9142 9143#if defined(SQLITE_ENABLE_SESSION) 9144 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ 9145 OpenSession *pSession = &p->aSession[0]; 9146 char **azCmd = &azArg[1]; 9147 int iSes = 0; 9148 int nCmd = nArg - 1; 9149 int i; 9150 if( nArg<=1 ) goto session_syntax_error; 9151 open_db(p, 0); 9152 if( nArg>=3 ){ 9153 for(iSes=0; iSes<p->nSession; iSes++){ 9154 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; 9155 } 9156 if( iSes<p->nSession ){ 9157 pSession = &p->aSession[iSes]; 9158 azCmd++; 9159 nCmd--; 9160 }else{ 9161 pSession = &p->aSession[0]; 9162 iSes = 0; 9163 } 9164 } 9165 9166 /* .session attach TABLE 9167 ** Invoke the sqlite3session_attach() interface to attach a particular 9168 ** table so that it is never filtered. 9169 */ 9170 if( strcmp(azCmd[0],"attach")==0 ){ 9171 if( nCmd!=2 ) goto session_syntax_error; 9172 if( pSession->p==0 ){ 9173 session_not_open: 9174 raw_printf(stderr, "ERROR: No sessions are open\n"); 9175 }else{ 9176 rc = sqlite3session_attach(pSession->p, azCmd[1]); 9177 if( rc ){ 9178 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); 9179 rc = 0; 9180 } 9181 } 9182 }else 9183 9184 /* .session changeset FILE 9185 ** .session patchset FILE 9186 ** Write a changeset or patchset into a file. The file is overwritten. 9187 */ 9188 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ 9189 FILE *out = 0; 9190 if( nCmd!=2 ) goto session_syntax_error; 9191 if( pSession->p==0 ) goto session_not_open; 9192 out = fopen(azCmd[1], "wb"); 9193 if( out==0 ){ 9194 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", 9195 azCmd[1]); 9196 }else{ 9197 int szChng; 9198 void *pChng; 9199 if( azCmd[0][0]=='c' ){ 9200 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); 9201 }else{ 9202 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); 9203 } 9204 if( rc ){ 9205 printf("Error: error code %d\n", rc); 9206 rc = 0; 9207 } 9208 if( pChng 9209 && fwrite(pChng, szChng, 1, out)!=1 ){ 9210 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", 9211 szChng); 9212 } 9213 sqlite3_free(pChng); 9214 fclose(out); 9215 } 9216 }else 9217 9218 /* .session close 9219 ** Close the identified session 9220 */ 9221 if( strcmp(azCmd[0], "close")==0 ){ 9222 if( nCmd!=1 ) goto session_syntax_error; 9223 if( p->nSession ){ 9224 session_close(pSession); 9225 p->aSession[iSes] = p->aSession[--p->nSession]; 9226 } 9227 }else 9228 9229 /* .session enable ?BOOLEAN? 9230 ** Query or set the enable flag 9231 */ 9232 if( strcmp(azCmd[0], "enable")==0 ){ 9233 int ii; 9234 if( nCmd>2 ) goto session_syntax_error; 9235 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9236 if( p->nSession ){ 9237 ii = sqlite3session_enable(pSession->p, ii); 9238 utf8_printf(p->out, "session %s enable flag = %d\n", 9239 pSession->zName, ii); 9240 } 9241 }else 9242 9243 /* .session filter GLOB .... 9244 ** Set a list of GLOB patterns of table names to be excluded. 9245 */ 9246 if( strcmp(azCmd[0], "filter")==0 ){ 9247 int ii, nByte; 9248 if( nCmd<2 ) goto session_syntax_error; 9249 if( p->nSession ){ 9250 for(ii=0; ii<pSession->nFilter; ii++){ 9251 sqlite3_free(pSession->azFilter[ii]); 9252 } 9253 sqlite3_free(pSession->azFilter); 9254 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); 9255 pSession->azFilter = sqlite3_malloc( nByte ); 9256 if( pSession->azFilter==0 ){ 9257 raw_printf(stderr, "Error: out or memory\n"); 9258 exit(1); 9259 } 9260 for(ii=1; ii<nCmd; ii++){ 9261 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); 9262 } 9263 pSession->nFilter = ii-1; 9264 } 9265 }else 9266 9267 /* .session indirect ?BOOLEAN? 9268 ** Query or set the indirect flag 9269 */ 9270 if( strcmp(azCmd[0], "indirect")==0 ){ 9271 int ii; 9272 if( nCmd>2 ) goto session_syntax_error; 9273 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9274 if( p->nSession ){ 9275 ii = sqlite3session_indirect(pSession->p, ii); 9276 utf8_printf(p->out, "session %s indirect flag = %d\n", 9277 pSession->zName, ii); 9278 } 9279 }else 9280 9281 /* .session isempty 9282 ** Determine if the session is empty 9283 */ 9284 if( strcmp(azCmd[0], "isempty")==0 ){ 9285 int ii; 9286 if( nCmd!=1 ) goto session_syntax_error; 9287 if( p->nSession ){ 9288 ii = sqlite3session_isempty(pSession->p); 9289 utf8_printf(p->out, "session %s isempty flag = %d\n", 9290 pSession->zName, ii); 9291 } 9292 }else 9293 9294 /* .session list 9295 ** List all currently open sessions 9296 */ 9297 if( strcmp(azCmd[0],"list")==0 ){ 9298 for(i=0; i<p->nSession; i++){ 9299 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); 9300 } 9301 }else 9302 9303 /* .session open DB NAME 9304 ** Open a new session called NAME on the attached database DB. 9305 ** DB is normally "main". 9306 */ 9307 if( strcmp(azCmd[0],"open")==0 ){ 9308 char *zName; 9309 if( nCmd!=3 ) goto session_syntax_error; 9310 zName = azCmd[2]; 9311 if( zName[0]==0 ) goto session_syntax_error; 9312 for(i=0; i<p->nSession; i++){ 9313 if( strcmp(p->aSession[i].zName,zName)==0 ){ 9314 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); 9315 goto meta_command_exit; 9316 } 9317 } 9318 if( p->nSession>=ArraySize(p->aSession) ){ 9319 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); 9320 goto meta_command_exit; 9321 } 9322 pSession = &p->aSession[p->nSession]; 9323 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); 9324 if( rc ){ 9325 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); 9326 rc = 0; 9327 goto meta_command_exit; 9328 } 9329 pSession->nFilter = 0; 9330 sqlite3session_table_filter(pSession->p, session_filter, pSession); 9331 p->nSession++; 9332 pSession->zName = sqlite3_mprintf("%s", zName); 9333 }else 9334 /* If no command name matches, show a syntax error */ 9335 session_syntax_error: 9336 showHelp(p->out, "session"); 9337 }else 9338#endif 9339 9340#ifdef SQLITE_DEBUG 9341 /* Undocumented commands for internal testing. Subject to change 9342 ** without notice. */ 9343 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ 9344 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ 9345 int i, v; 9346 for(i=1; i<nArg; i++){ 9347 v = booleanValue(azArg[i]); 9348 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); 9349 } 9350 } 9351 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ 9352 int i; sqlite3_int64 v; 9353 for(i=1; i<nArg; i++){ 9354 char zBuf[200]; 9355 v = integerValue(azArg[i]); 9356 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); 9357 utf8_printf(p->out, "%s", zBuf); 9358 } 9359 } 9360 }else 9361#endif 9362 9363 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ 9364 int bIsInit = 0; /* True to initialize the SELFTEST table */ 9365 int bVerbose = 0; /* Verbose output */ 9366 int bSelftestExists; /* True if SELFTEST already exists */ 9367 int i, k; /* Loop counters */ 9368 int nTest = 0; /* Number of tests runs */ 9369 int nErr = 0; /* Number of errors seen */ 9370 ShellText str; /* Answer for a query */ 9371 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ 9372 9373 open_db(p,0); 9374 for(i=1; i<nArg; i++){ 9375 const char *z = azArg[i]; 9376 if( z[0]=='-' && z[1]=='-' ) z++; 9377 if( strcmp(z,"-init")==0 ){ 9378 bIsInit = 1; 9379 }else 9380 if( strcmp(z,"-v")==0 ){ 9381 bVerbose++; 9382 }else 9383 { 9384 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 9385 azArg[i], azArg[0]); 9386 raw_printf(stderr, "Should be one of: --init -v\n"); 9387 rc = 1; 9388 goto meta_command_exit; 9389 } 9390 } 9391 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) 9392 != SQLITE_OK ){ 9393 bSelftestExists = 0; 9394 }else{ 9395 bSelftestExists = 1; 9396 } 9397 if( bIsInit ){ 9398 createSelftestTable(p); 9399 bSelftestExists = 1; 9400 } 9401 initText(&str); 9402 appendText(&str, "x", 0); 9403 for(k=bSelftestExists; k>=0; k--){ 9404 if( k==1 ){ 9405 rc = sqlite3_prepare_v2(p->db, 9406 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", 9407 -1, &pStmt, 0); 9408 }else{ 9409 rc = sqlite3_prepare_v2(p->db, 9410 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," 9411 " (1,'run','PRAGMA integrity_check','ok')", 9412 -1, &pStmt, 0); 9413 } 9414 if( rc ){ 9415 raw_printf(stderr, "Error querying the selftest table\n"); 9416 rc = 1; 9417 sqlite3_finalize(pStmt); 9418 goto meta_command_exit; 9419 } 9420 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ 9421 int tno = sqlite3_column_int(pStmt, 0); 9422 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); 9423 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); 9424 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); 9425 9426 k = 0; 9427 if( bVerbose>0 ){ 9428 char *zQuote = sqlite3_mprintf("%q", zSql); 9429 printf("%d: %s %s\n", tno, zOp, zSql); 9430 sqlite3_free(zQuote); 9431 } 9432 if( strcmp(zOp,"memo")==0 ){ 9433 utf8_printf(p->out, "%s\n", zSql); 9434 }else 9435 if( strcmp(zOp,"run")==0 ){ 9436 char *zErrMsg = 0; 9437 str.n = 0; 9438 str.z[0] = 0; 9439 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); 9440 nTest++; 9441 if( bVerbose ){ 9442 utf8_printf(p->out, "Result: %s\n", str.z); 9443 } 9444 if( rc || zErrMsg ){ 9445 nErr++; 9446 rc = 1; 9447 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); 9448 sqlite3_free(zErrMsg); 9449 }else if( strcmp(zAns,str.z)!=0 ){ 9450 nErr++; 9451 rc = 1; 9452 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); 9453 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); 9454 } 9455 }else 9456 { 9457 utf8_printf(stderr, 9458 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); 9459 rc = 1; 9460 break; 9461 } 9462 } /* End loop over rows of content from SELFTEST */ 9463 sqlite3_finalize(pStmt); 9464 } /* End loop over k */ 9465 freeText(&str); 9466 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); 9467 }else 9468 9469 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ 9470 if( nArg<2 || nArg>3 ){ 9471 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); 9472 rc = 1; 9473 } 9474 if( nArg>=2 ){ 9475 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, 9476 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); 9477 } 9478 if( nArg>=3 ){ 9479 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, 9480 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); 9481 } 9482 }else 9483 9484 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ 9485 const char *zLike = 0; /* Which table to checksum. 0 means everything */ 9486 int i; /* Loop counter */ 9487 int bSchema = 0; /* Also hash the schema */ 9488 int bSeparate = 0; /* Hash each table separately */ 9489 int iSize = 224; /* Hash algorithm to use */ 9490 int bDebug = 0; /* Only show the query that would have run */ 9491 sqlite3_stmt *pStmt; /* For querying tables names */ 9492 char *zSql; /* SQL to be run */ 9493 char *zSep; /* Separator */ 9494 ShellText sSql; /* Complete SQL for the query to run the hash */ 9495 ShellText sQuery; /* Set of queries used to read all content */ 9496 open_db(p, 0); 9497 for(i=1; i<nArg; i++){ 9498 const char *z = azArg[i]; 9499 if( z[0]=='-' ){ 9500 z++; 9501 if( z[0]=='-' ) z++; 9502 if( strcmp(z,"schema")==0 ){ 9503 bSchema = 1; 9504 }else 9505 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 9506 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 9507 ){ 9508 iSize = atoi(&z[5]); 9509 }else 9510 if( strcmp(z,"debug")==0 ){ 9511 bDebug = 1; 9512 }else 9513 { 9514 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 9515 azArg[i], azArg[0]); 9516 showHelp(p->out, azArg[0]); 9517 rc = 1; 9518 goto meta_command_exit; 9519 } 9520 }else if( zLike ){ 9521 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); 9522 rc = 1; 9523 goto meta_command_exit; 9524 }else{ 9525 zLike = z; 9526 bSeparate = 1; 9527 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; 9528 } 9529 } 9530 if( bSchema ){ 9531 zSql = "SELECT lower(name) FROM sqlite_master" 9532 " WHERE type='table' AND coalesce(rootpage,0)>1" 9533 " UNION ALL SELECT 'sqlite_master'" 9534 " ORDER BY 1 collate nocase"; 9535 }else{ 9536 zSql = "SELECT lower(name) FROM sqlite_master" 9537 " WHERE type='table' AND coalesce(rootpage,0)>1" 9538 " AND name NOT LIKE 'sqlite_%'" 9539 " ORDER BY 1 collate nocase"; 9540 } 9541 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 9542 initText(&sQuery); 9543 initText(&sSql); 9544 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); 9545 zSep = "VALUES("; 9546 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 9547 const char *zTab = (const char*)sqlite3_column_text(pStmt,0); 9548 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; 9549 if( strncmp(zTab, "sqlite_",7)!=0 ){ 9550 appendText(&sQuery,"SELECT * FROM ", 0); 9551 appendText(&sQuery,zTab,'"'); 9552 appendText(&sQuery," NOT INDEXED;", 0); 9553 }else if( strcmp(zTab, "sqlite_master")==0 ){ 9554 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" 9555 " ORDER BY name;", 0); 9556 }else if( strcmp(zTab, "sqlite_sequence")==0 ){ 9557 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" 9558 " ORDER BY name;", 0); 9559 }else if( strcmp(zTab, "sqlite_stat1")==0 ){ 9560 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" 9561 " ORDER BY tbl,idx;", 0); 9562 }else if( strcmp(zTab, "sqlite_stat4")==0 ){ 9563 appendText(&sQuery, "SELECT * FROM ", 0); 9564 appendText(&sQuery, zTab, 0); 9565 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); 9566 } 9567 appendText(&sSql, zSep, 0); 9568 appendText(&sSql, sQuery.z, '\''); 9569 sQuery.n = 0; 9570 appendText(&sSql, ",", 0); 9571 appendText(&sSql, zTab, '\''); 9572 zSep = "),("; 9573 } 9574 sqlite3_finalize(pStmt); 9575 if( bSeparate ){ 9576 zSql = sqlite3_mprintf( 9577 "%s))" 9578 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" 9579 " FROM [sha3sum$query]", 9580 sSql.z, iSize); 9581 }else{ 9582 zSql = sqlite3_mprintf( 9583 "%s))" 9584 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" 9585 " FROM [sha3sum$query]", 9586 sSql.z, iSize); 9587 } 9588 freeText(&sQuery); 9589 freeText(&sSql); 9590 if( bDebug ){ 9591 utf8_printf(p->out, "%s\n", zSql); 9592 }else{ 9593 shell_exec(p, zSql, 0); 9594 } 9595 sqlite3_free(zSql); 9596 }else 9597 9598#ifndef SQLITE_NOHAVE_SYSTEM 9599 if( c=='s' 9600 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) 9601 ){ 9602 char *zCmd; 9603 int i, x; 9604 if( nArg<2 ){ 9605 raw_printf(stderr, "Usage: .system COMMAND\n"); 9606 rc = 1; 9607 goto meta_command_exit; 9608 } 9609 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); 9610 for(i=2; i<nArg; i++){ 9611 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", 9612 zCmd, azArg[i]); 9613 } 9614 x = system(zCmd); 9615 sqlite3_free(zCmd); 9616 if( x ) raw_printf(stderr, "System command returns %d\n", x); 9617 }else 9618#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 9619 9620 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ 9621 static const char *azBool[] = { "off", "on", "trigger", "full"}; 9622 int i; 9623 if( nArg!=1 ){ 9624 raw_printf(stderr, "Usage: .show\n"); 9625 rc = 1; 9626 goto meta_command_exit; 9627 } 9628 utf8_printf(p->out, "%12.12s: %s\n","echo", 9629 azBool[ShellHasFlag(p, SHFLG_Echo)]); 9630 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); 9631 utf8_printf(p->out, "%12.12s: %s\n","explain", 9632 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); 9633 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); 9634 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); 9635 utf8_printf(p->out, "%12.12s: ", "nullvalue"); 9636 output_c_string(p->out, p->nullValue); 9637 raw_printf(p->out, "\n"); 9638 utf8_printf(p->out,"%12.12s: %s\n","output", 9639 strlen30(p->outfile) ? p->outfile : "stdout"); 9640 utf8_printf(p->out,"%12.12s: ", "colseparator"); 9641 output_c_string(p->out, p->colSeparator); 9642 raw_printf(p->out, "\n"); 9643 utf8_printf(p->out,"%12.12s: ", "rowseparator"); 9644 output_c_string(p->out, p->rowSeparator); 9645 raw_printf(p->out, "\n"); 9646 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); 9647 utf8_printf(p->out, "%12.12s: ", "width"); 9648 for (i=0;i<p->nWidth;i++) { 9649 raw_printf(p->out, "%d ", p->colWidth[i]); 9650 } 9651 raw_printf(p->out, "\n"); 9652 utf8_printf(p->out, "%12.12s: %s\n", "filename", 9653 p->zDbFilename ? p->zDbFilename : ""); 9654 }else 9655 9656 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ 9657 if( nArg==2 ){ 9658 p->statsOn = (u8)booleanValue(azArg[1]); 9659 }else if( nArg==1 ){ 9660 display_stats(p->db, p, 0); 9661 }else{ 9662 raw_printf(stderr, "Usage: .stats ?on|off?\n"); 9663 rc = 1; 9664 } 9665 }else 9666 9667 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) 9668 || (c=='i' && (strncmp(azArg[0], "indices", n)==0 9669 || strncmp(azArg[0], "indexes", n)==0) ) 9670 ){ 9671 sqlite3_stmt *pStmt; 9672 char **azResult; 9673 int nRow, nAlloc; 9674 int ii; 9675 ShellText s; 9676 initText(&s); 9677 open_db(p, 0); 9678 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 9679 if( rc ){ 9680 sqlite3_finalize(pStmt); 9681 return shellDatabaseError(p->db); 9682 } 9683 9684 if( nArg>2 && c=='i' ){ 9685 /* It is an historical accident that the .indexes command shows an error 9686 ** when called with the wrong number of arguments whereas the .tables 9687 ** command does not. */ 9688 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); 9689 rc = 1; 9690 sqlite3_finalize(pStmt); 9691 goto meta_command_exit; 9692 } 9693 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ 9694 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); 9695 if( zDbName==0 ) continue; 9696 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); 9697 if( sqlite3_stricmp(zDbName, "main")==0 ){ 9698 appendText(&s, "SELECT name FROM ", 0); 9699 }else{ 9700 appendText(&s, "SELECT ", 0); 9701 appendText(&s, zDbName, '\''); 9702 appendText(&s, "||'.'||name FROM ", 0); 9703 } 9704 appendText(&s, zDbName, '"'); 9705 appendText(&s, ".sqlite_master ", 0); 9706 if( c=='t' ){ 9707 appendText(&s," WHERE type IN ('table','view')" 9708 " AND name NOT LIKE 'sqlite_%'" 9709 " AND name LIKE ?1", 0); 9710 }else{ 9711 appendText(&s," WHERE type='index'" 9712 " AND tbl_name LIKE ?1", 0); 9713 } 9714 } 9715 rc = sqlite3_finalize(pStmt); 9716 appendText(&s, " ORDER BY 1", 0); 9717 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); 9718 freeText(&s); 9719 if( rc ) return shellDatabaseError(p->db); 9720 9721 /* Run the SQL statement prepared by the above block. Store the results 9722 ** as an array of nul-terminated strings in azResult[]. */ 9723 nRow = nAlloc = 0; 9724 azResult = 0; 9725 if( nArg>1 ){ 9726 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); 9727 }else{ 9728 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); 9729 } 9730 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9731 if( nRow>=nAlloc ){ 9732 char **azNew; 9733 int n2 = nAlloc*2 + 10; 9734 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); 9735 if( azNew==0 ) shell_out_of_memory(); 9736 nAlloc = n2; 9737 azResult = azNew; 9738 } 9739 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 9740 if( 0==azResult[nRow] ) shell_out_of_memory(); 9741 nRow++; 9742 } 9743 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ 9744 rc = shellDatabaseError(p->db); 9745 } 9746 9747 /* Pretty-print the contents of array azResult[] to the output */ 9748 if( rc==0 && nRow>0 ){ 9749 int len, maxlen = 0; 9750 int i, j; 9751 int nPrintCol, nPrintRow; 9752 for(i=0; i<nRow; i++){ 9753 len = strlen30(azResult[i]); 9754 if( len>maxlen ) maxlen = len; 9755 } 9756 nPrintCol = 80/(maxlen+2); 9757 if( nPrintCol<1 ) nPrintCol = 1; 9758 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; 9759 for(i=0; i<nPrintRow; i++){ 9760 for(j=i; j<nRow; j+=nPrintRow){ 9761 char *zSp = j<nPrintRow ? "" : " "; 9762 utf8_printf(p->out, "%s%-*s", zSp, maxlen, 9763 azResult[j] ? azResult[j]:""); 9764 } 9765 raw_printf(p->out, "\n"); 9766 } 9767 } 9768 9769 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); 9770 sqlite3_free(azResult); 9771 }else 9772 9773 /* Begin redirecting output to the file "testcase-out.txt" */ 9774 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ 9775 output_reset(p); 9776 p->out = output_file_open("testcase-out.txt", 0); 9777 if( p->out==0 ){ 9778 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); 9779 } 9780 if( nArg>=2 ){ 9781 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); 9782 }else{ 9783 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); 9784 } 9785 }else 9786 9787#ifndef SQLITE_UNTESTABLE 9788 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ 9789 static const struct { 9790 const char *zCtrlName; /* Name of a test-control option */ 9791 int ctrlCode; /* Integer code for that option */ 9792 const char *zUsage; /* Usage notes */ 9793 } aCtrl[] = { 9794 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, 9795 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, 9796 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ 9797 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ 9798 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, 9799 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" }, 9800 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/ 9801 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, 9802 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" }, 9803 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, 9804 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, 9805 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, 9806#ifdef YYCOVERAGE 9807 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, 9808#endif 9809 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, 9810 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, 9811 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, 9812 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" }, 9813 }; 9814 int testctrl = -1; 9815 int iCtrl = -1; 9816 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ 9817 int isOk = 0; 9818 int i, n2; 9819 const char *zCmd = 0; 9820 9821 open_db(p, 0); 9822 zCmd = nArg>=2 ? azArg[1] : "help"; 9823 9824 /* The argument can optionally begin with "-" or "--" */ 9825 if( zCmd[0]=='-' && zCmd[1] ){ 9826 zCmd++; 9827 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 9828 } 9829 9830 /* --help lists all test-controls */ 9831 if( strcmp(zCmd,"help")==0 ){ 9832 utf8_printf(p->out, "Available test-controls:\n"); 9833 for(i=0; i<ArraySize(aCtrl); i++){ 9834 utf8_printf(p->out, " .testctrl %s %s\n", 9835 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 9836 } 9837 rc = 1; 9838 goto meta_command_exit; 9839 } 9840 9841 /* convert testctrl text option to value. allow any unique prefix 9842 ** of the option name, or a numerical value. */ 9843 n2 = strlen30(zCmd); 9844 for(i=0; i<ArraySize(aCtrl); i++){ 9845 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 9846 if( testctrl<0 ){ 9847 testctrl = aCtrl[i].ctrlCode; 9848 iCtrl = i; 9849 }else{ 9850 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" 9851 "Use \".testctrl --help\" for help\n", zCmd); 9852 rc = 1; 9853 goto meta_command_exit; 9854 } 9855 } 9856 } 9857 if( testctrl<0 ){ 9858 utf8_printf(stderr,"Error: unknown test-control: %s\n" 9859 "Use \".testctrl --help\" for help\n", zCmd); 9860 }else{ 9861 switch(testctrl){ 9862 9863 /* sqlite3_test_control(int, db, int) */ 9864 case SQLITE_TESTCTRL_OPTIMIZATIONS: 9865 if( nArg==3 ){ 9866 int opt = (int)strtol(azArg[2], 0, 0); 9867 rc2 = sqlite3_test_control(testctrl, p->db, opt); 9868 isOk = 3; 9869 } 9870 break; 9871 9872 /* sqlite3_test_control(int) */ 9873 case SQLITE_TESTCTRL_PRNG_SAVE: 9874 case SQLITE_TESTCTRL_PRNG_RESTORE: 9875 case SQLITE_TESTCTRL_PRNG_RESET: 9876 case SQLITE_TESTCTRL_BYTEORDER: 9877 if( nArg==2 ){ 9878 rc2 = sqlite3_test_control(testctrl); 9879 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; 9880 } 9881 break; 9882 9883 /* sqlite3_test_control(int, uint) */ 9884 case SQLITE_TESTCTRL_PENDING_BYTE: 9885 if( nArg==3 ){ 9886 unsigned int opt = (unsigned int)integerValue(azArg[2]); 9887 rc2 = sqlite3_test_control(testctrl, opt); 9888 isOk = 3; 9889 } 9890 break; 9891 9892 /* sqlite3_test_control(int, int, sqlite3*) */ 9893 case SQLITE_TESTCTRL_PRNG_SEED: 9894 if( nArg==3 || nArg==4 ){ 9895 int ii = (int)integerValue(azArg[2]); 9896 sqlite3 *db; 9897 if( ii==0 && strcmp(azArg[2],"random")==0 ){ 9898 sqlite3_randomness(sizeof(ii),&ii); 9899 printf("-- random seed: %d\n", ii); 9900 } 9901 if( nArg==3 ){ 9902 db = 0; 9903 }else{ 9904 db = p->db; 9905 /* Make sure the schema has been loaded */ 9906 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); 9907 } 9908 rc2 = sqlite3_test_control(testctrl, ii, db); 9909 isOk = 3; 9910 } 9911 break; 9912 9913 /* sqlite3_test_control(int, int) */ 9914 case SQLITE_TESTCTRL_ASSERT: 9915 case SQLITE_TESTCTRL_ALWAYS: 9916 if( nArg==3 ){ 9917 int opt = booleanValue(azArg[2]); 9918 rc2 = sqlite3_test_control(testctrl, opt); 9919 isOk = 1; 9920 } 9921 break; 9922 9923 /* sqlite3_test_control(int, int) */ 9924 case SQLITE_TESTCTRL_LOCALTIME_FAULT: 9925 case SQLITE_TESTCTRL_NEVER_CORRUPT: 9926 if( nArg==3 ){ 9927 int opt = booleanValue(azArg[2]); 9928 rc2 = sqlite3_test_control(testctrl, opt); 9929 isOk = 3; 9930 } 9931 break; 9932 9933 /* sqlite3_test_control(sqlite3*) */ 9934 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: 9935 rc2 = sqlite3_test_control(testctrl, p->db); 9936 isOk = 3; 9937 break; 9938 9939 case SQLITE_TESTCTRL_IMPOSTER: 9940 if( nArg==5 ){ 9941 rc2 = sqlite3_test_control(testctrl, p->db, 9942 azArg[2], 9943 integerValue(azArg[3]), 9944 integerValue(azArg[4])); 9945 isOk = 3; 9946 } 9947 break; 9948 9949#ifdef YYCOVERAGE 9950 case SQLITE_TESTCTRL_PARSER_COVERAGE: 9951 if( nArg==2 ){ 9952 sqlite3_test_control(testctrl, p->out); 9953 isOk = 3; 9954 } 9955#endif 9956 } 9957 } 9958 if( isOk==0 && iCtrl>=0 ){ 9959 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 9960 rc = 1; 9961 }else if( isOk==1 ){ 9962 raw_printf(p->out, "%d\n", rc2); 9963 }else if( isOk==2 ){ 9964 raw_printf(p->out, "0x%08x\n", rc2); 9965 } 9966 }else 9967#endif /* !defined(SQLITE_UNTESTABLE) */ 9968 9969 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ 9970 open_db(p, 0); 9971 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); 9972 }else 9973 9974 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ 9975 if( nArg==2 ){ 9976 enableTimer = booleanValue(azArg[1]); 9977 if( enableTimer && !HAS_TIMER ){ 9978 raw_printf(stderr, "Error: timer not available on this system.\n"); 9979 enableTimer = 0; 9980 } 9981 }else{ 9982 raw_printf(stderr, "Usage: .timer on|off\n"); 9983 rc = 1; 9984 } 9985 }else 9986 9987#ifndef SQLITE_OMIT_TRACE 9988 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ 9989 int mType = 0; 9990 int jj; 9991 open_db(p, 0); 9992 for(jj=1; jj<nArg; jj++){ 9993 const char *z = azArg[jj]; 9994 if( z[0]=='-' ){ 9995 if( optionMatch(z, "expanded") ){ 9996 p->eTraceType = SHELL_TRACE_EXPANDED; 9997 } 9998#ifdef SQLITE_ENABLE_NORMALIZE 9999 else if( optionMatch(z, "normalized") ){ 10000 p->eTraceType = SHELL_TRACE_NORMALIZED; 10001 } 10002#endif 10003 else if( optionMatch(z, "plain") ){ 10004 p->eTraceType = SHELL_TRACE_PLAIN; 10005 } 10006 else if( optionMatch(z, "profile") ){ 10007 mType |= SQLITE_TRACE_PROFILE; 10008 } 10009 else if( optionMatch(z, "row") ){ 10010 mType |= SQLITE_TRACE_ROW; 10011 } 10012 else if( optionMatch(z, "stmt") ){ 10013 mType |= SQLITE_TRACE_STMT; 10014 } 10015 else if( optionMatch(z, "close") ){ 10016 mType |= SQLITE_TRACE_CLOSE; 10017 } 10018 else { 10019 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); 10020 rc = 1; 10021 goto meta_command_exit; 10022 } 10023 }else{ 10024 output_file_close(p->traceOut); 10025 p->traceOut = output_file_open(azArg[1], 0); 10026 } 10027 } 10028 if( p->traceOut==0 ){ 10029 sqlite3_trace_v2(p->db, 0, 0, 0); 10030 }else{ 10031 if( mType==0 ) mType = SQLITE_TRACE_STMT; 10032 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); 10033 } 10034 }else 10035#endif /* !defined(SQLITE_OMIT_TRACE) */ 10036 10037#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) 10038 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ 10039 int ii; 10040 int lenOpt; 10041 char *zOpt; 10042 if( nArg<2 ){ 10043 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); 10044 rc = 1; 10045 goto meta_command_exit; 10046 } 10047 open_db(p, 0); 10048 zOpt = azArg[1]; 10049 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; 10050 lenOpt = (int)strlen(zOpt); 10051 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ 10052 assert( azArg[nArg]==0 ); 10053 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); 10054 }else{ 10055 for(ii=1; ii<nArg; ii++){ 10056 sqlite3_create_module(p->db, azArg[ii], 0, 0); 10057 } 10058 } 10059 }else 10060#endif 10061 10062#if SQLITE_USER_AUTHENTICATION 10063 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ 10064 if( nArg<2 ){ 10065 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); 10066 rc = 1; 10067 goto meta_command_exit; 10068 } 10069 open_db(p, 0); 10070 if( strcmp(azArg[1],"login")==0 ){ 10071 if( nArg!=4 ){ 10072 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); 10073 rc = 1; 10074 goto meta_command_exit; 10075 } 10076 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], 10077 strlen30(azArg[3])); 10078 if( rc ){ 10079 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); 10080 rc = 1; 10081 } 10082 }else if( strcmp(azArg[1],"add")==0 ){ 10083 if( nArg!=5 ){ 10084 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); 10085 rc = 1; 10086 goto meta_command_exit; 10087 } 10088 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10089 booleanValue(azArg[4])); 10090 if( rc ){ 10091 raw_printf(stderr, "User-Add failed: %d\n", rc); 10092 rc = 1; 10093 } 10094 }else if( strcmp(azArg[1],"edit")==0 ){ 10095 if( nArg!=5 ){ 10096 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); 10097 rc = 1; 10098 goto meta_command_exit; 10099 } 10100 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10101 booleanValue(azArg[4])); 10102 if( rc ){ 10103 raw_printf(stderr, "User-Edit failed: %d\n", rc); 10104 rc = 1; 10105 } 10106 }else if( strcmp(azArg[1],"delete")==0 ){ 10107 if( nArg!=3 ){ 10108 raw_printf(stderr, "Usage: .user delete USER\n"); 10109 rc = 1; 10110 goto meta_command_exit; 10111 } 10112 rc = sqlite3_user_delete(p->db, azArg[2]); 10113 if( rc ){ 10114 raw_printf(stderr, "User-Delete failed: %d\n", rc); 10115 rc = 1; 10116 } 10117 }else{ 10118 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); 10119 rc = 1; 10120 goto meta_command_exit; 10121 } 10122 }else 10123#endif /* SQLITE_USER_AUTHENTICATION */ 10124 10125 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ 10126 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, 10127 sqlite3_libversion(), sqlite3_sourceid()); 10128#if SQLITE_HAVE_ZLIB 10129 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); 10130#endif 10131#define CTIMEOPT_VAL_(opt) #opt 10132#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) 10133#if defined(__clang__) && defined(__clang_major__) 10134 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." 10135 CTIMEOPT_VAL(__clang_minor__) "." 10136 CTIMEOPT_VAL(__clang_patchlevel__) "\n"); 10137#elif defined(_MSC_VER) 10138 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); 10139#elif defined(__GNUC__) && defined(__VERSION__) 10140 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); 10141#endif 10142 }else 10143 10144 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ 10145 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10146 sqlite3_vfs *pVfs = 0; 10147 if( p->db ){ 10148 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); 10149 if( pVfs ){ 10150 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); 10151 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10152 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10153 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10154 } 10155 } 10156 }else 10157 10158 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ 10159 sqlite3_vfs *pVfs; 10160 sqlite3_vfs *pCurrent = 0; 10161 if( p->db ){ 10162 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); 10163 } 10164 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 10165 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, 10166 pVfs==pCurrent ? " <--- CURRENT" : ""); 10167 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10168 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10169 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10170 if( pVfs->pNext ){ 10171 raw_printf(p->out, "-----------------------------------\n"); 10172 } 10173 } 10174 }else 10175 10176 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ 10177 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10178 char *zVfsName = 0; 10179 if( p->db ){ 10180 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); 10181 if( zVfsName ){ 10182 utf8_printf(p->out, "%s\n", zVfsName); 10183 sqlite3_free(zVfsName); 10184 } 10185 } 10186 }else 10187 10188#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 10189 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ 10190 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; 10191 }else 10192#endif 10193 10194 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ 10195 int j; 10196 assert( nArg<=ArraySize(azArg) ); 10197 p->nWidth = nArg-1; 10198 p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2); 10199 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); 10200 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; 10201 for(j=1; j<nArg; j++){ 10202 p->colWidth[j-1] = (int)integerValue(azArg[j]); 10203 } 10204 }else 10205 10206 { 10207 utf8_printf(stderr, "Error: unknown command or invalid arguments: " 10208 " \"%s\". Enter \".help\" for help\n", azArg[0]); 10209 rc = 1; 10210 } 10211 10212meta_command_exit: 10213 if( p->outCount ){ 10214 p->outCount--; 10215 if( p->outCount==0 ) output_reset(p); 10216 } 10217 return rc; 10218} 10219 10220/* 10221** Return TRUE if a semicolon occurs anywhere in the first N characters 10222** of string z[]. 10223*/ 10224static int line_contains_semicolon(const char *z, int N){ 10225 int i; 10226 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } 10227 return 0; 10228} 10229 10230/* 10231** Test to see if a line consists entirely of whitespace. 10232*/ 10233static int _all_whitespace(const char *z){ 10234 for(; *z; z++){ 10235 if( IsSpace(z[0]) ) continue; 10236 if( *z=='/' && z[1]=='*' ){ 10237 z += 2; 10238 while( *z && (*z!='*' || z[1]!='/') ){ z++; } 10239 if( *z==0 ) return 0; 10240 z++; 10241 continue; 10242 } 10243 if( *z=='-' && z[1]=='-' ){ 10244 z += 2; 10245 while( *z && *z!='\n' ){ z++; } 10246 if( *z==0 ) return 1; 10247 continue; 10248 } 10249 return 0; 10250 } 10251 return 1; 10252} 10253 10254/* 10255** Return TRUE if the line typed in is an SQL command terminator other 10256** than a semi-colon. The SQL Server style "go" command is understood 10257** as is the Oracle "/". 10258*/ 10259static int line_is_command_terminator(const char *zLine){ 10260 while( IsSpace(zLine[0]) ){ zLine++; }; 10261 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ 10262 return 1; /* Oracle */ 10263 } 10264 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' 10265 && _all_whitespace(&zLine[2]) ){ 10266 return 1; /* SQL Server */ 10267 } 10268 return 0; 10269} 10270 10271/* 10272** We need a default sqlite3_complete() implementation to use in case 10273** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes 10274** any arbitrary text is a complete SQL statement. This is not very 10275** user-friendly, but it does seem to work. 10276*/ 10277#ifdef SQLITE_OMIT_COMPLETE 10278#define sqlite3_complete(x) 1 10279#endif 10280 10281/* 10282** Return true if zSql is a complete SQL statement. Return false if it 10283** ends in the middle of a string literal or C-style comment. 10284*/ 10285static int line_is_complete(char *zSql, int nSql){ 10286 int rc; 10287 if( zSql==0 ) return 1; 10288 zSql[nSql] = ';'; 10289 zSql[nSql+1] = 0; 10290 rc = sqlite3_complete(zSql); 10291 zSql[nSql] = 0; 10292 return rc; 10293} 10294 10295/* 10296** Run a single line of SQL. Return the number of errors. 10297*/ 10298static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ 10299 int rc; 10300 char *zErrMsg = 0; 10301 10302 open_db(p, 0); 10303 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); 10304 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 10305 BEGIN_TIMER; 10306 rc = shell_exec(p, zSql, &zErrMsg); 10307 END_TIMER; 10308 if( rc || zErrMsg ){ 10309 char zPrefix[100]; 10310 if( in!=0 || !stdin_is_interactive ){ 10311 sqlite3_snprintf(sizeof(zPrefix), zPrefix, 10312 "Error: near line %d:", startline); 10313 }else{ 10314 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); 10315 } 10316 if( zErrMsg!=0 ){ 10317 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); 10318 sqlite3_free(zErrMsg); 10319 zErrMsg = 0; 10320 }else{ 10321 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); 10322 } 10323 return 1; 10324 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ 10325 raw_printf(p->out, "changes: %3d total_changes: %d\n", 10326 sqlite3_changes(p->db), sqlite3_total_changes(p->db)); 10327 } 10328 return 0; 10329} 10330 10331 10332/* 10333** Read input from *in and process it. If *in==0 then input 10334** is interactive - the user is typing it it. Otherwise, input 10335** is coming from a file or device. A prompt is issued and history 10336** is saved only if input is interactive. An interrupt signal will 10337** cause this routine to exit immediately, unless input is interactive. 10338** 10339** Return the number of errors. 10340*/ 10341static int process_input(ShellState *p){ 10342 char *zLine = 0; /* A single input line */ 10343 char *zSql = 0; /* Accumulated SQL text */ 10344 int nLine; /* Length of current line */ 10345 int nSql = 0; /* Bytes of zSql[] used */ 10346 int nAlloc = 0; /* Allocated zSql[] space */ 10347 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ 10348 int rc; /* Error code */ 10349 int errCnt = 0; /* Number of errors seen */ 10350 int startline = 0; /* Line number for start of current input */ 10351 10352 p->lineno = 0; 10353 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ 10354 fflush(p->out); 10355 zLine = one_input_line(p->in, zLine, nSql>0); 10356 if( zLine==0 ){ 10357 /* End of input */ 10358 if( p->in==0 && stdin_is_interactive ) printf("\n"); 10359 break; 10360 } 10361 if( seenInterrupt ){ 10362 if( p->in!=0 ) break; 10363 seenInterrupt = 0; 10364 } 10365 p->lineno++; 10366 if( nSql==0 && _all_whitespace(zLine) ){ 10367 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 10368 continue; 10369 } 10370 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ 10371 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 10372 if( zLine[0]=='.' ){ 10373 rc = do_meta_command(zLine, p); 10374 if( rc==2 ){ /* exit requested */ 10375 break; 10376 }else if( rc ){ 10377 errCnt++; 10378 } 10379 } 10380 continue; 10381 } 10382 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ 10383 memcpy(zLine,";",2); 10384 } 10385 nLine = strlen30(zLine); 10386 if( nSql+nLine+2>=nAlloc ){ 10387 nAlloc = nSql+nLine+100; 10388 zSql = realloc(zSql, nAlloc); 10389 if( zSql==0 ) shell_out_of_memory(); 10390 } 10391 nSqlPrior = nSql; 10392 if( nSql==0 ){ 10393 int i; 10394 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} 10395 assert( nAlloc>0 && zSql!=0 ); 10396 memcpy(zSql, zLine+i, nLine+1-i); 10397 startline = p->lineno; 10398 nSql = nLine-i; 10399 }else{ 10400 zSql[nSql++] = '\n'; 10401 memcpy(zSql+nSql, zLine, nLine+1); 10402 nSql += nLine; 10403 } 10404 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) 10405 && sqlite3_complete(zSql) ){ 10406 errCnt += runOneSqlLine(p, zSql, p->in, startline); 10407 nSql = 0; 10408 if( p->outCount ){ 10409 output_reset(p); 10410 p->outCount = 0; 10411 }else{ 10412 clearTempFile(p); 10413 } 10414 }else if( nSql && _all_whitespace(zSql) ){ 10415 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); 10416 nSql = 0; 10417 } 10418 } 10419 if( nSql && !_all_whitespace(zSql) ){ 10420 errCnt += runOneSqlLine(p, zSql, p->in, startline); 10421 } 10422 free(zSql); 10423 free(zLine); 10424 return errCnt>0; 10425} 10426 10427/* 10428** Return a pathname which is the user's home directory. A 10429** 0 return indicates an error of some kind. 10430*/ 10431static char *find_home_dir(int clearFlag){ 10432 static char *home_dir = NULL; 10433 if( clearFlag ){ 10434 free(home_dir); 10435 home_dir = 0; 10436 return 0; 10437 } 10438 if( home_dir ) return home_dir; 10439 10440#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 10441 && !defined(__RTP__) && !defined(_WRS_KERNEL) 10442 { 10443 struct passwd *pwent; 10444 uid_t uid = getuid(); 10445 if( (pwent=getpwuid(uid)) != NULL) { 10446 home_dir = pwent->pw_dir; 10447 } 10448 } 10449#endif 10450 10451#if defined(_WIN32_WCE) 10452 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() 10453 */ 10454 home_dir = "/"; 10455#else 10456 10457#if defined(_WIN32) || defined(WIN32) 10458 if (!home_dir) { 10459 home_dir = getenv("USERPROFILE"); 10460 } 10461#endif 10462 10463 if (!home_dir) { 10464 home_dir = getenv("HOME"); 10465 } 10466 10467#if defined(_WIN32) || defined(WIN32) 10468 if (!home_dir) { 10469 char *zDrive, *zPath; 10470 int n; 10471 zDrive = getenv("HOMEDRIVE"); 10472 zPath = getenv("HOMEPATH"); 10473 if( zDrive && zPath ){ 10474 n = strlen30(zDrive) + strlen30(zPath) + 1; 10475 home_dir = malloc( n ); 10476 if( home_dir==0 ) return 0; 10477 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); 10478 return home_dir; 10479 } 10480 home_dir = "c:\\"; 10481 } 10482#endif 10483 10484#endif /* !_WIN32_WCE */ 10485 10486 if( home_dir ){ 10487 int n = strlen30(home_dir) + 1; 10488 char *z = malloc( n ); 10489 if( z ) memcpy(z, home_dir, n); 10490 home_dir = z; 10491 } 10492 10493 return home_dir; 10494} 10495 10496/* 10497** Read input from the file given by sqliterc_override. Or if that 10498** parameter is NULL, take input from ~/.sqliterc 10499** 10500** Returns the number of errors. 10501*/ 10502static void process_sqliterc( 10503 ShellState *p, /* Configuration data */ 10504 const char *sqliterc_override /* Name of config file. NULL to use default */ 10505){ 10506 char *home_dir = NULL; 10507 const char *sqliterc = sqliterc_override; 10508 char *zBuf = 0; 10509 FILE *inSaved = p->in; 10510 int savedLineno = p->lineno; 10511 10512 if (sqliterc == NULL) { 10513 home_dir = find_home_dir(0); 10514 if( home_dir==0 ){ 10515 raw_printf(stderr, "-- warning: cannot find home directory;" 10516 " cannot read ~/.sqliterc\n"); 10517 return; 10518 } 10519 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); 10520 sqliterc = zBuf; 10521 } 10522 p->in = fopen(sqliterc,"rb"); 10523 if( p->in ){ 10524 if( stdin_is_interactive ){ 10525 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); 10526 } 10527 process_input(p); 10528 fclose(p->in); 10529 } 10530 p->in = inSaved; 10531 p->lineno = savedLineno; 10532 sqlite3_free(zBuf); 10533} 10534 10535/* 10536** Show available command line options 10537*/ 10538static const char zOptions[] = 10539#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 10540 " -A ARGS... run \".archive ARGS\" and exit\n" 10541#endif 10542 " -append append the database to the end of the file\n" 10543 " -ascii set output mode to 'ascii'\n" 10544 " -bail stop after hitting an error\n" 10545 " -batch force batch I/O\n" 10546 " -box set output mode to 'box'\n" 10547 " -column set output mode to 'column'\n" 10548 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 10549 " -csv set output mode to 'csv'\n" 10550#if defined(SQLITE_ENABLE_DESERIALIZE) 10551 " -deserialize open the database using sqlite3_deserialize()\n" 10552#endif 10553 " -echo print commands before execution\n" 10554 " -init FILENAME read/process named file\n" 10555 " -[no]header turn headers on or off\n" 10556#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 10557 " -heap SIZE Size of heap for memsys3 or memsys5\n" 10558#endif 10559 " -help show this message\n" 10560 " -html set output mode to HTML\n" 10561 " -interactive force interactive I/O\n" 10562 " -json set output mode to 'json'\n" 10563 " -line set output mode to 'line'\n" 10564 " -list set output mode to 'list'\n" 10565 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 10566 " -markdown set output mode to 'markdown'\n" 10567#if defined(SQLITE_ENABLE_DESERIALIZE) 10568 " -maxsize N maximum size for a --deserialize database\n" 10569#endif 10570 " -memtrace trace all memory allocations and deallocations\n" 10571 " -mmap N default mmap size set to N\n" 10572#ifdef SQLITE_ENABLE_MULTIPLEX 10573 " -multiplex enable the multiplexor VFS\n" 10574#endif 10575 " -newline SEP set output row separator. Default: '\\n'\n" 10576 " -nofollow refuse to open symbolic links to database files\n" 10577 " -nullvalue TEXT set text string for NULL values. Default ''\n" 10578 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 10579 " -quote set output mode to 'quote'\n" 10580 " -readonly open the database read-only\n" 10581 " -separator SEP set output column separator. Default: '|'\n" 10582#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10583 " -sorterref SIZE sorter references threshold size\n" 10584#endif 10585 " -stats print memory stats before each finalize\n" 10586 " -table set output mode to 'table'\n" 10587 " -version show SQLite version\n" 10588 " -vfs NAME use NAME as the default VFS\n" 10589#ifdef SQLITE_ENABLE_VFSTRACE 10590 " -vfstrace enable tracing of all VFS calls\n" 10591#endif 10592#ifdef SQLITE_HAVE_ZLIB 10593 " -zip open the file as a ZIP Archive\n" 10594#endif 10595; 10596static void usage(int showDetail){ 10597 utf8_printf(stderr, 10598 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 10599 "FILENAME is the name of an SQLite database. A new database is created\n" 10600 "if the file does not previously exist.\n", Argv0); 10601 if( showDetail ){ 10602 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); 10603 }else{ 10604 raw_printf(stderr, "Use the -help option for additional information\n"); 10605 } 10606 exit(1); 10607} 10608 10609/* 10610** Internal check: Verify that the SQLite is uninitialized. Print a 10611** error message if it is initialized. 10612*/ 10613static void verify_uninitialized(void){ 10614 if( sqlite3_config(-1)==SQLITE_MISUSE ){ 10615 utf8_printf(stdout, "WARNING: attempt to configure SQLite after" 10616 " initialization.\n"); 10617 } 10618} 10619 10620/* 10621** Initialize the state information in data 10622*/ 10623static void main_init(ShellState *data) { 10624 memset(data, 0, sizeof(*data)); 10625 data->normalMode = data->cMode = data->mode = MODE_List; 10626 data->autoExplain = 1; 10627 memcpy(data->colSeparator,SEP_Column, 2); 10628 memcpy(data->rowSeparator,SEP_Row, 2); 10629 data->showHeader = 0; 10630 data->shellFlgs = SHFLG_Lookaside; 10631 verify_uninitialized(); 10632 sqlite3_config(SQLITE_CONFIG_URI, 1); 10633 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); 10634 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 10635 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); 10636 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); 10637} 10638 10639/* 10640** Output text to the console in a font that attracts extra attention. 10641*/ 10642#ifdef _WIN32 10643static void printBold(const char *zText){ 10644#if !SQLITE_OS_WINRT 10645 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 10646 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; 10647 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); 10648 SetConsoleTextAttribute(out, 10649 FOREGROUND_RED|FOREGROUND_INTENSITY 10650 ); 10651#endif 10652 printf("%s", zText); 10653#if !SQLITE_OS_WINRT 10654 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); 10655#endif 10656} 10657#else 10658static void printBold(const char *zText){ 10659 printf("\033[1m%s\033[0m", zText); 10660} 10661#endif 10662 10663/* 10664** Get the argument to an --option. Throw an error and die if no argument 10665** is available. 10666*/ 10667static char *cmdline_option_value(int argc, char **argv, int i){ 10668 if( i==argc ){ 10669 utf8_printf(stderr, "%s: Error: missing argument to %s\n", 10670 argv[0], argv[argc-1]); 10671 exit(1); 10672 } 10673 return argv[i]; 10674} 10675 10676#ifndef SQLITE_SHELL_IS_UTF8 10677# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) 10678# define SQLITE_SHELL_IS_UTF8 (0) 10679# else 10680# define SQLITE_SHELL_IS_UTF8 (1) 10681# endif 10682#endif 10683 10684#if SQLITE_SHELL_IS_UTF8 10685int SQLITE_CDECL main(int argc, char **argv){ 10686#else 10687int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ 10688 char **argv; 10689#endif 10690 char *zErrMsg = 0; 10691 ShellState data; 10692 const char *zInitFile = 0; 10693 int i; 10694 int rc = 0; 10695 int warnInmemoryDb = 0; 10696 int readStdin = 1; 10697 int nCmd = 0; 10698 char **azCmd = 0; 10699 const char *zVfs = 0; /* Value of -vfs command-line option */ 10700#if !SQLITE_SHELL_IS_UTF8 10701 char **argvToFree = 0; 10702 int argcToFree = 0; 10703#endif 10704 10705 setBinaryMode(stdin, 0); 10706 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ 10707 stdin_is_interactive = isatty(0); 10708 stdout_is_console = isatty(1); 10709 10710#ifdef SQLITE_DEBUG 10711 registerOomSimulator(); 10712#endif 10713 10714#if !defined(_WIN32_WCE) 10715 if( getenv("SQLITE_DEBUG_BREAK") ){ 10716 if( isatty(0) && isatty(2) ){ 10717 fprintf(stderr, 10718 "attach debugger to process %d and press any key to continue.\n", 10719 GETPID()); 10720 fgetc(stdin); 10721 }else{ 10722#if defined(_WIN32) || defined(WIN32) 10723#if SQLITE_OS_WINRT 10724 __debugbreak(); 10725#else 10726 DebugBreak(); 10727#endif 10728#elif defined(SIGTRAP) 10729 raise(SIGTRAP); 10730#endif 10731 } 10732 } 10733#endif 10734 10735#if USE_SYSTEM_SQLITE+0!=1 10736 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ 10737 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", 10738 sqlite3_sourceid(), SQLITE_SOURCE_ID); 10739 exit(1); 10740 } 10741#endif 10742 main_init(&data); 10743 10744 /* On Windows, we must translate command-line arguments into UTF-8. 10745 ** The SQLite memory allocator subsystem has to be enabled in order to 10746 ** do this. But we want to run an sqlite3_shutdown() afterwards so that 10747 ** subsequent sqlite3_config() calls will work. So copy all results into 10748 ** memory that does not come from the SQLite memory allocator. 10749 */ 10750#if !SQLITE_SHELL_IS_UTF8 10751 sqlite3_initialize(); 10752 argvToFree = malloc(sizeof(argv[0])*argc*2); 10753 argcToFree = argc; 10754 argv = argvToFree + argc; 10755 if( argv==0 ) shell_out_of_memory(); 10756 for(i=0; i<argc; i++){ 10757 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); 10758 int n; 10759 if( z==0 ) shell_out_of_memory(); 10760 n = (int)strlen(z); 10761 argv[i] = malloc( n+1 ); 10762 if( argv[i]==0 ) shell_out_of_memory(); 10763 memcpy(argv[i], z, n+1); 10764 argvToFree[i] = argv[i]; 10765 sqlite3_free(z); 10766 } 10767 sqlite3_shutdown(); 10768#endif 10769 10770 assert( argc>=1 && argv && argv[0] ); 10771 Argv0 = argv[0]; 10772 10773 /* Make sure we have a valid signal handler early, before anything 10774 ** else is done. 10775 */ 10776#ifdef SIGINT 10777 signal(SIGINT, interrupt_handler); 10778#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 10779 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); 10780#endif 10781 10782#ifdef SQLITE_SHELL_DBNAME_PROC 10783 { 10784 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name 10785 ** of a C-function that will provide the name of the database file. Use 10786 ** this compile-time option to embed this shell program in larger 10787 ** applications. */ 10788 extern void SQLITE_SHELL_DBNAME_PROC(const char**); 10789 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); 10790 warnInmemoryDb = 0; 10791 } 10792#endif 10793 10794 /* Do an initial pass through the command-line argument to locate 10795 ** the name of the database file, the name of the initialization file, 10796 ** the size of the alternative malloc heap, 10797 ** and the first command to execute. 10798 */ 10799 verify_uninitialized(); 10800 for(i=1; i<argc; i++){ 10801 char *z; 10802 z = argv[i]; 10803 if( z[0]!='-' ){ 10804 if( data.zDbFilename==0 ){ 10805 data.zDbFilename = z; 10806 }else{ 10807 /* Excesss arguments are interpreted as SQL (or dot-commands) and 10808 ** mean that nothing is read from stdin */ 10809 readStdin = 0; 10810 nCmd++; 10811 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); 10812 if( azCmd==0 ) shell_out_of_memory(); 10813 azCmd[nCmd-1] = z; 10814 } 10815 } 10816 if( z[1]=='-' ) z++; 10817 if( strcmp(z,"-separator")==0 10818 || strcmp(z,"-nullvalue")==0 10819 || strcmp(z,"-newline")==0 10820 || strcmp(z,"-cmd")==0 10821 ){ 10822 (void)cmdline_option_value(argc, argv, ++i); 10823 }else if( strcmp(z,"-init")==0 ){ 10824 zInitFile = cmdline_option_value(argc, argv, ++i); 10825 }else if( strcmp(z,"-batch")==0 ){ 10826 /* Need to check for batch mode here to so we can avoid printing 10827 ** informational messages (like from process_sqliterc) before 10828 ** we do the actual processing of arguments later in a second pass. 10829 */ 10830 stdin_is_interactive = 0; 10831 }else if( strcmp(z,"-heap")==0 ){ 10832#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 10833 const char *zSize; 10834 sqlite3_int64 szHeap; 10835 10836 zSize = cmdline_option_value(argc, argv, ++i); 10837 szHeap = integerValue(zSize); 10838 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; 10839 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); 10840#else 10841 (void)cmdline_option_value(argc, argv, ++i); 10842#endif 10843 }else if( strcmp(z,"-pagecache")==0 ){ 10844 int n, sz; 10845 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10846 if( sz>70000 ) sz = 70000; 10847 if( sz<0 ) sz = 0; 10848 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10849 sqlite3_config(SQLITE_CONFIG_PAGECACHE, 10850 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); 10851 data.shellFlgs |= SHFLG_Pagecache; 10852 }else if( strcmp(z,"-lookaside")==0 ){ 10853 int n, sz; 10854 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10855 if( sz<0 ) sz = 0; 10856 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10857 if( n<0 ) n = 0; 10858 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); 10859 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; 10860#ifdef SQLITE_ENABLE_VFSTRACE 10861 }else if( strcmp(z,"-vfstrace")==0 ){ 10862 extern int vfstrace_register( 10863 const char *zTraceName, 10864 const char *zOldVfsName, 10865 int (*xOut)(const char*,void*), 10866 void *pOutArg, 10867 int makeDefault 10868 ); 10869 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); 10870#endif 10871#ifdef SQLITE_ENABLE_MULTIPLEX 10872 }else if( strcmp(z,"-multiplex")==0 ){ 10873 extern int sqlite3_multiple_initialize(const char*,int); 10874 sqlite3_multiplex_initialize(0, 1); 10875#endif 10876 }else if( strcmp(z,"-mmap")==0 ){ 10877 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10878 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); 10879#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10880 }else if( strcmp(z,"-sorterref")==0 ){ 10881 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10882 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); 10883#endif 10884 }else if( strcmp(z,"-vfs")==0 ){ 10885 zVfs = cmdline_option_value(argc, argv, ++i); 10886#ifdef SQLITE_HAVE_ZLIB 10887 }else if( strcmp(z,"-zip")==0 ){ 10888 data.openMode = SHELL_OPEN_ZIPFILE; 10889#endif 10890 }else if( strcmp(z,"-append")==0 ){ 10891 data.openMode = SHELL_OPEN_APPENDVFS; 10892#ifdef SQLITE_ENABLE_DESERIALIZE 10893 }else if( strcmp(z,"-deserialize")==0 ){ 10894 data.openMode = SHELL_OPEN_DESERIALIZE; 10895 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 10896 data.szMax = integerValue(argv[++i]); 10897#endif 10898 }else if( strcmp(z,"-readonly")==0 ){ 10899 data.openMode = SHELL_OPEN_READONLY; 10900 }else if( strcmp(z,"-nofollow")==0 ){ 10901 data.openFlags = SQLITE_OPEN_NOFOLLOW; 10902#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 10903 }else if( strncmp(z, "-A",2)==0 ){ 10904 /* All remaining command-line arguments are passed to the ".archive" 10905 ** command, so ignore them */ 10906 break; 10907#endif 10908 }else if( strcmp(z, "-memtrace")==0 ){ 10909 sqlite3MemTraceActivate(stderr); 10910 } 10911 } 10912 verify_uninitialized(); 10913 10914 10915#ifdef SQLITE_SHELL_INIT_PROC 10916 { 10917 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name 10918 ** of a C-function that will perform initialization actions on SQLite that 10919 ** occur just before or after sqlite3_initialize(). Use this compile-time 10920 ** option to embed this shell program in larger applications. */ 10921 extern void SQLITE_SHELL_INIT_PROC(void); 10922 SQLITE_SHELL_INIT_PROC(); 10923 } 10924#else 10925 /* All the sqlite3_config() calls have now been made. So it is safe 10926 ** to call sqlite3_initialize() and process any command line -vfs option. */ 10927 sqlite3_initialize(); 10928#endif 10929 10930 if( zVfs ){ 10931 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); 10932 if( pVfs ){ 10933 sqlite3_vfs_register(pVfs, 1); 10934 }else{ 10935 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); 10936 exit(1); 10937 } 10938 } 10939 10940 if( data.zDbFilename==0 ){ 10941#ifndef SQLITE_OMIT_MEMORYDB 10942 data.zDbFilename = ":memory:"; 10943 warnInmemoryDb = argc==1; 10944#else 10945 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); 10946 return 1; 10947#endif 10948 } 10949 data.out = stdout; 10950 sqlite3_appendvfs_init(0,0,0); 10951 10952 /* Go ahead and open the database file if it already exists. If the 10953 ** file does not exist, delay opening it. This prevents empty database 10954 ** files from being created if a user mistypes the database name argument 10955 ** to the sqlite command-line tool. 10956 */ 10957 if( access(data.zDbFilename, 0)==0 ){ 10958 open_db(&data, 0); 10959 } 10960 10961 /* Process the initialization file if there is one. If no -init option 10962 ** is given on the command line, look for a file named ~/.sqliterc and 10963 ** try to process it. 10964 */ 10965 process_sqliterc(&data,zInitFile); 10966 10967 /* Make a second pass through the command-line argument and set 10968 ** options. This second pass is delayed until after the initialization 10969 ** file is processed so that the command-line arguments will override 10970 ** settings in the initialization file. 10971 */ 10972 for(i=1; i<argc; i++){ 10973 char *z = argv[i]; 10974 if( z[0]!='-' ) continue; 10975 if( z[1]=='-' ){ z++; } 10976 if( strcmp(z,"-init")==0 ){ 10977 i++; 10978 }else if( strcmp(z,"-html")==0 ){ 10979 data.mode = MODE_Html; 10980 }else if( strcmp(z,"-list")==0 ){ 10981 data.mode = MODE_List; 10982 }else if( strcmp(z,"-quote")==0 ){ 10983 data.mode = MODE_Quote; 10984 }else if( strcmp(z,"-line")==0 ){ 10985 data.mode = MODE_Line; 10986 }else if( strcmp(z,"-column")==0 ){ 10987 data.mode = MODE_Column; 10988 }else if( strcmp(z,"-json")==0 ){ 10989 data.mode = MODE_Json; 10990 }else if( strcmp(z,"-markdown")==0 ){ 10991 data.mode = MODE_Markdown; 10992 }else if( strcmp(z,"-table")==0 ){ 10993 data.mode = MODE_Table; 10994 }else if( strcmp(z,"-box")==0 ){ 10995 data.mode = MODE_Box; 10996 }else if( strcmp(z,"-csv")==0 ){ 10997 data.mode = MODE_Csv; 10998 memcpy(data.colSeparator,",",2); 10999#ifdef SQLITE_HAVE_ZLIB 11000 }else if( strcmp(z,"-zip")==0 ){ 11001 data.openMode = SHELL_OPEN_ZIPFILE; 11002#endif 11003 }else if( strcmp(z,"-append")==0 ){ 11004 data.openMode = SHELL_OPEN_APPENDVFS; 11005#ifdef SQLITE_ENABLE_DESERIALIZE 11006 }else if( strcmp(z,"-deserialize")==0 ){ 11007 data.openMode = SHELL_OPEN_DESERIALIZE; 11008 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 11009 data.szMax = integerValue(argv[++i]); 11010#endif 11011 }else if( strcmp(z,"-readonly")==0 ){ 11012 data.openMode = SHELL_OPEN_READONLY; 11013 }else if( strcmp(z,"-nofollow")==0 ){ 11014 data.openFlags |= SQLITE_OPEN_NOFOLLOW; 11015 }else if( strcmp(z,"-ascii")==0 ){ 11016 data.mode = MODE_Ascii; 11017 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 11018 SEP_Unit); 11019 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 11020 SEP_Record); 11021 }else if( strcmp(z,"-separator")==0 ){ 11022 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 11023 "%s",cmdline_option_value(argc,argv,++i)); 11024 }else if( strcmp(z,"-newline")==0 ){ 11025 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 11026 "%s",cmdline_option_value(argc,argv,++i)); 11027 }else if( strcmp(z,"-nullvalue")==0 ){ 11028 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, 11029 "%s",cmdline_option_value(argc,argv,++i)); 11030 }else if( strcmp(z,"-header")==0 ){ 11031 data.showHeader = 1; 11032 }else if( strcmp(z,"-noheader")==0 ){ 11033 data.showHeader = 0; 11034 }else if( strcmp(z,"-echo")==0 ){ 11035 ShellSetFlag(&data, SHFLG_Echo); 11036 }else if( strcmp(z,"-eqp")==0 ){ 11037 data.autoEQP = AUTOEQP_on; 11038 }else if( strcmp(z,"-eqpfull")==0 ){ 11039 data.autoEQP = AUTOEQP_full; 11040 }else if( strcmp(z,"-stats")==0 ){ 11041 data.statsOn = 1; 11042 }else if( strcmp(z,"-scanstats")==0 ){ 11043 data.scanstatsOn = 1; 11044 }else if( strcmp(z,"-backslash")==0 ){ 11045 /* Undocumented command-line option: -backslash 11046 ** Causes C-style backslash escapes to be evaluated in SQL statements 11047 ** prior to sending the SQL into SQLite. Useful for injecting 11048 ** crazy bytes in the middle of SQL statements for testing and debugging. 11049 */ 11050 ShellSetFlag(&data, SHFLG_Backslash); 11051 }else if( strcmp(z,"-bail")==0 ){ 11052 bail_on_error = 1; 11053 }else if( strcmp(z,"-version")==0 ){ 11054 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 11055 return 0; 11056 }else if( strcmp(z,"-interactive")==0 ){ 11057 stdin_is_interactive = 1; 11058 }else if( strcmp(z,"-batch")==0 ){ 11059 stdin_is_interactive = 0; 11060 }else if( strcmp(z,"-heap")==0 ){ 11061 i++; 11062 }else if( strcmp(z,"-pagecache")==0 ){ 11063 i+=2; 11064 }else if( strcmp(z,"-lookaside")==0 ){ 11065 i+=2; 11066 }else if( strcmp(z,"-mmap")==0 ){ 11067 i++; 11068 }else if( strcmp(z,"-memtrace")==0 ){ 11069 i++; 11070#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11071 }else if( strcmp(z,"-sorterref")==0 ){ 11072 i++; 11073#endif 11074 }else if( strcmp(z,"-vfs")==0 ){ 11075 i++; 11076#ifdef SQLITE_ENABLE_VFSTRACE 11077 }else if( strcmp(z,"-vfstrace")==0 ){ 11078 i++; 11079#endif 11080#ifdef SQLITE_ENABLE_MULTIPLEX 11081 }else if( strcmp(z,"-multiplex")==0 ){ 11082 i++; 11083#endif 11084 }else if( strcmp(z,"-help")==0 ){ 11085 usage(1); 11086 }else if( strcmp(z,"-cmd")==0 ){ 11087 /* Run commands that follow -cmd first and separately from commands 11088 ** that simply appear on the command-line. This seems goofy. It would 11089 ** be better if all commands ran in the order that they appear. But 11090 ** we retain the goofy behavior for historical compatibility. */ 11091 if( i==argc-1 ) break; 11092 z = cmdline_option_value(argc,argv,++i); 11093 if( z[0]=='.' ){ 11094 rc = do_meta_command(z, &data); 11095 if( rc && bail_on_error ) return rc==2 ? 0 : rc; 11096 }else{ 11097 open_db(&data, 0); 11098 rc = shell_exec(&data, z, &zErrMsg); 11099 if( zErrMsg!=0 ){ 11100 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11101 if( bail_on_error ) return rc!=0 ? rc : 1; 11102 }else if( rc!=0 ){ 11103 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); 11104 if( bail_on_error ) return rc; 11105 } 11106 } 11107#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 11108 }else if( strncmp(z, "-A", 2)==0 ){ 11109 if( nCmd>0 ){ 11110 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" 11111 " with \"%s\"\n", z); 11112 return 1; 11113 } 11114 open_db(&data, OPEN_DB_ZIPFILE); 11115 if( z[2] ){ 11116 argv[i] = &z[2]; 11117 arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); 11118 }else{ 11119 arDotCommand(&data, 1, argv+i, argc-i); 11120 } 11121 readStdin = 0; 11122 break; 11123#endif 11124 }else{ 11125 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); 11126 raw_printf(stderr,"Use -help for a list of options.\n"); 11127 return 1; 11128 } 11129 data.cMode = data.mode; 11130 } 11131 11132 if( !readStdin ){ 11133 /* Run all arguments that do not begin with '-' as if they were separate 11134 ** command-line inputs, except for the argToSkip argument which contains 11135 ** the database filename. 11136 */ 11137 for(i=0; i<nCmd; i++){ 11138 if( azCmd[i][0]=='.' ){ 11139 rc = do_meta_command(azCmd[i], &data); 11140 if( rc ) return rc==2 ? 0 : rc; 11141 }else{ 11142 open_db(&data, 0); 11143 rc = shell_exec(&data, azCmd[i], &zErrMsg); 11144 if( zErrMsg!=0 ){ 11145 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11146 return rc!=0 ? rc : 1; 11147 }else if( rc!=0 ){ 11148 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); 11149 return rc; 11150 } 11151 } 11152 } 11153 free(azCmd); 11154 }else{ 11155 /* Run commands received from standard input 11156 */ 11157 if( stdin_is_interactive ){ 11158 char *zHome; 11159 char *zHistory; 11160 int nHistory; 11161 printf( 11162 "SQLite version %s %.19s\n" /*extra-version-info*/ 11163 "Enter \".help\" for usage hints.\n", 11164 sqlite3_libversion(), sqlite3_sourceid() 11165 ); 11166 if( warnInmemoryDb ){ 11167 printf("Connected to a "); 11168 printBold("transient in-memory database"); 11169 printf(".\nUse \".open FILENAME\" to reopen on a " 11170 "persistent database.\n"); 11171 } 11172 zHistory = getenv("SQLITE_HISTORY"); 11173 if( zHistory ){ 11174 zHistory = strdup(zHistory); 11175 }else if( (zHome = find_home_dir(0))!=0 ){ 11176 nHistory = strlen30(zHome) + 20; 11177 if( (zHistory = malloc(nHistory))!=0 ){ 11178 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); 11179 } 11180 } 11181 if( zHistory ){ shell_read_history(zHistory); } 11182#if HAVE_READLINE || HAVE_EDITLINE 11183 rl_attempted_completion_function = readline_completion; 11184#elif HAVE_LINENOISE 11185 linenoiseSetCompletionCallback(linenoise_completion); 11186#endif 11187 data.in = 0; 11188 rc = process_input(&data); 11189 if( zHistory ){ 11190 shell_stifle_history(2000); 11191 shell_write_history(zHistory); 11192 free(zHistory); 11193 } 11194 }else{ 11195 data.in = stdin; 11196 rc = process_input(&data); 11197 } 11198 } 11199 set_table_name(&data, 0); 11200 if( data.db ){ 11201 session_close_all(&data); 11202 close_db(data.db); 11203 } 11204 sqlite3_free(data.zFreeOnClose); 11205 find_home_dir(1); 11206 output_reset(&data); 11207 data.doXdgOpen = 0; 11208 clearTempFile(&data); 11209#if !SQLITE_SHELL_IS_UTF8 11210 for(i=0; i<argcToFree; i++) free(argvToFree[i]); 11211 free(argvToFree); 11212#endif 11213 free(data.colWidth); 11214 /* Clear the global data structure so that valgrind will detect memory 11215 ** leaks */ 11216 memset(&data, 0, sizeof(data)); 11217 return rc; 11218} 11219