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" |
drh | 1e506b5 | 2018-01-05 21:01:37 +0000 | [diff] [blame] | 64 | typedef sqlite3_int64 i64; |
| 65 | typedef sqlite3_uint64 u64; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 66 | typedef unsigned char u8; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 67 | #if SQLITE_USER_AUTHENTICATION |
| 68 | # include "sqlite3userauth.h" |
| 69 | #endif |
| 70 | #include <ctype.h> |
| 71 | #include <stdarg.h> |
| 72 | |
| 73 | #if !defined(_WIN32) && !defined(WIN32) |
| 74 | # include <signal.h> |
| 75 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 76 | # include <pwd.h> |
| 77 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 78 | #endif |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 79 | #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 80 | # include <unistd.h> |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 81 | # include <dirent.h> |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 82 | # if defined(__MINGW32__) |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 83 | # define DIRENT dirent |
mistachkin | 2f74b3c | 2018-01-05 20:26:06 +0000 | [diff] [blame] | 84 | # ifndef S_ISLNK |
| 85 | # define S_ISLNK(mode) (0) |
| 86 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 87 | # endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 88 | #endif |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 89 | #include <sys/types.h> |
| 90 | #include <sys/stat.h> |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 91 | |
| 92 | #if HAVE_READLINE |
| 93 | # include <readline/readline.h> |
| 94 | # include <readline/history.h> |
| 95 | #endif |
| 96 | |
| 97 | #if HAVE_EDITLINE |
| 98 | # include <editline/readline.h> |
| 99 | #endif |
| 100 | |
| 101 | #if HAVE_EDITLINE || HAVE_READLINE |
| 102 | |
| 103 | # define shell_add_history(X) add_history(X) |
| 104 | # define shell_read_history(X) read_history(X) |
| 105 | # define shell_write_history(X) write_history(X) |
| 106 | # define shell_stifle_history(X) stifle_history(X) |
| 107 | # define shell_readline(X) readline(X) |
| 108 | |
| 109 | #elif HAVE_LINENOISE |
| 110 | |
| 111 | # include "linenoise.h" |
| 112 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 113 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 114 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 115 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 116 | # define shell_readline(X) linenoise(X) |
| 117 | |
| 118 | #else |
| 119 | |
| 120 | # define shell_read_history(X) |
| 121 | # define shell_write_history(X) |
| 122 | # define shell_stifle_history(X) |
| 123 | |
| 124 | # define SHELL_USE_LOCAL_GETLINE 1 |
| 125 | #endif |
| 126 | |
| 127 | |
| 128 | #if defined(_WIN32) || defined(WIN32) |
| 129 | # include <io.h> |
| 130 | # include <fcntl.h> |
| 131 | # define isatty(h) _isatty(h) |
| 132 | # ifndef access |
| 133 | # define access(f,m) _access((f),(m)) |
| 134 | # endif |
| 135 | # undef popen |
| 136 | # define popen _popen |
| 137 | # undef pclose |
| 138 | # define pclose _pclose |
| 139 | #else |
| 140 | /* Make sure isatty() has a prototype. */ |
| 141 | extern int isatty(int); |
| 142 | |
| 143 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 144 | /* popen and pclose are not C89 functions and so are |
| 145 | ** sometimes omitted from the <stdio.h> header */ |
| 146 | extern FILE *popen(const char*,const char*); |
| 147 | extern int pclose(FILE*); |
| 148 | # else |
| 149 | # define SQLITE_OMIT_POPEN 1 |
| 150 | # endif |
| 151 | #endif |
| 152 | |
| 153 | #if defined(_WIN32_WCE) |
| 154 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 155 | * thus we always assume that we have a console. That can be |
| 156 | * overridden with the -batch command line option. |
| 157 | */ |
| 158 | #define isatty(x) 1 |
| 159 | #endif |
| 160 | |
| 161 | /* ctype macros that work with signed characters */ |
| 162 | #define IsSpace(X) isspace((unsigned char)X) |
| 163 | #define IsDigit(X) isdigit((unsigned char)X) |
| 164 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 165 | |
| 166 | #if defined(_WIN32) || defined(WIN32) |
| 167 | #include <windows.h> |
| 168 | |
| 169 | /* string conversion routines only needed on Win32 */ |
| 170 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 171 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 172 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| 173 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 174 | #endif |
| 175 | |
| 176 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 177 | ** are automatically translated into \r\n. However, this behavior needs |
| 178 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 179 | ** rendering quoted strings that contain \n characters). The following |
| 180 | ** routines take care of that. |
| 181 | */ |
| 182 | #if defined(_WIN32) || defined(WIN32) |
| 183 | static void setBinaryMode(FILE *file, int isOutput){ |
| 184 | if( isOutput ) fflush(file); |
| 185 | _setmode(_fileno(file), _O_BINARY); |
| 186 | } |
| 187 | static void setTextMode(FILE *file, int isOutput){ |
| 188 | if( isOutput ) fflush(file); |
| 189 | _setmode(_fileno(file), _O_TEXT); |
| 190 | } |
| 191 | #else |
| 192 | # define setBinaryMode(X,Y) |
| 193 | # define setTextMode(X,Y) |
| 194 | #endif |
| 195 | |
| 196 | |
| 197 | /* True if the timer is enabled */ |
| 198 | static int enableTimer = 0; |
| 199 | |
| 200 | /* Return the current wall-clock time */ |
| 201 | static sqlite3_int64 timeOfDay(void){ |
| 202 | static sqlite3_vfs *clockVfs = 0; |
| 203 | sqlite3_int64 t; |
| 204 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 205 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 206 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 207 | }else{ |
| 208 | double r; |
| 209 | clockVfs->xCurrentTime(clockVfs, &r); |
| 210 | t = (sqlite3_int64)(r*86400000.0); |
| 211 | } |
| 212 | return t; |
| 213 | } |
| 214 | |
| 215 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
| 216 | #include <sys/time.h> |
| 217 | #include <sys/resource.h> |
| 218 | |
| 219 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 220 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 221 | struct rusage { |
| 222 | struct timeval ru_utime; /* user CPU time used */ |
| 223 | struct timeval ru_stime; /* system CPU time used */ |
| 224 | }; |
| 225 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 226 | #endif |
| 227 | |
| 228 | /* Saved resource information for the beginning of an operation */ |
| 229 | static struct rusage sBegin; /* CPU time at start */ |
| 230 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 231 | |
| 232 | /* |
| 233 | ** Begin timing an operation |
| 234 | */ |
| 235 | static void beginTimer(void){ |
| 236 | if( enableTimer ){ |
| 237 | getrusage(RUSAGE_SELF, &sBegin); |
| 238 | iBegin = timeOfDay(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* Return the difference of two time_structs in seconds */ |
| 243 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 244 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 245 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ** Print the timing results. |
| 250 | */ |
| 251 | static void endTimer(void){ |
| 252 | if( enableTimer ){ |
| 253 | sqlite3_int64 iEnd = timeOfDay(); |
| 254 | struct rusage sEnd; |
| 255 | getrusage(RUSAGE_SELF, &sEnd); |
| 256 | printf("Run Time: real %.3f user %f sys %f\n", |
| 257 | (iEnd - iBegin)*0.001, |
| 258 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 259 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #define BEGIN_TIMER beginTimer() |
| 264 | #define END_TIMER endTimer() |
| 265 | #define HAS_TIMER 1 |
| 266 | |
| 267 | #elif (defined(_WIN32) || defined(WIN32)) |
| 268 | |
| 269 | /* Saved resource information for the beginning of an operation */ |
| 270 | static HANDLE hProcess; |
| 271 | static FILETIME ftKernelBegin; |
| 272 | static FILETIME ftUserBegin; |
| 273 | static sqlite3_int64 ftWallBegin; |
| 274 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 275 | LPFILETIME, LPFILETIME); |
| 276 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 277 | |
| 278 | /* |
| 279 | ** Check to see if we have timer support. Return 1 if necessary |
| 280 | ** support found (or found previously). |
| 281 | */ |
| 282 | static int hasTimer(void){ |
| 283 | if( getProcessTimesAddr ){ |
| 284 | return 1; |
| 285 | } else { |
| 286 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 287 | ** versions. See if the version we are running on has it, and if it |
| 288 | ** does, save off a pointer to it and the current process handle. |
| 289 | */ |
| 290 | hProcess = GetCurrentProcess(); |
| 291 | if( hProcess ){ |
| 292 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 293 | if( NULL != hinstLib ){ |
| 294 | getProcessTimesAddr = |
| 295 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 296 | if( NULL != getProcessTimesAddr ){ |
| 297 | return 1; |
| 298 | } |
| 299 | FreeLibrary(hinstLib); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | ** Begin timing an operation |
| 308 | */ |
| 309 | static void beginTimer(void){ |
| 310 | if( enableTimer && getProcessTimesAddr ){ |
| 311 | FILETIME ftCreation, ftExit; |
| 312 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 313 | &ftKernelBegin,&ftUserBegin); |
| 314 | ftWallBegin = timeOfDay(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* Return the difference of two FILETIME structs in seconds */ |
| 319 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 320 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 321 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 322 | return (double) ((i64End - i64Start) / 10000000.0); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | ** Print the timing results. |
| 327 | */ |
| 328 | static void endTimer(void){ |
| 329 | if( enableTimer && getProcessTimesAddr){ |
| 330 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 331 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 332 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 333 | printf("Run Time: real %.3f user %f sys %f\n", |
| 334 | (ftWallEnd - ftWallBegin)*0.001, |
| 335 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 336 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | #define BEGIN_TIMER beginTimer() |
| 341 | #define END_TIMER endTimer() |
| 342 | #define HAS_TIMER hasTimer() |
| 343 | |
| 344 | #else |
| 345 | #define BEGIN_TIMER |
| 346 | #define END_TIMER |
| 347 | #define HAS_TIMER 0 |
| 348 | #endif |
| 349 | |
| 350 | /* |
| 351 | ** Used to prevent warnings about unused parameters |
| 352 | */ |
| 353 | #define UNUSED_PARAMETER(x) (void)(x) |
| 354 | |
| 355 | /* |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 356 | ** Number of elements in an array |
| 357 | */ |
| 358 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
| 359 | |
| 360 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 361 | ** If the following flag is set, then command execution stops |
| 362 | ** at an error if we are not interactive. |
| 363 | */ |
| 364 | static int bail_on_error = 0; |
| 365 | |
| 366 | /* |
| 367 | ** Threat stdin as an interactive input if the following variable |
| 368 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 369 | */ |
| 370 | static int stdin_is_interactive = 1; |
| 371 | |
| 372 | /* |
| 373 | ** On Windows systems we have to know if standard output is a console |
| 374 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 375 | ** true if translation is required. |
| 376 | */ |
| 377 | static int stdout_is_console = 1; |
| 378 | |
| 379 | /* |
| 380 | ** The following is the open SQLite database. We make a pointer |
| 381 | ** to this database a static variable so that it can be accessed |
| 382 | ** by the SIGINT handler to interrupt database processing. |
| 383 | */ |
| 384 | static sqlite3 *globalDb = 0; |
| 385 | |
| 386 | /* |
| 387 | ** True if an interrupt (Control-C) has been received. |
| 388 | */ |
| 389 | static volatile int seenInterrupt = 0; |
| 390 | |
| 391 | /* |
| 392 | ** This is the name of our program. It is set in main(), used |
| 393 | ** in a number of other places, mostly for error messages. |
| 394 | */ |
| 395 | static char *Argv0; |
| 396 | |
| 397 | /* |
| 398 | ** Prompt strings. Initialized in main. Settable with |
| 399 | ** .prompt main continue |
| 400 | */ |
| 401 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 402 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 403 | |
| 404 | /* |
| 405 | ** Render output like fprintf(). Except, if the output is going to the |
| 406 | ** console and if this is running on a Windows machine, translate the |
| 407 | ** output from UTF-8 into MBCS. |
| 408 | */ |
| 409 | #if defined(_WIN32) || defined(WIN32) |
| 410 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 411 | va_list ap; |
| 412 | va_start(ap, zFormat); |
| 413 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
| 414 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
| 415 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
| 416 | sqlite3_free(z1); |
| 417 | fputs(z2, out); |
| 418 | sqlite3_free(z2); |
| 419 | }else{ |
| 420 | vfprintf(out, zFormat, ap); |
| 421 | } |
| 422 | va_end(ap); |
| 423 | } |
| 424 | #elif !defined(utf8_printf) |
| 425 | # define utf8_printf fprintf |
| 426 | #endif |
| 427 | |
| 428 | /* |
| 429 | ** Render output like fprintf(). This should not be used on anything that |
| 430 | ** includes string formatting (e.g. "%s"). |
| 431 | */ |
| 432 | #if !defined(raw_printf) |
| 433 | # define raw_printf fprintf |
| 434 | #endif |
| 435 | |
| 436 | /* |
| 437 | ** Write I/O traces to the following stream. |
| 438 | */ |
| 439 | #ifdef SQLITE_ENABLE_IOTRACE |
| 440 | static FILE *iotrace = 0; |
| 441 | #endif |
| 442 | |
| 443 | /* |
| 444 | ** This routine works like printf in that its first argument is a |
| 445 | ** format string and subsequent arguments are values to be substituted |
| 446 | ** in place of % fields. The result of formatting this string |
| 447 | ** is written to iotrace. |
| 448 | */ |
| 449 | #ifdef SQLITE_ENABLE_IOTRACE |
| 450 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
| 451 | va_list ap; |
| 452 | char *z; |
| 453 | if( iotrace==0 ) return; |
| 454 | va_start(ap, zFormat); |
| 455 | z = sqlite3_vmprintf(zFormat, ap); |
| 456 | va_end(ap); |
| 457 | utf8_printf(iotrace, "%s", z); |
| 458 | sqlite3_free(z); |
| 459 | } |
| 460 | #endif |
| 461 | |
| 462 | /* |
| 463 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 464 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 465 | ** in bytes. This is different from the %*.*s specification in printf |
| 466 | ** since with %*.*s the width is measured in bytes, not characters. |
| 467 | */ |
| 468 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 469 | int i; |
| 470 | int n; |
| 471 | int aw = w<0 ? -w : w; |
| 472 | char zBuf[1000]; |
| 473 | if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; |
| 474 | for(i=n=0; zUtf[i]; i++){ |
| 475 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 476 | n++; |
| 477 | if( n==aw ){ |
| 478 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | if( n>=aw ){ |
| 484 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 485 | }else if( w<0 ){ |
| 486 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 487 | }else{ |
| 488 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /* |
| 494 | ** Determines if a string is a number of not. |
| 495 | */ |
| 496 | static int isNumber(const char *z, int *realnum){ |
| 497 | if( *z=='-' || *z=='+' ) z++; |
| 498 | if( !IsDigit(*z) ){ |
| 499 | return 0; |
| 500 | } |
| 501 | z++; |
| 502 | if( realnum ) *realnum = 0; |
| 503 | while( IsDigit(*z) ){ z++; } |
| 504 | if( *z=='.' ){ |
| 505 | z++; |
| 506 | if( !IsDigit(*z) ) return 0; |
| 507 | while( IsDigit(*z) ){ z++; } |
| 508 | if( realnum ) *realnum = 1; |
| 509 | } |
| 510 | if( *z=='e' || *z=='E' ){ |
| 511 | z++; |
| 512 | if( *z=='+' || *z=='-' ) z++; |
| 513 | if( !IsDigit(*z) ) return 0; |
| 514 | while( IsDigit(*z) ){ z++; } |
| 515 | if( realnum ) *realnum = 1; |
| 516 | } |
| 517 | return *z==0; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | ** Compute a string length that is limited to what can be stored in |
| 522 | ** lower 30 bits of a 32-bit signed integer. |
| 523 | */ |
| 524 | static int strlen30(const char *z){ |
| 525 | const char *z2 = z; |
| 526 | while( *z2 ){ z2++; } |
| 527 | return 0x3fffffff & (int)(z2 - z); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 532 | ** count as a single character. |
| 533 | */ |
| 534 | static int strlenChar(const char *z){ |
| 535 | int n = 0; |
| 536 | while( *z ){ |
| 537 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 538 | } |
| 539 | return n; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | ** This routine reads a line of text from FILE in, stores |
| 544 | ** the text in memory obtained from malloc() and returns a pointer |
| 545 | ** to the text. NULL is returned at end of file, or if malloc() |
| 546 | ** fails. |
| 547 | ** |
| 548 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 549 | ** a previous call to this routine that may be reused. |
| 550 | */ |
| 551 | static char *local_getline(char *zLine, FILE *in){ |
| 552 | int nLine = zLine==0 ? 0 : 100; |
| 553 | int n = 0; |
| 554 | |
| 555 | while( 1 ){ |
| 556 | if( n+100>nLine ){ |
| 557 | nLine = nLine*2 + 100; |
| 558 | zLine = realloc(zLine, nLine); |
| 559 | if( zLine==0 ) return 0; |
| 560 | } |
| 561 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 562 | if( n==0 ){ |
| 563 | free(zLine); |
| 564 | return 0; |
| 565 | } |
| 566 | zLine[n] = 0; |
| 567 | break; |
| 568 | } |
| 569 | while( zLine[n] ) n++; |
| 570 | if( n>0 && zLine[n-1]=='\n' ){ |
| 571 | n--; |
| 572 | if( n>0 && zLine[n-1]=='\r' ) n--; |
| 573 | zLine[n] = 0; |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | #if defined(_WIN32) || defined(WIN32) |
| 578 | /* For interactive input on Windows systems, translate the |
| 579 | ** multi-byte characterset characters into UTF-8. */ |
| 580 | if( stdin_is_interactive && in==stdin ){ |
| 581 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| 582 | if( zTrans ){ |
| 583 | int nTrans = strlen30(zTrans)+1; |
| 584 | if( nTrans>nLine ){ |
| 585 | zLine = realloc(zLine, nTrans); |
| 586 | if( zLine==0 ){ |
| 587 | sqlite3_free(zTrans); |
| 588 | return 0; |
| 589 | } |
| 590 | } |
| 591 | memcpy(zLine, zTrans, nTrans); |
| 592 | sqlite3_free(zTrans); |
| 593 | } |
| 594 | } |
| 595 | #endif /* defined(_WIN32) || defined(WIN32) */ |
| 596 | return zLine; |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | ** Retrieve a single line of input text. |
| 601 | ** |
| 602 | ** If in==0 then read from standard input and prompt before each line. |
| 603 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 604 | ** If isContinuation is zero, then the main prompt should be used. |
| 605 | ** |
| 606 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 607 | ** routine that can be reused. |
| 608 | ** |
| 609 | ** The result is stored in space obtained from malloc() and must either |
| 610 | ** be freed by the caller or else passed back into this routine via the |
| 611 | ** zPrior argument for reuse. |
| 612 | */ |
| 613 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 614 | char *zPrompt; |
| 615 | char *zResult; |
| 616 | if( in!=0 ){ |
| 617 | zResult = local_getline(zPrior, in); |
| 618 | }else{ |
| 619 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
| 620 | #if SHELL_USE_LOCAL_GETLINE |
| 621 | printf("%s", zPrompt); |
| 622 | fflush(stdout); |
| 623 | zResult = local_getline(zPrior, stdin); |
| 624 | #else |
| 625 | free(zPrior); |
| 626 | zResult = shell_readline(zPrompt); |
| 627 | if( zResult && *zResult ) shell_add_history(zResult); |
| 628 | #endif |
| 629 | } |
| 630 | return zResult; |
| 631 | } |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 632 | |
| 633 | |
| 634 | /* |
| 635 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 636 | ** is not a hex digit. |
| 637 | */ |
| 638 | static int hexDigitValue(char c){ |
| 639 | if( c>='0' && c<='9' ) return c - '0'; |
| 640 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 641 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 647 | */ |
| 648 | static sqlite3_int64 integerValue(const char *zArg){ |
| 649 | sqlite3_int64 v = 0; |
| 650 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 651 | { "KiB", 1024 }, |
| 652 | { "MiB", 1024*1024 }, |
| 653 | { "GiB", 1024*1024*1024 }, |
| 654 | { "KB", 1000 }, |
| 655 | { "MB", 1000000 }, |
| 656 | { "GB", 1000000000 }, |
| 657 | { "K", 1000 }, |
| 658 | { "M", 1000000 }, |
| 659 | { "G", 1000000000 }, |
| 660 | }; |
| 661 | int i; |
| 662 | int isNeg = 0; |
| 663 | if( zArg[0]=='-' ){ |
| 664 | isNeg = 1; |
| 665 | zArg++; |
| 666 | }else if( zArg[0]=='+' ){ |
| 667 | zArg++; |
| 668 | } |
| 669 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 670 | int x; |
| 671 | zArg += 2; |
| 672 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 673 | v = (v<<4) + x; |
| 674 | zArg++; |
| 675 | } |
| 676 | }else{ |
| 677 | while( IsDigit(zArg[0]) ){ |
| 678 | v = v*10 + zArg[0] - '0'; |
| 679 | zArg++; |
| 680 | } |
| 681 | } |
| 682 | for(i=0; i<ArraySize(aMult); i++){ |
| 683 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 684 | v *= aMult[i].iMult; |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | return isNeg? -v : v; |
| 689 | } |
| 690 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 691 | /* |
| 692 | ** A variable length string to which one can append text. |
| 693 | */ |
| 694 | typedef struct ShellText ShellText; |
| 695 | struct ShellText { |
| 696 | char *z; |
| 697 | int n; |
| 698 | int nAlloc; |
| 699 | }; |
| 700 | |
| 701 | /* |
| 702 | ** Initialize and destroy a ShellText object |
| 703 | */ |
| 704 | static void initText(ShellText *p){ |
| 705 | memset(p, 0, sizeof(*p)); |
| 706 | } |
| 707 | static void freeText(ShellText *p){ |
| 708 | free(p->z); |
| 709 | initText(p); |
| 710 | } |
| 711 | |
| 712 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 713 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 714 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 715 | ** zIn, if it was not NULL, is freed. |
| 716 | ** |
| 717 | ** If the third argument, quote, is not '\0', then it is used as a |
| 718 | ** quote character for zAppend. |
| 719 | */ |
| 720 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 721 | int len; |
| 722 | int i; |
| 723 | int nAppend = strlen30(zAppend); |
| 724 | |
| 725 | len = nAppend+p->n+1; |
| 726 | if( quote ){ |
| 727 | len += 2; |
| 728 | for(i=0; i<nAppend; i++){ |
| 729 | if( zAppend[i]==quote ) len++; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if( p->n+len>=p->nAlloc ){ |
| 734 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 735 | p->z = realloc(p->z, p->nAlloc); |
| 736 | if( p->z==0 ){ |
| 737 | memset(p, 0, sizeof(*p)); |
| 738 | return; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if( quote ){ |
| 743 | char *zCsr = p->z+p->n; |
| 744 | *zCsr++ = quote; |
| 745 | for(i=0; i<nAppend; i++){ |
| 746 | *zCsr++ = zAppend[i]; |
| 747 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 748 | } |
| 749 | *zCsr++ = quote; |
| 750 | p->n = (int)(zCsr - p->z); |
| 751 | *zCsr = '\0'; |
| 752 | }else{ |
| 753 | memcpy(p->z+p->n, zAppend, nAppend); |
| 754 | p->n += nAppend; |
| 755 | p->z[p->n] = '\0'; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 761 | ** because it contains non-alphanumeric characters, or because it is an |
| 762 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 763 | ** that quoting is required. |
| 764 | ** |
| 765 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 766 | */ |
| 767 | static char quoteChar(const char *zName){ |
| 768 | /* All SQLite keywords, in alphabetical order */ |
| 769 | static const char *azKeywords[] = { |
| 770 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 771 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 772 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 773 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 774 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 775 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 776 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 777 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 778 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 779 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 780 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 781 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 782 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 783 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 784 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 785 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 786 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 787 | "WITH", "WITHOUT", |
| 788 | }; |
| 789 | int i, lwr, upr, mid, c; |
| 790 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 791 | for(i=0; zName[i]; i++){ |
| 792 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 793 | } |
| 794 | lwr = 0; |
| 795 | upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; |
| 796 | while( lwr<=upr ){ |
| 797 | mid = (lwr+upr)/2; |
| 798 | c = sqlite3_stricmp(azKeywords[mid], zName); |
| 799 | if( c==0 ) return '"'; |
| 800 | if( c<0 ){ |
| 801 | lwr = mid+1; |
| 802 | }else{ |
| 803 | upr = mid-1; |
| 804 | } |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 810 | ** Construct a fake object name and column list to describe the structure |
| 811 | ** of the view, virtual table, or table valued function zSchema.zName. |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 812 | */ |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 813 | static char *shellFakeSchema( |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 814 | sqlite3 *db, /* The database connection containing the vtab */ |
| 815 | const char *zSchema, /* Schema of the database holding the vtab */ |
| 816 | const char *zName /* The name of the virtual table */ |
| 817 | ){ |
| 818 | sqlite3_stmt *pStmt = 0; |
| 819 | char *zSql; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 820 | ShellText s; |
| 821 | char cQuote; |
| 822 | char *zDiv = "("; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 823 | int nRow = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 824 | |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 825 | zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", |
| 826 | zSchema ? zSchema : "main", zName); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 827 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 828 | sqlite3_free(zSql); |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 829 | initText(&s); |
| 830 | if( zSchema ){ |
| 831 | cQuote = quoteChar(zSchema); |
| 832 | if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; |
| 833 | appendText(&s, zSchema, cQuote); |
| 834 | appendText(&s, ".", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 835 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 836 | cQuote = quoteChar(zName); |
| 837 | appendText(&s, zName, cQuote); |
| 838 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 839 | const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 840 | nRow++; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 841 | appendText(&s, zDiv, 0); |
| 842 | zDiv = ","; |
| 843 | cQuote = quoteChar(zCol); |
| 844 | appendText(&s, zCol, cQuote); |
| 845 | } |
| 846 | appendText(&s, ")", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 847 | sqlite3_finalize(pStmt); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 848 | if( nRow==0 ){ |
| 849 | freeText(&s); |
| 850 | s.z = 0; |
| 851 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 852 | return s.z; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 856 | ** SQL function: shell_module_schema(X) |
| 857 | ** |
| 858 | ** Return a fake schema for the table-valued function or eponymous virtual |
| 859 | ** table X. |
| 860 | */ |
| 861 | static void shellModuleSchema( |
| 862 | sqlite3_context *pCtx, |
| 863 | int nVal, |
| 864 | sqlite3_value **apVal |
| 865 | ){ |
| 866 | const char *zName = (const char*)sqlite3_value_text(apVal[0]); |
| 867 | char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 868 | UNUSED_PARAMETER(nVal); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 869 | if( zFake ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 870 | sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 871 | -1, sqlite3_free); |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 872 | free(zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 877 | ** SQL function: shell_add_schema(S,X) |
| 878 | ** |
| 879 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 880 | ** Examples: |
| 881 | ** |
| 882 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 883 | ** |
| 884 | ** Also works on |
| 885 | ** |
| 886 | ** CREATE INDEX |
| 887 | ** CREATE UNIQUE INDEX |
| 888 | ** CREATE VIEW |
| 889 | ** CREATE TRIGGER |
| 890 | ** CREATE VIRTUAL TABLE |
| 891 | ** |
| 892 | ** This UDF is used by the .schema command to insert the schema name of |
| 893 | ** attached databases into the middle of the sqlite_master.sql field. |
| 894 | */ |
| 895 | static void shellAddSchemaName( |
| 896 | sqlite3_context *pCtx, |
| 897 | int nVal, |
| 898 | sqlite3_value **apVal |
| 899 | ){ |
| 900 | static const char *aPrefix[] = { |
| 901 | "TABLE", |
| 902 | "INDEX", |
| 903 | "UNIQUE INDEX", |
| 904 | "VIEW", |
| 905 | "TRIGGER", |
| 906 | "VIRTUAL TABLE" |
| 907 | }; |
| 908 | int i = 0; |
| 909 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 910 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 911 | const char *zName = (const char*)sqlite3_value_text(apVal[2]); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 912 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 913 | UNUSED_PARAMETER(nVal); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 914 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 915 | for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 916 | int n = strlen30(aPrefix[i]); |
| 917 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 918 | char *z = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 919 | char *zFake = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 920 | if( zSchema ){ |
| 921 | char cQuote = quoteChar(zSchema); |
| 922 | if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ |
| 923 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 924 | }else{ |
| 925 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 926 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 927 | } |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 928 | if( zName |
| 929 | && aPrefix[i][0]=='V' |
| 930 | && (zFake = shellFakeSchema(db, zSchema, zName))!=0 |
| 931 | ){ |
| 932 | if( z==0 ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 933 | z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 934 | }else{ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 935 | z = sqlite3_mprintf("%z\n/* %s */", z, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 936 | } |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 937 | free(zFake); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 938 | } |
| 939 | if( z ){ |
| 940 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 941 | return; |
| 942 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
| 946 | sqlite3_result_value(pCtx, apVal[0]); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | ** The source code for several run-time loadable extensions is inserted |
| 951 | ** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 952 | ** code, we need to override some macros to make the included program code |
| 953 | ** work here in the middle of this regular program. |
| 954 | */ |
| 955 | #define SQLITE_EXTENSION_INIT1 |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 956 | #define SQLITE_EXTENSION_INIT2(X) (void)(X) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 957 | |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 958 | #if defined(_WIN32) && defined(_MSC_VER) |
drh | 03491a1 | 2018-01-07 21:58:17 +0000 | [diff] [blame] | 959 | INCLUDE test_windirent.h |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 960 | INCLUDE test_windirent.c |
| 961 | #define dirent DIRENT |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 962 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 963 | INCLUDE ../ext/misc/shathree.c |
| 964 | INCLUDE ../ext/misc/fileio.c |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 965 | INCLUDE ../ext/misc/completion.c |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 966 | INCLUDE ../ext/misc/appendvfs.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 967 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 968 | INCLUDE ../ext/misc/zipfile.c |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 969 | INCLUDE ../ext/misc/sqlar.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 970 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 971 | INCLUDE ../ext/expert/sqlite3expert.h |
| 972 | INCLUDE ../ext/expert/sqlite3expert.c |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 973 | |
| 974 | #if defined(SQLITE_ENABLE_SESSION) |
| 975 | /* |
| 976 | ** State information for a single open session |
| 977 | */ |
| 978 | typedef struct OpenSession OpenSession; |
| 979 | struct OpenSession { |
| 980 | char *zName; /* Symbolic name for this session */ |
| 981 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 982 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 983 | sqlite3_session *p; /* The open session */ |
| 984 | }; |
| 985 | #endif |
| 986 | |
| 987 | /* |
| 988 | ** Shell output mode information from before ".explain on", |
| 989 | ** saved so that it can be restored by ".explain off" |
| 990 | */ |
| 991 | typedef struct SavedModeInfo SavedModeInfo; |
| 992 | struct SavedModeInfo { |
| 993 | int valid; /* Is there legit data in here? */ |
| 994 | int mode; /* Mode prior to ".explain on" */ |
| 995 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 996 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
| 997 | }; |
| 998 | |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 999 | typedef struct ExpertInfo ExpertInfo; |
| 1000 | struct ExpertInfo { |
| 1001 | sqlite3expert *pExpert; |
| 1002 | int bVerbose; |
| 1003 | }; |
| 1004 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1005 | /* |
| 1006 | ** State information about the database connection is contained in an |
| 1007 | ** instance of the following structure. |
| 1008 | */ |
| 1009 | typedef struct ShellState ShellState; |
| 1010 | struct ShellState { |
| 1011 | sqlite3 *db; /* The database */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1012 | u8 autoExplain; /* Automatically turn on .explain mode */ |
| 1013 | u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
| 1014 | u8 statsOn; /* True to display memory stats before each finalize */ |
| 1015 | u8 scanstatsOn; /* True to display scan stats before each finalize */ |
| 1016 | u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1017 | u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1018 | int outCount; /* Revert to stdout when reaching zero */ |
| 1019 | int cnt; /* Number of records displayed so far */ |
| 1020 | FILE *out; /* Write results here */ |
| 1021 | FILE *traceOut; /* Output for sqlite3_trace() */ |
| 1022 | int nErr; /* Number of errors seen */ |
| 1023 | int mode; /* An output mode setting */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1024 | int modePrior; /* Saved mode */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1025 | int cMode; /* temporary output mode for the current query */ |
| 1026 | int normalMode; /* Output mode before ".explain on" */ |
| 1027 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
| 1028 | int showHeader; /* True to show column names in List or Column mode */ |
| 1029 | int nCheck; /* Number of ".check" commands run */ |
| 1030 | unsigned shellFlgs; /* Various flags */ |
| 1031 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1032 | char *zTempFile; /* Temporary file that might need deleting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1033 | char zTestcase[30]; /* Name of current test case */ |
| 1034 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1035 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1036 | char colSepPrior[20]; /* Saved column separator */ |
| 1037 | char rowSepPrior[20]; /* Saved row separator */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1038 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1039 | int actualWidth[100]; /* Actual width of each column */ |
| 1040 | char nullValue[20]; /* The text to print when a NULL comes back from |
| 1041 | ** the database */ |
| 1042 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1043 | const char *zDbFilename; /* name of the database file */ |
| 1044 | char *zFreeOnClose; /* Filename to free when closing */ |
| 1045 | const char *zVfs; /* Name of VFS to use */ |
| 1046 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
| 1047 | FILE *pLog; /* Write log output here */ |
| 1048 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1049 | int nIndent; /* Size of array aiIndent[] */ |
| 1050 | int iIndent; /* Index of current op in aiIndent[] */ |
| 1051 | #if defined(SQLITE_ENABLE_SESSION) |
| 1052 | int nSession; /* Number of active sessions */ |
| 1053 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1054 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 1055 | ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1056 | }; |
| 1057 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1058 | |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 1059 | /* Allowed values for ShellState.autoEQP |
| 1060 | */ |
| 1061 | #define AUTOEQP_off 0 |
| 1062 | #define AUTOEQP_on 1 |
| 1063 | #define AUTOEQP_trigger 2 |
| 1064 | #define AUTOEQP_full 3 |
| 1065 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1066 | /* Allowed values for ShellState.openMode |
| 1067 | */ |
| 1068 | #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ |
| 1069 | #define SHELL_OPEN_NORMAL 1 /* Normal database file */ |
| 1070 | #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ |
| 1071 | #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ |
| 1072 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | ** These are the allowed shellFlgs values |
| 1075 | */ |
drh | b2a0f75 | 2017-08-28 15:51:35 +0000 | [diff] [blame] | 1076 | #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ |
| 1077 | #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ |
| 1078 | #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ |
| 1079 | #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ |
| 1080 | #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ |
| 1081 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1082 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1083 | |
| 1084 | /* |
| 1085 | ** Macros for testing and setting shellFlgs |
| 1086 | */ |
| 1087 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1088 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1089 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
| 1090 | |
| 1091 | /* |
| 1092 | ** These are the allowed modes. |
| 1093 | */ |
| 1094 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
| 1095 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1096 | #define MODE_List 2 /* One record per line with a separator */ |
| 1097 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1098 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1099 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
| 1100 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1101 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1102 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1103 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1104 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1105 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
| 1106 | |
| 1107 | static const char *modeDescr[] = { |
| 1108 | "line", |
| 1109 | "column", |
| 1110 | "list", |
| 1111 | "semi", |
| 1112 | "html", |
| 1113 | "insert", |
| 1114 | "quote", |
| 1115 | "tcl", |
| 1116 | "csv", |
| 1117 | "explain", |
| 1118 | "ascii", |
| 1119 | "prettyprint", |
| 1120 | }; |
| 1121 | |
| 1122 | /* |
| 1123 | ** These are the column/row/line separators used by the various |
| 1124 | ** import/export modes. |
| 1125 | */ |
| 1126 | #define SEP_Column "|" |
| 1127 | #define SEP_Row "\n" |
| 1128 | #define SEP_Tab "\t" |
| 1129 | #define SEP_Space " " |
| 1130 | #define SEP_Comma "," |
| 1131 | #define SEP_CrLf "\r\n" |
| 1132 | #define SEP_Unit "\x1F" |
| 1133 | #define SEP_Record "\x1E" |
| 1134 | |
| 1135 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1136 | ** A callback for the sqlite3_log() interface. |
| 1137 | */ |
| 1138 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 1139 | ShellState *p = (ShellState*)pArg; |
| 1140 | if( p->pLog==0 ) return; |
| 1141 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 1142 | fflush(p->pLog); |
| 1143 | } |
| 1144 | |
| 1145 | /* |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1146 | ** SQL function: shell_putsnl(X) |
| 1147 | ** |
| 1148 | ** Write the text X to the screen (or whatever output is being directed) |
| 1149 | ** adding a newline at the end, and then return X. |
| 1150 | */ |
| 1151 | static void shellPutsFunc( |
| 1152 | sqlite3_context *pCtx, |
| 1153 | int nVal, |
| 1154 | sqlite3_value **apVal |
| 1155 | ){ |
| 1156 | ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 1157 | (void)nVal; |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1158 | utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
| 1159 | sqlite3_result_value(pCtx, apVal[0]); |
| 1160 | } |
| 1161 | |
| 1162 | /* |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1163 | ** SQL function: edit(VALUE) |
| 1164 | ** edit(VALUE,EDITOR) |
| 1165 | ** |
| 1166 | ** These steps: |
| 1167 | ** |
| 1168 | ** (1) Write VALUE into a temporary file. |
| 1169 | ** (2) Run program EDITOR on that temporary file. |
| 1170 | ** (3) Read the temporary file back and return its content as the result. |
| 1171 | ** (4) Delete the temporary file |
| 1172 | ** |
| 1173 | ** If the EDITOR argument is omitted, use the value in the VISUAL |
| 1174 | ** environment variable. If still there is no EDITOR, through an error. |
| 1175 | ** |
| 1176 | ** Also throw an error if the EDITOR program returns a non-zero exit code. |
| 1177 | */ |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1178 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1179 | static void editFunc( |
| 1180 | sqlite3_context *context, |
| 1181 | int argc, |
| 1182 | sqlite3_value **argv |
| 1183 | ){ |
| 1184 | const char *zEditor; |
| 1185 | char *zTempFile = 0; |
| 1186 | sqlite3 *db; |
| 1187 | char *zCmd = 0; |
| 1188 | int bBin; |
| 1189 | int rc; |
| 1190 | FILE *f = 0; |
| 1191 | sqlite3_int64 sz; |
| 1192 | sqlite3_int64 x; |
| 1193 | unsigned char *p = 0; |
| 1194 | |
| 1195 | if( argc==2 ){ |
| 1196 | zEditor = (const char*)sqlite3_value_text(argv[1]); |
| 1197 | }else{ |
| 1198 | zEditor = getenv("VISUAL"); |
| 1199 | } |
| 1200 | if( zEditor==0 ){ |
| 1201 | sqlite3_result_error(context, "no editor for edit()", -1); |
| 1202 | return; |
| 1203 | } |
| 1204 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1205 | sqlite3_result_error(context, "NULL input to edit()", -1); |
| 1206 | return; |
| 1207 | } |
| 1208 | db = sqlite3_context_db_handle(context); |
| 1209 | zTempFile = 0; |
| 1210 | sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); |
| 1211 | if( zTempFile==0 ){ |
| 1212 | sqlite3_uint64 r = 0; |
| 1213 | sqlite3_randomness(sizeof(r), &r); |
| 1214 | zTempFile = sqlite3_mprintf("temp%llx", r); |
| 1215 | if( zTempFile==0 ){ |
| 1216 | sqlite3_result_error_nomem(context); |
| 1217 | return; |
| 1218 | } |
| 1219 | } |
| 1220 | bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
| 1221 | f = fopen(zTempFile, bBin ? "wb" : "w"); |
| 1222 | if( f==0 ){ |
| 1223 | sqlite3_result_error(context, "edit() cannot open temp file", -1); |
| 1224 | goto edit_func_end; |
| 1225 | } |
| 1226 | sz = sqlite3_value_bytes(argv[0]); |
| 1227 | if( bBin ){ |
| 1228 | x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f); |
| 1229 | }else{ |
| 1230 | x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f); |
| 1231 | } |
| 1232 | fclose(f); |
| 1233 | f = 0; |
| 1234 | if( x!=sz ){ |
| 1235 | sqlite3_result_error(context, "edit() could not write the whole file", -1); |
| 1236 | goto edit_func_end; |
| 1237 | } |
| 1238 | zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); |
| 1239 | if( zCmd==0 ){ |
| 1240 | sqlite3_result_error_nomem(context); |
| 1241 | goto edit_func_end; |
| 1242 | } |
| 1243 | rc = system(zCmd); |
| 1244 | sqlite3_free(zCmd); |
| 1245 | if( rc ){ |
| 1246 | sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
| 1247 | goto edit_func_end; |
| 1248 | } |
| 1249 | f = fopen(zTempFile, bBin ? "rb" : "r"); |
| 1250 | if( f==0 ){ |
| 1251 | sqlite3_result_error(context, |
| 1252 | "edit() cannot reopen temp file after edit", -1); |
| 1253 | goto edit_func_end; |
| 1254 | } |
| 1255 | fseek(f, 0, SEEK_END); |
| 1256 | sz = ftell(f); |
| 1257 | rewind(f); |
| 1258 | p = sqlite3_malloc64( sz+(bBin==0) ); |
| 1259 | if( p==0 ){ |
| 1260 | sqlite3_result_error_nomem(context); |
| 1261 | goto edit_func_end; |
| 1262 | } |
| 1263 | if( bBin ){ |
| 1264 | x = fread(p, 1, sz, f); |
| 1265 | }else{ |
| 1266 | x = fread(p, 1, sz, f); |
| 1267 | p[sz] = 0; |
| 1268 | } |
| 1269 | fclose(f); |
| 1270 | f = 0; |
| 1271 | if( x!=sz ){ |
| 1272 | sqlite3_result_error(context, "could not read back the whole file", -1); |
| 1273 | goto edit_func_end; |
| 1274 | } |
| 1275 | if( bBin ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1276 | sqlite3_result_blob64(context, p, sz, sqlite3_free); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1277 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1278 | sqlite3_result_text64(context, (const char*)p, sz, |
| 1279 | sqlite3_free, SQLITE_UTF8); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1280 | } |
| 1281 | p = 0; |
| 1282 | |
| 1283 | edit_func_end: |
| 1284 | if( f ) fclose(f); |
| 1285 | unlink(zTempFile); |
| 1286 | sqlite3_free(zTempFile); |
| 1287 | sqlite3_free(p); |
| 1288 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1289 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1290 | |
| 1291 | /* |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1292 | ** Save or restore the current output mode |
| 1293 | */ |
| 1294 | static void outputModePush(ShellState *p){ |
| 1295 | p->modePrior = p->mode; |
| 1296 | memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); |
| 1297 | memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); |
| 1298 | } |
| 1299 | static void outputModePop(ShellState *p){ |
| 1300 | p->mode = p->modePrior; |
| 1301 | memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
| 1302 | memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
| 1303 | } |
| 1304 | |
| 1305 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1306 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1307 | */ |
| 1308 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1309 | int i; |
| 1310 | char *zBlob = (char *)pBlob; |
| 1311 | raw_printf(out,"X'"); |
| 1312 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1313 | raw_printf(out,"'"); |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1318 | ** to that string. |
| 1319 | ** |
| 1320 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1321 | ** then make up some string and store it in the buffer zBuf. |
| 1322 | */ |
| 1323 | static const char *unused_string( |
| 1324 | const char *z, /* Result must not appear anywhere in z */ |
| 1325 | const char *zA, const char *zB, /* Try these first */ |
| 1326 | char *zBuf /* Space to store a generated string */ |
| 1327 | ){ |
| 1328 | unsigned i = 0; |
| 1329 | if( strstr(z, zA)==0 ) return zA; |
| 1330 | if( strstr(z, zB)==0 ) return zB; |
| 1331 | do{ |
| 1332 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1333 | }while( strstr(z,zBuf)!=0 ); |
| 1334 | return zBuf; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1339 | ** |
| 1340 | ** See also: output_quoted_escaped_string() |
| 1341 | */ |
| 1342 | static void output_quoted_string(FILE *out, const char *z){ |
| 1343 | int i; |
| 1344 | char c; |
| 1345 | setBinaryMode(out, 1); |
| 1346 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1347 | if( c==0 ){ |
| 1348 | utf8_printf(out,"'%s'",z); |
| 1349 | }else{ |
| 1350 | raw_printf(out, "'"); |
| 1351 | while( *z ){ |
| 1352 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1353 | if( c=='\'' ) i++; |
| 1354 | if( i ){ |
| 1355 | utf8_printf(out, "%.*s", i, z); |
| 1356 | z += i; |
| 1357 | } |
| 1358 | if( c=='\'' ){ |
| 1359 | raw_printf(out, "'"); |
| 1360 | continue; |
| 1361 | } |
| 1362 | if( c==0 ){ |
| 1363 | break; |
| 1364 | } |
| 1365 | z++; |
| 1366 | } |
| 1367 | raw_printf(out, "'"); |
| 1368 | } |
| 1369 | setTextMode(out, 1); |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1374 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1375 | ** get corrupted by end-of-line translation facilities in some operating |
| 1376 | ** systems. |
| 1377 | ** |
| 1378 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1379 | ** escape mechanism. |
| 1380 | */ |
| 1381 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1382 | int i; |
| 1383 | char c; |
| 1384 | setBinaryMode(out, 1); |
| 1385 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1386 | if( c==0 ){ |
| 1387 | utf8_printf(out,"'%s'",z); |
| 1388 | }else{ |
| 1389 | const char *zNL = 0; |
| 1390 | const char *zCR = 0; |
| 1391 | int nNL = 0; |
| 1392 | int nCR = 0; |
| 1393 | char zBuf1[20], zBuf2[20]; |
| 1394 | for(i=0; z[i]; i++){ |
| 1395 | if( z[i]=='\n' ) nNL++; |
| 1396 | if( z[i]=='\r' ) nCR++; |
| 1397 | } |
| 1398 | if( nNL ){ |
| 1399 | raw_printf(out, "replace("); |
| 1400 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1401 | } |
| 1402 | if( nCR ){ |
| 1403 | raw_printf(out, "replace("); |
| 1404 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1405 | } |
| 1406 | raw_printf(out, "'"); |
| 1407 | while( *z ){ |
| 1408 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1409 | if( c=='\'' ) i++; |
| 1410 | if( i ){ |
| 1411 | utf8_printf(out, "%.*s", i, z); |
| 1412 | z += i; |
| 1413 | } |
| 1414 | if( c=='\'' ){ |
| 1415 | raw_printf(out, "'"); |
| 1416 | continue; |
| 1417 | } |
| 1418 | if( c==0 ){ |
| 1419 | break; |
| 1420 | } |
| 1421 | z++; |
| 1422 | if( c=='\n' ){ |
| 1423 | raw_printf(out, "%s", zNL); |
| 1424 | continue; |
| 1425 | } |
| 1426 | raw_printf(out, "%s", zCR); |
| 1427 | } |
| 1428 | raw_printf(out, "'"); |
| 1429 | if( nCR ){ |
| 1430 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1431 | } |
| 1432 | if( nNL ){ |
| 1433 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1434 | } |
| 1435 | } |
| 1436 | setTextMode(out, 1); |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1441 | */ |
| 1442 | static void output_c_string(FILE *out, const char *z){ |
| 1443 | unsigned int c; |
| 1444 | fputc('"', out); |
| 1445 | while( (c = *(z++))!=0 ){ |
| 1446 | if( c=='\\' ){ |
| 1447 | fputc(c, out); |
| 1448 | fputc(c, out); |
| 1449 | }else if( c=='"' ){ |
| 1450 | fputc('\\', out); |
| 1451 | fputc('"', out); |
| 1452 | }else if( c=='\t' ){ |
| 1453 | fputc('\\', out); |
| 1454 | fputc('t', out); |
| 1455 | }else if( c=='\n' ){ |
| 1456 | fputc('\\', out); |
| 1457 | fputc('n', out); |
| 1458 | }else if( c=='\r' ){ |
| 1459 | fputc('\\', out); |
| 1460 | fputc('r', out); |
| 1461 | }else if( !isprint(c&0xff) ){ |
| 1462 | raw_printf(out, "\\%03o", c&0xff); |
| 1463 | }else{ |
| 1464 | fputc(c, out); |
| 1465 | } |
| 1466 | } |
| 1467 | fputc('"', out); |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | ** Output the given string with characters that are special to |
| 1472 | ** HTML escaped. |
| 1473 | */ |
| 1474 | static void output_html_string(FILE *out, const char *z){ |
| 1475 | int i; |
| 1476 | if( z==0 ) z = ""; |
| 1477 | while( *z ){ |
| 1478 | for(i=0; z[i] |
| 1479 | && z[i]!='<' |
| 1480 | && z[i]!='&' |
| 1481 | && z[i]!='>' |
| 1482 | && z[i]!='\"' |
| 1483 | && z[i]!='\''; |
| 1484 | i++){} |
| 1485 | if( i>0 ){ |
| 1486 | utf8_printf(out,"%.*s",i,z); |
| 1487 | } |
| 1488 | if( z[i]=='<' ){ |
| 1489 | raw_printf(out,"<"); |
| 1490 | }else if( z[i]=='&' ){ |
| 1491 | raw_printf(out,"&"); |
| 1492 | }else if( z[i]=='>' ){ |
| 1493 | raw_printf(out,">"); |
| 1494 | }else if( z[i]=='\"' ){ |
| 1495 | raw_printf(out,"""); |
| 1496 | }else if( z[i]=='\'' ){ |
| 1497 | raw_printf(out,"'"); |
| 1498 | }else{ |
| 1499 | break; |
| 1500 | } |
| 1501 | z += i + 1; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | ** If a field contains any character identified by a 1 in the following |
| 1507 | ** array, then the string must be quoted for CSV. |
| 1508 | */ |
| 1509 | static const char needCsvQuote[] = { |
| 1510 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1511 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1512 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1513 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1514 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1515 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1516 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1517 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1518 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1519 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1520 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1521 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1522 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1523 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1525 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1526 | }; |
| 1527 | |
| 1528 | /* |
| 1529 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
| 1530 | ** the separator, which may or may not be a comma. p->nullValue is |
| 1531 | ** the null value. Strings are quoted if necessary. The separator |
| 1532 | ** is only issued if bSep is true. |
| 1533 | */ |
| 1534 | static void output_csv(ShellState *p, const char *z, int bSep){ |
| 1535 | FILE *out = p->out; |
| 1536 | if( z==0 ){ |
| 1537 | utf8_printf(out,"%s",p->nullValue); |
| 1538 | }else{ |
| 1539 | int i; |
| 1540 | int nSep = strlen30(p->colSeparator); |
| 1541 | for(i=0; z[i]; i++){ |
| 1542 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1543 | || (z[i]==p->colSeparator[0] && |
| 1544 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
| 1545 | i = 0; |
| 1546 | break; |
| 1547 | } |
| 1548 | } |
| 1549 | if( i==0 ){ |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1550 | char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
| 1551 | utf8_printf(out, "%s", zQuoted); |
| 1552 | sqlite3_free(zQuoted); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1553 | }else{ |
| 1554 | utf8_printf(out, "%s", z); |
| 1555 | } |
| 1556 | } |
| 1557 | if( bSep ){ |
| 1558 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1559 | } |
| 1560 | } |
| 1561 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1562 | /* |
| 1563 | ** This routine runs when the user presses Ctrl-C |
| 1564 | */ |
| 1565 | static void interrupt_handler(int NotUsed){ |
| 1566 | UNUSED_PARAMETER(NotUsed); |
| 1567 | seenInterrupt++; |
| 1568 | if( seenInterrupt>2 ) exit(1); |
| 1569 | if( globalDb ) sqlite3_interrupt(globalDb); |
| 1570 | } |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 1571 | |
| 1572 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 1573 | /* |
| 1574 | ** This routine runs for console events (e.g. Ctrl-C) on Win32 |
| 1575 | */ |
| 1576 | static BOOL WINAPI ConsoleCtrlHandler( |
| 1577 | DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ |
| 1578 | ){ |
| 1579 | if( dwCtrlType==CTRL_C_EVENT ){ |
| 1580 | interrupt_handler(0); |
| 1581 | return TRUE; |
| 1582 | } |
| 1583 | return FALSE; |
| 1584 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1585 | #endif |
| 1586 | |
| 1587 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1588 | /* |
| 1589 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1590 | ** invoked. It always returns SQLITE_OK. |
| 1591 | */ |
| 1592 | static int shellAuth( |
| 1593 | void *pClientData, |
| 1594 | int op, |
| 1595 | const char *zA1, |
| 1596 | const char *zA2, |
| 1597 | const char *zA3, |
| 1598 | const char *zA4 |
| 1599 | ){ |
| 1600 | ShellState *p = (ShellState*)pClientData; |
| 1601 | static const char *azAction[] = { 0, |
| 1602 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1603 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1604 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1605 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1606 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1607 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1608 | "PRAGMA", "READ", "SELECT", |
| 1609 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1610 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1611 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1612 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1613 | }; |
| 1614 | int i; |
| 1615 | const char *az[4]; |
| 1616 | az[0] = zA1; |
| 1617 | az[1] = zA2; |
| 1618 | az[2] = zA3; |
| 1619 | az[3] = zA4; |
| 1620 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
| 1621 | for(i=0; i<4; i++){ |
| 1622 | raw_printf(p->out, " "); |
| 1623 | if( az[i] ){ |
| 1624 | output_c_string(p->out, az[i]); |
| 1625 | }else{ |
| 1626 | raw_printf(p->out, "NULL"); |
| 1627 | } |
| 1628 | } |
| 1629 | raw_printf(p->out, "\n"); |
| 1630 | return SQLITE_OK; |
| 1631 | } |
| 1632 | #endif |
| 1633 | |
| 1634 | /* |
| 1635 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1636 | ** |
| 1637 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1638 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1639 | */ |
| 1640 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1641 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1642 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1643 | }else{ |
| 1644 | utf8_printf(out, "%s%s", z, zTail); |
| 1645 | } |
| 1646 | } |
| 1647 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1648 | char c = z[n]; |
| 1649 | z[n] = 0; |
| 1650 | printSchemaLine(out, z, zTail); |
| 1651 | z[n] = c; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1655 | ** Return true if string z[] has nothing but whitespace and comments to the |
| 1656 | ** end of the first line. |
| 1657 | */ |
| 1658 | static int wsToEol(const char *z){ |
| 1659 | int i; |
| 1660 | for(i=0; z[i]; i++){ |
| 1661 | if( z[i]=='\n' ) return 1; |
| 1662 | if( IsSpace(z[i]) ) continue; |
| 1663 | if( z[i]=='-' && z[i+1]=='-' ) return 1; |
| 1664 | return 0; |
| 1665 | } |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1671 | ** This is the callback routine that the shell |
| 1672 | ** invokes for each row of a query result. |
| 1673 | */ |
| 1674 | static int shell_callback( |
| 1675 | void *pArg, |
| 1676 | int nArg, /* Number of result columns */ |
| 1677 | char **azArg, /* Text of each result column */ |
| 1678 | char **azCol, /* Column names */ |
| 1679 | int *aiType /* Column types */ |
| 1680 | ){ |
| 1681 | int i; |
| 1682 | ShellState *p = (ShellState*)pArg; |
| 1683 | |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 1684 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1685 | switch( p->cMode ){ |
| 1686 | case MODE_Line: { |
| 1687 | int w = 5; |
| 1688 | if( azArg==0 ) break; |
| 1689 | for(i=0; i<nArg; i++){ |
| 1690 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 1691 | if( len>w ) w = len; |
| 1692 | } |
| 1693 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
| 1694 | for(i=0; i<nArg; i++){ |
| 1695 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
| 1696 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 1697 | } |
| 1698 | break; |
| 1699 | } |
| 1700 | case MODE_Explain: |
| 1701 | case MODE_Column: { |
| 1702 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1703 | const int *colWidth; |
| 1704 | int showHdr; |
| 1705 | char *rowSep; |
| 1706 | if( p->cMode==MODE_Column ){ |
| 1707 | colWidth = p->colWidth; |
| 1708 | showHdr = p->showHeader; |
| 1709 | rowSep = p->rowSeparator; |
| 1710 | }else{ |
| 1711 | colWidth = aExplainWidths; |
| 1712 | showHdr = 1; |
| 1713 | rowSep = SEP_Row; |
| 1714 | } |
| 1715 | if( p->cnt++==0 ){ |
| 1716 | for(i=0; i<nArg; i++){ |
| 1717 | int w, n; |
| 1718 | if( i<ArraySize(p->colWidth) ){ |
| 1719 | w = colWidth[i]; |
| 1720 | }else{ |
| 1721 | w = 0; |
| 1722 | } |
| 1723 | if( w==0 ){ |
| 1724 | w = strlenChar(azCol[i] ? azCol[i] : ""); |
| 1725 | if( w<10 ) w = 10; |
| 1726 | n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); |
| 1727 | if( w<n ) w = n; |
| 1728 | } |
| 1729 | if( i<ArraySize(p->actualWidth) ){ |
| 1730 | p->actualWidth[i] = w; |
| 1731 | } |
| 1732 | if( showHdr ){ |
| 1733 | utf8_width_print(p->out, w, azCol[i]); |
| 1734 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
| 1735 | } |
| 1736 | } |
| 1737 | if( showHdr ){ |
| 1738 | for(i=0; i<nArg; i++){ |
| 1739 | int w; |
| 1740 | if( i<ArraySize(p->actualWidth) ){ |
| 1741 | w = p->actualWidth[i]; |
| 1742 | if( w<0 ) w = -w; |
| 1743 | }else{ |
| 1744 | w = 10; |
| 1745 | } |
| 1746 | utf8_printf(p->out,"%-*.*s%s",w,w, |
| 1747 | "----------------------------------------------------------" |
| 1748 | "----------------------------------------------------------", |
| 1749 | i==nArg-1 ? rowSep : " "); |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | if( azArg==0 ) break; |
| 1754 | for(i=0; i<nArg; i++){ |
| 1755 | int w; |
| 1756 | if( i<ArraySize(p->actualWidth) ){ |
| 1757 | w = p->actualWidth[i]; |
| 1758 | }else{ |
| 1759 | w = 10; |
| 1760 | } |
| 1761 | if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ |
| 1762 | w = strlenChar(azArg[i]); |
| 1763 | } |
| 1764 | if( i==1 && p->aiIndent && p->pStmt ){ |
| 1765 | if( p->iIndent<p->nIndent ){ |
| 1766 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
| 1767 | } |
| 1768 | p->iIndent++; |
| 1769 | } |
| 1770 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 1771 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
| 1772 | } |
| 1773 | break; |
| 1774 | } |
| 1775 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 1776 | printSchemaLine(p->out, azArg[0], ";\n"); |
| 1777 | break; |
| 1778 | } |
| 1779 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1780 | char *z; |
| 1781 | int j; |
| 1782 | int nParen = 0; |
| 1783 | char cEnd = 0; |
| 1784 | char c; |
| 1785 | int nLine = 0; |
| 1786 | assert( nArg==1 ); |
| 1787 | if( azArg[0]==0 ) break; |
| 1788 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1789 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1790 | ){ |
| 1791 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1792 | break; |
| 1793 | } |
| 1794 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1795 | j = 0; |
| 1796 | for(i=0; IsSpace(z[i]); i++){} |
| 1797 | for(; (c = z[i])!=0; i++){ |
| 1798 | if( IsSpace(c) ){ |
drh | c3cbd67 | 2017-10-05 19:12:10 +0000 | [diff] [blame] | 1799 | if( z[j-1]=='\r' ) z[j-1] = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1800 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1801 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1802 | j--; |
| 1803 | } |
| 1804 | z[j++] = c; |
| 1805 | } |
| 1806 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1807 | z[j] = 0; |
| 1808 | if( strlen30(z)>=79 ){ |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1809 | for(i=j=0; (c = z[i])!=0; i++){ /* Copy changes from z[i] back to z[j] */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1810 | if( c==cEnd ){ |
| 1811 | cEnd = 0; |
| 1812 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1813 | cEnd = c; |
| 1814 | }else if( c=='[' ){ |
| 1815 | cEnd = ']'; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1816 | }else if( c=='-' && z[i+1]=='-' ){ |
| 1817 | cEnd = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1818 | }else if( c=='(' ){ |
| 1819 | nParen++; |
| 1820 | }else if( c==')' ){ |
| 1821 | nParen--; |
| 1822 | if( nLine>0 && nParen==0 && j>0 ){ |
| 1823 | printSchemaLineN(p->out, z, j, "\n"); |
| 1824 | j = 0; |
| 1825 | } |
| 1826 | } |
| 1827 | z[j++] = c; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1828 | if( nParen==1 && cEnd==0 |
| 1829 | && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
| 1830 | ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1831 | if( c=='\n' ) j--; |
| 1832 | printSchemaLineN(p->out, z, j, "\n "); |
| 1833 | j = 0; |
| 1834 | nLine++; |
| 1835 | while( IsSpace(z[i+1]) ){ i++; } |
| 1836 | } |
| 1837 | } |
| 1838 | z[j] = 0; |
| 1839 | } |
| 1840 | printSchemaLine(p->out, z, ";\n"); |
| 1841 | sqlite3_free(z); |
| 1842 | break; |
| 1843 | } |
| 1844 | case MODE_List: { |
| 1845 | if( p->cnt++==0 && p->showHeader ){ |
| 1846 | for(i=0; i<nArg; i++){ |
| 1847 | utf8_printf(p->out,"%s%s",azCol[i], |
| 1848 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 1849 | } |
| 1850 | } |
| 1851 | if( azArg==0 ) break; |
| 1852 | for(i=0; i<nArg; i++){ |
| 1853 | char *z = azArg[i]; |
| 1854 | if( z==0 ) z = p->nullValue; |
| 1855 | utf8_printf(p->out, "%s", z); |
| 1856 | if( i<nArg-1 ){ |
| 1857 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1858 | }else{ |
| 1859 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1860 | } |
| 1861 | } |
| 1862 | break; |
| 1863 | } |
| 1864 | case MODE_Html: { |
| 1865 | if( p->cnt++==0 && p->showHeader ){ |
| 1866 | raw_printf(p->out,"<TR>"); |
| 1867 | for(i=0; i<nArg; i++){ |
| 1868 | raw_printf(p->out,"<TH>"); |
| 1869 | output_html_string(p->out, azCol[i]); |
| 1870 | raw_printf(p->out,"</TH>\n"); |
| 1871 | } |
| 1872 | raw_printf(p->out,"</TR>\n"); |
| 1873 | } |
| 1874 | if( azArg==0 ) break; |
| 1875 | raw_printf(p->out,"<TR>"); |
| 1876 | for(i=0; i<nArg; i++){ |
| 1877 | raw_printf(p->out,"<TD>"); |
| 1878 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1879 | raw_printf(p->out,"</TD>\n"); |
| 1880 | } |
| 1881 | raw_printf(p->out,"</TR>\n"); |
| 1882 | break; |
| 1883 | } |
| 1884 | case MODE_Tcl: { |
| 1885 | if( p->cnt++==0 && p->showHeader ){ |
| 1886 | for(i=0; i<nArg; i++){ |
| 1887 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
| 1888 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1889 | } |
| 1890 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1891 | } |
| 1892 | if( azArg==0 ) break; |
| 1893 | for(i=0; i<nArg; i++){ |
| 1894 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1895 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1896 | } |
| 1897 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1898 | break; |
| 1899 | } |
| 1900 | case MODE_Csv: { |
| 1901 | setBinaryMode(p->out, 1); |
| 1902 | if( p->cnt++==0 && p->showHeader ){ |
| 1903 | for(i=0; i<nArg; i++){ |
| 1904 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 1905 | } |
| 1906 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1907 | } |
| 1908 | if( nArg>0 ){ |
| 1909 | for(i=0; i<nArg; i++){ |
| 1910 | output_csv(p, azArg[i], i<nArg-1); |
| 1911 | } |
| 1912 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1913 | } |
| 1914 | setTextMode(p->out, 1); |
| 1915 | break; |
| 1916 | } |
| 1917 | case MODE_Insert: { |
| 1918 | if( azArg==0 ) break; |
| 1919 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 1920 | if( p->showHeader ){ |
| 1921 | raw_printf(p->out,"("); |
| 1922 | for(i=0; i<nArg; i++){ |
| 1923 | if( i>0 ) raw_printf(p->out, ","); |
| 1924 | if( quoteChar(azCol[i]) ){ |
| 1925 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 1926 | utf8_printf(p->out, "%s", z); |
| 1927 | sqlite3_free(z); |
| 1928 | }else{ |
| 1929 | raw_printf(p->out, "%s", azCol[i]); |
| 1930 | } |
| 1931 | } |
| 1932 | raw_printf(p->out,")"); |
| 1933 | } |
| 1934 | p->cnt++; |
| 1935 | for(i=0; i<nArg; i++){ |
| 1936 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 1937 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 1938 | utf8_printf(p->out,"NULL"); |
| 1939 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 1940 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 1941 | output_quoted_string(p->out, azArg[i]); |
| 1942 | }else{ |
| 1943 | output_quoted_escaped_string(p->out, azArg[i]); |
| 1944 | } |
| 1945 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 1946 | utf8_printf(p->out,"%s", azArg[i]); |
| 1947 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 1948 | char z[50]; |
| 1949 | double r = sqlite3_column_double(p->pStmt, i); |
| 1950 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 1951 | raw_printf(p->out, "%s", z); |
| 1952 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 1953 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 1954 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 1955 | output_hex_blob(p->out, pBlob, nBlob); |
| 1956 | }else if( isNumber(azArg[i], 0) ){ |
| 1957 | utf8_printf(p->out,"%s", azArg[i]); |
| 1958 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 1959 | output_quoted_string(p->out, azArg[i]); |
| 1960 | }else{ |
| 1961 | output_quoted_escaped_string(p->out, azArg[i]); |
| 1962 | } |
| 1963 | } |
| 1964 | raw_printf(p->out,");\n"); |
| 1965 | break; |
| 1966 | } |
| 1967 | case MODE_Quote: { |
| 1968 | if( azArg==0 ) break; |
| 1969 | if( p->cnt==0 && p->showHeader ){ |
| 1970 | for(i=0; i<nArg; i++){ |
| 1971 | if( i>0 ) raw_printf(p->out, ","); |
| 1972 | output_quoted_string(p->out, azCol[i]); |
| 1973 | } |
| 1974 | raw_printf(p->out,"\n"); |
| 1975 | } |
| 1976 | p->cnt++; |
| 1977 | for(i=0; i<nArg; i++){ |
| 1978 | if( i>0 ) raw_printf(p->out, ","); |
| 1979 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 1980 | utf8_printf(p->out,"NULL"); |
| 1981 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 1982 | output_quoted_string(p->out, azArg[i]); |
| 1983 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 1984 | utf8_printf(p->out,"%s", azArg[i]); |
| 1985 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 1986 | char z[50]; |
| 1987 | double r = sqlite3_column_double(p->pStmt, i); |
| 1988 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 1989 | raw_printf(p->out, "%s", z); |
| 1990 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 1991 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 1992 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 1993 | output_hex_blob(p->out, pBlob, nBlob); |
| 1994 | }else if( isNumber(azArg[i], 0) ){ |
| 1995 | utf8_printf(p->out,"%s", azArg[i]); |
| 1996 | }else{ |
| 1997 | output_quoted_string(p->out, azArg[i]); |
| 1998 | } |
| 1999 | } |
| 2000 | raw_printf(p->out,"\n"); |
| 2001 | break; |
| 2002 | } |
| 2003 | case MODE_Ascii: { |
| 2004 | if( p->cnt++==0 && p->showHeader ){ |
| 2005 | for(i=0; i<nArg; i++){ |
| 2006 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2007 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
| 2008 | } |
| 2009 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2010 | } |
| 2011 | if( azArg==0 ) break; |
| 2012 | for(i=0; i<nArg; i++){ |
| 2013 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2014 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
| 2015 | } |
| 2016 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| 2023 | /* |
| 2024 | ** This is the callback routine that the SQLite library |
| 2025 | ** invokes for each row of a query result. |
| 2026 | */ |
| 2027 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2028 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2029 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2034 | ** output onto the end of a ShellText object. |
| 2035 | */ |
| 2036 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2037 | ShellText *p = (ShellText*)pArg; |
| 2038 | int i; |
| 2039 | UNUSED_PARAMETER(az); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2040 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2041 | if( p->n ) appendText(p, "|", 0); |
| 2042 | for(i=0; i<nArg; i++){ |
| 2043 | if( i ) appendText(p, ",", 0); |
| 2044 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2045 | } |
| 2046 | return 0; |
| 2047 | } |
| 2048 | |
| 2049 | /* |
| 2050 | ** Generate an appropriate SELFTEST table in the main database. |
| 2051 | */ |
| 2052 | static void createSelftestTable(ShellState *p){ |
| 2053 | char *zErrMsg = 0; |
| 2054 | sqlite3_exec(p->db, |
| 2055 | "SAVEPOINT selftest_init;\n" |
| 2056 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
| 2057 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2058 | " op TEXT,\n" /* Operator: memo run */ |
| 2059 | " cmd TEXT,\n" /* Command text */ |
| 2060 | " ans TEXT\n" /* Desired answer */ |
| 2061 | ");" |
| 2062 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2063 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2064 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2065 | " 'memo','Tests generated by --init');\n" |
| 2066 | "INSERT INTO [_shell$self]\n" |
| 2067 | " SELECT 'run',\n" |
| 2068 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2069 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2070 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2071 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2072 | "INSERT INTO [_shell$self]\n" |
| 2073 | " SELECT 'run'," |
| 2074 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2075 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2076 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2077 | " FROM (\n" |
| 2078 | " SELECT name FROM sqlite_master\n" |
| 2079 | " WHERE type='table'\n" |
| 2080 | " AND name<>'selftest'\n" |
| 2081 | " AND coalesce(rootpage,0)>0\n" |
| 2082 | " )\n" |
| 2083 | " ORDER BY name;\n" |
| 2084 | "INSERT INTO [_shell$self]\n" |
| 2085 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
| 2086 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2087 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2088 | "DROP TABLE [_shell$self];" |
| 2089 | ,0,0,&zErrMsg); |
| 2090 | if( zErrMsg ){ |
| 2091 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2092 | sqlite3_free(zErrMsg); |
| 2093 | } |
| 2094 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
| 2095 | } |
| 2096 | |
| 2097 | |
| 2098 | /* |
| 2099 | ** Set the destination table field of the ShellState structure to |
| 2100 | ** the name of the table given. Escape any quote characters in the |
| 2101 | ** table name. |
| 2102 | */ |
| 2103 | static void set_table_name(ShellState *p, const char *zName){ |
| 2104 | int i, n; |
mistachkin | 2158a0c | 2017-09-09 00:51:36 +0000 | [diff] [blame] | 2105 | char cQuote; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2106 | char *z; |
| 2107 | |
| 2108 | if( p->zDestTable ){ |
| 2109 | free(p->zDestTable); |
| 2110 | p->zDestTable = 0; |
| 2111 | } |
| 2112 | if( zName==0 ) return; |
| 2113 | cQuote = quoteChar(zName); |
| 2114 | n = strlen30(zName); |
| 2115 | if( cQuote ) n += n+2; |
| 2116 | z = p->zDestTable = malloc( n+1 ); |
| 2117 | if( z==0 ){ |
| 2118 | raw_printf(stderr,"Error: out of memory\n"); |
| 2119 | exit(1); |
| 2120 | } |
| 2121 | n = 0; |
| 2122 | if( cQuote ) z[n++] = cQuote; |
| 2123 | for(i=0; zName[i]; i++){ |
| 2124 | z[n++] = zName[i]; |
| 2125 | if( zName[i]==cQuote ) z[n++] = cQuote; |
| 2126 | } |
| 2127 | if( cQuote ) z[n++] = cQuote; |
| 2128 | z[n] = 0; |
| 2129 | } |
| 2130 | |
| 2131 | |
| 2132 | /* |
| 2133 | ** Execute a query statement that will generate SQL output. Print |
| 2134 | ** the result columns, comma-separated, on a line and then add a |
| 2135 | ** semicolon terminator to the end of that line. |
| 2136 | ** |
| 2137 | ** If the number of columns is 1 and that column contains text "--" |
| 2138 | ** then write the semicolon on a separate line. That way, if a |
| 2139 | ** "--" comment occurs at the end of the statement, the comment |
| 2140 | ** won't consume the semicolon terminator. |
| 2141 | */ |
| 2142 | static int run_table_dump_query( |
| 2143 | ShellState *p, /* Query context */ |
| 2144 | const char *zSelect, /* SELECT statement to extract content */ |
| 2145 | const char *zFirstRow /* Print before first row, if not NULL */ |
| 2146 | ){ |
| 2147 | sqlite3_stmt *pSelect; |
| 2148 | int rc; |
| 2149 | int nResult; |
| 2150 | int i; |
| 2151 | const char *z; |
| 2152 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
| 2153 | if( rc!=SQLITE_OK || !pSelect ){ |
| 2154 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2155 | sqlite3_errmsg(p->db)); |
| 2156 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2157 | return rc; |
| 2158 | } |
| 2159 | rc = sqlite3_step(pSelect); |
| 2160 | nResult = sqlite3_column_count(pSelect); |
| 2161 | while( rc==SQLITE_ROW ){ |
| 2162 | if( zFirstRow ){ |
| 2163 | utf8_printf(p->out, "%s", zFirstRow); |
| 2164 | zFirstRow = 0; |
| 2165 | } |
| 2166 | z = (const char*)sqlite3_column_text(pSelect, 0); |
| 2167 | utf8_printf(p->out, "%s", z); |
| 2168 | for(i=1; i<nResult; i++){ |
| 2169 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
| 2170 | } |
| 2171 | if( z==0 ) z = ""; |
| 2172 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2173 | if( z[0] ){ |
| 2174 | raw_printf(p->out, "\n;\n"); |
| 2175 | }else{ |
| 2176 | raw_printf(p->out, ";\n"); |
| 2177 | } |
| 2178 | rc = sqlite3_step(pSelect); |
| 2179 | } |
| 2180 | rc = sqlite3_finalize(pSelect); |
| 2181 | if( rc!=SQLITE_OK ){ |
| 2182 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2183 | sqlite3_errmsg(p->db)); |
| 2184 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2185 | } |
| 2186 | return rc; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | ** Allocate space and save off current error string. |
| 2191 | */ |
| 2192 | static char *save_err_msg( |
| 2193 | sqlite3 *db /* Database to query */ |
| 2194 | ){ |
| 2195 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
| 2196 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
| 2197 | if( zErrMsg ){ |
| 2198 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2199 | } |
| 2200 | return zErrMsg; |
| 2201 | } |
| 2202 | |
| 2203 | #ifdef __linux__ |
| 2204 | /* |
| 2205 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2206 | */ |
| 2207 | static void displayLinuxIoStats(FILE *out){ |
| 2208 | FILE *in; |
| 2209 | char z[200]; |
| 2210 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2211 | in = fopen(z, "rb"); |
| 2212 | if( in==0 ) return; |
| 2213 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2214 | static const struct { |
| 2215 | const char *zPattern; |
| 2216 | const char *zDesc; |
| 2217 | } aTrans[] = { |
| 2218 | { "rchar: ", "Bytes received by read():" }, |
| 2219 | { "wchar: ", "Bytes sent to write():" }, |
| 2220 | { "syscr: ", "Read() system calls:" }, |
| 2221 | { "syscw: ", "Write() system calls:" }, |
| 2222 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2223 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2224 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 2225 | }; |
| 2226 | int i; |
| 2227 | for(i=0; i<ArraySize(aTrans); i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 2228 | int n = strlen30(aTrans[i].zPattern); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2229 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
| 2230 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
| 2231 | break; |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | fclose(in); |
| 2236 | } |
| 2237 | #endif |
| 2238 | |
| 2239 | /* |
| 2240 | ** Display a single line of status using 64-bit values. |
| 2241 | */ |
| 2242 | static void displayStatLine( |
| 2243 | ShellState *p, /* The shell context */ |
| 2244 | char *zLabel, /* Label for this one line */ |
| 2245 | char *zFormat, /* Format for the result */ |
| 2246 | int iStatusCtrl, /* Which status to display */ |
| 2247 | int bReset /* True to reset the stats */ |
| 2248 | ){ |
| 2249 | sqlite3_int64 iCur = -1; |
| 2250 | sqlite3_int64 iHiwtr = -1; |
| 2251 | int i, nPercent; |
| 2252 | char zLine[200]; |
| 2253 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2254 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2255 | if( zFormat[i]=='%' ) nPercent++; |
| 2256 | } |
| 2257 | if( nPercent>1 ){ |
| 2258 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2259 | }else{ |
| 2260 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2261 | } |
| 2262 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | ** Display memory stats. |
| 2267 | */ |
| 2268 | static int display_stats( |
| 2269 | sqlite3 *db, /* Database to query */ |
| 2270 | ShellState *pArg, /* Pointer to ShellState */ |
| 2271 | int bReset /* True to reset the stats */ |
| 2272 | ){ |
| 2273 | int iCur; |
| 2274 | int iHiwtr; |
| 2275 | |
| 2276 | if( pArg && pArg->out ){ |
| 2277 | displayStatLine(pArg, "Memory Used:", |
| 2278 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2279 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2280 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
| 2281 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 2282 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2283 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
| 2284 | } |
| 2285 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2286 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2287 | displayStatLine(pArg, "Largest Allocation:", |
| 2288 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2289 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2290 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2291 | #ifdef YYTRACKMAXSTACKDEPTH |
| 2292 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2293 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
| 2294 | #endif |
| 2295 | } |
| 2296 | |
| 2297 | if( pArg && pArg->out && db ){ |
| 2298 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2299 | iHiwtr = iCur = -1; |
| 2300 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2301 | &iCur, &iHiwtr, bReset); |
| 2302 | raw_printf(pArg->out, |
| 2303 | "Lookaside Slots Used: %d (max %d)\n", |
| 2304 | iCur, iHiwtr); |
| 2305 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2306 | &iCur, &iHiwtr, bReset); |
| 2307 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2308 | iHiwtr); |
| 2309 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2310 | &iCur, &iHiwtr, bReset); |
| 2311 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2312 | iHiwtr); |
| 2313 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2314 | &iCur, &iHiwtr, bReset); |
| 2315 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2316 | iHiwtr); |
| 2317 | } |
| 2318 | iHiwtr = iCur = -1; |
| 2319 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
| 2320 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2321 | iCur); |
| 2322 | iHiwtr = iCur = -1; |
| 2323 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
| 2324 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
| 2325 | iHiwtr = iCur = -1; |
| 2326 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
| 2327 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
| 2328 | iHiwtr = iCur = -1; |
| 2329 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
| 2330 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
| 2331 | iHiwtr = iCur = -1; |
| 2332 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
| 2333 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| 2334 | iCur); |
| 2335 | iHiwtr = iCur = -1; |
| 2336 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
| 2337 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| 2338 | iCur); |
| 2339 | } |
| 2340 | |
| 2341 | if( pArg && pArg->out && db && pArg->pStmt ){ |
| 2342 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2343 | bReset); |
| 2344 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
| 2345 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
| 2346 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
| 2347 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
| 2348 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
| 2349 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2350 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
| 2351 | } |
| 2352 | |
| 2353 | #ifdef __linux__ |
| 2354 | displayLinuxIoStats(pArg->out); |
| 2355 | #endif |
| 2356 | |
| 2357 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2358 | |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | /* |
| 2363 | ** Display scan stats. |
| 2364 | */ |
| 2365 | static void display_scanstats( |
| 2366 | sqlite3 *db, /* Database to query */ |
| 2367 | ShellState *pArg /* Pointer to ShellState */ |
| 2368 | ){ |
| 2369 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2370 | UNUSED_PARAMETER(db); |
| 2371 | UNUSED_PARAMETER(pArg); |
| 2372 | #else |
| 2373 | int i, k, n, mx; |
| 2374 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
| 2375 | mx = 0; |
| 2376 | for(k=0; k<=mx; k++){ |
| 2377 | double rEstLoop = 1.0; |
| 2378 | for(i=n=0; 1; i++){ |
| 2379 | sqlite3_stmt *p = pArg->pStmt; |
| 2380 | sqlite3_int64 nLoop, nVisit; |
| 2381 | double rEst; |
| 2382 | int iSid; |
| 2383 | const char *zExplain; |
| 2384 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2385 | break; |
| 2386 | } |
| 2387 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| 2388 | if( iSid>mx ) mx = iSid; |
| 2389 | if( iSid!=k ) continue; |
| 2390 | if( n==0 ){ |
| 2391 | rEstLoop = (double)nLoop; |
| 2392 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| 2393 | } |
| 2394 | n++; |
| 2395 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2396 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2397 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| 2398 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| 2399 | rEstLoop *= rEst; |
| 2400 | raw_printf(pArg->out, |
| 2401 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| 2402 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| 2403 | ); |
| 2404 | } |
| 2405 | } |
| 2406 | raw_printf(pArg->out, "---------------------------\n"); |
| 2407 | #endif |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2412 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2413 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2414 | ** Otherwise, return zero. |
| 2415 | */ |
| 2416 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2417 | int i; |
| 2418 | for(i=0; azArray[i]; i++){ |
| 2419 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2420 | } |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /* |
| 2425 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| 2426 | ** and populate the ShellState.aiIndent[] array with the number of |
| 2427 | ** spaces each opcode should be indented before it is output. |
| 2428 | ** |
| 2429 | ** The indenting rules are: |
| 2430 | ** |
| 2431 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2432 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2433 | ** itself by 2 spaces. |
| 2434 | ** |
| 2435 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2436 | ** and ends on one of: |
| 2437 | ** Yield SeekGt SeekLt RowSetRead Rewind |
| 2438 | ** or if the P1 parameter is one instead of zero, |
| 2439 | ** then indent all opcodes between the earlier instruction |
| 2440 | ** and "Goto" by 2 spaces. |
| 2441 | */ |
| 2442 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| 2443 | const char *zSql; /* The text of the SQL statement */ |
| 2444 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2445 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2446 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| 2447 | int iOp; /* Index of operation in p->aiIndent[] */ |
| 2448 | |
| 2449 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2450 | "NextIfOpen", "PrevIfOpen", 0 }; |
| 2451 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2452 | "Rewind", 0 }; |
| 2453 | const char *azGoto[] = { "Goto", 0 }; |
| 2454 | |
| 2455 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2456 | ** cannot be verified, return early. */ |
| 2457 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2458 | p->cMode = p->mode; |
| 2459 | return; |
| 2460 | } |
| 2461 | zSql = sqlite3_sql(pSql); |
| 2462 | if( zSql==0 ) return; |
| 2463 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| 2464 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2465 | p->cMode = p->mode; |
| 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2470 | int i; |
| 2471 | int iAddr = sqlite3_column_int(pSql, 0); |
| 2472 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| 2473 | |
| 2474 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2475 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2476 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2477 | ** the current instruction is part of a sub-program generated by an |
| 2478 | ** SQL trigger or foreign key. */ |
| 2479 | int p2 = sqlite3_column_int(pSql, 3); |
| 2480 | int p2op = (p2 + (iOp-iAddr)); |
| 2481 | |
| 2482 | /* Grow the p->aiIndent array as required */ |
| 2483 | if( iOp>=nAlloc ){ |
| 2484 | if( iOp==0 ){ |
| 2485 | /* Do further verfication that this is explain output. Abort if |
| 2486 | ** it is not */ |
| 2487 | static const char *explainCols[] = { |
| 2488 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2489 | int jj; |
| 2490 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2491 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2492 | p->cMode = p->mode; |
| 2493 | sqlite3_reset(pSql); |
| 2494 | return; |
| 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | nAlloc += 100; |
| 2499 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2500 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
| 2501 | } |
| 2502 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2503 | p->aiIndent[iOp] = 0; |
| 2504 | p->nIndent = iOp+1; |
| 2505 | |
| 2506 | if( str_in_array(zOp, azNext) ){ |
| 2507 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 2508 | } |
| 2509 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2510 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2511 | ){ |
| 2512 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | p->iIndent = 0; |
| 2517 | sqlite3_free(abYield); |
| 2518 | sqlite3_reset(pSql); |
| 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | ** Free the array allocated by explain_data_prepare(). |
| 2523 | */ |
| 2524 | static void explain_data_delete(ShellState *p){ |
| 2525 | sqlite3_free(p->aiIndent); |
| 2526 | p->aiIndent = 0; |
| 2527 | p->nIndent = 0; |
| 2528 | p->iIndent = 0; |
| 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2533 | */ |
| 2534 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2535 | extern int sqlite3SelectTrace; |
| 2536 | static int savedSelectTrace; |
| 2537 | #endif |
| 2538 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2539 | extern int sqlite3WhereTrace; |
| 2540 | static int savedWhereTrace; |
| 2541 | #endif |
| 2542 | static void disable_debug_trace_modes(void){ |
| 2543 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2544 | savedSelectTrace = sqlite3SelectTrace; |
| 2545 | sqlite3SelectTrace = 0; |
| 2546 | #endif |
| 2547 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2548 | savedWhereTrace = sqlite3WhereTrace; |
| 2549 | sqlite3WhereTrace = 0; |
| 2550 | #endif |
| 2551 | } |
| 2552 | static void restore_debug_trace_modes(void){ |
| 2553 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2554 | sqlite3SelectTrace = savedSelectTrace; |
| 2555 | #endif |
| 2556 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2557 | sqlite3WhereTrace = savedWhereTrace; |
| 2558 | #endif |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | ** Run a prepared statement |
| 2563 | */ |
| 2564 | static void exec_prepared_stmt( |
| 2565 | ShellState *pArg, /* Pointer to ShellState */ |
| 2566 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2567 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2568 | ){ |
| 2569 | int rc; |
| 2570 | |
| 2571 | /* perform the first step. this will tell us if we |
| 2572 | ** have a result set or not and how wide it is. |
| 2573 | */ |
| 2574 | rc = sqlite3_step(pStmt); |
| 2575 | /* if we have a result set... */ |
| 2576 | if( SQLITE_ROW == rc ){ |
| 2577 | /* if we have a callback... */ |
| 2578 | if( xCallback ){ |
| 2579 | /* allocate space for col name ptr, value ptr, and type */ |
| 2580 | int nCol = sqlite3_column_count(pStmt); |
| 2581 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2582 | if( !pData ){ |
| 2583 | rc = SQLITE_NOMEM; |
| 2584 | }else{ |
| 2585 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2586 | char **azVals = &azCols[nCol]; /* Results */ |
| 2587 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2588 | int i, x; |
| 2589 | assert(sizeof(int) <= sizeof(char *)); |
| 2590 | /* save off ptrs to column names */ |
| 2591 | for(i=0; i<nCol; i++){ |
| 2592 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2593 | } |
| 2594 | do{ |
| 2595 | /* extract the data and data types */ |
| 2596 | for(i=0; i<nCol; i++){ |
| 2597 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2598 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2599 | azVals[i] = ""; |
| 2600 | }else{ |
| 2601 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2602 | } |
| 2603 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2604 | rc = SQLITE_NOMEM; |
| 2605 | break; /* from for */ |
| 2606 | } |
| 2607 | } /* end for */ |
| 2608 | |
| 2609 | /* if data and types extracted successfully... */ |
| 2610 | if( SQLITE_ROW == rc ){ |
| 2611 | /* call the supplied callback with the result row data */ |
| 2612 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2613 | rc = SQLITE_ABORT; |
| 2614 | }else{ |
| 2615 | rc = sqlite3_step(pStmt); |
| 2616 | } |
| 2617 | } |
| 2618 | } while( SQLITE_ROW == rc ); |
| 2619 | sqlite3_free(pData); |
| 2620 | } |
| 2621 | }else{ |
| 2622 | do{ |
| 2623 | rc = sqlite3_step(pStmt); |
| 2624 | } while( rc == SQLITE_ROW ); |
| 2625 | } |
| 2626 | } |
| 2627 | } |
| 2628 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2629 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2630 | /* |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2631 | ** This function is called to process SQL if the previous shell command |
| 2632 | ** was ".expert". It passes the SQL in the second argument directly to |
| 2633 | ** the sqlite3expert object. |
| 2634 | ** |
| 2635 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 2636 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 2637 | ** an English language error message. It is the responsibility of the |
| 2638 | ** caller to eventually free this buffer using sqlite3_free(). |
| 2639 | */ |
| 2640 | static int expertHandleSQL( |
| 2641 | ShellState *pState, |
| 2642 | const char *zSql, |
| 2643 | char **pzErr |
| 2644 | ){ |
| 2645 | assert( pState->expert.pExpert ); |
| 2646 | assert( pzErr==0 || *pzErr==0 ); |
| 2647 | return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); |
| 2648 | } |
| 2649 | |
| 2650 | /* |
| 2651 | ** This function is called either to silently clean up the object |
| 2652 | ** created by the ".expert" command (if bCancel==1), or to generate a |
| 2653 | ** report from it and then clean it up (if bCancel==0). |
| 2654 | ** |
| 2655 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 2656 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 2657 | ** an English language error message. It is the responsibility of the |
| 2658 | ** caller to eventually free this buffer using sqlite3_free(). |
| 2659 | */ |
| 2660 | static int expertFinish( |
| 2661 | ShellState *pState, |
| 2662 | int bCancel, |
| 2663 | char **pzErr |
| 2664 | ){ |
| 2665 | int rc = SQLITE_OK; |
| 2666 | sqlite3expert *p = pState->expert.pExpert; |
| 2667 | assert( p ); |
| 2668 | assert( bCancel || pzErr==0 || *pzErr==0 ); |
| 2669 | if( bCancel==0 ){ |
| 2670 | FILE *out = pState->out; |
| 2671 | int bVerbose = pState->expert.bVerbose; |
| 2672 | |
| 2673 | rc = sqlite3_expert_analyze(p, pzErr); |
| 2674 | if( rc==SQLITE_OK ){ |
| 2675 | int nQuery = sqlite3_expert_count(p); |
| 2676 | int i; |
| 2677 | |
| 2678 | if( bVerbose ){ |
| 2679 | const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
| 2680 | raw_printf(out, "-- Candidates -----------------------------\n"); |
| 2681 | raw_printf(out, "%s\n", zCand); |
| 2682 | } |
| 2683 | for(i=0; i<nQuery; i++){ |
| 2684 | const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
| 2685 | const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
| 2686 | const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
| 2687 | if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
| 2688 | if( bVerbose ){ |
| 2689 | raw_printf(out, "-- Query %d --------------------------------\n",i+1); |
| 2690 | raw_printf(out, "%s\n\n", zSql); |
| 2691 | } |
| 2692 | raw_printf(out, "%s\n", zIdx); |
| 2693 | raw_printf(out, "%s\n", zEQP); |
| 2694 | } |
| 2695 | } |
| 2696 | } |
| 2697 | sqlite3_expert_destroy(p); |
| 2698 | pState->expert.pExpert = 0; |
| 2699 | return rc; |
| 2700 | } |
| 2701 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2702 | /* |
| 2703 | ** Implementation of ".expert" dot command. |
| 2704 | */ |
| 2705 | static int expertDotCommand( |
| 2706 | ShellState *pState, /* Current shell tool state */ |
| 2707 | char **azArg, /* Array of arguments passed to dot command */ |
| 2708 | int nArg /* Number of entries in azArg[] */ |
| 2709 | ){ |
| 2710 | int rc = SQLITE_OK; |
| 2711 | char *zErr = 0; |
| 2712 | int i; |
| 2713 | int iSample = 0; |
| 2714 | |
| 2715 | assert( pState->expert.pExpert==0 ); |
| 2716 | memset(&pState->expert, 0, sizeof(ExpertInfo)); |
| 2717 | |
| 2718 | for(i=1; rc==SQLITE_OK && i<nArg; i++){ |
| 2719 | char *z = azArg[i]; |
| 2720 | int n; |
| 2721 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 2722 | n = strlen30(z); |
| 2723 | if( n>=2 && 0==strncmp(z, "-verbose", n) ){ |
| 2724 | pState->expert.bVerbose = 1; |
| 2725 | } |
| 2726 | else if( n>=2 && 0==strncmp(z, "-sample", n) ){ |
| 2727 | if( i==(nArg-1) ){ |
| 2728 | raw_printf(stderr, "option requires an argument: %s\n", z); |
| 2729 | rc = SQLITE_ERROR; |
| 2730 | }else{ |
| 2731 | iSample = (int)integerValue(azArg[++i]); |
| 2732 | if( iSample<0 || iSample>100 ){ |
| 2733 | raw_printf(stderr, "value out of range: %s\n", azArg[i]); |
| 2734 | rc = SQLITE_ERROR; |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | else{ |
| 2739 | raw_printf(stderr, "unknown option: %s\n", z); |
| 2740 | rc = SQLITE_ERROR; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | if( rc==SQLITE_OK ){ |
| 2745 | pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); |
| 2746 | if( pState->expert.pExpert==0 ){ |
| 2747 | raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr); |
| 2748 | rc = SQLITE_ERROR; |
| 2749 | }else{ |
| 2750 | sqlite3_expert_config( |
| 2751 | pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample |
| 2752 | ); |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | return rc; |
| 2757 | } |
| 2758 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2759 | |
| 2760 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2761 | ** Execute a statement or set of statements. Print |
| 2762 | ** any result rows/columns depending on the current mode |
| 2763 | ** set via the supplied callback. |
| 2764 | ** |
| 2765 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2766 | ** function except it takes a slightly different callback |
| 2767 | ** and callback data argument. |
| 2768 | */ |
| 2769 | static int shell_exec( |
| 2770 | sqlite3 *db, /* An open database */ |
| 2771 | const char *zSql, /* SQL to be evaluated */ |
| 2772 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
| 2773 | /* (not the same as sqlite3_exec) */ |
| 2774 | ShellState *pArg, /* Pointer to ShellState */ |
| 2775 | char **pzErrMsg /* Error msg written here */ |
| 2776 | ){ |
| 2777 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2778 | int rc = SQLITE_OK; /* Return Code */ |
| 2779 | int rc2; |
| 2780 | const char *zLeftover; /* Tail of unprocessed SQL */ |
| 2781 | |
| 2782 | if( pzErrMsg ){ |
| 2783 | *pzErrMsg = NULL; |
| 2784 | } |
| 2785 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2786 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2787 | if( pArg->expert.pExpert ){ |
| 2788 | rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
| 2789 | return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
| 2790 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2791 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2792 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2793 | while( zSql[0] && (SQLITE_OK == rc) ){ |
| 2794 | static const char *zStmtSql; |
| 2795 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2796 | if( SQLITE_OK != rc ){ |
| 2797 | if( pzErrMsg ){ |
| 2798 | *pzErrMsg = save_err_msg(db); |
| 2799 | } |
| 2800 | }else{ |
| 2801 | if( !pStmt ){ |
| 2802 | /* this happens for a comment or white-space */ |
| 2803 | zSql = zLeftover; |
| 2804 | while( IsSpace(zSql[0]) ) zSql++; |
| 2805 | continue; |
| 2806 | } |
| 2807 | zStmtSql = sqlite3_sql(pStmt); |
| 2808 | if( zStmtSql==0 ) zStmtSql = ""; |
| 2809 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 2810 | |
| 2811 | /* save off the prepared statment handle and reset row count */ |
| 2812 | if( pArg ){ |
| 2813 | pArg->pStmt = pStmt; |
| 2814 | pArg->cnt = 0; |
| 2815 | } |
| 2816 | |
| 2817 | /* echo the sql statement if echo on */ |
| 2818 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
| 2819 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
| 2820 | } |
| 2821 | |
| 2822 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
| 2823 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
| 2824 | sqlite3_stmt *pExplain; |
| 2825 | char *zEQP; |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2826 | int triggerEQP = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2827 | disable_debug_trace_modes(); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2828 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
| 2829 | if( pArg->autoEQP>=AUTOEQP_trigger ){ |
| 2830 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); |
| 2831 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2832 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
| 2833 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2834 | if( rc==SQLITE_OK ){ |
| 2835 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
| 2836 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2837 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2838 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
| 2839 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
| 2840 | } |
| 2841 | } |
| 2842 | sqlite3_finalize(pExplain); |
| 2843 | sqlite3_free(zEQP); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2844 | if( pArg->autoEQP>=AUTOEQP_full ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2845 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2846 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2847 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2848 | if( rc==SQLITE_OK ){ |
| 2849 | pArg->cMode = MODE_Explain; |
| 2850 | explain_data_prepare(pArg, pExplain); |
| 2851 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2852 | explain_data_delete(pArg); |
| 2853 | } |
| 2854 | sqlite3_finalize(pExplain); |
| 2855 | sqlite3_free(zEQP); |
| 2856 | } |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2857 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2858 | restore_debug_trace_modes(); |
| 2859 | } |
| 2860 | |
| 2861 | if( pArg ){ |
| 2862 | pArg->cMode = pArg->mode; |
| 2863 | if( pArg->autoExplain |
| 2864 | && sqlite3_column_count(pStmt)==8 |
| 2865 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
| 2866 | ){ |
| 2867 | pArg->cMode = MODE_Explain; |
| 2868 | } |
| 2869 | |
| 2870 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2871 | ** data required to add indents to the output.*/ |
| 2872 | if( pArg->cMode==MODE_Explain ){ |
| 2873 | explain_data_prepare(pArg, pStmt); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | exec_prepared_stmt(pArg, pStmt, xCallback); |
| 2878 | explain_data_delete(pArg); |
| 2879 | |
| 2880 | /* print usage stats if stats on */ |
| 2881 | if( pArg && pArg->statsOn ){ |
| 2882 | display_stats(db, pArg, 0); |
| 2883 | } |
| 2884 | |
| 2885 | /* print loop-counters if required */ |
| 2886 | if( pArg && pArg->scanstatsOn ){ |
| 2887 | display_scanstats(db, pArg); |
| 2888 | } |
| 2889 | |
| 2890 | /* Finalize the statement just executed. If this fails, save a |
| 2891 | ** copy of the error message. Otherwise, set zSql to point to the |
| 2892 | ** next statement to execute. */ |
| 2893 | rc2 = sqlite3_finalize(pStmt); |
| 2894 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
| 2895 | if( rc==SQLITE_OK ){ |
| 2896 | zSql = zLeftover; |
| 2897 | while( IsSpace(zSql[0]) ) zSql++; |
| 2898 | }else if( pzErrMsg ){ |
| 2899 | *pzErrMsg = save_err_msg(db); |
| 2900 | } |
| 2901 | |
| 2902 | /* clear saved stmt handle */ |
| 2903 | if( pArg ){ |
| 2904 | pArg->pStmt = NULL; |
| 2905 | } |
| 2906 | } |
| 2907 | } /* end while */ |
| 2908 | |
| 2909 | return rc; |
| 2910 | } |
| 2911 | |
| 2912 | /* |
| 2913 | ** Release memory previously allocated by tableColumnList(). |
| 2914 | */ |
| 2915 | static void freeColumnList(char **azCol){ |
| 2916 | int i; |
| 2917 | for(i=1; azCol[i]; i++){ |
| 2918 | sqlite3_free(azCol[i]); |
| 2919 | } |
| 2920 | /* azCol[0] is a static string */ |
| 2921 | sqlite3_free(azCol); |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | ** Return a list of pointers to strings which are the names of all |
| 2926 | ** columns in table zTab. The memory to hold the names is dynamically |
| 2927 | ** allocated and must be released by the caller using a subsequent call |
| 2928 | ** to freeColumnList(). |
| 2929 | ** |
| 2930 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 2931 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 2932 | ** name of the rowid column. |
| 2933 | ** |
| 2934 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 2935 | ** by an entry with azCol[i]==0. |
| 2936 | */ |
| 2937 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 2938 | char **azCol = 0; |
| 2939 | sqlite3_stmt *pStmt; |
| 2940 | char *zSql; |
| 2941 | int nCol = 0; |
| 2942 | int nAlloc = 0; |
| 2943 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 2944 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 2945 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 2946 | int rc; |
| 2947 | |
| 2948 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 2949 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2950 | sqlite3_free(zSql); |
| 2951 | if( rc ) return 0; |
| 2952 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2953 | if( nCol>=nAlloc-2 ){ |
| 2954 | nAlloc = nAlloc*2 + nCol + 10; |
| 2955 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 2956 | if( azCol==0 ){ |
| 2957 | raw_printf(stderr, "Error: out of memory\n"); |
| 2958 | exit(1); |
| 2959 | } |
| 2960 | } |
| 2961 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 2962 | if( sqlite3_column_int(pStmt, 5) ){ |
| 2963 | nPK++; |
| 2964 | if( nPK==1 |
| 2965 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 2966 | "INTEGER")==0 |
| 2967 | ){ |
| 2968 | isIPK = 1; |
| 2969 | }else{ |
| 2970 | isIPK = 0; |
| 2971 | } |
| 2972 | } |
| 2973 | } |
| 2974 | sqlite3_finalize(pStmt); |
drh | 4c6cddc | 2017-10-12 10:28:30 +0000 | [diff] [blame] | 2975 | if( azCol==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2976 | azCol[0] = 0; |
| 2977 | azCol[nCol+1] = 0; |
| 2978 | |
| 2979 | /* The decision of whether or not a rowid really needs to be preserved |
| 2980 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 2981 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 2982 | ** rowids on tables where the rowid is inaccessible because there are other |
| 2983 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 2984 | */ |
| 2985 | if( preserveRowid && isIPK ){ |
| 2986 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 2987 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 2988 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 2989 | ** ROWID aliases. To distinguish these cases, check to see if |
| 2990 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 2991 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 2992 | */ |
| 2993 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 2994 | " WHERE origin='pk'", zTab); |
| 2995 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2996 | sqlite3_free(zSql); |
| 2997 | if( rc ){ |
| 2998 | freeColumnList(azCol); |
| 2999 | return 0; |
| 3000 | } |
| 3001 | rc = sqlite3_step(pStmt); |
| 3002 | sqlite3_finalize(pStmt); |
| 3003 | preserveRowid = rc==SQLITE_ROW; |
| 3004 | } |
| 3005 | if( preserveRowid ){ |
| 3006 | /* Only preserve the rowid if we can find a name to use for the |
| 3007 | ** rowid */ |
| 3008 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3009 | int i, j; |
| 3010 | for(j=0; j<3; j++){ |
| 3011 | for(i=1; i<=nCol; i++){ |
| 3012 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3013 | } |
| 3014 | if( i>nCol ){ |
| 3015 | /* At this point, we know that azRowid[j] is not the name of any |
| 3016 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3017 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3018 | ** tables will fail this last check */ |
| 3019 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3020 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3021 | break; |
| 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | return azCol; |
| 3026 | } |
| 3027 | |
| 3028 | /* |
| 3029 | ** Toggle the reverse_unordered_selects setting. |
| 3030 | */ |
| 3031 | static void toggleSelectOrder(sqlite3 *db){ |
| 3032 | sqlite3_stmt *pStmt = 0; |
| 3033 | int iSetting = 0; |
| 3034 | char zStmt[100]; |
| 3035 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3036 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3037 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3038 | } |
| 3039 | sqlite3_finalize(pStmt); |
| 3040 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3041 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3042 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | ** This is a different callback routine used for dumping the database. |
| 3047 | ** Each row received by this callback consists of a table name, |
| 3048 | ** the table type ("index" or "table") and SQL to create the table. |
| 3049 | ** This routine should print text sufficient to recreate the table. |
| 3050 | */ |
| 3051 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
| 3052 | int rc; |
| 3053 | const char *zTable; |
| 3054 | const char *zType; |
| 3055 | const char *zSql; |
| 3056 | ShellState *p = (ShellState *)pArg; |
| 3057 | |
| 3058 | UNUSED_PARAMETER(azNotUsed); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 3059 | if( nArg!=3 || azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3060 | zTable = azArg[0]; |
| 3061 | zType = azArg[1]; |
| 3062 | zSql = azArg[2]; |
| 3063 | |
| 3064 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
| 3065 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
| 3066 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
| 3067 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 3068 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3069 | return 0; |
| 3070 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3071 | char *zIns; |
| 3072 | if( !p->writableSchema ){ |
| 3073 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 3074 | p->writableSchema = 1; |
| 3075 | } |
| 3076 | zIns = sqlite3_mprintf( |
| 3077 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3078 | "VALUES('table','%q','%q',0,'%q');", |
| 3079 | zTable, zTable, zSql); |
| 3080 | utf8_printf(p->out, "%s\n", zIns); |
| 3081 | sqlite3_free(zIns); |
| 3082 | return 0; |
| 3083 | }else{ |
| 3084 | printSchemaLine(p->out, zSql, ";\n"); |
| 3085 | } |
| 3086 | |
| 3087 | if( strcmp(zType, "table")==0 ){ |
| 3088 | ShellText sSelect; |
| 3089 | ShellText sTable; |
| 3090 | char **azCol; |
| 3091 | int i; |
| 3092 | char *savedDestTable; |
| 3093 | int savedMode; |
| 3094 | |
| 3095 | azCol = tableColumnList(p, zTable); |
| 3096 | if( azCol==0 ){ |
| 3097 | p->nErr++; |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
| 3101 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3102 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 3103 | initText(&sTable); |
| 3104 | appendText(&sTable, zTable, quoteChar(zTable)); |
| 3105 | /* If preserving the rowid, add a column list after the table name. |
| 3106 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3107 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3108 | */ |
| 3109 | if( azCol[0] ){ |
| 3110 | appendText(&sTable, "(", 0); |
| 3111 | appendText(&sTable, azCol[0], 0); |
| 3112 | for(i=1; azCol[i]; i++){ |
| 3113 | appendText(&sTable, ",", 0); |
| 3114 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
| 3115 | } |
| 3116 | appendText(&sTable, ")", 0); |
| 3117 | } |
| 3118 | |
| 3119 | /* Build an appropriate SELECT statement */ |
| 3120 | initText(&sSelect); |
| 3121 | appendText(&sSelect, "SELECT ", 0); |
| 3122 | if( azCol[0] ){ |
| 3123 | appendText(&sSelect, azCol[0], 0); |
| 3124 | appendText(&sSelect, ",", 0); |
| 3125 | } |
| 3126 | for(i=1; azCol[i]; i++){ |
| 3127 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
| 3128 | if( azCol[i+1] ){ |
| 3129 | appendText(&sSelect, ",", 0); |
| 3130 | } |
| 3131 | } |
| 3132 | freeColumnList(azCol); |
| 3133 | appendText(&sSelect, " FROM ", 0); |
| 3134 | appendText(&sSelect, zTable, quoteChar(zTable)); |
| 3135 | |
| 3136 | savedDestTable = p->zDestTable; |
| 3137 | savedMode = p->mode; |
| 3138 | p->zDestTable = sTable.z; |
| 3139 | p->mode = p->cMode = MODE_Insert; |
| 3140 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3141 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3142 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3143 | toggleSelectOrder(p->db); |
| 3144 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3145 | toggleSelectOrder(p->db); |
| 3146 | } |
| 3147 | p->zDestTable = savedDestTable; |
| 3148 | p->mode = savedMode; |
| 3149 | freeText(&sTable); |
| 3150 | freeText(&sSelect); |
| 3151 | if( rc ) p->nErr++; |
| 3152 | } |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3158 | ** the contents of the query are output as SQL statements. |
| 3159 | ** |
| 3160 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3161 | ** "ORDER BY rowid DESC" to the end. |
| 3162 | */ |
| 3163 | static int run_schema_dump_query( |
| 3164 | ShellState *p, |
| 3165 | const char *zQuery |
| 3166 | ){ |
| 3167 | int rc; |
| 3168 | char *zErr = 0; |
| 3169 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
| 3170 | if( rc==SQLITE_CORRUPT ){ |
| 3171 | char *zQ2; |
| 3172 | int len = strlen30(zQuery); |
| 3173 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3174 | if( zErr ){ |
| 3175 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
| 3176 | sqlite3_free(zErr); |
| 3177 | zErr = 0; |
| 3178 | } |
| 3179 | zQ2 = malloc( len+100 ); |
| 3180 | if( zQ2==0 ) return rc; |
| 3181 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
| 3182 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3183 | if( rc ){ |
| 3184 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
| 3185 | }else{ |
| 3186 | rc = SQLITE_CORRUPT; |
| 3187 | } |
| 3188 | sqlite3_free(zErr); |
| 3189 | free(zQ2); |
| 3190 | } |
| 3191 | return rc; |
| 3192 | } |
| 3193 | |
| 3194 | /* |
| 3195 | ** Text of a help message |
| 3196 | */ |
| 3197 | static char zHelp[] = |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 3198 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 3199 | ".archive ... Manage SQL archives: \".archive --help\" for details\n" |
| 3200 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3201 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3202 | ".auth ON|OFF Show authorizer callbacks\n" |
| 3203 | #endif |
| 3204 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
| 3205 | ".bail on|off Stop after hitting an error. Default OFF\n" |
| 3206 | ".binary on|off Turn binary output on or off. Default OFF\n" |
| 3207 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
| 3208 | ".changes on|off Show number of rows changed by SQL\n" |
| 3209 | ".check GLOB Fail if output since .testcase does not match\n" |
| 3210 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
| 3211 | ".databases List names and files of attached databases\n" |
| 3212 | ".dbinfo ?DB? Show status information about the database\n" |
| 3213 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
| 3214 | " If TABLE specified, only dump tables matching\n" |
| 3215 | " LIKE pattern TABLE.\n" |
| 3216 | ".echo on|off Turn command echo on or off\n" |
| 3217 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 3218 | ".excel Display the output of next command in a spreadsheet\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3219 | ".exit Exit this program\n" |
dan | 2e1ea57 | 2017-12-21 18:55:24 +0000 | [diff] [blame] | 3220 | ".expert EXPERIMENTAL. Suggest indexes for specified queries\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3221 | /* Because explain mode comes on automatically now, the ".explain" mode |
| 3222 | ** is removed from the help screen. It is still supported for legacy, however */ |
| 3223 | /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/ |
| 3224 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
| 3225 | ".headers on|off Turn display of headers on or off\n" |
| 3226 | ".help Show this message\n" |
| 3227 | ".import FILE TABLE Import data from FILE into TABLE\n" |
| 3228 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3229 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3230 | #endif |
| 3231 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3232 | " If TABLE specified, only show indexes for tables\n" |
| 3233 | " matching LIKE pattern TABLE.\n" |
| 3234 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3235 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3236 | #endif |
| 3237 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
| 3238 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3239 | " fkey-indexes Find missing foreign key indexes\n" |
| 3240 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3241 | ".load FILE ?ENTRY? Load an extension library\n" |
| 3242 | #endif |
| 3243 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
| 3244 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
| 3245 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
| 3246 | " csv Comma-separated values\n" |
| 3247 | " column Left-aligned columns. (See .width)\n" |
| 3248 | " html HTML <table> code\n" |
| 3249 | " insert SQL insert statements for TABLE\n" |
| 3250 | " line One value per line\n" |
| 3251 | " list Values delimited by \"|\"\n" |
| 3252 | " quote Escape answers as for SQL\n" |
| 3253 | " tabs Tab-separated values\n" |
| 3254 | " tcl TCL list elements\n" |
| 3255 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 3256 | ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n" |
| 3257 | " or invoke system text editor (-e) or spreadsheet (-x)\n" |
| 3258 | " on the output.\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3259 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n" |
| 3260 | " The --new option starts with an empty file\n" |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 3261 | ".output ?FILE? Send output to FILE or stdout\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3262 | ".print STRING... Print literal STRING\n" |
| 3263 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
| 3264 | ".quit Exit this program\n" |
| 3265 | ".read FILENAME Execute SQL in FILENAME\n" |
| 3266 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
| 3267 | ".save FILE Write in-memory database into FILE\n" |
| 3268 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
| 3269 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3270 | " Add --indent for pretty-printing\n" |
| 3271 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
| 3272 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3273 | " separator for both the output mode and .import\n" |
| 3274 | #if defined(SQLITE_ENABLE_SESSION) |
| 3275 | ".session CMD ... Create or control sessions\n" |
| 3276 | #endif |
| 3277 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3278 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3279 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3280 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3281 | ".show Show the current values for various settings\n" |
| 3282 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3283 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3284 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3285 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3286 | ".tables ?TABLE? List names of tables\n" |
| 3287 | " If TABLE specified, only list tables matching\n" |
| 3288 | " LIKE pattern TABLE.\n" |
| 3289 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
| 3290 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
| 3291 | ".timer on|off Turn SQL timer on or off\n" |
| 3292 | ".trace FILE|off Output each SQL statement as it is run\n" |
| 3293 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
| 3294 | ".vfslist List all available VFSes\n" |
| 3295 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
| 3296 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
| 3297 | " Negative values right-justify\n" |
| 3298 | ; |
| 3299 | |
| 3300 | #if defined(SQLITE_ENABLE_SESSION) |
| 3301 | /* |
| 3302 | ** Print help information for the ".sessions" command |
| 3303 | */ |
| 3304 | void session_help(ShellState *p){ |
| 3305 | raw_printf(p->out, |
| 3306 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3307 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3308 | "Subcommands:\n" |
| 3309 | " attach TABLE Attach TABLE\n" |
| 3310 | " changeset FILE Write a changeset into FILE\n" |
| 3311 | " close Close one session\n" |
| 3312 | " enable ?BOOLEAN? Set or query the enable bit\n" |
| 3313 | " filter GLOB... Reject tables matching GLOBs\n" |
| 3314 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3315 | " isempty Query whether the session is empty\n" |
| 3316 | " list List currently open session names\n" |
| 3317 | " open DB NAME Open a new session on DB\n" |
| 3318 | " patchset FILE Write a patchset into FILE\n" |
| 3319 | ); |
| 3320 | } |
| 3321 | #endif |
| 3322 | |
| 3323 | |
| 3324 | /* Forward reference */ |
| 3325 | static int process_input(ShellState *p, FILE *in); |
| 3326 | |
| 3327 | /* |
| 3328 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 3329 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3330 | ** the memory. |
| 3331 | ** |
| 3332 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3333 | ** read. |
| 3334 | ** |
| 3335 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3336 | ** from the file before the buffer is returned. This byte is not included in |
| 3337 | ** the final value of (*pnByte), if applicable. |
| 3338 | ** |
| 3339 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3340 | ** is undefined in this case. |
| 3341 | */ |
| 3342 | static char *readFile(const char *zName, int *pnByte){ |
| 3343 | FILE *in = fopen(zName, "rb"); |
| 3344 | long nIn; |
| 3345 | size_t nRead; |
| 3346 | char *pBuf; |
| 3347 | if( in==0 ) return 0; |
| 3348 | fseek(in, 0, SEEK_END); |
| 3349 | nIn = ftell(in); |
| 3350 | rewind(in); |
| 3351 | pBuf = sqlite3_malloc64( nIn+1 ); |
| 3352 | if( pBuf==0 ) return 0; |
| 3353 | nRead = fread(pBuf, nIn, 1, in); |
| 3354 | fclose(in); |
| 3355 | if( nRead!=1 ){ |
| 3356 | sqlite3_free(pBuf); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | pBuf[nIn] = 0; |
| 3360 | if( pnByte ) *pnByte = nIn; |
| 3361 | return pBuf; |
| 3362 | } |
| 3363 | |
| 3364 | #if defined(SQLITE_ENABLE_SESSION) |
| 3365 | /* |
| 3366 | ** Close a single OpenSession object and release all of its associated |
| 3367 | ** resources. |
| 3368 | */ |
| 3369 | static void session_close(OpenSession *pSession){ |
| 3370 | int i; |
| 3371 | sqlite3session_delete(pSession->p); |
| 3372 | sqlite3_free(pSession->zName); |
| 3373 | for(i=0; i<pSession->nFilter; i++){ |
| 3374 | sqlite3_free(pSession->azFilter[i]); |
| 3375 | } |
| 3376 | sqlite3_free(pSession->azFilter); |
| 3377 | memset(pSession, 0, sizeof(OpenSession)); |
| 3378 | } |
| 3379 | #endif |
| 3380 | |
| 3381 | /* |
| 3382 | ** Close all OpenSession objects and release all associated resources. |
| 3383 | */ |
| 3384 | #if defined(SQLITE_ENABLE_SESSION) |
| 3385 | static void session_close_all(ShellState *p){ |
| 3386 | int i; |
| 3387 | for(i=0; i<p->nSession; i++){ |
| 3388 | session_close(&p->aSession[i]); |
| 3389 | } |
| 3390 | p->nSession = 0; |
| 3391 | } |
| 3392 | #else |
| 3393 | # define session_close_all(X) |
| 3394 | #endif |
| 3395 | |
| 3396 | /* |
| 3397 | ** Implementation of the xFilter function for an open session. Omit |
| 3398 | ** any tables named by ".session filter" but let all other table through. |
| 3399 | */ |
| 3400 | #if defined(SQLITE_ENABLE_SESSION) |
| 3401 | static int session_filter(void *pCtx, const char *zTab){ |
| 3402 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3403 | int i; |
| 3404 | for(i=0; i<pSession->nFilter; i++){ |
| 3405 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3406 | } |
| 3407 | return 1; |
| 3408 | } |
| 3409 | #endif |
| 3410 | |
| 3411 | /* |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3412 | ** Try to deduce the type of file for zName based on its content. Return |
| 3413 | ** one of the SHELL_OPEN_* constants. |
| 3414 | */ |
| 3415 | static int deduceDatabaseType(const char *zName){ |
| 3416 | FILE *f = fopen(zName, "rb"); |
| 3417 | size_t n; |
| 3418 | int rc = SHELL_OPEN_UNSPEC; |
| 3419 | char zBuf[100]; |
| 3420 | if( f==0 ) return SHELL_OPEN_NORMAL; |
| 3421 | fseek(f, -25, SEEK_END); |
| 3422 | n = fread(zBuf, 25, 1, f); |
| 3423 | if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
| 3424 | rc = SHELL_OPEN_APPENDVFS; |
| 3425 | }else{ |
| 3426 | fseek(f, -22, SEEK_END); |
| 3427 | n = fread(zBuf, 22, 1, f); |
| 3428 | if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
| 3429 | && zBuf[3]==0x06 ){ |
| 3430 | rc = SHELL_OPEN_ZIPFILE; |
| 3431 | } |
| 3432 | } |
| 3433 | fclose(f); |
| 3434 | return rc; |
| 3435 | } |
| 3436 | |
| 3437 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3438 | ** Make sure the database is open. If it is not, then open it. If |
| 3439 | ** the database fails to open, print an error message and exit. |
| 3440 | */ |
| 3441 | static void open_db(ShellState *p, int keepAlive){ |
| 3442 | if( p->db==0 ){ |
| 3443 | sqlite3_initialize(); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3444 | if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 3445 | p->openMode = (u8)deduceDatabaseType(p->zDbFilename); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3446 | } |
| 3447 | switch( p->openMode ){ |
| 3448 | case SHELL_OPEN_APPENDVFS: { |
| 3449 | sqlite3_open_v2(p->zDbFilename, &p->db, |
| 3450 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs"); |
| 3451 | break; |
| 3452 | } |
| 3453 | case SHELL_OPEN_ZIPFILE: { |
| 3454 | sqlite3_open(":memory:", &p->db); |
| 3455 | break; |
| 3456 | } |
| 3457 | case SHELL_OPEN_UNSPEC: |
| 3458 | case SHELL_OPEN_NORMAL: { |
| 3459 | sqlite3_open(p->zDbFilename, &p->db); |
| 3460 | break; |
| 3461 | } |
| 3462 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3463 | globalDb = p->db; |
| 3464 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 3465 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
| 3466 | p->zDbFilename, sqlite3_errmsg(p->db)); |
| 3467 | if( keepAlive ) return; |
| 3468 | exit(1); |
| 3469 | } |
| 3470 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3471 | sqlite3_enable_load_extension(p->db, 1); |
| 3472 | #endif |
| 3473 | sqlite3_fileio_init(p->db, 0, 0); |
| 3474 | sqlite3_shathree_init(p->db, 0, 0); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3475 | sqlite3_completion_init(p->db, 0, 0); |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 3476 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 3477 | sqlite3_zipfile_init(p->db, 0, 0); |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 3478 | sqlite3_sqlar_init(p->db, 0, 0); |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 3479 | #endif |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 3480 | sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3481 | shellAddSchemaName, 0, 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 3482 | sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, |
| 3483 | shellModuleSchema, 0, 0); |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 3484 | sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, |
| 3485 | shellPutsFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3486 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 3487 | sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, |
| 3488 | editFunc, 0, 0); |
| 3489 | sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, |
| 3490 | editFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3491 | #endif |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3492 | if( p->openMode==SHELL_OPEN_ZIPFILE ){ |
| 3493 | char *zSql = sqlite3_mprintf( |
| 3494 | "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); |
| 3495 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 3496 | sqlite3_free(zSql); |
| 3497 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3498 | } |
| 3499 | } |
| 3500 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3501 | #if HAVE_READLINE || HAVE_EDITLINE |
| 3502 | /* |
| 3503 | ** Readline completion callbacks |
| 3504 | */ |
| 3505 | static char *readline_completion_generator(const char *text, int state){ |
| 3506 | static sqlite3_stmt *pStmt = 0; |
| 3507 | char *zRet; |
| 3508 | if( state==0 ){ |
| 3509 | char *zSql; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3510 | sqlite3_finalize(pStmt); |
| 3511 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 3512 | " FROM completion(%Q) ORDER BY 1", text); |
| 3513 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 3514 | sqlite3_free(zSql); |
| 3515 | } |
| 3516 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 968d871 | 2017-07-14 00:28:28 +0000 | [diff] [blame] | 3517 | zRet = strdup((const char*)sqlite3_column_text(pStmt, 0)); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3518 | }else{ |
| 3519 | sqlite3_finalize(pStmt); |
| 3520 | pStmt = 0; |
| 3521 | zRet = 0; |
| 3522 | } |
| 3523 | return zRet; |
| 3524 | } |
| 3525 | static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 3526 | rl_attempted_completion_over = 1; |
| 3527 | return rl_completion_matches(zText, readline_completion_generator); |
| 3528 | } |
| 3529 | |
| 3530 | #elif HAVE_LINENOISE |
| 3531 | /* |
| 3532 | ** Linenoise completion callback |
| 3533 | */ |
| 3534 | static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3535 | int nLine = strlen30(zLine); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3536 | int i, iStart; |
| 3537 | sqlite3_stmt *pStmt = 0; |
| 3538 | char *zSql; |
| 3539 | char zBuf[1000]; |
| 3540 | |
| 3541 | if( nLine>sizeof(zBuf)-30 ) return; |
| 3542 | if( zLine[0]=='.' ) return; |
| 3543 | for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 3544 | if( i==nLine-1 ) return; |
| 3545 | iStart = i+1; |
| 3546 | memcpy(zBuf, zLine, iStart); |
| 3547 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 3548 | " FROM completion(%Q,%Q) ORDER BY 1", |
| 3549 | &zLine[iStart], zLine); |
| 3550 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 3551 | sqlite3_free(zSql); |
| 3552 | sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 3553 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3554 | const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 3555 | int nCompletion = sqlite3_column_bytes(pStmt, 0); |
| 3556 | if( iStart+nCompletion < sizeof(zBuf)-1 ){ |
| 3557 | memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 3558 | linenoiseAddCompletion(lc, zBuf); |
| 3559 | } |
| 3560 | } |
| 3561 | sqlite3_finalize(pStmt); |
| 3562 | } |
| 3563 | #endif |
| 3564 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3565 | /* |
| 3566 | ** Do C-language style dequoting. |
| 3567 | ** |
| 3568 | ** \a -> alarm |
| 3569 | ** \b -> backspace |
| 3570 | ** \t -> tab |
| 3571 | ** \n -> newline |
| 3572 | ** \v -> vertical tab |
| 3573 | ** \f -> form feed |
| 3574 | ** \r -> carriage return |
| 3575 | ** \s -> space |
| 3576 | ** \" -> " |
| 3577 | ** \' -> ' |
| 3578 | ** \\ -> backslash |
| 3579 | ** \NNN -> ascii character NNN in octal |
| 3580 | */ |
| 3581 | static void resolve_backslashes(char *z){ |
| 3582 | int i, j; |
| 3583 | char c; |
| 3584 | while( *z && *z!='\\' ) z++; |
| 3585 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 3586 | if( c=='\\' && z[i+1]!=0 ){ |
| 3587 | c = z[++i]; |
| 3588 | if( c=='a' ){ |
| 3589 | c = '\a'; |
| 3590 | }else if( c=='b' ){ |
| 3591 | c = '\b'; |
| 3592 | }else if( c=='t' ){ |
| 3593 | c = '\t'; |
| 3594 | }else if( c=='n' ){ |
| 3595 | c = '\n'; |
| 3596 | }else if( c=='v' ){ |
| 3597 | c = '\v'; |
| 3598 | }else if( c=='f' ){ |
| 3599 | c = '\f'; |
| 3600 | }else if( c=='r' ){ |
| 3601 | c = '\r'; |
| 3602 | }else if( c=='"' ){ |
| 3603 | c = '"'; |
| 3604 | }else if( c=='\'' ){ |
| 3605 | c = '\''; |
| 3606 | }else if( c=='\\' ){ |
| 3607 | c = '\\'; |
| 3608 | }else if( c>='0' && c<='7' ){ |
| 3609 | c -= '0'; |
| 3610 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3611 | i++; |
| 3612 | c = (c<<3) + z[i] - '0'; |
| 3613 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3614 | i++; |
| 3615 | c = (c<<3) + z[i] - '0'; |
| 3616 | } |
| 3617 | } |
| 3618 | } |
| 3619 | } |
| 3620 | z[j] = c; |
| 3621 | } |
| 3622 | if( j<i ) z[j] = 0; |
| 3623 | } |
| 3624 | |
| 3625 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3626 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3627 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3628 | */ |
| 3629 | static int booleanValue(const char *zArg){ |
| 3630 | int i; |
| 3631 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3632 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3633 | }else{ |
| 3634 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3635 | } |
| 3636 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3637 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3638 | return 1; |
| 3639 | } |
| 3640 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3641 | return 0; |
| 3642 | } |
| 3643 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 3644 | zArg); |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | ** Set or clear a shell flag according to a boolean value. |
| 3650 | */ |
| 3651 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3652 | if( booleanValue(zArg) ){ |
| 3653 | ShellSetFlag(p, mFlag); |
| 3654 | }else{ |
| 3655 | ShellClearFlag(p, mFlag); |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | /* |
| 3660 | ** Close an output file, assuming it is not stderr or stdout |
| 3661 | */ |
| 3662 | static void output_file_close(FILE *f){ |
| 3663 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | ** Try to open an output file. The names "stdout" and "stderr" are |
| 3668 | ** recognized and do the right thing. NULL is returned if the output |
| 3669 | ** filename is "off". |
| 3670 | */ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 3671 | static FILE *output_file_open(const char *zFile, int bTextMode){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3672 | FILE *f; |
| 3673 | if( strcmp(zFile,"stdout")==0 ){ |
| 3674 | f = stdout; |
| 3675 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3676 | f = stderr; |
| 3677 | }else if( strcmp(zFile, "off")==0 ){ |
| 3678 | f = 0; |
| 3679 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 3680 | f = fopen(zFile, bTextMode ? "w" : "wb"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3681 | if( f==0 ){ |
| 3682 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 3683 | } |
| 3684 | } |
| 3685 | return f; |
| 3686 | } |
| 3687 | |
| 3688 | #if !defined(SQLITE_UNTESTABLE) |
| 3689 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 3690 | /* |
| 3691 | ** A routine for handling output from sqlite3_trace(). |
| 3692 | */ |
| 3693 | static int sql_trace_callback( |
| 3694 | unsigned mType, |
| 3695 | void *pArg, |
| 3696 | void *pP, |
| 3697 | void *pX |
| 3698 | ){ |
| 3699 | FILE *f = (FILE*)pArg; |
| 3700 | UNUSED_PARAMETER(mType); |
| 3701 | UNUSED_PARAMETER(pP); |
| 3702 | if( f ){ |
| 3703 | const char *z = (const char*)pX; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3704 | int i = strlen30(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3705 | while( i>0 && z[i-1]==';' ){ i--; } |
| 3706 | utf8_printf(f, "%.*s;\n", i, z); |
| 3707 | } |
| 3708 | return 0; |
| 3709 | } |
| 3710 | #endif |
| 3711 | #endif |
| 3712 | |
| 3713 | /* |
| 3714 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3715 | ** a useful spot to set a debugger breakpoint. |
| 3716 | */ |
| 3717 | static void test_breakpoint(void){ |
| 3718 | static int nCall = 0; |
| 3719 | nCall++; |
| 3720 | } |
| 3721 | |
| 3722 | /* |
| 3723 | ** An object used to read a CSV and other files for import. |
| 3724 | */ |
| 3725 | typedef struct ImportCtx ImportCtx; |
| 3726 | struct ImportCtx { |
| 3727 | const char *zFile; /* Name of the input file */ |
| 3728 | FILE *in; /* Read the CSV text from this input stream */ |
| 3729 | char *z; /* Accumulated text for a field */ |
| 3730 | int n; /* Number of bytes in z */ |
| 3731 | int nAlloc; /* Space allocated for z[] */ |
| 3732 | int nLine; /* Current line number */ |
| 3733 | int bNotFirst; /* True if one or more bytes already read */ |
| 3734 | int cTerm; /* Character that terminated the most recent field */ |
| 3735 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3736 | int cRowSep; /* The row separator character. (Usually "\n") */ |
| 3737 | }; |
| 3738 | |
| 3739 | /* Append a single byte to z[] */ |
| 3740 | static void import_append_char(ImportCtx *p, int c){ |
| 3741 | if( p->n+1>=p->nAlloc ){ |
| 3742 | p->nAlloc += p->nAlloc + 100; |
| 3743 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
| 3744 | if( p->z==0 ){ |
| 3745 | raw_printf(stderr, "out of memory\n"); |
| 3746 | exit(1); |
| 3747 | } |
| 3748 | } |
| 3749 | p->z[p->n++] = (char)c; |
| 3750 | } |
| 3751 | |
| 3752 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3753 | ** with the option of having a separator other than ",". |
| 3754 | ** |
| 3755 | ** + Input comes from p->in. |
| 3756 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 3757 | ** from sqlite3_malloc64(). |
| 3758 | ** + Use p->cSep as the column separator. The default is ",". |
| 3759 | ** + Use p->rSep as the row separator. The default is "\n". |
| 3760 | ** + Keep track of the line number in p->nLine. |
| 3761 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3762 | ** EOF on end-of-file. |
| 3763 | ** + Report syntax errors on stderr |
| 3764 | */ |
| 3765 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 3766 | int c; |
| 3767 | int cSep = p->cColSep; |
| 3768 | int rSep = p->cRowSep; |
| 3769 | p->n = 0; |
| 3770 | c = fgetc(p->in); |
| 3771 | if( c==EOF || seenInterrupt ){ |
| 3772 | p->cTerm = EOF; |
| 3773 | return 0; |
| 3774 | } |
| 3775 | if( c=='"' ){ |
| 3776 | int pc, ppc; |
| 3777 | int startLine = p->nLine; |
| 3778 | int cQuote = c; |
| 3779 | pc = ppc = 0; |
| 3780 | while( 1 ){ |
| 3781 | c = fgetc(p->in); |
| 3782 | if( c==rSep ) p->nLine++; |
| 3783 | if( c==cQuote ){ |
| 3784 | if( pc==cQuote ){ |
| 3785 | pc = 0; |
| 3786 | continue; |
| 3787 | } |
| 3788 | } |
| 3789 | if( (c==cSep && pc==cQuote) |
| 3790 | || (c==rSep && pc==cQuote) |
| 3791 | || (c==rSep && pc=='\r' && ppc==cQuote) |
| 3792 | || (c==EOF && pc==cQuote) |
| 3793 | ){ |
| 3794 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
| 3795 | p->cTerm = c; |
| 3796 | break; |
| 3797 | } |
| 3798 | if( pc==cQuote && c!='\r' ){ |
| 3799 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
| 3800 | p->zFile, p->nLine, cQuote); |
| 3801 | } |
| 3802 | if( c==EOF ){ |
| 3803 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
| 3804 | p->zFile, startLine, cQuote); |
| 3805 | p->cTerm = c; |
| 3806 | break; |
| 3807 | } |
| 3808 | import_append_char(p, c); |
| 3809 | ppc = pc; |
| 3810 | pc = c; |
| 3811 | } |
| 3812 | }else{ |
| 3813 | /* If this is the first field being parsed and it begins with the |
| 3814 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 3815 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 3816 | import_append_char(p, c); |
| 3817 | c = fgetc(p->in); |
| 3818 | if( (c&0xff)==0xbb ){ |
| 3819 | import_append_char(p, c); |
| 3820 | c = fgetc(p->in); |
| 3821 | if( (c&0xff)==0xbf ){ |
| 3822 | p->bNotFirst = 1; |
| 3823 | p->n = 0; |
| 3824 | return csv_read_one_field(p); |
| 3825 | } |
| 3826 | } |
| 3827 | } |
| 3828 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3829 | import_append_char(p, c); |
| 3830 | c = fgetc(p->in); |
| 3831 | } |
| 3832 | if( c==rSep ){ |
| 3833 | p->nLine++; |
| 3834 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 3835 | } |
| 3836 | p->cTerm = c; |
| 3837 | } |
| 3838 | if( p->z ) p->z[p->n] = 0; |
| 3839 | p->bNotFirst = 1; |
| 3840 | return p->z; |
| 3841 | } |
| 3842 | |
| 3843 | /* Read a single field of ASCII delimited text. |
| 3844 | ** |
| 3845 | ** + Input comes from p->in. |
| 3846 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 3847 | ** from sqlite3_malloc64(). |
| 3848 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3849 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3850 | ** + Keep track of the row number in p->nLine. |
| 3851 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3852 | ** EOF on end-of-file. |
| 3853 | ** + Report syntax errors on stderr |
| 3854 | */ |
| 3855 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 3856 | int c; |
| 3857 | int cSep = p->cColSep; |
| 3858 | int rSep = p->cRowSep; |
| 3859 | p->n = 0; |
| 3860 | c = fgetc(p->in); |
| 3861 | if( c==EOF || seenInterrupt ){ |
| 3862 | p->cTerm = EOF; |
| 3863 | return 0; |
| 3864 | } |
| 3865 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3866 | import_append_char(p, c); |
| 3867 | c = fgetc(p->in); |
| 3868 | } |
| 3869 | if( c==rSep ){ |
| 3870 | p->nLine++; |
| 3871 | } |
| 3872 | p->cTerm = c; |
| 3873 | if( p->z ) p->z[p->n] = 0; |
| 3874 | return p->z; |
| 3875 | } |
| 3876 | |
| 3877 | /* |
| 3878 | ** Try to transfer data for table zTable. If an error is seen while |
| 3879 | ** moving forward, try to go backwards. The backwards movement won't |
| 3880 | ** work for WITHOUT ROWID tables. |
| 3881 | */ |
| 3882 | static void tryToCloneData( |
| 3883 | ShellState *p, |
| 3884 | sqlite3 *newDb, |
| 3885 | const char *zTable |
| 3886 | ){ |
| 3887 | sqlite3_stmt *pQuery = 0; |
| 3888 | sqlite3_stmt *pInsert = 0; |
| 3889 | char *zQuery = 0; |
| 3890 | char *zInsert = 0; |
| 3891 | int rc; |
| 3892 | int i, j, n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3893 | int nTable = strlen30(zTable); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3894 | int k = 0; |
| 3895 | int cnt = 0; |
| 3896 | const int spinRate = 10000; |
| 3897 | |
| 3898 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3899 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3900 | if( rc ){ |
| 3901 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 3902 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3903 | zQuery); |
| 3904 | goto end_data_xfer; |
| 3905 | } |
| 3906 | n = sqlite3_column_count(pQuery); |
| 3907 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
| 3908 | if( zInsert==0 ){ |
| 3909 | raw_printf(stderr, "out of memory\n"); |
| 3910 | goto end_data_xfer; |
| 3911 | } |
| 3912 | sqlite3_snprintf(200+nTable,zInsert, |
| 3913 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3914 | i = strlen30(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3915 | for(j=1; j<n; j++){ |
| 3916 | memcpy(zInsert+i, ",?", 2); |
| 3917 | i += 2; |
| 3918 | } |
| 3919 | memcpy(zInsert+i, ");", 3); |
| 3920 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 3921 | if( rc ){ |
| 3922 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 3923 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 3924 | zQuery); |
| 3925 | goto end_data_xfer; |
| 3926 | } |
| 3927 | for(k=0; k<2; k++){ |
| 3928 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3929 | for(i=0; i<n; i++){ |
| 3930 | switch( sqlite3_column_type(pQuery, i) ){ |
| 3931 | case SQLITE_NULL: { |
| 3932 | sqlite3_bind_null(pInsert, i+1); |
| 3933 | break; |
| 3934 | } |
| 3935 | case SQLITE_INTEGER: { |
| 3936 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 3937 | break; |
| 3938 | } |
| 3939 | case SQLITE_FLOAT: { |
| 3940 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 3941 | break; |
| 3942 | } |
| 3943 | case SQLITE_TEXT: { |
| 3944 | sqlite3_bind_text(pInsert, i+1, |
| 3945 | (const char*)sqlite3_column_text(pQuery,i), |
| 3946 | -1, SQLITE_STATIC); |
| 3947 | break; |
| 3948 | } |
| 3949 | case SQLITE_BLOB: { |
| 3950 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 3951 | sqlite3_column_bytes(pQuery,i), |
| 3952 | SQLITE_STATIC); |
| 3953 | break; |
| 3954 | } |
| 3955 | } |
| 3956 | } /* End for */ |
| 3957 | rc = sqlite3_step(pInsert); |
| 3958 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 3959 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
| 3960 | sqlite3_errmsg(newDb)); |
| 3961 | } |
| 3962 | sqlite3_reset(pInsert); |
| 3963 | cnt++; |
| 3964 | if( (cnt%spinRate)==0 ){ |
| 3965 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 3966 | fflush(stdout); |
| 3967 | } |
| 3968 | } /* End while */ |
| 3969 | if( rc==SQLITE_DONE ) break; |
| 3970 | sqlite3_finalize(pQuery); |
| 3971 | sqlite3_free(zQuery); |
| 3972 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 3973 | zTable); |
| 3974 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3975 | if( rc ){ |
| 3976 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
| 3977 | break; |
| 3978 | } |
| 3979 | } /* End for(k=0...) */ |
| 3980 | |
| 3981 | end_data_xfer: |
| 3982 | sqlite3_finalize(pQuery); |
| 3983 | sqlite3_finalize(pInsert); |
| 3984 | sqlite3_free(zQuery); |
| 3985 | sqlite3_free(zInsert); |
| 3986 | } |
| 3987 | |
| 3988 | |
| 3989 | /* |
| 3990 | ** Try to transfer all rows of the schema that match zWhere. For |
| 3991 | ** each row, invoke xForEach() on the object defined by that row. |
| 3992 | ** If an error is encountered while moving forward through the |
| 3993 | ** sqlite_master table, try again moving backwards. |
| 3994 | */ |
| 3995 | static void tryToCloneSchema( |
| 3996 | ShellState *p, |
| 3997 | sqlite3 *newDb, |
| 3998 | const char *zWhere, |
| 3999 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 4000 | ){ |
| 4001 | sqlite3_stmt *pQuery = 0; |
| 4002 | char *zQuery = 0; |
| 4003 | int rc; |
| 4004 | const unsigned char *zName; |
| 4005 | const unsigned char *zSql; |
| 4006 | char *zErrMsg = 0; |
| 4007 | |
| 4008 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4009 | " WHERE %s", zWhere); |
| 4010 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4011 | if( rc ){ |
| 4012 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 4013 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4014 | zQuery); |
| 4015 | goto end_schema_xfer; |
| 4016 | } |
| 4017 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4018 | zName = sqlite3_column_text(pQuery, 0); |
| 4019 | zSql = sqlite3_column_text(pQuery, 1); |
| 4020 | printf("%s... ", zName); fflush(stdout); |
| 4021 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4022 | if( zErrMsg ){ |
| 4023 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 4024 | sqlite3_free(zErrMsg); |
| 4025 | zErrMsg = 0; |
| 4026 | } |
| 4027 | if( xForEach ){ |
| 4028 | xForEach(p, newDb, (const char*)zName); |
| 4029 | } |
| 4030 | printf("done\n"); |
| 4031 | } |
| 4032 | if( rc!=SQLITE_DONE ){ |
| 4033 | sqlite3_finalize(pQuery); |
| 4034 | sqlite3_free(zQuery); |
| 4035 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4036 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4037 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4038 | if( rc ){ |
| 4039 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 4040 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4041 | zQuery); |
| 4042 | goto end_schema_xfer; |
| 4043 | } |
| 4044 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4045 | zName = sqlite3_column_text(pQuery, 0); |
| 4046 | zSql = sqlite3_column_text(pQuery, 1); |
| 4047 | printf("%s... ", zName); fflush(stdout); |
| 4048 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4049 | if( zErrMsg ){ |
| 4050 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 4051 | sqlite3_free(zErrMsg); |
| 4052 | zErrMsg = 0; |
| 4053 | } |
| 4054 | if( xForEach ){ |
| 4055 | xForEach(p, newDb, (const char*)zName); |
| 4056 | } |
| 4057 | printf("done\n"); |
| 4058 | } |
| 4059 | } |
| 4060 | end_schema_xfer: |
| 4061 | sqlite3_finalize(pQuery); |
| 4062 | sqlite3_free(zQuery); |
| 4063 | } |
| 4064 | |
| 4065 | /* |
| 4066 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4067 | ** as possible out of the main database (which might be corrupt) and write it |
| 4068 | ** into zNewDb. |
| 4069 | */ |
| 4070 | static void tryToClone(ShellState *p, const char *zNewDb){ |
| 4071 | int rc; |
| 4072 | sqlite3 *newDb = 0; |
| 4073 | if( access(zNewDb,0)==0 ){ |
| 4074 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
| 4075 | return; |
| 4076 | } |
| 4077 | rc = sqlite3_open(zNewDb, &newDb); |
| 4078 | if( rc ){ |
| 4079 | utf8_printf(stderr, "Cannot create output database: %s\n", |
| 4080 | sqlite3_errmsg(newDb)); |
| 4081 | }else{ |
| 4082 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
| 4083 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
| 4084 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4085 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
| 4086 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
| 4087 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4088 | } |
| 4089 | sqlite3_close(newDb); |
| 4090 | } |
| 4091 | |
| 4092 | /* |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4093 | ** Change the output file back to stdout. |
| 4094 | ** |
| 4095 | ** If the p->doXdgOpen flag is set, that means the output was being |
| 4096 | ** redirected to a temporary file named by p->zTempFile. In that case, |
| 4097 | ** launch start/open/xdg-open on that temporary file. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4098 | */ |
| 4099 | static void output_reset(ShellState *p){ |
| 4100 | if( p->outfile[0]=='|' ){ |
| 4101 | #ifndef SQLITE_OMIT_POPEN |
| 4102 | pclose(p->out); |
| 4103 | #endif |
| 4104 | }else{ |
| 4105 | output_file_close(p->out); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4106 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4107 | if( p->doXdgOpen ){ |
| 4108 | const char *zXdgOpenCmd = |
| 4109 | #if defined(_WIN32) |
| 4110 | "start"; |
| 4111 | #elif defined(__APPLE__) |
| 4112 | "open"; |
| 4113 | #else |
| 4114 | "xdg-open"; |
| 4115 | #endif |
| 4116 | char *zCmd; |
| 4117 | zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 4118 | if( system(zCmd) ){ |
| 4119 | utf8_printf(stderr, "Failed: [%s]\n", zCmd); |
| 4120 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4121 | sqlite3_free(zCmd); |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 4122 | outputModePop(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4123 | p->doXdgOpen = 0; |
| 4124 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4125 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4126 | } |
| 4127 | p->outfile[0] = 0; |
| 4128 | p->out = stdout; |
| 4129 | } |
| 4130 | |
| 4131 | /* |
| 4132 | ** Run an SQL command and return the single integer result. |
| 4133 | */ |
| 4134 | static int db_int(ShellState *p, const char *zSql){ |
| 4135 | sqlite3_stmt *pStmt; |
| 4136 | int res = 0; |
| 4137 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4138 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4139 | res = sqlite3_column_int(pStmt,0); |
| 4140 | } |
| 4141 | sqlite3_finalize(pStmt); |
| 4142 | return res; |
| 4143 | } |
| 4144 | |
| 4145 | /* |
| 4146 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4147 | */ |
| 4148 | static unsigned int get2byteInt(unsigned char *a){ |
| 4149 | return (a[0]<<8) + a[1]; |
| 4150 | } |
| 4151 | static unsigned int get4byteInt(unsigned char *a){ |
| 4152 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4153 | } |
| 4154 | |
| 4155 | /* |
| 4156 | ** Implementation of the ".info" command. |
| 4157 | ** |
| 4158 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4159 | */ |
| 4160 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 4161 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4162 | { "file change counter:", 24 }, |
| 4163 | { "database page count:", 28 }, |
| 4164 | { "freelist page count:", 36 }, |
| 4165 | { "schema cookie:", 40 }, |
| 4166 | { "schema format:", 44 }, |
| 4167 | { "default cache size:", 48 }, |
| 4168 | { "autovacuum top root:", 52 }, |
| 4169 | { "incremental vacuum:", 64 }, |
| 4170 | { "text encoding:", 56 }, |
| 4171 | { "user version:", 60 }, |
| 4172 | { "application id:", 68 }, |
| 4173 | { "software version:", 96 }, |
| 4174 | }; |
| 4175 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4176 | { "number of tables:", |
| 4177 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4178 | { "number of indexes:", |
| 4179 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4180 | { "number of triggers:", |
| 4181 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4182 | { "number of views:", |
| 4183 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4184 | { "schema size:", |
| 4185 | "SELECT total(length(sql)) FROM %s" }, |
| 4186 | }; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4187 | int i; |
| 4188 | char *zSchemaTab; |
| 4189 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4190 | sqlite3_stmt *pStmt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4191 | unsigned char aHdr[100]; |
| 4192 | open_db(p, 0); |
| 4193 | if( p->db==0 ) return 1; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4194 | sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
| 4195 | -1, &pStmt, 0); |
| 4196 | sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
| 4197 | if( sqlite3_step(pStmt)==SQLITE_ROW |
| 4198 | && sqlite3_column_bytes(pStmt,0)>100 |
| 4199 | ){ |
| 4200 | memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
| 4201 | sqlite3_finalize(pStmt); |
| 4202 | }else{ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4203 | raw_printf(stderr, "unable to read database header\n"); |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4204 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4205 | return 1; |
| 4206 | } |
| 4207 | i = get2byteInt(aHdr+16); |
| 4208 | if( i==1 ) i = 65536; |
| 4209 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4210 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4211 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4212 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
| 4213 | for(i=0; i<ArraySize(aField); i++){ |
| 4214 | int ofst = aField[i].ofst; |
| 4215 | unsigned int val = get4byteInt(aHdr + ofst); |
| 4216 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
| 4217 | switch( ofst ){ |
| 4218 | case 56: { |
| 4219 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4220 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4221 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
| 4222 | } |
| 4223 | } |
| 4224 | raw_printf(p->out, "\n"); |
| 4225 | } |
| 4226 | if( zDb==0 ){ |
| 4227 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4228 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4229 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4230 | }else{ |
| 4231 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4232 | } |
| 4233 | for(i=0; i<ArraySize(aQuery); i++){ |
| 4234 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4235 | int val = db_int(p, zSql); |
| 4236 | sqlite3_free(zSql); |
| 4237 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
| 4238 | } |
| 4239 | sqlite3_free(zSchemaTab); |
| 4240 | return 0; |
| 4241 | } |
| 4242 | |
| 4243 | /* |
| 4244 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4245 | */ |
| 4246 | static int shellDatabaseError(sqlite3 *db){ |
| 4247 | const char *zErr = sqlite3_errmsg(db); |
| 4248 | utf8_printf(stderr, "Error: %s\n", zErr); |
| 4249 | return 1; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | ** Print an out-of-memory message to stderr and return 1. |
| 4254 | */ |
| 4255 | static int shellNomemError(void){ |
| 4256 | raw_printf(stderr, "Error: out of memory\n"); |
| 4257 | return 1; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4262 | ** if they match and FALSE (0) if they do not match. |
| 4263 | ** |
| 4264 | ** Globbing rules: |
| 4265 | ** |
| 4266 | ** '*' Matches any sequence of zero or more characters. |
| 4267 | ** |
| 4268 | ** '?' Matches exactly one character. |
| 4269 | ** |
| 4270 | ** [...] Matches one character from the enclosed list of |
| 4271 | ** characters. |
| 4272 | ** |
| 4273 | ** [^...] Matches one character not in the enclosed list. |
| 4274 | ** |
| 4275 | ** '#' Matches any sequence of one or more digits with an |
| 4276 | ** optional + or - sign in front |
| 4277 | ** |
| 4278 | ** ' ' Any span of whitespace matches any other span of |
| 4279 | ** whitespace. |
| 4280 | ** |
| 4281 | ** Extra whitespace at the end of z[] is ignored. |
| 4282 | */ |
| 4283 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4284 | int c, c2; |
| 4285 | int invert; |
| 4286 | int seen; |
| 4287 | |
| 4288 | while( (c = (*(zGlob++)))!=0 ){ |
| 4289 | if( IsSpace(c) ){ |
| 4290 | if( !IsSpace(*z) ) return 0; |
| 4291 | while( IsSpace(*zGlob) ) zGlob++; |
| 4292 | while( IsSpace(*z) ) z++; |
| 4293 | }else if( c=='*' ){ |
| 4294 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4295 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4296 | } |
| 4297 | if( c==0 ){ |
| 4298 | return 1; |
| 4299 | }else if( c=='[' ){ |
| 4300 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4301 | z++; |
| 4302 | } |
| 4303 | return (*z)!=0; |
| 4304 | } |
| 4305 | while( (c2 = (*(z++)))!=0 ){ |
| 4306 | while( c2!=c ){ |
| 4307 | c2 = *(z++); |
| 4308 | if( c2==0 ) return 0; |
| 4309 | } |
| 4310 | if( testcase_glob(zGlob,z) ) return 1; |
| 4311 | } |
| 4312 | return 0; |
| 4313 | }else if( c=='?' ){ |
| 4314 | if( (*(z++))==0 ) return 0; |
| 4315 | }else if( c=='[' ){ |
| 4316 | int prior_c = 0; |
| 4317 | seen = 0; |
| 4318 | invert = 0; |
| 4319 | c = *(z++); |
| 4320 | if( c==0 ) return 0; |
| 4321 | c2 = *(zGlob++); |
| 4322 | if( c2=='^' ){ |
| 4323 | invert = 1; |
| 4324 | c2 = *(zGlob++); |
| 4325 | } |
| 4326 | if( c2==']' ){ |
| 4327 | if( c==']' ) seen = 1; |
| 4328 | c2 = *(zGlob++); |
| 4329 | } |
| 4330 | while( c2 && c2!=']' ){ |
| 4331 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4332 | c2 = *(zGlob++); |
| 4333 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4334 | prior_c = 0; |
| 4335 | }else{ |
| 4336 | if( c==c2 ){ |
| 4337 | seen = 1; |
| 4338 | } |
| 4339 | prior_c = c2; |
| 4340 | } |
| 4341 | c2 = *(zGlob++); |
| 4342 | } |
| 4343 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4344 | }else if( c=='#' ){ |
| 4345 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4346 | if( !IsDigit(z[0]) ) return 0; |
| 4347 | z++; |
| 4348 | while( IsDigit(z[0]) ){ z++; } |
| 4349 | }else{ |
| 4350 | if( c!=(*(z++)) ) return 0; |
| 4351 | } |
| 4352 | } |
| 4353 | while( IsSpace(*z) ){ z++; } |
| 4354 | return *z==0; |
| 4355 | } |
| 4356 | |
| 4357 | |
| 4358 | /* |
| 4359 | ** Compare the string as a command-line option with either one or two |
| 4360 | ** initial "-" characters. |
| 4361 | */ |
| 4362 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4363 | if( zStr[0]!='-' ) return 0; |
| 4364 | zStr++; |
| 4365 | if( zStr[0]=='-' ) zStr++; |
| 4366 | return strcmp(zStr, zOpt)==0; |
| 4367 | } |
| 4368 | |
| 4369 | /* |
| 4370 | ** Delete a file. |
| 4371 | */ |
| 4372 | int shellDeleteFile(const char *zFilename){ |
| 4373 | int rc; |
| 4374 | #ifdef _WIN32 |
| 4375 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
| 4376 | rc = _wunlink(z); |
| 4377 | sqlite3_free(z); |
| 4378 | #else |
| 4379 | rc = unlink(zFilename); |
| 4380 | #endif |
| 4381 | return rc; |
| 4382 | } |
| 4383 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4384 | /* |
| 4385 | ** Try to delete the temporary file (if there is one) and free the |
| 4386 | ** memory used to hold the name of the temp file. |
| 4387 | */ |
| 4388 | static void clearTempFile(ShellState *p){ |
| 4389 | if( p->zTempFile==0 ) return; |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 4390 | if( p->doXdgOpen ) return; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4391 | if( shellDeleteFile(p->zTempFile) ) return; |
| 4392 | sqlite3_free(p->zTempFile); |
| 4393 | p->zTempFile = 0; |
| 4394 | } |
| 4395 | |
| 4396 | /* |
| 4397 | ** Create a new temp file name with the given suffix. |
| 4398 | */ |
| 4399 | static void newTempFile(ShellState *p, const char *zSuffix){ |
| 4400 | clearTempFile(p); |
| 4401 | sqlite3_free(p->zTempFile); |
| 4402 | p->zTempFile = 0; |
drh | 7f3bf8a | 2018-01-10 21:50:08 +0000 | [diff] [blame] | 4403 | if( p->db ){ |
| 4404 | sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); |
| 4405 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4406 | if( p->zTempFile==0 ){ |
| 4407 | sqlite3_uint64 r; |
| 4408 | sqlite3_randomness(sizeof(r), &r); |
| 4409 | p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix); |
| 4410 | }else{ |
| 4411 | p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); |
| 4412 | } |
| 4413 | if( p->zTempFile==0 ){ |
| 4414 | raw_printf(stderr, "out of memory\n"); |
| 4415 | exit(1); |
| 4416 | } |
| 4417 | } |
| 4418 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4419 | |
| 4420 | /* |
| 4421 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
| 4422 | ** by the ".lint fkey-indexes" command. This scalar function is always |
| 4423 | ** called with four arguments - the parent table name, the parent column name, |
| 4424 | ** the child table name and the child column name. |
| 4425 | ** |
| 4426 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4427 | ** |
| 4428 | ** If either of the named tables or columns do not exist, this function |
| 4429 | ** returns an empty string. An empty string is also returned if both tables |
| 4430 | ** and columns exist but have the same default collation sequence. Or, |
| 4431 | ** if both exist but the default collation sequences are different, this |
| 4432 | ** function returns the string " COLLATE <parent-collation>", where |
| 4433 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4434 | */ |
| 4435 | static void shellFkeyCollateClause( |
| 4436 | sqlite3_context *pCtx, |
| 4437 | int nVal, |
| 4438 | sqlite3_value **apVal |
| 4439 | ){ |
| 4440 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4441 | const char *zParent; |
| 4442 | const char *zParentCol; |
| 4443 | const char *zParentSeq; |
| 4444 | const char *zChild; |
| 4445 | const char *zChildCol; |
| 4446 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
| 4447 | int rc; |
| 4448 | |
| 4449 | assert( nVal==4 ); |
| 4450 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4451 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4452 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4453 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4454 | |
| 4455 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4456 | rc = sqlite3_table_column_metadata( |
| 4457 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4458 | ); |
| 4459 | if( rc==SQLITE_OK ){ |
| 4460 | rc = sqlite3_table_column_metadata( |
| 4461 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4462 | ); |
| 4463 | } |
| 4464 | |
| 4465 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4466 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4467 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4468 | sqlite3_free(z); |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | |
| 4473 | /* |
| 4474 | ** The implementation of dot-command ".lint fkey-indexes". |
| 4475 | */ |
| 4476 | static int lintFkeyIndexes( |
| 4477 | ShellState *pState, /* Current shell tool state */ |
| 4478 | char **azArg, /* Array of arguments passed to dot command */ |
| 4479 | int nArg /* Number of entries in azArg[] */ |
| 4480 | ){ |
| 4481 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4482 | FILE *out = pState->out; /* Stream to write non-error output to */ |
| 4483 | int bVerbose = 0; /* If -verbose is present */ |
| 4484 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
| 4485 | int i; /* To iterate through azArg[] */ |
| 4486 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4487 | int rc; /* Return code */ |
| 4488 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
| 4489 | |
| 4490 | /* |
| 4491 | ** This SELECT statement returns one row for each foreign key constraint |
| 4492 | ** in the schema of the main database. The column values are: |
| 4493 | ** |
| 4494 | ** 0. The text of an SQL statement similar to: |
| 4495 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4496 | ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4497 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4498 | ** This SELECT is similar to the one that the foreign keys implementation |
| 4499 | ** 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] | 4500 | ** be used to optimize this query, then it can also be used by the FK |
| 4501 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4502 | ** table. |
| 4503 | ** |
| 4504 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4505 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4506 | ** contains an index that can be used to optimize the query. |
| 4507 | ** |
| 4508 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4509 | ** |
| 4510 | ** "child_table(child_key1, child_key2)" |
| 4511 | ** |
| 4512 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4513 | ** |
| 4514 | ** "parent_table(parent_key1, parent_key2)" |
| 4515 | ** |
| 4516 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4517 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4518 | ** |
| 4519 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4520 | ** |
| 4521 | ** 5. The name of the parent table. |
| 4522 | ** |
| 4523 | ** These six values are used by the C logic below to generate the report. |
| 4524 | */ |
| 4525 | const char *zSql = |
| 4526 | "SELECT " |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4527 | " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4528 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 4529 | " || fkey_collate_clause(" |
| 4530 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
| 4531 | ", " |
| 4532 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
| 4533 | " || group_concat('*=?', ' AND ') || ')'" |
| 4534 | ", " |
| 4535 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
| 4536 | ", " |
| 4537 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
| 4538 | ", " |
| 4539 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4540 | " || ' ON ' || quote(s.name) || '('" |
| 4541 | " || group_concat(quote(f.[from]) ||" |
| 4542 | " fkey_collate_clause(" |
| 4543 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
| 4544 | " || ');'" |
| 4545 | ", " |
| 4546 | " f.[table] " |
| 4547 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
| 4548 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
| 4549 | "GROUP BY s.name, f.id " |
| 4550 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
| 4551 | ; |
| 4552 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
| 4553 | |
| 4554 | for(i=2; i<nArg; i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4555 | int n = strlen30(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4556 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4557 | bVerbose = 1; |
| 4558 | } |
| 4559 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4560 | bGroupByParent = 1; |
| 4561 | zIndent = " "; |
| 4562 | } |
| 4563 | else{ |
| 4564 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4565 | azArg[0], azArg[1] |
| 4566 | ); |
| 4567 | return SQLITE_ERROR; |
| 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | /* Register the fkey_collate_clause() SQL function */ |
| 4572 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4573 | 0, shellFkeyCollateClause, 0, 0 |
| 4574 | ); |
| 4575 | |
| 4576 | |
| 4577 | if( rc==SQLITE_OK ){ |
| 4578 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4579 | } |
| 4580 | if( rc==SQLITE_OK ){ |
| 4581 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4582 | } |
| 4583 | |
| 4584 | if( rc==SQLITE_OK ){ |
| 4585 | int rc2; |
| 4586 | char *zPrev = 0; |
| 4587 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4588 | int res = -1; |
| 4589 | sqlite3_stmt *pExplain = 0; |
| 4590 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4591 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4592 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4593 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4594 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
| 4595 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
| 4596 | |
| 4597 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4598 | if( rc!=SQLITE_OK ) break; |
| 4599 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4600 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
| 4601 | res = ( |
| 4602 | 0==sqlite3_strglob(zGlob, zPlan) |
| 4603 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 4604 | ); |
| 4605 | } |
| 4606 | rc = sqlite3_finalize(pExplain); |
| 4607 | if( rc!=SQLITE_OK ) break; |
| 4608 | |
| 4609 | if( res<0 ){ |
| 4610 | raw_printf(stderr, "Error: internal error"); |
| 4611 | break; |
| 4612 | }else{ |
| 4613 | if( bGroupByParent |
| 4614 | && (bVerbose || res==0) |
| 4615 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 4616 | ){ |
| 4617 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4618 | sqlite3_free(zPrev); |
| 4619 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4620 | } |
| 4621 | |
| 4622 | if( res==0 ){ |
| 4623 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4624 | }else if( bVerbose ){ |
| 4625 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 4626 | zIndent, zFrom, zTarget |
| 4627 | ); |
| 4628 | } |
| 4629 | } |
| 4630 | } |
| 4631 | sqlite3_free(zPrev); |
| 4632 | |
| 4633 | if( rc!=SQLITE_OK ){ |
| 4634 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4635 | } |
| 4636 | |
| 4637 | rc2 = sqlite3_finalize(pSql); |
| 4638 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4639 | rc = rc2; |
| 4640 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4641 | } |
| 4642 | }else{ |
| 4643 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4644 | } |
| 4645 | |
| 4646 | return rc; |
| 4647 | } |
| 4648 | |
| 4649 | /* |
| 4650 | ** Implementation of ".lint" dot command. |
| 4651 | */ |
| 4652 | static int lintDotCommand( |
| 4653 | ShellState *pState, /* Current shell tool state */ |
| 4654 | char **azArg, /* Array of arguments passed to dot command */ |
| 4655 | int nArg /* Number of entries in azArg[] */ |
| 4656 | ){ |
| 4657 | int n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4658 | n = (nArg>=2 ? strlen30(azArg[1]) : 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4659 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4660 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4661 | |
| 4662 | usage: |
| 4663 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4664 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4665 | raw_printf(stderr, " fkey-indexes\n"); |
| 4666 | return SQLITE_ERROR; |
| 4667 | } |
| 4668 | |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4669 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 4670 | /********************************************************************************* |
| 4671 | ** The ".archive" or ".ar" command. |
| 4672 | */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4673 | static void shellPrepare( |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4674 | sqlite3 *db, |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4675 | int *pRc, |
| 4676 | const char *zSql, |
| 4677 | sqlite3_stmt **ppStmt |
| 4678 | ){ |
| 4679 | *ppStmt = 0; |
| 4680 | if( *pRc==SQLITE_OK ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4681 | int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4682 | if( rc!=SQLITE_OK ){ |
| 4683 | raw_printf(stderr, "sql error: %s (%d)\n", |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4684 | sqlite3_errmsg(db), sqlite3_errcode(db) |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4685 | ); |
| 4686 | *pRc = rc; |
| 4687 | } |
| 4688 | } |
| 4689 | } |
| 4690 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4691 | static void shellPreparePrintf( |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4692 | sqlite3 *db, |
| 4693 | int *pRc, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4694 | sqlite3_stmt **ppStmt, |
| 4695 | const char *zFmt, |
| 4696 | ... |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4697 | ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4698 | *ppStmt = 0; |
| 4699 | if( *pRc==SQLITE_OK ){ |
| 4700 | va_list ap; |
| 4701 | char *z; |
| 4702 | va_start(ap, zFmt); |
| 4703 | z = sqlite3_vmprintf(zFmt, ap); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4704 | if( z==0 ){ |
| 4705 | *pRc = SQLITE_NOMEM; |
| 4706 | }else{ |
| 4707 | shellPrepare(db, pRc, z, ppStmt); |
| 4708 | sqlite3_free(z); |
| 4709 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4713 | static void shellFinalize( |
| 4714 | int *pRc, |
| 4715 | sqlite3_stmt *pStmt |
| 4716 | ){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 4717 | if( pStmt ){ |
| 4718 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 4719 | int rc = sqlite3_finalize(pStmt); |
| 4720 | if( *pRc==SQLITE_OK ){ |
| 4721 | if( rc!=SQLITE_OK ){ |
| 4722 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 4723 | } |
| 4724 | *pRc = rc; |
| 4725 | } |
| 4726 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | static void shellReset( |
| 4730 | int *pRc, |
| 4731 | sqlite3_stmt *pStmt |
| 4732 | ){ |
| 4733 | int rc = sqlite3_reset(pStmt); |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 4734 | if( *pRc==SQLITE_OK ){ |
| 4735 | if( rc!=SQLITE_OK ){ |
| 4736 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 4737 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 4738 | } |
| 4739 | *pRc = rc; |
| 4740 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4741 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4742 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4743 | ** Structure representing a single ".ar" command. |
| 4744 | */ |
| 4745 | typedef struct ArCommand ArCommand; |
| 4746 | struct ArCommand { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4747 | u8 eCmd; /* An AR_CMD_* value */ |
| 4748 | u8 bVerbose; /* True if --verbose */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4749 | u8 bZip; /* True if the archive is a ZIP */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4750 | u8 bDryRun; /* True if --dry-run */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4751 | u8 bAppend; /* True if --append */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4752 | int nArg; /* Number of command arguments */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4753 | char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4754 | const char *zFile; /* --file argument, or NULL */ |
| 4755 | const char *zDir; /* --directory argument, or NULL */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4756 | char **azArg; /* Array of command arguments */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4757 | ShellState *p; /* Shell state */ |
| 4758 | sqlite3 *db; /* Database containing the archive */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4759 | }; |
| 4760 | |
| 4761 | /* |
| 4762 | ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. |
| 4763 | */ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4764 | static int arUsage(FILE *f){ |
| 4765 | raw_printf(f, |
| 4766 | "\n" |
| 4767 | "Usage: .ar [OPTION...] [FILE...]\n" |
| 4768 | "The .ar command manages sqlar archives.\n" |
| 4769 | "\n" |
| 4770 | "Examples:\n" |
| 4771 | " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar\n" |
| 4772 | " .ar -tf archive.sar # List members of archive.sar\n" |
| 4773 | " .ar -xvf archive.sar # Verbosely extract files from archive.sar\n" |
| 4774 | "\n" |
| 4775 | "Each command line must feature exactly one command option:\n" |
| 4776 | " -c, --create Create a new archive\n" |
| 4777 | " -u, --update Update or add files to an existing archive\n" |
| 4778 | " -t, --list List contents of archive\n" |
| 4779 | " -x, --extract Extract files from archive\n" |
| 4780 | "\n" |
| 4781 | "And zero or more optional options:\n" |
| 4782 | " -v, --verbose Print each filename as it is processed\n" |
| 4783 | " -f FILE, --file FILE Operate on archive FILE (default is current db)\n" |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 4784 | " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n" |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4785 | " -C DIR, --directory DIR Change to directory DIR to read/extract files\n" |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4786 | " -n, --dryrun Show the SQL that would have occurred\n" |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4787 | "\n" |
| 4788 | "See also: http://sqlite.org/cli.html#sqlar_archive_support\n" |
| 4789 | "\n" |
| 4790 | ); |
| 4791 | return SQLITE_ERROR; |
| 4792 | } |
| 4793 | |
| 4794 | /* |
| 4795 | ** Print an error message for the .ar command to stderr and return |
| 4796 | ** SQLITE_ERROR. |
| 4797 | */ |
| 4798 | static int arErrorMsg(const char *zFmt, ...){ |
| 4799 | va_list ap; |
| 4800 | char *z; |
| 4801 | va_start(ap, zFmt); |
| 4802 | z = sqlite3_vmprintf(zFmt, ap); |
| 4803 | va_end(ap); |
| 4804 | raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z); |
| 4805 | sqlite3_free(z); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4806 | return SQLITE_ERROR; |
| 4807 | } |
| 4808 | |
| 4809 | /* |
| 4810 | ** Values for ArCommand.eCmd. |
| 4811 | */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4812 | #define AR_CMD_CREATE 1 |
| 4813 | #define AR_CMD_EXTRACT 2 |
| 4814 | #define AR_CMD_LIST 3 |
| 4815 | #define AR_CMD_UPDATE 4 |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4816 | #define AR_CMD_HELP 5 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4817 | |
| 4818 | /* |
| 4819 | ** Other (non-command) switches. |
| 4820 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4821 | #define AR_SWITCH_VERBOSE 6 |
| 4822 | #define AR_SWITCH_FILE 7 |
| 4823 | #define AR_SWITCH_DIRECTORY 8 |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4824 | #define AR_SWITCH_APPEND 9 |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4825 | #define AR_SWITCH_DRYRUN 10 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4826 | |
| 4827 | static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ |
| 4828 | switch( eSwitch ){ |
| 4829 | case AR_CMD_CREATE: |
| 4830 | case AR_CMD_EXTRACT: |
| 4831 | case AR_CMD_LIST: |
| 4832 | case AR_CMD_UPDATE: |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4833 | case AR_CMD_HELP: |
| 4834 | if( pAr->eCmd ){ |
| 4835 | return arErrorMsg("multiple command options"); |
| 4836 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4837 | pAr->eCmd = eSwitch; |
| 4838 | break; |
| 4839 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4840 | case AR_SWITCH_DRYRUN: |
| 4841 | pAr->bDryRun = 1; |
| 4842 | break; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4843 | case AR_SWITCH_VERBOSE: |
| 4844 | pAr->bVerbose = 1; |
| 4845 | break; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4846 | case AR_SWITCH_APPEND: |
| 4847 | pAr->bAppend = 1; |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 4848 | /* Fall thru into --file */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4849 | case AR_SWITCH_FILE: |
| 4850 | pAr->zFile = zArg; |
| 4851 | break; |
| 4852 | case AR_SWITCH_DIRECTORY: |
| 4853 | pAr->zDir = zArg; |
| 4854 | break; |
| 4855 | } |
| 4856 | |
| 4857 | return SQLITE_OK; |
| 4858 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4859 | |
| 4860 | /* |
| 4861 | ** Parse the command line for an ".ar" command. The results are written into |
| 4862 | ** structure (*pAr). SQLITE_OK is returned if the command line is parsed |
| 4863 | ** successfully, otherwise an error message is written to stderr and |
| 4864 | ** SQLITE_ERROR returned. |
| 4865 | */ |
| 4866 | static int arParseCommand( |
| 4867 | char **azArg, /* Array of arguments passed to dot command */ |
| 4868 | int nArg, /* Number of entries in azArg[] */ |
| 4869 | ArCommand *pAr /* Populate this object */ |
| 4870 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4871 | struct ArSwitch { |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4872 | const char *zLong; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4873 | char cShort; |
| 4874 | u8 eSwitch; |
| 4875 | u8 bArg; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4876 | } aSwitch[] = { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4877 | { "create", 'c', AR_CMD_CREATE, 0 }, |
| 4878 | { "extract", 'x', AR_CMD_EXTRACT, 0 }, |
| 4879 | { "list", 't', AR_CMD_LIST, 0 }, |
| 4880 | { "update", 'u', AR_CMD_UPDATE, 0 }, |
| 4881 | { "help", 'h', AR_CMD_HELP, 0 }, |
| 4882 | { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, |
| 4883 | { "file", 'f', AR_SWITCH_FILE, 1 }, |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 4884 | { "append", 'a', AR_SWITCH_APPEND, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4885 | { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4886 | { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4887 | }; |
| 4888 | int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); |
| 4889 | struct ArSwitch *pEnd = &aSwitch[nSwitch]; |
| 4890 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4891 | if( nArg<=1 ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4892 | return arUsage(stderr); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4893 | }else{ |
| 4894 | char *z = azArg[1]; |
| 4895 | memset(pAr, 0, sizeof(ArCommand)); |
| 4896 | |
| 4897 | if( z[0]!='-' ){ |
| 4898 | /* Traditional style [tar] invocation */ |
| 4899 | int i; |
| 4900 | int iArg = 2; |
| 4901 | for(i=0; z[i]; i++){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4902 | const char *zArg = 0; |
| 4903 | struct ArSwitch *pOpt; |
| 4904 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 4905 | if( z[i]==pOpt->cShort ) break; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4906 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4907 | if( pOpt==pEnd ){ |
| 4908 | return arErrorMsg("unrecognized option: %c", z[i]); |
| 4909 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4910 | if( pOpt->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4911 | if( iArg>=nArg ){ |
| 4912 | return arErrorMsg("option requires an argument: %c",z[i]); |
| 4913 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4914 | zArg = azArg[iArg++]; |
| 4915 | } |
| 4916 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4917 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4918 | pAr->nArg = nArg-iArg; |
| 4919 | if( pAr->nArg>0 ){ |
| 4920 | pAr->azArg = &azArg[iArg]; |
| 4921 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4922 | }else{ |
| 4923 | /* Non-traditional invocation */ |
| 4924 | int iArg; |
| 4925 | for(iArg=1; iArg<nArg; iArg++){ |
| 4926 | int n; |
| 4927 | z = azArg[iArg]; |
| 4928 | if( z[0]!='-' ){ |
| 4929 | /* All remaining command line words are command arguments. */ |
| 4930 | pAr->azArg = &azArg[iArg]; |
| 4931 | pAr->nArg = nArg-iArg; |
| 4932 | break; |
| 4933 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4934 | n = strlen30(z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4935 | |
| 4936 | if( z[1]!='-' ){ |
| 4937 | int i; |
| 4938 | /* One or more short options */ |
| 4939 | for(i=1; i<n; i++){ |
| 4940 | const char *zArg = 0; |
| 4941 | struct ArSwitch *pOpt; |
| 4942 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 4943 | if( z[i]==pOpt->cShort ) break; |
| 4944 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4945 | if( pOpt==pEnd ){ |
| 4946 | return arErrorMsg("unrecognized option: %c\n", z[i]); |
| 4947 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4948 | if( pOpt->bArg ){ |
| 4949 | if( i<(n-1) ){ |
| 4950 | zArg = &z[i+1]; |
| 4951 | i = n; |
| 4952 | }else{ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4953 | if( iArg>=(nArg-1) ){ |
| 4954 | return arErrorMsg("option requires an argument: %c\n",z[i]); |
| 4955 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4956 | zArg = azArg[++iArg]; |
| 4957 | } |
| 4958 | } |
| 4959 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
| 4960 | } |
| 4961 | }else if( z[2]=='\0' ){ |
| 4962 | /* A -- option, indicating that all remaining command line words |
| 4963 | ** are command arguments. */ |
| 4964 | pAr->azArg = &azArg[iArg+1]; |
| 4965 | pAr->nArg = nArg-iArg-1; |
| 4966 | break; |
| 4967 | }else{ |
| 4968 | /* A long option */ |
| 4969 | const char *zArg = 0; /* Argument for option, if any */ |
| 4970 | struct ArSwitch *pMatch = 0; /* Matching option */ |
| 4971 | struct ArSwitch *pOpt; /* Iterator */ |
| 4972 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 4973 | const char *zLong = pOpt->zLong; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4974 | if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4975 | if( pMatch ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4976 | return arErrorMsg("ambiguous option: %s",z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4977 | }else{ |
| 4978 | pMatch = pOpt; |
| 4979 | } |
| 4980 | } |
| 4981 | } |
| 4982 | |
| 4983 | if( pMatch==0 ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4984 | return arErrorMsg("unrecognized option: %s", z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4985 | } |
| 4986 | if( pMatch->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4987 | if( iArg>=(nArg-1) ){ |
| 4988 | return arErrorMsg("option requires an argument: %s", z); |
| 4989 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4990 | zArg = azArg[++iArg]; |
| 4991 | } |
| 4992 | if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; |
| 4993 | } |
| 4994 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | return SQLITE_OK; |
| 4999 | } |
| 5000 | |
| 5001 | /* |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5002 | ** This function assumes that all arguments within the ArCommand.azArg[] |
| 5003 | ** array refer to archive members, as for the --extract or --list commands. |
| 5004 | ** It checks that each of them are present. If any specified file is not |
| 5005 | ** present in the archive, an error is printed to stderr and an error |
| 5006 | ** code returned. Otherwise, if all specified arguments are present in |
| 5007 | ** the archive, SQLITE_OK is returned. |
| 5008 | ** |
| 5009 | ** This function strips any trailing '/' characters from each argument. |
| 5010 | ** This is consistent with the way the [tar] command seems to work on |
| 5011 | ** Linux. |
| 5012 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5013 | static int arCheckEntries(ArCommand *pAr){ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5014 | int rc = SQLITE_OK; |
| 5015 | if( pAr->nArg ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5016 | int i, j; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5017 | sqlite3_stmt *pTest = 0; |
| 5018 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5019 | shellPreparePrintf(pAr->db, &rc, &pTest, |
| 5020 | "SELECT name FROM %s WHERE name=$name", |
| 5021 | pAr->zSrcTable |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5022 | ); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5023 | j = sqlite3_bind_parameter_index(pTest, "$name"); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5024 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
| 5025 | char *z = pAr->azArg[i]; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5026 | int n = strlen30(z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5027 | int bOk = 0; |
| 5028 | while( n>0 && z[n-1]=='/' ) n--; |
| 5029 | z[n] = '\0'; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5030 | sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5031 | if( SQLITE_ROW==sqlite3_step(pTest) ){ |
| 5032 | bOk = 1; |
| 5033 | } |
| 5034 | shellReset(&rc, pTest); |
| 5035 | if( rc==SQLITE_OK && bOk==0 ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5036 | utf8_printf(stderr, "not found in archive: %s\n", z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5037 | rc = SQLITE_ERROR; |
| 5038 | } |
| 5039 | } |
| 5040 | shellFinalize(&rc, pTest); |
| 5041 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5042 | return rc; |
| 5043 | } |
| 5044 | |
| 5045 | /* |
| 5046 | ** Format a WHERE clause that can be used against the "sqlar" table to |
| 5047 | ** identify all archive members that match the command arguments held |
| 5048 | ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. |
| 5049 | ** The caller is responsible for eventually calling sqlite3_free() on |
| 5050 | ** any non-NULL (*pzWhere) value. |
| 5051 | */ |
| 5052 | static void arWhereClause( |
| 5053 | int *pRc, |
| 5054 | ArCommand *pAr, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5055 | char **pzWhere /* OUT: New WHERE clause */ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5056 | ){ |
| 5057 | char *zWhere = 0; |
| 5058 | if( *pRc==SQLITE_OK ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5059 | if( pAr->nArg==0 ){ |
| 5060 | zWhere = sqlite3_mprintf("1"); |
| 5061 | }else{ |
| 5062 | int i; |
| 5063 | const char *zSep = ""; |
| 5064 | for(i=0; i<pAr->nArg; i++){ |
| 5065 | const char *z = pAr->azArg[i]; |
| 5066 | zWhere = sqlite3_mprintf( |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5067 | "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", |
| 5068 | zWhere, zSep, z, strlen30(z)+1, z |
| 5069 | ); |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5070 | if( zWhere==0 ){ |
| 5071 | *pRc = SQLITE_NOMEM; |
| 5072 | break; |
| 5073 | } |
| 5074 | zSep = " OR "; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5075 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | *pzWhere = zWhere; |
| 5079 | } |
| 5080 | |
| 5081 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5082 | ** Implementation of .ar "lisT" command. |
| 5083 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5084 | static int arListCommand(ArCommand *pAr){ |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5085 | const char *zSql = "SELECT %s FROM %s WHERE %s"; |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5086 | const char *azCols[] = { |
| 5087 | "name", |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5088 | "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5089 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5090 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5091 | char *zWhere = 0; |
| 5092 | sqlite3_stmt *pSql = 0; |
| 5093 | int rc; |
| 5094 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5095 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5096 | arWhereClause(&rc, pAr, &zWhere); |
| 5097 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5098 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], |
| 5099 | pAr->zSrcTable, zWhere); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5100 | if( pAr->bDryRun ){ |
| 5101 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 5102 | }else{ |
| 5103 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 5104 | if( pAr->bVerbose ){ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5105 | utf8_printf(pAr->p->out, "%s % 10d %s %s\n", |
| 5106 | sqlite3_column_text(pSql, 0), |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5107 | sqlite3_column_int(pSql, 1), |
| 5108 | sqlite3_column_text(pSql, 2), |
| 5109 | sqlite3_column_text(pSql, 3) |
| 5110 | ); |
| 5111 | }else{ |
| 5112 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 5113 | } |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5114 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5115 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5116 | shellFinalize(&rc, pSql); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5117 | return rc; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5121 | /* |
| 5122 | ** Implementation of .ar "eXtract" command. |
| 5123 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5124 | static int arExtractCommand(ArCommand *pAr){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5125 | const char *zSql1 = |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 5126 | "SELECT " |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5127 | " ($dir || name)," |
| 5128 | " writefile(($dir || name), %s, mode, mtime) " |
| 5129 | "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5130 | |
| 5131 | const char *azExtraArg[] = { |
| 5132 | "sqlar_uncompress(data, sz)", |
dan | 7c15ac1 | 2018-01-08 19:59:59 +0000 | [diff] [blame] | 5133 | "data" |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5134 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5135 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5136 | sqlite3_stmt *pSql = 0; |
| 5137 | int rc = SQLITE_OK; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5138 | char *zDir = 0; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5139 | char *zWhere = 0; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5140 | int i, j; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5141 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5142 | /* If arguments are specified, check that they actually exist within |
| 5143 | ** the archive before proceeding. And formulate a WHERE clause to |
| 5144 | ** match them. */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5145 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5146 | arWhereClause(&rc, pAr, &zWhere); |
| 5147 | |
| 5148 | if( rc==SQLITE_OK ){ |
| 5149 | if( pAr->zDir ){ |
| 5150 | zDir = sqlite3_mprintf("%s/", pAr->zDir); |
| 5151 | }else{ |
| 5152 | zDir = sqlite3_mprintf(""); |
| 5153 | } |
| 5154 | if( zDir==0 ) rc = SQLITE_NOMEM; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5155 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5156 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5157 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, |
| 5158 | azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5159 | ); |
| 5160 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5161 | if( rc==SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5162 | j = sqlite3_bind_parameter_index(pSql, "$dir"); |
| 5163 | sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5164 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5165 | /* Run the SELECT statement twice. The first time, writefile() is called |
| 5166 | ** for all archive members that should be extracted. The second time, |
| 5167 | ** only for the directories. This is because the timestamps for |
| 5168 | ** extracted directories must be reset after they are populated (as |
| 5169 | ** populating them changes the timestamp). */ |
| 5170 | for(i=0; i<2; i++){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5171 | j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); |
| 5172 | sqlite3_bind_int(pSql, j, i); |
| 5173 | if( pAr->bDryRun ){ |
| 5174 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 5175 | }else{ |
| 5176 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 5177 | if( i==0 && pAr->bVerbose ){ |
| 5178 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 5179 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | shellReset(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5183 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5184 | shellFinalize(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5185 | } |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5186 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5187 | sqlite3_free(zDir); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5188 | sqlite3_free(zWhere); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5189 | return rc; |
| 5190 | } |
| 5191 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5192 | /* |
| 5193 | ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. |
| 5194 | */ |
| 5195 | static int arExecSql(ArCommand *pAr, const char *zSql){ |
| 5196 | int rc; |
| 5197 | if( pAr->bDryRun ){ |
| 5198 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 5199 | rc = SQLITE_OK; |
| 5200 | }else{ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5201 | char *zErr = 0; |
| 5202 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 5203 | if( zErr ){ |
| 5204 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 5205 | sqlite3_free(zErr); |
| 5206 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5207 | } |
| 5208 | return rc; |
| 5209 | } |
| 5210 | |
dan | 1ad3f61 | 2017-12-11 20:22:02 +0000 | [diff] [blame] | 5211 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5212 | /* |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5213 | ** Implementation of .ar "create" and "update" commands. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5214 | ** |
| 5215 | ** Create the "sqlar" table in the database if it does not already exist. |
| 5216 | ** Then add each file in the azFile[] array to the archive. Directories |
| 5217 | ** are added recursively. If argument bVerbose is non-zero, a message is |
| 5218 | ** printed on stdout for each file archived. |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5219 | ** |
| 5220 | ** The create command is the same as update, except that it drops |
| 5221 | ** any existing "sqlar" table before beginning. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5222 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5223 | static int arCreateOrUpdateCommand( |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5224 | ArCommand *pAr, /* Command arguments and options */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5225 | int bUpdate /* true for a --create. false for --update */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5226 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5227 | const char *zCreate = |
drh | afba180 | 2018-01-06 15:49:57 +0000 | [diff] [blame] | 5228 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 5229 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 5230 | " mode INT, -- access permissions\n" |
| 5231 | " mtime INT, -- last modification time\n" |
| 5232 | " sz INT, -- original file size\n" |
| 5233 | " data BLOB -- compressed content\n" |
| 5234 | ")"; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5235 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5236 | const char *zInsertFmt = |
| 5237 | "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n" |
| 5238 | " SELECT\n" |
| 5239 | " %s,\n" |
| 5240 | " mode,\n" |
| 5241 | " mtime,\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5242 | " CASE substr(lsmode(mode),1,1)\n" |
| 5243 | " WHEN '-' THEN length(data)\n" |
| 5244 | " WHEN 'd' THEN 0\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5245 | " ELSE -1 END,\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5246 | " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5247 | " FROM fsdir(%Q,%Q)\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5248 | " WHERE lsmode(mode) NOT LIKE '?%%';"; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5249 | int i; /* For iterating through azFile[] */ |
| 5250 | int rc; /* Return code */ |
| 5251 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5252 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5253 | if( rc!=SQLITE_OK ) return rc; |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5254 | if( bUpdate==0 ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5255 | rc = arExecSql(pAr, zDrop); |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5256 | if( rc!=SQLITE_OK ) return rc; |
| 5257 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5258 | rc = arExecSql(pAr, zCreate); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5259 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5260 | char *zSql = sqlite3_mprintf(zInsertFmt, |
| 5261 | pAr->bVerbose ? "shell_putsnl(name)" : "name", |
| 5262 | pAr->azArg[i], pAr->zDir); |
| 5263 | rc = arExecSql(pAr, zSql); |
| 5264 | sqlite3_free(zSql); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5265 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5266 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5267 | arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5268 | }else{ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5269 | rc = arExecSql(pAr, "RELEASE ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5270 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5271 | return rc; |
| 5272 | } |
| 5273 | |
| 5274 | /* |
| 5275 | ** Implementation of ".ar" dot command. |
| 5276 | */ |
| 5277 | static int arDotCommand( |
| 5278 | ShellState *pState, /* Current shell tool state */ |
| 5279 | char **azArg, /* Array of arguments passed to dot command */ |
| 5280 | int nArg /* Number of entries in azArg[] */ |
| 5281 | ){ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5282 | ArCommand cmd; |
| 5283 | int rc; |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 5284 | memset(&cmd, 0, sizeof(cmd)); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5285 | rc = arParseCommand(azArg, nArg, &cmd); |
| 5286 | if( rc==SQLITE_OK ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5287 | int eDbType = SHELL_OPEN_UNSPEC; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5288 | cmd.p = pState; |
| 5289 | cmd.db = pState->db; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5290 | if( cmd.zFile ){ |
| 5291 | eDbType = deduceDatabaseType(cmd.zFile); |
| 5292 | }else{ |
| 5293 | eDbType = pState->openMode; |
| 5294 | } |
| 5295 | if( eDbType==SHELL_OPEN_ZIPFILE ){ |
| 5296 | if( cmd.zFile==0 ){ |
| 5297 | cmd.zSrcTable = sqlite3_mprintf("zip"); |
drh | a82c95b | 2018-01-10 14:00:00 +0000 | [diff] [blame] | 5298 | }else{ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5299 | cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5300 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5301 | if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5302 | utf8_printf(stderr, "zip archives are read-only\n"); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5303 | rc = SQLITE_ERROR; |
| 5304 | goto end_ar_command; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5305 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5306 | cmd.bZip = 1; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5307 | }else if( cmd.zFile ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5308 | int flags; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5309 | if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5310 | if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){ |
| 5311 | flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 5312 | }else{ |
| 5313 | flags = SQLITE_OPEN_READONLY; |
| 5314 | } |
drh | a82c95b | 2018-01-10 14:00:00 +0000 | [diff] [blame] | 5315 | cmd.db = 0; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5316 | if( cmd.bDryRun ){ |
| 5317 | utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, |
| 5318 | eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); |
| 5319 | } |
| 5320 | rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, |
| 5321 | eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5322 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5323 | utf8_printf(stderr, "cannot open file: %s (%s)\n", |
| 5324 | cmd.zFile, sqlite3_errmsg(cmd.db) |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5325 | ); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5326 | goto end_ar_command; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5327 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5328 | sqlite3_fileio_init(cmd.db, 0, 0); |
mistachkin | 3acaf4c | 2018-01-05 00:53:03 +0000 | [diff] [blame] | 5329 | #ifdef SQLITE_HAVE_ZLIB |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5330 | sqlite3_sqlar_init(cmd.db, 0, 0); |
mistachkin | 3acaf4c | 2018-01-05 00:53:03 +0000 | [diff] [blame] | 5331 | #endif |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 5332 | sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, |
| 5333 | shellPutsFunc, 0, 0); |
| 5334 | |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5335 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5336 | if( cmd.zSrcTable==0 ){ |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5337 | if( cmd.eCmd!=AR_CMD_CREATE |
| 5338 | && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) |
| 5339 | ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5340 | utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); |
| 5341 | rc = SQLITE_ERROR; |
| 5342 | goto end_ar_command; |
| 5343 | } |
| 5344 | cmd.zSrcTable = sqlite3_mprintf("sqlar"); |
| 5345 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5346 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5347 | switch( cmd.eCmd ){ |
| 5348 | case AR_CMD_CREATE: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5349 | rc = arCreateOrUpdateCommand(&cmd, 0); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5350 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5351 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5352 | case AR_CMD_EXTRACT: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5353 | rc = arExtractCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5354 | break; |
| 5355 | |
| 5356 | case AR_CMD_LIST: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5357 | rc = arListCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5358 | break; |
| 5359 | |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5360 | case AR_CMD_HELP: |
| 5361 | arUsage(pState->out); |
| 5362 | break; |
| 5363 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5364 | default: |
| 5365 | assert( cmd.eCmd==AR_CMD_UPDATE ); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5366 | rc = arCreateOrUpdateCommand(&cmd, 1); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5367 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5368 | } |
| 5369 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5370 | end_ar_command: |
| 5371 | if( cmd.db!=pState->db ){ |
| 5372 | sqlite3_close(cmd.db); |
| 5373 | } |
| 5374 | sqlite3_free(cmd.zSrcTable); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5375 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5376 | return rc; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5377 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 5378 | /* End of the ".archive" or ".ar" command logic |
| 5379 | **********************************************************************************/ |
| 5380 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5381 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5382 | |
| 5383 | /* |
| 5384 | ** If an input line begins with "." then invoke this routine to |
| 5385 | ** process that line. |
| 5386 | ** |
| 5387 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 5388 | */ |
| 5389 | static int do_meta_command(char *zLine, ShellState *p){ |
| 5390 | int h = 1; |
| 5391 | int nArg = 0; |
| 5392 | int n, c; |
| 5393 | int rc = 0; |
| 5394 | char *azArg[50]; |
| 5395 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5396 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5397 | if( p->expert.pExpert ){ |
| 5398 | expertFinish(p, 1, 0); |
| 5399 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5400 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5401 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5402 | /* Parse the input line into tokens. |
| 5403 | */ |
| 5404 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 5405 | while( IsSpace(zLine[h]) ){ h++; } |
| 5406 | if( zLine[h]==0 ) break; |
| 5407 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 5408 | int delim = zLine[h++]; |
| 5409 | azArg[nArg++] = &zLine[h]; |
| 5410 | while( zLine[h] && zLine[h]!=delim ){ |
| 5411 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
| 5412 | h++; |
| 5413 | } |
| 5414 | if( zLine[h]==delim ){ |
| 5415 | zLine[h++] = 0; |
| 5416 | } |
| 5417 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
| 5418 | }else{ |
| 5419 | azArg[nArg++] = &zLine[h]; |
| 5420 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 5421 | if( zLine[h] ) zLine[h++] = 0; |
| 5422 | resolve_backslashes(azArg[nArg-1]); |
| 5423 | } |
| 5424 | } |
| 5425 | |
| 5426 | /* Process the input line. |
| 5427 | */ |
| 5428 | if( nArg==0 ) return 0; /* no tokens, no error */ |
| 5429 | n = strlen30(azArg[0]); |
| 5430 | c = azArg[0][0]; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5431 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5432 | |
| 5433 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 5434 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 5435 | if( nArg!=2 ){ |
| 5436 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 5437 | rc = 1; |
| 5438 | goto meta_command_exit; |
| 5439 | } |
| 5440 | open_db(p, 0); |
| 5441 | if( booleanValue(azArg[1]) ){ |
| 5442 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 5443 | }else{ |
| 5444 | sqlite3_set_authorizer(p->db, 0, 0); |
| 5445 | } |
| 5446 | }else |
| 5447 | #endif |
| 5448 | |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 5449 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 5450 | if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5451 | open_db(p, 0); |
| 5452 | rc = arDotCommand(p, azArg, nArg); |
| 5453 | }else |
| 5454 | #endif |
| 5455 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5456 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 5457 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 5458 | ){ |
| 5459 | const char *zDestFile = 0; |
| 5460 | const char *zDb = 0; |
| 5461 | sqlite3 *pDest; |
| 5462 | sqlite3_backup *pBackup; |
| 5463 | int j; |
| 5464 | for(j=1; j<nArg; j++){ |
| 5465 | const char *z = azArg[j]; |
| 5466 | if( z[0]=='-' ){ |
| 5467 | while( z[0]=='-' ) z++; |
| 5468 | /* No options to process at this time */ |
| 5469 | { |
| 5470 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| 5471 | return 1; |
| 5472 | } |
| 5473 | }else if( zDestFile==0 ){ |
| 5474 | zDestFile = azArg[j]; |
| 5475 | }else if( zDb==0 ){ |
| 5476 | zDb = zDestFile; |
| 5477 | zDestFile = azArg[j]; |
| 5478 | }else{ |
| 5479 | raw_printf(stderr, "too many arguments to .backup\n"); |
| 5480 | return 1; |
| 5481 | } |
| 5482 | } |
| 5483 | if( zDestFile==0 ){ |
| 5484 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
| 5485 | return 1; |
| 5486 | } |
| 5487 | if( zDb==0 ) zDb = "main"; |
| 5488 | rc = sqlite3_open(zDestFile, &pDest); |
| 5489 | if( rc!=SQLITE_OK ){ |
| 5490 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
| 5491 | sqlite3_close(pDest); |
| 5492 | return 1; |
| 5493 | } |
| 5494 | open_db(p, 0); |
| 5495 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 5496 | if( pBackup==0 ){ |
| 5497 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 5498 | sqlite3_close(pDest); |
| 5499 | return 1; |
| 5500 | } |
| 5501 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 5502 | sqlite3_backup_finish(pBackup); |
| 5503 | if( rc==SQLITE_DONE ){ |
| 5504 | rc = 0; |
| 5505 | }else{ |
| 5506 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 5507 | rc = 1; |
| 5508 | } |
| 5509 | sqlite3_close(pDest); |
| 5510 | }else |
| 5511 | |
| 5512 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 5513 | if( nArg==2 ){ |
| 5514 | bail_on_error = booleanValue(azArg[1]); |
| 5515 | }else{ |
| 5516 | raw_printf(stderr, "Usage: .bail on|off\n"); |
| 5517 | rc = 1; |
| 5518 | } |
| 5519 | }else |
| 5520 | |
| 5521 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 5522 | if( nArg==2 ){ |
| 5523 | if( booleanValue(azArg[1]) ){ |
| 5524 | setBinaryMode(p->out, 1); |
| 5525 | }else{ |
| 5526 | setTextMode(p->out, 1); |
| 5527 | } |
| 5528 | }else{ |
| 5529 | raw_printf(stderr, "Usage: .binary on|off\n"); |
| 5530 | rc = 1; |
| 5531 | } |
| 5532 | }else |
| 5533 | |
| 5534 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 5535 | if( nArg==2 ){ |
| 5536 | #if defined(_WIN32) || defined(WIN32) |
| 5537 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 5538 | rc = !SetCurrentDirectoryW(z); |
| 5539 | sqlite3_free(z); |
| 5540 | #else |
| 5541 | rc = chdir(azArg[1]); |
| 5542 | #endif |
| 5543 | if( rc ){ |
| 5544 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 5545 | rc = 1; |
| 5546 | } |
| 5547 | }else{ |
| 5548 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 5549 | rc = 1; |
| 5550 | } |
| 5551 | }else |
| 5552 | |
| 5553 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 5554 | ** routine named test_breakpoint(). |
| 5555 | */ |
| 5556 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 5557 | test_breakpoint(); |
| 5558 | }else |
| 5559 | |
| 5560 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 5561 | if( nArg==2 ){ |
| 5562 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 5563 | }else{ |
| 5564 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 5565 | rc = 1; |
| 5566 | } |
| 5567 | }else |
| 5568 | |
| 5569 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 5570 | ** Then read the content of the testcase-out.txt file and compare against |
| 5571 | ** azArg[1]. If there are differences, report an error and exit. |
| 5572 | */ |
| 5573 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 5574 | char *zRes = 0; |
| 5575 | output_reset(p); |
| 5576 | if( nArg!=2 ){ |
| 5577 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
| 5578 | rc = 2; |
| 5579 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
| 5580 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 5581 | rc = 2; |
| 5582 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
| 5583 | utf8_printf(stderr, |
| 5584 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 5585 | p->zTestcase, azArg[1], zRes); |
drh | f30d345 | 2017-10-17 13:44:46 +0000 | [diff] [blame] | 5586 | rc = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5587 | }else{ |
| 5588 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 5589 | p->nCheck++; |
| 5590 | } |
| 5591 | sqlite3_free(zRes); |
| 5592 | }else |
| 5593 | |
| 5594 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 5595 | if( nArg==2 ){ |
| 5596 | tryToClone(p, azArg[1]); |
| 5597 | }else{ |
| 5598 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 5599 | rc = 1; |
| 5600 | } |
| 5601 | }else |
| 5602 | |
| 5603 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
| 5604 | ShellState data; |
| 5605 | char *zErrMsg = 0; |
| 5606 | open_db(p, 0); |
| 5607 | memcpy(&data, p, sizeof(data)); |
| 5608 | data.showHeader = 0; |
| 5609 | data.cMode = data.mode = MODE_List; |
| 5610 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
| 5611 | data.cnt = 0; |
| 5612 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 5613 | callback, &data, &zErrMsg); |
| 5614 | if( zErrMsg ){ |
| 5615 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 5616 | sqlite3_free(zErrMsg); |
| 5617 | rc = 1; |
| 5618 | } |
| 5619 | }else |
| 5620 | |
| 5621 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 5622 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 5623 | }else |
| 5624 | |
| 5625 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 5626 | const char *zLike = 0; |
| 5627 | int i; |
| 5628 | int savedShowHeader = p->showHeader; |
| 5629 | ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines); |
| 5630 | for(i=1; i<nArg; i++){ |
| 5631 | if( azArg[i][0]=='-' ){ |
| 5632 | const char *z = azArg[i]+1; |
| 5633 | if( z[0]=='-' ) z++; |
| 5634 | if( strcmp(z,"preserve-rowids")==0 ){ |
| 5635 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 5636 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 5637 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 5638 | rc = 1; |
| 5639 | goto meta_command_exit; |
| 5640 | #else |
| 5641 | ShellSetFlag(p, SHFLG_PreserveRowid); |
| 5642 | #endif |
| 5643 | }else |
| 5644 | if( strcmp(z,"newlines")==0 ){ |
| 5645 | ShellSetFlag(p, SHFLG_Newlines); |
| 5646 | }else |
| 5647 | { |
| 5648 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 5649 | rc = 1; |
| 5650 | goto meta_command_exit; |
| 5651 | } |
| 5652 | }else if( zLike ){ |
| 5653 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " |
| 5654 | "?--newlines? ?LIKE-PATTERN?\n"); |
| 5655 | rc = 1; |
| 5656 | goto meta_command_exit; |
| 5657 | }else{ |
| 5658 | zLike = azArg[i]; |
| 5659 | } |
| 5660 | } |
| 5661 | open_db(p, 0); |
| 5662 | /* When playing back a "dump", the content might appear in an order |
| 5663 | ** which causes immediate foreign key constraints to be violated. |
| 5664 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 5665 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 5666 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 5667 | p->writableSchema = 0; |
| 5668 | p->showHeader = 0; |
| 5669 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 5670 | ** as much of the schema as it can even if the sqlite_master table is |
| 5671 | ** corrupt. */ |
| 5672 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 5673 | p->nErr = 0; |
| 5674 | if( zLike==0 ){ |
| 5675 | run_schema_dump_query(p, |
| 5676 | "SELECT name, type, sql FROM sqlite_master " |
| 5677 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
| 5678 | ); |
| 5679 | run_schema_dump_query(p, |
| 5680 | "SELECT name, type, sql FROM sqlite_master " |
| 5681 | "WHERE name=='sqlite_sequence'" |
| 5682 | ); |
| 5683 | run_table_dump_query(p, |
| 5684 | "SELECT sql FROM sqlite_master " |
| 5685 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
| 5686 | ); |
| 5687 | }else{ |
| 5688 | char *zSql; |
| 5689 | zSql = sqlite3_mprintf( |
| 5690 | "SELECT name, type, sql FROM sqlite_master " |
| 5691 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 5692 | " AND sql NOT NULL", zLike); |
| 5693 | run_schema_dump_query(p,zSql); |
| 5694 | sqlite3_free(zSql); |
| 5695 | zSql = sqlite3_mprintf( |
| 5696 | "SELECT sql FROM sqlite_master " |
| 5697 | "WHERE sql NOT NULL" |
| 5698 | " AND type IN ('index','trigger','view')" |
| 5699 | " AND tbl_name LIKE %Q", zLike); |
| 5700 | run_table_dump_query(p, zSql, 0); |
| 5701 | sqlite3_free(zSql); |
| 5702 | } |
| 5703 | if( p->writableSchema ){ |
| 5704 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 5705 | p->writableSchema = 0; |
| 5706 | } |
| 5707 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5708 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
| 5709 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
| 5710 | p->showHeader = savedShowHeader; |
| 5711 | }else |
| 5712 | |
| 5713 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 5714 | if( nArg==2 ){ |
| 5715 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
| 5716 | }else{ |
| 5717 | raw_printf(stderr, "Usage: .echo on|off\n"); |
| 5718 | rc = 1; |
| 5719 | } |
| 5720 | }else |
| 5721 | |
| 5722 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 5723 | if( nArg==2 ){ |
| 5724 | if( strcmp(azArg[1],"full")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 5725 | p->autoEQP = AUTOEQP_full; |
| 5726 | }else if( strcmp(azArg[1],"trigger")==0 ){ |
| 5727 | p->autoEQP = AUTOEQP_trigger; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5728 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 5729 | p->autoEQP = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5730 | } |
| 5731 | }else{ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 5732 | raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5733 | rc = 1; |
| 5734 | } |
| 5735 | }else |
| 5736 | |
| 5737 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 5738 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 5739 | rc = 2; |
| 5740 | }else |
| 5741 | |
| 5742 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 5743 | ** retained purely for backwards compatibility */ |
| 5744 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 5745 | int val = 1; |
| 5746 | if( nArg>=2 ){ |
| 5747 | if( strcmp(azArg[1],"auto")==0 ){ |
| 5748 | val = 99; |
| 5749 | }else{ |
| 5750 | val = booleanValue(azArg[1]); |
| 5751 | } |
| 5752 | } |
| 5753 | if( val==1 && p->mode!=MODE_Explain ){ |
| 5754 | p->normalMode = p->mode; |
| 5755 | p->mode = MODE_Explain; |
| 5756 | p->autoExplain = 0; |
| 5757 | }else if( val==0 ){ |
| 5758 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5759 | p->autoExplain = 0; |
| 5760 | }else if( val==99 ){ |
| 5761 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5762 | p->autoExplain = 1; |
| 5763 | } |
| 5764 | }else |
| 5765 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5766 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5767 | if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ |
| 5768 | open_db(p, 0); |
| 5769 | expertDotCommand(p, azArg, nArg); |
| 5770 | }else |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5771 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5772 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5773 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
| 5774 | ShellState data; |
| 5775 | char *zErrMsg = 0; |
| 5776 | int doStats = 0; |
| 5777 | memcpy(&data, p, sizeof(data)); |
| 5778 | data.showHeader = 0; |
| 5779 | data.cMode = data.mode = MODE_Semi; |
| 5780 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 5781 | data.cMode = data.mode = MODE_Pretty; |
| 5782 | nArg = 1; |
| 5783 | } |
| 5784 | if( nArg!=1 ){ |
| 5785 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| 5786 | rc = 1; |
| 5787 | goto meta_command_exit; |
| 5788 | } |
| 5789 | open_db(p, 0); |
| 5790 | rc = sqlite3_exec(p->db, |
| 5791 | "SELECT sql FROM" |
| 5792 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 5793 | " FROM sqlite_master UNION ALL" |
| 5794 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| 5795 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
| 5796 | "ORDER BY rowid", |
| 5797 | callback, &data, &zErrMsg |
| 5798 | ); |
| 5799 | if( rc==SQLITE_OK ){ |
| 5800 | sqlite3_stmt *pStmt; |
| 5801 | rc = sqlite3_prepare_v2(p->db, |
| 5802 | "SELECT rowid FROM sqlite_master" |
| 5803 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 5804 | -1, &pStmt, 0); |
| 5805 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 5806 | sqlite3_finalize(pStmt); |
| 5807 | } |
| 5808 | if( doStats==0 ){ |
| 5809 | raw_printf(p->out, "/* No STAT tables available */\n"); |
| 5810 | }else{ |
| 5811 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 5812 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 5813 | callback, &data, &zErrMsg); |
| 5814 | data.cMode = data.mode = MODE_Insert; |
| 5815 | data.zDestTable = "sqlite_stat1"; |
| 5816 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 5817 | shell_callback, &data,&zErrMsg); |
| 5818 | data.zDestTable = "sqlite_stat3"; |
| 5819 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 5820 | shell_callback, &data,&zErrMsg); |
| 5821 | data.zDestTable = "sqlite_stat4"; |
| 5822 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 5823 | shell_callback, &data, &zErrMsg); |
| 5824 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 5825 | } |
| 5826 | }else |
| 5827 | |
| 5828 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 5829 | if( nArg==2 ){ |
| 5830 | p->showHeader = booleanValue(azArg[1]); |
| 5831 | }else{ |
| 5832 | raw_printf(stderr, "Usage: .headers on|off\n"); |
| 5833 | rc = 1; |
| 5834 | } |
| 5835 | }else |
| 5836 | |
| 5837 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 5838 | utf8_printf(p->out, "%s", zHelp); |
| 5839 | }else |
| 5840 | |
| 5841 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
| 5842 | char *zTable; /* Insert data into this table */ |
| 5843 | char *zFile; /* Name of file to extra content from */ |
| 5844 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 5845 | int nCol; /* Number of columns in the table */ |
| 5846 | int nByte; /* Number of bytes in an SQL string */ |
| 5847 | int i, j; /* Loop counters */ |
| 5848 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 5849 | int nSep; /* Number of bytes in p->colSeparator[] */ |
| 5850 | char *zSql; /* An SQL statement */ |
| 5851 | ImportCtx sCtx; /* Reader context */ |
| 5852 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 5853 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
| 5854 | |
| 5855 | if( nArg!=3 ){ |
| 5856 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
| 5857 | goto meta_command_exit; |
| 5858 | } |
| 5859 | zFile = azArg[1]; |
| 5860 | zTable = azArg[2]; |
| 5861 | seenInterrupt = 0; |
| 5862 | memset(&sCtx, 0, sizeof(sCtx)); |
| 5863 | open_db(p, 0); |
| 5864 | nSep = strlen30(p->colSeparator); |
| 5865 | if( nSep==0 ){ |
| 5866 | raw_printf(stderr, |
| 5867 | "Error: non-null column separator required for import\n"); |
| 5868 | return 1; |
| 5869 | } |
| 5870 | if( nSep>1 ){ |
| 5871 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
| 5872 | " for import\n"); |
| 5873 | return 1; |
| 5874 | } |
| 5875 | nSep = strlen30(p->rowSeparator); |
| 5876 | if( nSep==0 ){ |
| 5877 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
| 5878 | return 1; |
| 5879 | } |
| 5880 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 5881 | /* When importing CSV (only), if the row separator is set to the |
| 5882 | ** default output row separator, change it to the default input |
| 5883 | ** row separator. This avoids having to maintain different input |
| 5884 | ** and output row separators. */ |
| 5885 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 5886 | nSep = strlen30(p->rowSeparator); |
| 5887 | } |
| 5888 | if( nSep>1 ){ |
| 5889 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
| 5890 | " for import\n"); |
| 5891 | return 1; |
| 5892 | } |
| 5893 | sCtx.zFile = zFile; |
| 5894 | sCtx.nLine = 1; |
| 5895 | if( sCtx.zFile[0]=='|' ){ |
| 5896 | #ifdef SQLITE_OMIT_POPEN |
| 5897 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 5898 | return 1; |
| 5899 | #else |
| 5900 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 5901 | sCtx.zFile = "<pipe>"; |
| 5902 | xCloser = pclose; |
| 5903 | #endif |
| 5904 | }else{ |
| 5905 | sCtx.in = fopen(sCtx.zFile, "rb"); |
| 5906 | xCloser = fclose; |
| 5907 | } |
| 5908 | if( p->mode==MODE_Ascii ){ |
| 5909 | xRead = ascii_read_one_field; |
| 5910 | }else{ |
| 5911 | xRead = csv_read_one_field; |
| 5912 | } |
| 5913 | if( sCtx.in==0 ){ |
| 5914 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 5915 | return 1; |
| 5916 | } |
| 5917 | sCtx.cColSep = p->colSeparator[0]; |
| 5918 | sCtx.cRowSep = p->rowSeparator[0]; |
| 5919 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
| 5920 | if( zSql==0 ){ |
| 5921 | raw_printf(stderr, "Error: out of memory\n"); |
| 5922 | xCloser(sCtx.in); |
| 5923 | return 1; |
| 5924 | } |
| 5925 | nByte = strlen30(zSql); |
| 5926 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5927 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| 5928 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
| 5929 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 5930 | char cSep = '('; |
| 5931 | while( xRead(&sCtx) ){ |
| 5932 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
| 5933 | cSep = ','; |
| 5934 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
| 5935 | } |
| 5936 | if( cSep=='(' ){ |
| 5937 | sqlite3_free(zCreate); |
| 5938 | sqlite3_free(sCtx.z); |
| 5939 | xCloser(sCtx.in); |
| 5940 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
| 5941 | return 1; |
| 5942 | } |
| 5943 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 5944 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 5945 | sqlite3_free(zCreate); |
| 5946 | if( rc ){ |
| 5947 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
| 5948 | sqlite3_errmsg(p->db)); |
| 5949 | sqlite3_free(sCtx.z); |
| 5950 | xCloser(sCtx.in); |
| 5951 | return 1; |
| 5952 | } |
| 5953 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5954 | } |
| 5955 | sqlite3_free(zSql); |
| 5956 | if( rc ){ |
| 5957 | if (pStmt) sqlite3_finalize(pStmt); |
| 5958 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
| 5959 | xCloser(sCtx.in); |
| 5960 | return 1; |
| 5961 | } |
| 5962 | nCol = sqlite3_column_count(pStmt); |
| 5963 | sqlite3_finalize(pStmt); |
| 5964 | pStmt = 0; |
| 5965 | if( nCol==0 ) return 0; /* no columns, no error */ |
| 5966 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
| 5967 | if( zSql==0 ){ |
| 5968 | raw_printf(stderr, "Error: out of memory\n"); |
| 5969 | xCloser(sCtx.in); |
| 5970 | return 1; |
| 5971 | } |
| 5972 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
| 5973 | j = strlen30(zSql); |
| 5974 | for(i=1; i<nCol; i++){ |
| 5975 | zSql[j++] = ','; |
| 5976 | zSql[j++] = '?'; |
| 5977 | } |
| 5978 | zSql[j++] = ')'; |
| 5979 | zSql[j] = 0; |
| 5980 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5981 | sqlite3_free(zSql); |
| 5982 | if( rc ){ |
| 5983 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 5984 | if (pStmt) sqlite3_finalize(pStmt); |
| 5985 | xCloser(sCtx.in); |
| 5986 | return 1; |
| 5987 | } |
| 5988 | needCommit = sqlite3_get_autocommit(p->db); |
| 5989 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 5990 | do{ |
| 5991 | int startLine = sCtx.nLine; |
| 5992 | for(i=0; i<nCol; i++){ |
| 5993 | char *z = xRead(&sCtx); |
| 5994 | /* |
| 5995 | ** Did we reach end-of-file before finding any columns? |
| 5996 | ** If so, stop instead of NULL filling the remaining columns. |
| 5997 | */ |
| 5998 | if( z==0 && i==0 ) break; |
| 5999 | /* |
| 6000 | ** Did we reach end-of-file OR end-of-line before finding any |
| 6001 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 6002 | ** the remaining columns. |
| 6003 | */ |
| 6004 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| 6005 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 6006 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 6007 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 6008 | "filling the rest with NULL\n", |
| 6009 | sCtx.zFile, startLine, nCol, i+1); |
| 6010 | i += 2; |
| 6011 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| 6012 | } |
| 6013 | } |
| 6014 | if( sCtx.cTerm==sCtx.cColSep ){ |
| 6015 | do{ |
| 6016 | xRead(&sCtx); |
| 6017 | i++; |
| 6018 | }while( sCtx.cTerm==sCtx.cColSep ); |
| 6019 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 6020 | "extras ignored\n", |
| 6021 | sCtx.zFile, startLine, nCol, i); |
| 6022 | } |
| 6023 | if( i>=nCol ){ |
| 6024 | sqlite3_step(pStmt); |
| 6025 | rc = sqlite3_reset(pStmt); |
| 6026 | if( rc!=SQLITE_OK ){ |
| 6027 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 6028 | startLine, sqlite3_errmsg(p->db)); |
| 6029 | } |
| 6030 | } |
| 6031 | }while( sCtx.cTerm!=EOF ); |
| 6032 | |
| 6033 | xCloser(sCtx.in); |
| 6034 | sqlite3_free(sCtx.z); |
| 6035 | sqlite3_finalize(pStmt); |
| 6036 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
| 6037 | }else |
| 6038 | |
| 6039 | #ifndef SQLITE_UNTESTABLE |
| 6040 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 6041 | char *zSql; |
| 6042 | char *zCollist = 0; |
| 6043 | sqlite3_stmt *pStmt; |
| 6044 | int tnum = 0; |
| 6045 | int i; |
| 6046 | if( nArg!=3 ){ |
| 6047 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 6048 | rc = 1; |
| 6049 | goto meta_command_exit; |
| 6050 | } |
| 6051 | open_db(p, 0); |
| 6052 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 6053 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 6054 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6055 | sqlite3_free(zSql); |
| 6056 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6057 | tnum = sqlite3_column_int(pStmt, 0); |
| 6058 | } |
| 6059 | sqlite3_finalize(pStmt); |
| 6060 | if( tnum==0 ){ |
| 6061 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 6062 | rc = 1; |
| 6063 | goto meta_command_exit; |
| 6064 | } |
| 6065 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 6066 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6067 | sqlite3_free(zSql); |
| 6068 | i = 0; |
| 6069 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6070 | char zLabel[20]; |
| 6071 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
| 6072 | i++; |
| 6073 | if( zCol==0 ){ |
| 6074 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 6075 | zCol = "_ROWID_"; |
| 6076 | }else{ |
| 6077 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 6078 | zCol = zLabel; |
| 6079 | } |
| 6080 | } |
| 6081 | if( zCollist==0 ){ |
| 6082 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 6083 | }else{ |
| 6084 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 6085 | } |
| 6086 | } |
| 6087 | sqlite3_finalize(pStmt); |
| 6088 | zSql = sqlite3_mprintf( |
| 6089 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 6090 | azArg[2], zCollist, zCollist); |
| 6091 | sqlite3_free(zCollist); |
| 6092 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 6093 | if( rc==SQLITE_OK ){ |
| 6094 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 6095 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 6096 | if( rc ){ |
| 6097 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 6098 | }else{ |
| 6099 | utf8_printf(stdout, "%s;\n", zSql); |
| 6100 | raw_printf(stdout, |
| 6101 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 6102 | ); |
| 6103 | } |
| 6104 | }else{ |
| 6105 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
| 6106 | rc = 1; |
| 6107 | } |
| 6108 | sqlite3_free(zSql); |
| 6109 | }else |
| 6110 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 6111 | |
| 6112 | #ifdef SQLITE_ENABLE_IOTRACE |
| 6113 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
| 6114 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
| 6115 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 6116 | iotrace = 0; |
| 6117 | if( nArg<2 ){ |
| 6118 | sqlite3IoTrace = 0; |
| 6119 | }else if( strcmp(azArg[1], "-")==0 ){ |
| 6120 | sqlite3IoTrace = iotracePrintf; |
| 6121 | iotrace = stdout; |
| 6122 | }else{ |
| 6123 | iotrace = fopen(azArg[1], "w"); |
| 6124 | if( iotrace==0 ){ |
| 6125 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 6126 | sqlite3IoTrace = 0; |
| 6127 | rc = 1; |
| 6128 | }else{ |
| 6129 | sqlite3IoTrace = iotracePrintf; |
| 6130 | } |
| 6131 | } |
| 6132 | }else |
| 6133 | #endif |
| 6134 | |
| 6135 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 6136 | static const struct { |
| 6137 | const char *zLimitName; /* Name of a limit */ |
| 6138 | int limitCode; /* Integer code for that limit */ |
| 6139 | } aLimit[] = { |
| 6140 | { "length", SQLITE_LIMIT_LENGTH }, |
| 6141 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 6142 | { "column", SQLITE_LIMIT_COLUMN }, |
| 6143 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 6144 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 6145 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 6146 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 6147 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 6148 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 6149 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 6150 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 6151 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 6152 | }; |
| 6153 | int i, n2; |
| 6154 | open_db(p, 0); |
| 6155 | if( nArg==1 ){ |
| 6156 | for(i=0; i<ArraySize(aLimit); i++){ |
| 6157 | printf("%20s %d\n", aLimit[i].zLimitName, |
| 6158 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 6159 | } |
| 6160 | }else if( nArg>3 ){ |
| 6161 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
| 6162 | rc = 1; |
| 6163 | goto meta_command_exit; |
| 6164 | }else{ |
| 6165 | int iLimit = -1; |
| 6166 | n2 = strlen30(azArg[1]); |
| 6167 | for(i=0; i<ArraySize(aLimit); i++){ |
| 6168 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 6169 | if( iLimit<0 ){ |
| 6170 | iLimit = i; |
| 6171 | }else{ |
| 6172 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
| 6173 | rc = 1; |
| 6174 | goto meta_command_exit; |
| 6175 | } |
| 6176 | } |
| 6177 | } |
| 6178 | if( iLimit<0 ){ |
| 6179 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
| 6180 | "enter \".limits\" with no arguments for a list.\n", |
| 6181 | azArg[1]); |
| 6182 | rc = 1; |
| 6183 | goto meta_command_exit; |
| 6184 | } |
| 6185 | if( nArg==3 ){ |
| 6186 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 6187 | (int)integerValue(azArg[2])); |
| 6188 | } |
| 6189 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 6190 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 6191 | } |
| 6192 | }else |
| 6193 | |
| 6194 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
| 6195 | open_db(p, 0); |
| 6196 | lintDotCommand(p, azArg, nArg); |
| 6197 | }else |
| 6198 | |
| 6199 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6200 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
| 6201 | const char *zFile, *zProc; |
| 6202 | char *zErrMsg = 0; |
| 6203 | if( nArg<2 ){ |
| 6204 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
| 6205 | rc = 1; |
| 6206 | goto meta_command_exit; |
| 6207 | } |
| 6208 | zFile = azArg[1]; |
| 6209 | zProc = nArg>=3 ? azArg[2] : 0; |
| 6210 | open_db(p, 0); |
| 6211 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 6212 | if( rc!=SQLITE_OK ){ |
| 6213 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
| 6214 | sqlite3_free(zErrMsg); |
| 6215 | rc = 1; |
| 6216 | } |
| 6217 | }else |
| 6218 | #endif |
| 6219 | |
| 6220 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 6221 | if( nArg!=2 ){ |
| 6222 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 6223 | rc = 1; |
| 6224 | }else{ |
| 6225 | const char *zFile = azArg[1]; |
| 6226 | output_file_close(p->pLog); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6227 | p->pLog = output_file_open(zFile, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6228 | } |
| 6229 | }else |
| 6230 | |
| 6231 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 6232 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6233 | int n2 = strlen30(zMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6234 | int c2 = zMode[0]; |
| 6235 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
| 6236 | p->mode = MODE_Line; |
| 6237 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6238 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
| 6239 | p->mode = MODE_Column; |
| 6240 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6241 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
| 6242 | p->mode = MODE_List; |
| 6243 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 6244 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6245 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
| 6246 | p->mode = MODE_Html; |
| 6247 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
| 6248 | p->mode = MODE_Tcl; |
| 6249 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
| 6250 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6251 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
| 6252 | p->mode = MODE_Csv; |
| 6253 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 6254 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 6255 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
| 6256 | p->mode = MODE_List; |
| 6257 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
| 6258 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
| 6259 | p->mode = MODE_Insert; |
| 6260 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
| 6261 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 6262 | p->mode = MODE_Quote; |
| 6263 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 6264 | p->mode = MODE_Ascii; |
| 6265 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 6266 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
| 6267 | }else if( nArg==1 ){ |
| 6268 | raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 6269 | }else{ |
| 6270 | raw_printf(stderr, "Error: mode should be one of: " |
| 6271 | "ascii column csv html insert line list quote tabs tcl\n"); |
| 6272 | rc = 1; |
| 6273 | } |
| 6274 | p->cMode = p->mode; |
| 6275 | }else |
| 6276 | |
| 6277 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 6278 | if( nArg==2 ){ |
| 6279 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 6280 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| 6281 | }else{ |
| 6282 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
| 6283 | rc = 1; |
| 6284 | } |
| 6285 | }else |
| 6286 | |
| 6287 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
| 6288 | char *zNewFilename; /* Name of the database file to open */ |
| 6289 | int iName = 1; /* Index in azArg[] of the filename */ |
| 6290 | int newFlag = 0; /* True to delete file before opening */ |
| 6291 | /* Close the existing database */ |
| 6292 | session_close_all(p); |
| 6293 | sqlite3_close(p->db); |
| 6294 | p->db = 0; |
| 6295 | p->zDbFilename = 0; |
| 6296 | sqlite3_free(p->zFreeOnClose); |
| 6297 | p->zFreeOnClose = 0; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 6298 | p->openMode = SHELL_OPEN_UNSPEC; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6299 | /* Check for command-line arguments */ |
| 6300 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 6301 | const char *z = azArg[iName]; |
| 6302 | if( optionMatch(z,"new") ){ |
| 6303 | newFlag = 1; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 6304 | #ifdef SQLITE_HAVE_ZIP |
| 6305 | }else if( optionMatch(z, "zip") ){ |
| 6306 | p->openMode = SHELL_OPEN_ZIPFILE; |
| 6307 | #endif |
| 6308 | }else if( optionMatch(z, "append") ){ |
| 6309 | p->openMode = SHELL_OPEN_APPENDVFS; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6310 | }else if( z[0]=='-' ){ |
| 6311 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 6312 | rc = 1; |
| 6313 | goto meta_command_exit; |
| 6314 | } |
| 6315 | } |
| 6316 | /* If a filename is specified, try to open it first */ |
| 6317 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 6318 | if( zNewFilename ){ |
| 6319 | if( newFlag ) shellDeleteFile(zNewFilename); |
| 6320 | p->zDbFilename = zNewFilename; |
| 6321 | open_db(p, 1); |
| 6322 | if( p->db==0 ){ |
| 6323 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 6324 | sqlite3_free(zNewFilename); |
| 6325 | }else{ |
| 6326 | p->zFreeOnClose = zNewFilename; |
| 6327 | } |
| 6328 | } |
| 6329 | if( p->db==0 ){ |
| 6330 | /* As a fall-back open a TEMP database */ |
| 6331 | p->zDbFilename = 0; |
| 6332 | open_db(p, 0); |
| 6333 | } |
| 6334 | }else |
| 6335 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6336 | if( (c=='o' |
| 6337 | && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) |
| 6338 | || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6339 | ){ |
| 6340 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6341 | int bTxtMode = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6342 | if( azArg[0][0]=='e' ){ |
| 6343 | /* Transform the ".excel" command into ".once -x" */ |
| 6344 | nArg = 2; |
| 6345 | azArg[0] = "once"; |
| 6346 | zFile = azArg[1] = "-x"; |
| 6347 | n = 4; |
| 6348 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6349 | if( nArg>2 ){ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6350 | utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6351 | rc = 1; |
| 6352 | goto meta_command_exit; |
| 6353 | } |
| 6354 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 6355 | if( nArg<2 ){ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6356 | raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6357 | rc = 1; |
| 6358 | goto meta_command_exit; |
| 6359 | } |
| 6360 | p->outCount = 2; |
| 6361 | }else{ |
| 6362 | p->outCount = 0; |
| 6363 | } |
| 6364 | output_reset(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6365 | if( zFile[0]=='-' && zFile[1]=='-' ) zFile++; |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 6366 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6367 | if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 6368 | p->doXdgOpen = 1; |
| 6369 | outputModePush(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6370 | if( zFile[1]=='x' ){ |
| 6371 | newTempFile(p, "csv"); |
| 6372 | p->mode = MODE_Csv; |
| 6373 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 6374 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 6375 | }else{ |
| 6376 | newTempFile(p, "txt"); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6377 | bTxtMode = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6378 | } |
| 6379 | zFile = p->zTempFile; |
| 6380 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 6381 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6382 | if( zFile[0]=='|' ){ |
| 6383 | #ifdef SQLITE_OMIT_POPEN |
| 6384 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 6385 | rc = 1; |
| 6386 | p->out = stdout; |
| 6387 | #else |
| 6388 | p->out = popen(zFile + 1, "w"); |
| 6389 | if( p->out==0 ){ |
| 6390 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
| 6391 | p->out = stdout; |
| 6392 | rc = 1; |
| 6393 | }else{ |
| 6394 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 6395 | } |
| 6396 | #endif |
| 6397 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6398 | p->out = output_file_open(zFile, bTxtMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6399 | if( p->out==0 ){ |
| 6400 | if( strcmp(zFile,"off")!=0 ){ |
| 6401 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
| 6402 | } |
| 6403 | p->out = stdout; |
| 6404 | rc = 1; |
| 6405 | } else { |
| 6406 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 6407 | } |
| 6408 | } |
| 6409 | }else |
| 6410 | |
| 6411 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 6412 | int i; |
| 6413 | for(i=1; i<nArg; i++){ |
| 6414 | if( i>1 ) raw_printf(p->out, " "); |
| 6415 | utf8_printf(p->out, "%s", azArg[i]); |
| 6416 | } |
| 6417 | raw_printf(p->out, "\n"); |
| 6418 | }else |
| 6419 | |
| 6420 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
| 6421 | if( nArg >= 2) { |
| 6422 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 6423 | } |
| 6424 | if( nArg >= 3) { |
| 6425 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 6426 | } |
| 6427 | }else |
| 6428 | |
| 6429 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
| 6430 | rc = 2; |
| 6431 | }else |
| 6432 | |
| 6433 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 6434 | FILE *alt; |
| 6435 | if( nArg!=2 ){ |
| 6436 | raw_printf(stderr, "Usage: .read FILE\n"); |
| 6437 | rc = 1; |
| 6438 | goto meta_command_exit; |
| 6439 | } |
| 6440 | alt = fopen(azArg[1], "rb"); |
| 6441 | if( alt==0 ){ |
| 6442 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 6443 | rc = 1; |
| 6444 | }else{ |
| 6445 | rc = process_input(p, alt); |
| 6446 | fclose(alt); |
| 6447 | } |
| 6448 | }else |
| 6449 | |
| 6450 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
| 6451 | const char *zSrcFile; |
| 6452 | const char *zDb; |
| 6453 | sqlite3 *pSrc; |
| 6454 | sqlite3_backup *pBackup; |
| 6455 | int nTimeout = 0; |
| 6456 | |
| 6457 | if( nArg==2 ){ |
| 6458 | zSrcFile = azArg[1]; |
| 6459 | zDb = "main"; |
| 6460 | }else if( nArg==3 ){ |
| 6461 | zSrcFile = azArg[2]; |
| 6462 | zDb = azArg[1]; |
| 6463 | }else{ |
| 6464 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
| 6465 | rc = 1; |
| 6466 | goto meta_command_exit; |
| 6467 | } |
| 6468 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 6469 | if( rc!=SQLITE_OK ){ |
| 6470 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
| 6471 | sqlite3_close(pSrc); |
| 6472 | return 1; |
| 6473 | } |
| 6474 | open_db(p, 0); |
| 6475 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 6476 | if( pBackup==0 ){ |
| 6477 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6478 | sqlite3_close(pSrc); |
| 6479 | return 1; |
| 6480 | } |
| 6481 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 6482 | || rc==SQLITE_BUSY ){ |
| 6483 | if( rc==SQLITE_BUSY ){ |
| 6484 | if( nTimeout++ >= 3 ) break; |
| 6485 | sqlite3_sleep(100); |
| 6486 | } |
| 6487 | } |
| 6488 | sqlite3_backup_finish(pBackup); |
| 6489 | if( rc==SQLITE_DONE ){ |
| 6490 | rc = 0; |
| 6491 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 6492 | raw_printf(stderr, "Error: source database is busy\n"); |
| 6493 | rc = 1; |
| 6494 | }else{ |
| 6495 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6496 | rc = 1; |
| 6497 | } |
| 6498 | sqlite3_close(pSrc); |
| 6499 | }else |
| 6500 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6501 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 6502 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 6503 | p->scanstatsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6504 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 6505 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| 6506 | #endif |
| 6507 | }else{ |
| 6508 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| 6509 | rc = 1; |
| 6510 | } |
| 6511 | }else |
| 6512 | |
| 6513 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 6514 | ShellText sSelect; |
| 6515 | ShellState data; |
| 6516 | char *zErrMsg = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6517 | const char *zDiv = "("; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6518 | const char *zName = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6519 | int iSchema = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6520 | int bDebug = 0; |
| 6521 | int ii; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6522 | |
| 6523 | open_db(p, 0); |
| 6524 | memcpy(&data, p, sizeof(data)); |
| 6525 | data.showHeader = 0; |
| 6526 | data.cMode = data.mode = MODE_Semi; |
| 6527 | initText(&sSelect); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6528 | for(ii=1; ii<nArg; ii++){ |
| 6529 | if( optionMatch(azArg[ii],"indent") ){ |
| 6530 | data.cMode = data.mode = MODE_Pretty; |
| 6531 | }else if( optionMatch(azArg[ii],"debug") ){ |
| 6532 | bDebug = 1; |
| 6533 | }else if( zName==0 ){ |
| 6534 | zName = azArg[ii]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6535 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6536 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
| 6537 | rc = 1; |
| 6538 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6539 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6540 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6541 | if( zName!=0 ){ |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6542 | int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0; |
| 6543 | if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6544 | char *new_argv[2], *new_colv[2]; |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 6545 | new_argv[0] = sqlite3_mprintf( |
| 6546 | "CREATE TABLE %s (\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6547 | " type text,\n" |
| 6548 | " name text,\n" |
| 6549 | " tbl_name text,\n" |
| 6550 | " rootpage integer,\n" |
| 6551 | " sql text\n" |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6552 | ")", isMaster ? "sqlite_master" : "sqlite_temp_master"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6553 | new_argv[1] = 0; |
| 6554 | new_colv[0] = "sql"; |
| 6555 | new_colv[1] = 0; |
| 6556 | callback(&data, 1, new_argv, new_colv); |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 6557 | sqlite3_free(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6558 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6559 | } |
| 6560 | if( zDiv ){ |
| 6561 | sqlite3_stmt *pStmt = 0; |
| 6562 | rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 6563 | -1, &pStmt, 0); |
| 6564 | if( rc ){ |
| 6565 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6566 | sqlite3_finalize(pStmt); |
| 6567 | rc = 1; |
| 6568 | goto meta_command_exit; |
| 6569 | } |
| 6570 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 6571 | iSchema = 0; |
| 6572 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6573 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 6574 | char zScNum[30]; |
| 6575 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 6576 | appendText(&sSelect, zDiv, 0); |
| 6577 | zDiv = " UNION ALL "; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6578 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
| 6579 | if( sqlite3_stricmp(zDb, "main")!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6580 | appendText(&sSelect, zDb, '"'); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6581 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6582 | appendText(&sSelect, "NULL", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6583 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6584 | appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); |
| 6585 | appendText(&sSelect, zScNum, 0); |
| 6586 | appendText(&sSelect, " AS snum, ", 0); |
| 6587 | appendText(&sSelect, zDb, '\''); |
| 6588 | appendText(&sSelect, " AS sname FROM ", 0); |
| 6589 | appendText(&sSelect, zDb, '"'); |
| 6590 | appendText(&sSelect, ".sqlite_master", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6591 | } |
| 6592 | sqlite3_finalize(pStmt); |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 6593 | #ifdef SQLITE_INTROSPECTION_PRAGMAS |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6594 | if( zName ){ |
| 6595 | appendText(&sSelect, |
| 6596 | " UNION ALL SELECT shell_module_schema(name)," |
| 6597 | " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0); |
| 6598 | } |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 6599 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6600 | appendText(&sSelect, ") WHERE ", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6601 | if( zName ){ |
| 6602 | char *zQarg = sqlite3_mprintf("%Q", zName); |
| 6603 | if( strchr(zName, '.') ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6604 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 6605 | }else{ |
| 6606 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 6607 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6608 | appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6609 | appendText(&sSelect, zQarg, 0); |
| 6610 | appendText(&sSelect, " AND ", 0); |
| 6611 | sqlite3_free(zQarg); |
| 6612 | } |
| 6613 | appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" |
| 6614 | " ORDER BY snum, rowid", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6615 | if( bDebug ){ |
| 6616 | utf8_printf(p->out, "SQL: %s;\n", sSelect.z); |
| 6617 | }else{ |
| 6618 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 6619 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6620 | freeText(&sSelect); |
| 6621 | } |
| 6622 | if( zErrMsg ){ |
| 6623 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 6624 | sqlite3_free(zErrMsg); |
| 6625 | rc = 1; |
| 6626 | }else if( rc != SQLITE_OK ){ |
| 6627 | raw_printf(stderr,"Error: querying schema information\n"); |
| 6628 | rc = 1; |
| 6629 | }else{ |
| 6630 | rc = 0; |
| 6631 | } |
| 6632 | }else |
| 6633 | |
| 6634 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 6635 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
| 6636 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
| 6637 | }else |
| 6638 | #endif |
| 6639 | |
| 6640 | #if defined(SQLITE_ENABLE_SESSION) |
| 6641 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 6642 | OpenSession *pSession = &p->aSession[0]; |
| 6643 | char **azCmd = &azArg[1]; |
| 6644 | int iSes = 0; |
| 6645 | int nCmd = nArg - 1; |
| 6646 | int i; |
| 6647 | if( nArg<=1 ) goto session_syntax_error; |
| 6648 | open_db(p, 0); |
| 6649 | if( nArg>=3 ){ |
| 6650 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 6651 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 6652 | } |
| 6653 | if( iSes<p->nSession ){ |
| 6654 | pSession = &p->aSession[iSes]; |
| 6655 | azCmd++; |
| 6656 | nCmd--; |
| 6657 | }else{ |
| 6658 | pSession = &p->aSession[0]; |
| 6659 | iSes = 0; |
| 6660 | } |
| 6661 | } |
| 6662 | |
| 6663 | /* .session attach TABLE |
| 6664 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 6665 | ** table so that it is never filtered. |
| 6666 | */ |
| 6667 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 6668 | if( nCmd!=2 ) goto session_syntax_error; |
| 6669 | if( pSession->p==0 ){ |
| 6670 | session_not_open: |
| 6671 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
| 6672 | }else{ |
| 6673 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 6674 | if( rc ){ |
| 6675 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| 6676 | rc = 0; |
| 6677 | } |
| 6678 | } |
| 6679 | }else |
| 6680 | |
| 6681 | /* .session changeset FILE |
| 6682 | ** .session patchset FILE |
| 6683 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 6684 | */ |
| 6685 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 6686 | FILE *out = 0; |
| 6687 | if( nCmd!=2 ) goto session_syntax_error; |
| 6688 | if( pSession->p==0 ) goto session_not_open; |
| 6689 | out = fopen(azCmd[1], "wb"); |
| 6690 | if( out==0 ){ |
| 6691 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
| 6692 | }else{ |
| 6693 | int szChng; |
| 6694 | void *pChng; |
| 6695 | if( azCmd[0][0]=='c' ){ |
| 6696 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| 6697 | }else{ |
| 6698 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 6699 | } |
| 6700 | if( rc ){ |
| 6701 | printf("Error: error code %d\n", rc); |
| 6702 | rc = 0; |
| 6703 | } |
| 6704 | if( pChng |
| 6705 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
| 6706 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| 6707 | szChng); |
| 6708 | } |
| 6709 | sqlite3_free(pChng); |
| 6710 | fclose(out); |
| 6711 | } |
| 6712 | }else |
| 6713 | |
| 6714 | /* .session close |
| 6715 | ** Close the identified session |
| 6716 | */ |
| 6717 | if( strcmp(azCmd[0], "close")==0 ){ |
| 6718 | if( nCmd!=1 ) goto session_syntax_error; |
| 6719 | if( p->nSession ){ |
| 6720 | session_close(pSession); |
| 6721 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 6722 | } |
| 6723 | }else |
| 6724 | |
| 6725 | /* .session enable ?BOOLEAN? |
| 6726 | ** Query or set the enable flag |
| 6727 | */ |
| 6728 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 6729 | int ii; |
| 6730 | if( nCmd>2 ) goto session_syntax_error; |
| 6731 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6732 | if( p->nSession ){ |
| 6733 | ii = sqlite3session_enable(pSession->p, ii); |
| 6734 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 6735 | pSession->zName, ii); |
| 6736 | } |
| 6737 | }else |
| 6738 | |
| 6739 | /* .session filter GLOB .... |
| 6740 | ** Set a list of GLOB patterns of table names to be excluded. |
| 6741 | */ |
| 6742 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 6743 | int ii, nByte; |
| 6744 | if( nCmd<2 ) goto session_syntax_error; |
| 6745 | if( p->nSession ){ |
| 6746 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 6747 | sqlite3_free(pSession->azFilter[ii]); |
| 6748 | } |
| 6749 | sqlite3_free(pSession->azFilter); |
| 6750 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 6751 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 6752 | if( pSession->azFilter==0 ){ |
| 6753 | raw_printf(stderr, "Error: out or memory\n"); |
| 6754 | exit(1); |
| 6755 | } |
| 6756 | for(ii=1; ii<nCmd; ii++){ |
| 6757 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| 6758 | } |
| 6759 | pSession->nFilter = ii-1; |
| 6760 | } |
| 6761 | }else |
| 6762 | |
| 6763 | /* .session indirect ?BOOLEAN? |
| 6764 | ** Query or set the indirect flag |
| 6765 | */ |
| 6766 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 6767 | int ii; |
| 6768 | if( nCmd>2 ) goto session_syntax_error; |
| 6769 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6770 | if( p->nSession ){ |
| 6771 | ii = sqlite3session_indirect(pSession->p, ii); |
| 6772 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 6773 | pSession->zName, ii); |
| 6774 | } |
| 6775 | }else |
| 6776 | |
| 6777 | /* .session isempty |
| 6778 | ** Determine if the session is empty |
| 6779 | */ |
| 6780 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 6781 | int ii; |
| 6782 | if( nCmd!=1 ) goto session_syntax_error; |
| 6783 | if( p->nSession ){ |
| 6784 | ii = sqlite3session_isempty(pSession->p); |
| 6785 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 6786 | pSession->zName, ii); |
| 6787 | } |
| 6788 | }else |
| 6789 | |
| 6790 | /* .session list |
| 6791 | ** List all currently open sessions |
| 6792 | */ |
| 6793 | if( strcmp(azCmd[0],"list")==0 ){ |
| 6794 | for(i=0; i<p->nSession; i++){ |
| 6795 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
| 6796 | } |
| 6797 | }else |
| 6798 | |
| 6799 | /* .session open DB NAME |
| 6800 | ** Open a new session called NAME on the attached database DB. |
| 6801 | ** DB is normally "main". |
| 6802 | */ |
| 6803 | if( strcmp(azCmd[0],"open")==0 ){ |
| 6804 | char *zName; |
| 6805 | if( nCmd!=3 ) goto session_syntax_error; |
| 6806 | zName = azCmd[2]; |
| 6807 | if( zName[0]==0 ) goto session_syntax_error; |
| 6808 | for(i=0; i<p->nSession; i++){ |
| 6809 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
| 6810 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| 6811 | goto meta_command_exit; |
| 6812 | } |
| 6813 | } |
| 6814 | if( p->nSession>=ArraySize(p->aSession) ){ |
| 6815 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
| 6816 | goto meta_command_exit; |
| 6817 | } |
| 6818 | pSession = &p->aSession[p->nSession]; |
| 6819 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 6820 | if( rc ){ |
| 6821 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| 6822 | rc = 0; |
| 6823 | goto meta_command_exit; |
| 6824 | } |
| 6825 | pSession->nFilter = 0; |
| 6826 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
| 6827 | p->nSession++; |
| 6828 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 6829 | }else |
| 6830 | /* If no command name matches, show a syntax error */ |
| 6831 | session_syntax_error: |
| 6832 | session_help(p); |
| 6833 | }else |
| 6834 | #endif |
| 6835 | |
| 6836 | #ifdef SQLITE_DEBUG |
| 6837 | /* Undocumented commands for internal testing. Subject to change |
| 6838 | ** without notice. */ |
| 6839 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 6840 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 6841 | int i, v; |
| 6842 | for(i=1; i<nArg; i++){ |
| 6843 | v = booleanValue(azArg[i]); |
| 6844 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| 6845 | } |
| 6846 | } |
| 6847 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 6848 | int i; sqlite3_int64 v; |
| 6849 | for(i=1; i<nArg; i++){ |
| 6850 | char zBuf[200]; |
| 6851 | v = integerValue(azArg[i]); |
| 6852 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| 6853 | utf8_printf(p->out, "%s", zBuf); |
| 6854 | } |
| 6855 | } |
| 6856 | }else |
| 6857 | #endif |
| 6858 | |
| 6859 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 6860 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 6861 | int bVerbose = 0; /* Verbose output */ |
| 6862 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 6863 | int i, k; /* Loop counters */ |
| 6864 | int nTest = 0; /* Number of tests runs */ |
| 6865 | int nErr = 0; /* Number of errors seen */ |
| 6866 | ShellText str; /* Answer for a query */ |
| 6867 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
| 6868 | |
| 6869 | open_db(p,0); |
| 6870 | for(i=1; i<nArg; i++){ |
| 6871 | const char *z = azArg[i]; |
| 6872 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 6873 | if( strcmp(z,"-init")==0 ){ |
| 6874 | bIsInit = 1; |
| 6875 | }else |
| 6876 | if( strcmp(z,"-v")==0 ){ |
| 6877 | bVerbose++; |
| 6878 | }else |
| 6879 | { |
| 6880 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 6881 | azArg[i], azArg[0]); |
| 6882 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 6883 | rc = 1; |
| 6884 | goto meta_command_exit; |
| 6885 | } |
| 6886 | } |
| 6887 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 6888 | != SQLITE_OK ){ |
| 6889 | bSelftestExists = 0; |
| 6890 | }else{ |
| 6891 | bSelftestExists = 1; |
| 6892 | } |
| 6893 | if( bIsInit ){ |
| 6894 | createSelftestTable(p); |
| 6895 | bSelftestExists = 1; |
| 6896 | } |
| 6897 | initText(&str); |
| 6898 | appendText(&str, "x", 0); |
| 6899 | for(k=bSelftestExists; k>=0; k--){ |
| 6900 | if( k==1 ){ |
| 6901 | rc = sqlite3_prepare_v2(p->db, |
| 6902 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 6903 | -1, &pStmt, 0); |
| 6904 | }else{ |
| 6905 | rc = sqlite3_prepare_v2(p->db, |
| 6906 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 6907 | " (1,'run','PRAGMA integrity_check','ok')", |
| 6908 | -1, &pStmt, 0); |
| 6909 | } |
| 6910 | if( rc ){ |
| 6911 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 6912 | rc = 1; |
| 6913 | sqlite3_finalize(pStmt); |
| 6914 | goto meta_command_exit; |
| 6915 | } |
| 6916 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 6917 | int tno = sqlite3_column_int(pStmt, 0); |
| 6918 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 6919 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 6920 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
| 6921 | |
| 6922 | k = 0; |
| 6923 | if( bVerbose>0 ){ |
| 6924 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 6925 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 6926 | sqlite3_free(zQuote); |
| 6927 | } |
| 6928 | if( strcmp(zOp,"memo")==0 ){ |
| 6929 | utf8_printf(p->out, "%s\n", zSql); |
| 6930 | }else |
| 6931 | if( strcmp(zOp,"run")==0 ){ |
| 6932 | char *zErrMsg = 0; |
| 6933 | str.n = 0; |
| 6934 | str.z[0] = 0; |
| 6935 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 6936 | nTest++; |
| 6937 | if( bVerbose ){ |
| 6938 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 6939 | } |
| 6940 | if( rc || zErrMsg ){ |
| 6941 | nErr++; |
| 6942 | rc = 1; |
| 6943 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 6944 | sqlite3_free(zErrMsg); |
| 6945 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 6946 | nErr++; |
| 6947 | rc = 1; |
| 6948 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 6949 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 6950 | } |
| 6951 | }else |
| 6952 | { |
| 6953 | utf8_printf(stderr, |
| 6954 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 6955 | rc = 1; |
| 6956 | break; |
| 6957 | } |
| 6958 | } /* End loop over rows of content from SELFTEST */ |
| 6959 | sqlite3_finalize(pStmt); |
| 6960 | } /* End loop over k */ |
| 6961 | freeText(&str); |
| 6962 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 6963 | }else |
| 6964 | |
| 6965 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| 6966 | if( nArg<2 || nArg>3 ){ |
| 6967 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 6968 | rc = 1; |
| 6969 | } |
| 6970 | if( nArg>=2 ){ |
| 6971 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| 6972 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| 6973 | } |
| 6974 | if( nArg>=3 ){ |
| 6975 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 6976 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 6977 | } |
| 6978 | }else |
| 6979 | |
| 6980 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 6981 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 6982 | int i; /* Loop counter */ |
| 6983 | int bSchema = 0; /* Also hash the schema */ |
| 6984 | int bSeparate = 0; /* Hash each table separately */ |
| 6985 | int iSize = 224; /* Hash algorithm to use */ |
| 6986 | int bDebug = 0; /* Only show the query that would have run */ |
| 6987 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 6988 | char *zSql; /* SQL to be run */ |
| 6989 | char *zSep; /* Separator */ |
| 6990 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
| 6991 | ShellText sQuery; /* Set of queries used to read all content */ |
| 6992 | open_db(p, 0); |
| 6993 | for(i=1; i<nArg; i++){ |
| 6994 | const char *z = azArg[i]; |
| 6995 | if( z[0]=='-' ){ |
| 6996 | z++; |
| 6997 | if( z[0]=='-' ) z++; |
| 6998 | if( strcmp(z,"schema")==0 ){ |
| 6999 | bSchema = 1; |
| 7000 | }else |
| 7001 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 7002 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 7003 | ){ |
| 7004 | iSize = atoi(&z[5]); |
| 7005 | }else |
| 7006 | if( strcmp(z,"debug")==0 ){ |
| 7007 | bDebug = 1; |
| 7008 | }else |
| 7009 | { |
| 7010 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 7011 | azArg[i], azArg[0]); |
| 7012 | raw_printf(stderr, "Should be one of: --schema" |
| 7013 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 7014 | rc = 1; |
| 7015 | goto meta_command_exit; |
| 7016 | } |
| 7017 | }else if( zLike ){ |
| 7018 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 7019 | rc = 1; |
| 7020 | goto meta_command_exit; |
| 7021 | }else{ |
| 7022 | zLike = z; |
| 7023 | bSeparate = 1; |
| 7024 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
| 7025 | } |
| 7026 | } |
| 7027 | if( bSchema ){ |
| 7028 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7029 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7030 | " UNION ALL SELECT 'sqlite_master'" |
| 7031 | " ORDER BY 1 collate nocase"; |
| 7032 | }else{ |
| 7033 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7034 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7035 | " AND name NOT LIKE 'sqlite_%'" |
| 7036 | " ORDER BY 1 collate nocase"; |
| 7037 | } |
| 7038 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 7039 | initText(&sQuery); |
| 7040 | initText(&sSql); |
| 7041 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 7042 | zSep = "VALUES("; |
| 7043 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7044 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 7045 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 7046 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 7047 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 7048 | appendText(&sQuery,zTab,'"'); |
| 7049 | appendText(&sQuery," NOT INDEXED;", 0); |
| 7050 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 7051 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 7052 | " ORDER BY name;", 0); |
| 7053 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 7054 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 7055 | " ORDER BY name;", 0); |
| 7056 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 7057 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 7058 | " ORDER BY tbl,idx;", 0); |
| 7059 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 7060 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 7061 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 7062 | appendText(&sQuery, zTab, 0); |
| 7063 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 7064 | } |
| 7065 | appendText(&sSql, zSep, 0); |
| 7066 | appendText(&sSql, sQuery.z, '\''); |
| 7067 | sQuery.n = 0; |
| 7068 | appendText(&sSql, ",", 0); |
| 7069 | appendText(&sSql, zTab, '\''); |
| 7070 | zSep = "),("; |
| 7071 | } |
| 7072 | sqlite3_finalize(pStmt); |
| 7073 | if( bSeparate ){ |
| 7074 | zSql = sqlite3_mprintf( |
| 7075 | "%s))" |
| 7076 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 7077 | " FROM [sha3sum$query]", |
| 7078 | sSql.z, iSize); |
| 7079 | }else{ |
| 7080 | zSql = sqlite3_mprintf( |
| 7081 | "%s))" |
| 7082 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 7083 | " FROM [sha3sum$query]", |
| 7084 | sSql.z, iSize); |
| 7085 | } |
| 7086 | freeText(&sQuery); |
| 7087 | freeText(&sSql); |
| 7088 | if( bDebug ){ |
| 7089 | utf8_printf(p->out, "%s\n", zSql); |
| 7090 | }else{ |
| 7091 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 7092 | } |
| 7093 | sqlite3_free(zSql); |
| 7094 | }else |
| 7095 | |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 7096 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7097 | if( c=='s' |
| 7098 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 7099 | ){ |
| 7100 | char *zCmd; |
| 7101 | int i, x; |
| 7102 | if( nArg<2 ){ |
| 7103 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
| 7104 | rc = 1; |
| 7105 | goto meta_command_exit; |
| 7106 | } |
| 7107 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
| 7108 | for(i=2; i<nArg; i++){ |
| 7109 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 7110 | zCmd, azArg[i]); |
| 7111 | } |
| 7112 | x = system(zCmd); |
| 7113 | sqlite3_free(zCmd); |
| 7114 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 7115 | }else |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 7116 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7117 | |
| 7118 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 7119 | static const char *azBool[] = { "off", "on", "trigger", "full"}; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7120 | int i; |
| 7121 | if( nArg!=1 ){ |
| 7122 | raw_printf(stderr, "Usage: .show\n"); |
| 7123 | rc = 1; |
| 7124 | goto meta_command_exit; |
| 7125 | } |
| 7126 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 7127 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
| 7128 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 7129 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 7130 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 7131 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
| 7132 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 7133 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| 7134 | output_c_string(p->out, p->nullValue); |
| 7135 | raw_printf(p->out, "\n"); |
| 7136 | utf8_printf(p->out,"%12.12s: %s\n","output", |
| 7137 | strlen30(p->outfile) ? p->outfile : "stdout"); |
| 7138 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
| 7139 | output_c_string(p->out, p->colSeparator); |
| 7140 | raw_printf(p->out, "\n"); |
| 7141 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
| 7142 | output_c_string(p->out, p->rowSeparator); |
| 7143 | raw_printf(p->out, "\n"); |
| 7144 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
| 7145 | utf8_printf(p->out, "%12.12s: ", "width"); |
| 7146 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
| 7147 | raw_printf(p->out, "%d ", p->colWidth[i]); |
| 7148 | } |
| 7149 | raw_printf(p->out, "\n"); |
| 7150 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 7151 | p->zDbFilename ? p->zDbFilename : ""); |
| 7152 | }else |
| 7153 | |
| 7154 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 7155 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 7156 | p->statsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7157 | }else if( nArg==1 ){ |
| 7158 | display_stats(p->db, p, 0); |
| 7159 | }else{ |
| 7160 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
| 7161 | rc = 1; |
| 7162 | } |
| 7163 | }else |
| 7164 | |
| 7165 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 7166 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 7167 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 7168 | ){ |
| 7169 | sqlite3_stmt *pStmt; |
| 7170 | char **azResult; |
| 7171 | int nRow, nAlloc; |
| 7172 | int ii; |
| 7173 | ShellText s; |
| 7174 | initText(&s); |
| 7175 | open_db(p, 0); |
| 7176 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
| 7177 | if( rc ) return shellDatabaseError(p->db); |
| 7178 | |
| 7179 | if( nArg>2 && c=='i' ){ |
| 7180 | /* It is an historical accident that the .indexes command shows an error |
| 7181 | ** when called with the wrong number of arguments whereas the .tables |
| 7182 | ** command does not. */ |
| 7183 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 7184 | rc = 1; |
| 7185 | goto meta_command_exit; |
| 7186 | } |
| 7187 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
| 7188 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 7189 | if( zDbName==0 ) continue; |
| 7190 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 7191 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 7192 | appendText(&s, "SELECT name FROM ", 0); |
| 7193 | }else{ |
| 7194 | appendText(&s, "SELECT ", 0); |
| 7195 | appendText(&s, zDbName, '\''); |
| 7196 | appendText(&s, "||'.'||name FROM ", 0); |
| 7197 | } |
| 7198 | appendText(&s, zDbName, '"'); |
| 7199 | appendText(&s, ".sqlite_master ", 0); |
| 7200 | if( c=='t' ){ |
| 7201 | appendText(&s," WHERE type IN ('table','view')" |
| 7202 | " AND name NOT LIKE 'sqlite_%'" |
| 7203 | " AND name LIKE ?1", 0); |
| 7204 | }else{ |
| 7205 | appendText(&s," WHERE type='index'" |
| 7206 | " AND tbl_name LIKE ?1", 0); |
| 7207 | } |
| 7208 | } |
| 7209 | rc = sqlite3_finalize(pStmt); |
| 7210 | appendText(&s, " ORDER BY 1", 0); |
| 7211 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 7212 | freeText(&s); |
| 7213 | if( rc ) return shellDatabaseError(p->db); |
| 7214 | |
| 7215 | /* Run the SQL statement prepared by the above block. Store the results |
| 7216 | ** as an array of nul-terminated strings in azResult[]. */ |
| 7217 | nRow = nAlloc = 0; |
| 7218 | azResult = 0; |
| 7219 | if( nArg>1 ){ |
| 7220 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
| 7221 | }else{ |
| 7222 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 7223 | } |
| 7224 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 7225 | if( nRow>=nAlloc ){ |
| 7226 | char **azNew; |
| 7227 | int n2 = nAlloc*2 + 10; |
| 7228 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
| 7229 | if( azNew==0 ){ |
| 7230 | rc = shellNomemError(); |
| 7231 | break; |
| 7232 | } |
| 7233 | nAlloc = n2; |
| 7234 | azResult = azNew; |
| 7235 | } |
| 7236 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 7237 | if( 0==azResult[nRow] ){ |
| 7238 | rc = shellNomemError(); |
| 7239 | break; |
| 7240 | } |
| 7241 | nRow++; |
| 7242 | } |
| 7243 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 7244 | rc = shellDatabaseError(p->db); |
| 7245 | } |
| 7246 | |
| 7247 | /* Pretty-print the contents of array azResult[] to the output */ |
| 7248 | if( rc==0 && nRow>0 ){ |
| 7249 | int len, maxlen = 0; |
| 7250 | int i, j; |
| 7251 | int nPrintCol, nPrintRow; |
| 7252 | for(i=0; i<nRow; i++){ |
| 7253 | len = strlen30(azResult[i]); |
| 7254 | if( len>maxlen ) maxlen = len; |
| 7255 | } |
| 7256 | nPrintCol = 80/(maxlen+2); |
| 7257 | if( nPrintCol<1 ) nPrintCol = 1; |
| 7258 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 7259 | for(i=0; i<nPrintRow; i++){ |
| 7260 | for(j=i; j<nRow; j+=nPrintRow){ |
| 7261 | char *zSp = j<nPrintRow ? "" : " "; |
| 7262 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 7263 | azResult[j] ? azResult[j]:""); |
| 7264 | } |
| 7265 | raw_printf(p->out, "\n"); |
| 7266 | } |
| 7267 | } |
| 7268 | |
| 7269 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 7270 | sqlite3_free(azResult); |
| 7271 | }else |
| 7272 | |
| 7273 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 7274 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 7275 | output_reset(p); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 7276 | p->out = output_file_open("testcase-out.txt", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7277 | if( p->out==0 ){ |
| 7278 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
| 7279 | } |
| 7280 | if( nArg>=2 ){ |
| 7281 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 7282 | }else{ |
| 7283 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 7284 | } |
| 7285 | }else |
| 7286 | |
| 7287 | #ifndef SQLITE_UNTESTABLE |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7288 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7289 | static const struct { |
| 7290 | const char *zCtrlName; /* Name of a test-control option */ |
| 7291 | int ctrlCode; /* Integer code for that option */ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7292 | const char *zUsage; /* Usage notes */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7293 | } aCtrl[] = { |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7294 | { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, |
| 7295 | { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, |
| 7296 | /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ |
| 7297 | /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ |
| 7298 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, |
| 7299 | /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */ |
| 7300 | { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, |
| 7301 | #ifdef SQLITE_N_KEYWORD |
| 7302 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD, "IDENTIFIER" }, |
| 7303 | #endif |
| 7304 | { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, |
| 7305 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, |
| 7306 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 7307 | #ifdef YYCOVERAGE |
| 7308 | { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, |
| 7309 | #endif |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7310 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, |
| 7311 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" }, |
| 7312 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, |
| 7313 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, |
| 7314 | { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" }, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7315 | }; |
| 7316 | int testctrl = -1; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7317 | int iCtrl = -1; |
| 7318 | int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ |
| 7319 | int isOk = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7320 | int i, n2; |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 7321 | const char *zCmd = 0; |
| 7322 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7323 | open_db(p, 0); |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 7324 | zCmd = nArg>=2 ? azArg[1] : "help"; |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7325 | |
| 7326 | /* The argument can optionally begin with "-" or "--" */ |
| 7327 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 7328 | zCmd++; |
| 7329 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 7330 | } |
| 7331 | |
| 7332 | /* --help lists all test-controls */ |
| 7333 | if( strcmp(zCmd,"help")==0 ){ |
| 7334 | utf8_printf(p->out, "Available test-controls:\n"); |
| 7335 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7336 | utf8_printf(p->out, " .testctrl %s %s\n", |
| 7337 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7338 | } |
| 7339 | rc = 1; |
| 7340 | goto meta_command_exit; |
| 7341 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7342 | |
| 7343 | /* convert testctrl text option to value. allow any unique prefix |
| 7344 | ** of the option name, or a numerical value. */ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7345 | n2 = strlen30(zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7346 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7347 | if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7348 | if( testctrl<0 ){ |
| 7349 | testctrl = aCtrl[i].ctrlCode; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7350 | iCtrl = i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7351 | }else{ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7352 | utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" |
| 7353 | "Use \".testctrl --help\" for help\n", zCmd); |
| 7354 | rc = 1; |
| 7355 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7356 | } |
| 7357 | } |
| 7358 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7359 | if( testctrl<0 ){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7360 | utf8_printf(stderr,"Error: unknown test-control: %s\n" |
| 7361 | "Use \".testctrl --help\" for help\n", zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7362 | }else{ |
| 7363 | switch(testctrl){ |
| 7364 | |
| 7365 | /* sqlite3_test_control(int, db, int) */ |
| 7366 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
| 7367 | case SQLITE_TESTCTRL_RESERVE: |
| 7368 | if( nArg==3 ){ |
| 7369 | int opt = (int)strtol(azArg[2], 0, 0); |
| 7370 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7371 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7372 | } |
| 7373 | break; |
| 7374 | |
| 7375 | /* sqlite3_test_control(int) */ |
| 7376 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 7377 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
| 7378 | case SQLITE_TESTCTRL_PRNG_RESET: |
| 7379 | case SQLITE_TESTCTRL_BYTEORDER: |
| 7380 | if( nArg==2 ){ |
| 7381 | rc2 = sqlite3_test_control(testctrl); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7382 | isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7383 | } |
| 7384 | break; |
| 7385 | |
| 7386 | /* sqlite3_test_control(int, uint) */ |
| 7387 | case SQLITE_TESTCTRL_PENDING_BYTE: |
| 7388 | if( nArg==3 ){ |
| 7389 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
| 7390 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7391 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7392 | } |
| 7393 | break; |
| 7394 | |
| 7395 | /* sqlite3_test_control(int, int) */ |
| 7396 | case SQLITE_TESTCTRL_ASSERT: |
| 7397 | case SQLITE_TESTCTRL_ALWAYS: |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7398 | if( nArg==3 ){ |
| 7399 | int opt = booleanValue(azArg[2]); |
| 7400 | rc2 = sqlite3_test_control(testctrl, opt); |
| 7401 | isOk = 1; |
| 7402 | } |
| 7403 | break; |
| 7404 | |
| 7405 | /* sqlite3_test_control(int, int) */ |
| 7406 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7407 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
| 7408 | if( nArg==3 ){ |
| 7409 | int opt = booleanValue(azArg[2]); |
| 7410 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7411 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7412 | } |
| 7413 | break; |
| 7414 | |
| 7415 | /* sqlite3_test_control(int, char *) */ |
| 7416 | #ifdef SQLITE_N_KEYWORD |
| 7417 | case SQLITE_TESTCTRL_ISKEYWORD: |
| 7418 | if( nArg==3 ){ |
| 7419 | const char *opt = azArg[2]; |
| 7420 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7421 | isOk = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7422 | } |
| 7423 | break; |
| 7424 | #endif |
| 7425 | |
| 7426 | case SQLITE_TESTCTRL_IMPOSTER: |
| 7427 | if( nArg==5 ){ |
| 7428 | rc2 = sqlite3_test_control(testctrl, p->db, |
| 7429 | azArg[2], |
| 7430 | integerValue(azArg[3]), |
| 7431 | integerValue(azArg[4])); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7432 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7433 | } |
| 7434 | break; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 7435 | |
| 7436 | #ifdef YYCOVERAGE |
| 7437 | case SQLITE_TESTCTRL_PARSER_COVERAGE: |
| 7438 | if( nArg==2 ){ |
| 7439 | sqlite3_test_control(testctrl, p->out); |
| 7440 | isOk = 3; |
| 7441 | } |
| 7442 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7443 | } |
| 7444 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7445 | if( isOk==0 && iCtrl>=0 ){ |
| 7446 | utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage); |
| 7447 | rc = 1; |
| 7448 | }else if( isOk==1 ){ |
| 7449 | raw_printf(p->out, "%d\n", rc2); |
| 7450 | }else if( isOk==2 ){ |
| 7451 | raw_printf(p->out, "0x%08x\n", rc2); |
| 7452 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7453 | }else |
| 7454 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 7455 | |
| 7456 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
| 7457 | open_db(p, 0); |
| 7458 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
| 7459 | }else |
| 7460 | |
| 7461 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 7462 | if( nArg==2 ){ |
| 7463 | enableTimer = booleanValue(azArg[1]); |
| 7464 | if( enableTimer && !HAS_TIMER ){ |
| 7465 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
| 7466 | enableTimer = 0; |
| 7467 | } |
| 7468 | }else{ |
| 7469 | raw_printf(stderr, "Usage: .timer on|off\n"); |
| 7470 | rc = 1; |
| 7471 | } |
| 7472 | }else |
| 7473 | |
| 7474 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
| 7475 | open_db(p, 0); |
| 7476 | if( nArg!=2 ){ |
| 7477 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
| 7478 | rc = 1; |
| 7479 | goto meta_command_exit; |
| 7480 | } |
| 7481 | output_file_close(p->traceOut); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 7482 | p->traceOut = output_file_open(azArg[1], 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7483 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 7484 | if( p->traceOut==0 ){ |
| 7485 | sqlite3_trace_v2(p->db, 0, 0, 0); |
| 7486 | }else{ |
| 7487 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
| 7488 | } |
| 7489 | #endif |
| 7490 | }else |
| 7491 | |
| 7492 | #if SQLITE_USER_AUTHENTICATION |
| 7493 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 7494 | if( nArg<2 ){ |
| 7495 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
| 7496 | rc = 1; |
| 7497 | goto meta_command_exit; |
| 7498 | } |
| 7499 | open_db(p, 0); |
| 7500 | if( strcmp(azArg[1],"login")==0 ){ |
| 7501 | if( nArg!=4 ){ |
| 7502 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
| 7503 | rc = 1; |
| 7504 | goto meta_command_exit; |
| 7505 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7506 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3])); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7507 | if( rc ){ |
| 7508 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
| 7509 | rc = 1; |
| 7510 | } |
| 7511 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 7512 | if( nArg!=5 ){ |
| 7513 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
| 7514 | rc = 1; |
| 7515 | goto meta_command_exit; |
| 7516 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7517 | rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7518 | booleanValue(azArg[4])); |
| 7519 | if( rc ){ |
| 7520 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
| 7521 | rc = 1; |
| 7522 | } |
| 7523 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 7524 | if( nArg!=5 ){ |
| 7525 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
| 7526 | rc = 1; |
| 7527 | goto meta_command_exit; |
| 7528 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7529 | rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7530 | booleanValue(azArg[4])); |
| 7531 | if( rc ){ |
| 7532 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
| 7533 | rc = 1; |
| 7534 | } |
| 7535 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 7536 | if( nArg!=3 ){ |
| 7537 | raw_printf(stderr, "Usage: .user delete USER\n"); |
| 7538 | rc = 1; |
| 7539 | goto meta_command_exit; |
| 7540 | } |
| 7541 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 7542 | if( rc ){ |
| 7543 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
| 7544 | rc = 1; |
| 7545 | } |
| 7546 | }else{ |
| 7547 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
| 7548 | rc = 1; |
| 7549 | goto meta_command_exit; |
| 7550 | } |
| 7551 | }else |
| 7552 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 7553 | |
| 7554 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
| 7555 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
| 7556 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | 0ed2fd8 | 2018-01-16 20:05:27 +0000 | [diff] [blame] | 7557 | #if SQLITE_HAVE_ZLIB |
| 7558 | utf8_printf(p->out, "zlib version %s\n", zlibVersion()); |
| 7559 | #endif |
| 7560 | #define CTIMEOPT_VAL_(opt) #opt |
| 7561 | #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) |
| 7562 | #if defined(__clang__) && defined(__clang_major__) |
| 7563 | utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." |
| 7564 | CTIMEOPT_VAL(__clang_minor__) "." |
| 7565 | CTIMEOPT_VAL(__clang_patchlevel__) "\n"); |
| 7566 | #elif defined(_MSC_VER) |
| 7567 | utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); |
| 7568 | #elif defined(__GNUC__) && defined(__VERSION__) |
| 7569 | utf8_printf(p->out, "gcc-" __VERSION__ "\n"); |
| 7570 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7571 | }else |
| 7572 | |
| 7573 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 7574 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 7575 | sqlite3_vfs *pVfs = 0; |
| 7576 | if( p->db ){ |
| 7577 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 7578 | if( pVfs ){ |
| 7579 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 7580 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7581 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7582 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 7583 | } |
| 7584 | } |
| 7585 | }else |
| 7586 | |
| 7587 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 7588 | sqlite3_vfs *pVfs; |
| 7589 | sqlite3_vfs *pCurrent = 0; |
| 7590 | if( p->db ){ |
| 7591 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 7592 | } |
| 7593 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 7594 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 7595 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 7596 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7597 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7598 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 7599 | if( pVfs->pNext ){ |
| 7600 | raw_printf(p->out, "-----------------------------------\n"); |
| 7601 | } |
| 7602 | } |
| 7603 | }else |
| 7604 | |
| 7605 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 7606 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 7607 | char *zVfsName = 0; |
| 7608 | if( p->db ){ |
| 7609 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 7610 | if( zVfsName ){ |
| 7611 | utf8_printf(p->out, "%s\n", zVfsName); |
| 7612 | sqlite3_free(zVfsName); |
| 7613 | } |
| 7614 | } |
| 7615 | }else |
| 7616 | |
| 7617 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 7618 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
| 7619 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
| 7620 | }else |
| 7621 | #endif |
| 7622 | |
| 7623 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 7624 | int j; |
| 7625 | assert( nArg<=ArraySize(azArg) ); |
| 7626 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 7627 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
| 7628 | } |
| 7629 | }else |
| 7630 | |
| 7631 | { |
| 7632 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
| 7633 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
| 7634 | rc = 1; |
| 7635 | } |
| 7636 | |
| 7637 | meta_command_exit: |
| 7638 | if( p->outCount ){ |
| 7639 | p->outCount--; |
| 7640 | if( p->outCount==0 ) output_reset(p); |
| 7641 | } |
| 7642 | return rc; |
| 7643 | } |
| 7644 | |
| 7645 | /* |
| 7646 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 7647 | ** of string z[]. |
| 7648 | */ |
| 7649 | static int line_contains_semicolon(const char *z, int N){ |
| 7650 | int i; |
| 7651 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 7652 | return 0; |
| 7653 | } |
| 7654 | |
| 7655 | /* |
| 7656 | ** Test to see if a line consists entirely of whitespace. |
| 7657 | */ |
| 7658 | static int _all_whitespace(const char *z){ |
| 7659 | for(; *z; z++){ |
| 7660 | if( IsSpace(z[0]) ) continue; |
| 7661 | if( *z=='/' && z[1]=='*' ){ |
| 7662 | z += 2; |
| 7663 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 7664 | if( *z==0 ) return 0; |
| 7665 | z++; |
| 7666 | continue; |
| 7667 | } |
| 7668 | if( *z=='-' && z[1]=='-' ){ |
| 7669 | z += 2; |
| 7670 | while( *z && *z!='\n' ){ z++; } |
| 7671 | if( *z==0 ) return 1; |
| 7672 | continue; |
| 7673 | } |
| 7674 | return 0; |
| 7675 | } |
| 7676 | return 1; |
| 7677 | } |
| 7678 | |
| 7679 | /* |
| 7680 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 7681 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 7682 | ** as is the Oracle "/". |
| 7683 | */ |
| 7684 | static int line_is_command_terminator(const char *zLine){ |
| 7685 | while( IsSpace(zLine[0]) ){ zLine++; }; |
| 7686 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 7687 | return 1; /* Oracle */ |
| 7688 | } |
| 7689 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
| 7690 | && _all_whitespace(&zLine[2]) ){ |
| 7691 | return 1; /* SQL Server */ |
| 7692 | } |
| 7693 | return 0; |
| 7694 | } |
| 7695 | |
| 7696 | /* |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 7697 | ** We need a default sqlite3_complete() implementation to use in case |
| 7698 | ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes |
| 7699 | ** any arbitrary text is a complete SQL statement. This is not very |
| 7700 | ** user-friendly, but it does seem to work. |
| 7701 | */ |
| 7702 | #ifdef SQLITE_OMIT_COMPLETE |
| 7703 | int sqlite3_complete(const char *zSql){ return 1; } |
| 7704 | #endif |
| 7705 | |
| 7706 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7707 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 7708 | ** ends in the middle of a string literal or C-style comment. |
| 7709 | */ |
| 7710 | static int line_is_complete(char *zSql, int nSql){ |
| 7711 | int rc; |
| 7712 | if( zSql==0 ) return 1; |
| 7713 | zSql[nSql] = ';'; |
| 7714 | zSql[nSql+1] = 0; |
| 7715 | rc = sqlite3_complete(zSql); |
| 7716 | zSql[nSql] = 0; |
| 7717 | return rc; |
| 7718 | } |
| 7719 | |
| 7720 | /* |
| 7721 | ** Run a single line of SQL |
| 7722 | */ |
| 7723 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 7724 | int rc; |
| 7725 | char *zErrMsg = 0; |
| 7726 | |
| 7727 | open_db(p, 0); |
| 7728 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
| 7729 | BEGIN_TIMER; |
| 7730 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 7731 | END_TIMER; |
| 7732 | if( rc || zErrMsg ){ |
| 7733 | char zPrefix[100]; |
| 7734 | if( in!=0 || !stdin_is_interactive ){ |
| 7735 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 7736 | "Error: near line %d:", startline); |
| 7737 | }else{ |
| 7738 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 7739 | } |
| 7740 | if( zErrMsg!=0 ){ |
| 7741 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 7742 | sqlite3_free(zErrMsg); |
| 7743 | zErrMsg = 0; |
| 7744 | }else{ |
| 7745 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 7746 | } |
| 7747 | return 1; |
| 7748 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
| 7749 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 7750 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 7751 | } |
| 7752 | return 0; |
| 7753 | } |
| 7754 | |
| 7755 | |
| 7756 | /* |
| 7757 | ** Read input from *in and process it. If *in==0 then input |
| 7758 | ** is interactive - the user is typing it it. Otherwise, input |
| 7759 | ** is coming from a file or device. A prompt is issued and history |
| 7760 | ** is saved only if input is interactive. An interrupt signal will |
| 7761 | ** cause this routine to exit immediately, unless input is interactive. |
| 7762 | ** |
| 7763 | ** Return the number of errors. |
| 7764 | */ |
| 7765 | static int process_input(ShellState *p, FILE *in){ |
| 7766 | char *zLine = 0; /* A single input line */ |
| 7767 | char *zSql = 0; /* Accumulated SQL text */ |
| 7768 | int nLine; /* Length of current line */ |
| 7769 | int nSql = 0; /* Bytes of zSql[] used */ |
| 7770 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 7771 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
| 7772 | int rc; /* Error code */ |
| 7773 | int errCnt = 0; /* Number of errors seen */ |
| 7774 | int lineno = 0; /* Current line number */ |
| 7775 | int startline = 0; /* Line number for start of current input */ |
| 7776 | |
| 7777 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 7778 | fflush(p->out); |
| 7779 | zLine = one_input_line(in, zLine, nSql>0); |
| 7780 | if( zLine==0 ){ |
| 7781 | /* End of input */ |
| 7782 | if( in==0 && stdin_is_interactive ) printf("\n"); |
| 7783 | break; |
| 7784 | } |
| 7785 | if( seenInterrupt ){ |
| 7786 | if( in!=0 ) break; |
| 7787 | seenInterrupt = 0; |
| 7788 | } |
| 7789 | lineno++; |
| 7790 | if( nSql==0 && _all_whitespace(zLine) ){ |
| 7791 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
| 7792 | continue; |
| 7793 | } |
| 7794 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
| 7795 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
| 7796 | rc = do_meta_command(zLine, p); |
| 7797 | if( rc==2 ){ /* exit requested */ |
| 7798 | break; |
| 7799 | }else if( rc ){ |
| 7800 | errCnt++; |
| 7801 | } |
| 7802 | continue; |
| 7803 | } |
| 7804 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
| 7805 | memcpy(zLine,";",2); |
| 7806 | } |
| 7807 | nLine = strlen30(zLine); |
| 7808 | if( nSql+nLine+2>=nAlloc ){ |
| 7809 | nAlloc = nSql+nLine+100; |
| 7810 | zSql = realloc(zSql, nAlloc); |
| 7811 | if( zSql==0 ){ |
| 7812 | raw_printf(stderr, "Error: out of memory\n"); |
| 7813 | exit(1); |
| 7814 | } |
| 7815 | } |
| 7816 | nSqlPrior = nSql; |
| 7817 | if( nSql==0 ){ |
| 7818 | int i; |
| 7819 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
| 7820 | assert( nAlloc>0 && zSql!=0 ); |
| 7821 | memcpy(zSql, zLine+i, nLine+1-i); |
| 7822 | startline = lineno; |
| 7823 | nSql = nLine-i; |
| 7824 | }else{ |
| 7825 | zSql[nSql++] = '\n'; |
| 7826 | memcpy(zSql+nSql, zLine, nLine+1); |
| 7827 | nSql += nLine; |
| 7828 | } |
| 7829 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
| 7830 | && sqlite3_complete(zSql) ){ |
| 7831 | errCnt += runOneSqlLine(p, zSql, in, startline); |
| 7832 | nSql = 0; |
| 7833 | if( p->outCount ){ |
| 7834 | output_reset(p); |
| 7835 | p->outCount = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 7836 | }else{ |
| 7837 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7838 | } |
| 7839 | }else if( nSql && _all_whitespace(zSql) ){ |
| 7840 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
| 7841 | nSql = 0; |
| 7842 | } |
| 7843 | } |
| 7844 | if( nSql && !_all_whitespace(zSql) ){ |
| 7845 | runOneSqlLine(p, zSql, in, startline); |
| 7846 | } |
| 7847 | free(zSql); |
| 7848 | free(zLine); |
| 7849 | return errCnt>0; |
| 7850 | } |
| 7851 | |
| 7852 | /* |
| 7853 | ** Return a pathname which is the user's home directory. A |
| 7854 | ** 0 return indicates an error of some kind. |
| 7855 | */ |
| 7856 | static char *find_home_dir(int clearFlag){ |
| 7857 | static char *home_dir = NULL; |
| 7858 | if( clearFlag ){ |
| 7859 | free(home_dir); |
| 7860 | home_dir = 0; |
| 7861 | return 0; |
| 7862 | } |
| 7863 | if( home_dir ) return home_dir; |
| 7864 | |
| 7865 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 7866 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 7867 | { |
| 7868 | struct passwd *pwent; |
| 7869 | uid_t uid = getuid(); |
| 7870 | if( (pwent=getpwuid(uid)) != NULL) { |
| 7871 | home_dir = pwent->pw_dir; |
| 7872 | } |
| 7873 | } |
| 7874 | #endif |
| 7875 | |
| 7876 | #if defined(_WIN32_WCE) |
| 7877 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 7878 | */ |
| 7879 | home_dir = "/"; |
| 7880 | #else |
| 7881 | |
| 7882 | #if defined(_WIN32) || defined(WIN32) |
| 7883 | if (!home_dir) { |
| 7884 | home_dir = getenv("USERPROFILE"); |
| 7885 | } |
| 7886 | #endif |
| 7887 | |
| 7888 | if (!home_dir) { |
| 7889 | home_dir = getenv("HOME"); |
| 7890 | } |
| 7891 | |
| 7892 | #if defined(_WIN32) || defined(WIN32) |
| 7893 | if (!home_dir) { |
| 7894 | char *zDrive, *zPath; |
| 7895 | int n; |
| 7896 | zDrive = getenv("HOMEDRIVE"); |
| 7897 | zPath = getenv("HOMEPATH"); |
| 7898 | if( zDrive && zPath ){ |
| 7899 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
| 7900 | home_dir = malloc( n ); |
| 7901 | if( home_dir==0 ) return 0; |
| 7902 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 7903 | return home_dir; |
| 7904 | } |
| 7905 | home_dir = "c:\\"; |
| 7906 | } |
| 7907 | #endif |
| 7908 | |
| 7909 | #endif /* !_WIN32_WCE */ |
| 7910 | |
| 7911 | if( home_dir ){ |
| 7912 | int n = strlen30(home_dir) + 1; |
| 7913 | char *z = malloc( n ); |
| 7914 | if( z ) memcpy(z, home_dir, n); |
| 7915 | home_dir = z; |
| 7916 | } |
| 7917 | |
| 7918 | return home_dir; |
| 7919 | } |
| 7920 | |
| 7921 | /* |
| 7922 | ** Read input from the file given by sqliterc_override. Or if that |
| 7923 | ** parameter is NULL, take input from ~/.sqliterc |
| 7924 | ** |
| 7925 | ** Returns the number of errors. |
| 7926 | */ |
| 7927 | static void process_sqliterc( |
| 7928 | ShellState *p, /* Configuration data */ |
| 7929 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 7930 | ){ |
| 7931 | char *home_dir = NULL; |
| 7932 | const char *sqliterc = sqliterc_override; |
| 7933 | char *zBuf = 0; |
| 7934 | FILE *in = NULL; |
| 7935 | |
| 7936 | if (sqliterc == NULL) { |
| 7937 | home_dir = find_home_dir(0); |
| 7938 | if( home_dir==0 ){ |
| 7939 | raw_printf(stderr, "-- warning: cannot find home directory;" |
| 7940 | " cannot read ~/.sqliterc\n"); |
| 7941 | return; |
| 7942 | } |
| 7943 | sqlite3_initialize(); |
| 7944 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 7945 | sqliterc = zBuf; |
| 7946 | } |
| 7947 | in = fopen(sqliterc,"rb"); |
| 7948 | if( in ){ |
| 7949 | if( stdin_is_interactive ){ |
| 7950 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
| 7951 | } |
| 7952 | process_input(p,in); |
| 7953 | fclose(in); |
| 7954 | } |
| 7955 | sqlite3_free(zBuf); |
| 7956 | } |
| 7957 | |
| 7958 | /* |
| 7959 | ** Show available command line options |
| 7960 | */ |
| 7961 | static const char zOptions[] = |
| 7962 | " -ascii set output mode to 'ascii'\n" |
| 7963 | " -bail stop after hitting an error\n" |
| 7964 | " -batch force batch I/O\n" |
| 7965 | " -column set output mode to 'column'\n" |
| 7966 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
| 7967 | " -csv set output mode to 'csv'\n" |
| 7968 | " -echo print commands before execution\n" |
| 7969 | " -init FILENAME read/process named file\n" |
| 7970 | " -[no]header turn headers on or off\n" |
| 7971 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 7972 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 7973 | #endif |
| 7974 | " -help show this message\n" |
| 7975 | " -html set output mode to HTML\n" |
| 7976 | " -interactive force interactive I/O\n" |
| 7977 | " -line set output mode to 'line'\n" |
| 7978 | " -list set output mode to 'list'\n" |
| 7979 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
| 7980 | " -mmap N default mmap size set to N\n" |
| 7981 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7982 | " -multiplex enable the multiplexor VFS\n" |
| 7983 | #endif |
| 7984 | " -newline SEP set output row separator. Default: '\\n'\n" |
| 7985 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 7986 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 7987 | " -quote set output mode to 'quote'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7988 | " -separator SEP set output column separator. Default: '|'\n" |
| 7989 | " -stats print memory stats before each finalize\n" |
| 7990 | " -version show SQLite version\n" |
| 7991 | " -vfs NAME use NAME as the default VFS\n" |
| 7992 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 7993 | " -vfstrace enable tracing of all VFS calls\n" |
| 7994 | #endif |
| 7995 | ; |
| 7996 | static void usage(int showDetail){ |
| 7997 | utf8_printf(stderr, |
| 7998 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 7999 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 8000 | "if the file does not previously exist.\n", Argv0); |
| 8001 | if( showDetail ){ |
| 8002 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
| 8003 | }else{ |
| 8004 | raw_printf(stderr, "Use the -help option for additional information\n"); |
| 8005 | } |
| 8006 | exit(1); |
| 8007 | } |
| 8008 | |
| 8009 | /* |
| 8010 | ** Initialize the state information in data |
| 8011 | */ |
| 8012 | static void main_init(ShellState *data) { |
| 8013 | memset(data, 0, sizeof(*data)); |
| 8014 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 8015 | data->autoExplain = 1; |
| 8016 | memcpy(data->colSeparator,SEP_Column, 2); |
| 8017 | memcpy(data->rowSeparator,SEP_Row, 2); |
| 8018 | data->showHeader = 0; |
| 8019 | data->shellFlgs = SHFLG_Lookaside; |
| 8020 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
| 8021 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
| 8022 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 8023 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 8024 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
| 8025 | } |
| 8026 | |
| 8027 | /* |
| 8028 | ** Output text to the console in a font that attracts extra attention. |
| 8029 | */ |
| 8030 | #ifdef _WIN32 |
| 8031 | static void printBold(const char *zText){ |
| 8032 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 8033 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 8034 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 8035 | SetConsoleTextAttribute(out, |
| 8036 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 8037 | ); |
| 8038 | printf("%s", zText); |
| 8039 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
| 8040 | } |
| 8041 | #else |
| 8042 | static void printBold(const char *zText){ |
| 8043 | printf("\033[1m%s\033[0m", zText); |
| 8044 | } |
| 8045 | #endif |
| 8046 | |
| 8047 | /* |
| 8048 | ** Get the argument to an --option. Throw an error and die if no argument |
| 8049 | ** is available. |
| 8050 | */ |
| 8051 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 8052 | if( i==argc ){ |
| 8053 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
| 8054 | argv[0], argv[argc-1]); |
| 8055 | exit(1); |
| 8056 | } |
| 8057 | return argv[i]; |
| 8058 | } |
| 8059 | |
| 8060 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 8061 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 8062 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 8063 | # else |
| 8064 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 8065 | # endif |
| 8066 | #endif |
| 8067 | |
| 8068 | #if SQLITE_SHELL_IS_UTF8 |
| 8069 | int SQLITE_CDECL main(int argc, char **argv){ |
| 8070 | #else |
| 8071 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 8072 | char **argv; |
| 8073 | #endif |
| 8074 | char *zErrMsg = 0; |
| 8075 | ShellState data; |
| 8076 | const char *zInitFile = 0; |
| 8077 | int i; |
| 8078 | int rc = 0; |
| 8079 | int warnInmemoryDb = 0; |
| 8080 | int readStdin = 1; |
| 8081 | int nCmd = 0; |
| 8082 | char **azCmd = 0; |
| 8083 | |
| 8084 | setBinaryMode(stdin, 0); |
| 8085 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 8086 | stdin_is_interactive = isatty(0); |
| 8087 | stdout_is_console = isatty(1); |
| 8088 | |
| 8089 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 8090 | if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8091 | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
| 8092 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 8093 | exit(1); |
| 8094 | } |
| 8095 | #endif |
| 8096 | main_init(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8097 | sqlite3_initialize(); |
drh | 4b18c1d | 2018-02-04 20:33:13 +0000 | [diff] [blame] | 8098 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8099 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 8100 | if( argv==0 ){ |
| 8101 | raw_printf(stderr, "out of memory\n"); |
| 8102 | exit(1); |
| 8103 | } |
| 8104 | for(i=0; i<argc; i++){ |
| 8105 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 8106 | if( argv[i]==0 ){ |
| 8107 | raw_printf(stderr, "out of memory\n"); |
| 8108 | exit(1); |
| 8109 | } |
| 8110 | } |
| 8111 | #endif |
| 8112 | assert( argc>=1 && argv && argv[0] ); |
| 8113 | Argv0 = argv[0]; |
| 8114 | |
| 8115 | /* Make sure we have a valid signal handler early, before anything |
| 8116 | ** else is done. |
| 8117 | */ |
| 8118 | #ifdef SIGINT |
| 8119 | signal(SIGINT, interrupt_handler); |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 8120 | #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 8121 | SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8122 | #endif |
| 8123 | |
| 8124 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 8125 | { |
| 8126 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 8127 | ** of a C-function that will provide the name of the database file. Use |
| 8128 | ** this compile-time option to embed this shell program in larger |
| 8129 | ** applications. */ |
| 8130 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 8131 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 8132 | warnInmemoryDb = 0; |
| 8133 | } |
| 8134 | #endif |
| 8135 | |
| 8136 | /* Do an initial pass through the command-line argument to locate |
| 8137 | ** the name of the database file, the name of the initialization file, |
| 8138 | ** the size of the alternative malloc heap, |
| 8139 | ** and the first command to execute. |
| 8140 | */ |
| 8141 | for(i=1; i<argc; i++){ |
| 8142 | char *z; |
| 8143 | z = argv[i]; |
| 8144 | if( z[0]!='-' ){ |
| 8145 | if( data.zDbFilename==0 ){ |
| 8146 | data.zDbFilename = z; |
| 8147 | }else{ |
| 8148 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 8149 | ** mean that nothing is read from stdin */ |
| 8150 | readStdin = 0; |
| 8151 | nCmd++; |
| 8152 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 8153 | if( azCmd==0 ){ |
| 8154 | raw_printf(stderr, "out of memory\n"); |
| 8155 | exit(1); |
| 8156 | } |
| 8157 | azCmd[nCmd-1] = z; |
| 8158 | } |
| 8159 | } |
| 8160 | if( z[1]=='-' ) z++; |
| 8161 | if( strcmp(z,"-separator")==0 |
| 8162 | || strcmp(z,"-nullvalue")==0 |
| 8163 | || strcmp(z,"-newline")==0 |
| 8164 | || strcmp(z,"-cmd")==0 |
| 8165 | ){ |
| 8166 | (void)cmdline_option_value(argc, argv, ++i); |
| 8167 | }else if( strcmp(z,"-init")==0 ){ |
| 8168 | zInitFile = cmdline_option_value(argc, argv, ++i); |
| 8169 | }else if( strcmp(z,"-batch")==0 ){ |
| 8170 | /* Need to check for batch mode here to so we can avoid printing |
| 8171 | ** informational messages (like from process_sqliterc) before |
| 8172 | ** we do the actual processing of arguments later in a second pass. |
| 8173 | */ |
| 8174 | stdin_is_interactive = 0; |
| 8175 | }else if( strcmp(z,"-heap")==0 ){ |
| 8176 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 8177 | const char *zSize; |
| 8178 | sqlite3_int64 szHeap; |
| 8179 | |
| 8180 | zSize = cmdline_option_value(argc, argv, ++i); |
| 8181 | szHeap = integerValue(zSize); |
| 8182 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
| 8183 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
| 8184 | #else |
| 8185 | (void)cmdline_option_value(argc, argv, ++i); |
| 8186 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8187 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8188 | int n, sz; |
| 8189 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8190 | if( sz>70000 ) sz = 70000; |
| 8191 | if( sz<0 ) sz = 0; |
| 8192 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8193 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 8194 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
| 8195 | data.shellFlgs |= SHFLG_Pagecache; |
| 8196 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8197 | int n, sz; |
| 8198 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8199 | if( sz<0 ) sz = 0; |
| 8200 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8201 | if( n<0 ) n = 0; |
| 8202 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 8203 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
| 8204 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 8205 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 8206 | extern int vfstrace_register( |
| 8207 | const char *zTraceName, |
| 8208 | const char *zOldVfsName, |
| 8209 | int (*xOut)(const char*,void*), |
| 8210 | void *pOutArg, |
| 8211 | int makeDefault |
| 8212 | ); |
| 8213 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
| 8214 | #endif |
| 8215 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8216 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 8217 | extern int sqlite3_multiple_initialize(const char*,int); |
| 8218 | sqlite3_multiplex_initialize(0, 1); |
| 8219 | #endif |
| 8220 | }else if( strcmp(z,"-mmap")==0 ){ |
| 8221 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 8222 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
| 8223 | }else if( strcmp(z,"-vfs")==0 ){ |
| 8224 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
| 8225 | if( pVfs ){ |
| 8226 | sqlite3_vfs_register(pVfs, 1); |
| 8227 | }else{ |
| 8228 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
| 8229 | exit(1); |
| 8230 | } |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 8231 | #ifdef SQLITE_HAVE_ZIP |
| 8232 | }else if( strcmp(z,"-zip")==0 ){ |
| 8233 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 8234 | #endif |
| 8235 | }else if( strcmp(z,"-append")==0 ){ |
| 8236 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8237 | } |
| 8238 | } |
| 8239 | if( data.zDbFilename==0 ){ |
| 8240 | #ifndef SQLITE_OMIT_MEMORYDB |
| 8241 | data.zDbFilename = ":memory:"; |
| 8242 | warnInmemoryDb = argc==1; |
| 8243 | #else |
| 8244 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 8245 | return 1; |
| 8246 | #endif |
| 8247 | } |
| 8248 | data.out = stdout; |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 8249 | sqlite3_appendvfs_init(0,0,0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8250 | |
| 8251 | /* Go ahead and open the database file if it already exists. If the |
| 8252 | ** file does not exist, delay opening it. This prevents empty database |
| 8253 | ** files from being created if a user mistypes the database name argument |
| 8254 | ** to the sqlite command-line tool. |
| 8255 | */ |
| 8256 | if( access(data.zDbFilename, 0)==0 ){ |
| 8257 | open_db(&data, 0); |
| 8258 | } |
| 8259 | |
| 8260 | /* Process the initialization file if there is one. If no -init option |
| 8261 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 8262 | ** try to process it. |
| 8263 | */ |
| 8264 | process_sqliterc(&data,zInitFile); |
| 8265 | |
| 8266 | /* Make a second pass through the command-line argument and set |
| 8267 | ** options. This second pass is delayed until after the initialization |
| 8268 | ** file is processed so that the command-line arguments will override |
| 8269 | ** settings in the initialization file. |
| 8270 | */ |
| 8271 | for(i=1; i<argc; i++){ |
| 8272 | char *z = argv[i]; |
| 8273 | if( z[0]!='-' ) continue; |
| 8274 | if( z[1]=='-' ){ z++; } |
| 8275 | if( strcmp(z,"-init")==0 ){ |
| 8276 | i++; |
| 8277 | }else if( strcmp(z,"-html")==0 ){ |
| 8278 | data.mode = MODE_Html; |
| 8279 | }else if( strcmp(z,"-list")==0 ){ |
| 8280 | data.mode = MODE_List; |
| 8281 | }else if( strcmp(z,"-quote")==0 ){ |
| 8282 | data.mode = MODE_Quote; |
| 8283 | }else if( strcmp(z,"-line")==0 ){ |
| 8284 | data.mode = MODE_Line; |
| 8285 | }else if( strcmp(z,"-column")==0 ){ |
| 8286 | data.mode = MODE_Column; |
| 8287 | }else if( strcmp(z,"-csv")==0 ){ |
| 8288 | data.mode = MODE_Csv; |
| 8289 | memcpy(data.colSeparator,",",2); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 8290 | #ifdef SQLITE_HAVE_ZIP |
| 8291 | }else if( strcmp(z,"-zip")==0 ){ |
| 8292 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 8293 | #endif |
| 8294 | }else if( strcmp(z,"-append")==0 ){ |
| 8295 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8296 | }else if( strcmp(z,"-ascii")==0 ){ |
| 8297 | data.mode = MODE_Ascii; |
| 8298 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 8299 | SEP_Unit); |
| 8300 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 8301 | SEP_Record); |
| 8302 | }else if( strcmp(z,"-separator")==0 ){ |
| 8303 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 8304 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8305 | }else if( strcmp(z,"-newline")==0 ){ |
| 8306 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 8307 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8308 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 8309 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
| 8310 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8311 | }else if( strcmp(z,"-header")==0 ){ |
| 8312 | data.showHeader = 1; |
| 8313 | }else if( strcmp(z,"-noheader")==0 ){ |
| 8314 | data.showHeader = 0; |
| 8315 | }else if( strcmp(z,"-echo")==0 ){ |
| 8316 | ShellSetFlag(&data, SHFLG_Echo); |
| 8317 | }else if( strcmp(z,"-eqp")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8318 | data.autoEQP = AUTOEQP_on; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8319 | }else if( strcmp(z,"-eqpfull")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8320 | data.autoEQP = AUTOEQP_full; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8321 | }else if( strcmp(z,"-stats")==0 ){ |
| 8322 | data.statsOn = 1; |
| 8323 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 8324 | data.scanstatsOn = 1; |
| 8325 | }else if( strcmp(z,"-backslash")==0 ){ |
| 8326 | /* Undocumented command-line option: -backslash |
| 8327 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 8328 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 8329 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 8330 | */ |
| 8331 | ShellSetFlag(&data, SHFLG_Backslash); |
| 8332 | }else if( strcmp(z,"-bail")==0 ){ |
| 8333 | bail_on_error = 1; |
| 8334 | }else if( strcmp(z,"-version")==0 ){ |
| 8335 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 8336 | return 0; |
| 8337 | }else if( strcmp(z,"-interactive")==0 ){ |
| 8338 | stdin_is_interactive = 1; |
| 8339 | }else if( strcmp(z,"-batch")==0 ){ |
| 8340 | stdin_is_interactive = 0; |
| 8341 | }else if( strcmp(z,"-heap")==0 ){ |
| 8342 | i++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8343 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8344 | i+=2; |
| 8345 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8346 | i+=2; |
| 8347 | }else if( strcmp(z,"-mmap")==0 ){ |
| 8348 | i++; |
| 8349 | }else if( strcmp(z,"-vfs")==0 ){ |
| 8350 | i++; |
| 8351 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 8352 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 8353 | i++; |
| 8354 | #endif |
| 8355 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8356 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 8357 | i++; |
| 8358 | #endif |
| 8359 | }else if( strcmp(z,"-help")==0 ){ |
| 8360 | usage(1); |
| 8361 | }else if( strcmp(z,"-cmd")==0 ){ |
| 8362 | /* Run commands that follow -cmd first and separately from commands |
| 8363 | ** that simply appear on the command-line. This seems goofy. It would |
| 8364 | ** be better if all commands ran in the order that they appear. But |
| 8365 | ** we retain the goofy behavior for historical compatibility. */ |
| 8366 | if( i==argc-1 ) break; |
| 8367 | z = cmdline_option_value(argc,argv,++i); |
| 8368 | if( z[0]=='.' ){ |
| 8369 | rc = do_meta_command(z, &data); |
| 8370 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
| 8371 | }else{ |
| 8372 | open_db(&data, 0); |
| 8373 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 8374 | if( zErrMsg!=0 ){ |
| 8375 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 8376 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 8377 | }else if( rc!=0 ){ |
| 8378 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
| 8379 | if( bail_on_error ) return rc; |
| 8380 | } |
| 8381 | } |
| 8382 | }else{ |
| 8383 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 8384 | raw_printf(stderr,"Use -help for a list of options.\n"); |
| 8385 | return 1; |
| 8386 | } |
| 8387 | data.cMode = data.mode; |
| 8388 | } |
| 8389 | |
| 8390 | if( !readStdin ){ |
| 8391 | /* Run all arguments that do not begin with '-' as if they were separate |
| 8392 | ** command-line inputs, except for the argToSkip argument which contains |
| 8393 | ** the database filename. |
| 8394 | */ |
| 8395 | for(i=0; i<nCmd; i++){ |
| 8396 | if( azCmd[i][0]=='.' ){ |
| 8397 | rc = do_meta_command(azCmd[i], &data); |
| 8398 | if( rc ) return rc==2 ? 0 : rc; |
| 8399 | }else{ |
| 8400 | open_db(&data, 0); |
| 8401 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 8402 | if( zErrMsg!=0 ){ |
| 8403 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 8404 | return rc!=0 ? rc : 1; |
| 8405 | }else if( rc!=0 ){ |
| 8406 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| 8407 | return rc; |
| 8408 | } |
| 8409 | } |
| 8410 | } |
| 8411 | free(azCmd); |
| 8412 | }else{ |
| 8413 | /* Run commands received from standard input |
| 8414 | */ |
| 8415 | if( stdin_is_interactive ){ |
| 8416 | char *zHome; |
| 8417 | char *zHistory = 0; |
| 8418 | int nHistory; |
| 8419 | printf( |
| 8420 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
| 8421 | "Enter \".help\" for usage hints.\n", |
| 8422 | sqlite3_libversion(), sqlite3_sourceid() |
| 8423 | ); |
| 8424 | if( warnInmemoryDb ){ |
| 8425 | printf("Connected to a "); |
| 8426 | printBold("transient in-memory database"); |
| 8427 | printf(".\nUse \".open FILENAME\" to reopen on a " |
| 8428 | "persistent database.\n"); |
| 8429 | } |
| 8430 | zHome = find_home_dir(0); |
| 8431 | if( zHome ){ |
| 8432 | nHistory = strlen30(zHome) + 20; |
| 8433 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 8434 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 8435 | } |
| 8436 | } |
| 8437 | if( zHistory ){ shell_read_history(zHistory); } |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 8438 | #if HAVE_READLINE || HAVE_EDITLINE |
| 8439 | rl_attempted_completion_function = readline_completion; |
| 8440 | #elif HAVE_LINENOISE |
| 8441 | linenoiseSetCompletionCallback(linenoise_completion); |
| 8442 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8443 | rc = process_input(&data, 0); |
| 8444 | if( zHistory ){ |
drh | 5a75dd8 | 2017-07-18 20:59:40 +0000 | [diff] [blame] | 8445 | shell_stifle_history(2000); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8446 | shell_write_history(zHistory); |
| 8447 | free(zHistory); |
| 8448 | } |
| 8449 | }else{ |
| 8450 | rc = process_input(&data, stdin); |
| 8451 | } |
| 8452 | } |
| 8453 | set_table_name(&data, 0); |
| 8454 | if( data.db ){ |
| 8455 | session_close_all(&data); |
| 8456 | sqlite3_close(data.db); |
| 8457 | } |
| 8458 | sqlite3_free(data.zFreeOnClose); |
| 8459 | find_home_dir(1); |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 8460 | output_reset(&data); |
| 8461 | data.doXdgOpen = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 8462 | clearTempFile(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8463 | #if !SQLITE_SHELL_IS_UTF8 |
| 8464 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 8465 | sqlite3_free(argv); |
| 8466 | #endif |
| 8467 | return rc; |
| 8468 | } |