OLD | NEW |
(Empty) | |
| 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 ** If requested, include the SQLite compiler options file for MSVC. |
| 22 */ |
| 23 #if defined(INCLUDE_MSVC_H) |
| 24 #include "msvc.h" |
| 25 #endif |
| 26 |
| 27 /* |
| 28 ** No support for loadable extensions in VxWorks. |
| 29 */ |
| 30 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
| 31 # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 32 #endif |
| 33 |
| 34 /* |
| 35 ** Enable large-file support for fopen() and friends on unix. |
| 36 */ |
| 37 #ifndef SQLITE_DISABLE_LFS |
| 38 # define _LARGE_FILE 1 |
| 39 # ifndef _FILE_OFFSET_BITS |
| 40 # define _FILE_OFFSET_BITS 64 |
| 41 # endif |
| 42 # define _LARGEFILE_SOURCE 1 |
| 43 #endif |
| 44 |
| 45 #include <stdlib.h> |
| 46 #include <string.h> |
| 47 #include <stdio.h> |
| 48 #include <assert.h> |
| 49 #include "sqlite3.h" |
| 50 #if SQLITE_USER_AUTHENTICATION |
| 51 # include "sqlite3userauth.h" |
| 52 #endif |
| 53 #include <ctype.h> |
| 54 #include <stdarg.h> |
| 55 |
| 56 #if !defined(_WIN32) && !defined(WIN32) |
| 57 # include <signal.h> |
| 58 # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 59 # include <pwd.h> |
| 60 # endif |
| 61 # include <unistd.h> |
| 62 # include <sys/types.h> |
| 63 #endif |
| 64 |
| 65 #if HAVE_READLINE |
| 66 # include <readline/readline.h> |
| 67 # include <readline/history.h> |
| 68 #endif |
| 69 |
| 70 #if HAVE_EDITLINE |
| 71 # include <editline/readline.h> |
| 72 #endif |
| 73 |
| 74 #if HAVE_EDITLINE || HAVE_READLINE |
| 75 |
| 76 # define shell_add_history(X) add_history(X) |
| 77 # define shell_read_history(X) read_history(X) |
| 78 # define shell_write_history(X) write_history(X) |
| 79 # define shell_stifle_history(X) stifle_history(X) |
| 80 # define shell_readline(X) readline(X) |
| 81 |
| 82 #elif HAVE_LINENOISE |
| 83 |
| 84 # include "linenoise.h" |
| 85 # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 86 # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 87 # define shell_write_history(X) linenoiseHistorySave(X) |
| 88 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 89 # define shell_readline(X) linenoise(X) |
| 90 |
| 91 #else |
| 92 |
| 93 # define shell_read_history(X) |
| 94 # define shell_write_history(X) |
| 95 # define shell_stifle_history(X) |
| 96 |
| 97 # define SHELL_USE_LOCAL_GETLINE 1 |
| 98 #endif |
| 99 |
| 100 |
| 101 #if defined(_WIN32) || defined(WIN32) |
| 102 # include <io.h> |
| 103 # include <fcntl.h> |
| 104 # define isatty(h) _isatty(h) |
| 105 # ifndef access |
| 106 # define access(f,m) _access((f),(m)) |
| 107 # endif |
| 108 # undef popen |
| 109 # define popen _popen |
| 110 # undef pclose |
| 111 # define pclose _pclose |
| 112 #else |
| 113 /* Make sure isatty() has a prototype. */ |
| 114 extern int isatty(int); |
| 115 |
| 116 # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 117 /* popen and pclose are not C89 functions and so are |
| 118 ** sometimes omitted from the <stdio.h> header */ |
| 119 extern FILE *popen(const char*,const char*); |
| 120 extern int pclose(FILE*); |
| 121 # else |
| 122 # define SQLITE_OMIT_POPEN 1 |
| 123 # endif |
| 124 #endif |
| 125 |
| 126 #if defined(_WIN32_WCE) |
| 127 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 128 * thus we always assume that we have a console. That can be |
| 129 * overridden with the -batch command line option. |
| 130 */ |
| 131 #define isatty(x) 1 |
| 132 #endif |
| 133 |
| 134 /* ctype macros that work with signed characters */ |
| 135 #define IsSpace(X) isspace((unsigned char)X) |
| 136 #define IsDigit(X) isdigit((unsigned char)X) |
| 137 #define ToLower(X) (char)tolower((unsigned char)X) |
| 138 |
| 139 #if defined(_WIN32) || defined(WIN32) |
| 140 #include <windows.h> |
| 141 |
| 142 /* string conversion routines only needed on Win32 */ |
| 143 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 144 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 145 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| 146 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 147 #endif |
| 148 |
| 149 /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 150 ** are automatically translated into \r\n. However, this behavior needs |
| 151 ** to be disabled in some cases (ex: when generating CSV output and when |
| 152 ** rendering quoted strings that contain \n characters). The following |
| 153 ** routines take care of that. |
| 154 */ |
| 155 #if defined(_WIN32) || defined(WIN32) |
| 156 static void setBinaryMode(FILE *file, int isOutput){ |
| 157 if( isOutput ) fflush(file); |
| 158 _setmode(_fileno(file), _O_BINARY); |
| 159 } |
| 160 static void setTextMode(FILE *file, int isOutput){ |
| 161 if( isOutput ) fflush(file); |
| 162 _setmode(_fileno(file), _O_TEXT); |
| 163 } |
| 164 #else |
| 165 # define setBinaryMode(X,Y) |
| 166 # define setTextMode(X,Y) |
| 167 #endif |
| 168 |
| 169 |
| 170 /* True if the timer is enabled */ |
| 171 static int enableTimer = 0; |
| 172 |
| 173 /* Return the current wall-clock time */ |
| 174 static sqlite3_int64 timeOfDay(void){ |
| 175 static sqlite3_vfs *clockVfs = 0; |
| 176 sqlite3_int64 t; |
| 177 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 178 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 179 clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 180 }else{ |
| 181 double r; |
| 182 clockVfs->xCurrentTime(clockVfs, &r); |
| 183 t = (sqlite3_int64)(r*86400000.0); |
| 184 } |
| 185 return t; |
| 186 } |
| 187 |
| 188 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
| 189 #include <sys/time.h> |
| 190 #include <sys/resource.h> |
| 191 |
| 192 /* VxWorks does not support getrusage() as far as we can determine */ |
| 193 #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 194 struct rusage { |
| 195 struct timeval ru_utime; /* user CPU time used */ |
| 196 struct timeval ru_stime; /* system CPU time used */ |
| 197 }; |
| 198 #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 199 #endif |
| 200 |
| 201 /* Saved resource information for the beginning of an operation */ |
| 202 static struct rusage sBegin; /* CPU time at start */ |
| 203 static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 204 |
| 205 /* |
| 206 ** Begin timing an operation |
| 207 */ |
| 208 static void beginTimer(void){ |
| 209 if( enableTimer ){ |
| 210 getrusage(RUSAGE_SELF, &sBegin); |
| 211 iBegin = timeOfDay(); |
| 212 } |
| 213 } |
| 214 |
| 215 /* Return the difference of two time_structs in seconds */ |
| 216 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 217 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 218 (double)(pEnd->tv_sec - pStart->tv_sec); |
| 219 } |
| 220 |
| 221 /* |
| 222 ** Print the timing results. |
| 223 */ |
| 224 static void endTimer(void){ |
| 225 if( enableTimer ){ |
| 226 sqlite3_int64 iEnd = timeOfDay(); |
| 227 struct rusage sEnd; |
| 228 getrusage(RUSAGE_SELF, &sEnd); |
| 229 printf("Run Time: real %.3f user %f sys %f\n", |
| 230 (iEnd - iBegin)*0.001, |
| 231 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 232 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 233 } |
| 234 } |
| 235 |
| 236 #define BEGIN_TIMER beginTimer() |
| 237 #define END_TIMER endTimer() |
| 238 #define HAS_TIMER 1 |
| 239 |
| 240 #elif (defined(_WIN32) || defined(WIN32)) |
| 241 |
| 242 /* Saved resource information for the beginning of an operation */ |
| 243 static HANDLE hProcess; |
| 244 static FILETIME ftKernelBegin; |
| 245 static FILETIME ftUserBegin; |
| 246 static sqlite3_int64 ftWallBegin; |
| 247 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 248 LPFILETIME, LPFILETIME); |
| 249 static GETPROCTIMES getProcessTimesAddr = NULL; |
| 250 |
| 251 /* |
| 252 ** Check to see if we have timer support. Return 1 if necessary |
| 253 ** support found (or found previously). |
| 254 */ |
| 255 static int hasTimer(void){ |
| 256 if( getProcessTimesAddr ){ |
| 257 return 1; |
| 258 } else { |
| 259 /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 260 ** versions. See if the version we are running on has it, and if it |
| 261 ** does, save off a pointer to it and the current process handle. |
| 262 */ |
| 263 hProcess = GetCurrentProcess(); |
| 264 if( hProcess ){ |
| 265 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 266 if( NULL != hinstLib ){ |
| 267 getProcessTimesAddr = |
| 268 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 269 if( NULL != getProcessTimesAddr ){ |
| 270 return 1; |
| 271 } |
| 272 FreeLibrary(hinstLib); |
| 273 } |
| 274 } |
| 275 } |
| 276 return 0; |
| 277 } |
| 278 |
| 279 /* |
| 280 ** Begin timing an operation |
| 281 */ |
| 282 static void beginTimer(void){ |
| 283 if( enableTimer && getProcessTimesAddr ){ |
| 284 FILETIME ftCreation, ftExit; |
| 285 getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 286 &ftKernelBegin,&ftUserBegin); |
| 287 ftWallBegin = timeOfDay(); |
| 288 } |
| 289 } |
| 290 |
| 291 /* Return the difference of two FILETIME structs in seconds */ |
| 292 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 293 sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 294 sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 295 return (double) ((i64End - i64Start) / 10000000.0); |
| 296 } |
| 297 |
| 298 /* |
| 299 ** Print the timing results. |
| 300 */ |
| 301 static void endTimer(void){ |
| 302 if( enableTimer && getProcessTimesAddr){ |
| 303 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 304 sqlite3_int64 ftWallEnd = timeOfDay(); |
| 305 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 306 printf("Run Time: real %.3f user %f sys %f\n", |
| 307 (ftWallEnd - ftWallBegin)*0.001, |
| 308 timeDiff(&ftUserBegin, &ftUserEnd), |
| 309 timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 310 } |
| 311 } |
| 312 |
| 313 #define BEGIN_TIMER beginTimer() |
| 314 #define END_TIMER endTimer() |
| 315 #define HAS_TIMER hasTimer() |
| 316 |
| 317 #else |
| 318 #define BEGIN_TIMER |
| 319 #define END_TIMER |
| 320 #define HAS_TIMER 0 |
| 321 #endif |
| 322 |
| 323 /* |
| 324 ** Used to prevent warnings about unused parameters |
| 325 */ |
| 326 #define UNUSED_PARAMETER(x) (void)(x) |
| 327 |
| 328 /* |
| 329 ** If the following flag is set, then command execution stops |
| 330 ** at an error if we are not interactive. |
| 331 */ |
| 332 static int bail_on_error = 0; |
| 333 |
| 334 /* |
| 335 ** Threat stdin as an interactive input if the following variable |
| 336 ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 337 */ |
| 338 static int stdin_is_interactive = 1; |
| 339 |
| 340 /* |
| 341 ** On Windows systems we have to know if standard output is a console |
| 342 ** in order to translate UTF-8 into MBCS. The following variable is |
| 343 ** true if translation is required. |
| 344 */ |
| 345 static int stdout_is_console = 1; |
| 346 |
| 347 /* |
| 348 ** The following is the open SQLite database. We make a pointer |
| 349 ** to this database a static variable so that it can be accessed |
| 350 ** by the SIGINT handler to interrupt database processing. |
| 351 */ |
| 352 static sqlite3 *globalDb = 0; |
| 353 |
| 354 /* |
| 355 ** True if an interrupt (Control-C) has been received. |
| 356 */ |
| 357 static volatile int seenInterrupt = 0; |
| 358 |
| 359 /* |
| 360 ** This is the name of our program. It is set in main(), used |
| 361 ** in a number of other places, mostly for error messages. |
| 362 */ |
| 363 static char *Argv0; |
| 364 |
| 365 /* |
| 366 ** Prompt strings. Initialized in main. Settable with |
| 367 ** .prompt main continue |
| 368 */ |
| 369 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 370 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 371 |
| 372 /* |
| 373 ** Render output like fprintf(). Except, if the output is going to the |
| 374 ** console and if this is running on a Windows machine, translate the |
| 375 ** output from UTF-8 into MBCS. |
| 376 */ |
| 377 #if defined(_WIN32) || defined(WIN32) |
| 378 void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 379 va_list ap; |
| 380 va_start(ap, zFormat); |
| 381 if( stdout_is_console && (out==stdout || out==stderr) ){ |
| 382 char *z1 = sqlite3_vmprintf(zFormat, ap); |
| 383 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
| 384 sqlite3_free(z1); |
| 385 fputs(z2, out); |
| 386 sqlite3_free(z2); |
| 387 }else{ |
| 388 vfprintf(out, zFormat, ap); |
| 389 } |
| 390 va_end(ap); |
| 391 } |
| 392 #elif !defined(utf8_printf) |
| 393 # define utf8_printf fprintf |
| 394 #endif |
| 395 |
| 396 /* |
| 397 ** Render output like fprintf(). This should not be used on anything that |
| 398 ** includes string formatting (e.g. "%s"). |
| 399 */ |
| 400 #if !defined(raw_printf) |
| 401 # define raw_printf fprintf |
| 402 #endif |
| 403 |
| 404 /* |
| 405 ** Write I/O traces to the following stream. |
| 406 */ |
| 407 #ifdef SQLITE_ENABLE_IOTRACE |
| 408 static FILE *iotrace = 0; |
| 409 #endif |
| 410 |
| 411 /* |
| 412 ** This routine works like printf in that its first argument is a |
| 413 ** format string and subsequent arguments are values to be substituted |
| 414 ** in place of % fields. The result of formatting this string |
| 415 ** is written to iotrace. |
| 416 */ |
| 417 #ifdef SQLITE_ENABLE_IOTRACE |
| 418 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
| 419 va_list ap; |
| 420 char *z; |
| 421 if( iotrace==0 ) return; |
| 422 va_start(ap, zFormat); |
| 423 z = sqlite3_vmprintf(zFormat, ap); |
| 424 va_end(ap); |
| 425 utf8_printf(iotrace, "%s", z); |
| 426 sqlite3_free(z); |
| 427 } |
| 428 #endif |
| 429 |
| 430 |
| 431 /* |
| 432 ** Determines if a string is a number of not. |
| 433 */ |
| 434 static int isNumber(const char *z, int *realnum){ |
| 435 if( *z=='-' || *z=='+' ) z++; |
| 436 if( !IsDigit(*z) ){ |
| 437 return 0; |
| 438 } |
| 439 z++; |
| 440 if( realnum ) *realnum = 0; |
| 441 while( IsDigit(*z) ){ z++; } |
| 442 if( *z=='.' ){ |
| 443 z++; |
| 444 if( !IsDigit(*z) ) return 0; |
| 445 while( IsDigit(*z) ){ z++; } |
| 446 if( realnum ) *realnum = 1; |
| 447 } |
| 448 if( *z=='e' || *z=='E' ){ |
| 449 z++; |
| 450 if( *z=='+' || *z=='-' ) z++; |
| 451 if( !IsDigit(*z) ) return 0; |
| 452 while( IsDigit(*z) ){ z++; } |
| 453 if( realnum ) *realnum = 1; |
| 454 } |
| 455 return *z==0; |
| 456 } |
| 457 |
| 458 /* |
| 459 ** A global char* and an SQL function to access its current value |
| 460 ** from within an SQL statement. This program used to use the |
| 461 ** sqlite_exec_printf() API to substitue a string into an SQL statement. |
| 462 ** The correct way to do this with sqlite3 is to use the bind API, but |
| 463 ** since the shell is built around the callback paradigm it would be a lot |
| 464 ** of work. Instead just use this hack, which is quite harmless. |
| 465 */ |
| 466 static const char *zShellStatic = 0; |
| 467 static void shellstaticFunc( |
| 468 sqlite3_context *context, |
| 469 int argc, |
| 470 sqlite3_value **argv |
| 471 ){ |
| 472 assert( 0==argc ); |
| 473 assert( zShellStatic ); |
| 474 UNUSED_PARAMETER(argc); |
| 475 UNUSED_PARAMETER(argv); |
| 476 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); |
| 477 } |
| 478 |
| 479 |
| 480 /* |
| 481 ** Compute a string length that is limited to what can be stored in |
| 482 ** lower 30 bits of a 32-bit signed integer. |
| 483 */ |
| 484 static int strlen30(const char *z){ |
| 485 const char *z2 = z; |
| 486 while( *z2 ){ z2++; } |
| 487 return 0x3fffffff & (int)(z2 - z); |
| 488 } |
| 489 |
| 490 /* |
| 491 ** This routine reads a line of text from FILE in, stores |
| 492 ** the text in memory obtained from malloc() and returns a pointer |
| 493 ** to the text. NULL is returned at end of file, or if malloc() |
| 494 ** fails. |
| 495 ** |
| 496 ** If zLine is not NULL then it is a malloced buffer returned from |
| 497 ** a previous call to this routine that may be reused. |
| 498 */ |
| 499 static char *local_getline(char *zLine, FILE *in){ |
| 500 int nLine = zLine==0 ? 0 : 100; |
| 501 int n = 0; |
| 502 |
| 503 while( 1 ){ |
| 504 if( n+100>nLine ){ |
| 505 nLine = nLine*2 + 100; |
| 506 zLine = realloc(zLine, nLine); |
| 507 if( zLine==0 ) return 0; |
| 508 } |
| 509 if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 510 if( n==0 ){ |
| 511 free(zLine); |
| 512 return 0; |
| 513 } |
| 514 zLine[n] = 0; |
| 515 break; |
| 516 } |
| 517 while( zLine[n] ) n++; |
| 518 if( n>0 && zLine[n-1]=='\n' ){ |
| 519 n--; |
| 520 if( n>0 && zLine[n-1]=='\r' ) n--; |
| 521 zLine[n] = 0; |
| 522 break; |
| 523 } |
| 524 } |
| 525 #if defined(_WIN32) || defined(WIN32) |
| 526 /* For interactive input on Windows systems, translate the |
| 527 ** multi-byte characterset characters into UTF-8. */ |
| 528 if( stdin_is_interactive && in==stdin ){ |
| 529 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| 530 if( zTrans ){ |
| 531 int nTrans = strlen30(zTrans)+1; |
| 532 if( nTrans>nLine ){ |
| 533 zLine = realloc(zLine, nTrans); |
| 534 if( zLine==0 ){ |
| 535 sqlite3_free(zTrans); |
| 536 return 0; |
| 537 } |
| 538 } |
| 539 memcpy(zLine, zTrans, nTrans); |
| 540 sqlite3_free(zTrans); |
| 541 } |
| 542 } |
| 543 #endif /* defined(_WIN32) || defined(WIN32) */ |
| 544 return zLine; |
| 545 } |
| 546 |
| 547 /* |
| 548 ** Retrieve a single line of input text. |
| 549 ** |
| 550 ** If in==0 then read from standard input and prompt before each line. |
| 551 ** If isContinuation is true, then a continuation prompt is appropriate. |
| 552 ** If isContinuation is zero, then the main prompt should be used. |
| 553 ** |
| 554 ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 555 ** routine that can be reused. |
| 556 ** |
| 557 ** The result is stored in space obtained from malloc() and must either |
| 558 ** be freed by the caller or else passed back into this routine via the |
| 559 ** zPrior argument for reuse. |
| 560 */ |
| 561 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 562 char *zPrompt; |
| 563 char *zResult; |
| 564 if( in!=0 ){ |
| 565 zResult = local_getline(zPrior, in); |
| 566 }else{ |
| 567 zPrompt = isContinuation ? continuePrompt : mainPrompt; |
| 568 #if SHELL_USE_LOCAL_GETLINE |
| 569 printf("%s", zPrompt); |
| 570 fflush(stdout); |
| 571 zResult = local_getline(zPrior, stdin); |
| 572 #else |
| 573 free(zPrior); |
| 574 zResult = shell_readline(zPrompt); |
| 575 if( zResult && *zResult ) shell_add_history(zResult); |
| 576 #endif |
| 577 } |
| 578 return zResult; |
| 579 } |
| 580 |
| 581 #if defined(SQLITE_ENABLE_SESSION) |
| 582 /* |
| 583 ** State information for a single open session |
| 584 */ |
| 585 typedef struct OpenSession OpenSession; |
| 586 struct OpenSession { |
| 587 char *zName; /* Symbolic name for this session */ |
| 588 int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 589 char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 590 sqlite3_session *p; /* The open session */ |
| 591 }; |
| 592 #endif |
| 593 |
| 594 /* |
| 595 ** Shell output mode information from before ".explain on", |
| 596 ** saved so that it can be restored by ".explain off" |
| 597 */ |
| 598 typedef struct SavedModeInfo SavedModeInfo; |
| 599 struct SavedModeInfo { |
| 600 int valid; /* Is there legit data in here? */ |
| 601 int mode; /* Mode prior to ".explain on" */ |
| 602 int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 603 int colWidth[100]; /* Column widths prior to ".explain on" */ |
| 604 }; |
| 605 |
| 606 /* |
| 607 ** State information about the database connection is contained in an |
| 608 ** instance of the following structure. |
| 609 */ |
| 610 typedef struct ShellState ShellState; |
| 611 struct ShellState { |
| 612 sqlite3 *db; /* The database */ |
| 613 int echoOn; /* True to echo input commands */ |
| 614 int autoExplain; /* Automatically turn on .explain mode */ |
| 615 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
| 616 int statsOn; /* True to display memory stats before each finalize */ |
| 617 int scanstatsOn; /* True to display scan stats before each finalize */ |
| 618 int countChanges; /* True to display change counts */ |
| 619 int backslashOn; /* Resolve C-style \x escapes in SQL input text */ |
| 620 int outCount; /* Revert to stdout when reaching zero */ |
| 621 int cnt; /* Number of records displayed so far */ |
| 622 FILE *out; /* Write results here */ |
| 623 FILE *traceOut; /* Output for sqlite3_trace() */ |
| 624 int nErr; /* Number of errors seen */ |
| 625 int mode; /* An output mode setting */ |
| 626 int cMode; /* temporary output mode for the current query */ |
| 627 int normalMode; /* Output mode before ".explain on" */ |
| 628 int writableSchema; /* True if PRAGMA writable_schema=ON */ |
| 629 int showHeader; /* True to show column names in List or Column mode */ |
| 630 int nCheck; /* Number of ".check" commands run */ |
| 631 unsigned shellFlgs; /* Various flags */ |
| 632 char *zDestTable; /* Name of destination table when MODE_Insert */ |
| 633 char zTestcase[30]; /* Name of current test case */ |
| 634 char colSeparator[20]; /* Column separator character for several modes */ |
| 635 char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
| 636 int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 637 int actualWidth[100]; /* Actual width of each column */ |
| 638 char nullValue[20]; /* The text to print when a NULL comes back from |
| 639 ** the database */ |
| 640 char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 641 const char *zDbFilename; /* name of the database file */ |
| 642 char *zFreeOnClose; /* Filename to free when closing */ |
| 643 const char *zVfs; /* Name of VFS to use */ |
| 644 sqlite3_stmt *pStmt; /* Current statement if any. */ |
| 645 FILE *pLog; /* Write log output here */ |
| 646 int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 647 int nIndent; /* Size of array aiIndent[] */ |
| 648 int iIndent; /* Index of current op in aiIndent[] */ |
| 649 #if defined(SQLITE_ENABLE_SESSION) |
| 650 int nSession; /* Number of active sessions */ |
| 651 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 652 #endif |
| 653 }; |
| 654 |
| 655 /* |
| 656 ** These are the allowed shellFlgs values |
| 657 */ |
| 658 #define SHFLG_Scratch 0x00001 /* The --scratch option is used */ |
| 659 #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ |
| 660 #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ |
| 661 |
| 662 /* |
| 663 ** These are the allowed modes. |
| 664 */ |
| 665 #define MODE_Line 0 /* One column per line. Blank line between records */ |
| 666 #define MODE_Column 1 /* One record per line in neat columns */ |
| 667 #define MODE_List 2 /* One record per line with a separator */ |
| 668 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 669 #define MODE_Html 4 /* Generate an XHTML table */ |
| 670 #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
| 671 #define MODE_Quote 6 /* Quote values as for SQL */ |
| 672 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 673 #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 674 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 675 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 676 #define MODE_Pretty 11 /* Pretty-print schemas */ |
| 677 |
| 678 static const char *modeDescr[] = { |
| 679 "line", |
| 680 "column", |
| 681 "list", |
| 682 "semi", |
| 683 "html", |
| 684 "insert", |
| 685 "quote", |
| 686 "tcl", |
| 687 "csv", |
| 688 "explain", |
| 689 "ascii", |
| 690 "prettyprint", |
| 691 }; |
| 692 |
| 693 /* |
| 694 ** These are the column/row/line separators used by the various |
| 695 ** import/export modes. |
| 696 */ |
| 697 #define SEP_Column "|" |
| 698 #define SEP_Row "\n" |
| 699 #define SEP_Tab "\t" |
| 700 #define SEP_Space " " |
| 701 #define SEP_Comma "," |
| 702 #define SEP_CrLf "\r\n" |
| 703 #define SEP_Unit "\x1F" |
| 704 #define SEP_Record "\x1E" |
| 705 |
| 706 /* |
| 707 ** Number of elements in an array |
| 708 */ |
| 709 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
| 710 |
| 711 /* |
| 712 ** A callback for the sqlite3_log() interface. |
| 713 */ |
| 714 static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 715 ShellState *p = (ShellState*)pArg; |
| 716 if( p->pLog==0 ) return; |
| 717 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 718 fflush(p->pLog); |
| 719 } |
| 720 |
| 721 /* |
| 722 ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 723 */ |
| 724 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 725 int i; |
| 726 char *zBlob = (char *)pBlob; |
| 727 raw_printf(out,"X'"); |
| 728 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 729 raw_printf(out,"'"); |
| 730 } |
| 731 |
| 732 /* |
| 733 ** Output the given string as a quoted string using SQL quoting conventions. |
| 734 */ |
| 735 static void output_quoted_string(FILE *out, const char *z){ |
| 736 int i; |
| 737 int nSingle = 0; |
| 738 setBinaryMode(out, 1); |
| 739 for(i=0; z[i]; i++){ |
| 740 if( z[i]=='\'' ) nSingle++; |
| 741 } |
| 742 if( nSingle==0 ){ |
| 743 utf8_printf(out,"'%s'",z); |
| 744 }else{ |
| 745 raw_printf(out,"'"); |
| 746 while( *z ){ |
| 747 for(i=0; z[i] && z[i]!='\''; i++){} |
| 748 if( i==0 ){ |
| 749 raw_printf(out,"''"); |
| 750 z++; |
| 751 }else if( z[i]=='\'' ){ |
| 752 utf8_printf(out,"%.*s''",i,z); |
| 753 z += i+1; |
| 754 }else{ |
| 755 utf8_printf(out,"%s",z); |
| 756 break; |
| 757 } |
| 758 } |
| 759 raw_printf(out,"'"); |
| 760 } |
| 761 setTextMode(out, 1); |
| 762 } |
| 763 |
| 764 /* |
| 765 ** Output the given string as a quoted according to C or TCL quoting rules. |
| 766 */ |
| 767 static void output_c_string(FILE *out, const char *z){ |
| 768 unsigned int c; |
| 769 fputc('"', out); |
| 770 while( (c = *(z++))!=0 ){ |
| 771 if( c=='\\' ){ |
| 772 fputc(c, out); |
| 773 fputc(c, out); |
| 774 }else if( c=='"' ){ |
| 775 fputc('\\', out); |
| 776 fputc('"', out); |
| 777 }else if( c=='\t' ){ |
| 778 fputc('\\', out); |
| 779 fputc('t', out); |
| 780 }else if( c=='\n' ){ |
| 781 fputc('\\', out); |
| 782 fputc('n', out); |
| 783 }else if( c=='\r' ){ |
| 784 fputc('\\', out); |
| 785 fputc('r', out); |
| 786 }else if( !isprint(c&0xff) ){ |
| 787 raw_printf(out, "\\%03o", c&0xff); |
| 788 }else{ |
| 789 fputc(c, out); |
| 790 } |
| 791 } |
| 792 fputc('"', out); |
| 793 } |
| 794 |
| 795 /* |
| 796 ** Output the given string with characters that are special to |
| 797 ** HTML escaped. |
| 798 */ |
| 799 static void output_html_string(FILE *out, const char *z){ |
| 800 int i; |
| 801 if( z==0 ) z = ""; |
| 802 while( *z ){ |
| 803 for(i=0; z[i] |
| 804 && z[i]!='<' |
| 805 && z[i]!='&' |
| 806 && z[i]!='>' |
| 807 && z[i]!='\"' |
| 808 && z[i]!='\''; |
| 809 i++){} |
| 810 if( i>0 ){ |
| 811 utf8_printf(out,"%.*s",i,z); |
| 812 } |
| 813 if( z[i]=='<' ){ |
| 814 raw_printf(out,"<"); |
| 815 }else if( z[i]=='&' ){ |
| 816 raw_printf(out,"&"); |
| 817 }else if( z[i]=='>' ){ |
| 818 raw_printf(out,">"); |
| 819 }else if( z[i]=='\"' ){ |
| 820 raw_printf(out,"""); |
| 821 }else if( z[i]=='\'' ){ |
| 822 raw_printf(out,"'"); |
| 823 }else{ |
| 824 break; |
| 825 } |
| 826 z += i + 1; |
| 827 } |
| 828 } |
| 829 |
| 830 /* |
| 831 ** If a field contains any character identified by a 1 in the following |
| 832 ** array, then the string must be quoted for CSV. |
| 833 */ |
| 834 static const char needCsvQuote[] = { |
| 835 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 836 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 837 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 838 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 839 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 842 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 843 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 844 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 845 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 846 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 847 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 848 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 849 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 850 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 851 }; |
| 852 |
| 853 /* |
| 854 ** Output a single term of CSV. Actually, p->colSeparator is used for |
| 855 ** the separator, which may or may not be a comma. p->nullValue is |
| 856 ** the null value. Strings are quoted if necessary. The separator |
| 857 ** is only issued if bSep is true. |
| 858 */ |
| 859 static void output_csv(ShellState *p, const char *z, int bSep){ |
| 860 FILE *out = p->out; |
| 861 if( z==0 ){ |
| 862 utf8_printf(out,"%s",p->nullValue); |
| 863 }else{ |
| 864 int i; |
| 865 int nSep = strlen30(p->colSeparator); |
| 866 for(i=0; z[i]; i++){ |
| 867 if( needCsvQuote[((unsigned char*)z)[i]] |
| 868 || (z[i]==p->colSeparator[0] && |
| 869 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
| 870 i = 0; |
| 871 break; |
| 872 } |
| 873 } |
| 874 if( i==0 ){ |
| 875 putc('"', out); |
| 876 for(i=0; z[i]; i++){ |
| 877 if( z[i]=='"' ) putc('"', out); |
| 878 putc(z[i], out); |
| 879 } |
| 880 putc('"', out); |
| 881 }else{ |
| 882 utf8_printf(out, "%s", z); |
| 883 } |
| 884 } |
| 885 if( bSep ){ |
| 886 utf8_printf(p->out, "%s", p->colSeparator); |
| 887 } |
| 888 } |
| 889 |
| 890 #ifdef SIGINT |
| 891 /* |
| 892 ** This routine runs when the user presses Ctrl-C |
| 893 */ |
| 894 static void interrupt_handler(int NotUsed){ |
| 895 UNUSED_PARAMETER(NotUsed); |
| 896 seenInterrupt++; |
| 897 if( seenInterrupt>2 ) exit(1); |
| 898 if( globalDb ) sqlite3_interrupt(globalDb); |
| 899 } |
| 900 #endif |
| 901 |
| 902 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 903 /* |
| 904 ** When the ".auth ON" is set, the following authorizer callback is |
| 905 ** invoked. It always returns SQLITE_OK. |
| 906 */ |
| 907 static int shellAuth( |
| 908 void *pClientData, |
| 909 int op, |
| 910 const char *zA1, |
| 911 const char *zA2, |
| 912 const char *zA3, |
| 913 const char *zA4 |
| 914 ){ |
| 915 ShellState *p = (ShellState*)pClientData; |
| 916 static const char *azAction[] = { 0, |
| 917 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 918 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 919 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 920 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 921 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 922 "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 923 "PRAGMA", "READ", "SELECT", |
| 924 "TRANSACTION", "UPDATE", "ATTACH", |
| 925 "DETACH", "ALTER_TABLE", "REINDEX", |
| 926 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 927 "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 928 }; |
| 929 int i; |
| 930 const char *az[4]; |
| 931 az[0] = zA1; |
| 932 az[1] = zA2; |
| 933 az[2] = zA3; |
| 934 az[3] = zA4; |
| 935 utf8_printf(p->out, "authorizer: %s", azAction[op]); |
| 936 for(i=0; i<4; i++){ |
| 937 raw_printf(p->out, " "); |
| 938 if( az[i] ){ |
| 939 output_c_string(p->out, az[i]); |
| 940 }else{ |
| 941 raw_printf(p->out, "NULL"); |
| 942 } |
| 943 } |
| 944 raw_printf(p->out, "\n"); |
| 945 return SQLITE_OK; |
| 946 } |
| 947 #endif |
| 948 |
| 949 /* |
| 950 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 951 ** |
| 952 ** This routine converts some CREATE TABLE statements for shadow tables |
| 953 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 954 */ |
| 955 static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 956 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 957 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 958 }else{ |
| 959 utf8_printf(out, "%s%s", z, zTail); |
| 960 } |
| 961 } |
| 962 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 963 char c = z[n]; |
| 964 z[n] = 0; |
| 965 printSchemaLine(out, z, zTail); |
| 966 z[n] = c; |
| 967 } |
| 968 |
| 969 /* |
| 970 ** This is the callback routine that the shell |
| 971 ** invokes for each row of a query result. |
| 972 */ |
| 973 static int shell_callback( |
| 974 void *pArg, |
| 975 int nArg, /* Number of result columns */ |
| 976 char **azArg, /* Text of each result column */ |
| 977 char **azCol, /* Column names */ |
| 978 int *aiType /* Column types */ |
| 979 ){ |
| 980 int i; |
| 981 ShellState *p = (ShellState*)pArg; |
| 982 |
| 983 switch( p->cMode ){ |
| 984 case MODE_Line: { |
| 985 int w = 5; |
| 986 if( azArg==0 ) break; |
| 987 for(i=0; i<nArg; i++){ |
| 988 int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 989 if( len>w ) w = len; |
| 990 } |
| 991 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
| 992 for(i=0; i<nArg; i++){ |
| 993 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
| 994 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 995 } |
| 996 break; |
| 997 } |
| 998 case MODE_Explain: |
| 999 case MODE_Column: { |
| 1000 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1001 const int *colWidth; |
| 1002 int showHdr; |
| 1003 char *rowSep; |
| 1004 if( p->cMode==MODE_Column ){ |
| 1005 colWidth = p->colWidth; |
| 1006 showHdr = p->showHeader; |
| 1007 rowSep = p->rowSeparator; |
| 1008 }else{ |
| 1009 colWidth = aExplainWidths; |
| 1010 showHdr = 1; |
| 1011 rowSep = SEP_Row; |
| 1012 } |
| 1013 if( p->cnt++==0 ){ |
| 1014 for(i=0; i<nArg; i++){ |
| 1015 int w, n; |
| 1016 if( i<ArraySize(p->colWidth) ){ |
| 1017 w = colWidth[i]; |
| 1018 }else{ |
| 1019 w = 0; |
| 1020 } |
| 1021 if( w==0 ){ |
| 1022 w = strlen30(azCol[i] ? azCol[i] : ""); |
| 1023 if( w<10 ) w = 10; |
| 1024 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
| 1025 if( w<n ) w = n; |
| 1026 } |
| 1027 if( i<ArraySize(p->actualWidth) ){ |
| 1028 p->actualWidth[i] = w; |
| 1029 } |
| 1030 if( showHdr ){ |
| 1031 if( w<0 ){ |
| 1032 utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], |
| 1033 i==nArg-1 ? rowSep : " "); |
| 1034 }else{ |
| 1035 utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], |
| 1036 i==nArg-1 ? rowSep : " "); |
| 1037 } |
| 1038 } |
| 1039 } |
| 1040 if( showHdr ){ |
| 1041 for(i=0; i<nArg; i++){ |
| 1042 int w; |
| 1043 if( i<ArraySize(p->actualWidth) ){ |
| 1044 w = p->actualWidth[i]; |
| 1045 if( w<0 ) w = -w; |
| 1046 }else{ |
| 1047 w = 10; |
| 1048 } |
| 1049 utf8_printf(p->out,"%-*.*s%s",w,w, |
| 1050 "----------------------------------------------------------" |
| 1051 "----------------------------------------------------------", |
| 1052 i==nArg-1 ? rowSep : " "); |
| 1053 } |
| 1054 } |
| 1055 } |
| 1056 if( azArg==0 ) break; |
| 1057 for(i=0; i<nArg; i++){ |
| 1058 int w; |
| 1059 if( i<ArraySize(p->actualWidth) ){ |
| 1060 w = p->actualWidth[i]; |
| 1061 }else{ |
| 1062 w = 10; |
| 1063 } |
| 1064 if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
| 1065 w = strlen30(azArg[i]); |
| 1066 } |
| 1067 if( i==1 && p->aiIndent && p->pStmt ){ |
| 1068 if( p->iIndent<p->nIndent ){ |
| 1069 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
| 1070 } |
| 1071 p->iIndent++; |
| 1072 } |
| 1073 if( w<0 ){ |
| 1074 utf8_printf(p->out,"%*.*s%s",-w,-w, |
| 1075 azArg[i] ? azArg[i] : p->nullValue, |
| 1076 i==nArg-1 ? rowSep : " "); |
| 1077 }else{ |
| 1078 utf8_printf(p->out,"%-*.*s%s",w,w, |
| 1079 azArg[i] ? azArg[i] : p->nullValue, |
| 1080 i==nArg-1 ? rowSep : " "); |
| 1081 } |
| 1082 } |
| 1083 break; |
| 1084 } |
| 1085 case MODE_Semi: { /* .schema and .fullschema output */ |
| 1086 printSchemaLine(p->out, azArg[0], ";\n"); |
| 1087 break; |
| 1088 } |
| 1089 case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1090 char *z; |
| 1091 int j; |
| 1092 int nParen = 0; |
| 1093 char cEnd = 0; |
| 1094 char c; |
| 1095 int nLine = 0; |
| 1096 assert( nArg==1 ); |
| 1097 if( azArg[0]==0 ) break; |
| 1098 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1099 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1100 ){ |
| 1101 utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1102 break; |
| 1103 } |
| 1104 z = sqlite3_mprintf("%s", azArg[0]); |
| 1105 j = 0; |
| 1106 for(i=0; IsSpace(z[i]); i++){} |
| 1107 for(; (c = z[i])!=0; i++){ |
| 1108 if( IsSpace(c) ){ |
| 1109 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1110 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1111 j--; |
| 1112 } |
| 1113 z[j++] = c; |
| 1114 } |
| 1115 while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1116 z[j] = 0; |
| 1117 if( strlen30(z)>=79 ){ |
| 1118 for(i=j=0; (c = z[i])!=0; i++){ |
| 1119 if( c==cEnd ){ |
| 1120 cEnd = 0; |
| 1121 }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1122 cEnd = c; |
| 1123 }else if( c=='[' ){ |
| 1124 cEnd = ']'; |
| 1125 }else if( c=='(' ){ |
| 1126 nParen++; |
| 1127 }else if( c==')' ){ |
| 1128 nParen--; |
| 1129 if( nLine>0 && nParen==0 && j>0 ){ |
| 1130 printSchemaLineN(p->out, z, j, "\n"); |
| 1131 j = 0; |
| 1132 } |
| 1133 } |
| 1134 z[j++] = c; |
| 1135 if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 1136 if( c=='\n' ) j--; |
| 1137 printSchemaLineN(p->out, z, j, "\n "); |
| 1138 j = 0; |
| 1139 nLine++; |
| 1140 while( IsSpace(z[i+1]) ){ i++; } |
| 1141 } |
| 1142 } |
| 1143 z[j] = 0; |
| 1144 } |
| 1145 printSchemaLine(p->out, z, ";\n"); |
| 1146 sqlite3_free(z); |
| 1147 break; |
| 1148 } |
| 1149 case MODE_List: { |
| 1150 if( p->cnt++==0 && p->showHeader ){ |
| 1151 for(i=0; i<nArg; i++){ |
| 1152 utf8_printf(p->out,"%s%s",azCol[i], |
| 1153 i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 1154 } |
| 1155 } |
| 1156 if( azArg==0 ) break; |
| 1157 for(i=0; i<nArg; i++){ |
| 1158 char *z = azArg[i]; |
| 1159 if( z==0 ) z = p->nullValue; |
| 1160 utf8_printf(p->out, "%s", z); |
| 1161 if( i<nArg-1 ){ |
| 1162 utf8_printf(p->out, "%s", p->colSeparator); |
| 1163 }else{ |
| 1164 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1165 } |
| 1166 } |
| 1167 break; |
| 1168 } |
| 1169 case MODE_Html: { |
| 1170 if( p->cnt++==0 && p->showHeader ){ |
| 1171 raw_printf(p->out,"<TR>"); |
| 1172 for(i=0; i<nArg; i++){ |
| 1173 raw_printf(p->out,"<TH>"); |
| 1174 output_html_string(p->out, azCol[i]); |
| 1175 raw_printf(p->out,"</TH>\n"); |
| 1176 } |
| 1177 raw_printf(p->out,"</TR>\n"); |
| 1178 } |
| 1179 if( azArg==0 ) break; |
| 1180 raw_printf(p->out,"<TR>"); |
| 1181 for(i=0; i<nArg; i++){ |
| 1182 raw_printf(p->out,"<TD>"); |
| 1183 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1184 raw_printf(p->out,"</TD>\n"); |
| 1185 } |
| 1186 raw_printf(p->out,"</TR>\n"); |
| 1187 break; |
| 1188 } |
| 1189 case MODE_Tcl: { |
| 1190 if( p->cnt++==0 && p->showHeader ){ |
| 1191 for(i=0; i<nArg; i++){ |
| 1192 output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
| 1193 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1194 } |
| 1195 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1196 } |
| 1197 if( azArg==0 ) break; |
| 1198 for(i=0; i<nArg; i++){ |
| 1199 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1200 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1201 } |
| 1202 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1203 break; |
| 1204 } |
| 1205 case MODE_Csv: { |
| 1206 setBinaryMode(p->out, 1); |
| 1207 if( p->cnt++==0 && p->showHeader ){ |
| 1208 for(i=0; i<nArg; i++){ |
| 1209 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 1210 } |
| 1211 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1212 } |
| 1213 if( nArg>0 ){ |
| 1214 for(i=0; i<nArg; i++){ |
| 1215 output_csv(p, azArg[i], i<nArg-1); |
| 1216 } |
| 1217 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1218 } |
| 1219 setTextMode(p->out, 1); |
| 1220 break; |
| 1221 } |
| 1222 case MODE_Quote: |
| 1223 case MODE_Insert: { |
| 1224 if( azArg==0 ) break; |
| 1225 if( p->cMode==MODE_Insert ){ |
| 1226 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 1227 if( p->showHeader ){ |
| 1228 raw_printf(p->out,"("); |
| 1229 for(i=0; i<nArg; i++){ |
| 1230 char *zSep = i>0 ? ",": ""; |
| 1231 utf8_printf(p->out, "%s%s", zSep, azCol[i]); |
| 1232 } |
| 1233 raw_printf(p->out,")"); |
| 1234 } |
| 1235 raw_printf(p->out," VALUES("); |
| 1236 }else if( p->cnt==0 && p->showHeader ){ |
| 1237 for(i=0; i<nArg; i++){ |
| 1238 if( i>0 ) raw_printf(p->out, ","); |
| 1239 output_quoted_string(p->out, azCol[i]); |
| 1240 } |
| 1241 raw_printf(p->out,"\n"); |
| 1242 } |
| 1243 p->cnt++; |
| 1244 for(i=0; i<nArg; i++){ |
| 1245 char *zSep = i>0 ? ",": ""; |
| 1246 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 1247 utf8_printf(p->out,"%sNULL",zSep); |
| 1248 }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 1249 if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
| 1250 output_quoted_string(p->out, azArg[i]); |
| 1251 }else if( aiType && (aiType[i]==SQLITE_INTEGER |
| 1252 || aiType[i]==SQLITE_FLOAT) ){ |
| 1253 utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
| 1254 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 1255 const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 1256 int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 1257 if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
| 1258 output_hex_blob(p->out, pBlob, nBlob); |
| 1259 }else if( isNumber(azArg[i], 0) ){ |
| 1260 utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
| 1261 }else{ |
| 1262 if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
| 1263 output_quoted_string(p->out, azArg[i]); |
| 1264 } |
| 1265 } |
| 1266 raw_printf(p->out,p->cMode==MODE_Quote?"\n":");\n"); |
| 1267 break; |
| 1268 } |
| 1269 case MODE_Ascii: { |
| 1270 if( p->cnt++==0 && p->showHeader ){ |
| 1271 for(i=0; i<nArg; i++){ |
| 1272 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 1273 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
| 1274 } |
| 1275 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1276 } |
| 1277 if( azArg==0 ) break; |
| 1278 for(i=0; i<nArg; i++){ |
| 1279 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 1280 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
| 1281 } |
| 1282 utf8_printf(p->out, "%s", p->rowSeparator); |
| 1283 break; |
| 1284 } |
| 1285 } |
| 1286 return 0; |
| 1287 } |
| 1288 |
| 1289 /* |
| 1290 ** This is the callback routine that the SQLite library |
| 1291 ** invokes for each row of a query result. |
| 1292 */ |
| 1293 static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 1294 /* since we don't have type info, call the shell_callback with a NULL value */ |
| 1295 return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 1296 } |
| 1297 |
| 1298 /* |
| 1299 ** Set the destination table field of the ShellState structure to |
| 1300 ** the name of the table given. Escape any quote characters in the |
| 1301 ** table name. |
| 1302 */ |
| 1303 static void set_table_name(ShellState *p, const char *zName){ |
| 1304 int i, n; |
| 1305 int needQuote; |
| 1306 char *z; |
| 1307 |
| 1308 if( p->zDestTable ){ |
| 1309 free(p->zDestTable); |
| 1310 p->zDestTable = 0; |
| 1311 } |
| 1312 if( zName==0 ) return; |
| 1313 needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; |
| 1314 for(i=n=0; zName[i]; i++, n++){ |
| 1315 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ |
| 1316 needQuote = 1; |
| 1317 if( zName[i]=='\'' ) n++; |
| 1318 } |
| 1319 } |
| 1320 if( needQuote ) n += 2; |
| 1321 z = p->zDestTable = malloc( n+1 ); |
| 1322 if( z==0 ){ |
| 1323 raw_printf(stderr,"Error: out of memory\n"); |
| 1324 exit(1); |
| 1325 } |
| 1326 n = 0; |
| 1327 if( needQuote ) z[n++] = '\''; |
| 1328 for(i=0; zName[i]; i++){ |
| 1329 z[n++] = zName[i]; |
| 1330 if( zName[i]=='\'' ) z[n++] = '\''; |
| 1331 } |
| 1332 if( needQuote ) z[n++] = '\''; |
| 1333 z[n] = 0; |
| 1334 } |
| 1335 |
| 1336 /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 1337 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 1338 ** added to zIn, and the result returned in memory obtained from malloc(). |
| 1339 ** zIn, if it was not NULL, is freed. |
| 1340 ** |
| 1341 ** If the third argument, quote, is not '\0', then it is used as a |
| 1342 ** quote character for zAppend. |
| 1343 */ |
| 1344 static char *appendText(char *zIn, char const *zAppend, char quote){ |
| 1345 int len; |
| 1346 int i; |
| 1347 int nAppend = strlen30(zAppend); |
| 1348 int nIn = (zIn?strlen30(zIn):0); |
| 1349 |
| 1350 len = nAppend+nIn+1; |
| 1351 if( quote ){ |
| 1352 len += 2; |
| 1353 for(i=0; i<nAppend; i++){ |
| 1354 if( zAppend[i]==quote ) len++; |
| 1355 } |
| 1356 } |
| 1357 |
| 1358 zIn = (char *)realloc(zIn, len); |
| 1359 if( !zIn ){ |
| 1360 return 0; |
| 1361 } |
| 1362 |
| 1363 if( quote ){ |
| 1364 char *zCsr = &zIn[nIn]; |
| 1365 *zCsr++ = quote; |
| 1366 for(i=0; i<nAppend; i++){ |
| 1367 *zCsr++ = zAppend[i]; |
| 1368 if( zAppend[i]==quote ) *zCsr++ = quote; |
| 1369 } |
| 1370 *zCsr++ = quote; |
| 1371 *zCsr++ = '\0'; |
| 1372 assert( (zCsr-zIn)==len ); |
| 1373 }else{ |
| 1374 memcpy(&zIn[nIn], zAppend, nAppend); |
| 1375 zIn[len-1] = '\0'; |
| 1376 } |
| 1377 |
| 1378 return zIn; |
| 1379 } |
| 1380 |
| 1381 |
| 1382 /* |
| 1383 ** Execute a query statement that will generate SQL output. Print |
| 1384 ** the result columns, comma-separated, on a line and then add a |
| 1385 ** semicolon terminator to the end of that line. |
| 1386 ** |
| 1387 ** If the number of columns is 1 and that column contains text "--" |
| 1388 ** then write the semicolon on a separate line. That way, if a |
| 1389 ** "--" comment occurs at the end of the statement, the comment |
| 1390 ** won't consume the semicolon terminator. |
| 1391 */ |
| 1392 static int run_table_dump_query( |
| 1393 ShellState *p, /* Query context */ |
| 1394 const char *zSelect, /* SELECT statement to extract content */ |
| 1395 const char *zFirstRow /* Print before first row, if not NULL */ |
| 1396 ){ |
| 1397 sqlite3_stmt *pSelect; |
| 1398 int rc; |
| 1399 int nResult; |
| 1400 int i; |
| 1401 const char *z; |
| 1402 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
| 1403 if( rc!=SQLITE_OK || !pSelect ){ |
| 1404 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 1405 sqlite3_errmsg(p->db)); |
| 1406 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 1407 return rc; |
| 1408 } |
| 1409 rc = sqlite3_step(pSelect); |
| 1410 nResult = sqlite3_column_count(pSelect); |
| 1411 while( rc==SQLITE_ROW ){ |
| 1412 if( zFirstRow ){ |
| 1413 utf8_printf(p->out, "%s", zFirstRow); |
| 1414 zFirstRow = 0; |
| 1415 } |
| 1416 z = (const char*)sqlite3_column_text(pSelect, 0); |
| 1417 utf8_printf(p->out, "%s", z); |
| 1418 for(i=1; i<nResult; i++){ |
| 1419 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
| 1420 } |
| 1421 if( z==0 ) z = ""; |
| 1422 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 1423 if( z[0] ){ |
| 1424 raw_printf(p->out, "\n;\n"); |
| 1425 }else{ |
| 1426 raw_printf(p->out, ";\n"); |
| 1427 } |
| 1428 rc = sqlite3_step(pSelect); |
| 1429 } |
| 1430 rc = sqlite3_finalize(pSelect); |
| 1431 if( rc!=SQLITE_OK ){ |
| 1432 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 1433 sqlite3_errmsg(p->db)); |
| 1434 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 1435 } |
| 1436 return rc; |
| 1437 } |
| 1438 |
| 1439 /* |
| 1440 ** Allocate space and save off current error string. |
| 1441 */ |
| 1442 static char *save_err_msg( |
| 1443 sqlite3 *db /* Database to query */ |
| 1444 ){ |
| 1445 int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
| 1446 char *zErrMsg = sqlite3_malloc64(nErrMsg); |
| 1447 if( zErrMsg ){ |
| 1448 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 1449 } |
| 1450 return zErrMsg; |
| 1451 } |
| 1452 |
| 1453 #ifdef __linux__ |
| 1454 /* |
| 1455 ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 1456 */ |
| 1457 static void displayLinuxIoStats(FILE *out){ |
| 1458 FILE *in; |
| 1459 char z[200]; |
| 1460 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 1461 in = fopen(z, "rb"); |
| 1462 if( in==0 ) return; |
| 1463 while( fgets(z, sizeof(z), in)!=0 ){ |
| 1464 static const struct { |
| 1465 const char *zPattern; |
| 1466 const char *zDesc; |
| 1467 } aTrans[] = { |
| 1468 { "rchar: ", "Bytes received by read():" }, |
| 1469 { "wchar: ", "Bytes sent to write():" }, |
| 1470 { "syscr: ", "Read() system calls:" }, |
| 1471 { "syscw: ", "Write() system calls:" }, |
| 1472 { "read_bytes: ", "Bytes read from storage:" }, |
| 1473 { "write_bytes: ", "Bytes written to storage:" }, |
| 1474 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 1475 }; |
| 1476 int i; |
| 1477 for(i=0; i<ArraySize(aTrans); i++){ |
| 1478 int n = (int)strlen(aTrans[i].zPattern); |
| 1479 if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
| 1480 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
| 1481 break; |
| 1482 } |
| 1483 } |
| 1484 } |
| 1485 fclose(in); |
| 1486 } |
| 1487 #endif |
| 1488 |
| 1489 |
| 1490 /* |
| 1491 ** Display memory stats. |
| 1492 */ |
| 1493 static int display_stats( |
| 1494 sqlite3 *db, /* Database to query */ |
| 1495 ShellState *pArg, /* Pointer to ShellState */ |
| 1496 int bReset /* True to reset the stats */ |
| 1497 ){ |
| 1498 int iCur; |
| 1499 int iHiwtr; |
| 1500 |
| 1501 if( pArg && pArg->out ){ |
| 1502 |
| 1503 iHiwtr = iCur = -1; |
| 1504 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); |
| 1505 raw_printf(pArg->out, |
| 1506 "Memory Used: %d (max %d) bytes\n", |
| 1507 iCur, iHiwtr); |
| 1508 iHiwtr = iCur = -1; |
| 1509 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); |
| 1510 raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", |
| 1511 iCur, iHiwtr); |
| 1512 if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 1513 iHiwtr = iCur = -1; |
| 1514 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); |
| 1515 raw_printf(pArg->out, |
| 1516 "Number of Pcache Pages Used: %d (max %d) pages\n", |
| 1517 iCur, iHiwtr); |
| 1518 } |
| 1519 iHiwtr = iCur = -1; |
| 1520 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); |
| 1521 raw_printf(pArg->out, |
| 1522 "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", |
| 1523 iCur, iHiwtr); |
| 1524 if( pArg->shellFlgs & SHFLG_Scratch ){ |
| 1525 iHiwtr = iCur = -1; |
| 1526 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); |
| 1527 raw_printf(pArg->out, |
| 1528 "Number of Scratch Allocations Used: %d (max %d)\n", |
| 1529 iCur, iHiwtr); |
| 1530 } |
| 1531 iHiwtr = iCur = -1; |
| 1532 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); |
| 1533 raw_printf(pArg->out, |
| 1534 "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", |
| 1535 iCur, iHiwtr); |
| 1536 iHiwtr = iCur = -1; |
| 1537 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); |
| 1538 raw_printf(pArg->out, "Largest Allocation: %d bytes\n", |
| 1539 iHiwtr); |
| 1540 iHiwtr = iCur = -1; |
| 1541 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); |
| 1542 raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", |
| 1543 iHiwtr); |
| 1544 iHiwtr = iCur = -1; |
| 1545 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); |
| 1546 raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", |
| 1547 iHiwtr); |
| 1548 #ifdef YYTRACKMAXSTACKDEPTH |
| 1549 iHiwtr = iCur = -1; |
| 1550 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); |
| 1551 raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", |
| 1552 iCur, iHiwtr); |
| 1553 #endif |
| 1554 } |
| 1555 |
| 1556 if( pArg && pArg->out && db ){ |
| 1557 if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 1558 iHiwtr = iCur = -1; |
| 1559 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 1560 &iCur, &iHiwtr, bReset); |
| 1561 raw_printf(pArg->out, |
| 1562 "Lookaside Slots Used: %d (max %d)\n", |
| 1563 iCur, iHiwtr); |
| 1564 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 1565 &iCur, &iHiwtr, bReset); |
| 1566 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 1567 iHiwtr); |
| 1568 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 1569 &iCur, &iHiwtr, bReset); |
| 1570 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 1571 iHiwtr); |
| 1572 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 1573 &iCur, &iHiwtr, bReset); |
| 1574 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 1575 iHiwtr); |
| 1576 } |
| 1577 iHiwtr = iCur = -1; |
| 1578 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
| 1579 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 1580 iCur); |
| 1581 iHiwtr = iCur = -1; |
| 1582 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
| 1583 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
| 1584 iHiwtr = iCur = -1; |
| 1585 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
| 1586 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
| 1587 iHiwtr = iCur = -1; |
| 1588 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
| 1589 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
| 1590 iHiwtr = iCur = -1; |
| 1591 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
| 1592 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| 1593 iCur); |
| 1594 iHiwtr = iCur = -1; |
| 1595 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
| 1596 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| 1597 iCur); |
| 1598 } |
| 1599 |
| 1600 if( pArg && pArg->out && db && pArg->pStmt ){ |
| 1601 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 1602 bReset); |
| 1603 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
| 1604 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
| 1605 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
| 1606 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
| 1607 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
| 1608 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 1609 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
| 1610 } |
| 1611 |
| 1612 #ifdef __linux__ |
| 1613 displayLinuxIoStats(pArg->out); |
| 1614 #endif |
| 1615 |
| 1616 /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 1617 |
| 1618 return 0; |
| 1619 } |
| 1620 |
| 1621 /* |
| 1622 ** Display scan stats. |
| 1623 */ |
| 1624 static void display_scanstats( |
| 1625 sqlite3 *db, /* Database to query */ |
| 1626 ShellState *pArg /* Pointer to ShellState */ |
| 1627 ){ |
| 1628 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1629 UNUSED_PARAMETER(db); |
| 1630 UNUSED_PARAMETER(pArg); |
| 1631 #else |
| 1632 int i, k, n, mx; |
| 1633 raw_printf(pArg->out, "-------- scanstats --------\n"); |
| 1634 mx = 0; |
| 1635 for(k=0; k<=mx; k++){ |
| 1636 double rEstLoop = 1.0; |
| 1637 for(i=n=0; 1; i++){ |
| 1638 sqlite3_stmt *p = pArg->pStmt; |
| 1639 sqlite3_int64 nLoop, nVisit; |
| 1640 double rEst; |
| 1641 int iSid; |
| 1642 const char *zExplain; |
| 1643 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 1644 break; |
| 1645 } |
| 1646 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| 1647 if( iSid>mx ) mx = iSid; |
| 1648 if( iSid!=k ) continue; |
| 1649 if( n==0 ){ |
| 1650 rEstLoop = (double)nLoop; |
| 1651 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| 1652 } |
| 1653 n++; |
| 1654 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 1655 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 1656 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| 1657 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| 1658 rEstLoop *= rEst; |
| 1659 raw_printf(pArg->out, |
| 1660 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| 1661 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| 1662 ); |
| 1663 } |
| 1664 } |
| 1665 raw_printf(pArg->out, "---------------------------\n"); |
| 1666 #endif |
| 1667 } |
| 1668 |
| 1669 /* |
| 1670 ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 1671 ** points to a single nul-terminated string. Return non-zero if zStr |
| 1672 ** is equal, according to strcmp(), to any of the strings in the array. |
| 1673 ** Otherwise, return zero. |
| 1674 */ |
| 1675 static int str_in_array(const char *zStr, const char **azArray){ |
| 1676 int i; |
| 1677 for(i=0; azArray[i]; i++){ |
| 1678 if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 1679 } |
| 1680 return 0; |
| 1681 } |
| 1682 |
| 1683 /* |
| 1684 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| 1685 ** and populate the ShellState.aiIndent[] array with the number of |
| 1686 ** spaces each opcode should be indented before it is output. |
| 1687 ** |
| 1688 ** The indenting rules are: |
| 1689 ** |
| 1690 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 1691 ** all opcodes that occur between the p2 jump destination and the opcode |
| 1692 ** itself by 2 spaces. |
| 1693 ** |
| 1694 ** * For each "Goto", if the jump destination is earlier in the program |
| 1695 ** and ends on one of: |
| 1696 ** Yield SeekGt SeekLt RowSetRead Rewind |
| 1697 ** or if the P1 parameter is one instead of zero, |
| 1698 ** then indent all opcodes between the earlier instruction |
| 1699 ** and "Goto" by 2 spaces. |
| 1700 */ |
| 1701 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| 1702 const char *zSql; /* The text of the SQL statement */ |
| 1703 const char *z; /* Used to check if this is an EXPLAIN */ |
| 1704 int *abYield = 0; /* True if op is an OP_Yield */ |
| 1705 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| 1706 int iOp; /* Index of operation in p->aiIndent[] */ |
| 1707 |
| 1708 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 1709 "NextIfOpen", "PrevIfOpen", 0 }; |
| 1710 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 1711 "Rewind", 0 }; |
| 1712 const char *azGoto[] = { "Goto", 0 }; |
| 1713 |
| 1714 /* Try to figure out if this is really an EXPLAIN statement. If this |
| 1715 ** cannot be verified, return early. */ |
| 1716 if( sqlite3_column_count(pSql)!=8 ){ |
| 1717 p->cMode = p->mode; |
| 1718 return; |
| 1719 } |
| 1720 zSql = sqlite3_sql(pSql); |
| 1721 if( zSql==0 ) return; |
| 1722 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| 1723 if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 1724 p->cMode = p->mode; |
| 1725 return; |
| 1726 } |
| 1727 |
| 1728 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 1729 int i; |
| 1730 int iAddr = sqlite3_column_int(pSql, 0); |
| 1731 const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| 1732 |
| 1733 /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 1734 ** p2 is an instruction address, set variable p2op to the index of that |
| 1735 ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 1736 ** the current instruction is part of a sub-program generated by an |
| 1737 ** SQL trigger or foreign key. */ |
| 1738 int p2 = sqlite3_column_int(pSql, 3); |
| 1739 int p2op = (p2 + (iOp-iAddr)); |
| 1740 |
| 1741 /* Grow the p->aiIndent array as required */ |
| 1742 if( iOp>=nAlloc ){ |
| 1743 if( iOp==0 ){ |
| 1744 /* Do further verfication that this is explain output. Abort if |
| 1745 ** it is not */ |
| 1746 static const char *explainCols[] = { |
| 1747 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 1748 int jj; |
| 1749 for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 1750 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 1751 p->cMode = p->mode; |
| 1752 sqlite3_reset(pSql); |
| 1753 return; |
| 1754 } |
| 1755 } |
| 1756 } |
| 1757 nAlloc += 100; |
| 1758 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 1759 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
| 1760 } |
| 1761 abYield[iOp] = str_in_array(zOp, azYield); |
| 1762 p->aiIndent[iOp] = 0; |
| 1763 p->nIndent = iOp+1; |
| 1764 |
| 1765 if( str_in_array(zOp, azNext) ){ |
| 1766 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 1767 } |
| 1768 if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 1769 && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 1770 ){ |
| 1771 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 1772 } |
| 1773 } |
| 1774 |
| 1775 p->iIndent = 0; |
| 1776 sqlite3_free(abYield); |
| 1777 sqlite3_reset(pSql); |
| 1778 } |
| 1779 |
| 1780 /* |
| 1781 ** Free the array allocated by explain_data_prepare(). |
| 1782 */ |
| 1783 static void explain_data_delete(ShellState *p){ |
| 1784 sqlite3_free(p->aiIndent); |
| 1785 p->aiIndent = 0; |
| 1786 p->nIndent = 0; |
| 1787 p->iIndent = 0; |
| 1788 } |
| 1789 |
| 1790 /* |
| 1791 ** Disable and restore .wheretrace and .selecttrace settings. |
| 1792 */ |
| 1793 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1794 extern int sqlite3SelectTrace; |
| 1795 static int savedSelectTrace; |
| 1796 #endif |
| 1797 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1798 extern int sqlite3WhereTrace; |
| 1799 static int savedWhereTrace; |
| 1800 #endif |
| 1801 static void disable_debug_trace_modes(void){ |
| 1802 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1803 savedSelectTrace = sqlite3SelectTrace; |
| 1804 sqlite3SelectTrace = 0; |
| 1805 #endif |
| 1806 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1807 savedWhereTrace = sqlite3WhereTrace; |
| 1808 sqlite3WhereTrace = 0; |
| 1809 #endif |
| 1810 } |
| 1811 static void restore_debug_trace_modes(void){ |
| 1812 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1813 sqlite3SelectTrace = savedSelectTrace; |
| 1814 #endif |
| 1815 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1816 sqlite3WhereTrace = savedWhereTrace; |
| 1817 #endif |
| 1818 } |
| 1819 |
| 1820 /* |
| 1821 ** Run a prepared statement |
| 1822 */ |
| 1823 static void exec_prepared_stmt( |
| 1824 ShellState *pArg, /* Pointer to ShellState */ |
| 1825 sqlite3_stmt *pStmt, /* Statment to run */ |
| 1826 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 1827 ){ |
| 1828 int rc; |
| 1829 |
| 1830 /* perform the first step. this will tell us if we |
| 1831 ** have a result set or not and how wide it is. |
| 1832 */ |
| 1833 rc = sqlite3_step(pStmt); |
| 1834 /* if we have a result set... */ |
| 1835 if( SQLITE_ROW == rc ){ |
| 1836 /* if we have a callback... */ |
| 1837 if( xCallback ){ |
| 1838 /* allocate space for col name ptr, value ptr, and type */ |
| 1839 int nCol = sqlite3_column_count(pStmt); |
| 1840 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 1841 if( !pData ){ |
| 1842 rc = SQLITE_NOMEM; |
| 1843 }else{ |
| 1844 char **azCols = (char **)pData; /* Names of result columns */ |
| 1845 char **azVals = &azCols[nCol]; /* Results */ |
| 1846 int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 1847 int i, x; |
| 1848 assert(sizeof(int) <= sizeof(char *)); |
| 1849 /* save off ptrs to column names */ |
| 1850 for(i=0; i<nCol; i++){ |
| 1851 azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 1852 } |
| 1853 do{ |
| 1854 /* extract the data and data types */ |
| 1855 for(i=0; i<nCol; i++){ |
| 1856 aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 1857 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 1858 azVals[i] = ""; |
| 1859 }else{ |
| 1860 azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 1861 } |
| 1862 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 1863 rc = SQLITE_NOMEM; |
| 1864 break; /* from for */ |
| 1865 } |
| 1866 } /* end for */ |
| 1867 |
| 1868 /* if data and types extracted successfully... */ |
| 1869 if( SQLITE_ROW == rc ){ |
| 1870 /* call the supplied callback with the result row data */ |
| 1871 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 1872 rc = SQLITE_ABORT; |
| 1873 }else{ |
| 1874 rc = sqlite3_step(pStmt); |
| 1875 } |
| 1876 } |
| 1877 } while( SQLITE_ROW == rc ); |
| 1878 sqlite3_free(pData); |
| 1879 } |
| 1880 }else{ |
| 1881 do{ |
| 1882 rc = sqlite3_step(pStmt); |
| 1883 } while( rc == SQLITE_ROW ); |
| 1884 } |
| 1885 } |
| 1886 } |
| 1887 |
| 1888 /* |
| 1889 ** Execute a statement or set of statements. Print |
| 1890 ** any result rows/columns depending on the current mode |
| 1891 ** set via the supplied callback. |
| 1892 ** |
| 1893 ** This is very similar to SQLite's built-in sqlite3_exec() |
| 1894 ** function except it takes a slightly different callback |
| 1895 ** and callback data argument. |
| 1896 */ |
| 1897 static int shell_exec( |
| 1898 sqlite3 *db, /* An open database */ |
| 1899 const char *zSql, /* SQL to be evaluated */ |
| 1900 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
| 1901 /* (not the same as sqlite3_exec) */ |
| 1902 ShellState *pArg, /* Pointer to ShellState */ |
| 1903 char **pzErrMsg /* Error msg written here */ |
| 1904 ){ |
| 1905 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 1906 int rc = SQLITE_OK; /* Return Code */ |
| 1907 int rc2; |
| 1908 const char *zLeftover; /* Tail of unprocessed SQL */ |
| 1909 |
| 1910 if( pzErrMsg ){ |
| 1911 *pzErrMsg = NULL; |
| 1912 } |
| 1913 |
| 1914 while( zSql[0] && (SQLITE_OK == rc) ){ |
| 1915 static const char *zStmtSql; |
| 1916 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 1917 if( SQLITE_OK != rc ){ |
| 1918 if( pzErrMsg ){ |
| 1919 *pzErrMsg = save_err_msg(db); |
| 1920 } |
| 1921 }else{ |
| 1922 if( !pStmt ){ |
| 1923 /* this happens for a comment or white-space */ |
| 1924 zSql = zLeftover; |
| 1925 while( IsSpace(zSql[0]) ) zSql++; |
| 1926 continue; |
| 1927 } |
| 1928 zStmtSql = sqlite3_sql(pStmt); |
| 1929 if( zStmtSql==0 ) zStmtSql = ""; |
| 1930 while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 1931 |
| 1932 /* save off the prepared statment handle and reset row count */ |
| 1933 if( pArg ){ |
| 1934 pArg->pStmt = pStmt; |
| 1935 pArg->cnt = 0; |
| 1936 } |
| 1937 |
| 1938 /* echo the sql statement if echo on */ |
| 1939 if( pArg && pArg->echoOn ){ |
| 1940 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
| 1941 } |
| 1942 |
| 1943 /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
| 1944 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
| 1945 sqlite3_stmt *pExplain; |
| 1946 char *zEQP; |
| 1947 disable_debug_trace_modes(); |
| 1948 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
| 1949 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 1950 if( rc==SQLITE_OK ){ |
| 1951 while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
| 1952 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 1953 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 1954 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
| 1955 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
| 1956 } |
| 1957 } |
| 1958 sqlite3_finalize(pExplain); |
| 1959 sqlite3_free(zEQP); |
| 1960 if( pArg->autoEQP>=2 ){ |
| 1961 /* Also do an EXPLAIN for ".eqp full" mode */ |
| 1962 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 1963 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 1964 if( rc==SQLITE_OK ){ |
| 1965 pArg->cMode = MODE_Explain; |
| 1966 explain_data_prepare(pArg, pExplain); |
| 1967 exec_prepared_stmt(pArg, pExplain, xCallback); |
| 1968 explain_data_delete(pArg); |
| 1969 } |
| 1970 sqlite3_finalize(pExplain); |
| 1971 sqlite3_free(zEQP); |
| 1972 } |
| 1973 restore_debug_trace_modes(); |
| 1974 } |
| 1975 |
| 1976 if( pArg ){ |
| 1977 pArg->cMode = pArg->mode; |
| 1978 if( pArg->autoExplain |
| 1979 && sqlite3_column_count(pStmt)==8 |
| 1980 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
| 1981 ){ |
| 1982 pArg->cMode = MODE_Explain; |
| 1983 } |
| 1984 |
| 1985 /* If the shell is currently in ".explain" mode, gather the extra |
| 1986 ** data required to add indents to the output.*/ |
| 1987 if( pArg->cMode==MODE_Explain ){ |
| 1988 explain_data_prepare(pArg, pStmt); |
| 1989 } |
| 1990 } |
| 1991 |
| 1992 exec_prepared_stmt(pArg, pStmt, xCallback); |
| 1993 explain_data_delete(pArg); |
| 1994 |
| 1995 /* print usage stats if stats on */ |
| 1996 if( pArg && pArg->statsOn ){ |
| 1997 display_stats(db, pArg, 0); |
| 1998 } |
| 1999 |
| 2000 /* print loop-counters if required */ |
| 2001 if( pArg && pArg->scanstatsOn ){ |
| 2002 display_scanstats(db, pArg); |
| 2003 } |
| 2004 |
| 2005 /* Finalize the statement just executed. If this fails, save a |
| 2006 ** copy of the error message. Otherwise, set zSql to point to the |
| 2007 ** next statement to execute. */ |
| 2008 rc2 = sqlite3_finalize(pStmt); |
| 2009 if( rc!=SQLITE_NOMEM ) rc = rc2; |
| 2010 if( rc==SQLITE_OK ){ |
| 2011 zSql = zLeftover; |
| 2012 while( IsSpace(zSql[0]) ) zSql++; |
| 2013 }else if( pzErrMsg ){ |
| 2014 *pzErrMsg = save_err_msg(db); |
| 2015 } |
| 2016 |
| 2017 /* clear saved stmt handle */ |
| 2018 if( pArg ){ |
| 2019 pArg->pStmt = NULL; |
| 2020 } |
| 2021 } |
| 2022 } /* end while */ |
| 2023 |
| 2024 return rc; |
| 2025 } |
| 2026 |
| 2027 |
| 2028 /* |
| 2029 ** This is a different callback routine used for dumping the database. |
| 2030 ** Each row received by this callback consists of a table name, |
| 2031 ** the table type ("index" or "table") and SQL to create the table. |
| 2032 ** This routine should print text sufficient to recreate the table. |
| 2033 */ |
| 2034 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2035 int rc; |
| 2036 const char *zTable; |
| 2037 const char *zType; |
| 2038 const char *zSql; |
| 2039 const char *zPrepStmt = 0; |
| 2040 ShellState *p = (ShellState *)pArg; |
| 2041 |
| 2042 UNUSED_PARAMETER(azCol); |
| 2043 if( nArg!=3 ) return 1; |
| 2044 zTable = azArg[0]; |
| 2045 zType = azArg[1]; |
| 2046 zSql = azArg[2]; |
| 2047 |
| 2048 if( strcmp(zTable, "sqlite_sequence")==0 ){ |
| 2049 zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
| 2050 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
| 2051 raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 2052 }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 2053 return 0; |
| 2054 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 2055 char *zIns; |
| 2056 if( !p->writableSchema ){ |
| 2057 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 2058 p->writableSchema = 1; |
| 2059 } |
| 2060 zIns = sqlite3_mprintf( |
| 2061 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 2062 "VALUES('table','%q','%q',0,'%q');", |
| 2063 zTable, zTable, zSql); |
| 2064 utf8_printf(p->out, "%s\n", zIns); |
| 2065 sqlite3_free(zIns); |
| 2066 return 0; |
| 2067 }else{ |
| 2068 printSchemaLine(p->out, zSql, ";\n"); |
| 2069 } |
| 2070 |
| 2071 if( strcmp(zType, "table")==0 ){ |
| 2072 sqlite3_stmt *pTableInfo = 0; |
| 2073 char *zSelect = 0; |
| 2074 char *zTableInfo = 0; |
| 2075 char *zTmp = 0; |
| 2076 int nRow = 0; |
| 2077 |
| 2078 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 2079 zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 2080 zTableInfo = appendText(zTableInfo, ");", 0); |
| 2081 |
| 2082 rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0); |
| 2083 free(zTableInfo); |
| 2084 if( rc!=SQLITE_OK || !pTableInfo ){ |
| 2085 return 1; |
| 2086 } |
| 2087 |
| 2088 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
| 2089 /* Always quote the table name, even if it appears to be pure ascii, |
| 2090 ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 2091 zTmp = appendText(zTmp, zTable, '"'); |
| 2092 if( zTmp ){ |
| 2093 zSelect = appendText(zSelect, zTmp, '\''); |
| 2094 free(zTmp); |
| 2095 } |
| 2096 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 2097 rc = sqlite3_step(pTableInfo); |
| 2098 while( rc==SQLITE_ROW ){ |
| 2099 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); |
| 2100 zSelect = appendText(zSelect, "quote(", 0); |
| 2101 zSelect = appendText(zSelect, zText, '"'); |
| 2102 rc = sqlite3_step(pTableInfo); |
| 2103 if( rc==SQLITE_ROW ){ |
| 2104 zSelect = appendText(zSelect, "), ", 0); |
| 2105 }else{ |
| 2106 zSelect = appendText(zSelect, ") ", 0); |
| 2107 } |
| 2108 nRow++; |
| 2109 } |
| 2110 rc = sqlite3_finalize(pTableInfo); |
| 2111 if( rc!=SQLITE_OK || nRow==0 ){ |
| 2112 free(zSelect); |
| 2113 return 1; |
| 2114 } |
| 2115 zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 2116 zSelect = appendText(zSelect, zTable, '"'); |
| 2117 |
| 2118 rc = run_table_dump_query(p, zSelect, zPrepStmt); |
| 2119 if( rc==SQLITE_CORRUPT ){ |
| 2120 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); |
| 2121 run_table_dump_query(p, zSelect, 0); |
| 2122 } |
| 2123 free(zSelect); |
| 2124 } |
| 2125 return 0; |
| 2126 } |
| 2127 |
| 2128 /* |
| 2129 ** Run zQuery. Use dump_callback() as the callback routine so that |
| 2130 ** the contents of the query are output as SQL statements. |
| 2131 ** |
| 2132 ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 2133 ** "ORDER BY rowid DESC" to the end. |
| 2134 */ |
| 2135 static int run_schema_dump_query( |
| 2136 ShellState *p, |
| 2137 const char *zQuery |
| 2138 ){ |
| 2139 int rc; |
| 2140 char *zErr = 0; |
| 2141 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
| 2142 if( rc==SQLITE_CORRUPT ){ |
| 2143 char *zQ2; |
| 2144 int len = strlen30(zQuery); |
| 2145 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 2146 if( zErr ){ |
| 2147 utf8_printf(p->out, "/****** %s ******/\n", zErr); |
| 2148 sqlite3_free(zErr); |
| 2149 zErr = 0; |
| 2150 } |
| 2151 zQ2 = malloc( len+100 ); |
| 2152 if( zQ2==0 ) return rc; |
| 2153 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
| 2154 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 2155 if( rc ){ |
| 2156 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
| 2157 }else{ |
| 2158 rc = SQLITE_CORRUPT; |
| 2159 } |
| 2160 sqlite3_free(zErr); |
| 2161 free(zQ2); |
| 2162 } |
| 2163 return rc; |
| 2164 } |
| 2165 |
| 2166 /* |
| 2167 ** Text of a help message |
| 2168 */ |
| 2169 static char zHelp[] = |
| 2170 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2171 ".auth ON|OFF Show authorizer callbacks\n" |
| 2172 #endif |
| 2173 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
| 2174 ".bail on|off Stop after hitting an error. Default OFF\n" |
| 2175 ".binary on|off Turn binary output on or off. Default OFF\n" |
| 2176 ".changes on|off Show number of rows changed by SQL\n" |
| 2177 ".check GLOB Fail if output since .testcase does not match\n" |
| 2178 ".clone NEWDB Clone data into NEWDB from the existing database\n" |
| 2179 ".databases List names and files of attached databases\n" |
| 2180 ".dbinfo ?DB? Show status information about the database\n" |
| 2181 ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
| 2182 " If TABLE specified, only dump tables matching\n" |
| 2183 " LIKE pattern TABLE.\n" |
| 2184 ".echo on|off Turn command echo on or off\n" |
| 2185 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
| 2186 ".exit Exit this program\n" |
| 2187 ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" |
| 2188 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
| 2189 ".headers on|off Turn display of headers on or off\n" |
| 2190 ".help Show this message\n" |
| 2191 ".import FILE TABLE Import data from FILE into TABLE\n" |
| 2192 #ifndef SQLITE_OMIT_TEST_CONTROL |
| 2193 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 2194 #endif |
| 2195 ".indexes ?TABLE? Show names of all indexes\n" |
| 2196 " If TABLE specified, only show indexes for tables\n" |
| 2197 " matching LIKE pattern TABLE.\n" |
| 2198 #ifdef SQLITE_ENABLE_IOTRACE |
| 2199 ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 2200 #endif |
| 2201 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
| 2202 ".lint OPTIONS Report potential schema issues. Options:\n" |
| 2203 " fkey-indexes Find missing foreign key indexes\n" |
| 2204 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2205 ".load FILE ?ENTRY? Load an extension library\n" |
| 2206 #endif |
| 2207 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
| 2208 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
| 2209 " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
| 2210 " csv Comma-separated values\n" |
| 2211 " column Left-aligned columns. (See .width)\n" |
| 2212 " html HTML <table> code\n" |
| 2213 " insert SQL insert statements for TABLE\n" |
| 2214 " line One value per line\n" |
| 2215 " list Values delimited by .separator strings\n" |
| 2216 " quote Escape answers as for SQL\n" |
| 2217 " tabs Tab-separated values\n" |
| 2218 " tcl TCL list elements\n" |
| 2219 ".nullvalue STRING Use STRING in place of NULL values\n" |
| 2220 ".once FILENAME Output for the next SQL command only to FILENAME\n" |
| 2221 ".open ?--new? ?FILE? Close existing database and reopen FILE\n" |
| 2222 " The --new starts with an empty file\n" |
| 2223 ".output ?FILENAME? Send output to FILENAME or stdout\n" |
| 2224 ".print STRING... Print literal STRING\n" |
| 2225 ".prompt MAIN CONTINUE Replace the standard prompts\n" |
| 2226 ".quit Exit this program\n" |
| 2227 ".read FILENAME Execute SQL in FILENAME\n" |
| 2228 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
| 2229 ".save FILE Write in-memory database into FILE\n" |
| 2230 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
| 2231 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 2232 " Add --indent for pretty-printing\n" |
| 2233 ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 2234 " separator for both the output mode and .import\n" |
| 2235 #if defined(SQLITE_ENABLE_SESSION) |
| 2236 ".session CMD ... Create or control sessions\n" |
| 2237 #endif |
| 2238 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
| 2239 ".show Show the current values for various settings\n" |
| 2240 ".stats ?on|off? Show stats or turn stats on or off\n" |
| 2241 ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
| 2242 ".tables ?TABLE? List names of tables\n" |
| 2243 " If TABLE specified, only list tables matching\n" |
| 2244 " LIKE pattern TABLE.\n" |
| 2245 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
| 2246 ".timeout MS Try opening locked tables for MS milliseconds\n" |
| 2247 ".timer on|off Turn SQL timer on or off\n" |
| 2248 ".trace FILE|off Output each SQL statement as it is run\n" |
| 2249 ".vfsinfo ?AUX? Information about the top-level VFS\n" |
| 2250 ".vfslist List all available VFSes\n" |
| 2251 ".vfsname ?AUX? Print the name of the VFS stack\n" |
| 2252 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
| 2253 " Negative values right-justify\n" |
| 2254 ; |
| 2255 |
| 2256 #if defined(SQLITE_ENABLE_SESSION) |
| 2257 /* |
| 2258 ** Print help information for the ".sessions" command |
| 2259 */ |
| 2260 void session_help(ShellState *p){ |
| 2261 raw_printf(p->out, |
| 2262 ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 2263 "If ?NAME? is omitted, the first defined session is used.\n" |
| 2264 "Subcommands:\n" |
| 2265 " attach TABLE Attach TABLE\n" |
| 2266 " changeset FILE Write a changeset into FILE\n" |
| 2267 " close Close one session\n" |
| 2268 " enable ?BOOLEAN? Set or query the enable bit\n" |
| 2269 " filter GLOB... Reject tables matching GLOBs\n" |
| 2270 " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 2271 " isempty Query whether the session is empty\n" |
| 2272 " list List currently open session names\n" |
| 2273 " open DB NAME Open a new session on DB\n" |
| 2274 " patchset FILE Write a patchset into FILE\n" |
| 2275 ); |
| 2276 } |
| 2277 #endif |
| 2278 |
| 2279 |
| 2280 /* Forward reference */ |
| 2281 static int process_input(ShellState *p, FILE *in); |
| 2282 |
| 2283 /* |
| 2284 ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 2285 ** and return a pointer to the buffer. The caller is responsible for freeing |
| 2286 ** the memory. |
| 2287 ** |
| 2288 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 2289 ** read. |
| 2290 ** |
| 2291 ** For convenience, a nul-terminator byte is always appended to the data read |
| 2292 ** from the file before the buffer is returned. This byte is not included in |
| 2293 ** the final value of (*pnByte), if applicable. |
| 2294 ** |
| 2295 ** NULL is returned if any error is encountered. The final value of *pnByte |
| 2296 ** is undefined in this case. |
| 2297 */ |
| 2298 static char *readFile(const char *zName, int *pnByte){ |
| 2299 FILE *in = fopen(zName, "rb"); |
| 2300 long nIn; |
| 2301 size_t nRead; |
| 2302 char *pBuf; |
| 2303 if( in==0 ) return 0; |
| 2304 fseek(in, 0, SEEK_END); |
| 2305 nIn = ftell(in); |
| 2306 rewind(in); |
| 2307 pBuf = sqlite3_malloc64( nIn+1 ); |
| 2308 if( pBuf==0 ) return 0; |
| 2309 nRead = fread(pBuf, nIn, 1, in); |
| 2310 fclose(in); |
| 2311 if( nRead!=1 ){ |
| 2312 sqlite3_free(pBuf); |
| 2313 return 0; |
| 2314 } |
| 2315 pBuf[nIn] = 0; |
| 2316 if( pnByte ) *pnByte = nIn; |
| 2317 return pBuf; |
| 2318 } |
| 2319 |
| 2320 /* |
| 2321 ** Implementation of the "readfile(X)" SQL function. The entire content |
| 2322 ** of the file named X is read and returned as a BLOB. NULL is returned |
| 2323 ** if the file does not exist or is unreadable. |
| 2324 */ |
| 2325 static void readfileFunc( |
| 2326 sqlite3_context *context, |
| 2327 int argc, |
| 2328 sqlite3_value **argv |
| 2329 ){ |
| 2330 const char *zName; |
| 2331 void *pBuf; |
| 2332 int nBuf; |
| 2333 |
| 2334 UNUSED_PARAMETER(argc); |
| 2335 zName = (const char*)sqlite3_value_text(argv[0]); |
| 2336 if( zName==0 ) return; |
| 2337 pBuf = readFile(zName, &nBuf); |
| 2338 if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
| 2339 } |
| 2340 |
| 2341 /* |
| 2342 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 2343 ** is written into file X. The number of bytes written is returned. Or |
| 2344 ** NULL is returned if something goes wrong, such as being unable to open |
| 2345 ** file X for writing. |
| 2346 */ |
| 2347 static void writefileFunc( |
| 2348 sqlite3_context *context, |
| 2349 int argc, |
| 2350 sqlite3_value **argv |
| 2351 ){ |
| 2352 FILE *out; |
| 2353 const char *z; |
| 2354 sqlite3_int64 rc; |
| 2355 const char *zFile; |
| 2356 |
| 2357 UNUSED_PARAMETER(argc); |
| 2358 zFile = (const char*)sqlite3_value_text(argv[0]); |
| 2359 if( zFile==0 ) return; |
| 2360 out = fopen(zFile, "wb"); |
| 2361 if( out==0 ) return; |
| 2362 z = (const char*)sqlite3_value_blob(argv[1]); |
| 2363 if( z==0 ){ |
| 2364 rc = 0; |
| 2365 }else{ |
| 2366 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
| 2367 } |
| 2368 fclose(out); |
| 2369 sqlite3_result_int64(context, rc); |
| 2370 } |
| 2371 |
| 2372 #if defined(SQLITE_ENABLE_SESSION) |
| 2373 /* |
| 2374 ** Close a single OpenSession object and release all of its associated |
| 2375 ** resources. |
| 2376 */ |
| 2377 static void session_close(OpenSession *pSession){ |
| 2378 int i; |
| 2379 sqlite3session_delete(pSession->p); |
| 2380 sqlite3_free(pSession->zName); |
| 2381 for(i=0; i<pSession->nFilter; i++){ |
| 2382 sqlite3_free(pSession->azFilter[i]); |
| 2383 } |
| 2384 sqlite3_free(pSession->azFilter); |
| 2385 memset(pSession, 0, sizeof(OpenSession)); |
| 2386 } |
| 2387 #endif |
| 2388 |
| 2389 /* |
| 2390 ** Close all OpenSession objects and release all associated resources. |
| 2391 */ |
| 2392 #if defined(SQLITE_ENABLE_SESSION) |
| 2393 static void session_close_all(ShellState *p){ |
| 2394 int i; |
| 2395 for(i=0; i<p->nSession; i++){ |
| 2396 session_close(&p->aSession[i]); |
| 2397 } |
| 2398 p->nSession = 0; |
| 2399 } |
| 2400 #else |
| 2401 # define session_close_all(X) |
| 2402 #endif |
| 2403 |
| 2404 /* |
| 2405 ** Implementation of the xFilter function for an open session. Omit |
| 2406 ** any tables named by ".session filter" but let all other table through. |
| 2407 */ |
| 2408 #if defined(SQLITE_ENABLE_SESSION) |
| 2409 static int session_filter(void *pCtx, const char *zTab){ |
| 2410 OpenSession *pSession = (OpenSession*)pCtx; |
| 2411 int i; |
| 2412 for(i=0; i<pSession->nFilter; i++){ |
| 2413 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 2414 } |
| 2415 return 1; |
| 2416 } |
| 2417 #endif |
| 2418 |
| 2419 /* |
| 2420 ** Make sure the database is open. If it is not, then open it. If |
| 2421 ** the database fails to open, print an error message and exit. |
| 2422 */ |
| 2423 static void open_db(ShellState *p, int keepAlive){ |
| 2424 if( p->db==0 ){ |
| 2425 sqlite3_initialize(); |
| 2426 sqlite3_open(p->zDbFilename, &p->db); |
| 2427 globalDb = p->db; |
| 2428 if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){ |
| 2429 sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0, |
| 2430 shellstaticFunc, 0, 0); |
| 2431 } |
| 2432 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 2433 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
| 2434 p->zDbFilename, sqlite3_errmsg(p->db)); |
| 2435 if( keepAlive ) return; |
| 2436 exit(1); |
| 2437 } |
| 2438 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2439 sqlite3_enable_load_extension(p->db, 1); |
| 2440 #endif |
| 2441 sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
| 2442 readfileFunc, 0, 0); |
| 2443 sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
| 2444 writefileFunc, 0, 0); |
| 2445 } |
| 2446 } |
| 2447 |
| 2448 /* |
| 2449 ** Do C-language style dequoting. |
| 2450 ** |
| 2451 ** \a -> alarm |
| 2452 ** \b -> backspace |
| 2453 ** \t -> tab |
| 2454 ** \n -> newline |
| 2455 ** \v -> vertical tab |
| 2456 ** \f -> form feed |
| 2457 ** \r -> carriage return |
| 2458 ** \s -> space |
| 2459 ** \" -> " |
| 2460 ** \' -> ' |
| 2461 ** \\ -> backslash |
| 2462 ** \NNN -> ascii character NNN in octal |
| 2463 */ |
| 2464 static void resolve_backslashes(char *z){ |
| 2465 int i, j; |
| 2466 char c; |
| 2467 while( *z && *z!='\\' ) z++; |
| 2468 for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 2469 if( c=='\\' && z[i+1]!=0 ){ |
| 2470 c = z[++i]; |
| 2471 if( c=='a' ){ |
| 2472 c = '\a'; |
| 2473 }else if( c=='b' ){ |
| 2474 c = '\b'; |
| 2475 }else if( c=='t' ){ |
| 2476 c = '\t'; |
| 2477 }else if( c=='n' ){ |
| 2478 c = '\n'; |
| 2479 }else if( c=='v' ){ |
| 2480 c = '\v'; |
| 2481 }else if( c=='f' ){ |
| 2482 c = '\f'; |
| 2483 }else if( c=='r' ){ |
| 2484 c = '\r'; |
| 2485 }else if( c=='"' ){ |
| 2486 c = '"'; |
| 2487 }else if( c=='\'' ){ |
| 2488 c = '\''; |
| 2489 }else if( c=='\\' ){ |
| 2490 c = '\\'; |
| 2491 }else if( c>='0' && c<='7' ){ |
| 2492 c -= '0'; |
| 2493 if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2494 i++; |
| 2495 c = (c<<3) + z[i] - '0'; |
| 2496 if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2497 i++; |
| 2498 c = (c<<3) + z[i] - '0'; |
| 2499 } |
| 2500 } |
| 2501 } |
| 2502 } |
| 2503 z[j] = c; |
| 2504 } |
| 2505 if( j<i ) z[j] = 0; |
| 2506 } |
| 2507 |
| 2508 /* |
| 2509 ** Return the value of a hexadecimal digit. Return -1 if the input |
| 2510 ** is not a hex digit. |
| 2511 */ |
| 2512 static int hexDigitValue(char c){ |
| 2513 if( c>='0' && c<='9' ) return c - '0'; |
| 2514 if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 2515 if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 2516 return -1; |
| 2517 } |
| 2518 |
| 2519 /* |
| 2520 ** Interpret zArg as an integer value, possibly with suffixes. |
| 2521 */ |
| 2522 static sqlite3_int64 integerValue(const char *zArg){ |
| 2523 sqlite3_int64 v = 0; |
| 2524 static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 2525 { "KiB", 1024 }, |
| 2526 { "MiB", 1024*1024 }, |
| 2527 { "GiB", 1024*1024*1024 }, |
| 2528 { "KB", 1000 }, |
| 2529 { "MB", 1000000 }, |
| 2530 { "GB", 1000000000 }, |
| 2531 { "K", 1000 }, |
| 2532 { "M", 1000000 }, |
| 2533 { "G", 1000000000 }, |
| 2534 }; |
| 2535 int i; |
| 2536 int isNeg = 0; |
| 2537 if( zArg[0]=='-' ){ |
| 2538 isNeg = 1; |
| 2539 zArg++; |
| 2540 }else if( zArg[0]=='+' ){ |
| 2541 zArg++; |
| 2542 } |
| 2543 if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 2544 int x; |
| 2545 zArg += 2; |
| 2546 while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 2547 v = (v<<4) + x; |
| 2548 zArg++; |
| 2549 } |
| 2550 }else{ |
| 2551 while( IsDigit(zArg[0]) ){ |
| 2552 v = v*10 + zArg[0] - '0'; |
| 2553 zArg++; |
| 2554 } |
| 2555 } |
| 2556 for(i=0; i<ArraySize(aMult); i++){ |
| 2557 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 2558 v *= aMult[i].iMult; |
| 2559 break; |
| 2560 } |
| 2561 } |
| 2562 return isNeg? -v : v; |
| 2563 } |
| 2564 |
| 2565 /* |
| 2566 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 2567 ** for TRUE and FALSE. Return the integer value if appropriate. |
| 2568 */ |
| 2569 static int booleanValue(char *zArg){ |
| 2570 int i; |
| 2571 if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 2572 for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 2573 }else{ |
| 2574 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 2575 } |
| 2576 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 2577 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 2578 return 1; |
| 2579 } |
| 2580 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 2581 return 0; |
| 2582 } |
| 2583 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 2584 zArg); |
| 2585 return 0; |
| 2586 } |
| 2587 |
| 2588 /* |
| 2589 ** Close an output file, assuming it is not stderr or stdout |
| 2590 */ |
| 2591 static void output_file_close(FILE *f){ |
| 2592 if( f && f!=stdout && f!=stderr ) fclose(f); |
| 2593 } |
| 2594 |
| 2595 /* |
| 2596 ** Try to open an output file. The names "stdout" and "stderr" are |
| 2597 ** recognized and do the right thing. NULL is returned if the output |
| 2598 ** filename is "off". |
| 2599 */ |
| 2600 static FILE *output_file_open(const char *zFile){ |
| 2601 FILE *f; |
| 2602 if( strcmp(zFile,"stdout")==0 ){ |
| 2603 f = stdout; |
| 2604 }else if( strcmp(zFile, "stderr")==0 ){ |
| 2605 f = stderr; |
| 2606 }else if( strcmp(zFile, "off")==0 ){ |
| 2607 f = 0; |
| 2608 }else{ |
| 2609 f = fopen(zFile, "wb"); |
| 2610 if( f==0 ){ |
| 2611 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 2612 } |
| 2613 } |
| 2614 return f; |
| 2615 } |
| 2616 |
| 2617 #if !defined(SQLITE_UNTESTABLE) |
| 2618 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 2619 /* |
| 2620 ** A routine for handling output from sqlite3_trace(). |
| 2621 */ |
| 2622 static int sql_trace_callback( |
| 2623 unsigned mType, |
| 2624 void *pArg, |
| 2625 void *pP, |
| 2626 void *pX |
| 2627 ){ |
| 2628 FILE *f = (FILE*)pArg; |
| 2629 UNUSED_PARAMETER(mType); |
| 2630 UNUSED_PARAMETER(pP); |
| 2631 if( f ){ |
| 2632 const char *z = (const char*)pX; |
| 2633 int i = (int)strlen(z); |
| 2634 while( i>0 && z[i-1]==';' ){ i--; } |
| 2635 utf8_printf(f, "%.*s;\n", i, z); |
| 2636 } |
| 2637 return 0; |
| 2638 } |
| 2639 #endif |
| 2640 #endif |
| 2641 |
| 2642 /* |
| 2643 ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 2644 ** a useful spot to set a debugger breakpoint. |
| 2645 */ |
| 2646 static void test_breakpoint(void){ |
| 2647 static int nCall = 0; |
| 2648 nCall++; |
| 2649 } |
| 2650 |
| 2651 /* |
| 2652 ** An object used to read a CSV and other files for import. |
| 2653 */ |
| 2654 typedef struct ImportCtx ImportCtx; |
| 2655 struct ImportCtx { |
| 2656 const char *zFile; /* Name of the input file */ |
| 2657 FILE *in; /* Read the CSV text from this input stream */ |
| 2658 char *z; /* Accumulated text for a field */ |
| 2659 int n; /* Number of bytes in z */ |
| 2660 int nAlloc; /* Space allocated for z[] */ |
| 2661 int nLine; /* Current line number */ |
| 2662 int cTerm; /* Character that terminated the most recent field */ |
| 2663 int cColSep; /* The column separator character. (Usually ",") */ |
| 2664 int cRowSep; /* The row separator character. (Usually "\n") */ |
| 2665 }; |
| 2666 |
| 2667 /* Append a single byte to z[] */ |
| 2668 static void import_append_char(ImportCtx *p, int c){ |
| 2669 if( p->n+1>=p->nAlloc ){ |
| 2670 p->nAlloc += p->nAlloc + 100; |
| 2671 p->z = sqlite3_realloc64(p->z, p->nAlloc); |
| 2672 if( p->z==0 ){ |
| 2673 raw_printf(stderr, "out of memory\n"); |
| 2674 exit(1); |
| 2675 } |
| 2676 } |
| 2677 p->z[p->n++] = (char)c; |
| 2678 } |
| 2679 |
| 2680 /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 2681 ** with the option of having a separator other than ",". |
| 2682 ** |
| 2683 ** + Input comes from p->in. |
| 2684 ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 2685 ** from sqlite3_malloc64(). |
| 2686 ** + Use p->cSep as the column separator. The default is ",". |
| 2687 ** + Use p->rSep as the row separator. The default is "\n". |
| 2688 ** + Keep track of the line number in p->nLine. |
| 2689 ** + Store the character that terminates the field in p->cTerm. Store |
| 2690 ** EOF on end-of-file. |
| 2691 ** + Report syntax errors on stderr |
| 2692 */ |
| 2693 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 2694 int c; |
| 2695 int cSep = p->cColSep; |
| 2696 int rSep = p->cRowSep; |
| 2697 p->n = 0; |
| 2698 c = fgetc(p->in); |
| 2699 if( c==EOF || seenInterrupt ){ |
| 2700 p->cTerm = EOF; |
| 2701 return 0; |
| 2702 } |
| 2703 if( c=='"' ){ |
| 2704 int pc, ppc; |
| 2705 int startLine = p->nLine; |
| 2706 int cQuote = c; |
| 2707 pc = ppc = 0; |
| 2708 while( 1 ){ |
| 2709 c = fgetc(p->in); |
| 2710 if( c==rSep ) p->nLine++; |
| 2711 if( c==cQuote ){ |
| 2712 if( pc==cQuote ){ |
| 2713 pc = 0; |
| 2714 continue; |
| 2715 } |
| 2716 } |
| 2717 if( (c==cSep && pc==cQuote) |
| 2718 || (c==rSep && pc==cQuote) |
| 2719 || (c==rSep && pc=='\r' && ppc==cQuote) |
| 2720 || (c==EOF && pc==cQuote) |
| 2721 ){ |
| 2722 do{ p->n--; }while( p->z[p->n]!=cQuote ); |
| 2723 p->cTerm = c; |
| 2724 break; |
| 2725 } |
| 2726 if( pc==cQuote && c!='\r' ){ |
| 2727 utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
| 2728 p->zFile, p->nLine, cQuote); |
| 2729 } |
| 2730 if( c==EOF ){ |
| 2731 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
| 2732 p->zFile, startLine, cQuote); |
| 2733 p->cTerm = c; |
| 2734 break; |
| 2735 } |
| 2736 import_append_char(p, c); |
| 2737 ppc = pc; |
| 2738 pc = c; |
| 2739 } |
| 2740 }else{ |
| 2741 while( c!=EOF && c!=cSep && c!=rSep ){ |
| 2742 import_append_char(p, c); |
| 2743 c = fgetc(p->in); |
| 2744 } |
| 2745 if( c==rSep ){ |
| 2746 p->nLine++; |
| 2747 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 2748 } |
| 2749 p->cTerm = c; |
| 2750 } |
| 2751 if( p->z ) p->z[p->n] = 0; |
| 2752 return p->z; |
| 2753 } |
| 2754 |
| 2755 /* Read a single field of ASCII delimited text. |
| 2756 ** |
| 2757 ** + Input comes from p->in. |
| 2758 ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 2759 ** from sqlite3_malloc64(). |
| 2760 ** + Use p->cSep as the column separator. The default is "\x1F". |
| 2761 ** + Use p->rSep as the row separator. The default is "\x1E". |
| 2762 ** + Keep track of the row number in p->nLine. |
| 2763 ** + Store the character that terminates the field in p->cTerm. Store |
| 2764 ** EOF on end-of-file. |
| 2765 ** + Report syntax errors on stderr |
| 2766 */ |
| 2767 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 2768 int c; |
| 2769 int cSep = p->cColSep; |
| 2770 int rSep = p->cRowSep; |
| 2771 p->n = 0; |
| 2772 c = fgetc(p->in); |
| 2773 if( c==EOF || seenInterrupt ){ |
| 2774 p->cTerm = EOF; |
| 2775 return 0; |
| 2776 } |
| 2777 while( c!=EOF && c!=cSep && c!=rSep ){ |
| 2778 import_append_char(p, c); |
| 2779 c = fgetc(p->in); |
| 2780 } |
| 2781 if( c==rSep ){ |
| 2782 p->nLine++; |
| 2783 } |
| 2784 p->cTerm = c; |
| 2785 if( p->z ) p->z[p->n] = 0; |
| 2786 return p->z; |
| 2787 } |
| 2788 |
| 2789 /* |
| 2790 ** Try to transfer data for table zTable. If an error is seen while |
| 2791 ** moving forward, try to go backwards. The backwards movement won't |
| 2792 ** work for WITHOUT ROWID tables. |
| 2793 */ |
| 2794 static void tryToCloneData( |
| 2795 ShellState *p, |
| 2796 sqlite3 *newDb, |
| 2797 const char *zTable |
| 2798 ){ |
| 2799 sqlite3_stmt *pQuery = 0; |
| 2800 sqlite3_stmt *pInsert = 0; |
| 2801 char *zQuery = 0; |
| 2802 char *zInsert = 0; |
| 2803 int rc; |
| 2804 int i, j, n; |
| 2805 int nTable = (int)strlen(zTable); |
| 2806 int k = 0; |
| 2807 int cnt = 0; |
| 2808 const int spinRate = 10000; |
| 2809 |
| 2810 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 2811 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2812 if( rc ){ |
| 2813 utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 2814 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2815 zQuery); |
| 2816 goto end_data_xfer; |
| 2817 } |
| 2818 n = sqlite3_column_count(pQuery); |
| 2819 zInsert = sqlite3_malloc64(200 + nTable + n*3); |
| 2820 if( zInsert==0 ){ |
| 2821 raw_printf(stderr, "out of memory\n"); |
| 2822 goto end_data_xfer; |
| 2823 } |
| 2824 sqlite3_snprintf(200+nTable,zInsert, |
| 2825 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 2826 i = (int)strlen(zInsert); |
| 2827 for(j=1; j<n; j++){ |
| 2828 memcpy(zInsert+i, ",?", 2); |
| 2829 i += 2; |
| 2830 } |
| 2831 memcpy(zInsert+i, ");", 3); |
| 2832 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 2833 if( rc ){ |
| 2834 utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 2835 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 2836 zQuery); |
| 2837 goto end_data_xfer; |
| 2838 } |
| 2839 for(k=0; k<2; k++){ |
| 2840 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2841 for(i=0; i<n; i++){ |
| 2842 switch( sqlite3_column_type(pQuery, i) ){ |
| 2843 case SQLITE_NULL: { |
| 2844 sqlite3_bind_null(pInsert, i+1); |
| 2845 break; |
| 2846 } |
| 2847 case SQLITE_INTEGER: { |
| 2848 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 2849 break; |
| 2850 } |
| 2851 case SQLITE_FLOAT: { |
| 2852 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 2853 break; |
| 2854 } |
| 2855 case SQLITE_TEXT: { |
| 2856 sqlite3_bind_text(pInsert, i+1, |
| 2857 (const char*)sqlite3_column_text(pQuery,i), |
| 2858 -1, SQLITE_STATIC); |
| 2859 break; |
| 2860 } |
| 2861 case SQLITE_BLOB: { |
| 2862 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 2863 sqlite3_column_bytes(pQuery,i), |
| 2864 SQLITE_STATIC); |
| 2865 break; |
| 2866 } |
| 2867 } |
| 2868 } /* End for */ |
| 2869 rc = sqlite3_step(pInsert); |
| 2870 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 2871 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
| 2872 sqlite3_errmsg(newDb)); |
| 2873 } |
| 2874 sqlite3_reset(pInsert); |
| 2875 cnt++; |
| 2876 if( (cnt%spinRate)==0 ){ |
| 2877 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 2878 fflush(stdout); |
| 2879 } |
| 2880 } /* End while */ |
| 2881 if( rc==SQLITE_DONE ) break; |
| 2882 sqlite3_finalize(pQuery); |
| 2883 sqlite3_free(zQuery); |
| 2884 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 2885 zTable); |
| 2886 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2887 if( rc ){ |
| 2888 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
| 2889 break; |
| 2890 } |
| 2891 } /* End for(k=0...) */ |
| 2892 |
| 2893 end_data_xfer: |
| 2894 sqlite3_finalize(pQuery); |
| 2895 sqlite3_finalize(pInsert); |
| 2896 sqlite3_free(zQuery); |
| 2897 sqlite3_free(zInsert); |
| 2898 } |
| 2899 |
| 2900 |
| 2901 /* |
| 2902 ** Try to transfer all rows of the schema that match zWhere. For |
| 2903 ** each row, invoke xForEach() on the object defined by that row. |
| 2904 ** If an error is encountered while moving forward through the |
| 2905 ** sqlite_master table, try again moving backwards. |
| 2906 */ |
| 2907 static void tryToCloneSchema( |
| 2908 ShellState *p, |
| 2909 sqlite3 *newDb, |
| 2910 const char *zWhere, |
| 2911 void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 2912 ){ |
| 2913 sqlite3_stmt *pQuery = 0; |
| 2914 char *zQuery = 0; |
| 2915 int rc; |
| 2916 const unsigned char *zName; |
| 2917 const unsigned char *zSql; |
| 2918 char *zErrMsg = 0; |
| 2919 |
| 2920 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 2921 " WHERE %s", zWhere); |
| 2922 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2923 if( rc ){ |
| 2924 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 2925 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2926 zQuery); |
| 2927 goto end_schema_xfer; |
| 2928 } |
| 2929 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2930 zName = sqlite3_column_text(pQuery, 0); |
| 2931 zSql = sqlite3_column_text(pQuery, 1); |
| 2932 printf("%s... ", zName); fflush(stdout); |
| 2933 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 2934 if( zErrMsg ){ |
| 2935 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 2936 sqlite3_free(zErrMsg); |
| 2937 zErrMsg = 0; |
| 2938 } |
| 2939 if( xForEach ){ |
| 2940 xForEach(p, newDb, (const char*)zName); |
| 2941 } |
| 2942 printf("done\n"); |
| 2943 } |
| 2944 if( rc!=SQLITE_DONE ){ |
| 2945 sqlite3_finalize(pQuery); |
| 2946 sqlite3_free(zQuery); |
| 2947 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 2948 " WHERE %s ORDER BY rowid DESC", zWhere); |
| 2949 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2950 if( rc ){ |
| 2951 utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 2952 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2953 zQuery); |
| 2954 goto end_schema_xfer; |
| 2955 } |
| 2956 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2957 zName = sqlite3_column_text(pQuery, 0); |
| 2958 zSql = sqlite3_column_text(pQuery, 1); |
| 2959 printf("%s... ", zName); fflush(stdout); |
| 2960 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 2961 if( zErrMsg ){ |
| 2962 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 2963 sqlite3_free(zErrMsg); |
| 2964 zErrMsg = 0; |
| 2965 } |
| 2966 if( xForEach ){ |
| 2967 xForEach(p, newDb, (const char*)zName); |
| 2968 } |
| 2969 printf("done\n"); |
| 2970 } |
| 2971 } |
| 2972 end_schema_xfer: |
| 2973 sqlite3_finalize(pQuery); |
| 2974 sqlite3_free(zQuery); |
| 2975 } |
| 2976 |
| 2977 /* |
| 2978 ** Open a new database file named "zNewDb". Try to recover as much information |
| 2979 ** as possible out of the main database (which might be corrupt) and write it |
| 2980 ** into zNewDb. |
| 2981 */ |
| 2982 static void tryToClone(ShellState *p, const char *zNewDb){ |
| 2983 int rc; |
| 2984 sqlite3 *newDb = 0; |
| 2985 if( access(zNewDb,0)==0 ){ |
| 2986 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
| 2987 return; |
| 2988 } |
| 2989 rc = sqlite3_open(zNewDb, &newDb); |
| 2990 if( rc ){ |
| 2991 utf8_printf(stderr, "Cannot create output database: %s\n", |
| 2992 sqlite3_errmsg(newDb)); |
| 2993 }else{ |
| 2994 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
| 2995 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
| 2996 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 2997 tryToCloneSchema(p, newDb, "type!='table'", 0); |
| 2998 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
| 2999 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 3000 } |
| 3001 sqlite3_close(newDb); |
| 3002 } |
| 3003 |
| 3004 /* |
| 3005 ** Change the output file back to stdout |
| 3006 */ |
| 3007 static void output_reset(ShellState *p){ |
| 3008 if( p->outfile[0]=='|' ){ |
| 3009 #ifndef SQLITE_OMIT_POPEN |
| 3010 pclose(p->out); |
| 3011 #endif |
| 3012 }else{ |
| 3013 output_file_close(p->out); |
| 3014 } |
| 3015 p->outfile[0] = 0; |
| 3016 p->out = stdout; |
| 3017 } |
| 3018 |
| 3019 /* |
| 3020 ** Run an SQL command and return the single integer result. |
| 3021 */ |
| 3022 static int db_int(ShellState *p, const char *zSql){ |
| 3023 sqlite3_stmt *pStmt; |
| 3024 int res = 0; |
| 3025 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3026 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3027 res = sqlite3_column_int(pStmt,0); |
| 3028 } |
| 3029 sqlite3_finalize(pStmt); |
| 3030 return res; |
| 3031 } |
| 3032 |
| 3033 /* |
| 3034 ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 3035 */ |
| 3036 static unsigned int get2byteInt(unsigned char *a){ |
| 3037 return (a[0]<<8) + a[1]; |
| 3038 } |
| 3039 static unsigned int get4byteInt(unsigned char *a){ |
| 3040 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 3041 } |
| 3042 |
| 3043 /* |
| 3044 ** Implementation of the ".info" command. |
| 3045 ** |
| 3046 ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 3047 */ |
| 3048 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 3049 static const struct { const char *zName; int ofst; } aField[] = { |
| 3050 { "file change counter:", 24 }, |
| 3051 { "database page count:", 28 }, |
| 3052 { "freelist page count:", 36 }, |
| 3053 { "schema cookie:", 40 }, |
| 3054 { "schema format:", 44 }, |
| 3055 { "default cache size:", 48 }, |
| 3056 { "autovacuum top root:", 52 }, |
| 3057 { "incremental vacuum:", 64 }, |
| 3058 { "text encoding:", 56 }, |
| 3059 { "user version:", 60 }, |
| 3060 { "application id:", 68 }, |
| 3061 { "software version:", 96 }, |
| 3062 }; |
| 3063 static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 3064 { "number of tables:", |
| 3065 "SELECT count(*) FROM %s WHERE type='table'" }, |
| 3066 { "number of indexes:", |
| 3067 "SELECT count(*) FROM %s WHERE type='index'" }, |
| 3068 { "number of triggers:", |
| 3069 "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 3070 { "number of views:", |
| 3071 "SELECT count(*) FROM %s WHERE type='view'" }, |
| 3072 { "schema size:", |
| 3073 "SELECT total(length(sql)) FROM %s" }, |
| 3074 }; |
| 3075 sqlite3_file *pFile = 0; |
| 3076 int i; |
| 3077 char *zSchemaTab; |
| 3078 char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 3079 unsigned char aHdr[100]; |
| 3080 open_db(p, 0); |
| 3081 if( p->db==0 ) return 1; |
| 3082 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
| 3083 if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 3084 return 1; |
| 3085 } |
| 3086 i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 3087 if( i!=SQLITE_OK ){ |
| 3088 raw_printf(stderr, "unable to read database header\n"); |
| 3089 return 1; |
| 3090 } |
| 3091 i = get2byteInt(aHdr+16); |
| 3092 if( i==1 ) i = 65536; |
| 3093 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 3094 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 3095 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 3096 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
| 3097 for(i=0; i<ArraySize(aField); i++){ |
| 3098 int ofst = aField[i].ofst; |
| 3099 unsigned int val = get4byteInt(aHdr + ofst); |
| 3100 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
| 3101 switch( ofst ){ |
| 3102 case 56: { |
| 3103 if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 3104 if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 3105 if( val==3 ) raw_printf(p->out, " (utf16be)"); |
| 3106 } |
| 3107 } |
| 3108 raw_printf(p->out, "\n"); |
| 3109 } |
| 3110 if( zDb==0 ){ |
| 3111 zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 3112 }else if( strcmp(zDb,"temp")==0 ){ |
| 3113 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 3114 }else{ |
| 3115 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 3116 } |
| 3117 for(i=0; i<ArraySize(aQuery); i++){ |
| 3118 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 3119 int val = db_int(p, zSql); |
| 3120 sqlite3_free(zSql); |
| 3121 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
| 3122 } |
| 3123 sqlite3_free(zSchemaTab); |
| 3124 return 0; |
| 3125 } |
| 3126 |
| 3127 /* |
| 3128 ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 3129 */ |
| 3130 static int shellDatabaseError(sqlite3 *db){ |
| 3131 const char *zErr = sqlite3_errmsg(db); |
| 3132 utf8_printf(stderr, "Error: %s\n", zErr); |
| 3133 return 1; |
| 3134 } |
| 3135 |
| 3136 /* |
| 3137 ** Print an out-of-memory message to stderr and return 1. |
| 3138 */ |
| 3139 static int shellNomemError(void){ |
| 3140 raw_printf(stderr, "Error: out of memory\n"); |
| 3141 return 1; |
| 3142 } |
| 3143 |
| 3144 /* |
| 3145 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 3146 ** if they match and FALSE (0) if they do not match. |
| 3147 ** |
| 3148 ** Globbing rules: |
| 3149 ** |
| 3150 ** '*' Matches any sequence of zero or more characters. |
| 3151 ** |
| 3152 ** '?' Matches exactly one character. |
| 3153 ** |
| 3154 ** [...] Matches one character from the enclosed list of |
| 3155 ** characters. |
| 3156 ** |
| 3157 ** [^...] Matches one character not in the enclosed list. |
| 3158 ** |
| 3159 ** '#' Matches any sequence of one or more digits with an |
| 3160 ** optional + or - sign in front |
| 3161 ** |
| 3162 ** ' ' Any span of whitespace matches any other span of |
| 3163 ** whitespace. |
| 3164 ** |
| 3165 ** Extra whitespace at the end of z[] is ignored. |
| 3166 */ |
| 3167 static int testcase_glob(const char *zGlob, const char *z){ |
| 3168 int c, c2; |
| 3169 int invert; |
| 3170 int seen; |
| 3171 |
| 3172 while( (c = (*(zGlob++)))!=0 ){ |
| 3173 if( IsSpace(c) ){ |
| 3174 if( !IsSpace(*z) ) return 0; |
| 3175 while( IsSpace(*zGlob) ) zGlob++; |
| 3176 while( IsSpace(*z) ) z++; |
| 3177 }else if( c=='*' ){ |
| 3178 while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 3179 if( c=='?' && (*(z++))==0 ) return 0; |
| 3180 } |
| 3181 if( c==0 ){ |
| 3182 return 1; |
| 3183 }else if( c=='[' ){ |
| 3184 while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 3185 z++; |
| 3186 } |
| 3187 return (*z)!=0; |
| 3188 } |
| 3189 while( (c2 = (*(z++)))!=0 ){ |
| 3190 while( c2!=c ){ |
| 3191 c2 = *(z++); |
| 3192 if( c2==0 ) return 0; |
| 3193 } |
| 3194 if( testcase_glob(zGlob,z) ) return 1; |
| 3195 } |
| 3196 return 0; |
| 3197 }else if( c=='?' ){ |
| 3198 if( (*(z++))==0 ) return 0; |
| 3199 }else if( c=='[' ){ |
| 3200 int prior_c = 0; |
| 3201 seen = 0; |
| 3202 invert = 0; |
| 3203 c = *(z++); |
| 3204 if( c==0 ) return 0; |
| 3205 c2 = *(zGlob++); |
| 3206 if( c2=='^' ){ |
| 3207 invert = 1; |
| 3208 c2 = *(zGlob++); |
| 3209 } |
| 3210 if( c2==']' ){ |
| 3211 if( c==']' ) seen = 1; |
| 3212 c2 = *(zGlob++); |
| 3213 } |
| 3214 while( c2 && c2!=']' ){ |
| 3215 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 3216 c2 = *(zGlob++); |
| 3217 if( c>=prior_c && c<=c2 ) seen = 1; |
| 3218 prior_c = 0; |
| 3219 }else{ |
| 3220 if( c==c2 ){ |
| 3221 seen = 1; |
| 3222 } |
| 3223 prior_c = c2; |
| 3224 } |
| 3225 c2 = *(zGlob++); |
| 3226 } |
| 3227 if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 3228 }else if( c=='#' ){ |
| 3229 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 3230 if( !IsDigit(z[0]) ) return 0; |
| 3231 z++; |
| 3232 while( IsDigit(z[0]) ){ z++; } |
| 3233 }else{ |
| 3234 if( c!=(*(z++)) ) return 0; |
| 3235 } |
| 3236 } |
| 3237 while( IsSpace(*z) ){ z++; } |
| 3238 return *z==0; |
| 3239 } |
| 3240 |
| 3241 |
| 3242 /* |
| 3243 ** Compare the string as a command-line option with either one or two |
| 3244 ** initial "-" characters. |
| 3245 */ |
| 3246 static int optionMatch(const char *zStr, const char *zOpt){ |
| 3247 if( zStr[0]!='-' ) return 0; |
| 3248 zStr++; |
| 3249 if( zStr[0]=='-' ) zStr++; |
| 3250 return strcmp(zStr, zOpt)==0; |
| 3251 } |
| 3252 |
| 3253 /* |
| 3254 ** Delete a file. |
| 3255 */ |
| 3256 int shellDeleteFile(const char *zFilename){ |
| 3257 int rc; |
| 3258 #ifdef _WIN32 |
| 3259 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
| 3260 rc = _wunlink(z); |
| 3261 sqlite3_free(z); |
| 3262 #else |
| 3263 rc = unlink(zFilename); |
| 3264 #endif |
| 3265 return rc; |
| 3266 } |
| 3267 |
| 3268 |
| 3269 /* |
| 3270 ** The implementation of SQL scalar function fkey_collate_clause(), used |
| 3271 ** by the ".lint fkey-indexes" command. This scalar function is always |
| 3272 ** called with four arguments - the parent table name, the parent column name, |
| 3273 ** the child table name and the child column name. |
| 3274 ** |
| 3275 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 3276 ** |
| 3277 ** If either of the named tables or columns do not exist, this function |
| 3278 ** returns an empty string. An empty string is also returned if both tables |
| 3279 ** and columns exist but have the same default collation sequence. Or, |
| 3280 ** if both exist but the default collation sequences are different, this |
| 3281 ** function returns the string " COLLATE <parent-collation>", where |
| 3282 ** <parent-collation> is the default collation sequence of the parent column. |
| 3283 */ |
| 3284 static void shellFkeyCollateClause( |
| 3285 sqlite3_context *pCtx, |
| 3286 int nVal, |
| 3287 sqlite3_value **apVal |
| 3288 ){ |
| 3289 sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 3290 const char *zParent; |
| 3291 const char *zParentCol; |
| 3292 const char *zParentSeq; |
| 3293 const char *zChild; |
| 3294 const char *zChildCol; |
| 3295 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
| 3296 int rc; |
| 3297 |
| 3298 assert( nVal==4 ); |
| 3299 zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 3300 zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 3301 zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 3302 zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 3303 |
| 3304 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 3305 rc = sqlite3_table_column_metadata( |
| 3306 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 3307 ); |
| 3308 if( rc==SQLITE_OK ){ |
| 3309 rc = sqlite3_table_column_metadata( |
| 3310 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 3311 ); |
| 3312 } |
| 3313 |
| 3314 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 3315 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 3316 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 3317 sqlite3_free(z); |
| 3318 } |
| 3319 } |
| 3320 |
| 3321 |
| 3322 /* |
| 3323 ** The implementation of dot-command ".lint fkey-indexes". |
| 3324 */ |
| 3325 static int lintFkeyIndexes( |
| 3326 ShellState *pState, /* Current shell tool state */ |
| 3327 char **azArg, /* Array of arguments passed to dot command */ |
| 3328 int nArg /* Number of entries in azArg[] */ |
| 3329 ){ |
| 3330 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 3331 FILE *out = pState->out; /* Stream to write non-error output to */ |
| 3332 int bVerbose = 0; /* If -verbose is present */ |
| 3333 int bGroupByParent = 0; /* If -groupbyparent is present */ |
| 3334 int i; /* To iterate through azArg[] */ |
| 3335 const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 3336 int rc; /* Return code */ |
| 3337 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
| 3338 |
| 3339 /* |
| 3340 ** This SELECT statement returns one row for each foreign key constraint |
| 3341 ** in the schema of the main database. The column values are: |
| 3342 ** |
| 3343 ** 0. The text of an SQL statement similar to: |
| 3344 ** |
| 3345 ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 3346 ** |
| 3347 ** This is the same SELECT that the foreign keys implementation needs |
| 3348 ** to run internally on child tables. If there is an index that can |
| 3349 ** be used to optimize this query, then it can also be used by the FK |
| 3350 ** implementation to optimize DELETE or UPDATE statements on the parent |
| 3351 ** table. |
| 3352 ** |
| 3353 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 3354 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 3355 ** contains an index that can be used to optimize the query. |
| 3356 ** |
| 3357 ** 2. Human readable text that describes the child table and columns. e.g. |
| 3358 ** |
| 3359 ** "child_table(child_key1, child_key2)" |
| 3360 ** |
| 3361 ** 3. Human readable text that describes the parent table and columns. e.g. |
| 3362 ** |
| 3363 ** "parent_table(parent_key1, parent_key2)" |
| 3364 ** |
| 3365 ** 4. A full CREATE INDEX statement for an index that could be used to |
| 3366 ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 3367 ** |
| 3368 ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 3369 ** |
| 3370 ** 5. The name of the parent table. |
| 3371 ** |
| 3372 ** These six values are used by the C logic below to generate the report. |
| 3373 */ |
| 3374 const char *zSql = |
| 3375 "SELECT " |
| 3376 " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 3377 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 3378 " || fkey_collate_clause(f.[table], f.[to], s.name, f.[from]),' AND ')" |
| 3379 ", " |
| 3380 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
| 3381 " || group_concat('*=?', ' AND ') || ')'" |
| 3382 ", " |
| 3383 " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
| 3384 ", " |
| 3385 " f.[table] || '(' || group_concat(COALESCE(f.[to], " |
| 3386 " (SELECT name FROM pragma_table_info(f.[table]) WHERE pk=seq+1)" |
| 3387 " )) || ')'" |
| 3388 ", " |
| 3389 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 3390 " || ' ON ' || quote(s.name) || '('" |
| 3391 " || group_concat(quote(f.[from]) ||" |
| 3392 " fkey_collate_clause(f.[table], f.[to], s.name, f.[from]), ', ')" |
| 3393 " || ');'" |
| 3394 ", " |
| 3395 " f.[table] " |
| 3396 |
| 3397 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
| 3398 "GROUP BY s.name, f.id " |
| 3399 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
| 3400 ; |
| 3401 |
| 3402 for(i=2; i<nArg; i++){ |
| 3403 int n = (int)strlen(azArg[i]); |
| 3404 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 3405 bVerbose = 1; |
| 3406 } |
| 3407 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 3408 bGroupByParent = 1; |
| 3409 zIndent = " "; |
| 3410 } |
| 3411 else{ |
| 3412 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 3413 azArg[0], azArg[1] |
| 3414 ); |
| 3415 return SQLITE_ERROR; |
| 3416 } |
| 3417 } |
| 3418 |
| 3419 /* Register the fkey_collate_clause() SQL function */ |
| 3420 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 3421 0, shellFkeyCollateClause, 0, 0 |
| 3422 ); |
| 3423 |
| 3424 |
| 3425 if( rc==SQLITE_OK ){ |
| 3426 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 3427 } |
| 3428 if( rc==SQLITE_OK ){ |
| 3429 sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 3430 } |
| 3431 |
| 3432 if( rc==SQLITE_OK ){ |
| 3433 int rc2; |
| 3434 char *zPrev = 0; |
| 3435 while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 3436 int res = -1; |
| 3437 sqlite3_stmt *pExplain = 0; |
| 3438 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 3439 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 3440 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 3441 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 3442 const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
| 3443 const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
| 3444 |
| 3445 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3446 if( rc!=SQLITE_OK ) break; |
| 3447 if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 3448 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
| 3449 res = (0==sqlite3_strglob(zGlob, zPlan)); |
| 3450 } |
| 3451 rc = sqlite3_finalize(pExplain); |
| 3452 if( rc!=SQLITE_OK ) break; |
| 3453 |
| 3454 if( res<0 ){ |
| 3455 raw_printf(stderr, "Error: internal error"); |
| 3456 break; |
| 3457 }else{ |
| 3458 if( bGroupByParent |
| 3459 && (bVerbose || res==0) |
| 3460 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 3461 ){ |
| 3462 raw_printf(out, "-- Parent table %s\n", zParent); |
| 3463 sqlite3_free(zPrev); |
| 3464 zPrev = sqlite3_mprintf("%s", zParent); |
| 3465 } |
| 3466 |
| 3467 if( res==0 ){ |
| 3468 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 3469 }else if( bVerbose ){ |
| 3470 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 3471 zIndent, zFrom, zTarget |
| 3472 ); |
| 3473 } |
| 3474 } |
| 3475 } |
| 3476 sqlite3_free(zPrev); |
| 3477 |
| 3478 if( rc!=SQLITE_OK ){ |
| 3479 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 3480 } |
| 3481 |
| 3482 rc2 = sqlite3_finalize(pSql); |
| 3483 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 3484 rc = rc2; |
| 3485 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 3486 } |
| 3487 }else{ |
| 3488 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 3489 } |
| 3490 |
| 3491 return rc; |
| 3492 } |
| 3493 |
| 3494 /* |
| 3495 ** Implementation of ".lint" dot command. |
| 3496 */ |
| 3497 static int lintDotCommand( |
| 3498 ShellState *pState, /* Current shell tool state */ |
| 3499 char **azArg, /* Array of arguments passed to dot command */ |
| 3500 int nArg /* Number of entries in azArg[] */ |
| 3501 ){ |
| 3502 int n; |
| 3503 n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
| 3504 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 3505 return lintFkeyIndexes(pState, azArg, nArg); |
| 3506 |
| 3507 usage: |
| 3508 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 3509 raw_printf(stderr, "Where sub-commands are:\n"); |
| 3510 raw_printf(stderr, " fkey-indexes\n"); |
| 3511 return SQLITE_ERROR; |
| 3512 } |
| 3513 |
| 3514 |
| 3515 /* |
| 3516 ** If an input line begins with "." then invoke this routine to |
| 3517 ** process that line. |
| 3518 ** |
| 3519 ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 3520 */ |
| 3521 static int do_meta_command(char *zLine, ShellState *p){ |
| 3522 int h = 1; |
| 3523 int nArg = 0; |
| 3524 int n, c; |
| 3525 int rc = 0; |
| 3526 char *azArg[50]; |
| 3527 |
| 3528 /* Parse the input line into tokens. |
| 3529 */ |
| 3530 while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 3531 while( IsSpace(zLine[h]) ){ h++; } |
| 3532 if( zLine[h]==0 ) break; |
| 3533 if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 3534 int delim = zLine[h++]; |
| 3535 azArg[nArg++] = &zLine[h]; |
| 3536 while( zLine[h] && zLine[h]!=delim ){ |
| 3537 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
| 3538 h++; |
| 3539 } |
| 3540 if( zLine[h]==delim ){ |
| 3541 zLine[h++] = 0; |
| 3542 } |
| 3543 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
| 3544 }else{ |
| 3545 azArg[nArg++] = &zLine[h]; |
| 3546 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 3547 if( zLine[h] ) zLine[h++] = 0; |
| 3548 resolve_backslashes(azArg[nArg-1]); |
| 3549 } |
| 3550 } |
| 3551 |
| 3552 /* Process the input line. |
| 3553 */ |
| 3554 if( nArg==0 ) return 0; /* no tokens, no error */ |
| 3555 n = strlen30(azArg[0]); |
| 3556 c = azArg[0][0]; |
| 3557 |
| 3558 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3559 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 3560 if( nArg!=2 ){ |
| 3561 raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 3562 rc = 1; |
| 3563 goto meta_command_exit; |
| 3564 } |
| 3565 open_db(p, 0); |
| 3566 if( booleanValue(azArg[1]) ){ |
| 3567 sqlite3_set_authorizer(p->db, shellAuth, p); |
| 3568 }else{ |
| 3569 sqlite3_set_authorizer(p->db, 0, 0); |
| 3570 } |
| 3571 }else |
| 3572 #endif |
| 3573 |
| 3574 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 3575 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 3576 ){ |
| 3577 const char *zDestFile = 0; |
| 3578 const char *zDb = 0; |
| 3579 sqlite3 *pDest; |
| 3580 sqlite3_backup *pBackup; |
| 3581 int j; |
| 3582 for(j=1; j<nArg; j++){ |
| 3583 const char *z = azArg[j]; |
| 3584 if( z[0]=='-' ){ |
| 3585 while( z[0]=='-' ) z++; |
| 3586 /* No options to process at this time */ |
| 3587 { |
| 3588 utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| 3589 return 1; |
| 3590 } |
| 3591 }else if( zDestFile==0 ){ |
| 3592 zDestFile = azArg[j]; |
| 3593 }else if( zDb==0 ){ |
| 3594 zDb = zDestFile; |
| 3595 zDestFile = azArg[j]; |
| 3596 }else{ |
| 3597 raw_printf(stderr, "too many arguments to .backup\n"); |
| 3598 return 1; |
| 3599 } |
| 3600 } |
| 3601 if( zDestFile==0 ){ |
| 3602 raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
| 3603 return 1; |
| 3604 } |
| 3605 if( zDb==0 ) zDb = "main"; |
| 3606 rc = sqlite3_open(zDestFile, &pDest); |
| 3607 if( rc!=SQLITE_OK ){ |
| 3608 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
| 3609 sqlite3_close(pDest); |
| 3610 return 1; |
| 3611 } |
| 3612 open_db(p, 0); |
| 3613 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 3614 if( pBackup==0 ){ |
| 3615 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 3616 sqlite3_close(pDest); |
| 3617 return 1; |
| 3618 } |
| 3619 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 3620 sqlite3_backup_finish(pBackup); |
| 3621 if( rc==SQLITE_DONE ){ |
| 3622 rc = 0; |
| 3623 }else{ |
| 3624 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 3625 rc = 1; |
| 3626 } |
| 3627 sqlite3_close(pDest); |
| 3628 }else |
| 3629 |
| 3630 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 3631 if( nArg==2 ){ |
| 3632 bail_on_error = booleanValue(azArg[1]); |
| 3633 }else{ |
| 3634 raw_printf(stderr, "Usage: .bail on|off\n"); |
| 3635 rc = 1; |
| 3636 } |
| 3637 }else |
| 3638 |
| 3639 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 3640 if( nArg==2 ){ |
| 3641 if( booleanValue(azArg[1]) ){ |
| 3642 setBinaryMode(p->out, 1); |
| 3643 }else{ |
| 3644 setTextMode(p->out, 1); |
| 3645 } |
| 3646 }else{ |
| 3647 raw_printf(stderr, "Usage: .binary on|off\n"); |
| 3648 rc = 1; |
| 3649 } |
| 3650 }else |
| 3651 |
| 3652 /* The undocumented ".breakpoint" command causes a call to the no-op |
| 3653 ** routine named test_breakpoint(). |
| 3654 */ |
| 3655 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 3656 test_breakpoint(); |
| 3657 }else |
| 3658 |
| 3659 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 3660 if( nArg==2 ){ |
| 3661 p->countChanges = booleanValue(azArg[1]); |
| 3662 }else{ |
| 3663 raw_printf(stderr, "Usage: .changes on|off\n"); |
| 3664 rc = 1; |
| 3665 } |
| 3666 }else |
| 3667 |
| 3668 /* Cancel output redirection, if it is currently set (by .testcase) |
| 3669 ** Then read the content of the testcase-out.txt file and compare against |
| 3670 ** azArg[1]. If there are differences, report an error and exit. |
| 3671 */ |
| 3672 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 3673 char *zRes = 0; |
| 3674 output_reset(p); |
| 3675 if( nArg!=2 ){ |
| 3676 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
| 3677 rc = 2; |
| 3678 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
| 3679 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 3680 rc = 2; |
| 3681 }else if( testcase_glob(azArg[1],zRes)==0 ){ |
| 3682 utf8_printf(stderr, |
| 3683 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 3684 p->zTestcase, azArg[1], zRes); |
| 3685 rc = 2; |
| 3686 }else{ |
| 3687 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 3688 p->nCheck++; |
| 3689 } |
| 3690 sqlite3_free(zRes); |
| 3691 }else |
| 3692 |
| 3693 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 3694 if( nArg==2 ){ |
| 3695 tryToClone(p, azArg[1]); |
| 3696 }else{ |
| 3697 raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 3698 rc = 1; |
| 3699 } |
| 3700 }else |
| 3701 |
| 3702 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
| 3703 ShellState data; |
| 3704 char *zErrMsg = 0; |
| 3705 open_db(p, 0); |
| 3706 memcpy(&data, p, sizeof(data)); |
| 3707 data.showHeader = 0; |
| 3708 data.cMode = data.mode = MODE_List; |
| 3709 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
| 3710 data.cnt = 0; |
| 3711 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 3712 callback, &data, &zErrMsg); |
| 3713 if( zErrMsg ){ |
| 3714 utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 3715 sqlite3_free(zErrMsg); |
| 3716 rc = 1; |
| 3717 } |
| 3718 }else |
| 3719 |
| 3720 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 3721 rc = shell_dbinfo_command(p, nArg, azArg); |
| 3722 }else |
| 3723 |
| 3724 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 3725 open_db(p, 0); |
| 3726 /* When playing back a "dump", the content might appear in an order |
| 3727 ** which causes immediate foreign key constraints to be violated. |
| 3728 ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 3729 if( nArg!=1 && nArg!=2 ){ |
| 3730 raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n"); |
| 3731 rc = 1; |
| 3732 goto meta_command_exit; |
| 3733 } |
| 3734 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 3735 raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 3736 p->writableSchema = 0; |
| 3737 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 3738 p->nErr = 0; |
| 3739 if( nArg==1 ){ |
| 3740 run_schema_dump_query(p, |
| 3741 "SELECT name, type, sql FROM sqlite_master " |
| 3742 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
| 3743 ); |
| 3744 run_schema_dump_query(p, |
| 3745 "SELECT name, type, sql FROM sqlite_master " |
| 3746 "WHERE name=='sqlite_sequence'" |
| 3747 ); |
| 3748 run_table_dump_query(p, |
| 3749 "SELECT sql FROM sqlite_master " |
| 3750 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
| 3751 ); |
| 3752 }else{ |
| 3753 int i; |
| 3754 for(i=1; i<nArg; i++){ |
| 3755 zShellStatic = azArg[i]; |
| 3756 run_schema_dump_query(p, |
| 3757 "SELECT name, type, sql FROM sqlite_master " |
| 3758 "WHERE tbl_name LIKE shellstatic() AND type=='table'" |
| 3759 " AND sql NOT NULL"); |
| 3760 run_table_dump_query(p, |
| 3761 "SELECT sql FROM sqlite_master " |
| 3762 "WHERE sql NOT NULL" |
| 3763 " AND type IN ('index','trigger','view')" |
| 3764 " AND tbl_name LIKE shellstatic()", 0 |
| 3765 ); |
| 3766 zShellStatic = 0; |
| 3767 } |
| 3768 } |
| 3769 if( p->writableSchema ){ |
| 3770 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 3771 p->writableSchema = 0; |
| 3772 } |
| 3773 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 3774 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
| 3775 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
| 3776 }else |
| 3777 |
| 3778 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 3779 if( nArg==2 ){ |
| 3780 p->echoOn = booleanValue(azArg[1]); |
| 3781 }else{ |
| 3782 raw_printf(stderr, "Usage: .echo on|off\n"); |
| 3783 rc = 1; |
| 3784 } |
| 3785 }else |
| 3786 |
| 3787 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 3788 if( nArg==2 ){ |
| 3789 if( strcmp(azArg[1],"full")==0 ){ |
| 3790 p->autoEQP = 2; |
| 3791 }else{ |
| 3792 p->autoEQP = booleanValue(azArg[1]); |
| 3793 } |
| 3794 }else{ |
| 3795 raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
| 3796 rc = 1; |
| 3797 } |
| 3798 }else |
| 3799 |
| 3800 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 3801 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 3802 rc = 2; |
| 3803 }else |
| 3804 |
| 3805 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 3806 int val = 1; |
| 3807 if( nArg>=2 ){ |
| 3808 if( strcmp(azArg[1],"auto")==0 ){ |
| 3809 val = 99; |
| 3810 }else{ |
| 3811 val = booleanValue(azArg[1]); |
| 3812 } |
| 3813 } |
| 3814 if( val==1 && p->mode!=MODE_Explain ){ |
| 3815 p->normalMode = p->mode; |
| 3816 p->mode = MODE_Explain; |
| 3817 p->autoExplain = 0; |
| 3818 }else if( val==0 ){ |
| 3819 if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 3820 p->autoExplain = 0; |
| 3821 }else if( val==99 ){ |
| 3822 if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 3823 p->autoExplain = 1; |
| 3824 } |
| 3825 }else |
| 3826 |
| 3827 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
| 3828 ShellState data; |
| 3829 char *zErrMsg = 0; |
| 3830 int doStats = 0; |
| 3831 memcpy(&data, p, sizeof(data)); |
| 3832 data.showHeader = 0; |
| 3833 data.cMode = data.mode = MODE_Semi; |
| 3834 if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 3835 data.cMode = data.mode = MODE_Pretty; |
| 3836 nArg = 1; |
| 3837 } |
| 3838 if( nArg!=1 ){ |
| 3839 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| 3840 rc = 1; |
| 3841 goto meta_command_exit; |
| 3842 } |
| 3843 open_db(p, 0); |
| 3844 rc = sqlite3_exec(p->db, |
| 3845 "SELECT sql FROM" |
| 3846 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 3847 " FROM sqlite_master UNION ALL" |
| 3848 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| 3849 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
| 3850 "ORDER BY rowid", |
| 3851 callback, &data, &zErrMsg |
| 3852 ); |
| 3853 if( rc==SQLITE_OK ){ |
| 3854 sqlite3_stmt *pStmt; |
| 3855 rc = sqlite3_prepare_v2(p->db, |
| 3856 "SELECT rowid FROM sqlite_master" |
| 3857 " WHERE name GLOB 'sqlite_stat[134]'", |
| 3858 -1, &pStmt, 0); |
| 3859 doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 3860 sqlite3_finalize(pStmt); |
| 3861 } |
| 3862 if( doStats==0 ){ |
| 3863 raw_printf(p->out, "/* No STAT tables available */\n"); |
| 3864 }else{ |
| 3865 raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 3866 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 3867 callback, &data, &zErrMsg); |
| 3868 data.cMode = data.mode = MODE_Insert; |
| 3869 data.zDestTable = "sqlite_stat1"; |
| 3870 shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 3871 shell_callback, &data,&zErrMsg); |
| 3872 data.zDestTable = "sqlite_stat3"; |
| 3873 shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 3874 shell_callback, &data,&zErrMsg); |
| 3875 data.zDestTable = "sqlite_stat4"; |
| 3876 shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 3877 shell_callback, &data, &zErrMsg); |
| 3878 raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 3879 } |
| 3880 }else |
| 3881 |
| 3882 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 3883 if( nArg==2 ){ |
| 3884 p->showHeader = booleanValue(azArg[1]); |
| 3885 }else{ |
| 3886 raw_printf(stderr, "Usage: .headers on|off\n"); |
| 3887 rc = 1; |
| 3888 } |
| 3889 }else |
| 3890 |
| 3891 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 3892 utf8_printf(p->out, "%s", zHelp); |
| 3893 }else |
| 3894 |
| 3895 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
| 3896 char *zTable; /* Insert data into this table */ |
| 3897 char *zFile; /* Name of file to extra content from */ |
| 3898 sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 3899 int nCol; /* Number of columns in the table */ |
| 3900 int nByte; /* Number of bytes in an SQL string */ |
| 3901 int i, j; /* Loop counters */ |
| 3902 int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 3903 int nSep; /* Number of bytes in p->colSeparator[] */ |
| 3904 char *zSql; /* An SQL statement */ |
| 3905 ImportCtx sCtx; /* Reader context */ |
| 3906 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 3907 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
| 3908 |
| 3909 if( nArg!=3 ){ |
| 3910 raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
| 3911 goto meta_command_exit; |
| 3912 } |
| 3913 zFile = azArg[1]; |
| 3914 zTable = azArg[2]; |
| 3915 seenInterrupt = 0; |
| 3916 memset(&sCtx, 0, sizeof(sCtx)); |
| 3917 open_db(p, 0); |
| 3918 nSep = strlen30(p->colSeparator); |
| 3919 if( nSep==0 ){ |
| 3920 raw_printf(stderr, |
| 3921 "Error: non-null column separator required for import\n"); |
| 3922 return 1; |
| 3923 } |
| 3924 if( nSep>1 ){ |
| 3925 raw_printf(stderr, "Error: multi-character column separators not allowed" |
| 3926 " for import\n"); |
| 3927 return 1; |
| 3928 } |
| 3929 nSep = strlen30(p->rowSeparator); |
| 3930 if( nSep==0 ){ |
| 3931 raw_printf(stderr, "Error: non-null row separator required for import\n"); |
| 3932 return 1; |
| 3933 } |
| 3934 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 3935 /* When importing CSV (only), if the row separator is set to the |
| 3936 ** default output row separator, change it to the default input |
| 3937 ** row separator. This avoids having to maintain different input |
| 3938 ** and output row separators. */ |
| 3939 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 3940 nSep = strlen30(p->rowSeparator); |
| 3941 } |
| 3942 if( nSep>1 ){ |
| 3943 raw_printf(stderr, "Error: multi-character row separators not allowed" |
| 3944 " for import\n"); |
| 3945 return 1; |
| 3946 } |
| 3947 sCtx.zFile = zFile; |
| 3948 sCtx.nLine = 1; |
| 3949 if( sCtx.zFile[0]=='|' ){ |
| 3950 #ifdef SQLITE_OMIT_POPEN |
| 3951 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 3952 return 1; |
| 3953 #else |
| 3954 sCtx.in = popen(sCtx.zFile+1, "r"); |
| 3955 sCtx.zFile = "<pipe>"; |
| 3956 xCloser = pclose; |
| 3957 #endif |
| 3958 }else{ |
| 3959 sCtx.in = fopen(sCtx.zFile, "rb"); |
| 3960 xCloser = fclose; |
| 3961 } |
| 3962 if( p->mode==MODE_Ascii ){ |
| 3963 xRead = ascii_read_one_field; |
| 3964 }else{ |
| 3965 xRead = csv_read_one_field; |
| 3966 } |
| 3967 if( sCtx.in==0 ){ |
| 3968 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 3969 return 1; |
| 3970 } |
| 3971 sCtx.cColSep = p->colSeparator[0]; |
| 3972 sCtx.cRowSep = p->rowSeparator[0]; |
| 3973 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
| 3974 if( zSql==0 ){ |
| 3975 raw_printf(stderr, "Error: out of memory\n"); |
| 3976 xCloser(sCtx.in); |
| 3977 return 1; |
| 3978 } |
| 3979 nByte = strlen30(zSql); |
| 3980 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3981 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| 3982 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
| 3983 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 3984 char cSep = '('; |
| 3985 while( xRead(&sCtx) ){ |
| 3986 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
| 3987 cSep = ','; |
| 3988 if( sCtx.cTerm!=sCtx.cColSep ) break; |
| 3989 } |
| 3990 if( cSep=='(' ){ |
| 3991 sqlite3_free(zCreate); |
| 3992 sqlite3_free(sCtx.z); |
| 3993 xCloser(sCtx.in); |
| 3994 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
| 3995 return 1; |
| 3996 } |
| 3997 zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 3998 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 3999 sqlite3_free(zCreate); |
| 4000 if( rc ){ |
| 4001 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
| 4002 sqlite3_errmsg(p->db)); |
| 4003 sqlite3_free(sCtx.z); |
| 4004 xCloser(sCtx.in); |
| 4005 return 1; |
| 4006 } |
| 4007 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4008 } |
| 4009 sqlite3_free(zSql); |
| 4010 if( rc ){ |
| 4011 if (pStmt) sqlite3_finalize(pStmt); |
| 4012 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
| 4013 xCloser(sCtx.in); |
| 4014 return 1; |
| 4015 } |
| 4016 nCol = sqlite3_column_count(pStmt); |
| 4017 sqlite3_finalize(pStmt); |
| 4018 pStmt = 0; |
| 4019 if( nCol==0 ) return 0; /* no columns, no error */ |
| 4020 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
| 4021 if( zSql==0 ){ |
| 4022 raw_printf(stderr, "Error: out of memory\n"); |
| 4023 xCloser(sCtx.in); |
| 4024 return 1; |
| 4025 } |
| 4026 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
| 4027 j = strlen30(zSql); |
| 4028 for(i=1; i<nCol; i++){ |
| 4029 zSql[j++] = ','; |
| 4030 zSql[j++] = '?'; |
| 4031 } |
| 4032 zSql[j++] = ')'; |
| 4033 zSql[j] = 0; |
| 4034 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4035 sqlite3_free(zSql); |
| 4036 if( rc ){ |
| 4037 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 4038 if (pStmt) sqlite3_finalize(pStmt); |
| 4039 xCloser(sCtx.in); |
| 4040 return 1; |
| 4041 } |
| 4042 needCommit = sqlite3_get_autocommit(p->db); |
| 4043 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 4044 do{ |
| 4045 int startLine = sCtx.nLine; |
| 4046 for(i=0; i<nCol; i++){ |
| 4047 char *z = xRead(&sCtx); |
| 4048 /* |
| 4049 ** Did we reach end-of-file before finding any columns? |
| 4050 ** If so, stop instead of NULL filling the remaining columns. |
| 4051 */ |
| 4052 if( z==0 && i==0 ) break; |
| 4053 /* |
| 4054 ** Did we reach end-of-file OR end-of-line before finding any |
| 4055 ** columns in ASCII mode? If so, stop instead of NULL filling |
| 4056 ** the remaining columns. |
| 4057 */ |
| 4058 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| 4059 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 4060 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 4061 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 4062 "filling the rest with NULL\n", |
| 4063 sCtx.zFile, startLine, nCol, i+1); |
| 4064 i += 2; |
| 4065 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| 4066 } |
| 4067 } |
| 4068 if( sCtx.cTerm==sCtx.cColSep ){ |
| 4069 do{ |
| 4070 xRead(&sCtx); |
| 4071 i++; |
| 4072 }while( sCtx.cTerm==sCtx.cColSep ); |
| 4073 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 4074 "extras ignored\n", |
| 4075 sCtx.zFile, startLine, nCol, i); |
| 4076 } |
| 4077 if( i>=nCol ){ |
| 4078 sqlite3_step(pStmt); |
| 4079 rc = sqlite3_reset(pStmt); |
| 4080 if( rc!=SQLITE_OK ){ |
| 4081 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 4082 startLine, sqlite3_errmsg(p->db)); |
| 4083 } |
| 4084 } |
| 4085 }while( sCtx.cTerm!=EOF ); |
| 4086 |
| 4087 xCloser(sCtx.in); |
| 4088 sqlite3_free(sCtx.z); |
| 4089 sqlite3_finalize(pStmt); |
| 4090 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
| 4091 }else |
| 4092 |
| 4093 #ifndef SQLITE_UNTESTABLE |
| 4094 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 4095 char *zSql; |
| 4096 char *zCollist = 0; |
| 4097 sqlite3_stmt *pStmt; |
| 4098 int tnum = 0; |
| 4099 int i; |
| 4100 if( nArg!=3 ){ |
| 4101 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 4102 rc = 1; |
| 4103 goto meta_command_exit; |
| 4104 } |
| 4105 open_db(p, 0); |
| 4106 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 4107 " WHERE name='%q' AND type='index'", azArg[1]); |
| 4108 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4109 sqlite3_free(zSql); |
| 4110 if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4111 tnum = sqlite3_column_int(pStmt, 0); |
| 4112 } |
| 4113 sqlite3_finalize(pStmt); |
| 4114 if( tnum==0 ){ |
| 4115 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 4116 rc = 1; |
| 4117 goto meta_command_exit; |
| 4118 } |
| 4119 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 4120 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4121 sqlite3_free(zSql); |
| 4122 i = 0; |
| 4123 while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4124 char zLabel[20]; |
| 4125 const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
| 4126 i++; |
| 4127 if( zCol==0 ){ |
| 4128 if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 4129 zCol = "_ROWID_"; |
| 4130 }else{ |
| 4131 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 4132 zCol = zLabel; |
| 4133 } |
| 4134 } |
| 4135 if( zCollist==0 ){ |
| 4136 zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 4137 }else{ |
| 4138 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 4139 } |
| 4140 } |
| 4141 sqlite3_finalize(pStmt); |
| 4142 zSql = sqlite3_mprintf( |
| 4143 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 4144 azArg[2], zCollist, zCollist); |
| 4145 sqlite3_free(zCollist); |
| 4146 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 4147 if( rc==SQLITE_OK ){ |
| 4148 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 4149 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 4150 if( rc ){ |
| 4151 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 4152 }else{ |
| 4153 utf8_printf(stdout, "%s;\n", zSql); |
| 4154 raw_printf(stdout, |
| 4155 "WARNING: writing to an imposter table will corrupt the index!\n" |
| 4156 ); |
| 4157 } |
| 4158 }else{ |
| 4159 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
| 4160 rc = 1; |
| 4161 } |
| 4162 sqlite3_free(zSql); |
| 4163 }else |
| 4164 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 4165 |
| 4166 #ifdef SQLITE_ENABLE_IOTRACE |
| 4167 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
| 4168 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
| 4169 if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 4170 iotrace = 0; |
| 4171 if( nArg<2 ){ |
| 4172 sqlite3IoTrace = 0; |
| 4173 }else if( strcmp(azArg[1], "-")==0 ){ |
| 4174 sqlite3IoTrace = iotracePrintf; |
| 4175 iotrace = stdout; |
| 4176 }else{ |
| 4177 iotrace = fopen(azArg[1], "w"); |
| 4178 if( iotrace==0 ){ |
| 4179 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 4180 sqlite3IoTrace = 0; |
| 4181 rc = 1; |
| 4182 }else{ |
| 4183 sqlite3IoTrace = iotracePrintf; |
| 4184 } |
| 4185 } |
| 4186 }else |
| 4187 #endif |
| 4188 |
| 4189 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 4190 static const struct { |
| 4191 const char *zLimitName; /* Name of a limit */ |
| 4192 int limitCode; /* Integer code for that limit */ |
| 4193 } aLimit[] = { |
| 4194 { "length", SQLITE_LIMIT_LENGTH }, |
| 4195 { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 4196 { "column", SQLITE_LIMIT_COLUMN }, |
| 4197 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 4198 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 4199 { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 4200 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 4201 { "attached", SQLITE_LIMIT_ATTACHED }, |
| 4202 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 4203 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 4204 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 4205 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 4206 }; |
| 4207 int i, n2; |
| 4208 open_db(p, 0); |
| 4209 if( nArg==1 ){ |
| 4210 for(i=0; i<ArraySize(aLimit); i++){ |
| 4211 printf("%20s %d\n", aLimit[i].zLimitName, |
| 4212 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 4213 } |
| 4214 }else if( nArg>3 ){ |
| 4215 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
| 4216 rc = 1; |
| 4217 goto meta_command_exit; |
| 4218 }else{ |
| 4219 int iLimit = -1; |
| 4220 n2 = strlen30(azArg[1]); |
| 4221 for(i=0; i<ArraySize(aLimit); i++){ |
| 4222 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 4223 if( iLimit<0 ){ |
| 4224 iLimit = i; |
| 4225 }else{ |
| 4226 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
| 4227 rc = 1; |
| 4228 goto meta_command_exit; |
| 4229 } |
| 4230 } |
| 4231 } |
| 4232 if( iLimit<0 ){ |
| 4233 utf8_printf(stderr, "unknown limit: \"%s\"\n" |
| 4234 "enter \".limits\" with no arguments for a list.\n", |
| 4235 azArg[1]); |
| 4236 rc = 1; |
| 4237 goto meta_command_exit; |
| 4238 } |
| 4239 if( nArg==3 ){ |
| 4240 sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 4241 (int)integerValue(azArg[2])); |
| 4242 } |
| 4243 printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 4244 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 4245 } |
| 4246 }else |
| 4247 |
| 4248 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
| 4249 open_db(p, 0); |
| 4250 lintDotCommand(p, azArg, nArg); |
| 4251 }else |
| 4252 |
| 4253 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4254 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
| 4255 const char *zFile, *zProc; |
| 4256 char *zErrMsg = 0; |
| 4257 if( nArg<2 ){ |
| 4258 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
| 4259 rc = 1; |
| 4260 goto meta_command_exit; |
| 4261 } |
| 4262 zFile = azArg[1]; |
| 4263 zProc = nArg>=3 ? azArg[2] : 0; |
| 4264 open_db(p, 0); |
| 4265 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 4266 if( rc!=SQLITE_OK ){ |
| 4267 utf8_printf(stderr, "Error: %s\n", zErrMsg); |
| 4268 sqlite3_free(zErrMsg); |
| 4269 rc = 1; |
| 4270 } |
| 4271 }else |
| 4272 #endif |
| 4273 |
| 4274 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 4275 if( nArg!=2 ){ |
| 4276 raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 4277 rc = 1; |
| 4278 }else{ |
| 4279 const char *zFile = azArg[1]; |
| 4280 output_file_close(p->pLog); |
| 4281 p->pLog = output_file_open(zFile); |
| 4282 } |
| 4283 }else |
| 4284 |
| 4285 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 4286 const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 4287 int n2 = (int)strlen(zMode); |
| 4288 int c2 = zMode[0]; |
| 4289 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
| 4290 p->mode = MODE_Line; |
| 4291 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4292 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
| 4293 p->mode = MODE_Column; |
| 4294 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4295 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
| 4296 p->mode = MODE_List; |
| 4297 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 4298 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4299 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
| 4300 p->mode = MODE_Html; |
| 4301 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
| 4302 p->mode = MODE_Tcl; |
| 4303 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
| 4304 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4305 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
| 4306 p->mode = MODE_Csv; |
| 4307 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 4308 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 4309 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
| 4310 p->mode = MODE_List; |
| 4311 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
| 4312 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
| 4313 p->mode = MODE_Insert; |
| 4314 set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
| 4315 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 4316 p->mode = MODE_Quote; |
| 4317 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 4318 p->mode = MODE_Ascii; |
| 4319 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 4320 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
| 4321 }else { |
| 4322 raw_printf(stderr, "Error: mode should be one of: " |
| 4323 "ascii column csv html insert line list quote tabs tcl\n"); |
| 4324 rc = 1; |
| 4325 } |
| 4326 p->cMode = p->mode; |
| 4327 }else |
| 4328 |
| 4329 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 4330 if( nArg==2 ){ |
| 4331 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 4332 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| 4333 }else{ |
| 4334 raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
| 4335 rc = 1; |
| 4336 } |
| 4337 }else |
| 4338 |
| 4339 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
| 4340 char *zNewFilename; /* Name of the database file to open */ |
| 4341 int iName = 1; /* Index in azArg[] of the filename */ |
| 4342 int newFlag = 0; /* True to delete file before opening */ |
| 4343 /* Close the existing database */ |
| 4344 session_close_all(p); |
| 4345 sqlite3_close(p->db); |
| 4346 p->db = 0; |
| 4347 sqlite3_free(p->zFreeOnClose); |
| 4348 p->zFreeOnClose = 0; |
| 4349 /* Check for command-line arguments */ |
| 4350 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 4351 const char *z = azArg[iName]; |
| 4352 if( optionMatch(z,"new") ){ |
| 4353 newFlag = 1; |
| 4354 }else if( z[0]=='-' ){ |
| 4355 utf8_printf(stderr, "unknown option: %s\n", z); |
| 4356 rc = 1; |
| 4357 goto meta_command_exit; |
| 4358 } |
| 4359 } |
| 4360 /* If a filename is specified, try to open it first */ |
| 4361 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 4362 if( zNewFilename ){ |
| 4363 if( newFlag ) shellDeleteFile(zNewFilename); |
| 4364 p->zDbFilename = zNewFilename; |
| 4365 open_db(p, 1); |
| 4366 if( p->db==0 ){ |
| 4367 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 4368 sqlite3_free(zNewFilename); |
| 4369 }else{ |
| 4370 p->zFreeOnClose = zNewFilename; |
| 4371 } |
| 4372 } |
| 4373 if( p->db==0 ){ |
| 4374 /* As a fall-back open a TEMP database */ |
| 4375 p->zDbFilename = 0; |
| 4376 open_db(p, 0); |
| 4377 } |
| 4378 }else |
| 4379 |
| 4380 if( c=='o' |
| 4381 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 4382 ){ |
| 4383 const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 4384 if( nArg>2 ){ |
| 4385 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
| 4386 rc = 1; |
| 4387 goto meta_command_exit; |
| 4388 } |
| 4389 if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 4390 if( nArg<2 ){ |
| 4391 raw_printf(stderr, "Usage: .once FILE\n"); |
| 4392 rc = 1; |
| 4393 goto meta_command_exit; |
| 4394 } |
| 4395 p->outCount = 2; |
| 4396 }else{ |
| 4397 p->outCount = 0; |
| 4398 } |
| 4399 output_reset(p); |
| 4400 if( zFile[0]=='|' ){ |
| 4401 #ifdef SQLITE_OMIT_POPEN |
| 4402 raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 4403 rc = 1; |
| 4404 p->out = stdout; |
| 4405 #else |
| 4406 p->out = popen(zFile + 1, "w"); |
| 4407 if( p->out==0 ){ |
| 4408 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
| 4409 p->out = stdout; |
| 4410 rc = 1; |
| 4411 }else{ |
| 4412 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 4413 } |
| 4414 #endif |
| 4415 }else{ |
| 4416 p->out = output_file_open(zFile); |
| 4417 if( p->out==0 ){ |
| 4418 if( strcmp(zFile,"off")!=0 ){ |
| 4419 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
| 4420 } |
| 4421 p->out = stdout; |
| 4422 rc = 1; |
| 4423 } else { |
| 4424 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 4425 } |
| 4426 } |
| 4427 }else |
| 4428 |
| 4429 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 4430 int i; |
| 4431 for(i=1; i<nArg; i++){ |
| 4432 if( i>1 ) raw_printf(p->out, " "); |
| 4433 utf8_printf(p->out, "%s", azArg[i]); |
| 4434 } |
| 4435 raw_printf(p->out, "\n"); |
| 4436 }else |
| 4437 |
| 4438 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
| 4439 if( nArg >= 2) { |
| 4440 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 4441 } |
| 4442 if( nArg >= 3) { |
| 4443 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 4444 } |
| 4445 }else |
| 4446 |
| 4447 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
| 4448 rc = 2; |
| 4449 }else |
| 4450 |
| 4451 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 4452 FILE *alt; |
| 4453 if( nArg!=2 ){ |
| 4454 raw_printf(stderr, "Usage: .read FILE\n"); |
| 4455 rc = 1; |
| 4456 goto meta_command_exit; |
| 4457 } |
| 4458 alt = fopen(azArg[1], "rb"); |
| 4459 if( alt==0 ){ |
| 4460 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 4461 rc = 1; |
| 4462 }else{ |
| 4463 rc = process_input(p, alt); |
| 4464 fclose(alt); |
| 4465 } |
| 4466 }else |
| 4467 |
| 4468 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
| 4469 const char *zSrcFile; |
| 4470 const char *zDb; |
| 4471 sqlite3 *pSrc; |
| 4472 sqlite3_backup *pBackup; |
| 4473 int nTimeout = 0; |
| 4474 |
| 4475 if( nArg==2 ){ |
| 4476 zSrcFile = azArg[1]; |
| 4477 zDb = "main"; |
| 4478 }else if( nArg==3 ){ |
| 4479 zSrcFile = azArg[2]; |
| 4480 zDb = azArg[1]; |
| 4481 }else{ |
| 4482 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
| 4483 rc = 1; |
| 4484 goto meta_command_exit; |
| 4485 } |
| 4486 rc = sqlite3_open(zSrcFile, &pSrc); |
| 4487 if( rc!=SQLITE_OK ){ |
| 4488 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
| 4489 sqlite3_close(pSrc); |
| 4490 return 1; |
| 4491 } |
| 4492 open_db(p, 0); |
| 4493 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 4494 if( pBackup==0 ){ |
| 4495 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 4496 sqlite3_close(pSrc); |
| 4497 return 1; |
| 4498 } |
| 4499 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 4500 || rc==SQLITE_BUSY ){ |
| 4501 if( rc==SQLITE_BUSY ){ |
| 4502 if( nTimeout++ >= 3 ) break; |
| 4503 sqlite3_sleep(100); |
| 4504 } |
| 4505 } |
| 4506 sqlite3_backup_finish(pBackup); |
| 4507 if( rc==SQLITE_DONE ){ |
| 4508 rc = 0; |
| 4509 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 4510 raw_printf(stderr, "Error: source database is busy\n"); |
| 4511 rc = 1; |
| 4512 }else{ |
| 4513 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 4514 rc = 1; |
| 4515 } |
| 4516 sqlite3_close(pSrc); |
| 4517 }else |
| 4518 |
| 4519 |
| 4520 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 4521 if( nArg==2 ){ |
| 4522 p->scanstatsOn = booleanValue(azArg[1]); |
| 4523 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 4524 raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| 4525 #endif |
| 4526 }else{ |
| 4527 raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| 4528 rc = 1; |
| 4529 } |
| 4530 }else |
| 4531 |
| 4532 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 4533 ShellState data; |
| 4534 char *zErrMsg = 0; |
| 4535 open_db(p, 0); |
| 4536 memcpy(&data, p, sizeof(data)); |
| 4537 data.showHeader = 0; |
| 4538 data.cMode = data.mode = MODE_Semi; |
| 4539 if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 4540 data.cMode = data.mode = MODE_Pretty; |
| 4541 nArg--; |
| 4542 if( nArg==2 ) azArg[1] = azArg[2]; |
| 4543 } |
| 4544 if( nArg==2 && azArg[1][0]!='-' ){ |
| 4545 int i; |
| 4546 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); |
| 4547 if( strcmp(azArg[1],"sqlite_master")==0 ){ |
| 4548 char *new_argv[2], *new_colv[2]; |
| 4549 new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 4550 " type text,\n" |
| 4551 " name text,\n" |
| 4552 " tbl_name text,\n" |
| 4553 " rootpage integer,\n" |
| 4554 " sql text\n" |
| 4555 ")"; |
| 4556 new_argv[1] = 0; |
| 4557 new_colv[0] = "sql"; |
| 4558 new_colv[1] = 0; |
| 4559 callback(&data, 1, new_argv, new_colv); |
| 4560 rc = SQLITE_OK; |
| 4561 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
| 4562 char *new_argv[2], *new_colv[2]; |
| 4563 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 4564 " type text,\n" |
| 4565 " name text,\n" |
| 4566 " tbl_name text,\n" |
| 4567 " rootpage integer,\n" |
| 4568 " sql text\n" |
| 4569 ")"; |
| 4570 new_argv[1] = 0; |
| 4571 new_colv[0] = "sql"; |
| 4572 new_colv[1] = 0; |
| 4573 callback(&data, 1, new_argv, new_colv); |
| 4574 rc = SQLITE_OK; |
| 4575 }else{ |
| 4576 zShellStatic = azArg[1]; |
| 4577 rc = sqlite3_exec(p->db, |
| 4578 "SELECT sql FROM " |
| 4579 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4580 " FROM sqlite_master UNION ALL" |
| 4581 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| 4582 "WHERE lower(tbl_name) LIKE shellstatic()" |
| 4583 " AND type!='meta' AND sql NOTNULL " |
| 4584 "ORDER BY rowid", |
| 4585 callback, &data, &zErrMsg); |
| 4586 zShellStatic = 0; |
| 4587 } |
| 4588 }else if( nArg==1 ){ |
| 4589 rc = sqlite3_exec(p->db, |
| 4590 "SELECT sql FROM " |
| 4591 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4592 " FROM sqlite_master UNION ALL" |
| 4593 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| 4594 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
| 4595 "ORDER BY rowid", |
| 4596 callback, &data, &zErrMsg |
| 4597 ); |
| 4598 }else{ |
| 4599 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
| 4600 rc = 1; |
| 4601 goto meta_command_exit; |
| 4602 } |
| 4603 if( zErrMsg ){ |
| 4604 utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 4605 sqlite3_free(zErrMsg); |
| 4606 rc = 1; |
| 4607 }else if( rc != SQLITE_OK ){ |
| 4608 raw_printf(stderr,"Error: querying schema information\n"); |
| 4609 rc = 1; |
| 4610 }else{ |
| 4611 rc = 0; |
| 4612 } |
| 4613 }else |
| 4614 |
| 4615 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 4616 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
| 4617 sqlite3SelectTrace = integerValue(azArg[1]); |
| 4618 }else |
| 4619 #endif |
| 4620 |
| 4621 #if defined(SQLITE_ENABLE_SESSION) |
| 4622 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 4623 OpenSession *pSession = &p->aSession[0]; |
| 4624 char **azCmd = &azArg[1]; |
| 4625 int iSes = 0; |
| 4626 int nCmd = nArg - 1; |
| 4627 int i; |
| 4628 if( nArg<=1 ) goto session_syntax_error; |
| 4629 open_db(p, 0); |
| 4630 if( nArg>=3 ){ |
| 4631 for(iSes=0; iSes<p->nSession; iSes++){ |
| 4632 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 4633 } |
| 4634 if( iSes<p->nSession ){ |
| 4635 pSession = &p->aSession[iSes]; |
| 4636 azCmd++; |
| 4637 nCmd--; |
| 4638 }else{ |
| 4639 pSession = &p->aSession[0]; |
| 4640 iSes = 0; |
| 4641 } |
| 4642 } |
| 4643 |
| 4644 /* .session attach TABLE |
| 4645 ** Invoke the sqlite3session_attach() interface to attach a particular |
| 4646 ** table so that it is never filtered. |
| 4647 */ |
| 4648 if( strcmp(azCmd[0],"attach")==0 ){ |
| 4649 if( nCmd!=2 ) goto session_syntax_error; |
| 4650 if( pSession->p==0 ){ |
| 4651 session_not_open: |
| 4652 raw_printf(stderr, "ERROR: No sessions are open\n"); |
| 4653 }else{ |
| 4654 rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 4655 if( rc ){ |
| 4656 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| 4657 rc = 0; |
| 4658 } |
| 4659 } |
| 4660 }else |
| 4661 |
| 4662 /* .session changeset FILE |
| 4663 ** .session patchset FILE |
| 4664 ** Write a changeset or patchset into a file. The file is overwritten. |
| 4665 */ |
| 4666 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 4667 FILE *out = 0; |
| 4668 if( nCmd!=2 ) goto session_syntax_error; |
| 4669 if( pSession->p==0 ) goto session_not_open; |
| 4670 out = fopen(azCmd[1], "wb"); |
| 4671 if( out==0 ){ |
| 4672 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1])
; |
| 4673 }else{ |
| 4674 int szChng; |
| 4675 void *pChng; |
| 4676 if( azCmd[0][0]=='c' ){ |
| 4677 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| 4678 }else{ |
| 4679 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 4680 } |
| 4681 if( rc ){ |
| 4682 printf("Error: error code %d\n", rc); |
| 4683 rc = 0; |
| 4684 } |
| 4685 if( pChng |
| 4686 && fwrite(pChng, szChng, 1, out)!=1 ){ |
| 4687 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| 4688 szChng); |
| 4689 } |
| 4690 sqlite3_free(pChng); |
| 4691 fclose(out); |
| 4692 } |
| 4693 }else |
| 4694 |
| 4695 /* .session close |
| 4696 ** Close the identified session |
| 4697 */ |
| 4698 if( strcmp(azCmd[0], "close")==0 ){ |
| 4699 if( nCmd!=1 ) goto session_syntax_error; |
| 4700 if( p->nSession ){ |
| 4701 session_close(pSession); |
| 4702 p->aSession[iSes] = p->aSession[--p->nSession]; |
| 4703 } |
| 4704 }else |
| 4705 |
| 4706 /* .session enable ?BOOLEAN? |
| 4707 ** Query or set the enable flag |
| 4708 */ |
| 4709 if( strcmp(azCmd[0], "enable")==0 ){ |
| 4710 int ii; |
| 4711 if( nCmd>2 ) goto session_syntax_error; |
| 4712 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 4713 if( p->nSession ){ |
| 4714 ii = sqlite3session_enable(pSession->p, ii); |
| 4715 utf8_printf(p->out, "session %s enable flag = %d\n", |
| 4716 pSession->zName, ii); |
| 4717 } |
| 4718 }else |
| 4719 |
| 4720 /* .session filter GLOB .... |
| 4721 ** Set a list of GLOB patterns of table names to be excluded. |
| 4722 */ |
| 4723 if( strcmp(azCmd[0], "filter")==0 ){ |
| 4724 int ii, nByte; |
| 4725 if( nCmd<2 ) goto session_syntax_error; |
| 4726 if( p->nSession ){ |
| 4727 for(ii=0; ii<pSession->nFilter; ii++){ |
| 4728 sqlite3_free(pSession->azFilter[ii]); |
| 4729 } |
| 4730 sqlite3_free(pSession->azFilter); |
| 4731 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 4732 pSession->azFilter = sqlite3_malloc( nByte ); |
| 4733 if( pSession->azFilter==0 ){ |
| 4734 raw_printf(stderr, "Error: out or memory\n"); |
| 4735 exit(1); |
| 4736 } |
| 4737 for(ii=1; ii<nCmd; ii++){ |
| 4738 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| 4739 } |
| 4740 pSession->nFilter = ii-1; |
| 4741 } |
| 4742 }else |
| 4743 |
| 4744 /* .session indirect ?BOOLEAN? |
| 4745 ** Query or set the indirect flag |
| 4746 */ |
| 4747 if( strcmp(azCmd[0], "indirect")==0 ){ |
| 4748 int ii; |
| 4749 if( nCmd>2 ) goto session_syntax_error; |
| 4750 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 4751 if( p->nSession ){ |
| 4752 ii = sqlite3session_indirect(pSession->p, ii); |
| 4753 utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 4754 pSession->zName, ii); |
| 4755 } |
| 4756 }else |
| 4757 |
| 4758 /* .session isempty |
| 4759 ** Determine if the session is empty |
| 4760 */ |
| 4761 if( strcmp(azCmd[0], "isempty")==0 ){ |
| 4762 int ii; |
| 4763 if( nCmd!=1 ) goto session_syntax_error; |
| 4764 if( p->nSession ){ |
| 4765 ii = sqlite3session_isempty(pSession->p); |
| 4766 utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 4767 pSession->zName, ii); |
| 4768 } |
| 4769 }else |
| 4770 |
| 4771 /* .session list |
| 4772 ** List all currently open sessions |
| 4773 */ |
| 4774 if( strcmp(azCmd[0],"list")==0 ){ |
| 4775 for(i=0; i<p->nSession; i++){ |
| 4776 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
| 4777 } |
| 4778 }else |
| 4779 |
| 4780 /* .session open DB NAME |
| 4781 ** Open a new session called NAME on the attached database DB. |
| 4782 ** DB is normally "main". |
| 4783 */ |
| 4784 if( strcmp(azCmd[0],"open")==0 ){ |
| 4785 char *zName; |
| 4786 if( nCmd!=3 ) goto session_syntax_error; |
| 4787 zName = azCmd[2]; |
| 4788 if( zName[0]==0 ) goto session_syntax_error; |
| 4789 for(i=0; i<p->nSession; i++){ |
| 4790 if( strcmp(p->aSession[i].zName,zName)==0 ){ |
| 4791 utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| 4792 goto meta_command_exit; |
| 4793 } |
| 4794 } |
| 4795 if( p->nSession>=ArraySize(p->aSession) ){ |
| 4796 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
| 4797 goto meta_command_exit; |
| 4798 } |
| 4799 pSession = &p->aSession[p->nSession]; |
| 4800 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 4801 if( rc ){ |
| 4802 raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| 4803 rc = 0; |
| 4804 goto meta_command_exit; |
| 4805 } |
| 4806 pSession->nFilter = 0; |
| 4807 sqlite3session_table_filter(pSession->p, session_filter, pSession); |
| 4808 p->nSession++; |
| 4809 pSession->zName = sqlite3_mprintf("%s", zName); |
| 4810 }else |
| 4811 /* If no command name matches, show a syntax error */ |
| 4812 session_syntax_error: |
| 4813 session_help(p); |
| 4814 }else |
| 4815 #endif |
| 4816 |
| 4817 #ifdef SQLITE_DEBUG |
| 4818 /* Undocumented commands for internal testing. Subject to change |
| 4819 ** without notice. */ |
| 4820 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 4821 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 4822 int i, v; |
| 4823 for(i=1; i<nArg; i++){ |
| 4824 v = booleanValue(azArg[i]); |
| 4825 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| 4826 } |
| 4827 } |
| 4828 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 4829 int i; sqlite3_int64 v; |
| 4830 for(i=1; i<nArg; i++){ |
| 4831 char zBuf[200]; |
| 4832 v = integerValue(azArg[i]); |
| 4833 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| 4834 utf8_printf(p->out, "%s", zBuf); |
| 4835 } |
| 4836 } |
| 4837 }else |
| 4838 #endif |
| 4839 |
| 4840 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| 4841 if( nArg<2 || nArg>3 ){ |
| 4842 raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 4843 rc = 1; |
| 4844 } |
| 4845 if( nArg>=2 ){ |
| 4846 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| 4847 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| 4848 } |
| 4849 if( nArg>=3 ){ |
| 4850 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 4851 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 4852 } |
| 4853 }else |
| 4854 |
| 4855 if( c=='s' |
| 4856 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 4857 ){ |
| 4858 char *zCmd; |
| 4859 int i, x; |
| 4860 if( nArg<2 ){ |
| 4861 raw_printf(stderr, "Usage: .system COMMAND\n"); |
| 4862 rc = 1; |
| 4863 goto meta_command_exit; |
| 4864 } |
| 4865 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
| 4866 for(i=2; i<nArg; i++){ |
| 4867 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 4868 zCmd, azArg[i]); |
| 4869 } |
| 4870 x = system(zCmd); |
| 4871 sqlite3_free(zCmd); |
| 4872 if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 4873 }else |
| 4874 |
| 4875 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
| 4876 static const char *azBool[] = { "off", "on", "full", "unk" }; |
| 4877 int i; |
| 4878 if( nArg!=1 ){ |
| 4879 raw_printf(stderr, "Usage: .show\n"); |
| 4880 rc = 1; |
| 4881 goto meta_command_exit; |
| 4882 } |
| 4883 utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]); |
| 4884 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 4885 utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 4886 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 4887 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
| 4888 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 4889 utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| 4890 output_c_string(p->out, p->nullValue); |
| 4891 raw_printf(p->out, "\n"); |
| 4892 utf8_printf(p->out,"%12.12s: %s\n","output", |
| 4893 strlen30(p->outfile) ? p->outfile : "stdout"); |
| 4894 utf8_printf(p->out,"%12.12s: ", "colseparator"); |
| 4895 output_c_string(p->out, p->colSeparator); |
| 4896 raw_printf(p->out, "\n"); |
| 4897 utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
| 4898 output_c_string(p->out, p->rowSeparator); |
| 4899 raw_printf(p->out, "\n"); |
| 4900 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
| 4901 utf8_printf(p->out, "%12.12s: ", "width"); |
| 4902 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
| 4903 raw_printf(p->out, "%d ", p->colWidth[i]); |
| 4904 } |
| 4905 raw_printf(p->out, "\n"); |
| 4906 utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 4907 p->zDbFilename ? p->zDbFilename : ""); |
| 4908 }else |
| 4909 |
| 4910 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 4911 if( nArg==2 ){ |
| 4912 p->statsOn = booleanValue(azArg[1]); |
| 4913 }else if( nArg==1 ){ |
| 4914 display_stats(p->db, p, 0); |
| 4915 }else{ |
| 4916 raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
| 4917 rc = 1; |
| 4918 } |
| 4919 }else |
| 4920 |
| 4921 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 4922 || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 4923 || strncmp(azArg[0], "indexes", n)==0) ) |
| 4924 ){ |
| 4925 sqlite3_stmt *pStmt; |
| 4926 char **azResult; |
| 4927 int nRow, nAlloc; |
| 4928 char *zSql = 0; |
| 4929 int ii; |
| 4930 open_db(p, 0); |
| 4931 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
| 4932 if( rc ) return shellDatabaseError(p->db); |
| 4933 |
| 4934 /* Create an SQL statement to query for the list of tables in the |
| 4935 ** main and all attached databases where the table name matches the |
| 4936 ** LIKE pattern bound to variable "?1". */ |
| 4937 if( c=='t' ){ |
| 4938 zSql = sqlite3_mprintf( |
| 4939 "SELECT name FROM sqlite_master" |
| 4940 " WHERE type IN ('table','view')" |
| 4941 " AND name NOT LIKE 'sqlite_%%'" |
| 4942 " AND name LIKE ?1"); |
| 4943 }else if( nArg>2 ){ |
| 4944 /* It is an historical accident that the .indexes command shows an error |
| 4945 ** when called with the wrong number of arguments whereas the .tables |
| 4946 ** command does not. */ |
| 4947 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 4948 rc = 1; |
| 4949 goto meta_command_exit; |
| 4950 }else{ |
| 4951 zSql = sqlite3_mprintf( |
| 4952 "SELECT name FROM sqlite_master" |
| 4953 " WHERE type='index'" |
| 4954 " AND tbl_name LIKE ?1"); |
| 4955 } |
| 4956 for(ii=0; zSql && sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
| 4957 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 4958 if( zDbName==0 || ii==0 ) continue; |
| 4959 if( c=='t' ){ |
| 4960 zSql = sqlite3_mprintf( |
| 4961 "%z UNION ALL " |
| 4962 "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 4963 " WHERE type IN ('table','view')" |
| 4964 " AND name NOT LIKE 'sqlite_%%'" |
| 4965 " AND name LIKE ?1", zSql, zDbName, zDbName); |
| 4966 }else{ |
| 4967 zSql = sqlite3_mprintf( |
| 4968 "%z UNION ALL " |
| 4969 "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 4970 " WHERE type='index'" |
| 4971 " AND tbl_name LIKE ?1", zSql, zDbName, zDbName); |
| 4972 } |
| 4973 } |
| 4974 rc = sqlite3_finalize(pStmt); |
| 4975 if( zSql && rc==SQLITE_OK ){ |
| 4976 zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 4977 if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4978 } |
| 4979 sqlite3_free(zSql); |
| 4980 if( !zSql ) return shellNomemError(); |
| 4981 if( rc ) return shellDatabaseError(p->db); |
| 4982 |
| 4983 /* Run the SQL statement prepared by the above block. Store the results |
| 4984 ** as an array of nul-terminated strings in azResult[]. */ |
| 4985 nRow = nAlloc = 0; |
| 4986 azResult = 0; |
| 4987 if( nArg>1 ){ |
| 4988 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
| 4989 }else{ |
| 4990 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 4991 } |
| 4992 while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4993 if( nRow>=nAlloc ){ |
| 4994 char **azNew; |
| 4995 int n2 = nAlloc*2 + 10; |
| 4996 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
| 4997 if( azNew==0 ){ |
| 4998 rc = shellNomemError(); |
| 4999 break; |
| 5000 } |
| 5001 nAlloc = n2; |
| 5002 azResult = azNew; |
| 5003 } |
| 5004 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 5005 if( 0==azResult[nRow] ){ |
| 5006 rc = shellNomemError(); |
| 5007 break; |
| 5008 } |
| 5009 nRow++; |
| 5010 } |
| 5011 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 5012 rc = shellDatabaseError(p->db); |
| 5013 } |
| 5014 |
| 5015 /* Pretty-print the contents of array azResult[] to the output */ |
| 5016 if( rc==0 && nRow>0 ){ |
| 5017 int len, maxlen = 0; |
| 5018 int i, j; |
| 5019 int nPrintCol, nPrintRow; |
| 5020 for(i=0; i<nRow; i++){ |
| 5021 len = strlen30(azResult[i]); |
| 5022 if( len>maxlen ) maxlen = len; |
| 5023 } |
| 5024 nPrintCol = 80/(maxlen+2); |
| 5025 if( nPrintCol<1 ) nPrintCol = 1; |
| 5026 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 5027 for(i=0; i<nPrintRow; i++){ |
| 5028 for(j=i; j<nRow; j+=nPrintRow){ |
| 5029 char *zSp = j<nPrintRow ? "" : " "; |
| 5030 utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 5031 azResult[j] ? azResult[j]:""); |
| 5032 } |
| 5033 raw_printf(p->out, "\n"); |
| 5034 } |
| 5035 } |
| 5036 |
| 5037 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 5038 sqlite3_free(azResult); |
| 5039 }else |
| 5040 |
| 5041 /* Begin redirecting output to the file "testcase-out.txt" */ |
| 5042 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 5043 output_reset(p); |
| 5044 p->out = output_file_open("testcase-out.txt"); |
| 5045 if( p->out==0 ){ |
| 5046 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
| 5047 } |
| 5048 if( nArg>=2 ){ |
| 5049 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 5050 }else{ |
| 5051 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 5052 } |
| 5053 }else |
| 5054 |
| 5055 #ifndef SQLITE_UNTESTABLE |
| 5056 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
| 5057 static const struct { |
| 5058 const char *zCtrlName; /* Name of a test-control option */ |
| 5059 int ctrlCode; /* Integer code for that option */ |
| 5060 } aCtrl[] = { |
| 5061 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 5062 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 5063 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 5064 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 5065 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 5066 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 5067 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 5068 { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 5069 { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 5070 { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 5071 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 5072 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
| 5073 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
| 5074 { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
| 5075 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
| 5076 { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
| 5077 }; |
| 5078 int testctrl = -1; |
| 5079 int rc2 = 0; |
| 5080 int i, n2; |
| 5081 open_db(p, 0); |
| 5082 |
| 5083 /* convert testctrl text option to value. allow any unique prefix |
| 5084 ** of the option name, or a numerical value. */ |
| 5085 n2 = strlen30(azArg[1]); |
| 5086 for(i=0; i<ArraySize(aCtrl); i++){ |
| 5087 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
| 5088 if( testctrl<0 ){ |
| 5089 testctrl = aCtrl[i].ctrlCode; |
| 5090 }else{ |
| 5091 utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
| 5092 testctrl = -1; |
| 5093 break; |
| 5094 } |
| 5095 } |
| 5096 } |
| 5097 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
| 5098 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
| 5099 utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
| 5100 }else{ |
| 5101 switch(testctrl){ |
| 5102 |
| 5103 /* sqlite3_test_control(int, db, int) */ |
| 5104 case SQLITE_TESTCTRL_OPTIMIZATIONS: |
| 5105 case SQLITE_TESTCTRL_RESERVE: |
| 5106 if( nArg==3 ){ |
| 5107 int opt = (int)strtol(azArg[2], 0, 0); |
| 5108 rc2 = sqlite3_test_control(testctrl, p->db, opt); |
| 5109 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5110 } else { |
| 5111 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
| 5112 azArg[1]); |
| 5113 } |
| 5114 break; |
| 5115 |
| 5116 /* sqlite3_test_control(int) */ |
| 5117 case SQLITE_TESTCTRL_PRNG_SAVE: |
| 5118 case SQLITE_TESTCTRL_PRNG_RESTORE: |
| 5119 case SQLITE_TESTCTRL_PRNG_RESET: |
| 5120 case SQLITE_TESTCTRL_BYTEORDER: |
| 5121 if( nArg==2 ){ |
| 5122 rc2 = sqlite3_test_control(testctrl); |
| 5123 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5124 } else { |
| 5125 utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 5126 azArg[1]); |
| 5127 } |
| 5128 break; |
| 5129 |
| 5130 /* sqlite3_test_control(int, uint) */ |
| 5131 case SQLITE_TESTCTRL_PENDING_BYTE: |
| 5132 if( nArg==3 ){ |
| 5133 unsigned int opt = (unsigned int)integerValue(azArg[2]); |
| 5134 rc2 = sqlite3_test_control(testctrl, opt); |
| 5135 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5136 } else { |
| 5137 utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
| 5138 " int option\n", azArg[1]); |
| 5139 } |
| 5140 break; |
| 5141 |
| 5142 /* sqlite3_test_control(int, int) */ |
| 5143 case SQLITE_TESTCTRL_ASSERT: |
| 5144 case SQLITE_TESTCTRL_ALWAYS: |
| 5145 case SQLITE_TESTCTRL_NEVER_CORRUPT: |
| 5146 if( nArg==3 ){ |
| 5147 int opt = booleanValue(azArg[2]); |
| 5148 rc2 = sqlite3_test_control(testctrl, opt); |
| 5149 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5150 } else { |
| 5151 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
| 5152 azArg[1]); |
| 5153 } |
| 5154 break; |
| 5155 |
| 5156 /* sqlite3_test_control(int, char *) */ |
| 5157 #ifdef SQLITE_N_KEYWORD |
| 5158 case SQLITE_TESTCTRL_ISKEYWORD: |
| 5159 if( nArg==3 ){ |
| 5160 const char *opt = azArg[2]; |
| 5161 rc2 = sqlite3_test_control(testctrl, opt); |
| 5162 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5163 } else { |
| 5164 utf8_printf(stderr, |
| 5165 "Error: testctrl %s takes a single char * option\n", |
| 5166 azArg[1]); |
| 5167 } |
| 5168 break; |
| 5169 #endif |
| 5170 |
| 5171 case SQLITE_TESTCTRL_IMPOSTER: |
| 5172 if( nArg==5 ){ |
| 5173 rc2 = sqlite3_test_control(testctrl, p->db, |
| 5174 azArg[2], |
| 5175 integerValue(azArg[3]), |
| 5176 integerValue(azArg[4])); |
| 5177 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| 5178 }else{ |
| 5179 raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
| 5180 } |
| 5181 break; |
| 5182 |
| 5183 case SQLITE_TESTCTRL_BITVEC_TEST: |
| 5184 case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 5185 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 5186 case SQLITE_TESTCTRL_SCRATCHMALLOC: |
| 5187 default: |
| 5188 utf8_printf(stderr, |
| 5189 "Error: CLI support for testctrl %s not implemented\n", |
| 5190 azArg[1]); |
| 5191 break; |
| 5192 } |
| 5193 } |
| 5194 }else |
| 5195 |
| 5196 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
| 5197 open_db(p, 0); |
| 5198 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
| 5199 }else |
| 5200 |
| 5201 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 5202 if( nArg==2 ){ |
| 5203 enableTimer = booleanValue(azArg[1]); |
| 5204 if( enableTimer && !HAS_TIMER ){ |
| 5205 raw_printf(stderr, "Error: timer not available on this system.\n"); |
| 5206 enableTimer = 0; |
| 5207 } |
| 5208 }else{ |
| 5209 raw_printf(stderr, "Usage: .timer on|off\n"); |
| 5210 rc = 1; |
| 5211 } |
| 5212 }else |
| 5213 |
| 5214 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
| 5215 open_db(p, 0); |
| 5216 if( nArg!=2 ){ |
| 5217 raw_printf(stderr, "Usage: .trace FILE|off\n"); |
| 5218 rc = 1; |
| 5219 goto meta_command_exit; |
| 5220 } |
| 5221 output_file_close(p->traceOut); |
| 5222 p->traceOut = output_file_open(azArg[1]); |
| 5223 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 5224 if( p->traceOut==0 ){ |
| 5225 sqlite3_trace_v2(p->db, 0, 0, 0); |
| 5226 }else{ |
| 5227 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut)
; |
| 5228 } |
| 5229 #endif |
| 5230 }else |
| 5231 #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 5232 |
| 5233 #if SQLITE_USER_AUTHENTICATION |
| 5234 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 5235 if( nArg<2 ){ |
| 5236 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
| 5237 rc = 1; |
| 5238 goto meta_command_exit; |
| 5239 } |
| 5240 open_db(p, 0); |
| 5241 if( strcmp(azArg[1],"login")==0 ){ |
| 5242 if( nArg!=4 ){ |
| 5243 raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
| 5244 rc = 1; |
| 5245 goto meta_command_exit; |
| 5246 } |
| 5247 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 5248 (int)strlen(azArg[3])); |
| 5249 if( rc ){ |
| 5250 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
| 5251 rc = 1; |
| 5252 } |
| 5253 }else if( strcmp(azArg[1],"add")==0 ){ |
| 5254 if( nArg!=5 ){ |
| 5255 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
| 5256 rc = 1; |
| 5257 goto meta_command_exit; |
| 5258 } |
| 5259 rc = sqlite3_user_add(p->db, azArg[2], |
| 5260 azArg[3], (int)strlen(azArg[3]), |
| 5261 booleanValue(azArg[4])); |
| 5262 if( rc ){ |
| 5263 raw_printf(stderr, "User-Add failed: %d\n", rc); |
| 5264 rc = 1; |
| 5265 } |
| 5266 }else if( strcmp(azArg[1],"edit")==0 ){ |
| 5267 if( nArg!=5 ){ |
| 5268 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
| 5269 rc = 1; |
| 5270 goto meta_command_exit; |
| 5271 } |
| 5272 rc = sqlite3_user_change(p->db, azArg[2], |
| 5273 azArg[3], (int)strlen(azArg[3]), |
| 5274 booleanValue(azArg[4])); |
| 5275 if( rc ){ |
| 5276 raw_printf(stderr, "User-Edit failed: %d\n", rc); |
| 5277 rc = 1; |
| 5278 } |
| 5279 }else if( strcmp(azArg[1],"delete")==0 ){ |
| 5280 if( nArg!=3 ){ |
| 5281 raw_printf(stderr, "Usage: .user delete USER\n"); |
| 5282 rc = 1; |
| 5283 goto meta_command_exit; |
| 5284 } |
| 5285 rc = sqlite3_user_delete(p->db, azArg[2]); |
| 5286 if( rc ){ |
| 5287 raw_printf(stderr, "User-Delete failed: %d\n", rc); |
| 5288 rc = 1; |
| 5289 } |
| 5290 }else{ |
| 5291 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
| 5292 rc = 1; |
| 5293 goto meta_command_exit; |
| 5294 } |
| 5295 }else |
| 5296 #endif /* SQLITE_USER_AUTHENTICATION */ |
| 5297 |
| 5298 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
| 5299 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
| 5300 sqlite3_libversion(), sqlite3_sourceid()); |
| 5301 }else |
| 5302 |
| 5303 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 5304 const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 5305 sqlite3_vfs *pVfs = 0; |
| 5306 if( p->db ){ |
| 5307 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 5308 if( pVfs ){ |
| 5309 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 5310 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 5311 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 5312 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 5313 } |
| 5314 } |
| 5315 }else |
| 5316 |
| 5317 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 5318 sqlite3_vfs *pVfs; |
| 5319 sqlite3_vfs *pCurrent = 0; |
| 5320 if( p->db ){ |
| 5321 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 5322 } |
| 5323 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 5324 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 5325 pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 5326 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 5327 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 5328 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 5329 if( pVfs->pNext ){ |
| 5330 raw_printf(p->out, "-----------------------------------\n"); |
| 5331 } |
| 5332 } |
| 5333 }else |
| 5334 |
| 5335 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 5336 const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 5337 char *zVfsName = 0; |
| 5338 if( p->db ){ |
| 5339 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 5340 if( zVfsName ){ |
| 5341 utf8_printf(p->out, "%s\n", zVfsName); |
| 5342 sqlite3_free(zVfsName); |
| 5343 } |
| 5344 } |
| 5345 }else |
| 5346 |
| 5347 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 5348 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
| 5349 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
| 5350 }else |
| 5351 #endif |
| 5352 |
| 5353 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 5354 int j; |
| 5355 assert( nArg<=ArraySize(azArg) ); |
| 5356 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 5357 p->colWidth[j-1] = (int)integerValue(azArg[j]); |
| 5358 } |
| 5359 }else |
| 5360 |
| 5361 { |
| 5362 utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
| 5363 " \"%s\". Enter \".help\" for help\n", azArg[0]); |
| 5364 rc = 1; |
| 5365 } |
| 5366 |
| 5367 meta_command_exit: |
| 5368 if( p->outCount ){ |
| 5369 p->outCount--; |
| 5370 if( p->outCount==0 ) output_reset(p); |
| 5371 } |
| 5372 return rc; |
| 5373 } |
| 5374 |
| 5375 /* |
| 5376 ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 5377 ** of string z[]. |
| 5378 */ |
| 5379 static int line_contains_semicolon(const char *z, int N){ |
| 5380 int i; |
| 5381 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 5382 return 0; |
| 5383 } |
| 5384 |
| 5385 /* |
| 5386 ** Test to see if a line consists entirely of whitespace. |
| 5387 */ |
| 5388 static int _all_whitespace(const char *z){ |
| 5389 for(; *z; z++){ |
| 5390 if( IsSpace(z[0]) ) continue; |
| 5391 if( *z=='/' && z[1]=='*' ){ |
| 5392 z += 2; |
| 5393 while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 5394 if( *z==0 ) return 0; |
| 5395 z++; |
| 5396 continue; |
| 5397 } |
| 5398 if( *z=='-' && z[1]=='-' ){ |
| 5399 z += 2; |
| 5400 while( *z && *z!='\n' ){ z++; } |
| 5401 if( *z==0 ) return 1; |
| 5402 continue; |
| 5403 } |
| 5404 return 0; |
| 5405 } |
| 5406 return 1; |
| 5407 } |
| 5408 |
| 5409 /* |
| 5410 ** Return TRUE if the line typed in is an SQL command terminator other |
| 5411 ** than a semi-colon. The SQL Server style "go" command is understood |
| 5412 ** as is the Oracle "/". |
| 5413 */ |
| 5414 static int line_is_command_terminator(const char *zLine){ |
| 5415 while( IsSpace(zLine[0]) ){ zLine++; }; |
| 5416 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 5417 return 1; /* Oracle */ |
| 5418 } |
| 5419 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
| 5420 && _all_whitespace(&zLine[2]) ){ |
| 5421 return 1; /* SQL Server */ |
| 5422 } |
| 5423 return 0; |
| 5424 } |
| 5425 |
| 5426 /* |
| 5427 ** Return true if zSql is a complete SQL statement. Return false if it |
| 5428 ** ends in the middle of a string literal or C-style comment. |
| 5429 */ |
| 5430 static int line_is_complete(char *zSql, int nSql){ |
| 5431 int rc; |
| 5432 if( zSql==0 ) return 1; |
| 5433 zSql[nSql] = ';'; |
| 5434 zSql[nSql+1] = 0; |
| 5435 rc = sqlite3_complete(zSql); |
| 5436 zSql[nSql] = 0; |
| 5437 return rc; |
| 5438 } |
| 5439 |
| 5440 /* |
| 5441 ** Run a single line of SQL |
| 5442 */ |
| 5443 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 5444 int rc; |
| 5445 char *zErrMsg = 0; |
| 5446 |
| 5447 open_db(p, 0); |
| 5448 if( p->backslashOn ) resolve_backslashes(zSql); |
| 5449 BEGIN_TIMER; |
| 5450 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 5451 END_TIMER; |
| 5452 if( rc || zErrMsg ){ |
| 5453 char zPrefix[100]; |
| 5454 if( in!=0 || !stdin_is_interactive ){ |
| 5455 sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 5456 "Error: near line %d:", startline); |
| 5457 }else{ |
| 5458 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 5459 } |
| 5460 if( zErrMsg!=0 ){ |
| 5461 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 5462 sqlite3_free(zErrMsg); |
| 5463 zErrMsg = 0; |
| 5464 }else{ |
| 5465 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 5466 } |
| 5467 return 1; |
| 5468 }else if( p->countChanges ){ |
| 5469 raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 5470 sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 5471 } |
| 5472 return 0; |
| 5473 } |
| 5474 |
| 5475 |
| 5476 /* |
| 5477 ** Read input from *in and process it. If *in==0 then input |
| 5478 ** is interactive - the user is typing it it. Otherwise, input |
| 5479 ** is coming from a file or device. A prompt is issued and history |
| 5480 ** is saved only if input is interactive. An interrupt signal will |
| 5481 ** cause this routine to exit immediately, unless input is interactive. |
| 5482 ** |
| 5483 ** Return the number of errors. |
| 5484 */ |
| 5485 static int process_input(ShellState *p, FILE *in){ |
| 5486 char *zLine = 0; /* A single input line */ |
| 5487 char *zSql = 0; /* Accumulated SQL text */ |
| 5488 int nLine; /* Length of current line */ |
| 5489 int nSql = 0; /* Bytes of zSql[] used */ |
| 5490 int nAlloc = 0; /* Allocated zSql[] space */ |
| 5491 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
| 5492 int rc; /* Error code */ |
| 5493 int errCnt = 0; /* Number of errors seen */ |
| 5494 int lineno = 0; /* Current line number */ |
| 5495 int startline = 0; /* Line number for start of current input */ |
| 5496 |
| 5497 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 5498 fflush(p->out); |
| 5499 zLine = one_input_line(in, zLine, nSql>0); |
| 5500 if( zLine==0 ){ |
| 5501 /* End of input */ |
| 5502 if( in==0 && stdin_is_interactive ) printf("\n"); |
| 5503 break; |
| 5504 } |
| 5505 if( seenInterrupt ){ |
| 5506 if( in!=0 ) break; |
| 5507 seenInterrupt = 0; |
| 5508 } |
| 5509 lineno++; |
| 5510 if( nSql==0 && _all_whitespace(zLine) ){ |
| 5511 if( p->echoOn ) printf("%s\n", zLine); |
| 5512 continue; |
| 5513 } |
| 5514 if( zLine && zLine[0]=='.' && nSql==0 ){ |
| 5515 if( p->echoOn ) printf("%s\n", zLine); |
| 5516 rc = do_meta_command(zLine, p); |
| 5517 if( rc==2 ){ /* exit requested */ |
| 5518 break; |
| 5519 }else if( rc ){ |
| 5520 errCnt++; |
| 5521 } |
| 5522 continue; |
| 5523 } |
| 5524 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
| 5525 memcpy(zLine,";",2); |
| 5526 } |
| 5527 nLine = strlen30(zLine); |
| 5528 if( nSql+nLine+2>=nAlloc ){ |
| 5529 nAlloc = nSql+nLine+100; |
| 5530 zSql = realloc(zSql, nAlloc); |
| 5531 if( zSql==0 ){ |
| 5532 raw_printf(stderr, "Error: out of memory\n"); |
| 5533 exit(1); |
| 5534 } |
| 5535 } |
| 5536 nSqlPrior = nSql; |
| 5537 if( nSql==0 ){ |
| 5538 int i; |
| 5539 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
| 5540 assert( nAlloc>0 && zSql!=0 ); |
| 5541 memcpy(zSql, zLine+i, nLine+1-i); |
| 5542 startline = lineno; |
| 5543 nSql = nLine-i; |
| 5544 }else{ |
| 5545 zSql[nSql++] = '\n'; |
| 5546 memcpy(zSql+nSql, zLine, nLine+1); |
| 5547 nSql += nLine; |
| 5548 } |
| 5549 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
| 5550 && sqlite3_complete(zSql) ){ |
| 5551 errCnt += runOneSqlLine(p, zSql, in, startline); |
| 5552 nSql = 0; |
| 5553 if( p->outCount ){ |
| 5554 output_reset(p); |
| 5555 p->outCount = 0; |
| 5556 } |
| 5557 }else if( nSql && _all_whitespace(zSql) ){ |
| 5558 if( p->echoOn ) printf("%s\n", zSql); |
| 5559 nSql = 0; |
| 5560 } |
| 5561 } |
| 5562 if( nSql && !_all_whitespace(zSql) ){ |
| 5563 runOneSqlLine(p, zSql, in, startline); |
| 5564 } |
| 5565 free(zSql); |
| 5566 free(zLine); |
| 5567 return errCnt>0; |
| 5568 } |
| 5569 |
| 5570 /* |
| 5571 ** Return a pathname which is the user's home directory. A |
| 5572 ** 0 return indicates an error of some kind. |
| 5573 */ |
| 5574 static char *find_home_dir(int clearFlag){ |
| 5575 static char *home_dir = NULL; |
| 5576 if( clearFlag ){ |
| 5577 free(home_dir); |
| 5578 home_dir = 0; |
| 5579 return 0; |
| 5580 } |
| 5581 if( home_dir ) return home_dir; |
| 5582 |
| 5583 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 5584 && !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 5585 { |
| 5586 struct passwd *pwent; |
| 5587 uid_t uid = getuid(); |
| 5588 if( (pwent=getpwuid(uid)) != NULL) { |
| 5589 home_dir = pwent->pw_dir; |
| 5590 } |
| 5591 } |
| 5592 #endif |
| 5593 |
| 5594 #if defined(_WIN32_WCE) |
| 5595 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 5596 */ |
| 5597 home_dir = "/"; |
| 5598 #else |
| 5599 |
| 5600 #if defined(_WIN32) || defined(WIN32) |
| 5601 if (!home_dir) { |
| 5602 home_dir = getenv("USERPROFILE"); |
| 5603 } |
| 5604 #endif |
| 5605 |
| 5606 if (!home_dir) { |
| 5607 home_dir = getenv("HOME"); |
| 5608 } |
| 5609 |
| 5610 #if defined(_WIN32) || defined(WIN32) |
| 5611 if (!home_dir) { |
| 5612 char *zDrive, *zPath; |
| 5613 int n; |
| 5614 zDrive = getenv("HOMEDRIVE"); |
| 5615 zPath = getenv("HOMEPATH"); |
| 5616 if( zDrive && zPath ){ |
| 5617 n = strlen30(zDrive) + strlen30(zPath) + 1; |
| 5618 home_dir = malloc( n ); |
| 5619 if( home_dir==0 ) return 0; |
| 5620 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 5621 return home_dir; |
| 5622 } |
| 5623 home_dir = "c:\\"; |
| 5624 } |
| 5625 #endif |
| 5626 |
| 5627 #endif /* !_WIN32_WCE */ |
| 5628 |
| 5629 if( home_dir ){ |
| 5630 int n = strlen30(home_dir) + 1; |
| 5631 char *z = malloc( n ); |
| 5632 if( z ) memcpy(z, home_dir, n); |
| 5633 home_dir = z; |
| 5634 } |
| 5635 |
| 5636 return home_dir; |
| 5637 } |
| 5638 |
| 5639 /* |
| 5640 ** Read input from the file given by sqliterc_override. Or if that |
| 5641 ** parameter is NULL, take input from ~/.sqliterc |
| 5642 ** |
| 5643 ** Returns the number of errors. |
| 5644 */ |
| 5645 static void process_sqliterc( |
| 5646 ShellState *p, /* Configuration data */ |
| 5647 const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 5648 ){ |
| 5649 char *home_dir = NULL; |
| 5650 const char *sqliterc = sqliterc_override; |
| 5651 char *zBuf = 0; |
| 5652 FILE *in = NULL; |
| 5653 |
| 5654 if (sqliterc == NULL) { |
| 5655 home_dir = find_home_dir(0); |
| 5656 if( home_dir==0 ){ |
| 5657 raw_printf(stderr, "-- warning: cannot find home directory;" |
| 5658 " cannot read ~/.sqliterc\n"); |
| 5659 return; |
| 5660 } |
| 5661 sqlite3_initialize(); |
| 5662 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 5663 sqliterc = zBuf; |
| 5664 } |
| 5665 in = fopen(sqliterc,"rb"); |
| 5666 if( in ){ |
| 5667 if( stdin_is_interactive ){ |
| 5668 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
| 5669 } |
| 5670 process_input(p,in); |
| 5671 fclose(in); |
| 5672 } |
| 5673 sqlite3_free(zBuf); |
| 5674 } |
| 5675 |
| 5676 /* |
| 5677 ** Show available command line options |
| 5678 */ |
| 5679 static const char zOptions[] = |
| 5680 " -ascii set output mode to 'ascii'\n" |
| 5681 " -bail stop after hitting an error\n" |
| 5682 " -batch force batch I/O\n" |
| 5683 " -column set output mode to 'column'\n" |
| 5684 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
| 5685 " -csv set output mode to 'csv'\n" |
| 5686 " -echo print commands before execution\n" |
| 5687 " -init FILENAME read/process named file\n" |
| 5688 " -[no]header turn headers on or off\n" |
| 5689 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 5690 " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 5691 #endif |
| 5692 " -help show this message\n" |
| 5693 " -html set output mode to HTML\n" |
| 5694 " -interactive force interactive I/O\n" |
| 5695 " -line set output mode to 'line'\n" |
| 5696 " -list set output mode to 'list'\n" |
| 5697 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
| 5698 " -mmap N default mmap size set to N\n" |
| 5699 #ifdef SQLITE_ENABLE_MULTIPLEX |
| 5700 " -multiplex enable the multiplexor VFS\n" |
| 5701 #endif |
| 5702 " -newline SEP set output row separator. Default: '\\n'\n" |
| 5703 " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 5704 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 5705 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" |
| 5706 " -separator SEP set output column separator. Default: '|'\n" |
| 5707 " -stats print memory stats before each finalize\n" |
| 5708 " -version show SQLite version\n" |
| 5709 " -vfs NAME use NAME as the default VFS\n" |
| 5710 #ifdef SQLITE_ENABLE_VFSTRACE |
| 5711 " -vfstrace enable tracing of all VFS calls\n" |
| 5712 #endif |
| 5713 ; |
| 5714 static void usage(int showDetail){ |
| 5715 utf8_printf(stderr, |
| 5716 "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 5717 "FILENAME is the name of an SQLite database. A new database is created\n" |
| 5718 "if the file does not previously exist.\n", Argv0); |
| 5719 if( showDetail ){ |
| 5720 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
| 5721 }else{ |
| 5722 raw_printf(stderr, "Use the -help option for additional information\n"); |
| 5723 } |
| 5724 exit(1); |
| 5725 } |
| 5726 |
| 5727 /* |
| 5728 ** Initialize the state information in data |
| 5729 */ |
| 5730 static void main_init(ShellState *data) { |
| 5731 memset(data, 0, sizeof(*data)); |
| 5732 data->normalMode = data->cMode = data->mode = MODE_List; |
| 5733 data->autoExplain = 1; |
| 5734 memcpy(data->colSeparator,SEP_Column, 2); |
| 5735 memcpy(data->rowSeparator,SEP_Row, 2); |
| 5736 data->showHeader = 0; |
| 5737 data->shellFlgs = SHFLG_Lookaside; |
| 5738 sqlite3_config(SQLITE_CONFIG_URI, 1); |
| 5739 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
| 5740 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 5741 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 5742 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
| 5743 } |
| 5744 |
| 5745 /* |
| 5746 ** Output text to the console in a font that attracts extra attention. |
| 5747 */ |
| 5748 #ifdef _WIN32 |
| 5749 static void printBold(const char *zText){ |
| 5750 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 5751 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 5752 GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 5753 SetConsoleTextAttribute(out, |
| 5754 FOREGROUND_RED|FOREGROUND_INTENSITY |
| 5755 ); |
| 5756 printf("%s", zText); |
| 5757 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
| 5758 } |
| 5759 #else |
| 5760 static void printBold(const char *zText){ |
| 5761 printf("\033[1m%s\033[0m", zText); |
| 5762 } |
| 5763 #endif |
| 5764 |
| 5765 /* |
| 5766 ** Get the argument to an --option. Throw an error and die if no argument |
| 5767 ** is available. |
| 5768 */ |
| 5769 static char *cmdline_option_value(int argc, char **argv, int i){ |
| 5770 if( i==argc ){ |
| 5771 utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
| 5772 argv[0], argv[argc-1]); |
| 5773 exit(1); |
| 5774 } |
| 5775 return argv[i]; |
| 5776 } |
| 5777 |
| 5778 #ifndef SQLITE_SHELL_IS_UTF8 |
| 5779 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 5780 # define SQLITE_SHELL_IS_UTF8 (0) |
| 5781 # else |
| 5782 # define SQLITE_SHELL_IS_UTF8 (1) |
| 5783 # endif |
| 5784 #endif |
| 5785 |
| 5786 #if SQLITE_SHELL_IS_UTF8 |
| 5787 int SQLITE_CDECL main(int argc, char **argv){ |
| 5788 #else |
| 5789 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 5790 char **argv; |
| 5791 #endif |
| 5792 char *zErrMsg = 0; |
| 5793 ShellState data; |
| 5794 const char *zInitFile = 0; |
| 5795 int i; |
| 5796 int rc = 0; |
| 5797 int warnInmemoryDb = 0; |
| 5798 int readStdin = 1; |
| 5799 int nCmd = 0; |
| 5800 char **azCmd = 0; |
| 5801 |
| 5802 setBinaryMode(stdin, 0); |
| 5803 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 5804 stdin_is_interactive = isatty(0); |
| 5805 stdout_is_console = isatty(1); |
| 5806 |
| 5807 #if USE_SYSTEM_SQLITE+0!=1 |
| 5808 if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
| 5809 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
| 5810 sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 5811 exit(1); |
| 5812 } |
| 5813 #endif |
| 5814 main_init(&data); |
| 5815 #if !SQLITE_SHELL_IS_UTF8 |
| 5816 sqlite3_initialize(); |
| 5817 argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 5818 if( argv==0 ){ |
| 5819 raw_printf(stderr, "out of memory\n"); |
| 5820 exit(1); |
| 5821 } |
| 5822 for(i=0; i<argc; i++){ |
| 5823 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 5824 if( argv[i]==0 ){ |
| 5825 raw_printf(stderr, "out of memory\n"); |
| 5826 exit(1); |
| 5827 } |
| 5828 } |
| 5829 #endif |
| 5830 assert( argc>=1 && argv && argv[0] ); |
| 5831 Argv0 = argv[0]; |
| 5832 |
| 5833 /* Make sure we have a valid signal handler early, before anything |
| 5834 ** else is done. |
| 5835 */ |
| 5836 #ifdef SIGINT |
| 5837 signal(SIGINT, interrupt_handler); |
| 5838 #endif |
| 5839 |
| 5840 #ifdef SQLITE_SHELL_DBNAME_PROC |
| 5841 { |
| 5842 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 5843 ** of a C-function that will provide the name of the database file. Use |
| 5844 ** this compile-time option to embed this shell program in larger |
| 5845 ** applications. */ |
| 5846 extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 5847 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 5848 warnInmemoryDb = 0; |
| 5849 } |
| 5850 #endif |
| 5851 |
| 5852 /* Do an initial pass through the command-line argument to locate |
| 5853 ** the name of the database file, the name of the initialization file, |
| 5854 ** the size of the alternative malloc heap, |
| 5855 ** and the first command to execute. |
| 5856 */ |
| 5857 for(i=1; i<argc; i++){ |
| 5858 char *z; |
| 5859 z = argv[i]; |
| 5860 if( z[0]!='-' ){ |
| 5861 if( data.zDbFilename==0 ){ |
| 5862 data.zDbFilename = z; |
| 5863 }else{ |
| 5864 /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 5865 ** mean that nothing is read from stdin */ |
| 5866 readStdin = 0; |
| 5867 nCmd++; |
| 5868 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 5869 if( azCmd==0 ){ |
| 5870 raw_printf(stderr, "out of memory\n"); |
| 5871 exit(1); |
| 5872 } |
| 5873 azCmd[nCmd-1] = z; |
| 5874 } |
| 5875 } |
| 5876 if( z[1]=='-' ) z++; |
| 5877 if( strcmp(z,"-separator")==0 |
| 5878 || strcmp(z,"-nullvalue")==0 |
| 5879 || strcmp(z,"-newline")==0 |
| 5880 || strcmp(z,"-cmd")==0 |
| 5881 ){ |
| 5882 (void)cmdline_option_value(argc, argv, ++i); |
| 5883 }else if( strcmp(z,"-init")==0 ){ |
| 5884 zInitFile = cmdline_option_value(argc, argv, ++i); |
| 5885 }else if( strcmp(z,"-batch")==0 ){ |
| 5886 /* Need to check for batch mode here to so we can avoid printing |
| 5887 ** informational messages (like from process_sqliterc) before |
| 5888 ** we do the actual processing of arguments later in a second pass. |
| 5889 */ |
| 5890 stdin_is_interactive = 0; |
| 5891 }else if( strcmp(z,"-heap")==0 ){ |
| 5892 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 5893 const char *zSize; |
| 5894 sqlite3_int64 szHeap; |
| 5895 |
| 5896 zSize = cmdline_option_value(argc, argv, ++i); |
| 5897 szHeap = integerValue(zSize); |
| 5898 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
| 5899 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
| 5900 #else |
| 5901 (void)cmdline_option_value(argc, argv, ++i); |
| 5902 #endif |
| 5903 }else if( strcmp(z,"-scratch")==0 ){ |
| 5904 int n, sz; |
| 5905 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5906 if( sz>400000 ) sz = 400000; |
| 5907 if( sz<2500 ) sz = 2500; |
| 5908 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5909 if( n>10 ) n = 10; |
| 5910 if( n<1 ) n = 1; |
| 5911 sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 5912 data.shellFlgs |= SHFLG_Scratch; |
| 5913 }else if( strcmp(z,"-pagecache")==0 ){ |
| 5914 int n, sz; |
| 5915 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5916 if( sz>70000 ) sz = 70000; |
| 5917 if( sz<0 ) sz = 0; |
| 5918 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5919 sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 5920 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
| 5921 data.shellFlgs |= SHFLG_Pagecache; |
| 5922 }else if( strcmp(z,"-lookaside")==0 ){ |
| 5923 int n, sz; |
| 5924 sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5925 if( sz<0 ) sz = 0; |
| 5926 n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 5927 if( n<0 ) n = 0; |
| 5928 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 5929 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
| 5930 #ifdef SQLITE_ENABLE_VFSTRACE |
| 5931 }else if( strcmp(z,"-vfstrace")==0 ){ |
| 5932 extern int vfstrace_register( |
| 5933 const char *zTraceName, |
| 5934 const char *zOldVfsName, |
| 5935 int (*xOut)(const char*,void*), |
| 5936 void *pOutArg, |
| 5937 int makeDefault |
| 5938 ); |
| 5939 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
| 5940 #endif |
| 5941 #ifdef SQLITE_ENABLE_MULTIPLEX |
| 5942 }else if( strcmp(z,"-multiplex")==0 ){ |
| 5943 extern int sqlite3_multiple_initialize(const char*,int); |
| 5944 sqlite3_multiplex_initialize(0, 1); |
| 5945 #endif |
| 5946 }else if( strcmp(z,"-mmap")==0 ){ |
| 5947 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 5948 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
| 5949 }else if( strcmp(z,"-vfs")==0 ){ |
| 5950 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
| 5951 if( pVfs ){ |
| 5952 sqlite3_vfs_register(pVfs, 1); |
| 5953 }else{ |
| 5954 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
| 5955 exit(1); |
| 5956 } |
| 5957 } |
| 5958 } |
| 5959 if( data.zDbFilename==0 ){ |
| 5960 #ifndef SQLITE_OMIT_MEMORYDB |
| 5961 data.zDbFilename = ":memory:"; |
| 5962 warnInmemoryDb = argc==1; |
| 5963 #else |
| 5964 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 5965 return 1; |
| 5966 #endif |
| 5967 } |
| 5968 data.out = stdout; |
| 5969 |
| 5970 /* Go ahead and open the database file if it already exists. If the |
| 5971 ** file does not exist, delay opening it. This prevents empty database |
| 5972 ** files from being created if a user mistypes the database name argument |
| 5973 ** to the sqlite command-line tool. |
| 5974 */ |
| 5975 if( access(data.zDbFilename, 0)==0 ){ |
| 5976 open_db(&data, 0); |
| 5977 } |
| 5978 |
| 5979 /* Process the initialization file if there is one. If no -init option |
| 5980 ** is given on the command line, look for a file named ~/.sqliterc and |
| 5981 ** try to process it. |
| 5982 */ |
| 5983 process_sqliterc(&data,zInitFile); |
| 5984 |
| 5985 /* Make a second pass through the command-line argument and set |
| 5986 ** options. This second pass is delayed until after the initialization |
| 5987 ** file is processed so that the command-line arguments will override |
| 5988 ** settings in the initialization file. |
| 5989 */ |
| 5990 for(i=1; i<argc; i++){ |
| 5991 char *z = argv[i]; |
| 5992 if( z[0]!='-' ) continue; |
| 5993 if( z[1]=='-' ){ z++; } |
| 5994 if( strcmp(z,"-init")==0 ){ |
| 5995 i++; |
| 5996 }else if( strcmp(z,"-html")==0 ){ |
| 5997 data.mode = MODE_Html; |
| 5998 }else if( strcmp(z,"-list")==0 ){ |
| 5999 data.mode = MODE_List; |
| 6000 }else if( strcmp(z,"-line")==0 ){ |
| 6001 data.mode = MODE_Line; |
| 6002 }else if( strcmp(z,"-column")==0 ){ |
| 6003 data.mode = MODE_Column; |
| 6004 }else if( strcmp(z,"-csv")==0 ){ |
| 6005 data.mode = MODE_Csv; |
| 6006 memcpy(data.colSeparator,",",2); |
| 6007 }else if( strcmp(z,"-ascii")==0 ){ |
| 6008 data.mode = MODE_Ascii; |
| 6009 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 6010 SEP_Unit); |
| 6011 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 6012 SEP_Record); |
| 6013 }else if( strcmp(z,"-separator")==0 ){ |
| 6014 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 6015 "%s",cmdline_option_value(argc,argv,++i)); |
| 6016 }else if( strcmp(z,"-newline")==0 ){ |
| 6017 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 6018 "%s",cmdline_option_value(argc,argv,++i)); |
| 6019 }else if( strcmp(z,"-nullvalue")==0 ){ |
| 6020 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
| 6021 "%s",cmdline_option_value(argc,argv,++i)); |
| 6022 }else if( strcmp(z,"-header")==0 ){ |
| 6023 data.showHeader = 1; |
| 6024 }else if( strcmp(z,"-noheader")==0 ){ |
| 6025 data.showHeader = 0; |
| 6026 }else if( strcmp(z,"-echo")==0 ){ |
| 6027 data.echoOn = 1; |
| 6028 }else if( strcmp(z,"-eqp")==0 ){ |
| 6029 data.autoEQP = 1; |
| 6030 }else if( strcmp(z,"-eqpfull")==0 ){ |
| 6031 data.autoEQP = 2; |
| 6032 }else if( strcmp(z,"-stats")==0 ){ |
| 6033 data.statsOn = 1; |
| 6034 }else if( strcmp(z,"-scanstats")==0 ){ |
| 6035 data.scanstatsOn = 1; |
| 6036 }else if( strcmp(z,"-backslash")==0 ){ |
| 6037 /* Undocumented command-line option: -backslash |
| 6038 ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 6039 ** prior to sending the SQL into SQLite. Useful for injecting |
| 6040 ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 6041 */ |
| 6042 data.backslashOn = 1; |
| 6043 }else if( strcmp(z,"-bail")==0 ){ |
| 6044 bail_on_error = 1; |
| 6045 }else if( strcmp(z,"-version")==0 ){ |
| 6046 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 6047 return 0; |
| 6048 }else if( strcmp(z,"-interactive")==0 ){ |
| 6049 stdin_is_interactive = 1; |
| 6050 }else if( strcmp(z,"-batch")==0 ){ |
| 6051 stdin_is_interactive = 0; |
| 6052 }else if( strcmp(z,"-heap")==0 ){ |
| 6053 i++; |
| 6054 }else if( strcmp(z,"-scratch")==0 ){ |
| 6055 i+=2; |
| 6056 }else if( strcmp(z,"-pagecache")==0 ){ |
| 6057 i+=2; |
| 6058 }else if( strcmp(z,"-lookaside")==0 ){ |
| 6059 i+=2; |
| 6060 }else if( strcmp(z,"-mmap")==0 ){ |
| 6061 i++; |
| 6062 }else if( strcmp(z,"-vfs")==0 ){ |
| 6063 i++; |
| 6064 #ifdef SQLITE_ENABLE_VFSTRACE |
| 6065 }else if( strcmp(z,"-vfstrace")==0 ){ |
| 6066 i++; |
| 6067 #endif |
| 6068 #ifdef SQLITE_ENABLE_MULTIPLEX |
| 6069 }else if( strcmp(z,"-multiplex")==0 ){ |
| 6070 i++; |
| 6071 #endif |
| 6072 }else if( strcmp(z,"-help")==0 ){ |
| 6073 usage(1); |
| 6074 }else if( strcmp(z,"-cmd")==0 ){ |
| 6075 /* Run commands that follow -cmd first and separately from commands |
| 6076 ** that simply appear on the command-line. This seems goofy. It would |
| 6077 ** be better if all commands ran in the order that they appear. But |
| 6078 ** we retain the goofy behavior for historical compatibility. */ |
| 6079 if( i==argc-1 ) break; |
| 6080 z = cmdline_option_value(argc,argv,++i); |
| 6081 if( z[0]=='.' ){ |
| 6082 rc = do_meta_command(z, &data); |
| 6083 if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
| 6084 }else{ |
| 6085 open_db(&data, 0); |
| 6086 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 6087 if( zErrMsg!=0 ){ |
| 6088 utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 6089 if( bail_on_error ) return rc!=0 ? rc : 1; |
| 6090 }else if( rc!=0 ){ |
| 6091 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
| 6092 if( bail_on_error ) return rc; |
| 6093 } |
| 6094 } |
| 6095 }else{ |
| 6096 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 6097 raw_printf(stderr,"Use -help for a list of options.\n"); |
| 6098 return 1; |
| 6099 } |
| 6100 data.cMode = data.mode; |
| 6101 } |
| 6102 |
| 6103 if( !readStdin ){ |
| 6104 /* Run all arguments that do not begin with '-' as if they were separate |
| 6105 ** command-line inputs, except for the argToSkip argument which contains |
| 6106 ** the database filename. |
| 6107 */ |
| 6108 for(i=0; i<nCmd; i++){ |
| 6109 if( azCmd[i][0]=='.' ){ |
| 6110 rc = do_meta_command(azCmd[i], &data); |
| 6111 if( rc ) return rc==2 ? 0 : rc; |
| 6112 }else{ |
| 6113 open_db(&data, 0); |
| 6114 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 6115 if( zErrMsg!=0 ){ |
| 6116 utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 6117 return rc!=0 ? rc : 1; |
| 6118 }else if( rc!=0 ){ |
| 6119 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| 6120 return rc; |
| 6121 } |
| 6122 } |
| 6123 } |
| 6124 free(azCmd); |
| 6125 }else{ |
| 6126 /* Run commands received from standard input |
| 6127 */ |
| 6128 if( stdin_is_interactive ){ |
| 6129 char *zHome; |
| 6130 char *zHistory = 0; |
| 6131 int nHistory; |
| 6132 printf( |
| 6133 "SQLite version %s %.19s\n" /*extra-version-info*/ |
| 6134 "Enter \".help\" for usage hints.\n", |
| 6135 sqlite3_libversion(), sqlite3_sourceid() |
| 6136 ); |
| 6137 if( warnInmemoryDb ){ |
| 6138 printf("Connected to a "); |
| 6139 printBold("transient in-memory database"); |
| 6140 printf(".\nUse \".open FILENAME\" to reopen on a " |
| 6141 "persistent database.\n"); |
| 6142 } |
| 6143 zHome = find_home_dir(0); |
| 6144 if( zHome ){ |
| 6145 nHistory = strlen30(zHome) + 20; |
| 6146 if( (zHistory = malloc(nHistory))!=0 ){ |
| 6147 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 6148 } |
| 6149 } |
| 6150 if( zHistory ){ shell_read_history(zHistory); } |
| 6151 rc = process_input(&data, 0); |
| 6152 if( zHistory ){ |
| 6153 shell_stifle_history(100); |
| 6154 shell_write_history(zHistory); |
| 6155 free(zHistory); |
| 6156 } |
| 6157 }else{ |
| 6158 rc = process_input(&data, stdin); |
| 6159 } |
| 6160 } |
| 6161 set_table_name(&data, 0); |
| 6162 if( data.db ){ |
| 6163 session_close_all(&data); |
| 6164 sqlite3_close(data.db); |
| 6165 } |
| 6166 sqlite3_free(data.zFreeOnClose); |
| 6167 find_home_dir(1); |
| 6168 #if !SQLITE_SHELL_IS_UTF8 |
| 6169 for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 6170 sqlite3_free(argv); |
| 6171 #endif |
| 6172 return rc; |
| 6173 } |
OLD | NEW |