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** Warning pragmas copied from msvc.h in the core. 22*/ 23#if defined(_MSC_VER) 24#pragma warning(disable : 4054) 25#pragma warning(disable : 4055) 26#pragma warning(disable : 4100) 27#pragma warning(disable : 4127) 28#pragma warning(disable : 4130) 29#pragma warning(disable : 4152) 30#pragma warning(disable : 4189) 31#pragma warning(disable : 4206) 32#pragma warning(disable : 4210) 33#pragma warning(disable : 4232) 34#pragma warning(disable : 4244) 35#pragma warning(disable : 4305) 36#pragma warning(disable : 4306) 37#pragma warning(disable : 4702) 38#pragma warning(disable : 4706) 39#endif /* defined(_MSC_VER) */ 40 41/* 42** No support for loadable extensions in VxWorks. 43*/ 44#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION 45# define SQLITE_OMIT_LOAD_EXTENSION 1 46#endif 47 48/* 49** Enable large-file support for fopen() and friends on unix. 50*/ 51#ifndef SQLITE_DISABLE_LFS 52# define _LARGE_FILE 1 53# ifndef _FILE_OFFSET_BITS 54# define _FILE_OFFSET_BITS 64 55# endif 56# define _LARGEFILE_SOURCE 1 57#endif 58 59#include <stdlib.h> 60#include <string.h> 61#include <stdio.h> 62#include <assert.h> 63#include "sqlite3.h" 64typedef sqlite3_int64 i64; 65typedef sqlite3_uint64 u64; 66typedef unsigned char u8; 67#if SQLITE_USER_AUTHENTICATION 68# include "sqlite3userauth.h" 69#endif 70#include <ctype.h> 71#include <stdarg.h> 72 73#if !defined(_WIN32) && !defined(WIN32) 74# include <signal.h> 75# if !defined(__RTP__) && !defined(_WRS_KERNEL) 76# include <pwd.h> 77# endif 78#endif 79#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) 80# include <unistd.h> 81# include <dirent.h> 82# define GETPID getpid 83# if defined(__MINGW32__) 84# define DIRENT dirent 85# ifndef S_ISLNK 86# define S_ISLNK(mode) (0) 87# endif 88# endif 89#else 90# define GETPID (int)GetCurrentProcessId 91#endif 92#include <sys/types.h> 93#include <sys/stat.h> 94 95#if HAVE_READLINE 96# include <readline/readline.h> 97# include <readline/history.h> 98#endif 99 100#if HAVE_EDITLINE 101# include <editline/readline.h> 102#endif 103 104#if HAVE_EDITLINE || HAVE_READLINE 105 106# define shell_add_history(X) add_history(X) 107# define shell_read_history(X) read_history(X) 108# define shell_write_history(X) write_history(X) 109# define shell_stifle_history(X) stifle_history(X) 110# define shell_readline(X) readline(X) 111 112#elif HAVE_LINENOISE 113 114# include "linenoise.h" 115# define shell_add_history(X) linenoiseHistoryAdd(X) 116# define shell_read_history(X) linenoiseHistoryLoad(X) 117# define shell_write_history(X) linenoiseHistorySave(X) 118# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) 119# define shell_readline(X) linenoise(X) 120 121#else 122 123# define shell_read_history(X) 124# define shell_write_history(X) 125# define shell_stifle_history(X) 126 127# define SHELL_USE_LOCAL_GETLINE 1 128#endif 129 130 131#if defined(_WIN32) || defined(WIN32) 132# include <io.h> 133# include <fcntl.h> 134# define isatty(h) _isatty(h) 135# ifndef access 136# define access(f,m) _access((f),(m)) 137# endif 138# ifndef unlink 139# define unlink _unlink 140# endif 141# ifndef strdup 142# define strdup _strdup 143# endif 144# undef popen 145# define popen _popen 146# undef pclose 147# define pclose _pclose 148#else 149 /* Make sure isatty() has a prototype. */ 150 extern int isatty(int); 151 152# if !defined(__RTP__) && !defined(_WRS_KERNEL) 153 /* popen and pclose are not C89 functions and so are 154 ** sometimes omitted from the <stdio.h> header */ 155 extern FILE *popen(const char*,const char*); 156 extern int pclose(FILE*); 157# else 158# define SQLITE_OMIT_POPEN 1 159# endif 160#endif 161 162#if defined(_WIN32_WCE) 163/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() 164 * thus we always assume that we have a console. That can be 165 * overridden with the -batch command line option. 166 */ 167#define isatty(x) 1 168#endif 169 170/* ctype macros that work with signed characters */ 171#define IsSpace(X) isspace((unsigned char)X) 172#define IsDigit(X) isdigit((unsigned char)X) 173#define ToLower(X) (char)tolower((unsigned char)X) 174 175#if defined(_WIN32) || defined(WIN32) 176#include <windows.h> 177 178/* string conversion routines only needed on Win32 */ 179extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); 180extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); 181extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); 182extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); 183#endif 184 185/* On Windows, we normally run with output mode of TEXT so that \n characters 186** are automatically translated into \r\n. However, this behavior needs 187** to be disabled in some cases (ex: when generating CSV output and when 188** rendering quoted strings that contain \n characters). The following 189** routines take care of that. 190*/ 191#if defined(_WIN32) || defined(WIN32) 192static void setBinaryMode(FILE *file, int isOutput){ 193 if( isOutput ) fflush(file); 194 _setmode(_fileno(file), _O_BINARY); 195} 196static void setTextMode(FILE *file, int isOutput){ 197 if( isOutput ) fflush(file); 198 _setmode(_fileno(file), _O_TEXT); 199} 200#else 201# define setBinaryMode(X,Y) 202# define setTextMode(X,Y) 203#endif 204 205 206/* True if the timer is enabled */ 207static int enableTimer = 0; 208 209/* Return the current wall-clock time */ 210static sqlite3_int64 timeOfDay(void){ 211 static sqlite3_vfs *clockVfs = 0; 212 sqlite3_int64 t; 213 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 214 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 215 clockVfs->xCurrentTimeInt64(clockVfs, &t); 216 }else{ 217 double r; 218 clockVfs->xCurrentTime(clockVfs, &r); 219 t = (sqlite3_int64)(r*86400000.0); 220 } 221 return t; 222} 223 224#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) 225#include <sys/time.h> 226#include <sys/resource.h> 227 228/* VxWorks does not support getrusage() as far as we can determine */ 229#if defined(_WRS_KERNEL) || defined(__RTP__) 230struct rusage { 231 struct timeval ru_utime; /* user CPU time used */ 232 struct timeval ru_stime; /* system CPU time used */ 233}; 234#define getrusage(A,B) memset(B,0,sizeof(*B)) 235#endif 236 237/* Saved resource information for the beginning of an operation */ 238static struct rusage sBegin; /* CPU time at start */ 239static sqlite3_int64 iBegin; /* Wall-clock time at start */ 240 241/* 242** Begin timing an operation 243*/ 244static void beginTimer(void){ 245 if( enableTimer ){ 246 getrusage(RUSAGE_SELF, &sBegin); 247 iBegin = timeOfDay(); 248 } 249} 250 251/* Return the difference of two time_structs in seconds */ 252static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ 253 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 254 (double)(pEnd->tv_sec - pStart->tv_sec); 255} 256 257/* 258** Print the timing results. 259*/ 260static void endTimer(void){ 261 if( enableTimer ){ 262 sqlite3_int64 iEnd = timeOfDay(); 263 struct rusage sEnd; 264 getrusage(RUSAGE_SELF, &sEnd); 265 printf("Run Time: real %.3f user %f sys %f\n", 266 (iEnd - iBegin)*0.001, 267 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), 268 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); 269 } 270} 271 272#define BEGIN_TIMER beginTimer() 273#define END_TIMER endTimer() 274#define HAS_TIMER 1 275 276#elif (defined(_WIN32) || defined(WIN32)) 277 278/* Saved resource information for the beginning of an operation */ 279static HANDLE hProcess; 280static FILETIME ftKernelBegin; 281static FILETIME ftUserBegin; 282static sqlite3_int64 ftWallBegin; 283typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, 284 LPFILETIME, LPFILETIME); 285static GETPROCTIMES getProcessTimesAddr = NULL; 286 287/* 288** Check to see if we have timer support. Return 1 if necessary 289** support found (or found previously). 290*/ 291static int hasTimer(void){ 292 if( getProcessTimesAddr ){ 293 return 1; 294 } else { 295 /* GetProcessTimes() isn't supported in WIN95 and some other Windows 296 ** versions. See if the version we are running on has it, and if it 297 ** does, save off a pointer to it and the current process handle. 298 */ 299 hProcess = GetCurrentProcess(); 300 if( hProcess ){ 301 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); 302 if( NULL != hinstLib ){ 303 getProcessTimesAddr = 304 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); 305 if( NULL != getProcessTimesAddr ){ 306 return 1; 307 } 308 FreeLibrary(hinstLib); 309 } 310 } 311 } 312 return 0; 313} 314 315/* 316** Begin timing an operation 317*/ 318static void beginTimer(void){ 319 if( enableTimer && getProcessTimesAddr ){ 320 FILETIME ftCreation, ftExit; 321 getProcessTimesAddr(hProcess,&ftCreation,&ftExit, 322 &ftKernelBegin,&ftUserBegin); 323 ftWallBegin = timeOfDay(); 324 } 325} 326 327/* Return the difference of two FILETIME structs in seconds */ 328static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ 329 sqlite_int64 i64Start = *((sqlite_int64 *) pStart); 330 sqlite_int64 i64End = *((sqlite_int64 *) pEnd); 331 return (double) ((i64End - i64Start) / 10000000.0); 332} 333 334/* 335** Print the timing results. 336*/ 337static void endTimer(void){ 338 if( enableTimer && getProcessTimesAddr){ 339 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; 340 sqlite3_int64 ftWallEnd = timeOfDay(); 341 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); 342 printf("Run Time: real %.3f user %f sys %f\n", 343 (ftWallEnd - ftWallBegin)*0.001, 344 timeDiff(&ftUserBegin, &ftUserEnd), 345 timeDiff(&ftKernelBegin, &ftKernelEnd)); 346 } 347} 348 349#define BEGIN_TIMER beginTimer() 350#define END_TIMER endTimer() 351#define HAS_TIMER hasTimer() 352 353#else 354#define BEGIN_TIMER 355#define END_TIMER 356#define HAS_TIMER 0 357#endif 358 359/* 360** Used to prevent warnings about unused parameters 361*/ 362#define UNUSED_PARAMETER(x) (void)(x) 363 364/* 365** Number of elements in an array 366*/ 367#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) 368 369/* 370** If the following flag is set, then command execution stops 371** at an error if we are not interactive. 372*/ 373static int bail_on_error = 0; 374 375/* 376** Threat stdin as an interactive input if the following variable 377** is true. Otherwise, assume stdin is connected to a file or pipe. 378*/ 379static int stdin_is_interactive = 1; 380 381/* 382** On Windows systems we have to know if standard output is a console 383** in order to translate UTF-8 into MBCS. The following variable is 384** true if translation is required. 385*/ 386static int stdout_is_console = 1; 387 388/* 389** The following is the open SQLite database. We make a pointer 390** to this database a static variable so that it can be accessed 391** by the SIGINT handler to interrupt database processing. 392*/ 393static sqlite3 *globalDb = 0; 394 395/* 396** True if an interrupt (Control-C) has been received. 397*/ 398static volatile int seenInterrupt = 0; 399 400/* 401** This is the name of our program. It is set in main(), used 402** in a number of other places, mostly for error messages. 403*/ 404static char *Argv0; 405 406/* 407** Prompt strings. Initialized in main. Settable with 408** .prompt main continue 409*/ 410static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ 411static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ 412 413/* 414** Render output like fprintf(). Except, if the output is going to the 415** console and if this is running on a Windows machine, translate the 416** output from UTF-8 into MBCS. 417*/ 418#if defined(_WIN32) || defined(WIN32) 419void utf8_printf(FILE *out, const char *zFormat, ...){ 420 va_list ap; 421 va_start(ap, zFormat); 422 if( stdout_is_console && (out==stdout || out==stderr) ){ 423 char *z1 = sqlite3_vmprintf(zFormat, ap); 424 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); 425 sqlite3_free(z1); 426 fputs(z2, out); 427 sqlite3_free(z2); 428 }else{ 429 vfprintf(out, zFormat, ap); 430 } 431 va_end(ap); 432} 433#elif !defined(utf8_printf) 434# define utf8_printf fprintf 435#endif 436 437/* 438** Render output like fprintf(). This should not be used on anything that 439** includes string formatting (e.g. "%s"). 440*/ 441#if !defined(raw_printf) 442# define raw_printf fprintf 443#endif 444 445/* Indicate out-of-memory and exit. */ 446static void shell_out_of_memory(void){ 447 raw_printf(stderr,"Error: out of memory\n"); 448 exit(1); 449} 450 451/* 452** Write I/O traces to the following stream. 453*/ 454#ifdef SQLITE_ENABLE_IOTRACE 455static FILE *iotrace = 0; 456#endif 457 458/* 459** This routine works like printf in that its first argument is a 460** format string and subsequent arguments are values to be substituted 461** in place of % fields. The result of formatting this string 462** is written to iotrace. 463*/ 464#ifdef SQLITE_ENABLE_IOTRACE 465static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ 466 va_list ap; 467 char *z; 468 if( iotrace==0 ) return; 469 va_start(ap, zFormat); 470 z = sqlite3_vmprintf(zFormat, ap); 471 va_end(ap); 472 utf8_printf(iotrace, "%s", z); 473 sqlite3_free(z); 474} 475#endif 476 477/* 478** Output string zUtf to stream pOut as w characters. If w is negative, 479** then right-justify the text. W is the width in UTF-8 characters, not 480** in bytes. This is different from the %*.*s specification in printf 481** since with %*.*s the width is measured in bytes, not characters. 482*/ 483static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ 484 int i; 485 int n; 486 int aw = w<0 ? -w : w; 487 char zBuf[1000]; 488 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; 489 for(i=n=0; zUtf[i]; i++){ 490 if( (zUtf[i]&0xc0)!=0x80 ){ 491 n++; 492 if( n==aw ){ 493 do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); 494 break; 495 } 496 } 497 } 498 if( n>=aw ){ 499 utf8_printf(pOut, "%.*s", i, zUtf); 500 }else if( w<0 ){ 501 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); 502 }else{ 503 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); 504 } 505} 506 507 508/* 509** Determines if a string is a number of not. 510*/ 511static int isNumber(const char *z, int *realnum){ 512 if( *z=='-' || *z=='+' ) z++; 513 if( !IsDigit(*z) ){ 514 return 0; 515 } 516 z++; 517 if( realnum ) *realnum = 0; 518 while( IsDigit(*z) ){ z++; } 519 if( *z=='.' ){ 520 z++; 521 if( !IsDigit(*z) ) return 0; 522 while( IsDigit(*z) ){ z++; } 523 if( realnum ) *realnum = 1; 524 } 525 if( *z=='e' || *z=='E' ){ 526 z++; 527 if( *z=='+' || *z=='-' ) z++; 528 if( !IsDigit(*z) ) return 0; 529 while( IsDigit(*z) ){ z++; } 530 if( realnum ) *realnum = 1; 531 } 532 return *z==0; 533} 534 535/* 536** Compute a string length that is limited to what can be stored in 537** lower 30 bits of a 32-bit signed integer. 538*/ 539static int strlen30(const char *z){ 540 const char *z2 = z; 541 while( *z2 ){ z2++; } 542 return 0x3fffffff & (int)(z2 - z); 543} 544 545/* 546** Return the length of a string in characters. Multibyte UTF8 characters 547** count as a single character. 548*/ 549static int strlenChar(const char *z){ 550 int n = 0; 551 while( *z ){ 552 if( (0xc0&*(z++))!=0x80 ) n++; 553 } 554 return n; 555} 556 557/* 558** This routine reads a line of text from FILE in, stores 559** the text in memory obtained from malloc() and returns a pointer 560** to the text. NULL is returned at end of file, or if malloc() 561** fails. 562** 563** If zLine is not NULL then it is a malloced buffer returned from 564** a previous call to this routine that may be reused. 565*/ 566static char *local_getline(char *zLine, FILE *in){ 567 int nLine = zLine==0 ? 0 : 100; 568 int n = 0; 569 570 while( 1 ){ 571 if( n+100>nLine ){ 572 nLine = nLine*2 + 100; 573 zLine = realloc(zLine, nLine); 574 if( zLine==0 ) shell_out_of_memory(); 575 } 576 if( fgets(&zLine[n], nLine - n, in)==0 ){ 577 if( n==0 ){ 578 free(zLine); 579 return 0; 580 } 581 zLine[n] = 0; 582 break; 583 } 584 while( zLine[n] ) n++; 585 if( n>0 && zLine[n-1]=='\n' ){ 586 n--; 587 if( n>0 && zLine[n-1]=='\r' ) n--; 588 zLine[n] = 0; 589 break; 590 } 591 } 592#if defined(_WIN32) || defined(WIN32) 593 /* For interactive input on Windows systems, translate the 594 ** multi-byte characterset characters into UTF-8. */ 595 if( stdin_is_interactive && in==stdin ){ 596 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); 597 if( zTrans ){ 598 int nTrans = strlen30(zTrans)+1; 599 if( nTrans>nLine ){ 600 zLine = realloc(zLine, nTrans); 601 if( zLine==0 ) shell_out_of_memory(); 602 } 603 memcpy(zLine, zTrans, nTrans); 604 sqlite3_free(zTrans); 605 } 606 } 607#endif /* defined(_WIN32) || defined(WIN32) */ 608 return zLine; 609} 610 611/* 612** Retrieve a single line of input text. 613** 614** If in==0 then read from standard input and prompt before each line. 615** If isContinuation is true, then a continuation prompt is appropriate. 616** If isContinuation is zero, then the main prompt should be used. 617** 618** If zPrior is not NULL then it is a buffer from a prior call to this 619** routine that can be reused. 620** 621** The result is stored in space obtained from malloc() and must either 622** be freed by the caller or else passed back into this routine via the 623** zPrior argument for reuse. 624*/ 625static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 626 char *zPrompt; 627 char *zResult; 628 if( in!=0 ){ 629 zResult = local_getline(zPrior, in); 630 }else{ 631 zPrompt = isContinuation ? continuePrompt : mainPrompt; 632#if SHELL_USE_LOCAL_GETLINE 633 printf("%s", zPrompt); 634 fflush(stdout); 635 zResult = local_getline(zPrior, stdin); 636#else 637 free(zPrior); 638 zResult = shell_readline(zPrompt); 639 if( zResult && *zResult ) shell_add_history(zResult); 640#endif 641 } 642 return zResult; 643} 644 645 646/* 647** Return the value of a hexadecimal digit. Return -1 if the input 648** is not a hex digit. 649*/ 650static int hexDigitValue(char c){ 651 if( c>='0' && c<='9' ) return c - '0'; 652 if( c>='a' && c<='f' ) return c - 'a' + 10; 653 if( c>='A' && c<='F' ) return c - 'A' + 10; 654 return -1; 655} 656 657/* 658** Interpret zArg as an integer value, possibly with suffixes. 659*/ 660static sqlite3_int64 integerValue(const char *zArg){ 661 sqlite3_int64 v = 0; 662 static const struct { char *zSuffix; int iMult; } aMult[] = { 663 { "KiB", 1024 }, 664 { "MiB", 1024*1024 }, 665 { "GiB", 1024*1024*1024 }, 666 { "KB", 1000 }, 667 { "MB", 1000000 }, 668 { "GB", 1000000000 }, 669 { "K", 1000 }, 670 { "M", 1000000 }, 671 { "G", 1000000000 }, 672 }; 673 int i; 674 int isNeg = 0; 675 if( zArg[0]=='-' ){ 676 isNeg = 1; 677 zArg++; 678 }else if( zArg[0]=='+' ){ 679 zArg++; 680 } 681 if( zArg[0]=='0' && zArg[1]=='x' ){ 682 int x; 683 zArg += 2; 684 while( (x = hexDigitValue(zArg[0]))>=0 ){ 685 v = (v<<4) + x; 686 zArg++; 687 } 688 }else{ 689 while( IsDigit(zArg[0]) ){ 690 v = v*10 + zArg[0] - '0'; 691 zArg++; 692 } 693 } 694 for(i=0; i<ArraySize(aMult); i++){ 695 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 696 v *= aMult[i].iMult; 697 break; 698 } 699 } 700 return isNeg? -v : v; 701} 702 703/* 704** A variable length string to which one can append text. 705*/ 706typedef struct ShellText ShellText; 707struct ShellText { 708 char *z; 709 int n; 710 int nAlloc; 711}; 712 713/* 714** Initialize and destroy a ShellText object 715*/ 716static void initText(ShellText *p){ 717 memset(p, 0, sizeof(*p)); 718} 719static void freeText(ShellText *p){ 720 free(p->z); 721 initText(p); 722} 723 724/* zIn is either a pointer to a NULL-terminated string in memory obtained 725** from malloc(), or a NULL pointer. The string pointed to by zAppend is 726** added to zIn, and the result returned in memory obtained from malloc(). 727** zIn, if it was not NULL, is freed. 728** 729** If the third argument, quote, is not '\0', then it is used as a 730** quote character for zAppend. 731*/ 732static void appendText(ShellText *p, char const *zAppend, char quote){ 733 int len; 734 int i; 735 int nAppend = strlen30(zAppend); 736 737 len = nAppend+p->n+1; 738 if( quote ){ 739 len += 2; 740 for(i=0; i<nAppend; i++){ 741 if( zAppend[i]==quote ) len++; 742 } 743 } 744 745 if( p->n+len>=p->nAlloc ){ 746 p->nAlloc = p->nAlloc*2 + len + 20; 747 p->z = realloc(p->z, p->nAlloc); 748 if( p->z==0 ) shell_out_of_memory(); 749 } 750 751 if( quote ){ 752 char *zCsr = p->z+p->n; 753 *zCsr++ = quote; 754 for(i=0; i<nAppend; i++){ 755 *zCsr++ = zAppend[i]; 756 if( zAppend[i]==quote ) *zCsr++ = quote; 757 } 758 *zCsr++ = quote; 759 p->n = (int)(zCsr - p->z); 760 *zCsr = '\0'; 761 }else{ 762 memcpy(p->z+p->n, zAppend, nAppend); 763 p->n += nAppend; 764 p->z[p->n] = '\0'; 765 } 766} 767 768/* 769** Attempt to determine if identifier zName needs to be quoted, either 770** because it contains non-alphanumeric characters, or because it is an 771** SQLite keyword. Be conservative in this estimate: When in doubt assume 772** that quoting is required. 773** 774** Return '"' if quoting is required. Return 0 if no quoting is required. 775*/ 776static char quoteChar(const char *zName){ 777 int i; 778 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; 779 for(i=0; zName[i]; i++){ 780 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; 781 } 782 return sqlite3_keyword_check(zName, i) ? '"' : 0; 783} 784 785/* 786** Construct a fake object name and column list to describe the structure 787** of the view, virtual table, or table valued function zSchema.zName. 788*/ 789static char *shellFakeSchema( 790 sqlite3 *db, /* The database connection containing the vtab */ 791 const char *zSchema, /* Schema of the database holding the vtab */ 792 const char *zName /* The name of the virtual table */ 793){ 794 sqlite3_stmt *pStmt = 0; 795 char *zSql; 796 ShellText s; 797 char cQuote; 798 char *zDiv = "("; 799 int nRow = 0; 800 801 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", 802 zSchema ? zSchema : "main", zName); 803 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 804 sqlite3_free(zSql); 805 initText(&s); 806 if( zSchema ){ 807 cQuote = quoteChar(zSchema); 808 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; 809 appendText(&s, zSchema, cQuote); 810 appendText(&s, ".", 0); 811 } 812 cQuote = quoteChar(zName); 813 appendText(&s, zName, cQuote); 814 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 815 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); 816 nRow++; 817 appendText(&s, zDiv, 0); 818 zDiv = ","; 819 cQuote = quoteChar(zCol); 820 appendText(&s, zCol, cQuote); 821 } 822 appendText(&s, ")", 0); 823 sqlite3_finalize(pStmt); 824 if( nRow==0 ){ 825 freeText(&s); 826 s.z = 0; 827 } 828 return s.z; 829} 830 831/* 832** SQL function: shell_module_schema(X) 833** 834** Return a fake schema for the table-valued function or eponymous virtual 835** table X. 836*/ 837static void shellModuleSchema( 838 sqlite3_context *pCtx, 839 int nVal, 840 sqlite3_value **apVal 841){ 842 const char *zName = (const char*)sqlite3_value_text(apVal[0]); 843 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName); 844 UNUSED_PARAMETER(nVal); 845 if( zFake ){ 846 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), 847 -1, sqlite3_free); 848 free(zFake); 849 } 850} 851 852/* 853** SQL function: shell_add_schema(S,X) 854** 855** Add the schema name X to the CREATE statement in S and return the result. 856** Examples: 857** 858** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); 859** 860** Also works on 861** 862** CREATE INDEX 863** CREATE UNIQUE INDEX 864** CREATE VIEW 865** CREATE TRIGGER 866** CREATE VIRTUAL TABLE 867** 868** This UDF is used by the .schema command to insert the schema name of 869** attached databases into the middle of the sqlite_master.sql field. 870*/ 871static void shellAddSchemaName( 872 sqlite3_context *pCtx, 873 int nVal, 874 sqlite3_value **apVal 875){ 876 static const char *aPrefix[] = { 877 "TABLE", 878 "INDEX", 879 "UNIQUE INDEX", 880 "VIEW", 881 "TRIGGER", 882 "VIRTUAL TABLE" 883 }; 884 int i = 0; 885 const char *zIn = (const char*)sqlite3_value_text(apVal[0]); 886 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); 887 const char *zName = (const char*)sqlite3_value_text(apVal[2]); 888 sqlite3 *db = sqlite3_context_db_handle(pCtx); 889 UNUSED_PARAMETER(nVal); 890 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ 891 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ 892 int n = strlen30(aPrefix[i]); 893 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ 894 char *z = 0; 895 char *zFake = 0; 896 if( zSchema ){ 897 char cQuote = quoteChar(zSchema); 898 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ 899 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); 900 }else{ 901 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); 902 } 903 } 904 if( zName 905 && aPrefix[i][0]=='V' 906 && (zFake = shellFakeSchema(db, zSchema, zName))!=0 907 ){ 908 if( z==0 ){ 909 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); 910 }else{ 911 z = sqlite3_mprintf("%z\n/* %s */", z, zFake); 912 } 913 free(zFake); 914 } 915 if( z ){ 916 sqlite3_result_text(pCtx, z, -1, sqlite3_free); 917 return; 918 } 919 } 920 } 921 } 922 sqlite3_result_value(pCtx, apVal[0]); 923} 924 925/* 926** The source code for several run-time loadable extensions is inserted 927** below by the ../tool/mkshellc.tcl script. Before processing that included 928** code, we need to override some macros to make the included program code 929** work here in the middle of this regular program. 930*/ 931#define SQLITE_EXTENSION_INIT1 932#define SQLITE_EXTENSION_INIT2(X) (void)(X) 933 934#if defined(_WIN32) && defined(_MSC_VER) 935INCLUDE test_windirent.h 936INCLUDE test_windirent.c 937#define dirent DIRENT 938#endif 939INCLUDE ../ext/misc/shathree.c 940INCLUDE ../ext/misc/fileio.c 941INCLUDE ../ext/misc/completion.c 942INCLUDE ../ext/misc/appendvfs.c 943INCLUDE ../ext/misc/memtrace.c 944#ifdef SQLITE_HAVE_ZLIB 945INCLUDE ../ext/misc/zipfile.c 946INCLUDE ../ext/misc/sqlar.c 947#endif 948INCLUDE ../ext/expert/sqlite3expert.h 949INCLUDE ../ext/expert/sqlite3expert.c 950 951#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 952INCLUDE ../ext/misc/dbdata.c 953#endif 954 955#if defined(SQLITE_ENABLE_SESSION) 956/* 957** State information for a single open session 958*/ 959typedef struct OpenSession OpenSession; 960struct OpenSession { 961 char *zName; /* Symbolic name for this session */ 962 int nFilter; /* Number of xFilter rejection GLOB patterns */ 963 char **azFilter; /* Array of xFilter rejection GLOB patterns */ 964 sqlite3_session *p; /* The open session */ 965}; 966#endif 967 968/* 969** Shell output mode information from before ".explain on", 970** saved so that it can be restored by ".explain off" 971*/ 972typedef struct SavedModeInfo SavedModeInfo; 973struct SavedModeInfo { 974 int valid; /* Is there legit data in here? */ 975 int mode; /* Mode prior to ".explain on" */ 976 int showHeader; /* The ".header" setting prior to ".explain on" */ 977 int colWidth[100]; /* Column widths prior to ".explain on" */ 978}; 979 980typedef struct ExpertInfo ExpertInfo; 981struct ExpertInfo { 982 sqlite3expert *pExpert; 983 int bVerbose; 984}; 985 986/* A single line in the EQP output */ 987typedef struct EQPGraphRow EQPGraphRow; 988struct EQPGraphRow { 989 int iEqpId; /* ID for this row */ 990 int iParentId; /* ID of the parent row */ 991 EQPGraphRow *pNext; /* Next row in sequence */ 992 char zText[1]; /* Text to display for this row */ 993}; 994 995/* All EQP output is collected into an instance of the following */ 996typedef struct EQPGraph EQPGraph; 997struct EQPGraph { 998 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ 999 EQPGraphRow *pLast; /* Last element of the pRow list */ 1000 char zPrefix[100]; /* Graph prefix */ 1001}; 1002 1003/* 1004** State information about the database connection is contained in an 1005** instance of the following structure. 1006*/ 1007typedef struct ShellState ShellState; 1008struct ShellState { 1009 sqlite3 *db; /* The database */ 1010 u8 autoExplain; /* Automatically turn on .explain mode */ 1011 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ 1012 u8 autoEQPtest; /* autoEQP is in test mode */ 1013 u8 autoEQPtrace; /* autoEQP is in trace mode */ 1014 u8 statsOn; /* True to display memory stats before each finalize */ 1015 u8 scanstatsOn; /* True to display scan stats before each finalize */ 1016 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ 1017 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ 1018 u8 nEqpLevel; /* Depth of the EQP output graph */ 1019 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ 1020 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ 1021 int outCount; /* Revert to stdout when reaching zero */ 1022 int cnt; /* Number of records displayed so far */ 1023 int lineno; /* Line number of last line read from in */ 1024 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ 1025 FILE *in; /* Read commands from this stream */ 1026 FILE *out; /* Write results here */ 1027 FILE *traceOut; /* Output for sqlite3_trace() */ 1028 int nErr; /* Number of errors seen */ 1029 int mode; /* An output mode setting */ 1030 int modePrior; /* Saved mode */ 1031 int cMode; /* temporary output mode for the current query */ 1032 int normalMode; /* Output mode before ".explain on" */ 1033 int writableSchema; /* True if PRAGMA writable_schema=ON */ 1034 int showHeader; /* True to show column names in List or Column mode */ 1035 int nCheck; /* Number of ".check" commands run */ 1036 unsigned nProgress; /* Number of progress callbacks encountered */ 1037 unsigned mxProgress; /* Maximum progress callbacks before failing */ 1038 unsigned flgProgress; /* Flags for the progress callback */ 1039 unsigned shellFlgs; /* Various flags */ 1040 sqlite3_int64 szMax; /* --maxsize argument to .open */ 1041 char *zDestTable; /* Name of destination table when MODE_Insert */ 1042 char *zTempFile; /* Temporary file that might need deleting */ 1043 char zTestcase[30]; /* Name of current test case */ 1044 char colSeparator[20]; /* Column separator character for several modes */ 1045 char rowSeparator[20]; /* Row separator character for MODE_Ascii */ 1046 char colSepPrior[20]; /* Saved column separator */ 1047 char rowSepPrior[20]; /* Saved row separator */ 1048 int colWidth[100]; /* Requested width of each column when in column mode*/ 1049 int actualWidth[100]; /* Actual width of each column */ 1050 char nullValue[20]; /* The text to print when a NULL comes back from 1051 ** the database */ 1052 char outfile[FILENAME_MAX]; /* Filename for *out */ 1053 const char *zDbFilename; /* name of the database file */ 1054 char *zFreeOnClose; /* Filename to free when closing */ 1055 const char *zVfs; /* Name of VFS to use */ 1056 sqlite3_stmt *pStmt; /* Current statement if any. */ 1057 FILE *pLog; /* Write log output here */ 1058 int *aiIndent; /* Array of indents used in MODE_Explain */ 1059 int nIndent; /* Size of array aiIndent[] */ 1060 int iIndent; /* Index of current op in aiIndent[] */ 1061 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ 1062#if defined(SQLITE_ENABLE_SESSION) 1063 int nSession; /* Number of active sessions */ 1064 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ 1065#endif 1066 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ 1067}; 1068 1069 1070/* Allowed values for ShellState.autoEQP 1071*/ 1072#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ 1073#define AUTOEQP_on 1 /* Automatic EQP is on */ 1074#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ 1075#define AUTOEQP_full 3 /* Show full EXPLAIN */ 1076 1077/* Allowed values for ShellState.openMode 1078*/ 1079#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ 1080#define SHELL_OPEN_NORMAL 1 /* Normal database file */ 1081#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ 1082#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ 1083#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ 1084#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ 1085#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ 1086 1087/* Allowed values for ShellState.eTraceType 1088*/ 1089#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ 1090#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ 1091#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ 1092 1093/* Bits in the ShellState.flgProgress variable */ 1094#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ 1095#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres 1096 ** callback limit is reached, and for each 1097 ** top-level SQL statement */ 1098#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ 1099 1100/* 1101** These are the allowed shellFlgs values 1102*/ 1103#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ 1104#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ 1105#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ 1106#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ 1107#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ 1108#define SHFLG_CountChanges 0x00000020 /* .changes setting */ 1109#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ 1110 1111/* 1112** Macros for testing and setting shellFlgs 1113*/ 1114#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) 1115#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) 1116#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) 1117 1118/* 1119** These are the allowed modes. 1120*/ 1121#define MODE_Line 0 /* One column per line. Blank line between records */ 1122#define MODE_Column 1 /* One record per line in neat columns */ 1123#define MODE_List 2 /* One record per line with a separator */ 1124#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ 1125#define MODE_Html 4 /* Generate an XHTML table */ 1126#define MODE_Insert 5 /* Generate SQL "insert" statements */ 1127#define MODE_Quote 6 /* Quote values as for SQL */ 1128#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ 1129#define MODE_Csv 8 /* Quote strings, numbers are plain */ 1130#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ 1131#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ 1132#define MODE_Pretty 11 /* Pretty-print schemas */ 1133#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ 1134 1135static const char *modeDescr[] = { 1136 "line", 1137 "column", 1138 "list", 1139 "semi", 1140 "html", 1141 "insert", 1142 "quote", 1143 "tcl", 1144 "csv", 1145 "explain", 1146 "ascii", 1147 "prettyprint", 1148 "eqp" 1149}; 1150 1151/* 1152** These are the column/row/line separators used by the various 1153** import/export modes. 1154*/ 1155#define SEP_Column "|" 1156#define SEP_Row "\n" 1157#define SEP_Tab "\t" 1158#define SEP_Space " " 1159#define SEP_Comma "," 1160#define SEP_CrLf "\r\n" 1161#define SEP_Unit "\x1F" 1162#define SEP_Record "\x1E" 1163 1164/* 1165** A callback for the sqlite3_log() interface. 1166*/ 1167static void shellLog(void *pArg, int iErrCode, const char *zMsg){ 1168 ShellState *p = (ShellState*)pArg; 1169 if( p->pLog==0 ) return; 1170 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); 1171 fflush(p->pLog); 1172} 1173 1174/* 1175** SQL function: shell_putsnl(X) 1176** 1177** Write the text X to the screen (or whatever output is being directed) 1178** adding a newline at the end, and then return X. 1179*/ 1180static void shellPutsFunc( 1181 sqlite3_context *pCtx, 1182 int nVal, 1183 sqlite3_value **apVal 1184){ 1185 ShellState *p = (ShellState*)sqlite3_user_data(pCtx); 1186 (void)nVal; 1187 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); 1188 sqlite3_result_value(pCtx, apVal[0]); 1189} 1190 1191/* 1192** SQL function: edit(VALUE) 1193** edit(VALUE,EDITOR) 1194** 1195** These steps: 1196** 1197** (1) Write VALUE into a temporary file. 1198** (2) Run program EDITOR on that temporary file. 1199** (3) Read the temporary file back and return its content as the result. 1200** (4) Delete the temporary file 1201** 1202** If the EDITOR argument is omitted, use the value in the VISUAL 1203** environment variable. If still there is no EDITOR, through an error. 1204** 1205** Also throw an error if the EDITOR program returns a non-zero exit code. 1206*/ 1207#ifndef SQLITE_NOHAVE_SYSTEM 1208static void editFunc( 1209 sqlite3_context *context, 1210 int argc, 1211 sqlite3_value **argv 1212){ 1213 const char *zEditor; 1214 char *zTempFile = 0; 1215 sqlite3 *db; 1216 char *zCmd = 0; 1217 int bBin; 1218 int rc; 1219 int hasCRNL = 0; 1220 FILE *f = 0; 1221 sqlite3_int64 sz; 1222 sqlite3_int64 x; 1223 unsigned char *p = 0; 1224 1225 if( argc==2 ){ 1226 zEditor = (const char*)sqlite3_value_text(argv[1]); 1227 }else{ 1228 zEditor = getenv("VISUAL"); 1229 } 1230 if( zEditor==0 ){ 1231 sqlite3_result_error(context, "no editor for edit()", -1); 1232 return; 1233 } 1234 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1235 sqlite3_result_error(context, "NULL input to edit()", -1); 1236 return; 1237 } 1238 db = sqlite3_context_db_handle(context); 1239 zTempFile = 0; 1240 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); 1241 if( zTempFile==0 ){ 1242 sqlite3_uint64 r = 0; 1243 sqlite3_randomness(sizeof(r), &r); 1244 zTempFile = sqlite3_mprintf("temp%llx", r); 1245 if( zTempFile==0 ){ 1246 sqlite3_result_error_nomem(context); 1247 return; 1248 } 1249 } 1250 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; 1251 /* When writing the file to be edited, do \n to \r\n conversions on systems 1252 ** that want \r\n line endings */ 1253 f = fopen(zTempFile, bBin ? "wb" : "w"); 1254 if( f==0 ){ 1255 sqlite3_result_error(context, "edit() cannot open temp file", -1); 1256 goto edit_func_end; 1257 } 1258 sz = sqlite3_value_bytes(argv[0]); 1259 if( bBin ){ 1260 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); 1261 }else{ 1262 const char *z = (const char*)sqlite3_value_text(argv[0]); 1263 /* Remember whether or not the value originally contained \r\n */ 1264 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; 1265 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); 1266 } 1267 fclose(f); 1268 f = 0; 1269 if( x!=sz ){ 1270 sqlite3_result_error(context, "edit() could not write the whole file", -1); 1271 goto edit_func_end; 1272 } 1273 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); 1274 if( zCmd==0 ){ 1275 sqlite3_result_error_nomem(context); 1276 goto edit_func_end; 1277 } 1278 rc = system(zCmd); 1279 sqlite3_free(zCmd); 1280 if( rc ){ 1281 sqlite3_result_error(context, "EDITOR returned non-zero", -1); 1282 goto edit_func_end; 1283 } 1284 f = fopen(zTempFile, "rb"); 1285 if( f==0 ){ 1286 sqlite3_result_error(context, 1287 "edit() cannot reopen temp file after edit", -1); 1288 goto edit_func_end; 1289 } 1290 fseek(f, 0, SEEK_END); 1291 sz = ftell(f); 1292 rewind(f); 1293 p = sqlite3_malloc64( sz+1 ); 1294 if( p==0 ){ 1295 sqlite3_result_error_nomem(context); 1296 goto edit_func_end; 1297 } 1298 x = fread(p, 1, (size_t)sz, f); 1299 fclose(f); 1300 f = 0; 1301 if( x!=sz ){ 1302 sqlite3_result_error(context, "could not read back the whole file", -1); 1303 goto edit_func_end; 1304 } 1305 if( bBin ){ 1306 sqlite3_result_blob64(context, p, sz, sqlite3_free); 1307 }else{ 1308 sqlite3_int64 i, j; 1309 if( hasCRNL ){ 1310 /* If the original contains \r\n then do no conversions back to \n */ 1311 j = sz; 1312 }else{ 1313 /* If the file did not originally contain \r\n then convert any new 1314 ** \r\n back into \n */ 1315 for(i=j=0; i<sz; i++){ 1316 if( p[i]=='\r' && p[i+1]=='\n' ) i++; 1317 p[j++] = p[i]; 1318 } 1319 sz = j; 1320 p[sz] = 0; 1321 } 1322 sqlite3_result_text64(context, (const char*)p, sz, 1323 sqlite3_free, SQLITE_UTF8); 1324 } 1325 p = 0; 1326 1327edit_func_end: 1328 if( f ) fclose(f); 1329 unlink(zTempFile); 1330 sqlite3_free(zTempFile); 1331 sqlite3_free(p); 1332} 1333#endif /* SQLITE_NOHAVE_SYSTEM */ 1334 1335/* 1336** Save or restore the current output mode 1337*/ 1338static void outputModePush(ShellState *p){ 1339 p->modePrior = p->mode; 1340 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); 1341 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); 1342} 1343static void outputModePop(ShellState *p){ 1344 p->mode = p->modePrior; 1345 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); 1346 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); 1347} 1348 1349/* 1350** Output the given string as a hex-encoded blob (eg. X'1234' ) 1351*/ 1352static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ 1353 int i; 1354 char *zBlob = (char *)pBlob; 1355 raw_printf(out,"X'"); 1356 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } 1357 raw_printf(out,"'"); 1358} 1359 1360/* 1361** Find a string that is not found anywhere in z[]. Return a pointer 1362** to that string. 1363** 1364** Try to use zA and zB first. If both of those are already found in z[] 1365** then make up some string and store it in the buffer zBuf. 1366*/ 1367static const char *unused_string( 1368 const char *z, /* Result must not appear anywhere in z */ 1369 const char *zA, const char *zB, /* Try these first */ 1370 char *zBuf /* Space to store a generated string */ 1371){ 1372 unsigned i = 0; 1373 if( strstr(z, zA)==0 ) return zA; 1374 if( strstr(z, zB)==0 ) return zB; 1375 do{ 1376 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); 1377 }while( strstr(z,zBuf)!=0 ); 1378 return zBuf; 1379} 1380 1381/* 1382** Output the given string as a quoted string using SQL quoting conventions. 1383** 1384** See also: output_quoted_escaped_string() 1385*/ 1386static void output_quoted_string(FILE *out, const char *z){ 1387 int i; 1388 char c; 1389 setBinaryMode(out, 1); 1390 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1391 if( c==0 ){ 1392 utf8_printf(out,"'%s'",z); 1393 }else{ 1394 raw_printf(out, "'"); 1395 while( *z ){ 1396 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1397 if( c=='\'' ) i++; 1398 if( i ){ 1399 utf8_printf(out, "%.*s", i, z); 1400 z += i; 1401 } 1402 if( c=='\'' ){ 1403 raw_printf(out, "'"); 1404 continue; 1405 } 1406 if( c==0 ){ 1407 break; 1408 } 1409 z++; 1410 } 1411 raw_printf(out, "'"); 1412 } 1413 setTextMode(out, 1); 1414} 1415 1416/* 1417** Output the given string as a quoted string using SQL quoting conventions. 1418** Additionallly , escape the "\n" and "\r" characters so that they do not 1419** get corrupted by end-of-line translation facilities in some operating 1420** systems. 1421** 1422** This is like output_quoted_string() but with the addition of the \r\n 1423** escape mechanism. 1424*/ 1425static void output_quoted_escaped_string(FILE *out, const char *z){ 1426 int i; 1427 char c; 1428 setBinaryMode(out, 1); 1429 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} 1430 if( c==0 ){ 1431 utf8_printf(out,"'%s'",z); 1432 }else{ 1433 const char *zNL = 0; 1434 const char *zCR = 0; 1435 int nNL = 0; 1436 int nCR = 0; 1437 char zBuf1[20], zBuf2[20]; 1438 for(i=0; z[i]; i++){ 1439 if( z[i]=='\n' ) nNL++; 1440 if( z[i]=='\r' ) nCR++; 1441 } 1442 if( nNL ){ 1443 raw_printf(out, "replace("); 1444 zNL = unused_string(z, "\\n", "\\012", zBuf1); 1445 } 1446 if( nCR ){ 1447 raw_printf(out, "replace("); 1448 zCR = unused_string(z, "\\r", "\\015", zBuf2); 1449 } 1450 raw_printf(out, "'"); 1451 while( *z ){ 1452 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} 1453 if( c=='\'' ) i++; 1454 if( i ){ 1455 utf8_printf(out, "%.*s", i, z); 1456 z += i; 1457 } 1458 if( c=='\'' ){ 1459 raw_printf(out, "'"); 1460 continue; 1461 } 1462 if( c==0 ){ 1463 break; 1464 } 1465 z++; 1466 if( c=='\n' ){ 1467 raw_printf(out, "%s", zNL); 1468 continue; 1469 } 1470 raw_printf(out, "%s", zCR); 1471 } 1472 raw_printf(out, "'"); 1473 if( nCR ){ 1474 raw_printf(out, ",'%s',char(13))", zCR); 1475 } 1476 if( nNL ){ 1477 raw_printf(out, ",'%s',char(10))", zNL); 1478 } 1479 } 1480 setTextMode(out, 1); 1481} 1482 1483/* 1484** Output the given string as a quoted according to C or TCL quoting rules. 1485*/ 1486static void output_c_string(FILE *out, const char *z){ 1487 unsigned int c; 1488 fputc('"', out); 1489 while( (c = *(z++))!=0 ){ 1490 if( c=='\\' ){ 1491 fputc(c, out); 1492 fputc(c, out); 1493 }else if( c=='"' ){ 1494 fputc('\\', out); 1495 fputc('"', out); 1496 }else if( c=='\t' ){ 1497 fputc('\\', out); 1498 fputc('t', out); 1499 }else if( c=='\n' ){ 1500 fputc('\\', out); 1501 fputc('n', out); 1502 }else if( c=='\r' ){ 1503 fputc('\\', out); 1504 fputc('r', out); 1505 }else if( !isprint(c&0xff) ){ 1506 raw_printf(out, "\\%03o", c&0xff); 1507 }else{ 1508 fputc(c, out); 1509 } 1510 } 1511 fputc('"', out); 1512} 1513 1514/* 1515** Output the given string with characters that are special to 1516** HTML escaped. 1517*/ 1518static void output_html_string(FILE *out, const char *z){ 1519 int i; 1520 if( z==0 ) z = ""; 1521 while( *z ){ 1522 for(i=0; z[i] 1523 && z[i]!='<' 1524 && z[i]!='&' 1525 && z[i]!='>' 1526 && z[i]!='\"' 1527 && z[i]!='\''; 1528 i++){} 1529 if( i>0 ){ 1530 utf8_printf(out,"%.*s",i,z); 1531 } 1532 if( z[i]=='<' ){ 1533 raw_printf(out,"<"); 1534 }else if( z[i]=='&' ){ 1535 raw_printf(out,"&"); 1536 }else if( z[i]=='>' ){ 1537 raw_printf(out,">"); 1538 }else if( z[i]=='\"' ){ 1539 raw_printf(out,"""); 1540 }else if( z[i]=='\'' ){ 1541 raw_printf(out,"'"); 1542 }else{ 1543 break; 1544 } 1545 z += i + 1; 1546 } 1547} 1548 1549/* 1550** If a field contains any character identified by a 1 in the following 1551** array, then the string must be quoted for CSV. 1552*/ 1553static const char needCsvQuote[] = { 1554 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1555 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1556 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1557 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1558 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1561 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1562 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1563 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1564 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1565 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1566 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1570}; 1571 1572/* 1573** Output a single term of CSV. Actually, p->colSeparator is used for 1574** the separator, which may or may not be a comma. p->nullValue is 1575** the null value. Strings are quoted if necessary. The separator 1576** is only issued if bSep is true. 1577*/ 1578static void output_csv(ShellState *p, const char *z, int bSep){ 1579 FILE *out = p->out; 1580 if( z==0 ){ 1581 utf8_printf(out,"%s",p->nullValue); 1582 }else{ 1583 int i; 1584 int nSep = strlen30(p->colSeparator); 1585 for(i=0; z[i]; i++){ 1586 if( needCsvQuote[((unsigned char*)z)[i]] 1587 || (z[i]==p->colSeparator[0] && 1588 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ 1589 i = 0; 1590 break; 1591 } 1592 } 1593 if( i==0 ){ 1594 char *zQuoted = sqlite3_mprintf("\"%w\"", z); 1595 utf8_printf(out, "%s", zQuoted); 1596 sqlite3_free(zQuoted); 1597 }else{ 1598 utf8_printf(out, "%s", z); 1599 } 1600 } 1601 if( bSep ){ 1602 utf8_printf(p->out, "%s", p->colSeparator); 1603 } 1604} 1605 1606/* 1607** This routine runs when the user presses Ctrl-C 1608*/ 1609static void interrupt_handler(int NotUsed){ 1610 UNUSED_PARAMETER(NotUsed); 1611 seenInterrupt++; 1612 if( seenInterrupt>2 ) exit(1); 1613 if( globalDb ) sqlite3_interrupt(globalDb); 1614} 1615 1616#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 1617/* 1618** This routine runs for console events (e.g. Ctrl-C) on Win32 1619*/ 1620static BOOL WINAPI ConsoleCtrlHandler( 1621 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ 1622){ 1623 if( dwCtrlType==CTRL_C_EVENT ){ 1624 interrupt_handler(0); 1625 return TRUE; 1626 } 1627 return FALSE; 1628} 1629#endif 1630 1631#ifndef SQLITE_OMIT_AUTHORIZATION 1632/* 1633** When the ".auth ON" is set, the following authorizer callback is 1634** invoked. It always returns SQLITE_OK. 1635*/ 1636static int shellAuth( 1637 void *pClientData, 1638 int op, 1639 const char *zA1, 1640 const char *zA2, 1641 const char *zA3, 1642 const char *zA4 1643){ 1644 ShellState *p = (ShellState*)pClientData; 1645 static const char *azAction[] = { 0, 1646 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", 1647 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", 1648 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", 1649 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", 1650 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", 1651 "DROP_TRIGGER", "DROP_VIEW", "INSERT", 1652 "PRAGMA", "READ", "SELECT", 1653 "TRANSACTION", "UPDATE", "ATTACH", 1654 "DETACH", "ALTER_TABLE", "REINDEX", 1655 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", 1656 "FUNCTION", "SAVEPOINT", "RECURSIVE" 1657 }; 1658 int i; 1659 const char *az[4]; 1660 az[0] = zA1; 1661 az[1] = zA2; 1662 az[2] = zA3; 1663 az[3] = zA4; 1664 utf8_printf(p->out, "authorizer: %s", azAction[op]); 1665 for(i=0; i<4; i++){ 1666 raw_printf(p->out, " "); 1667 if( az[i] ){ 1668 output_c_string(p->out, az[i]); 1669 }else{ 1670 raw_printf(p->out, "NULL"); 1671 } 1672 } 1673 raw_printf(p->out, "\n"); 1674 return SQLITE_OK; 1675} 1676#endif 1677 1678/* 1679** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. 1680** 1681** This routine converts some CREATE TABLE statements for shadow tables 1682** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. 1683*/ 1684static void printSchemaLine(FILE *out, const char *z, const char *zTail){ 1685 if( z==0 ) return; 1686 if( zTail==0 ) return; 1687 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ 1688 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); 1689 }else{ 1690 utf8_printf(out, "%s%s", z, zTail); 1691 } 1692} 1693static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ 1694 char c = z[n]; 1695 z[n] = 0; 1696 printSchemaLine(out, z, zTail); 1697 z[n] = c; 1698} 1699 1700/* 1701** Return true if string z[] has nothing but whitespace and comments to the 1702** end of the first line. 1703*/ 1704static int wsToEol(const char *z){ 1705 int i; 1706 for(i=0; z[i]; i++){ 1707 if( z[i]=='\n' ) return 1; 1708 if( IsSpace(z[i]) ) continue; 1709 if( z[i]=='-' && z[i+1]=='-' ) return 1; 1710 return 0; 1711 } 1712 return 1; 1713} 1714 1715/* 1716** Add a new entry to the EXPLAIN QUERY PLAN data 1717*/ 1718static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ 1719 EQPGraphRow *pNew; 1720 int nText = strlen30(zText); 1721 if( p->autoEQPtest ){ 1722 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); 1723 } 1724 pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); 1725 if( pNew==0 ) shell_out_of_memory(); 1726 pNew->iEqpId = iEqpId; 1727 pNew->iParentId = p2; 1728 memcpy(pNew->zText, zText, nText+1); 1729 pNew->pNext = 0; 1730 if( p->sGraph.pLast ){ 1731 p->sGraph.pLast->pNext = pNew; 1732 }else{ 1733 p->sGraph.pRow = pNew; 1734 } 1735 p->sGraph.pLast = pNew; 1736} 1737 1738/* 1739** Free and reset the EXPLAIN QUERY PLAN data that has been collected 1740** in p->sGraph. 1741*/ 1742static void eqp_reset(ShellState *p){ 1743 EQPGraphRow *pRow, *pNext; 1744 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ 1745 pNext = pRow->pNext; 1746 sqlite3_free(pRow); 1747 } 1748 memset(&p->sGraph, 0, sizeof(p->sGraph)); 1749} 1750 1751/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after 1752** pOld, or return the first such line if pOld is NULL 1753*/ 1754static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ 1755 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; 1756 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; 1757 return pRow; 1758} 1759 1760/* Render a single level of the graph that has iEqpId as its parent. Called 1761** recursively to render sublevels. 1762*/ 1763static void eqp_render_level(ShellState *p, int iEqpId){ 1764 EQPGraphRow *pRow, *pNext; 1765 int n = strlen30(p->sGraph.zPrefix); 1766 char *z; 1767 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ 1768 pNext = eqp_next_row(p, iEqpId, pRow); 1769 z = pRow->zText; 1770 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, 1771 pNext ? "|--" : "`--", z); 1772 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ 1773 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); 1774 eqp_render_level(p, pRow->iEqpId); 1775 p->sGraph.zPrefix[n] = 0; 1776 } 1777 } 1778} 1779 1780/* 1781** Display and reset the EXPLAIN QUERY PLAN data 1782*/ 1783static void eqp_render(ShellState *p){ 1784 EQPGraphRow *pRow = p->sGraph.pRow; 1785 if( pRow ){ 1786 if( pRow->zText[0]=='-' ){ 1787 if( pRow->pNext==0 ){ 1788 eqp_reset(p); 1789 return; 1790 } 1791 utf8_printf(p->out, "%s\n", pRow->zText+3); 1792 p->sGraph.pRow = pRow->pNext; 1793 sqlite3_free(pRow); 1794 }else{ 1795 utf8_printf(p->out, "QUERY PLAN\n"); 1796 } 1797 p->sGraph.zPrefix[0] = 0; 1798 eqp_render_level(p, 0); 1799 eqp_reset(p); 1800 } 1801} 1802 1803#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1804/* 1805** Progress handler callback. 1806*/ 1807static int progress_handler(void *pClientData) { 1808 ShellState *p = (ShellState*)pClientData; 1809 p->nProgress++; 1810 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ 1811 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); 1812 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 1813 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; 1814 return 1; 1815 } 1816 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ 1817 raw_printf(p->out, "Progress %u\n", p->nProgress); 1818 } 1819 return 0; 1820} 1821#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 1822 1823/* 1824** This is the callback routine that the shell 1825** invokes for each row of a query result. 1826*/ 1827static int shell_callback( 1828 void *pArg, 1829 int nArg, /* Number of result columns */ 1830 char **azArg, /* Text of each result column */ 1831 char **azCol, /* Column names */ 1832 int *aiType /* Column types */ 1833){ 1834 int i; 1835 ShellState *p = (ShellState*)pArg; 1836 1837 if( azArg==0 ) return 0; 1838 switch( p->cMode ){ 1839 case MODE_Line: { 1840 int w = 5; 1841 if( azArg==0 ) break; 1842 for(i=0; i<nArg; i++){ 1843 int len = strlen30(azCol[i] ? azCol[i] : ""); 1844 if( len>w ) w = len; 1845 } 1846 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); 1847 for(i=0; i<nArg; i++){ 1848 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], 1849 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); 1850 } 1851 break; 1852 } 1853 case MODE_Explain: 1854 case MODE_Column: { 1855 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; 1856 const int *colWidth; 1857 int showHdr; 1858 char *rowSep; 1859 int nWidth; 1860 if( p->cMode==MODE_Column ){ 1861 colWidth = p->colWidth; 1862 nWidth = ArraySize(p->colWidth); 1863 showHdr = p->showHeader; 1864 rowSep = p->rowSeparator; 1865 }else{ 1866 colWidth = aExplainWidths; 1867 nWidth = ArraySize(aExplainWidths); 1868 showHdr = 1; 1869 rowSep = SEP_Row; 1870 } 1871 if( p->cnt++==0 ){ 1872 for(i=0; i<nArg; i++){ 1873 int w, n; 1874 if( i<nWidth ){ 1875 w = colWidth[i]; 1876 }else{ 1877 w = 0; 1878 } 1879 if( w==0 ){ 1880 w = strlenChar(azCol[i] ? azCol[i] : ""); 1881 if( w<10 ) w = 10; 1882 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); 1883 if( w<n ) w = n; 1884 } 1885 if( i<ArraySize(p->actualWidth) ){ 1886 p->actualWidth[i] = w; 1887 } 1888 if( showHdr ){ 1889 utf8_width_print(p->out, w, azCol[i]); 1890 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); 1891 } 1892 } 1893 if( showHdr ){ 1894 for(i=0; i<nArg; i++){ 1895 int w; 1896 if( i<ArraySize(p->actualWidth) ){ 1897 w = p->actualWidth[i]; 1898 if( w<0 ) w = -w; 1899 }else{ 1900 w = 10; 1901 } 1902 utf8_printf(p->out,"%-*.*s%s",w,w, 1903 "----------------------------------------------------------" 1904 "----------------------------------------------------------", 1905 i==nArg-1 ? rowSep : " "); 1906 } 1907 } 1908 } 1909 if( azArg==0 ) break; 1910 for(i=0; i<nArg; i++){ 1911 int w; 1912 if( i<ArraySize(p->actualWidth) ){ 1913 w = p->actualWidth[i]; 1914 }else{ 1915 w = 10; 1916 } 1917 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ 1918 w = strlenChar(azArg[i]); 1919 } 1920 if( i==1 && p->aiIndent && p->pStmt ){ 1921 if( p->iIndent<p->nIndent ){ 1922 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); 1923 } 1924 p->iIndent++; 1925 } 1926 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); 1927 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); 1928 } 1929 break; 1930 } 1931 case MODE_Semi: { /* .schema and .fullschema output */ 1932 printSchemaLine(p->out, azArg[0], ";\n"); 1933 break; 1934 } 1935 case MODE_Pretty: { /* .schema and .fullschema with --indent */ 1936 char *z; 1937 int j; 1938 int nParen = 0; 1939 char cEnd = 0; 1940 char c; 1941 int nLine = 0; 1942 assert( nArg==1 ); 1943 if( azArg[0]==0 ) break; 1944 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 1945 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 1946 ){ 1947 utf8_printf(p->out, "%s;\n", azArg[0]); 1948 break; 1949 } 1950 z = sqlite3_mprintf("%s", azArg[0]); 1951 j = 0; 1952 for(i=0; IsSpace(z[i]); i++){} 1953 for(; (c = z[i])!=0; i++){ 1954 if( IsSpace(c) ){ 1955 if( z[j-1]=='\r' ) z[j-1] = '\n'; 1956 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; 1957 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ 1958 j--; 1959 } 1960 z[j++] = c; 1961 } 1962 while( j>0 && IsSpace(z[j-1]) ){ j--; } 1963 z[j] = 0; 1964 if( strlen30(z)>=79 ){ 1965 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ 1966 if( c==cEnd ){ 1967 cEnd = 0; 1968 }else if( c=='"' || c=='\'' || c=='`' ){ 1969 cEnd = c; 1970 }else if( c=='[' ){ 1971 cEnd = ']'; 1972 }else if( c=='-' && z[i+1]=='-' ){ 1973 cEnd = '\n'; 1974 }else if( c=='(' ){ 1975 nParen++; 1976 }else if( c==')' ){ 1977 nParen--; 1978 if( nLine>0 && nParen==0 && j>0 ){ 1979 printSchemaLineN(p->out, z, j, "\n"); 1980 j = 0; 1981 } 1982 } 1983 z[j++] = c; 1984 if( nParen==1 && cEnd==0 1985 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) 1986 ){ 1987 if( c=='\n' ) j--; 1988 printSchemaLineN(p->out, z, j, "\n "); 1989 j = 0; 1990 nLine++; 1991 while( IsSpace(z[i+1]) ){ i++; } 1992 } 1993 } 1994 z[j] = 0; 1995 } 1996 printSchemaLine(p->out, z, ";\n"); 1997 sqlite3_free(z); 1998 break; 1999 } 2000 case MODE_List: { 2001 if( p->cnt++==0 && p->showHeader ){ 2002 for(i=0; i<nArg; i++){ 2003 utf8_printf(p->out,"%s%s",azCol[i], 2004 i==nArg-1 ? p->rowSeparator : p->colSeparator); 2005 } 2006 } 2007 if( azArg==0 ) break; 2008 for(i=0; i<nArg; i++){ 2009 char *z = azArg[i]; 2010 if( z==0 ) z = p->nullValue; 2011 utf8_printf(p->out, "%s", z); 2012 if( i<nArg-1 ){ 2013 utf8_printf(p->out, "%s", p->colSeparator); 2014 }else{ 2015 utf8_printf(p->out, "%s", p->rowSeparator); 2016 } 2017 } 2018 break; 2019 } 2020 case MODE_Html: { 2021 if( p->cnt++==0 && p->showHeader ){ 2022 raw_printf(p->out,"<TR>"); 2023 for(i=0; i<nArg; i++){ 2024 raw_printf(p->out,"<TH>"); 2025 output_html_string(p->out, azCol[i]); 2026 raw_printf(p->out,"</TH>\n"); 2027 } 2028 raw_printf(p->out,"</TR>\n"); 2029 } 2030 if( azArg==0 ) break; 2031 raw_printf(p->out,"<TR>"); 2032 for(i=0; i<nArg; i++){ 2033 raw_printf(p->out,"<TD>"); 2034 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2035 raw_printf(p->out,"</TD>\n"); 2036 } 2037 raw_printf(p->out,"</TR>\n"); 2038 break; 2039 } 2040 case MODE_Tcl: { 2041 if( p->cnt++==0 && p->showHeader ){ 2042 for(i=0; i<nArg; i++){ 2043 output_c_string(p->out,azCol[i] ? azCol[i] : ""); 2044 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2045 } 2046 utf8_printf(p->out, "%s", p->rowSeparator); 2047 } 2048 if( azArg==0 ) break; 2049 for(i=0; i<nArg; i++){ 2050 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2051 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2052 } 2053 utf8_printf(p->out, "%s", p->rowSeparator); 2054 break; 2055 } 2056 case MODE_Csv: { 2057 setBinaryMode(p->out, 1); 2058 if( p->cnt++==0 && p->showHeader ){ 2059 for(i=0; i<nArg; i++){ 2060 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); 2061 } 2062 utf8_printf(p->out, "%s", p->rowSeparator); 2063 } 2064 if( nArg>0 ){ 2065 for(i=0; i<nArg; i++){ 2066 output_csv(p, azArg[i], i<nArg-1); 2067 } 2068 utf8_printf(p->out, "%s", p->rowSeparator); 2069 } 2070 setTextMode(p->out, 1); 2071 break; 2072 } 2073 case MODE_Insert: { 2074 if( azArg==0 ) break; 2075 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); 2076 if( p->showHeader ){ 2077 raw_printf(p->out,"("); 2078 for(i=0; i<nArg; i++){ 2079 if( i>0 ) raw_printf(p->out, ","); 2080 if( quoteChar(azCol[i]) ){ 2081 char *z = sqlite3_mprintf("\"%w\"", azCol[i]); 2082 utf8_printf(p->out, "%s", z); 2083 sqlite3_free(z); 2084 }else{ 2085 raw_printf(p->out, "%s", azCol[i]); 2086 } 2087 } 2088 raw_printf(p->out,")"); 2089 } 2090 p->cnt++; 2091 for(i=0; i<nArg; i++){ 2092 raw_printf(p->out, i>0 ? "," : " VALUES("); 2093 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2094 utf8_printf(p->out,"NULL"); 2095 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2096 if( ShellHasFlag(p, SHFLG_Newlines) ){ 2097 output_quoted_string(p->out, azArg[i]); 2098 }else{ 2099 output_quoted_escaped_string(p->out, azArg[i]); 2100 } 2101 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2102 utf8_printf(p->out,"%s", azArg[i]); 2103 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2104 char z[50]; 2105 double r = sqlite3_column_double(p->pStmt, i); 2106 sqlite3_uint64 ur; 2107 memcpy(&ur,&r,sizeof(r)); 2108 if( ur==0x7ff0000000000000LL ){ 2109 raw_printf(p->out, "1e999"); 2110 }else if( ur==0xfff0000000000000LL ){ 2111 raw_printf(p->out, "-1e999"); 2112 }else{ 2113 sqlite3_snprintf(50,z,"%!.20g", r); 2114 raw_printf(p->out, "%s", z); 2115 } 2116 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2117 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2118 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2119 output_hex_blob(p->out, pBlob, nBlob); 2120 }else if( isNumber(azArg[i], 0) ){ 2121 utf8_printf(p->out,"%s", azArg[i]); 2122 }else if( ShellHasFlag(p, SHFLG_Newlines) ){ 2123 output_quoted_string(p->out, azArg[i]); 2124 }else{ 2125 output_quoted_escaped_string(p->out, azArg[i]); 2126 } 2127 } 2128 raw_printf(p->out,");\n"); 2129 break; 2130 } 2131 case MODE_Quote: { 2132 if( azArg==0 ) break; 2133 if( p->cnt==0 && p->showHeader ){ 2134 for(i=0; i<nArg; i++){ 2135 if( i>0 ) raw_printf(p->out, ","); 2136 output_quoted_string(p->out, azCol[i]); 2137 } 2138 raw_printf(p->out,"\n"); 2139 } 2140 p->cnt++; 2141 for(i=0; i<nArg; i++){ 2142 if( i>0 ) raw_printf(p->out, ","); 2143 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2144 utf8_printf(p->out,"NULL"); 2145 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2146 output_quoted_string(p->out, azArg[i]); 2147 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2148 utf8_printf(p->out,"%s", azArg[i]); 2149 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2150 char z[50]; 2151 double r = sqlite3_column_double(p->pStmt, i); 2152 sqlite3_snprintf(50,z,"%!.20g", r); 2153 raw_printf(p->out, "%s", z); 2154 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2155 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2156 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2157 output_hex_blob(p->out, pBlob, nBlob); 2158 }else if( isNumber(azArg[i], 0) ){ 2159 utf8_printf(p->out,"%s", azArg[i]); 2160 }else{ 2161 output_quoted_string(p->out, azArg[i]); 2162 } 2163 } 2164 raw_printf(p->out,"\n"); 2165 break; 2166 } 2167 case MODE_Ascii: { 2168 if( p->cnt++==0 && p->showHeader ){ 2169 for(i=0; i<nArg; i++){ 2170 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2171 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); 2172 } 2173 utf8_printf(p->out, "%s", p->rowSeparator); 2174 } 2175 if( azArg==0 ) break; 2176 for(i=0; i<nArg; i++){ 2177 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2178 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); 2179 } 2180 utf8_printf(p->out, "%s", p->rowSeparator); 2181 break; 2182 } 2183 case MODE_EQP: { 2184 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); 2185 break; 2186 } 2187 } 2188 return 0; 2189} 2190 2191/* 2192** This is the callback routine that the SQLite library 2193** invokes for each row of a query result. 2194*/ 2195static int callback(void *pArg, int nArg, char **azArg, char **azCol){ 2196 /* since we don't have type info, call the shell_callback with a NULL value */ 2197 return shell_callback(pArg, nArg, azArg, azCol, NULL); 2198} 2199 2200/* 2201** This is the callback routine from sqlite3_exec() that appends all 2202** output onto the end of a ShellText object. 2203*/ 2204static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ 2205 ShellText *p = (ShellText*)pArg; 2206 int i; 2207 UNUSED_PARAMETER(az); 2208 if( azArg==0 ) return 0; 2209 if( p->n ) appendText(p, "|", 0); 2210 for(i=0; i<nArg; i++){ 2211 if( i ) appendText(p, ",", 0); 2212 if( azArg[i] ) appendText(p, azArg[i], 0); 2213 } 2214 return 0; 2215} 2216 2217/* 2218** Generate an appropriate SELFTEST table in the main database. 2219*/ 2220static void createSelftestTable(ShellState *p){ 2221 char *zErrMsg = 0; 2222 sqlite3_exec(p->db, 2223 "SAVEPOINT selftest_init;\n" 2224 "CREATE TABLE IF NOT EXISTS selftest(\n" 2225 " tno INTEGER PRIMARY KEY,\n" /* Test number */ 2226 " op TEXT,\n" /* Operator: memo run */ 2227 " cmd TEXT,\n" /* Command text */ 2228 " ans TEXT\n" /* Desired answer */ 2229 ");" 2230 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" 2231 "INSERT INTO [_shell$self](rowid,op,cmd)\n" 2232 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" 2233 " 'memo','Tests generated by --init');\n" 2234 "INSERT INTO [_shell$self]\n" 2235 " SELECT 'run',\n" 2236 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " 2237 "FROM sqlite_master ORDER BY 2'',224))',\n" 2238 " hex(sha3_query('SELECT type,name,tbl_name,sql " 2239 "FROM sqlite_master ORDER BY 2',224));\n" 2240 "INSERT INTO [_shell$self]\n" 2241 " SELECT 'run'," 2242 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" 2243 " printf('%w',name) || '\" NOT INDEXED'',224))',\n" 2244 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" 2245 " FROM (\n" 2246 " SELECT name FROM sqlite_master\n" 2247 " WHERE type='table'\n" 2248 " AND name<>'selftest'\n" 2249 " AND coalesce(rootpage,0)>0\n" 2250 " )\n" 2251 " ORDER BY name;\n" 2252 "INSERT INTO [_shell$self]\n" 2253 " VALUES('run','PRAGMA integrity_check','ok');\n" 2254 "INSERT INTO selftest(tno,op,cmd,ans)" 2255 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" 2256 "DROP TABLE [_shell$self];" 2257 ,0,0,&zErrMsg); 2258 if( zErrMsg ){ 2259 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); 2260 sqlite3_free(zErrMsg); 2261 } 2262 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); 2263} 2264 2265 2266/* 2267** Set the destination table field of the ShellState structure to 2268** the name of the table given. Escape any quote characters in the 2269** table name. 2270*/ 2271static void set_table_name(ShellState *p, const char *zName){ 2272 int i, n; 2273 char cQuote; 2274 char *z; 2275 2276 if( p->zDestTable ){ 2277 free(p->zDestTable); 2278 p->zDestTable = 0; 2279 } 2280 if( zName==0 ) return; 2281 cQuote = quoteChar(zName); 2282 n = strlen30(zName); 2283 if( cQuote ) n += n+2; 2284 z = p->zDestTable = malloc( n+1 ); 2285 if( z==0 ) shell_out_of_memory(); 2286 n = 0; 2287 if( cQuote ) z[n++] = cQuote; 2288 for(i=0; zName[i]; i++){ 2289 z[n++] = zName[i]; 2290 if( zName[i]==cQuote ) z[n++] = cQuote; 2291 } 2292 if( cQuote ) z[n++] = cQuote; 2293 z[n] = 0; 2294} 2295 2296 2297/* 2298** Execute a query statement that will generate SQL output. Print 2299** the result columns, comma-separated, on a line and then add a 2300** semicolon terminator to the end of that line. 2301** 2302** If the number of columns is 1 and that column contains text "--" 2303** then write the semicolon on a separate line. That way, if a 2304** "--" comment occurs at the end of the statement, the comment 2305** won't consume the semicolon terminator. 2306*/ 2307static int run_table_dump_query( 2308 ShellState *p, /* Query context */ 2309 const char *zSelect, /* SELECT statement to extract content */ 2310 const char *zFirstRow /* Print before first row, if not NULL */ 2311){ 2312 sqlite3_stmt *pSelect; 2313 int rc; 2314 int nResult; 2315 int i; 2316 const char *z; 2317 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); 2318 if( rc!=SQLITE_OK || !pSelect ){ 2319 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2320 sqlite3_errmsg(p->db)); 2321 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2322 return rc; 2323 } 2324 rc = sqlite3_step(pSelect); 2325 nResult = sqlite3_column_count(pSelect); 2326 while( rc==SQLITE_ROW ){ 2327 if( zFirstRow ){ 2328 utf8_printf(p->out, "%s", zFirstRow); 2329 zFirstRow = 0; 2330 } 2331 z = (const char*)sqlite3_column_text(pSelect, 0); 2332 utf8_printf(p->out, "%s", z); 2333 for(i=1; i<nResult; i++){ 2334 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); 2335 } 2336 if( z==0 ) z = ""; 2337 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; 2338 if( z[0] ){ 2339 raw_printf(p->out, "\n;\n"); 2340 }else{ 2341 raw_printf(p->out, ";\n"); 2342 } 2343 rc = sqlite3_step(pSelect); 2344 } 2345 rc = sqlite3_finalize(pSelect); 2346 if( rc!=SQLITE_OK ){ 2347 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2348 sqlite3_errmsg(p->db)); 2349 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2350 } 2351 return rc; 2352} 2353 2354/* 2355** Allocate space and save off current error string. 2356*/ 2357static char *save_err_msg( 2358 sqlite3 *db /* Database to query */ 2359){ 2360 int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); 2361 char *zErrMsg = sqlite3_malloc64(nErrMsg); 2362 if( zErrMsg ){ 2363 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); 2364 } 2365 return zErrMsg; 2366} 2367 2368#ifdef __linux__ 2369/* 2370** Attempt to display I/O stats on Linux using /proc/PID/io 2371*/ 2372static void displayLinuxIoStats(FILE *out){ 2373 FILE *in; 2374 char z[200]; 2375 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 2376 in = fopen(z, "rb"); 2377 if( in==0 ) return; 2378 while( fgets(z, sizeof(z), in)!=0 ){ 2379 static const struct { 2380 const char *zPattern; 2381 const char *zDesc; 2382 } aTrans[] = { 2383 { "rchar: ", "Bytes received by read():" }, 2384 { "wchar: ", "Bytes sent to write():" }, 2385 { "syscr: ", "Read() system calls:" }, 2386 { "syscw: ", "Write() system calls:" }, 2387 { "read_bytes: ", "Bytes read from storage:" }, 2388 { "write_bytes: ", "Bytes written to storage:" }, 2389 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 2390 }; 2391 int i; 2392 for(i=0; i<ArraySize(aTrans); i++){ 2393 int n = strlen30(aTrans[i].zPattern); 2394 if( strncmp(aTrans[i].zPattern, z, n)==0 ){ 2395 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); 2396 break; 2397 } 2398 } 2399 } 2400 fclose(in); 2401} 2402#endif 2403 2404/* 2405** Display a single line of status using 64-bit values. 2406*/ 2407static void displayStatLine( 2408 ShellState *p, /* The shell context */ 2409 char *zLabel, /* Label for this one line */ 2410 char *zFormat, /* Format for the result */ 2411 int iStatusCtrl, /* Which status to display */ 2412 int bReset /* True to reset the stats */ 2413){ 2414 sqlite3_int64 iCur = -1; 2415 sqlite3_int64 iHiwtr = -1; 2416 int i, nPercent; 2417 char zLine[200]; 2418 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); 2419 for(i=0, nPercent=0; zFormat[i]; i++){ 2420 if( zFormat[i]=='%' ) nPercent++; 2421 } 2422 if( nPercent>1 ){ 2423 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); 2424 }else{ 2425 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); 2426 } 2427 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); 2428} 2429 2430/* 2431** Display memory stats. 2432*/ 2433static int display_stats( 2434 sqlite3 *db, /* Database to query */ 2435 ShellState *pArg, /* Pointer to ShellState */ 2436 int bReset /* True to reset the stats */ 2437){ 2438 int iCur; 2439 int iHiwtr; 2440 FILE *out; 2441 if( pArg==0 || pArg->out==0 ) return 0; 2442 out = pArg->out; 2443 2444 if( pArg->pStmt && (pArg->statsOn & 2) ){ 2445 int nCol, i, x; 2446 sqlite3_stmt *pStmt = pArg->pStmt; 2447 char z[100]; 2448 nCol = sqlite3_column_count(pStmt); 2449 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); 2450 for(i=0; i<nCol; i++){ 2451 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); 2452 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); 2453#ifndef SQLITE_OMIT_DECLTYPE 2454 sqlite3_snprintf(30, z+x, "declared type:"); 2455 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); 2456#endif 2457#ifdef SQLITE_ENABLE_COLUMN_METADATA 2458 sqlite3_snprintf(30, z+x, "database name:"); 2459 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); 2460 sqlite3_snprintf(30, z+x, "table name:"); 2461 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); 2462 sqlite3_snprintf(30, z+x, "origin name:"); 2463 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); 2464#endif 2465 } 2466 } 2467 2468 displayStatLine(pArg, "Memory Used:", 2469 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); 2470 displayStatLine(pArg, "Number of Outstanding Allocations:", 2471 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); 2472 if( pArg->shellFlgs & SHFLG_Pagecache ){ 2473 displayStatLine(pArg, "Number of Pcache Pages Used:", 2474 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); 2475 } 2476 displayStatLine(pArg, "Number of Pcache Overflow Bytes:", 2477 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); 2478 displayStatLine(pArg, "Largest Allocation:", 2479 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); 2480 displayStatLine(pArg, "Largest Pcache Allocation:", 2481 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); 2482#ifdef YYTRACKMAXSTACKDEPTH 2483 displayStatLine(pArg, "Deepest Parser Stack:", 2484 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); 2485#endif 2486 2487 if( db ){ 2488 if( pArg->shellFlgs & SHFLG_Lookaside ){ 2489 iHiwtr = iCur = -1; 2490 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, 2491 &iCur, &iHiwtr, bReset); 2492 raw_printf(pArg->out, 2493 "Lookaside Slots Used: %d (max %d)\n", 2494 iCur, iHiwtr); 2495 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, 2496 &iCur, &iHiwtr, bReset); 2497 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", 2498 iHiwtr); 2499 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, 2500 &iCur, &iHiwtr, bReset); 2501 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", 2502 iHiwtr); 2503 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, 2504 &iCur, &iHiwtr, bReset); 2505 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", 2506 iHiwtr); 2507 } 2508 iHiwtr = iCur = -1; 2509 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); 2510 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", 2511 iCur); 2512 iHiwtr = iCur = -1; 2513 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); 2514 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); 2515 iHiwtr = iCur = -1; 2516 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); 2517 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); 2518 iHiwtr = iCur = -1; 2519 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); 2520 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); 2521 iHiwtr = iCur = -1; 2522 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); 2523 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); 2524 iHiwtr = iCur = -1; 2525 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); 2526 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", 2527 iCur); 2528 iHiwtr = iCur = -1; 2529 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); 2530 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", 2531 iCur); 2532 } 2533 2534 if( pArg->pStmt ){ 2535 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, 2536 bReset); 2537 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); 2538 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); 2539 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); 2540 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); 2541 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); 2542 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2543 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); 2544 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); 2545 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); 2546 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); 2547 raw_printf(pArg->out, "Number of times run: %d\n", iCur); 2548 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); 2549 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); 2550 } 2551 2552#ifdef __linux__ 2553 displayLinuxIoStats(pArg->out); 2554#endif 2555 2556 /* Do not remove this machine readable comment: extra-stats-output-here */ 2557 2558 return 0; 2559} 2560 2561/* 2562** Display scan stats. 2563*/ 2564static void display_scanstats( 2565 sqlite3 *db, /* Database to query */ 2566 ShellState *pArg /* Pointer to ShellState */ 2567){ 2568#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 2569 UNUSED_PARAMETER(db); 2570 UNUSED_PARAMETER(pArg); 2571#else 2572 int i, k, n, mx; 2573 raw_printf(pArg->out, "-------- scanstats --------\n"); 2574 mx = 0; 2575 for(k=0; k<=mx; k++){ 2576 double rEstLoop = 1.0; 2577 for(i=n=0; 1; i++){ 2578 sqlite3_stmt *p = pArg->pStmt; 2579 sqlite3_int64 nLoop, nVisit; 2580 double rEst; 2581 int iSid; 2582 const char *zExplain; 2583 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ 2584 break; 2585 } 2586 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); 2587 if( iSid>mx ) mx = iSid; 2588 if( iSid!=k ) continue; 2589 if( n==0 ){ 2590 rEstLoop = (double)nLoop; 2591 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); 2592 } 2593 n++; 2594 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2595 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); 2596 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 2597 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); 2598 rEstLoop *= rEst; 2599 raw_printf(pArg->out, 2600 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", 2601 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst 2602 ); 2603 } 2604 } 2605 raw_printf(pArg->out, "---------------------------\n"); 2606#endif 2607} 2608 2609/* 2610** Parameter azArray points to a zero-terminated array of strings. zStr 2611** points to a single nul-terminated string. Return non-zero if zStr 2612** is equal, according to strcmp(), to any of the strings in the array. 2613** Otherwise, return zero. 2614*/ 2615static int str_in_array(const char *zStr, const char **azArray){ 2616 int i; 2617 for(i=0; azArray[i]; i++){ 2618 if( 0==strcmp(zStr, azArray[i]) ) return 1; 2619 } 2620 return 0; 2621} 2622 2623/* 2624** If compiled statement pSql appears to be an EXPLAIN statement, allocate 2625** and populate the ShellState.aiIndent[] array with the number of 2626** spaces each opcode should be indented before it is output. 2627** 2628** The indenting rules are: 2629** 2630** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent 2631** all opcodes that occur between the p2 jump destination and the opcode 2632** itself by 2 spaces. 2633** 2634** * For each "Goto", if the jump destination is earlier in the program 2635** and ends on one of: 2636** Yield SeekGt SeekLt RowSetRead Rewind 2637** or if the P1 parameter is one instead of zero, 2638** then indent all opcodes between the earlier instruction 2639** and "Goto" by 2 spaces. 2640*/ 2641static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ 2642 const char *zSql; /* The text of the SQL statement */ 2643 const char *z; /* Used to check if this is an EXPLAIN */ 2644 int *abYield = 0; /* True if op is an OP_Yield */ 2645 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ 2646 int iOp; /* Index of operation in p->aiIndent[] */ 2647 2648 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 }; 2649 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", 2650 "Rewind", 0 }; 2651 const char *azGoto[] = { "Goto", 0 }; 2652 2653 /* Try to figure out if this is really an EXPLAIN statement. If this 2654 ** cannot be verified, return early. */ 2655 if( sqlite3_column_count(pSql)!=8 ){ 2656 p->cMode = p->mode; 2657 return; 2658 } 2659 zSql = sqlite3_sql(pSql); 2660 if( zSql==0 ) return; 2661 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); 2662 if( sqlite3_strnicmp(z, "explain", 7) ){ 2663 p->cMode = p->mode; 2664 return; 2665 } 2666 2667 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ 2668 int i; 2669 int iAddr = sqlite3_column_int(pSql, 0); 2670 const char *zOp = (const char*)sqlite3_column_text(pSql, 1); 2671 2672 /* Set p2 to the P2 field of the current opcode. Then, assuming that 2673 ** p2 is an instruction address, set variable p2op to the index of that 2674 ** instruction in the aiIndent[] array. p2 and p2op may be different if 2675 ** the current instruction is part of a sub-program generated by an 2676 ** SQL trigger or foreign key. */ 2677 int p2 = sqlite3_column_int(pSql, 3); 2678 int p2op = (p2 + (iOp-iAddr)); 2679 2680 /* Grow the p->aiIndent array as required */ 2681 if( iOp>=nAlloc ){ 2682 if( iOp==0 ){ 2683 /* Do further verfication that this is explain output. Abort if 2684 ** it is not */ 2685 static const char *explainCols[] = { 2686 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; 2687 int jj; 2688 for(jj=0; jj<ArraySize(explainCols); jj++){ 2689 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ 2690 p->cMode = p->mode; 2691 sqlite3_reset(pSql); 2692 return; 2693 } 2694 } 2695 } 2696 nAlloc += 100; 2697 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); 2698 if( p->aiIndent==0 ) shell_out_of_memory(); 2699 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); 2700 if( abYield==0 ) shell_out_of_memory(); 2701 } 2702 abYield[iOp] = str_in_array(zOp, azYield); 2703 p->aiIndent[iOp] = 0; 2704 p->nIndent = iOp+1; 2705 2706 if( str_in_array(zOp, azNext) ){ 2707 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 2708 } 2709 if( str_in_array(zOp, azGoto) && p2op<p->nIndent 2710 && (abYield[p2op] || sqlite3_column_int(pSql, 2)) 2711 ){ 2712 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 2713 } 2714 } 2715 2716 p->iIndent = 0; 2717 sqlite3_free(abYield); 2718 sqlite3_reset(pSql); 2719} 2720 2721/* 2722** Free the array allocated by explain_data_prepare(). 2723*/ 2724static void explain_data_delete(ShellState *p){ 2725 sqlite3_free(p->aiIndent); 2726 p->aiIndent = 0; 2727 p->nIndent = 0; 2728 p->iIndent = 0; 2729} 2730 2731/* 2732** Disable and restore .wheretrace and .selecttrace settings. 2733*/ 2734#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2735extern int sqlite3SelectTrace; 2736static int savedSelectTrace; 2737#endif 2738#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2739extern int sqlite3WhereTrace; 2740static int savedWhereTrace; 2741#endif 2742static void disable_debug_trace_modes(void){ 2743#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2744 savedSelectTrace = sqlite3SelectTrace; 2745 sqlite3SelectTrace = 0; 2746#endif 2747#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2748 savedWhereTrace = sqlite3WhereTrace; 2749 sqlite3WhereTrace = 0; 2750#endif 2751} 2752static void restore_debug_trace_modes(void){ 2753#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 2754 sqlite3SelectTrace = savedSelectTrace; 2755#endif 2756#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 2757 sqlite3WhereTrace = savedWhereTrace; 2758#endif 2759} 2760 2761/* Create the TEMP table used to store parameter bindings */ 2762static void bind_table_init(ShellState *p){ 2763 int wrSchema = 0; 2764 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); 2765 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); 2766 sqlite3_exec(p->db, 2767 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" 2768 " key TEXT PRIMARY KEY,\n" 2769 " value ANY\n" 2770 ") WITHOUT ROWID;", 2771 0, 0, 0); 2772 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); 2773} 2774 2775/* 2776** Bind parameters on a prepared statement. 2777** 2778** Parameter bindings are taken from a TEMP table of the form: 2779** 2780** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) 2781** WITHOUT ROWID; 2782** 2783** No bindings occur if this table does not exist. The special character '$' 2784** is included in the table name to help prevent collisions with actual tables. 2785** The table must be in the TEMP schema. 2786*/ 2787static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ 2788 int nVar; 2789 int i; 2790 int rc; 2791 sqlite3_stmt *pQ = 0; 2792 2793 nVar = sqlite3_bind_parameter_count(pStmt); 2794 if( nVar==0 ) return; /* Nothing to do */ 2795 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", 2796 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ 2797 return; /* Parameter table does not exist */ 2798 } 2799 rc = sqlite3_prepare_v2(pArg->db, 2800 "SELECT value FROM temp.sqlite_parameters" 2801 " WHERE key=?1", -1, &pQ, 0); 2802 if( rc || pQ==0 ) return; 2803 for(i=1; i<=nVar; i++){ 2804 char zNum[30]; 2805 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 2806 if( zVar==0 ){ 2807 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); 2808 zVar = zNum; 2809 } 2810 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); 2811 if( sqlite3_step(pQ)==SQLITE_ROW ){ 2812 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); 2813 }else{ 2814 sqlite3_bind_null(pStmt, i); 2815 } 2816 sqlite3_reset(pQ); 2817 } 2818 sqlite3_finalize(pQ); 2819} 2820 2821/* 2822** Run a prepared statement 2823*/ 2824static void exec_prepared_stmt( 2825 ShellState *pArg, /* Pointer to ShellState */ 2826 sqlite3_stmt *pStmt /* Statment to run */ 2827){ 2828 int rc; 2829 2830 /* perform the first step. this will tell us if we 2831 ** have a result set or not and how wide it is. 2832 */ 2833 rc = sqlite3_step(pStmt); 2834 /* if we have a result set... */ 2835 if( SQLITE_ROW == rc ){ 2836 /* allocate space for col name ptr, value ptr, and type */ 2837 int nCol = sqlite3_column_count(pStmt); 2838 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); 2839 if( !pData ){ 2840 rc = SQLITE_NOMEM; 2841 }else{ 2842 char **azCols = (char **)pData; /* Names of result columns */ 2843 char **azVals = &azCols[nCol]; /* Results */ 2844 int *aiTypes = (int *)&azVals[nCol]; /* Result types */ 2845 int i, x; 2846 assert(sizeof(int) <= sizeof(char *)); 2847 /* save off ptrs to column names */ 2848 for(i=0; i<nCol; i++){ 2849 azCols[i] = (char *)sqlite3_column_name(pStmt, i); 2850 } 2851 do{ 2852 /* extract the data and data types */ 2853 for(i=0; i<nCol; i++){ 2854 aiTypes[i] = x = sqlite3_column_type(pStmt, i); 2855 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ 2856 azVals[i] = ""; 2857 }else{ 2858 azVals[i] = (char*)sqlite3_column_text(pStmt, i); 2859 } 2860 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ 2861 rc = SQLITE_NOMEM; 2862 break; /* from for */ 2863 } 2864 } /* end for */ 2865 2866 /* if data and types extracted successfully... */ 2867 if( SQLITE_ROW == rc ){ 2868 /* call the supplied callback with the result row data */ 2869 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ 2870 rc = SQLITE_ABORT; 2871 }else{ 2872 rc = sqlite3_step(pStmt); 2873 } 2874 } 2875 } while( SQLITE_ROW == rc ); 2876 sqlite3_free(pData); 2877 } 2878 } 2879} 2880 2881#ifndef SQLITE_OMIT_VIRTUALTABLE 2882/* 2883** This function is called to process SQL if the previous shell command 2884** was ".expert". It passes the SQL in the second argument directly to 2885** the sqlite3expert object. 2886** 2887** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 2888** code. In this case, (*pzErr) may be set to point to a buffer containing 2889** an English language error message. It is the responsibility of the 2890** caller to eventually free this buffer using sqlite3_free(). 2891*/ 2892static int expertHandleSQL( 2893 ShellState *pState, 2894 const char *zSql, 2895 char **pzErr 2896){ 2897 assert( pState->expert.pExpert ); 2898 assert( pzErr==0 || *pzErr==0 ); 2899 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); 2900} 2901 2902/* 2903** This function is called either to silently clean up the object 2904** created by the ".expert" command (if bCancel==1), or to generate a 2905** report from it and then clean it up (if bCancel==0). 2906** 2907** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 2908** code. In this case, (*pzErr) may be set to point to a buffer containing 2909** an English language error message. It is the responsibility of the 2910** caller to eventually free this buffer using sqlite3_free(). 2911*/ 2912static int expertFinish( 2913 ShellState *pState, 2914 int bCancel, 2915 char **pzErr 2916){ 2917 int rc = SQLITE_OK; 2918 sqlite3expert *p = pState->expert.pExpert; 2919 assert( p ); 2920 assert( bCancel || pzErr==0 || *pzErr==0 ); 2921 if( bCancel==0 ){ 2922 FILE *out = pState->out; 2923 int bVerbose = pState->expert.bVerbose; 2924 2925 rc = sqlite3_expert_analyze(p, pzErr); 2926 if( rc==SQLITE_OK ){ 2927 int nQuery = sqlite3_expert_count(p); 2928 int i; 2929 2930 if( bVerbose ){ 2931 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); 2932 raw_printf(out, "-- Candidates -----------------------------\n"); 2933 raw_printf(out, "%s\n", zCand); 2934 } 2935 for(i=0; i<nQuery; i++){ 2936 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); 2937 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); 2938 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); 2939 if( zIdx==0 ) zIdx = "(no new indexes)\n"; 2940 if( bVerbose ){ 2941 raw_printf(out, "-- Query %d --------------------------------\n",i+1); 2942 raw_printf(out, "%s\n\n", zSql); 2943 } 2944 raw_printf(out, "%s\n", zIdx); 2945 raw_printf(out, "%s\n", zEQP); 2946 } 2947 } 2948 } 2949 sqlite3_expert_destroy(p); 2950 pState->expert.pExpert = 0; 2951 return rc; 2952} 2953 2954/* 2955** Implementation of ".expert" dot command. 2956*/ 2957static int expertDotCommand( 2958 ShellState *pState, /* Current shell tool state */ 2959 char **azArg, /* Array of arguments passed to dot command */ 2960 int nArg /* Number of entries in azArg[] */ 2961){ 2962 int rc = SQLITE_OK; 2963 char *zErr = 0; 2964 int i; 2965 int iSample = 0; 2966 2967 assert( pState->expert.pExpert==0 ); 2968 memset(&pState->expert, 0, sizeof(ExpertInfo)); 2969 2970 for(i=1; rc==SQLITE_OK && i<nArg; i++){ 2971 char *z = azArg[i]; 2972 int n; 2973 if( z[0]=='-' && z[1]=='-' ) z++; 2974 n = strlen30(z); 2975 if( n>=2 && 0==strncmp(z, "-verbose", n) ){ 2976 pState->expert.bVerbose = 1; 2977 } 2978 else if( n>=2 && 0==strncmp(z, "-sample", n) ){ 2979 if( i==(nArg-1) ){ 2980 raw_printf(stderr, "option requires an argument: %s\n", z); 2981 rc = SQLITE_ERROR; 2982 }else{ 2983 iSample = (int)integerValue(azArg[++i]); 2984 if( iSample<0 || iSample>100 ){ 2985 raw_printf(stderr, "value out of range: %s\n", azArg[i]); 2986 rc = SQLITE_ERROR; 2987 } 2988 } 2989 } 2990 else{ 2991 raw_printf(stderr, "unknown option: %s\n", z); 2992 rc = SQLITE_ERROR; 2993 } 2994 } 2995 2996 if( rc==SQLITE_OK ){ 2997 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); 2998 if( pState->expert.pExpert==0 ){ 2999 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr); 3000 rc = SQLITE_ERROR; 3001 }else{ 3002 sqlite3_expert_config( 3003 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample 3004 ); 3005 } 3006 } 3007 3008 return rc; 3009} 3010#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 3011 3012/* 3013** Execute a statement or set of statements. Print 3014** any result rows/columns depending on the current mode 3015** set via the supplied callback. 3016** 3017** This is very similar to SQLite's built-in sqlite3_exec() 3018** function except it takes a slightly different callback 3019** and callback data argument. 3020*/ 3021static int shell_exec( 3022 ShellState *pArg, /* Pointer to ShellState */ 3023 const char *zSql, /* SQL to be evaluated */ 3024 char **pzErrMsg /* Error msg written here */ 3025){ 3026 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ 3027 int rc = SQLITE_OK; /* Return Code */ 3028 int rc2; 3029 const char *zLeftover; /* Tail of unprocessed SQL */ 3030 sqlite3 *db = pArg->db; 3031 3032 if( pzErrMsg ){ 3033 *pzErrMsg = NULL; 3034 } 3035 3036#ifndef SQLITE_OMIT_VIRTUALTABLE 3037 if( pArg->expert.pExpert ){ 3038 rc = expertHandleSQL(pArg, zSql, pzErrMsg); 3039 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); 3040 } 3041#endif 3042 3043 while( zSql[0] && (SQLITE_OK == rc) ){ 3044 static const char *zStmtSql; 3045 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); 3046 if( SQLITE_OK != rc ){ 3047 if( pzErrMsg ){ 3048 *pzErrMsg = save_err_msg(db); 3049 } 3050 }else{ 3051 if( !pStmt ){ 3052 /* this happens for a comment or white-space */ 3053 zSql = zLeftover; 3054 while( IsSpace(zSql[0]) ) zSql++; 3055 continue; 3056 } 3057 zStmtSql = sqlite3_sql(pStmt); 3058 if( zStmtSql==0 ) zStmtSql = ""; 3059 while( IsSpace(zStmtSql[0]) ) zStmtSql++; 3060 3061 /* save off the prepared statment handle and reset row count */ 3062 if( pArg ){ 3063 pArg->pStmt = pStmt; 3064 pArg->cnt = 0; 3065 } 3066 3067 /* echo the sql statement if echo on */ 3068 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ 3069 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); 3070 } 3071 3072 /* Show the EXPLAIN QUERY PLAN if .eqp is on */ 3073 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ 3074 sqlite3_stmt *pExplain; 3075 char *zEQP; 3076 int triggerEQP = 0; 3077 disable_debug_trace_modes(); 3078 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); 3079 if( pArg->autoEQP>=AUTOEQP_trigger ){ 3080 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); 3081 } 3082 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); 3083 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3084 if( rc==SQLITE_OK ){ 3085 while( sqlite3_step(pExplain)==SQLITE_ROW ){ 3086 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); 3087 int iEqpId = sqlite3_column_int(pExplain, 0); 3088 int iParentId = sqlite3_column_int(pExplain, 1); 3089 if( zEQPLine[0]=='-' ) eqp_render(pArg); 3090 eqp_append(pArg, iEqpId, iParentId, zEQPLine); 3091 } 3092 eqp_render(pArg); 3093 } 3094 sqlite3_finalize(pExplain); 3095 sqlite3_free(zEQP); 3096 if( pArg->autoEQP>=AUTOEQP_full ){ 3097 /* Also do an EXPLAIN for ".eqp full" mode */ 3098 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); 3099 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3100 if( rc==SQLITE_OK ){ 3101 pArg->cMode = MODE_Explain; 3102 explain_data_prepare(pArg, pExplain); 3103 exec_prepared_stmt(pArg, pExplain); 3104 explain_data_delete(pArg); 3105 } 3106 sqlite3_finalize(pExplain); 3107 sqlite3_free(zEQP); 3108 } 3109 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ 3110 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); 3111 /* Reprepare pStmt before reactiving trace modes */ 3112 sqlite3_finalize(pStmt); 3113 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 3114 if( pArg ) pArg->pStmt = pStmt; 3115 } 3116 restore_debug_trace_modes(); 3117 } 3118 3119 if( pArg ){ 3120 pArg->cMode = pArg->mode; 3121 if( pArg->autoExplain ){ 3122 if( sqlite3_stmt_isexplain(pStmt)==1 ){ 3123 pArg->cMode = MODE_Explain; 3124 } 3125 if( sqlite3_stmt_isexplain(pStmt)==2 ){ 3126 pArg->cMode = MODE_EQP; 3127 } 3128 } 3129 3130 /* If the shell is currently in ".explain" mode, gather the extra 3131 ** data required to add indents to the output.*/ 3132 if( pArg->cMode==MODE_Explain ){ 3133 explain_data_prepare(pArg, pStmt); 3134 } 3135 } 3136 3137 bind_prepared_stmt(pArg, pStmt); 3138 exec_prepared_stmt(pArg, pStmt); 3139 explain_data_delete(pArg); 3140 eqp_render(pArg); 3141 3142 /* print usage stats if stats on */ 3143 if( pArg && pArg->statsOn ){ 3144 display_stats(db, pArg, 0); 3145 } 3146 3147 /* print loop-counters if required */ 3148 if( pArg && pArg->scanstatsOn ){ 3149 display_scanstats(db, pArg); 3150 } 3151 3152 /* Finalize the statement just executed. If this fails, save a 3153 ** copy of the error message. Otherwise, set zSql to point to the 3154 ** next statement to execute. */ 3155 rc2 = sqlite3_finalize(pStmt); 3156 if( rc!=SQLITE_NOMEM ) rc = rc2; 3157 if( rc==SQLITE_OK ){ 3158 zSql = zLeftover; 3159 while( IsSpace(zSql[0]) ) zSql++; 3160 }else if( pzErrMsg ){ 3161 *pzErrMsg = save_err_msg(db); 3162 } 3163 3164 /* clear saved stmt handle */ 3165 if( pArg ){ 3166 pArg->pStmt = NULL; 3167 } 3168 } 3169 } /* end while */ 3170 3171 return rc; 3172} 3173 3174/* 3175** Release memory previously allocated by tableColumnList(). 3176*/ 3177static void freeColumnList(char **azCol){ 3178 int i; 3179 for(i=1; azCol[i]; i++){ 3180 sqlite3_free(azCol[i]); 3181 } 3182 /* azCol[0] is a static string */ 3183 sqlite3_free(azCol); 3184} 3185 3186/* 3187** Return a list of pointers to strings which are the names of all 3188** columns in table zTab. The memory to hold the names is dynamically 3189** allocated and must be released by the caller using a subsequent call 3190** to freeColumnList(). 3191** 3192** The azCol[0] entry is usually NULL. However, if zTab contains a rowid 3193** value that needs to be preserved, then azCol[0] is filled in with the 3194** name of the rowid column. 3195** 3196** The first regular column in the table is azCol[1]. The list is terminated 3197** by an entry with azCol[i]==0. 3198*/ 3199static char **tableColumnList(ShellState *p, const char *zTab){ 3200 char **azCol = 0; 3201 sqlite3_stmt *pStmt; 3202 char *zSql; 3203 int nCol = 0; 3204 int nAlloc = 0; 3205 int nPK = 0; /* Number of PRIMARY KEY columns seen */ 3206 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ 3207 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); 3208 int rc; 3209 3210 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); 3211 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 3212 sqlite3_free(zSql); 3213 if( rc ) return 0; 3214 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 3215 if( nCol>=nAlloc-2 ){ 3216 nAlloc = nAlloc*2 + nCol + 10; 3217 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); 3218 if( azCol==0 ) shell_out_of_memory(); 3219 } 3220 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); 3221 if( sqlite3_column_int(pStmt, 5) ){ 3222 nPK++; 3223 if( nPK==1 3224 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), 3225 "INTEGER")==0 3226 ){ 3227 isIPK = 1; 3228 }else{ 3229 isIPK = 0; 3230 } 3231 } 3232 } 3233 sqlite3_finalize(pStmt); 3234 if( azCol==0 ) return 0; 3235 azCol[0] = 0; 3236 azCol[nCol+1] = 0; 3237 3238 /* The decision of whether or not a rowid really needs to be preserved 3239 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table 3240 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve 3241 ** rowids on tables where the rowid is inaccessible because there are other 3242 ** columns in the table named "rowid", "_rowid_", and "oid". 3243 */ 3244 if( preserveRowid && isIPK ){ 3245 /* If a single PRIMARY KEY column with type INTEGER was seen, then it 3246 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID 3247 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are 3248 ** ROWID aliases. To distinguish these cases, check to see if 3249 ** there is a "pk" entry in "PRAGMA index_list". There will be 3250 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. 3251 */ 3252 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" 3253 " WHERE origin='pk'", zTab); 3254 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 3255 sqlite3_free(zSql); 3256 if( rc ){ 3257 freeColumnList(azCol); 3258 return 0; 3259 } 3260 rc = sqlite3_step(pStmt); 3261 sqlite3_finalize(pStmt); 3262 preserveRowid = rc==SQLITE_ROW; 3263 } 3264 if( preserveRowid ){ 3265 /* Only preserve the rowid if we can find a name to use for the 3266 ** rowid */ 3267 static char *azRowid[] = { "rowid", "_rowid_", "oid" }; 3268 int i, j; 3269 for(j=0; j<3; j++){ 3270 for(i=1; i<=nCol; i++){ 3271 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; 3272 } 3273 if( i>nCol ){ 3274 /* At this point, we know that azRowid[j] is not the name of any 3275 ** ordinary column in the table. Verify that azRowid[j] is a valid 3276 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID 3277 ** tables will fail this last check */ 3278 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); 3279 if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; 3280 break; 3281 } 3282 } 3283 } 3284 return azCol; 3285} 3286 3287/* 3288** Toggle the reverse_unordered_selects setting. 3289*/ 3290static void toggleSelectOrder(sqlite3 *db){ 3291 sqlite3_stmt *pStmt = 0; 3292 int iSetting = 0; 3293 char zStmt[100]; 3294 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); 3295 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 3296 iSetting = sqlite3_column_int(pStmt, 0); 3297 } 3298 sqlite3_finalize(pStmt); 3299 sqlite3_snprintf(sizeof(zStmt), zStmt, 3300 "PRAGMA reverse_unordered_selects(%d)", !iSetting); 3301 sqlite3_exec(db, zStmt, 0, 0, 0); 3302} 3303 3304/* 3305** This is a different callback routine used for dumping the database. 3306** Each row received by this callback consists of a table name, 3307** the table type ("index" or "table") and SQL to create the table. 3308** This routine should print text sufficient to recreate the table. 3309*/ 3310static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ 3311 int rc; 3312 const char *zTable; 3313 const char *zType; 3314 const char *zSql; 3315 ShellState *p = (ShellState *)pArg; 3316 3317 UNUSED_PARAMETER(azNotUsed); 3318 if( nArg!=3 || azArg==0 ) return 0; 3319 zTable = azArg[0]; 3320 zType = azArg[1]; 3321 zSql = azArg[2]; 3322 3323 if( strcmp(zTable, "sqlite_sequence")==0 ){ 3324 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); 3325 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ 3326 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 3327 }else if( strncmp(zTable, "sqlite_", 7)==0 ){ 3328 return 0; 3329 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ 3330 char *zIns; 3331 if( !p->writableSchema ){ 3332 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); 3333 p->writableSchema = 1; 3334 } 3335 zIns = sqlite3_mprintf( 3336 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" 3337 "VALUES('table','%q','%q',0,'%q');", 3338 zTable, zTable, zSql); 3339 utf8_printf(p->out, "%s\n", zIns); 3340 sqlite3_free(zIns); 3341 return 0; 3342 }else{ 3343 printSchemaLine(p->out, zSql, ";\n"); 3344 } 3345 3346 if( strcmp(zType, "table")==0 ){ 3347 ShellText sSelect; 3348 ShellText sTable; 3349 char **azCol; 3350 int i; 3351 char *savedDestTable; 3352 int savedMode; 3353 3354 azCol = tableColumnList(p, zTable); 3355 if( azCol==0 ){ 3356 p->nErr++; 3357 return 0; 3358 } 3359 3360 /* Always quote the table name, even if it appears to be pure ascii, 3361 ** in case it is a keyword. Ex: INSERT INTO "table" ... */ 3362 initText(&sTable); 3363 appendText(&sTable, zTable, quoteChar(zTable)); 3364 /* If preserving the rowid, add a column list after the table name. 3365 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" 3366 ** instead of the usual "INSERT INTO tab VALUES(...)". 3367 */ 3368 if( azCol[0] ){ 3369 appendText(&sTable, "(", 0); 3370 appendText(&sTable, azCol[0], 0); 3371 for(i=1; azCol[i]; i++){ 3372 appendText(&sTable, ",", 0); 3373 appendText(&sTable, azCol[i], quoteChar(azCol[i])); 3374 } 3375 appendText(&sTable, ")", 0); 3376 } 3377 3378 /* Build an appropriate SELECT statement */ 3379 initText(&sSelect); 3380 appendText(&sSelect, "SELECT ", 0); 3381 if( azCol[0] ){ 3382 appendText(&sSelect, azCol[0], 0); 3383 appendText(&sSelect, ",", 0); 3384 } 3385 for(i=1; azCol[i]; i++){ 3386 appendText(&sSelect, azCol[i], quoteChar(azCol[i])); 3387 if( azCol[i+1] ){ 3388 appendText(&sSelect, ",", 0); 3389 } 3390 } 3391 freeColumnList(azCol); 3392 appendText(&sSelect, " FROM ", 0); 3393 appendText(&sSelect, zTable, quoteChar(zTable)); 3394 3395 savedDestTable = p->zDestTable; 3396 savedMode = p->mode; 3397 p->zDestTable = sTable.z; 3398 p->mode = p->cMode = MODE_Insert; 3399 rc = shell_exec(p, sSelect.z, 0); 3400 if( (rc&0xff)==SQLITE_CORRUPT ){ 3401 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 3402 toggleSelectOrder(p->db); 3403 shell_exec(p, sSelect.z, 0); 3404 toggleSelectOrder(p->db); 3405 } 3406 p->zDestTable = savedDestTable; 3407 p->mode = savedMode; 3408 freeText(&sTable); 3409 freeText(&sSelect); 3410 if( rc ) p->nErr++; 3411 } 3412 return 0; 3413} 3414 3415/* 3416** Run zQuery. Use dump_callback() as the callback routine so that 3417** the contents of the query are output as SQL statements. 3418** 3419** If we get a SQLITE_CORRUPT error, rerun the query after appending 3420** "ORDER BY rowid DESC" to the end. 3421*/ 3422static int run_schema_dump_query( 3423 ShellState *p, 3424 const char *zQuery 3425){ 3426 int rc; 3427 char *zErr = 0; 3428 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); 3429 if( rc==SQLITE_CORRUPT ){ 3430 char *zQ2; 3431 int len = strlen30(zQuery); 3432 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 3433 if( zErr ){ 3434 utf8_printf(p->out, "/****** %s ******/\n", zErr); 3435 sqlite3_free(zErr); 3436 zErr = 0; 3437 } 3438 zQ2 = malloc( len+100 ); 3439 if( zQ2==0 ) return rc; 3440 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); 3441 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); 3442 if( rc ){ 3443 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); 3444 }else{ 3445 rc = SQLITE_CORRUPT; 3446 } 3447 sqlite3_free(zErr); 3448 free(zQ2); 3449 } 3450 return rc; 3451} 3452 3453/* 3454** Text of help messages. 3455** 3456** The help text for each individual command begins with a line that starts 3457** with ".". Subsequent lines are supplimental information. 3458** 3459** There must be two or more spaces between the end of the command and the 3460** start of the description of what that command does. 3461*/ 3462static const char *(azHelp[]) = { 3463#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 3464 ".archive ... Manage SQL archives", 3465 " Each command must have exactly one of the following options:", 3466 " -c, --create Create a new archive", 3467 " -u, --update Add or update files with changed mtime", 3468 " -i, --insert Like -u but always add even if unchanged", 3469 " -t, --list List contents of archive", 3470 " -x, --extract Extract files from archive", 3471 " Optional arguments:", 3472 " -v, --verbose Print each filename as it is processed", 3473 " -f FILE, --file FILE Use archive FILE (default is current db)", 3474 " -a FILE, --append FILE Open FILE using the apndvfs VFS", 3475 " -C DIR, --directory DIR Read/extract files from directory DIR", 3476 " -n, --dryrun Show the SQL that would have occurred", 3477 " Examples:", 3478 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", 3479 " .ar -tf ARCHIVE # List members of ARCHIVE", 3480 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", 3481 " See also:", 3482 " http://sqlite.org/cli.html#sqlar_archive_support", 3483#endif 3484#ifndef SQLITE_OMIT_AUTHORIZATION 3485 ".auth ON|OFF Show authorizer callbacks", 3486#endif 3487 ".backup ?DB? FILE Backup DB (default \"main\") to FILE", 3488 " --append Use the appendvfs", 3489 " --async Write to FILE without journal and fsync()", 3490 ".bail on|off Stop after hitting an error. Default OFF", 3491 ".binary on|off Turn binary output on or off. Default OFF", 3492 ".cd DIRECTORY Change the working directory to DIRECTORY", 3493 ".changes on|off Show number of rows changed by SQL", 3494 ".check GLOB Fail if output since .testcase does not match", 3495 ".clone NEWDB Clone data into NEWDB from the existing database", 3496 ".databases List names and files of attached databases", 3497 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", 3498 ".dbinfo ?DB? Show status information about the database", 3499 ".dump ?TABLE? ... Render all database content as SQL", 3500 " Options:", 3501 " --preserve-rowids Include ROWID values in the output", 3502 " --newlines Allow unescaped newline characters in output", 3503 " TABLE is a LIKE pattern for the tables to dump", 3504 ".echo on|off Turn command echo on or off", 3505 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", 3506 " Other Modes:", 3507#ifdef SQLITE_DEBUG 3508 " test Show raw EXPLAIN QUERY PLAN output", 3509 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", 3510#endif 3511 " trigger Like \"full\" but also show trigger bytecode", 3512 ".excel Display the output of next command in spreadsheet", 3513 ".exit ?CODE? Exit this program with return-code CODE", 3514 ".expert EXPERIMENTAL. Suggest indexes for queries", 3515 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", 3516 ".filectrl CMD ... Run various sqlite3_file_control() operations", 3517 " Run \".filectrl\" with no arguments for details", 3518 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", 3519 ".headers on|off Turn display of headers on or off", 3520 ".help ?-all? ?PATTERN? Show help text for PATTERN", 3521 ".import FILE TABLE Import data from FILE into TABLE", 3522#ifndef SQLITE_OMIT_TEST_CONTROL 3523 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", 3524#endif 3525 ".indexes ?TABLE? Show names of indexes", 3526 " If TABLE is specified, only show indexes for", 3527 " tables matching TABLE using the LIKE operator.", 3528#ifdef SQLITE_ENABLE_IOTRACE 3529 ".iotrace FILE Enable I/O diagnostic logging to FILE", 3530#endif 3531 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", 3532 ".lint OPTIONS Report potential schema issues.", 3533 " Options:", 3534 " fkey-indexes Find missing foreign key indexes", 3535#ifndef SQLITE_OMIT_LOAD_EXTENSION 3536 ".load FILE ?ENTRY? Load an extension library", 3537#endif 3538 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", 3539 ".mode MODE ?TABLE? Set output mode", 3540 " MODE is one of:", 3541 " ascii Columns/rows delimited by 0x1F and 0x1E", 3542 " csv Comma-separated values", 3543 " column Left-aligned columns. (See .width)", 3544 " html HTML <table> code", 3545 " insert SQL insert statements for TABLE", 3546 " line One value per line", 3547 " list Values delimited by \"|\"", 3548 " quote Escape answers as for SQL", 3549 " tabs Tab-separated values", 3550 " tcl TCL list elements", 3551 ".nullvalue STRING Use STRING in place of NULL values", 3552 ".once (-e|-x|FILE) Output for the next SQL command only to FILE", 3553 " If FILE begins with '|' then open as a pipe", 3554 " Other options:", 3555 " -e Invoke system text editor", 3556 " -x Open in a spreadsheet", 3557 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", 3558 " Options:", 3559 " --append Use appendvfs to append database to the end of FILE", 3560#ifdef SQLITE_ENABLE_DESERIALIZE 3561 " --deserialize Load into memory useing sqlite3_deserialize()", 3562 " --hexdb Load the output of \"dbtotxt\" as an in-memory db", 3563 " --maxsize N Maximum size for --hexdb or --deserialized database", 3564#endif 3565 " --new Initialize FILE to an empty database", 3566 " --nofollow Do not follow symbolic links", 3567 " --readonly Open FILE readonly", 3568 " --zip FILE is a ZIP archive", 3569 ".output ?FILE? Send output to FILE or stdout if FILE is omitted", 3570 " If FILE begins with '|' then open it as a pipe.", 3571 ".parameter CMD ... Manage SQL parameter bindings", 3572 " clear Erase all bindings", 3573 " init Initialize the TEMP table that holds bindings", 3574 " list List the current parameter bindings", 3575 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", 3576 " PARAMETER should start with one of: $ : @ ?", 3577 " unset PARAMETER Remove PARAMETER from the binding table", 3578 ".print STRING... Print literal STRING", 3579#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 3580 ".progress N Invoke progress handler after every N opcodes", 3581 " --limit N Interrupt after N progress callbacks", 3582 " --once Do no more than one progress interrupt", 3583 " --quiet|-q No output except at interrupts", 3584 " --reset Reset the count for each input and interrupt", 3585#endif 3586 ".prompt MAIN CONTINUE Replace the standard prompts", 3587 ".quit Exit this program", 3588 ".read FILE Read input from FILE", 3589#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 3590 ".recover Recover as much data as possible from corrupt db.", 3591 " --freelist-corrupt Assume the freelist is corrupt", 3592 " --recovery-db NAME Store recovery metadata in database file NAME", 3593 " --lost-and-found TABLE Alternative name for the lost-and-found table", 3594 " --no-rowids Do not attempt to recover rowid values", 3595 " that are not also INTEGER PRIMARY KEYs", 3596#endif 3597 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", 3598 ".save FILE Write in-memory database into FILE", 3599 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", 3600 ".schema ?PATTERN? Show the CREATE statements matching PATTERN", 3601 " Options:", 3602 " --indent Try to pretty-print the schema", 3603 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", 3604 " Options:", 3605 " --init Create a new SELFTEST table", 3606 " -v Verbose output", 3607 ".separator COL ?ROW? Change the column and row separators", 3608#if defined(SQLITE_ENABLE_SESSION) 3609 ".session ?NAME? CMD ... Create or control sessions", 3610 " Subcommands:", 3611 " attach TABLE Attach TABLE", 3612 " changeset FILE Write a changeset into FILE", 3613 " close Close one session", 3614 " enable ?BOOLEAN? Set or query the enable bit", 3615 " filter GLOB... Reject tables matching GLOBs", 3616 " indirect ?BOOLEAN? Mark or query the indirect status", 3617 " isempty Query whether the session is empty", 3618 " list List currently open session names", 3619 " open DB NAME Open a new session on DB", 3620 " patchset FILE Write a patchset into FILE", 3621 " If ?NAME? is omitted, the first defined session is used.", 3622#endif 3623 ".sha3sum ... Compute a SHA3 hash of database content", 3624 " Options:", 3625 " --schema Also hash the sqlite_master table", 3626 " --sha3-224 Use the sha3-224 algorithm", 3627 " --sha3-256 Use the sha3-256 algorithm (default)", 3628 " --sha3-384 Use the sha3-384 algorithm", 3629 " --sha3-512 Use the sha3-512 algorithm", 3630 " Any other argument is a LIKE pattern for tables to hash", 3631#ifndef SQLITE_NOHAVE_SYSTEM 3632 ".shell CMD ARGS... Run CMD ARGS... in a system shell", 3633#endif 3634 ".show Show the current values for various settings", 3635 ".stats ?on|off? Show stats or turn stats on or off", 3636#ifndef SQLITE_NOHAVE_SYSTEM 3637 ".system CMD ARGS... Run CMD ARGS... in a system shell", 3638#endif 3639 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", 3640 ".testcase NAME Begin redirecting output to 'testcase-out.txt'", 3641 ".testctrl CMD ... Run various sqlite3_test_control() operations", 3642 " Run \".testctrl\" with no arguments for details", 3643 ".timeout MS Try opening locked tables for MS milliseconds", 3644 ".timer on|off Turn SQL timer on or off", 3645#ifndef SQLITE_OMIT_TRACE 3646 ".trace ?OPTIONS? Output each SQL statement as it is run", 3647 " FILE Send output to FILE", 3648 " stdout Send output to stdout", 3649 " stderr Send output to stderr", 3650 " off Disable tracing", 3651 " --expanded Expand query parameters", 3652#ifdef SQLITE_ENABLE_NORMALIZE 3653 " --normalized Normal the SQL statements", 3654#endif 3655 " --plain Show SQL as it is input", 3656 " --stmt Trace statement execution (SQLITE_TRACE_STMT)", 3657 " --profile Profile statements (SQLITE_TRACE_PROFILE)", 3658 " --row Trace each row (SQLITE_TRACE_ROW)", 3659 " --close Trace connection close (SQLITE_TRACE_CLOSE)", 3660#endif /* SQLITE_OMIT_TRACE */ 3661#ifdef SQLITE_DEBUG 3662 ".unmodule NAME ... Unregister virtual table modules", 3663 " --allexcept Unregister everything except those named", 3664#endif 3665 ".vfsinfo ?AUX? Information about the top-level VFS", 3666 ".vfslist List all available VFSes", 3667 ".vfsname ?AUX? Print the name of the VFS stack", 3668 ".width NUM1 NUM2 ... Set column widths for \"column\" mode", 3669 " Negative values right-justify", 3670}; 3671 3672/* 3673** Output help text. 3674** 3675** zPattern describes the set of commands for which help text is provided. 3676** If zPattern is NULL, then show all commands, but only give a one-line 3677** description of each. 3678** 3679** Return the number of matches. 3680*/ 3681static int showHelp(FILE *out, const char *zPattern){ 3682 int i = 0; 3683 int j = 0; 3684 int n = 0; 3685 char *zPat; 3686 if( zPattern==0 3687 || zPattern[0]=='0' 3688 || strcmp(zPattern,"-a")==0 3689 || strcmp(zPattern,"-all")==0 3690 ){ 3691 /* Show all commands, but only one line per command */ 3692 if( zPattern==0 ) zPattern = ""; 3693 for(i=0; i<ArraySize(azHelp); i++){ 3694 if( azHelp[i][0]=='.' || zPattern[0] ){ 3695 utf8_printf(out, "%s\n", azHelp[i]); 3696 n++; 3697 } 3698 } 3699 }else{ 3700 /* Look for commands that for which zPattern is an exact prefix */ 3701 zPat = sqlite3_mprintf(".%s*", zPattern); 3702 for(i=0; i<ArraySize(azHelp); i++){ 3703 if( sqlite3_strglob(zPat, azHelp[i])==0 ){ 3704 utf8_printf(out, "%s\n", azHelp[i]); 3705 j = i+1; 3706 n++; 3707 } 3708 } 3709 sqlite3_free(zPat); 3710 if( n ){ 3711 if( n==1 ){ 3712 /* when zPattern is a prefix of exactly one command, then include the 3713 ** details of that command, which should begin at offset j */ 3714 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ 3715 utf8_printf(out, "%s\n", azHelp[j]); 3716 j++; 3717 } 3718 } 3719 return n; 3720 } 3721 /* Look for commands that contain zPattern anywhere. Show the complete 3722 ** text of all commands that match. */ 3723 zPat = sqlite3_mprintf("%%%s%%", zPattern); 3724 for(i=0; i<ArraySize(azHelp); i++){ 3725 if( azHelp[i][0]=='.' ) j = i; 3726 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ 3727 utf8_printf(out, "%s\n", azHelp[j]); 3728 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ 3729 j++; 3730 utf8_printf(out, "%s\n", azHelp[j]); 3731 } 3732 i = j; 3733 n++; 3734 } 3735 } 3736 sqlite3_free(zPat); 3737 } 3738 return n; 3739} 3740 3741/* Forward reference */ 3742static int process_input(ShellState *p); 3743 3744/* 3745** Read the content of file zName into memory obtained from sqlite3_malloc64() 3746** and return a pointer to the buffer. The caller is responsible for freeing 3747** the memory. 3748** 3749** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes 3750** read. 3751** 3752** For convenience, a nul-terminator byte is always appended to the data read 3753** from the file before the buffer is returned. This byte is not included in 3754** the final value of (*pnByte), if applicable. 3755** 3756** NULL is returned if any error is encountered. The final value of *pnByte 3757** is undefined in this case. 3758*/ 3759static char *readFile(const char *zName, int *pnByte){ 3760 FILE *in = fopen(zName, "rb"); 3761 long nIn; 3762 size_t nRead; 3763 char *pBuf; 3764 if( in==0 ) return 0; 3765 fseek(in, 0, SEEK_END); 3766 nIn = ftell(in); 3767 rewind(in); 3768 pBuf = sqlite3_malloc64( nIn+1 ); 3769 if( pBuf==0 ){ fclose(in); return 0; } 3770 nRead = fread(pBuf, nIn, 1, in); 3771 fclose(in); 3772 if( nRead!=1 ){ 3773 sqlite3_free(pBuf); 3774 return 0; 3775 } 3776 pBuf[nIn] = 0; 3777 if( pnByte ) *pnByte = nIn; 3778 return pBuf; 3779} 3780 3781#if defined(SQLITE_ENABLE_SESSION) 3782/* 3783** Close a single OpenSession object and release all of its associated 3784** resources. 3785*/ 3786static void session_close(OpenSession *pSession){ 3787 int i; 3788 sqlite3session_delete(pSession->p); 3789 sqlite3_free(pSession->zName); 3790 for(i=0; i<pSession->nFilter; i++){ 3791 sqlite3_free(pSession->azFilter[i]); 3792 } 3793 sqlite3_free(pSession->azFilter); 3794 memset(pSession, 0, sizeof(OpenSession)); 3795} 3796#endif 3797 3798/* 3799** Close all OpenSession objects and release all associated resources. 3800*/ 3801#if defined(SQLITE_ENABLE_SESSION) 3802static void session_close_all(ShellState *p){ 3803 int i; 3804 for(i=0; i<p->nSession; i++){ 3805 session_close(&p->aSession[i]); 3806 } 3807 p->nSession = 0; 3808} 3809#else 3810# define session_close_all(X) 3811#endif 3812 3813/* 3814** Implementation of the xFilter function for an open session. Omit 3815** any tables named by ".session filter" but let all other table through. 3816*/ 3817#if defined(SQLITE_ENABLE_SESSION) 3818static int session_filter(void *pCtx, const char *zTab){ 3819 OpenSession *pSession = (OpenSession*)pCtx; 3820 int i; 3821 for(i=0; i<pSession->nFilter; i++){ 3822 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; 3823 } 3824 return 1; 3825} 3826#endif 3827 3828/* 3829** Try to deduce the type of file for zName based on its content. Return 3830** one of the SHELL_OPEN_* constants. 3831** 3832** If the file does not exist or is empty but its name looks like a ZIP 3833** archive and the dfltZip flag is true, then assume it is a ZIP archive. 3834** Otherwise, assume an ordinary database regardless of the filename if 3835** the type cannot be determined from content. 3836*/ 3837int deduceDatabaseType(const char *zName, int dfltZip){ 3838 FILE *f = fopen(zName, "rb"); 3839 size_t n; 3840 int rc = SHELL_OPEN_UNSPEC; 3841 char zBuf[100]; 3842 if( f==0 ){ 3843 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 3844 return SHELL_OPEN_ZIPFILE; 3845 }else{ 3846 return SHELL_OPEN_NORMAL; 3847 } 3848 } 3849 n = fread(zBuf, 16, 1, f); 3850 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ 3851 fclose(f); 3852 return SHELL_OPEN_NORMAL; 3853 } 3854 fseek(f, -25, SEEK_END); 3855 n = fread(zBuf, 25, 1, f); 3856 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ 3857 rc = SHELL_OPEN_APPENDVFS; 3858 }else{ 3859 fseek(f, -22, SEEK_END); 3860 n = fread(zBuf, 22, 1, f); 3861 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 3862 && zBuf[3]==0x06 ){ 3863 rc = SHELL_OPEN_ZIPFILE; 3864 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 3865 rc = SHELL_OPEN_ZIPFILE; 3866 } 3867 } 3868 fclose(f); 3869 return rc; 3870} 3871 3872#ifdef SQLITE_ENABLE_DESERIALIZE 3873/* 3874** Reconstruct an in-memory database using the output from the "dbtotxt" 3875** program. Read content from the file in p->zDbFilename. If p->zDbFilename 3876** is 0, then read from standard input. 3877*/ 3878static unsigned char *readHexDb(ShellState *p, int *pnData){ 3879 unsigned char *a = 0; 3880 int nLine; 3881 int n = 0; 3882 int pgsz = 0; 3883 int iOffset = 0; 3884 int j, k; 3885 int rc; 3886 FILE *in; 3887 unsigned int x[16]; 3888 char zLine[1000]; 3889 if( p->zDbFilename ){ 3890 in = fopen(p->zDbFilename, "r"); 3891 if( in==0 ){ 3892 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename); 3893 return 0; 3894 } 3895 nLine = 0; 3896 }else{ 3897 in = p->in; 3898 nLine = p->lineno; 3899 if( in==0 ) in = stdin; 3900 } 3901 *pnData = 0; 3902 nLine++; 3903 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; 3904 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); 3905 if( rc!=2 ) goto readHexDb_error; 3906 if( n<0 ) goto readHexDb_error; 3907 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; 3908 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ 3909 a = sqlite3_malloc( n ? n : 1 ); 3910 if( a==0 ){ 3911 utf8_printf(stderr, "Out of memory!\n"); 3912 goto readHexDb_error; 3913 } 3914 memset(a, 0, n); 3915 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ 3916 utf8_printf(stderr, "invalid pagesize\n"); 3917 goto readHexDb_error; 3918 } 3919 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ 3920 rc = sscanf(zLine, "| page %d offset %d", &j, &k); 3921 if( rc==2 ){ 3922 iOffset = k; 3923 continue; 3924 } 3925 if( strncmp(zLine, "| end ", 6)==0 ){ 3926 break; 3927 } 3928 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 3929 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], 3930 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); 3931 if( rc==17 ){ 3932 k = iOffset+j; 3933 if( k+16<=n ){ 3934 int ii; 3935 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; 3936 } 3937 } 3938 } 3939 *pnData = n; 3940 if( in!=p->in ){ 3941 fclose(in); 3942 }else{ 3943 p->lineno = nLine; 3944 } 3945 return a; 3946 3947readHexDb_error: 3948 if( in!=p->in ){ 3949 fclose(in); 3950 }else{ 3951 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ 3952 nLine++; 3953 if(strncmp(zLine, "| end ", 6)==0 ) break; 3954 } 3955 p->lineno = nLine; 3956 } 3957 sqlite3_free(a); 3958 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); 3959 return 0; 3960} 3961#endif /* SQLITE_ENABLE_DESERIALIZE */ 3962 3963/* 3964** Scalar function "shell_int32". The first argument to this function 3965** must be a blob. The second a non-negative integer. This function 3966** reads and returns a 32-bit big-endian integer from byte 3967** offset (4*<arg2>) of the blob. 3968*/ 3969static void shellInt32( 3970 sqlite3_context *context, 3971 int argc, 3972 sqlite3_value **argv 3973){ 3974 const unsigned char *pBlob; 3975 int nBlob; 3976 int iInt; 3977 3978 UNUSED_PARAMETER(argc); 3979 nBlob = sqlite3_value_bytes(argv[0]); 3980 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); 3981 iInt = sqlite3_value_int(argv[1]); 3982 3983 if( iInt>=0 && (iInt+1)*4<=nBlob ){ 3984 const unsigned char *a = &pBlob[iInt*4]; 3985 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) 3986 + ((sqlite3_int64)a[1]<<16) 3987 + ((sqlite3_int64)a[2]<< 8) 3988 + ((sqlite3_int64)a[3]<< 0); 3989 sqlite3_result_int64(context, iVal); 3990 } 3991} 3992 3993/* 3994** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, 3995** using "..." with internal double-quote characters doubled. 3996*/ 3997static void shellIdQuote( 3998 sqlite3_context *context, 3999 int argc, 4000 sqlite3_value **argv 4001){ 4002 const char *zName = (const char*)sqlite3_value_text(argv[0]); 4003 UNUSED_PARAMETER(argc); 4004 if( zName ){ 4005 char *z = sqlite3_mprintf("\"%w\"", zName); 4006 sqlite3_result_text(context, z, -1, sqlite3_free); 4007 } 4008} 4009 4010/* 4011** Scalar function "shell_escape_crnl" used by the .recover command. 4012** The argument passed to this function is the output of built-in 4013** function quote(). If the first character of the input is "'", 4014** indicating that the value passed to quote() was a text value, 4015** then this function searches the input for "\n" and "\r" characters 4016** and adds a wrapper similar to the following: 4017** 4018** replace(replace(<input>, '\n', char(10), '\r', char(13)); 4019** 4020** Or, if the first character of the input is not "'", then a copy 4021** of the input is returned. 4022*/ 4023static void shellEscapeCrnl( 4024 sqlite3_context *context, 4025 int argc, 4026 sqlite3_value **argv 4027){ 4028 const char *zText = (const char*)sqlite3_value_text(argv[0]); 4029 UNUSED_PARAMETER(argc); 4030 if( zText[0]=='\'' ){ 4031 int nText = sqlite3_value_bytes(argv[0]); 4032 int i; 4033 char zBuf1[20]; 4034 char zBuf2[20]; 4035 const char *zNL = 0; 4036 const char *zCR = 0; 4037 int nCR = 0; 4038 int nNL = 0; 4039 4040 for(i=0; zText[i]; i++){ 4041 if( zNL==0 && zText[i]=='\n' ){ 4042 zNL = unused_string(zText, "\\n", "\\012", zBuf1); 4043 nNL = (int)strlen(zNL); 4044 } 4045 if( zCR==0 && zText[i]=='\r' ){ 4046 zCR = unused_string(zText, "\\r", "\\015", zBuf2); 4047 nCR = (int)strlen(zCR); 4048 } 4049 } 4050 4051 if( zNL || zCR ){ 4052 int iOut = 0; 4053 i64 nMax = (nNL > nCR) ? nNL : nCR; 4054 i64 nAlloc = nMax * nText + (nMax+64)*2; 4055 char *zOut = (char*)sqlite3_malloc64(nAlloc); 4056 if( zOut==0 ){ 4057 sqlite3_result_error_nomem(context); 4058 return; 4059 } 4060 4061 if( zNL && zCR ){ 4062 memcpy(&zOut[iOut], "replace(replace(", 16); 4063 iOut += 16; 4064 }else{ 4065 memcpy(&zOut[iOut], "replace(", 8); 4066 iOut += 8; 4067 } 4068 for(i=0; zText[i]; i++){ 4069 if( zText[i]=='\n' ){ 4070 memcpy(&zOut[iOut], zNL, nNL); 4071 iOut += nNL; 4072 }else if( zText[i]=='\r' ){ 4073 memcpy(&zOut[iOut], zCR, nCR); 4074 iOut += nCR; 4075 }else{ 4076 zOut[iOut] = zText[i]; 4077 iOut++; 4078 } 4079 } 4080 4081 if( zNL ){ 4082 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 4083 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; 4084 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; 4085 } 4086 if( zCR ){ 4087 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 4088 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; 4089 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; 4090 } 4091 4092 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); 4093 sqlite3_free(zOut); 4094 return; 4095 } 4096 } 4097 4098 sqlite3_result_value(context, argv[0]); 4099} 4100 4101/* Flags for open_db(). 4102** 4103** The default behavior of open_db() is to exit(1) if the database fails to 4104** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error 4105** but still returns without calling exit. 4106** 4107** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a 4108** ZIP archive if the file does not exist or is empty and its name matches 4109** the *.zip pattern. 4110*/ 4111#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ 4112#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ 4113 4114/* 4115** Make sure the database is open. If it is not, then open it. If 4116** the database fails to open, print an error message and exit. 4117*/ 4118static void open_db(ShellState *p, int openFlags){ 4119 if( p->db==0 ){ 4120 if( p->openMode==SHELL_OPEN_UNSPEC ){ 4121 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){ 4122 p->openMode = SHELL_OPEN_NORMAL; 4123 }else{ 4124 p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 4125 (openFlags & OPEN_DB_ZIPFILE)!=0); 4126 } 4127 } 4128 switch( p->openMode ){ 4129 case SHELL_OPEN_APPENDVFS: { 4130 sqlite3_open_v2(p->zDbFilename, &p->db, 4131 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); 4132 break; 4133 } 4134 case SHELL_OPEN_HEXDB: 4135 case SHELL_OPEN_DESERIALIZE: { 4136 sqlite3_open(0, &p->db); 4137 break; 4138 } 4139 case SHELL_OPEN_ZIPFILE: { 4140 sqlite3_open(":memory:", &p->db); 4141 break; 4142 } 4143 case SHELL_OPEN_READONLY: { 4144 sqlite3_open_v2(p->zDbFilename, &p->db, 4145 SQLITE_OPEN_READONLY|p->openFlags, 0); 4146 break; 4147 } 4148 case SHELL_OPEN_UNSPEC: 4149 case SHELL_OPEN_NORMAL: { 4150 sqlite3_open_v2(p->zDbFilename, &p->db, 4151 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); 4152 break; 4153 } 4154 } 4155 globalDb = p->db; 4156 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ 4157 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", 4158 p->zDbFilename, sqlite3_errmsg(p->db)); 4159 if( openFlags & OPEN_DB_KEEPALIVE ){ 4160 sqlite3_open(":memory:", &p->db); 4161 return; 4162 } 4163 exit(1); 4164 } 4165#ifndef SQLITE_OMIT_LOAD_EXTENSION 4166 sqlite3_enable_load_extension(p->db, 1); 4167#endif 4168 sqlite3_fileio_init(p->db, 0, 0); 4169 sqlite3_shathree_init(p->db, 0, 0); 4170 sqlite3_completion_init(p->db, 0, 0); 4171#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 4172 sqlite3_dbdata_init(p->db, 0, 0); 4173#endif 4174#ifdef SQLITE_HAVE_ZLIB 4175 sqlite3_zipfile_init(p->db, 0, 0); 4176 sqlite3_sqlar_init(p->db, 0, 0); 4177#endif 4178 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, 4179 shellAddSchemaName, 0, 0); 4180 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, 4181 shellModuleSchema, 0, 0); 4182 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, 4183 shellPutsFunc, 0, 0); 4184 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, 4185 shellEscapeCrnl, 0, 0); 4186 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, 4187 shellInt32, 0, 0); 4188 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, 4189 shellIdQuote, 0, 0); 4190#ifndef SQLITE_NOHAVE_SYSTEM 4191 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, 4192 editFunc, 0, 0); 4193 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, 4194 editFunc, 0, 0); 4195#endif 4196 if( p->openMode==SHELL_OPEN_ZIPFILE ){ 4197 char *zSql = sqlite3_mprintf( 4198 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); 4199 sqlite3_exec(p->db, zSql, 0, 0, 0); 4200 sqlite3_free(zSql); 4201 } 4202#ifdef SQLITE_ENABLE_DESERIALIZE 4203 else 4204 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ 4205 int rc; 4206 int nData = 0; 4207 unsigned char *aData; 4208 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ 4209 aData = (unsigned char*)readFile(p->zDbFilename, &nData); 4210 }else{ 4211 aData = readHexDb(p, &nData); 4212 if( aData==0 ){ 4213 return; 4214 } 4215 } 4216 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, 4217 SQLITE_DESERIALIZE_RESIZEABLE | 4218 SQLITE_DESERIALIZE_FREEONCLOSE); 4219 if( rc ){ 4220 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); 4221 } 4222 if( p->szMax>0 ){ 4223 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); 4224 } 4225 } 4226#endif 4227 } 4228} 4229 4230/* 4231** Attempt to close the databaes connection. Report errors. 4232*/ 4233void close_db(sqlite3 *db){ 4234 int rc = sqlite3_close(db); 4235 if( rc ){ 4236 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", 4237 rc, sqlite3_errmsg(db)); 4238 } 4239} 4240 4241#if HAVE_READLINE || HAVE_EDITLINE 4242/* 4243** Readline completion callbacks 4244*/ 4245static char *readline_completion_generator(const char *text, int state){ 4246 static sqlite3_stmt *pStmt = 0; 4247 char *zRet; 4248 if( state==0 ){ 4249 char *zSql; 4250 sqlite3_finalize(pStmt); 4251 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 4252 " FROM completion(%Q) ORDER BY 1", text); 4253 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 4254 sqlite3_free(zSql); 4255 } 4256 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 4257 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0)); 4258 }else{ 4259 sqlite3_finalize(pStmt); 4260 pStmt = 0; 4261 zRet = 0; 4262 } 4263 return zRet; 4264} 4265static char **readline_completion(const char *zText, int iStart, int iEnd){ 4266 rl_attempted_completion_over = 1; 4267 return rl_completion_matches(zText, readline_completion_generator); 4268} 4269 4270#elif HAVE_LINENOISE 4271/* 4272** Linenoise completion callback 4273*/ 4274static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ 4275 int nLine = strlen30(zLine); 4276 int i, iStart; 4277 sqlite3_stmt *pStmt = 0; 4278 char *zSql; 4279 char zBuf[1000]; 4280 4281 if( nLine>sizeof(zBuf)-30 ) return; 4282 if( zLine[0]=='.' || zLine[0]=='#') return; 4283 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} 4284 if( i==nLine-1 ) return; 4285 iStart = i+1; 4286 memcpy(zBuf, zLine, iStart); 4287 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 4288 " FROM completion(%Q,%Q) ORDER BY 1", 4289 &zLine[iStart], zLine); 4290 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 4291 sqlite3_free(zSql); 4292 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ 4293 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 4294 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); 4295 int nCompletion = sqlite3_column_bytes(pStmt, 0); 4296 if( iStart+nCompletion < sizeof(zBuf)-1 ){ 4297 memcpy(zBuf+iStart, zCompletion, nCompletion+1); 4298 linenoiseAddCompletion(lc, zBuf); 4299 } 4300 } 4301 sqlite3_finalize(pStmt); 4302} 4303#endif 4304 4305/* 4306** Do C-language style dequoting. 4307** 4308** \a -> alarm 4309** \b -> backspace 4310** \t -> tab 4311** \n -> newline 4312** \v -> vertical tab 4313** \f -> form feed 4314** \r -> carriage return 4315** \s -> space 4316** \" -> " 4317** \' -> ' 4318** \\ -> backslash 4319** \NNN -> ascii character NNN in octal 4320*/ 4321static void resolve_backslashes(char *z){ 4322 int i, j; 4323 char c; 4324 while( *z && *z!='\\' ) z++; 4325 for(i=j=0; (c = z[i])!=0; i++, j++){ 4326 if( c=='\\' && z[i+1]!=0 ){ 4327 c = z[++i]; 4328 if( c=='a' ){ 4329 c = '\a'; 4330 }else if( c=='b' ){ 4331 c = '\b'; 4332 }else if( c=='t' ){ 4333 c = '\t'; 4334 }else if( c=='n' ){ 4335 c = '\n'; 4336 }else if( c=='v' ){ 4337 c = '\v'; 4338 }else if( c=='f' ){ 4339 c = '\f'; 4340 }else if( c=='r' ){ 4341 c = '\r'; 4342 }else if( c=='"' ){ 4343 c = '"'; 4344 }else if( c=='\'' ){ 4345 c = '\''; 4346 }else if( c=='\\' ){ 4347 c = '\\'; 4348 }else if( c>='0' && c<='7' ){ 4349 c -= '0'; 4350 if( z[i+1]>='0' && z[i+1]<='7' ){ 4351 i++; 4352 c = (c<<3) + z[i] - '0'; 4353 if( z[i+1]>='0' && z[i+1]<='7' ){ 4354 i++; 4355 c = (c<<3) + z[i] - '0'; 4356 } 4357 } 4358 } 4359 } 4360 z[j] = c; 4361 } 4362 if( j<i ) z[j] = 0; 4363} 4364 4365/* 4366** Interpret zArg as either an integer or a boolean value. Return 1 or 0 4367** for TRUE and FALSE. Return the integer value if appropriate. 4368*/ 4369static int booleanValue(const char *zArg){ 4370 int i; 4371 if( zArg[0]=='0' && zArg[1]=='x' ){ 4372 for(i=2; hexDigitValue(zArg[i])>=0; i++){} 4373 }else{ 4374 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} 4375 } 4376 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); 4377 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ 4378 return 1; 4379 } 4380 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ 4381 return 0; 4382 } 4383 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", 4384 zArg); 4385 return 0; 4386} 4387 4388/* 4389** Set or clear a shell flag according to a boolean value. 4390*/ 4391static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ 4392 if( booleanValue(zArg) ){ 4393 ShellSetFlag(p, mFlag); 4394 }else{ 4395 ShellClearFlag(p, mFlag); 4396 } 4397} 4398 4399/* 4400** Close an output file, assuming it is not stderr or stdout 4401*/ 4402static void output_file_close(FILE *f){ 4403 if( f && f!=stdout && f!=stderr ) fclose(f); 4404} 4405 4406/* 4407** Try to open an output file. The names "stdout" and "stderr" are 4408** recognized and do the right thing. NULL is returned if the output 4409** filename is "off". 4410*/ 4411static FILE *output_file_open(const char *zFile, int bTextMode){ 4412 FILE *f; 4413 if( strcmp(zFile,"stdout")==0 ){ 4414 f = stdout; 4415 }else if( strcmp(zFile, "stderr")==0 ){ 4416 f = stderr; 4417 }else if( strcmp(zFile, "off")==0 ){ 4418 f = 0; 4419 }else{ 4420 f = fopen(zFile, bTextMode ? "w" : "wb"); 4421 if( f==0 ){ 4422 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 4423 } 4424 } 4425 return f; 4426} 4427 4428#ifndef SQLITE_OMIT_TRACE 4429/* 4430** A routine for handling output from sqlite3_trace(). 4431*/ 4432static int sql_trace_callback( 4433 unsigned mType, /* The trace type */ 4434 void *pArg, /* The ShellState pointer */ 4435 void *pP, /* Usually a pointer to sqlite_stmt */ 4436 void *pX /* Auxiliary output */ 4437){ 4438 ShellState *p = (ShellState*)pArg; 4439 sqlite3_stmt *pStmt; 4440 const char *zSql; 4441 int nSql; 4442 if( p->traceOut==0 ) return 0; 4443 if( mType==SQLITE_TRACE_CLOSE ){ 4444 utf8_printf(p->traceOut, "-- closing database connection\n"); 4445 return 0; 4446 } 4447 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ 4448 zSql = (const char*)pX; 4449 }else{ 4450 pStmt = (sqlite3_stmt*)pP; 4451 switch( p->eTraceType ){ 4452 case SHELL_TRACE_EXPANDED: { 4453 zSql = sqlite3_expanded_sql(pStmt); 4454 break; 4455 } 4456#ifdef SQLITE_ENABLE_NORMALIZE 4457 case SHELL_TRACE_NORMALIZED: { 4458 zSql = sqlite3_normalized_sql(pStmt); 4459 break; 4460 } 4461#endif 4462 default: { 4463 zSql = sqlite3_sql(pStmt); 4464 break; 4465 } 4466 } 4467 } 4468 if( zSql==0 ) return 0; 4469 nSql = strlen30(zSql); 4470 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } 4471 switch( mType ){ 4472 case SQLITE_TRACE_ROW: 4473 case SQLITE_TRACE_STMT: { 4474 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql); 4475 break; 4476 } 4477 case SQLITE_TRACE_PROFILE: { 4478 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; 4479 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec); 4480 break; 4481 } 4482 } 4483 return 0; 4484} 4485#endif 4486 4487/* 4488** A no-op routine that runs with the ".breakpoint" doc-command. This is 4489** a useful spot to set a debugger breakpoint. 4490*/ 4491static void test_breakpoint(void){ 4492 static int nCall = 0; 4493 nCall++; 4494} 4495 4496/* 4497** An object used to read a CSV and other files for import. 4498*/ 4499typedef struct ImportCtx ImportCtx; 4500struct ImportCtx { 4501 const char *zFile; /* Name of the input file */ 4502 FILE *in; /* Read the CSV text from this input stream */ 4503 char *z; /* Accumulated text for a field */ 4504 int n; /* Number of bytes in z */ 4505 int nAlloc; /* Space allocated for z[] */ 4506 int nLine; /* Current line number */ 4507 int bNotFirst; /* True if one or more bytes already read */ 4508 int cTerm; /* Character that terminated the most recent field */ 4509 int cColSep; /* The column separator character. (Usually ",") */ 4510 int cRowSep; /* The row separator character. (Usually "\n") */ 4511}; 4512 4513/* Append a single byte to z[] */ 4514static void import_append_char(ImportCtx *p, int c){ 4515 if( p->n+1>=p->nAlloc ){ 4516 p->nAlloc += p->nAlloc + 100; 4517 p->z = sqlite3_realloc64(p->z, p->nAlloc); 4518 if( p->z==0 ) shell_out_of_memory(); 4519 } 4520 p->z[p->n++] = (char)c; 4521} 4522 4523/* Read a single field of CSV text. Compatible with rfc4180 and extended 4524** with the option of having a separator other than ",". 4525** 4526** + Input comes from p->in. 4527** + Store results in p->z of length p->n. Space to hold p->z comes 4528** from sqlite3_malloc64(). 4529** + Use p->cSep as the column separator. The default is ",". 4530** + Use p->rSep as the row separator. The default is "\n". 4531** + Keep track of the line number in p->nLine. 4532** + Store the character that terminates the field in p->cTerm. Store 4533** EOF on end-of-file. 4534** + Report syntax errors on stderr 4535*/ 4536static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ 4537 int c; 4538 int cSep = p->cColSep; 4539 int rSep = p->cRowSep; 4540 p->n = 0; 4541 c = fgetc(p->in); 4542 if( c==EOF || seenInterrupt ){ 4543 p->cTerm = EOF; 4544 return 0; 4545 } 4546 if( c=='"' ){ 4547 int pc, ppc; 4548 int startLine = p->nLine; 4549 int cQuote = c; 4550 pc = ppc = 0; 4551 while( 1 ){ 4552 c = fgetc(p->in); 4553 if( c==rSep ) p->nLine++; 4554 if( c==cQuote ){ 4555 if( pc==cQuote ){ 4556 pc = 0; 4557 continue; 4558 } 4559 } 4560 if( (c==cSep && pc==cQuote) 4561 || (c==rSep && pc==cQuote) 4562 || (c==rSep && pc=='\r' && ppc==cQuote) 4563 || (c==EOF && pc==cQuote) 4564 ){ 4565 do{ p->n--; }while( p->z[p->n]!=cQuote ); 4566 p->cTerm = c; 4567 break; 4568 } 4569 if( pc==cQuote && c!='\r' ){ 4570 utf8_printf(stderr, "%s:%d: unescaped %c character\n", 4571 p->zFile, p->nLine, cQuote); 4572 } 4573 if( c==EOF ){ 4574 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", 4575 p->zFile, startLine, cQuote); 4576 p->cTerm = c; 4577 break; 4578 } 4579 import_append_char(p, c); 4580 ppc = pc; 4581 pc = c; 4582 } 4583 }else{ 4584 /* If this is the first field being parsed and it begins with the 4585 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ 4586 if( (c&0xff)==0xef && p->bNotFirst==0 ){ 4587 import_append_char(p, c); 4588 c = fgetc(p->in); 4589 if( (c&0xff)==0xbb ){ 4590 import_append_char(p, c); 4591 c = fgetc(p->in); 4592 if( (c&0xff)==0xbf ){ 4593 p->bNotFirst = 1; 4594 p->n = 0; 4595 return csv_read_one_field(p); 4596 } 4597 } 4598 } 4599 while( c!=EOF && c!=cSep && c!=rSep ){ 4600 import_append_char(p, c); 4601 c = fgetc(p->in); 4602 } 4603 if( c==rSep ){ 4604 p->nLine++; 4605 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; 4606 } 4607 p->cTerm = c; 4608 } 4609 if( p->z ) p->z[p->n] = 0; 4610 p->bNotFirst = 1; 4611 return p->z; 4612} 4613 4614/* Read a single field of ASCII delimited text. 4615** 4616** + Input comes from p->in. 4617** + Store results in p->z of length p->n. Space to hold p->z comes 4618** from sqlite3_malloc64(). 4619** + Use p->cSep as the column separator. The default is "\x1F". 4620** + Use p->rSep as the row separator. The default is "\x1E". 4621** + Keep track of the row number in p->nLine. 4622** + Store the character that terminates the field in p->cTerm. Store 4623** EOF on end-of-file. 4624** + Report syntax errors on stderr 4625*/ 4626static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ 4627 int c; 4628 int cSep = p->cColSep; 4629 int rSep = p->cRowSep; 4630 p->n = 0; 4631 c = fgetc(p->in); 4632 if( c==EOF || seenInterrupt ){ 4633 p->cTerm = EOF; 4634 return 0; 4635 } 4636 while( c!=EOF && c!=cSep && c!=rSep ){ 4637 import_append_char(p, c); 4638 c = fgetc(p->in); 4639 } 4640 if( c==rSep ){ 4641 p->nLine++; 4642 } 4643 p->cTerm = c; 4644 if( p->z ) p->z[p->n] = 0; 4645 return p->z; 4646} 4647 4648/* 4649** Try to transfer data for table zTable. If an error is seen while 4650** moving forward, try to go backwards. The backwards movement won't 4651** work for WITHOUT ROWID tables. 4652*/ 4653static void tryToCloneData( 4654 ShellState *p, 4655 sqlite3 *newDb, 4656 const char *zTable 4657){ 4658 sqlite3_stmt *pQuery = 0; 4659 sqlite3_stmt *pInsert = 0; 4660 char *zQuery = 0; 4661 char *zInsert = 0; 4662 int rc; 4663 int i, j, n; 4664 int nTable = strlen30(zTable); 4665 int k = 0; 4666 int cnt = 0; 4667 const int spinRate = 10000; 4668 4669 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); 4670 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 4671 if( rc ){ 4672 utf8_printf(stderr, "Error %d: %s on [%s]\n", 4673 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 4674 zQuery); 4675 goto end_data_xfer; 4676 } 4677 n = sqlite3_column_count(pQuery); 4678 zInsert = sqlite3_malloc64(200 + nTable + n*3); 4679 if( zInsert==0 ) shell_out_of_memory(); 4680 sqlite3_snprintf(200+nTable,zInsert, 4681 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); 4682 i = strlen30(zInsert); 4683 for(j=1; j<n; j++){ 4684 memcpy(zInsert+i, ",?", 2); 4685 i += 2; 4686 } 4687 memcpy(zInsert+i, ");", 3); 4688 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); 4689 if( rc ){ 4690 utf8_printf(stderr, "Error %d: %s on [%s]\n", 4691 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), 4692 zQuery); 4693 goto end_data_xfer; 4694 } 4695 for(k=0; k<2; k++){ 4696 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 4697 for(i=0; i<n; i++){ 4698 switch( sqlite3_column_type(pQuery, i) ){ 4699 case SQLITE_NULL: { 4700 sqlite3_bind_null(pInsert, i+1); 4701 break; 4702 } 4703 case SQLITE_INTEGER: { 4704 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); 4705 break; 4706 } 4707 case SQLITE_FLOAT: { 4708 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); 4709 break; 4710 } 4711 case SQLITE_TEXT: { 4712 sqlite3_bind_text(pInsert, i+1, 4713 (const char*)sqlite3_column_text(pQuery,i), 4714 -1, SQLITE_STATIC); 4715 break; 4716 } 4717 case SQLITE_BLOB: { 4718 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), 4719 sqlite3_column_bytes(pQuery,i), 4720 SQLITE_STATIC); 4721 break; 4722 } 4723 } 4724 } /* End for */ 4725 rc = sqlite3_step(pInsert); 4726 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 4727 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), 4728 sqlite3_errmsg(newDb)); 4729 } 4730 sqlite3_reset(pInsert); 4731 cnt++; 4732 if( (cnt%spinRate)==0 ){ 4733 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); 4734 fflush(stdout); 4735 } 4736 } /* End while */ 4737 if( rc==SQLITE_DONE ) break; 4738 sqlite3_finalize(pQuery); 4739 sqlite3_free(zQuery); 4740 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", 4741 zTable); 4742 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 4743 if( rc ){ 4744 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); 4745 break; 4746 } 4747 } /* End for(k=0...) */ 4748 4749end_data_xfer: 4750 sqlite3_finalize(pQuery); 4751 sqlite3_finalize(pInsert); 4752 sqlite3_free(zQuery); 4753 sqlite3_free(zInsert); 4754} 4755 4756 4757/* 4758** Try to transfer all rows of the schema that match zWhere. For 4759** each row, invoke xForEach() on the object defined by that row. 4760** If an error is encountered while moving forward through the 4761** sqlite_master table, try again moving backwards. 4762*/ 4763static void tryToCloneSchema( 4764 ShellState *p, 4765 sqlite3 *newDb, 4766 const char *zWhere, 4767 void (*xForEach)(ShellState*,sqlite3*,const char*) 4768){ 4769 sqlite3_stmt *pQuery = 0; 4770 char *zQuery = 0; 4771 int rc; 4772 const unsigned char *zName; 4773 const unsigned char *zSql; 4774 char *zErrMsg = 0; 4775 4776 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" 4777 " WHERE %s", zWhere); 4778 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 4779 if( rc ){ 4780 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 4781 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 4782 zQuery); 4783 goto end_schema_xfer; 4784 } 4785 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 4786 zName = sqlite3_column_text(pQuery, 0); 4787 zSql = sqlite3_column_text(pQuery, 1); 4788 printf("%s... ", zName); fflush(stdout); 4789 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 4790 if( zErrMsg ){ 4791 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 4792 sqlite3_free(zErrMsg); 4793 zErrMsg = 0; 4794 } 4795 if( xForEach ){ 4796 xForEach(p, newDb, (const char*)zName); 4797 } 4798 printf("done\n"); 4799 } 4800 if( rc!=SQLITE_DONE ){ 4801 sqlite3_finalize(pQuery); 4802 sqlite3_free(zQuery); 4803 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" 4804 " WHERE %s ORDER BY rowid DESC", zWhere); 4805 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 4806 if( rc ){ 4807 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 4808 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 4809 zQuery); 4810 goto end_schema_xfer; 4811 } 4812 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 4813 zName = sqlite3_column_text(pQuery, 0); 4814 zSql = sqlite3_column_text(pQuery, 1); 4815 printf("%s... ", zName); fflush(stdout); 4816 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 4817 if( zErrMsg ){ 4818 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 4819 sqlite3_free(zErrMsg); 4820 zErrMsg = 0; 4821 } 4822 if( xForEach ){ 4823 xForEach(p, newDb, (const char*)zName); 4824 } 4825 printf("done\n"); 4826 } 4827 } 4828end_schema_xfer: 4829 sqlite3_finalize(pQuery); 4830 sqlite3_free(zQuery); 4831} 4832 4833/* 4834** Open a new database file named "zNewDb". Try to recover as much information 4835** as possible out of the main database (which might be corrupt) and write it 4836** into zNewDb. 4837*/ 4838static void tryToClone(ShellState *p, const char *zNewDb){ 4839 int rc; 4840 sqlite3 *newDb = 0; 4841 if( access(zNewDb,0)==0 ){ 4842 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); 4843 return; 4844 } 4845 rc = sqlite3_open(zNewDb, &newDb); 4846 if( rc ){ 4847 utf8_printf(stderr, "Cannot create output database: %s\n", 4848 sqlite3_errmsg(newDb)); 4849 }else{ 4850 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); 4851 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); 4852 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); 4853 tryToCloneSchema(p, newDb, "type!='table'", 0); 4854 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); 4855 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 4856 } 4857 close_db(newDb); 4858} 4859 4860/* 4861** Change the output file back to stdout. 4862** 4863** If the p->doXdgOpen flag is set, that means the output was being 4864** redirected to a temporary file named by p->zTempFile. In that case, 4865** launch start/open/xdg-open on that temporary file. 4866*/ 4867static void output_reset(ShellState *p){ 4868 if( p->outfile[0]=='|' ){ 4869#ifndef SQLITE_OMIT_POPEN 4870 pclose(p->out); 4871#endif 4872 }else{ 4873 output_file_close(p->out); 4874#ifndef SQLITE_NOHAVE_SYSTEM 4875 if( p->doXdgOpen ){ 4876 const char *zXdgOpenCmd = 4877#if defined(_WIN32) 4878 "start"; 4879#elif defined(__APPLE__) 4880 "open"; 4881#else 4882 "xdg-open"; 4883#endif 4884 char *zCmd; 4885 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); 4886 if( system(zCmd) ){ 4887 utf8_printf(stderr, "Failed: [%s]\n", zCmd); 4888 } 4889 sqlite3_free(zCmd); 4890 outputModePop(p); 4891 p->doXdgOpen = 0; 4892 sqlite3_sleep(100); 4893 } 4894#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 4895 } 4896 p->outfile[0] = 0; 4897 p->out = stdout; 4898} 4899 4900/* 4901** Run an SQL command and return the single integer result. 4902*/ 4903static int db_int(ShellState *p, const char *zSql){ 4904 sqlite3_stmt *pStmt; 4905 int res = 0; 4906 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 4907 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 4908 res = sqlite3_column_int(pStmt,0); 4909 } 4910 sqlite3_finalize(pStmt); 4911 return res; 4912} 4913 4914/* 4915** Convert a 2-byte or 4-byte big-endian integer into a native integer 4916*/ 4917static unsigned int get2byteInt(unsigned char *a){ 4918 return (a[0]<<8) + a[1]; 4919} 4920static unsigned int get4byteInt(unsigned char *a){ 4921 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; 4922} 4923 4924/* 4925** Implementation of the ".info" command. 4926** 4927** Return 1 on error, 2 to exit, and 0 otherwise. 4928*/ 4929static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ 4930 static const struct { const char *zName; int ofst; } aField[] = { 4931 { "file change counter:", 24 }, 4932 { "database page count:", 28 }, 4933 { "freelist page count:", 36 }, 4934 { "schema cookie:", 40 }, 4935 { "schema format:", 44 }, 4936 { "default cache size:", 48 }, 4937 { "autovacuum top root:", 52 }, 4938 { "incremental vacuum:", 64 }, 4939 { "text encoding:", 56 }, 4940 { "user version:", 60 }, 4941 { "application id:", 68 }, 4942 { "software version:", 96 }, 4943 }; 4944 static const struct { const char *zName; const char *zSql; } aQuery[] = { 4945 { "number of tables:", 4946 "SELECT count(*) FROM %s WHERE type='table'" }, 4947 { "number of indexes:", 4948 "SELECT count(*) FROM %s WHERE type='index'" }, 4949 { "number of triggers:", 4950 "SELECT count(*) FROM %s WHERE type='trigger'" }, 4951 { "number of views:", 4952 "SELECT count(*) FROM %s WHERE type='view'" }, 4953 { "schema size:", 4954 "SELECT total(length(sql)) FROM %s" }, 4955 }; 4956 int i, rc; 4957 unsigned iDataVersion; 4958 char *zSchemaTab; 4959 char *zDb = nArg>=2 ? azArg[1] : "main"; 4960 sqlite3_stmt *pStmt = 0; 4961 unsigned char aHdr[100]; 4962 open_db(p, 0); 4963 if( p->db==0 ) return 1; 4964 rc = sqlite3_prepare_v2(p->db, 4965 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", 4966 -1, &pStmt, 0); 4967 if( rc ){ 4968 if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){ 4969 utf8_printf(stderr, "the \".dbinfo\" command requires the " 4970 "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n"); 4971 }else{ 4972 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); 4973 } 4974 sqlite3_finalize(pStmt); 4975 return 1; 4976 } 4977 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); 4978 if( sqlite3_step(pStmt)==SQLITE_ROW 4979 && sqlite3_column_bytes(pStmt,0)>100 4980 ){ 4981 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); 4982 sqlite3_finalize(pStmt); 4983 }else{ 4984 raw_printf(stderr, "unable to read database header\n"); 4985 sqlite3_finalize(pStmt); 4986 return 1; 4987 } 4988 i = get2byteInt(aHdr+16); 4989 if( i==1 ) i = 65536; 4990 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); 4991 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); 4992 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); 4993 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); 4994 for(i=0; i<ArraySize(aField); i++){ 4995 int ofst = aField[i].ofst; 4996 unsigned int val = get4byteInt(aHdr + ofst); 4997 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); 4998 switch( ofst ){ 4999 case 56: { 5000 if( val==1 ) raw_printf(p->out, " (utf8)"); 5001 if( val==2 ) raw_printf(p->out, " (utf16le)"); 5002 if( val==3 ) raw_printf(p->out, " (utf16be)"); 5003 } 5004 } 5005 raw_printf(p->out, "\n"); 5006 } 5007 if( zDb==0 ){ 5008 zSchemaTab = sqlite3_mprintf("main.sqlite_master"); 5009 }else if( strcmp(zDb,"temp")==0 ){ 5010 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); 5011 }else{ 5012 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); 5013 } 5014 for(i=0; i<ArraySize(aQuery); i++){ 5015 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); 5016 int val = db_int(p, zSql); 5017 sqlite3_free(zSql); 5018 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); 5019 } 5020 sqlite3_free(zSchemaTab); 5021 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); 5022 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); 5023 return 0; 5024} 5025 5026/* 5027** Print the current sqlite3_errmsg() value to stderr and return 1. 5028*/ 5029static int shellDatabaseError(sqlite3 *db){ 5030 const char *zErr = sqlite3_errmsg(db); 5031 utf8_printf(stderr, "Error: %s\n", zErr); 5032 return 1; 5033} 5034 5035/* 5036** Compare the pattern in zGlob[] against the text in z[]. Return TRUE 5037** if they match and FALSE (0) if they do not match. 5038** 5039** Globbing rules: 5040** 5041** '*' Matches any sequence of zero or more characters. 5042** 5043** '?' Matches exactly one character. 5044** 5045** [...] Matches one character from the enclosed list of 5046** characters. 5047** 5048** [^...] Matches one character not in the enclosed list. 5049** 5050** '#' Matches any sequence of one or more digits with an 5051** optional + or - sign in front 5052** 5053** ' ' Any span of whitespace matches any other span of 5054** whitespace. 5055** 5056** Extra whitespace at the end of z[] is ignored. 5057*/ 5058static int testcase_glob(const char *zGlob, const char *z){ 5059 int c, c2; 5060 int invert; 5061 int seen; 5062 5063 while( (c = (*(zGlob++)))!=0 ){ 5064 if( IsSpace(c) ){ 5065 if( !IsSpace(*z) ) return 0; 5066 while( IsSpace(*zGlob) ) zGlob++; 5067 while( IsSpace(*z) ) z++; 5068 }else if( c=='*' ){ 5069 while( (c=(*(zGlob++))) == '*' || c=='?' ){ 5070 if( c=='?' && (*(z++))==0 ) return 0; 5071 } 5072 if( c==0 ){ 5073 return 1; 5074 }else if( c=='[' ){ 5075 while( *z && testcase_glob(zGlob-1,z)==0 ){ 5076 z++; 5077 } 5078 return (*z)!=0; 5079 } 5080 while( (c2 = (*(z++)))!=0 ){ 5081 while( c2!=c ){ 5082 c2 = *(z++); 5083 if( c2==0 ) return 0; 5084 } 5085 if( testcase_glob(zGlob,z) ) return 1; 5086 } 5087 return 0; 5088 }else if( c=='?' ){ 5089 if( (*(z++))==0 ) return 0; 5090 }else if( c=='[' ){ 5091 int prior_c = 0; 5092 seen = 0; 5093 invert = 0; 5094 c = *(z++); 5095 if( c==0 ) return 0; 5096 c2 = *(zGlob++); 5097 if( c2=='^' ){ 5098 invert = 1; 5099 c2 = *(zGlob++); 5100 } 5101 if( c2==']' ){ 5102 if( c==']' ) seen = 1; 5103 c2 = *(zGlob++); 5104 } 5105 while( c2 && c2!=']' ){ 5106 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 5107 c2 = *(zGlob++); 5108 if( c>=prior_c && c<=c2 ) seen = 1; 5109 prior_c = 0; 5110 }else{ 5111 if( c==c2 ){ 5112 seen = 1; 5113 } 5114 prior_c = c2; 5115 } 5116 c2 = *(zGlob++); 5117 } 5118 if( c2==0 || (seen ^ invert)==0 ) return 0; 5119 }else if( c=='#' ){ 5120 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; 5121 if( !IsDigit(z[0]) ) return 0; 5122 z++; 5123 while( IsDigit(z[0]) ){ z++; } 5124 }else{ 5125 if( c!=(*(z++)) ) return 0; 5126 } 5127 } 5128 while( IsSpace(*z) ){ z++; } 5129 return *z==0; 5130} 5131 5132 5133/* 5134** Compare the string as a command-line option with either one or two 5135** initial "-" characters. 5136*/ 5137static int optionMatch(const char *zStr, const char *zOpt){ 5138 if( zStr[0]!='-' ) return 0; 5139 zStr++; 5140 if( zStr[0]=='-' ) zStr++; 5141 return strcmp(zStr, zOpt)==0; 5142} 5143 5144/* 5145** Delete a file. 5146*/ 5147int shellDeleteFile(const char *zFilename){ 5148 int rc; 5149#ifdef _WIN32 5150 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); 5151 rc = _wunlink(z); 5152 sqlite3_free(z); 5153#else 5154 rc = unlink(zFilename); 5155#endif 5156 return rc; 5157} 5158 5159/* 5160** Try to delete the temporary file (if there is one) and free the 5161** memory used to hold the name of the temp file. 5162*/ 5163static void clearTempFile(ShellState *p){ 5164 if( p->zTempFile==0 ) return; 5165 if( p->doXdgOpen ) return; 5166 if( shellDeleteFile(p->zTempFile) ) return; 5167 sqlite3_free(p->zTempFile); 5168 p->zTempFile = 0; 5169} 5170 5171/* 5172** Create a new temp file name with the given suffix. 5173*/ 5174static void newTempFile(ShellState *p, const char *zSuffix){ 5175 clearTempFile(p); 5176 sqlite3_free(p->zTempFile); 5177 p->zTempFile = 0; 5178 if( p->db ){ 5179 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); 5180 } 5181 if( p->zTempFile==0 ){ 5182 sqlite3_uint64 r; 5183 sqlite3_randomness(sizeof(r), &r); 5184 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix); 5185 }else{ 5186 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); 5187 } 5188 if( p->zTempFile==0 ){ 5189 raw_printf(stderr, "out of memory\n"); 5190 exit(1); 5191 } 5192} 5193 5194 5195/* 5196** The implementation of SQL scalar function fkey_collate_clause(), used 5197** by the ".lint fkey-indexes" command. This scalar function is always 5198** called with four arguments - the parent table name, the parent column name, 5199** the child table name and the child column name. 5200** 5201** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') 5202** 5203** If either of the named tables or columns do not exist, this function 5204** returns an empty string. An empty string is also returned if both tables 5205** and columns exist but have the same default collation sequence. Or, 5206** if both exist but the default collation sequences are different, this 5207** function returns the string " COLLATE <parent-collation>", where 5208** <parent-collation> is the default collation sequence of the parent column. 5209*/ 5210static void shellFkeyCollateClause( 5211 sqlite3_context *pCtx, 5212 int nVal, 5213 sqlite3_value **apVal 5214){ 5215 sqlite3 *db = sqlite3_context_db_handle(pCtx); 5216 const char *zParent; 5217 const char *zParentCol; 5218 const char *zParentSeq; 5219 const char *zChild; 5220 const char *zChildCol; 5221 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ 5222 int rc; 5223 5224 assert( nVal==4 ); 5225 zParent = (const char*)sqlite3_value_text(apVal[0]); 5226 zParentCol = (const char*)sqlite3_value_text(apVal[1]); 5227 zChild = (const char*)sqlite3_value_text(apVal[2]); 5228 zChildCol = (const char*)sqlite3_value_text(apVal[3]); 5229 5230 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); 5231 rc = sqlite3_table_column_metadata( 5232 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 5233 ); 5234 if( rc==SQLITE_OK ){ 5235 rc = sqlite3_table_column_metadata( 5236 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 5237 ); 5238 } 5239 5240 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ 5241 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); 5242 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); 5243 sqlite3_free(z); 5244 } 5245} 5246 5247 5248/* 5249** The implementation of dot-command ".lint fkey-indexes". 5250*/ 5251static int lintFkeyIndexes( 5252 ShellState *pState, /* Current shell tool state */ 5253 char **azArg, /* Array of arguments passed to dot command */ 5254 int nArg /* Number of entries in azArg[] */ 5255){ 5256 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ 5257 FILE *out = pState->out; /* Stream to write non-error output to */ 5258 int bVerbose = 0; /* If -verbose is present */ 5259 int bGroupByParent = 0; /* If -groupbyparent is present */ 5260 int i; /* To iterate through azArg[] */ 5261 const char *zIndent = ""; /* How much to indent CREATE INDEX by */ 5262 int rc; /* Return code */ 5263 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ 5264 5265 /* 5266 ** This SELECT statement returns one row for each foreign key constraint 5267 ** in the schema of the main database. The column values are: 5268 ** 5269 ** 0. The text of an SQL statement similar to: 5270 ** 5271 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" 5272 ** 5273 ** This SELECT is similar to the one that the foreign keys implementation 5274 ** needs to run internally on child tables. If there is an index that can 5275 ** be used to optimize this query, then it can also be used by the FK 5276 ** implementation to optimize DELETE or UPDATE statements on the parent 5277 ** table. 5278 ** 5279 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by 5280 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema 5281 ** contains an index that can be used to optimize the query. 5282 ** 5283 ** 2. Human readable text that describes the child table and columns. e.g. 5284 ** 5285 ** "child_table(child_key1, child_key2)" 5286 ** 5287 ** 3. Human readable text that describes the parent table and columns. e.g. 5288 ** 5289 ** "parent_table(parent_key1, parent_key2)" 5290 ** 5291 ** 4. A full CREATE INDEX statement for an index that could be used to 5292 ** optimize DELETE or UPDATE statements on the parent table. e.g. 5293 ** 5294 ** "CREATE INDEX child_table_child_key ON child_table(child_key)" 5295 ** 5296 ** 5. The name of the parent table. 5297 ** 5298 ** These six values are used by the C logic below to generate the report. 5299 */ 5300 const char *zSql = 5301 "SELECT " 5302 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" 5303 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " 5304 " || fkey_collate_clause(" 5305 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" 5306 ", " 5307 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" 5308 " || group_concat('*=?', ' AND ') || ')'" 5309 ", " 5310 " s.name || '(' || group_concat(f.[from], ', ') || ')'" 5311 ", " 5312 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" 5313 ", " 5314 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" 5315 " || ' ON ' || quote(s.name) || '('" 5316 " || group_concat(quote(f.[from]) ||" 5317 " fkey_collate_clause(" 5318 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" 5319 " || ');'" 5320 ", " 5321 " f.[table] " 5322 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " 5323 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " 5324 "GROUP BY s.name, f.id " 5325 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" 5326 ; 5327 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; 5328 5329 for(i=2; i<nArg; i++){ 5330 int n = strlen30(azArg[i]); 5331 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ 5332 bVerbose = 1; 5333 } 5334 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ 5335 bGroupByParent = 1; 5336 zIndent = " "; 5337 } 5338 else{ 5339 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", 5340 azArg[0], azArg[1] 5341 ); 5342 return SQLITE_ERROR; 5343 } 5344 } 5345 5346 /* Register the fkey_collate_clause() SQL function */ 5347 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, 5348 0, shellFkeyCollateClause, 0, 0 5349 ); 5350 5351 5352 if( rc==SQLITE_OK ){ 5353 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); 5354 } 5355 if( rc==SQLITE_OK ){ 5356 sqlite3_bind_int(pSql, 1, bGroupByParent); 5357 } 5358 5359 if( rc==SQLITE_OK ){ 5360 int rc2; 5361 char *zPrev = 0; 5362 while( SQLITE_ROW==sqlite3_step(pSql) ){ 5363 int res = -1; 5364 sqlite3_stmt *pExplain = 0; 5365 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); 5366 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); 5367 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); 5368 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); 5369 const char *zCI = (const char*)sqlite3_column_text(pSql, 4); 5370 const char *zParent = (const char*)sqlite3_column_text(pSql, 5); 5371 5372 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 5373 if( rc!=SQLITE_OK ) break; 5374 if( SQLITE_ROW==sqlite3_step(pExplain) ){ 5375 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); 5376 res = ( 5377 0==sqlite3_strglob(zGlob, zPlan) 5378 || 0==sqlite3_strglob(zGlobIPK, zPlan) 5379 ); 5380 } 5381 rc = sqlite3_finalize(pExplain); 5382 if( rc!=SQLITE_OK ) break; 5383 5384 if( res<0 ){ 5385 raw_printf(stderr, "Error: internal error"); 5386 break; 5387 }else{ 5388 if( bGroupByParent 5389 && (bVerbose || res==0) 5390 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) 5391 ){ 5392 raw_printf(out, "-- Parent table %s\n", zParent); 5393 sqlite3_free(zPrev); 5394 zPrev = sqlite3_mprintf("%s", zParent); 5395 } 5396 5397 if( res==0 ){ 5398 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); 5399 }else if( bVerbose ){ 5400 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", 5401 zIndent, zFrom, zTarget 5402 ); 5403 } 5404 } 5405 } 5406 sqlite3_free(zPrev); 5407 5408 if( rc!=SQLITE_OK ){ 5409 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5410 } 5411 5412 rc2 = sqlite3_finalize(pSql); 5413 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ 5414 rc = rc2; 5415 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5416 } 5417 }else{ 5418 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 5419 } 5420 5421 return rc; 5422} 5423 5424/* 5425** Implementation of ".lint" dot command. 5426*/ 5427static int lintDotCommand( 5428 ShellState *pState, /* Current shell tool state */ 5429 char **azArg, /* Array of arguments passed to dot command */ 5430 int nArg /* Number of entries in azArg[] */ 5431){ 5432 int n; 5433 n = (nArg>=2 ? strlen30(azArg[1]) : 0); 5434 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; 5435 return lintFkeyIndexes(pState, azArg, nArg); 5436 5437 usage: 5438 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); 5439 raw_printf(stderr, "Where sub-commands are:\n"); 5440 raw_printf(stderr, " fkey-indexes\n"); 5441 return SQLITE_ERROR; 5442} 5443 5444#if !defined SQLITE_OMIT_VIRTUALTABLE 5445static void shellPrepare( 5446 sqlite3 *db, 5447 int *pRc, 5448 const char *zSql, 5449 sqlite3_stmt **ppStmt 5450){ 5451 *ppStmt = 0; 5452 if( *pRc==SQLITE_OK ){ 5453 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); 5454 if( rc!=SQLITE_OK ){ 5455 raw_printf(stderr, "sql error: %s (%d)\n", 5456 sqlite3_errmsg(db), sqlite3_errcode(db) 5457 ); 5458 *pRc = rc; 5459 } 5460 } 5461} 5462 5463/* 5464** Create a prepared statement using printf-style arguments for the SQL. 5465** 5466** This routine is could be marked "static". But it is not always used, 5467** depending on compile-time options. By omitting the "static", we avoid 5468** nuisance compiler warnings about "defined but not used". 5469*/ 5470void shellPreparePrintf( 5471 sqlite3 *db, 5472 int *pRc, 5473 sqlite3_stmt **ppStmt, 5474 const char *zFmt, 5475 ... 5476){ 5477 *ppStmt = 0; 5478 if( *pRc==SQLITE_OK ){ 5479 va_list ap; 5480 char *z; 5481 va_start(ap, zFmt); 5482 z = sqlite3_vmprintf(zFmt, ap); 5483 va_end(ap); 5484 if( z==0 ){ 5485 *pRc = SQLITE_NOMEM; 5486 }else{ 5487 shellPrepare(db, pRc, z, ppStmt); 5488 sqlite3_free(z); 5489 } 5490 } 5491} 5492 5493/* Finalize the prepared statement created using shellPreparePrintf(). 5494** 5495** This routine is could be marked "static". But it is not always used, 5496** depending on compile-time options. By omitting the "static", we avoid 5497** nuisance compiler warnings about "defined but not used". 5498*/ 5499void shellFinalize( 5500 int *pRc, 5501 sqlite3_stmt *pStmt 5502){ 5503 if( pStmt ){ 5504 sqlite3 *db = sqlite3_db_handle(pStmt); 5505 int rc = sqlite3_finalize(pStmt); 5506 if( *pRc==SQLITE_OK ){ 5507 if( rc!=SQLITE_OK ){ 5508 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 5509 } 5510 *pRc = rc; 5511 } 5512 } 5513} 5514 5515/* Reset the prepared statement created using shellPreparePrintf(). 5516** 5517** This routine is could be marked "static". But it is not always used, 5518** depending on compile-time options. By omitting the "static", we avoid 5519** nuisance compiler warnings about "defined but not used". 5520*/ 5521void shellReset( 5522 int *pRc, 5523 sqlite3_stmt *pStmt 5524){ 5525 int rc = sqlite3_reset(pStmt); 5526 if( *pRc==SQLITE_OK ){ 5527 if( rc!=SQLITE_OK ){ 5528 sqlite3 *db = sqlite3_db_handle(pStmt); 5529 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 5530 } 5531 *pRc = rc; 5532 } 5533} 5534#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ 5535 5536#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 5537/****************************************************************************** 5538** The ".archive" or ".ar" command. 5539*/ 5540/* 5541** Structure representing a single ".ar" command. 5542*/ 5543typedef struct ArCommand ArCommand; 5544struct ArCommand { 5545 u8 eCmd; /* An AR_CMD_* value */ 5546 u8 bVerbose; /* True if --verbose */ 5547 u8 bZip; /* True if the archive is a ZIP */ 5548 u8 bDryRun; /* True if --dry-run */ 5549 u8 bAppend; /* True if --append */ 5550 u8 fromCmdLine; /* Run from -A instead of .archive */ 5551 int nArg; /* Number of command arguments */ 5552 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ 5553 const char *zFile; /* --file argument, or NULL */ 5554 const char *zDir; /* --directory argument, or NULL */ 5555 char **azArg; /* Array of command arguments */ 5556 ShellState *p; /* Shell state */ 5557 sqlite3 *db; /* Database containing the archive */ 5558}; 5559 5560/* 5561** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. 5562*/ 5563static int arUsage(FILE *f){ 5564 showHelp(f,"archive"); 5565 return SQLITE_ERROR; 5566} 5567 5568/* 5569** Print an error message for the .ar command to stderr and return 5570** SQLITE_ERROR. 5571*/ 5572static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ 5573 va_list ap; 5574 char *z; 5575 va_start(ap, zFmt); 5576 z = sqlite3_vmprintf(zFmt, ap); 5577 va_end(ap); 5578 utf8_printf(stderr, "Error: %s\n", z); 5579 if( pAr->fromCmdLine ){ 5580 utf8_printf(stderr, "Use \"-A\" for more help\n"); 5581 }else{ 5582 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); 5583 } 5584 sqlite3_free(z); 5585 return SQLITE_ERROR; 5586} 5587 5588/* 5589** Values for ArCommand.eCmd. 5590*/ 5591#define AR_CMD_CREATE 1 5592#define AR_CMD_UPDATE 2 5593#define AR_CMD_INSERT 3 5594#define AR_CMD_EXTRACT 4 5595#define AR_CMD_LIST 5 5596#define AR_CMD_HELP 6 5597 5598/* 5599** Other (non-command) switches. 5600*/ 5601#define AR_SWITCH_VERBOSE 7 5602#define AR_SWITCH_FILE 8 5603#define AR_SWITCH_DIRECTORY 9 5604#define AR_SWITCH_APPEND 10 5605#define AR_SWITCH_DRYRUN 11 5606 5607static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ 5608 switch( eSwitch ){ 5609 case AR_CMD_CREATE: 5610 case AR_CMD_EXTRACT: 5611 case AR_CMD_LIST: 5612 case AR_CMD_UPDATE: 5613 case AR_CMD_INSERT: 5614 case AR_CMD_HELP: 5615 if( pAr->eCmd ){ 5616 return arErrorMsg(pAr, "multiple command options"); 5617 } 5618 pAr->eCmd = eSwitch; 5619 break; 5620 5621 case AR_SWITCH_DRYRUN: 5622 pAr->bDryRun = 1; 5623 break; 5624 case AR_SWITCH_VERBOSE: 5625 pAr->bVerbose = 1; 5626 break; 5627 case AR_SWITCH_APPEND: 5628 pAr->bAppend = 1; 5629 /* Fall thru into --file */ 5630 case AR_SWITCH_FILE: 5631 pAr->zFile = zArg; 5632 break; 5633 case AR_SWITCH_DIRECTORY: 5634 pAr->zDir = zArg; 5635 break; 5636 } 5637 5638 return SQLITE_OK; 5639} 5640 5641/* 5642** Parse the command line for an ".ar" command. The results are written into 5643** structure (*pAr). SQLITE_OK is returned if the command line is parsed 5644** successfully, otherwise an error message is written to stderr and 5645** SQLITE_ERROR returned. 5646*/ 5647static int arParseCommand( 5648 char **azArg, /* Array of arguments passed to dot command */ 5649 int nArg, /* Number of entries in azArg[] */ 5650 ArCommand *pAr /* Populate this object */ 5651){ 5652 struct ArSwitch { 5653 const char *zLong; 5654 char cShort; 5655 u8 eSwitch; 5656 u8 bArg; 5657 } aSwitch[] = { 5658 { "create", 'c', AR_CMD_CREATE, 0 }, 5659 { "extract", 'x', AR_CMD_EXTRACT, 0 }, 5660 { "insert", 'i', AR_CMD_INSERT, 0 }, 5661 { "list", 't', AR_CMD_LIST, 0 }, 5662 { "update", 'u', AR_CMD_UPDATE, 0 }, 5663 { "help", 'h', AR_CMD_HELP, 0 }, 5664 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, 5665 { "file", 'f', AR_SWITCH_FILE, 1 }, 5666 { "append", 'a', AR_SWITCH_APPEND, 1 }, 5667 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, 5668 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, 5669 }; 5670 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); 5671 struct ArSwitch *pEnd = &aSwitch[nSwitch]; 5672 5673 if( nArg<=1 ){ 5674 utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); 5675 return arUsage(stderr); 5676 }else{ 5677 char *z = azArg[1]; 5678 if( z[0]!='-' ){ 5679 /* Traditional style [tar] invocation */ 5680 int i; 5681 int iArg = 2; 5682 for(i=0; z[i]; i++){ 5683 const char *zArg = 0; 5684 struct ArSwitch *pOpt; 5685 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 5686 if( z[i]==pOpt->cShort ) break; 5687 } 5688 if( pOpt==pEnd ){ 5689 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 5690 } 5691 if( pOpt->bArg ){ 5692 if( iArg>=nArg ){ 5693 return arErrorMsg(pAr, "option requires an argument: %c",z[i]); 5694 } 5695 zArg = azArg[iArg++]; 5696 } 5697 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 5698 } 5699 pAr->nArg = nArg-iArg; 5700 if( pAr->nArg>0 ){ 5701 pAr->azArg = &azArg[iArg]; 5702 } 5703 }else{ 5704 /* Non-traditional invocation */ 5705 int iArg; 5706 for(iArg=1; iArg<nArg; iArg++){ 5707 int n; 5708 z = azArg[iArg]; 5709 if( z[0]!='-' ){ 5710 /* All remaining command line words are command arguments. */ 5711 pAr->azArg = &azArg[iArg]; 5712 pAr->nArg = nArg-iArg; 5713 break; 5714 } 5715 n = strlen30(z); 5716 5717 if( z[1]!='-' ){ 5718 int i; 5719 /* One or more short options */ 5720 for(i=1; i<n; i++){ 5721 const char *zArg = 0; 5722 struct ArSwitch *pOpt; 5723 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 5724 if( z[i]==pOpt->cShort ) break; 5725 } 5726 if( pOpt==pEnd ){ 5727 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 5728 } 5729 if( pOpt->bArg ){ 5730 if( i<(n-1) ){ 5731 zArg = &z[i+1]; 5732 i = n; 5733 }else{ 5734 if( iArg>=(nArg-1) ){ 5735 return arErrorMsg(pAr, "option requires an argument: %c", 5736 z[i]); 5737 } 5738 zArg = azArg[++iArg]; 5739 } 5740 } 5741 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 5742 } 5743 }else if( z[2]=='\0' ){ 5744 /* A -- option, indicating that all remaining command line words 5745 ** are command arguments. */ 5746 pAr->azArg = &azArg[iArg+1]; 5747 pAr->nArg = nArg-iArg-1; 5748 break; 5749 }else{ 5750 /* A long option */ 5751 const char *zArg = 0; /* Argument for option, if any */ 5752 struct ArSwitch *pMatch = 0; /* Matching option */ 5753 struct ArSwitch *pOpt; /* Iterator */ 5754 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 5755 const char *zLong = pOpt->zLong; 5756 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ 5757 if( pMatch ){ 5758 return arErrorMsg(pAr, "ambiguous option: %s",z); 5759 }else{ 5760 pMatch = pOpt; 5761 } 5762 } 5763 } 5764 5765 if( pMatch==0 ){ 5766 return arErrorMsg(pAr, "unrecognized option: %s", z); 5767 } 5768 if( pMatch->bArg ){ 5769 if( iArg>=(nArg-1) ){ 5770 return arErrorMsg(pAr, "option requires an argument: %s", z); 5771 } 5772 zArg = azArg[++iArg]; 5773 } 5774 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; 5775 } 5776 } 5777 } 5778 } 5779 5780 return SQLITE_OK; 5781} 5782 5783/* 5784** This function assumes that all arguments within the ArCommand.azArg[] 5785** array refer to archive members, as for the --extract or --list commands. 5786** It checks that each of them are present. If any specified file is not 5787** present in the archive, an error is printed to stderr and an error 5788** code returned. Otherwise, if all specified arguments are present in 5789** the archive, SQLITE_OK is returned. 5790** 5791** This function strips any trailing '/' characters from each argument. 5792** This is consistent with the way the [tar] command seems to work on 5793** Linux. 5794*/ 5795static int arCheckEntries(ArCommand *pAr){ 5796 int rc = SQLITE_OK; 5797 if( pAr->nArg ){ 5798 int i, j; 5799 sqlite3_stmt *pTest = 0; 5800 5801 shellPreparePrintf(pAr->db, &rc, &pTest, 5802 "SELECT name FROM %s WHERE name=$name", 5803 pAr->zSrcTable 5804 ); 5805 j = sqlite3_bind_parameter_index(pTest, "$name"); 5806 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 5807 char *z = pAr->azArg[i]; 5808 int n = strlen30(z); 5809 int bOk = 0; 5810 while( n>0 && z[n-1]=='/' ) n--; 5811 z[n] = '\0'; 5812 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); 5813 if( SQLITE_ROW==sqlite3_step(pTest) ){ 5814 bOk = 1; 5815 } 5816 shellReset(&rc, pTest); 5817 if( rc==SQLITE_OK && bOk==0 ){ 5818 utf8_printf(stderr, "not found in archive: %s\n", z); 5819 rc = SQLITE_ERROR; 5820 } 5821 } 5822 shellFinalize(&rc, pTest); 5823 } 5824 return rc; 5825} 5826 5827/* 5828** Format a WHERE clause that can be used against the "sqlar" table to 5829** identify all archive members that match the command arguments held 5830** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. 5831** The caller is responsible for eventually calling sqlite3_free() on 5832** any non-NULL (*pzWhere) value. 5833*/ 5834static void arWhereClause( 5835 int *pRc, 5836 ArCommand *pAr, 5837 char **pzWhere /* OUT: New WHERE clause */ 5838){ 5839 char *zWhere = 0; 5840 if( *pRc==SQLITE_OK ){ 5841 if( pAr->nArg==0 ){ 5842 zWhere = sqlite3_mprintf("1"); 5843 }else{ 5844 int i; 5845 const char *zSep = ""; 5846 for(i=0; i<pAr->nArg; i++){ 5847 const char *z = pAr->azArg[i]; 5848 zWhere = sqlite3_mprintf( 5849 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 5850 zWhere, zSep, z, strlen30(z)+1, z 5851 ); 5852 if( zWhere==0 ){ 5853 *pRc = SQLITE_NOMEM; 5854 break; 5855 } 5856 zSep = " OR "; 5857 } 5858 } 5859 } 5860 *pzWhere = zWhere; 5861} 5862 5863/* 5864** Implementation of .ar "lisT" command. 5865*/ 5866static int arListCommand(ArCommand *pAr){ 5867 const char *zSql = "SELECT %s FROM %s WHERE %s"; 5868 const char *azCols[] = { 5869 "name", 5870 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" 5871 }; 5872 5873 char *zWhere = 0; 5874 sqlite3_stmt *pSql = 0; 5875 int rc; 5876 5877 rc = arCheckEntries(pAr); 5878 arWhereClause(&rc, pAr, &zWhere); 5879 5880 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], 5881 pAr->zSrcTable, zWhere); 5882 if( pAr->bDryRun ){ 5883 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 5884 }else{ 5885 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 5886 if( pAr->bVerbose ){ 5887 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", 5888 sqlite3_column_text(pSql, 0), 5889 sqlite3_column_int(pSql, 1), 5890 sqlite3_column_text(pSql, 2), 5891 sqlite3_column_text(pSql, 3) 5892 ); 5893 }else{ 5894 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 5895 } 5896 } 5897 } 5898 shellFinalize(&rc, pSql); 5899 sqlite3_free(zWhere); 5900 return rc; 5901} 5902 5903 5904/* 5905** Implementation of .ar "eXtract" command. 5906*/ 5907static int arExtractCommand(ArCommand *pAr){ 5908 const char *zSql1 = 5909 "SELECT " 5910 " ($dir || name)," 5911 " writefile(($dir || name), %s, mode, mtime) " 5912 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" 5913 " AND name NOT GLOB '*..[/\\]*'"; 5914 5915 const char *azExtraArg[] = { 5916 "sqlar_uncompress(data, sz)", 5917 "data" 5918 }; 5919 5920 sqlite3_stmt *pSql = 0; 5921 int rc = SQLITE_OK; 5922 char *zDir = 0; 5923 char *zWhere = 0; 5924 int i, j; 5925 5926 /* If arguments are specified, check that they actually exist within 5927 ** the archive before proceeding. And formulate a WHERE clause to 5928 ** match them. */ 5929 rc = arCheckEntries(pAr); 5930 arWhereClause(&rc, pAr, &zWhere); 5931 5932 if( rc==SQLITE_OK ){ 5933 if( pAr->zDir ){ 5934 zDir = sqlite3_mprintf("%s/", pAr->zDir); 5935 }else{ 5936 zDir = sqlite3_mprintf(""); 5937 } 5938 if( zDir==0 ) rc = SQLITE_NOMEM; 5939 } 5940 5941 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 5942 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere 5943 ); 5944 5945 if( rc==SQLITE_OK ){ 5946 j = sqlite3_bind_parameter_index(pSql, "$dir"); 5947 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); 5948 5949 /* Run the SELECT statement twice. The first time, writefile() is called 5950 ** for all archive members that should be extracted. The second time, 5951 ** only for the directories. This is because the timestamps for 5952 ** extracted directories must be reset after they are populated (as 5953 ** populating them changes the timestamp). */ 5954 for(i=0; i<2; i++){ 5955 j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); 5956 sqlite3_bind_int(pSql, j, i); 5957 if( pAr->bDryRun ){ 5958 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 5959 }else{ 5960 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 5961 if( i==0 && pAr->bVerbose ){ 5962 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 5963 } 5964 } 5965 } 5966 shellReset(&rc, pSql); 5967 } 5968 shellFinalize(&rc, pSql); 5969 } 5970 5971 sqlite3_free(zDir); 5972 sqlite3_free(zWhere); 5973 return rc; 5974} 5975 5976/* 5977** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. 5978*/ 5979static int arExecSql(ArCommand *pAr, const char *zSql){ 5980 int rc; 5981 if( pAr->bDryRun ){ 5982 utf8_printf(pAr->p->out, "%s\n", zSql); 5983 rc = SQLITE_OK; 5984 }else{ 5985 char *zErr = 0; 5986 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 5987 if( zErr ){ 5988 utf8_printf(stdout, "ERROR: %s\n", zErr); 5989 sqlite3_free(zErr); 5990 } 5991 } 5992 return rc; 5993} 5994 5995 5996/* 5997** Implementation of .ar "create", "insert", and "update" commands. 5998** 5999** create -> Create a new SQL archive 6000** insert -> Insert or reinsert all files listed 6001** update -> Insert files that have changed or that were not 6002** previously in the archive 6003** 6004** Create the "sqlar" table in the database if it does not already exist. 6005** Then add each file in the azFile[] array to the archive. Directories 6006** are added recursively. If argument bVerbose is non-zero, a message is 6007** printed on stdout for each file archived. 6008** 6009** The create command is the same as update, except that it drops 6010** any existing "sqlar" table before beginning. The "insert" command 6011** always overwrites every file named on the command-line, where as 6012** "update" only overwrites if the size or mtime or mode has changed. 6013*/ 6014static int arCreateOrUpdateCommand( 6015 ArCommand *pAr, /* Command arguments and options */ 6016 int bUpdate, /* true for a --create. */ 6017 int bOnlyIfChanged /* Only update if file has changed */ 6018){ 6019 const char *zCreate = 6020 "CREATE TABLE IF NOT EXISTS sqlar(\n" 6021 " name TEXT PRIMARY KEY, -- name of the file\n" 6022 " mode INT, -- access permissions\n" 6023 " mtime INT, -- last modification time\n" 6024 " sz INT, -- original file size\n" 6025 " data BLOB -- compressed content\n" 6026 ")"; 6027 const char *zDrop = "DROP TABLE IF EXISTS sqlar"; 6028 const char *zInsertFmt[2] = { 6029 "REPLACE INTO %s(name,mode,mtime,sz,data)\n" 6030 " SELECT\n" 6031 " %s,\n" 6032 " mode,\n" 6033 " mtime,\n" 6034 " CASE substr(lsmode(mode),1,1)\n" 6035 " WHEN '-' THEN length(data)\n" 6036 " WHEN 'd' THEN 0\n" 6037 " ELSE -1 END,\n" 6038 " sqlar_compress(data)\n" 6039 " FROM fsdir(%Q,%Q) AS disk\n" 6040 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 6041 , 6042 "REPLACE INTO %s(name,mode,mtime,data)\n" 6043 " SELECT\n" 6044 " %s,\n" 6045 " mode,\n" 6046 " mtime,\n" 6047 " data\n" 6048 " FROM fsdir(%Q,%Q) AS disk\n" 6049 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 6050 }; 6051 int i; /* For iterating through azFile[] */ 6052 int rc; /* Return code */ 6053 const char *zTab = 0; /* SQL table into which to insert */ 6054 char *zSql; 6055 char zTemp[50]; 6056 char *zExists = 0; 6057 6058 arExecSql(pAr, "PRAGMA page_size=512"); 6059 rc = arExecSql(pAr, "SAVEPOINT ar;"); 6060 if( rc!=SQLITE_OK ) return rc; 6061 zTemp[0] = 0; 6062 if( pAr->bZip ){ 6063 /* Initialize the zipfile virtual table, if necessary */ 6064 if( pAr->zFile ){ 6065 sqlite3_uint64 r; 6066 sqlite3_randomness(sizeof(r),&r); 6067 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); 6068 zTab = zTemp; 6069 zSql = sqlite3_mprintf( 6070 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", 6071 zTab, pAr->zFile 6072 ); 6073 rc = arExecSql(pAr, zSql); 6074 sqlite3_free(zSql); 6075 }else{ 6076 zTab = "zip"; 6077 } 6078 }else{ 6079 /* Initialize the table for an SQLAR */ 6080 zTab = "sqlar"; 6081 if( bUpdate==0 ){ 6082 rc = arExecSql(pAr, zDrop); 6083 if( rc!=SQLITE_OK ) goto end_ar_transaction; 6084 } 6085 rc = arExecSql(pAr, zCreate); 6086 } 6087 if( bOnlyIfChanged ){ 6088 zExists = sqlite3_mprintf( 6089 " AND NOT EXISTS(" 6090 "SELECT 1 FROM %s AS mem" 6091 " WHERE mem.name=disk.name" 6092 " AND mem.mtime=disk.mtime" 6093 " AND mem.mode=disk.mode)", zTab); 6094 }else{ 6095 zExists = sqlite3_mprintf(""); 6096 } 6097 if( zExists==0 ) rc = SQLITE_NOMEM; 6098 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 6099 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, 6100 pAr->bVerbose ? "shell_putsnl(name)" : "name", 6101 pAr->azArg[i], pAr->zDir, zExists); 6102 rc = arExecSql(pAr, zSql2); 6103 sqlite3_free(zSql2); 6104 } 6105end_ar_transaction: 6106 if( rc!=SQLITE_OK ){ 6107 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 6108 }else{ 6109 rc = arExecSql(pAr, "RELEASE ar;"); 6110 if( pAr->bZip && pAr->zFile ){ 6111 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); 6112 arExecSql(pAr, zSql); 6113 sqlite3_free(zSql); 6114 } 6115 } 6116 sqlite3_free(zExists); 6117 return rc; 6118} 6119 6120/* 6121** Implementation of ".ar" dot command. 6122*/ 6123static int arDotCommand( 6124 ShellState *pState, /* Current shell tool state */ 6125 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ 6126 char **azArg, /* Array of arguments passed to dot command */ 6127 int nArg /* Number of entries in azArg[] */ 6128){ 6129 ArCommand cmd; 6130 int rc; 6131 memset(&cmd, 0, sizeof(cmd)); 6132 cmd.fromCmdLine = fromCmdLine; 6133 rc = arParseCommand(azArg, nArg, &cmd); 6134 if( rc==SQLITE_OK ){ 6135 int eDbType = SHELL_OPEN_UNSPEC; 6136 cmd.p = pState; 6137 cmd.db = pState->db; 6138 if( cmd.zFile ){ 6139 eDbType = deduceDatabaseType(cmd.zFile, 1); 6140 }else{ 6141 eDbType = pState->openMode; 6142 } 6143 if( eDbType==SHELL_OPEN_ZIPFILE ){ 6144 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ 6145 if( cmd.zFile==0 ){ 6146 cmd.zSrcTable = sqlite3_mprintf("zip"); 6147 }else{ 6148 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); 6149 } 6150 } 6151 cmd.bZip = 1; 6152 }else if( cmd.zFile ){ 6153 int flags; 6154 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; 6155 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 6156 || cmd.eCmd==AR_CMD_UPDATE ){ 6157 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 6158 }else{ 6159 flags = SQLITE_OPEN_READONLY; 6160 } 6161 cmd.db = 0; 6162 if( cmd.bDryRun ){ 6163 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, 6164 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); 6165 } 6166 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 6167 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); 6168 if( rc!=SQLITE_OK ){ 6169 utf8_printf(stderr, "cannot open file: %s (%s)\n", 6170 cmd.zFile, sqlite3_errmsg(cmd.db) 6171 ); 6172 goto end_ar_command; 6173 } 6174 sqlite3_fileio_init(cmd.db, 0, 0); 6175 sqlite3_sqlar_init(cmd.db, 0, 0); 6176 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, 6177 shellPutsFunc, 0, 0); 6178 6179 } 6180 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ 6181 if( cmd.eCmd!=AR_CMD_CREATE 6182 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) 6183 ){ 6184 utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); 6185 rc = SQLITE_ERROR; 6186 goto end_ar_command; 6187 } 6188 cmd.zSrcTable = sqlite3_mprintf("sqlar"); 6189 } 6190 6191 switch( cmd.eCmd ){ 6192 case AR_CMD_CREATE: 6193 rc = arCreateOrUpdateCommand(&cmd, 0, 0); 6194 break; 6195 6196 case AR_CMD_EXTRACT: 6197 rc = arExtractCommand(&cmd); 6198 break; 6199 6200 case AR_CMD_LIST: 6201 rc = arListCommand(&cmd); 6202 break; 6203 6204 case AR_CMD_HELP: 6205 arUsage(pState->out); 6206 break; 6207 6208 case AR_CMD_INSERT: 6209 rc = arCreateOrUpdateCommand(&cmd, 1, 0); 6210 break; 6211 6212 default: 6213 assert( cmd.eCmd==AR_CMD_UPDATE ); 6214 rc = arCreateOrUpdateCommand(&cmd, 1, 1); 6215 break; 6216 } 6217 } 6218end_ar_command: 6219 if( cmd.db!=pState->db ){ 6220 close_db(cmd.db); 6221 } 6222 sqlite3_free(cmd.zSrcTable); 6223 6224 return rc; 6225} 6226/* End of the ".archive" or ".ar" command logic 6227*******************************************************************************/ 6228#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ 6229 6230#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 6231/* 6232** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op. 6233** Otherwise, the SQL statement or statements in zSql are executed using 6234** database connection db and the error code written to *pRc before 6235** this function returns. 6236*/ 6237static void shellExec(sqlite3 *db, int *pRc, const char *zSql){ 6238 int rc = *pRc; 6239 if( rc==SQLITE_OK ){ 6240 char *zErr = 0; 6241 rc = sqlite3_exec(db, zSql, 0, 0, &zErr); 6242 if( rc!=SQLITE_OK ){ 6243 raw_printf(stderr, "SQL error: %s\n", zErr); 6244 } 6245 *pRc = rc; 6246 } 6247} 6248 6249/* 6250** Like shellExec(), except that zFmt is a printf() style format string. 6251*/ 6252static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){ 6253 char *z = 0; 6254 if( *pRc==SQLITE_OK ){ 6255 va_list ap; 6256 va_start(ap, zFmt); 6257 z = sqlite3_vmprintf(zFmt, ap); 6258 va_end(ap); 6259 if( z==0 ){ 6260 *pRc = SQLITE_NOMEM; 6261 }else{ 6262 shellExec(db, pRc, z); 6263 } 6264 sqlite3_free(z); 6265 } 6266} 6267 6268/* 6269** If *pRc is not SQLITE_OK when this function is called, it is a no-op. 6270** Otherwise, an attempt is made to allocate, zero and return a pointer 6271** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set 6272** to SQLITE_NOMEM and NULL returned. 6273*/ 6274static void *shellMalloc(int *pRc, sqlite3_int64 nByte){ 6275 void *pRet = 0; 6276 if( *pRc==SQLITE_OK ){ 6277 pRet = sqlite3_malloc64(nByte); 6278 if( pRet==0 ){ 6279 *pRc = SQLITE_NOMEM; 6280 }else{ 6281 memset(pRet, 0, nByte); 6282 } 6283 } 6284 return pRet; 6285} 6286 6287/* 6288** If *pRc is not SQLITE_OK when this function is called, it is a no-op. 6289** Otherwise, zFmt is treated as a printf() style string. The result of 6290** formatting it along with any trailing arguments is written into a 6291** buffer obtained from sqlite3_malloc(), and pointer to which is returned. 6292** It is the responsibility of the caller to eventually free this buffer 6293** using a call to sqlite3_free(). 6294** 6295** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 6296** pointer returned. 6297*/ 6298static char *shellMPrintf(int *pRc, const char *zFmt, ...){ 6299 char *z = 0; 6300 if( *pRc==SQLITE_OK ){ 6301 va_list ap; 6302 va_start(ap, zFmt); 6303 z = sqlite3_vmprintf(zFmt, ap); 6304 va_end(ap); 6305 if( z==0 ){ 6306 *pRc = SQLITE_NOMEM; 6307 } 6308 } 6309 return z; 6310} 6311 6312/* 6313** When running the ".recover" command, each output table, and the special 6314** orphaned row table if it is required, is represented by an instance 6315** of the following struct. 6316*/ 6317typedef struct RecoverTable RecoverTable; 6318struct RecoverTable { 6319 char *zQuoted; /* Quoted version of table name */ 6320 int nCol; /* Number of columns in table */ 6321 char **azlCol; /* Array of column lists */ 6322 int iPk; /* Index of IPK column */ 6323}; 6324 6325/* 6326** Free a RecoverTable object allocated by recoverFindTable() or 6327** recoverOrphanTable(). 6328*/ 6329static void recoverFreeTable(RecoverTable *pTab){ 6330 if( pTab ){ 6331 sqlite3_free(pTab->zQuoted); 6332 if( pTab->azlCol ){ 6333 int i; 6334 for(i=0; i<=pTab->nCol; i++){ 6335 sqlite3_free(pTab->azlCol[i]); 6336 } 6337 sqlite3_free(pTab->azlCol); 6338 } 6339 sqlite3_free(pTab); 6340 } 6341} 6342 6343/* 6344** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. 6345** Otherwise, it allocates and returns a RecoverTable object based on the 6346** final four arguments passed to this function. It is the responsibility 6347** of the caller to eventually free the returned object using 6348** recoverFreeTable(). 6349*/ 6350static RecoverTable *recoverNewTable( 6351 int *pRc, /* IN/OUT: Error code */ 6352 const char *zName, /* Name of table */ 6353 const char *zSql, /* CREATE TABLE statement */ 6354 int bIntkey, 6355 int nCol 6356){ 6357 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */ 6358 int rc = *pRc; 6359 RecoverTable *pTab = 0; 6360 6361 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable)); 6362 if( rc==SQLITE_OK ){ 6363 int nSqlCol = 0; 6364 int bSqlIntkey = 0; 6365 sqlite3_stmt *pStmt = 0; 6366 6367 rc = sqlite3_open("", &dbtmp); 6368 if( rc==SQLITE_OK ){ 6369 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0, 6370 shellIdQuote, 0, 0); 6371 } 6372 if( rc==SQLITE_OK ){ 6373 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0); 6374 } 6375 if( rc==SQLITE_OK ){ 6376 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0); 6377 if( rc==SQLITE_ERROR ){ 6378 rc = SQLITE_OK; 6379 goto finished; 6380 } 6381 } 6382 shellPreparePrintf(dbtmp, &rc, &pStmt, 6383 "SELECT count(*) FROM pragma_table_info(%Q)", zName 6384 ); 6385 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6386 nSqlCol = sqlite3_column_int(pStmt, 0); 6387 } 6388 shellFinalize(&rc, pStmt); 6389 6390 if( rc!=SQLITE_OK || nSqlCol<nCol ){ 6391 goto finished; 6392 } 6393 6394 shellPreparePrintf(dbtmp, &rc, &pStmt, 6395 "SELECT (" 6396 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage" 6397 ") FROM sqlite_master WHERE name = %Q", zName 6398 ); 6399 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6400 bSqlIntkey = sqlite3_column_int(pStmt, 0); 6401 } 6402 shellFinalize(&rc, pStmt); 6403 6404 if( bIntkey==bSqlIntkey ){ 6405 int i; 6406 const char *zPk = "_rowid_"; 6407 sqlite3_stmt *pPkFinder = 0; 6408 6409 /* If this is an intkey table and there is an INTEGER PRIMARY KEY, 6410 ** set zPk to the name of the PK column, and pTab->iPk to the index 6411 ** of the column, where columns are 0-numbered from left to right. 6412 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column, 6413 ** leave zPk as "_rowid_" and pTab->iPk at -2. */ 6414 pTab->iPk = -2; 6415 if( bIntkey ){ 6416 shellPreparePrintf(dbtmp, &rc, &pPkFinder, 6417 "SELECT cid, name FROM pragma_table_info(%Q) " 6418 " WHERE pk=1 AND type='integer' COLLATE nocase" 6419 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)" 6420 , zName, zName 6421 ); 6422 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){ 6423 pTab->iPk = sqlite3_column_int(pPkFinder, 0); 6424 zPk = (const char*)sqlite3_column_text(pPkFinder, 1); 6425 } 6426 } 6427 6428 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName); 6429 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1)); 6430 pTab->nCol = nSqlCol; 6431 6432 if( bIntkey ){ 6433 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk); 6434 }else{ 6435 pTab->azlCol[0] = shellMPrintf(&rc, ""); 6436 } 6437 i = 1; 6438 shellPreparePrintf(dbtmp, &rc, &pStmt, 6439 "SELECT %Q || group_concat(shell_idquote(name), ', ') " 6440 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) " 6441 "FROM pragma_table_info(%Q)", 6442 bIntkey ? ", " : "", pTab->iPk, 6443 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ", 6444 zName 6445 ); 6446 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6447 const char *zText = (const char*)sqlite3_column_text(pStmt, 0); 6448 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText); 6449 i++; 6450 } 6451 shellFinalize(&rc, pStmt); 6452 6453 shellFinalize(&rc, pPkFinder); 6454 } 6455 } 6456 6457 finished: 6458 sqlite3_close(dbtmp); 6459 *pRc = rc; 6460 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){ 6461 recoverFreeTable(pTab); 6462 pTab = 0; 6463 } 6464 return pTab; 6465} 6466 6467/* 6468** This function is called to search the schema recovered from the 6469** sqlite_master table of the (possibly) corrupt database as part 6470** of a ".recover" command. Specifically, for a table with root page 6471** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the 6472** table must be a WITHOUT ROWID table, or if non-zero, not one of 6473** those. 6474** 6475** If a table is found, a (RecoverTable*) object is returned. Or, if 6476** no such table is found, but bIntkey is false and iRoot is the 6477** root page of an index in the recovered schema, then (*pbNoop) is 6478** set to true and NULL returned. Or, if there is no such table or 6479** index, NULL is returned and (*pbNoop) set to 0, indicating that 6480** the caller should write data to the orphans table. 6481*/ 6482static RecoverTable *recoverFindTable( 6483 ShellState *pState, /* Shell state object */ 6484 int *pRc, /* IN/OUT: Error code */ 6485 int iRoot, /* Root page of table */ 6486 int bIntkey, /* True for an intkey table */ 6487 int nCol, /* Number of columns in table */ 6488 int *pbNoop /* OUT: True if iRoot is root of index */ 6489){ 6490 sqlite3_stmt *pStmt = 0; 6491 RecoverTable *pRet = 0; 6492 int bNoop = 0; 6493 const char *zSql = 0; 6494 const char *zName = 0; 6495 6496 /* Search the recovered schema for an object with root page iRoot. */ 6497 shellPreparePrintf(pState->db, pRc, &pStmt, 6498 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot 6499 ); 6500 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6501 const char *zType = (const char*)sqlite3_column_text(pStmt, 0); 6502 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){ 6503 bNoop = 1; 6504 break; 6505 } 6506 if( sqlite3_stricmp(zType, "table")==0 ){ 6507 zName = (const char*)sqlite3_column_text(pStmt, 1); 6508 zSql = (const char*)sqlite3_column_text(pStmt, 2); 6509 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol); 6510 break; 6511 } 6512 } 6513 6514 shellFinalize(pRc, pStmt); 6515 *pbNoop = bNoop; 6516 return pRet; 6517} 6518 6519/* 6520** Return a RecoverTable object representing the orphans table. 6521*/ 6522static RecoverTable *recoverOrphanTable( 6523 ShellState *pState, /* Shell state object */ 6524 int *pRc, /* IN/OUT: Error code */ 6525 const char *zLostAndFound, /* Base name for orphans table */ 6526 int nCol /* Number of user data columns */ 6527){ 6528 RecoverTable *pTab = 0; 6529 if( nCol>=0 && *pRc==SQLITE_OK ){ 6530 int i; 6531 6532 /* This block determines the name of the orphan table. The prefered 6533 ** name is zLostAndFound. But if that clashes with another name 6534 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1 6535 ** and so on until a non-clashing name is found. */ 6536 int iTab = 0; 6537 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound); 6538 sqlite3_stmt *pTest = 0; 6539 shellPrepare(pState->db, pRc, 6540 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest 6541 ); 6542 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); 6543 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){ 6544 shellReset(pRc, pTest); 6545 sqlite3_free(zTab); 6546 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++); 6547 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); 6548 } 6549 shellFinalize(pRc, pTest); 6550 6551 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable)); 6552 if( pTab ){ 6553 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab); 6554 pTab->nCol = nCol; 6555 pTab->iPk = -2; 6556 if( nCol>0 ){ 6557 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1)); 6558 if( pTab->azlCol ){ 6559 pTab->azlCol[nCol] = shellMPrintf(pRc, ""); 6560 for(i=nCol-1; i>=0; i--){ 6561 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]); 6562 } 6563 } 6564 } 6565 6566 if( *pRc!=SQLITE_OK ){ 6567 recoverFreeTable(pTab); 6568 pTab = 0; 6569 }else{ 6570 raw_printf(pState->out, 6571 "CREATE TABLE %s(rootpgno INTEGER, " 6572 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted 6573 ); 6574 for(i=0; i<nCol; i++){ 6575 raw_printf(pState->out, ", c%d", i); 6576 } 6577 raw_printf(pState->out, ");\n"); 6578 } 6579 } 6580 sqlite3_free(zTab); 6581 } 6582 return pTab; 6583} 6584 6585/* 6586** This function is called to recover data from the database. A script 6587** to construct a new database containing all recovered data is output 6588** on stream pState->out. 6589*/ 6590static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ 6591 int rc = SQLITE_OK; 6592 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */ 6593 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */ 6594 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */ 6595 const char *zRecoveryDb = ""; /* Name of "recovery" database */ 6596 const char *zLostAndFound = "lost_and_found"; 6597 int i; 6598 int nOrphan = -1; 6599 RecoverTable *pOrphan = 0; 6600 6601 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ 6602 int bRowids = 1; /* 0 if --no-rowids */ 6603 for(i=1; i<nArg; i++){ 6604 char *z = azArg[i]; 6605 int n; 6606 if( z[0]=='-' && z[1]=='-' ) z++; 6607 n = strlen30(z); 6608 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ 6609 bFreelist = 0; 6610 }else 6611 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ 6612 i++; 6613 zRecoveryDb = azArg[i]; 6614 }else 6615 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ 6616 i++; 6617 zLostAndFound = azArg[i]; 6618 }else 6619 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ 6620 bRowids = 0; 6621 } 6622 else{ 6623 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 6624 showHelp(pState->out, azArg[0]); 6625 return 1; 6626 } 6627 } 6628 6629 shellExecPrintf(pState->db, &rc, 6630 /* Attach an in-memory database named 'recovery'. Create an indexed 6631 ** cache of the sqlite_dbptr virtual table. */ 6632 "PRAGMA writable_schema = on;" 6633 "ATTACH %Q AS recovery;" 6634 "DROP TABLE IF EXISTS recovery.dbptr;" 6635 "DROP TABLE IF EXISTS recovery.freelist;" 6636 "DROP TABLE IF EXISTS recovery.map;" 6637 "DROP TABLE IF EXISTS recovery.schema;" 6638 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb 6639 ); 6640 6641 if( bFreelist ){ 6642 shellExec(pState->db, &rc, 6643 "WITH trunk(pgno) AS (" 6644 " SELECT shell_int32(" 6645 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x " 6646 " WHERE x>0" 6647 " UNION" 6648 " SELECT shell_int32(" 6649 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x " 6650 " FROM trunk WHERE x>0" 6651 ")," 6652 "freelist(data, n, freepgno) AS (" 6653 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno " 6654 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno" 6655 " UNION ALL" 6656 " SELECT data, n-1, shell_int32(data, 2+n) " 6657 " FROM freelist WHERE n>=0" 6658 ")" 6659 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;" 6660 ); 6661 } 6662 6663 /* If this is an auto-vacuum database, add all pointer-map pages to 6664 ** the freelist table. Do this regardless of whether or not 6665 ** --freelist-corrupt was specified. */ 6666 shellExec(pState->db, &rc, 6667 "WITH ptrmap(pgno) AS (" 6668 " SELECT 2 WHERE shell_int32(" 6669 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13" 6670 " )" 6671 " UNION ALL " 6672 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp " 6673 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)" 6674 ")" 6675 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap" 6676 ); 6677 6678 shellExec(pState->db, &rc, 6679 "CREATE TABLE recovery.dbptr(" 6680 " pgno, child, PRIMARY KEY(child, pgno)" 6681 ") WITHOUT ROWID;" 6682 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) " 6683 " SELECT * FROM sqlite_dbptr" 6684 " WHERE pgno NOT IN freelist AND child NOT IN freelist;" 6685 6686 /* Delete any pointer to page 1. This ensures that page 1 is considered 6687 ** a root page, regardless of how corrupt the db is. */ 6688 "DELETE FROM recovery.dbptr WHERE child = 1;" 6689 6690 /* Delete all pointers to any pages that have more than one pointer 6691 ** to them. Such pages will be treated as root pages when recovering 6692 ** data. */ 6693 "DELETE FROM recovery.dbptr WHERE child IN (" 6694 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1" 6695 ");" 6696 6697 /* Create the "map" table that will (eventually) contain instructions 6698 ** for dealing with each page in the db that contains one or more 6699 ** records. */ 6700 "CREATE TABLE recovery.map(" 6701 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT" 6702 ");" 6703 6704 /* Populate table [map]. If there are circular loops of pages in the 6705 ** database, the following adds all pages in such a loop to the map 6706 ** as individual root pages. This could be handled better. */ 6707 "WITH pages(i, maxlen) AS (" 6708 " SELECT page_count, (" 6709 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count" 6710 " ) FROM pragma_page_count WHERE page_count>0" 6711 " UNION ALL" 6712 " SELECT i-1, (" 6713 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1" 6714 " ) FROM pages WHERE i>=2" 6715 ")" 6716 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) " 6717 " SELECT i, maxlen, NULL, (" 6718 " WITH p(orig, pgno, parent) AS (" 6719 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)" 6720 " UNION " 6721 " SELECT i, p.parent, " 6722 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p" 6723 " )" 6724 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)" 6725 ") " 6726 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;" 6727 "UPDATE recovery.map AS o SET intkey = (" 6728 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno" 6729 ");" 6730 6731 /* Extract data from page 1 and any linked pages into table 6732 ** recovery.schema. With the same schema as an sqlite_master table. */ 6733 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);" 6734 "INSERT INTO recovery.schema SELECT " 6735 " max(CASE WHEN field=0 THEN value ELSE NULL END)," 6736 " max(CASE WHEN field=1 THEN value ELSE NULL END)," 6737 " max(CASE WHEN field=2 THEN value ELSE NULL END)," 6738 " max(CASE WHEN field=3 THEN value ELSE NULL END)," 6739 " max(CASE WHEN field=4 THEN value ELSE NULL END)" 6740 "FROM sqlite_dbdata WHERE pgno IN (" 6741 " SELECT pgno FROM recovery.map WHERE root=1" 6742 ")" 6743 "GROUP BY pgno, cell;" 6744 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);" 6745 ); 6746 6747 /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 6748 ** CREATE TABLE statements that extracted from the existing schema. */ 6749 if( rc==SQLITE_OK ){ 6750 sqlite3_stmt *pStmt = 0; 6751 /* ".recover" might output content in an order which causes immediate 6752 ** foreign key constraints to be violated. So disable foreign-key 6753 ** constraint enforcement to prevent problems when running the output 6754 ** script. */ 6755 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n"); 6756 raw_printf(pState->out, "BEGIN;\n"); 6757 raw_printf(pState->out, "PRAGMA writable_schema = on;\n"); 6758 shellPrepare(pState->db, &rc, 6759 "SELECT sql FROM recovery.schema " 6760 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt 6761 ); 6762 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6763 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0); 6764 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 6765 &zCreateTable[12] 6766 ); 6767 } 6768 shellFinalize(&rc, pStmt); 6769 } 6770 6771 /* Figure out if an orphan table will be required. And if so, how many 6772 ** user columns it should contain */ 6773 shellPrepare(pState->db, &rc, 6774 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1" 6775 , &pLoop 6776 ); 6777 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ 6778 nOrphan = sqlite3_column_int(pLoop, 0); 6779 } 6780 shellFinalize(&rc, pLoop); 6781 pLoop = 0; 6782 6783 shellPrepare(pState->db, &rc, 6784 "SELECT pgno FROM recovery.map WHERE root=?", &pPages 6785 ); 6786 6787 shellPrepare(pState->db, &rc, 6788 "SELECT max(field), group_concat(shell_escape_crnl(quote" 6789 "(case when (? AND field<0) then NULL else value end)" 6790 "), ', ')" 6791 ", min(field) " 6792 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?" 6793 "GROUP BY cell", &pCells 6794 ); 6795 6796 /* Loop through each root page. */ 6797 shellPrepare(pState->db, &rc, 6798 "SELECT root, intkey, max(maxlen) FROM recovery.map" 6799 " WHERE root>1 GROUP BY root, intkey ORDER BY root=(" 6800 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'" 6801 ")", &pLoop 6802 ); 6803 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ 6804 int iRoot = sqlite3_column_int(pLoop, 0); 6805 int bIntkey = sqlite3_column_int(pLoop, 1); 6806 int nCol = sqlite3_column_int(pLoop, 2); 6807 int bNoop = 0; 6808 RecoverTable *pTab; 6809 6810 assert( bIntkey==0 || bIntkey==1 ); 6811 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop); 6812 if( bNoop || rc ) continue; 6813 if( pTab==0 ){ 6814 if( pOrphan==0 ){ 6815 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); 6816 } 6817 pTab = pOrphan; 6818 if( pTab==0 ) break; 6819 } 6820 6821 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){ 6822 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); 6823 } 6824 sqlite3_bind_int(pPages, 1, iRoot); 6825 if( bRowids==0 && pTab->iPk<0 ){ 6826 sqlite3_bind_int(pCells, 1, 1); 6827 }else{ 6828 sqlite3_bind_int(pCells, 1, 0); 6829 } 6830 sqlite3_bind_int(pCells, 3, pTab->iPk); 6831 6832 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){ 6833 int iPgno = sqlite3_column_int(pPages, 0); 6834 sqlite3_bind_int(pCells, 2, iPgno); 6835 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){ 6836 int nField = sqlite3_column_int(pCells, 0); 6837 int iMin = sqlite3_column_int(pCells, 2); 6838 const char *zVal = (const char*)sqlite3_column_text(pCells, 1); 6839 6840 RecoverTable *pTab2 = pTab; 6841 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){ 6842 if( pOrphan==0 ){ 6843 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); 6844 } 6845 pTab2 = pOrphan; 6846 if( pTab2==0 ) break; 6847 } 6848 6849 nField = nField+1; 6850 if( pTab2==pOrphan ){ 6851 raw_printf(pState->out, 6852 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n", 6853 pTab2->zQuoted, iRoot, iPgno, nField, 6854 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField] 6855 ); 6856 }else{ 6857 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 6858 pTab2->zQuoted, pTab2->azlCol[nField], zVal 6859 ); 6860 } 6861 } 6862 shellReset(&rc, pCells); 6863 } 6864 shellReset(&rc, pPages); 6865 if( pTab!=pOrphan ) recoverFreeTable(pTab); 6866 } 6867 shellFinalize(&rc, pLoop); 6868 shellFinalize(&rc, pPages); 6869 shellFinalize(&rc, pCells); 6870 recoverFreeTable(pOrphan); 6871 6872 /* The rest of the schema */ 6873 if( rc==SQLITE_OK ){ 6874 sqlite3_stmt *pStmt = 0; 6875 shellPrepare(pState->db, &rc, 6876 "SELECT sql, name FROM recovery.schema " 6877 "WHERE sql NOT LIKE 'create table%'", &pStmt 6878 ); 6879 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ 6880 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0); 6881 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){ 6882 const char *zName = (const char*)sqlite3_column_text(pStmt, 1); 6883 char *zPrint = shellMPrintf(&rc, 6884 "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)", 6885 zName, zName, zSql 6886 ); 6887 raw_printf(pState->out, "%s;\n", zPrint); 6888 sqlite3_free(zPrint); 6889 }else{ 6890 raw_printf(pState->out, "%s;\n", zSql); 6891 } 6892 } 6893 shellFinalize(&rc, pStmt); 6894 } 6895 6896 if( rc==SQLITE_OK ){ 6897 raw_printf(pState->out, "PRAGMA writable_schema = off;\n"); 6898 raw_printf(pState->out, "COMMIT;\n"); 6899 } 6900 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0); 6901 return rc; 6902} 6903#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 6904 6905 6906/* 6907** If an input line begins with "." then invoke this routine to 6908** process that line. 6909** 6910** Return 1 on error, 2 to exit, and 0 otherwise. 6911*/ 6912static int do_meta_command(char *zLine, ShellState *p){ 6913 int h = 1; 6914 int nArg = 0; 6915 int n, c; 6916 int rc = 0; 6917 char *azArg[52]; 6918 6919#ifndef SQLITE_OMIT_VIRTUALTABLE 6920 if( p->expert.pExpert ){ 6921 expertFinish(p, 1, 0); 6922 } 6923#endif 6924 6925 /* Parse the input line into tokens. 6926 */ 6927 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ 6928 while( IsSpace(zLine[h]) ){ h++; } 6929 if( zLine[h]==0 ) break; 6930 if( zLine[h]=='\'' || zLine[h]=='"' ){ 6931 int delim = zLine[h++]; 6932 azArg[nArg++] = &zLine[h]; 6933 while( zLine[h] && zLine[h]!=delim ){ 6934 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; 6935 h++; 6936 } 6937 if( zLine[h]==delim ){ 6938 zLine[h++] = 0; 6939 } 6940 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); 6941 }else{ 6942 azArg[nArg++] = &zLine[h]; 6943 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } 6944 if( zLine[h] ) zLine[h++] = 0; 6945 resolve_backslashes(azArg[nArg-1]); 6946 } 6947 } 6948 azArg[nArg] = 0; 6949 6950 /* Process the input line. 6951 */ 6952 if( nArg==0 ) return 0; /* no tokens, no error */ 6953 n = strlen30(azArg[0]); 6954 c = azArg[0][0]; 6955 clearTempFile(p); 6956 6957#ifndef SQLITE_OMIT_AUTHORIZATION 6958 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ 6959 if( nArg!=2 ){ 6960 raw_printf(stderr, "Usage: .auth ON|OFF\n"); 6961 rc = 1; 6962 goto meta_command_exit; 6963 } 6964 open_db(p, 0); 6965 if( booleanValue(azArg[1]) ){ 6966 sqlite3_set_authorizer(p->db, shellAuth, p); 6967 }else{ 6968 sqlite3_set_authorizer(p->db, 0, 0); 6969 } 6970 }else 6971#endif 6972 6973#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 6974 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ 6975 open_db(p, 0); 6976 rc = arDotCommand(p, 0, azArg, nArg); 6977 }else 6978#endif 6979 6980 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) 6981 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) 6982 ){ 6983 const char *zDestFile = 0; 6984 const char *zDb = 0; 6985 sqlite3 *pDest; 6986 sqlite3_backup *pBackup; 6987 int j; 6988 int bAsync = 0; 6989 const char *zVfs = 0; 6990 for(j=1; j<nArg; j++){ 6991 const char *z = azArg[j]; 6992 if( z[0]=='-' ){ 6993 if( z[1]=='-' ) z++; 6994 if( strcmp(z, "-append")==0 ){ 6995 zVfs = "apndvfs"; 6996 }else 6997 if( strcmp(z, "-async")==0 ){ 6998 bAsync = 1; 6999 }else 7000 { 7001 utf8_printf(stderr, "unknown option: %s\n", azArg[j]); 7002 return 1; 7003 } 7004 }else if( zDestFile==0 ){ 7005 zDestFile = azArg[j]; 7006 }else if( zDb==0 ){ 7007 zDb = zDestFile; 7008 zDestFile = azArg[j]; 7009 }else{ 7010 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); 7011 return 1; 7012 } 7013 } 7014 if( zDestFile==0 ){ 7015 raw_printf(stderr, "missing FILENAME argument on .backup\n"); 7016 return 1; 7017 } 7018 if( zDb==0 ) zDb = "main"; 7019 rc = sqlite3_open_v2(zDestFile, &pDest, 7020 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); 7021 if( rc!=SQLITE_OK ){ 7022 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); 7023 close_db(pDest); 7024 return 1; 7025 } 7026 if( bAsync ){ 7027 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", 7028 0, 0, 0); 7029 } 7030 open_db(p, 0); 7031 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); 7032 if( pBackup==0 ){ 7033 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7034 close_db(pDest); 7035 return 1; 7036 } 7037 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 7038 sqlite3_backup_finish(pBackup); 7039 if( rc==SQLITE_DONE ){ 7040 rc = 0; 7041 }else{ 7042 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7043 rc = 1; 7044 } 7045 close_db(pDest); 7046 }else 7047 7048 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ 7049 if( nArg==2 ){ 7050 bail_on_error = booleanValue(azArg[1]); 7051 }else{ 7052 raw_printf(stderr, "Usage: .bail on|off\n"); 7053 rc = 1; 7054 } 7055 }else 7056 7057 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ 7058 if( nArg==2 ){ 7059 if( booleanValue(azArg[1]) ){ 7060 setBinaryMode(p->out, 1); 7061 }else{ 7062 setTextMode(p->out, 1); 7063 } 7064 }else{ 7065 raw_printf(stderr, "Usage: .binary on|off\n"); 7066 rc = 1; 7067 } 7068 }else 7069 7070 if( c=='c' && strcmp(azArg[0],"cd")==0 ){ 7071 if( nArg==2 ){ 7072#if defined(_WIN32) || defined(WIN32) 7073 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); 7074 rc = !SetCurrentDirectoryW(z); 7075 sqlite3_free(z); 7076#else 7077 rc = chdir(azArg[1]); 7078#endif 7079 if( rc ){ 7080 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); 7081 rc = 1; 7082 } 7083 }else{ 7084 raw_printf(stderr, "Usage: .cd DIRECTORY\n"); 7085 rc = 1; 7086 } 7087 }else 7088 7089 /* The undocumented ".breakpoint" command causes a call to the no-op 7090 ** routine named test_breakpoint(). 7091 */ 7092 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ 7093 test_breakpoint(); 7094 }else 7095 7096 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ 7097 if( nArg==2 ){ 7098 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); 7099 }else{ 7100 raw_printf(stderr, "Usage: .changes on|off\n"); 7101 rc = 1; 7102 } 7103 }else 7104 7105 /* Cancel output redirection, if it is currently set (by .testcase) 7106 ** Then read the content of the testcase-out.txt file and compare against 7107 ** azArg[1]. If there are differences, report an error and exit. 7108 */ 7109 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ 7110 char *zRes = 0; 7111 output_reset(p); 7112 if( nArg!=2 ){ 7113 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); 7114 rc = 2; 7115 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ 7116 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); 7117 rc = 2; 7118 }else if( testcase_glob(azArg[1],zRes)==0 ){ 7119 utf8_printf(stderr, 7120 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", 7121 p->zTestcase, azArg[1], zRes); 7122 rc = 1; 7123 }else{ 7124 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); 7125 p->nCheck++; 7126 } 7127 sqlite3_free(zRes); 7128 }else 7129 7130 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ 7131 if( nArg==2 ){ 7132 tryToClone(p, azArg[1]); 7133 }else{ 7134 raw_printf(stderr, "Usage: .clone FILENAME\n"); 7135 rc = 1; 7136 } 7137 }else 7138 7139 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ 7140 ShellState data; 7141 char *zErrMsg = 0; 7142 open_db(p, 0); 7143 memcpy(&data, p, sizeof(data)); 7144 data.showHeader = 0; 7145 data.cMode = data.mode = MODE_List; 7146 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); 7147 data.cnt = 0; 7148 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", 7149 callback, &data, &zErrMsg); 7150 if( zErrMsg ){ 7151 utf8_printf(stderr,"Error: %s\n", zErrMsg); 7152 sqlite3_free(zErrMsg); 7153 rc = 1; 7154 } 7155 }else 7156 7157 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ 7158 static const struct DbConfigChoices { 7159 const char *zName; 7160 int op; 7161 } aDbConfig[] = { 7162 { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, 7163 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, 7164 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, 7165 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, 7166 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, 7167 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 7168 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, 7169 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 7170 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, 7171 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, 7172 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, 7173 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, 7174 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, 7175 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, 7176 { "unsafe_func_in_view",SQLITE_DBCONFIG_UNSAFE_FUNC_IN_VIEW }, 7177 { "vtab_in_view", SQLITE_DBCONFIG_VTAB_IN_VIEW }, 7178 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 7179 }; 7180 int ii, v; 7181 open_db(p, 0); 7182 for(ii=0; ii<ArraySize(aDbConfig); ii++){ 7183 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; 7184 if( nArg>=3 ){ 7185 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); 7186 } 7187 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); 7188 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); 7189 if( nArg>1 ) break; 7190 } 7191 if( nArg>1 && ii==ArraySize(aDbConfig) ){ 7192 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); 7193 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); 7194 } 7195 }else 7196 7197 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ 7198 rc = shell_dbinfo_command(p, nArg, azArg); 7199 }else 7200 7201#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 7202 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ 7203 open_db(p, 0); 7204 rc = recoverDatabaseCmd(p, nArg, azArg); 7205 }else 7206#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7207 7208 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ 7209 const char *zLike = 0; 7210 int i; 7211 int savedShowHeader = p->showHeader; 7212 int savedShellFlags = p->shellFlgs; 7213 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo); 7214 for(i=1; i<nArg; i++){ 7215 if( azArg[i][0]=='-' ){ 7216 const char *z = azArg[i]+1; 7217 if( z[0]=='-' ) z++; 7218 if( strcmp(z,"preserve-rowids")==0 ){ 7219#ifdef SQLITE_OMIT_VIRTUALTABLE 7220 raw_printf(stderr, "The --preserve-rowids option is not compatible" 7221 " with SQLITE_OMIT_VIRTUALTABLE\n"); 7222 rc = 1; 7223 goto meta_command_exit; 7224#else 7225 ShellSetFlag(p, SHFLG_PreserveRowid); 7226#endif 7227 }else 7228 if( strcmp(z,"newlines")==0 ){ 7229 ShellSetFlag(p, SHFLG_Newlines); 7230 }else 7231 { 7232 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); 7233 rc = 1; 7234 goto meta_command_exit; 7235 } 7236 }else if( zLike ){ 7237 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " 7238 "?--newlines? ?LIKE-PATTERN?\n"); 7239 rc = 1; 7240 goto meta_command_exit; 7241 }else{ 7242 zLike = azArg[i]; 7243 } 7244 } 7245 7246 open_db(p, 0); 7247 7248 /* When playing back a "dump", the content might appear in an order 7249 ** which causes immediate foreign key constraints to be violated. 7250 ** So disable foreign-key constraint enforcement to prevent problems. */ 7251 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); 7252 raw_printf(p->out, "BEGIN TRANSACTION;\n"); 7253 p->writableSchema = 0; 7254 p->showHeader = 0; 7255 /* Set writable_schema=ON since doing so forces SQLite to initialize 7256 ** as much of the schema as it can even if the sqlite_master table is 7257 ** corrupt. */ 7258 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); 7259 p->nErr = 0; 7260 if( zLike==0 ){ 7261 run_schema_dump_query(p, 7262 "SELECT name, type, sql FROM sqlite_master " 7263 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" 7264 ); 7265 run_schema_dump_query(p, 7266 "SELECT name, type, sql FROM sqlite_master " 7267 "WHERE name=='sqlite_sequence'" 7268 ); 7269 run_table_dump_query(p, 7270 "SELECT sql FROM sqlite_master " 7271 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 7272 ); 7273 }else{ 7274 char *zSql; 7275 zSql = sqlite3_mprintf( 7276 "SELECT name, type, sql FROM sqlite_master " 7277 "WHERE tbl_name LIKE %Q AND type=='table'" 7278 " AND sql NOT NULL", zLike); 7279 run_schema_dump_query(p,zSql); 7280 sqlite3_free(zSql); 7281 zSql = sqlite3_mprintf( 7282 "SELECT sql FROM sqlite_master " 7283 "WHERE sql NOT NULL" 7284 " AND type IN ('index','trigger','view')" 7285 " AND tbl_name LIKE %Q", zLike); 7286 run_table_dump_query(p, zSql, 0); 7287 sqlite3_free(zSql); 7288 } 7289 if( p->writableSchema ){ 7290 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); 7291 p->writableSchema = 0; 7292 } 7293 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 7294 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); 7295 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); 7296 p->showHeader = savedShowHeader; 7297 p->shellFlgs = savedShellFlags; 7298 }else 7299 7300 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ 7301 if( nArg==2 ){ 7302 setOrClearFlag(p, SHFLG_Echo, azArg[1]); 7303 }else{ 7304 raw_printf(stderr, "Usage: .echo on|off\n"); 7305 rc = 1; 7306 } 7307 }else 7308 7309 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ 7310 if( nArg==2 ){ 7311 p->autoEQPtest = 0; 7312 if( p->autoEQPtrace ){ 7313 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); 7314 p->autoEQPtrace = 0; 7315 } 7316 if( strcmp(azArg[1],"full")==0 ){ 7317 p->autoEQP = AUTOEQP_full; 7318 }else if( strcmp(azArg[1],"trigger")==0 ){ 7319 p->autoEQP = AUTOEQP_trigger; 7320#ifdef SQLITE_DEBUG 7321 }else if( strcmp(azArg[1],"test")==0 ){ 7322 p->autoEQP = AUTOEQP_on; 7323 p->autoEQPtest = 1; 7324 }else if( strcmp(azArg[1],"trace")==0 ){ 7325 p->autoEQP = AUTOEQP_full; 7326 p->autoEQPtrace = 1; 7327 open_db(p, 0); 7328 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0); 7329 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); 7330#endif 7331 }else{ 7332 p->autoEQP = (u8)booleanValue(azArg[1]); 7333 } 7334 }else{ 7335 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); 7336 rc = 1; 7337 } 7338 }else 7339 7340 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ 7341 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); 7342 rc = 2; 7343 }else 7344 7345 /* The ".explain" command is automatic now. It is largely pointless. It 7346 ** retained purely for backwards compatibility */ 7347 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ 7348 int val = 1; 7349 if( nArg>=2 ){ 7350 if( strcmp(azArg[1],"auto")==0 ){ 7351 val = 99; 7352 }else{ 7353 val = booleanValue(azArg[1]); 7354 } 7355 } 7356 if( val==1 && p->mode!=MODE_Explain ){ 7357 p->normalMode = p->mode; 7358 p->mode = MODE_Explain; 7359 p->autoExplain = 0; 7360 }else if( val==0 ){ 7361 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7362 p->autoExplain = 0; 7363 }else if( val==99 ){ 7364 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7365 p->autoExplain = 1; 7366 } 7367 }else 7368 7369#ifndef SQLITE_OMIT_VIRTUALTABLE 7370 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ 7371 open_db(p, 0); 7372 expertDotCommand(p, azArg, nArg); 7373 }else 7374#endif 7375 7376 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ 7377 static const struct { 7378 const char *zCtrlName; /* Name of a test-control option */ 7379 int ctrlCode; /* Integer code for that option */ 7380 const char *zUsage; /* Usage notes */ 7381 } aCtrl[] = { 7382 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, 7383 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, 7384 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ 7385 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, 7386 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, 7387 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ 7388 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, 7389 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, 7390 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, 7391 }; 7392 int filectrl = -1; 7393 int iCtrl = -1; 7394 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ 7395 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ 7396 int n2, i; 7397 const char *zCmd = 0; 7398 7399 open_db(p, 0); 7400 zCmd = nArg>=2 ? azArg[1] : "help"; 7401 7402 /* The argument can optionally begin with "-" or "--" */ 7403 if( zCmd[0]=='-' && zCmd[1] ){ 7404 zCmd++; 7405 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 7406 } 7407 7408 /* --help lists all file-controls */ 7409 if( strcmp(zCmd,"help")==0 ){ 7410 utf8_printf(p->out, "Available file-controls:\n"); 7411 for(i=0; i<ArraySize(aCtrl); i++){ 7412 utf8_printf(p->out, " .filectrl %s %s\n", 7413 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 7414 } 7415 rc = 1; 7416 goto meta_command_exit; 7417 } 7418 7419 /* convert filectrl text option to value. allow any unique prefix 7420 ** of the option name, or a numerical value. */ 7421 n2 = strlen30(zCmd); 7422 for(i=0; i<ArraySize(aCtrl); i++){ 7423 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 7424 if( filectrl<0 ){ 7425 filectrl = aCtrl[i].ctrlCode; 7426 iCtrl = i; 7427 }else{ 7428 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" 7429 "Use \".filectrl --help\" for help\n", zCmd); 7430 rc = 1; 7431 goto meta_command_exit; 7432 } 7433 } 7434 } 7435 if( filectrl<0 ){ 7436 utf8_printf(stderr,"Error: unknown file-control: %s\n" 7437 "Use \".filectrl --help\" for help\n", zCmd); 7438 }else{ 7439 switch(filectrl){ 7440 case SQLITE_FCNTL_SIZE_LIMIT: { 7441 if( nArg!=2 && nArg!=3 ) break; 7442 iRes = nArg==3 ? integerValue(azArg[2]) : -1; 7443 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes); 7444 isOk = 1; 7445 break; 7446 } 7447 case SQLITE_FCNTL_LOCK_TIMEOUT: 7448 case SQLITE_FCNTL_CHUNK_SIZE: { 7449 int x; 7450 if( nArg!=3 ) break; 7451 x = (int)integerValue(azArg[2]); 7452 sqlite3_file_control(p->db, 0, filectrl, &x); 7453 isOk = 2; 7454 break; 7455 } 7456 case SQLITE_FCNTL_PERSIST_WAL: 7457 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { 7458 int x; 7459 if( nArg!=2 && nArg!=3 ) break; 7460 x = nArg==3 ? booleanValue(azArg[2]) : -1; 7461 sqlite3_file_control(p->db, 0, filectrl, &x); 7462 iRes = x; 7463 isOk = 1; 7464 break; 7465 } 7466 case SQLITE_FCNTL_HAS_MOVED: { 7467 int x; 7468 if( nArg!=2 ) break; 7469 sqlite3_file_control(p->db, 0, filectrl, &x); 7470 iRes = x; 7471 isOk = 1; 7472 break; 7473 } 7474 case SQLITE_FCNTL_TEMPFILENAME: { 7475 char *z = 0; 7476 if( nArg!=2 ) break; 7477 sqlite3_file_control(p->db, 0, filectrl, &z); 7478 if( z ){ 7479 utf8_printf(p->out, "%s\n", z); 7480 sqlite3_free(z); 7481 } 7482 isOk = 2; 7483 break; 7484 } 7485 } 7486 } 7487 if( isOk==0 && iCtrl>=0 ){ 7488 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 7489 rc = 1; 7490 }else if( isOk==1 ){ 7491 char zBuf[100]; 7492 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); 7493 raw_printf(p->out, "%s\n", zBuf); 7494 } 7495 }else 7496 7497 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ 7498 ShellState data; 7499 char *zErrMsg = 0; 7500 int doStats = 0; 7501 memcpy(&data, p, sizeof(data)); 7502 data.showHeader = 0; 7503 data.cMode = data.mode = MODE_Semi; 7504 if( nArg==2 && optionMatch(azArg[1], "indent") ){ 7505 data.cMode = data.mode = MODE_Pretty; 7506 nArg = 1; 7507 } 7508 if( nArg!=1 ){ 7509 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); 7510 rc = 1; 7511 goto meta_command_exit; 7512 } 7513 open_db(p, 0); 7514 rc = sqlite3_exec(p->db, 7515 "SELECT sql FROM" 7516 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 7517 " FROM sqlite_master UNION ALL" 7518 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 7519 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 7520 "ORDER BY rowid", 7521 callback, &data, &zErrMsg 7522 ); 7523 if( rc==SQLITE_OK ){ 7524 sqlite3_stmt *pStmt; 7525 rc = sqlite3_prepare_v2(p->db, 7526 "SELECT rowid FROM sqlite_master" 7527 " WHERE name GLOB 'sqlite_stat[134]'", 7528 -1, &pStmt, 0); 7529 doStats = sqlite3_step(pStmt)==SQLITE_ROW; 7530 sqlite3_finalize(pStmt); 7531 } 7532 if( doStats==0 ){ 7533 raw_printf(p->out, "/* No STAT tables available */\n"); 7534 }else{ 7535 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7536 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", 7537 callback, &data, &zErrMsg); 7538 data.cMode = data.mode = MODE_Insert; 7539 data.zDestTable = "sqlite_stat1"; 7540 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg); 7541 data.zDestTable = "sqlite_stat4"; 7542 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg); 7543 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7544 } 7545 }else 7546 7547 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ 7548 if( nArg==2 ){ 7549 p->showHeader = booleanValue(azArg[1]); 7550 }else{ 7551 raw_printf(stderr, "Usage: .headers on|off\n"); 7552 rc = 1; 7553 } 7554 }else 7555 7556 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ 7557 if( nArg>=2 ){ 7558 n = showHelp(p->out, azArg[1]); 7559 if( n==0 ){ 7560 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); 7561 } 7562 }else{ 7563 showHelp(p->out, 0); 7564 } 7565 }else 7566 7567 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ 7568 char *zTable; /* Insert data into this table */ 7569 char *zFile; /* Name of file to extra content from */ 7570 sqlite3_stmt *pStmt = NULL; /* A statement */ 7571 int nCol; /* Number of columns in the table */ 7572 int nByte; /* Number of bytes in an SQL string */ 7573 int i, j; /* Loop counters */ 7574 int needCommit; /* True to COMMIT or ROLLBACK at end */ 7575 int nSep; /* Number of bytes in p->colSeparator[] */ 7576 char *zSql; /* An SQL statement */ 7577 ImportCtx sCtx; /* Reader context */ 7578 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ 7579 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ 7580 7581 if( nArg!=3 ){ 7582 raw_printf(stderr, "Usage: .import FILE TABLE\n"); 7583 goto meta_command_exit; 7584 } 7585 zFile = azArg[1]; 7586 zTable = azArg[2]; 7587 seenInterrupt = 0; 7588 memset(&sCtx, 0, sizeof(sCtx)); 7589 open_db(p, 0); 7590 nSep = strlen30(p->colSeparator); 7591 if( nSep==0 ){ 7592 raw_printf(stderr, 7593 "Error: non-null column separator required for import\n"); 7594 return 1; 7595 } 7596 if( nSep>1 ){ 7597 raw_printf(stderr, "Error: multi-character column separators not allowed" 7598 " for import\n"); 7599 return 1; 7600 } 7601 nSep = strlen30(p->rowSeparator); 7602 if( nSep==0 ){ 7603 raw_printf(stderr, "Error: non-null row separator required for import\n"); 7604 return 1; 7605 } 7606 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ 7607 /* When importing CSV (only), if the row separator is set to the 7608 ** default output row separator, change it to the default input 7609 ** row separator. This avoids having to maintain different input 7610 ** and output row separators. */ 7611 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7612 nSep = strlen30(p->rowSeparator); 7613 } 7614 if( nSep>1 ){ 7615 raw_printf(stderr, "Error: multi-character row separators not allowed" 7616 " for import\n"); 7617 return 1; 7618 } 7619 sCtx.zFile = zFile; 7620 sCtx.nLine = 1; 7621 if( sCtx.zFile[0]=='|' ){ 7622#ifdef SQLITE_OMIT_POPEN 7623 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 7624 return 1; 7625#else 7626 sCtx.in = popen(sCtx.zFile+1, "r"); 7627 sCtx.zFile = "<pipe>"; 7628 xCloser = pclose; 7629#endif 7630 }else{ 7631 sCtx.in = fopen(sCtx.zFile, "rb"); 7632 xCloser = fclose; 7633 } 7634 if( p->mode==MODE_Ascii ){ 7635 xRead = ascii_read_one_field; 7636 }else{ 7637 xRead = csv_read_one_field; 7638 } 7639 if( sCtx.in==0 ){ 7640 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 7641 return 1; 7642 } 7643 sCtx.cColSep = p->colSeparator[0]; 7644 sCtx.cRowSep = p->rowSeparator[0]; 7645 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); 7646 if( zSql==0 ){ 7647 xCloser(sCtx.in); 7648 shell_out_of_memory(); 7649 } 7650 nByte = strlen30(zSql); 7651 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7652 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ 7653 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ 7654 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); 7655 char cSep = '('; 7656 while( xRead(&sCtx) ){ 7657 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); 7658 cSep = ','; 7659 if( sCtx.cTerm!=sCtx.cColSep ) break; 7660 } 7661 if( cSep=='(' ){ 7662 sqlite3_free(zCreate); 7663 sqlite3_free(sCtx.z); 7664 xCloser(sCtx.in); 7665 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); 7666 return 1; 7667 } 7668 zCreate = sqlite3_mprintf("%z\n)", zCreate); 7669 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); 7670 sqlite3_free(zCreate); 7671 if( rc ){ 7672 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, 7673 sqlite3_errmsg(p->db)); 7674 sqlite3_free(sCtx.z); 7675 xCloser(sCtx.in); 7676 return 1; 7677 } 7678 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7679 } 7680 sqlite3_free(zSql); 7681 if( rc ){ 7682 if (pStmt) sqlite3_finalize(pStmt); 7683 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); 7684 xCloser(sCtx.in); 7685 return 1; 7686 } 7687 nCol = sqlite3_column_count(pStmt); 7688 sqlite3_finalize(pStmt); 7689 pStmt = 0; 7690 if( nCol==0 ) return 0; /* no columns, no error */ 7691 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); 7692 if( zSql==0 ){ 7693 xCloser(sCtx.in); 7694 shell_out_of_memory(); 7695 } 7696 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); 7697 j = strlen30(zSql); 7698 for(i=1; i<nCol; i++){ 7699 zSql[j++] = ','; 7700 zSql[j++] = '?'; 7701 } 7702 zSql[j++] = ')'; 7703 zSql[j] = 0; 7704 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7705 sqlite3_free(zSql); 7706 if( rc ){ 7707 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 7708 if (pStmt) sqlite3_finalize(pStmt); 7709 xCloser(sCtx.in); 7710 return 1; 7711 } 7712 needCommit = sqlite3_get_autocommit(p->db); 7713 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); 7714 do{ 7715 int startLine = sCtx.nLine; 7716 for(i=0; i<nCol; i++){ 7717 char *z = xRead(&sCtx); 7718 /* 7719 ** Did we reach end-of-file before finding any columns? 7720 ** If so, stop instead of NULL filling the remaining columns. 7721 */ 7722 if( z==0 && i==0 ) break; 7723 /* 7724 ** Did we reach end-of-file OR end-of-line before finding any 7725 ** columns in ASCII mode? If so, stop instead of NULL filling 7726 ** the remaining columns. 7727 */ 7728 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; 7729 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); 7730 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ 7731 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 7732 "filling the rest with NULL\n", 7733 sCtx.zFile, startLine, nCol, i+1); 7734 i += 2; 7735 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } 7736 } 7737 } 7738 if( sCtx.cTerm==sCtx.cColSep ){ 7739 do{ 7740 xRead(&sCtx); 7741 i++; 7742 }while( sCtx.cTerm==sCtx.cColSep ); 7743 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 7744 "extras ignored\n", 7745 sCtx.zFile, startLine, nCol, i); 7746 } 7747 if( i>=nCol ){ 7748 sqlite3_step(pStmt); 7749 rc = sqlite3_reset(pStmt); 7750 if( rc!=SQLITE_OK ){ 7751 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, 7752 startLine, sqlite3_errmsg(p->db)); 7753 } 7754 } 7755 }while( sCtx.cTerm!=EOF ); 7756 7757 xCloser(sCtx.in); 7758 sqlite3_free(sCtx.z); 7759 sqlite3_finalize(pStmt); 7760 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); 7761 }else 7762 7763#ifndef SQLITE_UNTESTABLE 7764 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ 7765 char *zSql; 7766 char *zCollist = 0; 7767 sqlite3_stmt *pStmt; 7768 int tnum = 0; 7769 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ 7770 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ 7771 int i; 7772 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ 7773 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" 7774 " .imposter off\n"); 7775 /* Also allowed, but not documented: 7776 ** 7777 ** .imposter TABLE IMPOSTER 7778 ** 7779 ** where TABLE is a WITHOUT ROWID table. In that case, the 7780 ** imposter is another WITHOUT ROWID table with the columns in 7781 ** storage order. */ 7782 rc = 1; 7783 goto meta_command_exit; 7784 } 7785 open_db(p, 0); 7786 if( nArg==2 ){ 7787 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); 7788 goto meta_command_exit; 7789 } 7790 zSql = sqlite3_mprintf( 7791 "SELECT rootpage, 0 FROM sqlite_master" 7792 " WHERE name='%q' AND type='index'" 7793 "UNION ALL " 7794 "SELECT rootpage, 1 FROM sqlite_master" 7795 " WHERE name='%q' AND type='table'" 7796 " AND sql LIKE '%%without%%rowid%%'", 7797 azArg[1], azArg[1] 7798 ); 7799 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7800 sqlite3_free(zSql); 7801 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 7802 tnum = sqlite3_column_int(pStmt, 0); 7803 isWO = sqlite3_column_int(pStmt, 1); 7804 } 7805 sqlite3_finalize(pStmt); 7806 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); 7807 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7808 sqlite3_free(zSql); 7809 i = 0; 7810 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 7811 char zLabel[20]; 7812 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); 7813 i++; 7814 if( zCol==0 ){ 7815 if( sqlite3_column_int(pStmt,1)==-1 ){ 7816 zCol = "_ROWID_"; 7817 }else{ 7818 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); 7819 zCol = zLabel; 7820 } 7821 } 7822 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ 7823 lenPK = (int)strlen(zCollist); 7824 } 7825 if( zCollist==0 ){ 7826 zCollist = sqlite3_mprintf("\"%w\"", zCol); 7827 }else{ 7828 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); 7829 } 7830 } 7831 sqlite3_finalize(pStmt); 7832 if( i==0 || tnum==0 ){ 7833 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); 7834 rc = 1; 7835 sqlite3_free(zCollist); 7836 goto meta_command_exit; 7837 } 7838 if( lenPK==0 ) lenPK = 100000; 7839 zSql = sqlite3_mprintf( 7840 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", 7841 azArg[2], zCollist, lenPK, zCollist); 7842 sqlite3_free(zCollist); 7843 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); 7844 if( rc==SQLITE_OK ){ 7845 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 7846 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); 7847 if( rc ){ 7848 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); 7849 }else{ 7850 utf8_printf(stdout, "%s;\n", zSql); 7851 raw_printf(stdout, 7852 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", 7853 azArg[1], isWO ? "table" : "index" 7854 ); 7855 } 7856 }else{ 7857 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); 7858 rc = 1; 7859 } 7860 sqlite3_free(zSql); 7861 }else 7862#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ 7863 7864#ifdef SQLITE_ENABLE_IOTRACE 7865 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ 7866 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); 7867 if( iotrace && iotrace!=stdout ) fclose(iotrace); 7868 iotrace = 0; 7869 if( nArg<2 ){ 7870 sqlite3IoTrace = 0; 7871 }else if( strcmp(azArg[1], "-")==0 ){ 7872 sqlite3IoTrace = iotracePrintf; 7873 iotrace = stdout; 7874 }else{ 7875 iotrace = fopen(azArg[1], "w"); 7876 if( iotrace==0 ){ 7877 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 7878 sqlite3IoTrace = 0; 7879 rc = 1; 7880 }else{ 7881 sqlite3IoTrace = iotracePrintf; 7882 } 7883 } 7884 }else 7885#endif 7886 7887 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ 7888 static const struct { 7889 const char *zLimitName; /* Name of a limit */ 7890 int limitCode; /* Integer code for that limit */ 7891 } aLimit[] = { 7892 { "length", SQLITE_LIMIT_LENGTH }, 7893 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, 7894 { "column", SQLITE_LIMIT_COLUMN }, 7895 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, 7896 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, 7897 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, 7898 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, 7899 { "attached", SQLITE_LIMIT_ATTACHED }, 7900 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 7901 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, 7902 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, 7903 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, 7904 }; 7905 int i, n2; 7906 open_db(p, 0); 7907 if( nArg==1 ){ 7908 for(i=0; i<ArraySize(aLimit); i++){ 7909 printf("%20s %d\n", aLimit[i].zLimitName, 7910 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); 7911 } 7912 }else if( nArg>3 ){ 7913 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); 7914 rc = 1; 7915 goto meta_command_exit; 7916 }else{ 7917 int iLimit = -1; 7918 n2 = strlen30(azArg[1]); 7919 for(i=0; i<ArraySize(aLimit); i++){ 7920 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ 7921 if( iLimit<0 ){ 7922 iLimit = i; 7923 }else{ 7924 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); 7925 rc = 1; 7926 goto meta_command_exit; 7927 } 7928 } 7929 } 7930 if( iLimit<0 ){ 7931 utf8_printf(stderr, "unknown limit: \"%s\"\n" 7932 "enter \".limits\" with no arguments for a list.\n", 7933 azArg[1]); 7934 rc = 1; 7935 goto meta_command_exit; 7936 } 7937 if( nArg==3 ){ 7938 sqlite3_limit(p->db, aLimit[iLimit].limitCode, 7939 (int)integerValue(azArg[2])); 7940 } 7941 printf("%20s %d\n", aLimit[iLimit].zLimitName, 7942 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); 7943 } 7944 }else 7945 7946 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ 7947 open_db(p, 0); 7948 lintDotCommand(p, azArg, nArg); 7949 }else 7950 7951#ifndef SQLITE_OMIT_LOAD_EXTENSION 7952 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ 7953 const char *zFile, *zProc; 7954 char *zErrMsg = 0; 7955 if( nArg<2 ){ 7956 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); 7957 rc = 1; 7958 goto meta_command_exit; 7959 } 7960 zFile = azArg[1]; 7961 zProc = nArg>=3 ? azArg[2] : 0; 7962 open_db(p, 0); 7963 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); 7964 if( rc!=SQLITE_OK ){ 7965 utf8_printf(stderr, "Error: %s\n", zErrMsg); 7966 sqlite3_free(zErrMsg); 7967 rc = 1; 7968 } 7969 }else 7970#endif 7971 7972 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ 7973 if( nArg!=2 ){ 7974 raw_printf(stderr, "Usage: .log FILENAME\n"); 7975 rc = 1; 7976 }else{ 7977 const char *zFile = azArg[1]; 7978 output_file_close(p->pLog); 7979 p->pLog = output_file_open(zFile, 0); 7980 } 7981 }else 7982 7983 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ 7984 const char *zMode = nArg>=2 ? azArg[1] : ""; 7985 int n2 = strlen30(zMode); 7986 int c2 = zMode[0]; 7987 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ 7988 p->mode = MODE_Line; 7989 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7990 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ 7991 p->mode = MODE_Column; 7992 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7993 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ 7994 p->mode = MODE_List; 7995 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); 7996 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7997 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ 7998 p->mode = MODE_Html; 7999 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ 8000 p->mode = MODE_Tcl; 8001 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); 8002 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8003 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ 8004 p->mode = MODE_Csv; 8005 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8006 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8007 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ 8008 p->mode = MODE_List; 8009 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); 8010 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ 8011 p->mode = MODE_Insert; 8012 set_table_name(p, nArg>=3 ? azArg[2] : "table"); 8013 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ 8014 p->mode = MODE_Quote; 8015 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ 8016 p->mode = MODE_Ascii; 8017 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); 8018 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); 8019 }else if( nArg==1 ){ 8020 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); 8021 }else{ 8022 raw_printf(stderr, "Error: mode should be one of: " 8023 "ascii column csv html insert line list quote tabs tcl\n"); 8024 rc = 1; 8025 } 8026 p->cMode = p->mode; 8027 }else 8028 8029 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ 8030 if( nArg==2 ){ 8031 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, 8032 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); 8033 }else{ 8034 raw_printf(stderr, "Usage: .nullvalue STRING\n"); 8035 rc = 1; 8036 } 8037 }else 8038 8039 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ 8040 char *zNewFilename; /* Name of the database file to open */ 8041 int iName = 1; /* Index in azArg[] of the filename */ 8042 int newFlag = 0; /* True to delete file before opening */ 8043 /* Close the existing database */ 8044 session_close_all(p); 8045 close_db(p->db); 8046 p->db = 0; 8047 p->zDbFilename = 0; 8048 sqlite3_free(p->zFreeOnClose); 8049 p->zFreeOnClose = 0; 8050 p->openMode = SHELL_OPEN_UNSPEC; 8051 p->openFlags = 0; 8052 p->szMax = 0; 8053 /* Check for command-line arguments */ 8054 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ 8055 const char *z = azArg[iName]; 8056 if( optionMatch(z,"new") ){ 8057 newFlag = 1; 8058#ifdef SQLITE_HAVE_ZLIB 8059 }else if( optionMatch(z, "zip") ){ 8060 p->openMode = SHELL_OPEN_ZIPFILE; 8061#endif 8062 }else if( optionMatch(z, "append") ){ 8063 p->openMode = SHELL_OPEN_APPENDVFS; 8064 }else if( optionMatch(z, "readonly") ){ 8065 p->openMode = SHELL_OPEN_READONLY; 8066 }else if( optionMatch(z, "nofollow") ){ 8067 p->openFlags |= SQLITE_OPEN_NOFOLLOW; 8068#ifdef SQLITE_ENABLE_DESERIALIZE 8069 }else if( optionMatch(z, "deserialize") ){ 8070 p->openMode = SHELL_OPEN_DESERIALIZE; 8071 }else if( optionMatch(z, "hexdb") ){ 8072 p->openMode = SHELL_OPEN_HEXDB; 8073 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ 8074 p->szMax = integerValue(azArg[++iName]); 8075#endif /* SQLITE_ENABLE_DESERIALIZE */ 8076 }else if( z[0]=='-' ){ 8077 utf8_printf(stderr, "unknown option: %s\n", z); 8078 rc = 1; 8079 goto meta_command_exit; 8080 } 8081 } 8082 /* If a filename is specified, try to open it first */ 8083 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; 8084 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){ 8085 if( newFlag ) shellDeleteFile(zNewFilename); 8086 p->zDbFilename = zNewFilename; 8087 open_db(p, OPEN_DB_KEEPALIVE); 8088 if( p->db==0 ){ 8089 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); 8090 sqlite3_free(zNewFilename); 8091 }else{ 8092 p->zFreeOnClose = zNewFilename; 8093 } 8094 } 8095 if( p->db==0 ){ 8096 /* As a fall-back open a TEMP database */ 8097 p->zDbFilename = 0; 8098 open_db(p, 0); 8099 } 8100 }else 8101 8102 if( (c=='o' 8103 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) 8104 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) 8105 ){ 8106 const char *zFile = nArg>=2 ? azArg[1] : "stdout"; 8107 int bTxtMode = 0; 8108 if( azArg[0][0]=='e' ){ 8109 /* Transform the ".excel" command into ".once -x" */ 8110 nArg = 2; 8111 azArg[0] = "once"; 8112 zFile = azArg[1] = "-x"; 8113 n = 4; 8114 } 8115 if( nArg>2 ){ 8116 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]); 8117 rc = 1; 8118 goto meta_command_exit; 8119 } 8120 if( n>1 && strncmp(azArg[0], "once", n)==0 ){ 8121 if( nArg<2 ){ 8122 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n"); 8123 rc = 1; 8124 goto meta_command_exit; 8125 } 8126 p->outCount = 2; 8127 }else{ 8128 p->outCount = 0; 8129 } 8130 output_reset(p); 8131 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++; 8132#ifndef SQLITE_NOHAVE_SYSTEM 8133 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){ 8134 p->doXdgOpen = 1; 8135 outputModePush(p); 8136 if( zFile[1]=='x' ){ 8137 newTempFile(p, "csv"); 8138 p->mode = MODE_Csv; 8139 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8140 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8141 }else{ 8142 newTempFile(p, "txt"); 8143 bTxtMode = 1; 8144 } 8145 zFile = p->zTempFile; 8146 } 8147#endif /* SQLITE_NOHAVE_SYSTEM */ 8148 if( zFile[0]=='|' ){ 8149#ifdef SQLITE_OMIT_POPEN 8150 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8151 rc = 1; 8152 p->out = stdout; 8153#else 8154 p->out = popen(zFile + 1, "w"); 8155 if( p->out==0 ){ 8156 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); 8157 p->out = stdout; 8158 rc = 1; 8159 }else{ 8160 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8161 } 8162#endif 8163 }else{ 8164 p->out = output_file_open(zFile, bTxtMode); 8165 if( p->out==0 ){ 8166 if( strcmp(zFile,"off")!=0 ){ 8167 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); 8168 } 8169 p->out = stdout; 8170 rc = 1; 8171 } else { 8172 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8173 } 8174 } 8175 }else 8176 8177 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ 8178 open_db(p,0); 8179 if( nArg<=1 ) goto parameter_syntax_error; 8180 8181 /* .parameter clear 8182 ** Clear all bind parameters by dropping the TEMP table that holds them. 8183 */ 8184 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ 8185 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", 8186 0, 0, 0); 8187 }else 8188 8189 /* .parameter list 8190 ** List all bind parameters. 8191 */ 8192 if( nArg==2 && strcmp(azArg[1],"list")==0 ){ 8193 sqlite3_stmt *pStmt = 0; 8194 int rx; 8195 int len = 0; 8196 rx = sqlite3_prepare_v2(p->db, 8197 "SELECT max(length(key)) " 8198 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8199 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 8200 len = sqlite3_column_int(pStmt, 0); 8201 if( len>40 ) len = 40; 8202 } 8203 sqlite3_finalize(pStmt); 8204 pStmt = 0; 8205 if( len ){ 8206 rx = sqlite3_prepare_v2(p->db, 8207 "SELECT key, quote(value) " 8208 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8209 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8210 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), 8211 sqlite3_column_text(pStmt,1)); 8212 } 8213 sqlite3_finalize(pStmt); 8214 } 8215 }else 8216 8217 /* .parameter init 8218 ** Make sure the TEMP table used to hold bind parameters exists. 8219 ** Create it if necessary. 8220 */ 8221 if( nArg==2 && strcmp(azArg[1],"init")==0 ){ 8222 bind_table_init(p); 8223 }else 8224 8225 /* .parameter set NAME VALUE 8226 ** Set or reset a bind parameter. NAME should be the full parameter 8227 ** name exactly as it appears in the query. (ex: $abc, @def). The 8228 ** VALUE can be in either SQL literal notation, or if not it will be 8229 ** understood to be a text string. 8230 */ 8231 if( nArg==4 && strcmp(azArg[1],"set")==0 ){ 8232 int rx; 8233 char *zSql; 8234 sqlite3_stmt *pStmt; 8235 const char *zKey = azArg[2]; 8236 const char *zValue = azArg[3]; 8237 bind_table_init(p); 8238 zSql = sqlite3_mprintf( 8239 "REPLACE INTO temp.sqlite_parameters(key,value)" 8240 "VALUES(%Q,%s);", zKey, zValue); 8241 if( zSql==0 ) shell_out_of_memory(); 8242 pStmt = 0; 8243 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8244 sqlite3_free(zSql); 8245 if( rx!=SQLITE_OK ){ 8246 sqlite3_finalize(pStmt); 8247 pStmt = 0; 8248 zSql = sqlite3_mprintf( 8249 "REPLACE INTO temp.sqlite_parameters(key,value)" 8250 "VALUES(%Q,%Q);", zKey, zValue); 8251 if( zSql==0 ) shell_out_of_memory(); 8252 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8253 sqlite3_free(zSql); 8254 if( rx!=SQLITE_OK ){ 8255 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); 8256 sqlite3_finalize(pStmt); 8257 pStmt = 0; 8258 rc = 1; 8259 } 8260 } 8261 sqlite3_step(pStmt); 8262 sqlite3_finalize(pStmt); 8263 }else 8264 8265 /* .parameter unset NAME 8266 ** Remove the NAME binding from the parameter binding table, if it 8267 ** exists. 8268 */ 8269 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ 8270 char *zSql = sqlite3_mprintf( 8271 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); 8272 if( zSql==0 ) shell_out_of_memory(); 8273 sqlite3_exec(p->db, zSql, 0, 0, 0); 8274 sqlite3_free(zSql); 8275 }else 8276 /* If no command name matches, show a syntax error */ 8277 parameter_syntax_error: 8278 showHelp(p->out, "parameter"); 8279 }else 8280 8281 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ 8282 int i; 8283 for(i=1; i<nArg; i++){ 8284 if( i>1 ) raw_printf(p->out, " "); 8285 utf8_printf(p->out, "%s", azArg[i]); 8286 } 8287 raw_printf(p->out, "\n"); 8288 }else 8289 8290#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 8291 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ 8292 int i; 8293 int nn = 0; 8294 p->flgProgress = 0; 8295 p->mxProgress = 0; 8296 p->nProgress = 0; 8297 for(i=1; i<nArg; i++){ 8298 const char *z = azArg[i]; 8299 if( z[0]=='-' ){ 8300 z++; 8301 if( z[0]=='-' ) z++; 8302 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 8303 p->flgProgress |= SHELL_PROGRESS_QUIET; 8304 continue; 8305 } 8306 if( strcmp(z,"reset")==0 ){ 8307 p->flgProgress |= SHELL_PROGRESS_RESET; 8308 continue; 8309 } 8310 if( strcmp(z,"once")==0 ){ 8311 p->flgProgress |= SHELL_PROGRESS_ONCE; 8312 continue; 8313 } 8314 if( strcmp(z,"limit")==0 ){ 8315 if( i+1>=nArg ){ 8316 utf8_printf(stderr, "Error: missing argument on --limit\n"); 8317 rc = 1; 8318 goto meta_command_exit; 8319 }else{ 8320 p->mxProgress = (int)integerValue(azArg[++i]); 8321 } 8322 continue; 8323 } 8324 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); 8325 rc = 1; 8326 goto meta_command_exit; 8327 }else{ 8328 nn = (int)integerValue(z); 8329 } 8330 } 8331 open_db(p, 0); 8332 sqlite3_progress_handler(p->db, nn, progress_handler, p); 8333 }else 8334#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 8335 8336 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ 8337 if( nArg >= 2) { 8338 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); 8339 } 8340 if( nArg >= 3) { 8341 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); 8342 } 8343 }else 8344 8345 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ 8346 rc = 2; 8347 }else 8348 8349 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ 8350 FILE *inSaved = p->in; 8351 int savedLineno = p->lineno; 8352 if( nArg!=2 ){ 8353 raw_printf(stderr, "Usage: .read FILE\n"); 8354 rc = 1; 8355 goto meta_command_exit; 8356 } 8357 p->in = fopen(azArg[1], "rb"); 8358 if( p->in==0 ){ 8359 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); 8360 rc = 1; 8361 }else{ 8362 rc = process_input(p); 8363 fclose(p->in); 8364 } 8365 p->in = inSaved; 8366 p->lineno = savedLineno; 8367 }else 8368 8369 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ 8370 const char *zSrcFile; 8371 const char *zDb; 8372 sqlite3 *pSrc; 8373 sqlite3_backup *pBackup; 8374 int nTimeout = 0; 8375 8376 if( nArg==2 ){ 8377 zSrcFile = azArg[1]; 8378 zDb = "main"; 8379 }else if( nArg==3 ){ 8380 zSrcFile = azArg[2]; 8381 zDb = azArg[1]; 8382 }else{ 8383 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); 8384 rc = 1; 8385 goto meta_command_exit; 8386 } 8387 rc = sqlite3_open(zSrcFile, &pSrc); 8388 if( rc!=SQLITE_OK ){ 8389 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); 8390 close_db(pSrc); 8391 return 1; 8392 } 8393 open_db(p, 0); 8394 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); 8395 if( pBackup==0 ){ 8396 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8397 close_db(pSrc); 8398 return 1; 8399 } 8400 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 8401 || rc==SQLITE_BUSY ){ 8402 if( rc==SQLITE_BUSY ){ 8403 if( nTimeout++ >= 3 ) break; 8404 sqlite3_sleep(100); 8405 } 8406 } 8407 sqlite3_backup_finish(pBackup); 8408 if( rc==SQLITE_DONE ){ 8409 rc = 0; 8410 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 8411 raw_printf(stderr, "Error: source database is busy\n"); 8412 rc = 1; 8413 }else{ 8414 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8415 rc = 1; 8416 } 8417 close_db(pSrc); 8418 }else 8419 8420 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ 8421 if( nArg==2 ){ 8422 p->scanstatsOn = (u8)booleanValue(azArg[1]); 8423#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 8424 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); 8425#endif 8426 }else{ 8427 raw_printf(stderr, "Usage: .scanstats on|off\n"); 8428 rc = 1; 8429 } 8430 }else 8431 8432 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ 8433 ShellText sSelect; 8434 ShellState data; 8435 char *zErrMsg = 0; 8436 const char *zDiv = "("; 8437 const char *zName = 0; 8438 int iSchema = 0; 8439 int bDebug = 0; 8440 int ii; 8441 8442 open_db(p, 0); 8443 memcpy(&data, p, sizeof(data)); 8444 data.showHeader = 0; 8445 data.cMode = data.mode = MODE_Semi; 8446 initText(&sSelect); 8447 for(ii=1; ii<nArg; ii++){ 8448 if( optionMatch(azArg[ii],"indent") ){ 8449 data.cMode = data.mode = MODE_Pretty; 8450 }else if( optionMatch(azArg[ii],"debug") ){ 8451 bDebug = 1; 8452 }else if( zName==0 ){ 8453 zName = azArg[ii]; 8454 }else{ 8455 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); 8456 rc = 1; 8457 goto meta_command_exit; 8458 } 8459 } 8460 if( zName!=0 ){ 8461 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0; 8462 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){ 8463 char *new_argv[2], *new_colv[2]; 8464 new_argv[0] = sqlite3_mprintf( 8465 "CREATE TABLE %s (\n" 8466 " type text,\n" 8467 " name text,\n" 8468 " tbl_name text,\n" 8469 " rootpage integer,\n" 8470 " sql text\n" 8471 ")", isMaster ? "sqlite_master" : "sqlite_temp_master"); 8472 new_argv[1] = 0; 8473 new_colv[0] = "sql"; 8474 new_colv[1] = 0; 8475 callback(&data, 1, new_argv, new_colv); 8476 sqlite3_free(new_argv[0]); 8477 } 8478 } 8479 if( zDiv ){ 8480 sqlite3_stmt *pStmt = 0; 8481 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", 8482 -1, &pStmt, 0); 8483 if( rc ){ 8484 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8485 sqlite3_finalize(pStmt); 8486 rc = 1; 8487 goto meta_command_exit; 8488 } 8489 appendText(&sSelect, "SELECT sql FROM", 0); 8490 iSchema = 0; 8491 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8492 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); 8493 char zScNum[30]; 8494 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); 8495 appendText(&sSelect, zDiv, 0); 8496 zDiv = " UNION ALL "; 8497 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); 8498 if( sqlite3_stricmp(zDb, "main")!=0 ){ 8499 appendText(&sSelect, zDb, '\''); 8500 }else{ 8501 appendText(&sSelect, "NULL", 0); 8502 } 8503 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); 8504 appendText(&sSelect, zScNum, 0); 8505 appendText(&sSelect, " AS snum, ", 0); 8506 appendText(&sSelect, zDb, '\''); 8507 appendText(&sSelect, " AS sname FROM ", 0); 8508 appendText(&sSelect, zDb, quoteChar(zDb)); 8509 appendText(&sSelect, ".sqlite_master", 0); 8510 } 8511 sqlite3_finalize(pStmt); 8512#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 8513 if( zName ){ 8514 appendText(&sSelect, 8515 " UNION ALL SELECT shell_module_schema(name)," 8516 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 8517 0); 8518 } 8519#endif 8520 appendText(&sSelect, ") WHERE ", 0); 8521 if( zName ){ 8522 char *zQarg = sqlite3_mprintf("%Q", zName); 8523 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || 8524 strchr(zName, '[') != 0; 8525 if( strchr(zName, '.') ){ 8526 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); 8527 }else{ 8528 appendText(&sSelect, "lower(tbl_name)", 0); 8529 } 8530 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); 8531 appendText(&sSelect, zQarg, 0); 8532 if( !bGlob ){ 8533 appendText(&sSelect, " ESCAPE '\\' ", 0); 8534 } 8535 appendText(&sSelect, " AND ", 0); 8536 sqlite3_free(zQarg); 8537 } 8538 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" 8539 " ORDER BY snum, rowid", 0); 8540 if( bDebug ){ 8541 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); 8542 }else{ 8543 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); 8544 } 8545 freeText(&sSelect); 8546 } 8547 if( zErrMsg ){ 8548 utf8_printf(stderr,"Error: %s\n", zErrMsg); 8549 sqlite3_free(zErrMsg); 8550 rc = 1; 8551 }else if( rc != SQLITE_OK ){ 8552 raw_printf(stderr,"Error: querying schema information\n"); 8553 rc = 1; 8554 }else{ 8555 rc = 0; 8556 } 8557 }else 8558 8559#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 8560 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ 8561 sqlite3SelectTrace = (int)integerValue(azArg[1]); 8562 }else 8563#endif 8564 8565#if defined(SQLITE_ENABLE_SESSION) 8566 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ 8567 OpenSession *pSession = &p->aSession[0]; 8568 char **azCmd = &azArg[1]; 8569 int iSes = 0; 8570 int nCmd = nArg - 1; 8571 int i; 8572 if( nArg<=1 ) goto session_syntax_error; 8573 open_db(p, 0); 8574 if( nArg>=3 ){ 8575 for(iSes=0; iSes<p->nSession; iSes++){ 8576 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; 8577 } 8578 if( iSes<p->nSession ){ 8579 pSession = &p->aSession[iSes]; 8580 azCmd++; 8581 nCmd--; 8582 }else{ 8583 pSession = &p->aSession[0]; 8584 iSes = 0; 8585 } 8586 } 8587 8588 /* .session attach TABLE 8589 ** Invoke the sqlite3session_attach() interface to attach a particular 8590 ** table so that it is never filtered. 8591 */ 8592 if( strcmp(azCmd[0],"attach")==0 ){ 8593 if( nCmd!=2 ) goto session_syntax_error; 8594 if( pSession->p==0 ){ 8595 session_not_open: 8596 raw_printf(stderr, "ERROR: No sessions are open\n"); 8597 }else{ 8598 rc = sqlite3session_attach(pSession->p, azCmd[1]); 8599 if( rc ){ 8600 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); 8601 rc = 0; 8602 } 8603 } 8604 }else 8605 8606 /* .session changeset FILE 8607 ** .session patchset FILE 8608 ** Write a changeset or patchset into a file. The file is overwritten. 8609 */ 8610 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ 8611 FILE *out = 0; 8612 if( nCmd!=2 ) goto session_syntax_error; 8613 if( pSession->p==0 ) goto session_not_open; 8614 out = fopen(azCmd[1], "wb"); 8615 if( out==0 ){ 8616 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", 8617 azCmd[1]); 8618 }else{ 8619 int szChng; 8620 void *pChng; 8621 if( azCmd[0][0]=='c' ){ 8622 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); 8623 }else{ 8624 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); 8625 } 8626 if( rc ){ 8627 printf("Error: error code %d\n", rc); 8628 rc = 0; 8629 } 8630 if( pChng 8631 && fwrite(pChng, szChng, 1, out)!=1 ){ 8632 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", 8633 szChng); 8634 } 8635 sqlite3_free(pChng); 8636 fclose(out); 8637 } 8638 }else 8639 8640 /* .session close 8641 ** Close the identified session 8642 */ 8643 if( strcmp(azCmd[0], "close")==0 ){ 8644 if( nCmd!=1 ) goto session_syntax_error; 8645 if( p->nSession ){ 8646 session_close(pSession); 8647 p->aSession[iSes] = p->aSession[--p->nSession]; 8648 } 8649 }else 8650 8651 /* .session enable ?BOOLEAN? 8652 ** Query or set the enable flag 8653 */ 8654 if( strcmp(azCmd[0], "enable")==0 ){ 8655 int ii; 8656 if( nCmd>2 ) goto session_syntax_error; 8657 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 8658 if( p->nSession ){ 8659 ii = sqlite3session_enable(pSession->p, ii); 8660 utf8_printf(p->out, "session %s enable flag = %d\n", 8661 pSession->zName, ii); 8662 } 8663 }else 8664 8665 /* .session filter GLOB .... 8666 ** Set a list of GLOB patterns of table names to be excluded. 8667 */ 8668 if( strcmp(azCmd[0], "filter")==0 ){ 8669 int ii, nByte; 8670 if( nCmd<2 ) goto session_syntax_error; 8671 if( p->nSession ){ 8672 for(ii=0; ii<pSession->nFilter; ii++){ 8673 sqlite3_free(pSession->azFilter[ii]); 8674 } 8675 sqlite3_free(pSession->azFilter); 8676 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); 8677 pSession->azFilter = sqlite3_malloc( nByte ); 8678 if( pSession->azFilter==0 ){ 8679 raw_printf(stderr, "Error: out or memory\n"); 8680 exit(1); 8681 } 8682 for(ii=1; ii<nCmd; ii++){ 8683 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); 8684 } 8685 pSession->nFilter = ii-1; 8686 } 8687 }else 8688 8689 /* .session indirect ?BOOLEAN? 8690 ** Query or set the indirect flag 8691 */ 8692 if( strcmp(azCmd[0], "indirect")==0 ){ 8693 int ii; 8694 if( nCmd>2 ) goto session_syntax_error; 8695 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 8696 if( p->nSession ){ 8697 ii = sqlite3session_indirect(pSession->p, ii); 8698 utf8_printf(p->out, "session %s indirect flag = %d\n", 8699 pSession->zName, ii); 8700 } 8701 }else 8702 8703 /* .session isempty 8704 ** Determine if the session is empty 8705 */ 8706 if( strcmp(azCmd[0], "isempty")==0 ){ 8707 int ii; 8708 if( nCmd!=1 ) goto session_syntax_error; 8709 if( p->nSession ){ 8710 ii = sqlite3session_isempty(pSession->p); 8711 utf8_printf(p->out, "session %s isempty flag = %d\n", 8712 pSession->zName, ii); 8713 } 8714 }else 8715 8716 /* .session list 8717 ** List all currently open sessions 8718 */ 8719 if( strcmp(azCmd[0],"list")==0 ){ 8720 for(i=0; i<p->nSession; i++){ 8721 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); 8722 } 8723 }else 8724 8725 /* .session open DB NAME 8726 ** Open a new session called NAME on the attached database DB. 8727 ** DB is normally "main". 8728 */ 8729 if( strcmp(azCmd[0],"open")==0 ){ 8730 char *zName; 8731 if( nCmd!=3 ) goto session_syntax_error; 8732 zName = azCmd[2]; 8733 if( zName[0]==0 ) goto session_syntax_error; 8734 for(i=0; i<p->nSession; i++){ 8735 if( strcmp(p->aSession[i].zName,zName)==0 ){ 8736 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); 8737 goto meta_command_exit; 8738 } 8739 } 8740 if( p->nSession>=ArraySize(p->aSession) ){ 8741 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); 8742 goto meta_command_exit; 8743 } 8744 pSession = &p->aSession[p->nSession]; 8745 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); 8746 if( rc ){ 8747 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); 8748 rc = 0; 8749 goto meta_command_exit; 8750 } 8751 pSession->nFilter = 0; 8752 sqlite3session_table_filter(pSession->p, session_filter, pSession); 8753 p->nSession++; 8754 pSession->zName = sqlite3_mprintf("%s", zName); 8755 }else 8756 /* If no command name matches, show a syntax error */ 8757 session_syntax_error: 8758 showHelp(p->out, "session"); 8759 }else 8760#endif 8761 8762#ifdef SQLITE_DEBUG 8763 /* Undocumented commands for internal testing. Subject to change 8764 ** without notice. */ 8765 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ 8766 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ 8767 int i, v; 8768 for(i=1; i<nArg; i++){ 8769 v = booleanValue(azArg[i]); 8770 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); 8771 } 8772 } 8773 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ 8774 int i; sqlite3_int64 v; 8775 for(i=1; i<nArg; i++){ 8776 char zBuf[200]; 8777 v = integerValue(azArg[i]); 8778 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); 8779 utf8_printf(p->out, "%s", zBuf); 8780 } 8781 } 8782 }else 8783#endif 8784 8785 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ 8786 int bIsInit = 0; /* True to initialize the SELFTEST table */ 8787 int bVerbose = 0; /* Verbose output */ 8788 int bSelftestExists; /* True if SELFTEST already exists */ 8789 int i, k; /* Loop counters */ 8790 int nTest = 0; /* Number of tests runs */ 8791 int nErr = 0; /* Number of errors seen */ 8792 ShellText str; /* Answer for a query */ 8793 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ 8794 8795 open_db(p,0); 8796 for(i=1; i<nArg; i++){ 8797 const char *z = azArg[i]; 8798 if( z[0]=='-' && z[1]=='-' ) z++; 8799 if( strcmp(z,"-init")==0 ){ 8800 bIsInit = 1; 8801 }else 8802 if( strcmp(z,"-v")==0 ){ 8803 bVerbose++; 8804 }else 8805 { 8806 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 8807 azArg[i], azArg[0]); 8808 raw_printf(stderr, "Should be one of: --init -v\n"); 8809 rc = 1; 8810 goto meta_command_exit; 8811 } 8812 } 8813 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) 8814 != SQLITE_OK ){ 8815 bSelftestExists = 0; 8816 }else{ 8817 bSelftestExists = 1; 8818 } 8819 if( bIsInit ){ 8820 createSelftestTable(p); 8821 bSelftestExists = 1; 8822 } 8823 initText(&str); 8824 appendText(&str, "x", 0); 8825 for(k=bSelftestExists; k>=0; k--){ 8826 if( k==1 ){ 8827 rc = sqlite3_prepare_v2(p->db, 8828 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", 8829 -1, &pStmt, 0); 8830 }else{ 8831 rc = sqlite3_prepare_v2(p->db, 8832 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," 8833 " (1,'run','PRAGMA integrity_check','ok')", 8834 -1, &pStmt, 0); 8835 } 8836 if( rc ){ 8837 raw_printf(stderr, "Error querying the selftest table\n"); 8838 rc = 1; 8839 sqlite3_finalize(pStmt); 8840 goto meta_command_exit; 8841 } 8842 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ 8843 int tno = sqlite3_column_int(pStmt, 0); 8844 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); 8845 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); 8846 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); 8847 8848 k = 0; 8849 if( bVerbose>0 ){ 8850 char *zQuote = sqlite3_mprintf("%q", zSql); 8851 printf("%d: %s %s\n", tno, zOp, zSql); 8852 sqlite3_free(zQuote); 8853 } 8854 if( strcmp(zOp,"memo")==0 ){ 8855 utf8_printf(p->out, "%s\n", zSql); 8856 }else 8857 if( strcmp(zOp,"run")==0 ){ 8858 char *zErrMsg = 0; 8859 str.n = 0; 8860 str.z[0] = 0; 8861 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); 8862 nTest++; 8863 if( bVerbose ){ 8864 utf8_printf(p->out, "Result: %s\n", str.z); 8865 } 8866 if( rc || zErrMsg ){ 8867 nErr++; 8868 rc = 1; 8869 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); 8870 sqlite3_free(zErrMsg); 8871 }else if( strcmp(zAns,str.z)!=0 ){ 8872 nErr++; 8873 rc = 1; 8874 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); 8875 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); 8876 } 8877 }else 8878 { 8879 utf8_printf(stderr, 8880 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); 8881 rc = 1; 8882 break; 8883 } 8884 } /* End loop over rows of content from SELFTEST */ 8885 sqlite3_finalize(pStmt); 8886 } /* End loop over k */ 8887 freeText(&str); 8888 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); 8889 }else 8890 8891 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ 8892 if( nArg<2 || nArg>3 ){ 8893 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); 8894 rc = 1; 8895 } 8896 if( nArg>=2 ){ 8897 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, 8898 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); 8899 } 8900 if( nArg>=3 ){ 8901 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, 8902 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); 8903 } 8904 }else 8905 8906 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ 8907 const char *zLike = 0; /* Which table to checksum. 0 means everything */ 8908 int i; /* Loop counter */ 8909 int bSchema = 0; /* Also hash the schema */ 8910 int bSeparate = 0; /* Hash each table separately */ 8911 int iSize = 224; /* Hash algorithm to use */ 8912 int bDebug = 0; /* Only show the query that would have run */ 8913 sqlite3_stmt *pStmt; /* For querying tables names */ 8914 char *zSql; /* SQL to be run */ 8915 char *zSep; /* Separator */ 8916 ShellText sSql; /* Complete SQL for the query to run the hash */ 8917 ShellText sQuery; /* Set of queries used to read all content */ 8918 open_db(p, 0); 8919 for(i=1; i<nArg; i++){ 8920 const char *z = azArg[i]; 8921 if( z[0]=='-' ){ 8922 z++; 8923 if( z[0]=='-' ) z++; 8924 if( strcmp(z,"schema")==0 ){ 8925 bSchema = 1; 8926 }else 8927 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 8928 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 8929 ){ 8930 iSize = atoi(&z[5]); 8931 }else 8932 if( strcmp(z,"debug")==0 ){ 8933 bDebug = 1; 8934 }else 8935 { 8936 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 8937 azArg[i], azArg[0]); 8938 showHelp(p->out, azArg[0]); 8939 rc = 1; 8940 goto meta_command_exit; 8941 } 8942 }else if( zLike ){ 8943 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); 8944 rc = 1; 8945 goto meta_command_exit; 8946 }else{ 8947 zLike = z; 8948 bSeparate = 1; 8949 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; 8950 } 8951 } 8952 if( bSchema ){ 8953 zSql = "SELECT lower(name) FROM sqlite_master" 8954 " WHERE type='table' AND coalesce(rootpage,0)>1" 8955 " UNION ALL SELECT 'sqlite_master'" 8956 " ORDER BY 1 collate nocase"; 8957 }else{ 8958 zSql = "SELECT lower(name) FROM sqlite_master" 8959 " WHERE type='table' AND coalesce(rootpage,0)>1" 8960 " AND name NOT LIKE 'sqlite_%'" 8961 " ORDER BY 1 collate nocase"; 8962 } 8963 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8964 initText(&sQuery); 8965 initText(&sSql); 8966 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); 8967 zSep = "VALUES("; 8968 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 8969 const char *zTab = (const char*)sqlite3_column_text(pStmt,0); 8970 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; 8971 if( strncmp(zTab, "sqlite_",7)!=0 ){ 8972 appendText(&sQuery,"SELECT * FROM ", 0); 8973 appendText(&sQuery,zTab,'"'); 8974 appendText(&sQuery," NOT INDEXED;", 0); 8975 }else if( strcmp(zTab, "sqlite_master")==0 ){ 8976 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" 8977 " ORDER BY name;", 0); 8978 }else if( strcmp(zTab, "sqlite_sequence")==0 ){ 8979 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" 8980 " ORDER BY name;", 0); 8981 }else if( strcmp(zTab, "sqlite_stat1")==0 ){ 8982 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" 8983 " ORDER BY tbl,idx;", 0); 8984 }else if( strcmp(zTab, "sqlite_stat4")==0 ){ 8985 appendText(&sQuery, "SELECT * FROM ", 0); 8986 appendText(&sQuery, zTab, 0); 8987 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); 8988 } 8989 appendText(&sSql, zSep, 0); 8990 appendText(&sSql, sQuery.z, '\''); 8991 sQuery.n = 0; 8992 appendText(&sSql, ",", 0); 8993 appendText(&sSql, zTab, '\''); 8994 zSep = "),("; 8995 } 8996 sqlite3_finalize(pStmt); 8997 if( bSeparate ){ 8998 zSql = sqlite3_mprintf( 8999 "%s))" 9000 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" 9001 " FROM [sha3sum$query]", 9002 sSql.z, iSize); 9003 }else{ 9004 zSql = sqlite3_mprintf( 9005 "%s))" 9006 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" 9007 " FROM [sha3sum$query]", 9008 sSql.z, iSize); 9009 } 9010 freeText(&sQuery); 9011 freeText(&sSql); 9012 if( bDebug ){ 9013 utf8_printf(p->out, "%s\n", zSql); 9014 }else{ 9015 shell_exec(p, zSql, 0); 9016 } 9017 sqlite3_free(zSql); 9018 }else 9019 9020#ifndef SQLITE_NOHAVE_SYSTEM 9021 if( c=='s' 9022 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) 9023 ){ 9024 char *zCmd; 9025 int i, x; 9026 if( nArg<2 ){ 9027 raw_printf(stderr, "Usage: .system COMMAND\n"); 9028 rc = 1; 9029 goto meta_command_exit; 9030 } 9031 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); 9032 for(i=2; i<nArg; i++){ 9033 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", 9034 zCmd, azArg[i]); 9035 } 9036 x = system(zCmd); 9037 sqlite3_free(zCmd); 9038 if( x ) raw_printf(stderr, "System command returns %d\n", x); 9039 }else 9040#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 9041 9042 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ 9043 static const char *azBool[] = { "off", "on", "trigger", "full"}; 9044 int i; 9045 if( nArg!=1 ){ 9046 raw_printf(stderr, "Usage: .show\n"); 9047 rc = 1; 9048 goto meta_command_exit; 9049 } 9050 utf8_printf(p->out, "%12.12s: %s\n","echo", 9051 azBool[ShellHasFlag(p, SHFLG_Echo)]); 9052 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); 9053 utf8_printf(p->out, "%12.12s: %s\n","explain", 9054 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); 9055 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); 9056 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); 9057 utf8_printf(p->out, "%12.12s: ", "nullvalue"); 9058 output_c_string(p->out, p->nullValue); 9059 raw_printf(p->out, "\n"); 9060 utf8_printf(p->out,"%12.12s: %s\n","output", 9061 strlen30(p->outfile) ? p->outfile : "stdout"); 9062 utf8_printf(p->out,"%12.12s: ", "colseparator"); 9063 output_c_string(p->out, p->colSeparator); 9064 raw_printf(p->out, "\n"); 9065 utf8_printf(p->out,"%12.12s: ", "rowseparator"); 9066 output_c_string(p->out, p->rowSeparator); 9067 raw_printf(p->out, "\n"); 9068 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); 9069 utf8_printf(p->out, "%12.12s: ", "width"); 9070 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { 9071 raw_printf(p->out, "%d ", p->colWidth[i]); 9072 } 9073 raw_printf(p->out, "\n"); 9074 utf8_printf(p->out, "%12.12s: %s\n", "filename", 9075 p->zDbFilename ? p->zDbFilename : ""); 9076 }else 9077 9078 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ 9079 if( nArg==2 ){ 9080 p->statsOn = (u8)booleanValue(azArg[1]); 9081 }else if( nArg==1 ){ 9082 display_stats(p->db, p, 0); 9083 }else{ 9084 raw_printf(stderr, "Usage: .stats ?on|off?\n"); 9085 rc = 1; 9086 } 9087 }else 9088 9089 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) 9090 || (c=='i' && (strncmp(azArg[0], "indices", n)==0 9091 || strncmp(azArg[0], "indexes", n)==0) ) 9092 ){ 9093 sqlite3_stmt *pStmt; 9094 char **azResult; 9095 int nRow, nAlloc; 9096 int ii; 9097 ShellText s; 9098 initText(&s); 9099 open_db(p, 0); 9100 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 9101 if( rc ){ 9102 sqlite3_finalize(pStmt); 9103 return shellDatabaseError(p->db); 9104 } 9105 9106 if( nArg>2 && c=='i' ){ 9107 /* It is an historical accident that the .indexes command shows an error 9108 ** when called with the wrong number of arguments whereas the .tables 9109 ** command does not. */ 9110 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); 9111 rc = 1; 9112 sqlite3_finalize(pStmt); 9113 goto meta_command_exit; 9114 } 9115 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ 9116 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); 9117 if( zDbName==0 ) continue; 9118 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); 9119 if( sqlite3_stricmp(zDbName, "main")==0 ){ 9120 appendText(&s, "SELECT name FROM ", 0); 9121 }else{ 9122 appendText(&s, "SELECT ", 0); 9123 appendText(&s, zDbName, '\''); 9124 appendText(&s, "||'.'||name FROM ", 0); 9125 } 9126 appendText(&s, zDbName, '"'); 9127 appendText(&s, ".sqlite_master ", 0); 9128 if( c=='t' ){ 9129 appendText(&s," WHERE type IN ('table','view')" 9130 " AND name NOT LIKE 'sqlite_%'" 9131 " AND name LIKE ?1", 0); 9132 }else{ 9133 appendText(&s," WHERE type='index'" 9134 " AND tbl_name LIKE ?1", 0); 9135 } 9136 } 9137 rc = sqlite3_finalize(pStmt); 9138 appendText(&s, " ORDER BY 1", 0); 9139 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); 9140 freeText(&s); 9141 if( rc ) return shellDatabaseError(p->db); 9142 9143 /* Run the SQL statement prepared by the above block. Store the results 9144 ** as an array of nul-terminated strings in azResult[]. */ 9145 nRow = nAlloc = 0; 9146 azResult = 0; 9147 if( nArg>1 ){ 9148 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); 9149 }else{ 9150 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); 9151 } 9152 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9153 if( nRow>=nAlloc ){ 9154 char **azNew; 9155 int n2 = nAlloc*2 + 10; 9156 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); 9157 if( azNew==0 ) shell_out_of_memory(); 9158 nAlloc = n2; 9159 azResult = azNew; 9160 } 9161 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 9162 if( 0==azResult[nRow] ) shell_out_of_memory(); 9163 nRow++; 9164 } 9165 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ 9166 rc = shellDatabaseError(p->db); 9167 } 9168 9169 /* Pretty-print the contents of array azResult[] to the output */ 9170 if( rc==0 && nRow>0 ){ 9171 int len, maxlen = 0; 9172 int i, j; 9173 int nPrintCol, nPrintRow; 9174 for(i=0; i<nRow; i++){ 9175 len = strlen30(azResult[i]); 9176 if( len>maxlen ) maxlen = len; 9177 } 9178 nPrintCol = 80/(maxlen+2); 9179 if( nPrintCol<1 ) nPrintCol = 1; 9180 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; 9181 for(i=0; i<nPrintRow; i++){ 9182 for(j=i; j<nRow; j+=nPrintRow){ 9183 char *zSp = j<nPrintRow ? "" : " "; 9184 utf8_printf(p->out, "%s%-*s", zSp, maxlen, 9185 azResult[j] ? azResult[j]:""); 9186 } 9187 raw_printf(p->out, "\n"); 9188 } 9189 } 9190 9191 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); 9192 sqlite3_free(azResult); 9193 }else 9194 9195 /* Begin redirecting output to the file "testcase-out.txt" */ 9196 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ 9197 output_reset(p); 9198 p->out = output_file_open("testcase-out.txt", 0); 9199 if( p->out==0 ){ 9200 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); 9201 } 9202 if( nArg>=2 ){ 9203 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); 9204 }else{ 9205 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); 9206 } 9207 }else 9208 9209#ifndef SQLITE_UNTESTABLE 9210 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ 9211 static const struct { 9212 const char *zCtrlName; /* Name of a test-control option */ 9213 int ctrlCode; /* Integer code for that option */ 9214 const char *zUsage; /* Usage notes */ 9215 } aCtrl[] = { 9216 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, 9217 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, 9218 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ 9219 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ 9220 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, 9221 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" }, 9222 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/ 9223 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, 9224 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" }, 9225 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, 9226 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, 9227 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, 9228#ifdef YYCOVERAGE 9229 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, 9230#endif 9231 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, 9232 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, 9233 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, 9234 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" }, 9235 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE"}, 9236 }; 9237 int testctrl = -1; 9238 int iCtrl = -1; 9239 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ 9240 int isOk = 0; 9241 int i, n2; 9242 const char *zCmd = 0; 9243 9244 open_db(p, 0); 9245 zCmd = nArg>=2 ? azArg[1] : "help"; 9246 9247 /* The argument can optionally begin with "-" or "--" */ 9248 if( zCmd[0]=='-' && zCmd[1] ){ 9249 zCmd++; 9250 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 9251 } 9252 9253 /* --help lists all test-controls */ 9254 if( strcmp(zCmd,"help")==0 ){ 9255 utf8_printf(p->out, "Available test-controls:\n"); 9256 for(i=0; i<ArraySize(aCtrl); i++){ 9257 utf8_printf(p->out, " .testctrl %s %s\n", 9258 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 9259 } 9260 rc = 1; 9261 goto meta_command_exit; 9262 } 9263 9264 /* convert testctrl text option to value. allow any unique prefix 9265 ** of the option name, or a numerical value. */ 9266 n2 = strlen30(zCmd); 9267 for(i=0; i<ArraySize(aCtrl); i++){ 9268 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 9269 if( testctrl<0 ){ 9270 testctrl = aCtrl[i].ctrlCode; 9271 iCtrl = i; 9272 }else{ 9273 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" 9274 "Use \".testctrl --help\" for help\n", zCmd); 9275 rc = 1; 9276 goto meta_command_exit; 9277 } 9278 } 9279 } 9280 if( testctrl<0 ){ 9281 utf8_printf(stderr,"Error: unknown test-control: %s\n" 9282 "Use \".testctrl --help\" for help\n", zCmd); 9283 }else{ 9284 switch(testctrl){ 9285 9286 /* sqlite3_test_control(int, db, int) */ 9287 case SQLITE_TESTCTRL_OPTIMIZATIONS: 9288 case SQLITE_TESTCTRL_RESERVE: 9289 if( nArg==3 ){ 9290 int opt = (int)strtol(azArg[2], 0, 0); 9291 rc2 = sqlite3_test_control(testctrl, p->db, opt); 9292 isOk = 3; 9293 } 9294 break; 9295 9296 /* sqlite3_test_control(int) */ 9297 case SQLITE_TESTCTRL_PRNG_SAVE: 9298 case SQLITE_TESTCTRL_PRNG_RESTORE: 9299 case SQLITE_TESTCTRL_PRNG_RESET: 9300 case SQLITE_TESTCTRL_BYTEORDER: 9301 if( nArg==2 ){ 9302 rc2 = sqlite3_test_control(testctrl); 9303 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; 9304 } 9305 break; 9306 9307 /* sqlite3_test_control(int, uint) */ 9308 case SQLITE_TESTCTRL_PENDING_BYTE: 9309 if( nArg==3 ){ 9310 unsigned int opt = (unsigned int)integerValue(azArg[2]); 9311 rc2 = sqlite3_test_control(testctrl, opt); 9312 isOk = 3; 9313 } 9314 break; 9315 9316 /* sqlite3_test_control(int, int, sqlite3*) */ 9317 case SQLITE_TESTCTRL_PRNG_SEED: 9318 if( nArg==3 || nArg==4 ){ 9319 int ii = (int)integerValue(azArg[2]); 9320 sqlite3 *db; 9321 if( ii==0 && strcmp(azArg[2],"random")==0 ){ 9322 sqlite3_randomness(sizeof(ii),&ii); 9323 printf("-- random seed: %d\n", ii); 9324 } 9325 if( nArg==3 ){ 9326 db = 0; 9327 }else{ 9328 db = p->db; 9329 /* Make sure the schema has been loaded */ 9330 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); 9331 } 9332 rc2 = sqlite3_test_control(testctrl, ii, db); 9333 isOk = 3; 9334 } 9335 break; 9336 9337 /* sqlite3_test_control(int, int) */ 9338 case SQLITE_TESTCTRL_ASSERT: 9339 case SQLITE_TESTCTRL_ALWAYS: 9340 if( nArg==3 ){ 9341 int opt = booleanValue(azArg[2]); 9342 rc2 = sqlite3_test_control(testctrl, opt); 9343 isOk = 1; 9344 } 9345 break; 9346 9347 /* sqlite3_test_control(int, int) */ 9348 case SQLITE_TESTCTRL_LOCALTIME_FAULT: 9349 case SQLITE_TESTCTRL_NEVER_CORRUPT: 9350 if( nArg==3 ){ 9351 int opt = booleanValue(azArg[2]); 9352 rc2 = sqlite3_test_control(testctrl, opt); 9353 isOk = 3; 9354 } 9355 break; 9356 9357 /* sqlite3_test_control(sqlite3*) */ 9358 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: 9359 rc2 = sqlite3_test_control(testctrl, p->db); 9360 isOk = 3; 9361 break; 9362 9363 case SQLITE_TESTCTRL_IMPOSTER: 9364 if( nArg==5 ){ 9365 rc2 = sqlite3_test_control(testctrl, p->db, 9366 azArg[2], 9367 integerValue(azArg[3]), 9368 integerValue(azArg[4])); 9369 isOk = 3; 9370 } 9371 break; 9372 9373#ifdef YYCOVERAGE 9374 case SQLITE_TESTCTRL_PARSER_COVERAGE: 9375 if( nArg==2 ){ 9376 sqlite3_test_control(testctrl, p->out); 9377 isOk = 3; 9378 } 9379#endif 9380 } 9381 } 9382 if( isOk==0 && iCtrl>=0 ){ 9383 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 9384 rc = 1; 9385 }else if( isOk==1 ){ 9386 raw_printf(p->out, "%d\n", rc2); 9387 }else if( isOk==2 ){ 9388 raw_printf(p->out, "0x%08x\n", rc2); 9389 } 9390 }else 9391#endif /* !defined(SQLITE_UNTESTABLE) */ 9392 9393 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ 9394 open_db(p, 0); 9395 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); 9396 }else 9397 9398 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ 9399 if( nArg==2 ){ 9400 enableTimer = booleanValue(azArg[1]); 9401 if( enableTimer && !HAS_TIMER ){ 9402 raw_printf(stderr, "Error: timer not available on this system.\n"); 9403 enableTimer = 0; 9404 } 9405 }else{ 9406 raw_printf(stderr, "Usage: .timer on|off\n"); 9407 rc = 1; 9408 } 9409 }else 9410 9411#ifndef SQLITE_OMIT_TRACE 9412 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ 9413 int mType = 0; 9414 int jj; 9415 open_db(p, 0); 9416 for(jj=1; jj<nArg; jj++){ 9417 const char *z = azArg[jj]; 9418 if( z[0]=='-' ){ 9419 if( optionMatch(z, "expanded") ){ 9420 p->eTraceType = SHELL_TRACE_EXPANDED; 9421 } 9422#ifdef SQLITE_ENABLE_NORMALIZE 9423 else if( optionMatch(z, "normalized") ){ 9424 p->eTraceType = SHELL_TRACE_NORMALIZED; 9425 } 9426#endif 9427 else if( optionMatch(z, "plain") ){ 9428 p->eTraceType = SHELL_TRACE_PLAIN; 9429 } 9430 else if( optionMatch(z, "profile") ){ 9431 mType |= SQLITE_TRACE_PROFILE; 9432 } 9433 else if( optionMatch(z, "row") ){ 9434 mType |= SQLITE_TRACE_ROW; 9435 } 9436 else if( optionMatch(z, "stmt") ){ 9437 mType |= SQLITE_TRACE_STMT; 9438 } 9439 else if( optionMatch(z, "close") ){ 9440 mType |= SQLITE_TRACE_CLOSE; 9441 } 9442 else { 9443 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); 9444 rc = 1; 9445 goto meta_command_exit; 9446 } 9447 }else{ 9448 output_file_close(p->traceOut); 9449 p->traceOut = output_file_open(azArg[1], 0); 9450 } 9451 } 9452 if( p->traceOut==0 ){ 9453 sqlite3_trace_v2(p->db, 0, 0, 0); 9454 }else{ 9455 if( mType==0 ) mType = SQLITE_TRACE_STMT; 9456 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); 9457 } 9458 }else 9459#endif /* !defined(SQLITE_OMIT_TRACE) */ 9460 9461#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) 9462 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ 9463 int ii; 9464 int lenOpt; 9465 char *zOpt; 9466 if( nArg<2 ){ 9467 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); 9468 rc = 1; 9469 goto meta_command_exit; 9470 } 9471 open_db(p, 0); 9472 zOpt = azArg[1]; 9473 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; 9474 lenOpt = (int)strlen(zOpt); 9475 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ 9476 assert( azArg[nArg]==0 ); 9477 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); 9478 }else{ 9479 for(ii=1; ii<nArg; ii++){ 9480 sqlite3_create_module(p->db, azArg[ii], 0, 0); 9481 } 9482 } 9483 }else 9484#endif 9485 9486#if SQLITE_USER_AUTHENTICATION 9487 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ 9488 if( nArg<2 ){ 9489 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); 9490 rc = 1; 9491 goto meta_command_exit; 9492 } 9493 open_db(p, 0); 9494 if( strcmp(azArg[1],"login")==0 ){ 9495 if( nArg!=4 ){ 9496 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); 9497 rc = 1; 9498 goto meta_command_exit; 9499 } 9500 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], 9501 strlen30(azArg[3])); 9502 if( rc ){ 9503 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); 9504 rc = 1; 9505 } 9506 }else if( strcmp(azArg[1],"add")==0 ){ 9507 if( nArg!=5 ){ 9508 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); 9509 rc = 1; 9510 goto meta_command_exit; 9511 } 9512 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 9513 booleanValue(azArg[4])); 9514 if( rc ){ 9515 raw_printf(stderr, "User-Add failed: %d\n", rc); 9516 rc = 1; 9517 } 9518 }else if( strcmp(azArg[1],"edit")==0 ){ 9519 if( nArg!=5 ){ 9520 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); 9521 rc = 1; 9522 goto meta_command_exit; 9523 } 9524 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 9525 booleanValue(azArg[4])); 9526 if( rc ){ 9527 raw_printf(stderr, "User-Edit failed: %d\n", rc); 9528 rc = 1; 9529 } 9530 }else if( strcmp(azArg[1],"delete")==0 ){ 9531 if( nArg!=3 ){ 9532 raw_printf(stderr, "Usage: .user delete USER\n"); 9533 rc = 1; 9534 goto meta_command_exit; 9535 } 9536 rc = sqlite3_user_delete(p->db, azArg[2]); 9537 if( rc ){ 9538 raw_printf(stderr, "User-Delete failed: %d\n", rc); 9539 rc = 1; 9540 } 9541 }else{ 9542 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); 9543 rc = 1; 9544 goto meta_command_exit; 9545 } 9546 }else 9547#endif /* SQLITE_USER_AUTHENTICATION */ 9548 9549 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ 9550 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, 9551 sqlite3_libversion(), sqlite3_sourceid()); 9552#if SQLITE_HAVE_ZLIB 9553 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); 9554#endif 9555#define CTIMEOPT_VAL_(opt) #opt 9556#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) 9557#if defined(__clang__) && defined(__clang_major__) 9558 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." 9559 CTIMEOPT_VAL(__clang_minor__) "." 9560 CTIMEOPT_VAL(__clang_patchlevel__) "\n"); 9561#elif defined(_MSC_VER) 9562 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); 9563#elif defined(__GNUC__) && defined(__VERSION__) 9564 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); 9565#endif 9566 }else 9567 9568 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ 9569 const char *zDbName = nArg==2 ? azArg[1] : "main"; 9570 sqlite3_vfs *pVfs = 0; 9571 if( p->db ){ 9572 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); 9573 if( pVfs ){ 9574 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); 9575 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 9576 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 9577 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 9578 } 9579 } 9580 }else 9581 9582 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ 9583 sqlite3_vfs *pVfs; 9584 sqlite3_vfs *pCurrent = 0; 9585 if( p->db ){ 9586 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); 9587 } 9588 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 9589 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, 9590 pVfs==pCurrent ? " <--- CURRENT" : ""); 9591 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 9592 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 9593 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 9594 if( pVfs->pNext ){ 9595 raw_printf(p->out, "-----------------------------------\n"); 9596 } 9597 } 9598 }else 9599 9600 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ 9601 const char *zDbName = nArg==2 ? azArg[1] : "main"; 9602 char *zVfsName = 0; 9603 if( p->db ){ 9604 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); 9605 if( zVfsName ){ 9606 utf8_printf(p->out, "%s\n", zVfsName); 9607 sqlite3_free(zVfsName); 9608 } 9609 } 9610 }else 9611 9612#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 9613 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ 9614 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; 9615 }else 9616#endif 9617 9618 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ 9619 int j; 9620 assert( nArg<=ArraySize(azArg) ); 9621 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ 9622 p->colWidth[j-1] = (int)integerValue(azArg[j]); 9623 } 9624 }else 9625 9626 { 9627 utf8_printf(stderr, "Error: unknown command or invalid arguments: " 9628 " \"%s\". Enter \".help\" for help\n", azArg[0]); 9629 rc = 1; 9630 } 9631 9632meta_command_exit: 9633 if( p->outCount ){ 9634 p->outCount--; 9635 if( p->outCount==0 ) output_reset(p); 9636 } 9637 return rc; 9638} 9639 9640/* 9641** Return TRUE if a semicolon occurs anywhere in the first N characters 9642** of string z[]. 9643*/ 9644static int line_contains_semicolon(const char *z, int N){ 9645 int i; 9646 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } 9647 return 0; 9648} 9649 9650/* 9651** Test to see if a line consists entirely of whitespace. 9652*/ 9653static int _all_whitespace(const char *z){ 9654 for(; *z; z++){ 9655 if( IsSpace(z[0]) ) continue; 9656 if( *z=='/' && z[1]=='*' ){ 9657 z += 2; 9658 while( *z && (*z!='*' || z[1]!='/') ){ z++; } 9659 if( *z==0 ) return 0; 9660 z++; 9661 continue; 9662 } 9663 if( *z=='-' && z[1]=='-' ){ 9664 z += 2; 9665 while( *z && *z!='\n' ){ z++; } 9666 if( *z==0 ) return 1; 9667 continue; 9668 } 9669 return 0; 9670 } 9671 return 1; 9672} 9673 9674/* 9675** Return TRUE if the line typed in is an SQL command terminator other 9676** than a semi-colon. The SQL Server style "go" command is understood 9677** as is the Oracle "/". 9678*/ 9679static int line_is_command_terminator(const char *zLine){ 9680 while( IsSpace(zLine[0]) ){ zLine++; }; 9681 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ 9682 return 1; /* Oracle */ 9683 } 9684 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' 9685 && _all_whitespace(&zLine[2]) ){ 9686 return 1; /* SQL Server */ 9687 } 9688 return 0; 9689} 9690 9691/* 9692** We need a default sqlite3_complete() implementation to use in case 9693** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes 9694** any arbitrary text is a complete SQL statement. This is not very 9695** user-friendly, but it does seem to work. 9696*/ 9697#ifdef SQLITE_OMIT_COMPLETE 9698#define sqlite3_complete(x) 1 9699#endif 9700 9701/* 9702** Return true if zSql is a complete SQL statement. Return false if it 9703** ends in the middle of a string literal or C-style comment. 9704*/ 9705static int line_is_complete(char *zSql, int nSql){ 9706 int rc; 9707 if( zSql==0 ) return 1; 9708 zSql[nSql] = ';'; 9709 zSql[nSql+1] = 0; 9710 rc = sqlite3_complete(zSql); 9711 zSql[nSql] = 0; 9712 return rc; 9713} 9714 9715/* 9716** Run a single line of SQL. Return the number of errors. 9717*/ 9718static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ 9719 int rc; 9720 char *zErrMsg = 0; 9721 9722 open_db(p, 0); 9723 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); 9724 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 9725 BEGIN_TIMER; 9726 rc = shell_exec(p, zSql, &zErrMsg); 9727 END_TIMER; 9728 if( rc || zErrMsg ){ 9729 char zPrefix[100]; 9730 if( in!=0 || !stdin_is_interactive ){ 9731 sqlite3_snprintf(sizeof(zPrefix), zPrefix, 9732 "Error: near line %d:", startline); 9733 }else{ 9734 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); 9735 } 9736 if( zErrMsg!=0 ){ 9737 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); 9738 sqlite3_free(zErrMsg); 9739 zErrMsg = 0; 9740 }else{ 9741 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); 9742 } 9743 return 1; 9744 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ 9745 raw_printf(p->out, "changes: %3d total_changes: %d\n", 9746 sqlite3_changes(p->db), sqlite3_total_changes(p->db)); 9747 } 9748 return 0; 9749} 9750 9751 9752/* 9753** Read input from *in and process it. If *in==0 then input 9754** is interactive - the user is typing it it. Otherwise, input 9755** is coming from a file or device. A prompt is issued and history 9756** is saved only if input is interactive. An interrupt signal will 9757** cause this routine to exit immediately, unless input is interactive. 9758** 9759** Return the number of errors. 9760*/ 9761static int process_input(ShellState *p){ 9762 char *zLine = 0; /* A single input line */ 9763 char *zSql = 0; /* Accumulated SQL text */ 9764 int nLine; /* Length of current line */ 9765 int nSql = 0; /* Bytes of zSql[] used */ 9766 int nAlloc = 0; /* Allocated zSql[] space */ 9767 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ 9768 int rc; /* Error code */ 9769 int errCnt = 0; /* Number of errors seen */ 9770 int startline = 0; /* Line number for start of current input */ 9771 9772 p->lineno = 0; 9773 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ 9774 fflush(p->out); 9775 zLine = one_input_line(p->in, zLine, nSql>0); 9776 if( zLine==0 ){ 9777 /* End of input */ 9778 if( p->in==0 && stdin_is_interactive ) printf("\n"); 9779 break; 9780 } 9781 if( seenInterrupt ){ 9782 if( p->in!=0 ) break; 9783 seenInterrupt = 0; 9784 } 9785 p->lineno++; 9786 if( nSql==0 && _all_whitespace(zLine) ){ 9787 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 9788 continue; 9789 } 9790 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ 9791 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 9792 if( zLine[0]=='.' ){ 9793 rc = do_meta_command(zLine, p); 9794 if( rc==2 ){ /* exit requested */ 9795 break; 9796 }else if( rc ){ 9797 errCnt++; 9798 } 9799 } 9800 continue; 9801 } 9802 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ 9803 memcpy(zLine,";",2); 9804 } 9805 nLine = strlen30(zLine); 9806 if( nSql+nLine+2>=nAlloc ){ 9807 nAlloc = nSql+nLine+100; 9808 zSql = realloc(zSql, nAlloc); 9809 if( zSql==0 ) shell_out_of_memory(); 9810 } 9811 nSqlPrior = nSql; 9812 if( nSql==0 ){ 9813 int i; 9814 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} 9815 assert( nAlloc>0 && zSql!=0 ); 9816 memcpy(zSql, zLine+i, nLine+1-i); 9817 startline = p->lineno; 9818 nSql = nLine-i; 9819 }else{ 9820 zSql[nSql++] = '\n'; 9821 memcpy(zSql+nSql, zLine, nLine+1); 9822 nSql += nLine; 9823 } 9824 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) 9825 && sqlite3_complete(zSql) ){ 9826 errCnt += runOneSqlLine(p, zSql, p->in, startline); 9827 nSql = 0; 9828 if( p->outCount ){ 9829 output_reset(p); 9830 p->outCount = 0; 9831 }else{ 9832 clearTempFile(p); 9833 } 9834 }else if( nSql && _all_whitespace(zSql) ){ 9835 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); 9836 nSql = 0; 9837 } 9838 } 9839 if( nSql && !_all_whitespace(zSql) ){ 9840 errCnt += runOneSqlLine(p, zSql, p->in, startline); 9841 } 9842 free(zSql); 9843 free(zLine); 9844 return errCnt>0; 9845} 9846 9847/* 9848** Return a pathname which is the user's home directory. A 9849** 0 return indicates an error of some kind. 9850*/ 9851static char *find_home_dir(int clearFlag){ 9852 static char *home_dir = NULL; 9853 if( clearFlag ){ 9854 free(home_dir); 9855 home_dir = 0; 9856 return 0; 9857 } 9858 if( home_dir ) return home_dir; 9859 9860#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 9861 && !defined(__RTP__) && !defined(_WRS_KERNEL) 9862 { 9863 struct passwd *pwent; 9864 uid_t uid = getuid(); 9865 if( (pwent=getpwuid(uid)) != NULL) { 9866 home_dir = pwent->pw_dir; 9867 } 9868 } 9869#endif 9870 9871#if defined(_WIN32_WCE) 9872 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() 9873 */ 9874 home_dir = "/"; 9875#else 9876 9877#if defined(_WIN32) || defined(WIN32) 9878 if (!home_dir) { 9879 home_dir = getenv("USERPROFILE"); 9880 } 9881#endif 9882 9883 if (!home_dir) { 9884 home_dir = getenv("HOME"); 9885 } 9886 9887#if defined(_WIN32) || defined(WIN32) 9888 if (!home_dir) { 9889 char *zDrive, *zPath; 9890 int n; 9891 zDrive = getenv("HOMEDRIVE"); 9892 zPath = getenv("HOMEPATH"); 9893 if( zDrive && zPath ){ 9894 n = strlen30(zDrive) + strlen30(zPath) + 1; 9895 home_dir = malloc( n ); 9896 if( home_dir==0 ) return 0; 9897 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); 9898 return home_dir; 9899 } 9900 home_dir = "c:\\"; 9901 } 9902#endif 9903 9904#endif /* !_WIN32_WCE */ 9905 9906 if( home_dir ){ 9907 int n = strlen30(home_dir) + 1; 9908 char *z = malloc( n ); 9909 if( z ) memcpy(z, home_dir, n); 9910 home_dir = z; 9911 } 9912 9913 return home_dir; 9914} 9915 9916/* 9917** Read input from the file given by sqliterc_override. Or if that 9918** parameter is NULL, take input from ~/.sqliterc 9919** 9920** Returns the number of errors. 9921*/ 9922static void process_sqliterc( 9923 ShellState *p, /* Configuration data */ 9924 const char *sqliterc_override /* Name of config file. NULL to use default */ 9925){ 9926 char *home_dir = NULL; 9927 const char *sqliterc = sqliterc_override; 9928 char *zBuf = 0; 9929 FILE *inSaved = p->in; 9930 int savedLineno = p->lineno; 9931 9932 if (sqliterc == NULL) { 9933 home_dir = find_home_dir(0); 9934 if( home_dir==0 ){ 9935 raw_printf(stderr, "-- warning: cannot find home directory;" 9936 " cannot read ~/.sqliterc\n"); 9937 return; 9938 } 9939 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); 9940 sqliterc = zBuf; 9941 } 9942 p->in = fopen(sqliterc,"rb"); 9943 if( p->in ){ 9944 if( stdin_is_interactive ){ 9945 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); 9946 } 9947 process_input(p); 9948 fclose(p->in); 9949 } 9950 p->in = inSaved; 9951 p->lineno = savedLineno; 9952 sqlite3_free(zBuf); 9953} 9954 9955/* 9956** Show available command line options 9957*/ 9958static const char zOptions[] = 9959#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 9960 " -A ARGS... run \".archive ARGS\" and exit\n" 9961#endif 9962 " -append append the database to the end of the file\n" 9963 " -ascii set output mode to 'ascii'\n" 9964 " -bail stop after hitting an error\n" 9965 " -batch force batch I/O\n" 9966 " -column set output mode to 'column'\n" 9967 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 9968 " -csv set output mode to 'csv'\n" 9969#if defined(SQLITE_ENABLE_DESERIALIZE) 9970 " -deserialize open the database using sqlite3_deserialize()\n" 9971#endif 9972 " -echo print commands before execution\n" 9973 " -init FILENAME read/process named file\n" 9974 " -[no]header turn headers on or off\n" 9975#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 9976 " -heap SIZE Size of heap for memsys3 or memsys5\n" 9977#endif 9978 " -help show this message\n" 9979 " -html set output mode to HTML\n" 9980 " -interactive force interactive I/O\n" 9981 " -line set output mode to 'line'\n" 9982 " -list set output mode to 'list'\n" 9983 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 9984#if defined(SQLITE_ENABLE_DESERIALIZE) 9985 " -maxsize N maximum size for a --deserialize database\n" 9986#endif 9987 " -memtrace trace all memory allocations and deallocations\n" 9988 " -mmap N default mmap size set to N\n" 9989#ifdef SQLITE_ENABLE_MULTIPLEX 9990 " -multiplex enable the multiplexor VFS\n" 9991#endif 9992 " -newline SEP set output row separator. Default: '\\n'\n" 9993 " -nofollow refuse to open symbolic links to database files\n" 9994 " -nullvalue TEXT set text string for NULL values. Default ''\n" 9995 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 9996 " -quote set output mode to 'quote'\n" 9997 " -readonly open the database read-only\n" 9998 " -separator SEP set output column separator. Default: '|'\n" 9999#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10000 " -sorterref SIZE sorter references threshold size\n" 10001#endif 10002 " -stats print memory stats before each finalize\n" 10003 " -version show SQLite version\n" 10004 " -vfs NAME use NAME as the default VFS\n" 10005#ifdef SQLITE_ENABLE_VFSTRACE 10006 " -vfstrace enable tracing of all VFS calls\n" 10007#endif 10008#ifdef SQLITE_HAVE_ZLIB 10009 " -zip open the file as a ZIP Archive\n" 10010#endif 10011; 10012static void usage(int showDetail){ 10013 utf8_printf(stderr, 10014 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 10015 "FILENAME is the name of an SQLite database. A new database is created\n" 10016 "if the file does not previously exist.\n", Argv0); 10017 if( showDetail ){ 10018 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); 10019 }else{ 10020 raw_printf(stderr, "Use the -help option for additional information\n"); 10021 } 10022 exit(1); 10023} 10024 10025/* 10026** Internal check: Verify that the SQLite is uninitialized. Print a 10027** error message if it is initialized. 10028*/ 10029static void verify_uninitialized(void){ 10030 if( sqlite3_config(-1)==SQLITE_MISUSE ){ 10031 utf8_printf(stdout, "WARNING: attempt to configure SQLite after" 10032 " initialization.\n"); 10033 } 10034} 10035 10036/* 10037** Initialize the state information in data 10038*/ 10039static void main_init(ShellState *data) { 10040 memset(data, 0, sizeof(*data)); 10041 data->normalMode = data->cMode = data->mode = MODE_List; 10042 data->autoExplain = 1; 10043 memcpy(data->colSeparator,SEP_Column, 2); 10044 memcpy(data->rowSeparator,SEP_Row, 2); 10045 data->showHeader = 0; 10046 data->shellFlgs = SHFLG_Lookaside; 10047 verify_uninitialized(); 10048 sqlite3_config(SQLITE_CONFIG_URI, 1); 10049 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); 10050 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 10051 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); 10052 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); 10053} 10054 10055/* 10056** Output text to the console in a font that attracts extra attention. 10057*/ 10058#ifdef _WIN32 10059static void printBold(const char *zText){ 10060 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 10061 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; 10062 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); 10063 SetConsoleTextAttribute(out, 10064 FOREGROUND_RED|FOREGROUND_INTENSITY 10065 ); 10066 printf("%s", zText); 10067 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); 10068} 10069#else 10070static void printBold(const char *zText){ 10071 printf("\033[1m%s\033[0m", zText); 10072} 10073#endif 10074 10075/* 10076** Get the argument to an --option. Throw an error and die if no argument 10077** is available. 10078*/ 10079static char *cmdline_option_value(int argc, char **argv, int i){ 10080 if( i==argc ){ 10081 utf8_printf(stderr, "%s: Error: missing argument to %s\n", 10082 argv[0], argv[argc-1]); 10083 exit(1); 10084 } 10085 return argv[i]; 10086} 10087 10088#ifndef SQLITE_SHELL_IS_UTF8 10089# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) 10090# define SQLITE_SHELL_IS_UTF8 (0) 10091# else 10092# define SQLITE_SHELL_IS_UTF8 (1) 10093# endif 10094#endif 10095 10096#if SQLITE_SHELL_IS_UTF8 10097int SQLITE_CDECL main(int argc, char **argv){ 10098#else 10099int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ 10100 char **argv; 10101#endif 10102 char *zErrMsg = 0; 10103 ShellState data; 10104 const char *zInitFile = 0; 10105 int i; 10106 int rc = 0; 10107 int warnInmemoryDb = 0; 10108 int readStdin = 1; 10109 int nCmd = 0; 10110 char **azCmd = 0; 10111 const char *zVfs = 0; /* Value of -vfs command-line option */ 10112#if !SQLITE_SHELL_IS_UTF8 10113 char **argvToFree = 0; 10114 int argcToFree = 0; 10115#endif 10116 10117 setBinaryMode(stdin, 0); 10118 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ 10119 stdin_is_interactive = isatty(0); 10120 stdout_is_console = isatty(1); 10121 10122#if !defined(_WIN32_WCE) 10123 if( getenv("SQLITE_DEBUG_BREAK") ){ 10124 if( isatty(0) && isatty(2) ){ 10125 fprintf(stderr, 10126 "attach debugger to process %d and press any key to continue.\n", 10127 GETPID()); 10128 fgetc(stdin); 10129 }else{ 10130#if defined(_WIN32) || defined(WIN32) 10131 DebugBreak(); 10132#elif defined(SIGTRAP) 10133 raise(SIGTRAP); 10134#endif 10135 } 10136 } 10137#endif 10138 10139#if USE_SYSTEM_SQLITE+0!=1 10140 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ 10141 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", 10142 sqlite3_sourceid(), SQLITE_SOURCE_ID); 10143 exit(1); 10144 } 10145#endif 10146 main_init(&data); 10147 10148 /* On Windows, we must translate command-line arguments into UTF-8. 10149 ** The SQLite memory allocator subsystem has to be enabled in order to 10150 ** do this. But we want to run an sqlite3_shutdown() afterwards so that 10151 ** subsequent sqlite3_config() calls will work. So copy all results into 10152 ** memory that does not come from the SQLite memory allocator. 10153 */ 10154#if !SQLITE_SHELL_IS_UTF8 10155 sqlite3_initialize(); 10156 argvToFree = malloc(sizeof(argv[0])*argc*2); 10157 argcToFree = argc; 10158 argv = argvToFree + argc; 10159 if( argv==0 ) shell_out_of_memory(); 10160 for(i=0; i<argc; i++){ 10161 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); 10162 int n; 10163 if( z==0 ) shell_out_of_memory(); 10164 n = (int)strlen(z); 10165 argv[i] = malloc( n+1 ); 10166 if( argv[i]==0 ) shell_out_of_memory(); 10167 memcpy(argv[i], z, n+1); 10168 argvToFree[i] = argv[i]; 10169 sqlite3_free(z); 10170 } 10171 sqlite3_shutdown(); 10172#endif 10173 10174 assert( argc>=1 && argv && argv[0] ); 10175 Argv0 = argv[0]; 10176 10177 /* Make sure we have a valid signal handler early, before anything 10178 ** else is done. 10179 */ 10180#ifdef SIGINT 10181 signal(SIGINT, interrupt_handler); 10182#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 10183 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); 10184#endif 10185 10186#ifdef SQLITE_SHELL_DBNAME_PROC 10187 { 10188 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name 10189 ** of a C-function that will provide the name of the database file. Use 10190 ** this compile-time option to embed this shell program in larger 10191 ** applications. */ 10192 extern void SQLITE_SHELL_DBNAME_PROC(const char**); 10193 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); 10194 warnInmemoryDb = 0; 10195 } 10196#endif 10197 10198 /* Do an initial pass through the command-line argument to locate 10199 ** the name of the database file, the name of the initialization file, 10200 ** the size of the alternative malloc heap, 10201 ** and the first command to execute. 10202 */ 10203 verify_uninitialized(); 10204 for(i=1; i<argc; i++){ 10205 char *z; 10206 z = argv[i]; 10207 if( z[0]!='-' ){ 10208 if( data.zDbFilename==0 ){ 10209 data.zDbFilename = z; 10210 }else{ 10211 /* Excesss arguments are interpreted as SQL (or dot-commands) and 10212 ** mean that nothing is read from stdin */ 10213 readStdin = 0; 10214 nCmd++; 10215 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); 10216 if( azCmd==0 ) shell_out_of_memory(); 10217 azCmd[nCmd-1] = z; 10218 } 10219 } 10220 if( z[1]=='-' ) z++; 10221 if( strcmp(z,"-separator")==0 10222 || strcmp(z,"-nullvalue")==0 10223 || strcmp(z,"-newline")==0 10224 || strcmp(z,"-cmd")==0 10225 ){ 10226 (void)cmdline_option_value(argc, argv, ++i); 10227 }else if( strcmp(z,"-init")==0 ){ 10228 zInitFile = cmdline_option_value(argc, argv, ++i); 10229 }else if( strcmp(z,"-batch")==0 ){ 10230 /* Need to check for batch mode here to so we can avoid printing 10231 ** informational messages (like from process_sqliterc) before 10232 ** we do the actual processing of arguments later in a second pass. 10233 */ 10234 stdin_is_interactive = 0; 10235 }else if( strcmp(z,"-heap")==0 ){ 10236#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 10237 const char *zSize; 10238 sqlite3_int64 szHeap; 10239 10240 zSize = cmdline_option_value(argc, argv, ++i); 10241 szHeap = integerValue(zSize); 10242 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; 10243 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); 10244#else 10245 (void)cmdline_option_value(argc, argv, ++i); 10246#endif 10247 }else if( strcmp(z,"-pagecache")==0 ){ 10248 int n, sz; 10249 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10250 if( sz>70000 ) sz = 70000; 10251 if( sz<0 ) sz = 0; 10252 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10253 sqlite3_config(SQLITE_CONFIG_PAGECACHE, 10254 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); 10255 data.shellFlgs |= SHFLG_Pagecache; 10256 }else if( strcmp(z,"-lookaside")==0 ){ 10257 int n, sz; 10258 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10259 if( sz<0 ) sz = 0; 10260 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10261 if( n<0 ) n = 0; 10262 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); 10263 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; 10264#ifdef SQLITE_ENABLE_VFSTRACE 10265 }else if( strcmp(z,"-vfstrace")==0 ){ 10266 extern int vfstrace_register( 10267 const char *zTraceName, 10268 const char *zOldVfsName, 10269 int (*xOut)(const char*,void*), 10270 void *pOutArg, 10271 int makeDefault 10272 ); 10273 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); 10274#endif 10275#ifdef SQLITE_ENABLE_MULTIPLEX 10276 }else if( strcmp(z,"-multiplex")==0 ){ 10277 extern int sqlite3_multiple_initialize(const char*,int); 10278 sqlite3_multiplex_initialize(0, 1); 10279#endif 10280 }else if( strcmp(z,"-mmap")==0 ){ 10281 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10282 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); 10283#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10284 }else if( strcmp(z,"-sorterref")==0 ){ 10285 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10286 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); 10287#endif 10288 }else if( strcmp(z,"-vfs")==0 ){ 10289 zVfs = cmdline_option_value(argc, argv, ++i); 10290#ifdef SQLITE_HAVE_ZLIB 10291 }else if( strcmp(z,"-zip")==0 ){ 10292 data.openMode = SHELL_OPEN_ZIPFILE; 10293#endif 10294 }else if( strcmp(z,"-append")==0 ){ 10295 data.openMode = SHELL_OPEN_APPENDVFS; 10296#ifdef SQLITE_ENABLE_DESERIALIZE 10297 }else if( strcmp(z,"-deserialize")==0 ){ 10298 data.openMode = SHELL_OPEN_DESERIALIZE; 10299 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 10300 data.szMax = integerValue(argv[++i]); 10301#endif 10302 }else if( strcmp(z,"-readonly")==0 ){ 10303 data.openMode = SHELL_OPEN_READONLY; 10304 }else if( strcmp(z,"-nofollow")==0 ){ 10305 data.openFlags = SQLITE_OPEN_NOFOLLOW; 10306#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 10307 }else if( strncmp(z, "-A",2)==0 ){ 10308 /* All remaining command-line arguments are passed to the ".archive" 10309 ** command, so ignore them */ 10310 break; 10311#endif 10312 }else if( strcmp(z, "-memtrace")==0 ){ 10313 sqlite3MemTraceActivate(stderr); 10314 } 10315 } 10316 verify_uninitialized(); 10317 10318 10319#ifdef SQLITE_SHELL_INIT_PROC 10320 { 10321 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name 10322 ** of a C-function that will perform initialization actions on SQLite that 10323 ** occur just before or after sqlite3_initialize(). Use this compile-time 10324 ** option to embed this shell program in larger applications. */ 10325 extern void SQLITE_SHELL_INIT_PROC(void); 10326 SQLITE_SHELL_INIT_PROC(); 10327 } 10328#else 10329 /* All the sqlite3_config() calls have now been made. So it is safe 10330 ** to call sqlite3_initialize() and process any command line -vfs option. */ 10331 sqlite3_initialize(); 10332#endif 10333 10334 if( zVfs ){ 10335 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); 10336 if( pVfs ){ 10337 sqlite3_vfs_register(pVfs, 1); 10338 }else{ 10339 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); 10340 exit(1); 10341 } 10342 } 10343 10344 if( data.zDbFilename==0 ){ 10345#ifndef SQLITE_OMIT_MEMORYDB 10346 data.zDbFilename = ":memory:"; 10347 warnInmemoryDb = argc==1; 10348#else 10349 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); 10350 return 1; 10351#endif 10352 } 10353 data.out = stdout; 10354 sqlite3_appendvfs_init(0,0,0); 10355 10356 /* Go ahead and open the database file if it already exists. If the 10357 ** file does not exist, delay opening it. This prevents empty database 10358 ** files from being created if a user mistypes the database name argument 10359 ** to the sqlite command-line tool. 10360 */ 10361 if( access(data.zDbFilename, 0)==0 ){ 10362 open_db(&data, 0); 10363 } 10364 10365 /* Process the initialization file if there is one. If no -init option 10366 ** is given on the command line, look for a file named ~/.sqliterc and 10367 ** try to process it. 10368 */ 10369 process_sqliterc(&data,zInitFile); 10370 10371 /* Make a second pass through the command-line argument and set 10372 ** options. This second pass is delayed until after the initialization 10373 ** file is processed so that the command-line arguments will override 10374 ** settings in the initialization file. 10375 */ 10376 for(i=1; i<argc; i++){ 10377 char *z = argv[i]; 10378 if( z[0]!='-' ) continue; 10379 if( z[1]=='-' ){ z++; } 10380 if( strcmp(z,"-init")==0 ){ 10381 i++; 10382 }else if( strcmp(z,"-html")==0 ){ 10383 data.mode = MODE_Html; 10384 }else if( strcmp(z,"-list")==0 ){ 10385 data.mode = MODE_List; 10386 }else if( strcmp(z,"-quote")==0 ){ 10387 data.mode = MODE_Quote; 10388 }else if( strcmp(z,"-line")==0 ){ 10389 data.mode = MODE_Line; 10390 }else if( strcmp(z,"-column")==0 ){ 10391 data.mode = MODE_Column; 10392 }else if( strcmp(z,"-csv")==0 ){ 10393 data.mode = MODE_Csv; 10394 memcpy(data.colSeparator,",",2); 10395#ifdef SQLITE_HAVE_ZLIB 10396 }else if( strcmp(z,"-zip")==0 ){ 10397 data.openMode = SHELL_OPEN_ZIPFILE; 10398#endif 10399 }else if( strcmp(z,"-append")==0 ){ 10400 data.openMode = SHELL_OPEN_APPENDVFS; 10401#ifdef SQLITE_ENABLE_DESERIALIZE 10402 }else if( strcmp(z,"-deserialize")==0 ){ 10403 data.openMode = SHELL_OPEN_DESERIALIZE; 10404 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 10405 data.szMax = integerValue(argv[++i]); 10406#endif 10407 }else if( strcmp(z,"-readonly")==0 ){ 10408 data.openMode = SHELL_OPEN_READONLY; 10409 }else if( strcmp(z,"-nofollow")==0 ){ 10410 data.openFlags |= SQLITE_OPEN_NOFOLLOW; 10411 }else if( strcmp(z,"-ascii")==0 ){ 10412 data.mode = MODE_Ascii; 10413 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 10414 SEP_Unit); 10415 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 10416 SEP_Record); 10417 }else if( strcmp(z,"-separator")==0 ){ 10418 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 10419 "%s",cmdline_option_value(argc,argv,++i)); 10420 }else if( strcmp(z,"-newline")==0 ){ 10421 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 10422 "%s",cmdline_option_value(argc,argv,++i)); 10423 }else if( strcmp(z,"-nullvalue")==0 ){ 10424 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, 10425 "%s",cmdline_option_value(argc,argv,++i)); 10426 }else if( strcmp(z,"-header")==0 ){ 10427 data.showHeader = 1; 10428 }else if( strcmp(z,"-noheader")==0 ){ 10429 data.showHeader = 0; 10430 }else if( strcmp(z,"-echo")==0 ){ 10431 ShellSetFlag(&data, SHFLG_Echo); 10432 }else if( strcmp(z,"-eqp")==0 ){ 10433 data.autoEQP = AUTOEQP_on; 10434 }else if( strcmp(z,"-eqpfull")==0 ){ 10435 data.autoEQP = AUTOEQP_full; 10436 }else if( strcmp(z,"-stats")==0 ){ 10437 data.statsOn = 1; 10438 }else if( strcmp(z,"-scanstats")==0 ){ 10439 data.scanstatsOn = 1; 10440 }else if( strcmp(z,"-backslash")==0 ){ 10441 /* Undocumented command-line option: -backslash 10442 ** Causes C-style backslash escapes to be evaluated in SQL statements 10443 ** prior to sending the SQL into SQLite. Useful for injecting 10444 ** crazy bytes in the middle of SQL statements for testing and debugging. 10445 */ 10446 ShellSetFlag(&data, SHFLG_Backslash); 10447 }else if( strcmp(z,"-bail")==0 ){ 10448 bail_on_error = 1; 10449 }else if( strcmp(z,"-version")==0 ){ 10450 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 10451 return 0; 10452 }else if( strcmp(z,"-interactive")==0 ){ 10453 stdin_is_interactive = 1; 10454 }else if( strcmp(z,"-batch")==0 ){ 10455 stdin_is_interactive = 0; 10456 }else if( strcmp(z,"-heap")==0 ){ 10457 i++; 10458 }else if( strcmp(z,"-pagecache")==0 ){ 10459 i+=2; 10460 }else if( strcmp(z,"-lookaside")==0 ){ 10461 i+=2; 10462 }else if( strcmp(z,"-mmap")==0 ){ 10463 i++; 10464 }else if( strcmp(z,"-memtrace")==0 ){ 10465 i++; 10466#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10467 }else if( strcmp(z,"-sorterref")==0 ){ 10468 i++; 10469#endif 10470 }else if( strcmp(z,"-vfs")==0 ){ 10471 i++; 10472#ifdef SQLITE_ENABLE_VFSTRACE 10473 }else if( strcmp(z,"-vfstrace")==0 ){ 10474 i++; 10475#endif 10476#ifdef SQLITE_ENABLE_MULTIPLEX 10477 }else if( strcmp(z,"-multiplex")==0 ){ 10478 i++; 10479#endif 10480 }else if( strcmp(z,"-help")==0 ){ 10481 usage(1); 10482 }else if( strcmp(z,"-cmd")==0 ){ 10483 /* Run commands that follow -cmd first and separately from commands 10484 ** that simply appear on the command-line. This seems goofy. It would 10485 ** be better if all commands ran in the order that they appear. But 10486 ** we retain the goofy behavior for historical compatibility. */ 10487 if( i==argc-1 ) break; 10488 z = cmdline_option_value(argc,argv,++i); 10489 if( z[0]=='.' ){ 10490 rc = do_meta_command(z, &data); 10491 if( rc && bail_on_error ) return rc==2 ? 0 : rc; 10492 }else{ 10493 open_db(&data, 0); 10494 rc = shell_exec(&data, z, &zErrMsg); 10495 if( zErrMsg!=0 ){ 10496 utf8_printf(stderr,"Error: %s\n", zErrMsg); 10497 if( bail_on_error ) return rc!=0 ? rc : 1; 10498 }else if( rc!=0 ){ 10499 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); 10500 if( bail_on_error ) return rc; 10501 } 10502 } 10503#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 10504 }else if( strncmp(z, "-A", 2)==0 ){ 10505 if( nCmd>0 ){ 10506 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" 10507 " with \"%s\"\n", z); 10508 return 1; 10509 } 10510 open_db(&data, OPEN_DB_ZIPFILE); 10511 if( z[2] ){ 10512 argv[i] = &z[2]; 10513 arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); 10514 }else{ 10515 arDotCommand(&data, 1, argv+i, argc-i); 10516 } 10517 readStdin = 0; 10518 break; 10519#endif 10520 }else{ 10521 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); 10522 raw_printf(stderr,"Use -help for a list of options.\n"); 10523 return 1; 10524 } 10525 data.cMode = data.mode; 10526 } 10527 10528 if( !readStdin ){ 10529 /* Run all arguments that do not begin with '-' as if they were separate 10530 ** command-line inputs, except for the argToSkip argument which contains 10531 ** the database filename. 10532 */ 10533 for(i=0; i<nCmd; i++){ 10534 if( azCmd[i][0]=='.' ){ 10535 rc = do_meta_command(azCmd[i], &data); 10536 if( rc ) return rc==2 ? 0 : rc; 10537 }else{ 10538 open_db(&data, 0); 10539 rc = shell_exec(&data, azCmd[i], &zErrMsg); 10540 if( zErrMsg!=0 ){ 10541 utf8_printf(stderr,"Error: %s\n", zErrMsg); 10542 return rc!=0 ? rc : 1; 10543 }else if( rc!=0 ){ 10544 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); 10545 return rc; 10546 } 10547 } 10548 } 10549 free(azCmd); 10550 }else{ 10551 /* Run commands received from standard input 10552 */ 10553 if( stdin_is_interactive ){ 10554 char *zHome; 10555 char *zHistory; 10556 int nHistory; 10557 printf( 10558 "SQLite version %s %.19s\n" /*extra-version-info*/ 10559 "Enter \".help\" for usage hints.\n", 10560 sqlite3_libversion(), sqlite3_sourceid() 10561 ); 10562 if( warnInmemoryDb ){ 10563 printf("Connected to a "); 10564 printBold("transient in-memory database"); 10565 printf(".\nUse \".open FILENAME\" to reopen on a " 10566 "persistent database.\n"); 10567 } 10568 zHistory = getenv("SQLITE_HISTORY"); 10569 if( zHistory ){ 10570 zHistory = strdup(zHistory); 10571 }else if( (zHome = find_home_dir(0))!=0 ){ 10572 nHistory = strlen30(zHome) + 20; 10573 if( (zHistory = malloc(nHistory))!=0 ){ 10574 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); 10575 } 10576 } 10577 if( zHistory ){ shell_read_history(zHistory); } 10578#if HAVE_READLINE || HAVE_EDITLINE 10579 rl_attempted_completion_function = readline_completion; 10580#elif HAVE_LINENOISE 10581 linenoiseSetCompletionCallback(linenoise_completion); 10582#endif 10583 data.in = 0; 10584 rc = process_input(&data); 10585 if( zHistory ){ 10586 shell_stifle_history(2000); 10587 shell_write_history(zHistory); 10588 free(zHistory); 10589 } 10590 }else{ 10591 data.in = stdin; 10592 rc = process_input(&data); 10593 } 10594 } 10595 set_table_name(&data, 0); 10596 if( data.db ){ 10597 session_close_all(&data); 10598 close_db(data.db); 10599 } 10600 sqlite3_free(data.zFreeOnClose); 10601 find_home_dir(1); 10602 output_reset(&data); 10603 data.doXdgOpen = 0; 10604 clearTempFile(&data); 10605#if !SQLITE_SHELL_IS_UTF8 10606 for(i=0; i<argcToFree; i++) free(argvToFree[i]); 10607 free(argvToFree); 10608#endif 10609 /* Clear the global data structure so that valgrind will detect memory 10610 ** leaks */ 10611 memset(&data, 0, sizeof(data)); 10612 return rc; 10613} 10614