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 | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 82 | # define GETPID getpid |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 83 | # if defined(__MINGW32__) |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 84 | # define DIRENT dirent |
mistachkin | 2f74b3c | 2018-01-05 20:26:06 +0000 | [diff] [blame] | 85 | # ifndef S_ISLNK |
| 86 | # define S_ISLNK(mode) (0) |
| 87 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 88 | # endif |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 89 | #else |
| 90 | # define GETPID (int)GetCurrentProcessId |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 91 | #endif |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 92 | #include <sys/types.h> |
| 93 | #include <sys/stat.h> |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 94 | |
| 95 | #if HAVE_READLINE |
| 96 | # include <readline/readline.h> |
| 97 | # include <readline/history.h> |
| 98 | #endif |
| 99 | |
| 100 | #if HAVE_EDITLINE |
| 101 | # include <editline/readline.h> |
| 102 | #endif |
| 103 | |
| 104 | #if HAVE_EDITLINE || HAVE_READLINE |
| 105 | |
| 106 | # define shell_add_history(X) add_history(X) |
| 107 | # define shell_read_history(X) read_history(X) |
| 108 | # define shell_write_history(X) write_history(X) |
| 109 | # define shell_stifle_history(X) stifle_history(X) |
| 110 | # define shell_readline(X) readline(X) |
| 111 | |
| 112 | #elif HAVE_LINENOISE |
| 113 | |
| 114 | # include "linenoise.h" |
| 115 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 116 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 117 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 118 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 119 | # define shell_readline(X) linenoise(X) |
| 120 | |
| 121 | #else |
| 122 | |
| 123 | # define shell_read_history(X) |
| 124 | # define shell_write_history(X) |
| 125 | # define shell_stifle_history(X) |
| 126 | |
| 127 | # define SHELL_USE_LOCAL_GETLINE 1 |
| 128 | #endif |
| 129 | |
| 130 | |
| 131 | #if defined(_WIN32) || defined(WIN32) |
| 132 | # include <io.h> |
| 133 | # include <fcntl.h> |
| 134 | # define isatty(h) _isatty(h) |
| 135 | # ifndef access |
| 136 | # define access(f,m) _access((f),(m)) |
| 137 | # endif |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 138 | # ifndef unlink |
| 139 | # define unlink _unlink |
| 140 | # endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 141 | # undef popen |
| 142 | # define popen _popen |
| 143 | # undef pclose |
| 144 | # define pclose _pclose |
| 145 | #else |
| 146 | /* Make sure isatty() has a prototype. */ |
| 147 | extern int isatty(int); |
| 148 | |
| 149 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 150 | /* popen and pclose are not C89 functions and so are |
| 151 | ** sometimes omitted from the <stdio.h> header */ |
| 152 | extern FILE *popen(const char*,const char*); |
| 153 | extern int pclose(FILE*); |
| 154 | # else |
| 155 | # define SQLITE_OMIT_POPEN 1 |
| 156 | # endif |
| 157 | #endif |
| 158 | |
| 159 | #if defined(_WIN32_WCE) |
| 160 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 161 | * thus we always assume that we have a console. That can be |
| 162 | * overridden with the -batch command line option. |
| 163 | */ |
| 164 | #define isatty(x) 1 |
| 165 | #endif |
| 166 | |
| 167 | /* ctype macros that work with signed characters */ |
| 168 | #define IsSpace(X) isspace((unsigned char)X) |
| 169 | #define IsDigit(X) isdigit((unsigned char)X) |
| 170 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 171 | |
| 172 | #if defined(_WIN32) || defined(WIN32) |
| 173 | #include <windows.h> |
| 174 | |
| 175 | /* string conversion routines only needed on Win32 */ |
| 176 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 177 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 178 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| 179 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 180 | #endif |
| 181 | |
| 182 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 183 | ** are automatically translated into \r\n. However, this behavior needs |
| 184 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 185 | ** rendering quoted strings that contain \n characters). The following |
| 186 | ** routines take care of that. |
| 187 | */ |
| 188 | #if defined(_WIN32) || defined(WIN32) |
| 189 | static void setBinaryMode(FILE *file, int isOutput){ |
| 190 | if( isOutput ) fflush(file); |
| 191 | _setmode(_fileno(file), _O_BINARY); |
| 192 | } |
| 193 | static void setTextMode(FILE *file, int isOutput){ |
| 194 | if( isOutput ) fflush(file); |
| 195 | _setmode(_fileno(file), _O_TEXT); |
| 196 | } |
| 197 | #else |
| 198 | # define setBinaryMode(X,Y) |
| 199 | # define setTextMode(X,Y) |
| 200 | #endif |
| 201 | |
| 202 | |
| 203 | /* True if the timer is enabled */ |
| 204 | static int enableTimer = 0; |
| 205 | |
| 206 | /* Return the current wall-clock time */ |
| 207 | static sqlite3_int64 timeOfDay(void){ |
| 208 | static sqlite3_vfs *clockVfs = 0; |
| 209 | sqlite3_int64 t; |
| 210 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 211 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 212 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 213 | }else{ |
| 214 | double r; |
| 215 | clockVfs->xCurrentTime(clockVfs, &r); |
| 216 | t = (sqlite3_int64)(r*86400000.0); |
| 217 | } |
| 218 | return t; |
| 219 | } |
| 220 | |
| 221 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
| 222 | #include <sys/time.h> |
| 223 | #include <sys/resource.h> |
| 224 | |
| 225 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 226 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 227 | struct rusage { |
| 228 | struct timeval ru_utime; /* user CPU time used */ |
| 229 | struct timeval ru_stime; /* system CPU time used */ |
| 230 | }; |
| 231 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 232 | #endif |
| 233 | |
| 234 | /* Saved resource information for the beginning of an operation */ |
| 235 | static struct rusage sBegin; /* CPU time at start */ |
| 236 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 237 | |
| 238 | /* |
| 239 | ** Begin timing an operation |
| 240 | */ |
| 241 | static void beginTimer(void){ |
| 242 | if( enableTimer ){ |
| 243 | getrusage(RUSAGE_SELF, &sBegin); |
| 244 | iBegin = timeOfDay(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* Return the difference of two time_structs in seconds */ |
| 249 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 250 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 251 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** Print the timing results. |
| 256 | */ |
| 257 | static void endTimer(void){ |
| 258 | if( enableTimer ){ |
| 259 | sqlite3_int64 iEnd = timeOfDay(); |
| 260 | struct rusage sEnd; |
| 261 | getrusage(RUSAGE_SELF, &sEnd); |
| 262 | printf("Run Time: real %.3f user %f sys %f\n", |
| 263 | (iEnd - iBegin)*0.001, |
| 264 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 265 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #define BEGIN_TIMER beginTimer() |
| 270 | #define END_TIMER endTimer() |
| 271 | #define HAS_TIMER 1 |
| 272 | |
| 273 | #elif (defined(_WIN32) || defined(WIN32)) |
| 274 | |
| 275 | /* Saved resource information for the beginning of an operation */ |
| 276 | static HANDLE hProcess; |
| 277 | static FILETIME ftKernelBegin; |
| 278 | static FILETIME ftUserBegin; |
| 279 | static sqlite3_int64 ftWallBegin; |
| 280 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 281 | LPFILETIME, LPFILETIME); |
| 282 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 283 | |
| 284 | /* |
| 285 | ** Check to see if we have timer support. Return 1 if necessary |
| 286 | ** support found (or found previously). |
| 287 | */ |
| 288 | static int hasTimer(void){ |
| 289 | if( getProcessTimesAddr ){ |
| 290 | return 1; |
| 291 | } else { |
| 292 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 293 | ** versions. See if the version we are running on has it, and if it |
| 294 | ** does, save off a pointer to it and the current process handle. |
| 295 | */ |
| 296 | hProcess = GetCurrentProcess(); |
| 297 | if( hProcess ){ |
| 298 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 299 | if( NULL != hinstLib ){ |
| 300 | getProcessTimesAddr = |
| 301 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 302 | if( NULL != getProcessTimesAddr ){ |
| 303 | return 1; |
| 304 | } |
| 305 | FreeLibrary(hinstLib); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | ** Begin timing an operation |
| 314 | */ |
| 315 | static void beginTimer(void){ |
| 316 | if( enableTimer && getProcessTimesAddr ){ |
| 317 | FILETIME ftCreation, ftExit; |
| 318 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 319 | &ftKernelBegin,&ftUserBegin); |
| 320 | ftWallBegin = timeOfDay(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* Return the difference of two FILETIME structs in seconds */ |
| 325 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 326 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 327 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 328 | return (double) ((i64End - i64Start) / 10000000.0); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | ** Print the timing results. |
| 333 | */ |
| 334 | static void endTimer(void){ |
| 335 | if( enableTimer && getProcessTimesAddr){ |
| 336 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 337 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 338 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 339 | printf("Run Time: real %.3f user %f sys %f\n", |
| 340 | (ftWallEnd - ftWallBegin)*0.001, |
| 341 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 342 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | #define BEGIN_TIMER beginTimer() |
| 347 | #define END_TIMER endTimer() |
| 348 | #define HAS_TIMER hasTimer() |
| 349 | |
| 350 | #else |
| 351 | #define BEGIN_TIMER |
| 352 | #define END_TIMER |
| 353 | #define HAS_TIMER 0 |
| 354 | #endif |
| 355 | |
| 356 | /* |
| 357 | ** Used to prevent warnings about unused parameters |
| 358 | */ |
| 359 | #define UNUSED_PARAMETER(x) (void)(x) |
| 360 | |
| 361 | /* |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 362 | ** Number of elements in an array |
| 363 | */ |
| 364 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
| 365 | |
| 366 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 367 | ** If the following flag is set, then command execution stops |
| 368 | ** at an error if we are not interactive. |
| 369 | */ |
| 370 | static int bail_on_error = 0; |
| 371 | |
| 372 | /* |
| 373 | ** Threat stdin as an interactive input if the following variable |
| 374 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 375 | */ |
| 376 | static int stdin_is_interactive = 1; |
| 377 | |
| 378 | /* |
| 379 | ** On Windows systems we have to know if standard output is a console |
| 380 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 381 | ** true if translation is required. |
| 382 | */ |
| 383 | static int stdout_is_console = 1; |
| 384 | |
| 385 | /* |
| 386 | ** The following is the open SQLite database. We make a pointer |
| 387 | ** to this database a static variable so that it can be accessed |
| 388 | ** by the SIGINT handler to interrupt database processing. |
| 389 | */ |
| 390 | static sqlite3 *globalDb = 0; |
| 391 | |
| 392 | /* |
| 393 | ** True if an interrupt (Control-C) has been received. |
| 394 | */ |
| 395 | static volatile int seenInterrupt = 0; |
| 396 | |
| 397 | /* |
| 398 | ** This is the name of our program. It is set in main(), used |
| 399 | ** in a number of other places, mostly for error messages. |
| 400 | */ |
| 401 | static char *Argv0; |
| 402 | |
| 403 | /* |
| 404 | ** Prompt strings. Initialized in main. Settable with |
| 405 | ** .prompt main continue |
| 406 | */ |
| 407 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 408 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 409 | |
| 410 | /* |
| 411 | ** Render output like fprintf(). Except, if the output is going to the |
| 412 | ** console and if this is running on a Windows machine, translate the |
| 413 | ** output from UTF-8 into MBCS. |
| 414 | */ |
| 415 | #if defined(_WIN32) || defined(WIN32) |
| 416 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 417 | va_list ap; |
| 418 | va_start(ap, zFormat); |
| 419 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
| 420 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
| 421 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
| 422 | sqlite3_free(z1); |
| 423 | fputs(z2, out); |
| 424 | sqlite3_free(z2); |
| 425 | }else{ |
| 426 | vfprintf(out, zFormat, ap); |
| 427 | } |
| 428 | va_end(ap); |
| 429 | } |
| 430 | #elif !defined(utf8_printf) |
| 431 | # define utf8_printf fprintf |
| 432 | #endif |
| 433 | |
| 434 | /* |
| 435 | ** Render output like fprintf(). This should not be used on anything that |
| 436 | ** includes string formatting (e.g. "%s"). |
| 437 | */ |
| 438 | #if !defined(raw_printf) |
| 439 | # define raw_printf fprintf |
| 440 | #endif |
| 441 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 442 | /* Indicate out-of-memory and exit. */ |
| 443 | static void shell_out_of_memory(void){ |
| 444 | raw_printf(stderr,"Error: out of memory\n"); |
| 445 | exit(1); |
| 446 | } |
| 447 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 448 | /* |
| 449 | ** Write I/O traces to the following stream. |
| 450 | */ |
| 451 | #ifdef SQLITE_ENABLE_IOTRACE |
| 452 | static FILE *iotrace = 0; |
| 453 | #endif |
| 454 | |
| 455 | /* |
| 456 | ** This routine works like printf in that its first argument is a |
| 457 | ** format string and subsequent arguments are values to be substituted |
| 458 | ** in place of % fields. The result of formatting this string |
| 459 | ** is written to iotrace. |
| 460 | */ |
| 461 | #ifdef SQLITE_ENABLE_IOTRACE |
| 462 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
| 463 | va_list ap; |
| 464 | char *z; |
| 465 | if( iotrace==0 ) return; |
| 466 | va_start(ap, zFormat); |
| 467 | z = sqlite3_vmprintf(zFormat, ap); |
| 468 | va_end(ap); |
| 469 | utf8_printf(iotrace, "%s", z); |
| 470 | sqlite3_free(z); |
| 471 | } |
| 472 | #endif |
| 473 | |
| 474 | /* |
| 475 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 476 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 477 | ** in bytes. This is different from the %*.*s specification in printf |
| 478 | ** since with %*.*s the width is measured in bytes, not characters. |
| 479 | */ |
| 480 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 481 | int i; |
| 482 | int n; |
| 483 | int aw = w<0 ? -w : w; |
| 484 | char zBuf[1000]; |
| 485 | if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; |
| 486 | for(i=n=0; zUtf[i]; i++){ |
| 487 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 488 | n++; |
| 489 | if( n==aw ){ |
| 490 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | if( n>=aw ){ |
| 496 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 497 | }else if( w<0 ){ |
| 498 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 499 | }else{ |
| 500 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /* |
| 506 | ** Determines if a string is a number of not. |
| 507 | */ |
| 508 | static int isNumber(const char *z, int *realnum){ |
| 509 | if( *z=='-' || *z=='+' ) z++; |
| 510 | if( !IsDigit(*z) ){ |
| 511 | return 0; |
| 512 | } |
| 513 | z++; |
| 514 | if( realnum ) *realnum = 0; |
| 515 | while( IsDigit(*z) ){ z++; } |
| 516 | if( *z=='.' ){ |
| 517 | z++; |
| 518 | if( !IsDigit(*z) ) return 0; |
| 519 | while( IsDigit(*z) ){ z++; } |
| 520 | if( realnum ) *realnum = 1; |
| 521 | } |
| 522 | if( *z=='e' || *z=='E' ){ |
| 523 | z++; |
| 524 | if( *z=='+' || *z=='-' ) z++; |
| 525 | if( !IsDigit(*z) ) return 0; |
| 526 | while( IsDigit(*z) ){ z++; } |
| 527 | if( realnum ) *realnum = 1; |
| 528 | } |
| 529 | return *z==0; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | ** Compute a string length that is limited to what can be stored in |
| 534 | ** lower 30 bits of a 32-bit signed integer. |
| 535 | */ |
| 536 | static int strlen30(const char *z){ |
| 537 | const char *z2 = z; |
| 538 | while( *z2 ){ z2++; } |
| 539 | return 0x3fffffff & (int)(z2 - z); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 544 | ** count as a single character. |
| 545 | */ |
| 546 | static int strlenChar(const char *z){ |
| 547 | int n = 0; |
| 548 | while( *z ){ |
| 549 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 550 | } |
| 551 | return n; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | ** This routine reads a line of text from FILE in, stores |
| 556 | ** the text in memory obtained from malloc() and returns a pointer |
| 557 | ** to the text. NULL is returned at end of file, or if malloc() |
| 558 | ** fails. |
| 559 | ** |
| 560 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 561 | ** a previous call to this routine that may be reused. |
| 562 | */ |
| 563 | static char *local_getline(char *zLine, FILE *in){ |
| 564 | int nLine = zLine==0 ? 0 : 100; |
| 565 | int n = 0; |
| 566 | |
| 567 | while( 1 ){ |
| 568 | if( n+100>nLine ){ |
| 569 | nLine = nLine*2 + 100; |
| 570 | zLine = realloc(zLine, nLine); |
| 571 | if( zLine==0 ) return 0; |
| 572 | } |
| 573 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 574 | if( n==0 ){ |
| 575 | free(zLine); |
| 576 | return 0; |
| 577 | } |
| 578 | zLine[n] = 0; |
| 579 | break; |
| 580 | } |
| 581 | while( zLine[n] ) n++; |
| 582 | if( n>0 && zLine[n-1]=='\n' ){ |
| 583 | n--; |
| 584 | if( n>0 && zLine[n-1]=='\r' ) n--; |
| 585 | zLine[n] = 0; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | #if defined(_WIN32) || defined(WIN32) |
| 590 | /* For interactive input on Windows systems, translate the |
| 591 | ** multi-byte characterset characters into UTF-8. */ |
| 592 | if( stdin_is_interactive && in==stdin ){ |
| 593 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| 594 | if( zTrans ){ |
| 595 | int nTrans = strlen30(zTrans)+1; |
| 596 | if( nTrans>nLine ){ |
| 597 | zLine = realloc(zLine, nTrans); |
| 598 | if( zLine==0 ){ |
| 599 | sqlite3_free(zTrans); |
| 600 | return 0; |
| 601 | } |
| 602 | } |
| 603 | memcpy(zLine, zTrans, nTrans); |
| 604 | sqlite3_free(zTrans); |
| 605 | } |
| 606 | } |
| 607 | #endif /* defined(_WIN32) || defined(WIN32) */ |
| 608 | return zLine; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** Retrieve a single line of input text. |
| 613 | ** |
| 614 | ** If in==0 then read from standard input and prompt before each line. |
| 615 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 616 | ** If isContinuation is zero, then the main prompt should be used. |
| 617 | ** |
| 618 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 619 | ** routine that can be reused. |
| 620 | ** |
| 621 | ** The result is stored in space obtained from malloc() and must either |
| 622 | ** be freed by the caller or else passed back into this routine via the |
| 623 | ** zPrior argument for reuse. |
| 624 | */ |
| 625 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 626 | char *zPrompt; |
| 627 | char *zResult; |
| 628 | if( in!=0 ){ |
| 629 | zResult = local_getline(zPrior, in); |
| 630 | }else{ |
| 631 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
| 632 | #if SHELL_USE_LOCAL_GETLINE |
| 633 | printf("%s", zPrompt); |
| 634 | fflush(stdout); |
| 635 | zResult = local_getline(zPrior, stdin); |
| 636 | #else |
| 637 | free(zPrior); |
| 638 | zResult = shell_readline(zPrompt); |
| 639 | if( zResult && *zResult ) shell_add_history(zResult); |
| 640 | #endif |
| 641 | } |
| 642 | return zResult; |
| 643 | } |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 644 | |
| 645 | |
| 646 | /* |
| 647 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 648 | ** is not a hex digit. |
| 649 | */ |
| 650 | static int hexDigitValue(char c){ |
| 651 | if( c>='0' && c<='9' ) return c - '0'; |
| 652 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 653 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 654 | return -1; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 659 | */ |
| 660 | static sqlite3_int64 integerValue(const char *zArg){ |
| 661 | sqlite3_int64 v = 0; |
| 662 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 663 | { "KiB", 1024 }, |
| 664 | { "MiB", 1024*1024 }, |
| 665 | { "GiB", 1024*1024*1024 }, |
| 666 | { "KB", 1000 }, |
| 667 | { "MB", 1000000 }, |
| 668 | { "GB", 1000000000 }, |
| 669 | { "K", 1000 }, |
| 670 | { "M", 1000000 }, |
| 671 | { "G", 1000000000 }, |
| 672 | }; |
| 673 | int i; |
| 674 | int isNeg = 0; |
| 675 | if( zArg[0]=='-' ){ |
| 676 | isNeg = 1; |
| 677 | zArg++; |
| 678 | }else if( zArg[0]=='+' ){ |
| 679 | zArg++; |
| 680 | } |
| 681 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 682 | int x; |
| 683 | zArg += 2; |
| 684 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 685 | v = (v<<4) + x; |
| 686 | zArg++; |
| 687 | } |
| 688 | }else{ |
| 689 | while( IsDigit(zArg[0]) ){ |
| 690 | v = v*10 + zArg[0] - '0'; |
| 691 | zArg++; |
| 692 | } |
| 693 | } |
| 694 | for(i=0; i<ArraySize(aMult); i++){ |
| 695 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 696 | v *= aMult[i].iMult; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | return isNeg? -v : v; |
| 701 | } |
| 702 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 703 | /* |
| 704 | ** A variable length string to which one can append text. |
| 705 | */ |
| 706 | typedef struct ShellText ShellText; |
| 707 | struct ShellText { |
| 708 | char *z; |
| 709 | int n; |
| 710 | int nAlloc; |
| 711 | }; |
| 712 | |
| 713 | /* |
| 714 | ** Initialize and destroy a ShellText object |
| 715 | */ |
| 716 | static void initText(ShellText *p){ |
| 717 | memset(p, 0, sizeof(*p)); |
| 718 | } |
| 719 | static void freeText(ShellText *p){ |
| 720 | free(p->z); |
| 721 | initText(p); |
| 722 | } |
| 723 | |
| 724 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 725 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 726 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 727 | ** zIn, if it was not NULL, is freed. |
| 728 | ** |
| 729 | ** If the third argument, quote, is not '\0', then it is used as a |
| 730 | ** quote character for zAppend. |
| 731 | */ |
| 732 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 733 | int len; |
| 734 | int i; |
| 735 | int nAppend = strlen30(zAppend); |
| 736 | |
| 737 | len = nAppend+p->n+1; |
| 738 | if( quote ){ |
| 739 | len += 2; |
| 740 | for(i=0; i<nAppend; i++){ |
| 741 | if( zAppend[i]==quote ) len++; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | if( p->n+len>=p->nAlloc ){ |
| 746 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 747 | p->z = realloc(p->z, p->nAlloc); |
| 748 | if( p->z==0 ){ |
| 749 | memset(p, 0, sizeof(*p)); |
| 750 | return; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if( quote ){ |
| 755 | char *zCsr = p->z+p->n; |
| 756 | *zCsr++ = quote; |
| 757 | for(i=0; i<nAppend; i++){ |
| 758 | *zCsr++ = zAppend[i]; |
| 759 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 760 | } |
| 761 | *zCsr++ = quote; |
| 762 | p->n = (int)(zCsr - p->z); |
| 763 | *zCsr = '\0'; |
| 764 | }else{ |
| 765 | memcpy(p->z+p->n, zAppend, nAppend); |
| 766 | p->n += nAppend; |
| 767 | p->z[p->n] = '\0'; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 773 | ** because it contains non-alphanumeric characters, or because it is an |
| 774 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 775 | ** that quoting is required. |
| 776 | ** |
| 777 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 778 | */ |
| 779 | static char quoteChar(const char *zName){ |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 780 | int i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 781 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 782 | for(i=0; zName[i]; i++){ |
| 783 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 784 | } |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 785 | return sqlite3_keyword_check(zName, i) ? '"' : 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 789 | ** Construct a fake object name and column list to describe the structure |
| 790 | ** of the view, virtual table, or table valued function zSchema.zName. |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 791 | */ |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 792 | static char *shellFakeSchema( |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 793 | sqlite3 *db, /* The database connection containing the vtab */ |
| 794 | const char *zSchema, /* Schema of the database holding the vtab */ |
| 795 | const char *zName /* The name of the virtual table */ |
| 796 | ){ |
| 797 | sqlite3_stmt *pStmt = 0; |
| 798 | char *zSql; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 799 | ShellText s; |
| 800 | char cQuote; |
| 801 | char *zDiv = "("; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 802 | int nRow = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 803 | |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 804 | zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", |
| 805 | zSchema ? zSchema : "main", zName); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 806 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 807 | sqlite3_free(zSql); |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 808 | initText(&s); |
| 809 | if( zSchema ){ |
| 810 | cQuote = quoteChar(zSchema); |
| 811 | if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; |
| 812 | appendText(&s, zSchema, cQuote); |
| 813 | appendText(&s, ".", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 814 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 815 | cQuote = quoteChar(zName); |
| 816 | appendText(&s, zName, cQuote); |
| 817 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 818 | const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 819 | nRow++; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 820 | appendText(&s, zDiv, 0); |
| 821 | zDiv = ","; |
| 822 | cQuote = quoteChar(zCol); |
| 823 | appendText(&s, zCol, cQuote); |
| 824 | } |
| 825 | appendText(&s, ")", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 826 | sqlite3_finalize(pStmt); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 827 | if( nRow==0 ){ |
| 828 | freeText(&s); |
| 829 | s.z = 0; |
| 830 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 831 | return s.z; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 835 | ** SQL function: shell_module_schema(X) |
| 836 | ** |
| 837 | ** Return a fake schema for the table-valued function or eponymous virtual |
| 838 | ** table X. |
| 839 | */ |
| 840 | static void shellModuleSchema( |
| 841 | sqlite3_context *pCtx, |
| 842 | int nVal, |
| 843 | sqlite3_value **apVal |
| 844 | ){ |
| 845 | const char *zName = (const char*)sqlite3_value_text(apVal[0]); |
| 846 | char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 847 | UNUSED_PARAMETER(nVal); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 848 | if( zFake ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 849 | sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 850 | -1, sqlite3_free); |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 851 | free(zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 856 | ** SQL function: shell_add_schema(S,X) |
| 857 | ** |
| 858 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 859 | ** Examples: |
| 860 | ** |
| 861 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 862 | ** |
| 863 | ** Also works on |
| 864 | ** |
| 865 | ** CREATE INDEX |
| 866 | ** CREATE UNIQUE INDEX |
| 867 | ** CREATE VIEW |
| 868 | ** CREATE TRIGGER |
| 869 | ** CREATE VIRTUAL TABLE |
| 870 | ** |
| 871 | ** This UDF is used by the .schema command to insert the schema name of |
| 872 | ** attached databases into the middle of the sqlite_master.sql field. |
| 873 | */ |
| 874 | static void shellAddSchemaName( |
| 875 | sqlite3_context *pCtx, |
| 876 | int nVal, |
| 877 | sqlite3_value **apVal |
| 878 | ){ |
| 879 | static const char *aPrefix[] = { |
| 880 | "TABLE", |
| 881 | "INDEX", |
| 882 | "UNIQUE INDEX", |
| 883 | "VIEW", |
| 884 | "TRIGGER", |
| 885 | "VIRTUAL TABLE" |
| 886 | }; |
| 887 | int i = 0; |
| 888 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 889 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 890 | const char *zName = (const char*)sqlite3_value_text(apVal[2]); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 891 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 892 | UNUSED_PARAMETER(nVal); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 893 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 894 | for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 895 | int n = strlen30(aPrefix[i]); |
| 896 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 897 | char *z = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 898 | char *zFake = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 899 | if( zSchema ){ |
| 900 | char cQuote = quoteChar(zSchema); |
| 901 | if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ |
| 902 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 903 | }else{ |
| 904 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 905 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 906 | } |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 907 | if( zName |
| 908 | && aPrefix[i][0]=='V' |
| 909 | && (zFake = shellFakeSchema(db, zSchema, zName))!=0 |
| 910 | ){ |
| 911 | if( z==0 ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 912 | z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 913 | }else{ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 914 | z = sqlite3_mprintf("%z\n/* %s */", z, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 915 | } |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 916 | free(zFake); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 917 | } |
| 918 | if( z ){ |
| 919 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 920 | return; |
| 921 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | sqlite3_result_value(pCtx, apVal[0]); |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | ** The source code for several run-time loadable extensions is inserted |
| 930 | ** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 931 | ** code, we need to override some macros to make the included program code |
| 932 | ** work here in the middle of this regular program. |
| 933 | */ |
| 934 | #define SQLITE_EXTENSION_INIT1 |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 935 | #define SQLITE_EXTENSION_INIT2(X) (void)(X) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 936 | |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 937 | #if defined(_WIN32) && defined(_MSC_VER) |
drh | 03491a1 | 2018-01-07 21:58:17 +0000 | [diff] [blame] | 938 | INCLUDE test_windirent.h |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 939 | INCLUDE test_windirent.c |
| 940 | #define dirent DIRENT |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 941 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 942 | INCLUDE ../ext/misc/shathree.c |
| 943 | INCLUDE ../ext/misc/fileio.c |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 944 | INCLUDE ../ext/misc/completion.c |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 945 | INCLUDE ../ext/misc/appendvfs.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 946 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 947 | INCLUDE ../ext/misc/zipfile.c |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 948 | INCLUDE ../ext/misc/sqlar.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 949 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 950 | INCLUDE ../ext/expert/sqlite3expert.h |
| 951 | INCLUDE ../ext/expert/sqlite3expert.c |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 952 | |
| 953 | #if defined(SQLITE_ENABLE_SESSION) |
| 954 | /* |
| 955 | ** State information for a single open session |
| 956 | */ |
| 957 | typedef struct OpenSession OpenSession; |
| 958 | struct OpenSession { |
| 959 | char *zName; /* Symbolic name for this session */ |
| 960 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 961 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 962 | sqlite3_session *p; /* The open session */ |
| 963 | }; |
| 964 | #endif |
| 965 | |
| 966 | /* |
| 967 | ** Shell output mode information from before ".explain on", |
| 968 | ** saved so that it can be restored by ".explain off" |
| 969 | */ |
| 970 | typedef struct SavedModeInfo SavedModeInfo; |
| 971 | struct SavedModeInfo { |
| 972 | int valid; /* Is there legit data in here? */ |
| 973 | int mode; /* Mode prior to ".explain on" */ |
| 974 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 975 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
| 976 | }; |
| 977 | |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 978 | typedef struct ExpertInfo ExpertInfo; |
| 979 | struct ExpertInfo { |
| 980 | sqlite3expert *pExpert; |
| 981 | int bVerbose; |
| 982 | }; |
| 983 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 984 | /* A single line in the EQP output */ |
| 985 | typedef struct EQPGraphRow EQPGraphRow; |
| 986 | struct EQPGraphRow { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 987 | int iEqpId; /* ID for this row */ |
| 988 | int iParentId; /* ID of the parent row */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 989 | EQPGraphRow *pNext; /* Next row in sequence */ |
| 990 | char zText[1]; /* Text to display for this row */ |
| 991 | }; |
| 992 | |
| 993 | /* All EQP output is collected into an instance of the following */ |
| 994 | typedef struct EQPGraph EQPGraph; |
| 995 | struct EQPGraph { |
| 996 | EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ |
| 997 | EQPGraphRow *pLast; /* Last element of the pRow list */ |
| 998 | char zPrefix[100]; /* Graph prefix */ |
| 999 | }; |
| 1000 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | ** State information about the database connection is contained in an |
| 1003 | ** instance of the following structure. |
| 1004 | */ |
| 1005 | typedef struct ShellState ShellState; |
| 1006 | struct ShellState { |
| 1007 | sqlite3 *db; /* The database */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1008 | u8 autoExplain; /* Automatically turn on .explain mode */ |
| 1009 | u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1010 | u8 autoEQPtest; /* autoEQP is in test mode */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1011 | u8 statsOn; /* True to display memory stats before each finalize */ |
| 1012 | u8 scanstatsOn; /* True to display scan stats before each finalize */ |
| 1013 | u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1014 | u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1015 | u8 nEqpLevel; /* Depth of the EQP output graph */ |
| 1016 | unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1017 | int outCount; /* Revert to stdout when reaching zero */ |
| 1018 | int cnt; /* Number of records displayed so far */ |
| 1019 | FILE *out; /* Write results here */ |
| 1020 | FILE *traceOut; /* Output for sqlite3_trace() */ |
| 1021 | int nErr; /* Number of errors seen */ |
| 1022 | int mode; /* An output mode setting */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1023 | int modePrior; /* Saved mode */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1024 | int cMode; /* temporary output mode for the current query */ |
| 1025 | int normalMode; /* Output mode before ".explain on" */ |
| 1026 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
| 1027 | int showHeader; /* True to show column names in List or Column mode */ |
| 1028 | int nCheck; /* Number of ".check" commands run */ |
| 1029 | unsigned shellFlgs; /* Various flags */ |
| 1030 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1031 | char *zTempFile; /* Temporary file that might need deleting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1032 | char zTestcase[30]; /* Name of current test case */ |
| 1033 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1034 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1035 | char colSepPrior[20]; /* Saved column separator */ |
| 1036 | char rowSepPrior[20]; /* Saved row separator */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1037 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1038 | int actualWidth[100]; /* Actual width of each column */ |
| 1039 | char nullValue[20]; /* The text to print when a NULL comes back from |
| 1040 | ** the database */ |
| 1041 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1042 | const char *zDbFilename; /* name of the database file */ |
| 1043 | char *zFreeOnClose; /* Filename to free when closing */ |
| 1044 | const char *zVfs; /* Name of VFS to use */ |
| 1045 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
| 1046 | FILE *pLog; /* Write log output here */ |
| 1047 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1048 | int nIndent; /* Size of array aiIndent[] */ |
| 1049 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1050 | EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 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 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1061 | #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ |
| 1062 | #define AUTOEQP_on 1 /* Automatic EQP is on */ |
| 1063 | #define AUTOEQP_trigger 2 /* On and also show plans for triggers */ |
| 1064 | #define AUTOEQP_full 3 /* Show full EXPLAIN */ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 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 */ |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 1072 | #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1073 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | ** These are the allowed shellFlgs values |
| 1076 | */ |
drh | b2a0f75 | 2017-08-28 15:51:35 +0000 | [diff] [blame] | 1077 | #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ |
| 1078 | #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ |
| 1079 | #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ |
| 1080 | #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ |
| 1081 | #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ |
| 1082 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1083 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | ** Macros for testing and setting shellFlgs |
| 1087 | */ |
| 1088 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1089 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1090 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
| 1091 | |
| 1092 | /* |
| 1093 | ** These are the allowed modes. |
| 1094 | */ |
| 1095 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
| 1096 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1097 | #define MODE_List 2 /* One record per line with a separator */ |
| 1098 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1099 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1100 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
| 1101 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1102 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1103 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1104 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1105 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1106 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1107 | #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1108 | |
| 1109 | static const char *modeDescr[] = { |
| 1110 | "line", |
| 1111 | "column", |
| 1112 | "list", |
| 1113 | "semi", |
| 1114 | "html", |
| 1115 | "insert", |
| 1116 | "quote", |
| 1117 | "tcl", |
| 1118 | "csv", |
| 1119 | "explain", |
| 1120 | "ascii", |
| 1121 | "prettyprint", |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1122 | "eqp" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1123 | }; |
| 1124 | |
| 1125 | /* |
| 1126 | ** These are the column/row/line separators used by the various |
| 1127 | ** import/export modes. |
| 1128 | */ |
| 1129 | #define SEP_Column "|" |
| 1130 | #define SEP_Row "\n" |
| 1131 | #define SEP_Tab "\t" |
| 1132 | #define SEP_Space " " |
| 1133 | #define SEP_Comma "," |
| 1134 | #define SEP_CrLf "\r\n" |
| 1135 | #define SEP_Unit "\x1F" |
| 1136 | #define SEP_Record "\x1E" |
| 1137 | |
| 1138 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1139 | ** A callback for the sqlite3_log() interface. |
| 1140 | */ |
| 1141 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 1142 | ShellState *p = (ShellState*)pArg; |
| 1143 | if( p->pLog==0 ) return; |
| 1144 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 1145 | fflush(p->pLog); |
| 1146 | } |
| 1147 | |
| 1148 | /* |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1149 | ** SQL function: shell_putsnl(X) |
| 1150 | ** |
| 1151 | ** Write the text X to the screen (or whatever output is being directed) |
| 1152 | ** adding a newline at the end, and then return X. |
| 1153 | */ |
| 1154 | static void shellPutsFunc( |
| 1155 | sqlite3_context *pCtx, |
| 1156 | int nVal, |
| 1157 | sqlite3_value **apVal |
| 1158 | ){ |
| 1159 | ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 1160 | (void)nVal; |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1161 | utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
| 1162 | sqlite3_result_value(pCtx, apVal[0]); |
| 1163 | } |
| 1164 | |
| 1165 | /* |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1166 | ** SQL function: edit(VALUE) |
| 1167 | ** edit(VALUE,EDITOR) |
| 1168 | ** |
| 1169 | ** These steps: |
| 1170 | ** |
| 1171 | ** (1) Write VALUE into a temporary file. |
| 1172 | ** (2) Run program EDITOR on that temporary file. |
| 1173 | ** (3) Read the temporary file back and return its content as the result. |
| 1174 | ** (4) Delete the temporary file |
| 1175 | ** |
| 1176 | ** If the EDITOR argument is omitted, use the value in the VISUAL |
| 1177 | ** environment variable. If still there is no EDITOR, through an error. |
| 1178 | ** |
| 1179 | ** Also throw an error if the EDITOR program returns a non-zero exit code. |
| 1180 | */ |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1181 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1182 | static void editFunc( |
| 1183 | sqlite3_context *context, |
| 1184 | int argc, |
| 1185 | sqlite3_value **argv |
| 1186 | ){ |
| 1187 | const char *zEditor; |
| 1188 | char *zTempFile = 0; |
| 1189 | sqlite3 *db; |
| 1190 | char *zCmd = 0; |
| 1191 | int bBin; |
| 1192 | int rc; |
| 1193 | FILE *f = 0; |
| 1194 | sqlite3_int64 sz; |
| 1195 | sqlite3_int64 x; |
| 1196 | unsigned char *p = 0; |
| 1197 | |
| 1198 | if( argc==2 ){ |
| 1199 | zEditor = (const char*)sqlite3_value_text(argv[1]); |
| 1200 | }else{ |
| 1201 | zEditor = getenv("VISUAL"); |
| 1202 | } |
| 1203 | if( zEditor==0 ){ |
| 1204 | sqlite3_result_error(context, "no editor for edit()", -1); |
| 1205 | return; |
| 1206 | } |
| 1207 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1208 | sqlite3_result_error(context, "NULL input to edit()", -1); |
| 1209 | return; |
| 1210 | } |
| 1211 | db = sqlite3_context_db_handle(context); |
| 1212 | zTempFile = 0; |
| 1213 | sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); |
| 1214 | if( zTempFile==0 ){ |
| 1215 | sqlite3_uint64 r = 0; |
| 1216 | sqlite3_randomness(sizeof(r), &r); |
| 1217 | zTempFile = sqlite3_mprintf("temp%llx", r); |
| 1218 | if( zTempFile==0 ){ |
| 1219 | sqlite3_result_error_nomem(context); |
| 1220 | return; |
| 1221 | } |
| 1222 | } |
| 1223 | bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
| 1224 | f = fopen(zTempFile, bBin ? "wb" : "w"); |
| 1225 | if( f==0 ){ |
| 1226 | sqlite3_result_error(context, "edit() cannot open temp file", -1); |
| 1227 | goto edit_func_end; |
| 1228 | } |
| 1229 | sz = sqlite3_value_bytes(argv[0]); |
| 1230 | if( bBin ){ |
| 1231 | x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f); |
| 1232 | }else{ |
| 1233 | x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f); |
| 1234 | } |
| 1235 | fclose(f); |
| 1236 | f = 0; |
| 1237 | if( x!=sz ){ |
| 1238 | sqlite3_result_error(context, "edit() could not write the whole file", -1); |
| 1239 | goto edit_func_end; |
| 1240 | } |
| 1241 | zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); |
| 1242 | if( zCmd==0 ){ |
| 1243 | sqlite3_result_error_nomem(context); |
| 1244 | goto edit_func_end; |
| 1245 | } |
| 1246 | rc = system(zCmd); |
| 1247 | sqlite3_free(zCmd); |
| 1248 | if( rc ){ |
| 1249 | sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
| 1250 | goto edit_func_end; |
| 1251 | } |
| 1252 | f = fopen(zTempFile, bBin ? "rb" : "r"); |
| 1253 | if( f==0 ){ |
| 1254 | sqlite3_result_error(context, |
| 1255 | "edit() cannot reopen temp file after edit", -1); |
| 1256 | goto edit_func_end; |
| 1257 | } |
| 1258 | fseek(f, 0, SEEK_END); |
| 1259 | sz = ftell(f); |
| 1260 | rewind(f); |
| 1261 | p = sqlite3_malloc64( sz+(bBin==0) ); |
| 1262 | if( p==0 ){ |
| 1263 | sqlite3_result_error_nomem(context); |
| 1264 | goto edit_func_end; |
| 1265 | } |
| 1266 | if( bBin ){ |
| 1267 | x = fread(p, 1, sz, f); |
| 1268 | }else{ |
| 1269 | x = fread(p, 1, sz, f); |
| 1270 | p[sz] = 0; |
| 1271 | } |
| 1272 | fclose(f); |
| 1273 | f = 0; |
| 1274 | if( x!=sz ){ |
| 1275 | sqlite3_result_error(context, "could not read back the whole file", -1); |
| 1276 | goto edit_func_end; |
| 1277 | } |
| 1278 | if( bBin ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1279 | sqlite3_result_blob64(context, p, sz, sqlite3_free); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1280 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1281 | sqlite3_result_text64(context, (const char*)p, sz, |
| 1282 | sqlite3_free, SQLITE_UTF8); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1283 | } |
| 1284 | p = 0; |
| 1285 | |
| 1286 | edit_func_end: |
| 1287 | if( f ) fclose(f); |
| 1288 | unlink(zTempFile); |
| 1289 | sqlite3_free(zTempFile); |
| 1290 | sqlite3_free(p); |
| 1291 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1292 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1295 | ** Save or restore the current output mode |
| 1296 | */ |
| 1297 | static void outputModePush(ShellState *p){ |
| 1298 | p->modePrior = p->mode; |
| 1299 | memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); |
| 1300 | memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); |
| 1301 | } |
| 1302 | static void outputModePop(ShellState *p){ |
| 1303 | p->mode = p->modePrior; |
| 1304 | memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
| 1305 | memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
| 1306 | } |
| 1307 | |
| 1308 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1309 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1310 | */ |
| 1311 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1312 | int i; |
| 1313 | char *zBlob = (char *)pBlob; |
| 1314 | raw_printf(out,"X'"); |
| 1315 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1316 | raw_printf(out,"'"); |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1321 | ** to that string. |
| 1322 | ** |
| 1323 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1324 | ** then make up some string and store it in the buffer zBuf. |
| 1325 | */ |
| 1326 | static const char *unused_string( |
| 1327 | const char *z, /* Result must not appear anywhere in z */ |
| 1328 | const char *zA, const char *zB, /* Try these first */ |
| 1329 | char *zBuf /* Space to store a generated string */ |
| 1330 | ){ |
| 1331 | unsigned i = 0; |
| 1332 | if( strstr(z, zA)==0 ) return zA; |
| 1333 | if( strstr(z, zB)==0 ) return zB; |
| 1334 | do{ |
| 1335 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1336 | }while( strstr(z,zBuf)!=0 ); |
| 1337 | return zBuf; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1342 | ** |
| 1343 | ** See also: output_quoted_escaped_string() |
| 1344 | */ |
| 1345 | static void output_quoted_string(FILE *out, const char *z){ |
| 1346 | int i; |
| 1347 | char c; |
| 1348 | setBinaryMode(out, 1); |
| 1349 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1350 | if( c==0 ){ |
| 1351 | utf8_printf(out,"'%s'",z); |
| 1352 | }else{ |
| 1353 | raw_printf(out, "'"); |
| 1354 | while( *z ){ |
| 1355 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1356 | if( c=='\'' ) i++; |
| 1357 | if( i ){ |
| 1358 | utf8_printf(out, "%.*s", i, z); |
| 1359 | z += i; |
| 1360 | } |
| 1361 | if( c=='\'' ){ |
| 1362 | raw_printf(out, "'"); |
| 1363 | continue; |
| 1364 | } |
| 1365 | if( c==0 ){ |
| 1366 | break; |
| 1367 | } |
| 1368 | z++; |
| 1369 | } |
| 1370 | raw_printf(out, "'"); |
| 1371 | } |
| 1372 | setTextMode(out, 1); |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1377 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1378 | ** get corrupted by end-of-line translation facilities in some operating |
| 1379 | ** systems. |
| 1380 | ** |
| 1381 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1382 | ** escape mechanism. |
| 1383 | */ |
| 1384 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1385 | int i; |
| 1386 | char c; |
| 1387 | setBinaryMode(out, 1); |
| 1388 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1389 | if( c==0 ){ |
| 1390 | utf8_printf(out,"'%s'",z); |
| 1391 | }else{ |
| 1392 | const char *zNL = 0; |
| 1393 | const char *zCR = 0; |
| 1394 | int nNL = 0; |
| 1395 | int nCR = 0; |
| 1396 | char zBuf1[20], zBuf2[20]; |
| 1397 | for(i=0; z[i]; i++){ |
| 1398 | if( z[i]=='\n' ) nNL++; |
| 1399 | if( z[i]=='\r' ) nCR++; |
| 1400 | } |
| 1401 | if( nNL ){ |
| 1402 | raw_printf(out, "replace("); |
| 1403 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1404 | } |
| 1405 | if( nCR ){ |
| 1406 | raw_printf(out, "replace("); |
| 1407 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1408 | } |
| 1409 | raw_printf(out, "'"); |
| 1410 | while( *z ){ |
| 1411 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1412 | if( c=='\'' ) i++; |
| 1413 | if( i ){ |
| 1414 | utf8_printf(out, "%.*s", i, z); |
| 1415 | z += i; |
| 1416 | } |
| 1417 | if( c=='\'' ){ |
| 1418 | raw_printf(out, "'"); |
| 1419 | continue; |
| 1420 | } |
| 1421 | if( c==0 ){ |
| 1422 | break; |
| 1423 | } |
| 1424 | z++; |
| 1425 | if( c=='\n' ){ |
| 1426 | raw_printf(out, "%s", zNL); |
| 1427 | continue; |
| 1428 | } |
| 1429 | raw_printf(out, "%s", zCR); |
| 1430 | } |
| 1431 | raw_printf(out, "'"); |
| 1432 | if( nCR ){ |
| 1433 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1434 | } |
| 1435 | if( nNL ){ |
| 1436 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1437 | } |
| 1438 | } |
| 1439 | setTextMode(out, 1); |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1444 | */ |
| 1445 | static void output_c_string(FILE *out, const char *z){ |
| 1446 | unsigned int c; |
| 1447 | fputc('"', out); |
| 1448 | while( (c = *(z++))!=0 ){ |
| 1449 | if( c=='\\' ){ |
| 1450 | fputc(c, out); |
| 1451 | fputc(c, out); |
| 1452 | }else if( c=='"' ){ |
| 1453 | fputc('\\', out); |
| 1454 | fputc('"', out); |
| 1455 | }else if( c=='\t' ){ |
| 1456 | fputc('\\', out); |
| 1457 | fputc('t', out); |
| 1458 | }else if( c=='\n' ){ |
| 1459 | fputc('\\', out); |
| 1460 | fputc('n', out); |
| 1461 | }else if( c=='\r' ){ |
| 1462 | fputc('\\', out); |
| 1463 | fputc('r', out); |
| 1464 | }else if( !isprint(c&0xff) ){ |
| 1465 | raw_printf(out, "\\%03o", c&0xff); |
| 1466 | }else{ |
| 1467 | fputc(c, out); |
| 1468 | } |
| 1469 | } |
| 1470 | fputc('"', out); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | ** Output the given string with characters that are special to |
| 1475 | ** HTML escaped. |
| 1476 | */ |
| 1477 | static void output_html_string(FILE *out, const char *z){ |
| 1478 | int i; |
| 1479 | if( z==0 ) z = ""; |
| 1480 | while( *z ){ |
| 1481 | for(i=0; z[i] |
| 1482 | && z[i]!='<' |
| 1483 | && z[i]!='&' |
| 1484 | && z[i]!='>' |
| 1485 | && z[i]!='\"' |
| 1486 | && z[i]!='\''; |
| 1487 | i++){} |
| 1488 | if( i>0 ){ |
| 1489 | utf8_printf(out,"%.*s",i,z); |
| 1490 | } |
| 1491 | if( z[i]=='<' ){ |
| 1492 | raw_printf(out,"<"); |
| 1493 | }else if( z[i]=='&' ){ |
| 1494 | raw_printf(out,"&"); |
| 1495 | }else if( z[i]=='>' ){ |
| 1496 | raw_printf(out,">"); |
| 1497 | }else if( z[i]=='\"' ){ |
| 1498 | raw_printf(out,"""); |
| 1499 | }else if( z[i]=='\'' ){ |
| 1500 | raw_printf(out,"'"); |
| 1501 | }else{ |
| 1502 | break; |
| 1503 | } |
| 1504 | z += i + 1; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /* |
| 1509 | ** If a field contains any character identified by a 1 in the following |
| 1510 | ** array, then the string must be quoted for CSV. |
| 1511 | */ |
| 1512 | static const char needCsvQuote[] = { |
| 1513 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1514 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1515 | 1, 0, 1, 0, 0, 0, 0, 1, 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, 0, |
| 1518 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1519 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1520 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1529 | }; |
| 1530 | |
| 1531 | /* |
| 1532 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
| 1533 | ** the separator, which may or may not be a comma. p->nullValue is |
| 1534 | ** the null value. Strings are quoted if necessary. The separator |
| 1535 | ** is only issued if bSep is true. |
| 1536 | */ |
| 1537 | static void output_csv(ShellState *p, const char *z, int bSep){ |
| 1538 | FILE *out = p->out; |
| 1539 | if( z==0 ){ |
| 1540 | utf8_printf(out,"%s",p->nullValue); |
| 1541 | }else{ |
| 1542 | int i; |
| 1543 | int nSep = strlen30(p->colSeparator); |
| 1544 | for(i=0; z[i]; i++){ |
| 1545 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1546 | || (z[i]==p->colSeparator[0] && |
| 1547 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
| 1548 | i = 0; |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | if( i==0 ){ |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1553 | char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
| 1554 | utf8_printf(out, "%s", zQuoted); |
| 1555 | sqlite3_free(zQuoted); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1556 | }else{ |
| 1557 | utf8_printf(out, "%s", z); |
| 1558 | } |
| 1559 | } |
| 1560 | if( bSep ){ |
| 1561 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1562 | } |
| 1563 | } |
| 1564 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1565 | /* |
| 1566 | ** This routine runs when the user presses Ctrl-C |
| 1567 | */ |
| 1568 | static void interrupt_handler(int NotUsed){ |
| 1569 | UNUSED_PARAMETER(NotUsed); |
| 1570 | seenInterrupt++; |
| 1571 | if( seenInterrupt>2 ) exit(1); |
| 1572 | if( globalDb ) sqlite3_interrupt(globalDb); |
| 1573 | } |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 1574 | |
| 1575 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 1576 | /* |
| 1577 | ** This routine runs for console events (e.g. Ctrl-C) on Win32 |
| 1578 | */ |
| 1579 | static BOOL WINAPI ConsoleCtrlHandler( |
| 1580 | DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ |
| 1581 | ){ |
| 1582 | if( dwCtrlType==CTRL_C_EVENT ){ |
| 1583 | interrupt_handler(0); |
| 1584 | return TRUE; |
| 1585 | } |
| 1586 | return FALSE; |
| 1587 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1588 | #endif |
| 1589 | |
| 1590 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1591 | /* |
| 1592 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1593 | ** invoked. It always returns SQLITE_OK. |
| 1594 | */ |
| 1595 | static int shellAuth( |
| 1596 | void *pClientData, |
| 1597 | int op, |
| 1598 | const char *zA1, |
| 1599 | const char *zA2, |
| 1600 | const char *zA3, |
| 1601 | const char *zA4 |
| 1602 | ){ |
| 1603 | ShellState *p = (ShellState*)pClientData; |
| 1604 | static const char *azAction[] = { 0, |
| 1605 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1606 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1607 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1608 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1609 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1610 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1611 | "PRAGMA", "READ", "SELECT", |
| 1612 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1613 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1614 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1615 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1616 | }; |
| 1617 | int i; |
| 1618 | const char *az[4]; |
| 1619 | az[0] = zA1; |
| 1620 | az[1] = zA2; |
| 1621 | az[2] = zA3; |
| 1622 | az[3] = zA4; |
| 1623 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
| 1624 | for(i=0; i<4; i++){ |
| 1625 | raw_printf(p->out, " "); |
| 1626 | if( az[i] ){ |
| 1627 | output_c_string(p->out, az[i]); |
| 1628 | }else{ |
| 1629 | raw_printf(p->out, "NULL"); |
| 1630 | } |
| 1631 | } |
| 1632 | raw_printf(p->out, "\n"); |
| 1633 | return SQLITE_OK; |
| 1634 | } |
| 1635 | #endif |
| 1636 | |
| 1637 | /* |
| 1638 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1639 | ** |
| 1640 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1641 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1642 | */ |
| 1643 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1644 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1645 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1646 | }else{ |
| 1647 | utf8_printf(out, "%s%s", z, zTail); |
| 1648 | } |
| 1649 | } |
| 1650 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1651 | char c = z[n]; |
| 1652 | z[n] = 0; |
| 1653 | printSchemaLine(out, z, zTail); |
| 1654 | z[n] = c; |
| 1655 | } |
| 1656 | |
| 1657 | /* |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1658 | ** Return true if string z[] has nothing but whitespace and comments to the |
| 1659 | ** end of the first line. |
| 1660 | */ |
| 1661 | static int wsToEol(const char *z){ |
| 1662 | int i; |
| 1663 | for(i=0; z[i]; i++){ |
| 1664 | if( z[i]=='\n' ) return 1; |
| 1665 | if( IsSpace(z[i]) ) continue; |
| 1666 | if( z[i]=='-' && z[i+1]=='-' ) return 1; |
| 1667 | return 0; |
| 1668 | } |
| 1669 | return 1; |
| 1670 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1671 | |
| 1672 | /* |
| 1673 | ** Add a new entry to the EXPLAIN QUERY PLAN data |
| 1674 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1675 | static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1676 | EQPGraphRow *pNew; |
| 1677 | int nText = strlen30(zText); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1678 | if( p->autoEQPtest ){ |
| 1679 | utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); |
| 1680 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1681 | pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
| 1682 | if( pNew==0 ) shell_out_of_memory(); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1683 | pNew->iEqpId = iEqpId; |
| 1684 | pNew->iParentId = p2; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1685 | memcpy(pNew->zText, zText, nText+1); |
| 1686 | pNew->pNext = 0; |
| 1687 | if( p->sGraph.pLast ){ |
| 1688 | p->sGraph.pLast->pNext = pNew; |
| 1689 | }else{ |
| 1690 | p->sGraph.pRow = pNew; |
| 1691 | } |
| 1692 | p->sGraph.pLast = pNew; |
| 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | ** Free and reset the EXPLAIN QUERY PLAN data that has been collected |
| 1697 | ** in p->sGraph. |
| 1698 | */ |
| 1699 | static void eqp_reset(ShellState *p){ |
| 1700 | EQPGraphRow *pRow, *pNext; |
| 1701 | for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ |
| 1702 | pNext = pRow->pNext; |
| 1703 | sqlite3_free(pRow); |
| 1704 | } |
| 1705 | memset(&p->sGraph, 0, sizeof(p->sGraph)); |
| 1706 | } |
| 1707 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1708 | /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1709 | ** pOld, or return the first such line if pOld is NULL |
| 1710 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1711 | static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1712 | EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1713 | while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1714 | return pRow; |
| 1715 | } |
| 1716 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1717 | /* Render a single level of the graph that has iEqpId as its parent. Called |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1718 | ** recursively to render sublevels. |
| 1719 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1720 | static void eqp_render_level(ShellState *p, int iEqpId){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1721 | EQPGraphRow *pRow, *pNext; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1722 | int n = strlen30(p->sGraph.zPrefix); |
| 1723 | char *z; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1724 | for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ |
| 1725 | pNext = eqp_next_row(p, iEqpId, pRow); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1726 | z = pRow->zText; |
| 1727 | utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z); |
drh | e2188f0 | 2018-05-07 11:37:34 +0000 | [diff] [blame] | 1728 | if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1729 | memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1730 | eqp_render_level(p, pRow->iEqpId); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1731 | p->sGraph.zPrefix[n] = 0; |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | /* |
| 1737 | ** Display and reset the EXPLAIN QUERY PLAN data |
| 1738 | */ |
| 1739 | static void eqp_render(ShellState *p){ |
| 1740 | EQPGraphRow *pRow = p->sGraph.pRow; |
| 1741 | if( pRow ){ |
| 1742 | if( pRow->zText[0]=='-' ){ |
| 1743 | if( pRow->pNext==0 ){ |
| 1744 | eqp_reset(p); |
| 1745 | return; |
| 1746 | } |
| 1747 | utf8_printf(p->out, "%s\n", pRow->zText+3); |
| 1748 | p->sGraph.pRow = pRow->pNext; |
| 1749 | sqlite3_free(pRow); |
| 1750 | }else{ |
| 1751 | utf8_printf(p->out, "QUERY PLAN\n"); |
| 1752 | } |
| 1753 | p->sGraph.zPrefix[0] = 0; |
| 1754 | eqp_render_level(p, 0); |
| 1755 | eqp_reset(p); |
| 1756 | } |
| 1757 | } |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1758 | |
| 1759 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1760 | ** This is the callback routine that the shell |
| 1761 | ** invokes for each row of a query result. |
| 1762 | */ |
| 1763 | static int shell_callback( |
| 1764 | void *pArg, |
| 1765 | int nArg, /* Number of result columns */ |
| 1766 | char **azArg, /* Text of each result column */ |
| 1767 | char **azCol, /* Column names */ |
| 1768 | int *aiType /* Column types */ |
| 1769 | ){ |
| 1770 | int i; |
| 1771 | ShellState *p = (ShellState*)pArg; |
| 1772 | |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 1773 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1774 | switch( p->cMode ){ |
| 1775 | case MODE_Line: { |
| 1776 | int w = 5; |
| 1777 | if( azArg==0 ) break; |
| 1778 | for(i=0; i<nArg; i++){ |
| 1779 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 1780 | if( len>w ) w = len; |
| 1781 | } |
| 1782 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
| 1783 | for(i=0; i<nArg; i++){ |
| 1784 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
| 1785 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 1786 | } |
| 1787 | break; |
| 1788 | } |
| 1789 | case MODE_Explain: |
| 1790 | case MODE_Column: { |
| 1791 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1792 | const int *colWidth; |
| 1793 | int showHdr; |
| 1794 | char *rowSep; |
| 1795 | if( p->cMode==MODE_Column ){ |
| 1796 | colWidth = p->colWidth; |
| 1797 | showHdr = p->showHeader; |
| 1798 | rowSep = p->rowSeparator; |
| 1799 | }else{ |
| 1800 | colWidth = aExplainWidths; |
| 1801 | showHdr = 1; |
| 1802 | rowSep = SEP_Row; |
| 1803 | } |
| 1804 | if( p->cnt++==0 ){ |
| 1805 | for(i=0; i<nArg; i++){ |
| 1806 | int w, n; |
| 1807 | if( i<ArraySize(p->colWidth) ){ |
| 1808 | w = colWidth[i]; |
| 1809 | }else{ |
| 1810 | w = 0; |
| 1811 | } |
| 1812 | if( w==0 ){ |
| 1813 | w = strlenChar(azCol[i] ? azCol[i] : ""); |
| 1814 | if( w<10 ) w = 10; |
| 1815 | n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); |
| 1816 | if( w<n ) w = n; |
| 1817 | } |
| 1818 | if( i<ArraySize(p->actualWidth) ){ |
| 1819 | p->actualWidth[i] = w; |
| 1820 | } |
| 1821 | if( showHdr ){ |
| 1822 | utf8_width_print(p->out, w, azCol[i]); |
| 1823 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
| 1824 | } |
| 1825 | } |
| 1826 | if( showHdr ){ |
| 1827 | for(i=0; i<nArg; i++){ |
| 1828 | int w; |
| 1829 | if( i<ArraySize(p->actualWidth) ){ |
| 1830 | w = p->actualWidth[i]; |
| 1831 | if( w<0 ) w = -w; |
| 1832 | }else{ |
| 1833 | w = 10; |
| 1834 | } |
| 1835 | utf8_printf(p->out,"%-*.*s%s",w,w, |
| 1836 | "----------------------------------------------------------" |
| 1837 | "----------------------------------------------------------", |
| 1838 | i==nArg-1 ? rowSep : " "); |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | if( azArg==0 ) break; |
| 1843 | for(i=0; i<nArg; i++){ |
| 1844 | int w; |
| 1845 | if( i<ArraySize(p->actualWidth) ){ |
| 1846 | w = p->actualWidth[i]; |
| 1847 | }else{ |
| 1848 | w = 10; |
| 1849 | } |
| 1850 | if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ |
| 1851 | w = strlenChar(azArg[i]); |
| 1852 | } |
| 1853 | if( i==1 && p->aiIndent && p->pStmt ){ |
| 1854 | if( p->iIndent<p->nIndent ){ |
| 1855 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
| 1856 | } |
| 1857 | p->iIndent++; |
| 1858 | } |
| 1859 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 1860 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
| 1861 | } |
| 1862 | break; |
| 1863 | } |
| 1864 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 1865 | printSchemaLine(p->out, azArg[0], ";\n"); |
| 1866 | break; |
| 1867 | } |
| 1868 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1869 | char *z; |
| 1870 | int j; |
| 1871 | int nParen = 0; |
| 1872 | char cEnd = 0; |
| 1873 | char c; |
| 1874 | int nLine = 0; |
| 1875 | assert( nArg==1 ); |
| 1876 | if( azArg[0]==0 ) break; |
| 1877 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1878 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1879 | ){ |
| 1880 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1881 | break; |
| 1882 | } |
| 1883 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1884 | j = 0; |
| 1885 | for(i=0; IsSpace(z[i]); i++){} |
| 1886 | for(; (c = z[i])!=0; i++){ |
| 1887 | if( IsSpace(c) ){ |
drh | c3cbd67 | 2017-10-05 19:12:10 +0000 | [diff] [blame] | 1888 | if( z[j-1]=='\r' ) z[j-1] = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1889 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1890 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1891 | j--; |
| 1892 | } |
| 1893 | z[j++] = c; |
| 1894 | } |
| 1895 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1896 | z[j] = 0; |
| 1897 | if( strlen30(z)>=79 ){ |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1898 | 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] | 1899 | if( c==cEnd ){ |
| 1900 | cEnd = 0; |
| 1901 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1902 | cEnd = c; |
| 1903 | }else if( c=='[' ){ |
| 1904 | cEnd = ']'; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1905 | }else if( c=='-' && z[i+1]=='-' ){ |
| 1906 | cEnd = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1907 | }else if( c=='(' ){ |
| 1908 | nParen++; |
| 1909 | }else if( c==')' ){ |
| 1910 | nParen--; |
| 1911 | if( nLine>0 && nParen==0 && j>0 ){ |
| 1912 | printSchemaLineN(p->out, z, j, "\n"); |
| 1913 | j = 0; |
| 1914 | } |
| 1915 | } |
| 1916 | z[j++] = c; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1917 | if( nParen==1 && cEnd==0 |
| 1918 | && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
| 1919 | ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1920 | if( c=='\n' ) j--; |
| 1921 | printSchemaLineN(p->out, z, j, "\n "); |
| 1922 | j = 0; |
| 1923 | nLine++; |
| 1924 | while( IsSpace(z[i+1]) ){ i++; } |
| 1925 | } |
| 1926 | } |
| 1927 | z[j] = 0; |
| 1928 | } |
| 1929 | printSchemaLine(p->out, z, ";\n"); |
| 1930 | sqlite3_free(z); |
| 1931 | break; |
| 1932 | } |
| 1933 | case MODE_List: { |
| 1934 | if( p->cnt++==0 && p->showHeader ){ |
| 1935 | for(i=0; i<nArg; i++){ |
| 1936 | utf8_printf(p->out,"%s%s",azCol[i], |
| 1937 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 1938 | } |
| 1939 | } |
| 1940 | if( azArg==0 ) break; |
| 1941 | for(i=0; i<nArg; i++){ |
| 1942 | char *z = azArg[i]; |
| 1943 | if( z==0 ) z = p->nullValue; |
| 1944 | utf8_printf(p->out, "%s", z); |
| 1945 | if( i<nArg-1 ){ |
| 1946 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1947 | }else{ |
| 1948 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1949 | } |
| 1950 | } |
| 1951 | break; |
| 1952 | } |
| 1953 | case MODE_Html: { |
| 1954 | if( p->cnt++==0 && p->showHeader ){ |
| 1955 | raw_printf(p->out,"<TR>"); |
| 1956 | for(i=0; i<nArg; i++){ |
| 1957 | raw_printf(p->out,"<TH>"); |
| 1958 | output_html_string(p->out, azCol[i]); |
| 1959 | raw_printf(p->out,"</TH>\n"); |
| 1960 | } |
| 1961 | raw_printf(p->out,"</TR>\n"); |
| 1962 | } |
| 1963 | if( azArg==0 ) break; |
| 1964 | raw_printf(p->out,"<TR>"); |
| 1965 | for(i=0; i<nArg; i++){ |
| 1966 | raw_printf(p->out,"<TD>"); |
| 1967 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1968 | raw_printf(p->out,"</TD>\n"); |
| 1969 | } |
| 1970 | raw_printf(p->out,"</TR>\n"); |
| 1971 | break; |
| 1972 | } |
| 1973 | case MODE_Tcl: { |
| 1974 | if( p->cnt++==0 && p->showHeader ){ |
| 1975 | for(i=0; i<nArg; i++){ |
| 1976 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
| 1977 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1978 | } |
| 1979 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1980 | } |
| 1981 | if( azArg==0 ) break; |
| 1982 | for(i=0; i<nArg; i++){ |
| 1983 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 1984 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 1985 | } |
| 1986 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1987 | break; |
| 1988 | } |
| 1989 | case MODE_Csv: { |
| 1990 | setBinaryMode(p->out, 1); |
| 1991 | if( p->cnt++==0 && p->showHeader ){ |
| 1992 | for(i=0; i<nArg; i++){ |
| 1993 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 1994 | } |
| 1995 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 1996 | } |
| 1997 | if( nArg>0 ){ |
| 1998 | for(i=0; i<nArg; i++){ |
| 1999 | output_csv(p, azArg[i], i<nArg-1); |
| 2000 | } |
| 2001 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2002 | } |
| 2003 | setTextMode(p->out, 1); |
| 2004 | break; |
| 2005 | } |
| 2006 | case MODE_Insert: { |
| 2007 | if( azArg==0 ) break; |
| 2008 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2009 | if( p->showHeader ){ |
| 2010 | raw_printf(p->out,"("); |
| 2011 | for(i=0; i<nArg; i++){ |
| 2012 | if( i>0 ) raw_printf(p->out, ","); |
| 2013 | if( quoteChar(azCol[i]) ){ |
| 2014 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 2015 | utf8_printf(p->out, "%s", z); |
| 2016 | sqlite3_free(z); |
| 2017 | }else{ |
| 2018 | raw_printf(p->out, "%s", azCol[i]); |
| 2019 | } |
| 2020 | } |
| 2021 | raw_printf(p->out,")"); |
| 2022 | } |
| 2023 | p->cnt++; |
| 2024 | for(i=0; i<nArg; i++){ |
| 2025 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2026 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2027 | utf8_printf(p->out,"NULL"); |
| 2028 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2029 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2030 | output_quoted_string(p->out, azArg[i]); |
| 2031 | }else{ |
| 2032 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2033 | } |
| 2034 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2035 | utf8_printf(p->out,"%s", azArg[i]); |
| 2036 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2037 | char z[50]; |
| 2038 | double r = sqlite3_column_double(p->pStmt, i); |
drh | 2f1f880 | 2018-06-13 17:19:20 +0000 | [diff] [blame] | 2039 | sqlite3_uint64 ur; |
| 2040 | memcpy(&ur,&r,sizeof(r)); |
| 2041 | if( ur==0x7ff0000000000000LL ){ |
| 2042 | raw_printf(p->out, "1e999"); |
| 2043 | }else if( ur==0xfff0000000000000LL ){ |
| 2044 | raw_printf(p->out, "-1e999"); |
| 2045 | }else{ |
| 2046 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2047 | raw_printf(p->out, "%s", z); |
| 2048 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2049 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2050 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2051 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2052 | output_hex_blob(p->out, pBlob, nBlob); |
| 2053 | }else if( isNumber(azArg[i], 0) ){ |
| 2054 | utf8_printf(p->out,"%s", azArg[i]); |
| 2055 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2056 | output_quoted_string(p->out, azArg[i]); |
| 2057 | }else{ |
| 2058 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2059 | } |
| 2060 | } |
| 2061 | raw_printf(p->out,");\n"); |
| 2062 | break; |
| 2063 | } |
| 2064 | case MODE_Quote: { |
| 2065 | if( azArg==0 ) break; |
| 2066 | if( p->cnt==0 && p->showHeader ){ |
| 2067 | for(i=0; i<nArg; i++){ |
| 2068 | if( i>0 ) raw_printf(p->out, ","); |
| 2069 | output_quoted_string(p->out, azCol[i]); |
| 2070 | } |
| 2071 | raw_printf(p->out,"\n"); |
| 2072 | } |
| 2073 | p->cnt++; |
| 2074 | for(i=0; i<nArg; i++){ |
| 2075 | if( i>0 ) raw_printf(p->out, ","); |
| 2076 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2077 | utf8_printf(p->out,"NULL"); |
| 2078 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2079 | output_quoted_string(p->out, azArg[i]); |
| 2080 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2081 | utf8_printf(p->out,"%s", azArg[i]); |
| 2082 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2083 | char z[50]; |
| 2084 | double r = sqlite3_column_double(p->pStmt, i); |
| 2085 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2086 | raw_printf(p->out, "%s", z); |
| 2087 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2088 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2089 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2090 | output_hex_blob(p->out, pBlob, nBlob); |
| 2091 | }else if( isNumber(azArg[i], 0) ){ |
| 2092 | utf8_printf(p->out,"%s", azArg[i]); |
| 2093 | }else{ |
| 2094 | output_quoted_string(p->out, azArg[i]); |
| 2095 | } |
| 2096 | } |
| 2097 | raw_printf(p->out,"\n"); |
| 2098 | break; |
| 2099 | } |
| 2100 | case MODE_Ascii: { |
| 2101 | if( p->cnt++==0 && p->showHeader ){ |
| 2102 | for(i=0; i<nArg; i++){ |
| 2103 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2104 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
| 2105 | } |
| 2106 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2107 | } |
| 2108 | if( azArg==0 ) break; |
| 2109 | for(i=0; i<nArg; i++){ |
| 2110 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2111 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
| 2112 | } |
| 2113 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2114 | break; |
| 2115 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2116 | case MODE_EQP: { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2117 | eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2118 | break; |
| 2119 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2120 | } |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | ** This is the callback routine that the SQLite library |
| 2126 | ** invokes for each row of a query result. |
| 2127 | */ |
| 2128 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2129 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2130 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2131 | } |
| 2132 | |
| 2133 | /* |
| 2134 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2135 | ** output onto the end of a ShellText object. |
| 2136 | */ |
| 2137 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2138 | ShellText *p = (ShellText*)pArg; |
| 2139 | int i; |
| 2140 | UNUSED_PARAMETER(az); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2141 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2142 | if( p->n ) appendText(p, "|", 0); |
| 2143 | for(i=0; i<nArg; i++){ |
| 2144 | if( i ) appendText(p, ",", 0); |
| 2145 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2146 | } |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | ** Generate an appropriate SELFTEST table in the main database. |
| 2152 | */ |
| 2153 | static void createSelftestTable(ShellState *p){ |
| 2154 | char *zErrMsg = 0; |
| 2155 | sqlite3_exec(p->db, |
| 2156 | "SAVEPOINT selftest_init;\n" |
| 2157 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
| 2158 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2159 | " op TEXT,\n" /* Operator: memo run */ |
| 2160 | " cmd TEXT,\n" /* Command text */ |
| 2161 | " ans TEXT\n" /* Desired answer */ |
| 2162 | ");" |
| 2163 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2164 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2165 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2166 | " 'memo','Tests generated by --init');\n" |
| 2167 | "INSERT INTO [_shell$self]\n" |
| 2168 | " SELECT 'run',\n" |
| 2169 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2170 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2171 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2172 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2173 | "INSERT INTO [_shell$self]\n" |
| 2174 | " SELECT 'run'," |
| 2175 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2176 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2177 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2178 | " FROM (\n" |
| 2179 | " SELECT name FROM sqlite_master\n" |
| 2180 | " WHERE type='table'\n" |
| 2181 | " AND name<>'selftest'\n" |
| 2182 | " AND coalesce(rootpage,0)>0\n" |
| 2183 | " )\n" |
| 2184 | " ORDER BY name;\n" |
| 2185 | "INSERT INTO [_shell$self]\n" |
| 2186 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
| 2187 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2188 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2189 | "DROP TABLE [_shell$self];" |
| 2190 | ,0,0,&zErrMsg); |
| 2191 | if( zErrMsg ){ |
| 2192 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2193 | sqlite3_free(zErrMsg); |
| 2194 | } |
| 2195 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
| 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | /* |
| 2200 | ** Set the destination table field of the ShellState structure to |
| 2201 | ** the name of the table given. Escape any quote characters in the |
| 2202 | ** table name. |
| 2203 | */ |
| 2204 | static void set_table_name(ShellState *p, const char *zName){ |
| 2205 | int i, n; |
mistachkin | 2158a0c | 2017-09-09 00:51:36 +0000 | [diff] [blame] | 2206 | char cQuote; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2207 | char *z; |
| 2208 | |
| 2209 | if( p->zDestTable ){ |
| 2210 | free(p->zDestTable); |
| 2211 | p->zDestTable = 0; |
| 2212 | } |
| 2213 | if( zName==0 ) return; |
| 2214 | cQuote = quoteChar(zName); |
| 2215 | n = strlen30(zName); |
| 2216 | if( cQuote ) n += n+2; |
| 2217 | z = p->zDestTable = malloc( n+1 ); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2218 | if( z==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2219 | n = 0; |
| 2220 | if( cQuote ) z[n++] = cQuote; |
| 2221 | for(i=0; zName[i]; i++){ |
| 2222 | z[n++] = zName[i]; |
| 2223 | if( zName[i]==cQuote ) z[n++] = cQuote; |
| 2224 | } |
| 2225 | if( cQuote ) z[n++] = cQuote; |
| 2226 | z[n] = 0; |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | /* |
| 2231 | ** Execute a query statement that will generate SQL output. Print |
| 2232 | ** the result columns, comma-separated, on a line and then add a |
| 2233 | ** semicolon terminator to the end of that line. |
| 2234 | ** |
| 2235 | ** If the number of columns is 1 and that column contains text "--" |
| 2236 | ** then write the semicolon on a separate line. That way, if a |
| 2237 | ** "--" comment occurs at the end of the statement, the comment |
| 2238 | ** won't consume the semicolon terminator. |
| 2239 | */ |
| 2240 | static int run_table_dump_query( |
| 2241 | ShellState *p, /* Query context */ |
| 2242 | const char *zSelect, /* SELECT statement to extract content */ |
| 2243 | const char *zFirstRow /* Print before first row, if not NULL */ |
| 2244 | ){ |
| 2245 | sqlite3_stmt *pSelect; |
| 2246 | int rc; |
| 2247 | int nResult; |
| 2248 | int i; |
| 2249 | const char *z; |
| 2250 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
| 2251 | if( rc!=SQLITE_OK || !pSelect ){ |
| 2252 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2253 | sqlite3_errmsg(p->db)); |
| 2254 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2255 | return rc; |
| 2256 | } |
| 2257 | rc = sqlite3_step(pSelect); |
| 2258 | nResult = sqlite3_column_count(pSelect); |
| 2259 | while( rc==SQLITE_ROW ){ |
| 2260 | if( zFirstRow ){ |
| 2261 | utf8_printf(p->out, "%s", zFirstRow); |
| 2262 | zFirstRow = 0; |
| 2263 | } |
| 2264 | z = (const char*)sqlite3_column_text(pSelect, 0); |
| 2265 | utf8_printf(p->out, "%s", z); |
| 2266 | for(i=1; i<nResult; i++){ |
| 2267 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
| 2268 | } |
| 2269 | if( z==0 ) z = ""; |
| 2270 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2271 | if( z[0] ){ |
| 2272 | raw_printf(p->out, "\n;\n"); |
| 2273 | }else{ |
| 2274 | raw_printf(p->out, ";\n"); |
| 2275 | } |
| 2276 | rc = sqlite3_step(pSelect); |
| 2277 | } |
| 2278 | rc = sqlite3_finalize(pSelect); |
| 2279 | if( rc!=SQLITE_OK ){ |
| 2280 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2281 | sqlite3_errmsg(p->db)); |
| 2282 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2283 | } |
| 2284 | return rc; |
| 2285 | } |
| 2286 | |
| 2287 | /* |
| 2288 | ** Allocate space and save off current error string. |
| 2289 | */ |
| 2290 | static char *save_err_msg( |
| 2291 | sqlite3 *db /* Database to query */ |
| 2292 | ){ |
| 2293 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
| 2294 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
| 2295 | if( zErrMsg ){ |
| 2296 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2297 | } |
| 2298 | return zErrMsg; |
| 2299 | } |
| 2300 | |
| 2301 | #ifdef __linux__ |
| 2302 | /* |
| 2303 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2304 | */ |
| 2305 | static void displayLinuxIoStats(FILE *out){ |
| 2306 | FILE *in; |
| 2307 | char z[200]; |
| 2308 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2309 | in = fopen(z, "rb"); |
| 2310 | if( in==0 ) return; |
| 2311 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2312 | static const struct { |
| 2313 | const char *zPattern; |
| 2314 | const char *zDesc; |
| 2315 | } aTrans[] = { |
| 2316 | { "rchar: ", "Bytes received by read():" }, |
| 2317 | { "wchar: ", "Bytes sent to write():" }, |
| 2318 | { "syscr: ", "Read() system calls:" }, |
| 2319 | { "syscw: ", "Write() system calls:" }, |
| 2320 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2321 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2322 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 2323 | }; |
| 2324 | int i; |
| 2325 | for(i=0; i<ArraySize(aTrans); i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 2326 | int n = strlen30(aTrans[i].zPattern); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2327 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
| 2328 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
| 2329 | break; |
| 2330 | } |
| 2331 | } |
| 2332 | } |
| 2333 | fclose(in); |
| 2334 | } |
| 2335 | #endif |
| 2336 | |
| 2337 | /* |
| 2338 | ** Display a single line of status using 64-bit values. |
| 2339 | */ |
| 2340 | static void displayStatLine( |
| 2341 | ShellState *p, /* The shell context */ |
| 2342 | char *zLabel, /* Label for this one line */ |
| 2343 | char *zFormat, /* Format for the result */ |
| 2344 | int iStatusCtrl, /* Which status to display */ |
| 2345 | int bReset /* True to reset the stats */ |
| 2346 | ){ |
| 2347 | sqlite3_int64 iCur = -1; |
| 2348 | sqlite3_int64 iHiwtr = -1; |
| 2349 | int i, nPercent; |
| 2350 | char zLine[200]; |
| 2351 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2352 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2353 | if( zFormat[i]=='%' ) nPercent++; |
| 2354 | } |
| 2355 | if( nPercent>1 ){ |
| 2356 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2357 | }else{ |
| 2358 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2359 | } |
| 2360 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2361 | } |
| 2362 | |
| 2363 | /* |
| 2364 | ** Display memory stats. |
| 2365 | */ |
| 2366 | static int display_stats( |
| 2367 | sqlite3 *db, /* Database to query */ |
| 2368 | ShellState *pArg, /* Pointer to ShellState */ |
| 2369 | int bReset /* True to reset the stats */ |
| 2370 | ){ |
| 2371 | int iCur; |
| 2372 | int iHiwtr; |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2373 | FILE *out; |
| 2374 | if( pArg==0 || pArg->out==0 ) return 0; |
| 2375 | out = pArg->out; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2376 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2377 | if( pArg->pStmt && (pArg->statsOn & 2) ){ |
| 2378 | int nCol, i, x; |
| 2379 | sqlite3_stmt *pStmt = pArg->pStmt; |
| 2380 | char z[100]; |
| 2381 | nCol = sqlite3_column_count(pStmt); |
| 2382 | raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); |
| 2383 | for(i=0; i<nCol; i++){ |
| 2384 | sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); |
| 2385 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2386 | #ifndef SQLITE_OMIT_DECLTYPE |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2387 | sqlite3_snprintf(30, z+x, "declared type:"); |
| 2388 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2389 | #endif |
| 2390 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2391 | sqlite3_snprintf(30, z+x, "database name:"); |
| 2392 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); |
| 2393 | sqlite3_snprintf(30, z+x, "table name:"); |
| 2394 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); |
| 2395 | sqlite3_snprintf(30, z+x, "origin name:"); |
| 2396 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2397 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2398 | } |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2399 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2400 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2401 | displayStatLine(pArg, "Memory Used:", |
| 2402 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2403 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2404 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
| 2405 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 2406 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2407 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
| 2408 | } |
| 2409 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2410 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
| 2411 | displayStatLine(pArg, "Largest Allocation:", |
| 2412 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2413 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2414 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2415 | #ifdef YYTRACKMAXSTACKDEPTH |
| 2416 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2417 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
| 2418 | #endif |
| 2419 | |
| 2420 | if( db ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2421 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2422 | iHiwtr = iCur = -1; |
| 2423 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2424 | &iCur, &iHiwtr, bReset); |
| 2425 | raw_printf(pArg->out, |
| 2426 | "Lookaside Slots Used: %d (max %d)\n", |
| 2427 | iCur, iHiwtr); |
| 2428 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2429 | &iCur, &iHiwtr, bReset); |
| 2430 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2431 | iHiwtr); |
| 2432 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2433 | &iCur, &iHiwtr, bReset); |
| 2434 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2435 | iHiwtr); |
| 2436 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2437 | &iCur, &iHiwtr, bReset); |
| 2438 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2439 | iHiwtr); |
| 2440 | } |
| 2441 | iHiwtr = iCur = -1; |
| 2442 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
| 2443 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2444 | iCur); |
| 2445 | iHiwtr = iCur = -1; |
| 2446 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
| 2447 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
| 2448 | iHiwtr = iCur = -1; |
| 2449 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
| 2450 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
| 2451 | iHiwtr = iCur = -1; |
| 2452 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
| 2453 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
| 2454 | iHiwtr = iCur = -1; |
drh | ffc78a4 | 2018-03-14 14:53:50 +0000 | [diff] [blame] | 2455 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); |
| 2456 | raw_printf(pArg->out, "Page cache spills: %d\n", iCur); |
| 2457 | iHiwtr = iCur = -1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2458 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
| 2459 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| 2460 | iCur); |
| 2461 | iHiwtr = iCur = -1; |
| 2462 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
| 2463 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| 2464 | iCur); |
| 2465 | } |
| 2466 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2467 | if( pArg->pStmt ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2468 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2469 | bReset); |
| 2470 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
| 2471 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
| 2472 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
| 2473 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
| 2474 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
| 2475 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2476 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2477 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset); |
| 2478 | raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); |
| 2479 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); |
| 2480 | raw_printf(pArg->out, "Number of times run: %d\n", iCur); |
| 2481 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); |
| 2482 | raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | #ifdef __linux__ |
| 2486 | displayLinuxIoStats(pArg->out); |
| 2487 | #endif |
| 2488 | |
| 2489 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2490 | |
| 2491 | return 0; |
| 2492 | } |
| 2493 | |
| 2494 | /* |
| 2495 | ** Display scan stats. |
| 2496 | */ |
| 2497 | static void display_scanstats( |
| 2498 | sqlite3 *db, /* Database to query */ |
| 2499 | ShellState *pArg /* Pointer to ShellState */ |
| 2500 | ){ |
| 2501 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2502 | UNUSED_PARAMETER(db); |
| 2503 | UNUSED_PARAMETER(pArg); |
| 2504 | #else |
| 2505 | int i, k, n, mx; |
| 2506 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
| 2507 | mx = 0; |
| 2508 | for(k=0; k<=mx; k++){ |
| 2509 | double rEstLoop = 1.0; |
| 2510 | for(i=n=0; 1; i++){ |
| 2511 | sqlite3_stmt *p = pArg->pStmt; |
| 2512 | sqlite3_int64 nLoop, nVisit; |
| 2513 | double rEst; |
| 2514 | int iSid; |
| 2515 | const char *zExplain; |
| 2516 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2517 | break; |
| 2518 | } |
| 2519 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| 2520 | if( iSid>mx ) mx = iSid; |
| 2521 | if( iSid!=k ) continue; |
| 2522 | if( n==0 ){ |
| 2523 | rEstLoop = (double)nLoop; |
| 2524 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| 2525 | } |
| 2526 | n++; |
| 2527 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2528 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2529 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| 2530 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| 2531 | rEstLoop *= rEst; |
| 2532 | raw_printf(pArg->out, |
| 2533 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| 2534 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| 2535 | ); |
| 2536 | } |
| 2537 | } |
| 2538 | raw_printf(pArg->out, "---------------------------\n"); |
| 2539 | #endif |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2544 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2545 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2546 | ** Otherwise, return zero. |
| 2547 | */ |
| 2548 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2549 | int i; |
| 2550 | for(i=0; azArray[i]; i++){ |
| 2551 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2552 | } |
| 2553 | return 0; |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| 2558 | ** and populate the ShellState.aiIndent[] array with the number of |
| 2559 | ** spaces each opcode should be indented before it is output. |
| 2560 | ** |
| 2561 | ** The indenting rules are: |
| 2562 | ** |
| 2563 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2564 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2565 | ** itself by 2 spaces. |
| 2566 | ** |
| 2567 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2568 | ** and ends on one of: |
| 2569 | ** Yield SeekGt SeekLt RowSetRead Rewind |
| 2570 | ** or if the P1 parameter is one instead of zero, |
| 2571 | ** then indent all opcodes between the earlier instruction |
| 2572 | ** and "Goto" by 2 spaces. |
| 2573 | */ |
| 2574 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| 2575 | const char *zSql; /* The text of the SQL statement */ |
| 2576 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2577 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2578 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| 2579 | int iOp; /* Index of operation in p->aiIndent[] */ |
| 2580 | |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 2581 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 }; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2582 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2583 | "Rewind", 0 }; |
| 2584 | const char *azGoto[] = { "Goto", 0 }; |
| 2585 | |
| 2586 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2587 | ** cannot be verified, return early. */ |
| 2588 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2589 | p->cMode = p->mode; |
| 2590 | return; |
| 2591 | } |
| 2592 | zSql = sqlite3_sql(pSql); |
| 2593 | if( zSql==0 ) return; |
| 2594 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| 2595 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2596 | p->cMode = p->mode; |
| 2597 | return; |
| 2598 | } |
| 2599 | |
| 2600 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2601 | int i; |
| 2602 | int iAddr = sqlite3_column_int(pSql, 0); |
| 2603 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| 2604 | |
| 2605 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2606 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2607 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2608 | ** the current instruction is part of a sub-program generated by an |
| 2609 | ** SQL trigger or foreign key. */ |
| 2610 | int p2 = sqlite3_column_int(pSql, 3); |
| 2611 | int p2op = (p2 + (iOp-iAddr)); |
| 2612 | |
| 2613 | /* Grow the p->aiIndent array as required */ |
| 2614 | if( iOp>=nAlloc ){ |
| 2615 | if( iOp==0 ){ |
| 2616 | /* Do further verfication that this is explain output. Abort if |
| 2617 | ** it is not */ |
| 2618 | static const char *explainCols[] = { |
| 2619 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2620 | int jj; |
| 2621 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2622 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2623 | p->cMode = p->mode; |
| 2624 | sqlite3_reset(pSql); |
| 2625 | return; |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | nAlloc += 100; |
| 2630 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2631 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
| 2632 | } |
| 2633 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2634 | p->aiIndent[iOp] = 0; |
| 2635 | p->nIndent = iOp+1; |
| 2636 | |
| 2637 | if( str_in_array(zOp, azNext) ){ |
| 2638 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 2639 | } |
| 2640 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2641 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2642 | ){ |
| 2643 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | p->iIndent = 0; |
| 2648 | sqlite3_free(abYield); |
| 2649 | sqlite3_reset(pSql); |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | ** Free the array allocated by explain_data_prepare(). |
| 2654 | */ |
| 2655 | static void explain_data_delete(ShellState *p){ |
| 2656 | sqlite3_free(p->aiIndent); |
| 2657 | p->aiIndent = 0; |
| 2658 | p->nIndent = 0; |
| 2659 | p->iIndent = 0; |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2664 | */ |
| 2665 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2666 | extern int sqlite3SelectTrace; |
| 2667 | static int savedSelectTrace; |
| 2668 | #endif |
| 2669 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2670 | extern int sqlite3WhereTrace; |
| 2671 | static int savedWhereTrace; |
| 2672 | #endif |
| 2673 | static void disable_debug_trace_modes(void){ |
| 2674 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2675 | savedSelectTrace = sqlite3SelectTrace; |
| 2676 | sqlite3SelectTrace = 0; |
| 2677 | #endif |
| 2678 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2679 | savedWhereTrace = sqlite3WhereTrace; |
| 2680 | sqlite3WhereTrace = 0; |
| 2681 | #endif |
| 2682 | } |
| 2683 | static void restore_debug_trace_modes(void){ |
| 2684 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2685 | sqlite3SelectTrace = savedSelectTrace; |
| 2686 | #endif |
| 2687 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2688 | sqlite3WhereTrace = savedWhereTrace; |
| 2689 | #endif |
| 2690 | } |
| 2691 | |
| 2692 | /* |
| 2693 | ** Run a prepared statement |
| 2694 | */ |
| 2695 | static void exec_prepared_stmt( |
| 2696 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2697 | sqlite3_stmt *pStmt /* Statment to run */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2698 | ){ |
| 2699 | int rc; |
| 2700 | |
| 2701 | /* perform the first step. this will tell us if we |
| 2702 | ** have a result set or not and how wide it is. |
| 2703 | */ |
| 2704 | rc = sqlite3_step(pStmt); |
| 2705 | /* if we have a result set... */ |
| 2706 | if( SQLITE_ROW == rc ){ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2707 | /* allocate space for col name ptr, value ptr, and type */ |
| 2708 | int nCol = sqlite3_column_count(pStmt); |
| 2709 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2710 | if( !pData ){ |
| 2711 | rc = SQLITE_NOMEM; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2712 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2713 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2714 | char **azVals = &azCols[nCol]; /* Results */ |
| 2715 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2716 | int i, x; |
| 2717 | assert(sizeof(int) <= sizeof(char *)); |
| 2718 | /* save off ptrs to column names */ |
| 2719 | for(i=0; i<nCol; i++){ |
| 2720 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2721 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2722 | do{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2723 | /* extract the data and data types */ |
| 2724 | for(i=0; i<nCol; i++){ |
| 2725 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2726 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2727 | azVals[i] = ""; |
| 2728 | }else{ |
| 2729 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2730 | } |
| 2731 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2732 | rc = SQLITE_NOMEM; |
| 2733 | break; /* from for */ |
| 2734 | } |
| 2735 | } /* end for */ |
| 2736 | |
| 2737 | /* if data and types extracted successfully... */ |
| 2738 | if( SQLITE_ROW == rc ){ |
| 2739 | /* call the supplied callback with the result row data */ |
| 2740 | if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2741 | rc = SQLITE_ABORT; |
| 2742 | }else{ |
| 2743 | rc = sqlite3_step(pStmt); |
| 2744 | } |
| 2745 | } |
| 2746 | } while( SQLITE_ROW == rc ); |
| 2747 | sqlite3_free(pData); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2752 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2753 | /* |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2754 | ** This function is called to process SQL if the previous shell command |
| 2755 | ** was ".expert". It passes the SQL in the second argument directly to |
| 2756 | ** the sqlite3expert object. |
| 2757 | ** |
| 2758 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 2759 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 2760 | ** an English language error message. It is the responsibility of the |
| 2761 | ** caller to eventually free this buffer using sqlite3_free(). |
| 2762 | */ |
| 2763 | static int expertHandleSQL( |
| 2764 | ShellState *pState, |
| 2765 | const char *zSql, |
| 2766 | char **pzErr |
| 2767 | ){ |
| 2768 | assert( pState->expert.pExpert ); |
| 2769 | assert( pzErr==0 || *pzErr==0 ); |
| 2770 | return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | ** This function is called either to silently clean up the object |
| 2775 | ** created by the ".expert" command (if bCancel==1), or to generate a |
| 2776 | ** report from it and then clean it up (if bCancel==0). |
| 2777 | ** |
| 2778 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 2779 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 2780 | ** an English language error message. It is the responsibility of the |
| 2781 | ** caller to eventually free this buffer using sqlite3_free(). |
| 2782 | */ |
| 2783 | static int expertFinish( |
| 2784 | ShellState *pState, |
| 2785 | int bCancel, |
| 2786 | char **pzErr |
| 2787 | ){ |
| 2788 | int rc = SQLITE_OK; |
| 2789 | sqlite3expert *p = pState->expert.pExpert; |
| 2790 | assert( p ); |
| 2791 | assert( bCancel || pzErr==0 || *pzErr==0 ); |
| 2792 | if( bCancel==0 ){ |
| 2793 | FILE *out = pState->out; |
| 2794 | int bVerbose = pState->expert.bVerbose; |
| 2795 | |
| 2796 | rc = sqlite3_expert_analyze(p, pzErr); |
| 2797 | if( rc==SQLITE_OK ){ |
| 2798 | int nQuery = sqlite3_expert_count(p); |
| 2799 | int i; |
| 2800 | |
| 2801 | if( bVerbose ){ |
| 2802 | const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
| 2803 | raw_printf(out, "-- Candidates -----------------------------\n"); |
| 2804 | raw_printf(out, "%s\n", zCand); |
| 2805 | } |
| 2806 | for(i=0; i<nQuery; i++){ |
| 2807 | const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
| 2808 | const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
| 2809 | const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
| 2810 | if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
| 2811 | if( bVerbose ){ |
| 2812 | raw_printf(out, "-- Query %d --------------------------------\n",i+1); |
| 2813 | raw_printf(out, "%s\n\n", zSql); |
| 2814 | } |
| 2815 | raw_printf(out, "%s\n", zIdx); |
| 2816 | raw_printf(out, "%s\n", zEQP); |
| 2817 | } |
| 2818 | } |
| 2819 | } |
| 2820 | sqlite3_expert_destroy(p); |
| 2821 | pState->expert.pExpert = 0; |
| 2822 | return rc; |
| 2823 | } |
| 2824 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2825 | /* |
| 2826 | ** Implementation of ".expert" dot command. |
| 2827 | */ |
| 2828 | static int expertDotCommand( |
| 2829 | ShellState *pState, /* Current shell tool state */ |
| 2830 | char **azArg, /* Array of arguments passed to dot command */ |
| 2831 | int nArg /* Number of entries in azArg[] */ |
| 2832 | ){ |
| 2833 | int rc = SQLITE_OK; |
| 2834 | char *zErr = 0; |
| 2835 | int i; |
| 2836 | int iSample = 0; |
| 2837 | |
| 2838 | assert( pState->expert.pExpert==0 ); |
| 2839 | memset(&pState->expert, 0, sizeof(ExpertInfo)); |
| 2840 | |
| 2841 | for(i=1; rc==SQLITE_OK && i<nArg; i++){ |
| 2842 | char *z = azArg[i]; |
| 2843 | int n; |
| 2844 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 2845 | n = strlen30(z); |
| 2846 | if( n>=2 && 0==strncmp(z, "-verbose", n) ){ |
| 2847 | pState->expert.bVerbose = 1; |
| 2848 | } |
| 2849 | else if( n>=2 && 0==strncmp(z, "-sample", n) ){ |
| 2850 | if( i==(nArg-1) ){ |
| 2851 | raw_printf(stderr, "option requires an argument: %s\n", z); |
| 2852 | rc = SQLITE_ERROR; |
| 2853 | }else{ |
| 2854 | iSample = (int)integerValue(azArg[++i]); |
| 2855 | if( iSample<0 || iSample>100 ){ |
| 2856 | raw_printf(stderr, "value out of range: %s\n", azArg[i]); |
| 2857 | rc = SQLITE_ERROR; |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | else{ |
| 2862 | raw_printf(stderr, "unknown option: %s\n", z); |
| 2863 | rc = SQLITE_ERROR; |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | if( rc==SQLITE_OK ){ |
| 2868 | pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); |
| 2869 | if( pState->expert.pExpert==0 ){ |
| 2870 | raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr); |
| 2871 | rc = SQLITE_ERROR; |
| 2872 | }else{ |
| 2873 | sqlite3_expert_config( |
| 2874 | pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample |
| 2875 | ); |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | return rc; |
| 2880 | } |
| 2881 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2884 | ** Execute a statement or set of statements. Print |
| 2885 | ** any result rows/columns depending on the current mode |
| 2886 | ** set via the supplied callback. |
| 2887 | ** |
| 2888 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2889 | ** function except it takes a slightly different callback |
| 2890 | ** and callback data argument. |
| 2891 | */ |
| 2892 | static int shell_exec( |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2893 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2894 | const char *zSql, /* SQL to be evaluated */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2895 | char **pzErrMsg /* Error msg written here */ |
| 2896 | ){ |
| 2897 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2898 | int rc = SQLITE_OK; /* Return Code */ |
| 2899 | int rc2; |
| 2900 | const char *zLeftover; /* Tail of unprocessed SQL */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2901 | sqlite3 *db = pArg->db; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2902 | |
| 2903 | if( pzErrMsg ){ |
| 2904 | *pzErrMsg = NULL; |
| 2905 | } |
| 2906 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2907 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2908 | if( pArg->expert.pExpert ){ |
| 2909 | rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
| 2910 | return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
| 2911 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 2912 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 2913 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2914 | while( zSql[0] && (SQLITE_OK == rc) ){ |
| 2915 | static const char *zStmtSql; |
| 2916 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2917 | if( SQLITE_OK != rc ){ |
| 2918 | if( pzErrMsg ){ |
| 2919 | *pzErrMsg = save_err_msg(db); |
| 2920 | } |
| 2921 | }else{ |
| 2922 | if( !pStmt ){ |
| 2923 | /* this happens for a comment or white-space */ |
| 2924 | zSql = zLeftover; |
| 2925 | while( IsSpace(zSql[0]) ) zSql++; |
| 2926 | continue; |
| 2927 | } |
| 2928 | zStmtSql = sqlite3_sql(pStmt); |
| 2929 | if( zStmtSql==0 ) zStmtSql = ""; |
| 2930 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 2931 | |
| 2932 | /* save off the prepared statment handle and reset row count */ |
| 2933 | if( pArg ){ |
| 2934 | pArg->pStmt = pStmt; |
| 2935 | pArg->cnt = 0; |
| 2936 | } |
| 2937 | |
| 2938 | /* echo the sql statement if echo on */ |
| 2939 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
| 2940 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
| 2941 | } |
| 2942 | |
| 2943 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
| 2944 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
| 2945 | sqlite3_stmt *pExplain; |
| 2946 | char *zEQP; |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2947 | int triggerEQP = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2948 | disable_debug_trace_modes(); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2949 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
| 2950 | if( pArg->autoEQP>=AUTOEQP_trigger ){ |
| 2951 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); |
| 2952 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2953 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
| 2954 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2955 | if( rc==SQLITE_OK ){ |
| 2956 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2957 | const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2958 | int iEqpId = sqlite3_column_int(pExplain, 0); |
| 2959 | int iParentId = sqlite3_column_int(pExplain, 1); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2960 | if( zEQPLine[0]=='-' ) eqp_render(pArg); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2961 | eqp_append(pArg, iEqpId, iParentId, zEQPLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2962 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2963 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2964 | } |
| 2965 | sqlite3_finalize(pExplain); |
| 2966 | sqlite3_free(zEQP); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 2967 | if( pArg->autoEQP>=AUTOEQP_full ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2968 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2969 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2970 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2971 | if( rc==SQLITE_OK ){ |
| 2972 | pArg->cMode = MODE_Explain; |
| 2973 | explain_data_prepare(pArg, pExplain); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 2974 | exec_prepared_stmt(pArg, pExplain); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2975 | explain_data_delete(pArg); |
| 2976 | } |
| 2977 | sqlite3_finalize(pExplain); |
| 2978 | sqlite3_free(zEQP); |
| 2979 | } |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 2980 | if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ |
| 2981 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); |
| 2982 | /* Reprepare pStmt before reactiving trace modes */ |
| 2983 | sqlite3_finalize(pStmt); |
| 2984 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
drh | 3c49eaf | 2018-06-07 15:23:43 +0000 | [diff] [blame] | 2985 | if( pArg ) pArg->pStmt = pStmt; |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 2986 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2987 | restore_debug_trace_modes(); |
| 2988 | } |
| 2989 | |
| 2990 | if( pArg ){ |
| 2991 | pArg->cMode = pArg->mode; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2992 | if( pArg->autoExplain ){ |
| 2993 | if( sqlite3_column_count(pStmt)==8 |
| 2994 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
| 2995 | ){ |
| 2996 | pArg->cMode = MODE_Explain; |
| 2997 | } |
| 2998 | if( sqlite3_column_count(pStmt)==4 |
| 2999 | && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){ |
| 3000 | pArg->cMode = MODE_EQP; |
| 3001 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* If the shell is currently in ".explain" mode, gather the extra |
| 3005 | ** data required to add indents to the output.*/ |
| 3006 | if( pArg->cMode==MODE_Explain ){ |
| 3007 | explain_data_prepare(pArg, pStmt); |
| 3008 | } |
| 3009 | } |
| 3010 | |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3011 | exec_prepared_stmt(pArg, pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3012 | explain_data_delete(pArg); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3013 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* print usage stats if stats on */ |
| 3016 | if( pArg && pArg->statsOn ){ |
| 3017 | display_stats(db, pArg, 0); |
| 3018 | } |
| 3019 | |
| 3020 | /* print loop-counters if required */ |
| 3021 | if( pArg && pArg->scanstatsOn ){ |
| 3022 | display_scanstats(db, pArg); |
| 3023 | } |
| 3024 | |
| 3025 | /* Finalize the statement just executed. If this fails, save a |
| 3026 | ** copy of the error message. Otherwise, set zSql to point to the |
| 3027 | ** next statement to execute. */ |
| 3028 | rc2 = sqlite3_finalize(pStmt); |
| 3029 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
| 3030 | if( rc==SQLITE_OK ){ |
| 3031 | zSql = zLeftover; |
| 3032 | while( IsSpace(zSql[0]) ) zSql++; |
| 3033 | }else if( pzErrMsg ){ |
| 3034 | *pzErrMsg = save_err_msg(db); |
| 3035 | } |
| 3036 | |
| 3037 | /* clear saved stmt handle */ |
| 3038 | if( pArg ){ |
| 3039 | pArg->pStmt = NULL; |
| 3040 | } |
| 3041 | } |
| 3042 | } /* end while */ |
| 3043 | |
| 3044 | return rc; |
| 3045 | } |
| 3046 | |
| 3047 | /* |
| 3048 | ** Release memory previously allocated by tableColumnList(). |
| 3049 | */ |
| 3050 | static void freeColumnList(char **azCol){ |
| 3051 | int i; |
| 3052 | for(i=1; azCol[i]; i++){ |
| 3053 | sqlite3_free(azCol[i]); |
| 3054 | } |
| 3055 | /* azCol[0] is a static string */ |
| 3056 | sqlite3_free(azCol); |
| 3057 | } |
| 3058 | |
| 3059 | /* |
| 3060 | ** Return a list of pointers to strings which are the names of all |
| 3061 | ** columns in table zTab. The memory to hold the names is dynamically |
| 3062 | ** allocated and must be released by the caller using a subsequent call |
| 3063 | ** to freeColumnList(). |
| 3064 | ** |
| 3065 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 3066 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 3067 | ** name of the rowid column. |
| 3068 | ** |
| 3069 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 3070 | ** by an entry with azCol[i]==0. |
| 3071 | */ |
| 3072 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 3073 | char **azCol = 0; |
| 3074 | sqlite3_stmt *pStmt; |
| 3075 | char *zSql; |
| 3076 | int nCol = 0; |
| 3077 | int nAlloc = 0; |
| 3078 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 3079 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 3080 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 3081 | int rc; |
| 3082 | |
| 3083 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 3084 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3085 | sqlite3_free(zSql); |
| 3086 | if( rc ) return 0; |
| 3087 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3088 | if( nCol>=nAlloc-2 ){ |
| 3089 | nAlloc = nAlloc*2 + nCol + 10; |
| 3090 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3091 | if( azCol==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3092 | } |
| 3093 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 3094 | if( sqlite3_column_int(pStmt, 5) ){ |
| 3095 | nPK++; |
| 3096 | if( nPK==1 |
| 3097 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 3098 | "INTEGER")==0 |
| 3099 | ){ |
| 3100 | isIPK = 1; |
| 3101 | }else{ |
| 3102 | isIPK = 0; |
| 3103 | } |
| 3104 | } |
| 3105 | } |
| 3106 | sqlite3_finalize(pStmt); |
drh | 4c6cddc | 2017-10-12 10:28:30 +0000 | [diff] [blame] | 3107 | if( azCol==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3108 | azCol[0] = 0; |
| 3109 | azCol[nCol+1] = 0; |
| 3110 | |
| 3111 | /* The decision of whether or not a rowid really needs to be preserved |
| 3112 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3113 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3114 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3115 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3116 | */ |
| 3117 | if( preserveRowid && isIPK ){ |
| 3118 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 3119 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 3120 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 3121 | ** ROWID aliases. To distinguish these cases, check to see if |
| 3122 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 3123 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 3124 | */ |
| 3125 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 3126 | " WHERE origin='pk'", zTab); |
| 3127 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3128 | sqlite3_free(zSql); |
| 3129 | if( rc ){ |
| 3130 | freeColumnList(azCol); |
| 3131 | return 0; |
| 3132 | } |
| 3133 | rc = sqlite3_step(pStmt); |
| 3134 | sqlite3_finalize(pStmt); |
| 3135 | preserveRowid = rc==SQLITE_ROW; |
| 3136 | } |
| 3137 | if( preserveRowid ){ |
| 3138 | /* Only preserve the rowid if we can find a name to use for the |
| 3139 | ** rowid */ |
| 3140 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3141 | int i, j; |
| 3142 | for(j=0; j<3; j++){ |
| 3143 | for(i=1; i<=nCol; i++){ |
| 3144 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3145 | } |
| 3146 | if( i>nCol ){ |
| 3147 | /* At this point, we know that azRowid[j] is not the name of any |
| 3148 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3149 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3150 | ** tables will fail this last check */ |
| 3151 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3152 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | return azCol; |
| 3158 | } |
| 3159 | |
| 3160 | /* |
| 3161 | ** Toggle the reverse_unordered_selects setting. |
| 3162 | */ |
| 3163 | static void toggleSelectOrder(sqlite3 *db){ |
| 3164 | sqlite3_stmt *pStmt = 0; |
| 3165 | int iSetting = 0; |
| 3166 | char zStmt[100]; |
| 3167 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3168 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3169 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3170 | } |
| 3171 | sqlite3_finalize(pStmt); |
| 3172 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3173 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3174 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | ** This is a different callback routine used for dumping the database. |
| 3179 | ** Each row received by this callback consists of a table name, |
| 3180 | ** the table type ("index" or "table") and SQL to create the table. |
| 3181 | ** This routine should print text sufficient to recreate the table. |
| 3182 | */ |
| 3183 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
| 3184 | int rc; |
| 3185 | const char *zTable; |
| 3186 | const char *zType; |
| 3187 | const char *zSql; |
| 3188 | ShellState *p = (ShellState *)pArg; |
| 3189 | |
| 3190 | UNUSED_PARAMETER(azNotUsed); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 3191 | if( nArg!=3 || azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3192 | zTable = azArg[0]; |
| 3193 | zType = azArg[1]; |
| 3194 | zSql = azArg[2]; |
| 3195 | |
| 3196 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
| 3197 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
| 3198 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
| 3199 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 3200 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3201 | return 0; |
| 3202 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3203 | char *zIns; |
| 3204 | if( !p->writableSchema ){ |
| 3205 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 3206 | p->writableSchema = 1; |
| 3207 | } |
| 3208 | zIns = sqlite3_mprintf( |
| 3209 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3210 | "VALUES('table','%q','%q',0,'%q');", |
| 3211 | zTable, zTable, zSql); |
| 3212 | utf8_printf(p->out, "%s\n", zIns); |
| 3213 | sqlite3_free(zIns); |
| 3214 | return 0; |
| 3215 | }else{ |
| 3216 | printSchemaLine(p->out, zSql, ";\n"); |
| 3217 | } |
| 3218 | |
| 3219 | if( strcmp(zType, "table")==0 ){ |
| 3220 | ShellText sSelect; |
| 3221 | ShellText sTable; |
| 3222 | char **azCol; |
| 3223 | int i; |
| 3224 | char *savedDestTable; |
| 3225 | int savedMode; |
| 3226 | |
| 3227 | azCol = tableColumnList(p, zTable); |
| 3228 | if( azCol==0 ){ |
| 3229 | p->nErr++; |
| 3230 | return 0; |
| 3231 | } |
| 3232 | |
| 3233 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3234 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 3235 | initText(&sTable); |
| 3236 | appendText(&sTable, zTable, quoteChar(zTable)); |
| 3237 | /* If preserving the rowid, add a column list after the table name. |
| 3238 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3239 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3240 | */ |
| 3241 | if( azCol[0] ){ |
| 3242 | appendText(&sTable, "(", 0); |
| 3243 | appendText(&sTable, azCol[0], 0); |
| 3244 | for(i=1; azCol[i]; i++){ |
| 3245 | appendText(&sTable, ",", 0); |
| 3246 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
| 3247 | } |
| 3248 | appendText(&sTable, ")", 0); |
| 3249 | } |
| 3250 | |
| 3251 | /* Build an appropriate SELECT statement */ |
| 3252 | initText(&sSelect); |
| 3253 | appendText(&sSelect, "SELECT ", 0); |
| 3254 | if( azCol[0] ){ |
| 3255 | appendText(&sSelect, azCol[0], 0); |
| 3256 | appendText(&sSelect, ",", 0); |
| 3257 | } |
| 3258 | for(i=1; azCol[i]; i++){ |
| 3259 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
| 3260 | if( azCol[i+1] ){ |
| 3261 | appendText(&sSelect, ",", 0); |
| 3262 | } |
| 3263 | } |
| 3264 | freeColumnList(azCol); |
| 3265 | appendText(&sSelect, " FROM ", 0); |
| 3266 | appendText(&sSelect, zTable, quoteChar(zTable)); |
| 3267 | |
| 3268 | savedDestTable = p->zDestTable; |
| 3269 | savedMode = p->mode; |
| 3270 | p->zDestTable = sTable.z; |
| 3271 | p->mode = p->cMode = MODE_Insert; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3272 | rc = shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3273 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3274 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3275 | toggleSelectOrder(p->db); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3276 | shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3277 | toggleSelectOrder(p->db); |
| 3278 | } |
| 3279 | p->zDestTable = savedDestTable; |
| 3280 | p->mode = savedMode; |
| 3281 | freeText(&sTable); |
| 3282 | freeText(&sSelect); |
| 3283 | if( rc ) p->nErr++; |
| 3284 | } |
| 3285 | return 0; |
| 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3290 | ** the contents of the query are output as SQL statements. |
| 3291 | ** |
| 3292 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3293 | ** "ORDER BY rowid DESC" to the end. |
| 3294 | */ |
| 3295 | static int run_schema_dump_query( |
| 3296 | ShellState *p, |
| 3297 | const char *zQuery |
| 3298 | ){ |
| 3299 | int rc; |
| 3300 | char *zErr = 0; |
| 3301 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
| 3302 | if( rc==SQLITE_CORRUPT ){ |
| 3303 | char *zQ2; |
| 3304 | int len = strlen30(zQuery); |
| 3305 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3306 | if( zErr ){ |
| 3307 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
| 3308 | sqlite3_free(zErr); |
| 3309 | zErr = 0; |
| 3310 | } |
| 3311 | zQ2 = malloc( len+100 ); |
| 3312 | if( zQ2==0 ) return rc; |
| 3313 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
| 3314 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3315 | if( rc ){ |
| 3316 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
| 3317 | }else{ |
| 3318 | rc = SQLITE_CORRUPT; |
| 3319 | } |
| 3320 | sqlite3_free(zErr); |
| 3321 | free(zQ2); |
| 3322 | } |
| 3323 | return rc; |
| 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | ** Text of a help message |
| 3328 | */ |
| 3329 | static char zHelp[] = |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 3330 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 3331 | ".archive ... Manage SQL archives: \".archive --help\" for details\n" |
| 3332 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3333 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3334 | ".auth ON|OFF Show authorizer callbacks\n" |
| 3335 | #endif |
| 3336 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 3337 | " Add \"--append\" to open using appendvfs.\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3338 | ".bail on|off Stop after hitting an error. Default OFF\n" |
| 3339 | ".binary on|off Turn binary output on or off. Default OFF\n" |
| 3340 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
| 3341 | ".changes on|off Show number of rows changed by SQL\n" |
| 3342 | ".check GLOB Fail if output since .testcase does not match\n" |
| 3343 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
| 3344 | ".databases List names and files of attached databases\n" |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 3345 | ".dbconfig ?op? ?val? List or change sqlite3_db_config() options\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3346 | ".dbinfo ?DB? Show status information about the database\n" |
| 3347 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
| 3348 | " If TABLE specified, only dump tables matching\n" |
| 3349 | " LIKE pattern TABLE.\n" |
| 3350 | ".echo on|off Turn command echo on or off\n" |
| 3351 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 3352 | ".excel Display the output of next command in a spreadsheet\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3353 | ".exit Exit this program\n" |
dan | 2e1ea57 | 2017-12-21 18:55:24 +0000 | [diff] [blame] | 3354 | ".expert EXPERIMENTAL. Suggest indexes for specified queries\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3355 | /* Because explain mode comes on automatically now, the ".explain" mode |
| 3356 | ** is removed from the help screen. It is still supported for legacy, however */ |
| 3357 | /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/ |
| 3358 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
| 3359 | ".headers on|off Turn display of headers on or off\n" |
| 3360 | ".help Show this message\n" |
| 3361 | ".import FILE TABLE Import data from FILE into TABLE\n" |
| 3362 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3363 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3364 | #endif |
| 3365 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3366 | " If TABLE specified, only show indexes for tables\n" |
| 3367 | " matching LIKE pattern TABLE.\n" |
| 3368 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3369 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3370 | #endif |
| 3371 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
| 3372 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3373 | " fkey-indexes Find missing foreign key indexes\n" |
| 3374 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3375 | ".load FILE ?ENTRY? Load an extension library\n" |
| 3376 | #endif |
| 3377 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
| 3378 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
| 3379 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
| 3380 | " csv Comma-separated values\n" |
| 3381 | " column Left-aligned columns. (See .width)\n" |
| 3382 | " html HTML <table> code\n" |
| 3383 | " insert SQL insert statements for TABLE\n" |
| 3384 | " line One value per line\n" |
| 3385 | " list Values delimited by \"|\"\n" |
| 3386 | " quote Escape answers as for SQL\n" |
| 3387 | " tabs Tab-separated values\n" |
| 3388 | " tcl TCL list elements\n" |
| 3389 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 3390 | ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n" |
| 3391 | " or invoke system text editor (-e) or spreadsheet (-x)\n" |
| 3392 | " on the output.\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3393 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n" |
| 3394 | " The --new option starts with an empty file\n" |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 3395 | " Other options: --readonly --append --zip\n" |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 3396 | ".output ?FILE? Send output to FILE or stdout\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3397 | ".print STRING... Print literal STRING\n" |
| 3398 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
| 3399 | ".quit Exit this program\n" |
| 3400 | ".read FILENAME Execute SQL in FILENAME\n" |
| 3401 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
| 3402 | ".save FILE Write in-memory database into FILE\n" |
| 3403 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
| 3404 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3405 | " Add --indent for pretty-printing\n" |
| 3406 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
| 3407 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3408 | " separator for both the output mode and .import\n" |
| 3409 | #if defined(SQLITE_ENABLE_SESSION) |
| 3410 | ".session CMD ... Create or control sessions\n" |
| 3411 | #endif |
| 3412 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3413 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3414 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3415 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3416 | ".show Show the current values for various settings\n" |
| 3417 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3418 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3419 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3420 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3421 | ".tables ?TABLE? List names of tables\n" |
| 3422 | " If TABLE specified, only list tables matching\n" |
| 3423 | " LIKE pattern TABLE.\n" |
| 3424 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
| 3425 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
| 3426 | ".timer on|off Turn SQL timer on or off\n" |
| 3427 | ".trace FILE|off Output each SQL statement as it is run\n" |
| 3428 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
| 3429 | ".vfslist List all available VFSes\n" |
| 3430 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
| 3431 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
| 3432 | " Negative values right-justify\n" |
| 3433 | ; |
| 3434 | |
| 3435 | #if defined(SQLITE_ENABLE_SESSION) |
| 3436 | /* |
| 3437 | ** Print help information for the ".sessions" command |
| 3438 | */ |
| 3439 | void session_help(ShellState *p){ |
| 3440 | raw_printf(p->out, |
| 3441 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3442 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3443 | "Subcommands:\n" |
| 3444 | " attach TABLE Attach TABLE\n" |
| 3445 | " changeset FILE Write a changeset into FILE\n" |
| 3446 | " close Close one session\n" |
| 3447 | " enable ?BOOLEAN? Set or query the enable bit\n" |
| 3448 | " filter GLOB... Reject tables matching GLOBs\n" |
| 3449 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3450 | " isempty Query whether the session is empty\n" |
| 3451 | " list List currently open session names\n" |
| 3452 | " open DB NAME Open a new session on DB\n" |
| 3453 | " patchset FILE Write a patchset into FILE\n" |
| 3454 | ); |
| 3455 | } |
| 3456 | #endif |
| 3457 | |
| 3458 | |
| 3459 | /* Forward reference */ |
| 3460 | static int process_input(ShellState *p, FILE *in); |
| 3461 | |
| 3462 | /* |
| 3463 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 3464 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3465 | ** the memory. |
| 3466 | ** |
| 3467 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3468 | ** read. |
| 3469 | ** |
| 3470 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3471 | ** from the file before the buffer is returned. This byte is not included in |
| 3472 | ** the final value of (*pnByte), if applicable. |
| 3473 | ** |
| 3474 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3475 | ** is undefined in this case. |
| 3476 | */ |
| 3477 | static char *readFile(const char *zName, int *pnByte){ |
| 3478 | FILE *in = fopen(zName, "rb"); |
| 3479 | long nIn; |
| 3480 | size_t nRead; |
| 3481 | char *pBuf; |
| 3482 | if( in==0 ) return 0; |
| 3483 | fseek(in, 0, SEEK_END); |
| 3484 | nIn = ftell(in); |
| 3485 | rewind(in); |
| 3486 | pBuf = sqlite3_malloc64( nIn+1 ); |
| 3487 | if( pBuf==0 ) return 0; |
| 3488 | nRead = fread(pBuf, nIn, 1, in); |
| 3489 | fclose(in); |
| 3490 | if( nRead!=1 ){ |
| 3491 | sqlite3_free(pBuf); |
| 3492 | return 0; |
| 3493 | } |
| 3494 | pBuf[nIn] = 0; |
| 3495 | if( pnByte ) *pnByte = nIn; |
| 3496 | return pBuf; |
| 3497 | } |
| 3498 | |
| 3499 | #if defined(SQLITE_ENABLE_SESSION) |
| 3500 | /* |
| 3501 | ** Close a single OpenSession object and release all of its associated |
| 3502 | ** resources. |
| 3503 | */ |
| 3504 | static void session_close(OpenSession *pSession){ |
| 3505 | int i; |
| 3506 | sqlite3session_delete(pSession->p); |
| 3507 | sqlite3_free(pSession->zName); |
| 3508 | for(i=0; i<pSession->nFilter; i++){ |
| 3509 | sqlite3_free(pSession->azFilter[i]); |
| 3510 | } |
| 3511 | sqlite3_free(pSession->azFilter); |
| 3512 | memset(pSession, 0, sizeof(OpenSession)); |
| 3513 | } |
| 3514 | #endif |
| 3515 | |
| 3516 | /* |
| 3517 | ** Close all OpenSession objects and release all associated resources. |
| 3518 | */ |
| 3519 | #if defined(SQLITE_ENABLE_SESSION) |
| 3520 | static void session_close_all(ShellState *p){ |
| 3521 | int i; |
| 3522 | for(i=0; i<p->nSession; i++){ |
| 3523 | session_close(&p->aSession[i]); |
| 3524 | } |
| 3525 | p->nSession = 0; |
| 3526 | } |
| 3527 | #else |
| 3528 | # define session_close_all(X) |
| 3529 | #endif |
| 3530 | |
| 3531 | /* |
| 3532 | ** Implementation of the xFilter function for an open session. Omit |
| 3533 | ** any tables named by ".session filter" but let all other table through. |
| 3534 | */ |
| 3535 | #if defined(SQLITE_ENABLE_SESSION) |
| 3536 | static int session_filter(void *pCtx, const char *zTab){ |
| 3537 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3538 | int i; |
| 3539 | for(i=0; i<pSession->nFilter; i++){ |
| 3540 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3541 | } |
| 3542 | return 1; |
| 3543 | } |
| 3544 | #endif |
| 3545 | |
| 3546 | /* |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3547 | ** Try to deduce the type of file for zName based on its content. Return |
| 3548 | ** one of the SHELL_OPEN_* constants. |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 3549 | ** |
| 3550 | ** If the file does not exist or is empty but its name looks like a ZIP |
| 3551 | ** archive and the dfltZip flag is true, then assume it is a ZIP archive. |
| 3552 | ** Otherwise, assume an ordinary database regardless of the filename if |
| 3553 | ** the type cannot be determined from content. |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3554 | */ |
drh | fc97c1c | 2018-05-14 00:41:12 +0000 | [diff] [blame] | 3555 | int deduceDatabaseType(const char *zName, int dfltZip){ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3556 | FILE *f = fopen(zName, "rb"); |
| 3557 | size_t n; |
| 3558 | int rc = SHELL_OPEN_UNSPEC; |
| 3559 | char zBuf[100]; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 3560 | if( f==0 ){ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 3561 | if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
| 3562 | return SHELL_OPEN_ZIPFILE; |
| 3563 | }else{ |
| 3564 | return SHELL_OPEN_NORMAL; |
| 3565 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 3566 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3567 | fseek(f, -25, SEEK_END); |
| 3568 | n = fread(zBuf, 25, 1, f); |
| 3569 | if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
| 3570 | rc = SHELL_OPEN_APPENDVFS; |
| 3571 | }else{ |
| 3572 | fseek(f, -22, SEEK_END); |
| 3573 | n = fread(zBuf, 22, 1, f); |
| 3574 | if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
| 3575 | && zBuf[3]==0x06 ){ |
| 3576 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 3577 | }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
mistachkin | a3926f4 | 2018-05-14 12:23:04 +0000 | [diff] [blame] | 3578 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | fclose(f); |
| 3582 | return rc; |
| 3583 | } |
| 3584 | |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 3585 | /* Flags for open_db(). |
| 3586 | ** |
| 3587 | ** The default behavior of open_db() is to exit(1) if the database fails to |
| 3588 | ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error |
| 3589 | ** but still returns without calling exit. |
| 3590 | ** |
| 3591 | ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a |
| 3592 | ** ZIP archive if the file does not exist or is empty and its name matches |
| 3593 | ** the *.zip pattern. |
| 3594 | */ |
| 3595 | #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ |
| 3596 | #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ |
| 3597 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3598 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3599 | ** Make sure the database is open. If it is not, then open it. If |
| 3600 | ** the database fails to open, print an error message and exit. |
| 3601 | */ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 3602 | static void open_db(ShellState *p, int openFlags){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3603 | if( p->db==0 ){ |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 3604 | if( p->openMode==SHELL_OPEN_UNSPEC ){ |
| 3605 | if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){ |
| 3606 | p->openMode = SHELL_OPEN_NORMAL; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 3607 | }else{ |
| 3608 | p->openMode = (u8)deduceDatabaseType(p->zDbFilename, |
| 3609 | (openFlags & OPEN_DB_ZIPFILE)!=0); |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 3610 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3611 | } |
| 3612 | switch( p->openMode ){ |
| 3613 | case SHELL_OPEN_APPENDVFS: { |
| 3614 | sqlite3_open_v2(p->zDbFilename, &p->db, |
| 3615 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs"); |
| 3616 | break; |
| 3617 | } |
| 3618 | case SHELL_OPEN_ZIPFILE: { |
| 3619 | sqlite3_open(":memory:", &p->db); |
| 3620 | break; |
| 3621 | } |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 3622 | case SHELL_OPEN_READONLY: { |
| 3623 | sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0); |
| 3624 | break; |
| 3625 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3626 | case SHELL_OPEN_UNSPEC: |
| 3627 | case SHELL_OPEN_NORMAL: { |
| 3628 | sqlite3_open(p->zDbFilename, &p->db); |
| 3629 | break; |
| 3630 | } |
| 3631 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3632 | globalDb = p->db; |
| 3633 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 3634 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
| 3635 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 3636 | if( openFlags & OPEN_DB_KEEPALIVE ) return; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3637 | exit(1); |
| 3638 | } |
| 3639 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3640 | sqlite3_enable_load_extension(p->db, 1); |
| 3641 | #endif |
| 3642 | sqlite3_fileio_init(p->db, 0, 0); |
| 3643 | sqlite3_shathree_init(p->db, 0, 0); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3644 | sqlite3_completion_init(p->db, 0, 0); |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 3645 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 3646 | sqlite3_zipfile_init(p->db, 0, 0); |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 3647 | sqlite3_sqlar_init(p->db, 0, 0); |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 3648 | #endif |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 3649 | sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3650 | shellAddSchemaName, 0, 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 3651 | sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, |
| 3652 | shellModuleSchema, 0, 0); |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 3653 | sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, |
| 3654 | shellPutsFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3655 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 3656 | sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, |
| 3657 | editFunc, 0, 0); |
| 3658 | sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, |
| 3659 | editFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 3660 | #endif |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 3661 | if( p->openMode==SHELL_OPEN_ZIPFILE ){ |
| 3662 | char *zSql = sqlite3_mprintf( |
| 3663 | "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); |
| 3664 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 3665 | sqlite3_free(zSql); |
| 3666 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3667 | } |
| 3668 | } |
| 3669 | |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 3670 | /* |
| 3671 | ** Attempt to close the databaes connection. Report errors. |
| 3672 | */ |
| 3673 | void close_db(sqlite3 *db){ |
| 3674 | int rc = sqlite3_close(db); |
| 3675 | if( rc ){ |
| 3676 | utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", |
| 3677 | rc, sqlite3_errmsg(db)); |
| 3678 | } |
| 3679 | } |
| 3680 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3681 | #if HAVE_READLINE || HAVE_EDITLINE |
| 3682 | /* |
| 3683 | ** Readline completion callbacks |
| 3684 | */ |
| 3685 | static char *readline_completion_generator(const char *text, int state){ |
| 3686 | static sqlite3_stmt *pStmt = 0; |
| 3687 | char *zRet; |
| 3688 | if( state==0 ){ |
| 3689 | char *zSql; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3690 | sqlite3_finalize(pStmt); |
| 3691 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 3692 | " FROM completion(%Q) ORDER BY 1", text); |
| 3693 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 3694 | sqlite3_free(zSql); |
| 3695 | } |
| 3696 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 968d871 | 2017-07-14 00:28:28 +0000 | [diff] [blame] | 3697 | zRet = strdup((const char*)sqlite3_column_text(pStmt, 0)); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3698 | }else{ |
| 3699 | sqlite3_finalize(pStmt); |
| 3700 | pStmt = 0; |
| 3701 | zRet = 0; |
| 3702 | } |
| 3703 | return zRet; |
| 3704 | } |
| 3705 | static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 3706 | rl_attempted_completion_over = 1; |
| 3707 | return rl_completion_matches(zText, readline_completion_generator); |
| 3708 | } |
| 3709 | |
| 3710 | #elif HAVE_LINENOISE |
| 3711 | /* |
| 3712 | ** Linenoise completion callback |
| 3713 | */ |
| 3714 | static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3715 | int nLine = strlen30(zLine); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3716 | int i, iStart; |
| 3717 | sqlite3_stmt *pStmt = 0; |
| 3718 | char *zSql; |
| 3719 | char zBuf[1000]; |
| 3720 | |
| 3721 | if( nLine>sizeof(zBuf)-30 ) return; |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 3722 | if( zLine[0]=='.' || zLine[0]=='#') return; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 3723 | for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 3724 | if( i==nLine-1 ) return; |
| 3725 | iStart = i+1; |
| 3726 | memcpy(zBuf, zLine, iStart); |
| 3727 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 3728 | " FROM completion(%Q,%Q) ORDER BY 1", |
| 3729 | &zLine[iStart], zLine); |
| 3730 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 3731 | sqlite3_free(zSql); |
| 3732 | sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 3733 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3734 | const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 3735 | int nCompletion = sqlite3_column_bytes(pStmt, 0); |
| 3736 | if( iStart+nCompletion < sizeof(zBuf)-1 ){ |
| 3737 | memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 3738 | linenoiseAddCompletion(lc, zBuf); |
| 3739 | } |
| 3740 | } |
| 3741 | sqlite3_finalize(pStmt); |
| 3742 | } |
| 3743 | #endif |
| 3744 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3745 | /* |
| 3746 | ** Do C-language style dequoting. |
| 3747 | ** |
| 3748 | ** \a -> alarm |
| 3749 | ** \b -> backspace |
| 3750 | ** \t -> tab |
| 3751 | ** \n -> newline |
| 3752 | ** \v -> vertical tab |
| 3753 | ** \f -> form feed |
| 3754 | ** \r -> carriage return |
| 3755 | ** \s -> space |
| 3756 | ** \" -> " |
| 3757 | ** \' -> ' |
| 3758 | ** \\ -> backslash |
| 3759 | ** \NNN -> ascii character NNN in octal |
| 3760 | */ |
| 3761 | static void resolve_backslashes(char *z){ |
| 3762 | int i, j; |
| 3763 | char c; |
| 3764 | while( *z && *z!='\\' ) z++; |
| 3765 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 3766 | if( c=='\\' && z[i+1]!=0 ){ |
| 3767 | c = z[++i]; |
| 3768 | if( c=='a' ){ |
| 3769 | c = '\a'; |
| 3770 | }else if( c=='b' ){ |
| 3771 | c = '\b'; |
| 3772 | }else if( c=='t' ){ |
| 3773 | c = '\t'; |
| 3774 | }else if( c=='n' ){ |
| 3775 | c = '\n'; |
| 3776 | }else if( c=='v' ){ |
| 3777 | c = '\v'; |
| 3778 | }else if( c=='f' ){ |
| 3779 | c = '\f'; |
| 3780 | }else if( c=='r' ){ |
| 3781 | c = '\r'; |
| 3782 | }else if( c=='"' ){ |
| 3783 | c = '"'; |
| 3784 | }else if( c=='\'' ){ |
| 3785 | c = '\''; |
| 3786 | }else if( c=='\\' ){ |
| 3787 | c = '\\'; |
| 3788 | }else if( c>='0' && c<='7' ){ |
| 3789 | c -= '0'; |
| 3790 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3791 | i++; |
| 3792 | c = (c<<3) + z[i] - '0'; |
| 3793 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3794 | i++; |
| 3795 | c = (c<<3) + z[i] - '0'; |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | z[j] = c; |
| 3801 | } |
| 3802 | if( j<i ) z[j] = 0; |
| 3803 | } |
| 3804 | |
| 3805 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3806 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3807 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3808 | */ |
| 3809 | static int booleanValue(const char *zArg){ |
| 3810 | int i; |
| 3811 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3812 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3813 | }else{ |
| 3814 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3815 | } |
| 3816 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3817 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3818 | return 1; |
| 3819 | } |
| 3820 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3821 | return 0; |
| 3822 | } |
| 3823 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 3824 | zArg); |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | ** Set or clear a shell flag according to a boolean value. |
| 3830 | */ |
| 3831 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3832 | if( booleanValue(zArg) ){ |
| 3833 | ShellSetFlag(p, mFlag); |
| 3834 | }else{ |
| 3835 | ShellClearFlag(p, mFlag); |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | /* |
| 3840 | ** Close an output file, assuming it is not stderr or stdout |
| 3841 | */ |
| 3842 | static void output_file_close(FILE *f){ |
| 3843 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3844 | } |
| 3845 | |
| 3846 | /* |
| 3847 | ** Try to open an output file. The names "stdout" and "stderr" are |
| 3848 | ** recognized and do the right thing. NULL is returned if the output |
| 3849 | ** filename is "off". |
| 3850 | */ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 3851 | static FILE *output_file_open(const char *zFile, int bTextMode){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3852 | FILE *f; |
| 3853 | if( strcmp(zFile,"stdout")==0 ){ |
| 3854 | f = stdout; |
| 3855 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3856 | f = stderr; |
| 3857 | }else if( strcmp(zFile, "off")==0 ){ |
| 3858 | f = 0; |
| 3859 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 3860 | f = fopen(zFile, bTextMode ? "w" : "wb"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3861 | if( f==0 ){ |
| 3862 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 3863 | } |
| 3864 | } |
| 3865 | return f; |
| 3866 | } |
| 3867 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3868 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 3869 | /* |
| 3870 | ** A routine for handling output from sqlite3_trace(). |
| 3871 | */ |
| 3872 | static int sql_trace_callback( |
| 3873 | unsigned mType, |
| 3874 | void *pArg, |
| 3875 | void *pP, |
| 3876 | void *pX |
| 3877 | ){ |
| 3878 | FILE *f = (FILE*)pArg; |
| 3879 | UNUSED_PARAMETER(mType); |
| 3880 | UNUSED_PARAMETER(pP); |
| 3881 | if( f ){ |
| 3882 | const char *z = (const char*)pX; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 3883 | int i = strlen30(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3884 | while( i>0 && z[i-1]==';' ){ i--; } |
| 3885 | utf8_printf(f, "%.*s;\n", i, z); |
| 3886 | } |
| 3887 | return 0; |
| 3888 | } |
| 3889 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3890 | |
| 3891 | /* |
| 3892 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3893 | ** a useful spot to set a debugger breakpoint. |
| 3894 | */ |
| 3895 | static void test_breakpoint(void){ |
| 3896 | static int nCall = 0; |
| 3897 | nCall++; |
| 3898 | } |
| 3899 | |
| 3900 | /* |
| 3901 | ** An object used to read a CSV and other files for import. |
| 3902 | */ |
| 3903 | typedef struct ImportCtx ImportCtx; |
| 3904 | struct ImportCtx { |
| 3905 | const char *zFile; /* Name of the input file */ |
| 3906 | FILE *in; /* Read the CSV text from this input stream */ |
| 3907 | char *z; /* Accumulated text for a field */ |
| 3908 | int n; /* Number of bytes in z */ |
| 3909 | int nAlloc; /* Space allocated for z[] */ |
| 3910 | int nLine; /* Current line number */ |
| 3911 | int bNotFirst; /* True if one or more bytes already read */ |
| 3912 | int cTerm; /* Character that terminated the most recent field */ |
| 3913 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3914 | int cRowSep; /* The row separator character. (Usually "\n") */ |
| 3915 | }; |
| 3916 | |
| 3917 | /* Append a single byte to z[] */ |
| 3918 | static void import_append_char(ImportCtx *p, int c){ |
| 3919 | if( p->n+1>=p->nAlloc ){ |
| 3920 | p->nAlloc += p->nAlloc + 100; |
| 3921 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3922 | if( p->z==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3923 | } |
| 3924 | p->z[p->n++] = (char)c; |
| 3925 | } |
| 3926 | |
| 3927 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3928 | ** with the option of having a separator other than ",". |
| 3929 | ** |
| 3930 | ** + Input comes from p->in. |
| 3931 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 3932 | ** from sqlite3_malloc64(). |
| 3933 | ** + Use p->cSep as the column separator. The default is ",". |
| 3934 | ** + Use p->rSep as the row separator. The default is "\n". |
| 3935 | ** + Keep track of the line number in p->nLine. |
| 3936 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3937 | ** EOF on end-of-file. |
| 3938 | ** + Report syntax errors on stderr |
| 3939 | */ |
| 3940 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 3941 | int c; |
| 3942 | int cSep = p->cColSep; |
| 3943 | int rSep = p->cRowSep; |
| 3944 | p->n = 0; |
| 3945 | c = fgetc(p->in); |
| 3946 | if( c==EOF || seenInterrupt ){ |
| 3947 | p->cTerm = EOF; |
| 3948 | return 0; |
| 3949 | } |
| 3950 | if( c=='"' ){ |
| 3951 | int pc, ppc; |
| 3952 | int startLine = p->nLine; |
| 3953 | int cQuote = c; |
| 3954 | pc = ppc = 0; |
| 3955 | while( 1 ){ |
| 3956 | c = fgetc(p->in); |
| 3957 | if( c==rSep ) p->nLine++; |
| 3958 | if( c==cQuote ){ |
| 3959 | if( pc==cQuote ){ |
| 3960 | pc = 0; |
| 3961 | continue; |
| 3962 | } |
| 3963 | } |
| 3964 | if( (c==cSep && pc==cQuote) |
| 3965 | || (c==rSep && pc==cQuote) |
| 3966 | || (c==rSep && pc=='\r' && ppc==cQuote) |
| 3967 | || (c==EOF && pc==cQuote) |
| 3968 | ){ |
| 3969 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
| 3970 | p->cTerm = c; |
| 3971 | break; |
| 3972 | } |
| 3973 | if( pc==cQuote && c!='\r' ){ |
| 3974 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
| 3975 | p->zFile, p->nLine, cQuote); |
| 3976 | } |
| 3977 | if( c==EOF ){ |
| 3978 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
| 3979 | p->zFile, startLine, cQuote); |
| 3980 | p->cTerm = c; |
| 3981 | break; |
| 3982 | } |
| 3983 | import_append_char(p, c); |
| 3984 | ppc = pc; |
| 3985 | pc = c; |
| 3986 | } |
| 3987 | }else{ |
| 3988 | /* If this is the first field being parsed and it begins with the |
| 3989 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 3990 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 3991 | import_append_char(p, c); |
| 3992 | c = fgetc(p->in); |
| 3993 | if( (c&0xff)==0xbb ){ |
| 3994 | import_append_char(p, c); |
| 3995 | c = fgetc(p->in); |
| 3996 | if( (c&0xff)==0xbf ){ |
| 3997 | p->bNotFirst = 1; |
| 3998 | p->n = 0; |
| 3999 | return csv_read_one_field(p); |
| 4000 | } |
| 4001 | } |
| 4002 | } |
| 4003 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 4004 | import_append_char(p, c); |
| 4005 | c = fgetc(p->in); |
| 4006 | } |
| 4007 | if( c==rSep ){ |
| 4008 | p->nLine++; |
| 4009 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 4010 | } |
| 4011 | p->cTerm = c; |
| 4012 | } |
| 4013 | if( p->z ) p->z[p->n] = 0; |
| 4014 | p->bNotFirst = 1; |
| 4015 | return p->z; |
| 4016 | } |
| 4017 | |
| 4018 | /* Read a single field of ASCII delimited text. |
| 4019 | ** |
| 4020 | ** + Input comes from p->in. |
| 4021 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 4022 | ** from sqlite3_malloc64(). |
| 4023 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 4024 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 4025 | ** + Keep track of the row number in p->nLine. |
| 4026 | ** + Store the character that terminates the field in p->cTerm. Store |
| 4027 | ** EOF on end-of-file. |
| 4028 | ** + Report syntax errors on stderr |
| 4029 | */ |
| 4030 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 4031 | int c; |
| 4032 | int cSep = p->cColSep; |
| 4033 | int rSep = p->cRowSep; |
| 4034 | p->n = 0; |
| 4035 | c = fgetc(p->in); |
| 4036 | if( c==EOF || seenInterrupt ){ |
| 4037 | p->cTerm = EOF; |
| 4038 | return 0; |
| 4039 | } |
| 4040 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 4041 | import_append_char(p, c); |
| 4042 | c = fgetc(p->in); |
| 4043 | } |
| 4044 | if( c==rSep ){ |
| 4045 | p->nLine++; |
| 4046 | } |
| 4047 | p->cTerm = c; |
| 4048 | if( p->z ) p->z[p->n] = 0; |
| 4049 | return p->z; |
| 4050 | } |
| 4051 | |
| 4052 | /* |
| 4053 | ** Try to transfer data for table zTable. If an error is seen while |
| 4054 | ** moving forward, try to go backwards. The backwards movement won't |
| 4055 | ** work for WITHOUT ROWID tables. |
| 4056 | */ |
| 4057 | static void tryToCloneData( |
| 4058 | ShellState *p, |
| 4059 | sqlite3 *newDb, |
| 4060 | const char *zTable |
| 4061 | ){ |
| 4062 | sqlite3_stmt *pQuery = 0; |
| 4063 | sqlite3_stmt *pInsert = 0; |
| 4064 | char *zQuery = 0; |
| 4065 | char *zInsert = 0; |
| 4066 | int rc; |
| 4067 | int i, j, n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4068 | int nTable = strlen30(zTable); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4069 | int k = 0; |
| 4070 | int cnt = 0; |
| 4071 | const int spinRate = 10000; |
| 4072 | |
| 4073 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 4074 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4075 | if( rc ){ |
| 4076 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 4077 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4078 | zQuery); |
| 4079 | goto end_data_xfer; |
| 4080 | } |
| 4081 | n = sqlite3_column_count(pQuery); |
| 4082 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 4083 | if( zInsert==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4084 | sqlite3_snprintf(200+nTable,zInsert, |
| 4085 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4086 | i = strlen30(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4087 | for(j=1; j<n; j++){ |
| 4088 | memcpy(zInsert+i, ",?", 2); |
| 4089 | i += 2; |
| 4090 | } |
| 4091 | memcpy(zInsert+i, ");", 3); |
| 4092 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 4093 | if( rc ){ |
| 4094 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 4095 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 4096 | zQuery); |
| 4097 | goto end_data_xfer; |
| 4098 | } |
| 4099 | for(k=0; k<2; k++){ |
| 4100 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4101 | for(i=0; i<n; i++){ |
| 4102 | switch( sqlite3_column_type(pQuery, i) ){ |
| 4103 | case SQLITE_NULL: { |
| 4104 | sqlite3_bind_null(pInsert, i+1); |
| 4105 | break; |
| 4106 | } |
| 4107 | case SQLITE_INTEGER: { |
| 4108 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 4109 | break; |
| 4110 | } |
| 4111 | case SQLITE_FLOAT: { |
| 4112 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 4113 | break; |
| 4114 | } |
| 4115 | case SQLITE_TEXT: { |
| 4116 | sqlite3_bind_text(pInsert, i+1, |
| 4117 | (const char*)sqlite3_column_text(pQuery,i), |
| 4118 | -1, SQLITE_STATIC); |
| 4119 | break; |
| 4120 | } |
| 4121 | case SQLITE_BLOB: { |
| 4122 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 4123 | sqlite3_column_bytes(pQuery,i), |
| 4124 | SQLITE_STATIC); |
| 4125 | break; |
| 4126 | } |
| 4127 | } |
| 4128 | } /* End for */ |
| 4129 | rc = sqlite3_step(pInsert); |
| 4130 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 4131 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
| 4132 | sqlite3_errmsg(newDb)); |
| 4133 | } |
| 4134 | sqlite3_reset(pInsert); |
| 4135 | cnt++; |
| 4136 | if( (cnt%spinRate)==0 ){ |
| 4137 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 4138 | fflush(stdout); |
| 4139 | } |
| 4140 | } /* End while */ |
| 4141 | if( rc==SQLITE_DONE ) break; |
| 4142 | sqlite3_finalize(pQuery); |
| 4143 | sqlite3_free(zQuery); |
| 4144 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 4145 | zTable); |
| 4146 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4147 | if( rc ){ |
| 4148 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
| 4149 | break; |
| 4150 | } |
| 4151 | } /* End for(k=0...) */ |
| 4152 | |
| 4153 | end_data_xfer: |
| 4154 | sqlite3_finalize(pQuery); |
| 4155 | sqlite3_finalize(pInsert); |
| 4156 | sqlite3_free(zQuery); |
| 4157 | sqlite3_free(zInsert); |
| 4158 | } |
| 4159 | |
| 4160 | |
| 4161 | /* |
| 4162 | ** Try to transfer all rows of the schema that match zWhere. For |
| 4163 | ** each row, invoke xForEach() on the object defined by that row. |
| 4164 | ** If an error is encountered while moving forward through the |
| 4165 | ** sqlite_master table, try again moving backwards. |
| 4166 | */ |
| 4167 | static void tryToCloneSchema( |
| 4168 | ShellState *p, |
| 4169 | sqlite3 *newDb, |
| 4170 | const char *zWhere, |
| 4171 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 4172 | ){ |
| 4173 | sqlite3_stmt *pQuery = 0; |
| 4174 | char *zQuery = 0; |
| 4175 | int rc; |
| 4176 | const unsigned char *zName; |
| 4177 | const unsigned char *zSql; |
| 4178 | char *zErrMsg = 0; |
| 4179 | |
| 4180 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4181 | " WHERE %s", zWhere); |
| 4182 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4183 | if( rc ){ |
| 4184 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 4185 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4186 | zQuery); |
| 4187 | goto end_schema_xfer; |
| 4188 | } |
| 4189 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4190 | zName = sqlite3_column_text(pQuery, 0); |
| 4191 | zSql = sqlite3_column_text(pQuery, 1); |
| 4192 | printf("%s... ", zName); fflush(stdout); |
| 4193 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4194 | if( zErrMsg ){ |
| 4195 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 4196 | sqlite3_free(zErrMsg); |
| 4197 | zErrMsg = 0; |
| 4198 | } |
| 4199 | if( xForEach ){ |
| 4200 | xForEach(p, newDb, (const char*)zName); |
| 4201 | } |
| 4202 | printf("done\n"); |
| 4203 | } |
| 4204 | if( rc!=SQLITE_DONE ){ |
| 4205 | sqlite3_finalize(pQuery); |
| 4206 | sqlite3_free(zQuery); |
| 4207 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4208 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4209 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4210 | if( rc ){ |
| 4211 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 4212 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4213 | zQuery); |
| 4214 | goto end_schema_xfer; |
| 4215 | } |
| 4216 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4217 | zName = sqlite3_column_text(pQuery, 0); |
| 4218 | zSql = sqlite3_column_text(pQuery, 1); |
| 4219 | printf("%s... ", zName); fflush(stdout); |
| 4220 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4221 | if( zErrMsg ){ |
| 4222 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 4223 | sqlite3_free(zErrMsg); |
| 4224 | zErrMsg = 0; |
| 4225 | } |
| 4226 | if( xForEach ){ |
| 4227 | xForEach(p, newDb, (const char*)zName); |
| 4228 | } |
| 4229 | printf("done\n"); |
| 4230 | } |
| 4231 | } |
| 4232 | end_schema_xfer: |
| 4233 | sqlite3_finalize(pQuery); |
| 4234 | sqlite3_free(zQuery); |
| 4235 | } |
| 4236 | |
| 4237 | /* |
| 4238 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4239 | ** as possible out of the main database (which might be corrupt) and write it |
| 4240 | ** into zNewDb. |
| 4241 | */ |
| 4242 | static void tryToClone(ShellState *p, const char *zNewDb){ |
| 4243 | int rc; |
| 4244 | sqlite3 *newDb = 0; |
| 4245 | if( access(zNewDb,0)==0 ){ |
| 4246 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
| 4247 | return; |
| 4248 | } |
| 4249 | rc = sqlite3_open(zNewDb, &newDb); |
| 4250 | if( rc ){ |
| 4251 | utf8_printf(stderr, "Cannot create output database: %s\n", |
| 4252 | sqlite3_errmsg(newDb)); |
| 4253 | }else{ |
| 4254 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
| 4255 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
| 4256 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4257 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
| 4258 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
| 4259 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4260 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 4261 | close_db(newDb); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | /* |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4265 | ** Change the output file back to stdout. |
| 4266 | ** |
| 4267 | ** If the p->doXdgOpen flag is set, that means the output was being |
| 4268 | ** redirected to a temporary file named by p->zTempFile. In that case, |
| 4269 | ** launch start/open/xdg-open on that temporary file. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4270 | */ |
| 4271 | static void output_reset(ShellState *p){ |
| 4272 | if( p->outfile[0]=='|' ){ |
| 4273 | #ifndef SQLITE_OMIT_POPEN |
| 4274 | pclose(p->out); |
| 4275 | #endif |
| 4276 | }else{ |
| 4277 | output_file_close(p->out); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4278 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4279 | if( p->doXdgOpen ){ |
| 4280 | const char *zXdgOpenCmd = |
| 4281 | #if defined(_WIN32) |
| 4282 | "start"; |
| 4283 | #elif defined(__APPLE__) |
| 4284 | "open"; |
| 4285 | #else |
| 4286 | "xdg-open"; |
| 4287 | #endif |
| 4288 | char *zCmd; |
| 4289 | zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 4290 | if( system(zCmd) ){ |
| 4291 | utf8_printf(stderr, "Failed: [%s]\n", zCmd); |
| 4292 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4293 | sqlite3_free(zCmd); |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 4294 | outputModePop(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4295 | p->doXdgOpen = 0; |
| 4296 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4297 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4298 | } |
| 4299 | p->outfile[0] = 0; |
| 4300 | p->out = stdout; |
| 4301 | } |
| 4302 | |
| 4303 | /* |
| 4304 | ** Run an SQL command and return the single integer result. |
| 4305 | */ |
| 4306 | static int db_int(ShellState *p, const char *zSql){ |
| 4307 | sqlite3_stmt *pStmt; |
| 4308 | int res = 0; |
| 4309 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4310 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4311 | res = sqlite3_column_int(pStmt,0); |
| 4312 | } |
| 4313 | sqlite3_finalize(pStmt); |
| 4314 | return res; |
| 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4319 | */ |
| 4320 | static unsigned int get2byteInt(unsigned char *a){ |
| 4321 | return (a[0]<<8) + a[1]; |
| 4322 | } |
| 4323 | static unsigned int get4byteInt(unsigned char *a){ |
| 4324 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4325 | } |
| 4326 | |
| 4327 | /* |
| 4328 | ** Implementation of the ".info" command. |
| 4329 | ** |
| 4330 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4331 | */ |
| 4332 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 4333 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4334 | { "file change counter:", 24 }, |
| 4335 | { "database page count:", 28 }, |
| 4336 | { "freelist page count:", 36 }, |
| 4337 | { "schema cookie:", 40 }, |
| 4338 | { "schema format:", 44 }, |
| 4339 | { "default cache size:", 48 }, |
| 4340 | { "autovacuum top root:", 52 }, |
| 4341 | { "incremental vacuum:", 64 }, |
| 4342 | { "text encoding:", 56 }, |
| 4343 | { "user version:", 60 }, |
| 4344 | { "application id:", 68 }, |
| 4345 | { "software version:", 96 }, |
| 4346 | }; |
| 4347 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4348 | { "number of tables:", |
| 4349 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4350 | { "number of indexes:", |
| 4351 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4352 | { "number of triggers:", |
| 4353 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4354 | { "number of views:", |
| 4355 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4356 | { "schema size:", |
| 4357 | "SELECT total(length(sql)) FROM %s" }, |
| 4358 | }; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4359 | int i; |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 4360 | unsigned iDataVersion; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4361 | char *zSchemaTab; |
| 4362 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4363 | sqlite3_stmt *pStmt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4364 | unsigned char aHdr[100]; |
| 4365 | open_db(p, 0); |
| 4366 | if( p->db==0 ) return 1; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4367 | sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
| 4368 | -1, &pStmt, 0); |
| 4369 | sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
| 4370 | if( sqlite3_step(pStmt)==SQLITE_ROW |
| 4371 | && sqlite3_column_bytes(pStmt,0)>100 |
| 4372 | ){ |
| 4373 | memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
| 4374 | sqlite3_finalize(pStmt); |
| 4375 | }else{ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4376 | raw_printf(stderr, "unable to read database header\n"); |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 4377 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4378 | return 1; |
| 4379 | } |
| 4380 | i = get2byteInt(aHdr+16); |
| 4381 | if( i==1 ) i = 65536; |
| 4382 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4383 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4384 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4385 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
| 4386 | for(i=0; i<ArraySize(aField); i++){ |
| 4387 | int ofst = aField[i].ofst; |
| 4388 | unsigned int val = get4byteInt(aHdr + ofst); |
| 4389 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
| 4390 | switch( ofst ){ |
| 4391 | case 56: { |
| 4392 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4393 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4394 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
| 4395 | } |
| 4396 | } |
| 4397 | raw_printf(p->out, "\n"); |
| 4398 | } |
| 4399 | if( zDb==0 ){ |
| 4400 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4401 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4402 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4403 | }else{ |
| 4404 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4405 | } |
| 4406 | for(i=0; i<ArraySize(aQuery); i++){ |
| 4407 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4408 | int val = db_int(p, zSql); |
| 4409 | sqlite3_free(zSql); |
| 4410 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
| 4411 | } |
| 4412 | sqlite3_free(zSchemaTab); |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 4413 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); |
| 4414 | utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4415 | return 0; |
| 4416 | } |
| 4417 | |
| 4418 | /* |
| 4419 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4420 | */ |
| 4421 | static int shellDatabaseError(sqlite3 *db){ |
| 4422 | const char *zErr = sqlite3_errmsg(db); |
| 4423 | utf8_printf(stderr, "Error: %s\n", zErr); |
| 4424 | return 1; |
| 4425 | } |
| 4426 | |
| 4427 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4428 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4429 | ** if they match and FALSE (0) if they do not match. |
| 4430 | ** |
| 4431 | ** Globbing rules: |
| 4432 | ** |
| 4433 | ** '*' Matches any sequence of zero or more characters. |
| 4434 | ** |
| 4435 | ** '?' Matches exactly one character. |
| 4436 | ** |
| 4437 | ** [...] Matches one character from the enclosed list of |
| 4438 | ** characters. |
| 4439 | ** |
| 4440 | ** [^...] Matches one character not in the enclosed list. |
| 4441 | ** |
| 4442 | ** '#' Matches any sequence of one or more digits with an |
| 4443 | ** optional + or - sign in front |
| 4444 | ** |
| 4445 | ** ' ' Any span of whitespace matches any other span of |
| 4446 | ** whitespace. |
| 4447 | ** |
| 4448 | ** Extra whitespace at the end of z[] is ignored. |
| 4449 | */ |
| 4450 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4451 | int c, c2; |
| 4452 | int invert; |
| 4453 | int seen; |
| 4454 | |
| 4455 | while( (c = (*(zGlob++)))!=0 ){ |
| 4456 | if( IsSpace(c) ){ |
| 4457 | if( !IsSpace(*z) ) return 0; |
| 4458 | while( IsSpace(*zGlob) ) zGlob++; |
| 4459 | while( IsSpace(*z) ) z++; |
| 4460 | }else if( c=='*' ){ |
| 4461 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4462 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4463 | } |
| 4464 | if( c==0 ){ |
| 4465 | return 1; |
| 4466 | }else if( c=='[' ){ |
| 4467 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4468 | z++; |
| 4469 | } |
| 4470 | return (*z)!=0; |
| 4471 | } |
| 4472 | while( (c2 = (*(z++)))!=0 ){ |
| 4473 | while( c2!=c ){ |
| 4474 | c2 = *(z++); |
| 4475 | if( c2==0 ) return 0; |
| 4476 | } |
| 4477 | if( testcase_glob(zGlob,z) ) return 1; |
| 4478 | } |
| 4479 | return 0; |
| 4480 | }else if( c=='?' ){ |
| 4481 | if( (*(z++))==0 ) return 0; |
| 4482 | }else if( c=='[' ){ |
| 4483 | int prior_c = 0; |
| 4484 | seen = 0; |
| 4485 | invert = 0; |
| 4486 | c = *(z++); |
| 4487 | if( c==0 ) return 0; |
| 4488 | c2 = *(zGlob++); |
| 4489 | if( c2=='^' ){ |
| 4490 | invert = 1; |
| 4491 | c2 = *(zGlob++); |
| 4492 | } |
| 4493 | if( c2==']' ){ |
| 4494 | if( c==']' ) seen = 1; |
| 4495 | c2 = *(zGlob++); |
| 4496 | } |
| 4497 | while( c2 && c2!=']' ){ |
| 4498 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4499 | c2 = *(zGlob++); |
| 4500 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4501 | prior_c = 0; |
| 4502 | }else{ |
| 4503 | if( c==c2 ){ |
| 4504 | seen = 1; |
| 4505 | } |
| 4506 | prior_c = c2; |
| 4507 | } |
| 4508 | c2 = *(zGlob++); |
| 4509 | } |
| 4510 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4511 | }else if( c=='#' ){ |
| 4512 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4513 | if( !IsDigit(z[0]) ) return 0; |
| 4514 | z++; |
| 4515 | while( IsDigit(z[0]) ){ z++; } |
| 4516 | }else{ |
| 4517 | if( c!=(*(z++)) ) return 0; |
| 4518 | } |
| 4519 | } |
| 4520 | while( IsSpace(*z) ){ z++; } |
| 4521 | return *z==0; |
| 4522 | } |
| 4523 | |
| 4524 | |
| 4525 | /* |
| 4526 | ** Compare the string as a command-line option with either one or two |
| 4527 | ** initial "-" characters. |
| 4528 | */ |
| 4529 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4530 | if( zStr[0]!='-' ) return 0; |
| 4531 | zStr++; |
| 4532 | if( zStr[0]=='-' ) zStr++; |
| 4533 | return strcmp(zStr, zOpt)==0; |
| 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | ** Delete a file. |
| 4538 | */ |
| 4539 | int shellDeleteFile(const char *zFilename){ |
| 4540 | int rc; |
| 4541 | #ifdef _WIN32 |
| 4542 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
| 4543 | rc = _wunlink(z); |
| 4544 | sqlite3_free(z); |
| 4545 | #else |
| 4546 | rc = unlink(zFilename); |
| 4547 | #endif |
| 4548 | return rc; |
| 4549 | } |
| 4550 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4551 | /* |
| 4552 | ** Try to delete the temporary file (if there is one) and free the |
| 4553 | ** memory used to hold the name of the temp file. |
| 4554 | */ |
| 4555 | static void clearTempFile(ShellState *p){ |
| 4556 | if( p->zTempFile==0 ) return; |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 4557 | if( p->doXdgOpen ) return; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4558 | if( shellDeleteFile(p->zTempFile) ) return; |
| 4559 | sqlite3_free(p->zTempFile); |
| 4560 | p->zTempFile = 0; |
| 4561 | } |
| 4562 | |
| 4563 | /* |
| 4564 | ** Create a new temp file name with the given suffix. |
| 4565 | */ |
| 4566 | static void newTempFile(ShellState *p, const char *zSuffix){ |
| 4567 | clearTempFile(p); |
| 4568 | sqlite3_free(p->zTempFile); |
| 4569 | p->zTempFile = 0; |
drh | 7f3bf8a | 2018-01-10 21:50:08 +0000 | [diff] [blame] | 4570 | if( p->db ){ |
| 4571 | sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); |
| 4572 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 4573 | if( p->zTempFile==0 ){ |
| 4574 | sqlite3_uint64 r; |
| 4575 | sqlite3_randomness(sizeof(r), &r); |
| 4576 | p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix); |
| 4577 | }else{ |
| 4578 | p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); |
| 4579 | } |
| 4580 | if( p->zTempFile==0 ){ |
| 4581 | raw_printf(stderr, "out of memory\n"); |
| 4582 | exit(1); |
| 4583 | } |
| 4584 | } |
| 4585 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4586 | |
| 4587 | /* |
| 4588 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
| 4589 | ** by the ".lint fkey-indexes" command. This scalar function is always |
| 4590 | ** called with four arguments - the parent table name, the parent column name, |
| 4591 | ** the child table name and the child column name. |
| 4592 | ** |
| 4593 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4594 | ** |
| 4595 | ** If either of the named tables or columns do not exist, this function |
| 4596 | ** returns an empty string. An empty string is also returned if both tables |
| 4597 | ** and columns exist but have the same default collation sequence. Or, |
| 4598 | ** if both exist but the default collation sequences are different, this |
| 4599 | ** function returns the string " COLLATE <parent-collation>", where |
| 4600 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4601 | */ |
| 4602 | static void shellFkeyCollateClause( |
| 4603 | sqlite3_context *pCtx, |
| 4604 | int nVal, |
| 4605 | sqlite3_value **apVal |
| 4606 | ){ |
| 4607 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4608 | const char *zParent; |
| 4609 | const char *zParentCol; |
| 4610 | const char *zParentSeq; |
| 4611 | const char *zChild; |
| 4612 | const char *zChildCol; |
| 4613 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
| 4614 | int rc; |
| 4615 | |
| 4616 | assert( nVal==4 ); |
| 4617 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4618 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4619 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4620 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4621 | |
| 4622 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4623 | rc = sqlite3_table_column_metadata( |
| 4624 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4625 | ); |
| 4626 | if( rc==SQLITE_OK ){ |
| 4627 | rc = sqlite3_table_column_metadata( |
| 4628 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4629 | ); |
| 4630 | } |
| 4631 | |
| 4632 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4633 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4634 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4635 | sqlite3_free(z); |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | |
| 4640 | /* |
| 4641 | ** The implementation of dot-command ".lint fkey-indexes". |
| 4642 | */ |
| 4643 | static int lintFkeyIndexes( |
| 4644 | ShellState *pState, /* Current shell tool state */ |
| 4645 | char **azArg, /* Array of arguments passed to dot command */ |
| 4646 | int nArg /* Number of entries in azArg[] */ |
| 4647 | ){ |
| 4648 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4649 | FILE *out = pState->out; /* Stream to write non-error output to */ |
| 4650 | int bVerbose = 0; /* If -verbose is present */ |
| 4651 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
| 4652 | int i; /* To iterate through azArg[] */ |
| 4653 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4654 | int rc; /* Return code */ |
| 4655 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
| 4656 | |
| 4657 | /* |
| 4658 | ** This SELECT statement returns one row for each foreign key constraint |
| 4659 | ** in the schema of the main database. The column values are: |
| 4660 | ** |
| 4661 | ** 0. The text of an SQL statement similar to: |
| 4662 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4663 | ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4664 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4665 | ** This SELECT is similar to the one that the foreign keys implementation |
| 4666 | ** 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] | 4667 | ** be used to optimize this query, then it can also be used by the FK |
| 4668 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4669 | ** table. |
| 4670 | ** |
| 4671 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4672 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4673 | ** contains an index that can be used to optimize the query. |
| 4674 | ** |
| 4675 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4676 | ** |
| 4677 | ** "child_table(child_key1, child_key2)" |
| 4678 | ** |
| 4679 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4680 | ** |
| 4681 | ** "parent_table(parent_key1, parent_key2)" |
| 4682 | ** |
| 4683 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4684 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4685 | ** |
| 4686 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4687 | ** |
| 4688 | ** 5. The name of the parent table. |
| 4689 | ** |
| 4690 | ** These six values are used by the C logic below to generate the report. |
| 4691 | */ |
| 4692 | const char *zSql = |
| 4693 | "SELECT " |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 4694 | " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4695 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 4696 | " || fkey_collate_clause(" |
| 4697 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
| 4698 | ", " |
| 4699 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
| 4700 | " || group_concat('*=?', ' AND ') || ')'" |
| 4701 | ", " |
| 4702 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
| 4703 | ", " |
| 4704 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
| 4705 | ", " |
| 4706 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4707 | " || ' ON ' || quote(s.name) || '('" |
| 4708 | " || group_concat(quote(f.[from]) ||" |
| 4709 | " fkey_collate_clause(" |
| 4710 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
| 4711 | " || ');'" |
| 4712 | ", " |
| 4713 | " f.[table] " |
| 4714 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
| 4715 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
| 4716 | "GROUP BY s.name, f.id " |
| 4717 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
| 4718 | ; |
| 4719 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
| 4720 | |
| 4721 | for(i=2; i<nArg; i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4722 | int n = strlen30(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4723 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4724 | bVerbose = 1; |
| 4725 | } |
| 4726 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4727 | bGroupByParent = 1; |
| 4728 | zIndent = " "; |
| 4729 | } |
| 4730 | else{ |
| 4731 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4732 | azArg[0], azArg[1] |
| 4733 | ); |
| 4734 | return SQLITE_ERROR; |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | /* Register the fkey_collate_clause() SQL function */ |
| 4739 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4740 | 0, shellFkeyCollateClause, 0, 0 |
| 4741 | ); |
| 4742 | |
| 4743 | |
| 4744 | if( rc==SQLITE_OK ){ |
| 4745 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4746 | } |
| 4747 | if( rc==SQLITE_OK ){ |
| 4748 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4749 | } |
| 4750 | |
| 4751 | if( rc==SQLITE_OK ){ |
| 4752 | int rc2; |
| 4753 | char *zPrev = 0; |
| 4754 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4755 | int res = -1; |
| 4756 | sqlite3_stmt *pExplain = 0; |
| 4757 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4758 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4759 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4760 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4761 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
| 4762 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
| 4763 | |
| 4764 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4765 | if( rc!=SQLITE_OK ) break; |
| 4766 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4767 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
| 4768 | res = ( |
| 4769 | 0==sqlite3_strglob(zGlob, zPlan) |
| 4770 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 4771 | ); |
| 4772 | } |
| 4773 | rc = sqlite3_finalize(pExplain); |
| 4774 | if( rc!=SQLITE_OK ) break; |
| 4775 | |
| 4776 | if( res<0 ){ |
| 4777 | raw_printf(stderr, "Error: internal error"); |
| 4778 | break; |
| 4779 | }else{ |
| 4780 | if( bGroupByParent |
| 4781 | && (bVerbose || res==0) |
| 4782 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 4783 | ){ |
| 4784 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4785 | sqlite3_free(zPrev); |
| 4786 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4787 | } |
| 4788 | |
| 4789 | if( res==0 ){ |
| 4790 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4791 | }else if( bVerbose ){ |
| 4792 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 4793 | zIndent, zFrom, zTarget |
| 4794 | ); |
| 4795 | } |
| 4796 | } |
| 4797 | } |
| 4798 | sqlite3_free(zPrev); |
| 4799 | |
| 4800 | if( rc!=SQLITE_OK ){ |
| 4801 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4802 | } |
| 4803 | |
| 4804 | rc2 = sqlite3_finalize(pSql); |
| 4805 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4806 | rc = rc2; |
| 4807 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4808 | } |
| 4809 | }else{ |
| 4810 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4811 | } |
| 4812 | |
| 4813 | return rc; |
| 4814 | } |
| 4815 | |
| 4816 | /* |
| 4817 | ** Implementation of ".lint" dot command. |
| 4818 | */ |
| 4819 | static int lintDotCommand( |
| 4820 | ShellState *pState, /* Current shell tool state */ |
| 4821 | char **azArg, /* Array of arguments passed to dot command */ |
| 4822 | int nArg /* Number of entries in azArg[] */ |
| 4823 | ){ |
| 4824 | int n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 4825 | n = (nArg>=2 ? strlen30(azArg[1]) : 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4826 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4827 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4828 | |
| 4829 | usage: |
| 4830 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4831 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4832 | raw_printf(stderr, " fkey-indexes\n"); |
| 4833 | return SQLITE_ERROR; |
| 4834 | } |
| 4835 | |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4836 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 4837 | /********************************************************************************* |
| 4838 | ** The ".archive" or ".ar" command. |
| 4839 | */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4840 | static void shellPrepare( |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4841 | sqlite3 *db, |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4842 | int *pRc, |
| 4843 | const char *zSql, |
| 4844 | sqlite3_stmt **ppStmt |
| 4845 | ){ |
| 4846 | *ppStmt = 0; |
| 4847 | if( *pRc==SQLITE_OK ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4848 | int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4849 | if( rc!=SQLITE_OK ){ |
| 4850 | raw_printf(stderr, "sql error: %s (%d)\n", |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4851 | sqlite3_errmsg(db), sqlite3_errcode(db) |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4852 | ); |
| 4853 | *pRc = rc; |
| 4854 | } |
| 4855 | } |
| 4856 | } |
| 4857 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4858 | static void shellPreparePrintf( |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4859 | sqlite3 *db, |
| 4860 | int *pRc, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4861 | sqlite3_stmt **ppStmt, |
| 4862 | const char *zFmt, |
| 4863 | ... |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4864 | ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 4865 | *ppStmt = 0; |
| 4866 | if( *pRc==SQLITE_OK ){ |
| 4867 | va_list ap; |
| 4868 | char *z; |
| 4869 | va_start(ap, zFmt); |
| 4870 | z = sqlite3_vmprintf(zFmt, ap); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4871 | if( z==0 ){ |
| 4872 | *pRc = SQLITE_NOMEM; |
| 4873 | }else{ |
| 4874 | shellPrepare(db, pRc, z, ppStmt); |
| 4875 | sqlite3_free(z); |
| 4876 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4880 | static void shellFinalize( |
| 4881 | int *pRc, |
| 4882 | sqlite3_stmt *pStmt |
| 4883 | ){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 4884 | if( pStmt ){ |
| 4885 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 4886 | int rc = sqlite3_finalize(pStmt); |
| 4887 | if( *pRc==SQLITE_OK ){ |
| 4888 | if( rc!=SQLITE_OK ){ |
| 4889 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 4890 | } |
| 4891 | *pRc = rc; |
| 4892 | } |
| 4893 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | static void shellReset( |
| 4897 | int *pRc, |
| 4898 | sqlite3_stmt *pStmt |
| 4899 | ){ |
| 4900 | int rc = sqlite3_reset(pStmt); |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 4901 | if( *pRc==SQLITE_OK ){ |
| 4902 | if( rc!=SQLITE_OK ){ |
| 4903 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 4904 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 4905 | } |
| 4906 | *pRc = rc; |
| 4907 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 4908 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4909 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4910 | ** Structure representing a single ".ar" command. |
| 4911 | */ |
| 4912 | typedef struct ArCommand ArCommand; |
| 4913 | struct ArCommand { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4914 | u8 eCmd; /* An AR_CMD_* value */ |
| 4915 | u8 bVerbose; /* True if --verbose */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4916 | u8 bZip; /* True if the archive is a ZIP */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4917 | u8 bDryRun; /* True if --dry-run */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4918 | u8 bAppend; /* True if --append */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 4919 | u8 fromCmdLine; /* Run from -A instead of .archive */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4920 | int nArg; /* Number of command arguments */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4921 | char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4922 | const char *zFile; /* --file argument, or NULL */ |
| 4923 | const char *zDir; /* --directory argument, or NULL */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4924 | char **azArg; /* Array of command arguments */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4925 | ShellState *p; /* Shell state */ |
| 4926 | sqlite3 *db; /* Database containing the archive */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4927 | }; |
| 4928 | |
| 4929 | /* |
| 4930 | ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. |
| 4931 | */ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4932 | static int arUsage(FILE *f){ |
| 4933 | raw_printf(f, |
| 4934 | "\n" |
| 4935 | "Usage: .ar [OPTION...] [FILE...]\n" |
| 4936 | "The .ar command manages sqlar archives.\n" |
| 4937 | "\n" |
| 4938 | "Examples:\n" |
| 4939 | " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar\n" |
| 4940 | " .ar -tf archive.sar # List members of archive.sar\n" |
| 4941 | " .ar -xvf archive.sar # Verbosely extract files from archive.sar\n" |
| 4942 | "\n" |
| 4943 | "Each command line must feature exactly one command option:\n" |
| 4944 | " -c, --create Create a new archive\n" |
| 4945 | " -u, --update Update or add files to an existing archive\n" |
| 4946 | " -t, --list List contents of archive\n" |
| 4947 | " -x, --extract Extract files from archive\n" |
| 4948 | "\n" |
| 4949 | "And zero or more optional options:\n" |
| 4950 | " -v, --verbose Print each filename as it is processed\n" |
| 4951 | " -f FILE, --file FILE Operate on archive FILE (default is current db)\n" |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 4952 | " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n" |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4953 | " -C DIR, --directory DIR Change to directory DIR to read/extract files\n" |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4954 | " -n, --dryrun Show the SQL that would have occurred\n" |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4955 | "\n" |
| 4956 | "See also: http://sqlite.org/cli.html#sqlar_archive_support\n" |
| 4957 | "\n" |
| 4958 | ); |
| 4959 | return SQLITE_ERROR; |
| 4960 | } |
| 4961 | |
| 4962 | /* |
| 4963 | ** Print an error message for the .ar command to stderr and return |
| 4964 | ** SQLITE_ERROR. |
| 4965 | */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 4966 | static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4967 | va_list ap; |
| 4968 | char *z; |
| 4969 | va_start(ap, zFmt); |
| 4970 | z = sqlite3_vmprintf(zFmt, ap); |
| 4971 | va_end(ap); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 4972 | utf8_printf(stderr, "Error: %s\n", z); |
| 4973 | if( pAr->fromCmdLine ){ |
| 4974 | utf8_printf(stderr, "Use \"-A\" for more help\n"); |
| 4975 | }else{ |
| 4976 | utf8_printf(stderr, "Use \".archive --help\" for more help\n"); |
| 4977 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4978 | sqlite3_free(z); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 4979 | return SQLITE_ERROR; |
| 4980 | } |
| 4981 | |
| 4982 | /* |
| 4983 | ** Values for ArCommand.eCmd. |
| 4984 | */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4985 | #define AR_CMD_CREATE 1 |
| 4986 | #define AR_CMD_EXTRACT 2 |
| 4987 | #define AR_CMD_LIST 3 |
| 4988 | #define AR_CMD_UPDATE 4 |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 4989 | #define AR_CMD_HELP 5 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4990 | |
| 4991 | /* |
| 4992 | ** Other (non-command) switches. |
| 4993 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4994 | #define AR_SWITCH_VERBOSE 6 |
| 4995 | #define AR_SWITCH_FILE 7 |
| 4996 | #define AR_SWITCH_DIRECTORY 8 |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 4997 | #define AR_SWITCH_APPEND 9 |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 4998 | #define AR_SWITCH_DRYRUN 10 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 4999 | |
| 5000 | static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ |
| 5001 | switch( eSwitch ){ |
| 5002 | case AR_CMD_CREATE: |
| 5003 | case AR_CMD_EXTRACT: |
| 5004 | case AR_CMD_LIST: |
| 5005 | case AR_CMD_UPDATE: |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5006 | case AR_CMD_HELP: |
| 5007 | if( pAr->eCmd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5008 | return arErrorMsg(pAr, "multiple command options"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5009 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5010 | pAr->eCmd = eSwitch; |
| 5011 | break; |
| 5012 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5013 | case AR_SWITCH_DRYRUN: |
| 5014 | pAr->bDryRun = 1; |
| 5015 | break; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5016 | case AR_SWITCH_VERBOSE: |
| 5017 | pAr->bVerbose = 1; |
| 5018 | break; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5019 | case AR_SWITCH_APPEND: |
| 5020 | pAr->bAppend = 1; |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 5021 | /* Fall thru into --file */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5022 | case AR_SWITCH_FILE: |
| 5023 | pAr->zFile = zArg; |
| 5024 | break; |
| 5025 | case AR_SWITCH_DIRECTORY: |
| 5026 | pAr->zDir = zArg; |
| 5027 | break; |
| 5028 | } |
| 5029 | |
| 5030 | return SQLITE_OK; |
| 5031 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5032 | |
| 5033 | /* |
| 5034 | ** Parse the command line for an ".ar" command. The results are written into |
| 5035 | ** structure (*pAr). SQLITE_OK is returned if the command line is parsed |
| 5036 | ** successfully, otherwise an error message is written to stderr and |
| 5037 | ** SQLITE_ERROR returned. |
| 5038 | */ |
| 5039 | static int arParseCommand( |
| 5040 | char **azArg, /* Array of arguments passed to dot command */ |
| 5041 | int nArg, /* Number of entries in azArg[] */ |
| 5042 | ArCommand *pAr /* Populate this object */ |
| 5043 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5044 | struct ArSwitch { |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5045 | const char *zLong; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5046 | char cShort; |
| 5047 | u8 eSwitch; |
| 5048 | u8 bArg; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5049 | } aSwitch[] = { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5050 | { "create", 'c', AR_CMD_CREATE, 0 }, |
| 5051 | { "extract", 'x', AR_CMD_EXTRACT, 0 }, |
| 5052 | { "list", 't', AR_CMD_LIST, 0 }, |
| 5053 | { "update", 'u', AR_CMD_UPDATE, 0 }, |
| 5054 | { "help", 'h', AR_CMD_HELP, 0 }, |
| 5055 | { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, |
| 5056 | { "file", 'f', AR_SWITCH_FILE, 1 }, |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 5057 | { "append", 'a', AR_SWITCH_APPEND, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5058 | { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5059 | { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5060 | }; |
| 5061 | int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); |
| 5062 | struct ArSwitch *pEnd = &aSwitch[nSwitch]; |
| 5063 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5064 | if( nArg<=1 ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5065 | return arUsage(stderr); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5066 | }else{ |
| 5067 | char *z = azArg[1]; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5068 | if( z[0]!='-' ){ |
| 5069 | /* Traditional style [tar] invocation */ |
| 5070 | int i; |
| 5071 | int iArg = 2; |
| 5072 | for(i=0; z[i]; i++){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5073 | const char *zArg = 0; |
| 5074 | struct ArSwitch *pOpt; |
| 5075 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 5076 | if( z[i]==pOpt->cShort ) break; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5077 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5078 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5079 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5080 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5081 | if( pOpt->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5082 | if( iArg>=nArg ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5083 | return arErrorMsg(pAr, "option requires an argument: %c",z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5084 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5085 | zArg = azArg[iArg++]; |
| 5086 | } |
| 5087 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5088 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5089 | pAr->nArg = nArg-iArg; |
| 5090 | if( pAr->nArg>0 ){ |
| 5091 | pAr->azArg = &azArg[iArg]; |
| 5092 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5093 | }else{ |
| 5094 | /* Non-traditional invocation */ |
| 5095 | int iArg; |
| 5096 | for(iArg=1; iArg<nArg; iArg++){ |
| 5097 | int n; |
| 5098 | z = azArg[iArg]; |
| 5099 | if( z[0]!='-' ){ |
| 5100 | /* All remaining command line words are command arguments. */ |
| 5101 | pAr->azArg = &azArg[iArg]; |
| 5102 | pAr->nArg = nArg-iArg; |
| 5103 | break; |
| 5104 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5105 | n = strlen30(z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5106 | |
| 5107 | if( z[1]!='-' ){ |
| 5108 | int i; |
| 5109 | /* One or more short options */ |
| 5110 | for(i=1; i<n; i++){ |
| 5111 | const char *zArg = 0; |
| 5112 | struct ArSwitch *pOpt; |
| 5113 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 5114 | if( z[i]==pOpt->cShort ) break; |
| 5115 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5116 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5117 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5118 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5119 | if( pOpt->bArg ){ |
| 5120 | if( i<(n-1) ){ |
| 5121 | zArg = &z[i+1]; |
| 5122 | i = n; |
| 5123 | }else{ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5124 | if( iArg>=(nArg-1) ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5125 | return arErrorMsg(pAr, "option requires an argument: %c",z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5126 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5127 | zArg = azArg[++iArg]; |
| 5128 | } |
| 5129 | } |
| 5130 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
| 5131 | } |
| 5132 | }else if( z[2]=='\0' ){ |
| 5133 | /* A -- option, indicating that all remaining command line words |
| 5134 | ** are command arguments. */ |
| 5135 | pAr->azArg = &azArg[iArg+1]; |
| 5136 | pAr->nArg = nArg-iArg-1; |
| 5137 | break; |
| 5138 | }else{ |
| 5139 | /* A long option */ |
| 5140 | const char *zArg = 0; /* Argument for option, if any */ |
| 5141 | struct ArSwitch *pMatch = 0; /* Matching option */ |
| 5142 | struct ArSwitch *pOpt; /* Iterator */ |
| 5143 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 5144 | const char *zLong = pOpt->zLong; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5145 | if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5146 | if( pMatch ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5147 | return arErrorMsg(pAr, "ambiguous option: %s",z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5148 | }else{ |
| 5149 | pMatch = pOpt; |
| 5150 | } |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | if( pMatch==0 ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5155 | return arErrorMsg(pAr, "unrecognized option: %s", z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5156 | } |
| 5157 | if( pMatch->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5158 | if( iArg>=(nArg-1) ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5159 | return arErrorMsg(pAr, "option requires an argument: %s", z); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5160 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5161 | zArg = azArg[++iArg]; |
| 5162 | } |
| 5163 | if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; |
| 5164 | } |
| 5165 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | return SQLITE_OK; |
| 5170 | } |
| 5171 | |
| 5172 | /* |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5173 | ** This function assumes that all arguments within the ArCommand.azArg[] |
| 5174 | ** array refer to archive members, as for the --extract or --list commands. |
| 5175 | ** It checks that each of them are present. If any specified file is not |
| 5176 | ** present in the archive, an error is printed to stderr and an error |
| 5177 | ** code returned. Otherwise, if all specified arguments are present in |
| 5178 | ** the archive, SQLITE_OK is returned. |
| 5179 | ** |
| 5180 | ** This function strips any trailing '/' characters from each argument. |
| 5181 | ** This is consistent with the way the [tar] command seems to work on |
| 5182 | ** Linux. |
| 5183 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5184 | static int arCheckEntries(ArCommand *pAr){ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5185 | int rc = SQLITE_OK; |
| 5186 | if( pAr->nArg ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5187 | int i, j; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5188 | sqlite3_stmt *pTest = 0; |
| 5189 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5190 | shellPreparePrintf(pAr->db, &rc, &pTest, |
| 5191 | "SELECT name FROM %s WHERE name=$name", |
| 5192 | pAr->zSrcTable |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5193 | ); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5194 | j = sqlite3_bind_parameter_index(pTest, "$name"); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5195 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
| 5196 | char *z = pAr->azArg[i]; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5197 | int n = strlen30(z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5198 | int bOk = 0; |
| 5199 | while( n>0 && z[n-1]=='/' ) n--; |
| 5200 | z[n] = '\0'; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5201 | sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5202 | if( SQLITE_ROW==sqlite3_step(pTest) ){ |
| 5203 | bOk = 1; |
| 5204 | } |
| 5205 | shellReset(&rc, pTest); |
| 5206 | if( rc==SQLITE_OK && bOk==0 ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5207 | utf8_printf(stderr, "not found in archive: %s\n", z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5208 | rc = SQLITE_ERROR; |
| 5209 | } |
| 5210 | } |
| 5211 | shellFinalize(&rc, pTest); |
| 5212 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5213 | return rc; |
| 5214 | } |
| 5215 | |
| 5216 | /* |
| 5217 | ** Format a WHERE clause that can be used against the "sqlar" table to |
| 5218 | ** identify all archive members that match the command arguments held |
| 5219 | ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. |
| 5220 | ** The caller is responsible for eventually calling sqlite3_free() on |
| 5221 | ** any non-NULL (*pzWhere) value. |
| 5222 | */ |
| 5223 | static void arWhereClause( |
| 5224 | int *pRc, |
| 5225 | ArCommand *pAr, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5226 | char **pzWhere /* OUT: New WHERE clause */ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5227 | ){ |
| 5228 | char *zWhere = 0; |
| 5229 | if( *pRc==SQLITE_OK ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5230 | if( pAr->nArg==0 ){ |
| 5231 | zWhere = sqlite3_mprintf("1"); |
| 5232 | }else{ |
| 5233 | int i; |
| 5234 | const char *zSep = ""; |
| 5235 | for(i=0; i<pAr->nArg; i++){ |
| 5236 | const char *z = pAr->azArg[i]; |
| 5237 | zWhere = sqlite3_mprintf( |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5238 | "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", |
| 5239 | zWhere, zSep, z, strlen30(z)+1, z |
| 5240 | ); |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5241 | if( zWhere==0 ){ |
| 5242 | *pRc = SQLITE_NOMEM; |
| 5243 | break; |
| 5244 | } |
| 5245 | zSep = " OR "; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5246 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5247 | } |
| 5248 | } |
| 5249 | *pzWhere = zWhere; |
| 5250 | } |
| 5251 | |
| 5252 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5253 | ** Implementation of .ar "lisT" command. |
| 5254 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5255 | static int arListCommand(ArCommand *pAr){ |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5256 | const char *zSql = "SELECT %s FROM %s WHERE %s"; |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5257 | const char *azCols[] = { |
| 5258 | "name", |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5259 | "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5260 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5261 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5262 | char *zWhere = 0; |
| 5263 | sqlite3_stmt *pSql = 0; |
| 5264 | int rc; |
| 5265 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5266 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5267 | arWhereClause(&rc, pAr, &zWhere); |
| 5268 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5269 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], |
| 5270 | pAr->zSrcTable, zWhere); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5271 | if( pAr->bDryRun ){ |
| 5272 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 5273 | }else{ |
| 5274 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 5275 | if( pAr->bVerbose ){ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5276 | utf8_printf(pAr->p->out, "%s % 10d %s %s\n", |
| 5277 | sqlite3_column_text(pSql, 0), |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5278 | sqlite3_column_int(pSql, 1), |
| 5279 | sqlite3_column_text(pSql, 2), |
| 5280 | sqlite3_column_text(pSql, 3) |
| 5281 | ); |
| 5282 | }else{ |
| 5283 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 5284 | } |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 5285 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5286 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5287 | shellFinalize(&rc, pSql); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5288 | sqlite3_free(zWhere); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5289 | return rc; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5293 | /* |
| 5294 | ** Implementation of .ar "eXtract" command. |
| 5295 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5296 | static int arExtractCommand(ArCommand *pAr){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5297 | const char *zSql1 = |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 5298 | "SELECT " |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5299 | " ($dir || name)," |
| 5300 | " writefile(($dir || name), %s, mode, mtime) " |
drh | 0cfd46a | 2018-06-06 01:18:01 +0000 | [diff] [blame] | 5301 | "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" |
| 5302 | " AND name NOT GLOB '*..[/\\]*'"; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5303 | |
| 5304 | const char *azExtraArg[] = { |
| 5305 | "sqlar_uncompress(data, sz)", |
dan | 7c15ac1 | 2018-01-08 19:59:59 +0000 | [diff] [blame] | 5306 | "data" |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5307 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5308 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5309 | sqlite3_stmt *pSql = 0; |
| 5310 | int rc = SQLITE_OK; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5311 | char *zDir = 0; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5312 | char *zWhere = 0; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5313 | int i, j; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5314 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5315 | /* If arguments are specified, check that they actually exist within |
| 5316 | ** the archive before proceeding. And formulate a WHERE clause to |
| 5317 | ** match them. */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5318 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5319 | arWhereClause(&rc, pAr, &zWhere); |
| 5320 | |
| 5321 | if( rc==SQLITE_OK ){ |
| 5322 | if( pAr->zDir ){ |
| 5323 | zDir = sqlite3_mprintf("%s/", pAr->zDir); |
| 5324 | }else{ |
| 5325 | zDir = sqlite3_mprintf(""); |
| 5326 | } |
| 5327 | if( zDir==0 ) rc = SQLITE_NOMEM; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5328 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5329 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5330 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, |
| 5331 | azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5332 | ); |
| 5333 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5334 | if( rc==SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5335 | j = sqlite3_bind_parameter_index(pSql, "$dir"); |
| 5336 | sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5337 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5338 | /* Run the SELECT statement twice. The first time, writefile() is called |
| 5339 | ** for all archive members that should be extracted. The second time, |
| 5340 | ** only for the directories. This is because the timestamps for |
| 5341 | ** extracted directories must be reset after they are populated (as |
| 5342 | ** populating them changes the timestamp). */ |
| 5343 | for(i=0; i<2; i++){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5344 | j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); |
| 5345 | sqlite3_bind_int(pSql, j, i); |
| 5346 | if( pAr->bDryRun ){ |
| 5347 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 5348 | }else{ |
| 5349 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 5350 | if( i==0 && pAr->bVerbose ){ |
| 5351 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 5352 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5353 | } |
| 5354 | } |
| 5355 | shellReset(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5356 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 5357 | shellFinalize(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5358 | } |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 5359 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 5360 | sqlite3_free(zDir); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 5361 | sqlite3_free(zWhere); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5362 | return rc; |
| 5363 | } |
| 5364 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5365 | /* |
| 5366 | ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. |
| 5367 | */ |
| 5368 | static int arExecSql(ArCommand *pAr, const char *zSql){ |
| 5369 | int rc; |
| 5370 | if( pAr->bDryRun ){ |
| 5371 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 5372 | rc = SQLITE_OK; |
| 5373 | }else{ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5374 | char *zErr = 0; |
| 5375 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 5376 | if( zErr ){ |
| 5377 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 5378 | sqlite3_free(zErr); |
| 5379 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5380 | } |
| 5381 | return rc; |
| 5382 | } |
| 5383 | |
dan | 1ad3f61 | 2017-12-11 20:22:02 +0000 | [diff] [blame] | 5384 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5385 | /* |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5386 | ** Implementation of .ar "create" and "update" commands. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5387 | ** |
| 5388 | ** Create the "sqlar" table in the database if it does not already exist. |
| 5389 | ** Then add each file in the azFile[] array to the archive. Directories |
| 5390 | ** are added recursively. If argument bVerbose is non-zero, a message is |
| 5391 | ** printed on stdout for each file archived. |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5392 | ** |
| 5393 | ** The create command is the same as update, except that it drops |
| 5394 | ** any existing "sqlar" table before beginning. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5395 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5396 | static int arCreateOrUpdateCommand( |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5397 | ArCommand *pAr, /* Command arguments and options */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5398 | int bUpdate /* true for a --create. false for --update */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5399 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5400 | const char *zCreate = |
drh | afba180 | 2018-01-06 15:49:57 +0000 | [diff] [blame] | 5401 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 5402 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 5403 | " mode INT, -- access permissions\n" |
| 5404 | " mtime INT, -- last modification time\n" |
| 5405 | " sz INT, -- original file size\n" |
| 5406 | " data BLOB -- compressed content\n" |
| 5407 | ")"; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5408 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5409 | const char *zInsertFmt[2] = { |
| 5410 | "REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5411 | " SELECT\n" |
| 5412 | " %s,\n" |
| 5413 | " mode,\n" |
| 5414 | " mtime,\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 5415 | " CASE substr(lsmode(mode),1,1)\n" |
| 5416 | " WHEN '-' THEN length(data)\n" |
| 5417 | " WHEN 'd' THEN 0\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5418 | " ELSE -1 END,\n" |
drh | 69d2d35 | 2018-03-09 22:18:53 +0000 | [diff] [blame] | 5419 | " sqlar_compress(data)\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5420 | " FROM fsdir(%Q,%Q)\n" |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5421 | " WHERE lsmode(mode) NOT LIKE '?%%';", |
| 5422 | "REPLACE INTO %s(name,mode,mtime,data)\n" |
| 5423 | " SELECT\n" |
| 5424 | " %s,\n" |
| 5425 | " mode,\n" |
| 5426 | " mtime,\n" |
| 5427 | " data\n" |
| 5428 | " FROM fsdir(%Q,%Q)\n" |
| 5429 | " WHERE lsmode(mode) NOT LIKE '?%%';" |
| 5430 | }; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5431 | int i; /* For iterating through azFile[] */ |
| 5432 | int rc; /* Return code */ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5433 | const char *zTab = 0; /* SQL table into which to insert */ |
| 5434 | char *zSql; |
| 5435 | char zTemp[50]; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5436 | |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5437 | arExecSql(pAr, "PRAGMA page_size=512"); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5438 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5439 | if( rc!=SQLITE_OK ) return rc; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5440 | zTemp[0] = 0; |
| 5441 | if( pAr->bZip ){ |
| 5442 | /* Initialize the zipfile virtual table, if necessary */ |
| 5443 | if( pAr->zFile ){ |
| 5444 | sqlite3_uint64 r; |
| 5445 | sqlite3_randomness(sizeof(r),&r); |
| 5446 | sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
| 5447 | zTab = zTemp; |
| 5448 | zSql = sqlite3_mprintf( |
| 5449 | "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
| 5450 | zTab, pAr->zFile |
| 5451 | ); |
| 5452 | rc = arExecSql(pAr, zSql); |
| 5453 | sqlite3_free(zSql); |
| 5454 | }else{ |
| 5455 | zTab = "zip"; |
| 5456 | } |
| 5457 | }else{ |
| 5458 | /* Initialize the table for an SQLAR */ |
| 5459 | zTab = "sqlar"; |
| 5460 | if( bUpdate==0 ){ |
| 5461 | rc = arExecSql(pAr, zDrop); |
| 5462 | if( rc!=SQLITE_OK ) goto end_ar_transaction; |
| 5463 | } |
| 5464 | rc = arExecSql(pAr, zCreate); |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 5465 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5466 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 5467 | char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5468 | pAr->bVerbose ? "shell_putsnl(name)" : "name", |
| 5469 | pAr->azArg[i], pAr->zDir); |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 5470 | rc = arExecSql(pAr, zSql2); |
| 5471 | sqlite3_free(zSql2); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5472 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5473 | end_ar_transaction: |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5474 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5475 | arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5476 | }else{ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5477 | rc = arExecSql(pAr, "RELEASE ar;"); |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5478 | if( pAr->bZip && pAr->zFile ){ |
| 5479 | zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); |
| 5480 | arExecSql(pAr, zSql); |
| 5481 | sqlite3_free(zSql); |
| 5482 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5483 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5484 | return rc; |
| 5485 | } |
| 5486 | |
| 5487 | /* |
| 5488 | ** Implementation of ".ar" dot command. |
| 5489 | */ |
| 5490 | static int arDotCommand( |
| 5491 | ShellState *pState, /* Current shell tool state */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5492 | int fromCmdLine, /* True if -A command-line option, not .ar cmd */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5493 | char **azArg, /* Array of arguments passed to dot command */ |
| 5494 | int nArg /* Number of entries in azArg[] */ |
| 5495 | ){ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5496 | ArCommand cmd; |
| 5497 | int rc; |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 5498 | memset(&cmd, 0, sizeof(cmd)); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5499 | cmd.fromCmdLine = fromCmdLine; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5500 | rc = arParseCommand(azArg, nArg, &cmd); |
| 5501 | if( rc==SQLITE_OK ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5502 | int eDbType = SHELL_OPEN_UNSPEC; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5503 | cmd.p = pState; |
| 5504 | cmd.db = pState->db; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5505 | if( cmd.zFile ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5506 | eDbType = deduceDatabaseType(cmd.zFile, 1); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5507 | }else{ |
| 5508 | eDbType = pState->openMode; |
| 5509 | } |
| 5510 | if( eDbType==SHELL_OPEN_ZIPFILE ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 5511 | if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ |
| 5512 | if( cmd.zFile==0 ){ |
| 5513 | cmd.zSrcTable = sqlite3_mprintf("zip"); |
| 5514 | }else{ |
| 5515 | cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); |
| 5516 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5517 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5518 | cmd.bZip = 1; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 5519 | }else if( cmd.zFile ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5520 | int flags; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5521 | if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5522 | if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){ |
| 5523 | flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 5524 | }else{ |
| 5525 | flags = SQLITE_OPEN_READONLY; |
| 5526 | } |
drh | a82c95b | 2018-01-10 14:00:00 +0000 | [diff] [blame] | 5527 | cmd.db = 0; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5528 | if( cmd.bDryRun ){ |
| 5529 | utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, |
| 5530 | eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); |
| 5531 | } |
| 5532 | rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, |
| 5533 | eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5534 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5535 | utf8_printf(stderr, "cannot open file: %s (%s)\n", |
| 5536 | cmd.zFile, sqlite3_errmsg(cmd.db) |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5537 | ); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5538 | goto end_ar_command; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5539 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5540 | sqlite3_fileio_init(cmd.db, 0, 0); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5541 | sqlite3_sqlar_init(cmd.db, 0, 0); |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 5542 | sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, |
| 5543 | shellPutsFunc, 0, 0); |
| 5544 | |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5545 | } |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5546 | if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5547 | if( cmd.eCmd!=AR_CMD_CREATE |
| 5548 | && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) |
| 5549 | ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5550 | utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); |
| 5551 | rc = SQLITE_ERROR; |
| 5552 | goto end_ar_command; |
| 5553 | } |
| 5554 | cmd.zSrcTable = sqlite3_mprintf("sqlar"); |
| 5555 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 5556 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5557 | switch( cmd.eCmd ){ |
| 5558 | case AR_CMD_CREATE: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5559 | rc = arCreateOrUpdateCommand(&cmd, 0); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5560 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5561 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5562 | case AR_CMD_EXTRACT: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5563 | rc = arExtractCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5564 | break; |
| 5565 | |
| 5566 | case AR_CMD_LIST: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5567 | rc = arListCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5568 | break; |
| 5569 | |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 5570 | case AR_CMD_HELP: |
| 5571 | arUsage(pState->out); |
| 5572 | break; |
| 5573 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5574 | default: |
| 5575 | assert( cmd.eCmd==AR_CMD_UPDATE ); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 5576 | rc = arCreateOrUpdateCommand(&cmd, 1); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5577 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5578 | } |
| 5579 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5580 | end_ar_command: |
| 5581 | if( cmd.db!=pState->db ){ |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5582 | close_db(cmd.db); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 5583 | } |
| 5584 | sqlite3_free(cmd.zSrcTable); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5585 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 5586 | return rc; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5587 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 5588 | /* End of the ".archive" or ".ar" command logic |
| 5589 | **********************************************************************************/ |
| 5590 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5591 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5592 | |
| 5593 | /* |
| 5594 | ** If an input line begins with "." then invoke this routine to |
| 5595 | ** process that line. |
| 5596 | ** |
| 5597 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 5598 | */ |
| 5599 | static int do_meta_command(char *zLine, ShellState *p){ |
| 5600 | int h = 1; |
| 5601 | int nArg = 0; |
| 5602 | int n, c; |
| 5603 | int rc = 0; |
| 5604 | char *azArg[50]; |
| 5605 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5606 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5607 | if( p->expert.pExpert ){ |
| 5608 | expertFinish(p, 1, 0); |
| 5609 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 5610 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 5611 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5612 | /* Parse the input line into tokens. |
| 5613 | */ |
| 5614 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 5615 | while( IsSpace(zLine[h]) ){ h++; } |
| 5616 | if( zLine[h]==0 ) break; |
| 5617 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 5618 | int delim = zLine[h++]; |
| 5619 | azArg[nArg++] = &zLine[h]; |
| 5620 | while( zLine[h] && zLine[h]!=delim ){ |
| 5621 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
| 5622 | h++; |
| 5623 | } |
| 5624 | if( zLine[h]==delim ){ |
| 5625 | zLine[h++] = 0; |
| 5626 | } |
| 5627 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
| 5628 | }else{ |
| 5629 | azArg[nArg++] = &zLine[h]; |
| 5630 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 5631 | if( zLine[h] ) zLine[h++] = 0; |
| 5632 | resolve_backslashes(azArg[nArg-1]); |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | /* Process the input line. |
| 5637 | */ |
| 5638 | if( nArg==0 ) return 0; /* no tokens, no error */ |
| 5639 | n = strlen30(azArg[0]); |
| 5640 | c = azArg[0][0]; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5641 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5642 | |
| 5643 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 5644 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 5645 | if( nArg!=2 ){ |
| 5646 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 5647 | rc = 1; |
| 5648 | goto meta_command_exit; |
| 5649 | } |
| 5650 | open_db(p, 0); |
| 5651 | if( booleanValue(azArg[1]) ){ |
| 5652 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 5653 | }else{ |
| 5654 | sqlite3_set_authorizer(p->db, 0, 0); |
| 5655 | } |
| 5656 | }else |
| 5657 | #endif |
| 5658 | |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 5659 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 5660 | if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5661 | open_db(p, 0); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 5662 | rc = arDotCommand(p, 0, azArg, nArg); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 5663 | }else |
| 5664 | #endif |
| 5665 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5666 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 5667 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 5668 | ){ |
| 5669 | const char *zDestFile = 0; |
| 5670 | const char *zDb = 0; |
| 5671 | sqlite3 *pDest; |
| 5672 | sqlite3_backup *pBackup; |
| 5673 | int j; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 5674 | const char *zVfs = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5675 | for(j=1; j<nArg; j++){ |
| 5676 | const char *z = azArg[j]; |
| 5677 | if( z[0]=='-' ){ |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 5678 | if( z[1]=='-' ) z++; |
| 5679 | if( strcmp(z, "-append")==0 ){ |
| 5680 | zVfs = "apndvfs"; |
| 5681 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5682 | { |
| 5683 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| 5684 | return 1; |
| 5685 | } |
| 5686 | }else if( zDestFile==0 ){ |
| 5687 | zDestFile = azArg[j]; |
| 5688 | }else if( zDb==0 ){ |
| 5689 | zDb = zDestFile; |
| 5690 | zDestFile = azArg[j]; |
| 5691 | }else{ |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 5692 | raw_printf(stderr, "Usage: .backup ?DB? ?--append? FILENAME\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5693 | return 1; |
| 5694 | } |
| 5695 | } |
| 5696 | if( zDestFile==0 ){ |
| 5697 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
| 5698 | return 1; |
| 5699 | } |
| 5700 | if( zDb==0 ) zDb = "main"; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 5701 | rc = sqlite3_open_v2(zDestFile, &pDest, |
| 5702 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5703 | if( rc!=SQLITE_OK ){ |
| 5704 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5705 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5706 | return 1; |
| 5707 | } |
| 5708 | open_db(p, 0); |
| 5709 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 5710 | if( pBackup==0 ){ |
| 5711 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5712 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5713 | return 1; |
| 5714 | } |
| 5715 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 5716 | sqlite3_backup_finish(pBackup); |
| 5717 | if( rc==SQLITE_DONE ){ |
| 5718 | rc = 0; |
| 5719 | }else{ |
| 5720 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 5721 | rc = 1; |
| 5722 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5723 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5724 | }else |
| 5725 | |
| 5726 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 5727 | if( nArg==2 ){ |
| 5728 | bail_on_error = booleanValue(azArg[1]); |
| 5729 | }else{ |
| 5730 | raw_printf(stderr, "Usage: .bail on|off\n"); |
| 5731 | rc = 1; |
| 5732 | } |
| 5733 | }else |
| 5734 | |
| 5735 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 5736 | if( nArg==2 ){ |
| 5737 | if( booleanValue(azArg[1]) ){ |
| 5738 | setBinaryMode(p->out, 1); |
| 5739 | }else{ |
| 5740 | setTextMode(p->out, 1); |
| 5741 | } |
| 5742 | }else{ |
| 5743 | raw_printf(stderr, "Usage: .binary on|off\n"); |
| 5744 | rc = 1; |
| 5745 | } |
| 5746 | }else |
| 5747 | |
| 5748 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 5749 | if( nArg==2 ){ |
| 5750 | #if defined(_WIN32) || defined(WIN32) |
| 5751 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 5752 | rc = !SetCurrentDirectoryW(z); |
| 5753 | sqlite3_free(z); |
| 5754 | #else |
| 5755 | rc = chdir(azArg[1]); |
| 5756 | #endif |
| 5757 | if( rc ){ |
| 5758 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 5759 | rc = 1; |
| 5760 | } |
| 5761 | }else{ |
| 5762 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 5763 | rc = 1; |
| 5764 | } |
| 5765 | }else |
| 5766 | |
| 5767 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 5768 | ** routine named test_breakpoint(). |
| 5769 | */ |
| 5770 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 5771 | test_breakpoint(); |
| 5772 | }else |
| 5773 | |
| 5774 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 5775 | if( nArg==2 ){ |
| 5776 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 5777 | }else{ |
| 5778 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 5779 | rc = 1; |
| 5780 | } |
| 5781 | }else |
| 5782 | |
| 5783 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 5784 | ** Then read the content of the testcase-out.txt file and compare against |
| 5785 | ** azArg[1]. If there are differences, report an error and exit. |
| 5786 | */ |
| 5787 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 5788 | char *zRes = 0; |
| 5789 | output_reset(p); |
| 5790 | if( nArg!=2 ){ |
| 5791 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
| 5792 | rc = 2; |
| 5793 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
| 5794 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 5795 | rc = 2; |
| 5796 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
| 5797 | utf8_printf(stderr, |
| 5798 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 5799 | p->zTestcase, azArg[1], zRes); |
drh | f30d345 | 2017-10-17 13:44:46 +0000 | [diff] [blame] | 5800 | rc = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5801 | }else{ |
| 5802 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 5803 | p->nCheck++; |
| 5804 | } |
| 5805 | sqlite3_free(zRes); |
| 5806 | }else |
| 5807 | |
| 5808 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 5809 | if( nArg==2 ){ |
| 5810 | tryToClone(p, azArg[1]); |
| 5811 | }else{ |
| 5812 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 5813 | rc = 1; |
| 5814 | } |
| 5815 | }else |
| 5816 | |
| 5817 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
| 5818 | ShellState data; |
| 5819 | char *zErrMsg = 0; |
| 5820 | open_db(p, 0); |
| 5821 | memcpy(&data, p, sizeof(data)); |
| 5822 | data.showHeader = 0; |
| 5823 | data.cMode = data.mode = MODE_List; |
| 5824 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
| 5825 | data.cnt = 0; |
| 5826 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 5827 | callback, &data, &zErrMsg); |
| 5828 | if( zErrMsg ){ |
| 5829 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 5830 | sqlite3_free(zErrMsg); |
| 5831 | rc = 1; |
| 5832 | } |
| 5833 | }else |
| 5834 | |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 5835 | if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ |
| 5836 | static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = { |
| 5837 | { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, |
| 5838 | { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, |
| 5839 | { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, |
| 5840 | { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, |
| 5841 | { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, |
| 5842 | { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, |
| 5843 | { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, |
| 5844 | { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, |
| 5845 | }; |
| 5846 | int ii, v; |
| 5847 | open_db(p, 0); |
| 5848 | for(ii=0; ii<ArraySize(aDbConfig); ii++){ |
| 5849 | if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; |
| 5850 | if( nArg>=3 ){ |
| 5851 | sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); |
| 5852 | } |
| 5853 | sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); |
| 5854 | utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); |
| 5855 | if( nArg>1 ) break; |
| 5856 | } |
| 5857 | if( nArg>1 && ii==ArraySize(aDbConfig) ){ |
| 5858 | utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); |
| 5859 | utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); |
| 5860 | } |
| 5861 | }else |
| 5862 | |
| 5863 | if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5864 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 5865 | }else |
| 5866 | |
| 5867 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 5868 | const char *zLike = 0; |
| 5869 | int i; |
| 5870 | int savedShowHeader = p->showHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 5871 | int savedShellFlags = p->shellFlgs; |
| 5872 | ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5873 | for(i=1; i<nArg; i++){ |
| 5874 | if( azArg[i][0]=='-' ){ |
| 5875 | const char *z = azArg[i]+1; |
| 5876 | if( z[0]=='-' ) z++; |
| 5877 | if( strcmp(z,"preserve-rowids")==0 ){ |
| 5878 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 5879 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 5880 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 5881 | rc = 1; |
| 5882 | goto meta_command_exit; |
| 5883 | #else |
| 5884 | ShellSetFlag(p, SHFLG_PreserveRowid); |
| 5885 | #endif |
| 5886 | }else |
| 5887 | if( strcmp(z,"newlines")==0 ){ |
| 5888 | ShellSetFlag(p, SHFLG_Newlines); |
| 5889 | }else |
| 5890 | { |
| 5891 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 5892 | rc = 1; |
| 5893 | goto meta_command_exit; |
| 5894 | } |
| 5895 | }else if( zLike ){ |
| 5896 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " |
| 5897 | "?--newlines? ?LIKE-PATTERN?\n"); |
| 5898 | rc = 1; |
| 5899 | goto meta_command_exit; |
| 5900 | }else{ |
| 5901 | zLike = azArg[i]; |
| 5902 | } |
| 5903 | } |
| 5904 | open_db(p, 0); |
| 5905 | /* When playing back a "dump", the content might appear in an order |
| 5906 | ** which causes immediate foreign key constraints to be violated. |
| 5907 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 5908 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 5909 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 5910 | p->writableSchema = 0; |
| 5911 | p->showHeader = 0; |
| 5912 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 5913 | ** as much of the schema as it can even if the sqlite_master table is |
| 5914 | ** corrupt. */ |
| 5915 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 5916 | p->nErr = 0; |
| 5917 | if( zLike==0 ){ |
| 5918 | run_schema_dump_query(p, |
| 5919 | "SELECT name, type, sql FROM sqlite_master " |
| 5920 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
| 5921 | ); |
| 5922 | run_schema_dump_query(p, |
| 5923 | "SELECT name, type, sql FROM sqlite_master " |
| 5924 | "WHERE name=='sqlite_sequence'" |
| 5925 | ); |
| 5926 | run_table_dump_query(p, |
| 5927 | "SELECT sql FROM sqlite_master " |
| 5928 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
| 5929 | ); |
| 5930 | }else{ |
| 5931 | char *zSql; |
| 5932 | zSql = sqlite3_mprintf( |
| 5933 | "SELECT name, type, sql FROM sqlite_master " |
| 5934 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 5935 | " AND sql NOT NULL", zLike); |
| 5936 | run_schema_dump_query(p,zSql); |
| 5937 | sqlite3_free(zSql); |
| 5938 | zSql = sqlite3_mprintf( |
| 5939 | "SELECT sql FROM sqlite_master " |
| 5940 | "WHERE sql NOT NULL" |
| 5941 | " AND type IN ('index','trigger','view')" |
| 5942 | " AND tbl_name LIKE %Q", zLike); |
| 5943 | run_table_dump_query(p, zSql, 0); |
| 5944 | sqlite3_free(zSql); |
| 5945 | } |
| 5946 | if( p->writableSchema ){ |
| 5947 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 5948 | p->writableSchema = 0; |
| 5949 | } |
| 5950 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5951 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
| 5952 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
| 5953 | p->showHeader = savedShowHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 5954 | p->shellFlgs = savedShellFlags; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5955 | }else |
| 5956 | |
| 5957 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 5958 | if( nArg==2 ){ |
| 5959 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
| 5960 | }else{ |
| 5961 | raw_printf(stderr, "Usage: .echo on|off\n"); |
| 5962 | rc = 1; |
| 5963 | } |
| 5964 | }else |
| 5965 | |
| 5966 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 5967 | if( nArg==2 ){ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 5968 | p->autoEQPtest = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5969 | if( strcmp(azArg[1],"full")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 5970 | p->autoEQP = AUTOEQP_full; |
| 5971 | }else if( strcmp(azArg[1],"trigger")==0 ){ |
| 5972 | p->autoEQP = AUTOEQP_trigger; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 5973 | }else if( strcmp(azArg[1],"test")==0 ){ |
| 5974 | p->autoEQP = AUTOEQP_on; |
| 5975 | p->autoEQPtest = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5976 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 5977 | p->autoEQP = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5978 | } |
| 5979 | }else{ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 5980 | raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5981 | rc = 1; |
| 5982 | } |
| 5983 | }else |
| 5984 | |
| 5985 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 5986 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 5987 | rc = 2; |
| 5988 | }else |
| 5989 | |
| 5990 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 5991 | ** retained purely for backwards compatibility */ |
| 5992 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 5993 | int val = 1; |
| 5994 | if( nArg>=2 ){ |
| 5995 | if( strcmp(azArg[1],"auto")==0 ){ |
| 5996 | val = 99; |
| 5997 | }else{ |
| 5998 | val = booleanValue(azArg[1]); |
| 5999 | } |
| 6000 | } |
| 6001 | if( val==1 && p->mode!=MODE_Explain ){ |
| 6002 | p->normalMode = p->mode; |
| 6003 | p->mode = MODE_Explain; |
| 6004 | p->autoExplain = 0; |
| 6005 | }else if( val==0 ){ |
| 6006 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 6007 | p->autoExplain = 0; |
| 6008 | }else if( val==99 ){ |
| 6009 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 6010 | p->autoExplain = 1; |
| 6011 | } |
| 6012 | }else |
| 6013 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 6014 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 6015 | if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ |
| 6016 | open_db(p, 0); |
| 6017 | expertDotCommand(p, azArg, nArg); |
| 6018 | }else |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 6019 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 6020 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6021 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
| 6022 | ShellState data; |
| 6023 | char *zErrMsg = 0; |
| 6024 | int doStats = 0; |
| 6025 | memcpy(&data, p, sizeof(data)); |
| 6026 | data.showHeader = 0; |
| 6027 | data.cMode = data.mode = MODE_Semi; |
| 6028 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 6029 | data.cMode = data.mode = MODE_Pretty; |
| 6030 | nArg = 1; |
| 6031 | } |
| 6032 | if( nArg!=1 ){ |
| 6033 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| 6034 | rc = 1; |
| 6035 | goto meta_command_exit; |
| 6036 | } |
| 6037 | open_db(p, 0); |
| 6038 | rc = sqlite3_exec(p->db, |
| 6039 | "SELECT sql FROM" |
| 6040 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 6041 | " FROM sqlite_master UNION ALL" |
| 6042 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| 6043 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
| 6044 | "ORDER BY rowid", |
| 6045 | callback, &data, &zErrMsg |
| 6046 | ); |
| 6047 | if( rc==SQLITE_OK ){ |
| 6048 | sqlite3_stmt *pStmt; |
| 6049 | rc = sqlite3_prepare_v2(p->db, |
| 6050 | "SELECT rowid FROM sqlite_master" |
| 6051 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 6052 | -1, &pStmt, 0); |
| 6053 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 6054 | sqlite3_finalize(pStmt); |
| 6055 | } |
| 6056 | if( doStats==0 ){ |
| 6057 | raw_printf(p->out, "/* No STAT tables available */\n"); |
| 6058 | }else{ |
| 6059 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 6060 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 6061 | callback, &data, &zErrMsg); |
| 6062 | data.cMode = data.mode = MODE_Insert; |
| 6063 | data.zDestTable = "sqlite_stat1"; |
drh | 4c54045 | 2018-05-08 23:17:36 +0000 | [diff] [blame] | 6064 | shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6065 | data.zDestTable = "sqlite_stat3"; |
drh | 4c54045 | 2018-05-08 23:17:36 +0000 | [diff] [blame] | 6066 | shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6067 | data.zDestTable = "sqlite_stat4"; |
drh | 4c54045 | 2018-05-08 23:17:36 +0000 | [diff] [blame] | 6068 | shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6069 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 6070 | } |
| 6071 | }else |
| 6072 | |
| 6073 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 6074 | if( nArg==2 ){ |
| 6075 | p->showHeader = booleanValue(azArg[1]); |
| 6076 | }else{ |
| 6077 | raw_printf(stderr, "Usage: .headers on|off\n"); |
| 6078 | rc = 1; |
| 6079 | } |
| 6080 | }else |
| 6081 | |
| 6082 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 6083 | utf8_printf(p->out, "%s", zHelp); |
| 6084 | }else |
| 6085 | |
| 6086 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
| 6087 | char *zTable; /* Insert data into this table */ |
| 6088 | char *zFile; /* Name of file to extra content from */ |
| 6089 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 6090 | int nCol; /* Number of columns in the table */ |
| 6091 | int nByte; /* Number of bytes in an SQL string */ |
| 6092 | int i, j; /* Loop counters */ |
| 6093 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 6094 | int nSep; /* Number of bytes in p->colSeparator[] */ |
| 6095 | char *zSql; /* An SQL statement */ |
| 6096 | ImportCtx sCtx; /* Reader context */ |
| 6097 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 6098 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
| 6099 | |
| 6100 | if( nArg!=3 ){ |
| 6101 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
| 6102 | goto meta_command_exit; |
| 6103 | } |
| 6104 | zFile = azArg[1]; |
| 6105 | zTable = azArg[2]; |
| 6106 | seenInterrupt = 0; |
| 6107 | memset(&sCtx, 0, sizeof(sCtx)); |
| 6108 | open_db(p, 0); |
| 6109 | nSep = strlen30(p->colSeparator); |
| 6110 | if( nSep==0 ){ |
| 6111 | raw_printf(stderr, |
| 6112 | "Error: non-null column separator required for import\n"); |
| 6113 | return 1; |
| 6114 | } |
| 6115 | if( nSep>1 ){ |
| 6116 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
| 6117 | " for import\n"); |
| 6118 | return 1; |
| 6119 | } |
| 6120 | nSep = strlen30(p->rowSeparator); |
| 6121 | if( nSep==0 ){ |
| 6122 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
| 6123 | return 1; |
| 6124 | } |
| 6125 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 6126 | /* When importing CSV (only), if the row separator is set to the |
| 6127 | ** default output row separator, change it to the default input |
| 6128 | ** row separator. This avoids having to maintain different input |
| 6129 | ** and output row separators. */ |
| 6130 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6131 | nSep = strlen30(p->rowSeparator); |
| 6132 | } |
| 6133 | if( nSep>1 ){ |
| 6134 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
| 6135 | " for import\n"); |
| 6136 | return 1; |
| 6137 | } |
| 6138 | sCtx.zFile = zFile; |
| 6139 | sCtx.nLine = 1; |
| 6140 | if( sCtx.zFile[0]=='|' ){ |
| 6141 | #ifdef SQLITE_OMIT_POPEN |
| 6142 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 6143 | return 1; |
| 6144 | #else |
| 6145 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 6146 | sCtx.zFile = "<pipe>"; |
| 6147 | xCloser = pclose; |
| 6148 | #endif |
| 6149 | }else{ |
| 6150 | sCtx.in = fopen(sCtx.zFile, "rb"); |
| 6151 | xCloser = fclose; |
| 6152 | } |
| 6153 | if( p->mode==MODE_Ascii ){ |
| 6154 | xRead = ascii_read_one_field; |
| 6155 | }else{ |
| 6156 | xRead = csv_read_one_field; |
| 6157 | } |
| 6158 | if( sCtx.in==0 ){ |
| 6159 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 6160 | return 1; |
| 6161 | } |
| 6162 | sCtx.cColSep = p->colSeparator[0]; |
| 6163 | sCtx.cRowSep = p->rowSeparator[0]; |
| 6164 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
| 6165 | if( zSql==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6166 | xCloser(sCtx.in); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 6167 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6168 | } |
| 6169 | nByte = strlen30(zSql); |
| 6170 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6171 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| 6172 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
| 6173 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 6174 | char cSep = '('; |
| 6175 | while( xRead(&sCtx) ){ |
| 6176 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
| 6177 | cSep = ','; |
| 6178 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
| 6179 | } |
| 6180 | if( cSep=='(' ){ |
| 6181 | sqlite3_free(zCreate); |
| 6182 | sqlite3_free(sCtx.z); |
| 6183 | xCloser(sCtx.in); |
| 6184 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
| 6185 | return 1; |
| 6186 | } |
| 6187 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 6188 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 6189 | sqlite3_free(zCreate); |
| 6190 | if( rc ){ |
| 6191 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
| 6192 | sqlite3_errmsg(p->db)); |
| 6193 | sqlite3_free(sCtx.z); |
| 6194 | xCloser(sCtx.in); |
| 6195 | return 1; |
| 6196 | } |
| 6197 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6198 | } |
| 6199 | sqlite3_free(zSql); |
| 6200 | if( rc ){ |
| 6201 | if (pStmt) sqlite3_finalize(pStmt); |
| 6202 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
| 6203 | xCloser(sCtx.in); |
| 6204 | return 1; |
| 6205 | } |
| 6206 | nCol = sqlite3_column_count(pStmt); |
| 6207 | sqlite3_finalize(pStmt); |
| 6208 | pStmt = 0; |
| 6209 | if( nCol==0 ) return 0; /* no columns, no error */ |
| 6210 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
| 6211 | if( zSql==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6212 | xCloser(sCtx.in); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 6213 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6214 | } |
| 6215 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
| 6216 | j = strlen30(zSql); |
| 6217 | for(i=1; i<nCol; i++){ |
| 6218 | zSql[j++] = ','; |
| 6219 | zSql[j++] = '?'; |
| 6220 | } |
| 6221 | zSql[j++] = ')'; |
| 6222 | zSql[j] = 0; |
| 6223 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6224 | sqlite3_free(zSql); |
| 6225 | if( rc ){ |
| 6226 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6227 | if (pStmt) sqlite3_finalize(pStmt); |
| 6228 | xCloser(sCtx.in); |
| 6229 | return 1; |
| 6230 | } |
| 6231 | needCommit = sqlite3_get_autocommit(p->db); |
| 6232 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 6233 | do{ |
| 6234 | int startLine = sCtx.nLine; |
| 6235 | for(i=0; i<nCol; i++){ |
| 6236 | char *z = xRead(&sCtx); |
| 6237 | /* |
| 6238 | ** Did we reach end-of-file before finding any columns? |
| 6239 | ** If so, stop instead of NULL filling the remaining columns. |
| 6240 | */ |
| 6241 | if( z==0 && i==0 ) break; |
| 6242 | /* |
| 6243 | ** Did we reach end-of-file OR end-of-line before finding any |
| 6244 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 6245 | ** the remaining columns. |
| 6246 | */ |
| 6247 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| 6248 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 6249 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 6250 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 6251 | "filling the rest with NULL\n", |
| 6252 | sCtx.zFile, startLine, nCol, i+1); |
| 6253 | i += 2; |
| 6254 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| 6255 | } |
| 6256 | } |
| 6257 | if( sCtx.cTerm==sCtx.cColSep ){ |
| 6258 | do{ |
| 6259 | xRead(&sCtx); |
| 6260 | i++; |
| 6261 | }while( sCtx.cTerm==sCtx.cColSep ); |
| 6262 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 6263 | "extras ignored\n", |
| 6264 | sCtx.zFile, startLine, nCol, i); |
| 6265 | } |
| 6266 | if( i>=nCol ){ |
| 6267 | sqlite3_step(pStmt); |
| 6268 | rc = sqlite3_reset(pStmt); |
| 6269 | if( rc!=SQLITE_OK ){ |
| 6270 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 6271 | startLine, sqlite3_errmsg(p->db)); |
| 6272 | } |
| 6273 | } |
| 6274 | }while( sCtx.cTerm!=EOF ); |
| 6275 | |
| 6276 | xCloser(sCtx.in); |
| 6277 | sqlite3_free(sCtx.z); |
| 6278 | sqlite3_finalize(pStmt); |
| 6279 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
| 6280 | }else |
| 6281 | |
| 6282 | #ifndef SQLITE_UNTESTABLE |
| 6283 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 6284 | char *zSql; |
| 6285 | char *zCollist = 0; |
| 6286 | sqlite3_stmt *pStmt; |
| 6287 | int tnum = 0; |
| 6288 | int i; |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 6289 | if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ |
| 6290 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" |
| 6291 | " .imposter off\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6292 | rc = 1; |
| 6293 | goto meta_command_exit; |
| 6294 | } |
| 6295 | open_db(p, 0); |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 6296 | if( nArg==2 ){ |
| 6297 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); |
| 6298 | goto meta_command_exit; |
| 6299 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6300 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 6301 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 6302 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6303 | sqlite3_free(zSql); |
| 6304 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6305 | tnum = sqlite3_column_int(pStmt, 0); |
| 6306 | } |
| 6307 | sqlite3_finalize(pStmt); |
| 6308 | if( tnum==0 ){ |
| 6309 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 6310 | rc = 1; |
| 6311 | goto meta_command_exit; |
| 6312 | } |
| 6313 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 6314 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6315 | sqlite3_free(zSql); |
| 6316 | i = 0; |
| 6317 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6318 | char zLabel[20]; |
| 6319 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
| 6320 | i++; |
| 6321 | if( zCol==0 ){ |
| 6322 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 6323 | zCol = "_ROWID_"; |
| 6324 | }else{ |
| 6325 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 6326 | zCol = zLabel; |
| 6327 | } |
| 6328 | } |
| 6329 | if( zCollist==0 ){ |
| 6330 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 6331 | }else{ |
| 6332 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 6333 | } |
| 6334 | } |
| 6335 | sqlite3_finalize(pStmt); |
| 6336 | zSql = sqlite3_mprintf( |
| 6337 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 6338 | azArg[2], zCollist, zCollist); |
| 6339 | sqlite3_free(zCollist); |
| 6340 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 6341 | if( rc==SQLITE_OK ){ |
| 6342 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 6343 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 6344 | if( rc ){ |
| 6345 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 6346 | }else{ |
| 6347 | utf8_printf(stdout, "%s;\n", zSql); |
| 6348 | raw_printf(stdout, |
| 6349 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 6350 | ); |
| 6351 | } |
| 6352 | }else{ |
| 6353 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
| 6354 | rc = 1; |
| 6355 | } |
| 6356 | sqlite3_free(zSql); |
| 6357 | }else |
| 6358 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 6359 | |
| 6360 | #ifdef SQLITE_ENABLE_IOTRACE |
| 6361 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
| 6362 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
| 6363 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 6364 | iotrace = 0; |
| 6365 | if( nArg<2 ){ |
| 6366 | sqlite3IoTrace = 0; |
| 6367 | }else if( strcmp(azArg[1], "-")==0 ){ |
| 6368 | sqlite3IoTrace = iotracePrintf; |
| 6369 | iotrace = stdout; |
| 6370 | }else{ |
| 6371 | iotrace = fopen(azArg[1], "w"); |
| 6372 | if( iotrace==0 ){ |
| 6373 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 6374 | sqlite3IoTrace = 0; |
| 6375 | rc = 1; |
| 6376 | }else{ |
| 6377 | sqlite3IoTrace = iotracePrintf; |
| 6378 | } |
| 6379 | } |
| 6380 | }else |
| 6381 | #endif |
| 6382 | |
| 6383 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 6384 | static const struct { |
| 6385 | const char *zLimitName; /* Name of a limit */ |
| 6386 | int limitCode; /* Integer code for that limit */ |
| 6387 | } aLimit[] = { |
| 6388 | { "length", SQLITE_LIMIT_LENGTH }, |
| 6389 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 6390 | { "column", SQLITE_LIMIT_COLUMN }, |
| 6391 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 6392 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 6393 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 6394 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 6395 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 6396 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 6397 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 6398 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 6399 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 6400 | }; |
| 6401 | int i, n2; |
| 6402 | open_db(p, 0); |
| 6403 | if( nArg==1 ){ |
| 6404 | for(i=0; i<ArraySize(aLimit); i++){ |
| 6405 | printf("%20s %d\n", aLimit[i].zLimitName, |
| 6406 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 6407 | } |
| 6408 | }else if( nArg>3 ){ |
| 6409 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
| 6410 | rc = 1; |
| 6411 | goto meta_command_exit; |
| 6412 | }else{ |
| 6413 | int iLimit = -1; |
| 6414 | n2 = strlen30(azArg[1]); |
| 6415 | for(i=0; i<ArraySize(aLimit); i++){ |
| 6416 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 6417 | if( iLimit<0 ){ |
| 6418 | iLimit = i; |
| 6419 | }else{ |
| 6420 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
| 6421 | rc = 1; |
| 6422 | goto meta_command_exit; |
| 6423 | } |
| 6424 | } |
| 6425 | } |
| 6426 | if( iLimit<0 ){ |
| 6427 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
| 6428 | "enter \".limits\" with no arguments for a list.\n", |
| 6429 | azArg[1]); |
| 6430 | rc = 1; |
| 6431 | goto meta_command_exit; |
| 6432 | } |
| 6433 | if( nArg==3 ){ |
| 6434 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 6435 | (int)integerValue(azArg[2])); |
| 6436 | } |
| 6437 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 6438 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 6439 | } |
| 6440 | }else |
| 6441 | |
| 6442 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
| 6443 | open_db(p, 0); |
| 6444 | lintDotCommand(p, azArg, nArg); |
| 6445 | }else |
| 6446 | |
| 6447 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6448 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
| 6449 | const char *zFile, *zProc; |
| 6450 | char *zErrMsg = 0; |
| 6451 | if( nArg<2 ){ |
| 6452 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
| 6453 | rc = 1; |
| 6454 | goto meta_command_exit; |
| 6455 | } |
| 6456 | zFile = azArg[1]; |
| 6457 | zProc = nArg>=3 ? azArg[2] : 0; |
| 6458 | open_db(p, 0); |
| 6459 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 6460 | if( rc!=SQLITE_OK ){ |
| 6461 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
| 6462 | sqlite3_free(zErrMsg); |
| 6463 | rc = 1; |
| 6464 | } |
| 6465 | }else |
| 6466 | #endif |
| 6467 | |
| 6468 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 6469 | if( nArg!=2 ){ |
| 6470 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 6471 | rc = 1; |
| 6472 | }else{ |
| 6473 | const char *zFile = azArg[1]; |
| 6474 | output_file_close(p->pLog); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6475 | p->pLog = output_file_open(zFile, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6476 | } |
| 6477 | }else |
| 6478 | |
| 6479 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 6480 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6481 | int n2 = strlen30(zMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6482 | int c2 = zMode[0]; |
| 6483 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
| 6484 | p->mode = MODE_Line; |
| 6485 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6486 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
| 6487 | p->mode = MODE_Column; |
| 6488 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6489 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
| 6490 | p->mode = MODE_List; |
| 6491 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 6492 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6493 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
| 6494 | p->mode = MODE_Html; |
| 6495 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
| 6496 | p->mode = MODE_Tcl; |
| 6497 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
| 6498 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 6499 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
| 6500 | p->mode = MODE_Csv; |
| 6501 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 6502 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 6503 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
| 6504 | p->mode = MODE_List; |
| 6505 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
| 6506 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
| 6507 | p->mode = MODE_Insert; |
| 6508 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
| 6509 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 6510 | p->mode = MODE_Quote; |
| 6511 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 6512 | p->mode = MODE_Ascii; |
| 6513 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 6514 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
| 6515 | }else if( nArg==1 ){ |
| 6516 | raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 6517 | }else{ |
| 6518 | raw_printf(stderr, "Error: mode should be one of: " |
| 6519 | "ascii column csv html insert line list quote tabs tcl\n"); |
| 6520 | rc = 1; |
| 6521 | } |
| 6522 | p->cMode = p->mode; |
| 6523 | }else |
| 6524 | |
| 6525 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 6526 | if( nArg==2 ){ |
| 6527 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 6528 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| 6529 | }else{ |
| 6530 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
| 6531 | rc = 1; |
| 6532 | } |
| 6533 | }else |
| 6534 | |
| 6535 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
| 6536 | char *zNewFilename; /* Name of the database file to open */ |
| 6537 | int iName = 1; /* Index in azArg[] of the filename */ |
| 6538 | int newFlag = 0; /* True to delete file before opening */ |
| 6539 | /* Close the existing database */ |
| 6540 | session_close_all(p); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 6541 | close_db(p->db); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6542 | p->db = 0; |
| 6543 | p->zDbFilename = 0; |
| 6544 | sqlite3_free(p->zFreeOnClose); |
| 6545 | p->zFreeOnClose = 0; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 6546 | p->openMode = SHELL_OPEN_UNSPEC; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6547 | /* Check for command-line arguments */ |
| 6548 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 6549 | const char *z = azArg[iName]; |
| 6550 | if( optionMatch(z,"new") ){ |
| 6551 | newFlag = 1; |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 6552 | #ifdef SQLITE_HAVE_ZLIB |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 6553 | }else if( optionMatch(z, "zip") ){ |
| 6554 | p->openMode = SHELL_OPEN_ZIPFILE; |
| 6555 | #endif |
| 6556 | }else if( optionMatch(z, "append") ){ |
| 6557 | p->openMode = SHELL_OPEN_APPENDVFS; |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 6558 | }else if( optionMatch(z, "readonly") ){ |
| 6559 | p->openMode = SHELL_OPEN_READONLY; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6560 | }else if( z[0]=='-' ){ |
| 6561 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 6562 | rc = 1; |
| 6563 | goto meta_command_exit; |
| 6564 | } |
| 6565 | } |
| 6566 | /* If a filename is specified, try to open it first */ |
| 6567 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 6568 | if( zNewFilename ){ |
| 6569 | if( newFlag ) shellDeleteFile(zNewFilename); |
| 6570 | p->zDbFilename = zNewFilename; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 6571 | open_db(p, OPEN_DB_KEEPALIVE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6572 | if( p->db==0 ){ |
| 6573 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 6574 | sqlite3_free(zNewFilename); |
| 6575 | }else{ |
| 6576 | p->zFreeOnClose = zNewFilename; |
| 6577 | } |
| 6578 | } |
| 6579 | if( p->db==0 ){ |
| 6580 | /* As a fall-back open a TEMP database */ |
| 6581 | p->zDbFilename = 0; |
| 6582 | open_db(p, 0); |
| 6583 | } |
| 6584 | }else |
| 6585 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6586 | if( (c=='o' |
| 6587 | && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) |
| 6588 | || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6589 | ){ |
| 6590 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6591 | int bTxtMode = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6592 | if( azArg[0][0]=='e' ){ |
| 6593 | /* Transform the ".excel" command into ".once -x" */ |
| 6594 | nArg = 2; |
| 6595 | azArg[0] = "once"; |
| 6596 | zFile = azArg[1] = "-x"; |
| 6597 | n = 4; |
| 6598 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6599 | if( nArg>2 ){ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6600 | utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6601 | rc = 1; |
| 6602 | goto meta_command_exit; |
| 6603 | } |
| 6604 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 6605 | if( nArg<2 ){ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6606 | raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6607 | rc = 1; |
| 6608 | goto meta_command_exit; |
| 6609 | } |
| 6610 | p->outCount = 2; |
| 6611 | }else{ |
| 6612 | p->outCount = 0; |
| 6613 | } |
| 6614 | output_reset(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6615 | if( zFile[0]=='-' && zFile[1]=='-' ) zFile++; |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 6616 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6617 | if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 6618 | p->doXdgOpen = 1; |
| 6619 | outputModePush(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6620 | if( zFile[1]=='x' ){ |
| 6621 | newTempFile(p, "csv"); |
| 6622 | p->mode = MODE_Csv; |
| 6623 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 6624 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 6625 | }else{ |
| 6626 | newTempFile(p, "txt"); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6627 | bTxtMode = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6628 | } |
| 6629 | zFile = p->zTempFile; |
| 6630 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 6631 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6632 | if( zFile[0]=='|' ){ |
| 6633 | #ifdef SQLITE_OMIT_POPEN |
| 6634 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 6635 | rc = 1; |
| 6636 | p->out = stdout; |
| 6637 | #else |
| 6638 | p->out = popen(zFile + 1, "w"); |
| 6639 | if( p->out==0 ){ |
| 6640 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
| 6641 | p->out = stdout; |
| 6642 | rc = 1; |
| 6643 | }else{ |
| 6644 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 6645 | } |
| 6646 | #endif |
| 6647 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 6648 | p->out = output_file_open(zFile, bTxtMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6649 | if( p->out==0 ){ |
| 6650 | if( strcmp(zFile,"off")!=0 ){ |
| 6651 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
| 6652 | } |
| 6653 | p->out = stdout; |
| 6654 | rc = 1; |
| 6655 | } else { |
| 6656 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 6657 | } |
| 6658 | } |
| 6659 | }else |
| 6660 | |
| 6661 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 6662 | int i; |
| 6663 | for(i=1; i<nArg; i++){ |
| 6664 | if( i>1 ) raw_printf(p->out, " "); |
| 6665 | utf8_printf(p->out, "%s", azArg[i]); |
| 6666 | } |
| 6667 | raw_printf(p->out, "\n"); |
| 6668 | }else |
| 6669 | |
| 6670 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
| 6671 | if( nArg >= 2) { |
| 6672 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 6673 | } |
| 6674 | if( nArg >= 3) { |
| 6675 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 6676 | } |
| 6677 | }else |
| 6678 | |
| 6679 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
| 6680 | rc = 2; |
| 6681 | }else |
| 6682 | |
| 6683 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 6684 | FILE *alt; |
| 6685 | if( nArg!=2 ){ |
| 6686 | raw_printf(stderr, "Usage: .read FILE\n"); |
| 6687 | rc = 1; |
| 6688 | goto meta_command_exit; |
| 6689 | } |
| 6690 | alt = fopen(azArg[1], "rb"); |
| 6691 | if( alt==0 ){ |
| 6692 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 6693 | rc = 1; |
| 6694 | }else{ |
| 6695 | rc = process_input(p, alt); |
| 6696 | fclose(alt); |
| 6697 | } |
| 6698 | }else |
| 6699 | |
| 6700 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
| 6701 | const char *zSrcFile; |
| 6702 | const char *zDb; |
| 6703 | sqlite3 *pSrc; |
| 6704 | sqlite3_backup *pBackup; |
| 6705 | int nTimeout = 0; |
| 6706 | |
| 6707 | if( nArg==2 ){ |
| 6708 | zSrcFile = azArg[1]; |
| 6709 | zDb = "main"; |
| 6710 | }else if( nArg==3 ){ |
| 6711 | zSrcFile = azArg[2]; |
| 6712 | zDb = azArg[1]; |
| 6713 | }else{ |
| 6714 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
| 6715 | rc = 1; |
| 6716 | goto meta_command_exit; |
| 6717 | } |
| 6718 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 6719 | if( rc!=SQLITE_OK ){ |
| 6720 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 6721 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6722 | return 1; |
| 6723 | } |
| 6724 | open_db(p, 0); |
| 6725 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 6726 | if( pBackup==0 ){ |
| 6727 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 6728 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6729 | return 1; |
| 6730 | } |
| 6731 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 6732 | || rc==SQLITE_BUSY ){ |
| 6733 | if( rc==SQLITE_BUSY ){ |
| 6734 | if( nTimeout++ >= 3 ) break; |
| 6735 | sqlite3_sleep(100); |
| 6736 | } |
| 6737 | } |
| 6738 | sqlite3_backup_finish(pBackup); |
| 6739 | if( rc==SQLITE_DONE ){ |
| 6740 | rc = 0; |
| 6741 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 6742 | raw_printf(stderr, "Error: source database is busy\n"); |
| 6743 | rc = 1; |
| 6744 | }else{ |
| 6745 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6746 | rc = 1; |
| 6747 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 6748 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6749 | }else |
| 6750 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6751 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 6752 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 6753 | p->scanstatsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6754 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 6755 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| 6756 | #endif |
| 6757 | }else{ |
| 6758 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| 6759 | rc = 1; |
| 6760 | } |
| 6761 | }else |
| 6762 | |
| 6763 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 6764 | ShellText sSelect; |
| 6765 | ShellState data; |
| 6766 | char *zErrMsg = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6767 | const char *zDiv = "("; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6768 | const char *zName = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6769 | int iSchema = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6770 | int bDebug = 0; |
| 6771 | int ii; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6772 | |
| 6773 | open_db(p, 0); |
| 6774 | memcpy(&data, p, sizeof(data)); |
| 6775 | data.showHeader = 0; |
| 6776 | data.cMode = data.mode = MODE_Semi; |
| 6777 | initText(&sSelect); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6778 | for(ii=1; ii<nArg; ii++){ |
| 6779 | if( optionMatch(azArg[ii],"indent") ){ |
| 6780 | data.cMode = data.mode = MODE_Pretty; |
| 6781 | }else if( optionMatch(azArg[ii],"debug") ){ |
| 6782 | bDebug = 1; |
| 6783 | }else if( zName==0 ){ |
| 6784 | zName = azArg[ii]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6785 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6786 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
| 6787 | rc = 1; |
| 6788 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6789 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6790 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6791 | if( zName!=0 ){ |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 6792 | int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0; |
| 6793 | if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6794 | char *new_argv[2], *new_colv[2]; |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 6795 | new_argv[0] = sqlite3_mprintf( |
| 6796 | "CREATE TABLE %s (\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6797 | " type text,\n" |
| 6798 | " name text,\n" |
| 6799 | " tbl_name text,\n" |
| 6800 | " rootpage integer,\n" |
| 6801 | " sql text\n" |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6802 | ")", isMaster ? "sqlite_master" : "sqlite_temp_master"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6803 | new_argv[1] = 0; |
| 6804 | new_colv[0] = "sql"; |
| 6805 | new_colv[1] = 0; |
| 6806 | callback(&data, 1, new_argv, new_colv); |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 6807 | sqlite3_free(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6808 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6809 | } |
| 6810 | if( zDiv ){ |
| 6811 | sqlite3_stmt *pStmt = 0; |
| 6812 | rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 6813 | -1, &pStmt, 0); |
| 6814 | if( rc ){ |
| 6815 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6816 | sqlite3_finalize(pStmt); |
| 6817 | rc = 1; |
| 6818 | goto meta_command_exit; |
| 6819 | } |
| 6820 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 6821 | iSchema = 0; |
| 6822 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6823 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 6824 | char zScNum[30]; |
| 6825 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 6826 | appendText(&sSelect, zDiv, 0); |
| 6827 | zDiv = " UNION ALL "; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6828 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
| 6829 | if( sqlite3_stricmp(zDb, "main")!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6830 | appendText(&sSelect, zDb, '"'); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6831 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6832 | appendText(&sSelect, "NULL", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6833 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6834 | appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); |
| 6835 | appendText(&sSelect, zScNum, 0); |
| 6836 | appendText(&sSelect, " AS snum, ", 0); |
| 6837 | appendText(&sSelect, zDb, '\''); |
| 6838 | appendText(&sSelect, " AS sname FROM ", 0); |
| 6839 | appendText(&sSelect, zDb, '"'); |
| 6840 | appendText(&sSelect, ".sqlite_master", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6841 | } |
| 6842 | sqlite3_finalize(pStmt); |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 6843 | #ifdef SQLITE_INTROSPECTION_PRAGMAS |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 6844 | if( zName ){ |
| 6845 | appendText(&sSelect, |
| 6846 | " UNION ALL SELECT shell_module_schema(name)," |
| 6847 | " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0); |
| 6848 | } |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 6849 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6850 | appendText(&sSelect, ") WHERE ", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6851 | if( zName ){ |
| 6852 | char *zQarg = sqlite3_mprintf("%Q", zName); |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 6853 | int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || |
| 6854 | strchr(zName, '[') != 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6855 | if( strchr(zName, '.') ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6856 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 6857 | }else{ |
| 6858 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 6859 | } |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 6860 | appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6861 | appendText(&sSelect, zQarg, 0); |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 6862 | if( !bGlob ){ |
| 6863 | appendText(&sSelect, " ESCAPE '\\' ", 0); |
| 6864 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6865 | appendText(&sSelect, " AND ", 0); |
| 6866 | sqlite3_free(zQarg); |
| 6867 | } |
| 6868 | appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" |
| 6869 | " ORDER BY snum, rowid", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 6870 | if( bDebug ){ |
| 6871 | utf8_printf(p->out, "SQL: %s;\n", sSelect.z); |
| 6872 | }else{ |
| 6873 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 6874 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6875 | freeText(&sSelect); |
| 6876 | } |
| 6877 | if( zErrMsg ){ |
| 6878 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 6879 | sqlite3_free(zErrMsg); |
| 6880 | rc = 1; |
| 6881 | }else if( rc != SQLITE_OK ){ |
| 6882 | raw_printf(stderr,"Error: querying schema information\n"); |
| 6883 | rc = 1; |
| 6884 | }else{ |
| 6885 | rc = 0; |
| 6886 | } |
| 6887 | }else |
| 6888 | |
| 6889 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 6890 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
| 6891 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
| 6892 | }else |
| 6893 | #endif |
| 6894 | |
| 6895 | #if defined(SQLITE_ENABLE_SESSION) |
| 6896 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 6897 | OpenSession *pSession = &p->aSession[0]; |
| 6898 | char **azCmd = &azArg[1]; |
| 6899 | int iSes = 0; |
| 6900 | int nCmd = nArg - 1; |
| 6901 | int i; |
| 6902 | if( nArg<=1 ) goto session_syntax_error; |
| 6903 | open_db(p, 0); |
| 6904 | if( nArg>=3 ){ |
| 6905 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 6906 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 6907 | } |
| 6908 | if( iSes<p->nSession ){ |
| 6909 | pSession = &p->aSession[iSes]; |
| 6910 | azCmd++; |
| 6911 | nCmd--; |
| 6912 | }else{ |
| 6913 | pSession = &p->aSession[0]; |
| 6914 | iSes = 0; |
| 6915 | } |
| 6916 | } |
| 6917 | |
| 6918 | /* .session attach TABLE |
| 6919 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 6920 | ** table so that it is never filtered. |
| 6921 | */ |
| 6922 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 6923 | if( nCmd!=2 ) goto session_syntax_error; |
| 6924 | if( pSession->p==0 ){ |
| 6925 | session_not_open: |
| 6926 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
| 6927 | }else{ |
| 6928 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 6929 | if( rc ){ |
| 6930 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| 6931 | rc = 0; |
| 6932 | } |
| 6933 | } |
| 6934 | }else |
| 6935 | |
| 6936 | /* .session changeset FILE |
| 6937 | ** .session patchset FILE |
| 6938 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 6939 | */ |
| 6940 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 6941 | FILE *out = 0; |
| 6942 | if( nCmd!=2 ) goto session_syntax_error; |
| 6943 | if( pSession->p==0 ) goto session_not_open; |
| 6944 | out = fopen(azCmd[1], "wb"); |
| 6945 | if( out==0 ){ |
| 6946 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
| 6947 | }else{ |
| 6948 | int szChng; |
| 6949 | void *pChng; |
| 6950 | if( azCmd[0][0]=='c' ){ |
| 6951 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| 6952 | }else{ |
| 6953 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 6954 | } |
| 6955 | if( rc ){ |
| 6956 | printf("Error: error code %d\n", rc); |
| 6957 | rc = 0; |
| 6958 | } |
| 6959 | if( pChng |
| 6960 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
| 6961 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| 6962 | szChng); |
| 6963 | } |
| 6964 | sqlite3_free(pChng); |
| 6965 | fclose(out); |
| 6966 | } |
| 6967 | }else |
| 6968 | |
| 6969 | /* .session close |
| 6970 | ** Close the identified session |
| 6971 | */ |
| 6972 | if( strcmp(azCmd[0], "close")==0 ){ |
| 6973 | if( nCmd!=1 ) goto session_syntax_error; |
| 6974 | if( p->nSession ){ |
| 6975 | session_close(pSession); |
| 6976 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 6977 | } |
| 6978 | }else |
| 6979 | |
| 6980 | /* .session enable ?BOOLEAN? |
| 6981 | ** Query or set the enable flag |
| 6982 | */ |
| 6983 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 6984 | int ii; |
| 6985 | if( nCmd>2 ) goto session_syntax_error; |
| 6986 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6987 | if( p->nSession ){ |
| 6988 | ii = sqlite3session_enable(pSession->p, ii); |
| 6989 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 6990 | pSession->zName, ii); |
| 6991 | } |
| 6992 | }else |
| 6993 | |
| 6994 | /* .session filter GLOB .... |
| 6995 | ** Set a list of GLOB patterns of table names to be excluded. |
| 6996 | */ |
| 6997 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 6998 | int ii, nByte; |
| 6999 | if( nCmd<2 ) goto session_syntax_error; |
| 7000 | if( p->nSession ){ |
| 7001 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 7002 | sqlite3_free(pSession->azFilter[ii]); |
| 7003 | } |
| 7004 | sqlite3_free(pSession->azFilter); |
| 7005 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 7006 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 7007 | if( pSession->azFilter==0 ){ |
| 7008 | raw_printf(stderr, "Error: out or memory\n"); |
| 7009 | exit(1); |
| 7010 | } |
| 7011 | for(ii=1; ii<nCmd; ii++){ |
| 7012 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| 7013 | } |
| 7014 | pSession->nFilter = ii-1; |
| 7015 | } |
| 7016 | }else |
| 7017 | |
| 7018 | /* .session indirect ?BOOLEAN? |
| 7019 | ** Query or set the indirect flag |
| 7020 | */ |
| 7021 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 7022 | int ii; |
| 7023 | if( nCmd>2 ) goto session_syntax_error; |
| 7024 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 7025 | if( p->nSession ){ |
| 7026 | ii = sqlite3session_indirect(pSession->p, ii); |
| 7027 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 7028 | pSession->zName, ii); |
| 7029 | } |
| 7030 | }else |
| 7031 | |
| 7032 | /* .session isempty |
| 7033 | ** Determine if the session is empty |
| 7034 | */ |
| 7035 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 7036 | int ii; |
| 7037 | if( nCmd!=1 ) goto session_syntax_error; |
| 7038 | if( p->nSession ){ |
| 7039 | ii = sqlite3session_isempty(pSession->p); |
| 7040 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 7041 | pSession->zName, ii); |
| 7042 | } |
| 7043 | }else |
| 7044 | |
| 7045 | /* .session list |
| 7046 | ** List all currently open sessions |
| 7047 | */ |
| 7048 | if( strcmp(azCmd[0],"list")==0 ){ |
| 7049 | for(i=0; i<p->nSession; i++){ |
| 7050 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
| 7051 | } |
| 7052 | }else |
| 7053 | |
| 7054 | /* .session open DB NAME |
| 7055 | ** Open a new session called NAME on the attached database DB. |
| 7056 | ** DB is normally "main". |
| 7057 | */ |
| 7058 | if( strcmp(azCmd[0],"open")==0 ){ |
| 7059 | char *zName; |
| 7060 | if( nCmd!=3 ) goto session_syntax_error; |
| 7061 | zName = azCmd[2]; |
| 7062 | if( zName[0]==0 ) goto session_syntax_error; |
| 7063 | for(i=0; i<p->nSession; i++){ |
| 7064 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
| 7065 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| 7066 | goto meta_command_exit; |
| 7067 | } |
| 7068 | } |
| 7069 | if( p->nSession>=ArraySize(p->aSession) ){ |
| 7070 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
| 7071 | goto meta_command_exit; |
| 7072 | } |
| 7073 | pSession = &p->aSession[p->nSession]; |
| 7074 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 7075 | if( rc ){ |
| 7076 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| 7077 | rc = 0; |
| 7078 | goto meta_command_exit; |
| 7079 | } |
| 7080 | pSession->nFilter = 0; |
| 7081 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
| 7082 | p->nSession++; |
| 7083 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 7084 | }else |
| 7085 | /* If no command name matches, show a syntax error */ |
| 7086 | session_syntax_error: |
| 7087 | session_help(p); |
| 7088 | }else |
| 7089 | #endif |
| 7090 | |
| 7091 | #ifdef SQLITE_DEBUG |
| 7092 | /* Undocumented commands for internal testing. Subject to change |
| 7093 | ** without notice. */ |
| 7094 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 7095 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 7096 | int i, v; |
| 7097 | for(i=1; i<nArg; i++){ |
| 7098 | v = booleanValue(azArg[i]); |
| 7099 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| 7100 | } |
| 7101 | } |
| 7102 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 7103 | int i; sqlite3_int64 v; |
| 7104 | for(i=1; i<nArg; i++){ |
| 7105 | char zBuf[200]; |
| 7106 | v = integerValue(azArg[i]); |
| 7107 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| 7108 | utf8_printf(p->out, "%s", zBuf); |
| 7109 | } |
| 7110 | } |
| 7111 | }else |
| 7112 | #endif |
| 7113 | |
| 7114 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 7115 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 7116 | int bVerbose = 0; /* Verbose output */ |
| 7117 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 7118 | int i, k; /* Loop counters */ |
| 7119 | int nTest = 0; /* Number of tests runs */ |
| 7120 | int nErr = 0; /* Number of errors seen */ |
| 7121 | ShellText str; /* Answer for a query */ |
| 7122 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
| 7123 | |
| 7124 | open_db(p,0); |
| 7125 | for(i=1; i<nArg; i++){ |
| 7126 | const char *z = azArg[i]; |
| 7127 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 7128 | if( strcmp(z,"-init")==0 ){ |
| 7129 | bIsInit = 1; |
| 7130 | }else |
| 7131 | if( strcmp(z,"-v")==0 ){ |
| 7132 | bVerbose++; |
| 7133 | }else |
| 7134 | { |
| 7135 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 7136 | azArg[i], azArg[0]); |
| 7137 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 7138 | rc = 1; |
| 7139 | goto meta_command_exit; |
| 7140 | } |
| 7141 | } |
| 7142 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 7143 | != SQLITE_OK ){ |
| 7144 | bSelftestExists = 0; |
| 7145 | }else{ |
| 7146 | bSelftestExists = 1; |
| 7147 | } |
| 7148 | if( bIsInit ){ |
| 7149 | createSelftestTable(p); |
| 7150 | bSelftestExists = 1; |
| 7151 | } |
| 7152 | initText(&str); |
| 7153 | appendText(&str, "x", 0); |
| 7154 | for(k=bSelftestExists; k>=0; k--){ |
| 7155 | if( k==1 ){ |
| 7156 | rc = sqlite3_prepare_v2(p->db, |
| 7157 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 7158 | -1, &pStmt, 0); |
| 7159 | }else{ |
| 7160 | rc = sqlite3_prepare_v2(p->db, |
| 7161 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 7162 | " (1,'run','PRAGMA integrity_check','ok')", |
| 7163 | -1, &pStmt, 0); |
| 7164 | } |
| 7165 | if( rc ){ |
| 7166 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 7167 | rc = 1; |
| 7168 | sqlite3_finalize(pStmt); |
| 7169 | goto meta_command_exit; |
| 7170 | } |
| 7171 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 7172 | int tno = sqlite3_column_int(pStmt, 0); |
| 7173 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 7174 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 7175 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
| 7176 | |
| 7177 | k = 0; |
| 7178 | if( bVerbose>0 ){ |
| 7179 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 7180 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 7181 | sqlite3_free(zQuote); |
| 7182 | } |
| 7183 | if( strcmp(zOp,"memo")==0 ){ |
| 7184 | utf8_printf(p->out, "%s\n", zSql); |
| 7185 | }else |
| 7186 | if( strcmp(zOp,"run")==0 ){ |
| 7187 | char *zErrMsg = 0; |
| 7188 | str.n = 0; |
| 7189 | str.z[0] = 0; |
| 7190 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 7191 | nTest++; |
| 7192 | if( bVerbose ){ |
| 7193 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 7194 | } |
| 7195 | if( rc || zErrMsg ){ |
| 7196 | nErr++; |
| 7197 | rc = 1; |
| 7198 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 7199 | sqlite3_free(zErrMsg); |
| 7200 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 7201 | nErr++; |
| 7202 | rc = 1; |
| 7203 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 7204 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 7205 | } |
| 7206 | }else |
| 7207 | { |
| 7208 | utf8_printf(stderr, |
| 7209 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 7210 | rc = 1; |
| 7211 | break; |
| 7212 | } |
| 7213 | } /* End loop over rows of content from SELFTEST */ |
| 7214 | sqlite3_finalize(pStmt); |
| 7215 | } /* End loop over k */ |
| 7216 | freeText(&str); |
| 7217 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 7218 | }else |
| 7219 | |
| 7220 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| 7221 | if( nArg<2 || nArg>3 ){ |
| 7222 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 7223 | rc = 1; |
| 7224 | } |
| 7225 | if( nArg>=2 ){ |
| 7226 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| 7227 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| 7228 | } |
| 7229 | if( nArg>=3 ){ |
| 7230 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 7231 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 7232 | } |
| 7233 | }else |
| 7234 | |
| 7235 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 7236 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 7237 | int i; /* Loop counter */ |
| 7238 | int bSchema = 0; /* Also hash the schema */ |
| 7239 | int bSeparate = 0; /* Hash each table separately */ |
| 7240 | int iSize = 224; /* Hash algorithm to use */ |
| 7241 | int bDebug = 0; /* Only show the query that would have run */ |
| 7242 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 7243 | char *zSql; /* SQL to be run */ |
| 7244 | char *zSep; /* Separator */ |
| 7245 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
| 7246 | ShellText sQuery; /* Set of queries used to read all content */ |
| 7247 | open_db(p, 0); |
| 7248 | for(i=1; i<nArg; i++){ |
| 7249 | const char *z = azArg[i]; |
| 7250 | if( z[0]=='-' ){ |
| 7251 | z++; |
| 7252 | if( z[0]=='-' ) z++; |
| 7253 | if( strcmp(z,"schema")==0 ){ |
| 7254 | bSchema = 1; |
| 7255 | }else |
| 7256 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 7257 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 7258 | ){ |
| 7259 | iSize = atoi(&z[5]); |
| 7260 | }else |
| 7261 | if( strcmp(z,"debug")==0 ){ |
| 7262 | bDebug = 1; |
| 7263 | }else |
| 7264 | { |
| 7265 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 7266 | azArg[i], azArg[0]); |
| 7267 | raw_printf(stderr, "Should be one of: --schema" |
drh | 003edba | 2018-05-11 15:10:43 +0000 | [diff] [blame] | 7268 | " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7269 | rc = 1; |
| 7270 | goto meta_command_exit; |
| 7271 | } |
| 7272 | }else if( zLike ){ |
| 7273 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 7274 | rc = 1; |
| 7275 | goto meta_command_exit; |
| 7276 | }else{ |
| 7277 | zLike = z; |
| 7278 | bSeparate = 1; |
drh | cedfecf | 2018-03-23 12:59:10 +0000 | [diff] [blame] | 7279 | if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7280 | } |
| 7281 | } |
| 7282 | if( bSchema ){ |
| 7283 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7284 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7285 | " UNION ALL SELECT 'sqlite_master'" |
| 7286 | " ORDER BY 1 collate nocase"; |
| 7287 | }else{ |
| 7288 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7289 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7290 | " AND name NOT LIKE 'sqlite_%'" |
| 7291 | " ORDER BY 1 collate nocase"; |
| 7292 | } |
| 7293 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 7294 | initText(&sQuery); |
| 7295 | initText(&sSql); |
| 7296 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 7297 | zSep = "VALUES("; |
| 7298 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7299 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 7300 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 7301 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 7302 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 7303 | appendText(&sQuery,zTab,'"'); |
| 7304 | appendText(&sQuery," NOT INDEXED;", 0); |
| 7305 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 7306 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 7307 | " ORDER BY name;", 0); |
| 7308 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 7309 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 7310 | " ORDER BY name;", 0); |
| 7311 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 7312 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 7313 | " ORDER BY tbl,idx;", 0); |
| 7314 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 7315 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 7316 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 7317 | appendText(&sQuery, zTab, 0); |
| 7318 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 7319 | } |
| 7320 | appendText(&sSql, zSep, 0); |
| 7321 | appendText(&sSql, sQuery.z, '\''); |
| 7322 | sQuery.n = 0; |
| 7323 | appendText(&sSql, ",", 0); |
| 7324 | appendText(&sSql, zTab, '\''); |
| 7325 | zSep = "),("; |
| 7326 | } |
| 7327 | sqlite3_finalize(pStmt); |
| 7328 | if( bSeparate ){ |
| 7329 | zSql = sqlite3_mprintf( |
| 7330 | "%s))" |
| 7331 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 7332 | " FROM [sha3sum$query]", |
| 7333 | sSql.z, iSize); |
| 7334 | }else{ |
| 7335 | zSql = sqlite3_mprintf( |
| 7336 | "%s))" |
| 7337 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 7338 | " FROM [sha3sum$query]", |
| 7339 | sSql.z, iSize); |
| 7340 | } |
| 7341 | freeText(&sQuery); |
| 7342 | freeText(&sSql); |
| 7343 | if( bDebug ){ |
| 7344 | utf8_printf(p->out, "%s\n", zSql); |
| 7345 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 7346 | shell_exec(p, zSql, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7347 | } |
| 7348 | sqlite3_free(zSql); |
| 7349 | }else |
| 7350 | |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 7351 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7352 | if( c=='s' |
| 7353 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 7354 | ){ |
| 7355 | char *zCmd; |
| 7356 | int i, x; |
| 7357 | if( nArg<2 ){ |
| 7358 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
| 7359 | rc = 1; |
| 7360 | goto meta_command_exit; |
| 7361 | } |
| 7362 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
| 7363 | for(i=2; i<nArg; i++){ |
| 7364 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 7365 | zCmd, azArg[i]); |
| 7366 | } |
| 7367 | x = system(zCmd); |
| 7368 | sqlite3_free(zCmd); |
| 7369 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 7370 | }else |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 7371 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7372 | |
| 7373 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 7374 | static const char *azBool[] = { "off", "on", "trigger", "full"}; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7375 | int i; |
| 7376 | if( nArg!=1 ){ |
| 7377 | raw_printf(stderr, "Usage: .show\n"); |
| 7378 | rc = 1; |
| 7379 | goto meta_command_exit; |
| 7380 | } |
| 7381 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 7382 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
| 7383 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 7384 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 7385 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 7386 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
| 7387 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 7388 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| 7389 | output_c_string(p->out, p->nullValue); |
| 7390 | raw_printf(p->out, "\n"); |
| 7391 | utf8_printf(p->out,"%12.12s: %s\n","output", |
| 7392 | strlen30(p->outfile) ? p->outfile : "stdout"); |
| 7393 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
| 7394 | output_c_string(p->out, p->colSeparator); |
| 7395 | raw_printf(p->out, "\n"); |
| 7396 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
| 7397 | output_c_string(p->out, p->rowSeparator); |
| 7398 | raw_printf(p->out, "\n"); |
| 7399 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
| 7400 | utf8_printf(p->out, "%12.12s: ", "width"); |
| 7401 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
| 7402 | raw_printf(p->out, "%d ", p->colWidth[i]); |
| 7403 | } |
| 7404 | raw_printf(p->out, "\n"); |
| 7405 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 7406 | p->zDbFilename ? p->zDbFilename : ""); |
| 7407 | }else |
| 7408 | |
| 7409 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 7410 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 7411 | p->statsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7412 | }else if( nArg==1 ){ |
| 7413 | display_stats(p->db, p, 0); |
| 7414 | }else{ |
| 7415 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
| 7416 | rc = 1; |
| 7417 | } |
| 7418 | }else |
| 7419 | |
| 7420 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 7421 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 7422 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 7423 | ){ |
| 7424 | sqlite3_stmt *pStmt; |
| 7425 | char **azResult; |
| 7426 | int nRow, nAlloc; |
| 7427 | int ii; |
| 7428 | ShellText s; |
| 7429 | initText(&s); |
| 7430 | open_db(p, 0); |
| 7431 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7432 | if( rc ){ |
| 7433 | sqlite3_finalize(pStmt); |
| 7434 | return shellDatabaseError(p->db); |
| 7435 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7436 | |
| 7437 | if( nArg>2 && c=='i' ){ |
| 7438 | /* It is an historical accident that the .indexes command shows an error |
| 7439 | ** when called with the wrong number of arguments whereas the .tables |
| 7440 | ** command does not. */ |
| 7441 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 7442 | rc = 1; |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7443 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7444 | goto meta_command_exit; |
| 7445 | } |
| 7446 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
| 7447 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 7448 | if( zDbName==0 ) continue; |
| 7449 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 7450 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 7451 | appendText(&s, "SELECT name FROM ", 0); |
| 7452 | }else{ |
| 7453 | appendText(&s, "SELECT ", 0); |
| 7454 | appendText(&s, zDbName, '\''); |
| 7455 | appendText(&s, "||'.'||name FROM ", 0); |
| 7456 | } |
| 7457 | appendText(&s, zDbName, '"'); |
| 7458 | appendText(&s, ".sqlite_master ", 0); |
| 7459 | if( c=='t' ){ |
| 7460 | appendText(&s," WHERE type IN ('table','view')" |
| 7461 | " AND name NOT LIKE 'sqlite_%'" |
| 7462 | " AND name LIKE ?1", 0); |
| 7463 | }else{ |
| 7464 | appendText(&s," WHERE type='index'" |
| 7465 | " AND tbl_name LIKE ?1", 0); |
| 7466 | } |
| 7467 | } |
| 7468 | rc = sqlite3_finalize(pStmt); |
| 7469 | appendText(&s, " ORDER BY 1", 0); |
| 7470 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 7471 | freeText(&s); |
| 7472 | if( rc ) return shellDatabaseError(p->db); |
| 7473 | |
| 7474 | /* Run the SQL statement prepared by the above block. Store the results |
| 7475 | ** as an array of nul-terminated strings in azResult[]. */ |
| 7476 | nRow = nAlloc = 0; |
| 7477 | azResult = 0; |
| 7478 | if( nArg>1 ){ |
| 7479 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
| 7480 | }else{ |
| 7481 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 7482 | } |
| 7483 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 7484 | if( nRow>=nAlloc ){ |
| 7485 | char **azNew; |
| 7486 | int n2 = nAlloc*2 + 10; |
| 7487 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 7488 | if( azNew==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7489 | nAlloc = n2; |
| 7490 | azResult = azNew; |
| 7491 | } |
| 7492 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 7493 | if( 0==azResult[nRow] ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7494 | nRow++; |
| 7495 | } |
| 7496 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 7497 | rc = shellDatabaseError(p->db); |
| 7498 | } |
| 7499 | |
| 7500 | /* Pretty-print the contents of array azResult[] to the output */ |
| 7501 | if( rc==0 && nRow>0 ){ |
| 7502 | int len, maxlen = 0; |
| 7503 | int i, j; |
| 7504 | int nPrintCol, nPrintRow; |
| 7505 | for(i=0; i<nRow; i++){ |
| 7506 | len = strlen30(azResult[i]); |
| 7507 | if( len>maxlen ) maxlen = len; |
| 7508 | } |
| 7509 | nPrintCol = 80/(maxlen+2); |
| 7510 | if( nPrintCol<1 ) nPrintCol = 1; |
| 7511 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 7512 | for(i=0; i<nPrintRow; i++){ |
| 7513 | for(j=i; j<nRow; j+=nPrintRow){ |
| 7514 | char *zSp = j<nPrintRow ? "" : " "; |
| 7515 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 7516 | azResult[j] ? azResult[j]:""); |
| 7517 | } |
| 7518 | raw_printf(p->out, "\n"); |
| 7519 | } |
| 7520 | } |
| 7521 | |
| 7522 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 7523 | sqlite3_free(azResult); |
| 7524 | }else |
| 7525 | |
| 7526 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 7527 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 7528 | output_reset(p); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 7529 | p->out = output_file_open("testcase-out.txt", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7530 | if( p->out==0 ){ |
| 7531 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
| 7532 | } |
| 7533 | if( nArg>=2 ){ |
| 7534 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 7535 | }else{ |
| 7536 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 7537 | } |
| 7538 | }else |
| 7539 | |
| 7540 | #ifndef SQLITE_UNTESTABLE |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7541 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7542 | static const struct { |
| 7543 | const char *zCtrlName; /* Name of a test-control option */ |
| 7544 | int ctrlCode; /* Integer code for that option */ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7545 | const char *zUsage; /* Usage notes */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7546 | } aCtrl[] = { |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7547 | { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, |
| 7548 | { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, |
| 7549 | /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ |
| 7550 | /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ |
| 7551 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, |
| 7552 | /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */ |
| 7553 | { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7554 | { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, |
| 7555 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, |
| 7556 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 7557 | #ifdef YYCOVERAGE |
| 7558 | { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, |
| 7559 | #endif |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7560 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, |
| 7561 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" }, |
| 7562 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, |
| 7563 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, |
| 7564 | { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" }, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7565 | }; |
| 7566 | int testctrl = -1; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7567 | int iCtrl = -1; |
| 7568 | int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ |
| 7569 | int isOk = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7570 | int i, n2; |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 7571 | const char *zCmd = 0; |
| 7572 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7573 | open_db(p, 0); |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 7574 | zCmd = nArg>=2 ? azArg[1] : "help"; |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7575 | |
| 7576 | /* The argument can optionally begin with "-" or "--" */ |
| 7577 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 7578 | zCmd++; |
| 7579 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 7580 | } |
| 7581 | |
| 7582 | /* --help lists all test-controls */ |
| 7583 | if( strcmp(zCmd,"help")==0 ){ |
| 7584 | utf8_printf(p->out, "Available test-controls:\n"); |
| 7585 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7586 | utf8_printf(p->out, " .testctrl %s %s\n", |
| 7587 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7588 | } |
| 7589 | rc = 1; |
| 7590 | goto meta_command_exit; |
| 7591 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7592 | |
| 7593 | /* convert testctrl text option to value. allow any unique prefix |
| 7594 | ** of the option name, or a numerical value. */ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7595 | n2 = strlen30(zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7596 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7597 | if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7598 | if( testctrl<0 ){ |
| 7599 | testctrl = aCtrl[i].ctrlCode; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7600 | iCtrl = i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7601 | }else{ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7602 | utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" |
| 7603 | "Use \".testctrl --help\" for help\n", zCmd); |
| 7604 | rc = 1; |
| 7605 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7606 | } |
| 7607 | } |
| 7608 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7609 | if( testctrl<0 ){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 7610 | utf8_printf(stderr,"Error: unknown test-control: %s\n" |
| 7611 | "Use \".testctrl --help\" for help\n", zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7612 | }else{ |
| 7613 | switch(testctrl){ |
| 7614 | |
| 7615 | /* sqlite3_test_control(int, db, int) */ |
| 7616 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
| 7617 | case SQLITE_TESTCTRL_RESERVE: |
| 7618 | if( nArg==3 ){ |
| 7619 | int opt = (int)strtol(azArg[2], 0, 0); |
| 7620 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7621 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7622 | } |
| 7623 | break; |
| 7624 | |
| 7625 | /* sqlite3_test_control(int) */ |
| 7626 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 7627 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
| 7628 | case SQLITE_TESTCTRL_PRNG_RESET: |
| 7629 | case SQLITE_TESTCTRL_BYTEORDER: |
| 7630 | if( nArg==2 ){ |
| 7631 | rc2 = sqlite3_test_control(testctrl); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7632 | isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7633 | } |
| 7634 | break; |
| 7635 | |
| 7636 | /* sqlite3_test_control(int, uint) */ |
| 7637 | case SQLITE_TESTCTRL_PENDING_BYTE: |
| 7638 | if( nArg==3 ){ |
| 7639 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
| 7640 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7641 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7642 | } |
| 7643 | break; |
| 7644 | |
| 7645 | /* sqlite3_test_control(int, int) */ |
| 7646 | case SQLITE_TESTCTRL_ASSERT: |
| 7647 | case SQLITE_TESTCTRL_ALWAYS: |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7648 | if( nArg==3 ){ |
| 7649 | int opt = booleanValue(azArg[2]); |
| 7650 | rc2 = sqlite3_test_control(testctrl, opt); |
| 7651 | isOk = 1; |
| 7652 | } |
| 7653 | break; |
| 7654 | |
| 7655 | /* sqlite3_test_control(int, int) */ |
| 7656 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7657 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
| 7658 | if( nArg==3 ){ |
| 7659 | int opt = booleanValue(azArg[2]); |
| 7660 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7661 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7662 | } |
| 7663 | break; |
| 7664 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7665 | case SQLITE_TESTCTRL_IMPOSTER: |
| 7666 | if( nArg==5 ){ |
| 7667 | rc2 = sqlite3_test_control(testctrl, p->db, |
| 7668 | azArg[2], |
| 7669 | integerValue(azArg[3]), |
| 7670 | integerValue(azArg[4])); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7671 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7672 | } |
| 7673 | break; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 7674 | |
| 7675 | #ifdef YYCOVERAGE |
| 7676 | case SQLITE_TESTCTRL_PARSER_COVERAGE: |
| 7677 | if( nArg==2 ){ |
| 7678 | sqlite3_test_control(testctrl, p->out); |
| 7679 | isOk = 3; |
| 7680 | } |
| 7681 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7682 | } |
| 7683 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 7684 | if( isOk==0 && iCtrl>=0 ){ |
| 7685 | utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage); |
| 7686 | rc = 1; |
| 7687 | }else if( isOk==1 ){ |
| 7688 | raw_printf(p->out, "%d\n", rc2); |
| 7689 | }else if( isOk==2 ){ |
| 7690 | raw_printf(p->out, "0x%08x\n", rc2); |
| 7691 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7692 | }else |
| 7693 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 7694 | |
| 7695 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
| 7696 | open_db(p, 0); |
| 7697 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
| 7698 | }else |
| 7699 | |
| 7700 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 7701 | if( nArg==2 ){ |
| 7702 | enableTimer = booleanValue(azArg[1]); |
| 7703 | if( enableTimer && !HAS_TIMER ){ |
| 7704 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
| 7705 | enableTimer = 0; |
| 7706 | } |
| 7707 | }else{ |
| 7708 | raw_printf(stderr, "Usage: .timer on|off\n"); |
| 7709 | rc = 1; |
| 7710 | } |
| 7711 | }else |
| 7712 | |
| 7713 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
| 7714 | open_db(p, 0); |
| 7715 | if( nArg!=2 ){ |
| 7716 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
| 7717 | rc = 1; |
| 7718 | goto meta_command_exit; |
| 7719 | } |
| 7720 | output_file_close(p->traceOut); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 7721 | p->traceOut = output_file_open(azArg[1], 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7722 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 7723 | if( p->traceOut==0 ){ |
| 7724 | sqlite3_trace_v2(p->db, 0, 0, 0); |
| 7725 | }else{ |
| 7726 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
| 7727 | } |
| 7728 | #endif |
| 7729 | }else |
| 7730 | |
| 7731 | #if SQLITE_USER_AUTHENTICATION |
| 7732 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 7733 | if( nArg<2 ){ |
| 7734 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
| 7735 | rc = 1; |
| 7736 | goto meta_command_exit; |
| 7737 | } |
| 7738 | open_db(p, 0); |
| 7739 | if( strcmp(azArg[1],"login")==0 ){ |
| 7740 | if( nArg!=4 ){ |
| 7741 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
| 7742 | rc = 1; |
| 7743 | goto meta_command_exit; |
| 7744 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7745 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3])); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7746 | if( rc ){ |
| 7747 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
| 7748 | rc = 1; |
| 7749 | } |
| 7750 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 7751 | if( nArg!=5 ){ |
| 7752 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
| 7753 | rc = 1; |
| 7754 | goto meta_command_exit; |
| 7755 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7756 | rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7757 | booleanValue(azArg[4])); |
| 7758 | if( rc ){ |
| 7759 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
| 7760 | rc = 1; |
| 7761 | } |
| 7762 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 7763 | if( nArg!=5 ){ |
| 7764 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
| 7765 | rc = 1; |
| 7766 | goto meta_command_exit; |
| 7767 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 7768 | rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7769 | booleanValue(azArg[4])); |
| 7770 | if( rc ){ |
| 7771 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
| 7772 | rc = 1; |
| 7773 | } |
| 7774 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 7775 | if( nArg!=3 ){ |
| 7776 | raw_printf(stderr, "Usage: .user delete USER\n"); |
| 7777 | rc = 1; |
| 7778 | goto meta_command_exit; |
| 7779 | } |
| 7780 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 7781 | if( rc ){ |
| 7782 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
| 7783 | rc = 1; |
| 7784 | } |
| 7785 | }else{ |
| 7786 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
| 7787 | rc = 1; |
| 7788 | goto meta_command_exit; |
| 7789 | } |
| 7790 | }else |
| 7791 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 7792 | |
| 7793 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
| 7794 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
| 7795 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | 0ed2fd8 | 2018-01-16 20:05:27 +0000 | [diff] [blame] | 7796 | #if SQLITE_HAVE_ZLIB |
| 7797 | utf8_printf(p->out, "zlib version %s\n", zlibVersion()); |
| 7798 | #endif |
| 7799 | #define CTIMEOPT_VAL_(opt) #opt |
| 7800 | #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) |
| 7801 | #if defined(__clang__) && defined(__clang_major__) |
| 7802 | utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." |
| 7803 | CTIMEOPT_VAL(__clang_minor__) "." |
| 7804 | CTIMEOPT_VAL(__clang_patchlevel__) "\n"); |
| 7805 | #elif defined(_MSC_VER) |
| 7806 | utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); |
| 7807 | #elif defined(__GNUC__) && defined(__VERSION__) |
| 7808 | utf8_printf(p->out, "gcc-" __VERSION__ "\n"); |
| 7809 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7810 | }else |
| 7811 | |
| 7812 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 7813 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 7814 | sqlite3_vfs *pVfs = 0; |
| 7815 | if( p->db ){ |
| 7816 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 7817 | if( pVfs ){ |
| 7818 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 7819 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7820 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7821 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 7822 | } |
| 7823 | } |
| 7824 | }else |
| 7825 | |
| 7826 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 7827 | sqlite3_vfs *pVfs; |
| 7828 | sqlite3_vfs *pCurrent = 0; |
| 7829 | if( p->db ){ |
| 7830 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 7831 | } |
| 7832 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 7833 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 7834 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 7835 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7836 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7837 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 7838 | if( pVfs->pNext ){ |
| 7839 | raw_printf(p->out, "-----------------------------------\n"); |
| 7840 | } |
| 7841 | } |
| 7842 | }else |
| 7843 | |
| 7844 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 7845 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 7846 | char *zVfsName = 0; |
| 7847 | if( p->db ){ |
| 7848 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 7849 | if( zVfsName ){ |
| 7850 | utf8_printf(p->out, "%s\n", zVfsName); |
| 7851 | sqlite3_free(zVfsName); |
| 7852 | } |
| 7853 | } |
| 7854 | }else |
| 7855 | |
| 7856 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 7857 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
| 7858 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
| 7859 | }else |
| 7860 | #endif |
| 7861 | |
| 7862 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 7863 | int j; |
| 7864 | assert( nArg<=ArraySize(azArg) ); |
| 7865 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 7866 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
| 7867 | } |
| 7868 | }else |
| 7869 | |
| 7870 | { |
| 7871 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
| 7872 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
| 7873 | rc = 1; |
| 7874 | } |
| 7875 | |
| 7876 | meta_command_exit: |
| 7877 | if( p->outCount ){ |
| 7878 | p->outCount--; |
| 7879 | if( p->outCount==0 ) output_reset(p); |
| 7880 | } |
| 7881 | return rc; |
| 7882 | } |
| 7883 | |
| 7884 | /* |
| 7885 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 7886 | ** of string z[]. |
| 7887 | */ |
| 7888 | static int line_contains_semicolon(const char *z, int N){ |
| 7889 | int i; |
| 7890 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 7891 | return 0; |
| 7892 | } |
| 7893 | |
| 7894 | /* |
| 7895 | ** Test to see if a line consists entirely of whitespace. |
| 7896 | */ |
| 7897 | static int _all_whitespace(const char *z){ |
| 7898 | for(; *z; z++){ |
| 7899 | if( IsSpace(z[0]) ) continue; |
| 7900 | if( *z=='/' && z[1]=='*' ){ |
| 7901 | z += 2; |
| 7902 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 7903 | if( *z==0 ) return 0; |
| 7904 | z++; |
| 7905 | continue; |
| 7906 | } |
| 7907 | if( *z=='-' && z[1]=='-' ){ |
| 7908 | z += 2; |
| 7909 | while( *z && *z!='\n' ){ z++; } |
| 7910 | if( *z==0 ) return 1; |
| 7911 | continue; |
| 7912 | } |
| 7913 | return 0; |
| 7914 | } |
| 7915 | return 1; |
| 7916 | } |
| 7917 | |
| 7918 | /* |
| 7919 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 7920 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 7921 | ** as is the Oracle "/". |
| 7922 | */ |
| 7923 | static int line_is_command_terminator(const char *zLine){ |
| 7924 | while( IsSpace(zLine[0]) ){ zLine++; }; |
| 7925 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 7926 | return 1; /* Oracle */ |
| 7927 | } |
| 7928 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
| 7929 | && _all_whitespace(&zLine[2]) ){ |
| 7930 | return 1; /* SQL Server */ |
| 7931 | } |
| 7932 | return 0; |
| 7933 | } |
| 7934 | |
| 7935 | /* |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 7936 | ** We need a default sqlite3_complete() implementation to use in case |
| 7937 | ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes |
| 7938 | ** any arbitrary text is a complete SQL statement. This is not very |
| 7939 | ** user-friendly, but it does seem to work. |
| 7940 | */ |
| 7941 | #ifdef SQLITE_OMIT_COMPLETE |
| 7942 | int sqlite3_complete(const char *zSql){ return 1; } |
| 7943 | #endif |
| 7944 | |
| 7945 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7946 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 7947 | ** ends in the middle of a string literal or C-style comment. |
| 7948 | */ |
| 7949 | static int line_is_complete(char *zSql, int nSql){ |
| 7950 | int rc; |
| 7951 | if( zSql==0 ) return 1; |
| 7952 | zSql[nSql] = ';'; |
| 7953 | zSql[nSql+1] = 0; |
| 7954 | rc = sqlite3_complete(zSql); |
| 7955 | zSql[nSql] = 0; |
| 7956 | return rc; |
| 7957 | } |
| 7958 | |
| 7959 | /* |
drh | fc29a86 | 2018-05-11 19:11:18 +0000 | [diff] [blame] | 7960 | ** Run a single line of SQL. Return the number of errors. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7961 | */ |
| 7962 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 7963 | int rc; |
| 7964 | char *zErrMsg = 0; |
| 7965 | |
| 7966 | open_db(p, 0); |
| 7967 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
| 7968 | BEGIN_TIMER; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 7969 | rc = shell_exec(p, zSql, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7970 | END_TIMER; |
| 7971 | if( rc || zErrMsg ){ |
| 7972 | char zPrefix[100]; |
| 7973 | if( in!=0 || !stdin_is_interactive ){ |
| 7974 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 7975 | "Error: near line %d:", startline); |
| 7976 | }else{ |
| 7977 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 7978 | } |
| 7979 | if( zErrMsg!=0 ){ |
| 7980 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 7981 | sqlite3_free(zErrMsg); |
| 7982 | zErrMsg = 0; |
| 7983 | }else{ |
| 7984 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 7985 | } |
| 7986 | return 1; |
| 7987 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
| 7988 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 7989 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 7990 | } |
| 7991 | return 0; |
| 7992 | } |
| 7993 | |
| 7994 | |
| 7995 | /* |
| 7996 | ** Read input from *in and process it. If *in==0 then input |
| 7997 | ** is interactive - the user is typing it it. Otherwise, input |
| 7998 | ** is coming from a file or device. A prompt is issued and history |
| 7999 | ** is saved only if input is interactive. An interrupt signal will |
| 8000 | ** cause this routine to exit immediately, unless input is interactive. |
| 8001 | ** |
| 8002 | ** Return the number of errors. |
| 8003 | */ |
| 8004 | static int process_input(ShellState *p, FILE *in){ |
| 8005 | char *zLine = 0; /* A single input line */ |
| 8006 | char *zSql = 0; /* Accumulated SQL text */ |
| 8007 | int nLine; /* Length of current line */ |
| 8008 | int nSql = 0; /* Bytes of zSql[] used */ |
| 8009 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 8010 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
| 8011 | int rc; /* Error code */ |
| 8012 | int errCnt = 0; /* Number of errors seen */ |
| 8013 | int lineno = 0; /* Current line number */ |
| 8014 | int startline = 0; /* Line number for start of current input */ |
| 8015 | |
| 8016 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 8017 | fflush(p->out); |
| 8018 | zLine = one_input_line(in, zLine, nSql>0); |
| 8019 | if( zLine==0 ){ |
| 8020 | /* End of input */ |
| 8021 | if( in==0 && stdin_is_interactive ) printf("\n"); |
| 8022 | break; |
| 8023 | } |
| 8024 | if( seenInterrupt ){ |
| 8025 | if( in!=0 ) break; |
| 8026 | seenInterrupt = 0; |
| 8027 | } |
| 8028 | lineno++; |
| 8029 | if( nSql==0 && _all_whitespace(zLine) ){ |
| 8030 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
| 8031 | continue; |
| 8032 | } |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 8033 | if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8034 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 8035 | if( zLine[0]=='.' ){ |
| 8036 | rc = do_meta_command(zLine, p); |
| 8037 | if( rc==2 ){ /* exit requested */ |
| 8038 | break; |
| 8039 | }else if( rc ){ |
| 8040 | errCnt++; |
| 8041 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8042 | } |
| 8043 | continue; |
| 8044 | } |
| 8045 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
| 8046 | memcpy(zLine,";",2); |
| 8047 | } |
| 8048 | nLine = strlen30(zLine); |
| 8049 | if( nSql+nLine+2>=nAlloc ){ |
| 8050 | nAlloc = nSql+nLine+100; |
| 8051 | zSql = realloc(zSql, nAlloc); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8052 | if( zSql==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8053 | } |
| 8054 | nSqlPrior = nSql; |
| 8055 | if( nSql==0 ){ |
| 8056 | int i; |
| 8057 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
| 8058 | assert( nAlloc>0 && zSql!=0 ); |
| 8059 | memcpy(zSql, zLine+i, nLine+1-i); |
| 8060 | startline = lineno; |
| 8061 | nSql = nLine-i; |
| 8062 | }else{ |
| 8063 | zSql[nSql++] = '\n'; |
| 8064 | memcpy(zSql+nSql, zLine, nLine+1); |
| 8065 | nSql += nLine; |
| 8066 | } |
| 8067 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
| 8068 | && sqlite3_complete(zSql) ){ |
| 8069 | errCnt += runOneSqlLine(p, zSql, in, startline); |
| 8070 | nSql = 0; |
| 8071 | if( p->outCount ){ |
| 8072 | output_reset(p); |
| 8073 | p->outCount = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 8074 | }else{ |
| 8075 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8076 | } |
| 8077 | }else if( nSql && _all_whitespace(zSql) ){ |
| 8078 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
| 8079 | nSql = 0; |
| 8080 | } |
| 8081 | } |
| 8082 | if( nSql && !_all_whitespace(zSql) ){ |
drh | fc29a86 | 2018-05-11 19:11:18 +0000 | [diff] [blame] | 8083 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8084 | } |
| 8085 | free(zSql); |
| 8086 | free(zLine); |
| 8087 | return errCnt>0; |
| 8088 | } |
| 8089 | |
| 8090 | /* |
| 8091 | ** Return a pathname which is the user's home directory. A |
| 8092 | ** 0 return indicates an error of some kind. |
| 8093 | */ |
| 8094 | static char *find_home_dir(int clearFlag){ |
| 8095 | static char *home_dir = NULL; |
| 8096 | if( clearFlag ){ |
| 8097 | free(home_dir); |
| 8098 | home_dir = 0; |
| 8099 | return 0; |
| 8100 | } |
| 8101 | if( home_dir ) return home_dir; |
| 8102 | |
| 8103 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 8104 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 8105 | { |
| 8106 | struct passwd *pwent; |
| 8107 | uid_t uid = getuid(); |
| 8108 | if( (pwent=getpwuid(uid)) != NULL) { |
| 8109 | home_dir = pwent->pw_dir; |
| 8110 | } |
| 8111 | } |
| 8112 | #endif |
| 8113 | |
| 8114 | #if defined(_WIN32_WCE) |
| 8115 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 8116 | */ |
| 8117 | home_dir = "/"; |
| 8118 | #else |
| 8119 | |
| 8120 | #if defined(_WIN32) || defined(WIN32) |
| 8121 | if (!home_dir) { |
| 8122 | home_dir = getenv("USERPROFILE"); |
| 8123 | } |
| 8124 | #endif |
| 8125 | |
| 8126 | if (!home_dir) { |
| 8127 | home_dir = getenv("HOME"); |
| 8128 | } |
| 8129 | |
| 8130 | #if defined(_WIN32) || defined(WIN32) |
| 8131 | if (!home_dir) { |
| 8132 | char *zDrive, *zPath; |
| 8133 | int n; |
| 8134 | zDrive = getenv("HOMEDRIVE"); |
| 8135 | zPath = getenv("HOMEPATH"); |
| 8136 | if( zDrive && zPath ){ |
| 8137 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
| 8138 | home_dir = malloc( n ); |
| 8139 | if( home_dir==0 ) return 0; |
| 8140 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 8141 | return home_dir; |
| 8142 | } |
| 8143 | home_dir = "c:\\"; |
| 8144 | } |
| 8145 | #endif |
| 8146 | |
| 8147 | #endif /* !_WIN32_WCE */ |
| 8148 | |
| 8149 | if( home_dir ){ |
| 8150 | int n = strlen30(home_dir) + 1; |
| 8151 | char *z = malloc( n ); |
| 8152 | if( z ) memcpy(z, home_dir, n); |
| 8153 | home_dir = z; |
| 8154 | } |
| 8155 | |
| 8156 | return home_dir; |
| 8157 | } |
| 8158 | |
| 8159 | /* |
| 8160 | ** Read input from the file given by sqliterc_override. Or if that |
| 8161 | ** parameter is NULL, take input from ~/.sqliterc |
| 8162 | ** |
| 8163 | ** Returns the number of errors. |
| 8164 | */ |
| 8165 | static void process_sqliterc( |
| 8166 | ShellState *p, /* Configuration data */ |
| 8167 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 8168 | ){ |
| 8169 | char *home_dir = NULL; |
| 8170 | const char *sqliterc = sqliterc_override; |
| 8171 | char *zBuf = 0; |
| 8172 | FILE *in = NULL; |
| 8173 | |
| 8174 | if (sqliterc == NULL) { |
| 8175 | home_dir = find_home_dir(0); |
| 8176 | if( home_dir==0 ){ |
| 8177 | raw_printf(stderr, "-- warning: cannot find home directory;" |
| 8178 | " cannot read ~/.sqliterc\n"); |
| 8179 | return; |
| 8180 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8181 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 8182 | sqliterc = zBuf; |
| 8183 | } |
| 8184 | in = fopen(sqliterc,"rb"); |
| 8185 | if( in ){ |
| 8186 | if( stdin_is_interactive ){ |
| 8187 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
| 8188 | } |
| 8189 | process_input(p,in); |
| 8190 | fclose(in); |
| 8191 | } |
| 8192 | sqlite3_free(zBuf); |
| 8193 | } |
| 8194 | |
| 8195 | /* |
| 8196 | ** Show available command line options |
| 8197 | */ |
| 8198 | static const char zOptions[] = |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8199 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | ad7fd5d | 2018-03-05 20:21:50 +0000 | [diff] [blame] | 8200 | " -A ARGS... run \".archive ARGS\" and exit\n" |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8201 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 8202 | " -append append the database to the end of the file\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8203 | " -ascii set output mode to 'ascii'\n" |
| 8204 | " -bail stop after hitting an error\n" |
| 8205 | " -batch force batch I/O\n" |
| 8206 | " -column set output mode to 'column'\n" |
| 8207 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
| 8208 | " -csv set output mode to 'csv'\n" |
| 8209 | " -echo print commands before execution\n" |
| 8210 | " -init FILENAME read/process named file\n" |
| 8211 | " -[no]header turn headers on or off\n" |
| 8212 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 8213 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 8214 | #endif |
| 8215 | " -help show this message\n" |
| 8216 | " -html set output mode to HTML\n" |
| 8217 | " -interactive force interactive I/O\n" |
| 8218 | " -line set output mode to 'line'\n" |
| 8219 | " -list set output mode to 'list'\n" |
| 8220 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
| 8221 | " -mmap N default mmap size set to N\n" |
| 8222 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8223 | " -multiplex enable the multiplexor VFS\n" |
| 8224 | #endif |
| 8225 | " -newline SEP set output row separator. Default: '\\n'\n" |
| 8226 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 8227 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 8228 | " -quote set output mode to 'quote'\n" |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 8229 | " -readonly open the database read-only\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8230 | " -separator SEP set output column separator. Default: '|'\n" |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 8231 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 8232 | " -sorterref SIZE sorter references threshold size\n" |
| 8233 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8234 | " -stats print memory stats before each finalize\n" |
| 8235 | " -version show SQLite version\n" |
| 8236 | " -vfs NAME use NAME as the default VFS\n" |
| 8237 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 8238 | " -vfstrace enable tracing of all VFS calls\n" |
| 8239 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 8240 | #ifdef SQLITE_HAVE_ZLIB |
| 8241 | " -zip open the file as a ZIP Archive\n" |
| 8242 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8243 | ; |
| 8244 | static void usage(int showDetail){ |
| 8245 | utf8_printf(stderr, |
| 8246 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 8247 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 8248 | "if the file does not previously exist.\n", Argv0); |
| 8249 | if( showDetail ){ |
| 8250 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
| 8251 | }else{ |
| 8252 | raw_printf(stderr, "Use the -help option for additional information\n"); |
| 8253 | } |
| 8254 | exit(1); |
| 8255 | } |
| 8256 | |
| 8257 | /* |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 8258 | ** Internal check: Verify that the SQLite is uninitialized. Print a |
| 8259 | ** error message if it is initialized. |
| 8260 | */ |
| 8261 | static void verify_uninitialized(void){ |
| 8262 | if( sqlite3_config(-1)==SQLITE_MISUSE ){ |
drh | 8e02a18 | 2018-05-30 07:24:41 +0000 | [diff] [blame] | 8263 | utf8_printf(stdout, "WARNING: attempt to configure SQLite after" |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 8264 | " initialization.\n"); |
| 8265 | } |
| 8266 | } |
| 8267 | |
| 8268 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8269 | ** Initialize the state information in data |
| 8270 | */ |
| 8271 | static void main_init(ShellState *data) { |
| 8272 | memset(data, 0, sizeof(*data)); |
| 8273 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 8274 | data->autoExplain = 1; |
| 8275 | memcpy(data->colSeparator,SEP_Column, 2); |
| 8276 | memcpy(data->rowSeparator,SEP_Row, 2); |
| 8277 | data->showHeader = 0; |
| 8278 | data->shellFlgs = SHFLG_Lookaside; |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 8279 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8280 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
| 8281 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
| 8282 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 8283 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 8284 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
| 8285 | } |
| 8286 | |
| 8287 | /* |
| 8288 | ** Output text to the console in a font that attracts extra attention. |
| 8289 | */ |
| 8290 | #ifdef _WIN32 |
| 8291 | static void printBold(const char *zText){ |
| 8292 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 8293 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 8294 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 8295 | SetConsoleTextAttribute(out, |
| 8296 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 8297 | ); |
| 8298 | printf("%s", zText); |
| 8299 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
| 8300 | } |
| 8301 | #else |
| 8302 | static void printBold(const char *zText){ |
| 8303 | printf("\033[1m%s\033[0m", zText); |
| 8304 | } |
| 8305 | #endif |
| 8306 | |
| 8307 | /* |
| 8308 | ** Get the argument to an --option. Throw an error and die if no argument |
| 8309 | ** is available. |
| 8310 | */ |
| 8311 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 8312 | if( i==argc ){ |
| 8313 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
| 8314 | argv[0], argv[argc-1]); |
| 8315 | exit(1); |
| 8316 | } |
| 8317 | return argv[i]; |
| 8318 | } |
| 8319 | |
| 8320 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 8321 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 8322 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 8323 | # else |
| 8324 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 8325 | # endif |
| 8326 | #endif |
| 8327 | |
| 8328 | #if SQLITE_SHELL_IS_UTF8 |
| 8329 | int SQLITE_CDECL main(int argc, char **argv){ |
| 8330 | #else |
| 8331 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 8332 | char **argv; |
| 8333 | #endif |
| 8334 | char *zErrMsg = 0; |
| 8335 | ShellState data; |
| 8336 | const char *zInitFile = 0; |
| 8337 | int i; |
| 8338 | int rc = 0; |
| 8339 | int warnInmemoryDb = 0; |
| 8340 | int readStdin = 1; |
| 8341 | int nCmd = 0; |
| 8342 | char **azCmd = 0; |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 8343 | const char *zVfs = 0; /* Value of -vfs command-line option */ |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 8344 | #if !SQLITE_SHELL_IS_UTF8 |
| 8345 | char **argvToFree = 0; |
| 8346 | int argcToFree = 0; |
| 8347 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8348 | |
| 8349 | setBinaryMode(stdin, 0); |
| 8350 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 8351 | stdin_is_interactive = isatty(0); |
| 8352 | stdout_is_console = isatty(1); |
| 8353 | |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 8354 | #if !defined(_WIN32_WCE) |
| 8355 | if( getenv("SQLITE_DEBUG_BREAK") ){ |
| 8356 | if( isatty(0) && isatty(2) ){ |
| 8357 | fprintf(stderr, |
| 8358 | "attach debugger to process %d and press any key to continue.\n", |
| 8359 | GETPID()); |
| 8360 | fgetc(stdin); |
| 8361 | }else{ |
| 8362 | #if defined(_WIN32) || defined(WIN32) |
| 8363 | DebugBreak(); |
| 8364 | #elif defined(SIGTRAP) |
| 8365 | raise(SIGTRAP); |
| 8366 | #endif |
| 8367 | } |
| 8368 | } |
| 8369 | #endif |
| 8370 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8371 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 8372 | if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8373 | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
| 8374 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 8375 | exit(1); |
| 8376 | } |
| 8377 | #endif |
| 8378 | main_init(&data); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8379 | |
| 8380 | /* On Windows, we must translate command-line arguments into UTF-8. |
| 8381 | ** The SQLite memory allocator subsystem has to be enabled in order to |
| 8382 | ** do this. But we want to run an sqlite3_shutdown() afterwards so that |
| 8383 | ** subsequent sqlite3_config() calls will work. So copy all results into |
| 8384 | ** memory that does not come from the SQLite memory allocator. |
| 8385 | */ |
drh | 4b18c1d | 2018-02-04 20:33:13 +0000 | [diff] [blame] | 8386 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8387 | sqlite3_initialize(); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 8388 | argvToFree = malloc(sizeof(argv[0])*argc*2); |
| 8389 | argcToFree = argc; |
| 8390 | argv = argvToFree + argc; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8391 | if( argv==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8392 | for(i=0; i<argc; i++){ |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8393 | char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 8394 | int n; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8395 | if( z==0 ) shell_out_of_memory(); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8396 | n = (int)strlen(z); |
| 8397 | argv[i] = malloc( n+1 ); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8398 | if( argv[i]==0 ) shell_out_of_memory(); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8399 | memcpy(argv[i], z, n+1); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 8400 | argvToFree[i] = argv[i]; |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8401 | sqlite3_free(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8402 | } |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8403 | sqlite3_shutdown(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8404 | #endif |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 8405 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8406 | assert( argc>=1 && argv && argv[0] ); |
| 8407 | Argv0 = argv[0]; |
| 8408 | |
| 8409 | /* Make sure we have a valid signal handler early, before anything |
| 8410 | ** else is done. |
| 8411 | */ |
| 8412 | #ifdef SIGINT |
| 8413 | signal(SIGINT, interrupt_handler); |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 8414 | #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 8415 | SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8416 | #endif |
| 8417 | |
| 8418 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 8419 | { |
| 8420 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 8421 | ** of a C-function that will provide the name of the database file. Use |
| 8422 | ** this compile-time option to embed this shell program in larger |
| 8423 | ** applications. */ |
| 8424 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 8425 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 8426 | warnInmemoryDb = 0; |
| 8427 | } |
| 8428 | #endif |
| 8429 | |
| 8430 | /* Do an initial pass through the command-line argument to locate |
| 8431 | ** the name of the database file, the name of the initialization file, |
| 8432 | ** the size of the alternative malloc heap, |
| 8433 | ** and the first command to execute. |
| 8434 | */ |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 8435 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8436 | for(i=1; i<argc; i++){ |
| 8437 | char *z; |
| 8438 | z = argv[i]; |
| 8439 | if( z[0]!='-' ){ |
| 8440 | if( data.zDbFilename==0 ){ |
| 8441 | data.zDbFilename = z; |
| 8442 | }else{ |
| 8443 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 8444 | ** mean that nothing is read from stdin */ |
| 8445 | readStdin = 0; |
| 8446 | nCmd++; |
| 8447 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8448 | if( azCmd==0 ) shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8449 | azCmd[nCmd-1] = z; |
| 8450 | } |
| 8451 | } |
| 8452 | if( z[1]=='-' ) z++; |
| 8453 | if( strcmp(z,"-separator")==0 |
| 8454 | || strcmp(z,"-nullvalue")==0 |
| 8455 | || strcmp(z,"-newline")==0 |
| 8456 | || strcmp(z,"-cmd")==0 |
| 8457 | ){ |
| 8458 | (void)cmdline_option_value(argc, argv, ++i); |
| 8459 | }else if( strcmp(z,"-init")==0 ){ |
| 8460 | zInitFile = cmdline_option_value(argc, argv, ++i); |
| 8461 | }else if( strcmp(z,"-batch")==0 ){ |
| 8462 | /* Need to check for batch mode here to so we can avoid printing |
| 8463 | ** informational messages (like from process_sqliterc) before |
| 8464 | ** we do the actual processing of arguments later in a second pass. |
| 8465 | */ |
| 8466 | stdin_is_interactive = 0; |
| 8467 | }else if( strcmp(z,"-heap")==0 ){ |
| 8468 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 8469 | const char *zSize; |
| 8470 | sqlite3_int64 szHeap; |
| 8471 | |
| 8472 | zSize = cmdline_option_value(argc, argv, ++i); |
| 8473 | szHeap = integerValue(zSize); |
| 8474 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
| 8475 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
| 8476 | #else |
| 8477 | (void)cmdline_option_value(argc, argv, ++i); |
| 8478 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8479 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8480 | int n, sz; |
| 8481 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8482 | if( sz>70000 ) sz = 70000; |
| 8483 | if( sz<0 ) sz = 0; |
| 8484 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8485 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 8486 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
| 8487 | data.shellFlgs |= SHFLG_Pagecache; |
| 8488 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8489 | int n, sz; |
| 8490 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8491 | if( sz<0 ) sz = 0; |
| 8492 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 8493 | if( n<0 ) n = 0; |
| 8494 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 8495 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
| 8496 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 8497 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 8498 | extern int vfstrace_register( |
| 8499 | const char *zTraceName, |
| 8500 | const char *zOldVfsName, |
| 8501 | int (*xOut)(const char*,void*), |
| 8502 | void *pOutArg, |
| 8503 | int makeDefault |
| 8504 | ); |
| 8505 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
| 8506 | #endif |
| 8507 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8508 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 8509 | extern int sqlite3_multiple_initialize(const char*,int); |
| 8510 | sqlite3_multiplex_initialize(0, 1); |
| 8511 | #endif |
| 8512 | }else if( strcmp(z,"-mmap")==0 ){ |
| 8513 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 8514 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 8515 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 8516 | }else if( strcmp(z,"-sorterref")==0 ){ |
| 8517 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 8518 | sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); |
| 8519 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8520 | }else if( strcmp(z,"-vfs")==0 ){ |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 8521 | zVfs = cmdline_option_value(argc, argv, ++i); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 8522 | #ifdef SQLITE_HAVE_ZLIB |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 8523 | }else if( strcmp(z,"-zip")==0 ){ |
| 8524 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 8525 | #endif |
| 8526 | }else if( strcmp(z,"-append")==0 ){ |
| 8527 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 8528 | }else if( strcmp(z,"-readonly")==0 ){ |
| 8529 | data.openMode = SHELL_OPEN_READONLY; |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8530 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 8531 | }else if( strncmp(z, "-A",2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8532 | /* All remaining command-line arguments are passed to the ".archive" |
| 8533 | ** command, so ignore them */ |
| 8534 | break; |
| 8535 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8536 | } |
| 8537 | } |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 8538 | verify_uninitialized(); |
| 8539 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 8540 | |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 8541 | #ifdef SQLITE_SHELL_INIT_PROC |
| 8542 | { |
| 8543 | /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name |
| 8544 | ** of a C-function that will perform initialization actions on SQLite that |
| 8545 | ** occur just before or after sqlite3_initialize(). Use this compile-time |
| 8546 | ** option to embed this shell program in larger applications. */ |
| 8547 | extern void SQLITE_SHELL_INIT_PROC(void); |
| 8548 | SQLITE_SHELL_INIT_PROC(); |
| 8549 | } |
| 8550 | #else |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 8551 | /* All the sqlite3_config() calls have now been made. So it is safe |
| 8552 | ** to call sqlite3_initialize() and process any command line -vfs option. */ |
| 8553 | sqlite3_initialize(); |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 8554 | #endif |
| 8555 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 8556 | if( zVfs ){ |
| 8557 | sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); |
| 8558 | if( pVfs ){ |
| 8559 | sqlite3_vfs_register(pVfs, 1); |
| 8560 | }else{ |
| 8561 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
| 8562 | exit(1); |
| 8563 | } |
| 8564 | } |
| 8565 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8566 | if( data.zDbFilename==0 ){ |
| 8567 | #ifndef SQLITE_OMIT_MEMORYDB |
| 8568 | data.zDbFilename = ":memory:"; |
| 8569 | warnInmemoryDb = argc==1; |
| 8570 | #else |
| 8571 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 8572 | return 1; |
| 8573 | #endif |
| 8574 | } |
| 8575 | data.out = stdout; |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 8576 | sqlite3_appendvfs_init(0,0,0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8577 | |
| 8578 | /* Go ahead and open the database file if it already exists. If the |
| 8579 | ** file does not exist, delay opening it. This prevents empty database |
| 8580 | ** files from being created if a user mistypes the database name argument |
| 8581 | ** to the sqlite command-line tool. |
| 8582 | */ |
| 8583 | if( access(data.zDbFilename, 0)==0 ){ |
| 8584 | open_db(&data, 0); |
| 8585 | } |
| 8586 | |
| 8587 | /* Process the initialization file if there is one. If no -init option |
| 8588 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 8589 | ** try to process it. |
| 8590 | */ |
| 8591 | process_sqliterc(&data,zInitFile); |
| 8592 | |
| 8593 | /* Make a second pass through the command-line argument and set |
| 8594 | ** options. This second pass is delayed until after the initialization |
| 8595 | ** file is processed so that the command-line arguments will override |
| 8596 | ** settings in the initialization file. |
| 8597 | */ |
| 8598 | for(i=1; i<argc; i++){ |
| 8599 | char *z = argv[i]; |
| 8600 | if( z[0]!='-' ) continue; |
| 8601 | if( z[1]=='-' ){ z++; } |
| 8602 | if( strcmp(z,"-init")==0 ){ |
| 8603 | i++; |
| 8604 | }else if( strcmp(z,"-html")==0 ){ |
| 8605 | data.mode = MODE_Html; |
| 8606 | }else if( strcmp(z,"-list")==0 ){ |
| 8607 | data.mode = MODE_List; |
| 8608 | }else if( strcmp(z,"-quote")==0 ){ |
| 8609 | data.mode = MODE_Quote; |
| 8610 | }else if( strcmp(z,"-line")==0 ){ |
| 8611 | data.mode = MODE_Line; |
| 8612 | }else if( strcmp(z,"-column")==0 ){ |
| 8613 | data.mode = MODE_Column; |
| 8614 | }else if( strcmp(z,"-csv")==0 ){ |
| 8615 | data.mode = MODE_Csv; |
| 8616 | memcpy(data.colSeparator,",",2); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 8617 | #ifdef SQLITE_HAVE_ZLIB |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 8618 | }else if( strcmp(z,"-zip")==0 ){ |
| 8619 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 8620 | #endif |
| 8621 | }else if( strcmp(z,"-append")==0 ){ |
| 8622 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 4aafe59 | 2018-03-23 16:08:30 +0000 | [diff] [blame] | 8623 | }else if( strcmp(z,"-readonly")==0 ){ |
| 8624 | data.openMode = SHELL_OPEN_READONLY; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8625 | }else if( strcmp(z,"-ascii")==0 ){ |
| 8626 | data.mode = MODE_Ascii; |
| 8627 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 8628 | SEP_Unit); |
| 8629 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 8630 | SEP_Record); |
| 8631 | }else if( strcmp(z,"-separator")==0 ){ |
| 8632 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 8633 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8634 | }else if( strcmp(z,"-newline")==0 ){ |
| 8635 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 8636 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8637 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 8638 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
| 8639 | "%s",cmdline_option_value(argc,argv,++i)); |
| 8640 | }else if( strcmp(z,"-header")==0 ){ |
| 8641 | data.showHeader = 1; |
| 8642 | }else if( strcmp(z,"-noheader")==0 ){ |
| 8643 | data.showHeader = 0; |
| 8644 | }else if( strcmp(z,"-echo")==0 ){ |
| 8645 | ShellSetFlag(&data, SHFLG_Echo); |
| 8646 | }else if( strcmp(z,"-eqp")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8647 | data.autoEQP = AUTOEQP_on; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8648 | }else if( strcmp(z,"-eqpfull")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8649 | data.autoEQP = AUTOEQP_full; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8650 | }else if( strcmp(z,"-stats")==0 ){ |
| 8651 | data.statsOn = 1; |
| 8652 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 8653 | data.scanstatsOn = 1; |
| 8654 | }else if( strcmp(z,"-backslash")==0 ){ |
| 8655 | /* Undocumented command-line option: -backslash |
| 8656 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 8657 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 8658 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 8659 | */ |
| 8660 | ShellSetFlag(&data, SHFLG_Backslash); |
| 8661 | }else if( strcmp(z,"-bail")==0 ){ |
| 8662 | bail_on_error = 1; |
| 8663 | }else if( strcmp(z,"-version")==0 ){ |
| 8664 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 8665 | return 0; |
| 8666 | }else if( strcmp(z,"-interactive")==0 ){ |
| 8667 | stdin_is_interactive = 1; |
| 8668 | }else if( strcmp(z,"-batch")==0 ){ |
| 8669 | stdin_is_interactive = 0; |
| 8670 | }else if( strcmp(z,"-heap")==0 ){ |
| 8671 | i++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8672 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8673 | i+=2; |
| 8674 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8675 | i+=2; |
| 8676 | }else if( strcmp(z,"-mmap")==0 ){ |
| 8677 | i++; |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 8678 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 8679 | }else if( strcmp(z,"-sorterref")==0 ){ |
| 8680 | i++; |
| 8681 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8682 | }else if( strcmp(z,"-vfs")==0 ){ |
| 8683 | i++; |
| 8684 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 8685 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 8686 | i++; |
| 8687 | #endif |
| 8688 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8689 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 8690 | i++; |
| 8691 | #endif |
| 8692 | }else if( strcmp(z,"-help")==0 ){ |
| 8693 | usage(1); |
| 8694 | }else if( strcmp(z,"-cmd")==0 ){ |
| 8695 | /* Run commands that follow -cmd first and separately from commands |
| 8696 | ** that simply appear on the command-line. This seems goofy. It would |
| 8697 | ** be better if all commands ran in the order that they appear. But |
| 8698 | ** we retain the goofy behavior for historical compatibility. */ |
| 8699 | if( i==argc-1 ) break; |
| 8700 | z = cmdline_option_value(argc,argv,++i); |
| 8701 | if( z[0]=='.' ){ |
| 8702 | rc = do_meta_command(z, &data); |
| 8703 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
| 8704 | }else{ |
| 8705 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 8706 | rc = shell_exec(&data, z, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8707 | if( zErrMsg!=0 ){ |
| 8708 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 8709 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 8710 | }else if( rc!=0 ){ |
| 8711 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
| 8712 | if( bail_on_error ) return rc; |
| 8713 | } |
| 8714 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8715 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 8716 | }else if( strncmp(z, "-A", 2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8717 | if( nCmd>0 ){ |
| 8718 | utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" |
| 8719 | " with \"%s\"\n", z); |
| 8720 | return 1; |
| 8721 | } |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 8722 | open_db(&data, OPEN_DB_ZIPFILE); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 8723 | if( z[2] ){ |
| 8724 | argv[i] = &z[2]; |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 8725 | arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 8726 | }else{ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 8727 | arDotCommand(&data, 1, argv+i, argc-i); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 8728 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 8729 | readStdin = 0; |
| 8730 | break; |
| 8731 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8732 | }else{ |
| 8733 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 8734 | raw_printf(stderr,"Use -help for a list of options.\n"); |
| 8735 | return 1; |
| 8736 | } |
| 8737 | data.cMode = data.mode; |
| 8738 | } |
| 8739 | |
| 8740 | if( !readStdin ){ |
| 8741 | /* Run all arguments that do not begin with '-' as if they were separate |
| 8742 | ** command-line inputs, except for the argToSkip argument which contains |
| 8743 | ** the database filename. |
| 8744 | */ |
| 8745 | for(i=0; i<nCmd; i++){ |
| 8746 | if( azCmd[i][0]=='.' ){ |
| 8747 | rc = do_meta_command(azCmd[i], &data); |
| 8748 | if( rc ) return rc==2 ? 0 : rc; |
| 8749 | }else{ |
| 8750 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 8751 | rc = shell_exec(&data, azCmd[i], &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8752 | if( zErrMsg!=0 ){ |
| 8753 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 8754 | return rc!=0 ? rc : 1; |
| 8755 | }else if( rc!=0 ){ |
| 8756 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| 8757 | return rc; |
| 8758 | } |
| 8759 | } |
| 8760 | } |
| 8761 | free(azCmd); |
| 8762 | }else{ |
| 8763 | /* Run commands received from standard input |
| 8764 | */ |
| 8765 | if( stdin_is_interactive ){ |
| 8766 | char *zHome; |
| 8767 | char *zHistory = 0; |
| 8768 | int nHistory; |
| 8769 | printf( |
| 8770 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
| 8771 | "Enter \".help\" for usage hints.\n", |
| 8772 | sqlite3_libversion(), sqlite3_sourceid() |
| 8773 | ); |
| 8774 | if( warnInmemoryDb ){ |
| 8775 | printf("Connected to a "); |
| 8776 | printBold("transient in-memory database"); |
| 8777 | printf(".\nUse \".open FILENAME\" to reopen on a " |
| 8778 | "persistent database.\n"); |
| 8779 | } |
| 8780 | zHome = find_home_dir(0); |
| 8781 | if( zHome ){ |
| 8782 | nHistory = strlen30(zHome) + 20; |
| 8783 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 8784 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 8785 | } |
| 8786 | } |
| 8787 | if( zHistory ){ shell_read_history(zHistory); } |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 8788 | #if HAVE_READLINE || HAVE_EDITLINE |
| 8789 | rl_attempted_completion_function = readline_completion; |
| 8790 | #elif HAVE_LINENOISE |
| 8791 | linenoiseSetCompletionCallback(linenoise_completion); |
| 8792 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8793 | rc = process_input(&data, 0); |
| 8794 | if( zHistory ){ |
drh | 5a75dd8 | 2017-07-18 20:59:40 +0000 | [diff] [blame] | 8795 | shell_stifle_history(2000); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8796 | shell_write_history(zHistory); |
| 8797 | free(zHistory); |
| 8798 | } |
| 8799 | }else{ |
| 8800 | rc = process_input(&data, stdin); |
| 8801 | } |
| 8802 | } |
| 8803 | set_table_name(&data, 0); |
| 8804 | if( data.db ){ |
| 8805 | session_close_all(&data); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 8806 | close_db(data.db); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8807 | } |
| 8808 | sqlite3_free(data.zFreeOnClose); |
| 8809 | find_home_dir(1); |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 8810 | output_reset(&data); |
| 8811 | data.doXdgOpen = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 8812 | clearTempFile(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8813 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 8814 | for(i=0; i<argcToFree; i++) free(argvToFree[i]); |
| 8815 | free(argvToFree); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8816 | #endif |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 8817 | /* Clear the global data structure so that valgrind will detect memory |
| 8818 | ** leaks */ |
| 8819 | memset(&data, 0, sizeof(data)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8820 | return rc; |
| 8821 | } |