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 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, 7177 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 7178 }; 7179 int ii, v; 7180 open_db(p, 0); 7181 for(ii=0; ii<ArraySize(aDbConfig); ii++){ 7182 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; 7183 if( nArg>=3 ){ 7184 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); 7185 } 7186 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); 7187 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); 7188 if( nArg>1 ) break; 7189 } 7190 if( nArg>1 && ii==ArraySize(aDbConfig) ){ 7191 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); 7192 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); 7193 } 7194 }else 7195 7196 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ 7197 rc = shell_dbinfo_command(p, nArg, azArg); 7198 }else 7199 7200#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 7201 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ 7202 open_db(p, 0); 7203 rc = recoverDatabaseCmd(p, nArg, azArg); 7204 }else 7205#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ 7206 7207 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ 7208 const char *zLike = 0; 7209 int i; 7210 int savedShowHeader = p->showHeader; 7211 int savedShellFlags = p->shellFlgs; 7212 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo); 7213 for(i=1; i<nArg; i++){ 7214 if( azArg[i][0]=='-' ){ 7215 const char *z = azArg[i]+1; 7216 if( z[0]=='-' ) z++; 7217 if( strcmp(z,"preserve-rowids")==0 ){ 7218#ifdef SQLITE_OMIT_VIRTUALTABLE 7219 raw_printf(stderr, "The --preserve-rowids option is not compatible" 7220 " with SQLITE_OMIT_VIRTUALTABLE\n"); 7221 rc = 1; 7222 goto meta_command_exit; 7223#else 7224 ShellSetFlag(p, SHFLG_PreserveRowid); 7225#endif 7226 }else 7227 if( strcmp(z,"newlines")==0 ){ 7228 ShellSetFlag(p, SHFLG_Newlines); 7229 }else 7230 { 7231 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); 7232 rc = 1; 7233 goto meta_command_exit; 7234 } 7235 }else if( zLike ){ 7236 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " 7237 "?--newlines? ?LIKE-PATTERN?\n"); 7238 rc = 1; 7239 goto meta_command_exit; 7240 }else{ 7241 zLike = azArg[i]; 7242 } 7243 } 7244 7245 open_db(p, 0); 7246 7247 /* When playing back a "dump", the content might appear in an order 7248 ** which causes immediate foreign key constraints to be violated. 7249 ** So disable foreign-key constraint enforcement to prevent problems. */ 7250 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); 7251 raw_printf(p->out, "BEGIN TRANSACTION;\n"); 7252 p->writableSchema = 0; 7253 p->showHeader = 0; 7254 /* Set writable_schema=ON since doing so forces SQLite to initialize 7255 ** as much of the schema as it can even if the sqlite_master table is 7256 ** corrupt. */ 7257 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); 7258 p->nErr = 0; 7259 if( zLike==0 ){ 7260 run_schema_dump_query(p, 7261 "SELECT name, type, sql FROM sqlite_master " 7262 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" 7263 ); 7264 run_schema_dump_query(p, 7265 "SELECT name, type, sql FROM sqlite_master " 7266 "WHERE name=='sqlite_sequence'" 7267 ); 7268 run_table_dump_query(p, 7269 "SELECT sql FROM sqlite_master " 7270 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 7271 ); 7272 }else{ 7273 char *zSql; 7274 zSql = sqlite3_mprintf( 7275 "SELECT name, type, sql FROM sqlite_master " 7276 "WHERE tbl_name LIKE %Q AND type=='table'" 7277 " AND sql NOT NULL", zLike); 7278 run_schema_dump_query(p,zSql); 7279 sqlite3_free(zSql); 7280 zSql = sqlite3_mprintf( 7281 "SELECT sql FROM sqlite_master " 7282 "WHERE sql NOT NULL" 7283 " AND type IN ('index','trigger','view')" 7284 " AND tbl_name LIKE %Q", zLike); 7285 run_table_dump_query(p, zSql, 0); 7286 sqlite3_free(zSql); 7287 } 7288 if( p->writableSchema ){ 7289 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); 7290 p->writableSchema = 0; 7291 } 7292 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 7293 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); 7294 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); 7295 p->showHeader = savedShowHeader; 7296 p->shellFlgs = savedShellFlags; 7297 }else 7298 7299 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ 7300 if( nArg==2 ){ 7301 setOrClearFlag(p, SHFLG_Echo, azArg[1]); 7302 }else{ 7303 raw_printf(stderr, "Usage: .echo on|off\n"); 7304 rc = 1; 7305 } 7306 }else 7307 7308 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ 7309 if( nArg==2 ){ 7310 p->autoEQPtest = 0; 7311 if( p->autoEQPtrace ){ 7312 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); 7313 p->autoEQPtrace = 0; 7314 } 7315 if( strcmp(azArg[1],"full")==0 ){ 7316 p->autoEQP = AUTOEQP_full; 7317 }else if( strcmp(azArg[1],"trigger")==0 ){ 7318 p->autoEQP = AUTOEQP_trigger; 7319#ifdef SQLITE_DEBUG 7320 }else if( strcmp(azArg[1],"test")==0 ){ 7321 p->autoEQP = AUTOEQP_on; 7322 p->autoEQPtest = 1; 7323 }else if( strcmp(azArg[1],"trace")==0 ){ 7324 p->autoEQP = AUTOEQP_full; 7325 p->autoEQPtrace = 1; 7326 open_db(p, 0); 7327 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0); 7328 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); 7329#endif 7330 }else{ 7331 p->autoEQP = (u8)booleanValue(azArg[1]); 7332 } 7333 }else{ 7334 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); 7335 rc = 1; 7336 } 7337 }else 7338 7339 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ 7340 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); 7341 rc = 2; 7342 }else 7343 7344 /* The ".explain" command is automatic now. It is largely pointless. It 7345 ** retained purely for backwards compatibility */ 7346 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ 7347 int val = 1; 7348 if( nArg>=2 ){ 7349 if( strcmp(azArg[1],"auto")==0 ){ 7350 val = 99; 7351 }else{ 7352 val = booleanValue(azArg[1]); 7353 } 7354 } 7355 if( val==1 && p->mode!=MODE_Explain ){ 7356 p->normalMode = p->mode; 7357 p->mode = MODE_Explain; 7358 p->autoExplain = 0; 7359 }else if( val==0 ){ 7360 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7361 p->autoExplain = 0; 7362 }else if( val==99 ){ 7363 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 7364 p->autoExplain = 1; 7365 } 7366 }else 7367 7368#ifndef SQLITE_OMIT_VIRTUALTABLE 7369 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ 7370 open_db(p, 0); 7371 expertDotCommand(p, azArg, nArg); 7372 }else 7373#endif 7374 7375 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ 7376 static const struct { 7377 const char *zCtrlName; /* Name of a test-control option */ 7378 int ctrlCode; /* Integer code for that option */ 7379 const char *zUsage; /* Usage notes */ 7380 } aCtrl[] = { 7381 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, 7382 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, 7383 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ 7384 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, 7385 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, 7386 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ 7387 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, 7388 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, 7389 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, 7390 }; 7391 int filectrl = -1; 7392 int iCtrl = -1; 7393 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ 7394 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ 7395 int n2, i; 7396 const char *zCmd = 0; 7397 7398 open_db(p, 0); 7399 zCmd = nArg>=2 ? azArg[1] : "help"; 7400 7401 /* The argument can optionally begin with "-" or "--" */ 7402 if( zCmd[0]=='-' && zCmd[1] ){ 7403 zCmd++; 7404 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 7405 } 7406 7407 /* --help lists all file-controls */ 7408 if( strcmp(zCmd,"help")==0 ){ 7409 utf8_printf(p->out, "Available file-controls:\n"); 7410 for(i=0; i<ArraySize(aCtrl); i++){ 7411 utf8_printf(p->out, " .filectrl %s %s\n", 7412 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 7413 } 7414 rc = 1; 7415 goto meta_command_exit; 7416 } 7417 7418 /* convert filectrl text option to value. allow any unique prefix 7419 ** of the option name, or a numerical value. */ 7420 n2 = strlen30(zCmd); 7421 for(i=0; i<ArraySize(aCtrl); i++){ 7422 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 7423 if( filectrl<0 ){ 7424 filectrl = aCtrl[i].ctrlCode; 7425 iCtrl = i; 7426 }else{ 7427 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" 7428 "Use \".filectrl --help\" for help\n", zCmd); 7429 rc = 1; 7430 goto meta_command_exit; 7431 } 7432 } 7433 } 7434 if( filectrl<0 ){ 7435 utf8_printf(stderr,"Error: unknown file-control: %s\n" 7436 "Use \".filectrl --help\" for help\n", zCmd); 7437 }else{ 7438 switch(filectrl){ 7439 case SQLITE_FCNTL_SIZE_LIMIT: { 7440 if( nArg!=2 && nArg!=3 ) break; 7441 iRes = nArg==3 ? integerValue(azArg[2]) : -1; 7442 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes); 7443 isOk = 1; 7444 break; 7445 } 7446 case SQLITE_FCNTL_LOCK_TIMEOUT: 7447 case SQLITE_FCNTL_CHUNK_SIZE: { 7448 int x; 7449 if( nArg!=3 ) break; 7450 x = (int)integerValue(azArg[2]); 7451 sqlite3_file_control(p->db, 0, filectrl, &x); 7452 isOk = 2; 7453 break; 7454 } 7455 case SQLITE_FCNTL_PERSIST_WAL: 7456 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { 7457 int x; 7458 if( nArg!=2 && nArg!=3 ) break; 7459 x = nArg==3 ? booleanValue(azArg[2]) : -1; 7460 sqlite3_file_control(p->db, 0, filectrl, &x); 7461 iRes = x; 7462 isOk = 1; 7463 break; 7464 } 7465 case SQLITE_FCNTL_HAS_MOVED: { 7466 int x; 7467 if( nArg!=2 ) break; 7468 sqlite3_file_control(p->db, 0, filectrl, &x); 7469 iRes = x; 7470 isOk = 1; 7471 break; 7472 } 7473 case SQLITE_FCNTL_TEMPFILENAME: { 7474 char *z = 0; 7475 if( nArg!=2 ) break; 7476 sqlite3_file_control(p->db, 0, filectrl, &z); 7477 if( z ){ 7478 utf8_printf(p->out, "%s\n", z); 7479 sqlite3_free(z); 7480 } 7481 isOk = 2; 7482 break; 7483 } 7484 } 7485 } 7486 if( isOk==0 && iCtrl>=0 ){ 7487 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 7488 rc = 1; 7489 }else if( isOk==1 ){ 7490 char zBuf[100]; 7491 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); 7492 raw_printf(p->out, "%s\n", zBuf); 7493 } 7494 }else 7495 7496 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ 7497 ShellState data; 7498 char *zErrMsg = 0; 7499 int doStats = 0; 7500 memcpy(&data, p, sizeof(data)); 7501 data.showHeader = 0; 7502 data.cMode = data.mode = MODE_Semi; 7503 if( nArg==2 && optionMatch(azArg[1], "indent") ){ 7504 data.cMode = data.mode = MODE_Pretty; 7505 nArg = 1; 7506 } 7507 if( nArg!=1 ){ 7508 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); 7509 rc = 1; 7510 goto meta_command_exit; 7511 } 7512 open_db(p, 0); 7513 rc = sqlite3_exec(p->db, 7514 "SELECT sql FROM" 7515 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 7516 " FROM sqlite_master UNION ALL" 7517 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 7518 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 7519 "ORDER BY rowid", 7520 callback, &data, &zErrMsg 7521 ); 7522 if( rc==SQLITE_OK ){ 7523 sqlite3_stmt *pStmt; 7524 rc = sqlite3_prepare_v2(p->db, 7525 "SELECT rowid FROM sqlite_master" 7526 " WHERE name GLOB 'sqlite_stat[134]'", 7527 -1, &pStmt, 0); 7528 doStats = sqlite3_step(pStmt)==SQLITE_ROW; 7529 sqlite3_finalize(pStmt); 7530 } 7531 if( doStats==0 ){ 7532 raw_printf(p->out, "/* No STAT tables available */\n"); 7533 }else{ 7534 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7535 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", 7536 callback, &data, &zErrMsg); 7537 data.cMode = data.mode = MODE_Insert; 7538 data.zDestTable = "sqlite_stat1"; 7539 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg); 7540 data.zDestTable = "sqlite_stat4"; 7541 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg); 7542 raw_printf(p->out, "ANALYZE sqlite_master;\n"); 7543 } 7544 }else 7545 7546 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ 7547 if( nArg==2 ){ 7548 p->showHeader = booleanValue(azArg[1]); 7549 }else{ 7550 raw_printf(stderr, "Usage: .headers on|off\n"); 7551 rc = 1; 7552 } 7553 }else 7554 7555 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ 7556 if( nArg>=2 ){ 7557 n = showHelp(p->out, azArg[1]); 7558 if( n==0 ){ 7559 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); 7560 } 7561 }else{ 7562 showHelp(p->out, 0); 7563 } 7564 }else 7565 7566 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ 7567 char *zTable; /* Insert data into this table */ 7568 char *zFile; /* Name of file to extra content from */ 7569 sqlite3_stmt *pStmt = NULL; /* A statement */ 7570 int nCol; /* Number of columns in the table */ 7571 int nByte; /* Number of bytes in an SQL string */ 7572 int i, j; /* Loop counters */ 7573 int needCommit; /* True to COMMIT or ROLLBACK at end */ 7574 int nSep; /* Number of bytes in p->colSeparator[] */ 7575 char *zSql; /* An SQL statement */ 7576 ImportCtx sCtx; /* Reader context */ 7577 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ 7578 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ 7579 7580 if( nArg!=3 ){ 7581 raw_printf(stderr, "Usage: .import FILE TABLE\n"); 7582 goto meta_command_exit; 7583 } 7584 zFile = azArg[1]; 7585 zTable = azArg[2]; 7586 seenInterrupt = 0; 7587 memset(&sCtx, 0, sizeof(sCtx)); 7588 open_db(p, 0); 7589 nSep = strlen30(p->colSeparator); 7590 if( nSep==0 ){ 7591 raw_printf(stderr, 7592 "Error: non-null column separator required for import\n"); 7593 return 1; 7594 } 7595 if( nSep>1 ){ 7596 raw_printf(stderr, "Error: multi-character column separators not allowed" 7597 " for import\n"); 7598 return 1; 7599 } 7600 nSep = strlen30(p->rowSeparator); 7601 if( nSep==0 ){ 7602 raw_printf(stderr, "Error: non-null row separator required for import\n"); 7603 return 1; 7604 } 7605 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ 7606 /* When importing CSV (only), if the row separator is set to the 7607 ** default output row separator, change it to the default input 7608 ** row separator. This avoids having to maintain different input 7609 ** and output row separators. */ 7610 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7611 nSep = strlen30(p->rowSeparator); 7612 } 7613 if( nSep>1 ){ 7614 raw_printf(stderr, "Error: multi-character row separators not allowed" 7615 " for import\n"); 7616 return 1; 7617 } 7618 sCtx.zFile = zFile; 7619 sCtx.nLine = 1; 7620 if( sCtx.zFile[0]=='|' ){ 7621#ifdef SQLITE_OMIT_POPEN 7622 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 7623 return 1; 7624#else 7625 sCtx.in = popen(sCtx.zFile+1, "r"); 7626 sCtx.zFile = "<pipe>"; 7627 xCloser = pclose; 7628#endif 7629 }else{ 7630 sCtx.in = fopen(sCtx.zFile, "rb"); 7631 xCloser = fclose; 7632 } 7633 if( p->mode==MODE_Ascii ){ 7634 xRead = ascii_read_one_field; 7635 }else{ 7636 xRead = csv_read_one_field; 7637 } 7638 if( sCtx.in==0 ){ 7639 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 7640 return 1; 7641 } 7642 sCtx.cColSep = p->colSeparator[0]; 7643 sCtx.cRowSep = p->rowSeparator[0]; 7644 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); 7645 if( zSql==0 ){ 7646 xCloser(sCtx.in); 7647 shell_out_of_memory(); 7648 } 7649 nByte = strlen30(zSql); 7650 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7651 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ 7652 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ 7653 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); 7654 char cSep = '('; 7655 while( xRead(&sCtx) ){ 7656 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); 7657 cSep = ','; 7658 if( sCtx.cTerm!=sCtx.cColSep ) break; 7659 } 7660 if( cSep=='(' ){ 7661 sqlite3_free(zCreate); 7662 sqlite3_free(sCtx.z); 7663 xCloser(sCtx.in); 7664 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); 7665 return 1; 7666 } 7667 zCreate = sqlite3_mprintf("%z\n)", zCreate); 7668 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); 7669 sqlite3_free(zCreate); 7670 if( rc ){ 7671 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, 7672 sqlite3_errmsg(p->db)); 7673 sqlite3_free(sCtx.z); 7674 xCloser(sCtx.in); 7675 return 1; 7676 } 7677 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7678 } 7679 sqlite3_free(zSql); 7680 if( rc ){ 7681 if (pStmt) sqlite3_finalize(pStmt); 7682 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); 7683 xCloser(sCtx.in); 7684 return 1; 7685 } 7686 nCol = sqlite3_column_count(pStmt); 7687 sqlite3_finalize(pStmt); 7688 pStmt = 0; 7689 if( nCol==0 ) return 0; /* no columns, no error */ 7690 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); 7691 if( zSql==0 ){ 7692 xCloser(sCtx.in); 7693 shell_out_of_memory(); 7694 } 7695 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); 7696 j = strlen30(zSql); 7697 for(i=1; i<nCol; i++){ 7698 zSql[j++] = ','; 7699 zSql[j++] = '?'; 7700 } 7701 zSql[j++] = ')'; 7702 zSql[j] = 0; 7703 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7704 sqlite3_free(zSql); 7705 if( rc ){ 7706 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 7707 if (pStmt) sqlite3_finalize(pStmt); 7708 xCloser(sCtx.in); 7709 return 1; 7710 } 7711 needCommit = sqlite3_get_autocommit(p->db); 7712 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); 7713 do{ 7714 int startLine = sCtx.nLine; 7715 for(i=0; i<nCol; i++){ 7716 char *z = xRead(&sCtx); 7717 /* 7718 ** Did we reach end-of-file before finding any columns? 7719 ** If so, stop instead of NULL filling the remaining columns. 7720 */ 7721 if( z==0 && i==0 ) break; 7722 /* 7723 ** Did we reach end-of-file OR end-of-line before finding any 7724 ** columns in ASCII mode? If so, stop instead of NULL filling 7725 ** the remaining columns. 7726 */ 7727 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; 7728 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); 7729 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ 7730 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 7731 "filling the rest with NULL\n", 7732 sCtx.zFile, startLine, nCol, i+1); 7733 i += 2; 7734 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } 7735 } 7736 } 7737 if( sCtx.cTerm==sCtx.cColSep ){ 7738 do{ 7739 xRead(&sCtx); 7740 i++; 7741 }while( sCtx.cTerm==sCtx.cColSep ); 7742 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 7743 "extras ignored\n", 7744 sCtx.zFile, startLine, nCol, i); 7745 } 7746 if( i>=nCol ){ 7747 sqlite3_step(pStmt); 7748 rc = sqlite3_reset(pStmt); 7749 if( rc!=SQLITE_OK ){ 7750 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, 7751 startLine, sqlite3_errmsg(p->db)); 7752 } 7753 } 7754 }while( sCtx.cTerm!=EOF ); 7755 7756 xCloser(sCtx.in); 7757 sqlite3_free(sCtx.z); 7758 sqlite3_finalize(pStmt); 7759 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); 7760 }else 7761 7762#ifndef SQLITE_UNTESTABLE 7763 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ 7764 char *zSql; 7765 char *zCollist = 0; 7766 sqlite3_stmt *pStmt; 7767 int tnum = 0; 7768 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ 7769 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ 7770 int i; 7771 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ 7772 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" 7773 " .imposter off\n"); 7774 /* Also allowed, but not documented: 7775 ** 7776 ** .imposter TABLE IMPOSTER 7777 ** 7778 ** where TABLE is a WITHOUT ROWID table. In that case, the 7779 ** imposter is another WITHOUT ROWID table with the columns in 7780 ** storage order. */ 7781 rc = 1; 7782 goto meta_command_exit; 7783 } 7784 open_db(p, 0); 7785 if( nArg==2 ){ 7786 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); 7787 goto meta_command_exit; 7788 } 7789 zSql = sqlite3_mprintf( 7790 "SELECT rootpage, 0 FROM sqlite_master" 7791 " WHERE name='%q' AND type='index'" 7792 "UNION ALL " 7793 "SELECT rootpage, 1 FROM sqlite_master" 7794 " WHERE name='%q' AND type='table'" 7795 " AND sql LIKE '%%without%%rowid%%'", 7796 azArg[1], azArg[1] 7797 ); 7798 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7799 sqlite3_free(zSql); 7800 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 7801 tnum = sqlite3_column_int(pStmt, 0); 7802 isWO = sqlite3_column_int(pStmt, 1); 7803 } 7804 sqlite3_finalize(pStmt); 7805 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); 7806 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 7807 sqlite3_free(zSql); 7808 i = 0; 7809 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 7810 char zLabel[20]; 7811 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); 7812 i++; 7813 if( zCol==0 ){ 7814 if( sqlite3_column_int(pStmt,1)==-1 ){ 7815 zCol = "_ROWID_"; 7816 }else{ 7817 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); 7818 zCol = zLabel; 7819 } 7820 } 7821 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ 7822 lenPK = (int)strlen(zCollist); 7823 } 7824 if( zCollist==0 ){ 7825 zCollist = sqlite3_mprintf("\"%w\"", zCol); 7826 }else{ 7827 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); 7828 } 7829 } 7830 sqlite3_finalize(pStmt); 7831 if( i==0 || tnum==0 ){ 7832 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); 7833 rc = 1; 7834 sqlite3_free(zCollist); 7835 goto meta_command_exit; 7836 } 7837 if( lenPK==0 ) lenPK = 100000; 7838 zSql = sqlite3_mprintf( 7839 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", 7840 azArg[2], zCollist, lenPK, zCollist); 7841 sqlite3_free(zCollist); 7842 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); 7843 if( rc==SQLITE_OK ){ 7844 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 7845 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); 7846 if( rc ){ 7847 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); 7848 }else{ 7849 utf8_printf(stdout, "%s;\n", zSql); 7850 raw_printf(stdout, 7851 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", 7852 azArg[1], isWO ? "table" : "index" 7853 ); 7854 } 7855 }else{ 7856 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); 7857 rc = 1; 7858 } 7859 sqlite3_free(zSql); 7860 }else 7861#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ 7862 7863#ifdef SQLITE_ENABLE_IOTRACE 7864 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ 7865 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); 7866 if( iotrace && iotrace!=stdout ) fclose(iotrace); 7867 iotrace = 0; 7868 if( nArg<2 ){ 7869 sqlite3IoTrace = 0; 7870 }else if( strcmp(azArg[1], "-")==0 ){ 7871 sqlite3IoTrace = iotracePrintf; 7872 iotrace = stdout; 7873 }else{ 7874 iotrace = fopen(azArg[1], "w"); 7875 if( iotrace==0 ){ 7876 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 7877 sqlite3IoTrace = 0; 7878 rc = 1; 7879 }else{ 7880 sqlite3IoTrace = iotracePrintf; 7881 } 7882 } 7883 }else 7884#endif 7885 7886 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ 7887 static const struct { 7888 const char *zLimitName; /* Name of a limit */ 7889 int limitCode; /* Integer code for that limit */ 7890 } aLimit[] = { 7891 { "length", SQLITE_LIMIT_LENGTH }, 7892 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, 7893 { "column", SQLITE_LIMIT_COLUMN }, 7894 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, 7895 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, 7896 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, 7897 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, 7898 { "attached", SQLITE_LIMIT_ATTACHED }, 7899 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 7900 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, 7901 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, 7902 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, 7903 }; 7904 int i, n2; 7905 open_db(p, 0); 7906 if( nArg==1 ){ 7907 for(i=0; i<ArraySize(aLimit); i++){ 7908 printf("%20s %d\n", aLimit[i].zLimitName, 7909 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); 7910 } 7911 }else if( nArg>3 ){ 7912 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); 7913 rc = 1; 7914 goto meta_command_exit; 7915 }else{ 7916 int iLimit = -1; 7917 n2 = strlen30(azArg[1]); 7918 for(i=0; i<ArraySize(aLimit); i++){ 7919 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ 7920 if( iLimit<0 ){ 7921 iLimit = i; 7922 }else{ 7923 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); 7924 rc = 1; 7925 goto meta_command_exit; 7926 } 7927 } 7928 } 7929 if( iLimit<0 ){ 7930 utf8_printf(stderr, "unknown limit: \"%s\"\n" 7931 "enter \".limits\" with no arguments for a list.\n", 7932 azArg[1]); 7933 rc = 1; 7934 goto meta_command_exit; 7935 } 7936 if( nArg==3 ){ 7937 sqlite3_limit(p->db, aLimit[iLimit].limitCode, 7938 (int)integerValue(azArg[2])); 7939 } 7940 printf("%20s %d\n", aLimit[iLimit].zLimitName, 7941 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); 7942 } 7943 }else 7944 7945 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ 7946 open_db(p, 0); 7947 lintDotCommand(p, azArg, nArg); 7948 }else 7949 7950#ifndef SQLITE_OMIT_LOAD_EXTENSION 7951 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ 7952 const char *zFile, *zProc; 7953 char *zErrMsg = 0; 7954 if( nArg<2 ){ 7955 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); 7956 rc = 1; 7957 goto meta_command_exit; 7958 } 7959 zFile = azArg[1]; 7960 zProc = nArg>=3 ? azArg[2] : 0; 7961 open_db(p, 0); 7962 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); 7963 if( rc!=SQLITE_OK ){ 7964 utf8_printf(stderr, "Error: %s\n", zErrMsg); 7965 sqlite3_free(zErrMsg); 7966 rc = 1; 7967 } 7968 }else 7969#endif 7970 7971 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ 7972 if( nArg!=2 ){ 7973 raw_printf(stderr, "Usage: .log FILENAME\n"); 7974 rc = 1; 7975 }else{ 7976 const char *zFile = azArg[1]; 7977 output_file_close(p->pLog); 7978 p->pLog = output_file_open(zFile, 0); 7979 } 7980 }else 7981 7982 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ 7983 const char *zMode = nArg>=2 ? azArg[1] : ""; 7984 int n2 = strlen30(zMode); 7985 int c2 = zMode[0]; 7986 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ 7987 p->mode = MODE_Line; 7988 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7989 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ 7990 p->mode = MODE_Column; 7991 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7992 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ 7993 p->mode = MODE_List; 7994 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); 7995 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 7996 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ 7997 p->mode = MODE_Html; 7998 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ 7999 p->mode = MODE_Tcl; 8000 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); 8001 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8002 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ 8003 p->mode = MODE_Csv; 8004 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8005 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8006 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ 8007 p->mode = MODE_List; 8008 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); 8009 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ 8010 p->mode = MODE_Insert; 8011 set_table_name(p, nArg>=3 ? azArg[2] : "table"); 8012 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ 8013 p->mode = MODE_Quote; 8014 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ 8015 p->mode = MODE_Ascii; 8016 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); 8017 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); 8018 }else if( nArg==1 ){ 8019 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); 8020 }else{ 8021 raw_printf(stderr, "Error: mode should be one of: " 8022 "ascii column csv html insert line list quote tabs tcl\n"); 8023 rc = 1; 8024 } 8025 p->cMode = p->mode; 8026 }else 8027 8028 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ 8029 if( nArg==2 ){ 8030 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, 8031 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); 8032 }else{ 8033 raw_printf(stderr, "Usage: .nullvalue STRING\n"); 8034 rc = 1; 8035 } 8036 }else 8037 8038 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ 8039 char *zNewFilename; /* Name of the database file to open */ 8040 int iName = 1; /* Index in azArg[] of the filename */ 8041 int newFlag = 0; /* True to delete file before opening */ 8042 /* Close the existing database */ 8043 session_close_all(p); 8044 close_db(p->db); 8045 p->db = 0; 8046 p->zDbFilename = 0; 8047 sqlite3_free(p->zFreeOnClose); 8048 p->zFreeOnClose = 0; 8049 p->openMode = SHELL_OPEN_UNSPEC; 8050 p->openFlags = 0; 8051 p->szMax = 0; 8052 /* Check for command-line arguments */ 8053 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ 8054 const char *z = azArg[iName]; 8055 if( optionMatch(z,"new") ){ 8056 newFlag = 1; 8057#ifdef SQLITE_HAVE_ZLIB 8058 }else if( optionMatch(z, "zip") ){ 8059 p->openMode = SHELL_OPEN_ZIPFILE; 8060#endif 8061 }else if( optionMatch(z, "append") ){ 8062 p->openMode = SHELL_OPEN_APPENDVFS; 8063 }else if( optionMatch(z, "readonly") ){ 8064 p->openMode = SHELL_OPEN_READONLY; 8065 }else if( optionMatch(z, "nofollow") ){ 8066 p->openFlags |= SQLITE_OPEN_NOFOLLOW; 8067#ifdef SQLITE_ENABLE_DESERIALIZE 8068 }else if( optionMatch(z, "deserialize") ){ 8069 p->openMode = SHELL_OPEN_DESERIALIZE; 8070 }else if( optionMatch(z, "hexdb") ){ 8071 p->openMode = SHELL_OPEN_HEXDB; 8072 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ 8073 p->szMax = integerValue(azArg[++iName]); 8074#endif /* SQLITE_ENABLE_DESERIALIZE */ 8075 }else if( z[0]=='-' ){ 8076 utf8_printf(stderr, "unknown option: %s\n", z); 8077 rc = 1; 8078 goto meta_command_exit; 8079 } 8080 } 8081 /* If a filename is specified, try to open it first */ 8082 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; 8083 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){ 8084 if( newFlag ) shellDeleteFile(zNewFilename); 8085 p->zDbFilename = zNewFilename; 8086 open_db(p, OPEN_DB_KEEPALIVE); 8087 if( p->db==0 ){ 8088 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); 8089 sqlite3_free(zNewFilename); 8090 }else{ 8091 p->zFreeOnClose = zNewFilename; 8092 } 8093 } 8094 if( p->db==0 ){ 8095 /* As a fall-back open a TEMP database */ 8096 p->zDbFilename = 0; 8097 open_db(p, 0); 8098 } 8099 }else 8100 8101 if( (c=='o' 8102 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) 8103 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) 8104 ){ 8105 const char *zFile = nArg>=2 ? azArg[1] : "stdout"; 8106 int bTxtMode = 0; 8107 if( azArg[0][0]=='e' ){ 8108 /* Transform the ".excel" command into ".once -x" */ 8109 nArg = 2; 8110 azArg[0] = "once"; 8111 zFile = azArg[1] = "-x"; 8112 n = 4; 8113 } 8114 if( nArg>2 ){ 8115 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]); 8116 rc = 1; 8117 goto meta_command_exit; 8118 } 8119 if( n>1 && strncmp(azArg[0], "once", n)==0 ){ 8120 if( nArg<2 ){ 8121 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n"); 8122 rc = 1; 8123 goto meta_command_exit; 8124 } 8125 p->outCount = 2; 8126 }else{ 8127 p->outCount = 0; 8128 } 8129 output_reset(p); 8130 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++; 8131#ifndef SQLITE_NOHAVE_SYSTEM 8132 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){ 8133 p->doXdgOpen = 1; 8134 outputModePush(p); 8135 if( zFile[1]=='x' ){ 8136 newTempFile(p, "csv"); 8137 p->mode = MODE_Csv; 8138 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8139 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8140 }else{ 8141 newTempFile(p, "txt"); 8142 bTxtMode = 1; 8143 } 8144 zFile = p->zTempFile; 8145 } 8146#endif /* SQLITE_NOHAVE_SYSTEM */ 8147 if( zFile[0]=='|' ){ 8148#ifdef SQLITE_OMIT_POPEN 8149 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8150 rc = 1; 8151 p->out = stdout; 8152#else 8153 p->out = popen(zFile + 1, "w"); 8154 if( p->out==0 ){ 8155 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); 8156 p->out = stdout; 8157 rc = 1; 8158 }else{ 8159 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8160 } 8161#endif 8162 }else{ 8163 p->out = output_file_open(zFile, bTxtMode); 8164 if( p->out==0 ){ 8165 if( strcmp(zFile,"off")!=0 ){ 8166 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); 8167 } 8168 p->out = stdout; 8169 rc = 1; 8170 } else { 8171 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 8172 } 8173 } 8174 }else 8175 8176 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ 8177 open_db(p,0); 8178 if( nArg<=1 ) goto parameter_syntax_error; 8179 8180 /* .parameter clear 8181 ** Clear all bind parameters by dropping the TEMP table that holds them. 8182 */ 8183 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ 8184 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", 8185 0, 0, 0); 8186 }else 8187 8188 /* .parameter list 8189 ** List all bind parameters. 8190 */ 8191 if( nArg==2 && strcmp(azArg[1],"list")==0 ){ 8192 sqlite3_stmt *pStmt = 0; 8193 int rx; 8194 int len = 0; 8195 rx = sqlite3_prepare_v2(p->db, 8196 "SELECT max(length(key)) " 8197 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8198 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 8199 len = sqlite3_column_int(pStmt, 0); 8200 if( len>40 ) len = 40; 8201 } 8202 sqlite3_finalize(pStmt); 8203 pStmt = 0; 8204 if( len ){ 8205 rx = sqlite3_prepare_v2(p->db, 8206 "SELECT key, quote(value) " 8207 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 8208 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8209 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), 8210 sqlite3_column_text(pStmt,1)); 8211 } 8212 sqlite3_finalize(pStmt); 8213 } 8214 }else 8215 8216 /* .parameter init 8217 ** Make sure the TEMP table used to hold bind parameters exists. 8218 ** Create it if necessary. 8219 */ 8220 if( nArg==2 && strcmp(azArg[1],"init")==0 ){ 8221 bind_table_init(p); 8222 }else 8223 8224 /* .parameter set NAME VALUE 8225 ** Set or reset a bind parameter. NAME should be the full parameter 8226 ** name exactly as it appears in the query. (ex: $abc, @def). The 8227 ** VALUE can be in either SQL literal notation, or if not it will be 8228 ** understood to be a text string. 8229 */ 8230 if( nArg==4 && strcmp(azArg[1],"set")==0 ){ 8231 int rx; 8232 char *zSql; 8233 sqlite3_stmt *pStmt; 8234 const char *zKey = azArg[2]; 8235 const char *zValue = azArg[3]; 8236 bind_table_init(p); 8237 zSql = sqlite3_mprintf( 8238 "REPLACE INTO temp.sqlite_parameters(key,value)" 8239 "VALUES(%Q,%s);", zKey, zValue); 8240 if( zSql==0 ) shell_out_of_memory(); 8241 pStmt = 0; 8242 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8243 sqlite3_free(zSql); 8244 if( rx!=SQLITE_OK ){ 8245 sqlite3_finalize(pStmt); 8246 pStmt = 0; 8247 zSql = sqlite3_mprintf( 8248 "REPLACE INTO temp.sqlite_parameters(key,value)" 8249 "VALUES(%Q,%Q);", zKey, zValue); 8250 if( zSql==0 ) shell_out_of_memory(); 8251 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8252 sqlite3_free(zSql); 8253 if( rx!=SQLITE_OK ){ 8254 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); 8255 sqlite3_finalize(pStmt); 8256 pStmt = 0; 8257 rc = 1; 8258 } 8259 } 8260 sqlite3_step(pStmt); 8261 sqlite3_finalize(pStmt); 8262 }else 8263 8264 /* .parameter unset NAME 8265 ** Remove the NAME binding from the parameter binding table, if it 8266 ** exists. 8267 */ 8268 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ 8269 char *zSql = sqlite3_mprintf( 8270 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); 8271 if( zSql==0 ) shell_out_of_memory(); 8272 sqlite3_exec(p->db, zSql, 0, 0, 0); 8273 sqlite3_free(zSql); 8274 }else 8275 /* If no command name matches, show a syntax error */ 8276 parameter_syntax_error: 8277 showHelp(p->out, "parameter"); 8278 }else 8279 8280 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ 8281 int i; 8282 for(i=1; i<nArg; i++){ 8283 if( i>1 ) raw_printf(p->out, " "); 8284 utf8_printf(p->out, "%s", azArg[i]); 8285 } 8286 raw_printf(p->out, "\n"); 8287 }else 8288 8289#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 8290 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ 8291 int i; 8292 int nn = 0; 8293 p->flgProgress = 0; 8294 p->mxProgress = 0; 8295 p->nProgress = 0; 8296 for(i=1; i<nArg; i++){ 8297 const char *z = azArg[i]; 8298 if( z[0]=='-' ){ 8299 z++; 8300 if( z[0]=='-' ) z++; 8301 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 8302 p->flgProgress |= SHELL_PROGRESS_QUIET; 8303 continue; 8304 } 8305 if( strcmp(z,"reset")==0 ){ 8306 p->flgProgress |= SHELL_PROGRESS_RESET; 8307 continue; 8308 } 8309 if( strcmp(z,"once")==0 ){ 8310 p->flgProgress |= SHELL_PROGRESS_ONCE; 8311 continue; 8312 } 8313 if( strcmp(z,"limit")==0 ){ 8314 if( i+1>=nArg ){ 8315 utf8_printf(stderr, "Error: missing argument on --limit\n"); 8316 rc = 1; 8317 goto meta_command_exit; 8318 }else{ 8319 p->mxProgress = (int)integerValue(azArg[++i]); 8320 } 8321 continue; 8322 } 8323 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); 8324 rc = 1; 8325 goto meta_command_exit; 8326 }else{ 8327 nn = (int)integerValue(z); 8328 } 8329 } 8330 open_db(p, 0); 8331 sqlite3_progress_handler(p->db, nn, progress_handler, p); 8332 }else 8333#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 8334 8335 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ 8336 if( nArg >= 2) { 8337 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); 8338 } 8339 if( nArg >= 3) { 8340 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); 8341 } 8342 }else 8343 8344 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ 8345 rc = 2; 8346 }else 8347 8348 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ 8349 FILE *inSaved = p->in; 8350 int savedLineno = p->lineno; 8351 if( nArg!=2 ){ 8352 raw_printf(stderr, "Usage: .read FILE\n"); 8353 rc = 1; 8354 goto meta_command_exit; 8355 } 8356 p->in = fopen(azArg[1], "rb"); 8357 if( p->in==0 ){ 8358 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); 8359 rc = 1; 8360 }else{ 8361 rc = process_input(p); 8362 fclose(p->in); 8363 } 8364 p->in = inSaved; 8365 p->lineno = savedLineno; 8366 }else 8367 8368 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ 8369 const char *zSrcFile; 8370 const char *zDb; 8371 sqlite3 *pSrc; 8372 sqlite3_backup *pBackup; 8373 int nTimeout = 0; 8374 8375 if( nArg==2 ){ 8376 zSrcFile = azArg[1]; 8377 zDb = "main"; 8378 }else if( nArg==3 ){ 8379 zSrcFile = azArg[2]; 8380 zDb = azArg[1]; 8381 }else{ 8382 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); 8383 rc = 1; 8384 goto meta_command_exit; 8385 } 8386 rc = sqlite3_open(zSrcFile, &pSrc); 8387 if( rc!=SQLITE_OK ){ 8388 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); 8389 close_db(pSrc); 8390 return 1; 8391 } 8392 open_db(p, 0); 8393 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); 8394 if( pBackup==0 ){ 8395 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8396 close_db(pSrc); 8397 return 1; 8398 } 8399 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 8400 || rc==SQLITE_BUSY ){ 8401 if( rc==SQLITE_BUSY ){ 8402 if( nTimeout++ >= 3 ) break; 8403 sqlite3_sleep(100); 8404 } 8405 } 8406 sqlite3_backup_finish(pBackup); 8407 if( rc==SQLITE_DONE ){ 8408 rc = 0; 8409 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 8410 raw_printf(stderr, "Error: source database is busy\n"); 8411 rc = 1; 8412 }else{ 8413 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8414 rc = 1; 8415 } 8416 close_db(pSrc); 8417 }else 8418 8419 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ 8420 if( nArg==2 ){ 8421 p->scanstatsOn = (u8)booleanValue(azArg[1]); 8422#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 8423 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); 8424#endif 8425 }else{ 8426 raw_printf(stderr, "Usage: .scanstats on|off\n"); 8427 rc = 1; 8428 } 8429 }else 8430 8431 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ 8432 ShellText sSelect; 8433 ShellState data; 8434 char *zErrMsg = 0; 8435 const char *zDiv = "("; 8436 const char *zName = 0; 8437 int iSchema = 0; 8438 int bDebug = 0; 8439 int ii; 8440 8441 open_db(p, 0); 8442 memcpy(&data, p, sizeof(data)); 8443 data.showHeader = 0; 8444 data.cMode = data.mode = MODE_Semi; 8445 initText(&sSelect); 8446 for(ii=1; ii<nArg; ii++){ 8447 if( optionMatch(azArg[ii],"indent") ){ 8448 data.cMode = data.mode = MODE_Pretty; 8449 }else if( optionMatch(azArg[ii],"debug") ){ 8450 bDebug = 1; 8451 }else if( zName==0 ){ 8452 zName = azArg[ii]; 8453 }else{ 8454 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); 8455 rc = 1; 8456 goto meta_command_exit; 8457 } 8458 } 8459 if( zName!=0 ){ 8460 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0; 8461 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){ 8462 char *new_argv[2], *new_colv[2]; 8463 new_argv[0] = sqlite3_mprintf( 8464 "CREATE TABLE %s (\n" 8465 " type text,\n" 8466 " name text,\n" 8467 " tbl_name text,\n" 8468 " rootpage integer,\n" 8469 " sql text\n" 8470 ")", isMaster ? "sqlite_master" : "sqlite_temp_master"); 8471 new_argv[1] = 0; 8472 new_colv[0] = "sql"; 8473 new_colv[1] = 0; 8474 callback(&data, 1, new_argv, new_colv); 8475 sqlite3_free(new_argv[0]); 8476 } 8477 } 8478 if( zDiv ){ 8479 sqlite3_stmt *pStmt = 0; 8480 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", 8481 -1, &pStmt, 0); 8482 if( rc ){ 8483 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8484 sqlite3_finalize(pStmt); 8485 rc = 1; 8486 goto meta_command_exit; 8487 } 8488 appendText(&sSelect, "SELECT sql FROM", 0); 8489 iSchema = 0; 8490 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 8491 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); 8492 char zScNum[30]; 8493 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); 8494 appendText(&sSelect, zDiv, 0); 8495 zDiv = " UNION ALL "; 8496 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); 8497 if( sqlite3_stricmp(zDb, "main")!=0 ){ 8498 appendText(&sSelect, zDb, '\''); 8499 }else{ 8500 appendText(&sSelect, "NULL", 0); 8501 } 8502 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); 8503 appendText(&sSelect, zScNum, 0); 8504 appendText(&sSelect, " AS snum, ", 0); 8505 appendText(&sSelect, zDb, '\''); 8506 appendText(&sSelect, " AS sname FROM ", 0); 8507 appendText(&sSelect, zDb, quoteChar(zDb)); 8508 appendText(&sSelect, ".sqlite_master", 0); 8509 } 8510 sqlite3_finalize(pStmt); 8511#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 8512 if( zName ){ 8513 appendText(&sSelect, 8514 " UNION ALL SELECT shell_module_schema(name)," 8515 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 8516 0); 8517 } 8518#endif 8519 appendText(&sSelect, ") WHERE ", 0); 8520 if( zName ){ 8521 char *zQarg = sqlite3_mprintf("%Q", zName); 8522 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || 8523 strchr(zName, '[') != 0; 8524 if( strchr(zName, '.') ){ 8525 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); 8526 }else{ 8527 appendText(&sSelect, "lower(tbl_name)", 0); 8528 } 8529 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); 8530 appendText(&sSelect, zQarg, 0); 8531 if( !bGlob ){ 8532 appendText(&sSelect, " ESCAPE '\\' ", 0); 8533 } 8534 appendText(&sSelect, " AND ", 0); 8535 sqlite3_free(zQarg); 8536 } 8537 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" 8538 " ORDER BY snum, rowid", 0); 8539 if( bDebug ){ 8540 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); 8541 }else{ 8542 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); 8543 } 8544 freeText(&sSelect); 8545 } 8546 if( zErrMsg ){ 8547 utf8_printf(stderr,"Error: %s\n", zErrMsg); 8548 sqlite3_free(zErrMsg); 8549 rc = 1; 8550 }else if( rc != SQLITE_OK ){ 8551 raw_printf(stderr,"Error: querying schema information\n"); 8552 rc = 1; 8553 }else{ 8554 rc = 0; 8555 } 8556 }else 8557 8558#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 8559 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ 8560 sqlite3SelectTrace = (int)integerValue(azArg[1]); 8561 }else 8562#endif 8563 8564#if defined(SQLITE_ENABLE_SESSION) 8565 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ 8566 OpenSession *pSession = &p->aSession[0]; 8567 char **azCmd = &azArg[1]; 8568 int iSes = 0; 8569 int nCmd = nArg - 1; 8570 int i; 8571 if( nArg<=1 ) goto session_syntax_error; 8572 open_db(p, 0); 8573 if( nArg>=3 ){ 8574 for(iSes=0; iSes<p->nSession; iSes++){ 8575 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; 8576 } 8577 if( iSes<p->nSession ){ 8578 pSession = &p->aSession[iSes]; 8579 azCmd++; 8580 nCmd--; 8581 }else{ 8582 pSession = &p->aSession[0]; 8583 iSes = 0; 8584 } 8585 } 8586 8587 /* .session attach TABLE 8588 ** Invoke the sqlite3session_attach() interface to attach a particular 8589 ** table so that it is never filtered. 8590 */ 8591 if( strcmp(azCmd[0],"attach")==0 ){ 8592 if( nCmd!=2 ) goto session_syntax_error; 8593 if( pSession->p==0 ){ 8594 session_not_open: 8595 raw_printf(stderr, "ERROR: No sessions are open\n"); 8596 }else{ 8597 rc = sqlite3session_attach(pSession->p, azCmd[1]); 8598 if( rc ){ 8599 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); 8600 rc = 0; 8601 } 8602 } 8603 }else 8604 8605 /* .session changeset FILE 8606 ** .session patchset FILE 8607 ** Write a changeset or patchset into a file. The file is overwritten. 8608 */ 8609 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ 8610 FILE *out = 0; 8611 if( nCmd!=2 ) goto session_syntax_error; 8612 if( pSession->p==0 ) goto session_not_open; 8613 out = fopen(azCmd[1], "wb"); 8614 if( out==0 ){ 8615 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", 8616 azCmd[1]); 8617 }else{ 8618 int szChng; 8619 void *pChng; 8620 if( azCmd[0][0]=='c' ){ 8621 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); 8622 }else{ 8623 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); 8624 } 8625 if( rc ){ 8626 printf("Error: error code %d\n", rc); 8627 rc = 0; 8628 } 8629 if( pChng 8630 && fwrite(pChng, szChng, 1, out)!=1 ){ 8631 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", 8632 szChng); 8633 } 8634 sqlite3_free(pChng); 8635 fclose(out); 8636 } 8637 }else 8638 8639 /* .session close 8640 ** Close the identified session 8641 */ 8642 if( strcmp(azCmd[0], "close")==0 ){ 8643 if( nCmd!=1 ) goto session_syntax_error; 8644 if( p->nSession ){ 8645 session_close(pSession); 8646 p->aSession[iSes] = p->aSession[--p->nSession]; 8647 } 8648 }else 8649 8650 /* .session enable ?BOOLEAN? 8651 ** Query or set the enable flag 8652 */ 8653 if( strcmp(azCmd[0], "enable")==0 ){ 8654 int ii; 8655 if( nCmd>2 ) goto session_syntax_error; 8656 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 8657 if( p->nSession ){ 8658 ii = sqlite3session_enable(pSession->p, ii); 8659 utf8_printf(p->out, "session %s enable flag = %d\n", 8660 pSession->zName, ii); 8661 } 8662 }else 8663 8664 /* .session filter GLOB .... 8665 ** Set a list of GLOB patterns of table names to be excluded. 8666 */ 8667 if( strcmp(azCmd[0], "filter")==0 ){ 8668 int ii, nByte; 8669 if( nCmd<2 ) goto session_syntax_error; 8670 if( p->nSession ){ 8671 for(ii=0; ii<pSession->nFilter; ii++){ 8672 sqlite3_free(pSession->azFilter[ii]); 8673 } 8674 sqlite3_free(pSession->azFilter); 8675 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); 8676 pSession->azFilter = sqlite3_malloc( nByte ); 8677 if( pSession->azFilter==0 ){ 8678 raw_printf(stderr, "Error: out or memory\n"); 8679 exit(1); 8680 } 8681 for(ii=1; ii<nCmd; ii++){ 8682 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); 8683 } 8684 pSession->nFilter = ii-1; 8685 } 8686 }else 8687 8688 /* .session indirect ?BOOLEAN? 8689 ** Query or set the indirect flag 8690 */ 8691 if( strcmp(azCmd[0], "indirect")==0 ){ 8692 int ii; 8693 if( nCmd>2 ) goto session_syntax_error; 8694 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 8695 if( p->nSession ){ 8696 ii = sqlite3session_indirect(pSession->p, ii); 8697 utf8_printf(p->out, "session %s indirect flag = %d\n", 8698 pSession->zName, ii); 8699 } 8700 }else 8701 8702 /* .session isempty 8703 ** Determine if the session is empty 8704 */ 8705 if( strcmp(azCmd[0], "isempty")==0 ){ 8706 int ii; 8707 if( nCmd!=1 ) goto session_syntax_error; 8708 if( p->nSession ){ 8709 ii = sqlite3session_isempty(pSession->p); 8710 utf8_printf(p->out, "session %s isempty flag = %d\n", 8711 pSession->zName, ii); 8712 } 8713 }else 8714 8715 /* .session list 8716 ** List all currently open sessions 8717 */ 8718 if( strcmp(azCmd[0],"list")==0 ){ 8719 for(i=0; i<p->nSession; i++){ 8720 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); 8721 } 8722 }else 8723 8724 /* .session open DB NAME 8725 ** Open a new session called NAME on the attached database DB. 8726 ** DB is normally "main". 8727 */ 8728 if( strcmp(azCmd[0],"open")==0 ){ 8729 char *zName; 8730 if( nCmd!=3 ) goto session_syntax_error; 8731 zName = azCmd[2]; 8732 if( zName[0]==0 ) goto session_syntax_error; 8733 for(i=0; i<p->nSession; i++){ 8734 if( strcmp(p->aSession[i].zName,zName)==0 ){ 8735 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); 8736 goto meta_command_exit; 8737 } 8738 } 8739 if( p->nSession>=ArraySize(p->aSession) ){ 8740 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); 8741 goto meta_command_exit; 8742 } 8743 pSession = &p->aSession[p->nSession]; 8744 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); 8745 if( rc ){ 8746 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); 8747 rc = 0; 8748 goto meta_command_exit; 8749 } 8750 pSession->nFilter = 0; 8751 sqlite3session_table_filter(pSession->p, session_filter, pSession); 8752 p->nSession++; 8753 pSession->zName = sqlite3_mprintf("%s", zName); 8754 }else 8755 /* If no command name matches, show a syntax error */ 8756 session_syntax_error: 8757 showHelp(p->out, "session"); 8758 }else 8759#endif 8760 8761#ifdef SQLITE_DEBUG 8762 /* Undocumented commands for internal testing. Subject to change 8763 ** without notice. */ 8764 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ 8765 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ 8766 int i, v; 8767 for(i=1; i<nArg; i++){ 8768 v = booleanValue(azArg[i]); 8769 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); 8770 } 8771 } 8772 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ 8773 int i; sqlite3_int64 v; 8774 for(i=1; i<nArg; i++){ 8775 char zBuf[200]; 8776 v = integerValue(azArg[i]); 8777 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); 8778 utf8_printf(p->out, "%s", zBuf); 8779 } 8780 } 8781 }else 8782#endif 8783 8784 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ 8785 int bIsInit = 0; /* True to initialize the SELFTEST table */ 8786 int bVerbose = 0; /* Verbose output */ 8787 int bSelftestExists; /* True if SELFTEST already exists */ 8788 int i, k; /* Loop counters */ 8789 int nTest = 0; /* Number of tests runs */ 8790 int nErr = 0; /* Number of errors seen */ 8791 ShellText str; /* Answer for a query */ 8792 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ 8793 8794 open_db(p,0); 8795 for(i=1; i<nArg; i++){ 8796 const char *z = azArg[i]; 8797 if( z[0]=='-' && z[1]=='-' ) z++; 8798 if( strcmp(z,"-init")==0 ){ 8799 bIsInit = 1; 8800 }else 8801 if( strcmp(z,"-v")==0 ){ 8802 bVerbose++; 8803 }else 8804 { 8805 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 8806 azArg[i], azArg[0]); 8807 raw_printf(stderr, "Should be one of: --init -v\n"); 8808 rc = 1; 8809 goto meta_command_exit; 8810 } 8811 } 8812 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) 8813 != SQLITE_OK ){ 8814 bSelftestExists = 0; 8815 }else{ 8816 bSelftestExists = 1; 8817 } 8818 if( bIsInit ){ 8819 createSelftestTable(p); 8820 bSelftestExists = 1; 8821 } 8822 initText(&str); 8823 appendText(&str, "x", 0); 8824 for(k=bSelftestExists; k>=0; k--){ 8825 if( k==1 ){ 8826 rc = sqlite3_prepare_v2(p->db, 8827 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", 8828 -1, &pStmt, 0); 8829 }else{ 8830 rc = sqlite3_prepare_v2(p->db, 8831 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," 8832 " (1,'run','PRAGMA integrity_check','ok')", 8833 -1, &pStmt, 0); 8834 } 8835 if( rc ){ 8836 raw_printf(stderr, "Error querying the selftest table\n"); 8837 rc = 1; 8838 sqlite3_finalize(pStmt); 8839 goto meta_command_exit; 8840 } 8841 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ 8842 int tno = sqlite3_column_int(pStmt, 0); 8843 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); 8844 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); 8845 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); 8846 8847 k = 0; 8848 if( bVerbose>0 ){ 8849 char *zQuote = sqlite3_mprintf("%q", zSql); 8850 printf("%d: %s %s\n", tno, zOp, zSql); 8851 sqlite3_free(zQuote); 8852 } 8853 if( strcmp(zOp,"memo")==0 ){ 8854 utf8_printf(p->out, "%s\n", zSql); 8855 }else 8856 if( strcmp(zOp,"run")==0 ){ 8857 char *zErrMsg = 0; 8858 str.n = 0; 8859 str.z[0] = 0; 8860 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); 8861 nTest++; 8862 if( bVerbose ){ 8863 utf8_printf(p->out, "Result: %s\n", str.z); 8864 } 8865 if( rc || zErrMsg ){ 8866 nErr++; 8867 rc = 1; 8868 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); 8869 sqlite3_free(zErrMsg); 8870 }else if( strcmp(zAns,str.z)!=0 ){ 8871 nErr++; 8872 rc = 1; 8873 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); 8874 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); 8875 } 8876 }else 8877 { 8878 utf8_printf(stderr, 8879 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); 8880 rc = 1; 8881 break; 8882 } 8883 } /* End loop over rows of content from SELFTEST */ 8884 sqlite3_finalize(pStmt); 8885 } /* End loop over k */ 8886 freeText(&str); 8887 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); 8888 }else 8889 8890 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ 8891 if( nArg<2 || nArg>3 ){ 8892 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); 8893 rc = 1; 8894 } 8895 if( nArg>=2 ){ 8896 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, 8897 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); 8898 } 8899 if( nArg>=3 ){ 8900 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, 8901 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); 8902 } 8903 }else 8904 8905 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ 8906 const char *zLike = 0; /* Which table to checksum. 0 means everything */ 8907 int i; /* Loop counter */ 8908 int bSchema = 0; /* Also hash the schema */ 8909 int bSeparate = 0; /* Hash each table separately */ 8910 int iSize = 224; /* Hash algorithm to use */ 8911 int bDebug = 0; /* Only show the query that would have run */ 8912 sqlite3_stmt *pStmt; /* For querying tables names */ 8913 char *zSql; /* SQL to be run */ 8914 char *zSep; /* Separator */ 8915 ShellText sSql; /* Complete SQL for the query to run the hash */ 8916 ShellText sQuery; /* Set of queries used to read all content */ 8917 open_db(p, 0); 8918 for(i=1; i<nArg; i++){ 8919 const char *z = azArg[i]; 8920 if( z[0]=='-' ){ 8921 z++; 8922 if( z[0]=='-' ) z++; 8923 if( strcmp(z,"schema")==0 ){ 8924 bSchema = 1; 8925 }else 8926 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 8927 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 8928 ){ 8929 iSize = atoi(&z[5]); 8930 }else 8931 if( strcmp(z,"debug")==0 ){ 8932 bDebug = 1; 8933 }else 8934 { 8935 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 8936 azArg[i], azArg[0]); 8937 showHelp(p->out, azArg[0]); 8938 rc = 1; 8939 goto meta_command_exit; 8940 } 8941 }else if( zLike ){ 8942 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); 8943 rc = 1; 8944 goto meta_command_exit; 8945 }else{ 8946 zLike = z; 8947 bSeparate = 1; 8948 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; 8949 } 8950 } 8951 if( bSchema ){ 8952 zSql = "SELECT lower(name) FROM sqlite_master" 8953 " WHERE type='table' AND coalesce(rootpage,0)>1" 8954 " UNION ALL SELECT 'sqlite_master'" 8955 " ORDER BY 1 collate nocase"; 8956 }else{ 8957 zSql = "SELECT lower(name) FROM sqlite_master" 8958 " WHERE type='table' AND coalesce(rootpage,0)>1" 8959 " AND name NOT LIKE 'sqlite_%'" 8960 " ORDER BY 1 collate nocase"; 8961 } 8962 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8963 initText(&sQuery); 8964 initText(&sSql); 8965 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); 8966 zSep = "VALUES("; 8967 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 8968 const char *zTab = (const char*)sqlite3_column_text(pStmt,0); 8969 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; 8970 if( strncmp(zTab, "sqlite_",7)!=0 ){ 8971 appendText(&sQuery,"SELECT * FROM ", 0); 8972 appendText(&sQuery,zTab,'"'); 8973 appendText(&sQuery," NOT INDEXED;", 0); 8974 }else if( strcmp(zTab, "sqlite_master")==0 ){ 8975 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" 8976 " ORDER BY name;", 0); 8977 }else if( strcmp(zTab, "sqlite_sequence")==0 ){ 8978 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" 8979 " ORDER BY name;", 0); 8980 }else if( strcmp(zTab, "sqlite_stat1")==0 ){ 8981 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" 8982 " ORDER BY tbl,idx;", 0); 8983 }else if( strcmp(zTab, "sqlite_stat4")==0 ){ 8984 appendText(&sQuery, "SELECT * FROM ", 0); 8985 appendText(&sQuery, zTab, 0); 8986 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); 8987 } 8988 appendText(&sSql, zSep, 0); 8989 appendText(&sSql, sQuery.z, '\''); 8990 sQuery.n = 0; 8991 appendText(&sSql, ",", 0); 8992 appendText(&sSql, zTab, '\''); 8993 zSep = "),("; 8994 } 8995 sqlite3_finalize(pStmt); 8996 if( bSeparate ){ 8997 zSql = sqlite3_mprintf( 8998 "%s))" 8999 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" 9000 " FROM [sha3sum$query]", 9001 sSql.z, iSize); 9002 }else{ 9003 zSql = sqlite3_mprintf( 9004 "%s))" 9005 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" 9006 " FROM [sha3sum$query]", 9007 sSql.z, iSize); 9008 } 9009 freeText(&sQuery); 9010 freeText(&sSql); 9011 if( bDebug ){ 9012 utf8_printf(p->out, "%s\n", zSql); 9013 }else{ 9014 shell_exec(p, zSql, 0); 9015 } 9016 sqlite3_free(zSql); 9017 }else 9018 9019#ifndef SQLITE_NOHAVE_SYSTEM 9020 if( c=='s' 9021 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) 9022 ){ 9023 char *zCmd; 9024 int i, x; 9025 if( nArg<2 ){ 9026 raw_printf(stderr, "Usage: .system COMMAND\n"); 9027 rc = 1; 9028 goto meta_command_exit; 9029 } 9030 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); 9031 for(i=2; i<nArg; i++){ 9032 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", 9033 zCmd, azArg[i]); 9034 } 9035 x = system(zCmd); 9036 sqlite3_free(zCmd); 9037 if( x ) raw_printf(stderr, "System command returns %d\n", x); 9038 }else 9039#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 9040 9041 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ 9042 static const char *azBool[] = { "off", "on", "trigger", "full"}; 9043 int i; 9044 if( nArg!=1 ){ 9045 raw_printf(stderr, "Usage: .show\n"); 9046 rc = 1; 9047 goto meta_command_exit; 9048 } 9049 utf8_printf(p->out, "%12.12s: %s\n","echo", 9050 azBool[ShellHasFlag(p, SHFLG_Echo)]); 9051 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); 9052 utf8_printf(p->out, "%12.12s: %s\n","explain", 9053 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); 9054 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); 9055 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); 9056 utf8_printf(p->out, "%12.12s: ", "nullvalue"); 9057 output_c_string(p->out, p->nullValue); 9058 raw_printf(p->out, "\n"); 9059 utf8_printf(p->out,"%12.12s: %s\n","output", 9060 strlen30(p->outfile) ? p->outfile : "stdout"); 9061 utf8_printf(p->out,"%12.12s: ", "colseparator"); 9062 output_c_string(p->out, p->colSeparator); 9063 raw_printf(p->out, "\n"); 9064 utf8_printf(p->out,"%12.12s: ", "rowseparator"); 9065 output_c_string(p->out, p->rowSeparator); 9066 raw_printf(p->out, "\n"); 9067 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); 9068 utf8_printf(p->out, "%12.12s: ", "width"); 9069 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { 9070 raw_printf(p->out, "%d ", p->colWidth[i]); 9071 } 9072 raw_printf(p->out, "\n"); 9073 utf8_printf(p->out, "%12.12s: %s\n", "filename", 9074 p->zDbFilename ? p->zDbFilename : ""); 9075 }else 9076 9077 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ 9078 if( nArg==2 ){ 9079 p->statsOn = (u8)booleanValue(azArg[1]); 9080 }else if( nArg==1 ){ 9081 display_stats(p->db, p, 0); 9082 }else{ 9083 raw_printf(stderr, "Usage: .stats ?on|off?\n"); 9084 rc = 1; 9085 } 9086 }else 9087 9088 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) 9089 || (c=='i' && (strncmp(azArg[0], "indices", n)==0 9090 || strncmp(azArg[0], "indexes", n)==0) ) 9091 ){ 9092 sqlite3_stmt *pStmt; 9093 char **azResult; 9094 int nRow, nAlloc; 9095 int ii; 9096 ShellText s; 9097 initText(&s); 9098 open_db(p, 0); 9099 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 9100 if( rc ){ 9101 sqlite3_finalize(pStmt); 9102 return shellDatabaseError(p->db); 9103 } 9104 9105 if( nArg>2 && c=='i' ){ 9106 /* It is an historical accident that the .indexes command shows an error 9107 ** when called with the wrong number of arguments whereas the .tables 9108 ** command does not. */ 9109 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); 9110 rc = 1; 9111 sqlite3_finalize(pStmt); 9112 goto meta_command_exit; 9113 } 9114 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ 9115 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); 9116 if( zDbName==0 ) continue; 9117 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); 9118 if( sqlite3_stricmp(zDbName, "main")==0 ){ 9119 appendText(&s, "SELECT name FROM ", 0); 9120 }else{ 9121 appendText(&s, "SELECT ", 0); 9122 appendText(&s, zDbName, '\''); 9123 appendText(&s, "||'.'||name FROM ", 0); 9124 } 9125 appendText(&s, zDbName, '"'); 9126 appendText(&s, ".sqlite_master ", 0); 9127 if( c=='t' ){ 9128 appendText(&s," WHERE type IN ('table','view')" 9129 " AND name NOT LIKE 'sqlite_%'" 9130 " AND name LIKE ?1", 0); 9131 }else{ 9132 appendText(&s," WHERE type='index'" 9133 " AND tbl_name LIKE ?1", 0); 9134 } 9135 } 9136 rc = sqlite3_finalize(pStmt); 9137 appendText(&s, " ORDER BY 1", 0); 9138 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); 9139 freeText(&s); 9140 if( rc ) return shellDatabaseError(p->db); 9141 9142 /* Run the SQL statement prepared by the above block. Store the results 9143 ** as an array of nul-terminated strings in azResult[]. */ 9144 nRow = nAlloc = 0; 9145 azResult = 0; 9146 if( nArg>1 ){ 9147 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); 9148 }else{ 9149 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); 9150 } 9151 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9152 if( nRow>=nAlloc ){ 9153 char **azNew; 9154 int n2 = nAlloc*2 + 10; 9155 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); 9156 if( azNew==0 ) shell_out_of_memory(); 9157 nAlloc = n2; 9158 azResult = azNew; 9159 } 9160 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 9161 if( 0==azResult[nRow] ) shell_out_of_memory(); 9162 nRow++; 9163 } 9164 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ 9165 rc = shellDatabaseError(p->db); 9166 } 9167 9168 /* Pretty-print the contents of array azResult[] to the output */ 9169 if( rc==0 && nRow>0 ){ 9170 int len, maxlen = 0; 9171 int i, j; 9172 int nPrintCol, nPrintRow; 9173 for(i=0; i<nRow; i++){ 9174 len = strlen30(azResult[i]); 9175 if( len>maxlen ) maxlen = len; 9176 } 9177 nPrintCol = 80/(maxlen+2); 9178 if( nPrintCol<1 ) nPrintCol = 1; 9179 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; 9180 for(i=0; i<nPrintRow; i++){ 9181 for(j=i; j<nRow; j+=nPrintRow){ 9182 char *zSp = j<nPrintRow ? "" : " "; 9183 utf8_printf(p->out, "%s%-*s", zSp, maxlen, 9184 azResult[j] ? azResult[j]:""); 9185 } 9186 raw_printf(p->out, "\n"); 9187 } 9188 } 9189 9190 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); 9191 sqlite3_free(azResult); 9192 }else 9193 9194 /* Begin redirecting output to the file "testcase-out.txt" */ 9195 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ 9196 output_reset(p); 9197 p->out = output_file_open("testcase-out.txt", 0); 9198 if( p->out==0 ){ 9199 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); 9200 } 9201 if( nArg>=2 ){ 9202 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); 9203 }else{ 9204 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); 9205 } 9206 }else 9207 9208#ifndef SQLITE_UNTESTABLE 9209 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ 9210 static const struct { 9211 const char *zCtrlName; /* Name of a test-control option */ 9212 int ctrlCode; /* Integer code for that option */ 9213 const char *zUsage; /* Usage notes */ 9214 } aCtrl[] = { 9215 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, 9216 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, 9217 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ 9218 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ 9219 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, 9220 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" }, 9221 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/ 9222 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, 9223 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" }, 9224 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, 9225 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, 9226 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, 9227#ifdef YYCOVERAGE 9228 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, 9229#endif 9230 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, 9231 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, 9232 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, 9233 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" }, 9234 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE"}, 9235 }; 9236 int testctrl = -1; 9237 int iCtrl = -1; 9238 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ 9239 int isOk = 0; 9240 int i, n2; 9241 const char *zCmd = 0; 9242 9243 open_db(p, 0); 9244 zCmd = nArg>=2 ? azArg[1] : "help"; 9245 9246 /* The argument can optionally begin with "-" or "--" */ 9247 if( zCmd[0]=='-' && zCmd[1] ){ 9248 zCmd++; 9249 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 9250 } 9251 9252 /* --help lists all test-controls */ 9253 if( strcmp(zCmd,"help")==0 ){ 9254 utf8_printf(p->out, "Available test-controls:\n"); 9255 for(i=0; i<ArraySize(aCtrl); i++){ 9256 utf8_printf(p->out, " .testctrl %s %s\n", 9257 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 9258 } 9259 rc = 1; 9260 goto meta_command_exit; 9261 } 9262 9263 /* convert testctrl text option to value. allow any unique prefix 9264 ** of the option name, or a numerical value. */ 9265 n2 = strlen30(zCmd); 9266 for(i=0; i<ArraySize(aCtrl); i++){ 9267 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 9268 if( testctrl<0 ){ 9269 testctrl = aCtrl[i].ctrlCode; 9270 iCtrl = i; 9271 }else{ 9272 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" 9273 "Use \".testctrl --help\" for help\n", zCmd); 9274 rc = 1; 9275 goto meta_command_exit; 9276 } 9277 } 9278 } 9279 if( testctrl<0 ){ 9280 utf8_printf(stderr,"Error: unknown test-control: %s\n" 9281 "Use \".testctrl --help\" for help\n", zCmd); 9282 }else{ 9283 switch(testctrl){ 9284 9285 /* sqlite3_test_control(int, db, int) */ 9286 case SQLITE_TESTCTRL_OPTIMIZATIONS: 9287 case SQLITE_TESTCTRL_RESERVE: 9288 if( nArg==3 ){ 9289 int opt = (int)strtol(azArg[2], 0, 0); 9290 rc2 = sqlite3_test_control(testctrl, p->db, opt); 9291 isOk = 3; 9292 } 9293 break; 9294 9295 /* sqlite3_test_control(int) */ 9296 case SQLITE_TESTCTRL_PRNG_SAVE: 9297 case SQLITE_TESTCTRL_PRNG_RESTORE: 9298 case SQLITE_TESTCTRL_PRNG_RESET: 9299 case SQLITE_TESTCTRL_BYTEORDER: 9300 if( nArg==2 ){ 9301 rc2 = sqlite3_test_control(testctrl); 9302 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; 9303 } 9304 break; 9305 9306 /* sqlite3_test_control(int, uint) */ 9307 case SQLITE_TESTCTRL_PENDING_BYTE: 9308 if( nArg==3 ){ 9309 unsigned int opt = (unsigned int)integerValue(azArg[2]); 9310 rc2 = sqlite3_test_control(testctrl, opt); 9311 isOk = 3; 9312 } 9313 break; 9314 9315 /* sqlite3_test_control(int, int, sqlite3*) */ 9316 case SQLITE_TESTCTRL_PRNG_SEED: 9317 if( nArg==3 || nArg==4 ){ 9318 int ii = (int)integerValue(azArg[2]); 9319 sqlite3 *db; 9320 if( ii==0 && strcmp(azArg[2],"random")==0 ){ 9321 sqlite3_randomness(sizeof(ii),&ii); 9322 printf("-- random seed: %d\n", ii); 9323 } 9324 if( nArg==3 ){ 9325 db = 0; 9326 }else{ 9327 db = p->db; 9328 /* Make sure the schema has been loaded */ 9329 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); 9330 } 9331 rc2 = sqlite3_test_control(testctrl, ii, db); 9332 isOk = 3; 9333 } 9334 break; 9335 9336 /* sqlite3_test_control(int, int) */ 9337 case SQLITE_TESTCTRL_ASSERT: 9338 case SQLITE_TESTCTRL_ALWAYS: 9339 if( nArg==3 ){ 9340 int opt = booleanValue(azArg[2]); 9341 rc2 = sqlite3_test_control(testctrl, opt); 9342 isOk = 1; 9343 } 9344 break; 9345 9346 /* sqlite3_test_control(int, int) */ 9347 case SQLITE_TESTCTRL_LOCALTIME_FAULT: 9348 case SQLITE_TESTCTRL_NEVER_CORRUPT: 9349 if( nArg==3 ){ 9350 int opt = booleanValue(azArg[2]); 9351 rc2 = sqlite3_test_control(testctrl, opt); 9352 isOk = 3; 9353 } 9354 break; 9355 9356 /* sqlite3_test_control(sqlite3*) */ 9357 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: 9358 rc2 = sqlite3_test_control(testctrl, p->db); 9359 isOk = 3; 9360 break; 9361 9362 case SQLITE_TESTCTRL_IMPOSTER: 9363 if( nArg==5 ){ 9364 rc2 = sqlite3_test_control(testctrl, p->db, 9365 azArg[2], 9366 integerValue(azArg[3]), 9367 integerValue(azArg[4])); 9368 isOk = 3; 9369 } 9370 break; 9371 9372#ifdef YYCOVERAGE 9373 case SQLITE_TESTCTRL_PARSER_COVERAGE: 9374 if( nArg==2 ){ 9375 sqlite3_test_control(testctrl, p->out); 9376 isOk = 3; 9377 } 9378#endif 9379 } 9380 } 9381 if( isOk==0 && iCtrl>=0 ){ 9382 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 9383 rc = 1; 9384 }else if( isOk==1 ){ 9385 raw_printf(p->out, "%d\n", rc2); 9386 }else if( isOk==2 ){ 9387 raw_printf(p->out, "0x%08x\n", rc2); 9388 } 9389 }else 9390#endif /* !defined(SQLITE_UNTESTABLE) */ 9391 9392 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ 9393 open_db(p, 0); 9394 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); 9395 }else 9396 9397 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ 9398 if( nArg==2 ){ 9399 enableTimer = booleanValue(azArg[1]); 9400 if( enableTimer && !HAS_TIMER ){ 9401 raw_printf(stderr, "Error: timer not available on this system.\n"); 9402 enableTimer = 0; 9403 } 9404 }else{ 9405 raw_printf(stderr, "Usage: .timer on|off\n"); 9406 rc = 1; 9407 } 9408 }else 9409 9410#ifndef SQLITE_OMIT_TRACE 9411 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ 9412 int mType = 0; 9413 int jj; 9414 open_db(p, 0); 9415 for(jj=1; jj<nArg; jj++){ 9416 const char *z = azArg[jj]; 9417 if( z[0]=='-' ){ 9418 if( optionMatch(z, "expanded") ){ 9419 p->eTraceType = SHELL_TRACE_EXPANDED; 9420 } 9421#ifdef SQLITE_ENABLE_NORMALIZE 9422 else if( optionMatch(z, "normalized") ){ 9423 p->eTraceType = SHELL_TRACE_NORMALIZED; 9424 } 9425#endif 9426 else if( optionMatch(z, "plain") ){ 9427 p->eTraceType = SHELL_TRACE_PLAIN; 9428 } 9429 else if( optionMatch(z, "profile") ){ 9430 mType |= SQLITE_TRACE_PROFILE; 9431 } 9432 else if( optionMatch(z, "row") ){ 9433 mType |= SQLITE_TRACE_ROW; 9434 } 9435 else if( optionMatch(z, "stmt") ){ 9436 mType |= SQLITE_TRACE_STMT; 9437 } 9438 else if( optionMatch(z, "close") ){ 9439 mType |= SQLITE_TRACE_CLOSE; 9440 } 9441 else { 9442 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); 9443 rc = 1; 9444 goto meta_command_exit; 9445 } 9446 }else{ 9447 output_file_close(p->traceOut); 9448 p->traceOut = output_file_open(azArg[1], 0); 9449 } 9450 } 9451 if( p->traceOut==0 ){ 9452 sqlite3_trace_v2(p->db, 0, 0, 0); 9453 }else{ 9454 if( mType==0 ) mType = SQLITE_TRACE_STMT; 9455 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); 9456 } 9457 }else 9458#endif /* !defined(SQLITE_OMIT_TRACE) */ 9459 9460#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) 9461 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ 9462 int ii; 9463 int lenOpt; 9464 char *zOpt; 9465 if( nArg<2 ){ 9466 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); 9467 rc = 1; 9468 goto meta_command_exit; 9469 } 9470 open_db(p, 0); 9471 zOpt = azArg[1]; 9472 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; 9473 lenOpt = (int)strlen(zOpt); 9474 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ 9475 assert( azArg[nArg]==0 ); 9476 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); 9477 }else{ 9478 for(ii=1; ii<nArg; ii++){ 9479 sqlite3_create_module(p->db, azArg[ii], 0, 0); 9480 } 9481 } 9482 }else 9483#endif 9484 9485#if SQLITE_USER_AUTHENTICATION 9486 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ 9487 if( nArg<2 ){ 9488 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); 9489 rc = 1; 9490 goto meta_command_exit; 9491 } 9492 open_db(p, 0); 9493 if( strcmp(azArg[1],"login")==0 ){ 9494 if( nArg!=4 ){ 9495 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); 9496 rc = 1; 9497 goto meta_command_exit; 9498 } 9499 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], 9500 strlen30(azArg[3])); 9501 if( rc ){ 9502 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); 9503 rc = 1; 9504 } 9505 }else if( strcmp(azArg[1],"add")==0 ){ 9506 if( nArg!=5 ){ 9507 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); 9508 rc = 1; 9509 goto meta_command_exit; 9510 } 9511 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 9512 booleanValue(azArg[4])); 9513 if( rc ){ 9514 raw_printf(stderr, "User-Add failed: %d\n", rc); 9515 rc = 1; 9516 } 9517 }else if( strcmp(azArg[1],"edit")==0 ){ 9518 if( nArg!=5 ){ 9519 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); 9520 rc = 1; 9521 goto meta_command_exit; 9522 } 9523 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 9524 booleanValue(azArg[4])); 9525 if( rc ){ 9526 raw_printf(stderr, "User-Edit failed: %d\n", rc); 9527 rc = 1; 9528 } 9529 }else if( strcmp(azArg[1],"delete")==0 ){ 9530 if( nArg!=3 ){ 9531 raw_printf(stderr, "Usage: .user delete USER\n"); 9532 rc = 1; 9533 goto meta_command_exit; 9534 } 9535 rc = sqlite3_user_delete(p->db, azArg[2]); 9536 if( rc ){ 9537 raw_printf(stderr, "User-Delete failed: %d\n", rc); 9538 rc = 1; 9539 } 9540 }else{ 9541 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); 9542 rc = 1; 9543 goto meta_command_exit; 9544 } 9545 }else 9546#endif /* SQLITE_USER_AUTHENTICATION */ 9547 9548 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ 9549 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, 9550 sqlite3_libversion(), sqlite3_sourceid()); 9551#if SQLITE_HAVE_ZLIB 9552 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); 9553#endif 9554#define CTIMEOPT_VAL_(opt) #opt 9555#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) 9556#if defined(__clang__) && defined(__clang_major__) 9557 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." 9558 CTIMEOPT_VAL(__clang_minor__) "." 9559 CTIMEOPT_VAL(__clang_patchlevel__) "\n"); 9560#elif defined(_MSC_VER) 9561 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); 9562#elif defined(__GNUC__) && defined(__VERSION__) 9563 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); 9564#endif 9565 }else 9566 9567 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ 9568 const char *zDbName = nArg==2 ? azArg[1] : "main"; 9569 sqlite3_vfs *pVfs = 0; 9570 if( p->db ){ 9571 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); 9572 if( pVfs ){ 9573 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); 9574 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 9575 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 9576 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 9577 } 9578 } 9579 }else 9580 9581 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ 9582 sqlite3_vfs *pVfs; 9583 sqlite3_vfs *pCurrent = 0; 9584 if( p->db ){ 9585 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); 9586 } 9587 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 9588 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, 9589 pVfs==pCurrent ? " <--- CURRENT" : ""); 9590 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 9591 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 9592 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 9593 if( pVfs->pNext ){ 9594 raw_printf(p->out, "-----------------------------------\n"); 9595 } 9596 } 9597 }else 9598 9599 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ 9600 const char *zDbName = nArg==2 ? azArg[1] : "main"; 9601 char *zVfsName = 0; 9602 if( p->db ){ 9603 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); 9604 if( zVfsName ){ 9605 utf8_printf(p->out, "%s\n", zVfsName); 9606 sqlite3_free(zVfsName); 9607 } 9608 } 9609 }else 9610 9611#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 9612 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ 9613 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; 9614 }else 9615#endif 9616 9617 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ 9618 int j; 9619 assert( nArg<=ArraySize(azArg) ); 9620 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ 9621 p->colWidth[j-1] = (int)integerValue(azArg[j]); 9622 } 9623 }else 9624 9625 { 9626 utf8_printf(stderr, "Error: unknown command or invalid arguments: " 9627 " \"%s\". Enter \".help\" for help\n", azArg[0]); 9628 rc = 1; 9629 } 9630 9631meta_command_exit: 9632 if( p->outCount ){ 9633 p->outCount--; 9634 if( p->outCount==0 ) output_reset(p); 9635 } 9636 return rc; 9637} 9638 9639/* 9640** Return TRUE if a semicolon occurs anywhere in the first N characters 9641** of string z[]. 9642*/ 9643static int line_contains_semicolon(const char *z, int N){ 9644 int i; 9645 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } 9646 return 0; 9647} 9648 9649/* 9650** Test to see if a line consists entirely of whitespace. 9651*/ 9652static int _all_whitespace(const char *z){ 9653 for(; *z; z++){ 9654 if( IsSpace(z[0]) ) continue; 9655 if( *z=='/' && z[1]=='*' ){ 9656 z += 2; 9657 while( *z && (*z!='*' || z[1]!='/') ){ z++; } 9658 if( *z==0 ) return 0; 9659 z++; 9660 continue; 9661 } 9662 if( *z=='-' && z[1]=='-' ){ 9663 z += 2; 9664 while( *z && *z!='\n' ){ z++; } 9665 if( *z==0 ) return 1; 9666 continue; 9667 } 9668 return 0; 9669 } 9670 return 1; 9671} 9672 9673/* 9674** Return TRUE if the line typed in is an SQL command terminator other 9675** than a semi-colon. The SQL Server style "go" command is understood 9676** as is the Oracle "/". 9677*/ 9678static int line_is_command_terminator(const char *zLine){ 9679 while( IsSpace(zLine[0]) ){ zLine++; }; 9680 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ 9681 return 1; /* Oracle */ 9682 } 9683 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' 9684 && _all_whitespace(&zLine[2]) ){ 9685 return 1; /* SQL Server */ 9686 } 9687 return 0; 9688} 9689 9690/* 9691** We need a default sqlite3_complete() implementation to use in case 9692** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes 9693** any arbitrary text is a complete SQL statement. This is not very 9694** user-friendly, but it does seem to work. 9695*/ 9696#ifdef SQLITE_OMIT_COMPLETE 9697#define sqlite3_complete(x) 1 9698#endif 9699 9700/* 9701** Return true if zSql is a complete SQL statement. Return false if it 9702** ends in the middle of a string literal or C-style comment. 9703*/ 9704static int line_is_complete(char *zSql, int nSql){ 9705 int rc; 9706 if( zSql==0 ) return 1; 9707 zSql[nSql] = ';'; 9708 zSql[nSql+1] = 0; 9709 rc = sqlite3_complete(zSql); 9710 zSql[nSql] = 0; 9711 return rc; 9712} 9713 9714/* 9715** Run a single line of SQL. Return the number of errors. 9716*/ 9717static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ 9718 int rc; 9719 char *zErrMsg = 0; 9720 9721 open_db(p, 0); 9722 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); 9723 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 9724 BEGIN_TIMER; 9725 rc = shell_exec(p, zSql, &zErrMsg); 9726 END_TIMER; 9727 if( rc || zErrMsg ){ 9728 char zPrefix[100]; 9729 if( in!=0 || !stdin_is_interactive ){ 9730 sqlite3_snprintf(sizeof(zPrefix), zPrefix, 9731 "Error: near line %d:", startline); 9732 }else{ 9733 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); 9734 } 9735 if( zErrMsg!=0 ){ 9736 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); 9737 sqlite3_free(zErrMsg); 9738 zErrMsg = 0; 9739 }else{ 9740 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); 9741 } 9742 return 1; 9743 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ 9744 raw_printf(p->out, "changes: %3d total_changes: %d\n", 9745 sqlite3_changes(p->db), sqlite3_total_changes(p->db)); 9746 } 9747 return 0; 9748} 9749 9750 9751/* 9752** Read input from *in and process it. If *in==0 then input 9753** is interactive - the user is typing it it. Otherwise, input 9754** is coming from a file or device. A prompt is issued and history 9755** is saved only if input is interactive. An interrupt signal will 9756** cause this routine to exit immediately, unless input is interactive. 9757** 9758** Return the number of errors. 9759*/ 9760static int process_input(ShellState *p){ 9761 char *zLine = 0; /* A single input line */ 9762 char *zSql = 0; /* Accumulated SQL text */ 9763 int nLine; /* Length of current line */ 9764 int nSql = 0; /* Bytes of zSql[] used */ 9765 int nAlloc = 0; /* Allocated zSql[] space */ 9766 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ 9767 int rc; /* Error code */ 9768 int errCnt = 0; /* Number of errors seen */ 9769 int startline = 0; /* Line number for start of current input */ 9770 9771 p->lineno = 0; 9772 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ 9773 fflush(p->out); 9774 zLine = one_input_line(p->in, zLine, nSql>0); 9775 if( zLine==0 ){ 9776 /* End of input */ 9777 if( p->in==0 && stdin_is_interactive ) printf("\n"); 9778 break; 9779 } 9780 if( seenInterrupt ){ 9781 if( p->in!=0 ) break; 9782 seenInterrupt = 0; 9783 } 9784 p->lineno++; 9785 if( nSql==0 && _all_whitespace(zLine) ){ 9786 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 9787 continue; 9788 } 9789 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ 9790 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); 9791 if( zLine[0]=='.' ){ 9792 rc = do_meta_command(zLine, p); 9793 if( rc==2 ){ /* exit requested */ 9794 break; 9795 }else if( rc ){ 9796 errCnt++; 9797 } 9798 } 9799 continue; 9800 } 9801 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ 9802 memcpy(zLine,";",2); 9803 } 9804 nLine = strlen30(zLine); 9805 if( nSql+nLine+2>=nAlloc ){ 9806 nAlloc = nSql+nLine+100; 9807 zSql = realloc(zSql, nAlloc); 9808 if( zSql==0 ) shell_out_of_memory(); 9809 } 9810 nSqlPrior = nSql; 9811 if( nSql==0 ){ 9812 int i; 9813 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} 9814 assert( nAlloc>0 && zSql!=0 ); 9815 memcpy(zSql, zLine+i, nLine+1-i); 9816 startline = p->lineno; 9817 nSql = nLine-i; 9818 }else{ 9819 zSql[nSql++] = '\n'; 9820 memcpy(zSql+nSql, zLine, nLine+1); 9821 nSql += nLine; 9822 } 9823 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) 9824 && sqlite3_complete(zSql) ){ 9825 errCnt += runOneSqlLine(p, zSql, p->in, startline); 9826 nSql = 0; 9827 if( p->outCount ){ 9828 output_reset(p); 9829 p->outCount = 0; 9830 }else{ 9831 clearTempFile(p); 9832 } 9833 }else if( nSql && _all_whitespace(zSql) ){ 9834 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); 9835 nSql = 0; 9836 } 9837 } 9838 if( nSql && !_all_whitespace(zSql) ){ 9839 errCnt += runOneSqlLine(p, zSql, p->in, startline); 9840 } 9841 free(zSql); 9842 free(zLine); 9843 return errCnt>0; 9844} 9845 9846/* 9847** Return a pathname which is the user's home directory. A 9848** 0 return indicates an error of some kind. 9849*/ 9850static char *find_home_dir(int clearFlag){ 9851 static char *home_dir = NULL; 9852 if( clearFlag ){ 9853 free(home_dir); 9854 home_dir = 0; 9855 return 0; 9856 } 9857 if( home_dir ) return home_dir; 9858 9859#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 9860 && !defined(__RTP__) && !defined(_WRS_KERNEL) 9861 { 9862 struct passwd *pwent; 9863 uid_t uid = getuid(); 9864 if( (pwent=getpwuid(uid)) != NULL) { 9865 home_dir = pwent->pw_dir; 9866 } 9867 } 9868#endif 9869 9870#if defined(_WIN32_WCE) 9871 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() 9872 */ 9873 home_dir = "/"; 9874#else 9875 9876#if defined(_WIN32) || defined(WIN32) 9877 if (!home_dir) { 9878 home_dir = getenv("USERPROFILE"); 9879 } 9880#endif 9881 9882 if (!home_dir) { 9883 home_dir = getenv("HOME"); 9884 } 9885 9886#if defined(_WIN32) || defined(WIN32) 9887 if (!home_dir) { 9888 char *zDrive, *zPath; 9889 int n; 9890 zDrive = getenv("HOMEDRIVE"); 9891 zPath = getenv("HOMEPATH"); 9892 if( zDrive && zPath ){ 9893 n = strlen30(zDrive) + strlen30(zPath) + 1; 9894 home_dir = malloc( n ); 9895 if( home_dir==0 ) return 0; 9896 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); 9897 return home_dir; 9898 } 9899 home_dir = "c:\\"; 9900 } 9901#endif 9902 9903#endif /* !_WIN32_WCE */ 9904 9905 if( home_dir ){ 9906 int n = strlen30(home_dir) + 1; 9907 char *z = malloc( n ); 9908 if( z ) memcpy(z, home_dir, n); 9909 home_dir = z; 9910 } 9911 9912 return home_dir; 9913} 9914 9915/* 9916** Read input from the file given by sqliterc_override. Or if that 9917** parameter is NULL, take input from ~/.sqliterc 9918** 9919** Returns the number of errors. 9920*/ 9921static void process_sqliterc( 9922 ShellState *p, /* Configuration data */ 9923 const char *sqliterc_override /* Name of config file. NULL to use default */ 9924){ 9925 char *home_dir = NULL; 9926 const char *sqliterc = sqliterc_override; 9927 char *zBuf = 0; 9928 FILE *inSaved = p->in; 9929 int savedLineno = p->lineno; 9930 9931 if (sqliterc == NULL) { 9932 home_dir = find_home_dir(0); 9933 if( home_dir==0 ){ 9934 raw_printf(stderr, "-- warning: cannot find home directory;" 9935 " cannot read ~/.sqliterc\n"); 9936 return; 9937 } 9938 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); 9939 sqliterc = zBuf; 9940 } 9941 p->in = fopen(sqliterc,"rb"); 9942 if( p->in ){ 9943 if( stdin_is_interactive ){ 9944 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); 9945 } 9946 process_input(p); 9947 fclose(p->in); 9948 } 9949 p->in = inSaved; 9950 p->lineno = savedLineno; 9951 sqlite3_free(zBuf); 9952} 9953 9954/* 9955** Show available command line options 9956*/ 9957static const char zOptions[] = 9958#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 9959 " -A ARGS... run \".archive ARGS\" and exit\n" 9960#endif 9961 " -append append the database to the end of the file\n" 9962 " -ascii set output mode to 'ascii'\n" 9963 " -bail stop after hitting an error\n" 9964 " -batch force batch I/O\n" 9965 " -column set output mode to 'column'\n" 9966 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 9967 " -csv set output mode to 'csv'\n" 9968#if defined(SQLITE_ENABLE_DESERIALIZE) 9969 " -deserialize open the database using sqlite3_deserialize()\n" 9970#endif 9971 " -echo print commands before execution\n" 9972 " -init FILENAME read/process named file\n" 9973 " -[no]header turn headers on or off\n" 9974#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 9975 " -heap SIZE Size of heap for memsys3 or memsys5\n" 9976#endif 9977 " -help show this message\n" 9978 " -html set output mode to HTML\n" 9979 " -interactive force interactive I/O\n" 9980 " -line set output mode to 'line'\n" 9981 " -list set output mode to 'list'\n" 9982 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 9983#if defined(SQLITE_ENABLE_DESERIALIZE) 9984 " -maxsize N maximum size for a --deserialize database\n" 9985#endif 9986 " -memtrace trace all memory allocations and deallocations\n" 9987 " -mmap N default mmap size set to N\n" 9988#ifdef SQLITE_ENABLE_MULTIPLEX 9989 " -multiplex enable the multiplexor VFS\n" 9990#endif 9991 " -newline SEP set output row separator. Default: '\\n'\n" 9992 " -nofollow refuse to open symbolic links to database files\n" 9993 " -nullvalue TEXT set text string for NULL values. Default ''\n" 9994 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 9995 " -quote set output mode to 'quote'\n" 9996 " -readonly open the database read-only\n" 9997 " -separator SEP set output column separator. Default: '|'\n" 9998#ifdef SQLITE_ENABLE_SORTER_REFERENCES 9999 " -sorterref SIZE sorter references threshold size\n" 10000#endif 10001 " -stats print memory stats before each finalize\n" 10002 " -version show SQLite version\n" 10003 " -vfs NAME use NAME as the default VFS\n" 10004#ifdef SQLITE_ENABLE_VFSTRACE 10005 " -vfstrace enable tracing of all VFS calls\n" 10006#endif 10007#ifdef SQLITE_HAVE_ZLIB 10008 " -zip open the file as a ZIP Archive\n" 10009#endif 10010; 10011static void usage(int showDetail){ 10012 utf8_printf(stderr, 10013 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 10014 "FILENAME is the name of an SQLite database. A new database is created\n" 10015 "if the file does not previously exist.\n", Argv0); 10016 if( showDetail ){ 10017 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); 10018 }else{ 10019 raw_printf(stderr, "Use the -help option for additional information\n"); 10020 } 10021 exit(1); 10022} 10023 10024/* 10025** Internal check: Verify that the SQLite is uninitialized. Print a 10026** error message if it is initialized. 10027*/ 10028static void verify_uninitialized(void){ 10029 if( sqlite3_config(-1)==SQLITE_MISUSE ){ 10030 utf8_printf(stdout, "WARNING: attempt to configure SQLite after" 10031 " initialization.\n"); 10032 } 10033} 10034 10035/* 10036** Initialize the state information in data 10037*/ 10038static void main_init(ShellState *data) { 10039 memset(data, 0, sizeof(*data)); 10040 data->normalMode = data->cMode = data->mode = MODE_List; 10041 data->autoExplain = 1; 10042 memcpy(data->colSeparator,SEP_Column, 2); 10043 memcpy(data->rowSeparator,SEP_Row, 2); 10044 data->showHeader = 0; 10045 data->shellFlgs = SHFLG_Lookaside; 10046 verify_uninitialized(); 10047 sqlite3_config(SQLITE_CONFIG_URI, 1); 10048 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); 10049 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 10050 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); 10051 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); 10052} 10053 10054/* 10055** Output text to the console in a font that attracts extra attention. 10056*/ 10057#ifdef _WIN32 10058static void printBold(const char *zText){ 10059 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 10060 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; 10061 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); 10062 SetConsoleTextAttribute(out, 10063 FOREGROUND_RED|FOREGROUND_INTENSITY 10064 ); 10065 printf("%s", zText); 10066 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); 10067} 10068#else 10069static void printBold(const char *zText){ 10070 printf("\033[1m%s\033[0m", zText); 10071} 10072#endif 10073 10074/* 10075** Get the argument to an --option. Throw an error and die if no argument 10076** is available. 10077*/ 10078static char *cmdline_option_value(int argc, char **argv, int i){ 10079 if( i==argc ){ 10080 utf8_printf(stderr, "%s: Error: missing argument to %s\n", 10081 argv[0], argv[argc-1]); 10082 exit(1); 10083 } 10084 return argv[i]; 10085} 10086 10087#ifndef SQLITE_SHELL_IS_UTF8 10088# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) 10089# define SQLITE_SHELL_IS_UTF8 (0) 10090# else 10091# define SQLITE_SHELL_IS_UTF8 (1) 10092# endif 10093#endif 10094 10095#if SQLITE_SHELL_IS_UTF8 10096int SQLITE_CDECL main(int argc, char **argv){ 10097#else 10098int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ 10099 char **argv; 10100#endif 10101 char *zErrMsg = 0; 10102 ShellState data; 10103 const char *zInitFile = 0; 10104 int i; 10105 int rc = 0; 10106 int warnInmemoryDb = 0; 10107 int readStdin = 1; 10108 int nCmd = 0; 10109 char **azCmd = 0; 10110 const char *zVfs = 0; /* Value of -vfs command-line option */ 10111#if !SQLITE_SHELL_IS_UTF8 10112 char **argvToFree = 0; 10113 int argcToFree = 0; 10114#endif 10115 10116 setBinaryMode(stdin, 0); 10117 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ 10118 stdin_is_interactive = isatty(0); 10119 stdout_is_console = isatty(1); 10120 10121#if !defined(_WIN32_WCE) 10122 if( getenv("SQLITE_DEBUG_BREAK") ){ 10123 if( isatty(0) && isatty(2) ){ 10124 fprintf(stderr, 10125 "attach debugger to process %d and press any key to continue.\n", 10126 GETPID()); 10127 fgetc(stdin); 10128 }else{ 10129#if defined(_WIN32) || defined(WIN32) 10130 DebugBreak(); 10131#elif defined(SIGTRAP) 10132 raise(SIGTRAP); 10133#endif 10134 } 10135 } 10136#endif 10137 10138#if USE_SYSTEM_SQLITE+0!=1 10139 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ 10140 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", 10141 sqlite3_sourceid(), SQLITE_SOURCE_ID); 10142 exit(1); 10143 } 10144#endif 10145 main_init(&data); 10146 10147 /* On Windows, we must translate command-line arguments into UTF-8. 10148 ** The SQLite memory allocator subsystem has to be enabled in order to 10149 ** do this. But we want to run an sqlite3_shutdown() afterwards so that 10150 ** subsequent sqlite3_config() calls will work. So copy all results into 10151 ** memory that does not come from the SQLite memory allocator. 10152 */ 10153#if !SQLITE_SHELL_IS_UTF8 10154 sqlite3_initialize(); 10155 argvToFree = malloc(sizeof(argv[0])*argc*2); 10156 argcToFree = argc; 10157 argv = argvToFree + argc; 10158 if( argv==0 ) shell_out_of_memory(); 10159 for(i=0; i<argc; i++){ 10160 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); 10161 int n; 10162 if( z==0 ) shell_out_of_memory(); 10163 n = (int)strlen(z); 10164 argv[i] = malloc( n+1 ); 10165 if( argv[i]==0 ) shell_out_of_memory(); 10166 memcpy(argv[i], z, n+1); 10167 argvToFree[i] = argv[i]; 10168 sqlite3_free(z); 10169 } 10170 sqlite3_shutdown(); 10171#endif 10172 10173 assert( argc>=1 && argv && argv[0] ); 10174 Argv0 = argv[0]; 10175 10176 /* Make sure we have a valid signal handler early, before anything 10177 ** else is done. 10178 */ 10179#ifdef SIGINT 10180 signal(SIGINT, interrupt_handler); 10181#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 10182 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); 10183#endif 10184 10185#ifdef SQLITE_SHELL_DBNAME_PROC 10186 { 10187 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name 10188 ** of a C-function that will provide the name of the database file. Use 10189 ** this compile-time option to embed this shell program in larger 10190 ** applications. */ 10191 extern void SQLITE_SHELL_DBNAME_PROC(const char**); 10192 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); 10193 warnInmemoryDb = 0; 10194 } 10195#endif 10196 10197 /* Do an initial pass through the command-line argument to locate 10198 ** the name of the database file, the name of the initialization file, 10199 ** the size of the alternative malloc heap, 10200 ** and the first command to execute. 10201 */ 10202 verify_uninitialized(); 10203 for(i=1; i<argc; i++){ 10204 char *z; 10205 z = argv[i]; 10206 if( z[0]!='-' ){ 10207 if( data.zDbFilename==0 ){ 10208 data.zDbFilename = z; 10209 }else{ 10210 /* Excesss arguments are interpreted as SQL (or dot-commands) and 10211 ** mean that nothing is read from stdin */ 10212 readStdin = 0; 10213 nCmd++; 10214 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); 10215 if( azCmd==0 ) shell_out_of_memory(); 10216 azCmd[nCmd-1] = z; 10217 } 10218 } 10219 if( z[1]=='-' ) z++; 10220 if( strcmp(z,"-separator")==0 10221 || strcmp(z,"-nullvalue")==0 10222 || strcmp(z,"-newline")==0 10223 || strcmp(z,"-cmd")==0 10224 ){ 10225 (void)cmdline_option_value(argc, argv, ++i); 10226 }else if( strcmp(z,"-init")==0 ){ 10227 zInitFile = cmdline_option_value(argc, argv, ++i); 10228 }else if( strcmp(z,"-batch")==0 ){ 10229 /* Need to check for batch mode here to so we can avoid printing 10230 ** informational messages (like from process_sqliterc) before 10231 ** we do the actual processing of arguments later in a second pass. 10232 */ 10233 stdin_is_interactive = 0; 10234 }else if( strcmp(z,"-heap")==0 ){ 10235#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 10236 const char *zSize; 10237 sqlite3_int64 szHeap; 10238 10239 zSize = cmdline_option_value(argc, argv, ++i); 10240 szHeap = integerValue(zSize); 10241 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; 10242 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); 10243#else 10244 (void)cmdline_option_value(argc, argv, ++i); 10245#endif 10246 }else if( strcmp(z,"-pagecache")==0 ){ 10247 int n, sz; 10248 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10249 if( sz>70000 ) sz = 70000; 10250 if( sz<0 ) sz = 0; 10251 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10252 sqlite3_config(SQLITE_CONFIG_PAGECACHE, 10253 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); 10254 data.shellFlgs |= SHFLG_Pagecache; 10255 }else if( strcmp(z,"-lookaside")==0 ){ 10256 int n, sz; 10257 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10258 if( sz<0 ) sz = 0; 10259 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 10260 if( n<0 ) n = 0; 10261 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); 10262 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; 10263#ifdef SQLITE_ENABLE_VFSTRACE 10264 }else if( strcmp(z,"-vfstrace")==0 ){ 10265 extern int vfstrace_register( 10266 const char *zTraceName, 10267 const char *zOldVfsName, 10268 int (*xOut)(const char*,void*), 10269 void *pOutArg, 10270 int makeDefault 10271 ); 10272 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); 10273#endif 10274#ifdef SQLITE_ENABLE_MULTIPLEX 10275 }else if( strcmp(z,"-multiplex")==0 ){ 10276 extern int sqlite3_multiple_initialize(const char*,int); 10277 sqlite3_multiplex_initialize(0, 1); 10278#endif 10279 }else if( strcmp(z,"-mmap")==0 ){ 10280 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10281 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); 10282#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10283 }else if( strcmp(z,"-sorterref")==0 ){ 10284 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 10285 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); 10286#endif 10287 }else if( strcmp(z,"-vfs")==0 ){ 10288 zVfs = cmdline_option_value(argc, argv, ++i); 10289#ifdef SQLITE_HAVE_ZLIB 10290 }else if( strcmp(z,"-zip")==0 ){ 10291 data.openMode = SHELL_OPEN_ZIPFILE; 10292#endif 10293 }else if( strcmp(z,"-append")==0 ){ 10294 data.openMode = SHELL_OPEN_APPENDVFS; 10295#ifdef SQLITE_ENABLE_DESERIALIZE 10296 }else if( strcmp(z,"-deserialize")==0 ){ 10297 data.openMode = SHELL_OPEN_DESERIALIZE; 10298 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 10299 data.szMax = integerValue(argv[++i]); 10300#endif 10301 }else if( strcmp(z,"-readonly")==0 ){ 10302 data.openMode = SHELL_OPEN_READONLY; 10303 }else if( strcmp(z,"-nofollow")==0 ){ 10304 data.openFlags = SQLITE_OPEN_NOFOLLOW; 10305#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 10306 }else if( strncmp(z, "-A",2)==0 ){ 10307 /* All remaining command-line arguments are passed to the ".archive" 10308 ** command, so ignore them */ 10309 break; 10310#endif 10311 }else if( strcmp(z, "-memtrace")==0 ){ 10312 sqlite3MemTraceActivate(stderr); 10313 } 10314 } 10315 verify_uninitialized(); 10316 10317 10318#ifdef SQLITE_SHELL_INIT_PROC 10319 { 10320 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name 10321 ** of a C-function that will perform initialization actions on SQLite that 10322 ** occur just before or after sqlite3_initialize(). Use this compile-time 10323 ** option to embed this shell program in larger applications. */ 10324 extern void SQLITE_SHELL_INIT_PROC(void); 10325 SQLITE_SHELL_INIT_PROC(); 10326 } 10327#else 10328 /* All the sqlite3_config() calls have now been made. So it is safe 10329 ** to call sqlite3_initialize() and process any command line -vfs option. */ 10330 sqlite3_initialize(); 10331#endif 10332 10333 if( zVfs ){ 10334 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); 10335 if( pVfs ){ 10336 sqlite3_vfs_register(pVfs, 1); 10337 }else{ 10338 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); 10339 exit(1); 10340 } 10341 } 10342 10343 if( data.zDbFilename==0 ){ 10344#ifndef SQLITE_OMIT_MEMORYDB 10345 data.zDbFilename = ":memory:"; 10346 warnInmemoryDb = argc==1; 10347#else 10348 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); 10349 return 1; 10350#endif 10351 } 10352 data.out = stdout; 10353 sqlite3_appendvfs_init(0,0,0); 10354 10355 /* Go ahead and open the database file if it already exists. If the 10356 ** file does not exist, delay opening it. This prevents empty database 10357 ** files from being created if a user mistypes the database name argument 10358 ** to the sqlite command-line tool. 10359 */ 10360 if( access(data.zDbFilename, 0)==0 ){ 10361 open_db(&data, 0); 10362 } 10363 10364 /* Process the initialization file if there is one. If no -init option 10365 ** is given on the command line, look for a file named ~/.sqliterc and 10366 ** try to process it. 10367 */ 10368 process_sqliterc(&data,zInitFile); 10369 10370 /* Make a second pass through the command-line argument and set 10371 ** options. This second pass is delayed until after the initialization 10372 ** file is processed so that the command-line arguments will override 10373 ** settings in the initialization file. 10374 */ 10375 for(i=1; i<argc; i++){ 10376 char *z = argv[i]; 10377 if( z[0]!='-' ) continue; 10378 if( z[1]=='-' ){ z++; } 10379 if( strcmp(z,"-init")==0 ){ 10380 i++; 10381 }else if( strcmp(z,"-html")==0 ){ 10382 data.mode = MODE_Html; 10383 }else if( strcmp(z,"-list")==0 ){ 10384 data.mode = MODE_List; 10385 }else if( strcmp(z,"-quote")==0 ){ 10386 data.mode = MODE_Quote; 10387 }else if( strcmp(z,"-line")==0 ){ 10388 data.mode = MODE_Line; 10389 }else if( strcmp(z,"-column")==0 ){ 10390 data.mode = MODE_Column; 10391 }else if( strcmp(z,"-csv")==0 ){ 10392 data.mode = MODE_Csv; 10393 memcpy(data.colSeparator,",",2); 10394#ifdef SQLITE_HAVE_ZLIB 10395 }else if( strcmp(z,"-zip")==0 ){ 10396 data.openMode = SHELL_OPEN_ZIPFILE; 10397#endif 10398 }else if( strcmp(z,"-append")==0 ){ 10399 data.openMode = SHELL_OPEN_APPENDVFS; 10400#ifdef SQLITE_ENABLE_DESERIALIZE 10401 }else if( strcmp(z,"-deserialize")==0 ){ 10402 data.openMode = SHELL_OPEN_DESERIALIZE; 10403 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ 10404 data.szMax = integerValue(argv[++i]); 10405#endif 10406 }else if( strcmp(z,"-readonly")==0 ){ 10407 data.openMode = SHELL_OPEN_READONLY; 10408 }else if( strcmp(z,"-nofollow")==0 ){ 10409 data.openFlags |= SQLITE_OPEN_NOFOLLOW; 10410 }else if( strcmp(z,"-ascii")==0 ){ 10411 data.mode = MODE_Ascii; 10412 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 10413 SEP_Unit); 10414 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 10415 SEP_Record); 10416 }else if( strcmp(z,"-separator")==0 ){ 10417 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 10418 "%s",cmdline_option_value(argc,argv,++i)); 10419 }else if( strcmp(z,"-newline")==0 ){ 10420 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 10421 "%s",cmdline_option_value(argc,argv,++i)); 10422 }else if( strcmp(z,"-nullvalue")==0 ){ 10423 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, 10424 "%s",cmdline_option_value(argc,argv,++i)); 10425 }else if( strcmp(z,"-header")==0 ){ 10426 data.showHeader = 1; 10427 }else if( strcmp(z,"-noheader")==0 ){ 10428 data.showHeader = 0; 10429 }else if( strcmp(z,"-echo")==0 ){ 10430 ShellSetFlag(&data, SHFLG_Echo); 10431 }else if( strcmp(z,"-eqp")==0 ){ 10432 data.autoEQP = AUTOEQP_on; 10433 }else if( strcmp(z,"-eqpfull")==0 ){ 10434 data.autoEQP = AUTOEQP_full; 10435 }else if( strcmp(z,"-stats")==0 ){ 10436 data.statsOn = 1; 10437 }else if( strcmp(z,"-scanstats")==0 ){ 10438 data.scanstatsOn = 1; 10439 }else if( strcmp(z,"-backslash")==0 ){ 10440 /* Undocumented command-line option: -backslash 10441 ** Causes C-style backslash escapes to be evaluated in SQL statements 10442 ** prior to sending the SQL into SQLite. Useful for injecting 10443 ** crazy bytes in the middle of SQL statements for testing and debugging. 10444 */ 10445 ShellSetFlag(&data, SHFLG_Backslash); 10446 }else if( strcmp(z,"-bail")==0 ){ 10447 bail_on_error = 1; 10448 }else if( strcmp(z,"-version")==0 ){ 10449 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 10450 return 0; 10451 }else if( strcmp(z,"-interactive")==0 ){ 10452 stdin_is_interactive = 1; 10453 }else if( strcmp(z,"-batch")==0 ){ 10454 stdin_is_interactive = 0; 10455 }else if( strcmp(z,"-heap")==0 ){ 10456 i++; 10457 }else if( strcmp(z,"-pagecache")==0 ){ 10458 i+=2; 10459 }else if( strcmp(z,"-lookaside")==0 ){ 10460 i+=2; 10461 }else if( strcmp(z,"-mmap")==0 ){ 10462 i++; 10463 }else if( strcmp(z,"-memtrace")==0 ){ 10464 i++; 10465#ifdef SQLITE_ENABLE_SORTER_REFERENCES 10466 }else if( strcmp(z,"-sorterref")==0 ){ 10467 i++; 10468#endif 10469 }else if( strcmp(z,"-vfs")==0 ){ 10470 i++; 10471#ifdef SQLITE_ENABLE_VFSTRACE 10472 }else if( strcmp(z,"-vfstrace")==0 ){ 10473 i++; 10474#endif 10475#ifdef SQLITE_ENABLE_MULTIPLEX 10476 }else if( strcmp(z,"-multiplex")==0 ){ 10477 i++; 10478#endif 10479 }else if( strcmp(z,"-help")==0 ){ 10480 usage(1); 10481 }else if( strcmp(z,"-cmd")==0 ){ 10482 /* Run commands that follow -cmd first and separately from commands 10483 ** that simply appear on the command-line. This seems goofy. It would 10484 ** be better if all commands ran in the order that they appear. But 10485 ** we retain the goofy behavior for historical compatibility. */ 10486 if( i==argc-1 ) break; 10487 z = cmdline_option_value(argc,argv,++i); 10488 if( z[0]=='.' ){ 10489 rc = do_meta_command(z, &data); 10490 if( rc && bail_on_error ) return rc==2 ? 0 : rc; 10491 }else{ 10492 open_db(&data, 0); 10493 rc = shell_exec(&data, z, &zErrMsg); 10494 if( zErrMsg!=0 ){ 10495 utf8_printf(stderr,"Error: %s\n", zErrMsg); 10496 if( bail_on_error ) return rc!=0 ? rc : 1; 10497 }else if( rc!=0 ){ 10498 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); 10499 if( bail_on_error ) return rc; 10500 } 10501 } 10502#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 10503 }else if( strncmp(z, "-A", 2)==0 ){ 10504 if( nCmd>0 ){ 10505 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" 10506 " with \"%s\"\n", z); 10507 return 1; 10508 } 10509 open_db(&data, OPEN_DB_ZIPFILE); 10510 if( z[2] ){ 10511 argv[i] = &z[2]; 10512 arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); 10513 }else{ 10514 arDotCommand(&data, 1, argv+i, argc-i); 10515 } 10516 readStdin = 0; 10517 break; 10518#endif 10519 }else{ 10520 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); 10521 raw_printf(stderr,"Use -help for a list of options.\n"); 10522 return 1; 10523 } 10524 data.cMode = data.mode; 10525 } 10526 10527 if( !readStdin ){ 10528 /* Run all arguments that do not begin with '-' as if they were separate 10529 ** command-line inputs, except for the argToSkip argument which contains 10530 ** the database filename. 10531 */ 10532 for(i=0; i<nCmd; i++){ 10533 if( azCmd[i][0]=='.' ){ 10534 rc = do_meta_command(azCmd[i], &data); 10535 if( rc ) return rc==2 ? 0 : rc; 10536 }else{ 10537 open_db(&data, 0); 10538 rc = shell_exec(&data, azCmd[i], &zErrMsg); 10539 if( zErrMsg!=0 ){ 10540 utf8_printf(stderr,"Error: %s\n", zErrMsg); 10541 return rc!=0 ? rc : 1; 10542 }else if( rc!=0 ){ 10543 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); 10544 return rc; 10545 } 10546 } 10547 } 10548 free(azCmd); 10549 }else{ 10550 /* Run commands received from standard input 10551 */ 10552 if( stdin_is_interactive ){ 10553 char *zHome; 10554 char *zHistory; 10555 int nHistory; 10556 printf( 10557 "SQLite version %s %.19s\n" /*extra-version-info*/ 10558 "Enter \".help\" for usage hints.\n", 10559 sqlite3_libversion(), sqlite3_sourceid() 10560 ); 10561 if( warnInmemoryDb ){ 10562 printf("Connected to a "); 10563 printBold("transient in-memory database"); 10564 printf(".\nUse \".open FILENAME\" to reopen on a " 10565 "persistent database.\n"); 10566 } 10567 zHistory = getenv("SQLITE_HISTORY"); 10568 if( zHistory ){ 10569 zHistory = strdup(zHistory); 10570 }else if( (zHome = find_home_dir(0))!=0 ){ 10571 nHistory = strlen30(zHome) + 20; 10572 if( (zHistory = malloc(nHistory))!=0 ){ 10573 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); 10574 } 10575 } 10576 if( zHistory ){ shell_read_history(zHistory); } 10577#if HAVE_READLINE || HAVE_EDITLINE 10578 rl_attempted_completion_function = readline_completion; 10579#elif HAVE_LINENOISE 10580 linenoiseSetCompletionCallback(linenoise_completion); 10581#endif 10582 data.in = 0; 10583 rc = process_input(&data); 10584 if( zHistory ){ 10585 shell_stifle_history(2000); 10586 shell_write_history(zHistory); 10587 free(zHistory); 10588 } 10589 }else{ 10590 data.in = stdin; 10591 rc = process_input(&data); 10592 } 10593 } 10594 set_table_name(&data, 0); 10595 if( data.db ){ 10596 session_close_all(&data); 10597 close_db(data.db); 10598 } 10599 sqlite3_free(data.zFreeOnClose); 10600 find_home_dir(1); 10601 output_reset(&data); 10602 data.doXdgOpen = 0; 10603 clearTempFile(&data); 10604#if !SQLITE_SHELL_IS_UTF8 10605 for(i=0; i<argcToFree; i++) free(argvToFree[i]); 10606 free(argvToFree); 10607#endif 10608 /* Clear the global data structure so that valgrind will detect memory 10609 ** leaks */ 10610 memset(&data, 0, sizeof(data)); 10611 return rc; 10612} 10613