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