1/* 2** 2001 September 15 3** 4** The author disclaims copyright to this source code. In place of 5** a legal notice, here is a blessing: 6** 7** May you do good and not evil. 8** May you find forgiveness for yourself and forgive others. 9** May you share freely, never taking more than you give. 10** 11************************************************************************* 12** This file contains code to implement the "sqlite" command line 13** utility for accessing SQLite databases. 14*/ 15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) 16/* This needs to come before any includes for MSVC compiler */ 17#define _CRT_SECURE_NO_WARNINGS 18#endif 19 20/* 21** Optionally #include a user-defined header, whereby compilation options 22** may be set prior to where they take effect, but after platform setup. 23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include 24** file. Note that this macro has a like effect on sqlite3.c compilation. 25*/ 26# define SHELL_STRINGIFY_(f) #f 27# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f) 28#ifdef SQLITE_CUSTOM_INCLUDE 29# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE) 30#endif 31 32/* 33** Determine if we are dealing with WinRT, which provides only a subset of 34** the full Win32 API. 35*/ 36#if !defined(SQLITE_OS_WINRT) 37# define SQLITE_OS_WINRT 0 38#endif 39 40/* 41** If SQLITE_SHELL_FIDDLE is defined then the shell is modified 42** somewhat for use as a WASM module in a web browser. This flag 43** should only be used when building the "fiddle" web application, as 44** the browser-mode build has much different user input requirements 45** and this build mode rewires the user input subsystem to account for 46** that. 47*/ 48 49/* 50** Warning pragmas copied from msvc.h in the core. 51*/ 52#if defined(_MSC_VER) 53#pragma warning(disable : 4054) 54#pragma warning(disable : 4055) 55#pragma warning(disable : 4100) 56#pragma warning(disable : 4127) 57#pragma warning(disable : 4130) 58#pragma warning(disable : 4152) 59#pragma warning(disable : 4189) 60#pragma warning(disable : 4206) 61#pragma warning(disable : 4210) 62#pragma warning(disable : 4232) 63#pragma warning(disable : 4244) 64#pragma warning(disable : 4305) 65#pragma warning(disable : 4306) 66#pragma warning(disable : 4702) 67#pragma warning(disable : 4706) 68#endif /* defined(_MSC_VER) */ 69 70/* 71** No support for loadable extensions in VxWorks. 72*/ 73#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION 74# define SQLITE_OMIT_LOAD_EXTENSION 1 75#endif 76 77/* 78** Enable large-file support for fopen() and friends on unix. 79*/ 80#ifndef SQLITE_DISABLE_LFS 81# define _LARGE_FILE 1 82# ifndef _FILE_OFFSET_BITS 83# define _FILE_OFFSET_BITS 64 84# endif 85# define _LARGEFILE_SOURCE 1 86#endif 87 88#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE) 89/* 90** emcc requires _POSIX_SOURCE (or one of several similar defines) 91** to expose strdup(). 92*/ 93# define _POSIX_SOURCE 94#endif 95 96#include <stdlib.h> 97#include <string.h> 98#include <stdio.h> 99#include <assert.h> 100#include "sqlite3.h" 101typedef sqlite3_int64 i64; 102typedef sqlite3_uint64 u64; 103typedef unsigned char u8; 104#if SQLITE_USER_AUTHENTICATION 105# include "sqlite3userauth.h" 106#endif 107#include <ctype.h> 108#include <stdarg.h> 109 110#if !defined(_WIN32) && !defined(WIN32) 111# include <signal.h> 112# if !defined(__RTP__) && !defined(_WRS_KERNEL) 113# include <pwd.h> 114# endif 115#endif 116#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) 117# include <unistd.h> 118# include <dirent.h> 119# define GETPID getpid 120# if defined(__MINGW32__) 121# define DIRENT dirent 122# ifndef S_ISLNK 123# define S_ISLNK(mode) (0) 124# endif 125# endif 126#else 127# define GETPID (int)GetCurrentProcessId 128#endif 129#include <sys/types.h> 130#include <sys/stat.h> 131 132#if HAVE_READLINE 133# include <readline/readline.h> 134# include <readline/history.h> 135#endif 136 137#if HAVE_EDITLINE 138# include <editline/readline.h> 139#endif 140 141#if HAVE_EDITLINE || HAVE_READLINE 142 143# define shell_add_history(X) add_history(X) 144# define shell_read_history(X) read_history(X) 145# define shell_write_history(X) write_history(X) 146# define shell_stifle_history(X) stifle_history(X) 147# define shell_readline(X) readline(X) 148 149#elif HAVE_LINENOISE 150 151# include "linenoise.h" 152# define shell_add_history(X) linenoiseHistoryAdd(X) 153# define shell_read_history(X) linenoiseHistoryLoad(X) 154# define shell_write_history(X) linenoiseHistorySave(X) 155# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) 156# define shell_readline(X) linenoise(X) 157 158#else 159 160# define shell_read_history(X) 161# define shell_write_history(X) 162# define shell_stifle_history(X) 163 164# define SHELL_USE_LOCAL_GETLINE 1 165#endif 166 167 168#if defined(_WIN32) || defined(WIN32) 169# if SQLITE_OS_WINRT 170# define SQLITE_OMIT_POPEN 1 171# else 172# include <io.h> 173# include <fcntl.h> 174# define isatty(h) _isatty(h) 175# ifndef access 176# define access(f,m) _access((f),(m)) 177# endif 178# ifndef unlink 179# define unlink _unlink 180# endif 181# ifndef strdup 182# define strdup _strdup 183# endif 184# undef popen 185# define popen _popen 186# undef pclose 187# define pclose _pclose 188# endif 189#else 190 /* Make sure isatty() has a prototype. */ 191 extern int isatty(int); 192 193# if !defined(__RTP__) && !defined(_WRS_KERNEL) 194 /* popen and pclose are not C89 functions and so are 195 ** sometimes omitted from the <stdio.h> header */ 196 extern FILE *popen(const char*,const char*); 197 extern int pclose(FILE*); 198# else 199# define SQLITE_OMIT_POPEN 1 200# endif 201#endif 202 203#if defined(_WIN32_WCE) 204/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() 205 * thus we always assume that we have a console. That can be 206 * overridden with the -batch command line option. 207 */ 208#define isatty(x) 1 209#endif 210 211/* ctype macros that work with signed characters */ 212#define IsSpace(X) isspace((unsigned char)X) 213#define IsDigit(X) isdigit((unsigned char)X) 214#define ToLower(X) (char)tolower((unsigned char)X) 215 216#if defined(_WIN32) || defined(WIN32) 217#if SQLITE_OS_WINRT 218#include <intrin.h> 219#endif 220#include <windows.h> 221 222/* string conversion routines only needed on Win32 */ 223extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); 224extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); 225extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); 226extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); 227#endif 228 229/* On Windows, we normally run with output mode of TEXT so that \n characters 230** are automatically translated into \r\n. However, this behavior needs 231** to be disabled in some cases (ex: when generating CSV output and when 232** rendering quoted strings that contain \n characters). The following 233** routines take care of that. 234*/ 235#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT 236static void setBinaryMode(FILE *file, int isOutput){ 237 if( isOutput ) fflush(file); 238 _setmode(_fileno(file), _O_BINARY); 239} 240static void setTextMode(FILE *file, int isOutput){ 241 if( isOutput ) fflush(file); 242 _setmode(_fileno(file), _O_TEXT); 243} 244#else 245# define setBinaryMode(X,Y) 246# define setTextMode(X,Y) 247#endif 248 249/* True if the timer is enabled */ 250static int enableTimer = 0; 251 252/* A version of strcmp() that works with NULL values */ 253static int cli_strcmp(const char *a, const char *b){ 254 if( a==0 ) a = ""; 255 if( b==0 ) b = ""; 256 return strcmp(a,b); 257} 258static int cli_strncmp(const char *a, const char *b, size_t n){ 259 if( a==0 ) a = ""; 260 if( b==0 ) b = ""; 261 return strncmp(a,b,n); 262} 263 264/* Return the current wall-clock time */ 265static sqlite3_int64 timeOfDay(void){ 266 static sqlite3_vfs *clockVfs = 0; 267 sqlite3_int64 t; 268 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 269 if( clockVfs==0 ) return 0; /* Never actually happens */ 270 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 271 clockVfs->xCurrentTimeInt64(clockVfs, &t); 272 }else{ 273 double r; 274 clockVfs->xCurrentTime(clockVfs, &r); 275 t = (sqlite3_int64)(r*86400000.0); 276 } 277 return t; 278} 279 280#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) 281#include <sys/time.h> 282#include <sys/resource.h> 283 284/* VxWorks does not support getrusage() as far as we can determine */ 285#if defined(_WRS_KERNEL) || defined(__RTP__) 286struct rusage { 287 struct timeval ru_utime; /* user CPU time used */ 288 struct timeval ru_stime; /* system CPU time used */ 289}; 290#define getrusage(A,B) memset(B,0,sizeof(*B)) 291#endif 292 293/* Saved resource information for the beginning of an operation */ 294static struct rusage sBegin; /* CPU time at start */ 295static sqlite3_int64 iBegin; /* Wall-clock time at start */ 296 297/* 298** Begin timing an operation 299*/ 300static void beginTimer(void){ 301 if( enableTimer ){ 302 getrusage(RUSAGE_SELF, &sBegin); 303 iBegin = timeOfDay(); 304 } 305} 306 307/* Return the difference of two time_structs in seconds */ 308static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ 309 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 310 (double)(pEnd->tv_sec - pStart->tv_sec); 311} 312 313/* 314** Print the timing results. 315*/ 316static void endTimer(void){ 317 if( enableTimer ){ 318 sqlite3_int64 iEnd = timeOfDay(); 319 struct rusage sEnd; 320 getrusage(RUSAGE_SELF, &sEnd); 321 printf("Run Time: real %.3f user %f sys %f\n", 322 (iEnd - iBegin)*0.001, 323 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), 324 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); 325 } 326} 327 328#define BEGIN_TIMER beginTimer() 329#define END_TIMER endTimer() 330#define HAS_TIMER 1 331 332#elif (defined(_WIN32) || defined(WIN32)) 333 334/* Saved resource information for the beginning of an operation */ 335static HANDLE hProcess; 336static FILETIME ftKernelBegin; 337static FILETIME ftUserBegin; 338static sqlite3_int64 ftWallBegin; 339typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, 340 LPFILETIME, LPFILETIME); 341static GETPROCTIMES getProcessTimesAddr = NULL; 342 343/* 344** Check to see if we have timer support. Return 1 if necessary 345** support found (or found previously). 346*/ 347static int hasTimer(void){ 348 if( getProcessTimesAddr ){ 349 return 1; 350 } else { 351#if !SQLITE_OS_WINRT 352 /* GetProcessTimes() isn't supported in WIN95 and some other Windows 353 ** versions. See if the version we are running on has it, and if it 354 ** does, save off a pointer to it and the current process handle. 355 */ 356 hProcess = GetCurrentProcess(); 357 if( hProcess ){ 358 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); 359 if( NULL != hinstLib ){ 360 getProcessTimesAddr = 361 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); 362 if( NULL != getProcessTimesAddr ){ 363 return 1; 364 } 365 FreeLibrary(hinstLib); 366 } 367 } 368#endif 369 } 370 return 0; 371} 372 373/* 374** Begin timing an operation 375*/ 376static void beginTimer(void){ 377 if( enableTimer && getProcessTimesAddr ){ 378 FILETIME ftCreation, ftExit; 379 getProcessTimesAddr(hProcess,&ftCreation,&ftExit, 380 &ftKernelBegin,&ftUserBegin); 381 ftWallBegin = timeOfDay(); 382 } 383} 384 385/* Return the difference of two FILETIME structs in seconds */ 386static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ 387 sqlite_int64 i64Start = *((sqlite_int64 *) pStart); 388 sqlite_int64 i64End = *((sqlite_int64 *) pEnd); 389 return (double) ((i64End - i64Start) / 10000000.0); 390} 391 392/* 393** Print the timing results. 394*/ 395static void endTimer(void){ 396 if( enableTimer && getProcessTimesAddr){ 397 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; 398 sqlite3_int64 ftWallEnd = timeOfDay(); 399 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); 400 printf("Run Time: real %.3f user %f sys %f\n", 401 (ftWallEnd - ftWallBegin)*0.001, 402 timeDiff(&ftUserBegin, &ftUserEnd), 403 timeDiff(&ftKernelBegin, &ftKernelEnd)); 404 } 405} 406 407#define BEGIN_TIMER beginTimer() 408#define END_TIMER endTimer() 409#define HAS_TIMER hasTimer() 410 411#else 412#define BEGIN_TIMER 413#define END_TIMER 414#define HAS_TIMER 0 415#endif 416 417/* 418** Used to prevent warnings about unused parameters 419*/ 420#define UNUSED_PARAMETER(x) (void)(x) 421 422/* 423** Number of elements in an array 424*/ 425#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) 426 427/* 428** If the following flag is set, then command execution stops 429** at an error if we are not interactive. 430*/ 431static int bail_on_error = 0; 432 433/* 434** Threat stdin as an interactive input if the following variable 435** is true. Otherwise, assume stdin is connected to a file or pipe. 436*/ 437static int stdin_is_interactive = 1; 438 439/* 440** On Windows systems we have to know if standard output is a console 441** in order to translate UTF-8 into MBCS. The following variable is 442** true if translation is required. 443*/ 444static int stdout_is_console = 1; 445 446/* 447** The following is the open SQLite database. We make a pointer 448** to this database a static variable so that it can be accessed 449** by the SIGINT handler to interrupt database processing. 450*/ 451static sqlite3 *globalDb = 0; 452 453/* 454** True if an interrupt (Control-C) has been received. 455*/ 456static volatile int seenInterrupt = 0; 457 458/* 459** This is the name of our program. It is set in main(), used 460** in a number of other places, mostly for error messages. 461*/ 462static char *Argv0; 463 464/* 465** Prompt strings. Initialized in main. Settable with 466** .prompt main continue 467*/ 468static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ 469static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ 470 471/* 472** Render output like fprintf(). Except, if the output is going to the 473** console and if this is running on a Windows machine, translate the 474** output from UTF-8 into MBCS. 475*/ 476#if defined(_WIN32) || defined(WIN32) 477void utf8_printf(FILE *out, const char *zFormat, ...){ 478 va_list ap; 479 va_start(ap, zFormat); 480 if( stdout_is_console && (out==stdout || out==stderr) ){ 481 char *z1 = sqlite3_vmprintf(zFormat, ap); 482 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); 483 sqlite3_free(z1); 484 fputs(z2, out); 485 sqlite3_free(z2); 486 }else{ 487 vfprintf(out, zFormat, ap); 488 } 489 va_end(ap); 490} 491#elif !defined(utf8_printf) 492# define utf8_printf fprintf 493#endif 494 495/* 496** Render output like fprintf(). This should not be used on anything that 497** includes string formatting (e.g. "%s"). 498*/ 499#if !defined(raw_printf) 500# define raw_printf fprintf 501#endif 502 503/* Indicate out-of-memory and exit. */ 504static void shell_out_of_memory(void){ 505 raw_printf(stderr,"Error: out of memory\n"); 506 exit(1); 507} 508 509/* Check a pointer to see if it is NULL. If it is NULL, exit with an 510** out-of-memory error. 511*/ 512static void shell_check_oom(void *p){ 513 if( p==0 ) shell_out_of_memory(); 514} 515 516/* 517** Write I/O traces to the following stream. 518*/ 519#ifdef SQLITE_ENABLE_IOTRACE 520static FILE *iotrace = 0; 521#endif 522 523/* 524** This routine works like printf in that its first argument is a 525** format string and subsequent arguments are values to be substituted 526** in place of % fields. The result of formatting this string 527** is written to iotrace. 528*/ 529#ifdef SQLITE_ENABLE_IOTRACE 530static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ 531 va_list ap; 532 char *z; 533 if( iotrace==0 ) return; 534 va_start(ap, zFormat); 535 z = sqlite3_vmprintf(zFormat, ap); 536 va_end(ap); 537 utf8_printf(iotrace, "%s", z); 538 sqlite3_free(z); 539} 540#endif 541 542/* 543** Output string zUtf to stream pOut as w characters. If w is negative, 544** then right-justify the text. W is the width in UTF-8 characters, not 545** in bytes. This is different from the %*.*s specification in printf 546** since with %*.*s the width is measured in bytes, not characters. 547*/ 548static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ 549 int i; 550 int n; 551 int aw = w<0 ? -w : w; 552 for(i=n=0; zUtf[i]; i++){ 553 if( (zUtf[i]&0xc0)!=0x80 ){ 554 n++; 555 if( n==aw ){ 556 do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); 557 break; 558 } 559 } 560 } 561 if( n>=aw ){ 562 utf8_printf(pOut, "%.*s", i, zUtf); 563 }else if( w<0 ){ 564 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); 565 }else{ 566 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); 567 } 568} 569 570 571/* 572** Determines if a string is a number of not. 573*/ 574static int isNumber(const char *z, int *realnum){ 575 if( *z=='-' || *z=='+' ) z++; 576 if( !IsDigit(*z) ){ 577 return 0; 578 } 579 z++; 580 if( realnum ) *realnum = 0; 581 while( IsDigit(*z) ){ z++; } 582 if( *z=='.' ){ 583 z++; 584 if( !IsDigit(*z) ) return 0; 585 while( IsDigit(*z) ){ z++; } 586 if( realnum ) *realnum = 1; 587 } 588 if( *z=='e' || *z=='E' ){ 589 z++; 590 if( *z=='+' || *z=='-' ) z++; 591 if( !IsDigit(*z) ) return 0; 592 while( IsDigit(*z) ){ z++; } 593 if( realnum ) *realnum = 1; 594 } 595 return *z==0; 596} 597 598/* 599** Compute a string length that is limited to what can be stored in 600** lower 30 bits of a 32-bit signed integer. 601*/ 602static int strlen30(const char *z){ 603 const char *z2 = z; 604 while( *z2 ){ z2++; } 605 return 0x3fffffff & (int)(z2 - z); 606} 607 608/* 609** Return the length of a string in characters. Multibyte UTF8 characters 610** count as a single character. 611*/ 612static int strlenChar(const char *z){ 613 int n = 0; 614 while( *z ){ 615 if( (0xc0&*(z++))!=0x80 ) n++; 616 } 617 return n; 618} 619 620/* 621** Return open FILE * if zFile exists, can be opened for read 622** and is an ordinary file or a character stream source. 623** Otherwise return 0. 624*/ 625static FILE * openChrSource(const char *zFile){ 626#ifdef _WIN32 627 struct _stat x = {0}; 628# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) 629 /* On Windows, open first, then check the stream nature. This order 630 ** is necessary because _stat() and sibs, when checking a named pipe, 631 ** effectively break the pipe as its supplier sees it. */ 632 FILE *rv = fopen(zFile, "rb"); 633 if( rv==0 ) return 0; 634 if( _fstat(_fileno(rv), &x) != 0 635 || !STAT_CHR_SRC(x.st_mode)){ 636 fclose(rv); 637 rv = 0; 638 } 639 return rv; 640#else 641 struct stat x = {0}; 642 int rc = stat(zFile, &x); 643# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) 644 if( rc!=0 ) return 0; 645 if( STAT_CHR_SRC(x.st_mode) ){ 646 return fopen(zFile, "rb"); 647 }else{ 648 return 0; 649 } 650#endif 651#undef STAT_CHR_SRC 652} 653 654/* 655** This routine reads a line of text from FILE in, stores 656** the text in memory obtained from malloc() and returns a pointer 657** to the text. NULL is returned at end of file, or if malloc() 658** fails. 659** 660** If zLine is not NULL then it is a malloced buffer returned from 661** a previous call to this routine that may be reused. 662*/ 663static char *local_getline(char *zLine, FILE *in){ 664 int nLine = zLine==0 ? 0 : 100; 665 int n = 0; 666 667 while( 1 ){ 668 if( n+100>nLine ){ 669 nLine = nLine*2 + 100; 670 zLine = realloc(zLine, nLine); 671 shell_check_oom(zLine); 672 } 673 if( fgets(&zLine[n], nLine - n, in)==0 ){ 674 if( n==0 ){ 675 free(zLine); 676 return 0; 677 } 678 zLine[n] = 0; 679 break; 680 } 681 while( zLine[n] ) n++; 682 if( n>0 && zLine[n-1]=='\n' ){ 683 n--; 684 if( n>0 && zLine[n-1]=='\r' ) n--; 685 zLine[n] = 0; 686 break; 687 } 688 } 689#if defined(_WIN32) || defined(WIN32) 690 /* For interactive input on Windows systems, translate the 691 ** multi-byte characterset characters into UTF-8. */ 692 if( stdin_is_interactive && in==stdin ){ 693 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); 694 if( zTrans ){ 695 i64 nTrans = strlen(zTrans)+1; 696 if( nTrans>nLine ){ 697 zLine = realloc(zLine, nTrans); 698 shell_check_oom(zLine); 699 } 700 memcpy(zLine, zTrans, nTrans); 701 sqlite3_free(zTrans); 702 } 703 } 704#endif /* defined(_WIN32) || defined(WIN32) */ 705 return zLine; 706} 707 708/* 709** Retrieve a single line of input text. 710** 711** If in==0 then read from standard input and prompt before each line. 712** If isContinuation is true, then a continuation prompt is appropriate. 713** If isContinuation is zero, then the main prompt should be used. 714** 715** If zPrior is not NULL then it is a buffer from a prior call to this 716** routine that can be reused. 717** 718** The result is stored in space obtained from malloc() and must either 719** be freed by the caller or else passed back into this routine via the 720** zPrior argument for reuse. 721*/ 722#ifndef SQLITE_SHELL_FIDDLE 723static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 724 char *zPrompt; 725 char *zResult; 726 if( in!=0 ){ 727 zResult = local_getline(zPrior, in); 728 }else{ 729 zPrompt = isContinuation ? continuePrompt : mainPrompt; 730#if SHELL_USE_LOCAL_GETLINE 731 printf("%s", zPrompt); 732 fflush(stdout); 733 zResult = local_getline(zPrior, stdin); 734#else 735 free(zPrior); 736 zResult = shell_readline(zPrompt); 737 if( zResult && *zResult ) shell_add_history(zResult); 738#endif 739 } 740 return zResult; 741} 742#endif /* !SQLITE_SHELL_FIDDLE */ 743 744/* 745** Return the value of a hexadecimal digit. Return -1 if the input 746** is not a hex digit. 747*/ 748static int hexDigitValue(char c){ 749 if( c>='0' && c<='9' ) return c - '0'; 750 if( c>='a' && c<='f' ) return c - 'a' + 10; 751 if( c>='A' && c<='F' ) return c - 'A' + 10; 752 return -1; 753} 754 755/* 756** Interpret zArg as an integer value, possibly with suffixes. 757*/ 758static sqlite3_int64 integerValue(const char *zArg){ 759 sqlite3_int64 v = 0; 760 static const struct { char *zSuffix; int iMult; } aMult[] = { 761 { "KiB", 1024 }, 762 { "MiB", 1024*1024 }, 763 { "GiB", 1024*1024*1024 }, 764 { "KB", 1000 }, 765 { "MB", 1000000 }, 766 { "GB", 1000000000 }, 767 { "K", 1000 }, 768 { "M", 1000000 }, 769 { "G", 1000000000 }, 770 }; 771 int i; 772 int isNeg = 0; 773 if( zArg[0]=='-' ){ 774 isNeg = 1; 775 zArg++; 776 }else if( zArg[0]=='+' ){ 777 zArg++; 778 } 779 if( zArg[0]=='0' && zArg[1]=='x' ){ 780 int x; 781 zArg += 2; 782 while( (x = hexDigitValue(zArg[0]))>=0 ){ 783 v = (v<<4) + x; 784 zArg++; 785 } 786 }else{ 787 while( IsDigit(zArg[0]) ){ 788 v = v*10 + zArg[0] - '0'; 789 zArg++; 790 } 791 } 792 for(i=0; i<ArraySize(aMult); i++){ 793 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 794 v *= aMult[i].iMult; 795 break; 796 } 797 } 798 return isNeg? -v : v; 799} 800 801/* 802** A variable length string to which one can append text. 803*/ 804typedef struct ShellText ShellText; 805struct ShellText { 806 char *z; 807 int n; 808 int nAlloc; 809}; 810 811/* 812** Initialize and destroy a ShellText object 813*/ 814static void initText(ShellText *p){ 815 memset(p, 0, sizeof(*p)); 816} 817static void freeText(ShellText *p){ 818 free(p->z); 819 initText(p); 820} 821 822/* zIn is either a pointer to a NULL-terminated string in memory obtained 823** from malloc(), or a NULL pointer. The string pointed to by zAppend is 824** added to zIn, and the result returned in memory obtained from malloc(). 825** zIn, if it was not NULL, is freed. 826** 827** If the third argument, quote, is not '\0', then it is used as a 828** quote character for zAppend. 829*/ 830static void appendText(ShellText *p, const char *zAppend, char quote){ 831 i64 len; 832 i64 i; 833 i64 nAppend = strlen30(zAppend); 834 835 len = nAppend+p->n+1; 836 if( quote ){ 837 len += 2; 838 for(i=0; i<nAppend; i++){ 839 if( zAppend[i]==quote ) len++; 840 } 841 } 842 843 if( p->z==0 || p->n+len>=p->nAlloc ){ 844 p->nAlloc = p->nAlloc*2 + len + 20; 845 p->z = realloc(p->z, p->nAlloc); 846 shell_check_oom(p->z); 847 } 848 849 if( quote ){ 850 char *zCsr = p->z+p->n; 851 *zCsr++ = quote; 852 for(i=0; i<nAppend; i++){ 853 *zCsr++ = zAppend[i]; 854 if( zAppend[i]==quote ) *zCsr++ = quote; 855 } 856 *zCsr++ = quote; 857 p->n = (int)(zCsr - p->z); 858 *zCsr = '\0'; 859 }else{ 860 memcpy(p->z+p->n, zAppend, nAppend); 861 p->n += nAppend; 862 p->z[p->n] = '\0'; 863 } 864} 865 866/* 867** Attempt to determine if identifier zName needs to be quoted, either 868** because it contains non-alphanumeric characters, or because it is an 869** SQLite keyword. Be conservative in this estimate: When in doubt assume 870** that quoting is required. 871** 872** Return '"' if quoting is required. Return 0 if no quoting is required. 873*/ 874static char quoteChar(const char *zName){ 875 int i; 876 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; 877 for(i=0; zName[i]; i++){ 878 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; 879 } 880 return sqlite3_keyword_check(zName, i) ? '"' : 0; 881} 882 883/* 884** Construct a fake object name and column list to describe the structure 885** of the view, virtual table, or table valued function zSchema.zName. 886*/ 887static char *shellFakeSchema( 888 sqlite3 *db, /* The database connection containing the vtab */ 889 const char *zSchema, /* Schema of the database holding the vtab */ 890 const char *zName /* The name of the virtual table */ 891){ 892 sqlite3_stmt *pStmt = 0; 893 char *zSql; 894 ShellText s; 895 char cQuote; 896 char *zDiv = "("; 897 int nRow = 0; 898 899 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", 900 zSchema ? zSchema : "main", zName); 901 shell_check_oom(zSql); 902 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 903 sqlite3_free(zSql); 904 initText(&s); 905 if( zSchema ){ 906 cQuote = quoteChar(zSchema); 907 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; 908 appendText(&s, zSchema, cQuote); 909 appendText(&s, ".", 0); 910 } 911 cQuote = quoteChar(zName); 912 appendText(&s, zName, cQuote); 913 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 914 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); 915 nRow++; 916 appendText(&s, zDiv, 0); 917 zDiv = ","; 918 if( zCol==0 ) zCol = ""; 919 cQuote = quoteChar(zCol); 920 appendText(&s, zCol, cQuote); 921 } 922 appendText(&s, ")", 0); 923 sqlite3_finalize(pStmt); 924 if( nRow==0 ){ 925 freeText(&s); 926 s.z = 0; 927 } 928 return s.z; 929} 930 931/* 932** SQL function: shell_module_schema(X) 933** 934** Return a fake schema for the table-valued function or eponymous virtual 935** table X. 936*/ 937static void shellModuleSchema( 938 sqlite3_context *pCtx, 939 int nVal, 940 sqlite3_value **apVal 941){ 942 const char *zName; 943 char *zFake; 944 UNUSED_PARAMETER(nVal); 945 zName = (const char*)sqlite3_value_text(apVal[0]); 946 zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0; 947 if( zFake ){ 948 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), 949 -1, sqlite3_free); 950 free(zFake); 951 } 952} 953 954/* 955** SQL function: shell_add_schema(S,X) 956** 957** Add the schema name X to the CREATE statement in S and return the result. 958** Examples: 959** 960** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); 961** 962** Also works on 963** 964** CREATE INDEX 965** CREATE UNIQUE INDEX 966** CREATE VIEW 967** CREATE TRIGGER 968** CREATE VIRTUAL TABLE 969** 970** This UDF is used by the .schema command to insert the schema name of 971** attached databases into the middle of the sqlite_schema.sql field. 972*/ 973static void shellAddSchemaName( 974 sqlite3_context *pCtx, 975 int nVal, 976 sqlite3_value **apVal 977){ 978 static const char *aPrefix[] = { 979 "TABLE", 980 "INDEX", 981 "UNIQUE INDEX", 982 "VIEW", 983 "TRIGGER", 984 "VIRTUAL TABLE" 985 }; 986 int i = 0; 987 const char *zIn = (const char*)sqlite3_value_text(apVal[0]); 988 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); 989 const char *zName = (const char*)sqlite3_value_text(apVal[2]); 990 sqlite3 *db = sqlite3_context_db_handle(pCtx); 991 UNUSED_PARAMETER(nVal); 992 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){ 993 for(i=0; i<ArraySize(aPrefix); i++){ 994 int n = strlen30(aPrefix[i]); 995 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ 996 char *z = 0; 997 char *zFake = 0; 998 if( zSchema ){ 999 char cQuote = quoteChar(zSchema); 1000 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ 1001 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); 1002 }else{ 1003 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); 1004 } 1005 } 1006 if( zName 1007 && aPrefix[i][0]=='V' 1008 && (zFake = shellFakeSchema(db, zSchema, zName))!=0 1009 ){ 1010 if( z==0 ){ 1011 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); 1012 }else{ 1013 z = sqlite3_mprintf("%z\n/* %s */", z, zFake); 1014 } 1015 free(zFake); 1016 } 1017 if( z ){ 1018 sqlite3_result_text(pCtx, z, -1, sqlite3_free); 1019 return; 1020 } 1021 } 1022 } 1023 } 1024 sqlite3_result_value(pCtx, apVal[0]); 1025} 1026 1027/* 1028** The source code for several run-time loadable extensions is inserted 1029** below by the ../tool/mkshellc.tcl script. Before processing that included 1030** code, we need to override some macros to make the included program code 1031** work here in the middle of this regular program. 1032*/ 1033#define SQLITE_EXTENSION_INIT1 1034#define SQLITE_EXTENSION_INIT2(X) (void)(X) 1035 1036#if defined(_WIN32) && defined(_MSC_VER) 1037INCLUDE test_windirent.h 1038INCLUDE test_windirent.c 1039#define dirent DIRENT 1040#endif 1041INCLUDE ../ext/misc/memtrace.c 1042INCLUDE ../ext/misc/shathree.c 1043INCLUDE ../ext/misc/uint.c 1044INCLUDE ../ext/misc/decimal.c 1045INCLUDE ../ext/misc/ieee754.c 1046INCLUDE ../ext/misc/series.c 1047INCLUDE ../ext/misc/regexp.c 1048#ifndef SQLITE_SHELL_FIDDLE 1049INCLUDE ../ext/misc/fileio.c 1050INCLUDE ../ext/misc/completion.c 1051INCLUDE ../ext/misc/appendvfs.c 1052#endif 1053#ifdef SQLITE_HAVE_ZLIB 1054INCLUDE ../ext/misc/zipfile.c 1055INCLUDE ../ext/misc/sqlar.c 1056#endif 1057INCLUDE ../ext/expert/sqlite3expert.h 1058INCLUDE ../ext/expert/sqlite3expert.c 1059 1060#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 1061#define SQLITE_SHELL_HAVE_RECOVER 1 1062#else 1063#define SQLITE_SHELL_HAVE_RECOVER 0 1064#endif 1065#if SQLITE_SHELL_HAVE_RECOVER 1066INCLUDE ../ext/recover/dbdata.c 1067INCLUDE ../ext/recover/sqlite3recover.h 1068INCLUDE ../ext/recover/sqlite3recover.c 1069#endif 1070 1071#if defined(SQLITE_ENABLE_SESSION) 1072/* 1073** State information for a single open session 1074*/ 1075typedef struct OpenSession OpenSession; 1076struct OpenSession { 1077 char *zName; /* Symbolic name for this session */ 1078 int nFilter; /* Number of xFilter rejection GLOB patterns */ 1079 char **azFilter; /* Array of xFilter rejection GLOB patterns */ 1080 sqlite3_session *p; /* The open session */ 1081}; 1082#endif 1083 1084typedef struct ExpertInfo ExpertInfo; 1085struct ExpertInfo { 1086 sqlite3expert *pExpert; 1087 int bVerbose; 1088}; 1089 1090/* A single line in the EQP output */ 1091typedef struct EQPGraphRow EQPGraphRow; 1092struct EQPGraphRow { 1093 int iEqpId; /* ID for this row */ 1094 int iParentId; /* ID of the parent row */ 1095 EQPGraphRow *pNext; /* Next row in sequence */ 1096 char zText[1]; /* Text to display for this row */ 1097}; 1098 1099/* All EQP output is collected into an instance of the following */ 1100typedef struct EQPGraph EQPGraph; 1101struct EQPGraph { 1102 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ 1103 EQPGraphRow *pLast; /* Last element of the pRow list */ 1104 char zPrefix[100]; /* Graph prefix */ 1105}; 1106 1107/* Parameters affecting columnar mode result display (defaulting together) */ 1108typedef struct ColModeOpts { 1109 int iWrap; /* In columnar modes, wrap lines reaching this limit */ 1110 u8 bQuote; /* Quote results for .mode box and table */ 1111 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */ 1112} ColModeOpts; 1113#define ColModeOpts_default { 60, 0, 0 } 1114#define ColModeOpts_default_qbox { 60, 1, 0 } 1115 1116/* 1117** State information about the database connection is contained in an 1118** instance of the following structure. 1119*/ 1120typedef struct ShellState ShellState; 1121struct ShellState { 1122 sqlite3 *db; /* The database */ 1123 u8 autoExplain; /* Automatically turn on .explain mode */ 1124 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ 1125 u8 autoEQPtest; /* autoEQP is in test mode */ 1126 u8 autoEQPtrace; /* autoEQP is in trace mode */ 1127 u8 scanstatsOn; /* True to display scan stats before each finalize */ 1128 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ 1129 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ 1130 u8 nEqpLevel; /* Depth of the EQP output graph */ 1131 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ 1132 u8 bSafeMode; /* True to prohibit unsafe operations */ 1133 u8 bSafeModePersist; /* The long-term value of bSafeMode */ 1134 ColModeOpts cmOpts; /* Option values affecting columnar mode output */ 1135 unsigned statsOn; /* True to display memory stats before each finalize */ 1136 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ 1137 int inputNesting; /* Track nesting level of .read and other redirects */ 1138 int outCount; /* Revert to stdout when reaching zero */ 1139 int cnt; /* Number of records displayed so far */ 1140 int lineno; /* Line number of last line read from in */ 1141 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ 1142 FILE *in; /* Read commands from this stream */ 1143 FILE *out; /* Write results here */ 1144 FILE *traceOut; /* Output for sqlite3_trace() */ 1145 int nErr; /* Number of errors seen */ 1146 int mode; /* An output mode setting */ 1147 int modePrior; /* Saved mode */ 1148 int cMode; /* temporary output mode for the current query */ 1149 int normalMode; /* Output mode before ".explain on" */ 1150 int writableSchema; /* True if PRAGMA writable_schema=ON */ 1151 int showHeader; /* True to show column names in List or Column mode */ 1152 int nCheck; /* Number of ".check" commands run */ 1153 unsigned nProgress; /* Number of progress callbacks encountered */ 1154 unsigned mxProgress; /* Maximum progress callbacks before failing */ 1155 unsigned flgProgress; /* Flags for the progress callback */ 1156 unsigned shellFlgs; /* Various flags */ 1157 unsigned priorShFlgs; /* Saved copy of flags */ 1158 sqlite3_int64 szMax; /* --maxsize argument to .open */ 1159 char *zDestTable; /* Name of destination table when MODE_Insert */ 1160 char *zTempFile; /* Temporary file that might need deleting */ 1161 char zTestcase[30]; /* Name of current test case */ 1162 char colSeparator[20]; /* Column separator character for several modes */ 1163 char rowSeparator[20]; /* Row separator character for MODE_Ascii */ 1164 char colSepPrior[20]; /* Saved column separator */ 1165 char rowSepPrior[20]; /* Saved row separator */ 1166 int *colWidth; /* Requested width of each column in columnar modes */ 1167 int *actualWidth; /* Actual width of each column */ 1168 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ 1169 char nullValue[20]; /* The text to print when a NULL comes back from 1170 ** the database */ 1171 char outfile[FILENAME_MAX]; /* Filename for *out */ 1172 sqlite3_stmt *pStmt; /* Current statement if any. */ 1173 FILE *pLog; /* Write log output here */ 1174 struct AuxDb { /* Storage space for auxiliary database connections */ 1175 sqlite3 *db; /* Connection pointer */ 1176 const char *zDbFilename; /* Filename used to open the connection */ 1177 char *zFreeOnClose; /* Free this memory allocation on close */ 1178#if defined(SQLITE_ENABLE_SESSION) 1179 int nSession; /* Number of active sessions */ 1180 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ 1181#endif 1182 } aAuxDb[5], /* Array of all database connections */ 1183 *pAuxDb; /* Currently active database connection */ 1184 int *aiIndent; /* Array of indents used in MODE_Explain */ 1185 int nIndent; /* Size of array aiIndent[] */ 1186 int iIndent; /* Index of current op in aiIndent[] */ 1187 char *zNonce; /* Nonce for temporary safe-mode excapes */ 1188 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ 1189 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ 1190#ifdef SQLITE_SHELL_FIDDLE 1191 struct { 1192 const char * zInput; /* Input string from wasm/JS proxy */ 1193 const char * zPos; /* Cursor pos into zInput */ 1194 const char * zDefaultDbName; /* Default name for db file */ 1195 } wasm; 1196#endif 1197}; 1198 1199#ifdef SQLITE_SHELL_FIDDLE 1200static ShellState shellState; 1201#endif 1202 1203 1204/* Allowed values for ShellState.autoEQP 1205*/ 1206#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ 1207#define AUTOEQP_on 1 /* Automatic EQP is on */ 1208#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ 1209#define AUTOEQP_full 3 /* Show full EXPLAIN */ 1210 1211/* Allowed values for ShellState.openMode 1212*/ 1213#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ 1214#define SHELL_OPEN_NORMAL 1 /* Normal database file */ 1215#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ 1216#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ 1217#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ 1218#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ 1219#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ 1220 1221/* Allowed values for ShellState.eTraceType 1222*/ 1223#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ 1224#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ 1225#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ 1226 1227/* Bits in the ShellState.flgProgress variable */ 1228#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ 1229#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres 1230 ** callback limit is reached, and for each 1231 ** top-level SQL statement */ 1232#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ 1233 1234/* 1235** These are the allowed shellFlgs values 1236*/ 1237#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ 1238#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ 1239#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ 1240#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ 1241#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ 1242#define SHFLG_CountChanges 0x00000020 /* .changes setting */ 1243#define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */ 1244#define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */ 1245#define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ 1246#define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ 1247 1248/* 1249** Macros for testing and setting shellFlgs 1250*/ 1251#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) 1252#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) 1253#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) 1254 1255/* 1256** These are the allowed modes. 1257*/ 1258#define MODE_Line 0 /* One column per line. Blank line between records */ 1259#define MODE_Column 1 /* One record per line in neat columns */ 1260#define MODE_List 2 /* One record per line with a separator */ 1261#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ 1262#define MODE_Html 4 /* Generate an XHTML table */ 1263#define MODE_Insert 5 /* Generate SQL "insert" statements */ 1264#define MODE_Quote 6 /* Quote values as for SQL */ 1265#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ 1266#define MODE_Csv 8 /* Quote strings, numbers are plain */ 1267#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ 1268#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ 1269#define MODE_Pretty 11 /* Pretty-print schemas */ 1270#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ 1271#define MODE_Json 13 /* Output JSON */ 1272#define MODE_Markdown 14 /* Markdown formatting */ 1273#define MODE_Table 15 /* MySQL-style table formatting */ 1274#define MODE_Box 16 /* Unicode box-drawing characters */ 1275#define MODE_Count 17 /* Output only a count of the rows of output */ 1276#define MODE_Off 18 /* No query output shown */ 1277 1278static const char *modeDescr[] = { 1279 "line", 1280 "column", 1281 "list", 1282 "semi", 1283 "html", 1284 "insert", 1285 "quote", 1286 "tcl", 1287 "csv", 1288 "explain", 1289 "ascii", 1290 "prettyprint", 1291 "eqp", 1292 "json", 1293 "markdown", 1294 "table", 1295 "box", 1296 "count", 1297 "off" 1298}; 1299 1300/* 1301** These are the column/row/line separators used by the various 1302** import/export modes. 1303*/ 1304#define SEP_Column "|" 1305#define SEP_Row "\n" 1306#define SEP_Tab "\t" 1307#define SEP_Space " " 1308#define SEP_Comma "," 1309#define SEP_CrLf "\r\n" 1310#define SEP_Unit "\x1F" 1311#define SEP_Record "\x1E" 1312 1313/* 1314** Limit input nesting via .read or any other input redirect. 1315** It's not too expensive, so a generous allowance can be made. 1316*/ 1317#define MAX_INPUT_NESTING 25 1318 1319/* 1320** A callback for the sqlite3_log() interface. 1321*/ 1322static void shellLog(void *pArg, int iErrCode, const char *zMsg){ 1323 ShellState *p = (ShellState*)pArg; 1324 if( p->pLog==0 ) return; 1325 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); 1326 fflush(p->pLog); 1327} 1328 1329/* 1330** SQL function: shell_putsnl(X) 1331** 1332** Write the text X to the screen (or whatever output is being directed) 1333** adding a newline at the end, and then return X. 1334*/ 1335static void shellPutsFunc( 1336 sqlite3_context *pCtx, 1337 int nVal, 1338 sqlite3_value **apVal 1339){ 1340 ShellState *p = (ShellState*)sqlite3_user_data(pCtx); 1341 (void)nVal; 1342 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); 1343 sqlite3_result_value(pCtx, apVal[0]); 1344} 1345 1346/* 1347** If in safe mode, print an error message described by the arguments 1348** and exit immediately. 1349*/ 1350static void failIfSafeMode( 1351 ShellState *p, 1352 const char *zErrMsg, 1353 ... 1354){ 1355 if( p->bSafeMode ){ 1356 va_list ap; 1357 char *zMsg; 1358 va_start(ap, zErrMsg); 1359 zMsg = sqlite3_vmprintf(zErrMsg, ap); 1360 va_end(ap); 1361 raw_printf(stderr, "line %d: ", p->lineno); 1362 utf8_printf(stderr, "%s\n", zMsg); 1363 exit(1); 1364 } 1365} 1366 1367/* 1368** SQL function: edit(VALUE) 1369** edit(VALUE,EDITOR) 1370** 1371** These steps: 1372** 1373** (1) Write VALUE into a temporary file. 1374** (2) Run program EDITOR on that temporary file. 1375** (3) Read the temporary file back and return its content as the result. 1376** (4) Delete the temporary file 1377** 1378** If the EDITOR argument is omitted, use the value in the VISUAL 1379** environment variable. If still there is no EDITOR, through an error. 1380** 1381** Also throw an error if the EDITOR program returns a non-zero exit code. 1382*/ 1383#ifndef SQLITE_NOHAVE_SYSTEM 1384static void editFunc( 1385 sqlite3_context *context, 1386 int argc, 1387 sqlite3_value **argv 1388){ 1389 const char *zEditor; 1390 char *zTempFile = 0; 1391 sqlite3 *db; 1392 char *zCmd = 0; 1393 int bBin; 1394 int rc; 1395 int hasCRNL = 0; 1396 FILE *f = 0; 1397 sqlite3_int64 sz; 1398 sqlite3_int64 x; 1399 unsigned char *p = 0; 1400 1401 if( argc==2 ){ 1402 zEditor = (const char*)sqlite3_value_text(argv[1]); 1403 }else{ 1404 zEditor = getenv("VISUAL"); 1405 } 1406 if( zEditor==0 ){ 1407 sqlite3_result_error(context, "no editor for edit()", -1); 1408 return; 1409 } 1410 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1411 sqlite3_result_error(context, "NULL input to edit()", -1); 1412 return; 1413 } 1414 db = sqlite3_context_db_handle(context); 1415 zTempFile = 0; 1416 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); 1417 if( zTempFile==0 ){ 1418 sqlite3_uint64 r = 0; 1419 sqlite3_randomness(sizeof(r), &r); 1420 zTempFile = sqlite3_mprintf("temp%llx", r); 1421 if( zTempFile==0 ){ 1422 sqlite3_result_error_nomem(context); 1423 return; 1424 } 1425 } 1426 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; 1427 /* When writing the file to be edited, do \n to \r\n conversions on systems 1428 ** that want \r\n line endings */ 1429 f = fopen(zTempFile, bBin ? "wb" : "w"); 1430 if( f==0 ){ 1431 sqlite3_result_error(context, "edit() cannot open temp file", -1); 1432 goto edit_func_end; 1433 } 1434 sz = sqlite3_value_bytes(argv[0]); 1435 if( bBin ){ 1436 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); 1437 }else{ 1438 const char *z = (const char*)sqlite3_value_text(argv[0]); 1439 /* Remember whether or not the value originally contained \r\n */ 1440 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; 1441 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); 1442 } 1443 fclose(f); 1444 f = 0; 1445 if( x!=sz ){ 1446 sqlite3_result_error(context, "edit() could not write the whole file", -1); 1447 goto edit_func_end; 1448 } 1449 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); 1450 if( zCmd==0 ){ 1451 sqlite3_result_error_nomem(context); 1452 goto edit_func_end; 1453 } 1454 rc = system(zCmd); 1455 sqlite3_free(zCmd); 1456 if( rc ){ 1457 sqlite3_result_error(context, "EDITOR returned non-zero", -1); 1458 goto edit_func_end; 1459 } 1460 f = fopen(zTempFile, "rb"); 1461 if( f==0 ){ 1462 sqlite3_result_error(context, 1463 "edit() cannot reopen temp file after edit", -1); 1464 goto edit_func_end; 1465 } 1466 fseek(f, 0, SEEK_END); 1467 sz = ftell(f); 1468 rewind(f); 1469 p = sqlite3_malloc64( sz+1 ); 1470 if( p==0 ){ 1471 sqlite3_result_error_nomem(context); 1472 goto edit_func_end; 1473 } 1474 x = fread(p, 1, (size_t)sz, f); 1475 fclose(f); 1476 f = 0; 1477 if( x!=sz ){ 1478 sqlite3_result_error(context, "could not read back the whole file", -1); 1479 goto edit_func_end; 1480 } 1481 if( bBin ){ 1482 sqlite3_result_blob64(context, p, sz, sqlite3_free); 1483 }else{ 1484 sqlite3_int64 i, j; 1485 if( hasCRNL ){ 1486 /* If the original contains \r\n then do no conversions back to \n */ 1487 }else{ 1488 /* If the file did not originally contain \r\n then convert any new 1489 ** \r\n back into \n */ 1490 for(i=j=0; i<sz; i++){ 1491 if( p[i]=='\r' && p[i+1]=='\n' ) i++; 1492 p[j++] = p[i]; 1493 } 1494 sz = j; 1495 p[sz] = 0; 1496 } 1497 sqlite3_result_text64(context, (const char*)p, sz, 1498 sqlite3_free, SQLITE_UTF8); 1499 } 1500 p = 0; 1501 1502edit_func_end: 1503 if( f ) fclose(f); 1504 unlink(zTempFile); 1505 sqlite3_free(zTempFile); 1506 sqlite3_free(p); 1507} 1508#endif /* SQLITE_NOHAVE_SYSTEM */ 1509 1510/* 1511** Save or restore the current output mode 1512*/ 1513static void outputModePush(ShellState *p){ 1514 p->modePrior = p->mode; 1515 p->priorShFlgs = p->shellFlgs; 1516 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); 1517 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); 1518} 1519static void outputModePop(ShellState *p){ 1520 p->mode = p->modePrior; 1521 p->shellFlgs = p->priorShFlgs; 1522 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); 1523 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); 1524} 1525 1526/* 1527** Output the given string as a hex-encoded blob (eg. X'1234' ) 1528*/ 1529static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ 1530 int i; 1531 unsigned char *aBlob = (unsigned char*)pBlob; 1532 1533 char *zStr = sqlite3_malloc(nBlob*2 + 1); 1534 shell_check_oom(zStr); 1535 1536 for(i=0; i<nBlob; i++){ 1537 static const char aHex[] = { 1538 '0', '1', '2', '3', '4', '5', '6', '7', 1539 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 1540 }; 1541 zStr[i*2] = aHex[ (aBlob[i] >> 4) ]; 1542 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; 1543 } 1544 zStr[i*2] = '\0'; 1545 1546 raw_printf(out,"X'%s'", zStr); 1547 sqlite3_free(zStr); 1548} 1549 1550/* 1551** Find a string that is not found anywhere in z[]. Return a pointer 1552** to that string. 1553** 1554** Try to use zA and zB first. If both of those are already found in z[] 1555** then make up some string and store it in the buffer zBuf. 1556*/ 1557static const char *unused_string( 1558 const char *z, /* Result must not appear anywhere in z */ 1559 const char *zA, const char *zB, /* Try these first */ 1560 char *zBuf /* Space to store a generated string */ 1561){ 1562 unsigned i = 0; 1563 if( strstr(z, zA)==0 ) return zA; 1564 if( strstr(z, zB)==0 ) return zB; 1565 do{ 1566 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); 1567 }while( strstr(z,zBuf)!=0 ); 1568 return zBuf; 1569} 1570 1571/* 1572** Output the given string as a quoted string using SQL quoting conventions. 1573** 1574** See also: output_quoted_escaped_string() 1575*/ 1576static void output_quoted_string(FILE *out, const char *z){ 1577 int i; 1578 char c; 1579 setBinaryMode(out, 1); 1580 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1581 if( c==0 ){ 1582 utf8_printf(out,"'%s'",z); 1583 }else{ 1584 raw_printf(out, "'"); 1585 while( *z ){ 1586 for(i=0; (c = z[i])!=0 && c!='\''; i++){} 1587 if( c=='\'' ) i++; 1588 if( i ){ 1589 utf8_printf(out, "%.*s", i, z); 1590 z += i; 1591 } 1592 if( c=='\'' ){ 1593 raw_printf(out, "'"); 1594 continue; 1595 } 1596 if( c==0 ){ 1597 break; 1598 } 1599 z++; 1600 } 1601 raw_printf(out, "'"); 1602 } 1603 setTextMode(out, 1); 1604} 1605 1606/* 1607** Output the given string as a quoted string using SQL quoting conventions. 1608** Additionallly , escape the "\n" and "\r" characters so that they do not 1609** get corrupted by end-of-line translation facilities in some operating 1610** systems. 1611** 1612** This is like output_quoted_string() but with the addition of the \r\n 1613** escape mechanism. 1614*/ 1615static void output_quoted_escaped_string(FILE *out, const char *z){ 1616 int i; 1617 char c; 1618 setBinaryMode(out, 1); 1619 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} 1620 if( c==0 ){ 1621 utf8_printf(out,"'%s'",z); 1622 }else{ 1623 const char *zNL = 0; 1624 const char *zCR = 0; 1625 int nNL = 0; 1626 int nCR = 0; 1627 char zBuf1[20], zBuf2[20]; 1628 for(i=0; z[i]; i++){ 1629 if( z[i]=='\n' ) nNL++; 1630 if( z[i]=='\r' ) nCR++; 1631 } 1632 if( nNL ){ 1633 raw_printf(out, "replace("); 1634 zNL = unused_string(z, "\\n", "\\012", zBuf1); 1635 } 1636 if( nCR ){ 1637 raw_printf(out, "replace("); 1638 zCR = unused_string(z, "\\r", "\\015", zBuf2); 1639 } 1640 raw_printf(out, "'"); 1641 while( *z ){ 1642 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} 1643 if( c=='\'' ) i++; 1644 if( i ){ 1645 utf8_printf(out, "%.*s", i, z); 1646 z += i; 1647 } 1648 if( c=='\'' ){ 1649 raw_printf(out, "'"); 1650 continue; 1651 } 1652 if( c==0 ){ 1653 break; 1654 } 1655 z++; 1656 if( c=='\n' ){ 1657 raw_printf(out, "%s", zNL); 1658 continue; 1659 } 1660 raw_printf(out, "%s", zCR); 1661 } 1662 raw_printf(out, "'"); 1663 if( nCR ){ 1664 raw_printf(out, ",'%s',char(13))", zCR); 1665 } 1666 if( nNL ){ 1667 raw_printf(out, ",'%s',char(10))", zNL); 1668 } 1669 } 1670 setTextMode(out, 1); 1671} 1672 1673/* 1674** Output the given string as a quoted according to C or TCL quoting rules. 1675*/ 1676static void output_c_string(FILE *out, const char *z){ 1677 unsigned int c; 1678 fputc('"', out); 1679 while( (c = *(z++))!=0 ){ 1680 if( c=='\\' ){ 1681 fputc(c, out); 1682 fputc(c, out); 1683 }else if( c=='"' ){ 1684 fputc('\\', out); 1685 fputc('"', out); 1686 }else if( c=='\t' ){ 1687 fputc('\\', out); 1688 fputc('t', out); 1689 }else if( c=='\n' ){ 1690 fputc('\\', out); 1691 fputc('n', out); 1692 }else if( c=='\r' ){ 1693 fputc('\\', out); 1694 fputc('r', out); 1695 }else if( !isprint(c&0xff) ){ 1696 raw_printf(out, "\\%03o", c&0xff); 1697 }else{ 1698 fputc(c, out); 1699 } 1700 } 1701 fputc('"', out); 1702} 1703 1704/* 1705** Output the given string as a quoted according to JSON quoting rules. 1706*/ 1707static void output_json_string(FILE *out, const char *z, i64 n){ 1708 unsigned int c; 1709 if( n<0 ) n = strlen(z); 1710 fputc('"', out); 1711 while( n-- ){ 1712 c = *(z++); 1713 if( c=='\\' || c=='"' ){ 1714 fputc('\\', out); 1715 fputc(c, out); 1716 }else if( c<=0x1f ){ 1717 fputc('\\', out); 1718 if( c=='\b' ){ 1719 fputc('b', out); 1720 }else if( c=='\f' ){ 1721 fputc('f', out); 1722 }else if( c=='\n' ){ 1723 fputc('n', out); 1724 }else if( c=='\r' ){ 1725 fputc('r', out); 1726 }else if( c=='\t' ){ 1727 fputc('t', out); 1728 }else{ 1729 raw_printf(out, "u%04x",c); 1730 } 1731 }else{ 1732 fputc(c, out); 1733 } 1734 } 1735 fputc('"', out); 1736} 1737 1738/* 1739** Output the given string with characters that are special to 1740** HTML escaped. 1741*/ 1742static void output_html_string(FILE *out, const char *z){ 1743 int i; 1744 if( z==0 ) z = ""; 1745 while( *z ){ 1746 for(i=0; z[i] 1747 && z[i]!='<' 1748 && z[i]!='&' 1749 && z[i]!='>' 1750 && z[i]!='\"' 1751 && z[i]!='\''; 1752 i++){} 1753 if( i>0 ){ 1754 utf8_printf(out,"%.*s",i,z); 1755 } 1756 if( z[i]=='<' ){ 1757 raw_printf(out,"<"); 1758 }else if( z[i]=='&' ){ 1759 raw_printf(out,"&"); 1760 }else if( z[i]=='>' ){ 1761 raw_printf(out,">"); 1762 }else if( z[i]=='\"' ){ 1763 raw_printf(out,"""); 1764 }else if( z[i]=='\'' ){ 1765 raw_printf(out,"'"); 1766 }else{ 1767 break; 1768 } 1769 z += i + 1; 1770 } 1771} 1772 1773/* 1774** If a field contains any character identified by a 1 in the following 1775** array, then the string must be quoted for CSV. 1776*/ 1777static const char needCsvQuote[] = { 1778 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1779 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1780 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1786 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1787 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1788 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1789 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1790 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1791 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1792 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1793 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1794}; 1795 1796/* 1797** Output a single term of CSV. Actually, p->colSeparator is used for 1798** the separator, which may or may not be a comma. p->nullValue is 1799** the null value. Strings are quoted if necessary. The separator 1800** is only issued if bSep is true. 1801*/ 1802static void output_csv(ShellState *p, const char *z, int bSep){ 1803 FILE *out = p->out; 1804 if( z==0 ){ 1805 utf8_printf(out,"%s",p->nullValue); 1806 }else{ 1807 unsigned i; 1808 for(i=0; z[i]; i++){ 1809 if( needCsvQuote[((unsigned char*)z)[i]] ){ 1810 i = 0; 1811 break; 1812 } 1813 } 1814 if( i==0 || strstr(z, p->colSeparator)!=0 ){ 1815 char *zQuoted = sqlite3_mprintf("\"%w\"", z); 1816 shell_check_oom(zQuoted); 1817 utf8_printf(out, "%s", zQuoted); 1818 sqlite3_free(zQuoted); 1819 }else{ 1820 utf8_printf(out, "%s", z); 1821 } 1822 } 1823 if( bSep ){ 1824 utf8_printf(p->out, "%s", p->colSeparator); 1825 } 1826} 1827 1828/* 1829** This routine runs when the user presses Ctrl-C 1830*/ 1831static void interrupt_handler(int NotUsed){ 1832 UNUSED_PARAMETER(NotUsed); 1833 seenInterrupt++; 1834 if( seenInterrupt>2 ) exit(1); 1835 if( globalDb ) sqlite3_interrupt(globalDb); 1836} 1837 1838#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 1839/* 1840** This routine runs for console events (e.g. Ctrl-C) on Win32 1841*/ 1842static BOOL WINAPI ConsoleCtrlHandler( 1843 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ 1844){ 1845 if( dwCtrlType==CTRL_C_EVENT ){ 1846 interrupt_handler(0); 1847 return TRUE; 1848 } 1849 return FALSE; 1850} 1851#endif 1852 1853#ifndef SQLITE_OMIT_AUTHORIZATION 1854/* 1855** This authorizer runs in safe mode. 1856*/ 1857static int safeModeAuth( 1858 void *pClientData, 1859 int op, 1860 const char *zA1, 1861 const char *zA2, 1862 const char *zA3, 1863 const char *zA4 1864){ 1865 ShellState *p = (ShellState*)pClientData; 1866 static const char *azProhibitedFunctions[] = { 1867 "edit", 1868 "fts3_tokenizer", 1869 "load_extension", 1870 "readfile", 1871 "writefile", 1872 "zipfile", 1873 "zipfile_cds", 1874 }; 1875 UNUSED_PARAMETER(zA2); 1876 UNUSED_PARAMETER(zA3); 1877 UNUSED_PARAMETER(zA4); 1878 switch( op ){ 1879 case SQLITE_ATTACH: { 1880#ifndef SQLITE_SHELL_FIDDLE 1881 /* In WASM builds the filesystem is a virtual sandbox, so 1882 ** there's no harm in using ATTACH. */ 1883 failIfSafeMode(p, "cannot run ATTACH in safe mode"); 1884#endif 1885 break; 1886 } 1887 case SQLITE_FUNCTION: { 1888 int i; 1889 for(i=0; i<ArraySize(azProhibitedFunctions); i++){ 1890 if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){ 1891 failIfSafeMode(p, "cannot use the %s() function in safe mode", 1892 azProhibitedFunctions[i]); 1893 } 1894 } 1895 break; 1896 } 1897 } 1898 return SQLITE_OK; 1899} 1900 1901/* 1902** When the ".auth ON" is set, the following authorizer callback is 1903** invoked. It always returns SQLITE_OK. 1904*/ 1905static int shellAuth( 1906 void *pClientData, 1907 int op, 1908 const char *zA1, 1909 const char *zA2, 1910 const char *zA3, 1911 const char *zA4 1912){ 1913 ShellState *p = (ShellState*)pClientData; 1914 static const char *azAction[] = { 0, 1915 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", 1916 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", 1917 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", 1918 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", 1919 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", 1920 "DROP_TRIGGER", "DROP_VIEW", "INSERT", 1921 "PRAGMA", "READ", "SELECT", 1922 "TRANSACTION", "UPDATE", "ATTACH", 1923 "DETACH", "ALTER_TABLE", "REINDEX", 1924 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", 1925 "FUNCTION", "SAVEPOINT", "RECURSIVE" 1926 }; 1927 int i; 1928 const char *az[4]; 1929 az[0] = zA1; 1930 az[1] = zA2; 1931 az[2] = zA3; 1932 az[3] = zA4; 1933 utf8_printf(p->out, "authorizer: %s", azAction[op]); 1934 for(i=0; i<4; i++){ 1935 raw_printf(p->out, " "); 1936 if( az[i] ){ 1937 output_c_string(p->out, az[i]); 1938 }else{ 1939 raw_printf(p->out, "NULL"); 1940 } 1941 } 1942 raw_printf(p->out, "\n"); 1943 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); 1944 return SQLITE_OK; 1945} 1946#endif 1947 1948/* 1949** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. 1950** 1951** This routine converts some CREATE TABLE statements for shadow tables 1952** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. 1953** 1954** If the schema statement in z[] contains a start-of-comment and if 1955** sqlite3_complete() returns false, try to terminate the comment before 1956** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c 1957*/ 1958static void printSchemaLine(FILE *out, const char *z, const char *zTail){ 1959 char *zToFree = 0; 1960 if( z==0 ) return; 1961 if( zTail==0 ) return; 1962 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ 1963 const char *zOrig = z; 1964 static const char *azTerm[] = { "", "*/", "\n" }; 1965 int i; 1966 for(i=0; i<ArraySize(azTerm); i++){ 1967 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]); 1968 if( sqlite3_complete(zNew) ){ 1969 size_t n = strlen(zNew); 1970 zNew[n-1] = 0; 1971 zToFree = zNew; 1972 z = zNew; 1973 break; 1974 } 1975 sqlite3_free(zNew); 1976 } 1977 } 1978 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ 1979 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); 1980 }else{ 1981 utf8_printf(out, "%s%s", z, zTail); 1982 } 1983 sqlite3_free(zToFree); 1984} 1985static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ 1986 char c = z[n]; 1987 z[n] = 0; 1988 printSchemaLine(out, z, zTail); 1989 z[n] = c; 1990} 1991 1992/* 1993** Return true if string z[] has nothing but whitespace and comments to the 1994** end of the first line. 1995*/ 1996static int wsToEol(const char *z){ 1997 int i; 1998 for(i=0; z[i]; i++){ 1999 if( z[i]=='\n' ) return 1; 2000 if( IsSpace(z[i]) ) continue; 2001 if( z[i]=='-' && z[i+1]=='-' ) return 1; 2002 return 0; 2003 } 2004 return 1; 2005} 2006 2007/* 2008** Add a new entry to the EXPLAIN QUERY PLAN data 2009*/ 2010static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ 2011 EQPGraphRow *pNew; 2012 i64 nText; 2013 if( zText==0 ) return; 2014 nText = strlen(zText); 2015 if( p->autoEQPtest ){ 2016 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); 2017 } 2018 pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); 2019 shell_check_oom(pNew); 2020 pNew->iEqpId = iEqpId; 2021 pNew->iParentId = p2; 2022 memcpy(pNew->zText, zText, nText+1); 2023 pNew->pNext = 0; 2024 if( p->sGraph.pLast ){ 2025 p->sGraph.pLast->pNext = pNew; 2026 }else{ 2027 p->sGraph.pRow = pNew; 2028 } 2029 p->sGraph.pLast = pNew; 2030} 2031 2032/* 2033** Free and reset the EXPLAIN QUERY PLAN data that has been collected 2034** in p->sGraph. 2035*/ 2036static void eqp_reset(ShellState *p){ 2037 EQPGraphRow *pRow, *pNext; 2038 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ 2039 pNext = pRow->pNext; 2040 sqlite3_free(pRow); 2041 } 2042 memset(&p->sGraph, 0, sizeof(p->sGraph)); 2043} 2044 2045/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after 2046** pOld, or return the first such line if pOld is NULL 2047*/ 2048static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ 2049 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; 2050 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; 2051 return pRow; 2052} 2053 2054/* Render a single level of the graph that has iEqpId as its parent. Called 2055** recursively to render sublevels. 2056*/ 2057static void eqp_render_level(ShellState *p, int iEqpId){ 2058 EQPGraphRow *pRow, *pNext; 2059 i64 n = strlen(p->sGraph.zPrefix); 2060 char *z; 2061 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ 2062 pNext = eqp_next_row(p, iEqpId, pRow); 2063 z = pRow->zText; 2064 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, 2065 pNext ? "|--" : "`--", z); 2066 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ 2067 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); 2068 eqp_render_level(p, pRow->iEqpId); 2069 p->sGraph.zPrefix[n] = 0; 2070 } 2071 } 2072} 2073 2074/* 2075** Display and reset the EXPLAIN QUERY PLAN data 2076*/ 2077static void eqp_render(ShellState *p){ 2078 EQPGraphRow *pRow = p->sGraph.pRow; 2079 if( pRow ){ 2080 if( pRow->zText[0]=='-' ){ 2081 if( pRow->pNext==0 ){ 2082 eqp_reset(p); 2083 return; 2084 } 2085 utf8_printf(p->out, "%s\n", pRow->zText+3); 2086 p->sGraph.pRow = pRow->pNext; 2087 sqlite3_free(pRow); 2088 }else{ 2089 utf8_printf(p->out, "QUERY PLAN\n"); 2090 } 2091 p->sGraph.zPrefix[0] = 0; 2092 eqp_render_level(p, 0); 2093 eqp_reset(p); 2094 } 2095} 2096 2097#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 2098/* 2099** Progress handler callback. 2100*/ 2101static int progress_handler(void *pClientData) { 2102 ShellState *p = (ShellState*)pClientData; 2103 p->nProgress++; 2104 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ 2105 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); 2106 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 2107 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; 2108 return 1; 2109 } 2110 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ 2111 raw_printf(p->out, "Progress %u\n", p->nProgress); 2112 } 2113 return 0; 2114} 2115#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 2116 2117/* 2118** Print N dashes 2119*/ 2120static void print_dashes(FILE *out, int N){ 2121 const char zDash[] = "--------------------------------------------------"; 2122 const int nDash = sizeof(zDash) - 1; 2123 while( N>nDash ){ 2124 fputs(zDash, out); 2125 N -= nDash; 2126 } 2127 raw_printf(out, "%.*s", N, zDash); 2128} 2129 2130/* 2131** Print a markdown or table-style row separator using ascii-art 2132*/ 2133static void print_row_separator( 2134 ShellState *p, 2135 int nArg, 2136 const char *zSep 2137){ 2138 int i; 2139 if( nArg>0 ){ 2140 fputs(zSep, p->out); 2141 print_dashes(p->out, p->actualWidth[0]+2); 2142 for(i=1; i<nArg; i++){ 2143 fputs(zSep, p->out); 2144 print_dashes(p->out, p->actualWidth[i]+2); 2145 } 2146 fputs(zSep, p->out); 2147 } 2148 fputs("\n", p->out); 2149} 2150 2151/* 2152** This is the callback routine that the shell 2153** invokes for each row of a query result. 2154*/ 2155static int shell_callback( 2156 void *pArg, 2157 int nArg, /* Number of result columns */ 2158 char **azArg, /* Text of each result column */ 2159 char **azCol, /* Column names */ 2160 int *aiType /* Column types. Might be NULL */ 2161){ 2162 int i; 2163 ShellState *p = (ShellState*)pArg; 2164 2165 if( azArg==0 ) return 0; 2166 switch( p->cMode ){ 2167 case MODE_Count: 2168 case MODE_Off: { 2169 break; 2170 } 2171 case MODE_Line: { 2172 int w = 5; 2173 if( azArg==0 ) break; 2174 for(i=0; i<nArg; i++){ 2175 int len = strlen30(azCol[i] ? azCol[i] : ""); 2176 if( len>w ) w = len; 2177 } 2178 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); 2179 for(i=0; i<nArg; i++){ 2180 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], 2181 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); 2182 } 2183 break; 2184 } 2185 case MODE_Explain: { 2186 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; 2187 if( nArg>ArraySize(aExplainWidth) ){ 2188 nArg = ArraySize(aExplainWidth); 2189 } 2190 if( p->cnt++==0 ){ 2191 for(i=0; i<nArg; i++){ 2192 int w = aExplainWidth[i]; 2193 utf8_width_print(p->out, w, azCol[i]); 2194 fputs(i==nArg-1 ? "\n" : " ", p->out); 2195 } 2196 for(i=0; i<nArg; i++){ 2197 int w = aExplainWidth[i]; 2198 print_dashes(p->out, w); 2199 fputs(i==nArg-1 ? "\n" : " ", p->out); 2200 } 2201 } 2202 if( azArg==0 ) break; 2203 for(i=0; i<nArg; i++){ 2204 int w = aExplainWidth[i]; 2205 if( i==nArg-1 ) w = 0; 2206 if( azArg[i] && strlenChar(azArg[i])>w ){ 2207 w = strlenChar(azArg[i]); 2208 } 2209 if( i==1 && p->aiIndent && p->pStmt ){ 2210 if( p->iIndent<p->nIndent ){ 2211 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); 2212 } 2213 p->iIndent++; 2214 } 2215 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); 2216 fputs(i==nArg-1 ? "\n" : " ", p->out); 2217 } 2218 break; 2219 } 2220 case MODE_Semi: { /* .schema and .fullschema output */ 2221 printSchemaLine(p->out, azArg[0], ";\n"); 2222 break; 2223 } 2224 case MODE_Pretty: { /* .schema and .fullschema with --indent */ 2225 char *z; 2226 int j; 2227 int nParen = 0; 2228 char cEnd = 0; 2229 char c; 2230 int nLine = 0; 2231 assert( nArg==1 ); 2232 if( azArg[0]==0 ) break; 2233 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 2234 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 2235 ){ 2236 utf8_printf(p->out, "%s;\n", azArg[0]); 2237 break; 2238 } 2239 z = sqlite3_mprintf("%s", azArg[0]); 2240 shell_check_oom(z); 2241 j = 0; 2242 for(i=0; IsSpace(z[i]); i++){} 2243 for(; (c = z[i])!=0; i++){ 2244 if( IsSpace(c) ){ 2245 if( z[j-1]=='\r' ) z[j-1] = '\n'; 2246 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; 2247 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ 2248 j--; 2249 } 2250 z[j++] = c; 2251 } 2252 while( j>0 && IsSpace(z[j-1]) ){ j--; } 2253 z[j] = 0; 2254 if( strlen30(z)>=79 ){ 2255 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ 2256 if( c==cEnd ){ 2257 cEnd = 0; 2258 }else if( c=='"' || c=='\'' || c=='`' ){ 2259 cEnd = c; 2260 }else if( c=='[' ){ 2261 cEnd = ']'; 2262 }else if( c=='-' && z[i+1]=='-' ){ 2263 cEnd = '\n'; 2264 }else if( c=='(' ){ 2265 nParen++; 2266 }else if( c==')' ){ 2267 nParen--; 2268 if( nLine>0 && nParen==0 && j>0 ){ 2269 printSchemaLineN(p->out, z, j, "\n"); 2270 j = 0; 2271 } 2272 } 2273 z[j++] = c; 2274 if( nParen==1 && cEnd==0 2275 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) 2276 ){ 2277 if( c=='\n' ) j--; 2278 printSchemaLineN(p->out, z, j, "\n "); 2279 j = 0; 2280 nLine++; 2281 while( IsSpace(z[i+1]) ){ i++; } 2282 } 2283 } 2284 z[j] = 0; 2285 } 2286 printSchemaLine(p->out, z, ";\n"); 2287 sqlite3_free(z); 2288 break; 2289 } 2290 case MODE_List: { 2291 if( p->cnt++==0 && p->showHeader ){ 2292 for(i=0; i<nArg; i++){ 2293 utf8_printf(p->out,"%s%s",azCol[i], 2294 i==nArg-1 ? p->rowSeparator : p->colSeparator); 2295 } 2296 } 2297 if( azArg==0 ) break; 2298 for(i=0; i<nArg; i++){ 2299 char *z = azArg[i]; 2300 if( z==0 ) z = p->nullValue; 2301 utf8_printf(p->out, "%s", z); 2302 if( i<nArg-1 ){ 2303 utf8_printf(p->out, "%s", p->colSeparator); 2304 }else{ 2305 utf8_printf(p->out, "%s", p->rowSeparator); 2306 } 2307 } 2308 break; 2309 } 2310 case MODE_Html: { 2311 if( p->cnt++==0 && p->showHeader ){ 2312 raw_printf(p->out,"<TR>"); 2313 for(i=0; i<nArg; i++){ 2314 raw_printf(p->out,"<TH>"); 2315 output_html_string(p->out, azCol[i]); 2316 raw_printf(p->out,"</TH>\n"); 2317 } 2318 raw_printf(p->out,"</TR>\n"); 2319 } 2320 if( azArg==0 ) break; 2321 raw_printf(p->out,"<TR>"); 2322 for(i=0; i<nArg; i++){ 2323 raw_printf(p->out,"<TD>"); 2324 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2325 raw_printf(p->out,"</TD>\n"); 2326 } 2327 raw_printf(p->out,"</TR>\n"); 2328 break; 2329 } 2330 case MODE_Tcl: { 2331 if( p->cnt++==0 && p->showHeader ){ 2332 for(i=0; i<nArg; i++){ 2333 output_c_string(p->out,azCol[i] ? azCol[i] : ""); 2334 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2335 } 2336 utf8_printf(p->out, "%s", p->rowSeparator); 2337 } 2338 if( azArg==0 ) break; 2339 for(i=0; i<nArg; i++){ 2340 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); 2341 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); 2342 } 2343 utf8_printf(p->out, "%s", p->rowSeparator); 2344 break; 2345 } 2346 case MODE_Csv: { 2347 setBinaryMode(p->out, 1); 2348 if( p->cnt++==0 && p->showHeader ){ 2349 for(i=0; i<nArg; i++){ 2350 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); 2351 } 2352 utf8_printf(p->out, "%s", p->rowSeparator); 2353 } 2354 if( nArg>0 ){ 2355 for(i=0; i<nArg; i++){ 2356 output_csv(p, azArg[i], i<nArg-1); 2357 } 2358 utf8_printf(p->out, "%s", p->rowSeparator); 2359 } 2360 setTextMode(p->out, 1); 2361 break; 2362 } 2363 case MODE_Insert: { 2364 if( azArg==0 ) break; 2365 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); 2366 if( p->showHeader ){ 2367 raw_printf(p->out,"("); 2368 for(i=0; i<nArg; i++){ 2369 if( i>0 ) raw_printf(p->out, ","); 2370 if( quoteChar(azCol[i]) ){ 2371 char *z = sqlite3_mprintf("\"%w\"", azCol[i]); 2372 shell_check_oom(z); 2373 utf8_printf(p->out, "%s", z); 2374 sqlite3_free(z); 2375 }else{ 2376 raw_printf(p->out, "%s", azCol[i]); 2377 } 2378 } 2379 raw_printf(p->out,")"); 2380 } 2381 p->cnt++; 2382 for(i=0; i<nArg; i++){ 2383 raw_printf(p->out, i>0 ? "," : " VALUES("); 2384 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2385 utf8_printf(p->out,"NULL"); 2386 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2387 if( ShellHasFlag(p, SHFLG_Newlines) ){ 2388 output_quoted_string(p->out, azArg[i]); 2389 }else{ 2390 output_quoted_escaped_string(p->out, azArg[i]); 2391 } 2392 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2393 utf8_printf(p->out,"%s", azArg[i]); 2394 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2395 char z[50]; 2396 double r = sqlite3_column_double(p->pStmt, i); 2397 sqlite3_uint64 ur; 2398 memcpy(&ur,&r,sizeof(r)); 2399 if( ur==0x7ff0000000000000LL ){ 2400 raw_printf(p->out, "1e999"); 2401 }else if( ur==0xfff0000000000000LL ){ 2402 raw_printf(p->out, "-1e999"); 2403 }else{ 2404 sqlite3_int64 ir = (sqlite3_int64)r; 2405 if( r==(double)ir ){ 2406 sqlite3_snprintf(50,z,"%lld.0", ir); 2407 }else{ 2408 sqlite3_snprintf(50,z,"%!.20g", r); 2409 } 2410 raw_printf(p->out, "%s", z); 2411 } 2412 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2413 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2414 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2415 output_hex_blob(p->out, pBlob, nBlob); 2416 }else if( isNumber(azArg[i], 0) ){ 2417 utf8_printf(p->out,"%s", azArg[i]); 2418 }else if( ShellHasFlag(p, SHFLG_Newlines) ){ 2419 output_quoted_string(p->out, azArg[i]); 2420 }else{ 2421 output_quoted_escaped_string(p->out, azArg[i]); 2422 } 2423 } 2424 raw_printf(p->out,");\n"); 2425 break; 2426 } 2427 case MODE_Json: { 2428 if( azArg==0 ) break; 2429 if( p->cnt==0 ){ 2430 fputs("[{", p->out); 2431 }else{ 2432 fputs(",\n{", p->out); 2433 } 2434 p->cnt++; 2435 for(i=0; i<nArg; i++){ 2436 output_json_string(p->out, azCol[i], -1); 2437 putc(':', p->out); 2438 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2439 fputs("null",p->out); 2440 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2441 char z[50]; 2442 double r = sqlite3_column_double(p->pStmt, i); 2443 sqlite3_uint64 ur; 2444 memcpy(&ur,&r,sizeof(r)); 2445 if( ur==0x7ff0000000000000LL ){ 2446 raw_printf(p->out, "1e999"); 2447 }else if( ur==0xfff0000000000000LL ){ 2448 raw_printf(p->out, "-1e999"); 2449 }else{ 2450 sqlite3_snprintf(50,z,"%!.20g", r); 2451 raw_printf(p->out, "%s", z); 2452 } 2453 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2454 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2455 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2456 output_json_string(p->out, pBlob, nBlob); 2457 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2458 output_json_string(p->out, azArg[i], -1); 2459 }else{ 2460 utf8_printf(p->out,"%s", azArg[i]); 2461 } 2462 if( i<nArg-1 ){ 2463 putc(',', p->out); 2464 } 2465 } 2466 putc('}', p->out); 2467 break; 2468 } 2469 case MODE_Quote: { 2470 if( azArg==0 ) break; 2471 if( p->cnt==0 && p->showHeader ){ 2472 for(i=0; i<nArg; i++){ 2473 if( i>0 ) fputs(p->colSeparator, p->out); 2474 output_quoted_string(p->out, azCol[i]); 2475 } 2476 fputs(p->rowSeparator, p->out); 2477 } 2478 p->cnt++; 2479 for(i=0; i<nArg; i++){ 2480 if( i>0 ) fputs(p->colSeparator, p->out); 2481 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ 2482 utf8_printf(p->out,"NULL"); 2483 }else if( aiType && aiType[i]==SQLITE_TEXT ){ 2484 output_quoted_string(p->out, azArg[i]); 2485 }else if( aiType && aiType[i]==SQLITE_INTEGER ){ 2486 utf8_printf(p->out,"%s", azArg[i]); 2487 }else if( aiType && aiType[i]==SQLITE_FLOAT ){ 2488 char z[50]; 2489 double r = sqlite3_column_double(p->pStmt, i); 2490 sqlite3_snprintf(50,z,"%!.20g", r); 2491 raw_printf(p->out, "%s", z); 2492 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ 2493 const void *pBlob = sqlite3_column_blob(p->pStmt, i); 2494 int nBlob = sqlite3_column_bytes(p->pStmt, i); 2495 output_hex_blob(p->out, pBlob, nBlob); 2496 }else if( isNumber(azArg[i], 0) ){ 2497 utf8_printf(p->out,"%s", azArg[i]); 2498 }else{ 2499 output_quoted_string(p->out, azArg[i]); 2500 } 2501 } 2502 fputs(p->rowSeparator, p->out); 2503 break; 2504 } 2505 case MODE_Ascii: { 2506 if( p->cnt++==0 && p->showHeader ){ 2507 for(i=0; i<nArg; i++){ 2508 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2509 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); 2510 } 2511 utf8_printf(p->out, "%s", p->rowSeparator); 2512 } 2513 if( azArg==0 ) break; 2514 for(i=0; i<nArg; i++){ 2515 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); 2516 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); 2517 } 2518 utf8_printf(p->out, "%s", p->rowSeparator); 2519 break; 2520 } 2521 case MODE_EQP: { 2522 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); 2523 break; 2524 } 2525 } 2526 return 0; 2527} 2528 2529/* 2530** This is the callback routine that the SQLite library 2531** invokes for each row of a query result. 2532*/ 2533static int callback(void *pArg, int nArg, char **azArg, char **azCol){ 2534 /* since we don't have type info, call the shell_callback with a NULL value */ 2535 return shell_callback(pArg, nArg, azArg, azCol, NULL); 2536} 2537 2538/* 2539** This is the callback routine from sqlite3_exec() that appends all 2540** output onto the end of a ShellText object. 2541*/ 2542static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ 2543 ShellText *p = (ShellText*)pArg; 2544 int i; 2545 UNUSED_PARAMETER(az); 2546 if( azArg==0 ) return 0; 2547 if( p->n ) appendText(p, "|", 0); 2548 for(i=0; i<nArg; i++){ 2549 if( i ) appendText(p, ",", 0); 2550 if( azArg[i] ) appendText(p, azArg[i], 0); 2551 } 2552 return 0; 2553} 2554 2555/* 2556** Generate an appropriate SELFTEST table in the main database. 2557*/ 2558static void createSelftestTable(ShellState *p){ 2559 char *zErrMsg = 0; 2560 sqlite3_exec(p->db, 2561 "SAVEPOINT selftest_init;\n" 2562 "CREATE TABLE IF NOT EXISTS selftest(\n" 2563 " tno INTEGER PRIMARY KEY,\n" /* Test number */ 2564 " op TEXT,\n" /* Operator: memo run */ 2565 " cmd TEXT,\n" /* Command text */ 2566 " ans TEXT\n" /* Desired answer */ 2567 ");" 2568 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" 2569 "INSERT INTO [_shell$self](rowid,op,cmd)\n" 2570 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" 2571 " 'memo','Tests generated by --init');\n" 2572 "INSERT INTO [_shell$self]\n" 2573 " SELECT 'run',\n" 2574 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " 2575 "FROM sqlite_schema ORDER BY 2'',224))',\n" 2576 " hex(sha3_query('SELECT type,name,tbl_name,sql " 2577 "FROM sqlite_schema ORDER BY 2',224));\n" 2578 "INSERT INTO [_shell$self]\n" 2579 " SELECT 'run'," 2580 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" 2581 " printf('%w',name) || '\" NOT INDEXED'',224))',\n" 2582 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" 2583 " FROM (\n" 2584 " SELECT name FROM sqlite_schema\n" 2585 " WHERE type='table'\n" 2586 " AND name<>'selftest'\n" 2587 " AND coalesce(rootpage,0)>0\n" 2588 " )\n" 2589 " ORDER BY name;\n" 2590 "INSERT INTO [_shell$self]\n" 2591 " VALUES('run','PRAGMA integrity_check','ok');\n" 2592 "INSERT INTO selftest(tno,op,cmd,ans)" 2593 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" 2594 "DROP TABLE [_shell$self];" 2595 ,0,0,&zErrMsg); 2596 if( zErrMsg ){ 2597 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); 2598 sqlite3_free(zErrMsg); 2599 } 2600 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); 2601} 2602 2603 2604/* 2605** Set the destination table field of the ShellState structure to 2606** the name of the table given. Escape any quote characters in the 2607** table name. 2608*/ 2609static void set_table_name(ShellState *p, const char *zName){ 2610 int i, n; 2611 char cQuote; 2612 char *z; 2613 2614 if( p->zDestTable ){ 2615 free(p->zDestTable); 2616 p->zDestTable = 0; 2617 } 2618 if( zName==0 ) return; 2619 cQuote = quoteChar(zName); 2620 n = strlen30(zName); 2621 if( cQuote ) n += n+2; 2622 z = p->zDestTable = malloc( n+1 ); 2623 shell_check_oom(z); 2624 n = 0; 2625 if( cQuote ) z[n++] = cQuote; 2626 for(i=0; zName[i]; i++){ 2627 z[n++] = zName[i]; 2628 if( zName[i]==cQuote ) z[n++] = cQuote; 2629 } 2630 if( cQuote ) z[n++] = cQuote; 2631 z[n] = 0; 2632} 2633 2634/* 2635** Maybe construct two lines of text that point out the position of a 2636** syntax error. Return a pointer to the text, in memory obtained from 2637** sqlite3_malloc(). Or, if the most recent error does not involve a 2638** specific token that we can point to, return an empty string. 2639** 2640** In all cases, the memory returned is obtained from sqlite3_malloc64() 2641** and should be released by the caller invoking sqlite3_free(). 2642*/ 2643static char *shell_error_context(const char *zSql, sqlite3 *db){ 2644 int iOffset; 2645 size_t len; 2646 char *zCode; 2647 char *zMsg; 2648 int i; 2649 if( db==0 2650 || zSql==0 2651 || (iOffset = sqlite3_error_offset(db))<0 2652 ){ 2653 return sqlite3_mprintf(""); 2654 } 2655 while( iOffset>50 ){ 2656 iOffset--; 2657 zSql++; 2658 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } 2659 } 2660 len = strlen(zSql); 2661 if( len>78 ){ 2662 len = 78; 2663 while( (zSql[len]&0xc0)==0x80 ) len--; 2664 } 2665 zCode = sqlite3_mprintf("%.*s", len, zSql); 2666 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } 2667 if( iOffset<25 ){ 2668 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, ""); 2669 }else{ 2670 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, ""); 2671 } 2672 return zMsg; 2673} 2674 2675 2676/* 2677** Execute a query statement that will generate SQL output. Print 2678** the result columns, comma-separated, on a line and then add a 2679** semicolon terminator to the end of that line. 2680** 2681** If the number of columns is 1 and that column contains text "--" 2682** then write the semicolon on a separate line. That way, if a 2683** "--" comment occurs at the end of the statement, the comment 2684** won't consume the semicolon terminator. 2685*/ 2686static int run_table_dump_query( 2687 ShellState *p, /* Query context */ 2688 const char *zSelect /* SELECT statement to extract content */ 2689){ 2690 sqlite3_stmt *pSelect; 2691 int rc; 2692 int nResult; 2693 int i; 2694 const char *z; 2695 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); 2696 if( rc!=SQLITE_OK || !pSelect ){ 2697 char *zContext = shell_error_context(zSelect, p->db); 2698 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc, 2699 sqlite3_errmsg(p->db), zContext); 2700 sqlite3_free(zContext); 2701 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2702 return rc; 2703 } 2704 rc = sqlite3_step(pSelect); 2705 nResult = sqlite3_column_count(pSelect); 2706 while( rc==SQLITE_ROW ){ 2707 z = (const char*)sqlite3_column_text(pSelect, 0); 2708 utf8_printf(p->out, "%s", z); 2709 for(i=1; i<nResult; i++){ 2710 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); 2711 } 2712 if( z==0 ) z = ""; 2713 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; 2714 if( z[0] ){ 2715 raw_printf(p->out, "\n;\n"); 2716 }else{ 2717 raw_printf(p->out, ";\n"); 2718 } 2719 rc = sqlite3_step(pSelect); 2720 } 2721 rc = sqlite3_finalize(pSelect); 2722 if( rc!=SQLITE_OK ){ 2723 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, 2724 sqlite3_errmsg(p->db)); 2725 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; 2726 } 2727 return rc; 2728} 2729 2730/* 2731** Allocate space and save off string indicating current error. 2732*/ 2733static char *save_err_msg( 2734 sqlite3 *db, /* Database to query */ 2735 const char *zPhase, /* When the error occcurs */ 2736 int rc, /* Error code returned from API */ 2737 const char *zSql /* SQL string, or NULL */ 2738){ 2739 char *zErr; 2740 char *zContext; 2741 sqlite3_str *pStr = sqlite3_str_new(0); 2742 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); 2743 if( rc>1 ){ 2744 sqlite3_str_appendf(pStr, " (%d)", rc); 2745 } 2746 zContext = shell_error_context(zSql, db); 2747 if( zContext ){ 2748 sqlite3_str_appendall(pStr, zContext); 2749 sqlite3_free(zContext); 2750 } 2751 zErr = sqlite3_str_finish(pStr); 2752 shell_check_oom(zErr); 2753 return zErr; 2754} 2755 2756#ifdef __linux__ 2757/* 2758** Attempt to display I/O stats on Linux using /proc/PID/io 2759*/ 2760static void displayLinuxIoStats(FILE *out){ 2761 FILE *in; 2762 char z[200]; 2763 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 2764 in = fopen(z, "rb"); 2765 if( in==0 ) return; 2766 while( fgets(z, sizeof(z), in)!=0 ){ 2767 static const struct { 2768 const char *zPattern; 2769 const char *zDesc; 2770 } aTrans[] = { 2771 { "rchar: ", "Bytes received by read():" }, 2772 { "wchar: ", "Bytes sent to write():" }, 2773 { "syscr: ", "Read() system calls:" }, 2774 { "syscw: ", "Write() system calls:" }, 2775 { "read_bytes: ", "Bytes read from storage:" }, 2776 { "write_bytes: ", "Bytes written to storage:" }, 2777 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 2778 }; 2779 int i; 2780 for(i=0; i<ArraySize(aTrans); i++){ 2781 int n = strlen30(aTrans[i].zPattern); 2782 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){ 2783 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); 2784 break; 2785 } 2786 } 2787 } 2788 fclose(in); 2789} 2790#endif 2791 2792/* 2793** Display a single line of status using 64-bit values. 2794*/ 2795static void displayStatLine( 2796 ShellState *p, /* The shell context */ 2797 char *zLabel, /* Label for this one line */ 2798 char *zFormat, /* Format for the result */ 2799 int iStatusCtrl, /* Which status to display */ 2800 int bReset /* True to reset the stats */ 2801){ 2802 sqlite3_int64 iCur = -1; 2803 sqlite3_int64 iHiwtr = -1; 2804 int i, nPercent; 2805 char zLine[200]; 2806 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); 2807 for(i=0, nPercent=0; zFormat[i]; i++){ 2808 if( zFormat[i]=='%' ) nPercent++; 2809 } 2810 if( nPercent>1 ){ 2811 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); 2812 }else{ 2813 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); 2814 } 2815 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); 2816} 2817 2818/* 2819** Display memory stats. 2820*/ 2821static int display_stats( 2822 sqlite3 *db, /* Database to query */ 2823 ShellState *pArg, /* Pointer to ShellState */ 2824 int bReset /* True to reset the stats */ 2825){ 2826 int iCur; 2827 int iHiwtr; 2828 FILE *out; 2829 if( pArg==0 || pArg->out==0 ) return 0; 2830 out = pArg->out; 2831 2832 if( pArg->pStmt && pArg->statsOn==2 ){ 2833 int nCol, i, x; 2834 sqlite3_stmt *pStmt = pArg->pStmt; 2835 char z[100]; 2836 nCol = sqlite3_column_count(pStmt); 2837 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); 2838 for(i=0; i<nCol; i++){ 2839 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); 2840 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); 2841#ifndef SQLITE_OMIT_DECLTYPE 2842 sqlite3_snprintf(30, z+x, "declared type:"); 2843 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); 2844#endif 2845#ifdef SQLITE_ENABLE_COLUMN_METADATA 2846 sqlite3_snprintf(30, z+x, "database name:"); 2847 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); 2848 sqlite3_snprintf(30, z+x, "table name:"); 2849 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); 2850 sqlite3_snprintf(30, z+x, "origin name:"); 2851 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); 2852#endif 2853 } 2854 } 2855 2856 if( pArg->statsOn==3 ){ 2857 if( pArg->pStmt ){ 2858 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2859 raw_printf(pArg->out, "VM-steps: %d\n", iCur); 2860 } 2861 return 0; 2862 } 2863 2864 displayStatLine(pArg, "Memory Used:", 2865 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); 2866 displayStatLine(pArg, "Number of Outstanding Allocations:", 2867 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); 2868 if( pArg->shellFlgs & SHFLG_Pagecache ){ 2869 displayStatLine(pArg, "Number of Pcache Pages Used:", 2870 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); 2871 } 2872 displayStatLine(pArg, "Number of Pcache Overflow Bytes:", 2873 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); 2874 displayStatLine(pArg, "Largest Allocation:", 2875 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); 2876 displayStatLine(pArg, "Largest Pcache Allocation:", 2877 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); 2878#ifdef YYTRACKMAXSTACKDEPTH 2879 displayStatLine(pArg, "Deepest Parser Stack:", 2880 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); 2881#endif 2882 2883 if( db ){ 2884 if( pArg->shellFlgs & SHFLG_Lookaside ){ 2885 iHiwtr = iCur = -1; 2886 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, 2887 &iCur, &iHiwtr, bReset); 2888 raw_printf(pArg->out, 2889 "Lookaside Slots Used: %d (max %d)\n", 2890 iCur, iHiwtr); 2891 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, 2892 &iCur, &iHiwtr, bReset); 2893 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", 2894 iHiwtr); 2895 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, 2896 &iCur, &iHiwtr, bReset); 2897 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", 2898 iHiwtr); 2899 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, 2900 &iCur, &iHiwtr, bReset); 2901 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", 2902 iHiwtr); 2903 } 2904 iHiwtr = iCur = -1; 2905 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); 2906 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", 2907 iCur); 2908 iHiwtr = iCur = -1; 2909 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); 2910 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); 2911 iHiwtr = iCur = -1; 2912 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); 2913 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); 2914 iHiwtr = iCur = -1; 2915 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); 2916 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); 2917 iHiwtr = iCur = -1; 2918 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); 2919 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); 2920 iHiwtr = iCur = -1; 2921 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); 2922 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", 2923 iCur); 2924 iHiwtr = iCur = -1; 2925 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); 2926 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", 2927 iCur); 2928 } 2929 2930 if( pArg->pStmt ){ 2931 int iHit, iMiss; 2932 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, 2933 bReset); 2934 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); 2935 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); 2936 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); 2937 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); 2938 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); 2939 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset); 2940 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset); 2941 if( iHit || iMiss ){ 2942 raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n", 2943 iHit, iHit+iMiss); 2944 } 2945 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); 2946 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); 2947 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); 2948 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); 2949 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); 2950 raw_printf(pArg->out, "Number of times run: %d\n", iCur); 2951 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); 2952 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); 2953 } 2954 2955#ifdef __linux__ 2956 displayLinuxIoStats(pArg->out); 2957#endif 2958 2959 /* Do not remove this machine readable comment: extra-stats-output-here */ 2960 2961 return 0; 2962} 2963 2964/* 2965** Display scan stats. 2966*/ 2967static void display_scanstats( 2968 sqlite3 *db, /* Database to query */ 2969 ShellState *pArg /* Pointer to ShellState */ 2970){ 2971#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 2972 UNUSED_PARAMETER(db); 2973 UNUSED_PARAMETER(pArg); 2974#else 2975 int i, k, n, mx; 2976 raw_printf(pArg->out, "-------- scanstats --------\n"); 2977 mx = 0; 2978 for(k=0; k<=mx; k++){ 2979 double rEstLoop = 1.0; 2980 for(i=n=0; 1; i++){ 2981 sqlite3_stmt *p = pArg->pStmt; 2982 sqlite3_int64 nLoop, nVisit; 2983 double rEst; 2984 int iSid; 2985 const char *zExplain; 2986 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ 2987 break; 2988 } 2989 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); 2990 if( iSid>mx ) mx = iSid; 2991 if( iSid!=k ) continue; 2992 if( n==0 ){ 2993 rEstLoop = (double)nLoop; 2994 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); 2995 } 2996 n++; 2997 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2998 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); 2999 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 3000 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); 3001 rEstLoop *= rEst; 3002 raw_printf(pArg->out, 3003 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", 3004 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst 3005 ); 3006 } 3007 } 3008 raw_printf(pArg->out, "---------------------------\n"); 3009#endif 3010} 3011 3012/* 3013** Parameter azArray points to a zero-terminated array of strings. zStr 3014** points to a single nul-terminated string. Return non-zero if zStr 3015** is equal, according to strcmp(), to any of the strings in the array. 3016** Otherwise, return zero. 3017*/ 3018static int str_in_array(const char *zStr, const char **azArray){ 3019 int i; 3020 for(i=0; azArray[i]; i++){ 3021 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1; 3022 } 3023 return 0; 3024} 3025 3026/* 3027** If compiled statement pSql appears to be an EXPLAIN statement, allocate 3028** and populate the ShellState.aiIndent[] array with the number of 3029** spaces each opcode should be indented before it is output. 3030** 3031** The indenting rules are: 3032** 3033** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent 3034** all opcodes that occur between the p2 jump destination and the opcode 3035** itself by 2 spaces. 3036** 3037** * Do the previous for "Return" instructions for when P2 is positive. 3038** See tag-20220407a in wherecode.c and vdbe.c. 3039** 3040** * For each "Goto", if the jump destination is earlier in the program 3041** and ends on one of: 3042** Yield SeekGt SeekLt RowSetRead Rewind 3043** or if the P1 parameter is one instead of zero, 3044** then indent all opcodes between the earlier instruction 3045** and "Goto" by 2 spaces. 3046*/ 3047static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ 3048 const char *zSql; /* The text of the SQL statement */ 3049 const char *z; /* Used to check if this is an EXPLAIN */ 3050 int *abYield = 0; /* True if op is an OP_Yield */ 3051 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ 3052 int iOp; /* Index of operation in p->aiIndent[] */ 3053 3054 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 3055 "Return", 0 }; 3056 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", 3057 "Rewind", 0 }; 3058 const char *azGoto[] = { "Goto", 0 }; 3059 3060 /* Try to figure out if this is really an EXPLAIN statement. If this 3061 ** cannot be verified, return early. */ 3062 if( sqlite3_column_count(pSql)!=8 ){ 3063 p->cMode = p->mode; 3064 return; 3065 } 3066 zSql = sqlite3_sql(pSql); 3067 if( zSql==0 ) return; 3068 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); 3069 if( sqlite3_strnicmp(z, "explain", 7) ){ 3070 p->cMode = p->mode; 3071 return; 3072 } 3073 3074 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ 3075 int i; 3076 int iAddr = sqlite3_column_int(pSql, 0); 3077 const char *zOp = (const char*)sqlite3_column_text(pSql, 1); 3078 3079 /* Set p2 to the P2 field of the current opcode. Then, assuming that 3080 ** p2 is an instruction address, set variable p2op to the index of that 3081 ** instruction in the aiIndent[] array. p2 and p2op may be different if 3082 ** the current instruction is part of a sub-program generated by an 3083 ** SQL trigger or foreign key. */ 3084 int p2 = sqlite3_column_int(pSql, 3); 3085 int p2op = (p2 + (iOp-iAddr)); 3086 3087 /* Grow the p->aiIndent array as required */ 3088 if( iOp>=nAlloc ){ 3089 if( iOp==0 ){ 3090 /* Do further verfication that this is explain output. Abort if 3091 ** it is not */ 3092 static const char *explainCols[] = { 3093 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; 3094 int jj; 3095 for(jj=0; jj<ArraySize(explainCols); jj++){ 3096 if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ 3097 p->cMode = p->mode; 3098 sqlite3_reset(pSql); 3099 return; 3100 } 3101 } 3102 } 3103 nAlloc += 100; 3104 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); 3105 shell_check_oom(p->aiIndent); 3106 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); 3107 shell_check_oom(abYield); 3108 } 3109 abYield[iOp] = str_in_array(zOp, azYield); 3110 p->aiIndent[iOp] = 0; 3111 p->nIndent = iOp+1; 3112 3113 if( str_in_array(zOp, azNext) && p2op>0 ){ 3114 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 3115 } 3116 if( str_in_array(zOp, azGoto) && p2op<p->nIndent 3117 && (abYield[p2op] || sqlite3_column_int(pSql, 2)) 3118 ){ 3119 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; 3120 } 3121 } 3122 3123 p->iIndent = 0; 3124 sqlite3_free(abYield); 3125 sqlite3_reset(pSql); 3126} 3127 3128/* 3129** Free the array allocated by explain_data_prepare(). 3130*/ 3131static void explain_data_delete(ShellState *p){ 3132 sqlite3_free(p->aiIndent); 3133 p->aiIndent = 0; 3134 p->nIndent = 0; 3135 p->iIndent = 0; 3136} 3137 3138/* 3139** Disable and restore .wheretrace and .treetrace/.selecttrace settings. 3140*/ 3141static unsigned int savedSelectTrace; 3142static unsigned int savedWhereTrace; 3143static void disable_debug_trace_modes(void){ 3144 unsigned int zero = 0; 3145 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); 3146 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); 3147 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); 3148 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); 3149} 3150static void restore_debug_trace_modes(void){ 3151 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); 3152 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); 3153} 3154 3155/* Create the TEMP table used to store parameter bindings */ 3156static void bind_table_init(ShellState *p){ 3157 int wrSchema = 0; 3158 int defensiveMode = 0; 3159 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); 3160 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); 3161 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); 3162 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); 3163 sqlite3_exec(p->db, 3164 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" 3165 " key TEXT PRIMARY KEY,\n" 3166 " value\n" 3167 ") WITHOUT ROWID;", 3168 0, 0, 0); 3169 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); 3170 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); 3171} 3172 3173/* 3174** Bind parameters on a prepared statement. 3175** 3176** Parameter bindings are taken from a TEMP table of the form: 3177** 3178** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) 3179** WITHOUT ROWID; 3180** 3181** No bindings occur if this table does not exist. The name of the table 3182** begins with "sqlite_" so that it will not collide with ordinary application 3183** tables. The table must be in the TEMP schema. 3184*/ 3185static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ 3186 int nVar; 3187 int i; 3188 int rc; 3189 sqlite3_stmt *pQ = 0; 3190 3191 nVar = sqlite3_bind_parameter_count(pStmt); 3192 if( nVar==0 ) return; /* Nothing to do */ 3193 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", 3194 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ 3195 return; /* Parameter table does not exist */ 3196 } 3197 rc = sqlite3_prepare_v2(pArg->db, 3198 "SELECT value FROM temp.sqlite_parameters" 3199 " WHERE key=?1", -1, &pQ, 0); 3200 if( rc || pQ==0 ) return; 3201 for(i=1; i<=nVar; i++){ 3202 char zNum[30]; 3203 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 3204 if( zVar==0 ){ 3205 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); 3206 zVar = zNum; 3207 } 3208 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); 3209 if( sqlite3_step(pQ)==SQLITE_ROW ){ 3210 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); 3211 }else{ 3212 sqlite3_bind_null(pStmt, i); 3213 } 3214 sqlite3_reset(pQ); 3215 } 3216 sqlite3_finalize(pQ); 3217} 3218 3219/* 3220** UTF8 box-drawing characters. Imagine box lines like this: 3221** 3222** 1 3223** | 3224** 4 --+-- 2 3225** | 3226** 3 3227** 3228** Each box characters has between 2 and 4 of the lines leading from 3229** the center. The characters are here identified by the numbers of 3230** their corresponding lines. 3231*/ 3232#define BOX_24 "\342\224\200" /* U+2500 --- */ 3233#define BOX_13 "\342\224\202" /* U+2502 | */ 3234#define BOX_23 "\342\224\214" /* U+250c ,- */ 3235#define BOX_34 "\342\224\220" /* U+2510 -, */ 3236#define BOX_12 "\342\224\224" /* U+2514 '- */ 3237#define BOX_14 "\342\224\230" /* U+2518 -' */ 3238#define BOX_123 "\342\224\234" /* U+251c |- */ 3239#define BOX_134 "\342\224\244" /* U+2524 -| */ 3240#define BOX_234 "\342\224\254" /* U+252c -,- */ 3241#define BOX_124 "\342\224\264" /* U+2534 -'- */ 3242#define BOX_1234 "\342\224\274" /* U+253c -|- */ 3243 3244/* Draw horizontal line N characters long using unicode box 3245** characters 3246*/ 3247static void print_box_line(FILE *out, int N){ 3248 const char zDash[] = 3249 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 3250 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; 3251 const int nDash = sizeof(zDash) - 1; 3252 N *= 3; 3253 while( N>nDash ){ 3254 utf8_printf(out, zDash); 3255 N -= nDash; 3256 } 3257 utf8_printf(out, "%.*s", N, zDash); 3258} 3259 3260/* 3261** Draw a horizontal separator for a MODE_Box table. 3262*/ 3263static void print_box_row_separator( 3264 ShellState *p, 3265 int nArg, 3266 const char *zSep1, 3267 const char *zSep2, 3268 const char *zSep3 3269){ 3270 int i; 3271 if( nArg>0 ){ 3272 utf8_printf(p->out, "%s", zSep1); 3273 print_box_line(p->out, p->actualWidth[0]+2); 3274 for(i=1; i<nArg; i++){ 3275 utf8_printf(p->out, "%s", zSep2); 3276 print_box_line(p->out, p->actualWidth[i]+2); 3277 } 3278 utf8_printf(p->out, "%s", zSep3); 3279 } 3280 fputs("\n", p->out); 3281} 3282 3283/* 3284** z[] is a line of text that is to be displayed the .mode box or table or 3285** similar tabular formats. z[] might contain control characters such 3286** as \n, \t, \f, or \r. 3287** 3288** Compute characters to display on the first line of z[]. Stop at the 3289** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained 3290** from malloc()) of that first line, which caller should free sometime. 3291** Write anything to display on the next line into *pzTail. If this is 3292** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) 3293*/ 3294static char *translateForDisplayAndDup( 3295 const unsigned char *z, /* Input text to be transformed */ 3296 const unsigned char **pzTail, /* OUT: Tail of the input for next line */ 3297 int mxWidth, /* Max width. 0 means no limit */ 3298 u8 bWordWrap /* If true, avoid breaking mid-word */ 3299){ 3300 int i; /* Input bytes consumed */ 3301 int j; /* Output bytes generated */ 3302 int k; /* Input bytes to be displayed */ 3303 int n; /* Output column number */ 3304 unsigned char *zOut; /* Output text */ 3305 3306 if( z==0 ){ 3307 *pzTail = 0; 3308 return 0; 3309 } 3310 if( mxWidth<0 ) mxWidth = -mxWidth; 3311 if( mxWidth==0 ) mxWidth = 1000000; 3312 i = j = n = 0; 3313 while( n<mxWidth ){ 3314 if( z[i]>=' ' ){ 3315 n++; 3316 do{ i++; j++; }while( (z[i]&0xc0)==0x80 ); 3317 continue; 3318 } 3319 if( z[i]=='\t' ){ 3320 do{ 3321 n++; 3322 j++; 3323 }while( (n&7)!=0 && n<mxWidth ); 3324 i++; 3325 continue; 3326 } 3327 break; 3328 } 3329 if( n>=mxWidth && bWordWrap ){ 3330 /* Perhaps try to back up to a better place to break the line */ 3331 for(k=i; k>i/2; k--){ 3332 if( isspace(z[k-1]) ) break; 3333 } 3334 if( k<=i/2 ){ 3335 for(k=i; k>i/2; k--){ 3336 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; 3337 } 3338 } 3339 if( k<=i/2 ){ 3340 k = i; 3341 }else{ 3342 i = k; 3343 while( z[i]==' ' ) i++; 3344 } 3345 }else{ 3346 k = i; 3347 } 3348 if( n>=mxWidth && z[i]>=' ' ){ 3349 *pzTail = &z[i]; 3350 }else if( z[i]=='\r' && z[i+1]=='\n' ){ 3351 *pzTail = z[i+2] ? &z[i+2] : 0; 3352 }else if( z[i]==0 || z[i+1]==0 ){ 3353 *pzTail = 0; 3354 }else{ 3355 *pzTail = &z[i+1]; 3356 } 3357 zOut = malloc( j+1 ); 3358 shell_check_oom(zOut); 3359 i = j = n = 0; 3360 while( i<k ){ 3361 if( z[i]>=' ' ){ 3362 n++; 3363 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 ); 3364 continue; 3365 } 3366 if( z[i]=='\t' ){ 3367 do{ 3368 n++; 3369 zOut[j++] = ' '; 3370 }while( (n&7)!=0 && n<mxWidth ); 3371 i++; 3372 continue; 3373 } 3374 break; 3375 } 3376 zOut[j] = 0; 3377 return (char*)zOut; 3378} 3379 3380/* Extract the value of the i-th current column for pStmt as an SQL literal 3381** value. Memory is obtained from sqlite3_malloc64() and must be freed by 3382** the caller. 3383*/ 3384static char *quoted_column(sqlite3_stmt *pStmt, int i){ 3385 switch( sqlite3_column_type(pStmt, i) ){ 3386 case SQLITE_NULL: { 3387 return sqlite3_mprintf("NULL"); 3388 } 3389 case SQLITE_INTEGER: 3390 case SQLITE_FLOAT: { 3391 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); 3392 } 3393 case SQLITE_TEXT: { 3394 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i)); 3395 } 3396 case SQLITE_BLOB: { 3397 int j; 3398 sqlite3_str *pStr = sqlite3_str_new(0); 3399 const unsigned char *a = sqlite3_column_blob(pStmt,i); 3400 int n = sqlite3_column_bytes(pStmt,i); 3401 sqlite3_str_append(pStr, "x'", 2); 3402 for(j=0; j<n; j++){ 3403 sqlite3_str_appendf(pStr, "%02x", a[j]); 3404 } 3405 sqlite3_str_append(pStr, "'", 1); 3406 return sqlite3_str_finish(pStr); 3407 } 3408 } 3409 return 0; /* Not reached */ 3410} 3411 3412/* 3413** Run a prepared statement and output the result in one of the 3414** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, 3415** or MODE_Box. 3416** 3417** This is different from ordinary exec_prepared_stmt() in that 3418** it has to run the entire query and gather the results into memory 3419** first, in order to determine column widths, before providing 3420** any output. 3421*/ 3422static void exec_prepared_stmt_columnar( 3423 ShellState *p, /* Pointer to ShellState */ 3424 sqlite3_stmt *pStmt /* Statment to run */ 3425){ 3426 sqlite3_int64 nRow = 0; 3427 int nColumn = 0; 3428 char **azData = 0; 3429 sqlite3_int64 nAlloc = 0; 3430 char *abRowDiv = 0; 3431 const unsigned char *uz; 3432 const char *z; 3433 char **azQuoted = 0; 3434 int rc; 3435 sqlite3_int64 i, nData; 3436 int j, nTotal, w, n; 3437 const char *colSep = 0; 3438 const char *rowSep = 0; 3439 const unsigned char **azNextLine = 0; 3440 int bNextLine = 0; 3441 int bMultiLineRowExists = 0; 3442 int bw = p->cmOpts.bWordWrap; 3443 const char *zEmpty = ""; 3444 const char *zShowNull = p->nullValue; 3445 3446 rc = sqlite3_step(pStmt); 3447 if( rc!=SQLITE_ROW ) return; 3448 nColumn = sqlite3_column_count(pStmt); 3449 nAlloc = nColumn*4; 3450 if( nAlloc<=0 ) nAlloc = 1; 3451 azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); 3452 shell_check_oom(azData); 3453 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); 3454 shell_check_oom((void*)azNextLine); 3455 memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); 3456 if( p->cmOpts.bQuote ){ 3457 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); 3458 shell_check_oom(azQuoted); 3459 memset(azQuoted, 0, nColumn*sizeof(char*) ); 3460 } 3461 abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); 3462 shell_check_oom(abRowDiv); 3463 if( nColumn>p->nWidth ){ 3464 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); 3465 shell_check_oom(p->colWidth); 3466 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; 3467 p->nWidth = nColumn; 3468 p->actualWidth = &p->colWidth[nColumn]; 3469 } 3470 memset(p->actualWidth, 0, nColumn*sizeof(int)); 3471 for(i=0; i<nColumn; i++){ 3472 w = p->colWidth[i]; 3473 if( w<0 ) w = -w; 3474 p->actualWidth[i] = w; 3475 } 3476 for(i=0; i<nColumn; i++){ 3477 const unsigned char *zNotUsed; 3478 int wx = p->colWidth[i]; 3479 if( wx==0 ){ 3480 wx = p->cmOpts.iWrap; 3481 } 3482 if( wx<0 ) wx = -wx; 3483 uz = (const unsigned char*)sqlite3_column_name(pStmt,i); 3484 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw); 3485 } 3486 do{ 3487 int useNextLine = bNextLine; 3488 bNextLine = 0; 3489 if( (nRow+2)*nColumn >= nAlloc ){ 3490 nAlloc *= 2; 3491 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); 3492 shell_check_oom(azData); 3493 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); 3494 shell_check_oom(abRowDiv); 3495 } 3496 abRowDiv[nRow] = 1; 3497 nRow++; 3498 for(i=0; i<nColumn; i++){ 3499 int wx = p->colWidth[i]; 3500 if( wx==0 ){ 3501 wx = p->cmOpts.iWrap; 3502 } 3503 if( wx<0 ) wx = -wx; 3504 if( useNextLine ){ 3505 uz = azNextLine[i]; 3506 if( uz==0 ) uz = (u8*)zEmpty; 3507 }else if( p->cmOpts.bQuote ){ 3508 sqlite3_free(azQuoted[i]); 3509 azQuoted[i] = quoted_column(pStmt,i); 3510 uz = (const unsigned char*)azQuoted[i]; 3511 }else{ 3512 uz = (const unsigned char*)sqlite3_column_text(pStmt,i); 3513 if( uz==0 ) uz = (u8*)zShowNull; 3514 } 3515 azData[nRow*nColumn + i] 3516 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw); 3517 if( azNextLine[i] ){ 3518 bNextLine = 1; 3519 abRowDiv[nRow-1] = 0; 3520 bMultiLineRowExists = 1; 3521 } 3522 } 3523 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); 3524 nTotal = nColumn*(nRow+1); 3525 for(i=0; i<nTotal; i++){ 3526 z = azData[i]; 3527 if( z==0 ) z = (char*)zEmpty; 3528 n = strlenChar(z); 3529 j = i%nColumn; 3530 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; 3531 } 3532 if( seenInterrupt ) goto columnar_end; 3533 if( nColumn==0 ) goto columnar_end; 3534 switch( p->cMode ){ 3535 case MODE_Column: { 3536 colSep = " "; 3537 rowSep = "\n"; 3538 if( p->showHeader ){ 3539 for(i=0; i<nColumn; i++){ 3540 w = p->actualWidth[i]; 3541 if( p->colWidth[i]<0 ) w = -w; 3542 utf8_width_print(p->out, w, azData[i]); 3543 fputs(i==nColumn-1?"\n":" ", p->out); 3544 } 3545 for(i=0; i<nColumn; i++){ 3546 print_dashes(p->out, p->actualWidth[i]); 3547 fputs(i==nColumn-1?"\n":" ", p->out); 3548 } 3549 } 3550 break; 3551 } 3552 case MODE_Table: { 3553 colSep = " | "; 3554 rowSep = " |\n"; 3555 print_row_separator(p, nColumn, "+"); 3556 fputs("| ", p->out); 3557 for(i=0; i<nColumn; i++){ 3558 w = p->actualWidth[i]; 3559 n = strlenChar(azData[i]); 3560 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3561 fputs(i==nColumn-1?" |\n":" | ", p->out); 3562 } 3563 print_row_separator(p, nColumn, "+"); 3564 break; 3565 } 3566 case MODE_Markdown: { 3567 colSep = " | "; 3568 rowSep = " |\n"; 3569 fputs("| ", p->out); 3570 for(i=0; i<nColumn; i++){ 3571 w = p->actualWidth[i]; 3572 n = strlenChar(azData[i]); 3573 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); 3574 fputs(i==nColumn-1?" |\n":" | ", p->out); 3575 } 3576 print_row_separator(p, nColumn, "|"); 3577 break; 3578 } 3579 case MODE_Box: { 3580 colSep = " " BOX_13 " "; 3581 rowSep = " " BOX_13 "\n"; 3582 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); 3583 utf8_printf(p->out, BOX_13 " "); 3584 for(i=0; i<nColumn; i++){ 3585 w = p->actualWidth[i]; 3586 n = strlenChar(azData[i]); 3587 utf8_printf(p->out, "%*s%s%*s%s", 3588 (w-n)/2, "", azData[i], (w-n+1)/2, "", 3589 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); 3590 } 3591 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); 3592 break; 3593 } 3594 } 3595 for(i=nColumn, j=0; i<nTotal; i++, j++){ 3596 if( j==0 && p->cMode!=MODE_Column ){ 3597 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); 3598 } 3599 z = azData[i]; 3600 if( z==0 ) z = p->nullValue; 3601 w = p->actualWidth[j]; 3602 if( p->colWidth[j]<0 ) w = -w; 3603 utf8_width_print(p->out, w, z); 3604 if( j==nColumn-1 ){ 3605 utf8_printf(p->out, "%s", rowSep); 3606 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ 3607 if( p->cMode==MODE_Table ){ 3608 print_row_separator(p, nColumn, "+"); 3609 }else if( p->cMode==MODE_Box ){ 3610 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); 3611 }else if( p->cMode==MODE_Column ){ 3612 raw_printf(p->out, "\n"); 3613 } 3614 } 3615 j = -1; 3616 if( seenInterrupt ) goto columnar_end; 3617 }else{ 3618 utf8_printf(p->out, "%s", colSep); 3619 } 3620 } 3621 if( p->cMode==MODE_Table ){ 3622 print_row_separator(p, nColumn, "+"); 3623 }else if( p->cMode==MODE_Box ){ 3624 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); 3625 } 3626columnar_end: 3627 if( seenInterrupt ){ 3628 utf8_printf(p->out, "Interrupt\n"); 3629 } 3630 nData = (nRow+1)*nColumn; 3631 for(i=0; i<nData; i++){ 3632 z = azData[i]; 3633 if( z!=zEmpty && z!=zShowNull ) free(azData[i]); 3634 } 3635 sqlite3_free(azData); 3636 sqlite3_free((void*)azNextLine); 3637 sqlite3_free(abRowDiv); 3638 if( azQuoted ){ 3639 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]); 3640 sqlite3_free(azQuoted); 3641 } 3642} 3643 3644/* 3645** Run a prepared statement 3646*/ 3647static void exec_prepared_stmt( 3648 ShellState *pArg, /* Pointer to ShellState */ 3649 sqlite3_stmt *pStmt /* Statment to run */ 3650){ 3651 int rc; 3652 sqlite3_uint64 nRow = 0; 3653 3654 if( pArg->cMode==MODE_Column 3655 || pArg->cMode==MODE_Table 3656 || pArg->cMode==MODE_Box 3657 || pArg->cMode==MODE_Markdown 3658 ){ 3659 exec_prepared_stmt_columnar(pArg, pStmt); 3660 return; 3661 } 3662 3663 /* perform the first step. this will tell us if we 3664 ** have a result set or not and how wide it is. 3665 */ 3666 rc = sqlite3_step(pStmt); 3667 /* if we have a result set... */ 3668 if( SQLITE_ROW == rc ){ 3669 /* allocate space for col name ptr, value ptr, and type */ 3670 int nCol = sqlite3_column_count(pStmt); 3671 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); 3672 if( !pData ){ 3673 shell_out_of_memory(); 3674 }else{ 3675 char **azCols = (char **)pData; /* Names of result columns */ 3676 char **azVals = &azCols[nCol]; /* Results */ 3677 int *aiTypes = (int *)&azVals[nCol]; /* Result types */ 3678 int i, x; 3679 assert(sizeof(int) <= sizeof(char *)); 3680 /* save off ptrs to column names */ 3681 for(i=0; i<nCol; i++){ 3682 azCols[i] = (char *)sqlite3_column_name(pStmt, i); 3683 } 3684 do{ 3685 nRow++; 3686 /* extract the data and data types */ 3687 for(i=0; i<nCol; i++){ 3688 aiTypes[i] = x = sqlite3_column_type(pStmt, i); 3689 if( x==SQLITE_BLOB 3690 && pArg 3691 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) 3692 ){ 3693 azVals[i] = ""; 3694 }else{ 3695 azVals[i] = (char*)sqlite3_column_text(pStmt, i); 3696 } 3697 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ 3698 rc = SQLITE_NOMEM; 3699 break; /* from for */ 3700 } 3701 } /* end for */ 3702 3703 /* if data and types extracted successfully... */ 3704 if( SQLITE_ROW == rc ){ 3705 /* call the supplied callback with the result row data */ 3706 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ 3707 rc = SQLITE_ABORT; 3708 }else{ 3709 rc = sqlite3_step(pStmt); 3710 } 3711 } 3712 } while( SQLITE_ROW == rc ); 3713 sqlite3_free(pData); 3714 if( pArg->cMode==MODE_Json ){ 3715 fputs("]\n", pArg->out); 3716 }else if( pArg->cMode==MODE_Count ){ 3717 char zBuf[200]; 3718 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n", 3719 nRow, nRow!=1 ? "s" : ""); 3720 printf("%s", zBuf); 3721 } 3722 } 3723 } 3724} 3725 3726#ifndef SQLITE_OMIT_VIRTUALTABLE 3727/* 3728** This function is called to process SQL if the previous shell command 3729** was ".expert". It passes the SQL in the second argument directly to 3730** the sqlite3expert object. 3731** 3732** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3733** code. In this case, (*pzErr) may be set to point to a buffer containing 3734** an English language error message. It is the responsibility of the 3735** caller to eventually free this buffer using sqlite3_free(). 3736*/ 3737static int expertHandleSQL( 3738 ShellState *pState, 3739 const char *zSql, 3740 char **pzErr 3741){ 3742 assert( pState->expert.pExpert ); 3743 assert( pzErr==0 || *pzErr==0 ); 3744 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); 3745} 3746 3747/* 3748** This function is called either to silently clean up the object 3749** created by the ".expert" command (if bCancel==1), or to generate a 3750** report from it and then clean it up (if bCancel==0). 3751** 3752** If successful, SQLITE_OK is returned. Otherwise, an SQLite error 3753** code. In this case, (*pzErr) may be set to point to a buffer containing 3754** an English language error message. It is the responsibility of the 3755** caller to eventually free this buffer using sqlite3_free(). 3756*/ 3757static int expertFinish( 3758 ShellState *pState, 3759 int bCancel, 3760 char **pzErr 3761){ 3762 int rc = SQLITE_OK; 3763 sqlite3expert *p = pState->expert.pExpert; 3764 assert( p ); 3765 assert( bCancel || pzErr==0 || *pzErr==0 ); 3766 if( bCancel==0 ){ 3767 FILE *out = pState->out; 3768 int bVerbose = pState->expert.bVerbose; 3769 3770 rc = sqlite3_expert_analyze(p, pzErr); 3771 if( rc==SQLITE_OK ){ 3772 int nQuery = sqlite3_expert_count(p); 3773 int i; 3774 3775 if( bVerbose ){ 3776 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); 3777 raw_printf(out, "-- Candidates -----------------------------\n"); 3778 raw_printf(out, "%s\n", zCand); 3779 } 3780 for(i=0; i<nQuery; i++){ 3781 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); 3782 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); 3783 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); 3784 if( zIdx==0 ) zIdx = "(no new indexes)\n"; 3785 if( bVerbose ){ 3786 raw_printf(out, "-- Query %d --------------------------------\n",i+1); 3787 raw_printf(out, "%s\n\n", zSql); 3788 } 3789 raw_printf(out, "%s\n", zIdx); 3790 raw_printf(out, "%s\n", zEQP); 3791 } 3792 } 3793 } 3794 sqlite3_expert_destroy(p); 3795 pState->expert.pExpert = 0; 3796 return rc; 3797} 3798 3799/* 3800** Implementation of ".expert" dot command. 3801*/ 3802static int expertDotCommand( 3803 ShellState *pState, /* Current shell tool state */ 3804 char **azArg, /* Array of arguments passed to dot command */ 3805 int nArg /* Number of entries in azArg[] */ 3806){ 3807 int rc = SQLITE_OK; 3808 char *zErr = 0; 3809 int i; 3810 int iSample = 0; 3811 3812 assert( pState->expert.pExpert==0 ); 3813 memset(&pState->expert, 0, sizeof(ExpertInfo)); 3814 3815 for(i=1; rc==SQLITE_OK && i<nArg; i++){ 3816 char *z = azArg[i]; 3817 int n; 3818 if( z[0]=='-' && z[1]=='-' ) z++; 3819 n = strlen30(z); 3820 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){ 3821 pState->expert.bVerbose = 1; 3822 } 3823 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){ 3824 if( i==(nArg-1) ){ 3825 raw_printf(stderr, "option requires an argument: %s\n", z); 3826 rc = SQLITE_ERROR; 3827 }else{ 3828 iSample = (int)integerValue(azArg[++i]); 3829 if( iSample<0 || iSample>100 ){ 3830 raw_printf(stderr, "value out of range: %s\n", azArg[i]); 3831 rc = SQLITE_ERROR; 3832 } 3833 } 3834 } 3835 else{ 3836 raw_printf(stderr, "unknown option: %s\n", z); 3837 rc = SQLITE_ERROR; 3838 } 3839 } 3840 3841 if( rc==SQLITE_OK ){ 3842 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); 3843 if( pState->expert.pExpert==0 ){ 3844 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory"); 3845 rc = SQLITE_ERROR; 3846 }else{ 3847 sqlite3_expert_config( 3848 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample 3849 ); 3850 } 3851 } 3852 sqlite3_free(zErr); 3853 3854 return rc; 3855} 3856#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 3857 3858/* 3859** Execute a statement or set of statements. Print 3860** any result rows/columns depending on the current mode 3861** set via the supplied callback. 3862** 3863** This is very similar to SQLite's built-in sqlite3_exec() 3864** function except it takes a slightly different callback 3865** and callback data argument. 3866*/ 3867static int shell_exec( 3868 ShellState *pArg, /* Pointer to ShellState */ 3869 const char *zSql, /* SQL to be evaluated */ 3870 char **pzErrMsg /* Error msg written here */ 3871){ 3872 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ 3873 int rc = SQLITE_OK; /* Return Code */ 3874 int rc2; 3875 const char *zLeftover; /* Tail of unprocessed SQL */ 3876 sqlite3 *db = pArg->db; 3877 3878 if( pzErrMsg ){ 3879 *pzErrMsg = NULL; 3880 } 3881 3882#ifndef SQLITE_OMIT_VIRTUALTABLE 3883 if( pArg->expert.pExpert ){ 3884 rc = expertHandleSQL(pArg, zSql, pzErrMsg); 3885 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); 3886 } 3887#endif 3888 3889 while( zSql[0] && (SQLITE_OK == rc) ){ 3890 static const char *zStmtSql; 3891 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); 3892 if( SQLITE_OK != rc ){ 3893 if( pzErrMsg ){ 3894 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql); 3895 } 3896 }else{ 3897 if( !pStmt ){ 3898 /* this happens for a comment or white-space */ 3899 zSql = zLeftover; 3900 while( IsSpace(zSql[0]) ) zSql++; 3901 continue; 3902 } 3903 zStmtSql = sqlite3_sql(pStmt); 3904 if( zStmtSql==0 ) zStmtSql = ""; 3905 while( IsSpace(zStmtSql[0]) ) zStmtSql++; 3906 3907 /* save off the prepared statment handle and reset row count */ 3908 if( pArg ){ 3909 pArg->pStmt = pStmt; 3910 pArg->cnt = 0; 3911 } 3912 3913 /* Show the EXPLAIN QUERY PLAN if .eqp is on */ 3914 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ 3915 sqlite3_stmt *pExplain; 3916 char *zEQP; 3917 int triggerEQP = 0; 3918 disable_debug_trace_modes(); 3919 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); 3920 if( pArg->autoEQP>=AUTOEQP_trigger ){ 3921 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); 3922 } 3923 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); 3924 shell_check_oom(zEQP); 3925 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3926 if( rc==SQLITE_OK ){ 3927 while( sqlite3_step(pExplain)==SQLITE_ROW ){ 3928 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); 3929 int iEqpId = sqlite3_column_int(pExplain, 0); 3930 int iParentId = sqlite3_column_int(pExplain, 1); 3931 if( zEQPLine==0 ) zEQPLine = ""; 3932 if( zEQPLine[0]=='-' ) eqp_render(pArg); 3933 eqp_append(pArg, iEqpId, iParentId, zEQPLine); 3934 } 3935 eqp_render(pArg); 3936 } 3937 sqlite3_finalize(pExplain); 3938 sqlite3_free(zEQP); 3939 if( pArg->autoEQP>=AUTOEQP_full ){ 3940 /* Also do an EXPLAIN for ".eqp full" mode */ 3941 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); 3942 shell_check_oom(zEQP); 3943 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 3944 if( rc==SQLITE_OK ){ 3945 pArg->cMode = MODE_Explain; 3946 explain_data_prepare(pArg, pExplain); 3947 exec_prepared_stmt(pArg, pExplain); 3948 explain_data_delete(pArg); 3949 } 3950 sqlite3_finalize(pExplain); 3951 sqlite3_free(zEQP); 3952 } 3953 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ 3954 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); 3955 /* Reprepare pStmt before reactiving trace modes */ 3956 sqlite3_finalize(pStmt); 3957 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 3958 if( pArg ) pArg->pStmt = pStmt; 3959 } 3960 restore_debug_trace_modes(); 3961 } 3962 3963 if( pArg ){ 3964 pArg->cMode = pArg->mode; 3965 if( pArg->autoExplain ){ 3966 if( sqlite3_stmt_isexplain(pStmt)==1 ){ 3967 pArg->cMode = MODE_Explain; 3968 } 3969 if( sqlite3_stmt_isexplain(pStmt)==2 ){ 3970 pArg->cMode = MODE_EQP; 3971 } 3972 } 3973 3974 /* If the shell is currently in ".explain" mode, gather the extra 3975 ** data required to add indents to the output.*/ 3976 if( pArg->cMode==MODE_Explain ){ 3977 explain_data_prepare(pArg, pStmt); 3978 } 3979 } 3980 3981 bind_prepared_stmt(pArg, pStmt); 3982 exec_prepared_stmt(pArg, pStmt); 3983 explain_data_delete(pArg); 3984 eqp_render(pArg); 3985 3986 /* print usage stats if stats on */ 3987 if( pArg && pArg->statsOn ){ 3988 display_stats(db, pArg, 0); 3989 } 3990 3991 /* print loop-counters if required */ 3992 if( pArg && pArg->scanstatsOn ){ 3993 display_scanstats(db, pArg); 3994 } 3995 3996 /* Finalize the statement just executed. If this fails, save a 3997 ** copy of the error message. Otherwise, set zSql to point to the 3998 ** next statement to execute. */ 3999 rc2 = sqlite3_finalize(pStmt); 4000 if( rc!=SQLITE_NOMEM ) rc = rc2; 4001 if( rc==SQLITE_OK ){ 4002 zSql = zLeftover; 4003 while( IsSpace(zSql[0]) ) zSql++; 4004 }else if( pzErrMsg ){ 4005 *pzErrMsg = save_err_msg(db, "stepping", rc, 0); 4006 } 4007 4008 /* clear saved stmt handle */ 4009 if( pArg ){ 4010 pArg->pStmt = NULL; 4011 } 4012 } 4013 } /* end while */ 4014 4015 return rc; 4016} 4017 4018/* 4019** Release memory previously allocated by tableColumnList(). 4020*/ 4021static void freeColumnList(char **azCol){ 4022 int i; 4023 for(i=1; azCol[i]; i++){ 4024 sqlite3_free(azCol[i]); 4025 } 4026 /* azCol[0] is a static string */ 4027 sqlite3_free(azCol); 4028} 4029 4030/* 4031** Return a list of pointers to strings which are the names of all 4032** columns in table zTab. The memory to hold the names is dynamically 4033** allocated and must be released by the caller using a subsequent call 4034** to freeColumnList(). 4035** 4036** The azCol[0] entry is usually NULL. However, if zTab contains a rowid 4037** value that needs to be preserved, then azCol[0] is filled in with the 4038** name of the rowid column. 4039** 4040** The first regular column in the table is azCol[1]. The list is terminated 4041** by an entry with azCol[i]==0. 4042*/ 4043static char **tableColumnList(ShellState *p, const char *zTab){ 4044 char **azCol = 0; 4045 sqlite3_stmt *pStmt; 4046 char *zSql; 4047 int nCol = 0; 4048 int nAlloc = 0; 4049 int nPK = 0; /* Number of PRIMARY KEY columns seen */ 4050 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ 4051 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); 4052 int rc; 4053 4054 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); 4055 shell_check_oom(zSql); 4056 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 4057 sqlite3_free(zSql); 4058 if( rc ) return 0; 4059 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 4060 if( nCol>=nAlloc-2 ){ 4061 nAlloc = nAlloc*2 + nCol + 10; 4062 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); 4063 shell_check_oom(azCol); 4064 } 4065 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); 4066 shell_check_oom(azCol[nCol]); 4067 if( sqlite3_column_int(pStmt, 5) ){ 4068 nPK++; 4069 if( nPK==1 4070 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), 4071 "INTEGER")==0 4072 ){ 4073 isIPK = 1; 4074 }else{ 4075 isIPK = 0; 4076 } 4077 } 4078 } 4079 sqlite3_finalize(pStmt); 4080 if( azCol==0 ) return 0; 4081 azCol[0] = 0; 4082 azCol[nCol+1] = 0; 4083 4084 /* The decision of whether or not a rowid really needs to be preserved 4085 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table 4086 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve 4087 ** rowids on tables where the rowid is inaccessible because there are other 4088 ** columns in the table named "rowid", "_rowid_", and "oid". 4089 */ 4090 if( preserveRowid && isIPK ){ 4091 /* If a single PRIMARY KEY column with type INTEGER was seen, then it 4092 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID 4093 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are 4094 ** ROWID aliases. To distinguish these cases, check to see if 4095 ** there is a "pk" entry in "PRAGMA index_list". There will be 4096 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. 4097 */ 4098 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" 4099 " WHERE origin='pk'", zTab); 4100 shell_check_oom(zSql); 4101 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 4102 sqlite3_free(zSql); 4103 if( rc ){ 4104 freeColumnList(azCol); 4105 return 0; 4106 } 4107 rc = sqlite3_step(pStmt); 4108 sqlite3_finalize(pStmt); 4109 preserveRowid = rc==SQLITE_ROW; 4110 } 4111 if( preserveRowid ){ 4112 /* Only preserve the rowid if we can find a name to use for the 4113 ** rowid */ 4114 static char *azRowid[] = { "rowid", "_rowid_", "oid" }; 4115 int i, j; 4116 for(j=0; j<3; j++){ 4117 for(i=1; i<=nCol; i++){ 4118 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; 4119 } 4120 if( i>nCol ){ 4121 /* At this point, we know that azRowid[j] is not the name of any 4122 ** ordinary column in the table. Verify that azRowid[j] is a valid 4123 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID 4124 ** tables will fail this last check */ 4125 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); 4126 if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; 4127 break; 4128 } 4129 } 4130 } 4131 return azCol; 4132} 4133 4134/* 4135** Toggle the reverse_unordered_selects setting. 4136*/ 4137static void toggleSelectOrder(sqlite3 *db){ 4138 sqlite3_stmt *pStmt = 0; 4139 int iSetting = 0; 4140 char zStmt[100]; 4141 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); 4142 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 4143 iSetting = sqlite3_column_int(pStmt, 0); 4144 } 4145 sqlite3_finalize(pStmt); 4146 sqlite3_snprintf(sizeof(zStmt), zStmt, 4147 "PRAGMA reverse_unordered_selects(%d)", !iSetting); 4148 sqlite3_exec(db, zStmt, 0, 0, 0); 4149} 4150 4151/* 4152** This is a different callback routine used for dumping the database. 4153** Each row received by this callback consists of a table name, 4154** the table type ("index" or "table") and SQL to create the table. 4155** This routine should print text sufficient to recreate the table. 4156*/ 4157static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ 4158 int rc; 4159 const char *zTable; 4160 const char *zType; 4161 const char *zSql; 4162 ShellState *p = (ShellState *)pArg; 4163 int dataOnly; 4164 int noSys; 4165 4166 UNUSED_PARAMETER(azNotUsed); 4167 if( nArg!=3 || azArg==0 ) return 0; 4168 zTable = azArg[0]; 4169 zType = azArg[1]; 4170 zSql = azArg[2]; 4171 if( zTable==0 ) return 0; 4172 if( zType==0 ) return 0; 4173 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; 4174 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; 4175 4176 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){ 4177 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); 4178 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){ 4179 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 4180 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){ 4181 return 0; 4182 }else if( dataOnly ){ 4183 /* no-op */ 4184 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ 4185 char *zIns; 4186 if( !p->writableSchema ){ 4187 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); 4188 p->writableSchema = 1; 4189 } 4190 zIns = sqlite3_mprintf( 4191 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" 4192 "VALUES('table','%q','%q',0,'%q');", 4193 zTable, zTable, zSql); 4194 shell_check_oom(zIns); 4195 utf8_printf(p->out, "%s\n", zIns); 4196 sqlite3_free(zIns); 4197 return 0; 4198 }else{ 4199 printSchemaLine(p->out, zSql, ";\n"); 4200 } 4201 4202 if( cli_strcmp(zType, "table")==0 ){ 4203 ShellText sSelect; 4204 ShellText sTable; 4205 char **azCol; 4206 int i; 4207 char *savedDestTable; 4208 int savedMode; 4209 4210 azCol = tableColumnList(p, zTable); 4211 if( azCol==0 ){ 4212 p->nErr++; 4213 return 0; 4214 } 4215 4216 /* Always quote the table name, even if it appears to be pure ascii, 4217 ** in case it is a keyword. Ex: INSERT INTO "table" ... */ 4218 initText(&sTable); 4219 appendText(&sTable, zTable, quoteChar(zTable)); 4220 /* If preserving the rowid, add a column list after the table name. 4221 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" 4222 ** instead of the usual "INSERT INTO tab VALUES(...)". 4223 */ 4224 if( azCol[0] ){ 4225 appendText(&sTable, "(", 0); 4226 appendText(&sTable, azCol[0], 0); 4227 for(i=1; azCol[i]; i++){ 4228 appendText(&sTable, ",", 0); 4229 appendText(&sTable, azCol[i], quoteChar(azCol[i])); 4230 } 4231 appendText(&sTable, ")", 0); 4232 } 4233 4234 /* Build an appropriate SELECT statement */ 4235 initText(&sSelect); 4236 appendText(&sSelect, "SELECT ", 0); 4237 if( azCol[0] ){ 4238 appendText(&sSelect, azCol[0], 0); 4239 appendText(&sSelect, ",", 0); 4240 } 4241 for(i=1; azCol[i]; i++){ 4242 appendText(&sSelect, azCol[i], quoteChar(azCol[i])); 4243 if( azCol[i+1] ){ 4244 appendText(&sSelect, ",", 0); 4245 } 4246 } 4247 freeColumnList(azCol); 4248 appendText(&sSelect, " FROM ", 0); 4249 appendText(&sSelect, zTable, quoteChar(zTable)); 4250 4251 savedDestTable = p->zDestTable; 4252 savedMode = p->mode; 4253 p->zDestTable = sTable.z; 4254 p->mode = p->cMode = MODE_Insert; 4255 rc = shell_exec(p, sSelect.z, 0); 4256 if( (rc&0xff)==SQLITE_CORRUPT ){ 4257 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 4258 toggleSelectOrder(p->db); 4259 shell_exec(p, sSelect.z, 0); 4260 toggleSelectOrder(p->db); 4261 } 4262 p->zDestTable = savedDestTable; 4263 p->mode = savedMode; 4264 freeText(&sTable); 4265 freeText(&sSelect); 4266 if( rc ) p->nErr++; 4267 } 4268 return 0; 4269} 4270 4271/* 4272** Run zQuery. Use dump_callback() as the callback routine so that 4273** the contents of the query are output as SQL statements. 4274** 4275** If we get a SQLITE_CORRUPT error, rerun the query after appending 4276** "ORDER BY rowid DESC" to the end. 4277*/ 4278static int run_schema_dump_query( 4279 ShellState *p, 4280 const char *zQuery 4281){ 4282 int rc; 4283 char *zErr = 0; 4284 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); 4285 if( rc==SQLITE_CORRUPT ){ 4286 char *zQ2; 4287 int len = strlen30(zQuery); 4288 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); 4289 if( zErr ){ 4290 utf8_printf(p->out, "/****** %s ******/\n", zErr); 4291 sqlite3_free(zErr); 4292 zErr = 0; 4293 } 4294 zQ2 = malloc( len+100 ); 4295 if( zQ2==0 ) return rc; 4296 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); 4297 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); 4298 if( rc ){ 4299 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); 4300 }else{ 4301 rc = SQLITE_CORRUPT; 4302 } 4303 sqlite3_free(zErr); 4304 free(zQ2); 4305 } 4306 return rc; 4307} 4308 4309/* 4310** Text of help messages. 4311** 4312** The help text for each individual command begins with a line that starts 4313** with ".". Subsequent lines are supplemental information. 4314** 4315** There must be two or more spaces between the end of the command and the 4316** start of the description of what that command does. 4317*/ 4318static const char *(azHelp[]) = { 4319#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \ 4320 && !defined(SQLITE_SHELL_FIDDLE) 4321 ".archive ... Manage SQL archives", 4322 " Each command must have exactly one of the following options:", 4323 " -c, --create Create a new archive", 4324 " -u, --update Add or update files with changed mtime", 4325 " -i, --insert Like -u but always add even if unchanged", 4326 " -r, --remove Remove files from archive", 4327 " -t, --list List contents of archive", 4328 " -x, --extract Extract files from archive", 4329 " Optional arguments:", 4330 " -v, --verbose Print each filename as it is processed", 4331 " -f FILE, --file FILE Use archive FILE (default is current db)", 4332 " -a FILE, --append FILE Open FILE using the apndvfs VFS", 4333 " -C DIR, --directory DIR Read/extract files from directory DIR", 4334 " -g, --glob Use glob matching for names in archive", 4335 " -n, --dryrun Show the SQL that would have occurred", 4336 " Examples:", 4337 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", 4338 " .ar -tf ARCHIVE # List members of ARCHIVE", 4339 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", 4340 " See also:", 4341 " http://sqlite.org/cli.html#sqlite_archive_support", 4342#endif 4343#ifndef SQLITE_OMIT_AUTHORIZATION 4344 ".auth ON|OFF Show authorizer callbacks", 4345#endif 4346#ifndef SQLITE_SHELL_FIDDLE 4347 ".backup ?DB? FILE Backup DB (default \"main\") to FILE", 4348 " Options:", 4349 " --append Use the appendvfs", 4350 " --async Write to FILE without journal and fsync()", 4351#endif 4352 ".bail on|off Stop after hitting an error. Default OFF", 4353 ".binary on|off Turn binary output on or off. Default OFF", 4354#ifndef SQLITE_SHELL_FIDDLE 4355 ".cd DIRECTORY Change the working directory to DIRECTORY", 4356#endif 4357 ".changes on|off Show number of rows changed by SQL", 4358#ifndef SQLITE_SHELL_FIDDLE 4359 ".check GLOB Fail if output since .testcase does not match", 4360 ".clone NEWDB Clone data into NEWDB from the existing database", 4361#endif 4362 ".connection [close] [#] Open or close an auxiliary database connection", 4363 ".databases List names and files of attached databases", 4364 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", 4365#if SQLITE_SHELL_HAVE_RECOVER 4366 ".dbinfo ?DB? Show status information about the database", 4367#endif 4368 ".dump ?OBJECTS? Render database content as SQL", 4369 " Options:", 4370 " --data-only Output only INSERT statements", 4371 " --newlines Allow unescaped newline characters in output", 4372 " --nosys Omit system tables (ex: \"sqlite_stat1\")", 4373 " --preserve-rowids Include ROWID values in the output", 4374 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump", 4375 " Additional LIKE patterns can be given in subsequent arguments", 4376 ".echo on|off Turn command echo on or off", 4377 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", 4378 " Other Modes:", 4379#ifdef SQLITE_DEBUG 4380 " test Show raw EXPLAIN QUERY PLAN output", 4381 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", 4382#endif 4383 " trigger Like \"full\" but also show trigger bytecode", 4384#ifndef SQLITE_SHELL_FIDDLE 4385 ".excel Display the output of next command in spreadsheet", 4386 " --bom Put a UTF8 byte-order mark on intermediate file", 4387#endif 4388#ifndef SQLITE_SHELL_FIDDLE 4389 ".exit ?CODE? Exit this program with return-code CODE", 4390#endif 4391 ".expert EXPERIMENTAL. Suggest indexes for queries", 4392 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", 4393 ".filectrl CMD ... Run various sqlite3_file_control() operations", 4394 " --schema SCHEMA Use SCHEMA instead of \"main\"", 4395 " --help Show CMD details", 4396 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", 4397 ".headers on|off Turn display of headers on or off", 4398 ".help ?-all? ?PATTERN? Show help text for PATTERN", 4399#ifndef SQLITE_SHELL_FIDDLE 4400 ".import FILE TABLE Import data from FILE into TABLE", 4401 " Options:", 4402 " --ascii Use \\037 and \\036 as column and row separators", 4403 " --csv Use , and \\n as column and row separators", 4404 " --skip N Skip the first N rows of input", 4405 " --schema S Target table to be S.TABLE", 4406 " -v \"Verbose\" - increase auxiliary output", 4407 " Notes:", 4408 " * If TABLE does not exist, it is created. The first row of input", 4409 " determines the column names.", 4410 " * If neither --csv or --ascii are used, the input mode is derived", 4411 " from the \".mode\" output mode", 4412 " * If FILE begins with \"|\" then it is a command that generates the", 4413 " input text.", 4414#endif 4415#ifndef SQLITE_OMIT_TEST_CONTROL 4416 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", 4417#endif 4418 ".indexes ?TABLE? Show names of indexes", 4419 " If TABLE is specified, only show indexes for", 4420 " tables matching TABLE using the LIKE operator.", 4421#ifdef SQLITE_ENABLE_IOTRACE 4422 ".iotrace FILE Enable I/O diagnostic logging to FILE", 4423#endif 4424 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", 4425 ".lint OPTIONS Report potential schema issues.", 4426 " Options:", 4427 " fkey-indexes Find missing foreign key indexes", 4428#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) 4429 ".load FILE ?ENTRY? Load an extension library", 4430#endif 4431#ifndef SQLITE_SHELL_FIDDLE 4432 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", 4433#endif 4434 ".mode MODE ?OPTIONS? Set output mode", 4435 " MODE is one of:", 4436 " ascii Columns/rows delimited by 0x1F and 0x1E", 4437 " box Tables using unicode box-drawing characters", 4438 " csv Comma-separated values", 4439 " column Output in columns. (See .width)", 4440 " html HTML <table> code", 4441 " insert SQL insert statements for TABLE", 4442 " json Results in a JSON array", 4443 " line One value per line", 4444 " list Values delimited by \"|\"", 4445 " markdown Markdown table format", 4446 " qbox Shorthand for \"box --wrap 60 --quote\"", 4447 " quote Escape answers as for SQL", 4448 " table ASCII-art table", 4449 " tabs Tab-separated values", 4450 " tcl TCL list elements", 4451 " OPTIONS: (for columnar modes or insert mode):", 4452 " --wrap N Wrap output lines to no longer than N characters", 4453 " --wordwrap B Wrap or not at word boundaries per B (on/off)", 4454 " --ww Shorthand for \"--wordwrap 1\"", 4455 " --quote Quote output text as SQL literals", 4456 " --noquote Do not quote output text", 4457 " TABLE The name of SQL table used for \"insert\" mode", 4458#ifndef SQLITE_SHELL_FIDDLE 4459 ".nonce STRING Suspend safe mode for one command if nonce matches", 4460#endif 4461 ".nullvalue STRING Use STRING in place of NULL values", 4462#ifndef SQLITE_SHELL_FIDDLE 4463 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", 4464 " If FILE begins with '|' then open as a pipe", 4465 " --bom Put a UTF8 byte-order mark at the beginning", 4466 " -e Send output to the system text editor", 4467 " -x Send output as CSV to a spreadsheet (same as \".excel\")", 4468 /* Note that .open is (partially) available in WASM builds but is 4469 ** currently only intended to be used by the fiddle tool, not 4470 ** end users, so is "undocumented." */ 4471 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", 4472 " Options:", 4473 " --append Use appendvfs to append database to the end of FILE", 4474#endif 4475#ifndef SQLITE_OMIT_DESERIALIZE 4476 " --deserialize Load into memory using sqlite3_deserialize()", 4477 " --hexdb Load the output of \"dbtotxt\" as an in-memory db", 4478 " --maxsize N Maximum size for --hexdb or --deserialized database", 4479#endif 4480 " --new Initialize FILE to an empty database", 4481 " --nofollow Do not follow symbolic links", 4482 " --readonly Open FILE readonly", 4483 " --zip FILE is a ZIP archive", 4484#ifndef SQLITE_SHELL_FIDDLE 4485 ".output ?FILE? Send output to FILE or stdout if FILE is omitted", 4486 " If FILE begins with '|' then open it as a pipe.", 4487 " Options:", 4488 " --bom Prefix output with a UTF8 byte-order mark", 4489 " -e Send output to the system text editor", 4490 " -x Send output as CSV to a spreadsheet", 4491#endif 4492 ".parameter CMD ... Manage SQL parameter bindings", 4493 " clear Erase all bindings", 4494 " init Initialize the TEMP table that holds bindings", 4495 " list List the current parameter bindings", 4496 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", 4497 " PARAMETER should start with one of: $ : @ ?", 4498 " unset PARAMETER Remove PARAMETER from the binding table", 4499 ".print STRING... Print literal STRING", 4500#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 4501 ".progress N Invoke progress handler after every N opcodes", 4502 " --limit N Interrupt after N progress callbacks", 4503 " --once Do no more than one progress interrupt", 4504 " --quiet|-q No output except at interrupts", 4505 " --reset Reset the count for each input and interrupt", 4506#endif 4507 ".prompt MAIN CONTINUE Replace the standard prompts", 4508#ifndef SQLITE_SHELL_FIDDLE 4509 ".quit Exit this program", 4510 ".read FILE Read input from FILE or command output", 4511 " If FILE begins with \"|\", it is a command that generates the input.", 4512#endif 4513#if SQLITE_SHELL_HAVE_RECOVER 4514 ".recover Recover as much data as possible from corrupt db.", 4515 " --freelist-corrupt Assume the freelist is corrupt", 4516 " --recovery-db NAME Store recovery metadata in database file NAME", 4517 " --lost-and-found TABLE Alternative name for the lost-and-found table", 4518 " --no-rowids Do not attempt to recover rowid values", 4519 " that are not also INTEGER PRIMARY KEYs", 4520#endif 4521#ifndef SQLITE_SHELL_FIDDLE 4522 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", 4523 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", 4524#endif 4525 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", 4526 ".schema ?PATTERN? Show the CREATE statements matching PATTERN", 4527 " Options:", 4528 " --indent Try to pretty-print the schema", 4529 " --nosys Omit objects whose names start with \"sqlite_\"", 4530 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", 4531 " Options:", 4532 " --init Create a new SELFTEST table", 4533 " -v Verbose output", 4534 ".separator COL ?ROW? Change the column and row separators", 4535#if defined(SQLITE_ENABLE_SESSION) 4536 ".session ?NAME? CMD ... Create or control sessions", 4537 " Subcommands:", 4538 " attach TABLE Attach TABLE", 4539 " changeset FILE Write a changeset into FILE", 4540 " close Close one session", 4541 " enable ?BOOLEAN? Set or query the enable bit", 4542 " filter GLOB... Reject tables matching GLOBs", 4543 " indirect ?BOOLEAN? Mark or query the indirect status", 4544 " isempty Query whether the session is empty", 4545 " list List currently open session names", 4546 " open DB NAME Open a new session on DB", 4547 " patchset FILE Write a patchset into FILE", 4548 " If ?NAME? is omitted, the first defined session is used.", 4549#endif 4550 ".sha3sum ... Compute a SHA3 hash of database content", 4551 " Options:", 4552 " --schema Also hash the sqlite_schema table", 4553 " --sha3-224 Use the sha3-224 algorithm", 4554 " --sha3-256 Use the sha3-256 algorithm (default)", 4555 " --sha3-384 Use the sha3-384 algorithm", 4556 " --sha3-512 Use the sha3-512 algorithm", 4557 " Any other argument is a LIKE pattern for tables to hash", 4558#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 4559 ".shell CMD ARGS... Run CMD ARGS... in a system shell", 4560#endif 4561 ".show Show the current values for various settings", 4562 ".stats ?ARG? Show stats or turn stats on or off", 4563 " off Turn off automatic stat display", 4564 " on Turn on automatic stat display", 4565 " stmt Show statement stats", 4566 " vmstep Show the virtual machine step count only", 4567#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 4568 ".system CMD ARGS... Run CMD ARGS... in a system shell", 4569#endif 4570 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", 4571#ifndef SQLITE_SHELL_FIDDLE 4572 ".testcase NAME Begin redirecting output to 'testcase-out.txt'", 4573#endif 4574 ".testctrl CMD ... Run various sqlite3_test_control() operations", 4575 " Run \".testctrl\" with no arguments for details", 4576 ".timeout MS Try opening locked tables for MS milliseconds", 4577 ".timer on|off Turn SQL timer on or off", 4578#ifndef SQLITE_OMIT_TRACE 4579 ".trace ?OPTIONS? Output each SQL statement as it is run", 4580 " FILE Send output to FILE", 4581 " stdout Send output to stdout", 4582 " stderr Send output to stderr", 4583 " off Disable tracing", 4584 " --expanded Expand query parameters", 4585#ifdef SQLITE_ENABLE_NORMALIZE 4586 " --normalized Normal the SQL statements", 4587#endif 4588 " --plain Show SQL as it is input", 4589 " --stmt Trace statement execution (SQLITE_TRACE_STMT)", 4590 " --profile Profile statements (SQLITE_TRACE_PROFILE)", 4591 " --row Trace each row (SQLITE_TRACE_ROW)", 4592 " --close Trace connection close (SQLITE_TRACE_CLOSE)", 4593#endif /* SQLITE_OMIT_TRACE */ 4594#ifdef SQLITE_DEBUG 4595 ".unmodule NAME ... Unregister virtual table modules", 4596 " --allexcept Unregister everything except those named", 4597#endif 4598 ".vfsinfo ?AUX? Information about the top-level VFS", 4599 ".vfslist List all available VFSes", 4600 ".vfsname ?AUX? Print the name of the VFS stack", 4601 ".width NUM1 NUM2 ... Set minimum column widths for columnar output", 4602 " Negative values right-justify", 4603}; 4604 4605/* 4606** Output help text. 4607** 4608** zPattern describes the set of commands for which help text is provided. 4609** If zPattern is NULL, then show all commands, but only give a one-line 4610** description of each. 4611** 4612** Return the number of matches. 4613*/ 4614static int showHelp(FILE *out, const char *zPattern){ 4615 int i = 0; 4616 int j = 0; 4617 int n = 0; 4618 char *zPat; 4619 if( zPattern==0 4620 || zPattern[0]=='0' 4621 || cli_strcmp(zPattern,"-a")==0 4622 || cli_strcmp(zPattern,"-all")==0 4623 || cli_strcmp(zPattern,"--all")==0 4624 ){ 4625 /* Show all commands, but only one line per command */ 4626 if( zPattern==0 ) zPattern = ""; 4627 for(i=0; i<ArraySize(azHelp); i++){ 4628 if( azHelp[i][0]=='.' || zPattern[0] ){ 4629 utf8_printf(out, "%s\n", azHelp[i]); 4630 n++; 4631 } 4632 } 4633 }else{ 4634 /* Look for commands that for which zPattern is an exact prefix */ 4635 zPat = sqlite3_mprintf(".%s*", zPattern); 4636 shell_check_oom(zPat); 4637 for(i=0; i<ArraySize(azHelp); i++){ 4638 if( sqlite3_strglob(zPat, azHelp[i])==0 ){ 4639 utf8_printf(out, "%s\n", azHelp[i]); 4640 j = i+1; 4641 n++; 4642 } 4643 } 4644 sqlite3_free(zPat); 4645 if( n ){ 4646 if( n==1 ){ 4647 /* when zPattern is a prefix of exactly one command, then include the 4648 ** details of that command, which should begin at offset j */ 4649 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ 4650 utf8_printf(out, "%s\n", azHelp[j]); 4651 j++; 4652 } 4653 } 4654 return n; 4655 } 4656 /* Look for commands that contain zPattern anywhere. Show the complete 4657 ** text of all commands that match. */ 4658 zPat = sqlite3_mprintf("%%%s%%", zPattern); 4659 shell_check_oom(zPat); 4660 for(i=0; i<ArraySize(azHelp); i++){ 4661 if( azHelp[i][0]=='.' ) j = i; 4662 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ 4663 utf8_printf(out, "%s\n", azHelp[j]); 4664 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ 4665 j++; 4666 utf8_printf(out, "%s\n", azHelp[j]); 4667 } 4668 i = j; 4669 n++; 4670 } 4671 } 4672 sqlite3_free(zPat); 4673 } 4674 return n; 4675} 4676 4677/* Forward reference */ 4678static int process_input(ShellState *p); 4679 4680/* 4681** Read the content of file zName into memory obtained from sqlite3_malloc64() 4682** and return a pointer to the buffer. The caller is responsible for freeing 4683** the memory. 4684** 4685** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes 4686** read. 4687** 4688** For convenience, a nul-terminator byte is always appended to the data read 4689** from the file before the buffer is returned. This byte is not included in 4690** the final value of (*pnByte), if applicable. 4691** 4692** NULL is returned if any error is encountered. The final value of *pnByte 4693** is undefined in this case. 4694*/ 4695static char *readFile(const char *zName, int *pnByte){ 4696 FILE *in = fopen(zName, "rb"); 4697 long nIn; 4698 size_t nRead; 4699 char *pBuf; 4700 if( in==0 ) return 0; 4701 fseek(in, 0, SEEK_END); 4702 nIn = ftell(in); 4703 rewind(in); 4704 pBuf = sqlite3_malloc64( nIn+1 ); 4705 if( pBuf==0 ){ fclose(in); return 0; } 4706 nRead = fread(pBuf, nIn, 1, in); 4707 fclose(in); 4708 if( nRead!=1 ){ 4709 sqlite3_free(pBuf); 4710 return 0; 4711 } 4712 pBuf[nIn] = 0; 4713 if( pnByte ) *pnByte = nIn; 4714 return pBuf; 4715} 4716 4717#if defined(SQLITE_ENABLE_SESSION) 4718/* 4719** Close a single OpenSession object and release all of its associated 4720** resources. 4721*/ 4722static void session_close(OpenSession *pSession){ 4723 int i; 4724 sqlite3session_delete(pSession->p); 4725 sqlite3_free(pSession->zName); 4726 for(i=0; i<pSession->nFilter; i++){ 4727 sqlite3_free(pSession->azFilter[i]); 4728 } 4729 sqlite3_free(pSession->azFilter); 4730 memset(pSession, 0, sizeof(OpenSession)); 4731} 4732#endif 4733 4734/* 4735** Close all OpenSession objects and release all associated resources. 4736*/ 4737#if defined(SQLITE_ENABLE_SESSION) 4738static void session_close_all(ShellState *p, int i){ 4739 int j; 4740 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; 4741 for(j=0; j<pAuxDb->nSession; j++){ 4742 session_close(&pAuxDb->aSession[j]); 4743 } 4744 pAuxDb->nSession = 0; 4745} 4746#else 4747# define session_close_all(X,Y) 4748#endif 4749 4750/* 4751** Implementation of the xFilter function for an open session. Omit 4752** any tables named by ".session filter" but let all other table through. 4753*/ 4754#if defined(SQLITE_ENABLE_SESSION) 4755static int session_filter(void *pCtx, const char *zTab){ 4756 OpenSession *pSession = (OpenSession*)pCtx; 4757 int i; 4758 for(i=0; i<pSession->nFilter; i++){ 4759 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; 4760 } 4761 return 1; 4762} 4763#endif 4764 4765/* 4766** Try to deduce the type of file for zName based on its content. Return 4767** one of the SHELL_OPEN_* constants. 4768** 4769** If the file does not exist or is empty but its name looks like a ZIP 4770** archive and the dfltZip flag is true, then assume it is a ZIP archive. 4771** Otherwise, assume an ordinary database regardless of the filename if 4772** the type cannot be determined from content. 4773*/ 4774int deduceDatabaseType(const char *zName, int dfltZip){ 4775 FILE *f = fopen(zName, "rb"); 4776 size_t n; 4777 int rc = SHELL_OPEN_UNSPEC; 4778 char zBuf[100]; 4779 if( f==0 ){ 4780 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4781 return SHELL_OPEN_ZIPFILE; 4782 }else{ 4783 return SHELL_OPEN_NORMAL; 4784 } 4785 } 4786 n = fread(zBuf, 16, 1, f); 4787 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ 4788 fclose(f); 4789 return SHELL_OPEN_NORMAL; 4790 } 4791 fseek(f, -25, SEEK_END); 4792 n = fread(zBuf, 25, 1, f); 4793 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ 4794 rc = SHELL_OPEN_APPENDVFS; 4795 }else{ 4796 fseek(f, -22, SEEK_END); 4797 n = fread(zBuf, 22, 1, f); 4798 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 4799 && zBuf[3]==0x06 ){ 4800 rc = SHELL_OPEN_ZIPFILE; 4801 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ 4802 rc = SHELL_OPEN_ZIPFILE; 4803 } 4804 } 4805 fclose(f); 4806 return rc; 4807} 4808 4809#ifndef SQLITE_OMIT_DESERIALIZE 4810/* 4811** Reconstruct an in-memory database using the output from the "dbtotxt" 4812** program. Read content from the file in p->aAuxDb[].zDbFilename. 4813** If p->aAuxDb[].zDbFilename is 0, then read from standard input. 4814*/ 4815static unsigned char *readHexDb(ShellState *p, int *pnData){ 4816 unsigned char *a = 0; 4817 int nLine; 4818 int n = 0; 4819 int pgsz = 0; 4820 int iOffset = 0; 4821 int j, k; 4822 int rc; 4823 FILE *in; 4824 const char *zDbFilename = p->pAuxDb->zDbFilename; 4825 unsigned int x[16]; 4826 char zLine[1000]; 4827 if( zDbFilename ){ 4828 in = fopen(zDbFilename, "r"); 4829 if( in==0 ){ 4830 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename); 4831 return 0; 4832 } 4833 nLine = 0; 4834 }else{ 4835 in = p->in; 4836 nLine = p->lineno; 4837 if( in==0 ) in = stdin; 4838 } 4839 *pnData = 0; 4840 nLine++; 4841 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; 4842 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); 4843 if( rc!=2 ) goto readHexDb_error; 4844 if( n<0 ) goto readHexDb_error; 4845 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; 4846 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ 4847 a = sqlite3_malloc( n ? n : 1 ); 4848 shell_check_oom(a); 4849 memset(a, 0, n); 4850 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ 4851 utf8_printf(stderr, "invalid pagesize\n"); 4852 goto readHexDb_error; 4853 } 4854 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ 4855 rc = sscanf(zLine, "| page %d offset %d", &j, &k); 4856 if( rc==2 ){ 4857 iOffset = k; 4858 continue; 4859 } 4860 if( cli_strncmp(zLine, "| end ", 6)==0 ){ 4861 break; 4862 } 4863 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 4864 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], 4865 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); 4866 if( rc==17 ){ 4867 k = iOffset+j; 4868 if( k+16<=n && k>=0 ){ 4869 int ii; 4870 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; 4871 } 4872 } 4873 } 4874 *pnData = n; 4875 if( in!=p->in ){ 4876 fclose(in); 4877 }else{ 4878 p->lineno = nLine; 4879 } 4880 return a; 4881 4882readHexDb_error: 4883 if( in!=p->in ){ 4884 fclose(in); 4885 }else{ 4886 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ 4887 nLine++; 4888 if(cli_strncmp(zLine, "| end ", 6)==0 ) break; 4889 } 4890 p->lineno = nLine; 4891 } 4892 sqlite3_free(a); 4893 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); 4894 return 0; 4895} 4896#endif /* SQLITE_OMIT_DESERIALIZE */ 4897 4898/* 4899** Scalar function "shell_int32". The first argument to this function 4900** must be a blob. The second a non-negative integer. This function 4901** reads and returns a 32-bit big-endian integer from byte 4902** offset (4*<arg2>) of the blob. 4903*/ 4904static void shellInt32( 4905 sqlite3_context *context, 4906 int argc, 4907 sqlite3_value **argv 4908){ 4909 const unsigned char *pBlob; 4910 int nBlob; 4911 int iInt; 4912 4913 UNUSED_PARAMETER(argc); 4914 nBlob = sqlite3_value_bytes(argv[0]); 4915 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); 4916 iInt = sqlite3_value_int(argv[1]); 4917 4918 if( iInt>=0 && (iInt+1)*4<=nBlob ){ 4919 const unsigned char *a = &pBlob[iInt*4]; 4920 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) 4921 + ((sqlite3_int64)a[1]<<16) 4922 + ((sqlite3_int64)a[2]<< 8) 4923 + ((sqlite3_int64)a[3]<< 0); 4924 sqlite3_result_int64(context, iVal); 4925 } 4926} 4927 4928/* 4929** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, 4930** using "..." with internal double-quote characters doubled. 4931*/ 4932static void shellIdQuote( 4933 sqlite3_context *context, 4934 int argc, 4935 sqlite3_value **argv 4936){ 4937 const char *zName = (const char*)sqlite3_value_text(argv[0]); 4938 UNUSED_PARAMETER(argc); 4939 if( zName ){ 4940 char *z = sqlite3_mprintf("\"%w\"", zName); 4941 sqlite3_result_text(context, z, -1, sqlite3_free); 4942 } 4943} 4944 4945/* 4946** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X. 4947*/ 4948static void shellUSleepFunc( 4949 sqlite3_context *context, 4950 int argcUnused, 4951 sqlite3_value **argv 4952){ 4953 int sleep = sqlite3_value_int(argv[0]); 4954 (void)argcUnused; 4955 sqlite3_sleep(sleep/1000); 4956 sqlite3_result_int(context, sleep); 4957} 4958 4959/* 4960** Scalar function "shell_escape_crnl" used by the .recover command. 4961** The argument passed to this function is the output of built-in 4962** function quote(). If the first character of the input is "'", 4963** indicating that the value passed to quote() was a text value, 4964** then this function searches the input for "\n" and "\r" characters 4965** and adds a wrapper similar to the following: 4966** 4967** replace(replace(<input>, '\n', char(10), '\r', char(13)); 4968** 4969** Or, if the first character of the input is not "'", then a copy 4970** of the input is returned. 4971*/ 4972static void shellEscapeCrnl( 4973 sqlite3_context *context, 4974 int argc, 4975 sqlite3_value **argv 4976){ 4977 const char *zText = (const char*)sqlite3_value_text(argv[0]); 4978 UNUSED_PARAMETER(argc); 4979 if( zText && zText[0]=='\'' ){ 4980 i64 nText = sqlite3_value_bytes(argv[0]); 4981 i64 i; 4982 char zBuf1[20]; 4983 char zBuf2[20]; 4984 const char *zNL = 0; 4985 const char *zCR = 0; 4986 i64 nCR = 0; 4987 i64 nNL = 0; 4988 4989 for(i=0; zText[i]; i++){ 4990 if( zNL==0 && zText[i]=='\n' ){ 4991 zNL = unused_string(zText, "\\n", "\\012", zBuf1); 4992 nNL = strlen(zNL); 4993 } 4994 if( zCR==0 && zText[i]=='\r' ){ 4995 zCR = unused_string(zText, "\\r", "\\015", zBuf2); 4996 nCR = strlen(zCR); 4997 } 4998 } 4999 5000 if( zNL || zCR ){ 5001 i64 iOut = 0; 5002 i64 nMax = (nNL > nCR) ? nNL : nCR; 5003 i64 nAlloc = nMax * nText + (nMax+64)*2; 5004 char *zOut = (char*)sqlite3_malloc64(nAlloc); 5005 if( zOut==0 ){ 5006 sqlite3_result_error_nomem(context); 5007 return; 5008 } 5009 5010 if( zNL && zCR ){ 5011 memcpy(&zOut[iOut], "replace(replace(", 16); 5012 iOut += 16; 5013 }else{ 5014 memcpy(&zOut[iOut], "replace(", 8); 5015 iOut += 8; 5016 } 5017 for(i=0; zText[i]; i++){ 5018 if( zText[i]=='\n' ){ 5019 memcpy(&zOut[iOut], zNL, nNL); 5020 iOut += nNL; 5021 }else if( zText[i]=='\r' ){ 5022 memcpy(&zOut[iOut], zCR, nCR); 5023 iOut += nCR; 5024 }else{ 5025 zOut[iOut] = zText[i]; 5026 iOut++; 5027 } 5028 } 5029 5030 if( zNL ){ 5031 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 5032 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; 5033 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; 5034 } 5035 if( zCR ){ 5036 memcpy(&zOut[iOut], ",'", 2); iOut += 2; 5037 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; 5038 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; 5039 } 5040 5041 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); 5042 sqlite3_free(zOut); 5043 return; 5044 } 5045 } 5046 5047 sqlite3_result_value(context, argv[0]); 5048} 5049 5050/* Flags for open_db(). 5051** 5052** The default behavior of open_db() is to exit(1) if the database fails to 5053** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error 5054** but still returns without calling exit. 5055** 5056** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a 5057** ZIP archive if the file does not exist or is empty and its name matches 5058** the *.zip pattern. 5059*/ 5060#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ 5061#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ 5062 5063/* 5064** Make sure the database is open. If it is not, then open it. If 5065** the database fails to open, print an error message and exit. 5066*/ 5067static void open_db(ShellState *p, int openFlags){ 5068 if( p->db==0 ){ 5069 const char *zDbFilename = p->pAuxDb->zDbFilename; 5070 if( p->openMode==SHELL_OPEN_UNSPEC ){ 5071 if( zDbFilename==0 || zDbFilename[0]==0 ){ 5072 p->openMode = SHELL_OPEN_NORMAL; 5073 }else{ 5074 p->openMode = (u8)deduceDatabaseType(zDbFilename, 5075 (openFlags & OPEN_DB_ZIPFILE)!=0); 5076 } 5077 } 5078 switch( p->openMode ){ 5079 case SHELL_OPEN_APPENDVFS: { 5080 sqlite3_open_v2(zDbFilename, &p->db, 5081 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); 5082 break; 5083 } 5084 case SHELL_OPEN_HEXDB: 5085 case SHELL_OPEN_DESERIALIZE: { 5086 sqlite3_open(0, &p->db); 5087 break; 5088 } 5089 case SHELL_OPEN_ZIPFILE: { 5090 sqlite3_open(":memory:", &p->db); 5091 break; 5092 } 5093 case SHELL_OPEN_READONLY: { 5094 sqlite3_open_v2(zDbFilename, &p->db, 5095 SQLITE_OPEN_READONLY|p->openFlags, 0); 5096 break; 5097 } 5098 case SHELL_OPEN_UNSPEC: 5099 case SHELL_OPEN_NORMAL: { 5100 sqlite3_open_v2(zDbFilename, &p->db, 5101 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); 5102 break; 5103 } 5104 } 5105 globalDb = p->db; 5106 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ 5107 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", 5108 zDbFilename, sqlite3_errmsg(p->db)); 5109 if( openFlags & OPEN_DB_KEEPALIVE ){ 5110 sqlite3_open(":memory:", &p->db); 5111 return; 5112 } 5113 exit(1); 5114 } 5115#ifndef SQLITE_OMIT_LOAD_EXTENSION 5116 sqlite3_enable_load_extension(p->db, 1); 5117#endif 5118 sqlite3_shathree_init(p->db, 0, 0); 5119 sqlite3_uint_init(p->db, 0, 0); 5120 sqlite3_decimal_init(p->db, 0, 0); 5121 sqlite3_regexp_init(p->db, 0, 0); 5122 sqlite3_ieee_init(p->db, 0, 0); 5123 sqlite3_series_init(p->db, 0, 0); 5124#ifndef SQLITE_SHELL_FIDDLE 5125 sqlite3_fileio_init(p->db, 0, 0); 5126 sqlite3_completion_init(p->db, 0, 0); 5127#endif 5128#if SQLITE_SHELL_HAVE_RECOVER 5129 sqlite3_dbdata_init(p->db, 0, 0); 5130#endif 5131#ifdef SQLITE_HAVE_ZLIB 5132 if( !p->bSafeModePersist ){ 5133 sqlite3_zipfile_init(p->db, 0, 0); 5134 sqlite3_sqlar_init(p->db, 0, 0); 5135 } 5136#endif 5137 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, 5138 shellAddSchemaName, 0, 0); 5139 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, 5140 shellModuleSchema, 0, 0); 5141 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, 5142 shellPutsFunc, 0, 0); 5143 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, 5144 shellEscapeCrnl, 0, 0); 5145 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, 5146 shellInt32, 0, 0); 5147 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, 5148 shellIdQuote, 0, 0); 5149 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, 5150 shellUSleepFunc, 0, 0); 5151#ifndef SQLITE_NOHAVE_SYSTEM 5152 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, 5153 editFunc, 0, 0); 5154 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, 5155 editFunc, 0, 0); 5156#endif 5157 if( p->openMode==SHELL_OPEN_ZIPFILE ){ 5158 char *zSql = sqlite3_mprintf( 5159 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename); 5160 shell_check_oom(zSql); 5161 sqlite3_exec(p->db, zSql, 0, 0, 0); 5162 sqlite3_free(zSql); 5163 } 5164#ifndef SQLITE_OMIT_DESERIALIZE 5165 else 5166 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ 5167 int rc; 5168 int nData = 0; 5169 unsigned char *aData; 5170 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ 5171 aData = (unsigned char*)readFile(zDbFilename, &nData); 5172 }else{ 5173 aData = readHexDb(p, &nData); 5174 if( aData==0 ){ 5175 return; 5176 } 5177 } 5178 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, 5179 SQLITE_DESERIALIZE_RESIZEABLE | 5180 SQLITE_DESERIALIZE_FREEONCLOSE); 5181 if( rc ){ 5182 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); 5183 } 5184 if( p->szMax>0 ){ 5185 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); 5186 } 5187 } 5188#endif 5189 } 5190 if( p->bSafeModePersist && p->db!=0 ){ 5191 sqlite3_set_authorizer(p->db, safeModeAuth, p); 5192 } 5193} 5194 5195/* 5196** Attempt to close the databaes connection. Report errors. 5197*/ 5198void close_db(sqlite3 *db){ 5199 int rc = sqlite3_close(db); 5200 if( rc ){ 5201 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", 5202 rc, sqlite3_errmsg(db)); 5203 } 5204} 5205 5206#if HAVE_READLINE || HAVE_EDITLINE 5207/* 5208** Readline completion callbacks 5209*/ 5210static char *readline_completion_generator(const char *text, int state){ 5211 static sqlite3_stmt *pStmt = 0; 5212 char *zRet; 5213 if( state==0 ){ 5214 char *zSql; 5215 sqlite3_finalize(pStmt); 5216 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 5217 " FROM completion(%Q) ORDER BY 1", text); 5218 shell_check_oom(zSql); 5219 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 5220 sqlite3_free(zSql); 5221 } 5222 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 5223 const char *z = (const char*)sqlite3_column_text(pStmt,0); 5224 zRet = z ? strdup(z) : 0; 5225 }else{ 5226 sqlite3_finalize(pStmt); 5227 pStmt = 0; 5228 zRet = 0; 5229 } 5230 return zRet; 5231} 5232static char **readline_completion(const char *zText, int iStart, int iEnd){ 5233 rl_attempted_completion_over = 1; 5234 return rl_completion_matches(zText, readline_completion_generator); 5235} 5236 5237#elif HAVE_LINENOISE 5238/* 5239** Linenoise completion callback 5240*/ 5241static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ 5242 i64 nLine = strlen(zLine); 5243 i64 i, iStart; 5244 sqlite3_stmt *pStmt = 0; 5245 char *zSql; 5246 char zBuf[1000]; 5247 5248 if( nLine>sizeof(zBuf)-30 ) return; 5249 if( zLine[0]=='.' || zLine[0]=='#') return; 5250 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} 5251 if( i==nLine-1 ) return; 5252 iStart = i+1; 5253 memcpy(zBuf, zLine, iStart); 5254 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" 5255 " FROM completion(%Q,%Q) ORDER BY 1", 5256 &zLine[iStart], zLine); 5257 shell_check_oom(zSql); 5258 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); 5259 sqlite3_free(zSql); 5260 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ 5261 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 5262 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); 5263 int nCompletion = sqlite3_column_bytes(pStmt, 0); 5264 if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){ 5265 memcpy(zBuf+iStart, zCompletion, nCompletion+1); 5266 linenoiseAddCompletion(lc, zBuf); 5267 } 5268 } 5269 sqlite3_finalize(pStmt); 5270} 5271#endif 5272 5273/* 5274** Do C-language style dequoting. 5275** 5276** \a -> alarm 5277** \b -> backspace 5278** \t -> tab 5279** \n -> newline 5280** \v -> vertical tab 5281** \f -> form feed 5282** \r -> carriage return 5283** \s -> space 5284** \" -> " 5285** \' -> ' 5286** \\ -> backslash 5287** \NNN -> ascii character NNN in octal 5288*/ 5289static void resolve_backslashes(char *z){ 5290 int i, j; 5291 char c; 5292 while( *z && *z!='\\' ) z++; 5293 for(i=j=0; (c = z[i])!=0; i++, j++){ 5294 if( c=='\\' && z[i+1]!=0 ){ 5295 c = z[++i]; 5296 if( c=='a' ){ 5297 c = '\a'; 5298 }else if( c=='b' ){ 5299 c = '\b'; 5300 }else if( c=='t' ){ 5301 c = '\t'; 5302 }else if( c=='n' ){ 5303 c = '\n'; 5304 }else if( c=='v' ){ 5305 c = '\v'; 5306 }else if( c=='f' ){ 5307 c = '\f'; 5308 }else if( c=='r' ){ 5309 c = '\r'; 5310 }else if( c=='"' ){ 5311 c = '"'; 5312 }else if( c=='\'' ){ 5313 c = '\''; 5314 }else if( c=='\\' ){ 5315 c = '\\'; 5316 }else if( c>='0' && c<='7' ){ 5317 c -= '0'; 5318 if( z[i+1]>='0' && z[i+1]<='7' ){ 5319 i++; 5320 c = (c<<3) + z[i] - '0'; 5321 if( z[i+1]>='0' && z[i+1]<='7' ){ 5322 i++; 5323 c = (c<<3) + z[i] - '0'; 5324 } 5325 } 5326 } 5327 } 5328 z[j] = c; 5329 } 5330 if( j<i ) z[j] = 0; 5331} 5332 5333/* 5334** Interpret zArg as either an integer or a boolean value. Return 1 or 0 5335** for TRUE and FALSE. Return the integer value if appropriate. 5336*/ 5337static int booleanValue(const char *zArg){ 5338 int i; 5339 if( zArg[0]=='0' && zArg[1]=='x' ){ 5340 for(i=2; hexDigitValue(zArg[i])>=0; i++){} 5341 }else{ 5342 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} 5343 } 5344 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); 5345 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ 5346 return 1; 5347 } 5348 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ 5349 return 0; 5350 } 5351 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", 5352 zArg); 5353 return 0; 5354} 5355 5356/* 5357** Set or clear a shell flag according to a boolean value. 5358*/ 5359static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ 5360 if( booleanValue(zArg) ){ 5361 ShellSetFlag(p, mFlag); 5362 }else{ 5363 ShellClearFlag(p, mFlag); 5364 } 5365} 5366 5367/* 5368** Close an output file, assuming it is not stderr or stdout 5369*/ 5370static void output_file_close(FILE *f){ 5371 if( f && f!=stdout && f!=stderr ) fclose(f); 5372} 5373 5374/* 5375** Try to open an output file. The names "stdout" and "stderr" are 5376** recognized and do the right thing. NULL is returned if the output 5377** filename is "off". 5378*/ 5379static FILE *output_file_open(const char *zFile, int bTextMode){ 5380 FILE *f; 5381 if( cli_strcmp(zFile,"stdout")==0 ){ 5382 f = stdout; 5383 }else if( cli_strcmp(zFile, "stderr")==0 ){ 5384 f = stderr; 5385 }else if( cli_strcmp(zFile, "off")==0 ){ 5386 f = 0; 5387 }else{ 5388 f = fopen(zFile, bTextMode ? "w" : "wb"); 5389 if( f==0 ){ 5390 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 5391 } 5392 } 5393 return f; 5394} 5395 5396#ifndef SQLITE_OMIT_TRACE 5397/* 5398** A routine for handling output from sqlite3_trace(). 5399*/ 5400static int sql_trace_callback( 5401 unsigned mType, /* The trace type */ 5402 void *pArg, /* The ShellState pointer */ 5403 void *pP, /* Usually a pointer to sqlite_stmt */ 5404 void *pX /* Auxiliary output */ 5405){ 5406 ShellState *p = (ShellState*)pArg; 5407 sqlite3_stmt *pStmt; 5408 const char *zSql; 5409 i64 nSql; 5410 if( p->traceOut==0 ) return 0; 5411 if( mType==SQLITE_TRACE_CLOSE ){ 5412 utf8_printf(p->traceOut, "-- closing database connection\n"); 5413 return 0; 5414 } 5415 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ 5416 zSql = (const char*)pX; 5417 }else{ 5418 pStmt = (sqlite3_stmt*)pP; 5419 switch( p->eTraceType ){ 5420 case SHELL_TRACE_EXPANDED: { 5421 zSql = sqlite3_expanded_sql(pStmt); 5422 break; 5423 } 5424#ifdef SQLITE_ENABLE_NORMALIZE 5425 case SHELL_TRACE_NORMALIZED: { 5426 zSql = sqlite3_normalized_sql(pStmt); 5427 break; 5428 } 5429#endif 5430 default: { 5431 zSql = sqlite3_sql(pStmt); 5432 break; 5433 } 5434 } 5435 } 5436 if( zSql==0 ) return 0; 5437 nSql = strlen(zSql); 5438 if( nSql>1000000000 ) nSql = 1000000000; 5439 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } 5440 switch( mType ){ 5441 case SQLITE_TRACE_ROW: 5442 case SQLITE_TRACE_STMT: { 5443 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql); 5444 break; 5445 } 5446 case SQLITE_TRACE_PROFILE: { 5447 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; 5448 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec); 5449 break; 5450 } 5451 } 5452 return 0; 5453} 5454#endif 5455 5456/* 5457** A no-op routine that runs with the ".breakpoint" doc-command. This is 5458** a useful spot to set a debugger breakpoint. 5459*/ 5460static void test_breakpoint(void){ 5461 static int nCall = 0; 5462 nCall++; 5463} 5464 5465/* 5466** An object used to read a CSV and other files for import. 5467*/ 5468typedef struct ImportCtx ImportCtx; 5469struct ImportCtx { 5470 const char *zFile; /* Name of the input file */ 5471 FILE *in; /* Read the CSV text from this input stream */ 5472 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ 5473 char *z; /* Accumulated text for a field */ 5474 int n; /* Number of bytes in z */ 5475 int nAlloc; /* Space allocated for z[] */ 5476 int nLine; /* Current line number */ 5477 int nRow; /* Number of rows imported */ 5478 int nErr; /* Number of errors encountered */ 5479 int bNotFirst; /* True if one or more bytes already read */ 5480 int cTerm; /* Character that terminated the most recent field */ 5481 int cColSep; /* The column separator character. (Usually ",") */ 5482 int cRowSep; /* The row separator character. (Usually "\n") */ 5483}; 5484 5485/* Clean up resourced used by an ImportCtx */ 5486static void import_cleanup(ImportCtx *p){ 5487 if( p->in!=0 && p->xCloser!=0 ){ 5488 p->xCloser(p->in); 5489 p->in = 0; 5490 } 5491 sqlite3_free(p->z); 5492 p->z = 0; 5493} 5494 5495/* Append a single byte to z[] */ 5496static void import_append_char(ImportCtx *p, int c){ 5497 if( p->n+1>=p->nAlloc ){ 5498 p->nAlloc += p->nAlloc + 100; 5499 p->z = sqlite3_realloc64(p->z, p->nAlloc); 5500 shell_check_oom(p->z); 5501 } 5502 p->z[p->n++] = (char)c; 5503} 5504 5505/* Read a single field of CSV text. Compatible with rfc4180 and extended 5506** with the option of having a separator other than ",". 5507** 5508** + Input comes from p->in. 5509** + Store results in p->z of length p->n. Space to hold p->z comes 5510** from sqlite3_malloc64(). 5511** + Use p->cSep as the column separator. The default is ",". 5512** + Use p->rSep as the row separator. The default is "\n". 5513** + Keep track of the line number in p->nLine. 5514** + Store the character that terminates the field in p->cTerm. Store 5515** EOF on end-of-file. 5516** + Report syntax errors on stderr 5517*/ 5518static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ 5519 int c; 5520 int cSep = p->cColSep; 5521 int rSep = p->cRowSep; 5522 p->n = 0; 5523 c = fgetc(p->in); 5524 if( c==EOF || seenInterrupt ){ 5525 p->cTerm = EOF; 5526 return 0; 5527 } 5528 if( c=='"' ){ 5529 int pc, ppc; 5530 int startLine = p->nLine; 5531 int cQuote = c; 5532 pc = ppc = 0; 5533 while( 1 ){ 5534 c = fgetc(p->in); 5535 if( c==rSep ) p->nLine++; 5536 if( c==cQuote ){ 5537 if( pc==cQuote ){ 5538 pc = 0; 5539 continue; 5540 } 5541 } 5542 if( (c==cSep && pc==cQuote) 5543 || (c==rSep && pc==cQuote) 5544 || (c==rSep && pc=='\r' && ppc==cQuote) 5545 || (c==EOF && pc==cQuote) 5546 ){ 5547 do{ p->n--; }while( p->z[p->n]!=cQuote ); 5548 p->cTerm = c; 5549 break; 5550 } 5551 if( pc==cQuote && c!='\r' ){ 5552 utf8_printf(stderr, "%s:%d: unescaped %c character\n", 5553 p->zFile, p->nLine, cQuote); 5554 } 5555 if( c==EOF ){ 5556 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", 5557 p->zFile, startLine, cQuote); 5558 p->cTerm = c; 5559 break; 5560 } 5561 import_append_char(p, c); 5562 ppc = pc; 5563 pc = c; 5564 } 5565 }else{ 5566 /* If this is the first field being parsed and it begins with the 5567 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ 5568 if( (c&0xff)==0xef && p->bNotFirst==0 ){ 5569 import_append_char(p, c); 5570 c = fgetc(p->in); 5571 if( (c&0xff)==0xbb ){ 5572 import_append_char(p, c); 5573 c = fgetc(p->in); 5574 if( (c&0xff)==0xbf ){ 5575 p->bNotFirst = 1; 5576 p->n = 0; 5577 return csv_read_one_field(p); 5578 } 5579 } 5580 } 5581 while( c!=EOF && c!=cSep && c!=rSep ){ 5582 import_append_char(p, c); 5583 c = fgetc(p->in); 5584 } 5585 if( c==rSep ){ 5586 p->nLine++; 5587 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; 5588 } 5589 p->cTerm = c; 5590 } 5591 if( p->z ) p->z[p->n] = 0; 5592 p->bNotFirst = 1; 5593 return p->z; 5594} 5595 5596/* Read a single field of ASCII delimited text. 5597** 5598** + Input comes from p->in. 5599** + Store results in p->z of length p->n. Space to hold p->z comes 5600** from sqlite3_malloc64(). 5601** + Use p->cSep as the column separator. The default is "\x1F". 5602** + Use p->rSep as the row separator. The default is "\x1E". 5603** + Keep track of the row number in p->nLine. 5604** + Store the character that terminates the field in p->cTerm. Store 5605** EOF on end-of-file. 5606** + Report syntax errors on stderr 5607*/ 5608static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ 5609 int c; 5610 int cSep = p->cColSep; 5611 int rSep = p->cRowSep; 5612 p->n = 0; 5613 c = fgetc(p->in); 5614 if( c==EOF || seenInterrupt ){ 5615 p->cTerm = EOF; 5616 return 0; 5617 } 5618 while( c!=EOF && c!=cSep && c!=rSep ){ 5619 import_append_char(p, c); 5620 c = fgetc(p->in); 5621 } 5622 if( c==rSep ){ 5623 p->nLine++; 5624 } 5625 p->cTerm = c; 5626 if( p->z ) p->z[p->n] = 0; 5627 return p->z; 5628} 5629 5630/* 5631** Try to transfer data for table zTable. If an error is seen while 5632** moving forward, try to go backwards. The backwards movement won't 5633** work for WITHOUT ROWID tables. 5634*/ 5635static void tryToCloneData( 5636 ShellState *p, 5637 sqlite3 *newDb, 5638 const char *zTable 5639){ 5640 sqlite3_stmt *pQuery = 0; 5641 sqlite3_stmt *pInsert = 0; 5642 char *zQuery = 0; 5643 char *zInsert = 0; 5644 int rc; 5645 int i, j, n; 5646 int nTable = strlen30(zTable); 5647 int k = 0; 5648 int cnt = 0; 5649 const int spinRate = 10000; 5650 5651 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); 5652 shell_check_oom(zQuery); 5653 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5654 if( rc ){ 5655 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5656 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5657 zQuery); 5658 goto end_data_xfer; 5659 } 5660 n = sqlite3_column_count(pQuery); 5661 zInsert = sqlite3_malloc64(200 + nTable + n*3); 5662 shell_check_oom(zInsert); 5663 sqlite3_snprintf(200+nTable,zInsert, 5664 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); 5665 i = strlen30(zInsert); 5666 for(j=1; j<n; j++){ 5667 memcpy(zInsert+i, ",?", 2); 5668 i += 2; 5669 } 5670 memcpy(zInsert+i, ");", 3); 5671 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); 5672 if( rc ){ 5673 utf8_printf(stderr, "Error %d: %s on [%s]\n", 5674 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), 5675 zQuery); 5676 goto end_data_xfer; 5677 } 5678 for(k=0; k<2; k++){ 5679 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5680 for(i=0; i<n; i++){ 5681 switch( sqlite3_column_type(pQuery, i) ){ 5682 case SQLITE_NULL: { 5683 sqlite3_bind_null(pInsert, i+1); 5684 break; 5685 } 5686 case SQLITE_INTEGER: { 5687 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); 5688 break; 5689 } 5690 case SQLITE_FLOAT: { 5691 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); 5692 break; 5693 } 5694 case SQLITE_TEXT: { 5695 sqlite3_bind_text(pInsert, i+1, 5696 (const char*)sqlite3_column_text(pQuery,i), 5697 -1, SQLITE_STATIC); 5698 break; 5699 } 5700 case SQLITE_BLOB: { 5701 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), 5702 sqlite3_column_bytes(pQuery,i), 5703 SQLITE_STATIC); 5704 break; 5705 } 5706 } 5707 } /* End for */ 5708 rc = sqlite3_step(pInsert); 5709 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 5710 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), 5711 sqlite3_errmsg(newDb)); 5712 } 5713 sqlite3_reset(pInsert); 5714 cnt++; 5715 if( (cnt%spinRate)==0 ){ 5716 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); 5717 fflush(stdout); 5718 } 5719 } /* End while */ 5720 if( rc==SQLITE_DONE ) break; 5721 sqlite3_finalize(pQuery); 5722 sqlite3_free(zQuery); 5723 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", 5724 zTable); 5725 shell_check_oom(zQuery); 5726 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5727 if( rc ){ 5728 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); 5729 break; 5730 } 5731 } /* End for(k=0...) */ 5732 5733end_data_xfer: 5734 sqlite3_finalize(pQuery); 5735 sqlite3_finalize(pInsert); 5736 sqlite3_free(zQuery); 5737 sqlite3_free(zInsert); 5738} 5739 5740 5741/* 5742** Try to transfer all rows of the schema that match zWhere. For 5743** each row, invoke xForEach() on the object defined by that row. 5744** If an error is encountered while moving forward through the 5745** sqlite_schema table, try again moving backwards. 5746*/ 5747static void tryToCloneSchema( 5748 ShellState *p, 5749 sqlite3 *newDb, 5750 const char *zWhere, 5751 void (*xForEach)(ShellState*,sqlite3*,const char*) 5752){ 5753 sqlite3_stmt *pQuery = 0; 5754 char *zQuery = 0; 5755 int rc; 5756 const unsigned char *zName; 5757 const unsigned char *zSql; 5758 char *zErrMsg = 0; 5759 5760 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" 5761 " WHERE %s", zWhere); 5762 shell_check_oom(zQuery); 5763 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5764 if( rc ){ 5765 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5766 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5767 zQuery); 5768 goto end_schema_xfer; 5769 } 5770 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ 5771 zName = sqlite3_column_text(pQuery, 0); 5772 zSql = sqlite3_column_text(pQuery, 1); 5773 if( zName==0 || zSql==0 ) continue; 5774 printf("%s... ", zName); fflush(stdout); 5775 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5776 if( zErrMsg ){ 5777 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5778 sqlite3_free(zErrMsg); 5779 zErrMsg = 0; 5780 } 5781 if( xForEach ){ 5782 xForEach(p, newDb, (const char*)zName); 5783 } 5784 printf("done\n"); 5785 } 5786 if( rc!=SQLITE_DONE ){ 5787 sqlite3_finalize(pQuery); 5788 sqlite3_free(zQuery); 5789 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" 5790 " WHERE %s ORDER BY rowid DESC", zWhere); 5791 shell_check_oom(zQuery); 5792 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); 5793 if( rc ){ 5794 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", 5795 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), 5796 zQuery); 5797 goto end_schema_xfer; 5798 } 5799 while( sqlite3_step(pQuery)==SQLITE_ROW ){ 5800 zName = sqlite3_column_text(pQuery, 0); 5801 zSql = sqlite3_column_text(pQuery, 1); 5802 if( zName==0 || zSql==0 ) continue; 5803 printf("%s... ", zName); fflush(stdout); 5804 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); 5805 if( zErrMsg ){ 5806 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); 5807 sqlite3_free(zErrMsg); 5808 zErrMsg = 0; 5809 } 5810 if( xForEach ){ 5811 xForEach(p, newDb, (const char*)zName); 5812 } 5813 printf("done\n"); 5814 } 5815 } 5816end_schema_xfer: 5817 sqlite3_finalize(pQuery); 5818 sqlite3_free(zQuery); 5819} 5820 5821/* 5822** Open a new database file named "zNewDb". Try to recover as much information 5823** as possible out of the main database (which might be corrupt) and write it 5824** into zNewDb. 5825*/ 5826static void tryToClone(ShellState *p, const char *zNewDb){ 5827 int rc; 5828 sqlite3 *newDb = 0; 5829 if( access(zNewDb,0)==0 ){ 5830 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); 5831 return; 5832 } 5833 rc = sqlite3_open(zNewDb, &newDb); 5834 if( rc ){ 5835 utf8_printf(stderr, "Cannot create output database: %s\n", 5836 sqlite3_errmsg(newDb)); 5837 }else{ 5838 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); 5839 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); 5840 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); 5841 tryToCloneSchema(p, newDb, "type!='table'", 0); 5842 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); 5843 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 5844 } 5845 close_db(newDb); 5846} 5847 5848/* 5849** Change the output file back to stdout. 5850** 5851** If the p->doXdgOpen flag is set, that means the output was being 5852** redirected to a temporary file named by p->zTempFile. In that case, 5853** launch start/open/xdg-open on that temporary file. 5854*/ 5855static void output_reset(ShellState *p){ 5856 if( p->outfile[0]=='|' ){ 5857#ifndef SQLITE_OMIT_POPEN 5858 pclose(p->out); 5859#endif 5860 }else{ 5861 output_file_close(p->out); 5862#ifndef SQLITE_NOHAVE_SYSTEM 5863 if( p->doXdgOpen ){ 5864 const char *zXdgOpenCmd = 5865#if defined(_WIN32) 5866 "start"; 5867#elif defined(__APPLE__) 5868 "open"; 5869#else 5870 "xdg-open"; 5871#endif 5872 char *zCmd; 5873 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); 5874 if( system(zCmd) ){ 5875 utf8_printf(stderr, "Failed: [%s]\n", zCmd); 5876 }else{ 5877 /* Give the start/open/xdg-open command some time to get 5878 ** going before we continue, and potential delete the 5879 ** p->zTempFile data file out from under it */ 5880 sqlite3_sleep(2000); 5881 } 5882 sqlite3_free(zCmd); 5883 outputModePop(p); 5884 p->doXdgOpen = 0; 5885 } 5886#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ 5887 } 5888 p->outfile[0] = 0; 5889 p->out = stdout; 5890} 5891 5892/* 5893** Run an SQL command and return the single integer result. 5894*/ 5895static int db_int(sqlite3 *db, const char *zSql){ 5896 sqlite3_stmt *pStmt; 5897 int res = 0; 5898 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 5899 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 5900 res = sqlite3_column_int(pStmt,0); 5901 } 5902 sqlite3_finalize(pStmt); 5903 return res; 5904} 5905 5906#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) 5907/* 5908** Convert a 2-byte or 4-byte big-endian integer into a native integer 5909*/ 5910static unsigned int get2byteInt(unsigned char *a){ 5911 return (a[0]<<8) + a[1]; 5912} 5913static unsigned int get4byteInt(unsigned char *a){ 5914 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; 5915} 5916 5917/* 5918** Implementation of the ".dbinfo" command. 5919** 5920** Return 1 on error, 2 to exit, and 0 otherwise. 5921*/ 5922static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ 5923 static const struct { const char *zName; int ofst; } aField[] = { 5924 { "file change counter:", 24 }, 5925 { "database page count:", 28 }, 5926 { "freelist page count:", 36 }, 5927 { "schema cookie:", 40 }, 5928 { "schema format:", 44 }, 5929 { "default cache size:", 48 }, 5930 { "autovacuum top root:", 52 }, 5931 { "incremental vacuum:", 64 }, 5932 { "text encoding:", 56 }, 5933 { "user version:", 60 }, 5934 { "application id:", 68 }, 5935 { "software version:", 96 }, 5936 }; 5937 static const struct { const char *zName; const char *zSql; } aQuery[] = { 5938 { "number of tables:", 5939 "SELECT count(*) FROM %s WHERE type='table'" }, 5940 { "number of indexes:", 5941 "SELECT count(*) FROM %s WHERE type='index'" }, 5942 { "number of triggers:", 5943 "SELECT count(*) FROM %s WHERE type='trigger'" }, 5944 { "number of views:", 5945 "SELECT count(*) FROM %s WHERE type='view'" }, 5946 { "schema size:", 5947 "SELECT total(length(sql)) FROM %s" }, 5948 }; 5949 int i, rc; 5950 unsigned iDataVersion; 5951 char *zSchemaTab; 5952 char *zDb = nArg>=2 ? azArg[1] : "main"; 5953 sqlite3_stmt *pStmt = 0; 5954 unsigned char aHdr[100]; 5955 open_db(p, 0); 5956 if( p->db==0 ) return 1; 5957 rc = sqlite3_prepare_v2(p->db, 5958 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", 5959 -1, &pStmt, 0); 5960 if( rc ){ 5961 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); 5962 sqlite3_finalize(pStmt); 5963 return 1; 5964 } 5965 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); 5966 if( sqlite3_step(pStmt)==SQLITE_ROW 5967 && sqlite3_column_bytes(pStmt,0)>100 5968 ){ 5969 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); 5970 sqlite3_finalize(pStmt); 5971 }else{ 5972 raw_printf(stderr, "unable to read database header\n"); 5973 sqlite3_finalize(pStmt); 5974 return 1; 5975 } 5976 i = get2byteInt(aHdr+16); 5977 if( i==1 ) i = 65536; 5978 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); 5979 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); 5980 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); 5981 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); 5982 for(i=0; i<ArraySize(aField); i++){ 5983 int ofst = aField[i].ofst; 5984 unsigned int val = get4byteInt(aHdr + ofst); 5985 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); 5986 switch( ofst ){ 5987 case 56: { 5988 if( val==1 ) raw_printf(p->out, " (utf8)"); 5989 if( val==2 ) raw_printf(p->out, " (utf16le)"); 5990 if( val==3 ) raw_printf(p->out, " (utf16be)"); 5991 } 5992 } 5993 raw_printf(p->out, "\n"); 5994 } 5995 if( zDb==0 ){ 5996 zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); 5997 }else if( cli_strcmp(zDb,"temp")==0 ){ 5998 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); 5999 }else{ 6000 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); 6001 } 6002 for(i=0; i<ArraySize(aQuery); i++){ 6003 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); 6004 int val = db_int(p->db, zSql); 6005 sqlite3_free(zSql); 6006 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); 6007 } 6008 sqlite3_free(zSchemaTab); 6009 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); 6010 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); 6011 return 0; 6012} 6013#endif /* SQLITE_SHELL_HAVE_RECOVER */ 6014 6015/* 6016** Print the current sqlite3_errmsg() value to stderr and return 1. 6017*/ 6018static int shellDatabaseError(sqlite3 *db){ 6019 const char *zErr = sqlite3_errmsg(db); 6020 utf8_printf(stderr, "Error: %s\n", zErr); 6021 return 1; 6022} 6023 6024/* 6025** Compare the pattern in zGlob[] against the text in z[]. Return TRUE 6026** if they match and FALSE (0) if they do not match. 6027** 6028** Globbing rules: 6029** 6030** '*' Matches any sequence of zero or more characters. 6031** 6032** '?' Matches exactly one character. 6033** 6034** [...] Matches one character from the enclosed list of 6035** characters. 6036** 6037** [^...] Matches one character not in the enclosed list. 6038** 6039** '#' Matches any sequence of one or more digits with an 6040** optional + or - sign in front 6041** 6042** ' ' Any span of whitespace matches any other span of 6043** whitespace. 6044** 6045** Extra whitespace at the end of z[] is ignored. 6046*/ 6047static int testcase_glob(const char *zGlob, const char *z){ 6048 int c, c2; 6049 int invert; 6050 int seen; 6051 6052 while( (c = (*(zGlob++)))!=0 ){ 6053 if( IsSpace(c) ){ 6054 if( !IsSpace(*z) ) return 0; 6055 while( IsSpace(*zGlob) ) zGlob++; 6056 while( IsSpace(*z) ) z++; 6057 }else if( c=='*' ){ 6058 while( (c=(*(zGlob++))) == '*' || c=='?' ){ 6059 if( c=='?' && (*(z++))==0 ) return 0; 6060 } 6061 if( c==0 ){ 6062 return 1; 6063 }else if( c=='[' ){ 6064 while( *z && testcase_glob(zGlob-1,z)==0 ){ 6065 z++; 6066 } 6067 return (*z)!=0; 6068 } 6069 while( (c2 = (*(z++)))!=0 ){ 6070 while( c2!=c ){ 6071 c2 = *(z++); 6072 if( c2==0 ) return 0; 6073 } 6074 if( testcase_glob(zGlob,z) ) return 1; 6075 } 6076 return 0; 6077 }else if( c=='?' ){ 6078 if( (*(z++))==0 ) return 0; 6079 }else if( c=='[' ){ 6080 int prior_c = 0; 6081 seen = 0; 6082 invert = 0; 6083 c = *(z++); 6084 if( c==0 ) return 0; 6085 c2 = *(zGlob++); 6086 if( c2=='^' ){ 6087 invert = 1; 6088 c2 = *(zGlob++); 6089 } 6090 if( c2==']' ){ 6091 if( c==']' ) seen = 1; 6092 c2 = *(zGlob++); 6093 } 6094 while( c2 && c2!=']' ){ 6095 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 6096 c2 = *(zGlob++); 6097 if( c>=prior_c && c<=c2 ) seen = 1; 6098 prior_c = 0; 6099 }else{ 6100 if( c==c2 ){ 6101 seen = 1; 6102 } 6103 prior_c = c2; 6104 } 6105 c2 = *(zGlob++); 6106 } 6107 if( c2==0 || (seen ^ invert)==0 ) return 0; 6108 }else if( c=='#' ){ 6109 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; 6110 if( !IsDigit(z[0]) ) return 0; 6111 z++; 6112 while( IsDigit(z[0]) ){ z++; } 6113 }else{ 6114 if( c!=(*(z++)) ) return 0; 6115 } 6116 } 6117 while( IsSpace(*z) ){ z++; } 6118 return *z==0; 6119} 6120 6121 6122/* 6123** Compare the string as a command-line option with either one or two 6124** initial "-" characters. 6125*/ 6126static int optionMatch(const char *zStr, const char *zOpt){ 6127 if( zStr[0]!='-' ) return 0; 6128 zStr++; 6129 if( zStr[0]=='-' ) zStr++; 6130 return cli_strcmp(zStr, zOpt)==0; 6131} 6132 6133/* 6134** Delete a file. 6135*/ 6136int shellDeleteFile(const char *zFilename){ 6137 int rc; 6138#ifdef _WIN32 6139 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); 6140 rc = _wunlink(z); 6141 sqlite3_free(z); 6142#else 6143 rc = unlink(zFilename); 6144#endif 6145 return rc; 6146} 6147 6148/* 6149** Try to delete the temporary file (if there is one) and free the 6150** memory used to hold the name of the temp file. 6151*/ 6152static void clearTempFile(ShellState *p){ 6153 if( p->zTempFile==0 ) return; 6154 if( p->doXdgOpen ) return; 6155 if( shellDeleteFile(p->zTempFile) ) return; 6156 sqlite3_free(p->zTempFile); 6157 p->zTempFile = 0; 6158} 6159 6160/* 6161** Create a new temp file name with the given suffix. 6162*/ 6163static void newTempFile(ShellState *p, const char *zSuffix){ 6164 clearTempFile(p); 6165 sqlite3_free(p->zTempFile); 6166 p->zTempFile = 0; 6167 if( p->db ){ 6168 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); 6169 } 6170 if( p->zTempFile==0 ){ 6171 /* If p->db is an in-memory database then the TEMPFILENAME file-control 6172 ** will not work and we will need to fallback to guessing */ 6173 char *zTemp; 6174 sqlite3_uint64 r; 6175 sqlite3_randomness(sizeof(r), &r); 6176 zTemp = getenv("TEMP"); 6177 if( zTemp==0 ) zTemp = getenv("TMP"); 6178 if( zTemp==0 ){ 6179#ifdef _WIN32 6180 zTemp = "\\tmp"; 6181#else 6182 zTemp = "/tmp"; 6183#endif 6184 } 6185 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); 6186 }else{ 6187 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); 6188 } 6189 shell_check_oom(p->zTempFile); 6190} 6191 6192 6193/* 6194** The implementation of SQL scalar function fkey_collate_clause(), used 6195** by the ".lint fkey-indexes" command. This scalar function is always 6196** called with four arguments - the parent table name, the parent column name, 6197** the child table name and the child column name. 6198** 6199** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') 6200** 6201** If either of the named tables or columns do not exist, this function 6202** returns an empty string. An empty string is also returned if both tables 6203** and columns exist but have the same default collation sequence. Or, 6204** if both exist but the default collation sequences are different, this 6205** function returns the string " COLLATE <parent-collation>", where 6206** <parent-collation> is the default collation sequence of the parent column. 6207*/ 6208static void shellFkeyCollateClause( 6209 sqlite3_context *pCtx, 6210 int nVal, 6211 sqlite3_value **apVal 6212){ 6213 sqlite3 *db = sqlite3_context_db_handle(pCtx); 6214 const char *zParent; 6215 const char *zParentCol; 6216 const char *zParentSeq; 6217 const char *zChild; 6218 const char *zChildCol; 6219 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ 6220 int rc; 6221 6222 assert( nVal==4 ); 6223 zParent = (const char*)sqlite3_value_text(apVal[0]); 6224 zParentCol = (const char*)sqlite3_value_text(apVal[1]); 6225 zChild = (const char*)sqlite3_value_text(apVal[2]); 6226 zChildCol = (const char*)sqlite3_value_text(apVal[3]); 6227 6228 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); 6229 rc = sqlite3_table_column_metadata( 6230 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 6231 ); 6232 if( rc==SQLITE_OK ){ 6233 rc = sqlite3_table_column_metadata( 6234 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 6235 ); 6236 } 6237 6238 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ 6239 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); 6240 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); 6241 sqlite3_free(z); 6242 } 6243} 6244 6245 6246/* 6247** The implementation of dot-command ".lint fkey-indexes". 6248*/ 6249static int lintFkeyIndexes( 6250 ShellState *pState, /* Current shell tool state */ 6251 char **azArg, /* Array of arguments passed to dot command */ 6252 int nArg /* Number of entries in azArg[] */ 6253){ 6254 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ 6255 FILE *out = pState->out; /* Stream to write non-error output to */ 6256 int bVerbose = 0; /* If -verbose is present */ 6257 int bGroupByParent = 0; /* If -groupbyparent is present */ 6258 int i; /* To iterate through azArg[] */ 6259 const char *zIndent = ""; /* How much to indent CREATE INDEX by */ 6260 int rc; /* Return code */ 6261 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ 6262 6263 /* 6264 ** This SELECT statement returns one row for each foreign key constraint 6265 ** in the schema of the main database. The column values are: 6266 ** 6267 ** 0. The text of an SQL statement similar to: 6268 ** 6269 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" 6270 ** 6271 ** This SELECT is similar to the one that the foreign keys implementation 6272 ** needs to run internally on child tables. If there is an index that can 6273 ** be used to optimize this query, then it can also be used by the FK 6274 ** implementation to optimize DELETE or UPDATE statements on the parent 6275 ** table. 6276 ** 6277 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by 6278 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema 6279 ** contains an index that can be used to optimize the query. 6280 ** 6281 ** 2. Human readable text that describes the child table and columns. e.g. 6282 ** 6283 ** "child_table(child_key1, child_key2)" 6284 ** 6285 ** 3. Human readable text that describes the parent table and columns. e.g. 6286 ** 6287 ** "parent_table(parent_key1, parent_key2)" 6288 ** 6289 ** 4. A full CREATE INDEX statement for an index that could be used to 6290 ** optimize DELETE or UPDATE statements on the parent table. e.g. 6291 ** 6292 ** "CREATE INDEX child_table_child_key ON child_table(child_key)" 6293 ** 6294 ** 5. The name of the parent table. 6295 ** 6296 ** These six values are used by the C logic below to generate the report. 6297 */ 6298 const char *zSql = 6299 "SELECT " 6300 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" 6301 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " 6302 " || fkey_collate_clause(" 6303 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" 6304 ", " 6305 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" 6306 " || group_concat('*=?', ' AND ') || ')'" 6307 ", " 6308 " s.name || '(' || group_concat(f.[from], ', ') || ')'" 6309 ", " 6310 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" 6311 ", " 6312 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" 6313 " || ' ON ' || quote(s.name) || '('" 6314 " || group_concat(quote(f.[from]) ||" 6315 " fkey_collate_clause(" 6316 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" 6317 " || ');'" 6318 ", " 6319 " f.[table] " 6320 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " 6321 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " 6322 "GROUP BY s.name, f.id " 6323 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" 6324 ; 6325 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; 6326 6327 for(i=2; i<nArg; i++){ 6328 int n = strlen30(azArg[i]); 6329 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ 6330 bVerbose = 1; 6331 } 6332 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ 6333 bGroupByParent = 1; 6334 zIndent = " "; 6335 } 6336 else{ 6337 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", 6338 azArg[0], azArg[1] 6339 ); 6340 return SQLITE_ERROR; 6341 } 6342 } 6343 6344 /* Register the fkey_collate_clause() SQL function */ 6345 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, 6346 0, shellFkeyCollateClause, 0, 0 6347 ); 6348 6349 6350 if( rc==SQLITE_OK ){ 6351 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); 6352 } 6353 if( rc==SQLITE_OK ){ 6354 sqlite3_bind_int(pSql, 1, bGroupByParent); 6355 } 6356 6357 if( rc==SQLITE_OK ){ 6358 int rc2; 6359 char *zPrev = 0; 6360 while( SQLITE_ROW==sqlite3_step(pSql) ){ 6361 int res = -1; 6362 sqlite3_stmt *pExplain = 0; 6363 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); 6364 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); 6365 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); 6366 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); 6367 const char *zCI = (const char*)sqlite3_column_text(pSql, 4); 6368 const char *zParent = (const char*)sqlite3_column_text(pSql, 5); 6369 6370 if( zEQP==0 ) continue; 6371 if( zGlob==0 ) continue; 6372 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); 6373 if( rc!=SQLITE_OK ) break; 6374 if( SQLITE_ROW==sqlite3_step(pExplain) ){ 6375 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); 6376 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) 6377 || 0==sqlite3_strglob(zGlobIPK, zPlan)); 6378 } 6379 rc = sqlite3_finalize(pExplain); 6380 if( rc!=SQLITE_OK ) break; 6381 6382 if( res<0 ){ 6383 raw_printf(stderr, "Error: internal error"); 6384 break; 6385 }else{ 6386 if( bGroupByParent 6387 && (bVerbose || res==0) 6388 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) 6389 ){ 6390 raw_printf(out, "-- Parent table %s\n", zParent); 6391 sqlite3_free(zPrev); 6392 zPrev = sqlite3_mprintf("%s", zParent); 6393 } 6394 6395 if( res==0 ){ 6396 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); 6397 }else if( bVerbose ){ 6398 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", 6399 zIndent, zFrom, zTarget 6400 ); 6401 } 6402 } 6403 } 6404 sqlite3_free(zPrev); 6405 6406 if( rc!=SQLITE_OK ){ 6407 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6408 } 6409 6410 rc2 = sqlite3_finalize(pSql); 6411 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ 6412 rc = rc2; 6413 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6414 } 6415 }else{ 6416 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); 6417 } 6418 6419 return rc; 6420} 6421 6422/* 6423** Implementation of ".lint" dot command. 6424*/ 6425static int lintDotCommand( 6426 ShellState *pState, /* Current shell tool state */ 6427 char **azArg, /* Array of arguments passed to dot command */ 6428 int nArg /* Number of entries in azArg[] */ 6429){ 6430 int n; 6431 n = (nArg>=2 ? strlen30(azArg[1]) : 0); 6432 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; 6433 return lintFkeyIndexes(pState, azArg, nArg); 6434 6435 usage: 6436 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); 6437 raw_printf(stderr, "Where sub-commands are:\n"); 6438 raw_printf(stderr, " fkey-indexes\n"); 6439 return SQLITE_ERROR; 6440} 6441 6442#if !defined SQLITE_OMIT_VIRTUALTABLE 6443static void shellPrepare( 6444 sqlite3 *db, 6445 int *pRc, 6446 const char *zSql, 6447 sqlite3_stmt **ppStmt 6448){ 6449 *ppStmt = 0; 6450 if( *pRc==SQLITE_OK ){ 6451 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); 6452 if( rc!=SQLITE_OK ){ 6453 raw_printf(stderr, "sql error: %s (%d)\n", 6454 sqlite3_errmsg(db), sqlite3_errcode(db) 6455 ); 6456 *pRc = rc; 6457 } 6458 } 6459} 6460 6461/* 6462** Create a prepared statement using printf-style arguments for the SQL. 6463** 6464** This routine is could be marked "static". But it is not always used, 6465** depending on compile-time options. By omitting the "static", we avoid 6466** nuisance compiler warnings about "defined but not used". 6467*/ 6468void shellPreparePrintf( 6469 sqlite3 *db, 6470 int *pRc, 6471 sqlite3_stmt **ppStmt, 6472 const char *zFmt, 6473 ... 6474){ 6475 *ppStmt = 0; 6476 if( *pRc==SQLITE_OK ){ 6477 va_list ap; 6478 char *z; 6479 va_start(ap, zFmt); 6480 z = sqlite3_vmprintf(zFmt, ap); 6481 va_end(ap); 6482 if( z==0 ){ 6483 *pRc = SQLITE_NOMEM; 6484 }else{ 6485 shellPrepare(db, pRc, z, ppStmt); 6486 sqlite3_free(z); 6487 } 6488 } 6489} 6490 6491/* Finalize the prepared statement created using shellPreparePrintf(). 6492** 6493** This routine is could be marked "static". But it is not always used, 6494** depending on compile-time options. By omitting the "static", we avoid 6495** nuisance compiler warnings about "defined but not used". 6496*/ 6497void shellFinalize( 6498 int *pRc, 6499 sqlite3_stmt *pStmt 6500){ 6501 if( pStmt ){ 6502 sqlite3 *db = sqlite3_db_handle(pStmt); 6503 int rc = sqlite3_finalize(pStmt); 6504 if( *pRc==SQLITE_OK ){ 6505 if( rc!=SQLITE_OK ){ 6506 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 6507 } 6508 *pRc = rc; 6509 } 6510 } 6511} 6512 6513/* Reset the prepared statement created using shellPreparePrintf(). 6514** 6515** This routine is could be marked "static". But it is not always used, 6516** depending on compile-time options. By omitting the "static", we avoid 6517** nuisance compiler warnings about "defined but not used". 6518*/ 6519void shellReset( 6520 int *pRc, 6521 sqlite3_stmt *pStmt 6522){ 6523 int rc = sqlite3_reset(pStmt); 6524 if( *pRc==SQLITE_OK ){ 6525 if( rc!=SQLITE_OK ){ 6526 sqlite3 *db = sqlite3_db_handle(pStmt); 6527 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 6528 } 6529 *pRc = rc; 6530 } 6531} 6532#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ 6533 6534#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 6535/****************************************************************************** 6536** The ".archive" or ".ar" command. 6537*/ 6538/* 6539** Structure representing a single ".ar" command. 6540*/ 6541typedef struct ArCommand ArCommand; 6542struct ArCommand { 6543 u8 eCmd; /* An AR_CMD_* value */ 6544 u8 bVerbose; /* True if --verbose */ 6545 u8 bZip; /* True if the archive is a ZIP */ 6546 u8 bDryRun; /* True if --dry-run */ 6547 u8 bAppend; /* True if --append */ 6548 u8 bGlob; /* True if --glob */ 6549 u8 fromCmdLine; /* Run from -A instead of .archive */ 6550 int nArg; /* Number of command arguments */ 6551 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ 6552 const char *zFile; /* --file argument, or NULL */ 6553 const char *zDir; /* --directory argument, or NULL */ 6554 char **azArg; /* Array of command arguments */ 6555 ShellState *p; /* Shell state */ 6556 sqlite3 *db; /* Database containing the archive */ 6557}; 6558 6559/* 6560** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. 6561*/ 6562static int arUsage(FILE *f){ 6563 showHelp(f,"archive"); 6564 return SQLITE_ERROR; 6565} 6566 6567/* 6568** Print an error message for the .ar command to stderr and return 6569** SQLITE_ERROR. 6570*/ 6571static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ 6572 va_list ap; 6573 char *z; 6574 va_start(ap, zFmt); 6575 z = sqlite3_vmprintf(zFmt, ap); 6576 va_end(ap); 6577 utf8_printf(stderr, "Error: %s\n", z); 6578 if( pAr->fromCmdLine ){ 6579 utf8_printf(stderr, "Use \"-A\" for more help\n"); 6580 }else{ 6581 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); 6582 } 6583 sqlite3_free(z); 6584 return SQLITE_ERROR; 6585} 6586 6587/* 6588** Values for ArCommand.eCmd. 6589*/ 6590#define AR_CMD_CREATE 1 6591#define AR_CMD_UPDATE 2 6592#define AR_CMD_INSERT 3 6593#define AR_CMD_EXTRACT 4 6594#define AR_CMD_LIST 5 6595#define AR_CMD_HELP 6 6596#define AR_CMD_REMOVE 7 6597 6598/* 6599** Other (non-command) switches. 6600*/ 6601#define AR_SWITCH_VERBOSE 8 6602#define AR_SWITCH_FILE 9 6603#define AR_SWITCH_DIRECTORY 10 6604#define AR_SWITCH_APPEND 11 6605#define AR_SWITCH_DRYRUN 12 6606#define AR_SWITCH_GLOB 13 6607 6608static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ 6609 switch( eSwitch ){ 6610 case AR_CMD_CREATE: 6611 case AR_CMD_EXTRACT: 6612 case AR_CMD_LIST: 6613 case AR_CMD_REMOVE: 6614 case AR_CMD_UPDATE: 6615 case AR_CMD_INSERT: 6616 case AR_CMD_HELP: 6617 if( pAr->eCmd ){ 6618 return arErrorMsg(pAr, "multiple command options"); 6619 } 6620 pAr->eCmd = eSwitch; 6621 break; 6622 6623 case AR_SWITCH_DRYRUN: 6624 pAr->bDryRun = 1; 6625 break; 6626 case AR_SWITCH_GLOB: 6627 pAr->bGlob = 1; 6628 break; 6629 case AR_SWITCH_VERBOSE: 6630 pAr->bVerbose = 1; 6631 break; 6632 case AR_SWITCH_APPEND: 6633 pAr->bAppend = 1; 6634 /* Fall thru into --file */ 6635 case AR_SWITCH_FILE: 6636 pAr->zFile = zArg; 6637 break; 6638 case AR_SWITCH_DIRECTORY: 6639 pAr->zDir = zArg; 6640 break; 6641 } 6642 6643 return SQLITE_OK; 6644} 6645 6646/* 6647** Parse the command line for an ".ar" command. The results are written into 6648** structure (*pAr). SQLITE_OK is returned if the command line is parsed 6649** successfully, otherwise an error message is written to stderr and 6650** SQLITE_ERROR returned. 6651*/ 6652static int arParseCommand( 6653 char **azArg, /* Array of arguments passed to dot command */ 6654 int nArg, /* Number of entries in azArg[] */ 6655 ArCommand *pAr /* Populate this object */ 6656){ 6657 struct ArSwitch { 6658 const char *zLong; 6659 char cShort; 6660 u8 eSwitch; 6661 u8 bArg; 6662 } aSwitch[] = { 6663 { "create", 'c', AR_CMD_CREATE, 0 }, 6664 { "extract", 'x', AR_CMD_EXTRACT, 0 }, 6665 { "insert", 'i', AR_CMD_INSERT, 0 }, 6666 { "list", 't', AR_CMD_LIST, 0 }, 6667 { "remove", 'r', AR_CMD_REMOVE, 0 }, 6668 { "update", 'u', AR_CMD_UPDATE, 0 }, 6669 { "help", 'h', AR_CMD_HELP, 0 }, 6670 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, 6671 { "file", 'f', AR_SWITCH_FILE, 1 }, 6672 { "append", 'a', AR_SWITCH_APPEND, 1 }, 6673 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, 6674 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, 6675 { "glob", 'g', AR_SWITCH_GLOB, 0 }, 6676 }; 6677 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); 6678 struct ArSwitch *pEnd = &aSwitch[nSwitch]; 6679 6680 if( nArg<=1 ){ 6681 utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); 6682 return arUsage(stderr); 6683 }else{ 6684 char *z = azArg[1]; 6685 if( z[0]!='-' ){ 6686 /* Traditional style [tar] invocation */ 6687 int i; 6688 int iArg = 2; 6689 for(i=0; z[i]; i++){ 6690 const char *zArg = 0; 6691 struct ArSwitch *pOpt; 6692 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6693 if( z[i]==pOpt->cShort ) break; 6694 } 6695 if( pOpt==pEnd ){ 6696 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6697 } 6698 if( pOpt->bArg ){ 6699 if( iArg>=nArg ){ 6700 return arErrorMsg(pAr, "option requires an argument: %c",z[i]); 6701 } 6702 zArg = azArg[iArg++]; 6703 } 6704 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6705 } 6706 pAr->nArg = nArg-iArg; 6707 if( pAr->nArg>0 ){ 6708 pAr->azArg = &azArg[iArg]; 6709 } 6710 }else{ 6711 /* Non-traditional invocation */ 6712 int iArg; 6713 for(iArg=1; iArg<nArg; iArg++){ 6714 int n; 6715 z = azArg[iArg]; 6716 if( z[0]!='-' ){ 6717 /* All remaining command line words are command arguments. */ 6718 pAr->azArg = &azArg[iArg]; 6719 pAr->nArg = nArg-iArg; 6720 break; 6721 } 6722 n = strlen30(z); 6723 6724 if( z[1]!='-' ){ 6725 int i; 6726 /* One or more short options */ 6727 for(i=1; i<n; i++){ 6728 const char *zArg = 0; 6729 struct ArSwitch *pOpt; 6730 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6731 if( z[i]==pOpt->cShort ) break; 6732 } 6733 if( pOpt==pEnd ){ 6734 return arErrorMsg(pAr, "unrecognized option: %c", z[i]); 6735 } 6736 if( pOpt->bArg ){ 6737 if( i<(n-1) ){ 6738 zArg = &z[i+1]; 6739 i = n; 6740 }else{ 6741 if( iArg>=(nArg-1) ){ 6742 return arErrorMsg(pAr, "option requires an argument: %c", 6743 z[i]); 6744 } 6745 zArg = azArg[++iArg]; 6746 } 6747 } 6748 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; 6749 } 6750 }else if( z[2]=='\0' ){ 6751 /* A -- option, indicating that all remaining command line words 6752 ** are command arguments. */ 6753 pAr->azArg = &azArg[iArg+1]; 6754 pAr->nArg = nArg-iArg-1; 6755 break; 6756 }else{ 6757 /* A long option */ 6758 const char *zArg = 0; /* Argument for option, if any */ 6759 struct ArSwitch *pMatch = 0; /* Matching option */ 6760 struct ArSwitch *pOpt; /* Iterator */ 6761 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ 6762 const char *zLong = pOpt->zLong; 6763 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ 6764 if( pMatch ){ 6765 return arErrorMsg(pAr, "ambiguous option: %s",z); 6766 }else{ 6767 pMatch = pOpt; 6768 } 6769 } 6770 } 6771 6772 if( pMatch==0 ){ 6773 return arErrorMsg(pAr, "unrecognized option: %s", z); 6774 } 6775 if( pMatch->bArg ){ 6776 if( iArg>=(nArg-1) ){ 6777 return arErrorMsg(pAr, "option requires an argument: %s", z); 6778 } 6779 zArg = azArg[++iArg]; 6780 } 6781 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; 6782 } 6783 } 6784 } 6785 } 6786 6787 return SQLITE_OK; 6788} 6789 6790/* 6791** This function assumes that all arguments within the ArCommand.azArg[] 6792** array refer to archive members, as for the --extract, --list or --remove 6793** commands. It checks that each of them are "present". If any specified 6794** file is not present in the archive, an error is printed to stderr and an 6795** error code returned. Otherwise, if all specified arguments are present 6796** in the archive, SQLITE_OK is returned. Here, "present" means either an 6797** exact equality when pAr->bGlob is false or a "name GLOB pattern" match 6798** when pAr->bGlob is true. 6799** 6800** This function strips any trailing '/' characters from each argument. 6801** This is consistent with the way the [tar] command seems to work on 6802** Linux. 6803*/ 6804static int arCheckEntries(ArCommand *pAr){ 6805 int rc = SQLITE_OK; 6806 if( pAr->nArg ){ 6807 int i, j; 6808 sqlite3_stmt *pTest = 0; 6809 const char *zSel = (pAr->bGlob) 6810 ? "SELECT name FROM %s WHERE glob($name,name)" 6811 : "SELECT name FROM %s WHERE name=$name"; 6812 6813 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); 6814 j = sqlite3_bind_parameter_index(pTest, "$name"); 6815 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 6816 char *z = pAr->azArg[i]; 6817 int n = strlen30(z); 6818 int bOk = 0; 6819 while( n>0 && z[n-1]=='/' ) n--; 6820 z[n] = '\0'; 6821 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); 6822 if( SQLITE_ROW==sqlite3_step(pTest) ){ 6823 bOk = 1; 6824 } 6825 shellReset(&rc, pTest); 6826 if( rc==SQLITE_OK && bOk==0 ){ 6827 utf8_printf(stderr, "not found in archive: %s\n", z); 6828 rc = SQLITE_ERROR; 6829 } 6830 } 6831 shellFinalize(&rc, pTest); 6832 } 6833 return rc; 6834} 6835 6836/* 6837** Format a WHERE clause that can be used against the "sqlar" table to 6838** identify all archive members that match the command arguments held 6839** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. 6840** The caller is responsible for eventually calling sqlite3_free() on 6841** any non-NULL (*pzWhere) value. Here, "match" means strict equality 6842** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. 6843*/ 6844static void arWhereClause( 6845 int *pRc, 6846 ArCommand *pAr, 6847 char **pzWhere /* OUT: New WHERE clause */ 6848){ 6849 char *zWhere = 0; 6850 const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; 6851 if( *pRc==SQLITE_OK ){ 6852 if( pAr->nArg==0 ){ 6853 zWhere = sqlite3_mprintf("1"); 6854 }else{ 6855 int i; 6856 const char *zSep = ""; 6857 for(i=0; i<pAr->nArg; i++){ 6858 const char *z = pAr->azArg[i]; 6859 zWhere = sqlite3_mprintf( 6860 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", 6861 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z 6862 ); 6863 if( zWhere==0 ){ 6864 *pRc = SQLITE_NOMEM; 6865 break; 6866 } 6867 zSep = " OR "; 6868 } 6869 } 6870 } 6871 *pzWhere = zWhere; 6872} 6873 6874/* 6875** Implementation of .ar "lisT" command. 6876*/ 6877static int arListCommand(ArCommand *pAr){ 6878 const char *zSql = "SELECT %s FROM %s WHERE %s"; 6879 const char *azCols[] = { 6880 "name", 6881 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" 6882 }; 6883 6884 char *zWhere = 0; 6885 sqlite3_stmt *pSql = 0; 6886 int rc; 6887 6888 rc = arCheckEntries(pAr); 6889 arWhereClause(&rc, pAr, &zWhere); 6890 6891 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], 6892 pAr->zSrcTable, zWhere); 6893 if( pAr->bDryRun ){ 6894 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 6895 }else{ 6896 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 6897 if( pAr->bVerbose ){ 6898 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", 6899 sqlite3_column_text(pSql, 0), 6900 sqlite3_column_int(pSql, 1), 6901 sqlite3_column_text(pSql, 2), 6902 sqlite3_column_text(pSql, 3) 6903 ); 6904 }else{ 6905 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 6906 } 6907 } 6908 } 6909 shellFinalize(&rc, pSql); 6910 sqlite3_free(zWhere); 6911 return rc; 6912} 6913 6914 6915/* 6916** Implementation of .ar "Remove" command. 6917*/ 6918static int arRemoveCommand(ArCommand *pAr){ 6919 int rc = 0; 6920 char *zSql = 0; 6921 char *zWhere = 0; 6922 6923 if( pAr->nArg ){ 6924 /* Verify that args actually exist within the archive before proceeding. 6925 ** And formulate a WHERE clause to match them. */ 6926 rc = arCheckEntries(pAr); 6927 arWhereClause(&rc, pAr, &zWhere); 6928 } 6929 if( rc==SQLITE_OK ){ 6930 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", 6931 pAr->zSrcTable, zWhere); 6932 if( pAr->bDryRun ){ 6933 utf8_printf(pAr->p->out, "%s\n", zSql); 6934 }else{ 6935 char *zErr = 0; 6936 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); 6937 if( rc==SQLITE_OK ){ 6938 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 6939 if( rc!=SQLITE_OK ){ 6940 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 6941 }else{ 6942 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); 6943 } 6944 } 6945 if( zErr ){ 6946 utf8_printf(stdout, "ERROR: %s\n", zErr); 6947 sqlite3_free(zErr); 6948 } 6949 } 6950 } 6951 sqlite3_free(zWhere); 6952 sqlite3_free(zSql); 6953 return rc; 6954} 6955 6956/* 6957** Implementation of .ar "eXtract" command. 6958*/ 6959static int arExtractCommand(ArCommand *pAr){ 6960 const char *zSql1 = 6961 "SELECT " 6962 " ($dir || name)," 6963 " writefile(($dir || name), %s, mode, mtime) " 6964 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" 6965 " AND name NOT GLOB '*..[/\\]*'"; 6966 6967 const char *azExtraArg[] = { 6968 "sqlar_uncompress(data, sz)", 6969 "data" 6970 }; 6971 6972 sqlite3_stmt *pSql = 0; 6973 int rc = SQLITE_OK; 6974 char *zDir = 0; 6975 char *zWhere = 0; 6976 int i, j; 6977 6978 /* If arguments are specified, check that they actually exist within 6979 ** the archive before proceeding. And formulate a WHERE clause to 6980 ** match them. */ 6981 rc = arCheckEntries(pAr); 6982 arWhereClause(&rc, pAr, &zWhere); 6983 6984 if( rc==SQLITE_OK ){ 6985 if( pAr->zDir ){ 6986 zDir = sqlite3_mprintf("%s/", pAr->zDir); 6987 }else{ 6988 zDir = sqlite3_mprintf(""); 6989 } 6990 if( zDir==0 ) rc = SQLITE_NOMEM; 6991 } 6992 6993 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 6994 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere 6995 ); 6996 6997 if( rc==SQLITE_OK ){ 6998 j = sqlite3_bind_parameter_index(pSql, "$dir"); 6999 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); 7000 7001 /* Run the SELECT statement twice. The first time, writefile() is called 7002 ** for all archive members that should be extracted. The second time, 7003 ** only for the directories. This is because the timestamps for 7004 ** extracted directories must be reset after they are populated (as 7005 ** populating them changes the timestamp). */ 7006 for(i=0; i<2; i++){ 7007 j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); 7008 sqlite3_bind_int(pSql, j, i); 7009 if( pAr->bDryRun ){ 7010 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); 7011 }else{ 7012 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ 7013 if( i==0 && pAr->bVerbose ){ 7014 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); 7015 } 7016 } 7017 } 7018 shellReset(&rc, pSql); 7019 } 7020 shellFinalize(&rc, pSql); 7021 } 7022 7023 sqlite3_free(zDir); 7024 sqlite3_free(zWhere); 7025 return rc; 7026} 7027 7028/* 7029** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. 7030*/ 7031static int arExecSql(ArCommand *pAr, const char *zSql){ 7032 int rc; 7033 if( pAr->bDryRun ){ 7034 utf8_printf(pAr->p->out, "%s\n", zSql); 7035 rc = SQLITE_OK; 7036 }else{ 7037 char *zErr = 0; 7038 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); 7039 if( zErr ){ 7040 utf8_printf(stdout, "ERROR: %s\n", zErr); 7041 sqlite3_free(zErr); 7042 } 7043 } 7044 return rc; 7045} 7046 7047 7048/* 7049** Implementation of .ar "create", "insert", and "update" commands. 7050** 7051** create -> Create a new SQL archive 7052** insert -> Insert or reinsert all files listed 7053** update -> Insert files that have changed or that were not 7054** previously in the archive 7055** 7056** Create the "sqlar" table in the database if it does not already exist. 7057** Then add each file in the azFile[] array to the archive. Directories 7058** are added recursively. If argument bVerbose is non-zero, a message is 7059** printed on stdout for each file archived. 7060** 7061** The create command is the same as update, except that it drops 7062** any existing "sqlar" table before beginning. The "insert" command 7063** always overwrites every file named on the command-line, where as 7064** "update" only overwrites if the size or mtime or mode has changed. 7065*/ 7066static int arCreateOrUpdateCommand( 7067 ArCommand *pAr, /* Command arguments and options */ 7068 int bUpdate, /* true for a --create. */ 7069 int bOnlyIfChanged /* Only update if file has changed */ 7070){ 7071 const char *zCreate = 7072 "CREATE TABLE IF NOT EXISTS sqlar(\n" 7073 " name TEXT PRIMARY KEY, -- name of the file\n" 7074 " mode INT, -- access permissions\n" 7075 " mtime INT, -- last modification time\n" 7076 " sz INT, -- original file size\n" 7077 " data BLOB -- compressed content\n" 7078 ")"; 7079 const char *zDrop = "DROP TABLE IF EXISTS sqlar"; 7080 const char *zInsertFmt[2] = { 7081 "REPLACE INTO %s(name,mode,mtime,sz,data)\n" 7082 " SELECT\n" 7083 " %s,\n" 7084 " mode,\n" 7085 " mtime,\n" 7086 " CASE substr(lsmode(mode),1,1)\n" 7087 " WHEN '-' THEN length(data)\n" 7088 " WHEN 'd' THEN 0\n" 7089 " ELSE -1 END,\n" 7090 " sqlar_compress(data)\n" 7091 " FROM fsdir(%Q,%Q) AS disk\n" 7092 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 7093 , 7094 "REPLACE INTO %s(name,mode,mtime,data)\n" 7095 " SELECT\n" 7096 " %s,\n" 7097 " mode,\n" 7098 " mtime,\n" 7099 " data\n" 7100 " FROM fsdir(%Q,%Q) AS disk\n" 7101 " WHERE lsmode(mode) NOT LIKE '?%%'%s;" 7102 }; 7103 int i; /* For iterating through azFile[] */ 7104 int rc; /* Return code */ 7105 const char *zTab = 0; /* SQL table into which to insert */ 7106 char *zSql; 7107 char zTemp[50]; 7108 char *zExists = 0; 7109 7110 arExecSql(pAr, "PRAGMA page_size=512"); 7111 rc = arExecSql(pAr, "SAVEPOINT ar;"); 7112 if( rc!=SQLITE_OK ) return rc; 7113 zTemp[0] = 0; 7114 if( pAr->bZip ){ 7115 /* Initialize the zipfile virtual table, if necessary */ 7116 if( pAr->zFile ){ 7117 sqlite3_uint64 r; 7118 sqlite3_randomness(sizeof(r),&r); 7119 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); 7120 zTab = zTemp; 7121 zSql = sqlite3_mprintf( 7122 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", 7123 zTab, pAr->zFile 7124 ); 7125 rc = arExecSql(pAr, zSql); 7126 sqlite3_free(zSql); 7127 }else{ 7128 zTab = "zip"; 7129 } 7130 }else{ 7131 /* Initialize the table for an SQLAR */ 7132 zTab = "sqlar"; 7133 if( bUpdate==0 ){ 7134 rc = arExecSql(pAr, zDrop); 7135 if( rc!=SQLITE_OK ) goto end_ar_transaction; 7136 } 7137 rc = arExecSql(pAr, zCreate); 7138 } 7139 if( bOnlyIfChanged ){ 7140 zExists = sqlite3_mprintf( 7141 " AND NOT EXISTS(" 7142 "SELECT 1 FROM %s AS mem" 7143 " WHERE mem.name=disk.name" 7144 " AND mem.mtime=disk.mtime" 7145 " AND mem.mode=disk.mode)", zTab); 7146 }else{ 7147 zExists = sqlite3_mprintf(""); 7148 } 7149 if( zExists==0 ) rc = SQLITE_NOMEM; 7150 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ 7151 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, 7152 pAr->bVerbose ? "shell_putsnl(name)" : "name", 7153 pAr->azArg[i], pAr->zDir, zExists); 7154 rc = arExecSql(pAr, zSql2); 7155 sqlite3_free(zSql2); 7156 } 7157end_ar_transaction: 7158 if( rc!=SQLITE_OK ){ 7159 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); 7160 }else{ 7161 rc = arExecSql(pAr, "RELEASE ar;"); 7162 if( pAr->bZip && pAr->zFile ){ 7163 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); 7164 arExecSql(pAr, zSql); 7165 sqlite3_free(zSql); 7166 } 7167 } 7168 sqlite3_free(zExists); 7169 return rc; 7170} 7171 7172/* 7173** Implementation of ".ar" dot command. 7174*/ 7175static int arDotCommand( 7176 ShellState *pState, /* Current shell tool state */ 7177 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ 7178 char **azArg, /* Array of arguments passed to dot command */ 7179 int nArg /* Number of entries in azArg[] */ 7180){ 7181 ArCommand cmd; 7182 int rc; 7183 memset(&cmd, 0, sizeof(cmd)); 7184 cmd.fromCmdLine = fromCmdLine; 7185 rc = arParseCommand(azArg, nArg, &cmd); 7186 if( rc==SQLITE_OK ){ 7187 int eDbType = SHELL_OPEN_UNSPEC; 7188 cmd.p = pState; 7189 cmd.db = pState->db; 7190 if( cmd.zFile ){ 7191 eDbType = deduceDatabaseType(cmd.zFile, 1); 7192 }else{ 7193 eDbType = pState->openMode; 7194 } 7195 if( eDbType==SHELL_OPEN_ZIPFILE ){ 7196 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ 7197 if( cmd.zFile==0 ){ 7198 cmd.zSrcTable = sqlite3_mprintf("zip"); 7199 }else{ 7200 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); 7201 } 7202 } 7203 cmd.bZip = 1; 7204 }else if( cmd.zFile ){ 7205 int flags; 7206 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; 7207 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 7208 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ 7209 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 7210 }else{ 7211 flags = SQLITE_OPEN_READONLY; 7212 } 7213 cmd.db = 0; 7214 if( cmd.bDryRun ){ 7215 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, 7216 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); 7217 } 7218 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 7219 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); 7220 if( rc!=SQLITE_OK ){ 7221 utf8_printf(stderr, "cannot open file: %s (%s)\n", 7222 cmd.zFile, sqlite3_errmsg(cmd.db) 7223 ); 7224 goto end_ar_command; 7225 } 7226 sqlite3_fileio_init(cmd.db, 0, 0); 7227 sqlite3_sqlar_init(cmd.db, 0, 0); 7228 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, 7229 shellPutsFunc, 0, 0); 7230 7231 } 7232 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ 7233 if( cmd.eCmd!=AR_CMD_CREATE 7234 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) 7235 ){ 7236 utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); 7237 rc = SQLITE_ERROR; 7238 goto end_ar_command; 7239 } 7240 cmd.zSrcTable = sqlite3_mprintf("sqlar"); 7241 } 7242 7243 switch( cmd.eCmd ){ 7244 case AR_CMD_CREATE: 7245 rc = arCreateOrUpdateCommand(&cmd, 0, 0); 7246 break; 7247 7248 case AR_CMD_EXTRACT: 7249 rc = arExtractCommand(&cmd); 7250 break; 7251 7252 case AR_CMD_LIST: 7253 rc = arListCommand(&cmd); 7254 break; 7255 7256 case AR_CMD_HELP: 7257 arUsage(pState->out); 7258 break; 7259 7260 case AR_CMD_INSERT: 7261 rc = arCreateOrUpdateCommand(&cmd, 1, 0); 7262 break; 7263 7264 case AR_CMD_REMOVE: 7265 rc = arRemoveCommand(&cmd); 7266 break; 7267 7268 default: 7269 assert( cmd.eCmd==AR_CMD_UPDATE ); 7270 rc = arCreateOrUpdateCommand(&cmd, 1, 1); 7271 break; 7272 } 7273 } 7274end_ar_command: 7275 if( cmd.db!=pState->db ){ 7276 close_db(cmd.db); 7277 } 7278 sqlite3_free(cmd.zSrcTable); 7279 7280 return rc; 7281} 7282/* End of the ".archive" or ".ar" command logic 7283*******************************************************************************/ 7284#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ 7285 7286#if SQLITE_SHELL_HAVE_RECOVER 7287 7288/* 7289** This function is used as a callback by the recover extension. Simply 7290** print the supplied SQL statement to stdout. 7291*/ 7292static int recoverSqlCb(void *pCtx, const char *zSql){ 7293 ShellState *pState = (ShellState*)pCtx; 7294 utf8_printf(pState->out, "%s;\n", zSql); 7295 return SQLITE_OK; 7296} 7297 7298/* 7299** This function is called to recover data from the database. A script 7300** to construct a new database containing all recovered data is output 7301** on stream pState->out. 7302*/ 7303static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ 7304 int rc = SQLITE_OK; 7305 const char *zRecoveryDb = ""; /* Name of "recovery" database */ 7306 const char *zLAF = "lost_and_found"; 7307 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ 7308 int bRowids = 1; /* 0 if --no-rowids */ 7309 sqlite3_recover *p = 0; 7310 int i = 0; 7311 7312 for(i=1; i<nArg; i++){ 7313 char *z = azArg[i]; 7314 int n; 7315 if( z[0]=='-' && z[1]=='-' ) z++; 7316 n = strlen30(z); 7317 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ 7318 bFreelist = 0; 7319 }else 7320 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ 7321 i++; 7322 zRecoveryDb = azArg[i]; 7323 }else 7324 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ 7325 i++; 7326 zLAF = azArg[i]; 7327 }else 7328 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ 7329 bRowids = 0; 7330 } 7331 else{ 7332 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 7333 showHelp(pState->out, azArg[0]); 7334 return 1; 7335 } 7336 } 7337 7338 p = sqlite3_recover_init_sql( 7339 pState->db, "main", recoverSqlCb, (void*)pState 7340 ); 7341 7342 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); 7343 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); 7344 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); 7345 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); 7346 7347 sqlite3_recover_run(p); 7348 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ 7349 const char *zErr = sqlite3_recover_errmsg(p); 7350 int errCode = sqlite3_recover_errcode(p); 7351 raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode); 7352 } 7353 rc = sqlite3_recover_finish(p); 7354 return rc; 7355} 7356#endif /* SQLITE_SHELL_HAVE_RECOVER */ 7357 7358 7359/* 7360 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. 7361 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, 7362 * close db and set it to 0, and return the columns spec, to later 7363 * be sqlite3_free()'ed by the caller. 7364 * The return is 0 when either: 7365 * (a) The db was not initialized and zCol==0 (There are no columns.) 7366 * (b) zCol!=0 (Column was added, db initialized as needed.) 7367 * The 3rd argument, pRenamed, references an out parameter. If the 7368 * pointer is non-zero, its referent will be set to a summary of renames 7369 * done if renaming was necessary, or set to 0 if none was done. The out 7370 * string (if any) must be sqlite3_free()'ed by the caller. 7371 */ 7372#ifdef SHELL_DEBUG 7373#define rc_err_oom_die(rc) \ 7374 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ 7375 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ 7376 fprintf(stderr,"E:%d\n",rc), assert(0) 7377#else 7378static void rc_err_oom_die(int rc){ 7379 if( rc==SQLITE_NOMEM ) shell_check_oom(0); 7380 assert(rc==SQLITE_OK||rc==SQLITE_DONE); 7381} 7382#endif 7383 7384#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ 7385static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); 7386#else /* Otherwise, memory is faster/better for the transient DB. */ 7387static const char *zCOL_DB = ":memory:"; 7388#endif 7389 7390/* Define character (as C string) to separate generated column ordinal 7391 * from protected part of incoming column names. This defaults to "_" 7392 * so that incoming column identifiers that did not need not be quoted 7393 * remain usable without being quoted. It must be one character. 7394 */ 7395#ifndef SHELL_AUTOCOLUMN_SEP 7396# define AUTOCOLUMN_SEP "_" 7397#else 7398# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) 7399#endif 7400 7401static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ 7402 /* Queries and D{D,M}L used here */ 7403 static const char * const zTabMake = "\ 7404CREATE TABLE ColNames(\ 7405 cpos INTEGER PRIMARY KEY,\ 7406 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ 7407CREATE VIEW RepeatedNames AS \ 7408SELECT DISTINCT t.name FROM ColNames t \ 7409WHERE t.name COLLATE NOCASE IN (\ 7410 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ 7411);\ 7412"; 7413 static const char * const zTabFill = "\ 7414INSERT INTO ColNames(name,nlen,chop,reps,suff)\ 7415 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ 7416"; 7417 static const char * const zHasDupes = "\ 7418SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ 7419 <count(name) FROM ColNames\ 7420"; 7421#ifdef SHELL_COLUMN_RENAME_CLEAN 7422 static const char * const zDedoctor = "\ 7423UPDATE ColNames SET chop=iif(\ 7424 (substring(name,nlen,1) BETWEEN '0' AND '9')\ 7425 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\ 7426 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ 7427 0\ 7428)\ 7429"; 7430#endif 7431 static const char * const zSetReps = "\ 7432UPDATE ColNames AS t SET reps=\ 7433(SELECT count(*) FROM ColNames d \ 7434 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ 7435 COLLATE NOCASE\ 7436)\ 7437"; 7438#ifdef SQLITE_ENABLE_MATH_FUNCTIONS 7439 static const char * const zColDigits = "\ 7440SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \ 7441"; 7442#else 7443 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */ 7444 static const char * const zColDigits = "\ 7445SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \ 7446 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \ 7447 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \ 7448"; 7449#endif 7450 static const char * const zRenameRank = 7451#ifdef SHELL_COLUMN_RENAME_CLEAN 7452 "UPDATE ColNames AS t SET suff=" 7453 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" 7454#else /* ...RENAME_MINIMAL_ONE_PASS */ 7455"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ 7456" SELECT 0 AS nlz" 7457" UNION" 7458" SELECT nlz+1 AS nlz FROM Lzn" 7459" WHERE EXISTS(" 7460" SELECT 1" 7461" FROM ColNames t, ColNames o" 7462" WHERE" 7463" iif(t.name IN (SELECT * FROM RepeatedNames)," 7464" printf('%s"AUTOCOLUMN_SEP"%s'," 7465" t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," 7466" t.name" 7467" )" 7468" =" 7469" iif(o.name IN (SELECT * FROM RepeatedNames)," 7470" printf('%s"AUTOCOLUMN_SEP"%s'," 7471" o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," 7472" o.name" 7473" )" 7474" COLLATE NOCASE" 7475" AND o.cpos<>t.cpos" 7476" GROUP BY t.cpos" 7477" )" 7478") UPDATE Colnames AS t SET" 7479" chop = 0," /* No chopping, never touch incoming names. */ 7480" suff = iif(name IN (SELECT * FROM RepeatedNames)," 7481" printf('"AUTOCOLUMN_SEP"%s', substring(" 7482" printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," 7483" ''" 7484" )" 7485#endif 7486 ; 7487 static const char * const zCollectVar = "\ 7488SELECT\ 7489 '('||x'0a'\ 7490 || group_concat(\ 7491 cname||' TEXT',\ 7492 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ 7493 ||')' AS ColsSpec \ 7494FROM (\ 7495 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ 7496 FROM ColNames ORDER BY cpos\ 7497)"; 7498 static const char * const zRenamesDone = 7499 "SELECT group_concat(" 7500 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," 7501 " ','||x'0a')" 7502 "FROM ColNames WHERE suff<>'' OR chop!=0" 7503 ; 7504 int rc; 7505 sqlite3_stmt *pStmt = 0; 7506 assert(pDb!=0); 7507 if( zColNew ){ 7508 /* Add initial or additional column. Init db if necessary. */ 7509 if( *pDb==0 ){ 7510 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; 7511#ifdef SHELL_COLFIX_DB 7512 if(*zCOL_DB!=':') 7513 sqlite3_exec(*pDb,"drop table if exists ColNames;" 7514 "drop view if exists RepeatedNames;",0,0,0); 7515#endif 7516 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); 7517 rc_err_oom_die(rc); 7518 } 7519 assert(*pDb!=0); 7520 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); 7521 rc_err_oom_die(rc); 7522 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); 7523 rc_err_oom_die(rc); 7524 rc = sqlite3_step(pStmt); 7525 rc_err_oom_die(rc); 7526 sqlite3_finalize(pStmt); 7527 return 0; 7528 }else if( *pDb==0 ){ 7529 return 0; 7530 }else{ 7531 /* Formulate the columns spec, close the DB, zero *pDb. */ 7532 char *zColsSpec = 0; 7533 int hasDupes = db_int(*pDb, zHasDupes); 7534 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0; 7535 if( hasDupes ){ 7536#ifdef SHELL_COLUMN_RENAME_CLEAN 7537 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); 7538 rc_err_oom_die(rc); 7539#endif 7540 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); 7541 rc_err_oom_die(rc); 7542 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); 7543 rc_err_oom_die(rc); 7544 sqlite3_bind_int(pStmt, 1, nDigits); 7545 rc = sqlite3_step(pStmt); 7546 sqlite3_finalize(pStmt); 7547 assert(rc==SQLITE_DONE); 7548 } 7549 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */ 7550 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); 7551 rc_err_oom_die(rc); 7552 rc = sqlite3_step(pStmt); 7553 if( rc==SQLITE_ROW ){ 7554 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 7555 }else{ 7556 zColsSpec = 0; 7557 } 7558 if( pzRenamed!=0 ){ 7559 if( !hasDupes ) *pzRenamed = 0; 7560 else{ 7561 sqlite3_finalize(pStmt); 7562 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) 7563 && SQLITE_ROW==sqlite3_step(pStmt) ){ 7564 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 7565 }else 7566 *pzRenamed = 0; 7567 } 7568 } 7569 sqlite3_finalize(pStmt); 7570 sqlite3_close(*pDb); 7571 *pDb = 0; 7572 return zColsSpec; 7573 } 7574} 7575 7576/* 7577** If an input line begins with "." then invoke this routine to 7578** process that line. 7579** 7580** Return 1 on error, 2 to exit, and 0 otherwise. 7581*/ 7582static int do_meta_command(char *zLine, ShellState *p){ 7583 int h = 1; 7584 int nArg = 0; 7585 int n, c; 7586 int rc = 0; 7587 char *azArg[52]; 7588 7589#ifndef SQLITE_OMIT_VIRTUALTABLE 7590 if( p->expert.pExpert ){ 7591 expertFinish(p, 1, 0); 7592 } 7593#endif 7594 7595 /* Parse the input line into tokens. 7596 */ 7597 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ 7598 while( IsSpace(zLine[h]) ){ h++; } 7599 if( zLine[h]==0 ) break; 7600 if( zLine[h]=='\'' || zLine[h]=='"' ){ 7601 int delim = zLine[h++]; 7602 azArg[nArg++] = &zLine[h]; 7603 while( zLine[h] && zLine[h]!=delim ){ 7604 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; 7605 h++; 7606 } 7607 if( zLine[h]==delim ){ 7608 zLine[h++] = 0; 7609 } 7610 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); 7611 }else{ 7612 azArg[nArg++] = &zLine[h]; 7613 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } 7614 if( zLine[h] ) zLine[h++] = 0; 7615 resolve_backslashes(azArg[nArg-1]); 7616 } 7617 } 7618 azArg[nArg] = 0; 7619 7620 /* Process the input line. 7621 */ 7622 if( nArg==0 ) return 0; /* no tokens, no error */ 7623 n = strlen30(azArg[0]); 7624 c = azArg[0][0]; 7625 clearTempFile(p); 7626 7627#ifndef SQLITE_OMIT_AUTHORIZATION 7628 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){ 7629 if( nArg!=2 ){ 7630 raw_printf(stderr, "Usage: .auth ON|OFF\n"); 7631 rc = 1; 7632 goto meta_command_exit; 7633 } 7634 open_db(p, 0); 7635 if( booleanValue(azArg[1]) ){ 7636 sqlite3_set_authorizer(p->db, shellAuth, p); 7637 }else if( p->bSafeModePersist ){ 7638 sqlite3_set_authorizer(p->db, safeModeAuth, p); 7639 }else{ 7640 sqlite3_set_authorizer(p->db, 0, 0); 7641 } 7642 }else 7643#endif 7644 7645#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ 7646 && !defined(SQLITE_SHELL_FIDDLE) 7647 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){ 7648 open_db(p, 0); 7649 failIfSafeMode(p, "cannot run .archive in safe mode"); 7650 rc = arDotCommand(p, 0, azArg, nArg); 7651 }else 7652#endif 7653 7654#ifndef SQLITE_SHELL_FIDDLE 7655 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0) 7656 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0) 7657 ){ 7658 const char *zDestFile = 0; 7659 const char *zDb = 0; 7660 sqlite3 *pDest; 7661 sqlite3_backup *pBackup; 7662 int j; 7663 int bAsync = 0; 7664 const char *zVfs = 0; 7665 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 7666 for(j=1; j<nArg; j++){ 7667 const char *z = azArg[j]; 7668 if( z[0]=='-' ){ 7669 if( z[1]=='-' ) z++; 7670 if( cli_strcmp(z, "-append")==0 ){ 7671 zVfs = "apndvfs"; 7672 }else 7673 if( cli_strcmp(z, "-async")==0 ){ 7674 bAsync = 1; 7675 }else 7676 { 7677 utf8_printf(stderr, "unknown option: %s\n", azArg[j]); 7678 return 1; 7679 } 7680 }else if( zDestFile==0 ){ 7681 zDestFile = azArg[j]; 7682 }else if( zDb==0 ){ 7683 zDb = zDestFile; 7684 zDestFile = azArg[j]; 7685 }else{ 7686 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); 7687 return 1; 7688 } 7689 } 7690 if( zDestFile==0 ){ 7691 raw_printf(stderr, "missing FILENAME argument on .backup\n"); 7692 return 1; 7693 } 7694 if( zDb==0 ) zDb = "main"; 7695 rc = sqlite3_open_v2(zDestFile, &pDest, 7696 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); 7697 if( rc!=SQLITE_OK ){ 7698 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); 7699 close_db(pDest); 7700 return 1; 7701 } 7702 if( bAsync ){ 7703 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", 7704 0, 0, 0); 7705 } 7706 open_db(p, 0); 7707 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); 7708 if( pBackup==0 ){ 7709 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7710 close_db(pDest); 7711 return 1; 7712 } 7713 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 7714 sqlite3_backup_finish(pBackup); 7715 if( rc==SQLITE_DONE ){ 7716 rc = 0; 7717 }else{ 7718 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); 7719 rc = 1; 7720 } 7721 close_db(pDest); 7722 }else 7723#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7724 7725 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){ 7726 if( nArg==2 ){ 7727 bail_on_error = booleanValue(azArg[1]); 7728 }else{ 7729 raw_printf(stderr, "Usage: .bail on|off\n"); 7730 rc = 1; 7731 } 7732 }else 7733 7734 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){ 7735 if( nArg==2 ){ 7736 if( booleanValue(azArg[1]) ){ 7737 setBinaryMode(p->out, 1); 7738 }else{ 7739 setTextMode(p->out, 1); 7740 } 7741 }else{ 7742 raw_printf(stderr, "Usage: .binary on|off\n"); 7743 rc = 1; 7744 } 7745 }else 7746 7747 /* The undocumented ".breakpoint" command causes a call to the no-op 7748 ** routine named test_breakpoint(). 7749 */ 7750 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){ 7751 test_breakpoint(); 7752 }else 7753 7754#ifndef SQLITE_SHELL_FIDDLE 7755 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){ 7756 failIfSafeMode(p, "cannot run .cd in safe mode"); 7757 if( nArg==2 ){ 7758#if defined(_WIN32) || defined(WIN32) 7759 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); 7760 rc = !SetCurrentDirectoryW(z); 7761 sqlite3_free(z); 7762#else 7763 rc = chdir(azArg[1]); 7764#endif 7765 if( rc ){ 7766 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); 7767 rc = 1; 7768 } 7769 }else{ 7770 raw_printf(stderr, "Usage: .cd DIRECTORY\n"); 7771 rc = 1; 7772 } 7773 }else 7774#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7775 7776 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){ 7777 if( nArg==2 ){ 7778 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); 7779 }else{ 7780 raw_printf(stderr, "Usage: .changes on|off\n"); 7781 rc = 1; 7782 } 7783 }else 7784 7785#ifndef SQLITE_SHELL_FIDDLE 7786 /* Cancel output redirection, if it is currently set (by .testcase) 7787 ** Then read the content of the testcase-out.txt file and compare against 7788 ** azArg[1]. If there are differences, report an error and exit. 7789 */ 7790 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){ 7791 char *zRes = 0; 7792 output_reset(p); 7793 if( nArg!=2 ){ 7794 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); 7795 rc = 2; 7796 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ 7797 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); 7798 rc = 2; 7799 }else if( testcase_glob(azArg[1],zRes)==0 ){ 7800 utf8_printf(stderr, 7801 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", 7802 p->zTestcase, azArg[1], zRes); 7803 rc = 1; 7804 }else{ 7805 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); 7806 p->nCheck++; 7807 } 7808 sqlite3_free(zRes); 7809 }else 7810#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7811 7812#ifndef SQLITE_SHELL_FIDDLE 7813 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){ 7814 failIfSafeMode(p, "cannot run .clone in safe mode"); 7815 if( nArg==2 ){ 7816 tryToClone(p, azArg[1]); 7817 }else{ 7818 raw_printf(stderr, "Usage: .clone FILENAME\n"); 7819 rc = 1; 7820 } 7821 }else 7822#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 7823 7824 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){ 7825 if( nArg==1 ){ 7826 /* List available connections */ 7827 int i; 7828 for(i=0; i<ArraySize(p->aAuxDb); i++){ 7829 const char *zFile = p->aAuxDb[i].zDbFilename; 7830 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ 7831 zFile = "(not open)"; 7832 }else if( zFile==0 ){ 7833 zFile = "(memory)"; 7834 }else if( zFile[0]==0 ){ 7835 zFile = "(temporary-file)"; 7836 } 7837 if( p->pAuxDb == &p->aAuxDb[i] ){ 7838 utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile); 7839 }else if( p->aAuxDb[i].db!=0 ){ 7840 utf8_printf(stdout, " %d: %s\n", i, zFile); 7841 } 7842 } 7843 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ 7844 int i = azArg[1][0] - '0'; 7845 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ 7846 p->pAuxDb->db = p->db; 7847 p->pAuxDb = &p->aAuxDb[i]; 7848 globalDb = p->db = p->pAuxDb->db; 7849 p->pAuxDb->db = 0; 7850 } 7851 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0 7852 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ 7853 int i = azArg[2][0] - '0'; 7854 if( i<0 || i>=ArraySize(p->aAuxDb) ){ 7855 /* No-op */ 7856 }else if( p->pAuxDb == &p->aAuxDb[i] ){ 7857 raw_printf(stderr, "cannot close the active database connection\n"); 7858 rc = 1; 7859 }else if( p->aAuxDb[i].db ){ 7860 session_close_all(p, i); 7861 close_db(p->aAuxDb[i].db); 7862 p->aAuxDb[i].db = 0; 7863 } 7864 }else{ 7865 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); 7866 rc = 1; 7867 } 7868 }else 7869 7870 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){ 7871 char **azName = 0; 7872 int nName = 0; 7873 sqlite3_stmt *pStmt; 7874 int i; 7875 open_db(p, 0); 7876 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 7877 if( rc ){ 7878 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 7879 rc = 1; 7880 }else{ 7881 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 7882 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); 7883 const char *zFile = (const char*)sqlite3_column_text(pStmt,2); 7884 if( zSchema==0 || zFile==0 ) continue; 7885 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); 7886 shell_check_oom(azName); 7887 azName[nName*2] = strdup(zSchema); 7888 azName[nName*2+1] = strdup(zFile); 7889 nName++; 7890 } 7891 } 7892 sqlite3_finalize(pStmt); 7893 for(i=0; i<nName; i++){ 7894 int eTxn = sqlite3_txn_state(p->db, azName[i*2]); 7895 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); 7896 const char *z = azName[i*2+1]; 7897 utf8_printf(p->out, "%s: %s %s%s\n", 7898 azName[i*2], 7899 z && z[0] ? z : "\"\"", 7900 bRdonly ? "r/o" : "r/w", 7901 eTxn==SQLITE_TXN_NONE ? "" : 7902 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); 7903 free(azName[i*2]); 7904 free(azName[i*2+1]); 7905 } 7906 sqlite3_free(azName); 7907 }else 7908 7909 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){ 7910 static const struct DbConfigChoices { 7911 const char *zName; 7912 int op; 7913 } aDbConfig[] = { 7914 { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, 7915 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, 7916 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, 7917 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, 7918 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, 7919 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 7920 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, 7921 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 7922 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, 7923 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, 7924 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, 7925 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, 7926 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, 7927 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, 7928 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, 7929 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 7930 }; 7931 int ii, v; 7932 open_db(p, 0); 7933 for(ii=0; ii<ArraySize(aDbConfig); ii++){ 7934 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; 7935 if( nArg>=3 ){ 7936 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); 7937 } 7938 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); 7939 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); 7940 if( nArg>1 ) break; 7941 } 7942 if( nArg>1 && ii==ArraySize(aDbConfig) ){ 7943 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); 7944 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); 7945 } 7946 }else 7947 7948#if SQLITE_SHELL_HAVE_RECOVER 7949 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){ 7950 rc = shell_dbinfo_command(p, nArg, azArg); 7951 }else 7952 7953 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){ 7954 open_db(p, 0); 7955 rc = recoverDatabaseCmd(p, nArg, azArg); 7956 }else 7957#endif /* SQLITE_SHELL_HAVE_RECOVER */ 7958 7959 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){ 7960 char *zLike = 0; 7961 char *zSql; 7962 int i; 7963 int savedShowHeader = p->showHeader; 7964 int savedShellFlags = p->shellFlgs; 7965 ShellClearFlag(p, 7966 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo 7967 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); 7968 for(i=1; i<nArg; i++){ 7969 if( azArg[i][0]=='-' ){ 7970 const char *z = azArg[i]+1; 7971 if( z[0]=='-' ) z++; 7972 if( cli_strcmp(z,"preserve-rowids")==0 ){ 7973#ifdef SQLITE_OMIT_VIRTUALTABLE 7974 raw_printf(stderr, "The --preserve-rowids option is not compatible" 7975 " with SQLITE_OMIT_VIRTUALTABLE\n"); 7976 rc = 1; 7977 sqlite3_free(zLike); 7978 goto meta_command_exit; 7979#else 7980 ShellSetFlag(p, SHFLG_PreserveRowid); 7981#endif 7982 }else 7983 if( cli_strcmp(z,"newlines")==0 ){ 7984 ShellSetFlag(p, SHFLG_Newlines); 7985 }else 7986 if( cli_strcmp(z,"data-only")==0 ){ 7987 ShellSetFlag(p, SHFLG_DumpDataOnly); 7988 }else 7989 if( cli_strcmp(z,"nosys")==0 ){ 7990 ShellSetFlag(p, SHFLG_DumpNoSys); 7991 }else 7992 { 7993 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); 7994 rc = 1; 7995 sqlite3_free(zLike); 7996 goto meta_command_exit; 7997 } 7998 }else{ 7999 /* azArg[i] contains a LIKE pattern. This ".dump" request should 8000 ** only dump data for tables for which either the table name matches 8001 ** the LIKE pattern, or the table appears to be a shadow table of 8002 ** a virtual table for which the name matches the LIKE pattern. 8003 */ 8004 char *zExpr = sqlite3_mprintf( 8005 "name LIKE %Q ESCAPE '\\' OR EXISTS (" 8006 " SELECT 1 FROM sqlite_schema WHERE " 8007 " name LIKE %Q ESCAPE '\\' AND" 8008 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND" 8009 " substr(o.name, 1, length(name)+1) == (name||'_')" 8010 ")", azArg[i], azArg[i] 8011 ); 8012 8013 if( zLike ){ 8014 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr); 8015 }else{ 8016 zLike = zExpr; 8017 } 8018 } 8019 } 8020 8021 open_db(p, 0); 8022 8023 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8024 /* When playing back a "dump", the content might appear in an order 8025 ** which causes immediate foreign key constraints to be violated. 8026 ** So disable foreign-key constraint enforcement to prevent problems. */ 8027 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); 8028 raw_printf(p->out, "BEGIN TRANSACTION;\n"); 8029 } 8030 p->writableSchema = 0; 8031 p->showHeader = 0; 8032 /* Set writable_schema=ON since doing so forces SQLite to initialize 8033 ** as much of the schema as it can even if the sqlite_schema table is 8034 ** corrupt. */ 8035 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); 8036 p->nErr = 0; 8037 if( zLike==0 ) zLike = sqlite3_mprintf("true"); 8038 zSql = sqlite3_mprintf( 8039 "SELECT name, type, sql FROM sqlite_schema AS o " 8040 "WHERE (%s) AND type=='table'" 8041 " AND sql NOT NULL" 8042 " ORDER BY tbl_name='sqlite_sequence', rowid", 8043 zLike 8044 ); 8045 run_schema_dump_query(p,zSql); 8046 sqlite3_free(zSql); 8047 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8048 zSql = sqlite3_mprintf( 8049 "SELECT sql FROM sqlite_schema AS o " 8050 "WHERE (%s) AND sql NOT NULL" 8051 " AND type IN ('index','trigger','view')", 8052 zLike 8053 ); 8054 run_table_dump_query(p, zSql); 8055 sqlite3_free(zSql); 8056 } 8057 sqlite3_free(zLike); 8058 if( p->writableSchema ){ 8059 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); 8060 p->writableSchema = 0; 8061 } 8062 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); 8063 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); 8064 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ 8065 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); 8066 } 8067 p->showHeader = savedShowHeader; 8068 p->shellFlgs = savedShellFlags; 8069 }else 8070 8071 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){ 8072 if( nArg==2 ){ 8073 setOrClearFlag(p, SHFLG_Echo, azArg[1]); 8074 }else{ 8075 raw_printf(stderr, "Usage: .echo on|off\n"); 8076 rc = 1; 8077 } 8078 }else 8079 8080 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){ 8081 if( nArg==2 ){ 8082 p->autoEQPtest = 0; 8083 if( p->autoEQPtrace ){ 8084 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); 8085 p->autoEQPtrace = 0; 8086 } 8087 if( cli_strcmp(azArg[1],"full")==0 ){ 8088 p->autoEQP = AUTOEQP_full; 8089 }else if( cli_strcmp(azArg[1],"trigger")==0 ){ 8090 p->autoEQP = AUTOEQP_trigger; 8091#ifdef SQLITE_DEBUG 8092 }else if( cli_strcmp(azArg[1],"test")==0 ){ 8093 p->autoEQP = AUTOEQP_on; 8094 p->autoEQPtest = 1; 8095 }else if( cli_strcmp(azArg[1],"trace")==0 ){ 8096 p->autoEQP = AUTOEQP_full; 8097 p->autoEQPtrace = 1; 8098 open_db(p, 0); 8099 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); 8100 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); 8101#endif 8102 }else{ 8103 p->autoEQP = (u8)booleanValue(azArg[1]); 8104 } 8105 }else{ 8106 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); 8107 rc = 1; 8108 } 8109 }else 8110 8111#ifndef SQLITE_SHELL_FIDDLE 8112 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){ 8113 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); 8114 rc = 2; 8115 }else 8116#endif 8117 8118 /* The ".explain" command is automatic now. It is largely pointless. It 8119 ** retained purely for backwards compatibility */ 8120 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){ 8121 int val = 1; 8122 if( nArg>=2 ){ 8123 if( cli_strcmp(azArg[1],"auto")==0 ){ 8124 val = 99; 8125 }else{ 8126 val = booleanValue(azArg[1]); 8127 } 8128 } 8129 if( val==1 && p->mode!=MODE_Explain ){ 8130 p->normalMode = p->mode; 8131 p->mode = MODE_Explain; 8132 p->autoExplain = 0; 8133 }else if( val==0 ){ 8134 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 8135 p->autoExplain = 0; 8136 }else if( val==99 ){ 8137 if( p->mode==MODE_Explain ) p->mode = p->normalMode; 8138 p->autoExplain = 1; 8139 } 8140 }else 8141 8142#ifndef SQLITE_OMIT_VIRTUALTABLE 8143 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){ 8144 if( p->bSafeMode ){ 8145 raw_printf(stderr, 8146 "Cannot run experimental commands such as \"%s\" in safe mode\n", 8147 azArg[0]); 8148 rc = 1; 8149 }else{ 8150 open_db(p, 0); 8151 expertDotCommand(p, azArg, nArg); 8152 } 8153 }else 8154#endif 8155 8156 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){ 8157 static const struct { 8158 const char *zCtrlName; /* Name of a test-control option */ 8159 int ctrlCode; /* Integer code for that option */ 8160 const char *zUsage; /* Usage notes */ 8161 } aCtrl[] = { 8162 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, 8163 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, 8164 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, 8165 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, 8166 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, 8167 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ 8168 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, 8169 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, 8170 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, 8171 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, 8172 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ 8173 }; 8174 int filectrl = -1; 8175 int iCtrl = -1; 8176 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ 8177 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ 8178 int n2, i; 8179 const char *zCmd = 0; 8180 const char *zSchema = 0; 8181 8182 open_db(p, 0); 8183 zCmd = nArg>=2 ? azArg[1] : "help"; 8184 8185 if( zCmd[0]=='-' 8186 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) 8187 && nArg>=4 8188 ){ 8189 zSchema = azArg[2]; 8190 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; 8191 nArg -= 2; 8192 zCmd = azArg[1]; 8193 } 8194 8195 /* The argument can optionally begin with "-" or "--" */ 8196 if( zCmd[0]=='-' && zCmd[1] ){ 8197 zCmd++; 8198 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 8199 } 8200 8201 /* --help lists all file-controls */ 8202 if( cli_strcmp(zCmd,"help")==0 ){ 8203 utf8_printf(p->out, "Available file-controls:\n"); 8204 for(i=0; i<ArraySize(aCtrl); i++){ 8205 utf8_printf(p->out, " .filectrl %s %s\n", 8206 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 8207 } 8208 rc = 1; 8209 goto meta_command_exit; 8210 } 8211 8212 /* convert filectrl text option to value. allow any unique prefix 8213 ** of the option name, or a numerical value. */ 8214 n2 = strlen30(zCmd); 8215 for(i=0; i<ArraySize(aCtrl); i++){ 8216 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 8217 if( filectrl<0 ){ 8218 filectrl = aCtrl[i].ctrlCode; 8219 iCtrl = i; 8220 }else{ 8221 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" 8222 "Use \".filectrl --help\" for help\n", zCmd); 8223 rc = 1; 8224 goto meta_command_exit; 8225 } 8226 } 8227 } 8228 if( filectrl<0 ){ 8229 utf8_printf(stderr,"Error: unknown file-control: %s\n" 8230 "Use \".filectrl --help\" for help\n", zCmd); 8231 }else{ 8232 switch(filectrl){ 8233 case SQLITE_FCNTL_SIZE_LIMIT: { 8234 if( nArg!=2 && nArg!=3 ) break; 8235 iRes = nArg==3 ? integerValue(azArg[2]) : -1; 8236 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); 8237 isOk = 1; 8238 break; 8239 } 8240 case SQLITE_FCNTL_LOCK_TIMEOUT: 8241 case SQLITE_FCNTL_CHUNK_SIZE: { 8242 int x; 8243 if( nArg!=3 ) break; 8244 x = (int)integerValue(azArg[2]); 8245 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8246 isOk = 2; 8247 break; 8248 } 8249 case SQLITE_FCNTL_PERSIST_WAL: 8250 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { 8251 int x; 8252 if( nArg!=2 && nArg!=3 ) break; 8253 x = nArg==3 ? booleanValue(azArg[2]) : -1; 8254 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8255 iRes = x; 8256 isOk = 1; 8257 break; 8258 } 8259 case SQLITE_FCNTL_DATA_VERSION: 8260 case SQLITE_FCNTL_HAS_MOVED: { 8261 int x; 8262 if( nArg!=2 ) break; 8263 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8264 iRes = x; 8265 isOk = 1; 8266 break; 8267 } 8268 case SQLITE_FCNTL_TEMPFILENAME: { 8269 char *z = 0; 8270 if( nArg!=2 ) break; 8271 sqlite3_file_control(p->db, zSchema, filectrl, &z); 8272 if( z ){ 8273 utf8_printf(p->out, "%s\n", z); 8274 sqlite3_free(z); 8275 } 8276 isOk = 2; 8277 break; 8278 } 8279 case SQLITE_FCNTL_RESERVE_BYTES: { 8280 int x; 8281 if( nArg>=3 ){ 8282 x = atoi(azArg[2]); 8283 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8284 } 8285 x = -1; 8286 sqlite3_file_control(p->db, zSchema, filectrl, &x); 8287 utf8_printf(p->out,"%d\n", x); 8288 isOk = 2; 8289 break; 8290 } 8291 } 8292 } 8293 if( isOk==0 && iCtrl>=0 ){ 8294 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 8295 rc = 1; 8296 }else if( isOk==1 ){ 8297 char zBuf[100]; 8298 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); 8299 raw_printf(p->out, "%s\n", zBuf); 8300 } 8301 }else 8302 8303 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){ 8304 ShellState data; 8305 int doStats = 0; 8306 memcpy(&data, p, sizeof(data)); 8307 data.showHeader = 0; 8308 data.cMode = data.mode = MODE_Semi; 8309 if( nArg==2 && optionMatch(azArg[1], "indent") ){ 8310 data.cMode = data.mode = MODE_Pretty; 8311 nArg = 1; 8312 } 8313 if( nArg!=1 ){ 8314 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); 8315 rc = 1; 8316 goto meta_command_exit; 8317 } 8318 open_db(p, 0); 8319 rc = sqlite3_exec(p->db, 8320 "SELECT sql FROM" 8321 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 8322 " FROM sqlite_schema UNION ALL" 8323 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " 8324 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 8325 "ORDER BY x", 8326 callback, &data, 0 8327 ); 8328 if( rc==SQLITE_OK ){ 8329 sqlite3_stmt *pStmt; 8330 rc = sqlite3_prepare_v2(p->db, 8331 "SELECT rowid FROM sqlite_schema" 8332 " WHERE name GLOB 'sqlite_stat[134]'", 8333 -1, &pStmt, 0); 8334 doStats = sqlite3_step(pStmt)==SQLITE_ROW; 8335 sqlite3_finalize(pStmt); 8336 } 8337 if( doStats==0 ){ 8338 raw_printf(p->out, "/* No STAT tables available */\n"); 8339 }else{ 8340 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 8341 data.cMode = data.mode = MODE_Insert; 8342 data.zDestTable = "sqlite_stat1"; 8343 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); 8344 data.zDestTable = "sqlite_stat4"; 8345 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); 8346 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); 8347 } 8348 }else 8349 8350 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){ 8351 if( nArg==2 ){ 8352 p->showHeader = booleanValue(azArg[1]); 8353 p->shellFlgs |= SHFLG_HeaderSet; 8354 }else{ 8355 raw_printf(stderr, "Usage: .headers on|off\n"); 8356 rc = 1; 8357 } 8358 }else 8359 8360 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){ 8361 if( nArg>=2 ){ 8362 n = showHelp(p->out, azArg[1]); 8363 if( n==0 ){ 8364 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); 8365 } 8366 }else{ 8367 showHelp(p->out, 0); 8368 } 8369 }else 8370 8371#ifndef SQLITE_SHELL_FIDDLE 8372 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){ 8373 char *zTable = 0; /* Insert data into this table */ 8374 char *zSchema = 0; /* within this schema (may default to "main") */ 8375 char *zFile = 0; /* Name of file to extra content from */ 8376 sqlite3_stmt *pStmt = NULL; /* A statement */ 8377 int nCol; /* Number of columns in the table */ 8378 int nByte; /* Number of bytes in an SQL string */ 8379 int i, j; /* Loop counters */ 8380 int needCommit; /* True to COMMIT or ROLLBACK at end */ 8381 int nSep; /* Number of bytes in p->colSeparator[] */ 8382 char *zSql; /* An SQL statement */ 8383 char *zFullTabName; /* Table name with schema if applicable */ 8384 ImportCtx sCtx; /* Reader context */ 8385 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ 8386 int eVerbose = 0; /* Larger for more console output */ 8387 int nSkip = 0; /* Initial lines to skip */ 8388 int useOutputMode = 1; /* Use output mode to determine separators */ 8389 char *zCreate = 0; /* CREATE TABLE statement text */ 8390 8391 failIfSafeMode(p, "cannot run .import in safe mode"); 8392 memset(&sCtx, 0, sizeof(sCtx)); 8393 if( p->mode==MODE_Ascii ){ 8394 xRead = ascii_read_one_field; 8395 }else{ 8396 xRead = csv_read_one_field; 8397 } 8398 rc = 1; 8399 for(i=1; i<nArg; i++){ 8400 char *z = azArg[i]; 8401 if( z[0]=='-' && z[1]=='-' ) z++; 8402 if( z[0]!='-' ){ 8403 if( zFile==0 ){ 8404 zFile = z; 8405 }else if( zTable==0 ){ 8406 zTable = z; 8407 }else{ 8408 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); 8409 showHelp(p->out, "import"); 8410 goto meta_command_exit; 8411 } 8412 }else if( cli_strcmp(z,"-v")==0 ){ 8413 eVerbose++; 8414 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){ 8415 zSchema = azArg[++i]; 8416 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){ 8417 nSkip = integerValue(azArg[++i]); 8418 }else if( cli_strcmp(z,"-ascii")==0 ){ 8419 sCtx.cColSep = SEP_Unit[0]; 8420 sCtx.cRowSep = SEP_Record[0]; 8421 xRead = ascii_read_one_field; 8422 useOutputMode = 0; 8423 }else if( cli_strcmp(z,"-csv")==0 ){ 8424 sCtx.cColSep = ','; 8425 sCtx.cRowSep = '\n'; 8426 xRead = csv_read_one_field; 8427 useOutputMode = 0; 8428 }else{ 8429 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); 8430 showHelp(p->out, "import"); 8431 goto meta_command_exit; 8432 } 8433 } 8434 if( zTable==0 ){ 8435 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", 8436 zFile==0 ? "FILE" : "TABLE"); 8437 showHelp(p->out, "import"); 8438 goto meta_command_exit; 8439 } 8440 seenInterrupt = 0; 8441 open_db(p, 0); 8442 if( useOutputMode ){ 8443 /* If neither the --csv or --ascii options are specified, then set 8444 ** the column and row separator characters from the output mode. */ 8445 nSep = strlen30(p->colSeparator); 8446 if( nSep==0 ){ 8447 raw_printf(stderr, 8448 "Error: non-null column separator required for import\n"); 8449 goto meta_command_exit; 8450 } 8451 if( nSep>1 ){ 8452 raw_printf(stderr, 8453 "Error: multi-character column separators not allowed" 8454 " for import\n"); 8455 goto meta_command_exit; 8456 } 8457 nSep = strlen30(p->rowSeparator); 8458 if( nSep==0 ){ 8459 raw_printf(stderr, 8460 "Error: non-null row separator required for import\n"); 8461 goto meta_command_exit; 8462 } 8463 if( nSep==2 && p->mode==MODE_Csv 8464 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 8465 ){ 8466 /* When importing CSV (only), if the row separator is set to the 8467 ** default output row separator, change it to the default input 8468 ** row separator. This avoids having to maintain different input 8469 ** and output row separators. */ 8470 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8471 nSep = strlen30(p->rowSeparator); 8472 } 8473 if( nSep>1 ){ 8474 raw_printf(stderr, "Error: multi-character row separators not allowed" 8475 " for import\n"); 8476 goto meta_command_exit; 8477 } 8478 sCtx.cColSep = p->colSeparator[0]; 8479 sCtx.cRowSep = p->rowSeparator[0]; 8480 } 8481 sCtx.zFile = zFile; 8482 sCtx.nLine = 1; 8483 if( sCtx.zFile[0]=='|' ){ 8484#ifdef SQLITE_OMIT_POPEN 8485 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 8486 goto meta_command_exit; 8487#else 8488 sCtx.in = popen(sCtx.zFile+1, "r"); 8489 sCtx.zFile = "<pipe>"; 8490 sCtx.xCloser = pclose; 8491#endif 8492 }else{ 8493 sCtx.in = fopen(sCtx.zFile, "rb"); 8494 sCtx.xCloser = fclose; 8495 } 8496 if( sCtx.in==0 ){ 8497 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); 8498 goto meta_command_exit; 8499 } 8500 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ 8501 char zSep[2]; 8502 zSep[1] = 0; 8503 zSep[0] = sCtx.cColSep; 8504 utf8_printf(p->out, "Column separator "); 8505 output_c_string(p->out, zSep); 8506 utf8_printf(p->out, ", row separator "); 8507 zSep[0] = sCtx.cRowSep; 8508 output_c_string(p->out, zSep); 8509 utf8_printf(p->out, "\n"); 8510 } 8511 sCtx.z = sqlite3_malloc64(120); 8512 if( sCtx.z==0 ){ 8513 import_cleanup(&sCtx); 8514 shell_out_of_memory(); 8515 } 8516 /* Below, resources must be freed before exit. */ 8517 while( (nSkip--)>0 ){ 8518 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} 8519 } 8520 if( zSchema!=0 ){ 8521 zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable); 8522 }else{ 8523 zFullTabName = sqlite3_mprintf("\"%w\"", zTable); 8524 } 8525 zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName); 8526 if( zSql==0 || zFullTabName==0 ){ 8527 import_cleanup(&sCtx); 8528 shell_out_of_memory(); 8529 } 8530 nByte = strlen30(zSql); 8531 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8532 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ 8533 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ 8534 sqlite3 *dbCols = 0; 8535 char *zRenames = 0; 8536 char *zColDefs; 8537 zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName); 8538 while( xRead(&sCtx) ){ 8539 zAutoColumn(sCtx.z, &dbCols, 0); 8540 if( sCtx.cTerm!=sCtx.cColSep ) break; 8541 } 8542 zColDefs = zAutoColumn(0, &dbCols, &zRenames); 8543 if( zRenames!=0 ){ 8544 utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr, 8545 "Columns renamed during .import %s due to duplicates:\n" 8546 "%s\n", sCtx.zFile, zRenames); 8547 sqlite3_free(zRenames); 8548 } 8549 assert(dbCols==0); 8550 if( zColDefs==0 ){ 8551 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); 8552 import_fail: 8553 sqlite3_free(zCreate); 8554 sqlite3_free(zSql); 8555 sqlite3_free(zFullTabName); 8556 import_cleanup(&sCtx); 8557 rc = 1; 8558 goto meta_command_exit; 8559 } 8560 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); 8561 if( eVerbose>=1 ){ 8562 utf8_printf(p->out, "%s\n", zCreate); 8563 } 8564 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); 8565 if( rc ){ 8566 utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); 8567 goto import_fail; 8568 } 8569 sqlite3_free(zCreate); 8570 zCreate = 0; 8571 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8572 } 8573 if( rc ){ 8574 if (pStmt) sqlite3_finalize(pStmt); 8575 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); 8576 goto import_fail; 8577 } 8578 sqlite3_free(zSql); 8579 nCol = sqlite3_column_count(pStmt); 8580 sqlite3_finalize(pStmt); 8581 pStmt = 0; 8582 if( nCol==0 ) return 0; /* no columns, no error */ 8583 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); 8584 if( zSql==0 ){ 8585 import_cleanup(&sCtx); 8586 shell_out_of_memory(); 8587 } 8588 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName); 8589 j = strlen30(zSql); 8590 for(i=1; i<nCol; i++){ 8591 zSql[j++] = ','; 8592 zSql[j++] = '?'; 8593 } 8594 zSql[j++] = ')'; 8595 zSql[j] = 0; 8596 if( eVerbose>=2 ){ 8597 utf8_printf(p->out, "Insert using: %s\n", zSql); 8598 } 8599 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8600 if( rc ){ 8601 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 8602 if (pStmt) sqlite3_finalize(pStmt); 8603 goto import_fail; 8604 } 8605 sqlite3_free(zSql); 8606 sqlite3_free(zFullTabName); 8607 needCommit = sqlite3_get_autocommit(p->db); 8608 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); 8609 do{ 8610 int startLine = sCtx.nLine; 8611 for(i=0; i<nCol; i++){ 8612 char *z = xRead(&sCtx); 8613 /* 8614 ** Did we reach end-of-file before finding any columns? 8615 ** If so, stop instead of NULL filling the remaining columns. 8616 */ 8617 if( z==0 && i==0 ) break; 8618 /* 8619 ** Did we reach end-of-file OR end-of-line before finding any 8620 ** columns in ASCII mode? If so, stop instead of NULL filling 8621 ** the remaining columns. 8622 */ 8623 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; 8624 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); 8625 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ 8626 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8627 "filling the rest with NULL\n", 8628 sCtx.zFile, startLine, nCol, i+1); 8629 i += 2; 8630 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } 8631 } 8632 } 8633 if( sCtx.cTerm==sCtx.cColSep ){ 8634 do{ 8635 xRead(&sCtx); 8636 i++; 8637 }while( sCtx.cTerm==sCtx.cColSep ); 8638 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " 8639 "extras ignored\n", 8640 sCtx.zFile, startLine, nCol, i); 8641 } 8642 if( i>=nCol ){ 8643 sqlite3_step(pStmt); 8644 rc = sqlite3_reset(pStmt); 8645 if( rc!=SQLITE_OK ){ 8646 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, 8647 startLine, sqlite3_errmsg(p->db)); 8648 sCtx.nErr++; 8649 }else{ 8650 sCtx.nRow++; 8651 } 8652 } 8653 }while( sCtx.cTerm!=EOF ); 8654 8655 import_cleanup(&sCtx); 8656 sqlite3_finalize(pStmt); 8657 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); 8658 if( eVerbose>0 ){ 8659 utf8_printf(p->out, 8660 "Added %d rows with %d errors using %d lines of input\n", 8661 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); 8662 } 8663 }else 8664#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 8665 8666#ifndef SQLITE_UNTESTABLE 8667 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){ 8668 char *zSql; 8669 char *zCollist = 0; 8670 sqlite3_stmt *pStmt; 8671 int tnum = 0; 8672 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ 8673 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ 8674 int i; 8675 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ 8676 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" 8677 " .imposter off\n"); 8678 /* Also allowed, but not documented: 8679 ** 8680 ** .imposter TABLE IMPOSTER 8681 ** 8682 ** where TABLE is a WITHOUT ROWID table. In that case, the 8683 ** imposter is another WITHOUT ROWID table with the columns in 8684 ** storage order. */ 8685 rc = 1; 8686 goto meta_command_exit; 8687 } 8688 open_db(p, 0); 8689 if( nArg==2 ){ 8690 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); 8691 goto meta_command_exit; 8692 } 8693 zSql = sqlite3_mprintf( 8694 "SELECT rootpage, 0 FROM sqlite_schema" 8695 " WHERE name='%q' AND type='index'" 8696 "UNION ALL " 8697 "SELECT rootpage, 1 FROM sqlite_schema" 8698 " WHERE name='%q' AND type='table'" 8699 " AND sql LIKE '%%without%%rowid%%'", 8700 azArg[1], azArg[1] 8701 ); 8702 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8703 sqlite3_free(zSql); 8704 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 8705 tnum = sqlite3_column_int(pStmt, 0); 8706 isWO = sqlite3_column_int(pStmt, 1); 8707 } 8708 sqlite3_finalize(pStmt); 8709 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); 8710 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 8711 sqlite3_free(zSql); 8712 i = 0; 8713 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 8714 char zLabel[20]; 8715 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); 8716 i++; 8717 if( zCol==0 ){ 8718 if( sqlite3_column_int(pStmt,1)==-1 ){ 8719 zCol = "_ROWID_"; 8720 }else{ 8721 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); 8722 zCol = zLabel; 8723 } 8724 } 8725 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ 8726 lenPK = (int)strlen(zCollist); 8727 } 8728 if( zCollist==0 ){ 8729 zCollist = sqlite3_mprintf("\"%w\"", zCol); 8730 }else{ 8731 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); 8732 } 8733 } 8734 sqlite3_finalize(pStmt); 8735 if( i==0 || tnum==0 ){ 8736 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); 8737 rc = 1; 8738 sqlite3_free(zCollist); 8739 goto meta_command_exit; 8740 } 8741 if( lenPK==0 ) lenPK = 100000; 8742 zSql = sqlite3_mprintf( 8743 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", 8744 azArg[2], zCollist, lenPK, zCollist); 8745 sqlite3_free(zCollist); 8746 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); 8747 if( rc==SQLITE_OK ){ 8748 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); 8749 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); 8750 if( rc ){ 8751 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); 8752 }else{ 8753 utf8_printf(stdout, "%s;\n", zSql); 8754 raw_printf(stdout, 8755 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", 8756 azArg[1], isWO ? "table" : "index" 8757 ); 8758 } 8759 }else{ 8760 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); 8761 rc = 1; 8762 } 8763 sqlite3_free(zSql); 8764 }else 8765#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ 8766 8767#ifdef SQLITE_ENABLE_IOTRACE 8768 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){ 8769 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); 8770 if( iotrace && iotrace!=stdout ) fclose(iotrace); 8771 iotrace = 0; 8772 if( nArg<2 ){ 8773 sqlite3IoTrace = 0; 8774 }else if( cli_strcmp(azArg[1], "-")==0 ){ 8775 sqlite3IoTrace = iotracePrintf; 8776 iotrace = stdout; 8777 }else{ 8778 iotrace = fopen(azArg[1], "w"); 8779 if( iotrace==0 ){ 8780 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 8781 sqlite3IoTrace = 0; 8782 rc = 1; 8783 }else{ 8784 sqlite3IoTrace = iotracePrintf; 8785 } 8786 } 8787 }else 8788#endif 8789 8790 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){ 8791 static const struct { 8792 const char *zLimitName; /* Name of a limit */ 8793 int limitCode; /* Integer code for that limit */ 8794 } aLimit[] = { 8795 { "length", SQLITE_LIMIT_LENGTH }, 8796 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, 8797 { "column", SQLITE_LIMIT_COLUMN }, 8798 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, 8799 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, 8800 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, 8801 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, 8802 { "attached", SQLITE_LIMIT_ATTACHED }, 8803 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 8804 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, 8805 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, 8806 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, 8807 }; 8808 int i, n2; 8809 open_db(p, 0); 8810 if( nArg==1 ){ 8811 for(i=0; i<ArraySize(aLimit); i++){ 8812 printf("%20s %d\n", aLimit[i].zLimitName, 8813 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); 8814 } 8815 }else if( nArg>3 ){ 8816 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); 8817 rc = 1; 8818 goto meta_command_exit; 8819 }else{ 8820 int iLimit = -1; 8821 n2 = strlen30(azArg[1]); 8822 for(i=0; i<ArraySize(aLimit); i++){ 8823 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ 8824 if( iLimit<0 ){ 8825 iLimit = i; 8826 }else{ 8827 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); 8828 rc = 1; 8829 goto meta_command_exit; 8830 } 8831 } 8832 } 8833 if( iLimit<0 ){ 8834 utf8_printf(stderr, "unknown limit: \"%s\"\n" 8835 "enter \".limits\" with no arguments for a list.\n", 8836 azArg[1]); 8837 rc = 1; 8838 goto meta_command_exit; 8839 } 8840 if( nArg==3 ){ 8841 sqlite3_limit(p->db, aLimit[iLimit].limitCode, 8842 (int)integerValue(azArg[2])); 8843 } 8844 printf("%20s %d\n", aLimit[iLimit].zLimitName, 8845 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); 8846 } 8847 }else 8848 8849 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){ 8850 open_db(p, 0); 8851 lintDotCommand(p, azArg, nArg); 8852 }else 8853 8854#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) 8855 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){ 8856 const char *zFile, *zProc; 8857 char *zErrMsg = 0; 8858 failIfSafeMode(p, "cannot run .load in safe mode"); 8859 if( nArg<2 ){ 8860 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); 8861 rc = 1; 8862 goto meta_command_exit; 8863 } 8864 zFile = azArg[1]; 8865 zProc = nArg>=3 ? azArg[2] : 0; 8866 open_db(p, 0); 8867 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); 8868 if( rc!=SQLITE_OK ){ 8869 utf8_printf(stderr, "Error: %s\n", zErrMsg); 8870 sqlite3_free(zErrMsg); 8871 rc = 1; 8872 } 8873 }else 8874#endif 8875 8876#ifndef SQLITE_SHELL_FIDDLE 8877 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){ 8878 failIfSafeMode(p, "cannot run .log in safe mode"); 8879 if( nArg!=2 ){ 8880 raw_printf(stderr, "Usage: .log FILENAME\n"); 8881 rc = 1; 8882 }else{ 8883 const char *zFile = azArg[1]; 8884 output_file_close(p->pLog); 8885 p->pLog = output_file_open(zFile, 0); 8886 } 8887 }else 8888#endif 8889 8890 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){ 8891 const char *zMode = 0; 8892 const char *zTabname = 0; 8893 int i, n2; 8894 ColModeOpts cmOpts = ColModeOpts_default; 8895 for(i=1; i<nArg; i++){ 8896 const char *z = azArg[i]; 8897 if( optionMatch(z,"wrap") && i+1<nArg ){ 8898 cmOpts.iWrap = integerValue(azArg[++i]); 8899 }else if( optionMatch(z,"ww") ){ 8900 cmOpts.bWordWrap = 1; 8901 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){ 8902 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]); 8903 }else if( optionMatch(z,"quote") ){ 8904 cmOpts.bQuote = 1; 8905 }else if( optionMatch(z,"noquote") ){ 8906 cmOpts.bQuote = 0; 8907 }else if( zMode==0 ){ 8908 zMode = z; 8909 /* Apply defaults for qbox pseudo-mode. If that 8910 * overwrites already-set values, user was informed of this. 8911 */ 8912 if( cli_strcmp(z, "qbox")==0 ){ 8913 ColModeOpts cmo = ColModeOpts_default_qbox; 8914 zMode = "box"; 8915 cmOpts = cmo; 8916 } 8917 }else if( zTabname==0 ){ 8918 zTabname = z; 8919 }else if( z[0]=='-' ){ 8920 utf8_printf(stderr, "unknown option: %s\n", z); 8921 utf8_printf(stderr, "options:\n" 8922 " --noquote\n" 8923 " --quote\n" 8924 " --wordwrap on/off\n" 8925 " --wrap N\n" 8926 " --ww\n"); 8927 rc = 1; 8928 goto meta_command_exit; 8929 }else{ 8930 utf8_printf(stderr, "extra argument: \"%s\"\n", z); 8931 rc = 1; 8932 goto meta_command_exit; 8933 } 8934 } 8935 if( zMode==0 ){ 8936 if( p->mode==MODE_Column 8937 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) 8938 ){ 8939 raw_printf 8940 (p->out, 8941 "current output mode: %s --wrap %d --wordwrap %s --%squote\n", 8942 modeDescr[p->mode], p->cmOpts.iWrap, 8943 p->cmOpts.bWordWrap ? "on" : "off", 8944 p->cmOpts.bQuote ? "" : "no"); 8945 }else{ 8946 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); 8947 } 8948 zMode = modeDescr[p->mode]; 8949 } 8950 n2 = strlen30(zMode); 8951 if( cli_strncmp(zMode,"lines",n2)==0 ){ 8952 p->mode = MODE_Line; 8953 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8954 }else if( cli_strncmp(zMode,"columns",n2)==0 ){ 8955 p->mode = MODE_Column; 8956 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ 8957 p->showHeader = 1; 8958 } 8959 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8960 p->cmOpts = cmOpts; 8961 }else if( cli_strncmp(zMode,"list",n2)==0 ){ 8962 p->mode = MODE_List; 8963 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); 8964 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8965 }else if( cli_strncmp(zMode,"html",n2)==0 ){ 8966 p->mode = MODE_Html; 8967 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){ 8968 p->mode = MODE_Tcl; 8969 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); 8970 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8971 }else if( cli_strncmp(zMode,"csv",n2)==0 ){ 8972 p->mode = MODE_Csv; 8973 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8974 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 8975 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){ 8976 p->mode = MODE_List; 8977 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); 8978 }else if( cli_strncmp(zMode,"insert",n2)==0 ){ 8979 p->mode = MODE_Insert; 8980 set_table_name(p, zTabname ? zTabname : "table"); 8981 }else if( cli_strncmp(zMode,"quote",n2)==0 ){ 8982 p->mode = MODE_Quote; 8983 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 8984 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); 8985 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){ 8986 p->mode = MODE_Ascii; 8987 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); 8988 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); 8989 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){ 8990 p->mode = MODE_Markdown; 8991 p->cmOpts = cmOpts; 8992 }else if( cli_strncmp(zMode,"table",n2)==0 ){ 8993 p->mode = MODE_Table; 8994 p->cmOpts = cmOpts; 8995 }else if( cli_strncmp(zMode,"box",n2)==0 ){ 8996 p->mode = MODE_Box; 8997 p->cmOpts = cmOpts; 8998 }else if( cli_strncmp(zMode,"count",n2)==0 ){ 8999 p->mode = MODE_Count; 9000 }else if( cli_strncmp(zMode,"off",n2)==0 ){ 9001 p->mode = MODE_Off; 9002 }else if( cli_strncmp(zMode,"json",n2)==0 ){ 9003 p->mode = MODE_Json; 9004 }else{ 9005 raw_printf(stderr, "Error: mode should be one of: " 9006 "ascii box column csv html insert json line list markdown " 9007 "qbox quote table tabs tcl\n"); 9008 rc = 1; 9009 } 9010 p->cMode = p->mode; 9011 }else 9012 9013#ifndef SQLITE_SHELL_FIDDLE 9014 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){ 9015 if( nArg!=2 ){ 9016 raw_printf(stderr, "Usage: .nonce NONCE\n"); 9017 rc = 1; 9018 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ 9019 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", 9020 p->lineno, azArg[1]); 9021 exit(1); 9022 }else{ 9023 p->bSafeMode = 0; 9024 return 0; /* Return immediately to bypass the safe mode reset 9025 ** at the end of this procedure */ 9026 } 9027 }else 9028#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9029 9030 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){ 9031 if( nArg==2 ){ 9032 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, 9033 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); 9034 }else{ 9035 raw_printf(stderr, "Usage: .nullvalue STRING\n"); 9036 rc = 1; 9037 } 9038 }else 9039 9040 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){ 9041 const char *zFN = 0; /* Pointer to constant filename */ 9042 char *zNewFilename = 0; /* Name of the database file to open */ 9043 int iName = 1; /* Index in azArg[] of the filename */ 9044 int newFlag = 0; /* True to delete file before opening */ 9045 int openMode = SHELL_OPEN_UNSPEC; 9046 9047 /* Check for command-line arguments */ 9048 for(iName=1; iName<nArg; iName++){ 9049 const char *z = azArg[iName]; 9050#ifndef SQLITE_SHELL_FIDDLE 9051 if( optionMatch(z,"new") ){ 9052 newFlag = 1; 9053#ifdef SQLITE_HAVE_ZLIB 9054 }else if( optionMatch(z, "zip") ){ 9055 openMode = SHELL_OPEN_ZIPFILE; 9056#endif 9057 }else if( optionMatch(z, "append") ){ 9058 openMode = SHELL_OPEN_APPENDVFS; 9059 }else if( optionMatch(z, "readonly") ){ 9060 openMode = SHELL_OPEN_READONLY; 9061 }else if( optionMatch(z, "nofollow") ){ 9062 p->openFlags |= SQLITE_OPEN_NOFOLLOW; 9063#ifndef SQLITE_OMIT_DESERIALIZE 9064 }else if( optionMatch(z, "deserialize") ){ 9065 openMode = SHELL_OPEN_DESERIALIZE; 9066 }else if( optionMatch(z, "hexdb") ){ 9067 openMode = SHELL_OPEN_HEXDB; 9068 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ 9069 p->szMax = integerValue(azArg[++iName]); 9070#endif /* SQLITE_OMIT_DESERIALIZE */ 9071 }else 9072#endif /* !SQLITE_SHELL_FIDDLE */ 9073 if( z[0]=='-' ){ 9074 utf8_printf(stderr, "unknown option: %s\n", z); 9075 rc = 1; 9076 goto meta_command_exit; 9077 }else if( zFN ){ 9078 utf8_printf(stderr, "extra argument: \"%s\"\n", z); 9079 rc = 1; 9080 goto meta_command_exit; 9081 }else{ 9082 zFN = z; 9083 } 9084 } 9085 9086 /* Close the existing database */ 9087 session_close_all(p, -1); 9088 close_db(p->db); 9089 p->db = 0; 9090 p->pAuxDb->zDbFilename = 0; 9091 sqlite3_free(p->pAuxDb->zFreeOnClose); 9092 p->pAuxDb->zFreeOnClose = 0; 9093 p->openMode = openMode; 9094 p->openFlags = 0; 9095 p->szMax = 0; 9096 9097 /* If a filename is specified, try to open it first */ 9098 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ 9099 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); 9100#ifndef SQLITE_SHELL_FIDDLE 9101 if( p->bSafeMode 9102 && p->openMode!=SHELL_OPEN_HEXDB 9103 && zFN 9104 && cli_strcmp(zFN,":memory:")!=0 9105 ){ 9106 failIfSafeMode(p, "cannot open disk-based database files in safe mode"); 9107 } 9108#else 9109 /* WASM mode has its own sandboxed pseudo-filesystem. */ 9110#endif 9111 if( zFN ){ 9112 zNewFilename = sqlite3_mprintf("%s", zFN); 9113 shell_check_oom(zNewFilename); 9114 }else{ 9115 zNewFilename = 0; 9116 } 9117 p->pAuxDb->zDbFilename = zNewFilename; 9118 open_db(p, OPEN_DB_KEEPALIVE); 9119 if( p->db==0 ){ 9120 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); 9121 sqlite3_free(zNewFilename); 9122 }else{ 9123 p->pAuxDb->zFreeOnClose = zNewFilename; 9124 } 9125 } 9126 if( p->db==0 ){ 9127 /* As a fall-back open a TEMP database */ 9128 p->pAuxDb->zDbFilename = 0; 9129 open_db(p, 0); 9130 } 9131 }else 9132 9133#ifndef SQLITE_SHELL_FIDDLE 9134 if( (c=='o' 9135 && (cli_strncmp(azArg[0], "output", n)==0 9136 || cli_strncmp(azArg[0], "once", n)==0)) 9137 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0) 9138 ){ 9139 char *zFile = 0; 9140 int bTxtMode = 0; 9141 int i; 9142 int eMode = 0; 9143 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ 9144 unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */ 9145 9146 zBOM[0] = 0; 9147 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 9148 if( c=='e' ){ 9149 eMode = 'x'; 9150 bOnce = 2; 9151 }else if( cli_strncmp(azArg[0],"once",n)==0 ){ 9152 bOnce = 1; 9153 } 9154 for(i=1; i<nArg; i++){ 9155 char *z = azArg[i]; 9156 if( z[0]=='-' ){ 9157 if( z[1]=='-' ) z++; 9158 if( cli_strcmp(z,"-bom")==0 ){ 9159 zBOM[0] = 0xef; 9160 zBOM[1] = 0xbb; 9161 zBOM[2] = 0xbf; 9162 zBOM[3] = 0; 9163 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){ 9164 eMode = 'x'; /* spreadsheet */ 9165 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){ 9166 eMode = 'e'; /* text editor */ 9167 }else{ 9168 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", 9169 azArg[i]); 9170 showHelp(p->out, azArg[0]); 9171 rc = 1; 9172 goto meta_command_exit; 9173 } 9174 }else if( zFile==0 && eMode!='e' && eMode!='x' ){ 9175 zFile = sqlite3_mprintf("%s", z); 9176 if( zFile && zFile[0]=='|' ){ 9177 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]); 9178 break; 9179 } 9180 }else{ 9181 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", 9182 azArg[i]); 9183 showHelp(p->out, azArg[0]); 9184 rc = 1; 9185 sqlite3_free(zFile); 9186 goto meta_command_exit; 9187 } 9188 } 9189 if( zFile==0 ){ 9190 zFile = sqlite3_mprintf("stdout"); 9191 } 9192 if( bOnce ){ 9193 p->outCount = 2; 9194 }else{ 9195 p->outCount = 0; 9196 } 9197 output_reset(p); 9198#ifndef SQLITE_NOHAVE_SYSTEM 9199 if( eMode=='e' || eMode=='x' ){ 9200 p->doXdgOpen = 1; 9201 outputModePush(p); 9202 if( eMode=='x' ){ 9203 /* spreadsheet mode. Output as CSV. */ 9204 newTempFile(p, "csv"); 9205 ShellClearFlag(p, SHFLG_Echo); 9206 p->mode = MODE_Csv; 9207 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); 9208 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); 9209 }else{ 9210 /* text editor mode */ 9211 newTempFile(p, "txt"); 9212 bTxtMode = 1; 9213 } 9214 sqlite3_free(zFile); 9215 zFile = sqlite3_mprintf("%s", p->zTempFile); 9216 } 9217#endif /* SQLITE_NOHAVE_SYSTEM */ 9218 shell_check_oom(zFile); 9219 if( zFile[0]=='|' ){ 9220#ifdef SQLITE_OMIT_POPEN 9221 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 9222 rc = 1; 9223 p->out = stdout; 9224#else 9225 p->out = popen(zFile + 1, "w"); 9226 if( p->out==0 ){ 9227 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); 9228 p->out = stdout; 9229 rc = 1; 9230 }else{ 9231 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); 9232 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 9233 } 9234#endif 9235 }else{ 9236 p->out = output_file_open(zFile, bTxtMode); 9237 if( p->out==0 ){ 9238 if( cli_strcmp(zFile,"off")!=0 ){ 9239 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); 9240 } 9241 p->out = stdout; 9242 rc = 1; 9243 } else { 9244 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); 9245 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); 9246 } 9247 } 9248 sqlite3_free(zFile); 9249 }else 9250#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9251 9252 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){ 9253 open_db(p,0); 9254 if( nArg<=1 ) goto parameter_syntax_error; 9255 9256 /* .parameter clear 9257 ** Clear all bind parameters by dropping the TEMP table that holds them. 9258 */ 9259 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){ 9260 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", 9261 0, 0, 0); 9262 }else 9263 9264 /* .parameter list 9265 ** List all bind parameters. 9266 */ 9267 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){ 9268 sqlite3_stmt *pStmt = 0; 9269 int rx; 9270 int len = 0; 9271 rx = sqlite3_prepare_v2(p->db, 9272 "SELECT max(length(key)) " 9273 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 9274 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 9275 len = sqlite3_column_int(pStmt, 0); 9276 if( len>40 ) len = 40; 9277 } 9278 sqlite3_finalize(pStmt); 9279 pStmt = 0; 9280 if( len ){ 9281 rx = sqlite3_prepare_v2(p->db, 9282 "SELECT key, quote(value) " 9283 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); 9284 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ 9285 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), 9286 sqlite3_column_text(pStmt,1)); 9287 } 9288 sqlite3_finalize(pStmt); 9289 } 9290 }else 9291 9292 /* .parameter init 9293 ** Make sure the TEMP table used to hold bind parameters exists. 9294 ** Create it if necessary. 9295 */ 9296 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){ 9297 bind_table_init(p); 9298 }else 9299 9300 /* .parameter set NAME VALUE 9301 ** Set or reset a bind parameter. NAME should be the full parameter 9302 ** name exactly as it appears in the query. (ex: $abc, @def). The 9303 ** VALUE can be in either SQL literal notation, or if not it will be 9304 ** understood to be a text string. 9305 */ 9306 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){ 9307 int rx; 9308 char *zSql; 9309 sqlite3_stmt *pStmt; 9310 const char *zKey = azArg[2]; 9311 const char *zValue = azArg[3]; 9312 bind_table_init(p); 9313 zSql = sqlite3_mprintf( 9314 "REPLACE INTO temp.sqlite_parameters(key,value)" 9315 "VALUES(%Q,%s);", zKey, zValue); 9316 shell_check_oom(zSql); 9317 pStmt = 0; 9318 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 9319 sqlite3_free(zSql); 9320 if( rx!=SQLITE_OK ){ 9321 sqlite3_finalize(pStmt); 9322 pStmt = 0; 9323 zSql = sqlite3_mprintf( 9324 "REPLACE INTO temp.sqlite_parameters(key,value)" 9325 "VALUES(%Q,%Q);", zKey, zValue); 9326 shell_check_oom(zSql); 9327 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 9328 sqlite3_free(zSql); 9329 if( rx!=SQLITE_OK ){ 9330 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); 9331 sqlite3_finalize(pStmt); 9332 pStmt = 0; 9333 rc = 1; 9334 } 9335 } 9336 sqlite3_step(pStmt); 9337 sqlite3_finalize(pStmt); 9338 }else 9339 9340 /* .parameter unset NAME 9341 ** Remove the NAME binding from the parameter binding table, if it 9342 ** exists. 9343 */ 9344 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){ 9345 char *zSql = sqlite3_mprintf( 9346 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); 9347 shell_check_oom(zSql); 9348 sqlite3_exec(p->db, zSql, 0, 0, 0); 9349 sqlite3_free(zSql); 9350 }else 9351 /* If no command name matches, show a syntax error */ 9352 parameter_syntax_error: 9353 showHelp(p->out, "parameter"); 9354 }else 9355 9356 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){ 9357 int i; 9358 for(i=1; i<nArg; i++){ 9359 if( i>1 ) raw_printf(p->out, " "); 9360 utf8_printf(p->out, "%s", azArg[i]); 9361 } 9362 raw_printf(p->out, "\n"); 9363 }else 9364 9365#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 9366 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){ 9367 int i; 9368 int nn = 0; 9369 p->flgProgress = 0; 9370 p->mxProgress = 0; 9371 p->nProgress = 0; 9372 for(i=1; i<nArg; i++){ 9373 const char *z = azArg[i]; 9374 if( z[0]=='-' ){ 9375 z++; 9376 if( z[0]=='-' ) z++; 9377 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){ 9378 p->flgProgress |= SHELL_PROGRESS_QUIET; 9379 continue; 9380 } 9381 if( cli_strcmp(z,"reset")==0 ){ 9382 p->flgProgress |= SHELL_PROGRESS_RESET; 9383 continue; 9384 } 9385 if( cli_strcmp(z,"once")==0 ){ 9386 p->flgProgress |= SHELL_PROGRESS_ONCE; 9387 continue; 9388 } 9389 if( cli_strcmp(z,"limit")==0 ){ 9390 if( i+1>=nArg ){ 9391 utf8_printf(stderr, "Error: missing argument on --limit\n"); 9392 rc = 1; 9393 goto meta_command_exit; 9394 }else{ 9395 p->mxProgress = (int)integerValue(azArg[++i]); 9396 } 9397 continue; 9398 } 9399 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); 9400 rc = 1; 9401 goto meta_command_exit; 9402 }else{ 9403 nn = (int)integerValue(z); 9404 } 9405 } 9406 open_db(p, 0); 9407 sqlite3_progress_handler(p->db, nn, progress_handler, p); 9408 }else 9409#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ 9410 9411 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){ 9412 if( nArg >= 2) { 9413 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); 9414 } 9415 if( nArg >= 3) { 9416 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); 9417 } 9418 }else 9419 9420#ifndef SQLITE_SHELL_FIDDLE 9421 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){ 9422 rc = 2; 9423 }else 9424#endif 9425 9426#ifndef SQLITE_SHELL_FIDDLE 9427 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){ 9428 FILE *inSaved = p->in; 9429 int savedLineno = p->lineno; 9430 failIfSafeMode(p, "cannot run .read in safe mode"); 9431 if( nArg!=2 ){ 9432 raw_printf(stderr, "Usage: .read FILE\n"); 9433 rc = 1; 9434 goto meta_command_exit; 9435 } 9436 if( azArg[1][0]=='|' ){ 9437#ifdef SQLITE_OMIT_POPEN 9438 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); 9439 rc = 1; 9440 p->out = stdout; 9441#else 9442 p->in = popen(azArg[1]+1, "r"); 9443 if( p->in==0 ){ 9444 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); 9445 rc = 1; 9446 }else{ 9447 rc = process_input(p); 9448 pclose(p->in); 9449 } 9450#endif 9451 }else if( (p->in = openChrSource(azArg[1]))==0 ){ 9452 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); 9453 rc = 1; 9454 }else{ 9455 rc = process_input(p); 9456 fclose(p->in); 9457 } 9458 p->in = inSaved; 9459 p->lineno = savedLineno; 9460 }else 9461#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9462 9463#ifndef SQLITE_SHELL_FIDDLE 9464 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){ 9465 const char *zSrcFile; 9466 const char *zDb; 9467 sqlite3 *pSrc; 9468 sqlite3_backup *pBackup; 9469 int nTimeout = 0; 9470 9471 failIfSafeMode(p, "cannot run .restore in safe mode"); 9472 if( nArg==2 ){ 9473 zSrcFile = azArg[1]; 9474 zDb = "main"; 9475 }else if( nArg==3 ){ 9476 zSrcFile = azArg[2]; 9477 zDb = azArg[1]; 9478 }else{ 9479 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); 9480 rc = 1; 9481 goto meta_command_exit; 9482 } 9483 rc = sqlite3_open(zSrcFile, &pSrc); 9484 if( rc!=SQLITE_OK ){ 9485 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); 9486 close_db(pSrc); 9487 return 1; 9488 } 9489 open_db(p, 0); 9490 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); 9491 if( pBackup==0 ){ 9492 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9493 close_db(pSrc); 9494 return 1; 9495 } 9496 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 9497 || rc==SQLITE_BUSY ){ 9498 if( rc==SQLITE_BUSY ){ 9499 if( nTimeout++ >= 3 ) break; 9500 sqlite3_sleep(100); 9501 } 9502 } 9503 sqlite3_backup_finish(pBackup); 9504 if( rc==SQLITE_DONE ){ 9505 rc = 0; 9506 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 9507 raw_printf(stderr, "Error: source database is busy\n"); 9508 rc = 1; 9509 }else{ 9510 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9511 rc = 1; 9512 } 9513 close_db(pSrc); 9514 }else 9515#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 9516 9517 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){ 9518 if( nArg==2 ){ 9519 p->scanstatsOn = (u8)booleanValue(azArg[1]); 9520#ifndef SQLITE_ENABLE_STMT_SCANSTATUS 9521 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); 9522#endif 9523 }else{ 9524 raw_printf(stderr, "Usage: .scanstats on|off\n"); 9525 rc = 1; 9526 } 9527 }else 9528 9529 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){ 9530 ShellText sSelect; 9531 ShellState data; 9532 char *zErrMsg = 0; 9533 const char *zDiv = "("; 9534 const char *zName = 0; 9535 int iSchema = 0; 9536 int bDebug = 0; 9537 int bNoSystemTabs = 0; 9538 int ii; 9539 9540 open_db(p, 0); 9541 memcpy(&data, p, sizeof(data)); 9542 data.showHeader = 0; 9543 data.cMode = data.mode = MODE_Semi; 9544 initText(&sSelect); 9545 for(ii=1; ii<nArg; ii++){ 9546 if( optionMatch(azArg[ii],"indent") ){ 9547 data.cMode = data.mode = MODE_Pretty; 9548 }else if( optionMatch(azArg[ii],"debug") ){ 9549 bDebug = 1; 9550 }else if( optionMatch(azArg[ii],"nosys") ){ 9551 bNoSystemTabs = 1; 9552 }else if( azArg[ii][0]=='-' ){ 9553 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]); 9554 rc = 1; 9555 goto meta_command_exit; 9556 }else if( zName==0 ){ 9557 zName = azArg[ii]; 9558 }else{ 9559 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); 9560 rc = 1; 9561 goto meta_command_exit; 9562 } 9563 } 9564 if( zName!=0 ){ 9565 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 9566 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 9567 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 9568 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; 9569 if( isSchema ){ 9570 char *new_argv[2], *new_colv[2]; 9571 new_argv[0] = sqlite3_mprintf( 9572 "CREATE TABLE %s (\n" 9573 " type text,\n" 9574 " name text,\n" 9575 " tbl_name text,\n" 9576 " rootpage integer,\n" 9577 " sql text\n" 9578 ")", zName); 9579 shell_check_oom(new_argv[0]); 9580 new_argv[1] = 0; 9581 new_colv[0] = "sql"; 9582 new_colv[1] = 0; 9583 callback(&data, 1, new_argv, new_colv); 9584 sqlite3_free(new_argv[0]); 9585 } 9586 } 9587 if( zDiv ){ 9588 sqlite3_stmt *pStmt = 0; 9589 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", 9590 -1, &pStmt, 0); 9591 if( rc ){ 9592 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); 9593 sqlite3_finalize(pStmt); 9594 rc = 1; 9595 goto meta_command_exit; 9596 } 9597 appendText(&sSelect, "SELECT sql FROM", 0); 9598 iSchema = 0; 9599 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 9600 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); 9601 char zScNum[30]; 9602 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); 9603 appendText(&sSelect, zDiv, 0); 9604 zDiv = " UNION ALL "; 9605 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); 9606 if( sqlite3_stricmp(zDb, "main")!=0 ){ 9607 appendText(&sSelect, zDb, '\''); 9608 }else{ 9609 appendText(&sSelect, "NULL", 0); 9610 } 9611 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); 9612 appendText(&sSelect, zScNum, 0); 9613 appendText(&sSelect, " AS snum, ", 0); 9614 appendText(&sSelect, zDb, '\''); 9615 appendText(&sSelect, " AS sname FROM ", 0); 9616 appendText(&sSelect, zDb, quoteChar(zDb)); 9617 appendText(&sSelect, ".sqlite_schema", 0); 9618 } 9619 sqlite3_finalize(pStmt); 9620#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 9621 if( zName ){ 9622 appendText(&sSelect, 9623 " UNION ALL SELECT shell_module_schema(name)," 9624 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 9625 0); 9626 } 9627#endif 9628 appendText(&sSelect, ") WHERE ", 0); 9629 if( zName ){ 9630 char *zQarg = sqlite3_mprintf("%Q", zName); 9631 int bGlob; 9632 shell_check_oom(zQarg); 9633 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || 9634 strchr(zName, '[') != 0; 9635 if( strchr(zName, '.') ){ 9636 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); 9637 }else{ 9638 appendText(&sSelect, "lower(tbl_name)", 0); 9639 } 9640 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); 9641 appendText(&sSelect, zQarg, 0); 9642 if( !bGlob ){ 9643 appendText(&sSelect, " ESCAPE '\\' ", 0); 9644 } 9645 appendText(&sSelect, " AND ", 0); 9646 sqlite3_free(zQarg); 9647 } 9648 if( bNoSystemTabs ){ 9649 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); 9650 } 9651 appendText(&sSelect, "sql IS NOT NULL" 9652 " ORDER BY snum, rowid", 0); 9653 if( bDebug ){ 9654 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); 9655 }else{ 9656 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); 9657 } 9658 freeText(&sSelect); 9659 } 9660 if( zErrMsg ){ 9661 utf8_printf(stderr,"Error: %s\n", zErrMsg); 9662 sqlite3_free(zErrMsg); 9663 rc = 1; 9664 }else if( rc != SQLITE_OK ){ 9665 raw_printf(stderr,"Error: querying schema information\n"); 9666 rc = 1; 9667 }else{ 9668 rc = 0; 9669 } 9670 }else 9671 9672 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0) 9673 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0) 9674 ){ 9675 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; 9676 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); 9677 }else 9678 9679#if defined(SQLITE_ENABLE_SESSION) 9680 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){ 9681 struct AuxDb *pAuxDb = p->pAuxDb; 9682 OpenSession *pSession = &pAuxDb->aSession[0]; 9683 char **azCmd = &azArg[1]; 9684 int iSes = 0; 9685 int nCmd = nArg - 1; 9686 int i; 9687 if( nArg<=1 ) goto session_syntax_error; 9688 open_db(p, 0); 9689 if( nArg>=3 ){ 9690 for(iSes=0; iSes<pAuxDb->nSession; iSes++){ 9691 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; 9692 } 9693 if( iSes<pAuxDb->nSession ){ 9694 pSession = &pAuxDb->aSession[iSes]; 9695 azCmd++; 9696 nCmd--; 9697 }else{ 9698 pSession = &pAuxDb->aSession[0]; 9699 iSes = 0; 9700 } 9701 } 9702 9703 /* .session attach TABLE 9704 ** Invoke the sqlite3session_attach() interface to attach a particular 9705 ** table so that it is never filtered. 9706 */ 9707 if( cli_strcmp(azCmd[0],"attach")==0 ){ 9708 if( nCmd!=2 ) goto session_syntax_error; 9709 if( pSession->p==0 ){ 9710 session_not_open: 9711 raw_printf(stderr, "ERROR: No sessions are open\n"); 9712 }else{ 9713 rc = sqlite3session_attach(pSession->p, azCmd[1]); 9714 if( rc ){ 9715 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); 9716 rc = 0; 9717 } 9718 } 9719 }else 9720 9721 /* .session changeset FILE 9722 ** .session patchset FILE 9723 ** Write a changeset or patchset into a file. The file is overwritten. 9724 */ 9725 if( cli_strcmp(azCmd[0],"changeset")==0 9726 || cli_strcmp(azCmd[0],"patchset")==0 9727 ){ 9728 FILE *out = 0; 9729 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); 9730 if( nCmd!=2 ) goto session_syntax_error; 9731 if( pSession->p==0 ) goto session_not_open; 9732 out = fopen(azCmd[1], "wb"); 9733 if( out==0 ){ 9734 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", 9735 azCmd[1]); 9736 }else{ 9737 int szChng; 9738 void *pChng; 9739 if( azCmd[0][0]=='c' ){ 9740 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); 9741 }else{ 9742 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); 9743 } 9744 if( rc ){ 9745 printf("Error: error code %d\n", rc); 9746 rc = 0; 9747 } 9748 if( pChng 9749 && fwrite(pChng, szChng, 1, out)!=1 ){ 9750 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", 9751 szChng); 9752 } 9753 sqlite3_free(pChng); 9754 fclose(out); 9755 } 9756 }else 9757 9758 /* .session close 9759 ** Close the identified session 9760 */ 9761 if( cli_strcmp(azCmd[0], "close")==0 ){ 9762 if( nCmd!=1 ) goto session_syntax_error; 9763 if( pAuxDb->nSession ){ 9764 session_close(pSession); 9765 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; 9766 } 9767 }else 9768 9769 /* .session enable ?BOOLEAN? 9770 ** Query or set the enable flag 9771 */ 9772 if( cli_strcmp(azCmd[0], "enable")==0 ){ 9773 int ii; 9774 if( nCmd>2 ) goto session_syntax_error; 9775 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9776 if( pAuxDb->nSession ){ 9777 ii = sqlite3session_enable(pSession->p, ii); 9778 utf8_printf(p->out, "session %s enable flag = %d\n", 9779 pSession->zName, ii); 9780 } 9781 }else 9782 9783 /* .session filter GLOB .... 9784 ** Set a list of GLOB patterns of table names to be excluded. 9785 */ 9786 if( cli_strcmp(azCmd[0], "filter")==0 ){ 9787 int ii, nByte; 9788 if( nCmd<2 ) goto session_syntax_error; 9789 if( pAuxDb->nSession ){ 9790 for(ii=0; ii<pSession->nFilter; ii++){ 9791 sqlite3_free(pSession->azFilter[ii]); 9792 } 9793 sqlite3_free(pSession->azFilter); 9794 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); 9795 pSession->azFilter = sqlite3_malloc( nByte ); 9796 if( pSession->azFilter==0 ){ 9797 raw_printf(stderr, "Error: out or memory\n"); 9798 exit(1); 9799 } 9800 for(ii=1; ii<nCmd; ii++){ 9801 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); 9802 shell_check_oom(x); 9803 } 9804 pSession->nFilter = ii-1; 9805 } 9806 }else 9807 9808 /* .session indirect ?BOOLEAN? 9809 ** Query or set the indirect flag 9810 */ 9811 if( cli_strcmp(azCmd[0], "indirect")==0 ){ 9812 int ii; 9813 if( nCmd>2 ) goto session_syntax_error; 9814 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); 9815 if( pAuxDb->nSession ){ 9816 ii = sqlite3session_indirect(pSession->p, ii); 9817 utf8_printf(p->out, "session %s indirect flag = %d\n", 9818 pSession->zName, ii); 9819 } 9820 }else 9821 9822 /* .session isempty 9823 ** Determine if the session is empty 9824 */ 9825 if( cli_strcmp(azCmd[0], "isempty")==0 ){ 9826 int ii; 9827 if( nCmd!=1 ) goto session_syntax_error; 9828 if( pAuxDb->nSession ){ 9829 ii = sqlite3session_isempty(pSession->p); 9830 utf8_printf(p->out, "session %s isempty flag = %d\n", 9831 pSession->zName, ii); 9832 } 9833 }else 9834 9835 /* .session list 9836 ** List all currently open sessions 9837 */ 9838 if( cli_strcmp(azCmd[0],"list")==0 ){ 9839 for(i=0; i<pAuxDb->nSession; i++){ 9840 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); 9841 } 9842 }else 9843 9844 /* .session open DB NAME 9845 ** Open a new session called NAME on the attached database DB. 9846 ** DB is normally "main". 9847 */ 9848 if( cli_strcmp(azCmd[0],"open")==0 ){ 9849 char *zName; 9850 if( nCmd!=3 ) goto session_syntax_error; 9851 zName = azCmd[2]; 9852 if( zName[0]==0 ) goto session_syntax_error; 9853 for(i=0; i<pAuxDb->nSession; i++){ 9854 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ 9855 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); 9856 goto meta_command_exit; 9857 } 9858 } 9859 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ 9860 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); 9861 goto meta_command_exit; 9862 } 9863 pSession = &pAuxDb->aSession[pAuxDb->nSession]; 9864 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); 9865 if( rc ){ 9866 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); 9867 rc = 0; 9868 goto meta_command_exit; 9869 } 9870 pSession->nFilter = 0; 9871 sqlite3session_table_filter(pSession->p, session_filter, pSession); 9872 pAuxDb->nSession++; 9873 pSession->zName = sqlite3_mprintf("%s", zName); 9874 shell_check_oom(pSession->zName); 9875 }else 9876 /* If no command name matches, show a syntax error */ 9877 session_syntax_error: 9878 showHelp(p->out, "session"); 9879 }else 9880#endif 9881 9882#ifdef SQLITE_DEBUG 9883 /* Undocumented commands for internal testing. Subject to change 9884 ** without notice. */ 9885 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ 9886 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ 9887 int i, v; 9888 for(i=1; i<nArg; i++){ 9889 v = booleanValue(azArg[i]); 9890 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); 9891 } 9892 } 9893 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ 9894 int i; sqlite3_int64 v; 9895 for(i=1; i<nArg; i++){ 9896 char zBuf[200]; 9897 v = integerValue(azArg[i]); 9898 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); 9899 utf8_printf(p->out, "%s", zBuf); 9900 } 9901 } 9902 }else 9903#endif 9904 9905 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){ 9906 int bIsInit = 0; /* True to initialize the SELFTEST table */ 9907 int bVerbose = 0; /* Verbose output */ 9908 int bSelftestExists; /* True if SELFTEST already exists */ 9909 int i, k; /* Loop counters */ 9910 int nTest = 0; /* Number of tests runs */ 9911 int nErr = 0; /* Number of errors seen */ 9912 ShellText str; /* Answer for a query */ 9913 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ 9914 9915 open_db(p,0); 9916 for(i=1; i<nArg; i++){ 9917 const char *z = azArg[i]; 9918 if( z[0]=='-' && z[1]=='-' ) z++; 9919 if( cli_strcmp(z,"-init")==0 ){ 9920 bIsInit = 1; 9921 }else 9922 if( cli_strcmp(z,"-v")==0 ){ 9923 bVerbose++; 9924 }else 9925 { 9926 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 9927 azArg[i], azArg[0]); 9928 raw_printf(stderr, "Should be one of: --init -v\n"); 9929 rc = 1; 9930 goto meta_command_exit; 9931 } 9932 } 9933 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) 9934 != SQLITE_OK ){ 9935 bSelftestExists = 0; 9936 }else{ 9937 bSelftestExists = 1; 9938 } 9939 if( bIsInit ){ 9940 createSelftestTable(p); 9941 bSelftestExists = 1; 9942 } 9943 initText(&str); 9944 appendText(&str, "x", 0); 9945 for(k=bSelftestExists; k>=0; k--){ 9946 if( k==1 ){ 9947 rc = sqlite3_prepare_v2(p->db, 9948 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", 9949 -1, &pStmt, 0); 9950 }else{ 9951 rc = sqlite3_prepare_v2(p->db, 9952 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," 9953 " (1,'run','PRAGMA integrity_check','ok')", 9954 -1, &pStmt, 0); 9955 } 9956 if( rc ){ 9957 raw_printf(stderr, "Error querying the selftest table\n"); 9958 rc = 1; 9959 sqlite3_finalize(pStmt); 9960 goto meta_command_exit; 9961 } 9962 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ 9963 int tno = sqlite3_column_int(pStmt, 0); 9964 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); 9965 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); 9966 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); 9967 9968 if( zOp==0 ) continue; 9969 if( zSql==0 ) continue; 9970 if( zAns==0 ) continue; 9971 k = 0; 9972 if( bVerbose>0 ){ 9973 printf("%d: %s %s\n", tno, zOp, zSql); 9974 } 9975 if( cli_strcmp(zOp,"memo")==0 ){ 9976 utf8_printf(p->out, "%s\n", zSql); 9977 }else 9978 if( cli_strcmp(zOp,"run")==0 ){ 9979 char *zErrMsg = 0; 9980 str.n = 0; 9981 str.z[0] = 0; 9982 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); 9983 nTest++; 9984 if( bVerbose ){ 9985 utf8_printf(p->out, "Result: %s\n", str.z); 9986 } 9987 if( rc || zErrMsg ){ 9988 nErr++; 9989 rc = 1; 9990 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); 9991 sqlite3_free(zErrMsg); 9992 }else if( cli_strcmp(zAns,str.z)!=0 ){ 9993 nErr++; 9994 rc = 1; 9995 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); 9996 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); 9997 } 9998 }else 9999 { 10000 utf8_printf(stderr, 10001 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); 10002 rc = 1; 10003 break; 10004 } 10005 } /* End loop over rows of content from SELFTEST */ 10006 sqlite3_finalize(pStmt); 10007 } /* End loop over k */ 10008 freeText(&str); 10009 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); 10010 }else 10011 10012 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){ 10013 if( nArg<2 || nArg>3 ){ 10014 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); 10015 rc = 1; 10016 } 10017 if( nArg>=2 ){ 10018 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, 10019 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); 10020 } 10021 if( nArg>=3 ){ 10022 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, 10023 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); 10024 } 10025 }else 10026 10027 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){ 10028 const char *zLike = 0; /* Which table to checksum. 0 means everything */ 10029 int i; /* Loop counter */ 10030 int bSchema = 0; /* Also hash the schema */ 10031 int bSeparate = 0; /* Hash each table separately */ 10032 int iSize = 224; /* Hash algorithm to use */ 10033 int bDebug = 0; /* Only show the query that would have run */ 10034 sqlite3_stmt *pStmt; /* For querying tables names */ 10035 char *zSql; /* SQL to be run */ 10036 char *zSep; /* Separator */ 10037 ShellText sSql; /* Complete SQL for the query to run the hash */ 10038 ShellText sQuery; /* Set of queries used to read all content */ 10039 open_db(p, 0); 10040 for(i=1; i<nArg; i++){ 10041 const char *z = azArg[i]; 10042 if( z[0]=='-' ){ 10043 z++; 10044 if( z[0]=='-' ) z++; 10045 if( cli_strcmp(z,"schema")==0 ){ 10046 bSchema = 1; 10047 }else 10048 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 10049 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 10050 ){ 10051 iSize = atoi(&z[5]); 10052 }else 10053 if( cli_strcmp(z,"debug")==0 ){ 10054 bDebug = 1; 10055 }else 10056 { 10057 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", 10058 azArg[i], azArg[0]); 10059 showHelp(p->out, azArg[0]); 10060 rc = 1; 10061 goto meta_command_exit; 10062 } 10063 }else if( zLike ){ 10064 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); 10065 rc = 1; 10066 goto meta_command_exit; 10067 }else{ 10068 zLike = z; 10069 bSeparate = 1; 10070 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; 10071 } 10072 } 10073 if( bSchema ){ 10074 zSql = "SELECT lower(name) FROM sqlite_schema" 10075 " WHERE type='table' AND coalesce(rootpage,0)>1" 10076 " UNION ALL SELECT 'sqlite_schema'" 10077 " ORDER BY 1 collate nocase"; 10078 }else{ 10079 zSql = "SELECT lower(name) FROM sqlite_schema" 10080 " WHERE type='table' AND coalesce(rootpage,0)>1" 10081 " AND name NOT LIKE 'sqlite_%'" 10082 " ORDER BY 1 collate nocase"; 10083 } 10084 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); 10085 initText(&sQuery); 10086 initText(&sSql); 10087 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); 10088 zSep = "VALUES("; 10089 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 10090 const char *zTab = (const char*)sqlite3_column_text(pStmt,0); 10091 if( zTab==0 ) continue; 10092 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; 10093 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){ 10094 appendText(&sQuery,"SELECT * FROM ", 0); 10095 appendText(&sQuery,zTab,'"'); 10096 appendText(&sQuery," NOT INDEXED;", 0); 10097 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){ 10098 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" 10099 " ORDER BY name;", 0); 10100 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){ 10101 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" 10102 " ORDER BY name;", 0); 10103 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){ 10104 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" 10105 " ORDER BY tbl,idx;", 0); 10106 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){ 10107 appendText(&sQuery, "SELECT * FROM ", 0); 10108 appendText(&sQuery, zTab, 0); 10109 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); 10110 } 10111 appendText(&sSql, zSep, 0); 10112 appendText(&sSql, sQuery.z, '\''); 10113 sQuery.n = 0; 10114 appendText(&sSql, ",", 0); 10115 appendText(&sSql, zTab, '\''); 10116 zSep = "),("; 10117 } 10118 sqlite3_finalize(pStmt); 10119 if( bSeparate ){ 10120 zSql = sqlite3_mprintf( 10121 "%s))" 10122 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" 10123 " FROM [sha3sum$query]", 10124 sSql.z, iSize); 10125 }else{ 10126 zSql = sqlite3_mprintf( 10127 "%s))" 10128 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" 10129 " FROM [sha3sum$query]", 10130 sSql.z, iSize); 10131 } 10132 shell_check_oom(zSql); 10133 freeText(&sQuery); 10134 freeText(&sSql); 10135 if( bDebug ){ 10136 utf8_printf(p->out, "%s\n", zSql); 10137 }else{ 10138 shell_exec(p, zSql, 0); 10139 } 10140 sqlite3_free(zSql); 10141 }else 10142 10143#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) 10144 if( c=='s' 10145 && (cli_strncmp(azArg[0], "shell", n)==0 10146 || cli_strncmp(azArg[0],"system",n)==0) 10147 ){ 10148 char *zCmd; 10149 int i, x; 10150 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); 10151 if( nArg<2 ){ 10152 raw_printf(stderr, "Usage: .system COMMAND\n"); 10153 rc = 1; 10154 goto meta_command_exit; 10155 } 10156 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); 10157 for(i=2; i<nArg && zCmd!=0; i++){ 10158 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", 10159 zCmd, azArg[i]); 10160 } 10161 x = zCmd!=0 ? system(zCmd) : 1; 10162 sqlite3_free(zCmd); 10163 if( x ) raw_printf(stderr, "System command returns %d\n", x); 10164 }else 10165#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */ 10166 10167 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){ 10168 static const char *azBool[] = { "off", "on", "trigger", "full"}; 10169 const char *zOut; 10170 int i; 10171 if( nArg!=1 ){ 10172 raw_printf(stderr, "Usage: .show\n"); 10173 rc = 1; 10174 goto meta_command_exit; 10175 } 10176 utf8_printf(p->out, "%12.12s: %s\n","echo", 10177 azBool[ShellHasFlag(p, SHFLG_Echo)]); 10178 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); 10179 utf8_printf(p->out, "%12.12s: %s\n","explain", 10180 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); 10181 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); 10182 if( p->mode==MODE_Column 10183 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) 10184 ){ 10185 utf8_printf 10186 (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", 10187 modeDescr[p->mode], p->cmOpts.iWrap, 10188 p->cmOpts.bWordWrap ? "on" : "off", 10189 p->cmOpts.bQuote ? "" : "no"); 10190 }else{ 10191 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); 10192 } 10193 utf8_printf(p->out, "%12.12s: ", "nullvalue"); 10194 output_c_string(p->out, p->nullValue); 10195 raw_printf(p->out, "\n"); 10196 utf8_printf(p->out,"%12.12s: %s\n","output", 10197 strlen30(p->outfile) ? p->outfile : "stdout"); 10198 utf8_printf(p->out,"%12.12s: ", "colseparator"); 10199 output_c_string(p->out, p->colSeparator); 10200 raw_printf(p->out, "\n"); 10201 utf8_printf(p->out,"%12.12s: ", "rowseparator"); 10202 output_c_string(p->out, p->rowSeparator); 10203 raw_printf(p->out, "\n"); 10204 switch( p->statsOn ){ 10205 case 0: zOut = "off"; break; 10206 default: zOut = "on"; break; 10207 case 2: zOut = "stmt"; break; 10208 case 3: zOut = "vmstep"; break; 10209 } 10210 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); 10211 utf8_printf(p->out, "%12.12s: ", "width"); 10212 for (i=0;i<p->nWidth;i++) { 10213 raw_printf(p->out, "%d ", p->colWidth[i]); 10214 } 10215 raw_printf(p->out, "\n"); 10216 utf8_printf(p->out, "%12.12s: %s\n", "filename", 10217 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); 10218 }else 10219 10220 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){ 10221 if( nArg==2 ){ 10222 if( cli_strcmp(azArg[1],"stmt")==0 ){ 10223 p->statsOn = 2; 10224 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){ 10225 p->statsOn = 3; 10226 }else{ 10227 p->statsOn = (u8)booleanValue(azArg[1]); 10228 } 10229 }else if( nArg==1 ){ 10230 display_stats(p->db, p, 0); 10231 }else{ 10232 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n"); 10233 rc = 1; 10234 } 10235 }else 10236 10237 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) 10238 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 10239 || cli_strncmp(azArg[0], "indexes", n)==0) ) 10240 ){ 10241 sqlite3_stmt *pStmt; 10242 char **azResult; 10243 int nRow, nAlloc; 10244 int ii; 10245 ShellText s; 10246 initText(&s); 10247 open_db(p, 0); 10248 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); 10249 if( rc ){ 10250 sqlite3_finalize(pStmt); 10251 return shellDatabaseError(p->db); 10252 } 10253 10254 if( nArg>2 && c=='i' ){ 10255 /* It is an historical accident that the .indexes command shows an error 10256 ** when called with the wrong number of arguments whereas the .tables 10257 ** command does not. */ 10258 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); 10259 rc = 1; 10260 sqlite3_finalize(pStmt); 10261 goto meta_command_exit; 10262 } 10263 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ 10264 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); 10265 if( zDbName==0 ) continue; 10266 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); 10267 if( sqlite3_stricmp(zDbName, "main")==0 ){ 10268 appendText(&s, "SELECT name FROM ", 0); 10269 }else{ 10270 appendText(&s, "SELECT ", 0); 10271 appendText(&s, zDbName, '\''); 10272 appendText(&s, "||'.'||name FROM ", 0); 10273 } 10274 appendText(&s, zDbName, '"'); 10275 appendText(&s, ".sqlite_schema ", 0); 10276 if( c=='t' ){ 10277 appendText(&s," WHERE type IN ('table','view')" 10278 " AND name NOT LIKE 'sqlite_%'" 10279 " AND name LIKE ?1", 0); 10280 }else{ 10281 appendText(&s," WHERE type='index'" 10282 " AND tbl_name LIKE ?1", 0); 10283 } 10284 } 10285 rc = sqlite3_finalize(pStmt); 10286 if( rc==SQLITE_OK ){ 10287 appendText(&s, " ORDER BY 1", 0); 10288 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); 10289 } 10290 freeText(&s); 10291 if( rc ) return shellDatabaseError(p->db); 10292 10293 /* Run the SQL statement prepared by the above block. Store the results 10294 ** as an array of nul-terminated strings in azResult[]. */ 10295 nRow = nAlloc = 0; 10296 azResult = 0; 10297 if( nArg>1 ){ 10298 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); 10299 }else{ 10300 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); 10301 } 10302 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 10303 if( nRow>=nAlloc ){ 10304 char **azNew; 10305 int n2 = nAlloc*2 + 10; 10306 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); 10307 shell_check_oom(azNew); 10308 nAlloc = n2; 10309 azResult = azNew; 10310 } 10311 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 10312 shell_check_oom(azResult[nRow]); 10313 nRow++; 10314 } 10315 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ 10316 rc = shellDatabaseError(p->db); 10317 } 10318 10319 /* Pretty-print the contents of array azResult[] to the output */ 10320 if( rc==0 && nRow>0 ){ 10321 int len, maxlen = 0; 10322 int i, j; 10323 int nPrintCol, nPrintRow; 10324 for(i=0; i<nRow; i++){ 10325 len = strlen30(azResult[i]); 10326 if( len>maxlen ) maxlen = len; 10327 } 10328 nPrintCol = 80/(maxlen+2); 10329 if( nPrintCol<1 ) nPrintCol = 1; 10330 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; 10331 for(i=0; i<nPrintRow; i++){ 10332 for(j=i; j<nRow; j+=nPrintRow){ 10333 char *zSp = j<nPrintRow ? "" : " "; 10334 utf8_printf(p->out, "%s%-*s", zSp, maxlen, 10335 azResult[j] ? azResult[j]:""); 10336 } 10337 raw_printf(p->out, "\n"); 10338 } 10339 } 10340 10341 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); 10342 sqlite3_free(azResult); 10343 }else 10344 10345#ifndef SQLITE_SHELL_FIDDLE 10346 /* Begin redirecting output to the file "testcase-out.txt" */ 10347 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){ 10348 output_reset(p); 10349 p->out = output_file_open("testcase-out.txt", 0); 10350 if( p->out==0 ){ 10351 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); 10352 } 10353 if( nArg>=2 ){ 10354 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); 10355 }else{ 10356 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); 10357 } 10358 }else 10359#endif /* !defined(SQLITE_SHELL_FIDDLE) */ 10360 10361#ifndef SQLITE_UNTESTABLE 10362 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){ 10363 static const struct { 10364 const char *zCtrlName; /* Name of a test-control option */ 10365 int ctrlCode; /* Integer code for that option */ 10366 int unSafe; /* Not valid for --safe mode */ 10367 const char *zUsage; /* Usage notes */ 10368 } aCtrl[] = { 10369 { "always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, 10370 { "assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, 10371 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ 10372 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/ 10373 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, 10374 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, 10375 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/ 10376 { "imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, 10377 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, 10378 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, 10379 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, 10380 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, 10381#ifdef YYCOVERAGE 10382 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, 10383#endif 10384 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " }, 10385 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, 10386 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, 10387 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, 10388 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, 10389 { "sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, 10390 { "tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, 10391 }; 10392 int testctrl = -1; 10393 int iCtrl = -1; 10394 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ 10395 int isOk = 0; 10396 int i, n2; 10397 const char *zCmd = 0; 10398 10399 open_db(p, 0); 10400 zCmd = nArg>=2 ? azArg[1] : "help"; 10401 10402 /* The argument can optionally begin with "-" or "--" */ 10403 if( zCmd[0]=='-' && zCmd[1] ){ 10404 zCmd++; 10405 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; 10406 } 10407 10408 /* --help lists all test-controls */ 10409 if( cli_strcmp(zCmd,"help")==0 ){ 10410 utf8_printf(p->out, "Available test-controls:\n"); 10411 for(i=0; i<ArraySize(aCtrl); i++){ 10412 utf8_printf(p->out, " .testctrl %s %s\n", 10413 aCtrl[i].zCtrlName, aCtrl[i].zUsage); 10414 } 10415 rc = 1; 10416 goto meta_command_exit; 10417 } 10418 10419 /* convert testctrl text option to value. allow any unique prefix 10420 ** of the option name, or a numerical value. */ 10421 n2 = strlen30(zCmd); 10422 for(i=0; i<ArraySize(aCtrl); i++){ 10423 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ 10424 if( testctrl<0 ){ 10425 testctrl = aCtrl[i].ctrlCode; 10426 iCtrl = i; 10427 }else{ 10428 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" 10429 "Use \".testctrl --help\" for help\n", zCmd); 10430 rc = 1; 10431 goto meta_command_exit; 10432 } 10433 } 10434 } 10435 if( testctrl<0 ){ 10436 utf8_printf(stderr,"Error: unknown test-control: %s\n" 10437 "Use \".testctrl --help\" for help\n", zCmd); 10438 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ 10439 utf8_printf(stderr, 10440 "line %d: \".testctrl %s\" may not be used in safe mode\n", 10441 p->lineno, aCtrl[iCtrl].zCtrlName); 10442 exit(1); 10443 }else{ 10444 switch(testctrl){ 10445 10446 /* sqlite3_test_control(int, db, int) */ 10447 case SQLITE_TESTCTRL_OPTIMIZATIONS: 10448 if( nArg==3 ){ 10449 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); 10450 rc2 = sqlite3_test_control(testctrl, p->db, opt); 10451 isOk = 3; 10452 } 10453 break; 10454 10455 /* sqlite3_test_control(int) */ 10456 case SQLITE_TESTCTRL_PRNG_SAVE: 10457 case SQLITE_TESTCTRL_PRNG_RESTORE: 10458 case SQLITE_TESTCTRL_BYTEORDER: 10459 if( nArg==2 ){ 10460 rc2 = sqlite3_test_control(testctrl); 10461 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; 10462 } 10463 break; 10464 10465 /* sqlite3_test_control(int, uint) */ 10466 case SQLITE_TESTCTRL_PENDING_BYTE: 10467 if( nArg==3 ){ 10468 unsigned int opt = (unsigned int)integerValue(azArg[2]); 10469 rc2 = sqlite3_test_control(testctrl, opt); 10470 isOk = 3; 10471 } 10472 break; 10473 10474 /* sqlite3_test_control(int, int, sqlite3*) */ 10475 case SQLITE_TESTCTRL_PRNG_SEED: 10476 if( nArg==3 || nArg==4 ){ 10477 int ii = (int)integerValue(azArg[2]); 10478 sqlite3 *db; 10479 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){ 10480 sqlite3_randomness(sizeof(ii),&ii); 10481 printf("-- random seed: %d\n", ii); 10482 } 10483 if( nArg==3 ){ 10484 db = 0; 10485 }else{ 10486 db = p->db; 10487 /* Make sure the schema has been loaded */ 10488 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); 10489 } 10490 rc2 = sqlite3_test_control(testctrl, ii, db); 10491 isOk = 3; 10492 } 10493 break; 10494 10495 /* sqlite3_test_control(int, int) */ 10496 case SQLITE_TESTCTRL_ASSERT: 10497 case SQLITE_TESTCTRL_ALWAYS: 10498 if( nArg==3 ){ 10499 int opt = booleanValue(azArg[2]); 10500 rc2 = sqlite3_test_control(testctrl, opt); 10501 isOk = 1; 10502 } 10503 break; 10504 10505 /* sqlite3_test_control(int, int) */ 10506 case SQLITE_TESTCTRL_LOCALTIME_FAULT: 10507 case SQLITE_TESTCTRL_NEVER_CORRUPT: 10508 if( nArg==3 ){ 10509 int opt = booleanValue(azArg[2]); 10510 rc2 = sqlite3_test_control(testctrl, opt); 10511 isOk = 3; 10512 } 10513 break; 10514 10515 /* sqlite3_test_control(sqlite3*) */ 10516 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: 10517 rc2 = sqlite3_test_control(testctrl, p->db); 10518 isOk = 3; 10519 break; 10520 10521 case SQLITE_TESTCTRL_IMPOSTER: 10522 if( nArg==5 ){ 10523 rc2 = sqlite3_test_control(testctrl, p->db, 10524 azArg[2], 10525 integerValue(azArg[3]), 10526 integerValue(azArg[4])); 10527 isOk = 3; 10528 } 10529 break; 10530 10531 case SQLITE_TESTCTRL_SEEK_COUNT: { 10532 u64 x = 0; 10533 rc2 = sqlite3_test_control(testctrl, p->db, &x); 10534 utf8_printf(p->out, "%llu\n", x); 10535 isOk = 3; 10536 break; 10537 } 10538 10539#ifdef YYCOVERAGE 10540 case SQLITE_TESTCTRL_PARSER_COVERAGE: { 10541 if( nArg==2 ){ 10542 sqlite3_test_control(testctrl, p->out); 10543 isOk = 3; 10544 } 10545 break; 10546 } 10547#endif 10548#ifdef SQLITE_DEBUG 10549 case SQLITE_TESTCTRL_TUNE: { 10550 if( nArg==4 ){ 10551 int id = (int)integerValue(azArg[2]); 10552 int val = (int)integerValue(azArg[3]); 10553 sqlite3_test_control(testctrl, id, &val); 10554 isOk = 3; 10555 }else if( nArg==3 ){ 10556 int id = (int)integerValue(azArg[2]); 10557 sqlite3_test_control(testctrl, -id, &rc2); 10558 isOk = 1; 10559 }else if( nArg==2 ){ 10560 int id = 1; 10561 while(1){ 10562 int val = 0; 10563 rc2 = sqlite3_test_control(testctrl, -id, &val); 10564 if( rc2!=SQLITE_OK ) break; 10565 if( id>1 ) utf8_printf(p->out, " "); 10566 utf8_printf(p->out, "%d: %d", id, val); 10567 id++; 10568 } 10569 if( id>1 ) utf8_printf(p->out, "\n"); 10570 isOk = 3; 10571 } 10572 break; 10573 } 10574#endif 10575 case SQLITE_TESTCTRL_SORTER_MMAP: 10576 if( nArg==3 ){ 10577 int opt = (unsigned int)integerValue(azArg[2]); 10578 rc2 = sqlite3_test_control(testctrl, p->db, opt); 10579 isOk = 3; 10580 } 10581 break; 10582 } 10583 } 10584 if( isOk==0 && iCtrl>=0 ){ 10585 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); 10586 rc = 1; 10587 }else if( isOk==1 ){ 10588 raw_printf(p->out, "%d\n", rc2); 10589 }else if( isOk==2 ){ 10590 raw_printf(p->out, "0x%08x\n", rc2); 10591 } 10592 }else 10593#endif /* !defined(SQLITE_UNTESTABLE) */ 10594 10595 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){ 10596 open_db(p, 0); 10597 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); 10598 }else 10599 10600 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){ 10601 if( nArg==2 ){ 10602 enableTimer = booleanValue(azArg[1]); 10603 if( enableTimer && !HAS_TIMER ){ 10604 raw_printf(stderr, "Error: timer not available on this system.\n"); 10605 enableTimer = 0; 10606 } 10607 }else{ 10608 raw_printf(stderr, "Usage: .timer on|off\n"); 10609 rc = 1; 10610 } 10611 }else 10612 10613#ifndef SQLITE_OMIT_TRACE 10614 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){ 10615 int mType = 0; 10616 int jj; 10617 open_db(p, 0); 10618 for(jj=1; jj<nArg; jj++){ 10619 const char *z = azArg[jj]; 10620 if( z[0]=='-' ){ 10621 if( optionMatch(z, "expanded") ){ 10622 p->eTraceType = SHELL_TRACE_EXPANDED; 10623 } 10624#ifdef SQLITE_ENABLE_NORMALIZE 10625 else if( optionMatch(z, "normalized") ){ 10626 p->eTraceType = SHELL_TRACE_NORMALIZED; 10627 } 10628#endif 10629 else if( optionMatch(z, "plain") ){ 10630 p->eTraceType = SHELL_TRACE_PLAIN; 10631 } 10632 else if( optionMatch(z, "profile") ){ 10633 mType |= SQLITE_TRACE_PROFILE; 10634 } 10635 else if( optionMatch(z, "row") ){ 10636 mType |= SQLITE_TRACE_ROW; 10637 } 10638 else if( optionMatch(z, "stmt") ){ 10639 mType |= SQLITE_TRACE_STMT; 10640 } 10641 else if( optionMatch(z, "close") ){ 10642 mType |= SQLITE_TRACE_CLOSE; 10643 } 10644 else { 10645 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); 10646 rc = 1; 10647 goto meta_command_exit; 10648 } 10649 }else{ 10650 output_file_close(p->traceOut); 10651 p->traceOut = output_file_open(azArg[1], 0); 10652 } 10653 } 10654 if( p->traceOut==0 ){ 10655 sqlite3_trace_v2(p->db, 0, 0, 0); 10656 }else{ 10657 if( mType==0 ) mType = SQLITE_TRACE_STMT; 10658 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); 10659 } 10660 }else 10661#endif /* !defined(SQLITE_OMIT_TRACE) */ 10662 10663#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) 10664 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){ 10665 int ii; 10666 int lenOpt; 10667 char *zOpt; 10668 if( nArg<2 ){ 10669 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); 10670 rc = 1; 10671 goto meta_command_exit; 10672 } 10673 open_db(p, 0); 10674 zOpt = azArg[1]; 10675 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; 10676 lenOpt = (int)strlen(zOpt); 10677 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ 10678 assert( azArg[nArg]==0 ); 10679 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); 10680 }else{ 10681 for(ii=1; ii<nArg; ii++){ 10682 sqlite3_create_module(p->db, azArg[ii], 0, 0); 10683 } 10684 } 10685 }else 10686#endif 10687 10688#if SQLITE_USER_AUTHENTICATION 10689 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){ 10690 if( nArg<2 ){ 10691 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); 10692 rc = 1; 10693 goto meta_command_exit; 10694 } 10695 open_db(p, 0); 10696 if( cli_strcmp(azArg[1],"login")==0 ){ 10697 if( nArg!=4 ){ 10698 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); 10699 rc = 1; 10700 goto meta_command_exit; 10701 } 10702 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], 10703 strlen30(azArg[3])); 10704 if( rc ){ 10705 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); 10706 rc = 1; 10707 } 10708 }else if( cli_strcmp(azArg[1],"add")==0 ){ 10709 if( nArg!=5 ){ 10710 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); 10711 rc = 1; 10712 goto meta_command_exit; 10713 } 10714 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10715 booleanValue(azArg[4])); 10716 if( rc ){ 10717 raw_printf(stderr, "User-Add failed: %d\n", rc); 10718 rc = 1; 10719 } 10720 }else if( cli_strcmp(azArg[1],"edit")==0 ){ 10721 if( nArg!=5 ){ 10722 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); 10723 rc = 1; 10724 goto meta_command_exit; 10725 } 10726 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), 10727 booleanValue(azArg[4])); 10728 if( rc ){ 10729 raw_printf(stderr, "User-Edit failed: %d\n", rc); 10730 rc = 1; 10731 } 10732 }else if( cli_strcmp(azArg[1],"delete")==0 ){ 10733 if( nArg!=3 ){ 10734 raw_printf(stderr, "Usage: .user delete USER\n"); 10735 rc = 1; 10736 goto meta_command_exit; 10737 } 10738 rc = sqlite3_user_delete(p->db, azArg[2]); 10739 if( rc ){ 10740 raw_printf(stderr, "User-Delete failed: %d\n", rc); 10741 rc = 1; 10742 } 10743 }else{ 10744 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); 10745 rc = 1; 10746 goto meta_command_exit; 10747 } 10748 }else 10749#endif /* SQLITE_USER_AUTHENTICATION */ 10750 10751 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){ 10752 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, 10753 sqlite3_libversion(), sqlite3_sourceid()); 10754#if SQLITE_HAVE_ZLIB 10755 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); 10756#endif 10757#define CTIMEOPT_VAL_(opt) #opt 10758#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) 10759#if defined(__clang__) && defined(__clang_major__) 10760 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." 10761 CTIMEOPT_VAL(__clang_minor__) "." 10762 CTIMEOPT_VAL(__clang_patchlevel__) "\n"); 10763#elif defined(_MSC_VER) 10764 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); 10765#elif defined(__GNUC__) && defined(__VERSION__) 10766 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); 10767#endif 10768 }else 10769 10770 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){ 10771 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10772 sqlite3_vfs *pVfs = 0; 10773 if( p->db ){ 10774 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); 10775 if( pVfs ){ 10776 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); 10777 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10778 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10779 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10780 } 10781 } 10782 }else 10783 10784 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){ 10785 sqlite3_vfs *pVfs; 10786 sqlite3_vfs *pCurrent = 0; 10787 if( p->db ){ 10788 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); 10789 } 10790 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 10791 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, 10792 pVfs==pCurrent ? " <--- CURRENT" : ""); 10793 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); 10794 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); 10795 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); 10796 if( pVfs->pNext ){ 10797 raw_printf(p->out, "-----------------------------------\n"); 10798 } 10799 } 10800 }else 10801 10802 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){ 10803 const char *zDbName = nArg==2 ? azArg[1] : "main"; 10804 char *zVfsName = 0; 10805 if( p->db ){ 10806 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); 10807 if( zVfsName ){ 10808 utf8_printf(p->out, "%s\n", zVfsName); 10809 sqlite3_free(zVfsName); 10810 } 10811 } 10812 }else 10813 10814 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){ 10815 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; 10816 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); 10817 }else 10818 10819 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){ 10820 int j; 10821 assert( nArg<=ArraySize(azArg) ); 10822 p->nWidth = nArg-1; 10823 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); 10824 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); 10825 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; 10826 for(j=1; j<nArg; j++){ 10827 p->colWidth[j-1] = (int)integerValue(azArg[j]); 10828 } 10829 }else 10830 10831 { 10832 utf8_printf(stderr, "Error: unknown command or invalid arguments: " 10833 " \"%s\". Enter \".help\" for help\n", azArg[0]); 10834 rc = 1; 10835 } 10836 10837meta_command_exit: 10838 if( p->outCount ){ 10839 p->outCount--; 10840 if( p->outCount==0 ) output_reset(p); 10841 } 10842 p->bSafeMode = p->bSafeModePersist; 10843 return rc; 10844} 10845 10846/* Line scan result and intermediate states (supporting scan resumption) 10847*/ 10848#ifndef CHAR_BIT 10849# define CHAR_BIT 8 10850#endif 10851typedef enum { 10852 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, 10853 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, 10854 QSS_Start = 0 10855} QuickScanState; 10856#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) 10857#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) 10858#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) 10859#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) 10860#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) 10861 10862/* 10863** Scan line for classification to guide shell's handling. 10864** The scan is resumable for subsequent lines when prior 10865** return values are passed as the 2nd argument. 10866*/ 10867static QuickScanState quickscan(char *zLine, QuickScanState qss){ 10868 char cin; 10869 char cWait = (char)qss; /* intentional narrowing loss */ 10870 if( cWait==0 ){ 10871 PlainScan: 10872 assert( cWait==0 ); 10873 while( (cin = *zLine++)!=0 ){ 10874 if( IsSpace(cin) ) 10875 continue; 10876 switch (cin){ 10877 case '-': 10878 if( *zLine!='-' ) 10879 break; 10880 while((cin = *++zLine)!=0 ) 10881 if( cin=='\n') 10882 goto PlainScan; 10883 return qss; 10884 case ';': 10885 qss |= QSS_EndingSemi; 10886 continue; 10887 case '/': 10888 if( *zLine=='*' ){ 10889 ++zLine; 10890 cWait = '*'; 10891 qss = QSS_SETV(qss, cWait); 10892 goto TermScan; 10893 } 10894 break; 10895 case '[': 10896 cin = ']'; 10897 /* fall thru */ 10898 case '`': case '\'': case '"': 10899 cWait = cin; 10900 qss = QSS_HasDark | cWait; 10901 goto TermScan; 10902 default: 10903 break; 10904 } 10905 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; 10906 } 10907 }else{ 10908 TermScan: 10909 while( (cin = *zLine++)!=0 ){ 10910 if( cin==cWait ){ 10911 switch( cWait ){ 10912 case '*': 10913 if( *zLine != '/' ) 10914 continue; 10915 ++zLine; 10916 cWait = 0; 10917 qss = QSS_SETV(qss, 0); 10918 goto PlainScan; 10919 case '`': case '\'': case '"': 10920 if(*zLine==cWait){ 10921 ++zLine; 10922 continue; 10923 } 10924 /* fall thru */ 10925 case ']': 10926 cWait = 0; 10927 qss = QSS_SETV(qss, 0); 10928 goto PlainScan; 10929 default: assert(0); 10930 } 10931 } 10932 } 10933 } 10934 return qss; 10935} 10936 10937/* 10938** Return TRUE if the line typed in is an SQL command terminator other 10939** than a semi-colon. The SQL Server style "go" command is understood 10940** as is the Oracle "/". 10941*/ 10942static int line_is_command_terminator(char *zLine){ 10943 while( IsSpace(zLine[0]) ){ zLine++; }; 10944 if( zLine[0]=='/' ) 10945 zLine += 1; /* Oracle */ 10946 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) 10947 zLine += 2; /* SQL Server */ 10948 else 10949 return 0; 10950 return quickscan(zLine, QSS_Start)==QSS_Start; 10951} 10952 10953/* 10954** We need a default sqlite3_complete() implementation to use in case 10955** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes 10956** any arbitrary text is a complete SQL statement. This is not very 10957** user-friendly, but it does seem to work. 10958*/ 10959#ifdef SQLITE_OMIT_COMPLETE 10960#define sqlite3_complete(x) 1 10961#endif 10962 10963/* 10964** Return true if zSql is a complete SQL statement. Return false if it 10965** ends in the middle of a string literal or C-style comment. 10966*/ 10967static int line_is_complete(char *zSql, int nSql){ 10968 int rc; 10969 if( zSql==0 ) return 1; 10970 zSql[nSql] = ';'; 10971 zSql[nSql+1] = 0; 10972 rc = sqlite3_complete(zSql); 10973 zSql[nSql] = 0; 10974 return rc; 10975} 10976 10977/* 10978** Run a single line of SQL. Return the number of errors. 10979*/ 10980static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ 10981 int rc; 10982 char *zErrMsg = 0; 10983 10984 open_db(p, 0); 10985 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); 10986 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; 10987 BEGIN_TIMER; 10988 rc = shell_exec(p, zSql, &zErrMsg); 10989 END_TIMER; 10990 if( rc || zErrMsg ){ 10991 char zPrefix[100]; 10992 const char *zErrorTail; 10993 const char *zErrorType; 10994 if( zErrMsg==0 ){ 10995 zErrorType = "Error"; 10996 zErrorTail = sqlite3_errmsg(p->db); 10997 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){ 10998 zErrorType = "Parse error"; 10999 zErrorTail = &zErrMsg[12]; 11000 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){ 11001 zErrorType = "Runtime error"; 11002 zErrorTail = &zErrMsg[10]; 11003 }else{ 11004 zErrorType = "Error"; 11005 zErrorTail = zErrMsg; 11006 } 11007 if( in!=0 || !stdin_is_interactive ){ 11008 sqlite3_snprintf(sizeof(zPrefix), zPrefix, 11009 "%s near line %d:", zErrorType, startline); 11010 }else{ 11011 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); 11012 } 11013 utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail); 11014 sqlite3_free(zErrMsg); 11015 zErrMsg = 0; 11016 return 1; 11017 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ 11018 char zLineBuf[2000]; 11019 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, 11020 "changes: %lld total_changes: %lld", 11021 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); 11022 raw_printf(p->out, "%s\n", zLineBuf); 11023 } 11024 return 0; 11025} 11026 11027static void echo_group_input(ShellState *p, const char *zDo){ 11028 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); 11029} 11030 11031#ifdef SQLITE_SHELL_FIDDLE 11032/* 11033** Alternate one_input_line() impl for wasm mode. This is not in the primary impl 11034** because we need the global shellState and cannot access it from that function 11035** without moving lots of code around (creating a larger/messier diff). 11036*/ 11037static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ 11038 /* Parse the next line from shellState.wasm.zInput. */ 11039 const char *zBegin = shellState.wasm.zPos; 11040 const char *z = zBegin; 11041 char *zLine = 0; 11042 i64 nZ = 0; 11043 11044 UNUSED_PARAMETER(in); 11045 UNUSED_PARAMETER(isContinuation); 11046 if(!z || !*z){ 11047 return 0; 11048 } 11049 while(*z && isspace(*z)) ++z; 11050 zBegin = z; 11051 for(; *z && '\n'!=*z; ++nZ, ++z){} 11052 if(nZ>0 && '\r'==zBegin[nZ-1]){ 11053 --nZ; 11054 } 11055 shellState.wasm.zPos = z; 11056 zLine = realloc(zPrior, nZ+1); 11057 shell_check_oom(zLine); 11058 memcpy(zLine, zBegin, nZ); 11059 zLine[nZ] = 0; 11060 return zLine; 11061} 11062#endif /* SQLITE_SHELL_FIDDLE */ 11063 11064/* 11065** Read input from *in and process it. If *in==0 then input 11066** is interactive - the user is typing it it. Otherwise, input 11067** is coming from a file or device. A prompt is issued and history 11068** is saved only if input is interactive. An interrupt signal will 11069** cause this routine to exit immediately, unless input is interactive. 11070** 11071** Return the number of errors. 11072*/ 11073static int process_input(ShellState *p){ 11074 char *zLine = 0; /* A single input line */ 11075 char *zSql = 0; /* Accumulated SQL text */ 11076 i64 nLine; /* Length of current line */ 11077 i64 nSql = 0; /* Bytes of zSql[] used */ 11078 i64 nAlloc = 0; /* Allocated zSql[] space */ 11079 int rc; /* Error code */ 11080 int errCnt = 0; /* Number of errors seen */ 11081 i64 startline = 0; /* Line number for start of current input */ 11082 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ 11083 11084 if( p->inputNesting==MAX_INPUT_NESTING ){ 11085 /* This will be more informative in a later version. */ 11086 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d." 11087 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); 11088 return 1; 11089 } 11090 ++p->inputNesting; 11091 p->lineno = 0; 11092 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ 11093 fflush(p->out); 11094 zLine = one_input_line(p->in, zLine, nSql>0); 11095 if( zLine==0 ){ 11096 /* End of input */ 11097 if( p->in==0 && stdin_is_interactive ) printf("\n"); 11098 break; 11099 } 11100 if( seenInterrupt ){ 11101 if( p->in!=0 ) break; 11102 seenInterrupt = 0; 11103 } 11104 p->lineno++; 11105 if( QSS_INPLAIN(qss) 11106 && line_is_command_terminator(zLine) 11107 && line_is_complete(zSql, nSql) ){ 11108 memcpy(zLine,";",2); 11109 } 11110 qss = quickscan(zLine, qss); 11111 if( QSS_PLAINWHITE(qss) && nSql==0 ){ 11112 /* Just swallow single-line whitespace */ 11113 echo_group_input(p, zLine); 11114 qss = QSS_Start; 11115 continue; 11116 } 11117 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ 11118 echo_group_input(p, zLine); 11119 if( zLine[0]=='.' ){ 11120 rc = do_meta_command(zLine, p); 11121 if( rc==2 ){ /* exit requested */ 11122 break; 11123 }else if( rc ){ 11124 errCnt++; 11125 } 11126 } 11127 qss = QSS_Start; 11128 continue; 11129 } 11130 /* No single-line dispositions remain; accumulate line(s). */ 11131 nLine = strlen(zLine); 11132 if( nSql+nLine+2>=nAlloc ){ 11133 /* Grow buffer by half-again increments when big. */ 11134 nAlloc = nSql+(nSql>>1)+nLine+100; 11135 zSql = realloc(zSql, nAlloc); 11136 shell_check_oom(zSql); 11137 } 11138 if( nSql==0 ){ 11139 i64 i; 11140 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} 11141 assert( nAlloc>0 && zSql!=0 ); 11142 memcpy(zSql, zLine+i, nLine+1-i); 11143 startline = p->lineno; 11144 nSql = nLine-i; 11145 }else{ 11146 zSql[nSql++] = '\n'; 11147 memcpy(zSql+nSql, zLine, nLine+1); 11148 nSql += nLine; 11149 } 11150 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ 11151 echo_group_input(p, zSql); 11152 errCnt += runOneSqlLine(p, zSql, p->in, startline); 11153 nSql = 0; 11154 if( p->outCount ){ 11155 output_reset(p); 11156 p->outCount = 0; 11157 }else{ 11158 clearTempFile(p); 11159 } 11160 p->bSafeMode = p->bSafeModePersist; 11161 qss = QSS_Start; 11162 }else if( nSql && QSS_PLAINWHITE(qss) ){ 11163 echo_group_input(p, zSql); 11164 nSql = 0; 11165 qss = QSS_Start; 11166 } 11167 } 11168 if( nSql ){ 11169 /* This may be incomplete. Let the SQL parser deal with that. */ 11170 echo_group_input(p, zSql); 11171 errCnt += runOneSqlLine(p, zSql, p->in, startline); 11172 } 11173 free(zSql); 11174 free(zLine); 11175 --p->inputNesting; 11176 return errCnt>0; 11177} 11178 11179/* 11180** Return a pathname which is the user's home directory. A 11181** 0 return indicates an error of some kind. 11182*/ 11183static char *find_home_dir(int clearFlag){ 11184 static char *home_dir = NULL; 11185 if( clearFlag ){ 11186 free(home_dir); 11187 home_dir = 0; 11188 return 0; 11189 } 11190 if( home_dir ) return home_dir; 11191 11192#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 11193 && !defined(__RTP__) && !defined(_WRS_KERNEL) 11194 { 11195 struct passwd *pwent; 11196 uid_t uid = getuid(); 11197 if( (pwent=getpwuid(uid)) != NULL) { 11198 home_dir = pwent->pw_dir; 11199 } 11200 } 11201#endif 11202 11203#if defined(_WIN32_WCE) 11204 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() 11205 */ 11206 home_dir = "/"; 11207#else 11208 11209#if defined(_WIN32) || defined(WIN32) 11210 if (!home_dir) { 11211 home_dir = getenv("USERPROFILE"); 11212 } 11213#endif 11214 11215 if (!home_dir) { 11216 home_dir = getenv("HOME"); 11217 } 11218 11219#if defined(_WIN32) || defined(WIN32) 11220 if (!home_dir) { 11221 char *zDrive, *zPath; 11222 int n; 11223 zDrive = getenv("HOMEDRIVE"); 11224 zPath = getenv("HOMEPATH"); 11225 if( zDrive && zPath ){ 11226 n = strlen30(zDrive) + strlen30(zPath) + 1; 11227 home_dir = malloc( n ); 11228 if( home_dir==0 ) return 0; 11229 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); 11230 return home_dir; 11231 } 11232 home_dir = "c:\\"; 11233 } 11234#endif 11235 11236#endif /* !_WIN32_WCE */ 11237 11238 if( home_dir ){ 11239 i64 n = strlen(home_dir) + 1; 11240 char *z = malloc( n ); 11241 if( z ) memcpy(z, home_dir, n); 11242 home_dir = z; 11243 } 11244 11245 return home_dir; 11246} 11247 11248/* 11249** Read input from the file given by sqliterc_override. Or if that 11250** parameter is NULL, take input from ~/.sqliterc 11251** 11252** Returns the number of errors. 11253*/ 11254static void process_sqliterc( 11255 ShellState *p, /* Configuration data */ 11256 const char *sqliterc_override /* Name of config file. NULL to use default */ 11257){ 11258 char *home_dir = NULL; 11259 const char *sqliterc = sqliterc_override; 11260 char *zBuf = 0; 11261 FILE *inSaved = p->in; 11262 int savedLineno = p->lineno; 11263 11264 if (sqliterc == NULL) { 11265 home_dir = find_home_dir(0); 11266 if( home_dir==0 ){ 11267 raw_printf(stderr, "-- warning: cannot find home directory;" 11268 " cannot read ~/.sqliterc\n"); 11269 return; 11270 } 11271 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); 11272 shell_check_oom(zBuf); 11273 sqliterc = zBuf; 11274 } 11275 p->in = fopen(sqliterc,"rb"); 11276 if( p->in ){ 11277 if( stdin_is_interactive ){ 11278 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); 11279 } 11280 if( process_input(p) && bail_on_error ) exit(1); 11281 fclose(p->in); 11282 }else if( sqliterc_override!=0 ){ 11283 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc); 11284 if( bail_on_error ) exit(1); 11285 } 11286 p->in = inSaved; 11287 p->lineno = savedLineno; 11288 sqlite3_free(zBuf); 11289} 11290 11291/* 11292** Show available command line options 11293*/ 11294static const char zOptions[] = 11295#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) 11296 " -A ARGS... run \".archive ARGS\" and exit\n" 11297#endif 11298 " -append append the database to the end of the file\n" 11299 " -ascii set output mode to 'ascii'\n" 11300 " -bail stop after hitting an error\n" 11301 " -batch force batch I/O\n" 11302 " -box set output mode to 'box'\n" 11303 " -column set output mode to 'column'\n" 11304 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 11305 " -csv set output mode to 'csv'\n" 11306#if !defined(SQLITE_OMIT_DESERIALIZE) 11307 " -deserialize open the database using sqlite3_deserialize()\n" 11308#endif 11309 " -echo print inputs before execution\n" 11310 " -init FILENAME read/process named file\n" 11311 " -[no]header turn headers on or off\n" 11312#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 11313 " -heap SIZE Size of heap for memsys3 or memsys5\n" 11314#endif 11315 " -help show this message\n" 11316 " -html set output mode to HTML\n" 11317 " -interactive force interactive I/O\n" 11318 " -json set output mode to 'json'\n" 11319 " -line set output mode to 'line'\n" 11320 " -list set output mode to 'list'\n" 11321 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 11322 " -markdown set output mode to 'markdown'\n" 11323#if !defined(SQLITE_OMIT_DESERIALIZE) 11324 " -maxsize N maximum size for a --deserialize database\n" 11325#endif 11326 " -memtrace trace all memory allocations and deallocations\n" 11327 " -mmap N default mmap size set to N\n" 11328#ifdef SQLITE_ENABLE_MULTIPLEX 11329 " -multiplex enable the multiplexor VFS\n" 11330#endif 11331 " -newline SEP set output row separator. Default: '\\n'\n" 11332 " -nofollow refuse to open symbolic links to database files\n" 11333 " -nonce STRING set the safe-mode escape nonce\n" 11334 " -nullvalue TEXT set text string for NULL values. Default ''\n" 11335 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 11336 " -quote set output mode to 'quote'\n" 11337 " -readonly open the database read-only\n" 11338 " -safe enable safe-mode\n" 11339 " -separator SEP set output column separator. Default: '|'\n" 11340#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11341 " -sorterref SIZE sorter references threshold size\n" 11342#endif 11343 " -stats print memory stats before each finalize\n" 11344 " -table set output mode to 'table'\n" 11345 " -tabs set output mode to 'tabs'\n" 11346 " -version show SQLite version\n" 11347 " -vfs NAME use NAME as the default VFS\n" 11348#ifdef SQLITE_ENABLE_VFSTRACE 11349 " -vfstrace enable tracing of all VFS calls\n" 11350#endif 11351#ifdef SQLITE_HAVE_ZLIB 11352 " -zip open the file as a ZIP Archive\n" 11353#endif 11354; 11355static void usage(int showDetail){ 11356 utf8_printf(stderr, 11357 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 11358 "FILENAME is the name of an SQLite database. A new database is created\n" 11359 "if the file does not previously exist.\n", Argv0); 11360 if( showDetail ){ 11361 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); 11362 }else{ 11363 raw_printf(stderr, "Use the -help option for additional information\n"); 11364 } 11365 exit(1); 11366} 11367 11368/* 11369** Internal check: Verify that the SQLite is uninitialized. Print a 11370** error message if it is initialized. 11371*/ 11372static void verify_uninitialized(void){ 11373 if( sqlite3_config(-1)==SQLITE_MISUSE ){ 11374 utf8_printf(stdout, "WARNING: attempt to configure SQLite after" 11375 " initialization.\n"); 11376 } 11377} 11378 11379/* 11380** Initialize the state information in data 11381*/ 11382static void main_init(ShellState *data) { 11383 memset(data, 0, sizeof(*data)); 11384 data->normalMode = data->cMode = data->mode = MODE_List; 11385 data->autoExplain = 1; 11386 data->pAuxDb = &data->aAuxDb[0]; 11387 memcpy(data->colSeparator,SEP_Column, 2); 11388 memcpy(data->rowSeparator,SEP_Row, 2); 11389 data->showHeader = 0; 11390 data->shellFlgs = SHFLG_Lookaside; 11391 verify_uninitialized(); 11392 sqlite3_config(SQLITE_CONFIG_URI, 1); 11393 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); 11394 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 11395 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); 11396 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); 11397} 11398 11399/* 11400** Output text to the console in a font that attracts extra attention. 11401*/ 11402#ifdef _WIN32 11403static void printBold(const char *zText){ 11404#if !SQLITE_OS_WINRT 11405 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 11406 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; 11407 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); 11408 SetConsoleTextAttribute(out, 11409 FOREGROUND_RED|FOREGROUND_INTENSITY 11410 ); 11411#endif 11412 printf("%s", zText); 11413#if !SQLITE_OS_WINRT 11414 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); 11415#endif 11416} 11417#else 11418static void printBold(const char *zText){ 11419 printf("\033[1m%s\033[0m", zText); 11420} 11421#endif 11422 11423/* 11424** Get the argument to an --option. Throw an error and die if no argument 11425** is available. 11426*/ 11427static char *cmdline_option_value(int argc, char **argv, int i){ 11428 if( i==argc ){ 11429 utf8_printf(stderr, "%s: Error: missing argument to %s\n", 11430 argv[0], argv[argc-1]); 11431 exit(1); 11432 } 11433 return argv[i]; 11434} 11435 11436#ifndef SQLITE_SHELL_IS_UTF8 11437# if (defined(_WIN32) || defined(WIN32)) \ 11438 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) 11439# define SQLITE_SHELL_IS_UTF8 (0) 11440# else 11441# define SQLITE_SHELL_IS_UTF8 (1) 11442# endif 11443#endif 11444 11445#ifdef SQLITE_SHELL_FIDDLE 11446# define main fiddle_main 11447#endif 11448 11449#if SQLITE_SHELL_IS_UTF8 11450int SQLITE_CDECL main(int argc, char **argv){ 11451#else 11452int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ 11453 char **argv; 11454#endif 11455#ifdef SQLITE_DEBUG 11456 sqlite3_int64 mem_main_enter = sqlite3_memory_used(); 11457#endif 11458 char *zErrMsg = 0; 11459#ifdef SQLITE_SHELL_FIDDLE 11460# define data shellState 11461#else 11462 ShellState data; 11463#endif 11464 const char *zInitFile = 0; 11465 int i; 11466 int rc = 0; 11467 int warnInmemoryDb = 0; 11468 int readStdin = 1; 11469 int nCmd = 0; 11470 char **azCmd = 0; 11471 const char *zVfs = 0; /* Value of -vfs command-line option */ 11472#if !SQLITE_SHELL_IS_UTF8 11473 char **argvToFree = 0; 11474 int argcToFree = 0; 11475#endif 11476 11477 setBinaryMode(stdin, 0); 11478 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ 11479#ifdef SQLITE_SHELL_FIDDLE 11480 stdin_is_interactive = 0; 11481 stdout_is_console = 1; 11482 data.wasm.zDefaultDbName = "/fiddle.sqlite3"; 11483#else 11484 stdin_is_interactive = isatty(0); 11485 stdout_is_console = isatty(1); 11486#endif 11487 11488#if !defined(_WIN32_WCE) 11489 if( getenv("SQLITE_DEBUG_BREAK") ){ 11490 if( isatty(0) && isatty(2) ){ 11491 fprintf(stderr, 11492 "attach debugger to process %d and press any key to continue.\n", 11493 GETPID()); 11494 fgetc(stdin); 11495 }else{ 11496#if defined(_WIN32) || defined(WIN32) 11497#if SQLITE_OS_WINRT 11498 __debugbreak(); 11499#else 11500 DebugBreak(); 11501#endif 11502#elif defined(SIGTRAP) 11503 raise(SIGTRAP); 11504#endif 11505 } 11506 } 11507#endif 11508 11509#if USE_SYSTEM_SQLITE+0!=1 11510 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ 11511 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", 11512 sqlite3_sourceid(), SQLITE_SOURCE_ID); 11513 exit(1); 11514 } 11515#endif 11516 main_init(&data); 11517 11518 /* On Windows, we must translate command-line arguments into UTF-8. 11519 ** The SQLite memory allocator subsystem has to be enabled in order to 11520 ** do this. But we want to run an sqlite3_shutdown() afterwards so that 11521 ** subsequent sqlite3_config() calls will work. So copy all results into 11522 ** memory that does not come from the SQLite memory allocator. 11523 */ 11524#if !SQLITE_SHELL_IS_UTF8 11525 sqlite3_initialize(); 11526 argvToFree = malloc(sizeof(argv[0])*argc*2); 11527 shell_check_oom(argvToFree); 11528 argcToFree = argc; 11529 argv = argvToFree + argc; 11530 for(i=0; i<argc; i++){ 11531 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); 11532 i64 n; 11533 shell_check_oom(z); 11534 n = strlen(z); 11535 argv[i] = malloc( n+1 ); 11536 shell_check_oom(argv[i]); 11537 memcpy(argv[i], z, n+1); 11538 argvToFree[i] = argv[i]; 11539 sqlite3_free(z); 11540 } 11541 sqlite3_shutdown(); 11542#endif 11543 11544 assert( argc>=1 && argv && argv[0] ); 11545 Argv0 = argv[0]; 11546 11547 /* Make sure we have a valid signal handler early, before anything 11548 ** else is done. 11549 */ 11550#ifdef SIGINT 11551 signal(SIGINT, interrupt_handler); 11552#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) 11553 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); 11554#endif 11555 11556#ifdef SQLITE_SHELL_DBNAME_PROC 11557 { 11558 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name 11559 ** of a C-function that will provide the name of the database file. Use 11560 ** this compile-time option to embed this shell program in larger 11561 ** applications. */ 11562 extern void SQLITE_SHELL_DBNAME_PROC(const char**); 11563 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); 11564 warnInmemoryDb = 0; 11565 } 11566#endif 11567 11568 /* Do an initial pass through the command-line argument to locate 11569 ** the name of the database file, the name of the initialization file, 11570 ** the size of the alternative malloc heap, 11571 ** and the first command to execute. 11572 */ 11573 verify_uninitialized(); 11574 for(i=1; i<argc; i++){ 11575 char *z; 11576 z = argv[i]; 11577 if( z[0]!='-' ){ 11578 if( data.aAuxDb->zDbFilename==0 ){ 11579 data.aAuxDb->zDbFilename = z; 11580 }else{ 11581 /* Excesss arguments are interpreted as SQL (or dot-commands) and 11582 ** mean that nothing is read from stdin */ 11583 readStdin = 0; 11584 nCmd++; 11585 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); 11586 shell_check_oom(azCmd); 11587 azCmd[nCmd-1] = z; 11588 } 11589 } 11590 if( z[1]=='-' ) z++; 11591 if( cli_strcmp(z,"-separator")==0 11592 || cli_strcmp(z,"-nullvalue")==0 11593 || cli_strcmp(z,"-newline")==0 11594 || cli_strcmp(z,"-cmd")==0 11595 ){ 11596 (void)cmdline_option_value(argc, argv, ++i); 11597 }else if( cli_strcmp(z,"-init")==0 ){ 11598 zInitFile = cmdline_option_value(argc, argv, ++i); 11599 }else if( cli_strcmp(z,"-batch")==0 ){ 11600 /* Need to check for batch mode here to so we can avoid printing 11601 ** informational messages (like from process_sqliterc) before 11602 ** we do the actual processing of arguments later in a second pass. 11603 */ 11604 stdin_is_interactive = 0; 11605 }else if( cli_strcmp(z,"-heap")==0 ){ 11606#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 11607 const char *zSize; 11608 sqlite3_int64 szHeap; 11609 11610 zSize = cmdline_option_value(argc, argv, ++i); 11611 szHeap = integerValue(zSize); 11612 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; 11613 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); 11614#else 11615 (void)cmdline_option_value(argc, argv, ++i); 11616#endif 11617 }else if( cli_strcmp(z,"-pagecache")==0 ){ 11618 sqlite3_int64 n, sz; 11619 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11620 if( sz>70000 ) sz = 70000; 11621 if( sz<0 ) sz = 0; 11622 n = integerValue(cmdline_option_value(argc,argv,++i)); 11623 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ 11624 n = 0xffffffffffffLL/sz; 11625 } 11626 sqlite3_config(SQLITE_CONFIG_PAGECACHE, 11627 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); 11628 data.shellFlgs |= SHFLG_Pagecache; 11629 }else if( cli_strcmp(z,"-lookaside")==0 ){ 11630 int n, sz; 11631 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11632 if( sz<0 ) sz = 0; 11633 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11634 if( n<0 ) n = 0; 11635 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); 11636 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; 11637 }else if( cli_strcmp(z,"-threadsafe")==0 ){ 11638 int n; 11639 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); 11640 switch( n ){ 11641 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; 11642 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; 11643 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; 11644 } 11645#ifdef SQLITE_ENABLE_VFSTRACE 11646 }else if( cli_strcmp(z,"-vfstrace")==0 ){ 11647 extern int vfstrace_register( 11648 const char *zTraceName, 11649 const char *zOldVfsName, 11650 int (*xOut)(const char*,void*), 11651 void *pOutArg, 11652 int makeDefault 11653 ); 11654 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); 11655#endif 11656#ifdef SQLITE_ENABLE_MULTIPLEX 11657 }else if( cli_strcmp(z,"-multiplex")==0 ){ 11658 extern int sqlite3_multiple_initialize(const char*,int); 11659 sqlite3_multiplex_initialize(0, 1); 11660#endif 11661 }else if( cli_strcmp(z,"-mmap")==0 ){ 11662 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11663 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); 11664#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11665 }else if( cli_strcmp(z,"-sorterref")==0 ){ 11666 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); 11667 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); 11668#endif 11669 }else if( cli_strcmp(z,"-vfs")==0 ){ 11670 zVfs = cmdline_option_value(argc, argv, ++i); 11671#ifdef SQLITE_HAVE_ZLIB 11672 }else if( cli_strcmp(z,"-zip")==0 ){ 11673 data.openMode = SHELL_OPEN_ZIPFILE; 11674#endif 11675 }else if( cli_strcmp(z,"-append")==0 ){ 11676 data.openMode = SHELL_OPEN_APPENDVFS; 11677#ifndef SQLITE_OMIT_DESERIALIZE 11678 }else if( cli_strcmp(z,"-deserialize")==0 ){ 11679 data.openMode = SHELL_OPEN_DESERIALIZE; 11680 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ 11681 data.szMax = integerValue(argv[++i]); 11682#endif 11683 }else if( cli_strcmp(z,"-readonly")==0 ){ 11684 data.openMode = SHELL_OPEN_READONLY; 11685 }else if( cli_strcmp(z,"-nofollow")==0 ){ 11686 data.openFlags = SQLITE_OPEN_NOFOLLOW; 11687#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 11688 }else if( cli_strncmp(z, "-A",2)==0 ){ 11689 /* All remaining command-line arguments are passed to the ".archive" 11690 ** command, so ignore them */ 11691 break; 11692#endif 11693 }else if( cli_strcmp(z, "-memtrace")==0 ){ 11694 sqlite3MemTraceActivate(stderr); 11695 }else if( cli_strcmp(z,"-bail")==0 ){ 11696 bail_on_error = 1; 11697 }else if( cli_strcmp(z,"-nonce")==0 ){ 11698 free(data.zNonce); 11699 data.zNonce = strdup(argv[++i]); 11700 }else if( cli_strcmp(z,"-safe")==0 ){ 11701 /* no-op - catch this on the second pass */ 11702 } 11703 } 11704 verify_uninitialized(); 11705 11706 11707#ifdef SQLITE_SHELL_INIT_PROC 11708 { 11709 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name 11710 ** of a C-function that will perform initialization actions on SQLite that 11711 ** occur just before or after sqlite3_initialize(). Use this compile-time 11712 ** option to embed this shell program in larger applications. */ 11713 extern void SQLITE_SHELL_INIT_PROC(void); 11714 SQLITE_SHELL_INIT_PROC(); 11715 } 11716#else 11717 /* All the sqlite3_config() calls have now been made. So it is safe 11718 ** to call sqlite3_initialize() and process any command line -vfs option. */ 11719 sqlite3_initialize(); 11720#endif 11721 11722 if( zVfs ){ 11723 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); 11724 if( pVfs ){ 11725 sqlite3_vfs_register(pVfs, 1); 11726 }else{ 11727 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); 11728 exit(1); 11729 } 11730 } 11731 11732 if( data.pAuxDb->zDbFilename==0 ){ 11733#ifndef SQLITE_OMIT_MEMORYDB 11734 data.pAuxDb->zDbFilename = ":memory:"; 11735 warnInmemoryDb = argc==1; 11736#else 11737 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); 11738 return 1; 11739#endif 11740 } 11741 data.out = stdout; 11742#ifndef SQLITE_SHELL_FIDDLE 11743 sqlite3_appendvfs_init(0,0,0); 11744#endif 11745 11746 /* Go ahead and open the database file if it already exists. If the 11747 ** file does not exist, delay opening it. This prevents empty database 11748 ** files from being created if a user mistypes the database name argument 11749 ** to the sqlite command-line tool. 11750 */ 11751 if( access(data.pAuxDb->zDbFilename, 0)==0 ){ 11752 open_db(&data, 0); 11753 } 11754 11755 /* Process the initialization file if there is one. If no -init option 11756 ** is given on the command line, look for a file named ~/.sqliterc and 11757 ** try to process it. 11758 */ 11759 process_sqliterc(&data,zInitFile); 11760 11761 /* Make a second pass through the command-line argument and set 11762 ** options. This second pass is delayed until after the initialization 11763 ** file is processed so that the command-line arguments will override 11764 ** settings in the initialization file. 11765 */ 11766 for(i=1; i<argc; i++){ 11767 char *z = argv[i]; 11768 if( z[0]!='-' ) continue; 11769 if( z[1]=='-' ){ z++; } 11770 if( cli_strcmp(z,"-init")==0 ){ 11771 i++; 11772 }else if( cli_strcmp(z,"-html")==0 ){ 11773 data.mode = MODE_Html; 11774 }else if( cli_strcmp(z,"-list")==0 ){ 11775 data.mode = MODE_List; 11776 }else if( cli_strcmp(z,"-quote")==0 ){ 11777 data.mode = MODE_Quote; 11778 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); 11779 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); 11780 }else if( cli_strcmp(z,"-line")==0 ){ 11781 data.mode = MODE_Line; 11782 }else if( cli_strcmp(z,"-column")==0 ){ 11783 data.mode = MODE_Column; 11784 }else if( cli_strcmp(z,"-json")==0 ){ 11785 data.mode = MODE_Json; 11786 }else if( cli_strcmp(z,"-markdown")==0 ){ 11787 data.mode = MODE_Markdown; 11788 }else if( cli_strcmp(z,"-table")==0 ){ 11789 data.mode = MODE_Table; 11790 }else if( cli_strcmp(z,"-box")==0 ){ 11791 data.mode = MODE_Box; 11792 }else if( cli_strcmp(z,"-csv")==0 ){ 11793 data.mode = MODE_Csv; 11794 memcpy(data.colSeparator,",",2); 11795#ifdef SQLITE_HAVE_ZLIB 11796 }else if( cli_strcmp(z,"-zip")==0 ){ 11797 data.openMode = SHELL_OPEN_ZIPFILE; 11798#endif 11799 }else if( cli_strcmp(z,"-append")==0 ){ 11800 data.openMode = SHELL_OPEN_APPENDVFS; 11801#ifndef SQLITE_OMIT_DESERIALIZE 11802 }else if( cli_strcmp(z,"-deserialize")==0 ){ 11803 data.openMode = SHELL_OPEN_DESERIALIZE; 11804 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ 11805 data.szMax = integerValue(argv[++i]); 11806#endif 11807 }else if( cli_strcmp(z,"-readonly")==0 ){ 11808 data.openMode = SHELL_OPEN_READONLY; 11809 }else if( cli_strcmp(z,"-nofollow")==0 ){ 11810 data.openFlags |= SQLITE_OPEN_NOFOLLOW; 11811 }else if( cli_strcmp(z,"-ascii")==0 ){ 11812 data.mode = MODE_Ascii; 11813 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit); 11814 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record); 11815 }else if( cli_strcmp(z,"-tabs")==0 ){ 11816 data.mode = MODE_List; 11817 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab); 11818 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); 11819 }else if( cli_strcmp(z,"-separator")==0 ){ 11820 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, 11821 "%s",cmdline_option_value(argc,argv,++i)); 11822 }else if( cli_strcmp(z,"-newline")==0 ){ 11823 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, 11824 "%s",cmdline_option_value(argc,argv,++i)); 11825 }else if( cli_strcmp(z,"-nullvalue")==0 ){ 11826 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, 11827 "%s",cmdline_option_value(argc,argv,++i)); 11828 }else if( cli_strcmp(z,"-header")==0 ){ 11829 data.showHeader = 1; 11830 ShellSetFlag(&data, SHFLG_HeaderSet); 11831 }else if( cli_strcmp(z,"-noheader")==0 ){ 11832 data.showHeader = 0; 11833 ShellSetFlag(&data, SHFLG_HeaderSet); 11834 }else if( cli_strcmp(z,"-echo")==0 ){ 11835 ShellSetFlag(&data, SHFLG_Echo); 11836 }else if( cli_strcmp(z,"-eqp")==0 ){ 11837 data.autoEQP = AUTOEQP_on; 11838 }else if( cli_strcmp(z,"-eqpfull")==0 ){ 11839 data.autoEQP = AUTOEQP_full; 11840 }else if( cli_strcmp(z,"-stats")==0 ){ 11841 data.statsOn = 1; 11842 }else if( cli_strcmp(z,"-scanstats")==0 ){ 11843 data.scanstatsOn = 1; 11844 }else if( cli_strcmp(z,"-backslash")==0 ){ 11845 /* Undocumented command-line option: -backslash 11846 ** Causes C-style backslash escapes to be evaluated in SQL statements 11847 ** prior to sending the SQL into SQLite. Useful for injecting 11848 ** crazy bytes in the middle of SQL statements for testing and debugging. 11849 */ 11850 ShellSetFlag(&data, SHFLG_Backslash); 11851 }else if( cli_strcmp(z,"-bail")==0 ){ 11852 /* No-op. The bail_on_error flag should already be set. */ 11853 }else if( cli_strcmp(z,"-version")==0 ){ 11854 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 11855 return 0; 11856 }else if( cli_strcmp(z,"-interactive")==0 ){ 11857 stdin_is_interactive = 1; 11858 }else if( cli_strcmp(z,"-batch")==0 ){ 11859 stdin_is_interactive = 0; 11860 }else if( cli_strcmp(z,"-heap")==0 ){ 11861 i++; 11862 }else if( cli_strcmp(z,"-pagecache")==0 ){ 11863 i+=2; 11864 }else if( cli_strcmp(z,"-lookaside")==0 ){ 11865 i+=2; 11866 }else if( cli_strcmp(z,"-threadsafe")==0 ){ 11867 i+=2; 11868 }else if( cli_strcmp(z,"-nonce")==0 ){ 11869 i += 2; 11870 }else if( cli_strcmp(z,"-mmap")==0 ){ 11871 i++; 11872 }else if( cli_strcmp(z,"-memtrace")==0 ){ 11873 i++; 11874#ifdef SQLITE_ENABLE_SORTER_REFERENCES 11875 }else if( cli_strcmp(z,"-sorterref")==0 ){ 11876 i++; 11877#endif 11878 }else if( cli_strcmp(z,"-vfs")==0 ){ 11879 i++; 11880#ifdef SQLITE_ENABLE_VFSTRACE 11881 }else if( cli_strcmp(z,"-vfstrace")==0 ){ 11882 i++; 11883#endif 11884#ifdef SQLITE_ENABLE_MULTIPLEX 11885 }else if( cli_strcmp(z,"-multiplex")==0 ){ 11886 i++; 11887#endif 11888 }else if( cli_strcmp(z,"-help")==0 ){ 11889 usage(1); 11890 }else if( cli_strcmp(z,"-cmd")==0 ){ 11891 /* Run commands that follow -cmd first and separately from commands 11892 ** that simply appear on the command-line. This seems goofy. It would 11893 ** be better if all commands ran in the order that they appear. But 11894 ** we retain the goofy behavior for historical compatibility. */ 11895 if( i==argc-1 ) break; 11896 z = cmdline_option_value(argc,argv,++i); 11897 if( z[0]=='.' ){ 11898 rc = do_meta_command(z, &data); 11899 if( rc && bail_on_error ) return rc==2 ? 0 : rc; 11900 }else{ 11901 open_db(&data, 0); 11902 rc = shell_exec(&data, z, &zErrMsg); 11903 if( zErrMsg!=0 ){ 11904 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11905 if( bail_on_error ) return rc!=0 ? rc : 1; 11906 }else if( rc!=0 ){ 11907 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); 11908 if( bail_on_error ) return rc; 11909 } 11910 } 11911#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) 11912 }else if( cli_strncmp(z, "-A", 2)==0 ){ 11913 if( nCmd>0 ){ 11914 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" 11915 " with \"%s\"\n", z); 11916 return 1; 11917 } 11918 open_db(&data, OPEN_DB_ZIPFILE); 11919 if( z[2] ){ 11920 argv[i] = &z[2]; 11921 arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); 11922 }else{ 11923 arDotCommand(&data, 1, argv+i, argc-i); 11924 } 11925 readStdin = 0; 11926 break; 11927#endif 11928 }else if( cli_strcmp(z,"-safe")==0 ){ 11929 data.bSafeMode = data.bSafeModePersist = 1; 11930 }else{ 11931 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); 11932 raw_printf(stderr,"Use -help for a list of options.\n"); 11933 return 1; 11934 } 11935 data.cMode = data.mode; 11936 } 11937 11938 if( !readStdin ){ 11939 /* Run all arguments that do not begin with '-' as if they were separate 11940 ** command-line inputs, except for the argToSkip argument which contains 11941 ** the database filename. 11942 */ 11943 for(i=0; i<nCmd; i++){ 11944 if( azCmd[i][0]=='.' ){ 11945 rc = do_meta_command(azCmd[i], &data); 11946 if( rc ){ 11947 free(azCmd); 11948 return rc==2 ? 0 : rc; 11949 } 11950 }else{ 11951 open_db(&data, 0); 11952 rc = shell_exec(&data, azCmd[i], &zErrMsg); 11953 if( zErrMsg || rc ){ 11954 if( zErrMsg!=0 ){ 11955 utf8_printf(stderr,"Error: %s\n", zErrMsg); 11956 }else{ 11957 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); 11958 } 11959 sqlite3_free(zErrMsg); 11960 free(azCmd); 11961 return rc!=0 ? rc : 1; 11962 } 11963 } 11964 } 11965 }else{ 11966 /* Run commands received from standard input 11967 */ 11968 if( stdin_is_interactive ){ 11969 char *zHome; 11970 char *zHistory; 11971 int nHistory; 11972 printf( 11973 "SQLite version %s %.19s\n" /*extra-version-info*/ 11974 "Enter \".help\" for usage hints.\n", 11975 sqlite3_libversion(), sqlite3_sourceid() 11976 ); 11977 if( warnInmemoryDb ){ 11978 printf("Connected to a "); 11979 printBold("transient in-memory database"); 11980 printf(".\nUse \".open FILENAME\" to reopen on a " 11981 "persistent database.\n"); 11982 } 11983 zHistory = getenv("SQLITE_HISTORY"); 11984 if( zHistory ){ 11985 zHistory = strdup(zHistory); 11986 }else if( (zHome = find_home_dir(0))!=0 ){ 11987 nHistory = strlen30(zHome) + 20; 11988 if( (zHistory = malloc(nHistory))!=0 ){ 11989 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); 11990 } 11991 } 11992 if( zHistory ){ shell_read_history(zHistory); } 11993#if HAVE_READLINE || HAVE_EDITLINE 11994 rl_attempted_completion_function = readline_completion; 11995#elif HAVE_LINENOISE 11996 linenoiseSetCompletionCallback(linenoise_completion); 11997#endif 11998 data.in = 0; 11999 rc = process_input(&data); 12000 if( zHistory ){ 12001 shell_stifle_history(2000); 12002 shell_write_history(zHistory); 12003 free(zHistory); 12004 } 12005 }else{ 12006 data.in = stdin; 12007 rc = process_input(&data); 12008 } 12009 } 12010#ifndef SQLITE_SHELL_FIDDLE 12011 /* In WASM mode we have to leave the db state in place so that 12012 ** client code can "push" SQL into it after this call returns. */ 12013 free(azCmd); 12014 set_table_name(&data, 0); 12015 if( data.db ){ 12016 session_close_all(&data, -1); 12017 close_db(data.db); 12018 } 12019 for(i=0; i<ArraySize(data.aAuxDb); i++){ 12020 sqlite3_free(data.aAuxDb[i].zFreeOnClose); 12021 if( data.aAuxDb[i].db ){ 12022 session_close_all(&data, i); 12023 close_db(data.aAuxDb[i].db); 12024 } 12025 } 12026 find_home_dir(1); 12027 output_reset(&data); 12028 data.doXdgOpen = 0; 12029 clearTempFile(&data); 12030#if !SQLITE_SHELL_IS_UTF8 12031 for(i=0; i<argcToFree; i++) free(argvToFree[i]); 12032 free(argvToFree); 12033#endif 12034 free(data.colWidth); 12035 free(data.zNonce); 12036 /* Clear the global data structure so that valgrind will detect memory 12037 ** leaks */ 12038 memset(&data, 0, sizeof(data)); 12039#ifdef SQLITE_DEBUG 12040 if( sqlite3_memory_used()>mem_main_enter ){ 12041 utf8_printf(stderr, "Memory leaked: %u bytes\n", 12042 (unsigned int)(sqlite3_memory_used()-mem_main_enter)); 12043 } 12044#endif 12045#endif /* !SQLITE_SHELL_FIDDLE */ 12046 return rc; 12047} 12048 12049 12050#ifdef SQLITE_SHELL_FIDDLE 12051/* Only for emcc experimentation purposes. */ 12052int fiddle_experiment(int a,int b){ 12053 return a + b; 12054} 12055 12056/* 12057** Returns a pointer to the current DB handle. 12058*/ 12059sqlite3 * fiddle_db_handle(){ 12060 return globalDb; 12061} 12062 12063/* 12064** Returns a pointer to the given DB name's VFS. If zDbName is 0 then 12065** "main" is assumed. Returns 0 if no db with the given name is 12066** open. 12067*/ 12068sqlite3_vfs * fiddle_db_vfs(const char *zDbName){ 12069 sqlite3_vfs * pVfs = 0; 12070 if(globalDb){ 12071 sqlite3_file_control(globalDb, zDbName ? zDbName : "main", 12072 SQLITE_FCNTL_VFS_POINTER, &pVfs); 12073 } 12074 return pVfs; 12075} 12076 12077/* Only for emcc experimentation purposes. */ 12078sqlite3 * fiddle_db_arg(sqlite3 *arg){ 12079 printf("fiddle_db_arg(%p)\n", (const void*)arg); 12080 return arg; 12081} 12082 12083/* 12084** Intended to be called via a SharedWorker() while a separate 12085** SharedWorker() (which manages the wasm module) is performing work 12086** which should be interrupted. Unfortunately, SharedWorker is not 12087** portable enough to make real use of. 12088*/ 12089void fiddle_interrupt(void){ 12090 if( globalDb ) sqlite3_interrupt(globalDb); 12091} 12092 12093/* 12094** Returns the filename of the given db name, assuming "main" if 12095** zDbName is NULL. Returns NULL if globalDb is not opened. 12096*/ 12097const char * fiddle_db_filename(const char * zDbName){ 12098 return globalDb 12099 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") 12100 : NULL; 12101} 12102 12103/* 12104** Completely wipes out the contents of the currently-opened database 12105** but leaves its storage intact for reuse. 12106*/ 12107void fiddle_reset_db(void){ 12108 if( globalDb ){ 12109 int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); 12110 if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0); 12111 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); 12112 } 12113} 12114 12115/* 12116** Uses the current database's VFS xRead to stream the db file's 12117** contents out to the given callback. The callback gets a single 12118** chunk of size n (its 2nd argument) on each call and must return 0 12119** on success, non-0 on error. This function returns 0 on success, 12120** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 12121** code from the callback. Note that this is not thread-friendly: it 12122** expects that it will be the only thread reading the db file and 12123** takes no measures to ensure that is the case. 12124*/ 12125int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){ 12126 sqlite3_int64 nSize = 0; 12127 sqlite3_int64 nPos = 0; 12128 sqlite3_file * pFile = 0; 12129 unsigned char buf[1024 * 8]; 12130 int nBuf = (int)sizeof(buf); 12131 int rc = shellState.db 12132 ? sqlite3_file_control(shellState.db, "main", 12133 SQLITE_FCNTL_FILE_POINTER, &pFile) 12134 : SQLITE_NOTFOUND; 12135 if( rc ) return rc; 12136 rc = pFile->pMethods->xFileSize(pFile, &nSize); 12137 if( rc ) return rc; 12138 if(nSize % nBuf){ 12139 /* DB size is not an even multiple of the buffer size. Reduce 12140 ** buffer size so that we do not unduly inflate the db size when 12141 ** exporting. */ 12142 if(0 == nSize % 4096) nBuf = 4096; 12143 else if(0 == nSize % 2048) nBuf = 2048; 12144 else if(0 == nSize % 1024) nBuf = 1024; 12145 else nBuf = 512; 12146 } 12147 for( ; 0==rc && nPos<nSize; nPos += nBuf ){ 12148 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); 12149 if(SQLITE_IOERR_SHORT_READ == rc){ 12150 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; 12151 } 12152 if( 0==rc ) rc = xCallback(buf, nBuf); 12153 } 12154 return rc; 12155} 12156 12157/* 12158** Trivial exportable function for emscripten. It processes zSql as if 12159** it were input to the sqlite3 shell and redirects all output to the 12160** wasm binding. fiddle_main() must have been called before this 12161** is called, or results are undefined. 12162*/ 12163void fiddle_exec(const char * zSql){ 12164 if(zSql && *zSql){ 12165 if('.'==*zSql) puts(zSql); 12166 shellState.wasm.zInput = zSql; 12167 shellState.wasm.zPos = zSql; 12168 process_input(&shellState); 12169 shellState.wasm.zInput = shellState.wasm.zPos = 0; 12170 } 12171} 12172#endif /* SQLITE_SHELL_FIDDLE */ 12173