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