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 | /* |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 21 | ** Optionally #include a user-defined header, whereby compilation options |
| 22 | ** may be set prior to where they take effect, but after platform setup. |
| 23 | ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include |
| 24 | ** file. Note that this macro has a like effect on sqlite3.c compilation. |
| 25 | */ |
larrybr | 7813839 | 2022-02-12 02:15:37 +0000 | [diff] [blame] | 26 | # define SHELL_STRINGIFY_(f) #f |
| 27 | # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f) |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 28 | #ifdef SQLITE_CUSTOM_INCLUDE |
larrybr | 7813839 | 2022-02-12 02:15:37 +0000 | [diff] [blame] | 29 | # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE) |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | /* |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 33 | ** Determine if we are dealing with WinRT, which provides only a subset of |
| 34 | ** the full Win32 API. |
| 35 | */ |
| 36 | #if !defined(SQLITE_OS_WINRT) |
| 37 | # define SQLITE_OS_WINRT 0 |
| 38 | #endif |
| 39 | |
| 40 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 41 | ** Warning pragmas copied from msvc.h in the core. |
| 42 | */ |
| 43 | #if defined(_MSC_VER) |
| 44 | #pragma warning(disable : 4054) |
| 45 | #pragma warning(disable : 4055) |
| 46 | #pragma warning(disable : 4100) |
| 47 | #pragma warning(disable : 4127) |
| 48 | #pragma warning(disable : 4130) |
| 49 | #pragma warning(disable : 4152) |
| 50 | #pragma warning(disable : 4189) |
| 51 | #pragma warning(disable : 4206) |
| 52 | #pragma warning(disable : 4210) |
| 53 | #pragma warning(disable : 4232) |
| 54 | #pragma warning(disable : 4244) |
| 55 | #pragma warning(disable : 4305) |
| 56 | #pragma warning(disable : 4306) |
| 57 | #pragma warning(disable : 4702) |
| 58 | #pragma warning(disable : 4706) |
| 59 | #endif /* defined(_MSC_VER) */ |
| 60 | |
| 61 | /* |
| 62 | ** No support for loadable extensions in VxWorks. |
| 63 | */ |
| 64 | #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
| 65 | # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 66 | #endif |
| 67 | |
| 68 | /* |
| 69 | ** Enable large-file support for fopen() and friends on unix. |
| 70 | */ |
| 71 | #ifndef SQLITE_DISABLE_LFS |
| 72 | # define _LARGE_FILE 1 |
| 73 | # ifndef _FILE_OFFSET_BITS |
| 74 | # define _FILE_OFFSET_BITS 64 |
| 75 | # endif |
| 76 | # define _LARGEFILE_SOURCE 1 |
| 77 | #endif |
| 78 | |
| 79 | #include <stdlib.h> |
| 80 | #include <string.h> |
| 81 | #include <stdio.h> |
| 82 | #include <assert.h> |
| 83 | #include "sqlite3.h" |
drh | 1e506b5 | 2018-01-05 21:01:37 +0000 | [diff] [blame] | 84 | typedef sqlite3_int64 i64; |
| 85 | typedef sqlite3_uint64 u64; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 86 | typedef unsigned char u8; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 87 | #if SQLITE_USER_AUTHENTICATION |
| 88 | # include "sqlite3userauth.h" |
| 89 | #endif |
| 90 | #include <ctype.h> |
| 91 | #include <stdarg.h> |
| 92 | |
| 93 | #if !defined(_WIN32) && !defined(WIN32) |
| 94 | # include <signal.h> |
| 95 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 96 | # include <pwd.h> |
| 97 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 98 | #endif |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 99 | #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 100 | # include <unistd.h> |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 101 | # include <dirent.h> |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 102 | # define GETPID getpid |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 103 | # if defined(__MINGW32__) |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 104 | # define DIRENT dirent |
mistachkin | 2f74b3c | 2018-01-05 20:26:06 +0000 | [diff] [blame] | 105 | # ifndef S_ISLNK |
| 106 | # define S_ISLNK(mode) (0) |
| 107 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 108 | # endif |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 109 | #else |
| 110 | # define GETPID (int)GetCurrentProcessId |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 111 | #endif |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 112 | #include <sys/types.h> |
| 113 | #include <sys/stat.h> |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 114 | |
| 115 | #if HAVE_READLINE |
| 116 | # include <readline/readline.h> |
| 117 | # include <readline/history.h> |
| 118 | #endif |
| 119 | |
| 120 | #if HAVE_EDITLINE |
| 121 | # include <editline/readline.h> |
| 122 | #endif |
| 123 | |
| 124 | #if HAVE_EDITLINE || HAVE_READLINE |
| 125 | |
| 126 | # define shell_add_history(X) add_history(X) |
| 127 | # define shell_read_history(X) read_history(X) |
| 128 | # define shell_write_history(X) write_history(X) |
| 129 | # define shell_stifle_history(X) stifle_history(X) |
| 130 | # define shell_readline(X) readline(X) |
| 131 | |
| 132 | #elif HAVE_LINENOISE |
| 133 | |
| 134 | # include "linenoise.h" |
| 135 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 136 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 137 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 138 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 139 | # define shell_readline(X) linenoise(X) |
| 140 | |
| 141 | #else |
| 142 | |
| 143 | # define shell_read_history(X) |
| 144 | # define shell_write_history(X) |
| 145 | # define shell_stifle_history(X) |
| 146 | |
| 147 | # define SHELL_USE_LOCAL_GETLINE 1 |
| 148 | #endif |
| 149 | |
| 150 | |
| 151 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 152 | # if SQLITE_OS_WINRT |
| 153 | # define SQLITE_OMIT_POPEN 1 |
| 154 | # else |
| 155 | # include <io.h> |
| 156 | # include <fcntl.h> |
| 157 | # define isatty(h) _isatty(h) |
| 158 | # ifndef access |
| 159 | # define access(f,m) _access((f),(m)) |
| 160 | # endif |
| 161 | # ifndef unlink |
| 162 | # define unlink _unlink |
| 163 | # endif |
| 164 | # ifndef strdup |
| 165 | # define strdup _strdup |
| 166 | # endif |
| 167 | # undef popen |
| 168 | # define popen _popen |
| 169 | # undef pclose |
| 170 | # define pclose _pclose |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 171 | # endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 172 | #else |
| 173 | /* Make sure isatty() has a prototype. */ |
| 174 | extern int isatty(int); |
| 175 | |
| 176 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 177 | /* popen and pclose are not C89 functions and so are |
| 178 | ** sometimes omitted from the <stdio.h> header */ |
| 179 | extern FILE *popen(const char*,const char*); |
| 180 | extern int pclose(FILE*); |
| 181 | # else |
| 182 | # define SQLITE_OMIT_POPEN 1 |
| 183 | # endif |
| 184 | #endif |
| 185 | |
| 186 | #if defined(_WIN32_WCE) |
| 187 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 188 | * thus we always assume that we have a console. That can be |
| 189 | * overridden with the -batch command line option. |
| 190 | */ |
| 191 | #define isatty(x) 1 |
| 192 | #endif |
| 193 | |
| 194 | /* ctype macros that work with signed characters */ |
| 195 | #define IsSpace(X) isspace((unsigned char)X) |
| 196 | #define IsDigit(X) isdigit((unsigned char)X) |
| 197 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 198 | |
| 199 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 200 | #if SQLITE_OS_WINRT |
| 201 | #include <intrin.h> |
| 202 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 203 | #include <windows.h> |
| 204 | |
| 205 | /* string conversion routines only needed on Win32 */ |
| 206 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 207 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 208 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| 209 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 210 | #endif |
| 211 | |
| 212 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 213 | ** are automatically translated into \r\n. However, this behavior needs |
| 214 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 215 | ** rendering quoted strings that contain \n characters). The following |
| 216 | ** routines take care of that. |
| 217 | */ |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 218 | #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 219 | static void setBinaryMode(FILE *file, int isOutput){ |
| 220 | if( isOutput ) fflush(file); |
| 221 | _setmode(_fileno(file), _O_BINARY); |
| 222 | } |
| 223 | static void setTextMode(FILE *file, int isOutput){ |
| 224 | if( isOutput ) fflush(file); |
| 225 | _setmode(_fileno(file), _O_TEXT); |
| 226 | } |
| 227 | #else |
| 228 | # define setBinaryMode(X,Y) |
| 229 | # define setTextMode(X,Y) |
| 230 | #endif |
| 231 | |
| 232 | |
| 233 | /* True if the timer is enabled */ |
| 234 | static int enableTimer = 0; |
| 235 | |
| 236 | /* Return the current wall-clock time */ |
| 237 | static sqlite3_int64 timeOfDay(void){ |
| 238 | static sqlite3_vfs *clockVfs = 0; |
| 239 | sqlite3_int64 t; |
| 240 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
drh | a959bf5 | 2021-06-15 15:15:40 +0000 | [diff] [blame] | 241 | if( clockVfs==0 ) return 0; /* Never actually happens */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 242 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 243 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 244 | }else{ |
| 245 | double r; |
| 246 | clockVfs->xCurrentTime(clockVfs, &r); |
| 247 | t = (sqlite3_int64)(r*86400000.0); |
| 248 | } |
| 249 | return t; |
| 250 | } |
| 251 | |
| 252 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
| 253 | #include <sys/time.h> |
| 254 | #include <sys/resource.h> |
| 255 | |
| 256 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 257 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 258 | struct rusage { |
| 259 | struct timeval ru_utime; /* user CPU time used */ |
| 260 | struct timeval ru_stime; /* system CPU time used */ |
| 261 | }; |
| 262 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 263 | #endif |
| 264 | |
| 265 | /* Saved resource information for the beginning of an operation */ |
| 266 | static struct rusage sBegin; /* CPU time at start */ |
| 267 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 268 | |
| 269 | /* |
| 270 | ** Begin timing an operation |
| 271 | */ |
| 272 | static void beginTimer(void){ |
| 273 | if( enableTimer ){ |
| 274 | getrusage(RUSAGE_SELF, &sBegin); |
| 275 | iBegin = timeOfDay(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* Return the difference of two time_structs in seconds */ |
| 280 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 281 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 282 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | ** Print the timing results. |
| 287 | */ |
| 288 | static void endTimer(void){ |
| 289 | if( enableTimer ){ |
| 290 | sqlite3_int64 iEnd = timeOfDay(); |
| 291 | struct rusage sEnd; |
| 292 | getrusage(RUSAGE_SELF, &sEnd); |
| 293 | printf("Run Time: real %.3f user %f sys %f\n", |
| 294 | (iEnd - iBegin)*0.001, |
| 295 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 296 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #define BEGIN_TIMER beginTimer() |
| 301 | #define END_TIMER endTimer() |
| 302 | #define HAS_TIMER 1 |
| 303 | |
| 304 | #elif (defined(_WIN32) || defined(WIN32)) |
| 305 | |
| 306 | /* Saved resource information for the beginning of an operation */ |
| 307 | static HANDLE hProcess; |
| 308 | static FILETIME ftKernelBegin; |
| 309 | static FILETIME ftUserBegin; |
| 310 | static sqlite3_int64 ftWallBegin; |
| 311 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 312 | LPFILETIME, LPFILETIME); |
| 313 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 314 | |
| 315 | /* |
| 316 | ** Check to see if we have timer support. Return 1 if necessary |
| 317 | ** support found (or found previously). |
| 318 | */ |
| 319 | static int hasTimer(void){ |
| 320 | if( getProcessTimesAddr ){ |
| 321 | return 1; |
| 322 | } else { |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 323 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 324 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 325 | ** versions. See if the version we are running on has it, and if it |
| 326 | ** does, save off a pointer to it and the current process handle. |
| 327 | */ |
| 328 | hProcess = GetCurrentProcess(); |
| 329 | if( hProcess ){ |
| 330 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 331 | if( NULL != hinstLib ){ |
| 332 | getProcessTimesAddr = |
| 333 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 334 | if( NULL != getProcessTimesAddr ){ |
| 335 | return 1; |
| 336 | } |
| 337 | FreeLibrary(hinstLib); |
| 338 | } |
| 339 | } |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 340 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | ** Begin timing an operation |
| 347 | */ |
| 348 | static void beginTimer(void){ |
| 349 | if( enableTimer && getProcessTimesAddr ){ |
| 350 | FILETIME ftCreation, ftExit; |
| 351 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 352 | &ftKernelBegin,&ftUserBegin); |
| 353 | ftWallBegin = timeOfDay(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* Return the difference of two FILETIME structs in seconds */ |
| 358 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 359 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 360 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 361 | return (double) ((i64End - i64Start) / 10000000.0); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | ** Print the timing results. |
| 366 | */ |
| 367 | static void endTimer(void){ |
| 368 | if( enableTimer && getProcessTimesAddr){ |
| 369 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 370 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 371 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 372 | printf("Run Time: real %.3f user %f sys %f\n", |
| 373 | (ftWallEnd - ftWallBegin)*0.001, |
| 374 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 375 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | #define BEGIN_TIMER beginTimer() |
| 380 | #define END_TIMER endTimer() |
| 381 | #define HAS_TIMER hasTimer() |
| 382 | |
| 383 | #else |
| 384 | #define BEGIN_TIMER |
| 385 | #define END_TIMER |
| 386 | #define HAS_TIMER 0 |
| 387 | #endif |
| 388 | |
| 389 | /* |
| 390 | ** Used to prevent warnings about unused parameters |
| 391 | */ |
| 392 | #define UNUSED_PARAMETER(x) (void)(x) |
| 393 | |
| 394 | /* |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 395 | ** Number of elements in an array |
| 396 | */ |
| 397 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
| 398 | |
| 399 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 400 | ** If the following flag is set, then command execution stops |
| 401 | ** at an error if we are not interactive. |
| 402 | */ |
| 403 | static int bail_on_error = 0; |
| 404 | |
| 405 | /* |
| 406 | ** Threat stdin as an interactive input if the following variable |
| 407 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 408 | */ |
| 409 | static int stdin_is_interactive = 1; |
| 410 | |
| 411 | /* |
| 412 | ** On Windows systems we have to know if standard output is a console |
| 413 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 414 | ** true if translation is required. |
| 415 | */ |
| 416 | static int stdout_is_console = 1; |
| 417 | |
| 418 | /* |
| 419 | ** The following is the open SQLite database. We make a pointer |
| 420 | ** to this database a static variable so that it can be accessed |
| 421 | ** by the SIGINT handler to interrupt database processing. |
| 422 | */ |
| 423 | static sqlite3 *globalDb = 0; |
| 424 | |
| 425 | /* |
| 426 | ** True if an interrupt (Control-C) has been received. |
| 427 | */ |
| 428 | static volatile int seenInterrupt = 0; |
| 429 | |
| 430 | /* |
| 431 | ** This is the name of our program. It is set in main(), used |
| 432 | ** in a number of other places, mostly for error messages. |
| 433 | */ |
| 434 | static char *Argv0; |
| 435 | |
| 436 | /* |
| 437 | ** Prompt strings. Initialized in main. Settable with |
| 438 | ** .prompt main continue |
| 439 | */ |
| 440 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 441 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 442 | |
| 443 | /* |
| 444 | ** Render output like fprintf(). Except, if the output is going to the |
| 445 | ** console and if this is running on a Windows machine, translate the |
| 446 | ** output from UTF-8 into MBCS. |
| 447 | */ |
| 448 | #if defined(_WIN32) || defined(WIN32) |
| 449 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 450 | va_list ap; |
| 451 | va_start(ap, zFormat); |
| 452 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
| 453 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
| 454 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
| 455 | sqlite3_free(z1); |
| 456 | fputs(z2, out); |
| 457 | sqlite3_free(z2); |
| 458 | }else{ |
| 459 | vfprintf(out, zFormat, ap); |
| 460 | } |
| 461 | va_end(ap); |
| 462 | } |
| 463 | #elif !defined(utf8_printf) |
| 464 | # define utf8_printf fprintf |
| 465 | #endif |
| 466 | |
| 467 | /* |
| 468 | ** Render output like fprintf(). This should not be used on anything that |
| 469 | ** includes string formatting (e.g. "%s"). |
| 470 | */ |
| 471 | #if !defined(raw_printf) |
| 472 | # define raw_printf fprintf |
| 473 | #endif |
| 474 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 475 | /* Indicate out-of-memory and exit. */ |
| 476 | static void shell_out_of_memory(void){ |
| 477 | raw_printf(stderr,"Error: out of memory\n"); |
| 478 | exit(1); |
| 479 | } |
| 480 | |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 481 | /* Check a pointer to see if it is NULL. If it is NULL, exit with an |
| 482 | ** out-of-memory error. |
| 483 | */ |
| 484 | static void shell_check_oom(void *p){ |
| 485 | if( p==0 ) shell_out_of_memory(); |
| 486 | } |
| 487 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 488 | /* |
| 489 | ** Write I/O traces to the following stream. |
| 490 | */ |
| 491 | #ifdef SQLITE_ENABLE_IOTRACE |
| 492 | static FILE *iotrace = 0; |
| 493 | #endif |
| 494 | |
| 495 | /* |
| 496 | ** This routine works like printf in that its first argument is a |
| 497 | ** format string and subsequent arguments are values to be substituted |
| 498 | ** in place of % fields. The result of formatting this string |
| 499 | ** is written to iotrace. |
| 500 | */ |
| 501 | #ifdef SQLITE_ENABLE_IOTRACE |
| 502 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
| 503 | va_list ap; |
| 504 | char *z; |
| 505 | if( iotrace==0 ) return; |
| 506 | va_start(ap, zFormat); |
| 507 | z = sqlite3_vmprintf(zFormat, ap); |
| 508 | va_end(ap); |
| 509 | utf8_printf(iotrace, "%s", z); |
| 510 | sqlite3_free(z); |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | /* |
| 515 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 516 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 517 | ** in bytes. This is different from the %*.*s specification in printf |
| 518 | ** since with %*.*s the width is measured in bytes, not characters. |
| 519 | */ |
| 520 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 521 | int i; |
| 522 | int n; |
| 523 | int aw = w<0 ? -w : w; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 524 | for(i=n=0; zUtf[i]; i++){ |
| 525 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 526 | n++; |
| 527 | if( n==aw ){ |
| 528 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | if( n>=aw ){ |
| 534 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 535 | }else if( w<0 ){ |
| 536 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 537 | }else{ |
| 538 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /* |
| 544 | ** Determines if a string is a number of not. |
| 545 | */ |
| 546 | static int isNumber(const char *z, int *realnum){ |
| 547 | if( *z=='-' || *z=='+' ) z++; |
| 548 | if( !IsDigit(*z) ){ |
| 549 | return 0; |
| 550 | } |
| 551 | z++; |
| 552 | if( realnum ) *realnum = 0; |
| 553 | while( IsDigit(*z) ){ z++; } |
| 554 | if( *z=='.' ){ |
| 555 | z++; |
| 556 | if( !IsDigit(*z) ) return 0; |
| 557 | while( IsDigit(*z) ){ z++; } |
| 558 | if( realnum ) *realnum = 1; |
| 559 | } |
| 560 | if( *z=='e' || *z=='E' ){ |
| 561 | z++; |
| 562 | if( *z=='+' || *z=='-' ) z++; |
| 563 | if( !IsDigit(*z) ) return 0; |
| 564 | while( IsDigit(*z) ){ z++; } |
| 565 | if( realnum ) *realnum = 1; |
| 566 | } |
| 567 | return *z==0; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | ** Compute a string length that is limited to what can be stored in |
| 572 | ** lower 30 bits of a 32-bit signed integer. |
| 573 | */ |
| 574 | static int strlen30(const char *z){ |
| 575 | const char *z2 = z; |
| 576 | while( *z2 ){ z2++; } |
| 577 | return 0x3fffffff & (int)(z2 - z); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 582 | ** count as a single character. |
| 583 | */ |
| 584 | static int strlenChar(const char *z){ |
| 585 | int n = 0; |
| 586 | while( *z ){ |
| 587 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 588 | } |
| 589 | return n; |
| 590 | } |
| 591 | |
| 592 | /* |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 593 | ** Return open FILE * if zFile exists, can be opened for read |
| 594 | ** and is an ordinary file or a character stream source. |
| 595 | ** Otherwise return 0. |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 596 | */ |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 597 | static FILE * openChrSource(const char *zFile){ |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 598 | #ifdef _WIN32 |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 599 | struct _stat x = {0}; |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 600 | # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 601 | /* On Windows, open first, then check the stream nature. This order |
| 602 | ** is necessary because _stat() and sibs, when checking a named pipe, |
| 603 | ** effectively break the pipe as its supplier sees it. */ |
| 604 | FILE *rv = fopen(zFile, "rb"); |
| 605 | if( rv==0 ) return 0; |
| 606 | if( _fstat(_fileno(rv), &x) != 0 |
| 607 | || !STAT_CHR_SRC(x.st_mode)){ |
| 608 | fclose(rv); |
| 609 | rv = 0; |
| 610 | } |
| 611 | return rv; |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 612 | #else |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 613 | struct stat x = {0}; |
| 614 | int rc = stat(zFile, &x); |
| 615 | # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 616 | if( rc!=0 ) return 0; |
| 617 | if( STAT_CHR_SRC(x.st_mode) ){ |
| 618 | return fopen(zFile, "rb"); |
| 619 | }else{ |
| 620 | return 0; |
| 621 | } |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 622 | #endif |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 623 | #undef STAT_CHR_SRC |
| 624 | } |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 625 | |
| 626 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 627 | ** This routine reads a line of text from FILE in, stores |
| 628 | ** the text in memory obtained from malloc() and returns a pointer |
| 629 | ** to the text. NULL is returned at end of file, or if malloc() |
| 630 | ** fails. |
| 631 | ** |
| 632 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 633 | ** a previous call to this routine that may be reused. |
| 634 | */ |
| 635 | static char *local_getline(char *zLine, FILE *in){ |
| 636 | int nLine = zLine==0 ? 0 : 100; |
| 637 | int n = 0; |
| 638 | |
| 639 | while( 1 ){ |
| 640 | if( n+100>nLine ){ |
| 641 | nLine = nLine*2 + 100; |
| 642 | zLine = realloc(zLine, nLine); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 643 | shell_check_oom(zLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 644 | } |
| 645 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 646 | if( n==0 ){ |
| 647 | free(zLine); |
| 648 | return 0; |
| 649 | } |
| 650 | zLine[n] = 0; |
| 651 | break; |
| 652 | } |
| 653 | while( zLine[n] ) n++; |
| 654 | if( n>0 && zLine[n-1]=='\n' ){ |
| 655 | n--; |
| 656 | if( n>0 && zLine[n-1]=='\r' ) n--; |
| 657 | zLine[n] = 0; |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | #if defined(_WIN32) || defined(WIN32) |
| 662 | /* For interactive input on Windows systems, translate the |
| 663 | ** multi-byte characterset characters into UTF-8. */ |
| 664 | if( stdin_is_interactive && in==stdin ){ |
| 665 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| 666 | if( zTrans ){ |
| 667 | int nTrans = strlen30(zTrans)+1; |
| 668 | if( nTrans>nLine ){ |
| 669 | zLine = realloc(zLine, nTrans); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 670 | shell_check_oom(zLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 671 | } |
| 672 | memcpy(zLine, zTrans, nTrans); |
| 673 | sqlite3_free(zTrans); |
| 674 | } |
| 675 | } |
| 676 | #endif /* defined(_WIN32) || defined(WIN32) */ |
| 677 | return zLine; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | ** Retrieve a single line of input text. |
| 682 | ** |
| 683 | ** If in==0 then read from standard input and prompt before each line. |
| 684 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 685 | ** If isContinuation is zero, then the main prompt should be used. |
| 686 | ** |
| 687 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 688 | ** routine that can be reused. |
| 689 | ** |
| 690 | ** The result is stored in space obtained from malloc() and must either |
| 691 | ** be freed by the caller or else passed back into this routine via the |
| 692 | ** zPrior argument for reuse. |
| 693 | */ |
| 694 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 695 | char *zPrompt; |
| 696 | char *zResult; |
| 697 | if( in!=0 ){ |
| 698 | zResult = local_getline(zPrior, in); |
| 699 | }else{ |
| 700 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
| 701 | #if SHELL_USE_LOCAL_GETLINE |
| 702 | printf("%s", zPrompt); |
| 703 | fflush(stdout); |
| 704 | zResult = local_getline(zPrior, stdin); |
| 705 | #else |
| 706 | free(zPrior); |
| 707 | zResult = shell_readline(zPrompt); |
| 708 | if( zResult && *zResult ) shell_add_history(zResult); |
| 709 | #endif |
| 710 | } |
| 711 | return zResult; |
| 712 | } |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 713 | |
| 714 | |
| 715 | /* |
| 716 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 717 | ** is not a hex digit. |
| 718 | */ |
| 719 | static int hexDigitValue(char c){ |
| 720 | if( c>='0' && c<='9' ) return c - '0'; |
| 721 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 722 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 728 | */ |
| 729 | static sqlite3_int64 integerValue(const char *zArg){ |
| 730 | sqlite3_int64 v = 0; |
| 731 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 732 | { "KiB", 1024 }, |
| 733 | { "MiB", 1024*1024 }, |
| 734 | { "GiB", 1024*1024*1024 }, |
| 735 | { "KB", 1000 }, |
| 736 | { "MB", 1000000 }, |
| 737 | { "GB", 1000000000 }, |
| 738 | { "K", 1000 }, |
| 739 | { "M", 1000000 }, |
| 740 | { "G", 1000000000 }, |
| 741 | }; |
| 742 | int i; |
| 743 | int isNeg = 0; |
| 744 | if( zArg[0]=='-' ){ |
| 745 | isNeg = 1; |
| 746 | zArg++; |
| 747 | }else if( zArg[0]=='+' ){ |
| 748 | zArg++; |
| 749 | } |
| 750 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 751 | int x; |
| 752 | zArg += 2; |
| 753 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 754 | v = (v<<4) + x; |
| 755 | zArg++; |
| 756 | } |
| 757 | }else{ |
| 758 | while( IsDigit(zArg[0]) ){ |
| 759 | v = v*10 + zArg[0] - '0'; |
| 760 | zArg++; |
| 761 | } |
| 762 | } |
| 763 | for(i=0; i<ArraySize(aMult); i++){ |
| 764 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 765 | v *= aMult[i].iMult; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | return isNeg? -v : v; |
| 770 | } |
| 771 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 772 | /* |
| 773 | ** A variable length string to which one can append text. |
| 774 | */ |
| 775 | typedef struct ShellText ShellText; |
| 776 | struct ShellText { |
| 777 | char *z; |
| 778 | int n; |
| 779 | int nAlloc; |
| 780 | }; |
| 781 | |
| 782 | /* |
| 783 | ** Initialize and destroy a ShellText object |
| 784 | */ |
| 785 | static void initText(ShellText *p){ |
| 786 | memset(p, 0, sizeof(*p)); |
| 787 | } |
| 788 | static void freeText(ShellText *p){ |
| 789 | free(p->z); |
| 790 | initText(p); |
| 791 | } |
| 792 | |
| 793 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 794 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 795 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 796 | ** zIn, if it was not NULL, is freed. |
| 797 | ** |
| 798 | ** If the third argument, quote, is not '\0', then it is used as a |
| 799 | ** quote character for zAppend. |
| 800 | */ |
| 801 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 802 | int len; |
| 803 | int i; |
| 804 | int nAppend = strlen30(zAppend); |
| 805 | |
| 806 | len = nAppend+p->n+1; |
| 807 | if( quote ){ |
| 808 | len += 2; |
| 809 | for(i=0; i<nAppend; i++){ |
| 810 | if( zAppend[i]==quote ) len++; |
| 811 | } |
| 812 | } |
| 813 | |
drh | 11a9ad5 | 2021-10-04 18:21:14 +0000 | [diff] [blame] | 814 | if( p->z==0 || p->n+len>=p->nAlloc ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 815 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 816 | p->z = realloc(p->z, p->nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 817 | shell_check_oom(p->z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if( quote ){ |
| 821 | char *zCsr = p->z+p->n; |
| 822 | *zCsr++ = quote; |
| 823 | for(i=0; i<nAppend; i++){ |
| 824 | *zCsr++ = zAppend[i]; |
| 825 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 826 | } |
| 827 | *zCsr++ = quote; |
| 828 | p->n = (int)(zCsr - p->z); |
| 829 | *zCsr = '\0'; |
| 830 | }else{ |
| 831 | memcpy(p->z+p->n, zAppend, nAppend); |
| 832 | p->n += nAppend; |
| 833 | p->z[p->n] = '\0'; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 839 | ** because it contains non-alphanumeric characters, or because it is an |
| 840 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 841 | ** that quoting is required. |
| 842 | ** |
| 843 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 844 | */ |
| 845 | static char quoteChar(const char *zName){ |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 846 | int i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 847 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 848 | for(i=0; zName[i]; i++){ |
| 849 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 850 | } |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 851 | return sqlite3_keyword_check(zName, i) ? '"' : 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 855 | ** Construct a fake object name and column list to describe the structure |
| 856 | ** of the view, virtual table, or table valued function zSchema.zName. |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 857 | */ |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 858 | static char *shellFakeSchema( |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 859 | sqlite3 *db, /* The database connection containing the vtab */ |
| 860 | const char *zSchema, /* Schema of the database holding the vtab */ |
| 861 | const char *zName /* The name of the virtual table */ |
| 862 | ){ |
| 863 | sqlite3_stmt *pStmt = 0; |
| 864 | char *zSql; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 865 | ShellText s; |
| 866 | char cQuote; |
| 867 | char *zDiv = "("; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 868 | int nRow = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 869 | |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 870 | zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", |
| 871 | zSchema ? zSchema : "main", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 872 | shell_check_oom(zSql); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 873 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 874 | sqlite3_free(zSql); |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 875 | initText(&s); |
| 876 | if( zSchema ){ |
| 877 | cQuote = quoteChar(zSchema); |
| 878 | if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; |
| 879 | appendText(&s, zSchema, cQuote); |
| 880 | appendText(&s, ".", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 881 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 882 | cQuote = quoteChar(zName); |
| 883 | appendText(&s, zName, cQuote); |
| 884 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 885 | const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 886 | nRow++; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 887 | appendText(&s, zDiv, 0); |
| 888 | zDiv = ","; |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 889 | if( zCol==0 ) zCol = ""; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 890 | cQuote = quoteChar(zCol); |
| 891 | appendText(&s, zCol, cQuote); |
| 892 | } |
| 893 | appendText(&s, ")", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 894 | sqlite3_finalize(pStmt); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 895 | if( nRow==0 ){ |
| 896 | freeText(&s); |
| 897 | s.z = 0; |
| 898 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 899 | return s.z; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 903 | ** SQL function: shell_module_schema(X) |
| 904 | ** |
| 905 | ** Return a fake schema for the table-valued function or eponymous virtual |
| 906 | ** table X. |
| 907 | */ |
| 908 | static void shellModuleSchema( |
| 909 | sqlite3_context *pCtx, |
| 910 | int nVal, |
| 911 | sqlite3_value **apVal |
| 912 | ){ |
drh | 511b118 | 2021-12-16 13:56:04 +0000 | [diff] [blame] | 913 | const char *zName; |
| 914 | char *zFake; |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 915 | UNUSED_PARAMETER(nVal); |
drh | 511b118 | 2021-12-16 13:56:04 +0000 | [diff] [blame] | 916 | zName = (const char*)sqlite3_value_text(apVal[0]); |
| 917 | zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 918 | if( zFake ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 919 | sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 920 | -1, sqlite3_free); |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 921 | free(zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 926 | ** SQL function: shell_add_schema(S,X) |
| 927 | ** |
| 928 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 929 | ** Examples: |
| 930 | ** |
| 931 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 932 | ** |
| 933 | ** Also works on |
| 934 | ** |
| 935 | ** CREATE INDEX |
| 936 | ** CREATE UNIQUE INDEX |
| 937 | ** CREATE VIEW |
| 938 | ** CREATE TRIGGER |
| 939 | ** CREATE VIRTUAL TABLE |
| 940 | ** |
| 941 | ** This UDF is used by the .schema command to insert the schema name of |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 942 | ** attached databases into the middle of the sqlite_schema.sql field. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 943 | */ |
| 944 | static void shellAddSchemaName( |
| 945 | sqlite3_context *pCtx, |
| 946 | int nVal, |
| 947 | sqlite3_value **apVal |
| 948 | ){ |
| 949 | static const char *aPrefix[] = { |
| 950 | "TABLE", |
| 951 | "INDEX", |
| 952 | "UNIQUE INDEX", |
| 953 | "VIEW", |
| 954 | "TRIGGER", |
| 955 | "VIRTUAL TABLE" |
| 956 | }; |
| 957 | int i = 0; |
| 958 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 959 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 960 | const char *zName = (const char*)sqlite3_value_text(apVal[2]); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 961 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 962 | UNUSED_PARAMETER(nVal); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 963 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 964 | for(i=0; i<ArraySize(aPrefix); i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 965 | int n = strlen30(aPrefix[i]); |
| 966 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 967 | char *z = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 968 | char *zFake = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 969 | if( zSchema ){ |
| 970 | char cQuote = quoteChar(zSchema); |
| 971 | if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ |
| 972 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 973 | }else{ |
| 974 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 975 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 976 | } |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 977 | if( zName |
| 978 | && aPrefix[i][0]=='V' |
| 979 | && (zFake = shellFakeSchema(db, zSchema, zName))!=0 |
| 980 | ){ |
| 981 | if( z==0 ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 982 | z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 983 | }else{ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 984 | z = sqlite3_mprintf("%z\n/* %s */", z, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 985 | } |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 986 | free(zFake); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 987 | } |
| 988 | if( z ){ |
| 989 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 990 | return; |
| 991 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } |
| 995 | sqlite3_result_value(pCtx, apVal[0]); |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | ** The source code for several run-time loadable extensions is inserted |
| 1000 | ** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 1001 | ** code, we need to override some macros to make the included program code |
| 1002 | ** work here in the middle of this regular program. |
| 1003 | */ |
| 1004 | #define SQLITE_EXTENSION_INIT1 |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1005 | #define SQLITE_EXTENSION_INIT2(X) (void)(X) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1006 | |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 1007 | #if defined(_WIN32) && defined(_MSC_VER) |
drh | 03491a1 | 2018-01-07 21:58:17 +0000 | [diff] [blame] | 1008 | INCLUDE test_windirent.h |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 1009 | INCLUDE test_windirent.c |
| 1010 | #define dirent DIRENT |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 1011 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1012 | INCLUDE ../ext/misc/shathree.c |
| 1013 | INCLUDE ../ext/misc/fileio.c |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 1014 | INCLUDE ../ext/misc/completion.c |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 1015 | INCLUDE ../ext/misc/appendvfs.c |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 1016 | INCLUDE ../ext/misc/memtrace.c |
drh | f05dd03 | 2020-04-14 15:53:58 +0000 | [diff] [blame] | 1017 | INCLUDE ../ext/misc/uint.c |
drh | beb9def | 2020-06-22 19:12:23 +0000 | [diff] [blame] | 1018 | INCLUDE ../ext/misc/decimal.c |
drh | 8cda77d | 2020-06-24 15:06:29 +0000 | [diff] [blame] | 1019 | INCLUDE ../ext/misc/ieee754.c |
mistachkin | 72c38d8 | 2020-08-28 18:47:39 +0000 | [diff] [blame] | 1020 | INCLUDE ../ext/misc/series.c |
drh | 6468990 | 2021-06-03 13:51:31 +0000 | [diff] [blame] | 1021 | INCLUDE ../ext/misc/regexp.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 1022 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 1023 | INCLUDE ../ext/misc/zipfile.c |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 1024 | INCLUDE ../ext/misc/sqlar.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 1025 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 1026 | INCLUDE ../ext/expert/sqlite3expert.h |
| 1027 | INCLUDE ../ext/expert/sqlite3expert.c |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1028 | |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 1029 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 1030 | INCLUDE ../ext/misc/dbdata.c |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 1031 | #endif |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 1032 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1033 | #if defined(SQLITE_ENABLE_SESSION) |
| 1034 | /* |
| 1035 | ** State information for a single open session |
| 1036 | */ |
| 1037 | typedef struct OpenSession OpenSession; |
| 1038 | struct OpenSession { |
| 1039 | char *zName; /* Symbolic name for this session */ |
| 1040 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1041 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1042 | sqlite3_session *p; /* The open session */ |
| 1043 | }; |
| 1044 | #endif |
| 1045 | |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 1046 | typedef struct ExpertInfo ExpertInfo; |
| 1047 | struct ExpertInfo { |
| 1048 | sqlite3expert *pExpert; |
| 1049 | int bVerbose; |
| 1050 | }; |
| 1051 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1052 | /* A single line in the EQP output */ |
| 1053 | typedef struct EQPGraphRow EQPGraphRow; |
| 1054 | struct EQPGraphRow { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1055 | int iEqpId; /* ID for this row */ |
| 1056 | int iParentId; /* ID of the parent row */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1057 | EQPGraphRow *pNext; /* Next row in sequence */ |
| 1058 | char zText[1]; /* Text to display for this row */ |
| 1059 | }; |
| 1060 | |
| 1061 | /* All EQP output is collected into an instance of the following */ |
| 1062 | typedef struct EQPGraph EQPGraph; |
| 1063 | struct EQPGraph { |
| 1064 | EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ |
| 1065 | EQPGraphRow *pLast; /* Last element of the pRow list */ |
| 1066 | char zPrefix[100]; /* Graph prefix */ |
| 1067 | }; |
| 1068 | |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1069 | /* Parameters affecting columnar mode result display (defaulting together) */ |
| 1070 | typedef struct ColModeOpts { |
| 1071 | int iWrap; /* In columnar modes, wrap lines reaching this limit */ |
| 1072 | u8 bQuote; /* Quote results for .mode box and table */ |
| 1073 | u8 bWordWrap; /* In columnar modes, wrap at word boundaries */ |
| 1074 | } ColModeOpts; |
| 1075 | #define ColModeOpts_default { 60, 0, 0 } |
| 1076 | #define ColModeOpts_default_qbox { 60, 1, 0 } |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1077 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1078 | /* |
| 1079 | ** State information about the database connection is contained in an |
| 1080 | ** instance of the following structure. |
| 1081 | */ |
| 1082 | typedef struct ShellState ShellState; |
| 1083 | struct ShellState { |
| 1084 | sqlite3 *db; /* The database */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1085 | u8 autoExplain; /* Automatically turn on .explain mode */ |
| 1086 | u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1087 | u8 autoEQPtest; /* autoEQP is in test mode */ |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 1088 | u8 autoEQPtrace; /* autoEQP is in trace mode */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1089 | u8 scanstatsOn; /* True to display scan stats before each finalize */ |
| 1090 | u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1091 | u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1092 | u8 nEqpLevel; /* Depth of the EQP output graph */ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 1093 | u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1094 | u8 bSafeMode; /* True to prohibit unsafe operations */ |
| 1095 | u8 bSafeModePersist; /* The long-term value of bSafeMode */ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1096 | ColModeOpts cmOpts; /* Option values affecting columnar mode output */ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 1097 | unsigned statsOn; /* True to display memory stats before each finalize */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1098 | unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 1099 | int inputNesting; /* Track nesting level of .read and other redirects */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1100 | int outCount; /* Revert to stdout when reaching zero */ |
| 1101 | int cnt; /* Number of records displayed so far */ |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 1102 | int lineno; /* Line number of last line read from in */ |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 1103 | int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 1104 | FILE *in; /* Read commands from this stream */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1105 | FILE *out; /* Write results here */ |
| 1106 | FILE *traceOut; /* Output for sqlite3_trace() */ |
| 1107 | int nErr; /* Number of errors seen */ |
| 1108 | int mode; /* An output mode setting */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1109 | int modePrior; /* Saved mode */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1110 | int cMode; /* temporary output mode for the current query */ |
| 1111 | int normalMode; /* Output mode before ".explain on" */ |
| 1112 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
| 1113 | int showHeader; /* True to show column names in List or Column mode */ |
| 1114 | int nCheck; /* Number of ".check" commands run */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1115 | unsigned nProgress; /* Number of progress callbacks encountered */ |
| 1116 | unsigned mxProgress; /* Maximum progress callbacks before failing */ |
| 1117 | unsigned flgProgress; /* Flags for the progress callback */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1118 | unsigned shellFlgs; /* Various flags */ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1119 | unsigned priorShFlgs; /* Saved copy of flags */ |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 1120 | sqlite3_int64 szMax; /* --maxsize argument to .open */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1121 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1122 | char *zTempFile; /* Temporary file that might need deleting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1123 | char zTestcase[30]; /* Name of current test case */ |
| 1124 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1125 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1126 | char colSepPrior[20]; /* Saved column separator */ |
| 1127 | char rowSepPrior[20]; /* Saved row separator */ |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 1128 | int *colWidth; /* Requested width of each column in columnar modes */ |
| 1129 | int *actualWidth; /* Actual width of each column */ |
| 1130 | int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1131 | char nullValue[20]; /* The text to print when a NULL comes back from |
| 1132 | ** the database */ |
| 1133 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1134 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
| 1135 | FILE *pLog; /* Write log output here */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 1136 | struct AuxDb { /* Storage space for auxiliary database connections */ |
| 1137 | sqlite3 *db; /* Connection pointer */ |
| 1138 | const char *zDbFilename; /* Filename used to open the connection */ |
| 1139 | char *zFreeOnClose; /* Free this memory allocation on close */ |
| 1140 | #if defined(SQLITE_ENABLE_SESSION) |
| 1141 | int nSession; /* Number of active sessions */ |
| 1142 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1143 | #endif |
| 1144 | } aAuxDb[5], /* Array of all database connections */ |
| 1145 | *pAuxDb; /* Currently active database connection */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1146 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1147 | int nIndent; /* Size of array aiIndent[] */ |
| 1148 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1149 | char *zNonce; /* Nonce for temporary safe-mode excapes */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1150 | EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1151 | ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1152 | }; |
| 1153 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1154 | |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 1155 | /* Allowed values for ShellState.autoEQP |
| 1156 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1157 | #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ |
| 1158 | #define AUTOEQP_on 1 /* Automatic EQP is on */ |
| 1159 | #define AUTOEQP_trigger 2 /* On and also show plans for triggers */ |
| 1160 | #define AUTOEQP_full 3 /* Show full EXPLAIN */ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 1161 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1162 | /* Allowed values for ShellState.openMode |
| 1163 | */ |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 1164 | #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ |
| 1165 | #define SHELL_OPEN_NORMAL 1 /* Normal database file */ |
| 1166 | #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ |
| 1167 | #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ |
| 1168 | #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ |
| 1169 | #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 1170 | #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1171 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 1172 | /* Allowed values for ShellState.eTraceType |
| 1173 | */ |
| 1174 | #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ |
| 1175 | #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ |
| 1176 | #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ |
| 1177 | |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1178 | /* Bits in the ShellState.flgProgress variable */ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 1179 | #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ |
| 1180 | #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres |
| 1181 | ** callback limit is reached, and for each |
| 1182 | ** top-level SQL statement */ |
| 1183 | #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1184 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | ** These are the allowed shellFlgs values |
| 1187 | */ |
drh | b2a0f75 | 2017-08-28 15:51:35 +0000 | [diff] [blame] | 1188 | #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ |
| 1189 | #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ |
| 1190 | #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ |
| 1191 | #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ |
| 1192 | #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ |
| 1193 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 1194 | #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */ |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 1195 | #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */ |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 1196 | #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ |
| 1197 | #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1198 | |
| 1199 | /* |
| 1200 | ** Macros for testing and setting shellFlgs |
| 1201 | */ |
| 1202 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1203 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1204 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
| 1205 | |
| 1206 | /* |
| 1207 | ** These are the allowed modes. |
| 1208 | */ |
| 1209 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
| 1210 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1211 | #define MODE_List 2 /* One record per line with a separator */ |
| 1212 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1213 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1214 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
| 1215 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1216 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1217 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1218 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1219 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1220 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1221 | #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 1222 | #define MODE_Json 13 /* Output JSON */ |
| 1223 | #define MODE_Markdown 14 /* Markdown formatting */ |
| 1224 | #define MODE_Table 15 /* MySQL-style table formatting */ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 1225 | #define MODE_Box 16 /* Unicode box-drawing characters */ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 1226 | #define MODE_Count 17 /* Output only a count of the rows of output */ |
| 1227 | #define MODE_Off 18 /* No query output shown */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1228 | |
| 1229 | static const char *modeDescr[] = { |
| 1230 | "line", |
| 1231 | "column", |
| 1232 | "list", |
| 1233 | "semi", |
| 1234 | "html", |
| 1235 | "insert", |
| 1236 | "quote", |
| 1237 | "tcl", |
| 1238 | "csv", |
| 1239 | "explain", |
| 1240 | "ascii", |
| 1241 | "prettyprint", |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 1242 | "eqp", |
| 1243 | "json", |
| 1244 | "markdown", |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 1245 | "table", |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 1246 | "box", |
| 1247 | "count", |
| 1248 | "off" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1249 | }; |
| 1250 | |
| 1251 | /* |
| 1252 | ** These are the column/row/line separators used by the various |
| 1253 | ** import/export modes. |
| 1254 | */ |
| 1255 | #define SEP_Column "|" |
| 1256 | #define SEP_Row "\n" |
| 1257 | #define SEP_Tab "\t" |
| 1258 | #define SEP_Space " " |
| 1259 | #define SEP_Comma "," |
| 1260 | #define SEP_CrLf "\r\n" |
| 1261 | #define SEP_Unit "\x1F" |
| 1262 | #define SEP_Record "\x1E" |
| 1263 | |
| 1264 | /* |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 1265 | ** Limit input nesting via .read or any other input redirect. |
| 1266 | ** It's not too expensive, so a generous allowance can be made. |
| 1267 | */ |
| 1268 | #define MAX_INPUT_NESTING 25 |
| 1269 | |
| 1270 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1271 | ** A callback for the sqlite3_log() interface. |
| 1272 | */ |
| 1273 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 1274 | ShellState *p = (ShellState*)pArg; |
| 1275 | if( p->pLog==0 ) return; |
| 1276 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 1277 | fflush(p->pLog); |
| 1278 | } |
| 1279 | |
| 1280 | /* |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1281 | ** SQL function: shell_putsnl(X) |
| 1282 | ** |
| 1283 | ** Write the text X to the screen (or whatever output is being directed) |
| 1284 | ** adding a newline at the end, and then return X. |
| 1285 | */ |
| 1286 | static void shellPutsFunc( |
| 1287 | sqlite3_context *pCtx, |
| 1288 | int nVal, |
| 1289 | sqlite3_value **apVal |
| 1290 | ){ |
| 1291 | ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 1292 | (void)nVal; |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1293 | utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
| 1294 | sqlite3_result_value(pCtx, apVal[0]); |
| 1295 | } |
| 1296 | |
| 1297 | /* |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1298 | ** If in safe mode, print an error message described by the arguments |
| 1299 | ** and exit immediately. |
| 1300 | */ |
| 1301 | static void failIfSafeMode( |
| 1302 | ShellState *p, |
| 1303 | const char *zErrMsg, |
| 1304 | ... |
| 1305 | ){ |
| 1306 | if( p->bSafeMode ){ |
| 1307 | va_list ap; |
| 1308 | char *zMsg; |
| 1309 | va_start(ap, zErrMsg); |
| 1310 | zMsg = sqlite3_vmprintf(zErrMsg, ap); |
| 1311 | va_end(ap); |
| 1312 | raw_printf(stderr, "line %d: ", p->lineno); |
| 1313 | utf8_printf(stderr, "%s\n", zMsg); |
| 1314 | exit(1); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | /* |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1319 | ** SQL function: edit(VALUE) |
| 1320 | ** edit(VALUE,EDITOR) |
| 1321 | ** |
| 1322 | ** These steps: |
| 1323 | ** |
| 1324 | ** (1) Write VALUE into a temporary file. |
| 1325 | ** (2) Run program EDITOR on that temporary file. |
| 1326 | ** (3) Read the temporary file back and return its content as the result. |
| 1327 | ** (4) Delete the temporary file |
| 1328 | ** |
| 1329 | ** If the EDITOR argument is omitted, use the value in the VISUAL |
| 1330 | ** environment variable. If still there is no EDITOR, through an error. |
| 1331 | ** |
| 1332 | ** Also throw an error if the EDITOR program returns a non-zero exit code. |
| 1333 | */ |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1334 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1335 | static void editFunc( |
| 1336 | sqlite3_context *context, |
| 1337 | int argc, |
| 1338 | sqlite3_value **argv |
| 1339 | ){ |
| 1340 | const char *zEditor; |
| 1341 | char *zTempFile = 0; |
| 1342 | sqlite3 *db; |
| 1343 | char *zCmd = 0; |
| 1344 | int bBin; |
| 1345 | int rc; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1346 | int hasCRNL = 0; |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1347 | FILE *f = 0; |
| 1348 | sqlite3_int64 sz; |
| 1349 | sqlite3_int64 x; |
| 1350 | unsigned char *p = 0; |
| 1351 | |
| 1352 | if( argc==2 ){ |
| 1353 | zEditor = (const char*)sqlite3_value_text(argv[1]); |
| 1354 | }else{ |
| 1355 | zEditor = getenv("VISUAL"); |
| 1356 | } |
| 1357 | if( zEditor==0 ){ |
| 1358 | sqlite3_result_error(context, "no editor for edit()", -1); |
| 1359 | return; |
| 1360 | } |
| 1361 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1362 | sqlite3_result_error(context, "NULL input to edit()", -1); |
| 1363 | return; |
| 1364 | } |
| 1365 | db = sqlite3_context_db_handle(context); |
| 1366 | zTempFile = 0; |
| 1367 | sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); |
| 1368 | if( zTempFile==0 ){ |
| 1369 | sqlite3_uint64 r = 0; |
| 1370 | sqlite3_randomness(sizeof(r), &r); |
| 1371 | zTempFile = sqlite3_mprintf("temp%llx", r); |
| 1372 | if( zTempFile==0 ){ |
| 1373 | sqlite3_result_error_nomem(context); |
| 1374 | return; |
| 1375 | } |
| 1376 | } |
| 1377 | bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1378 | /* When writing the file to be edited, do \n to \r\n conversions on systems |
| 1379 | ** that want \r\n line endings */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1380 | f = fopen(zTempFile, bBin ? "wb" : "w"); |
| 1381 | if( f==0 ){ |
| 1382 | sqlite3_result_error(context, "edit() cannot open temp file", -1); |
| 1383 | goto edit_func_end; |
| 1384 | } |
| 1385 | sz = sqlite3_value_bytes(argv[0]); |
| 1386 | if( bBin ){ |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1387 | x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1388 | }else{ |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1389 | const char *z = (const char*)sqlite3_value_text(argv[0]); |
| 1390 | /* Remember whether or not the value originally contained \r\n */ |
| 1391 | if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1392 | x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1393 | } |
| 1394 | fclose(f); |
| 1395 | f = 0; |
| 1396 | if( x!=sz ){ |
| 1397 | sqlite3_result_error(context, "edit() could not write the whole file", -1); |
| 1398 | goto edit_func_end; |
| 1399 | } |
| 1400 | zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); |
| 1401 | if( zCmd==0 ){ |
| 1402 | sqlite3_result_error_nomem(context); |
| 1403 | goto edit_func_end; |
| 1404 | } |
| 1405 | rc = system(zCmd); |
| 1406 | sqlite3_free(zCmd); |
| 1407 | if( rc ){ |
| 1408 | sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
| 1409 | goto edit_func_end; |
| 1410 | } |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1411 | f = fopen(zTempFile, "rb"); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1412 | if( f==0 ){ |
| 1413 | sqlite3_result_error(context, |
| 1414 | "edit() cannot reopen temp file after edit", -1); |
| 1415 | goto edit_func_end; |
| 1416 | } |
| 1417 | fseek(f, 0, SEEK_END); |
| 1418 | sz = ftell(f); |
| 1419 | rewind(f); |
drh | ee37f8b | 2019-08-23 23:05:32 +0000 | [diff] [blame] | 1420 | p = sqlite3_malloc64( sz+1 ); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1421 | if( p==0 ){ |
| 1422 | sqlite3_result_error_nomem(context); |
| 1423 | goto edit_func_end; |
| 1424 | } |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1425 | x = fread(p, 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1426 | fclose(f); |
| 1427 | f = 0; |
| 1428 | if( x!=sz ){ |
| 1429 | sqlite3_result_error(context, "could not read back the whole file", -1); |
| 1430 | goto edit_func_end; |
| 1431 | } |
| 1432 | if( bBin ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1433 | sqlite3_result_blob64(context, p, sz, sqlite3_free); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1434 | }else{ |
dan | 60bdcf5 | 2018-10-03 11:13:30 +0000 | [diff] [blame] | 1435 | sqlite3_int64 i, j; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1436 | if( hasCRNL ){ |
| 1437 | /* If the original contains \r\n then do no conversions back to \n */ |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1438 | }else{ |
| 1439 | /* If the file did not originally contain \r\n then convert any new |
| 1440 | ** \r\n back into \n */ |
| 1441 | for(i=j=0; i<sz; i++){ |
| 1442 | if( p[i]=='\r' && p[i+1]=='\n' ) i++; |
| 1443 | p[j++] = p[i]; |
| 1444 | } |
| 1445 | sz = j; |
| 1446 | p[sz] = 0; |
| 1447 | } |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1448 | sqlite3_result_text64(context, (const char*)p, sz, |
| 1449 | sqlite3_free, SQLITE_UTF8); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1450 | } |
| 1451 | p = 0; |
| 1452 | |
| 1453 | edit_func_end: |
| 1454 | if( f ) fclose(f); |
| 1455 | unlink(zTempFile); |
| 1456 | sqlite3_free(zTempFile); |
| 1457 | sqlite3_free(p); |
| 1458 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1459 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1460 | |
| 1461 | /* |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1462 | ** Save or restore the current output mode |
| 1463 | */ |
| 1464 | static void outputModePush(ShellState *p){ |
| 1465 | p->modePrior = p->mode; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1466 | p->priorShFlgs = p->shellFlgs; |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1467 | memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); |
| 1468 | memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); |
| 1469 | } |
| 1470 | static void outputModePop(ShellState *p){ |
| 1471 | p->mode = p->modePrior; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1472 | p->shellFlgs = p->priorShFlgs; |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1473 | memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
| 1474 | memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1478 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1479 | */ |
| 1480 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1481 | int i; |
| 1482 | char *zBlob = (char *)pBlob; |
| 1483 | raw_printf(out,"X'"); |
| 1484 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1485 | raw_printf(out,"'"); |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1490 | ** to that string. |
| 1491 | ** |
| 1492 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1493 | ** then make up some string and store it in the buffer zBuf. |
| 1494 | */ |
| 1495 | static const char *unused_string( |
| 1496 | const char *z, /* Result must not appear anywhere in z */ |
| 1497 | const char *zA, const char *zB, /* Try these first */ |
| 1498 | char *zBuf /* Space to store a generated string */ |
| 1499 | ){ |
| 1500 | unsigned i = 0; |
| 1501 | if( strstr(z, zA)==0 ) return zA; |
| 1502 | if( strstr(z, zB)==0 ) return zB; |
| 1503 | do{ |
| 1504 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1505 | }while( strstr(z,zBuf)!=0 ); |
| 1506 | return zBuf; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1511 | ** |
| 1512 | ** See also: output_quoted_escaped_string() |
| 1513 | */ |
| 1514 | static void output_quoted_string(FILE *out, const char *z){ |
| 1515 | int i; |
| 1516 | char c; |
| 1517 | setBinaryMode(out, 1); |
| 1518 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1519 | if( c==0 ){ |
| 1520 | utf8_printf(out,"'%s'",z); |
| 1521 | }else{ |
| 1522 | raw_printf(out, "'"); |
| 1523 | while( *z ){ |
| 1524 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1525 | if( c=='\'' ) i++; |
| 1526 | if( i ){ |
| 1527 | utf8_printf(out, "%.*s", i, z); |
| 1528 | z += i; |
| 1529 | } |
| 1530 | if( c=='\'' ){ |
| 1531 | raw_printf(out, "'"); |
| 1532 | continue; |
| 1533 | } |
| 1534 | if( c==0 ){ |
| 1535 | break; |
| 1536 | } |
| 1537 | z++; |
| 1538 | } |
| 1539 | raw_printf(out, "'"); |
| 1540 | } |
| 1541 | setTextMode(out, 1); |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1546 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1547 | ** get corrupted by end-of-line translation facilities in some operating |
| 1548 | ** systems. |
| 1549 | ** |
| 1550 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1551 | ** escape mechanism. |
| 1552 | */ |
| 1553 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1554 | int i; |
| 1555 | char c; |
| 1556 | setBinaryMode(out, 1); |
| 1557 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1558 | if( c==0 ){ |
| 1559 | utf8_printf(out,"'%s'",z); |
| 1560 | }else{ |
| 1561 | const char *zNL = 0; |
| 1562 | const char *zCR = 0; |
| 1563 | int nNL = 0; |
| 1564 | int nCR = 0; |
| 1565 | char zBuf1[20], zBuf2[20]; |
| 1566 | for(i=0; z[i]; i++){ |
| 1567 | if( z[i]=='\n' ) nNL++; |
| 1568 | if( z[i]=='\r' ) nCR++; |
| 1569 | } |
| 1570 | if( nNL ){ |
| 1571 | raw_printf(out, "replace("); |
| 1572 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1573 | } |
| 1574 | if( nCR ){ |
| 1575 | raw_printf(out, "replace("); |
| 1576 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1577 | } |
| 1578 | raw_printf(out, "'"); |
| 1579 | while( *z ){ |
| 1580 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1581 | if( c=='\'' ) i++; |
| 1582 | if( i ){ |
| 1583 | utf8_printf(out, "%.*s", i, z); |
| 1584 | z += i; |
| 1585 | } |
| 1586 | if( c=='\'' ){ |
| 1587 | raw_printf(out, "'"); |
| 1588 | continue; |
| 1589 | } |
| 1590 | if( c==0 ){ |
| 1591 | break; |
| 1592 | } |
| 1593 | z++; |
| 1594 | if( c=='\n' ){ |
| 1595 | raw_printf(out, "%s", zNL); |
| 1596 | continue; |
| 1597 | } |
| 1598 | raw_printf(out, "%s", zCR); |
| 1599 | } |
| 1600 | raw_printf(out, "'"); |
| 1601 | if( nCR ){ |
| 1602 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1603 | } |
| 1604 | if( nNL ){ |
| 1605 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1606 | } |
| 1607 | } |
| 1608 | setTextMode(out, 1); |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1613 | */ |
| 1614 | static void output_c_string(FILE *out, const char *z){ |
| 1615 | unsigned int c; |
| 1616 | fputc('"', out); |
| 1617 | while( (c = *(z++))!=0 ){ |
| 1618 | if( c=='\\' ){ |
| 1619 | fputc(c, out); |
| 1620 | fputc(c, out); |
| 1621 | }else if( c=='"' ){ |
| 1622 | fputc('\\', out); |
| 1623 | fputc('"', out); |
| 1624 | }else if( c=='\t' ){ |
| 1625 | fputc('\\', out); |
| 1626 | fputc('t', out); |
| 1627 | }else if( c=='\n' ){ |
| 1628 | fputc('\\', out); |
| 1629 | fputc('n', out); |
| 1630 | }else if( c=='\r' ){ |
| 1631 | fputc('\\', out); |
| 1632 | fputc('r', out); |
| 1633 | }else if( !isprint(c&0xff) ){ |
| 1634 | raw_printf(out, "\\%03o", c&0xff); |
| 1635 | }else{ |
| 1636 | fputc(c, out); |
| 1637 | } |
| 1638 | } |
| 1639 | fputc('"', out); |
| 1640 | } |
| 1641 | |
| 1642 | /* |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 1643 | ** Output the given string as a quoted according to JSON quoting rules. |
| 1644 | */ |
| 1645 | static void output_json_string(FILE *out, const char *z, int n){ |
| 1646 | unsigned int c; |
| 1647 | if( n<0 ) n = (int)strlen(z); |
| 1648 | fputc('"', out); |
| 1649 | while( n-- ){ |
| 1650 | c = *(z++); |
| 1651 | if( c=='\\' || c=='"' ){ |
| 1652 | fputc('\\', out); |
| 1653 | fputc(c, out); |
| 1654 | }else if( c<=0x1f ){ |
| 1655 | fputc('\\', out); |
| 1656 | if( c=='\b' ){ |
| 1657 | fputc('b', out); |
| 1658 | }else if( c=='\f' ){ |
| 1659 | fputc('f', out); |
| 1660 | }else if( c=='\n' ){ |
| 1661 | fputc('n', out); |
| 1662 | }else if( c=='\r' ){ |
| 1663 | fputc('r', out); |
| 1664 | }else if( c=='\t' ){ |
| 1665 | fputc('t', out); |
| 1666 | }else{ |
| 1667 | raw_printf(out, "u%04x",c); |
| 1668 | } |
| 1669 | }else{ |
| 1670 | fputc(c, out); |
| 1671 | } |
| 1672 | } |
| 1673 | fputc('"', out); |
| 1674 | } |
| 1675 | |
| 1676 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1677 | ** Output the given string with characters that are special to |
| 1678 | ** HTML escaped. |
| 1679 | */ |
| 1680 | static void output_html_string(FILE *out, const char *z){ |
| 1681 | int i; |
| 1682 | if( z==0 ) z = ""; |
| 1683 | while( *z ){ |
| 1684 | for(i=0; z[i] |
| 1685 | && z[i]!='<' |
| 1686 | && z[i]!='&' |
| 1687 | && z[i]!='>' |
| 1688 | && z[i]!='\"' |
| 1689 | && z[i]!='\''; |
| 1690 | i++){} |
| 1691 | if( i>0 ){ |
| 1692 | utf8_printf(out,"%.*s",i,z); |
| 1693 | } |
| 1694 | if( z[i]=='<' ){ |
| 1695 | raw_printf(out,"<"); |
| 1696 | }else if( z[i]=='&' ){ |
| 1697 | raw_printf(out,"&"); |
| 1698 | }else if( z[i]=='>' ){ |
| 1699 | raw_printf(out,">"); |
| 1700 | }else if( z[i]=='\"' ){ |
| 1701 | raw_printf(out,"""); |
| 1702 | }else if( z[i]=='\'' ){ |
| 1703 | raw_printf(out,"'"); |
| 1704 | }else{ |
| 1705 | break; |
| 1706 | } |
| 1707 | z += i + 1; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | ** If a field contains any character identified by a 1 in the following |
| 1713 | ** array, then the string must be quoted for CSV. |
| 1714 | */ |
| 1715 | static const char needCsvQuote[] = { |
| 1716 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1717 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1718 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1719 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1720 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1721 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1722 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1723 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1724 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1725 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1726 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1727 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1728 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1729 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1730 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1731 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1732 | }; |
| 1733 | |
| 1734 | /* |
| 1735 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
| 1736 | ** the separator, which may or may not be a comma. p->nullValue is |
| 1737 | ** the null value. Strings are quoted if necessary. The separator |
| 1738 | ** is only issued if bSep is true. |
| 1739 | */ |
| 1740 | static void output_csv(ShellState *p, const char *z, int bSep){ |
| 1741 | FILE *out = p->out; |
| 1742 | if( z==0 ){ |
| 1743 | utf8_printf(out,"%s",p->nullValue); |
| 1744 | }else{ |
drh | 9cd0c3d | 2021-11-18 15:40:05 +0000 | [diff] [blame] | 1745 | unsigned i; |
| 1746 | for(i=0; z[i]; i++){ |
| 1747 | if( needCsvQuote[((unsigned char*)z)[i]] ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1748 | i = 0; |
| 1749 | break; |
| 1750 | } |
| 1751 | } |
drh | 9cd0c3d | 2021-11-18 15:40:05 +0000 | [diff] [blame] | 1752 | if( i==0 || strstr(z, p->colSeparator)!=0 ){ |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1753 | char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 1754 | shell_check_oom(zQuoted); |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1755 | utf8_printf(out, "%s", zQuoted); |
| 1756 | sqlite3_free(zQuoted); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1757 | }else{ |
| 1758 | utf8_printf(out, "%s", z); |
| 1759 | } |
| 1760 | } |
| 1761 | if( bSep ){ |
| 1762 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1763 | } |
| 1764 | } |
| 1765 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1766 | /* |
| 1767 | ** This routine runs when the user presses Ctrl-C |
| 1768 | */ |
| 1769 | static void interrupt_handler(int NotUsed){ |
| 1770 | UNUSED_PARAMETER(NotUsed); |
| 1771 | seenInterrupt++; |
| 1772 | if( seenInterrupt>2 ) exit(1); |
| 1773 | if( globalDb ) sqlite3_interrupt(globalDb); |
| 1774 | } |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 1775 | |
| 1776 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 1777 | /* |
| 1778 | ** This routine runs for console events (e.g. Ctrl-C) on Win32 |
| 1779 | */ |
| 1780 | static BOOL WINAPI ConsoleCtrlHandler( |
| 1781 | DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ |
| 1782 | ){ |
| 1783 | if( dwCtrlType==CTRL_C_EVENT ){ |
| 1784 | interrupt_handler(0); |
| 1785 | return TRUE; |
| 1786 | } |
| 1787 | return FALSE; |
| 1788 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1789 | #endif |
| 1790 | |
| 1791 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1792 | /* |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1793 | ** This authorizer runs in safe mode. |
| 1794 | */ |
| 1795 | static int safeModeAuth( |
| 1796 | void *pClientData, |
| 1797 | int op, |
| 1798 | const char *zA1, |
| 1799 | const char *zA2, |
| 1800 | const char *zA3, |
| 1801 | const char *zA4 |
| 1802 | ){ |
| 1803 | ShellState *p = (ShellState*)pClientData; |
| 1804 | static const char *azProhibitedFunctions[] = { |
| 1805 | "edit", |
| 1806 | "fts3_tokenizer", |
| 1807 | "load_extension", |
| 1808 | "readfile", |
| 1809 | "writefile", |
| 1810 | "zipfile", |
| 1811 | "zipfile_cds", |
| 1812 | }; |
| 1813 | UNUSED_PARAMETER(zA2); |
| 1814 | UNUSED_PARAMETER(zA3); |
| 1815 | UNUSED_PARAMETER(zA4); |
| 1816 | switch( op ){ |
| 1817 | case SQLITE_ATTACH: { |
| 1818 | failIfSafeMode(p, "cannot run ATTACH in safe mode"); |
| 1819 | break; |
| 1820 | } |
| 1821 | case SQLITE_FUNCTION: { |
| 1822 | int i; |
| 1823 | for(i=0; i<ArraySize(azProhibitedFunctions); i++){ |
| 1824 | if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){ |
| 1825 | failIfSafeMode(p, "cannot use the %s() function in safe mode", |
| 1826 | azProhibitedFunctions[i]); |
| 1827 | } |
| 1828 | } |
| 1829 | break; |
| 1830 | } |
| 1831 | } |
| 1832 | return SQLITE_OK; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1836 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1837 | ** invoked. It always returns SQLITE_OK. |
| 1838 | */ |
| 1839 | static int shellAuth( |
| 1840 | void *pClientData, |
| 1841 | int op, |
| 1842 | const char *zA1, |
| 1843 | const char *zA2, |
| 1844 | const char *zA3, |
| 1845 | const char *zA4 |
| 1846 | ){ |
| 1847 | ShellState *p = (ShellState*)pClientData; |
| 1848 | static const char *azAction[] = { 0, |
| 1849 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1850 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1851 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1852 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1853 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1854 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1855 | "PRAGMA", "READ", "SELECT", |
| 1856 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1857 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1858 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1859 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1860 | }; |
| 1861 | int i; |
| 1862 | const char *az[4]; |
| 1863 | az[0] = zA1; |
| 1864 | az[1] = zA2; |
| 1865 | az[2] = zA3; |
| 1866 | az[3] = zA4; |
| 1867 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
| 1868 | for(i=0; i<4; i++){ |
| 1869 | raw_printf(p->out, " "); |
| 1870 | if( az[i] ){ |
| 1871 | output_c_string(p->out, az[i]); |
| 1872 | }else{ |
| 1873 | raw_printf(p->out, "NULL"); |
| 1874 | } |
| 1875 | } |
| 1876 | raw_printf(p->out, "\n"); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1877 | if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1878 | return SQLITE_OK; |
| 1879 | } |
| 1880 | #endif |
| 1881 | |
| 1882 | /* |
| 1883 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1884 | ** |
| 1885 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1886 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1887 | */ |
| 1888 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
drh | 0a0536a | 2019-05-09 18:13:30 +0000 | [diff] [blame] | 1889 | if( z==0 ) return; |
| 1890 | if( zTail==0 ) return; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1891 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1892 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1893 | }else{ |
| 1894 | utf8_printf(out, "%s%s", z, zTail); |
| 1895 | } |
| 1896 | } |
| 1897 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1898 | char c = z[n]; |
| 1899 | z[n] = 0; |
| 1900 | printSchemaLine(out, z, zTail); |
| 1901 | z[n] = c; |
| 1902 | } |
| 1903 | |
| 1904 | /* |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1905 | ** Return true if string z[] has nothing but whitespace and comments to the |
| 1906 | ** end of the first line. |
| 1907 | */ |
| 1908 | static int wsToEol(const char *z){ |
| 1909 | int i; |
| 1910 | for(i=0; z[i]; i++){ |
| 1911 | if( z[i]=='\n' ) return 1; |
| 1912 | if( IsSpace(z[i]) ) continue; |
| 1913 | if( z[i]=='-' && z[i+1]=='-' ) return 1; |
| 1914 | return 0; |
| 1915 | } |
| 1916 | return 1; |
| 1917 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1918 | |
| 1919 | /* |
| 1920 | ** Add a new entry to the EXPLAIN QUERY PLAN data |
| 1921 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1922 | static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1923 | EQPGraphRow *pNew; |
| 1924 | int nText = strlen30(zText); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1925 | if( p->autoEQPtest ){ |
| 1926 | utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); |
| 1927 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1928 | pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 1929 | shell_check_oom(pNew); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1930 | pNew->iEqpId = iEqpId; |
| 1931 | pNew->iParentId = p2; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1932 | memcpy(pNew->zText, zText, nText+1); |
| 1933 | pNew->pNext = 0; |
| 1934 | if( p->sGraph.pLast ){ |
| 1935 | p->sGraph.pLast->pNext = pNew; |
| 1936 | }else{ |
| 1937 | p->sGraph.pRow = pNew; |
| 1938 | } |
| 1939 | p->sGraph.pLast = pNew; |
| 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | ** Free and reset the EXPLAIN QUERY PLAN data that has been collected |
| 1944 | ** in p->sGraph. |
| 1945 | */ |
| 1946 | static void eqp_reset(ShellState *p){ |
| 1947 | EQPGraphRow *pRow, *pNext; |
| 1948 | for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ |
| 1949 | pNext = pRow->pNext; |
| 1950 | sqlite3_free(pRow); |
| 1951 | } |
| 1952 | memset(&p->sGraph, 0, sizeof(p->sGraph)); |
| 1953 | } |
| 1954 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1955 | /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1956 | ** pOld, or return the first such line if pOld is NULL |
| 1957 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1958 | static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1959 | EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1960 | while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1961 | return pRow; |
| 1962 | } |
| 1963 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1964 | /* 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] | 1965 | ** recursively to render sublevels. |
| 1966 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1967 | static void eqp_render_level(ShellState *p, int iEqpId){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1968 | EQPGraphRow *pRow, *pNext; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1969 | int n = strlen30(p->sGraph.zPrefix); |
| 1970 | char *z; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1971 | for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ |
| 1972 | pNext = eqp_next_row(p, iEqpId, pRow); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1973 | z = pRow->zText; |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 1974 | utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, |
| 1975 | pNext ? "|--" : "`--", z); |
drh | e2188f0 | 2018-05-07 11:37:34 +0000 | [diff] [blame] | 1976 | if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1977 | memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1978 | eqp_render_level(p, pRow->iEqpId); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1979 | p->sGraph.zPrefix[n] = 0; |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | ** Display and reset the EXPLAIN QUERY PLAN data |
| 1986 | */ |
| 1987 | static void eqp_render(ShellState *p){ |
| 1988 | EQPGraphRow *pRow = p->sGraph.pRow; |
| 1989 | if( pRow ){ |
| 1990 | if( pRow->zText[0]=='-' ){ |
| 1991 | if( pRow->pNext==0 ){ |
| 1992 | eqp_reset(p); |
| 1993 | return; |
| 1994 | } |
| 1995 | utf8_printf(p->out, "%s\n", pRow->zText+3); |
| 1996 | p->sGraph.pRow = pRow->pNext; |
| 1997 | sqlite3_free(pRow); |
| 1998 | }else{ |
| 1999 | utf8_printf(p->out, "QUERY PLAN\n"); |
| 2000 | } |
| 2001 | p->sGraph.zPrefix[0] = 0; |
| 2002 | eqp_render_level(p, 0); |
| 2003 | eqp_reset(p); |
| 2004 | } |
| 2005 | } |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2006 | |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 2007 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2008 | /* |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2009 | ** Progress handler callback. |
| 2010 | */ |
| 2011 | static int progress_handler(void *pClientData) { |
| 2012 | ShellState *p = (ShellState*)pClientData; |
| 2013 | p->nProgress++; |
| 2014 | if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ |
| 2015 | raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 2016 | if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
| 2017 | if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2018 | return 1; |
| 2019 | } |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 2020 | if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2021 | raw_printf(p->out, "Progress %u\n", p->nProgress); |
| 2022 | } |
| 2023 | return 0; |
| 2024 | } |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 2025 | #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2026 | |
| 2027 | /* |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2028 | ** Print N dashes |
| 2029 | */ |
| 2030 | static void print_dashes(FILE *out, int N){ |
| 2031 | const char zDash[] = "--------------------------------------------------"; |
| 2032 | const int nDash = sizeof(zDash) - 1; |
| 2033 | while( N>nDash ){ |
| 2034 | fputs(zDash, out); |
| 2035 | N -= nDash; |
| 2036 | } |
| 2037 | raw_printf(out, "%.*s", N, zDash); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2041 | ** Print a markdown or table-style row separator using ascii-art |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2042 | */ |
| 2043 | static void print_row_separator( |
| 2044 | ShellState *p, |
| 2045 | int nArg, |
| 2046 | const char *zSep |
| 2047 | ){ |
| 2048 | int i; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2049 | if( nArg>0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2050 | fputs(zSep, p->out); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2051 | print_dashes(p->out, p->actualWidth[0]+2); |
| 2052 | for(i=1; i<nArg; i++){ |
| 2053 | fputs(zSep, p->out); |
| 2054 | print_dashes(p->out, p->actualWidth[i]+2); |
| 2055 | } |
| 2056 | fputs(zSep, p->out); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2057 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2058 | fputs("\n", p->out); |
| 2059 | } |
| 2060 | |
| 2061 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2062 | ** This is the callback routine that the shell |
| 2063 | ** invokes for each row of a query result. |
| 2064 | */ |
| 2065 | static int shell_callback( |
| 2066 | void *pArg, |
| 2067 | int nArg, /* Number of result columns */ |
| 2068 | char **azArg, /* Text of each result column */ |
| 2069 | char **azCol, /* Column names */ |
drh | d6f2524 | 2020-05-29 12:31:53 +0000 | [diff] [blame] | 2070 | int *aiType /* Column types. Might be NULL */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2071 | ){ |
| 2072 | int i; |
| 2073 | ShellState *p = (ShellState*)pArg; |
| 2074 | |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2075 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2076 | switch( p->cMode ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 2077 | case MODE_Count: |
| 2078 | case MODE_Off: { |
| 2079 | break; |
| 2080 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2081 | case MODE_Line: { |
| 2082 | int w = 5; |
| 2083 | if( azArg==0 ) break; |
| 2084 | for(i=0; i<nArg; i++){ |
| 2085 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 2086 | if( len>w ) w = len; |
| 2087 | } |
| 2088 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
| 2089 | for(i=0; i<nArg; i++){ |
| 2090 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
| 2091 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 2092 | } |
| 2093 | break; |
| 2094 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2095 | case MODE_Explain: { |
| 2096 | static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 2097 | if( nArg>ArraySize(aExplainWidth) ){ |
| 2098 | nArg = ArraySize(aExplainWidth); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2099 | } |
| 2100 | if( p->cnt++==0 ){ |
| 2101 | for(i=0; i<nArg; i++){ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2102 | int w = aExplainWidth[i]; |
| 2103 | utf8_width_print(p->out, w, azCol[i]); |
| 2104 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2105 | } |
drh | e566ceb | 2020-05-30 15:34:49 +0000 | [diff] [blame] | 2106 | for(i=0; i<nArg; i++){ |
| 2107 | int w = aExplainWidth[i]; |
| 2108 | print_dashes(p->out, w); |
| 2109 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
| 2110 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2111 | } |
| 2112 | if( azArg==0 ) break; |
| 2113 | for(i=0; i<nArg; i++){ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2114 | int w = aExplainWidth[i]; |
drh | aa556b0 | 2021-01-13 12:59:20 +0000 | [diff] [blame] | 2115 | if( i==nArg-1 ) w = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2116 | if( azArg[i] && strlenChar(azArg[i])>w ){ |
| 2117 | w = strlenChar(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2118 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2119 | if( i==1 && p->aiIndent && p->pStmt ){ |
| 2120 | if( p->iIndent<p->nIndent ){ |
| 2121 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2122 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2123 | p->iIndent++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2124 | } |
| 2125 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2126 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2127 | } |
| 2128 | break; |
| 2129 | } |
| 2130 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 2131 | printSchemaLine(p->out, azArg[0], ";\n"); |
| 2132 | break; |
| 2133 | } |
| 2134 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 2135 | char *z; |
| 2136 | int j; |
| 2137 | int nParen = 0; |
| 2138 | char cEnd = 0; |
| 2139 | char c; |
| 2140 | int nLine = 0; |
| 2141 | assert( nArg==1 ); |
| 2142 | if( azArg[0]==0 ) break; |
| 2143 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 2144 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 2145 | ){ |
| 2146 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 2147 | break; |
| 2148 | } |
| 2149 | z = sqlite3_mprintf("%s", azArg[0]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2150 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2151 | j = 0; |
| 2152 | for(i=0; IsSpace(z[i]); i++){} |
| 2153 | for(; (c = z[i])!=0; i++){ |
| 2154 | if( IsSpace(c) ){ |
drh | c3cbd67 | 2017-10-05 19:12:10 +0000 | [diff] [blame] | 2155 | if( z[j-1]=='\r' ) z[j-1] = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2156 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 2157 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 2158 | j--; |
| 2159 | } |
| 2160 | z[j++] = c; |
| 2161 | } |
| 2162 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 2163 | z[j] = 0; |
| 2164 | if( strlen30(z)>=79 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 2165 | for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2166 | if( c==cEnd ){ |
| 2167 | cEnd = 0; |
| 2168 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 2169 | cEnd = c; |
| 2170 | }else if( c=='[' ){ |
| 2171 | cEnd = ']'; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2172 | }else if( c=='-' && z[i+1]=='-' ){ |
| 2173 | cEnd = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2174 | }else if( c=='(' ){ |
| 2175 | nParen++; |
| 2176 | }else if( c==')' ){ |
| 2177 | nParen--; |
| 2178 | if( nLine>0 && nParen==0 && j>0 ){ |
| 2179 | printSchemaLineN(p->out, z, j, "\n"); |
| 2180 | j = 0; |
| 2181 | } |
| 2182 | } |
| 2183 | z[j++] = c; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2184 | if( nParen==1 && cEnd==0 |
| 2185 | && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
| 2186 | ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2187 | if( c=='\n' ) j--; |
| 2188 | printSchemaLineN(p->out, z, j, "\n "); |
| 2189 | j = 0; |
| 2190 | nLine++; |
| 2191 | while( IsSpace(z[i+1]) ){ i++; } |
| 2192 | } |
| 2193 | } |
| 2194 | z[j] = 0; |
| 2195 | } |
| 2196 | printSchemaLine(p->out, z, ";\n"); |
| 2197 | sqlite3_free(z); |
| 2198 | break; |
| 2199 | } |
| 2200 | case MODE_List: { |
| 2201 | if( p->cnt++==0 && p->showHeader ){ |
| 2202 | for(i=0; i<nArg; i++){ |
| 2203 | utf8_printf(p->out,"%s%s",azCol[i], |
| 2204 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 2205 | } |
| 2206 | } |
| 2207 | if( azArg==0 ) break; |
| 2208 | for(i=0; i<nArg; i++){ |
| 2209 | char *z = azArg[i]; |
| 2210 | if( z==0 ) z = p->nullValue; |
| 2211 | utf8_printf(p->out, "%s", z); |
| 2212 | if( i<nArg-1 ){ |
| 2213 | utf8_printf(p->out, "%s", p->colSeparator); |
| 2214 | }else{ |
| 2215 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2216 | } |
| 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | case MODE_Html: { |
| 2221 | if( p->cnt++==0 && p->showHeader ){ |
| 2222 | raw_printf(p->out,"<TR>"); |
| 2223 | for(i=0; i<nArg; i++){ |
| 2224 | raw_printf(p->out,"<TH>"); |
| 2225 | output_html_string(p->out, azCol[i]); |
| 2226 | raw_printf(p->out,"</TH>\n"); |
| 2227 | } |
| 2228 | raw_printf(p->out,"</TR>\n"); |
| 2229 | } |
| 2230 | if( azArg==0 ) break; |
| 2231 | raw_printf(p->out,"<TR>"); |
| 2232 | for(i=0; i<nArg; i++){ |
| 2233 | raw_printf(p->out,"<TD>"); |
| 2234 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 2235 | raw_printf(p->out,"</TD>\n"); |
| 2236 | } |
| 2237 | raw_printf(p->out,"</TR>\n"); |
| 2238 | break; |
| 2239 | } |
| 2240 | case MODE_Tcl: { |
| 2241 | if( p->cnt++==0 && p->showHeader ){ |
| 2242 | for(i=0; i<nArg; i++){ |
| 2243 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
| 2244 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 2245 | } |
| 2246 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2247 | } |
| 2248 | if( azArg==0 ) break; |
| 2249 | for(i=0; i<nArg; i++){ |
| 2250 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 2251 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 2252 | } |
| 2253 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2254 | break; |
| 2255 | } |
| 2256 | case MODE_Csv: { |
| 2257 | setBinaryMode(p->out, 1); |
| 2258 | if( p->cnt++==0 && p->showHeader ){ |
| 2259 | for(i=0; i<nArg; i++){ |
| 2260 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 2261 | } |
| 2262 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2263 | } |
| 2264 | if( nArg>0 ){ |
| 2265 | for(i=0; i<nArg; i++){ |
| 2266 | output_csv(p, azArg[i], i<nArg-1); |
| 2267 | } |
| 2268 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2269 | } |
| 2270 | setTextMode(p->out, 1); |
| 2271 | break; |
| 2272 | } |
| 2273 | case MODE_Insert: { |
| 2274 | if( azArg==0 ) break; |
| 2275 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2276 | if( p->showHeader ){ |
| 2277 | raw_printf(p->out,"("); |
| 2278 | for(i=0; i<nArg; i++){ |
| 2279 | if( i>0 ) raw_printf(p->out, ","); |
| 2280 | if( quoteChar(azCol[i]) ){ |
| 2281 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2282 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2283 | utf8_printf(p->out, "%s", z); |
| 2284 | sqlite3_free(z); |
| 2285 | }else{ |
| 2286 | raw_printf(p->out, "%s", azCol[i]); |
| 2287 | } |
| 2288 | } |
| 2289 | raw_printf(p->out,")"); |
| 2290 | } |
| 2291 | p->cnt++; |
| 2292 | for(i=0; i<nArg; i++){ |
| 2293 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2294 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2295 | utf8_printf(p->out,"NULL"); |
| 2296 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2297 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2298 | output_quoted_string(p->out, azArg[i]); |
| 2299 | }else{ |
| 2300 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2301 | } |
| 2302 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2303 | utf8_printf(p->out,"%s", azArg[i]); |
| 2304 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2305 | char z[50]; |
| 2306 | double r = sqlite3_column_double(p->pStmt, i); |
drh | 2f1f880 | 2018-06-13 17:19:20 +0000 | [diff] [blame] | 2307 | sqlite3_uint64 ur; |
| 2308 | memcpy(&ur,&r,sizeof(r)); |
| 2309 | if( ur==0x7ff0000000000000LL ){ |
| 2310 | raw_printf(p->out, "1e999"); |
| 2311 | }else if( ur==0xfff0000000000000LL ){ |
| 2312 | raw_printf(p->out, "-1e999"); |
| 2313 | }else{ |
drh | 537a6bf | 2022-02-15 13:23:09 +0000 | [diff] [blame] | 2314 | sqlite3_int64 ir = (sqlite3_int64)r; |
| 2315 | if( r==(double)ir ){ |
| 2316 | sqlite3_snprintf(50,z,"%lld.0", ir); |
| 2317 | }else{ |
| 2318 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2319 | } |
drh | 2f1f880 | 2018-06-13 17:19:20 +0000 | [diff] [blame] | 2320 | raw_printf(p->out, "%s", z); |
| 2321 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2322 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2323 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2324 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2325 | output_hex_blob(p->out, pBlob, nBlob); |
| 2326 | }else if( isNumber(azArg[i], 0) ){ |
| 2327 | utf8_printf(p->out,"%s", azArg[i]); |
| 2328 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2329 | output_quoted_string(p->out, azArg[i]); |
| 2330 | }else{ |
| 2331 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2332 | } |
| 2333 | } |
| 2334 | raw_printf(p->out,");\n"); |
| 2335 | break; |
| 2336 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2337 | case MODE_Json: { |
| 2338 | if( azArg==0 ) break; |
| 2339 | if( p->cnt==0 ){ |
| 2340 | fputs("[{", p->out); |
| 2341 | }else{ |
| 2342 | fputs(",\n{", p->out); |
| 2343 | } |
| 2344 | p->cnt++; |
| 2345 | for(i=0; i<nArg; i++){ |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2346 | output_json_string(p->out, azCol[i], -1); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2347 | putc(':', p->out); |
| 2348 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2349 | fputs("null",p->out); |
| 2350 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2351 | char z[50]; |
| 2352 | double r = sqlite3_column_double(p->pStmt, i); |
| 2353 | sqlite3_uint64 ur; |
| 2354 | memcpy(&ur,&r,sizeof(r)); |
| 2355 | if( ur==0x7ff0000000000000LL ){ |
| 2356 | raw_printf(p->out, "1e999"); |
| 2357 | }else if( ur==0xfff0000000000000LL ){ |
| 2358 | raw_printf(p->out, "-1e999"); |
| 2359 | }else{ |
| 2360 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2361 | raw_printf(p->out, "%s", z); |
| 2362 | } |
| 2363 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2364 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2365 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2366 | output_json_string(p->out, pBlob, nBlob); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2367 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2368 | output_json_string(p->out, azArg[i], -1); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2369 | }else{ |
| 2370 | utf8_printf(p->out,"%s", azArg[i]); |
| 2371 | } |
| 2372 | if( i<nArg-1 ){ |
| 2373 | putc(',', p->out); |
| 2374 | } |
| 2375 | } |
| 2376 | putc('}', p->out); |
| 2377 | break; |
| 2378 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2379 | case MODE_Quote: { |
| 2380 | if( azArg==0 ) break; |
| 2381 | if( p->cnt==0 && p->showHeader ){ |
| 2382 | for(i=0; i<nArg; i++){ |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2383 | if( i>0 ) fputs(p->colSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2384 | output_quoted_string(p->out, azCol[i]); |
| 2385 | } |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2386 | fputs(p->rowSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2387 | } |
| 2388 | p->cnt++; |
| 2389 | for(i=0; i<nArg; i++){ |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2390 | if( i>0 ) fputs(p->colSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2391 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2392 | utf8_printf(p->out,"NULL"); |
| 2393 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2394 | output_quoted_string(p->out, azArg[i]); |
| 2395 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2396 | utf8_printf(p->out,"%s", azArg[i]); |
| 2397 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2398 | char z[50]; |
| 2399 | double r = sqlite3_column_double(p->pStmt, i); |
| 2400 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2401 | raw_printf(p->out, "%s", z); |
| 2402 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2403 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2404 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2405 | output_hex_blob(p->out, pBlob, nBlob); |
| 2406 | }else if( isNumber(azArg[i], 0) ){ |
| 2407 | utf8_printf(p->out,"%s", azArg[i]); |
| 2408 | }else{ |
| 2409 | output_quoted_string(p->out, azArg[i]); |
| 2410 | } |
| 2411 | } |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2412 | fputs(p->rowSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2413 | break; |
| 2414 | } |
| 2415 | case MODE_Ascii: { |
| 2416 | if( p->cnt++==0 && p->showHeader ){ |
| 2417 | for(i=0; i<nArg; i++){ |
| 2418 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2419 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
| 2420 | } |
| 2421 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2422 | } |
| 2423 | if( azArg==0 ) break; |
| 2424 | for(i=0; i<nArg; i++){ |
| 2425 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2426 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
| 2427 | } |
| 2428 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2429 | break; |
| 2430 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2431 | case MODE_EQP: { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2432 | eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2433 | break; |
| 2434 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2435 | } |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
| 2440 | ** This is the callback routine that the SQLite library |
| 2441 | ** invokes for each row of a query result. |
| 2442 | */ |
| 2443 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2444 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2445 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2450 | ** output onto the end of a ShellText object. |
| 2451 | */ |
| 2452 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2453 | ShellText *p = (ShellText*)pArg; |
| 2454 | int i; |
| 2455 | UNUSED_PARAMETER(az); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2456 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2457 | if( p->n ) appendText(p, "|", 0); |
| 2458 | for(i=0; i<nArg; i++){ |
| 2459 | if( i ) appendText(p, ",", 0); |
| 2460 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2461 | } |
| 2462 | return 0; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | ** Generate an appropriate SELFTEST table in the main database. |
| 2467 | */ |
| 2468 | static void createSelftestTable(ShellState *p){ |
| 2469 | char *zErrMsg = 0; |
| 2470 | sqlite3_exec(p->db, |
| 2471 | "SAVEPOINT selftest_init;\n" |
| 2472 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
| 2473 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2474 | " op TEXT,\n" /* Operator: memo run */ |
| 2475 | " cmd TEXT,\n" /* Command text */ |
| 2476 | " ans TEXT\n" /* Desired answer */ |
| 2477 | ");" |
| 2478 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2479 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2480 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2481 | " 'memo','Tests generated by --init');\n" |
| 2482 | "INSERT INTO [_shell$self]\n" |
| 2483 | " SELECT 'run',\n" |
| 2484 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2485 | "FROM sqlite_schema ORDER BY 2'',224))',\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2486 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2487 | "FROM sqlite_schema ORDER BY 2',224));\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2488 | "INSERT INTO [_shell$self]\n" |
| 2489 | " SELECT 'run'," |
| 2490 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2491 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2492 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2493 | " FROM (\n" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2494 | " SELECT name FROM sqlite_schema\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2495 | " WHERE type='table'\n" |
| 2496 | " AND name<>'selftest'\n" |
| 2497 | " AND coalesce(rootpage,0)>0\n" |
| 2498 | " )\n" |
| 2499 | " ORDER BY name;\n" |
| 2500 | "INSERT INTO [_shell$self]\n" |
| 2501 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
| 2502 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2503 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2504 | "DROP TABLE [_shell$self];" |
| 2505 | ,0,0,&zErrMsg); |
| 2506 | if( zErrMsg ){ |
| 2507 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2508 | sqlite3_free(zErrMsg); |
| 2509 | } |
| 2510 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
| 2511 | } |
| 2512 | |
| 2513 | |
| 2514 | /* |
| 2515 | ** Set the destination table field of the ShellState structure to |
| 2516 | ** the name of the table given. Escape any quote characters in the |
| 2517 | ** table name. |
| 2518 | */ |
| 2519 | static void set_table_name(ShellState *p, const char *zName){ |
| 2520 | int i, n; |
mistachkin | 2158a0c | 2017-09-09 00:51:36 +0000 | [diff] [blame] | 2521 | char cQuote; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2522 | char *z; |
| 2523 | |
| 2524 | if( p->zDestTable ){ |
| 2525 | free(p->zDestTable); |
| 2526 | p->zDestTable = 0; |
| 2527 | } |
| 2528 | if( zName==0 ) return; |
| 2529 | cQuote = quoteChar(zName); |
| 2530 | n = strlen30(zName); |
| 2531 | if( cQuote ) n += n+2; |
| 2532 | z = p->zDestTable = malloc( n+1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2533 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2534 | n = 0; |
| 2535 | if( cQuote ) z[n++] = cQuote; |
| 2536 | for(i=0; zName[i]; i++){ |
| 2537 | z[n++] = zName[i]; |
| 2538 | if( zName[i]==cQuote ) z[n++] = cQuote; |
| 2539 | } |
| 2540 | if( cQuote ) z[n++] = cQuote; |
| 2541 | z[n] = 0; |
| 2542 | } |
| 2543 | |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2544 | /* |
| 2545 | ** Maybe construct two lines of text that point out the position of a |
| 2546 | ** syntax error. Return a pointer to the text, in memory obtained from |
| 2547 | ** sqlite3_malloc(). Or, if the most recent error does not involve a |
| 2548 | ** specific token that we can point to, return an empty string. |
| 2549 | ** |
| 2550 | ** In all cases, the memory returned is obtained from sqlite3_malloc64() |
| 2551 | ** and should be released by the caller invoking sqlite3_free(). |
| 2552 | */ |
| 2553 | static char *shell_error_context(const char *zSql, sqlite3 *db){ |
| 2554 | int iOffset; |
| 2555 | size_t len; |
| 2556 | char *zCode; |
| 2557 | char *zMsg; |
| 2558 | int i; |
| 2559 | if( db==0 |
| 2560 | || zSql==0 |
| 2561 | || (iOffset = sqlite3_error_offset(db))<0 |
| 2562 | ){ |
| 2563 | return sqlite3_mprintf(""); |
| 2564 | } |
| 2565 | while( iOffset>50 ){ |
| 2566 | iOffset--; |
| 2567 | zSql++; |
| 2568 | while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } |
| 2569 | } |
| 2570 | len = strlen(zSql); |
| 2571 | if( len>78 ){ |
| 2572 | len = 78; |
| 2573 | while( (zSql[len]&0xc0)==0x80 ) len--; |
| 2574 | } |
| 2575 | zCode = sqlite3_mprintf("%.*s", len, zSql); |
| 2576 | for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } |
| 2577 | if( iOffset<25 ){ |
| 2578 | zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, ""); |
| 2579 | }else{ |
| 2580 | zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, ""); |
| 2581 | } |
| 2582 | return zMsg; |
| 2583 | } |
| 2584 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2585 | |
| 2586 | /* |
| 2587 | ** Execute a query statement that will generate SQL output. Print |
| 2588 | ** the result columns, comma-separated, on a line and then add a |
| 2589 | ** semicolon terminator to the end of that line. |
| 2590 | ** |
| 2591 | ** If the number of columns is 1 and that column contains text "--" |
| 2592 | ** then write the semicolon on a separate line. That way, if a |
| 2593 | ** "--" comment occurs at the end of the statement, the comment |
| 2594 | ** won't consume the semicolon terminator. |
| 2595 | */ |
| 2596 | static int run_table_dump_query( |
| 2597 | ShellState *p, /* Query context */ |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 2598 | const char *zSelect /* SELECT statement to extract content */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2599 | ){ |
| 2600 | sqlite3_stmt *pSelect; |
| 2601 | int rc; |
| 2602 | int nResult; |
| 2603 | int i; |
| 2604 | const char *z; |
| 2605 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
| 2606 | if( rc!=SQLITE_OK || !pSelect ){ |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2607 | char *zContext = shell_error_context(zSelect, p->db); |
| 2608 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc, |
| 2609 | sqlite3_errmsg(p->db), zContext); |
| 2610 | sqlite3_free(zContext); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2611 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2612 | return rc; |
| 2613 | } |
| 2614 | rc = sqlite3_step(pSelect); |
| 2615 | nResult = sqlite3_column_count(pSelect); |
| 2616 | while( rc==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2617 | z = (const char*)sqlite3_column_text(pSelect, 0); |
| 2618 | utf8_printf(p->out, "%s", z); |
| 2619 | for(i=1; i<nResult; i++){ |
| 2620 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
| 2621 | } |
| 2622 | if( z==0 ) z = ""; |
| 2623 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2624 | if( z[0] ){ |
| 2625 | raw_printf(p->out, "\n;\n"); |
| 2626 | }else{ |
| 2627 | raw_printf(p->out, ";\n"); |
| 2628 | } |
| 2629 | rc = sqlite3_step(pSelect); |
| 2630 | } |
| 2631 | rc = sqlite3_finalize(pSelect); |
| 2632 | if( rc!=SQLITE_OK ){ |
| 2633 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2634 | sqlite3_errmsg(p->db)); |
| 2635 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2636 | } |
| 2637 | return rc; |
| 2638 | } |
| 2639 | |
| 2640 | /* |
larrybr | f9a49b0 | 2021-10-26 16:57:09 +0000 | [diff] [blame] | 2641 | ** Allocate space and save off string indicating current error. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2642 | */ |
| 2643 | static char *save_err_msg( |
larrybr | f9a49b0 | 2021-10-26 16:57:09 +0000 | [diff] [blame] | 2644 | sqlite3 *db, /* Database to query */ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2645 | const char *zPhase, /* When the error occcurs */ |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2646 | int rc, /* Error code returned from API */ |
| 2647 | const char *zSql /* SQL string, or NULL */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2648 | ){ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2649 | char *zErr; |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2650 | char *zContext; |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2651 | sqlite3_str *pStr = sqlite3_str_new(0); |
| 2652 | sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); |
| 2653 | if( rc>1 ){ |
| 2654 | sqlite3_str_appendf(pStr, " (%d)", rc); |
| 2655 | } |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2656 | zContext = shell_error_context(zSql, db); |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2657 | if( zContext ){ |
| 2658 | sqlite3_str_appendall(pStr, zContext); |
| 2659 | sqlite3_free(zContext); |
| 2660 | } |
| 2661 | zErr = sqlite3_str_finish(pStr); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2662 | shell_check_oom(zErr); |
| 2663 | return zErr; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | #ifdef __linux__ |
| 2667 | /* |
| 2668 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2669 | */ |
| 2670 | static void displayLinuxIoStats(FILE *out){ |
| 2671 | FILE *in; |
| 2672 | char z[200]; |
| 2673 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2674 | in = fopen(z, "rb"); |
| 2675 | if( in==0 ) return; |
| 2676 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2677 | static const struct { |
| 2678 | const char *zPattern; |
| 2679 | const char *zDesc; |
| 2680 | } aTrans[] = { |
| 2681 | { "rchar: ", "Bytes received by read():" }, |
| 2682 | { "wchar: ", "Bytes sent to write():" }, |
| 2683 | { "syscr: ", "Read() system calls:" }, |
| 2684 | { "syscw: ", "Write() system calls:" }, |
| 2685 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2686 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2687 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 2688 | }; |
| 2689 | int i; |
| 2690 | for(i=0; i<ArraySize(aTrans); i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 2691 | int n = strlen30(aTrans[i].zPattern); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2692 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
| 2693 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
| 2694 | break; |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | fclose(in); |
| 2699 | } |
| 2700 | #endif |
| 2701 | |
| 2702 | /* |
| 2703 | ** Display a single line of status using 64-bit values. |
| 2704 | */ |
| 2705 | static void displayStatLine( |
| 2706 | ShellState *p, /* The shell context */ |
| 2707 | char *zLabel, /* Label for this one line */ |
| 2708 | char *zFormat, /* Format for the result */ |
| 2709 | int iStatusCtrl, /* Which status to display */ |
| 2710 | int bReset /* True to reset the stats */ |
| 2711 | ){ |
| 2712 | sqlite3_int64 iCur = -1; |
| 2713 | sqlite3_int64 iHiwtr = -1; |
| 2714 | int i, nPercent; |
| 2715 | char zLine[200]; |
| 2716 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2717 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2718 | if( zFormat[i]=='%' ) nPercent++; |
| 2719 | } |
| 2720 | if( nPercent>1 ){ |
| 2721 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2722 | }else{ |
| 2723 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2724 | } |
| 2725 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2726 | } |
| 2727 | |
| 2728 | /* |
| 2729 | ** Display memory stats. |
| 2730 | */ |
| 2731 | static int display_stats( |
| 2732 | sqlite3 *db, /* Database to query */ |
| 2733 | ShellState *pArg, /* Pointer to ShellState */ |
| 2734 | int bReset /* True to reset the stats */ |
| 2735 | ){ |
| 2736 | int iCur; |
| 2737 | int iHiwtr; |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2738 | FILE *out; |
| 2739 | if( pArg==0 || pArg->out==0 ) return 0; |
| 2740 | out = pArg->out; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2741 | |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 2742 | if( pArg->pStmt && pArg->statsOn==2 ){ |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2743 | int nCol, i, x; |
| 2744 | sqlite3_stmt *pStmt = pArg->pStmt; |
| 2745 | char z[100]; |
| 2746 | nCol = sqlite3_column_count(pStmt); |
| 2747 | raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); |
| 2748 | for(i=0; i<nCol; i++){ |
| 2749 | sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); |
| 2750 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2751 | #ifndef SQLITE_OMIT_DECLTYPE |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2752 | sqlite3_snprintf(30, z+x, "declared type:"); |
| 2753 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2754 | #endif |
| 2755 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2756 | sqlite3_snprintf(30, z+x, "database name:"); |
| 2757 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); |
| 2758 | sqlite3_snprintf(30, z+x, "table name:"); |
| 2759 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); |
| 2760 | sqlite3_snprintf(30, z+x, "origin name:"); |
| 2761 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2762 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2763 | } |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2764 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2765 | |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 2766 | if( pArg->statsOn==3 ){ |
| 2767 | if( pArg->pStmt ){ |
| 2768 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2769 | raw_printf(pArg->out, "VM-steps: %d\n", iCur); |
| 2770 | } |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2774 | displayStatLine(pArg, "Memory Used:", |
| 2775 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2776 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2777 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
| 2778 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 2779 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2780 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
| 2781 | } |
| 2782 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2783 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
| 2784 | displayStatLine(pArg, "Largest Allocation:", |
| 2785 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2786 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2787 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2788 | #ifdef YYTRACKMAXSTACKDEPTH |
| 2789 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2790 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
| 2791 | #endif |
| 2792 | |
| 2793 | if( db ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2794 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2795 | iHiwtr = iCur = -1; |
| 2796 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2797 | &iCur, &iHiwtr, bReset); |
| 2798 | raw_printf(pArg->out, |
| 2799 | "Lookaside Slots Used: %d (max %d)\n", |
| 2800 | iCur, iHiwtr); |
| 2801 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2802 | &iCur, &iHiwtr, bReset); |
| 2803 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2804 | iHiwtr); |
| 2805 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2806 | &iCur, &iHiwtr, bReset); |
| 2807 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2808 | iHiwtr); |
| 2809 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2810 | &iCur, &iHiwtr, bReset); |
| 2811 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2812 | iHiwtr); |
| 2813 | } |
| 2814 | iHiwtr = iCur = -1; |
| 2815 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
| 2816 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2817 | iCur); |
| 2818 | iHiwtr = iCur = -1; |
| 2819 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
| 2820 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
| 2821 | iHiwtr = iCur = -1; |
| 2822 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
| 2823 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
| 2824 | iHiwtr = iCur = -1; |
| 2825 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
| 2826 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
| 2827 | iHiwtr = iCur = -1; |
drh | ffc78a4 | 2018-03-14 14:53:50 +0000 | [diff] [blame] | 2828 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); |
| 2829 | raw_printf(pArg->out, "Page cache spills: %d\n", iCur); |
| 2830 | iHiwtr = iCur = -1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2831 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
| 2832 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| 2833 | iCur); |
| 2834 | iHiwtr = iCur = -1; |
| 2835 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
| 2836 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| 2837 | iCur); |
| 2838 | } |
| 2839 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2840 | if( pArg->pStmt ){ |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 2841 | int iHit, iMiss; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2842 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2843 | bReset); |
| 2844 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
| 2845 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
| 2846 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
| 2847 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
| 2848 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 2849 | iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset); |
| 2850 | iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset); |
| 2851 | if( iHit || iMiss ){ |
| 2852 | raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n", |
| 2853 | iHit, iHit+iMiss); |
| 2854 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2855 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2856 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 2857 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2858 | raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); |
| 2859 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); |
| 2860 | raw_printf(pArg->out, "Number of times run: %d\n", iCur); |
| 2861 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); |
| 2862 | raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | #ifdef __linux__ |
| 2866 | displayLinuxIoStats(pArg->out); |
| 2867 | #endif |
| 2868 | |
| 2869 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2870 | |
| 2871 | return 0; |
| 2872 | } |
| 2873 | |
| 2874 | /* |
| 2875 | ** Display scan stats. |
| 2876 | */ |
| 2877 | static void display_scanstats( |
| 2878 | sqlite3 *db, /* Database to query */ |
| 2879 | ShellState *pArg /* Pointer to ShellState */ |
| 2880 | ){ |
| 2881 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2882 | UNUSED_PARAMETER(db); |
| 2883 | UNUSED_PARAMETER(pArg); |
| 2884 | #else |
| 2885 | int i, k, n, mx; |
| 2886 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
| 2887 | mx = 0; |
| 2888 | for(k=0; k<=mx; k++){ |
| 2889 | double rEstLoop = 1.0; |
| 2890 | for(i=n=0; 1; i++){ |
| 2891 | sqlite3_stmt *p = pArg->pStmt; |
| 2892 | sqlite3_int64 nLoop, nVisit; |
| 2893 | double rEst; |
| 2894 | int iSid; |
| 2895 | const char *zExplain; |
| 2896 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2897 | break; |
| 2898 | } |
| 2899 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| 2900 | if( iSid>mx ) mx = iSid; |
| 2901 | if( iSid!=k ) continue; |
| 2902 | if( n==0 ){ |
| 2903 | rEstLoop = (double)nLoop; |
| 2904 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| 2905 | } |
| 2906 | n++; |
| 2907 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2908 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2909 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| 2910 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| 2911 | rEstLoop *= rEst; |
| 2912 | raw_printf(pArg->out, |
| 2913 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| 2914 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| 2915 | ); |
| 2916 | } |
| 2917 | } |
| 2918 | raw_printf(pArg->out, "---------------------------\n"); |
| 2919 | #endif |
| 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2924 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2925 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2926 | ** Otherwise, return zero. |
| 2927 | */ |
| 2928 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2929 | int i; |
| 2930 | for(i=0; azArray[i]; i++){ |
| 2931 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2932 | } |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | /* |
| 2937 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| 2938 | ** and populate the ShellState.aiIndent[] array with the number of |
| 2939 | ** spaces each opcode should be indented before it is output. |
| 2940 | ** |
| 2941 | ** The indenting rules are: |
| 2942 | ** |
| 2943 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2944 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2945 | ** itself by 2 spaces. |
| 2946 | ** |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 2947 | ** * Do the previous for "Return" instructions for when P2 is positive. |
| 2948 | ** See tag-20220407a in wherecode.c and vdbe.c. |
| 2949 | ** |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2950 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2951 | ** and ends on one of: |
| 2952 | ** Yield SeekGt SeekLt RowSetRead Rewind |
| 2953 | ** or if the P1 parameter is one instead of zero, |
| 2954 | ** then indent all opcodes between the earlier instruction |
| 2955 | ** and "Goto" by 2 spaces. |
| 2956 | */ |
| 2957 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| 2958 | const char *zSql; /* The text of the SQL statement */ |
| 2959 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2960 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2961 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| 2962 | int iOp; /* Index of operation in p->aiIndent[] */ |
| 2963 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 2964 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2965 | "Return", 0 }; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2966 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2967 | "Rewind", 0 }; |
| 2968 | const char *azGoto[] = { "Goto", 0 }; |
| 2969 | |
| 2970 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2971 | ** cannot be verified, return early. */ |
| 2972 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2973 | p->cMode = p->mode; |
| 2974 | return; |
| 2975 | } |
| 2976 | zSql = sqlite3_sql(pSql); |
| 2977 | if( zSql==0 ) return; |
| 2978 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| 2979 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2980 | p->cMode = p->mode; |
| 2981 | return; |
| 2982 | } |
| 2983 | |
| 2984 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2985 | int i; |
| 2986 | int iAddr = sqlite3_column_int(pSql, 0); |
| 2987 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| 2988 | |
| 2989 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2990 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2991 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2992 | ** the current instruction is part of a sub-program generated by an |
| 2993 | ** SQL trigger or foreign key. */ |
| 2994 | int p2 = sqlite3_column_int(pSql, 3); |
| 2995 | int p2op = (p2 + (iOp-iAddr)); |
| 2996 | |
| 2997 | /* Grow the p->aiIndent array as required */ |
| 2998 | if( iOp>=nAlloc ){ |
| 2999 | if( iOp==0 ){ |
| 3000 | /* Do further verfication that this is explain output. Abort if |
| 3001 | ** it is not */ |
| 3002 | static const char *explainCols[] = { |
| 3003 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 3004 | int jj; |
| 3005 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 3006 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 3007 | p->cMode = p->mode; |
| 3008 | sqlite3_reset(pSql); |
| 3009 | return; |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | nAlloc += 100; |
| 3014 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3015 | shell_check_oom(p->aiIndent); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3016 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3017 | shell_check_oom(abYield); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3018 | } |
| 3019 | abYield[iOp] = str_in_array(zOp, azYield); |
| 3020 | p->aiIndent[iOp] = 0; |
| 3021 | p->nIndent = iOp+1; |
| 3022 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 3023 | if( str_in_array(zOp, azNext) && p2op>0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3024 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 3025 | } |
| 3026 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 3027 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 3028 | ){ |
| 3029 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | p->iIndent = 0; |
| 3034 | sqlite3_free(abYield); |
| 3035 | sqlite3_reset(pSql); |
| 3036 | } |
| 3037 | |
| 3038 | /* |
| 3039 | ** Free the array allocated by explain_data_prepare(). |
| 3040 | */ |
| 3041 | static void explain_data_delete(ShellState *p){ |
| 3042 | sqlite3_free(p->aiIndent); |
| 3043 | p->aiIndent = 0; |
| 3044 | p->nIndent = 0; |
| 3045 | p->iIndent = 0; |
| 3046 | } |
| 3047 | |
| 3048 | /* |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 3049 | ** Disable and restore .wheretrace and .treetrace/.selecttrace settings. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3050 | */ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3051 | static unsigned int savedSelectTrace; |
| 3052 | static unsigned int savedWhereTrace; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3053 | static void disable_debug_trace_modes(void){ |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3054 | unsigned int zero = 0; |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3055 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3056 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3057 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3058 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3059 | } |
| 3060 | static void restore_debug_trace_modes(void){ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3061 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); |
| 3062 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3065 | /* Create the TEMP table used to store parameter bindings */ |
| 3066 | static void bind_table_init(ShellState *p){ |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3067 | int wrSchema = 0; |
drh | 4b86e20 | 2020-01-19 20:37:26 +0000 | [diff] [blame] | 3068 | int defensiveMode = 0; |
| 3069 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); |
| 3070 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3071 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); |
| 3072 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3073 | sqlite3_exec(p->db, |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3074 | "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3075 | " key TEXT PRIMARY KEY,\n" |
larrybr | dabada6 | 2021-04-04 12:52:58 +0000 | [diff] [blame] | 3076 | " value\n" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3077 | ") WITHOUT ROWID;", |
| 3078 | 0, 0, 0); |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3079 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); |
drh | 4b86e20 | 2020-01-19 20:37:26 +0000 | [diff] [blame] | 3080 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3083 | /* |
| 3084 | ** Bind parameters on a prepared statement. |
| 3085 | ** |
| 3086 | ** Parameter bindings are taken from a TEMP table of the form: |
| 3087 | ** |
drh | 1cb0263 | 2019-03-25 22:05:22 +0000 | [diff] [blame] | 3088 | ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3089 | ** WITHOUT ROWID; |
| 3090 | ** |
drh | 91654b2 | 2020-04-02 13:21:10 +0000 | [diff] [blame] | 3091 | ** No bindings occur if this table does not exist. The name of the table |
| 3092 | ** begins with "sqlite_" so that it will not collide with ordinary application |
| 3093 | ** tables. The table must be in the TEMP schema. |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3094 | */ |
| 3095 | static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ |
| 3096 | int nVar; |
| 3097 | int i; |
| 3098 | int rc; |
| 3099 | sqlite3_stmt *pQ = 0; |
| 3100 | |
| 3101 | nVar = sqlite3_bind_parameter_count(pStmt); |
| 3102 | if( nVar==0 ) return; /* Nothing to do */ |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3103 | if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3104 | "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ |
| 3105 | return; /* Parameter table does not exist */ |
| 3106 | } |
| 3107 | rc = sqlite3_prepare_v2(pArg->db, |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3108 | "SELECT value FROM temp.sqlite_parameters" |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3109 | " WHERE key=?1", -1, &pQ, 0); |
| 3110 | if( rc || pQ==0 ) return; |
| 3111 | for(i=1; i<=nVar; i++){ |
| 3112 | char zNum[30]; |
| 3113 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
| 3114 | if( zVar==0 ){ |
| 3115 | sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); |
| 3116 | zVar = zNum; |
| 3117 | } |
| 3118 | sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); |
| 3119 | if( sqlite3_step(pQ)==SQLITE_ROW ){ |
| 3120 | sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); |
| 3121 | }else{ |
| 3122 | sqlite3_bind_null(pStmt, i); |
| 3123 | } |
| 3124 | sqlite3_reset(pQ); |
| 3125 | } |
| 3126 | sqlite3_finalize(pQ); |
| 3127 | } |
| 3128 | |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3129 | /* |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3130 | ** UTF8 box-drawing characters. Imagine box lines like this: |
| 3131 | ** |
| 3132 | ** 1 |
| 3133 | ** | |
| 3134 | ** 4 --+-- 2 |
| 3135 | ** | |
| 3136 | ** 3 |
| 3137 | ** |
| 3138 | ** Each box characters has between 2 and 4 of the lines leading from |
| 3139 | ** the center. The characters are here identified by the numbers of |
| 3140 | ** their corresponding lines. |
| 3141 | */ |
| 3142 | #define BOX_24 "\342\224\200" /* U+2500 --- */ |
| 3143 | #define BOX_13 "\342\224\202" /* U+2502 | */ |
| 3144 | #define BOX_23 "\342\224\214" /* U+250c ,- */ |
| 3145 | #define BOX_34 "\342\224\220" /* U+2510 -, */ |
| 3146 | #define BOX_12 "\342\224\224" /* U+2514 '- */ |
| 3147 | #define BOX_14 "\342\224\230" /* U+2518 -' */ |
| 3148 | #define BOX_123 "\342\224\234" /* U+251c |- */ |
| 3149 | #define BOX_134 "\342\224\244" /* U+2524 -| */ |
| 3150 | #define BOX_234 "\342\224\254" /* U+252c -,- */ |
| 3151 | #define BOX_124 "\342\224\264" /* U+2534 -'- */ |
| 3152 | #define BOX_1234 "\342\224\274" /* U+253c -|- */ |
| 3153 | |
| 3154 | /* Draw horizontal line N characters long using unicode box |
| 3155 | ** characters |
| 3156 | */ |
| 3157 | static void print_box_line(FILE *out, int N){ |
| 3158 | const char zDash[] = |
| 3159 | BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 |
| 3160 | BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; |
| 3161 | const int nDash = sizeof(zDash) - 1; |
| 3162 | N *= 3; |
| 3163 | while( N>nDash ){ |
| 3164 | utf8_printf(out, zDash); |
| 3165 | N -= nDash; |
| 3166 | } |
| 3167 | utf8_printf(out, "%.*s", N, zDash); |
| 3168 | } |
| 3169 | |
| 3170 | /* |
| 3171 | ** Draw a horizontal separator for a MODE_Box table. |
| 3172 | */ |
| 3173 | static void print_box_row_separator( |
| 3174 | ShellState *p, |
| 3175 | int nArg, |
| 3176 | const char *zSep1, |
| 3177 | const char *zSep2, |
| 3178 | const char *zSep3 |
| 3179 | ){ |
| 3180 | int i; |
| 3181 | if( nArg>0 ){ |
| 3182 | utf8_printf(p->out, "%s", zSep1); |
| 3183 | print_box_line(p->out, p->actualWidth[0]+2); |
| 3184 | for(i=1; i<nArg; i++){ |
| 3185 | utf8_printf(p->out, "%s", zSep2); |
| 3186 | print_box_line(p->out, p->actualWidth[i]+2); |
| 3187 | } |
| 3188 | utf8_printf(p->out, "%s", zSep3); |
| 3189 | } |
| 3190 | fputs("\n", p->out); |
| 3191 | } |
| 3192 | |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3193 | /* |
| 3194 | ** z[] is a line of text that is to be displayed the .mode box or table or |
| 3195 | ** similar tabular formats. z[] might contain control characters such |
| 3196 | ** as \n, \t, \f, or \r. |
| 3197 | ** |
| 3198 | ** Compute characters to display on the first line of z[]. Stop at the |
| 3199 | ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3200 | ** from malloc()) of that first line, which caller should free sometime. |
| 3201 | ** Write anything to display on the next line into *pzTail. If this is |
| 3202 | ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3203 | */ |
| 3204 | static char *translateForDisplayAndDup( |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3205 | const unsigned char *z, /* Input text to be transformed */ |
| 3206 | const unsigned char **pzTail, /* OUT: Tail of the input for next line */ |
| 3207 | int mxWidth, /* Max width. 0 means no limit */ |
| 3208 | u8 bWordWrap /* If true, avoid breaking mid-word */ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3209 | ){ |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3210 | int i; /* Input bytes consumed */ |
| 3211 | int j; /* Output bytes generated */ |
| 3212 | int k; /* Input bytes to be displayed */ |
| 3213 | int n; /* Output column number */ |
| 3214 | unsigned char *zOut; /* Output text */ |
| 3215 | |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3216 | if( z==0 ){ |
| 3217 | *pzTail = 0; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | if( mxWidth<0 ) mxWidth = -mxWidth; |
| 3221 | if( mxWidth==0 ) mxWidth = 1000000; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3222 | i = j = n = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3223 | while( n<mxWidth ){ |
| 3224 | if( z[i]>=' ' ){ |
| 3225 | n++; |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3226 | do{ i++; j++; }while( (z[i]&0xc0)==0x80 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3227 | continue; |
| 3228 | } |
| 3229 | if( z[i]=='\t' ){ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3230 | do{ |
| 3231 | n++; |
| 3232 | j++; |
| 3233 | }while( (n&7)!=0 && n<mxWidth ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3234 | i++; |
| 3235 | continue; |
| 3236 | } |
| 3237 | break; |
| 3238 | } |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3239 | if( n>=mxWidth && bWordWrap ){ |
| 3240 | /* Perhaps try to back up to a better place to break the line */ |
| 3241 | for(k=i; k>i/2; k--){ |
| 3242 | if( isspace(z[k-1]) ) break; |
| 3243 | } |
| 3244 | if( k<=i/2 ){ |
| 3245 | for(k=i; k>i/2; k--){ |
drh | e66532a | 2022-02-01 13:17:11 +0000 | [diff] [blame] | 3246 | if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | if( k<=i/2 ){ |
| 3250 | k = i; |
| 3251 | }else{ |
| 3252 | i = k; |
| 3253 | while( z[i]==' ' ) i++; |
| 3254 | } |
| 3255 | }else{ |
| 3256 | k = i; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3257 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3258 | if( n>=mxWidth && z[i]>=' ' ){ |
| 3259 | *pzTail = &z[i]; |
| 3260 | }else if( z[i]=='\r' && z[i+1]=='\n' ){ |
| 3261 | *pzTail = z[i+2] ? &z[i+2] : 0; |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3262 | }else if( z[i]==0 || z[i+1]==0 ){ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3263 | *pzTail = 0; |
| 3264 | }else{ |
| 3265 | *pzTail = &z[i+1]; |
| 3266 | } |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3267 | zOut = malloc( j+1 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3268 | shell_check_oom(zOut); |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3269 | i = j = n = 0; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3270 | while( i<k ){ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3271 | if( z[i]>=' ' ){ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3272 | n++; |
| 3273 | do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3274 | continue; |
| 3275 | } |
| 3276 | if( z[i]=='\t' ){ |
| 3277 | do{ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3278 | n++; |
| 3279 | zOut[j++] = ' '; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3280 | }while( (n&7)!=0 && n<mxWidth ); |
| 3281 | i++; |
| 3282 | continue; |
| 3283 | } |
| 3284 | break; |
| 3285 | } |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3286 | zOut[j] = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3287 | return (char*)zOut; |
| 3288 | } |
| 3289 | |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3290 | /* Extract the value of the i-th current column for pStmt as an SQL literal |
| 3291 | ** value. Memory is obtained from sqlite3_malloc64() and must be freed by |
| 3292 | ** the caller. |
| 3293 | */ |
| 3294 | static char *quoted_column(sqlite3_stmt *pStmt, int i){ |
| 3295 | switch( sqlite3_column_type(pStmt, i) ){ |
| 3296 | case SQLITE_NULL: { |
| 3297 | return sqlite3_mprintf("NULL"); |
| 3298 | } |
| 3299 | case SQLITE_INTEGER: |
| 3300 | case SQLITE_FLOAT: { |
| 3301 | return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); |
| 3302 | } |
| 3303 | case SQLITE_TEXT: { |
| 3304 | return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i)); |
| 3305 | } |
| 3306 | case SQLITE_BLOB: { |
| 3307 | int j; |
| 3308 | sqlite3_str *pStr = sqlite3_str_new(0); |
| 3309 | const unsigned char *a = sqlite3_column_blob(pStmt,i); |
| 3310 | int n = sqlite3_column_bytes(pStmt,i); |
| 3311 | sqlite3_str_append(pStr, "x'", 2); |
| 3312 | for(j=0; j<n; j++){ |
| 3313 | sqlite3_str_appendf(pStr, "%02x", a[j]); |
| 3314 | } |
| 3315 | sqlite3_str_append(pStr, "'", 1); |
| 3316 | return sqlite3_str_finish(pStr); |
| 3317 | } |
| 3318 | } |
| 3319 | return 0; /* Not reached */ |
| 3320 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3321 | |
| 3322 | /* |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3323 | ** Run a prepared statement and output the result in one of the |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3324 | ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, |
| 3325 | ** or MODE_Box. |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3326 | ** |
| 3327 | ** This is different from ordinary exec_prepared_stmt() in that |
| 3328 | ** it has to run the entire query and gather the results into memory |
| 3329 | ** first, in order to determine column widths, before providing |
| 3330 | ** any output. |
| 3331 | */ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3332 | static void exec_prepared_stmt_columnar( |
| 3333 | ShellState *p, /* Pointer to ShellState */ |
| 3334 | sqlite3_stmt *pStmt /* Statment to run */ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3335 | ){ |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3336 | sqlite3_int64 nRow = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3337 | int nColumn = 0; |
| 3338 | char **azData = 0; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3339 | sqlite3_int64 nAlloc = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3340 | char *abRowDiv = 0; |
| 3341 | const unsigned char *uz; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3342 | const char *z; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3343 | char **azQuoted = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3344 | int rc; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3345 | sqlite3_int64 i, nData; |
| 3346 | int j, nTotal, w, n; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3347 | const char *colSep = 0; |
| 3348 | const char *rowSep = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3349 | const unsigned char **azNextLine = 0; |
| 3350 | int bNextLine = 0; |
| 3351 | int bMultiLineRowExists = 0; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3352 | int bw = p->cmOpts.bWordWrap; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3353 | const char *zEmpty = ""; |
| 3354 | const char *zShowNull = p->nullValue; |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3355 | |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3356 | rc = sqlite3_step(pStmt); |
| 3357 | if( rc!=SQLITE_ROW ) return; |
| 3358 | nColumn = sqlite3_column_count(pStmt); |
| 3359 | nAlloc = nColumn*4; |
drh | 01a8ad2 | 2021-03-20 23:15:52 +0000 | [diff] [blame] | 3360 | if( nAlloc<=0 ) nAlloc = 1; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3361 | azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3362 | shell_check_oom(azData); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3363 | azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3364 | shell_check_oom((void*)azNextLine); |
| 3365 | memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3366 | if( p->cmOpts.bQuote ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3367 | azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); |
| 3368 | shell_check_oom(azQuoted); |
| 3369 | memset(azQuoted, 0, nColumn*sizeof(char*) ); |
| 3370 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3371 | abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); |
| 3372 | shell_check_oom(abRowDiv); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3373 | if( nColumn>p->nWidth ){ |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 3374 | p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3375 | shell_check_oom(p->colWidth); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3376 | for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; |
| 3377 | p->nWidth = nColumn; |
| 3378 | p->actualWidth = &p->colWidth[nColumn]; |
| 3379 | } |
| 3380 | memset(p->actualWidth, 0, nColumn*sizeof(int)); |
| 3381 | for(i=0; i<nColumn; i++){ |
| 3382 | w = p->colWidth[i]; |
| 3383 | if( w<0 ) w = -w; |
| 3384 | p->actualWidth[i] = w; |
| 3385 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3386 | for(i=0; i<nColumn; i++){ |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3387 | const unsigned char *zNotUsed; |
| 3388 | int wx = p->colWidth[i]; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3389 | if( wx==0 ){ |
| 3390 | wx = p->cmOpts.iWrap; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3391 | } |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3392 | if( wx<0 ) wx = -wx; |
| 3393 | uz = (const unsigned char*)sqlite3_column_name(pStmt,i); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3394 | azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3395 | } |
| 3396 | do{ |
| 3397 | int useNextLine = bNextLine; |
| 3398 | bNextLine = 0; |
| 3399 | if( (nRow+2)*nColumn >= nAlloc ){ |
| 3400 | nAlloc *= 2; |
| 3401 | azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); |
| 3402 | shell_check_oom(azData); |
| 3403 | abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); |
| 3404 | shell_check_oom(abRowDiv); |
| 3405 | } |
| 3406 | abRowDiv[nRow] = 1; |
| 3407 | nRow++; |
| 3408 | for(i=0; i<nColumn; i++){ |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3409 | int wx = p->colWidth[i]; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3410 | if( wx==0 ){ |
| 3411 | wx = p->cmOpts.iWrap; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3412 | } |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3413 | if( wx<0 ) wx = -wx; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3414 | if( useNextLine ){ |
| 3415 | uz = azNextLine[i]; |
drh | 7e9a56f | 2022-04-21 19:14:23 +0000 | [diff] [blame] | 3416 | if( uz==0 ) uz = (u8*)zEmpty; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3417 | }else if( p->cmOpts.bQuote ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3418 | sqlite3_free(azQuoted[i]); |
| 3419 | azQuoted[i] = quoted_column(pStmt,i); |
| 3420 | uz = (const unsigned char*)azQuoted[i]; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3421 | }else{ |
| 3422 | uz = (const unsigned char*)sqlite3_column_text(pStmt,i); |
drh | 7e9a56f | 2022-04-21 19:14:23 +0000 | [diff] [blame] | 3423 | if( uz==0 ) uz = (u8*)zShowNull; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3424 | } |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3425 | azData[nRow*nColumn + i] |
| 3426 | = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3427 | if( azNextLine[i] ){ |
| 3428 | bNextLine = 1; |
| 3429 | abRowDiv[nRow-1] = 0; |
| 3430 | bMultiLineRowExists = 1; |
| 3431 | } |
| 3432 | } |
| 3433 | }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3434 | nTotal = nColumn*(nRow+1); |
| 3435 | for(i=0; i<nTotal; i++){ |
| 3436 | z = azData[i]; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3437 | if( z==0 ) z = (char*)zEmpty; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3438 | n = strlenChar(z); |
| 3439 | j = i%nColumn; |
| 3440 | if( n>p->actualWidth[j] ) p->actualWidth[j] = n; |
| 3441 | } |
drh | 9994298 | 2020-06-15 20:05:37 +0000 | [diff] [blame] | 3442 | if( seenInterrupt ) goto columnar_end; |
drh | 01a8ad2 | 2021-03-20 23:15:52 +0000 | [diff] [blame] | 3443 | if( nColumn==0 ) goto columnar_end; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3444 | switch( p->cMode ){ |
| 3445 | case MODE_Column: { |
| 3446 | colSep = " "; |
| 3447 | rowSep = "\n"; |
| 3448 | if( p->showHeader ){ |
| 3449 | for(i=0; i<nColumn; i++){ |
| 3450 | w = p->actualWidth[i]; |
| 3451 | if( p->colWidth[i]<0 ) w = -w; |
| 3452 | utf8_width_print(p->out, w, azData[i]); |
| 3453 | fputs(i==nColumn-1?"\n":" ", p->out); |
| 3454 | } |
| 3455 | for(i=0; i<nColumn; i++){ |
| 3456 | print_dashes(p->out, p->actualWidth[i]); |
| 3457 | fputs(i==nColumn-1?"\n":" ", p->out); |
| 3458 | } |
| 3459 | } |
| 3460 | break; |
| 3461 | } |
| 3462 | case MODE_Table: { |
| 3463 | colSep = " | "; |
| 3464 | rowSep = " |\n"; |
| 3465 | print_row_separator(p, nColumn, "+"); |
| 3466 | fputs("| ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3467 | for(i=0; i<nColumn; i++){ |
| 3468 | w = p->actualWidth[i]; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3469 | n = strlenChar(azData[i]); |
| 3470 | utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
| 3471 | fputs(i==nColumn-1?" |\n":" | ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3472 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3473 | print_row_separator(p, nColumn, "+"); |
| 3474 | break; |
| 3475 | } |
| 3476 | case MODE_Markdown: { |
| 3477 | colSep = " | "; |
| 3478 | rowSep = " |\n"; |
| 3479 | fputs("| ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3480 | for(i=0; i<nColumn; i++){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3481 | w = p->actualWidth[i]; |
| 3482 | n = strlenChar(azData[i]); |
| 3483 | utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
| 3484 | fputs(i==nColumn-1?" |\n":" | ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3485 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3486 | print_row_separator(p, nColumn, "|"); |
| 3487 | break; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3488 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3489 | case MODE_Box: { |
| 3490 | colSep = " " BOX_13 " "; |
| 3491 | rowSep = " " BOX_13 "\n"; |
| 3492 | print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); |
| 3493 | utf8_printf(p->out, BOX_13 " "); |
| 3494 | for(i=0; i<nColumn; i++){ |
| 3495 | w = p->actualWidth[i]; |
| 3496 | n = strlenChar(azData[i]); |
| 3497 | utf8_printf(p->out, "%*s%s%*s%s", |
| 3498 | (w-n)/2, "", azData[i], (w-n+1)/2, "", |
| 3499 | i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); |
| 3500 | } |
| 3501 | print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); |
| 3502 | break; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3503 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3504 | } |
| 3505 | for(i=nColumn, j=0; i<nTotal; i++, j++){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3506 | if( j==0 && p->cMode!=MODE_Column ){ |
| 3507 | utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); |
| 3508 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3509 | z = azData[i]; |
| 3510 | if( z==0 ) z = p->nullValue; |
| 3511 | w = p->actualWidth[j]; |
| 3512 | if( p->colWidth[j]<0 ) w = -w; |
| 3513 | utf8_width_print(p->out, w, z); |
| 3514 | if( j==nColumn-1 ){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3515 | utf8_printf(p->out, "%s", rowSep); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3516 | if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ |
| 3517 | if( p->cMode==MODE_Table ){ |
| 3518 | print_row_separator(p, nColumn, "+"); |
| 3519 | }else if( p->cMode==MODE_Box ){ |
| 3520 | print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); |
drh | 5aabdae | 2022-02-01 00:00:08 +0000 | [diff] [blame] | 3521 | }else if( p->cMode==MODE_Column ){ |
| 3522 | raw_printf(p->out, "\n"); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3523 | } |
| 3524 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3525 | j = -1; |
drh | dd853c3 | 2020-06-16 17:34:40 +0000 | [diff] [blame] | 3526 | if( seenInterrupt ) goto columnar_end; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3527 | }else{ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3528 | utf8_printf(p->out, "%s", colSep); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3529 | } |
| 3530 | } |
| 3531 | if( p->cMode==MODE_Table ){ |
| 3532 | print_row_separator(p, nColumn, "+"); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3533 | }else if( p->cMode==MODE_Box ){ |
| 3534 | print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3535 | } |
drh | 9994298 | 2020-06-15 20:05:37 +0000 | [diff] [blame] | 3536 | columnar_end: |
drh | dd853c3 | 2020-06-16 17:34:40 +0000 | [diff] [blame] | 3537 | if( seenInterrupt ){ |
| 3538 | utf8_printf(p->out, "Interrupt\n"); |
| 3539 | } |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3540 | nData = (nRow+1)*nColumn; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3541 | for(i=0; i<nData; i++){ |
| 3542 | z = azData[i]; |
drh | 744c17c | 2022-05-05 10:02:19 +0000 | [diff] [blame] | 3543 | if( z!=zEmpty && z!=zShowNull ) free(azData[i]); |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3544 | } |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3545 | sqlite3_free(azData); |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3546 | sqlite3_free((void*)azNextLine); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3547 | sqlite3_free(abRowDiv); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3548 | if( azQuoted ){ |
| 3549 | for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]); |
| 3550 | sqlite3_free(azQuoted); |
| 3551 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3552 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3553 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3554 | /* |
| 3555 | ** Run a prepared statement |
| 3556 | */ |
| 3557 | static void exec_prepared_stmt( |
| 3558 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3559 | sqlite3_stmt *pStmt /* Statment to run */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3560 | ){ |
| 3561 | int rc; |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3562 | sqlite3_uint64 nRow = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3563 | |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3564 | if( pArg->cMode==MODE_Column |
| 3565 | || pArg->cMode==MODE_Table |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3566 | || pArg->cMode==MODE_Box |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3567 | || pArg->cMode==MODE_Markdown |
| 3568 | ){ |
| 3569 | exec_prepared_stmt_columnar(pArg, pStmt); |
| 3570 | return; |
| 3571 | } |
| 3572 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3573 | /* perform the first step. this will tell us if we |
| 3574 | ** have a result set or not and how wide it is. |
| 3575 | */ |
| 3576 | rc = sqlite3_step(pStmt); |
| 3577 | /* if we have a result set... */ |
| 3578 | if( SQLITE_ROW == rc ){ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3579 | /* allocate space for col name ptr, value ptr, and type */ |
| 3580 | int nCol = sqlite3_column_count(pStmt); |
| 3581 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 3582 | if( !pData ){ |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 3583 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3584 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3585 | char **azCols = (char **)pData; /* Names of result columns */ |
| 3586 | char **azVals = &azCols[nCol]; /* Results */ |
| 3587 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 3588 | int i, x; |
| 3589 | assert(sizeof(int) <= sizeof(char *)); |
| 3590 | /* save off ptrs to column names */ |
| 3591 | for(i=0; i<nCol; i++){ |
| 3592 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 3593 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3594 | do{ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3595 | nRow++; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3596 | /* extract the data and data types */ |
| 3597 | for(i=0; i<nCol; i++){ |
| 3598 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
drh | 5d1bf4f | 2022-01-02 20:54:33 +0000 | [diff] [blame] | 3599 | if( x==SQLITE_BLOB |
| 3600 | && pArg |
| 3601 | && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) |
| 3602 | ){ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3603 | azVals[i] = ""; |
| 3604 | }else{ |
| 3605 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 3606 | } |
| 3607 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 3608 | rc = SQLITE_NOMEM; |
| 3609 | break; /* from for */ |
| 3610 | } |
| 3611 | } /* end for */ |
| 3612 | |
| 3613 | /* if data and types extracted successfully... */ |
| 3614 | if( SQLITE_ROW == rc ){ |
| 3615 | /* call the supplied callback with the result row data */ |
| 3616 | if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 3617 | rc = SQLITE_ABORT; |
| 3618 | }else{ |
| 3619 | rc = sqlite3_step(pStmt); |
| 3620 | } |
| 3621 | } |
| 3622 | } while( SQLITE_ROW == rc ); |
| 3623 | sqlite3_free(pData); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3624 | if( pArg->cMode==MODE_Json ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3625 | fputs("]\n", pArg->out); |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3626 | }else if( pArg->cMode==MODE_Count ){ |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 3627 | char zBuf[200]; |
| 3628 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n", |
| 3629 | nRow, nRow!=1 ? "s" : ""); |
| 3630 | printf("%s", zBuf); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3631 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3636 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3637 | /* |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3638 | ** This function is called to process SQL if the previous shell command |
| 3639 | ** was ".expert". It passes the SQL in the second argument directly to |
| 3640 | ** the sqlite3expert object. |
| 3641 | ** |
| 3642 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 3643 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 3644 | ** an English language error message. It is the responsibility of the |
| 3645 | ** caller to eventually free this buffer using sqlite3_free(). |
| 3646 | */ |
| 3647 | static int expertHandleSQL( |
| 3648 | ShellState *pState, |
| 3649 | const char *zSql, |
| 3650 | char **pzErr |
| 3651 | ){ |
| 3652 | assert( pState->expert.pExpert ); |
| 3653 | assert( pzErr==0 || *pzErr==0 ); |
| 3654 | return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); |
| 3655 | } |
| 3656 | |
| 3657 | /* |
| 3658 | ** This function is called either to silently clean up the object |
| 3659 | ** created by the ".expert" command (if bCancel==1), or to generate a |
| 3660 | ** report from it and then clean it up (if bCancel==0). |
| 3661 | ** |
| 3662 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 3663 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 3664 | ** an English language error message. It is the responsibility of the |
| 3665 | ** caller to eventually free this buffer using sqlite3_free(). |
| 3666 | */ |
| 3667 | static int expertFinish( |
| 3668 | ShellState *pState, |
| 3669 | int bCancel, |
| 3670 | char **pzErr |
| 3671 | ){ |
| 3672 | int rc = SQLITE_OK; |
| 3673 | sqlite3expert *p = pState->expert.pExpert; |
| 3674 | assert( p ); |
| 3675 | assert( bCancel || pzErr==0 || *pzErr==0 ); |
| 3676 | if( bCancel==0 ){ |
| 3677 | FILE *out = pState->out; |
| 3678 | int bVerbose = pState->expert.bVerbose; |
| 3679 | |
| 3680 | rc = sqlite3_expert_analyze(p, pzErr); |
| 3681 | if( rc==SQLITE_OK ){ |
| 3682 | int nQuery = sqlite3_expert_count(p); |
| 3683 | int i; |
| 3684 | |
| 3685 | if( bVerbose ){ |
| 3686 | const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
| 3687 | raw_printf(out, "-- Candidates -----------------------------\n"); |
| 3688 | raw_printf(out, "%s\n", zCand); |
| 3689 | } |
| 3690 | for(i=0; i<nQuery; i++){ |
| 3691 | const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
| 3692 | const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
| 3693 | const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
| 3694 | if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
| 3695 | if( bVerbose ){ |
| 3696 | raw_printf(out, "-- Query %d --------------------------------\n",i+1); |
| 3697 | raw_printf(out, "%s\n\n", zSql); |
| 3698 | } |
| 3699 | raw_printf(out, "%s\n", zIdx); |
| 3700 | raw_printf(out, "%s\n", zEQP); |
| 3701 | } |
| 3702 | } |
| 3703 | } |
| 3704 | sqlite3_expert_destroy(p); |
| 3705 | pState->expert.pExpert = 0; |
| 3706 | return rc; |
| 3707 | } |
| 3708 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3709 | /* |
| 3710 | ** Implementation of ".expert" dot command. |
| 3711 | */ |
| 3712 | static int expertDotCommand( |
| 3713 | ShellState *pState, /* Current shell tool state */ |
| 3714 | char **azArg, /* Array of arguments passed to dot command */ |
| 3715 | int nArg /* Number of entries in azArg[] */ |
| 3716 | ){ |
| 3717 | int rc = SQLITE_OK; |
| 3718 | char *zErr = 0; |
| 3719 | int i; |
| 3720 | int iSample = 0; |
| 3721 | |
| 3722 | assert( pState->expert.pExpert==0 ); |
| 3723 | memset(&pState->expert, 0, sizeof(ExpertInfo)); |
| 3724 | |
| 3725 | for(i=1; rc==SQLITE_OK && i<nArg; i++){ |
| 3726 | char *z = azArg[i]; |
| 3727 | int n; |
| 3728 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 3729 | n = strlen30(z); |
| 3730 | if( n>=2 && 0==strncmp(z, "-verbose", n) ){ |
| 3731 | pState->expert.bVerbose = 1; |
| 3732 | } |
| 3733 | else if( n>=2 && 0==strncmp(z, "-sample", n) ){ |
| 3734 | if( i==(nArg-1) ){ |
| 3735 | raw_printf(stderr, "option requires an argument: %s\n", z); |
| 3736 | rc = SQLITE_ERROR; |
| 3737 | }else{ |
| 3738 | iSample = (int)integerValue(azArg[++i]); |
| 3739 | if( iSample<0 || iSample>100 ){ |
| 3740 | raw_printf(stderr, "value out of range: %s\n", azArg[i]); |
| 3741 | rc = SQLITE_ERROR; |
| 3742 | } |
| 3743 | } |
| 3744 | } |
| 3745 | else{ |
| 3746 | raw_printf(stderr, "unknown option: %s\n", z); |
| 3747 | rc = SQLITE_ERROR; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | if( rc==SQLITE_OK ){ |
| 3752 | pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); |
| 3753 | if( pState->expert.pExpert==0 ){ |
drh | e0adf60 | 2021-12-16 14:26:16 +0000 | [diff] [blame] | 3754 | raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory"); |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3755 | rc = SQLITE_ERROR; |
| 3756 | }else{ |
| 3757 | sqlite3_expert_config( |
| 3758 | pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample |
| 3759 | ); |
| 3760 | } |
| 3761 | } |
drh | e0adf60 | 2021-12-16 14:26:16 +0000 | [diff] [blame] | 3762 | sqlite3_free(zErr); |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3763 | |
| 3764 | return rc; |
| 3765 | } |
| 3766 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3767 | |
| 3768 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3769 | ** Execute a statement or set of statements. Print |
| 3770 | ** any result rows/columns depending on the current mode |
| 3771 | ** set via the supplied callback. |
| 3772 | ** |
| 3773 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 3774 | ** function except it takes a slightly different callback |
| 3775 | ** and callback data argument. |
| 3776 | */ |
| 3777 | static int shell_exec( |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3778 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3779 | const char *zSql, /* SQL to be evaluated */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3780 | char **pzErrMsg /* Error msg written here */ |
| 3781 | ){ |
| 3782 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 3783 | int rc = SQLITE_OK; /* Return Code */ |
| 3784 | int rc2; |
| 3785 | const char *zLeftover; /* Tail of unprocessed SQL */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3786 | sqlite3 *db = pArg->db; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3787 | |
| 3788 | if( pzErrMsg ){ |
| 3789 | *pzErrMsg = NULL; |
| 3790 | } |
| 3791 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3792 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3793 | if( pArg->expert.pExpert ){ |
| 3794 | rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
| 3795 | return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
| 3796 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3797 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3798 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3799 | while( zSql[0] && (SQLITE_OK == rc) ){ |
| 3800 | static const char *zStmtSql; |
| 3801 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 3802 | if( SQLITE_OK != rc ){ |
| 3803 | if( pzErrMsg ){ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 3804 | *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3805 | } |
| 3806 | }else{ |
| 3807 | if( !pStmt ){ |
| 3808 | /* this happens for a comment or white-space */ |
| 3809 | zSql = zLeftover; |
| 3810 | while( IsSpace(zSql[0]) ) zSql++; |
| 3811 | continue; |
| 3812 | } |
| 3813 | zStmtSql = sqlite3_sql(pStmt); |
| 3814 | if( zStmtSql==0 ) zStmtSql = ""; |
| 3815 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 3816 | |
| 3817 | /* save off the prepared statment handle and reset row count */ |
| 3818 | if( pArg ){ |
| 3819 | pArg->pStmt = pStmt; |
| 3820 | pArg->cnt = 0; |
| 3821 | } |
| 3822 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3823 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3824 | if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3825 | sqlite3_stmt *pExplain; |
| 3826 | char *zEQP; |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3827 | int triggerEQP = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3828 | disable_debug_trace_modes(); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3829 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
| 3830 | if( pArg->autoEQP>=AUTOEQP_trigger ){ |
| 3831 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); |
| 3832 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3833 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3834 | shell_check_oom(zEQP); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3835 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3836 | if( rc==SQLITE_OK ){ |
| 3837 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3838 | const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3839 | int iEqpId = sqlite3_column_int(pExplain, 0); |
| 3840 | int iParentId = sqlite3_column_int(pExplain, 1); |
drh | 7e088a6 | 2020-05-02 00:01:39 +0000 | [diff] [blame] | 3841 | if( zEQPLine==0 ) zEQPLine = ""; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3842 | if( zEQPLine[0]=='-' ) eqp_render(pArg); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3843 | eqp_append(pArg, iEqpId, iParentId, zEQPLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3844 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3845 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3846 | } |
| 3847 | sqlite3_finalize(pExplain); |
| 3848 | sqlite3_free(zEQP); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3849 | if( pArg->autoEQP>=AUTOEQP_full ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3850 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 3851 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3852 | shell_check_oom(zEQP); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3853 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3854 | if( rc==SQLITE_OK ){ |
| 3855 | pArg->cMode = MODE_Explain; |
| 3856 | explain_data_prepare(pArg, pExplain); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3857 | exec_prepared_stmt(pArg, pExplain); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3858 | explain_data_delete(pArg); |
| 3859 | } |
| 3860 | sqlite3_finalize(pExplain); |
| 3861 | sqlite3_free(zEQP); |
| 3862 | } |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 3863 | if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ |
| 3864 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); |
| 3865 | /* Reprepare pStmt before reactiving trace modes */ |
| 3866 | sqlite3_finalize(pStmt); |
| 3867 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
drh | 3c49eaf | 2018-06-07 15:23:43 +0000 | [diff] [blame] | 3868 | if( pArg ) pArg->pStmt = pStmt; |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 3869 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3870 | restore_debug_trace_modes(); |
| 3871 | } |
| 3872 | |
| 3873 | if( pArg ){ |
| 3874 | pArg->cMode = pArg->mode; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3875 | if( pArg->autoExplain ){ |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3876 | if( sqlite3_stmt_isexplain(pStmt)==1 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3877 | pArg->cMode = MODE_Explain; |
| 3878 | } |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3879 | if( sqlite3_stmt_isexplain(pStmt)==2 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3880 | pArg->cMode = MODE_EQP; |
| 3881 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | /* If the shell is currently in ".explain" mode, gather the extra |
| 3885 | ** data required to add indents to the output.*/ |
| 3886 | if( pArg->cMode==MODE_Explain ){ |
| 3887 | explain_data_prepare(pArg, pStmt); |
| 3888 | } |
| 3889 | } |
| 3890 | |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3891 | bind_prepared_stmt(pArg, pStmt); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3892 | exec_prepared_stmt(pArg, pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3893 | explain_data_delete(pArg); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3894 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3895 | |
| 3896 | /* print usage stats if stats on */ |
| 3897 | if( pArg && pArg->statsOn ){ |
| 3898 | display_stats(db, pArg, 0); |
| 3899 | } |
| 3900 | |
| 3901 | /* print loop-counters if required */ |
| 3902 | if( pArg && pArg->scanstatsOn ){ |
| 3903 | display_scanstats(db, pArg); |
| 3904 | } |
| 3905 | |
| 3906 | /* Finalize the statement just executed. If this fails, save a |
| 3907 | ** copy of the error message. Otherwise, set zSql to point to the |
| 3908 | ** next statement to execute. */ |
| 3909 | rc2 = sqlite3_finalize(pStmt); |
| 3910 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
| 3911 | if( rc==SQLITE_OK ){ |
| 3912 | zSql = zLeftover; |
| 3913 | while( IsSpace(zSql[0]) ) zSql++; |
| 3914 | }else if( pzErrMsg ){ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 3915 | *pzErrMsg = save_err_msg(db, "stepping", rc, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | /* clear saved stmt handle */ |
| 3919 | if( pArg ){ |
| 3920 | pArg->pStmt = NULL; |
| 3921 | } |
| 3922 | } |
| 3923 | } /* end while */ |
| 3924 | |
| 3925 | return rc; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
| 3929 | ** Release memory previously allocated by tableColumnList(). |
| 3930 | */ |
| 3931 | static void freeColumnList(char **azCol){ |
| 3932 | int i; |
| 3933 | for(i=1; azCol[i]; i++){ |
| 3934 | sqlite3_free(azCol[i]); |
| 3935 | } |
| 3936 | /* azCol[0] is a static string */ |
| 3937 | sqlite3_free(azCol); |
| 3938 | } |
| 3939 | |
| 3940 | /* |
| 3941 | ** Return a list of pointers to strings which are the names of all |
| 3942 | ** columns in table zTab. The memory to hold the names is dynamically |
| 3943 | ** allocated and must be released by the caller using a subsequent call |
| 3944 | ** to freeColumnList(). |
| 3945 | ** |
| 3946 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 3947 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 3948 | ** name of the rowid column. |
| 3949 | ** |
| 3950 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 3951 | ** by an entry with azCol[i]==0. |
| 3952 | */ |
| 3953 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 3954 | char **azCol = 0; |
| 3955 | sqlite3_stmt *pStmt; |
| 3956 | char *zSql; |
| 3957 | int nCol = 0; |
| 3958 | int nAlloc = 0; |
| 3959 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 3960 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 3961 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 3962 | int rc; |
| 3963 | |
| 3964 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3965 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3966 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3967 | sqlite3_free(zSql); |
| 3968 | if( rc ) return 0; |
| 3969 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3970 | if( nCol>=nAlloc-2 ){ |
| 3971 | nAlloc = nAlloc*2 + nCol + 10; |
| 3972 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3973 | shell_check_oom(azCol); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3974 | } |
| 3975 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3976 | shell_check_oom(azCol[nCol]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3977 | if( sqlite3_column_int(pStmt, 5) ){ |
| 3978 | nPK++; |
| 3979 | if( nPK==1 |
| 3980 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 3981 | "INTEGER")==0 |
| 3982 | ){ |
| 3983 | isIPK = 1; |
| 3984 | }else{ |
| 3985 | isIPK = 0; |
| 3986 | } |
| 3987 | } |
| 3988 | } |
| 3989 | sqlite3_finalize(pStmt); |
drh | 4c6cddc | 2017-10-12 10:28:30 +0000 | [diff] [blame] | 3990 | if( azCol==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3991 | azCol[0] = 0; |
| 3992 | azCol[nCol+1] = 0; |
| 3993 | |
| 3994 | /* The decision of whether or not a rowid really needs to be preserved |
| 3995 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3996 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3997 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3998 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3999 | */ |
| 4000 | if( preserveRowid && isIPK ){ |
| 4001 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 4002 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 4003 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 4004 | ** ROWID aliases. To distinguish these cases, check to see if |
| 4005 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 4006 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 4007 | */ |
| 4008 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 4009 | " WHERE origin='pk'", zTab); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4010 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4011 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4012 | sqlite3_free(zSql); |
| 4013 | if( rc ){ |
| 4014 | freeColumnList(azCol); |
| 4015 | return 0; |
| 4016 | } |
| 4017 | rc = sqlite3_step(pStmt); |
| 4018 | sqlite3_finalize(pStmt); |
| 4019 | preserveRowid = rc==SQLITE_ROW; |
| 4020 | } |
| 4021 | if( preserveRowid ){ |
| 4022 | /* Only preserve the rowid if we can find a name to use for the |
| 4023 | ** rowid */ |
| 4024 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 4025 | int i, j; |
| 4026 | for(j=0; j<3; j++){ |
| 4027 | for(i=1; i<=nCol; i++){ |
| 4028 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 4029 | } |
| 4030 | if( i>nCol ){ |
| 4031 | /* At this point, we know that azRowid[j] is not the name of any |
| 4032 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 4033 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 4034 | ** tables will fail this last check */ |
| 4035 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 4036 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 4037 | break; |
| 4038 | } |
| 4039 | } |
| 4040 | } |
| 4041 | return azCol; |
| 4042 | } |
| 4043 | |
| 4044 | /* |
| 4045 | ** Toggle the reverse_unordered_selects setting. |
| 4046 | */ |
| 4047 | static void toggleSelectOrder(sqlite3 *db){ |
| 4048 | sqlite3_stmt *pStmt = 0; |
| 4049 | int iSetting = 0; |
| 4050 | char zStmt[100]; |
| 4051 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 4052 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4053 | iSetting = sqlite3_column_int(pStmt, 0); |
| 4054 | } |
| 4055 | sqlite3_finalize(pStmt); |
| 4056 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 4057 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 4058 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 4059 | } |
| 4060 | |
| 4061 | /* |
| 4062 | ** This is a different callback routine used for dumping the database. |
| 4063 | ** Each row received by this callback consists of a table name, |
| 4064 | ** the table type ("index" or "table") and SQL to create the table. |
| 4065 | ** This routine should print text sufficient to recreate the table. |
| 4066 | */ |
| 4067 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
| 4068 | int rc; |
| 4069 | const char *zTable; |
| 4070 | const char *zType; |
| 4071 | const char *zSql; |
| 4072 | ShellState *p = (ShellState *)pArg; |
mistachkin | a00a016 | 2020-10-18 18:35:34 +0000 | [diff] [blame] | 4073 | int dataOnly; |
| 4074 | int noSys; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4075 | |
| 4076 | UNUSED_PARAMETER(azNotUsed); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 4077 | if( nArg!=3 || azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4078 | zTable = azArg[0]; |
| 4079 | zType = azArg[1]; |
| 4080 | zSql = azArg[2]; |
mistachkin | a00a016 | 2020-10-18 18:35:34 +0000 | [diff] [blame] | 4081 | dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; |
| 4082 | noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4083 | |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4084 | if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){ |
| 4085 | if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
| 4086 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){ |
| 4087 | if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4088 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 4089 | return 0; |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4090 | }else if( dataOnly ){ |
| 4091 | /* no-op */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4092 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 4093 | char *zIns; |
| 4094 | if( !p->writableSchema ){ |
| 4095 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 4096 | p->writableSchema = 1; |
| 4097 | } |
| 4098 | zIns = sqlite3_mprintf( |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4099 | "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4100 | "VALUES('table','%q','%q',0,'%q');", |
| 4101 | zTable, zTable, zSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4102 | shell_check_oom(zIns); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4103 | utf8_printf(p->out, "%s\n", zIns); |
| 4104 | sqlite3_free(zIns); |
| 4105 | return 0; |
| 4106 | }else{ |
| 4107 | printSchemaLine(p->out, zSql, ";\n"); |
| 4108 | } |
| 4109 | |
| 4110 | if( strcmp(zType, "table")==0 ){ |
| 4111 | ShellText sSelect; |
| 4112 | ShellText sTable; |
| 4113 | char **azCol; |
| 4114 | int i; |
| 4115 | char *savedDestTable; |
| 4116 | int savedMode; |
| 4117 | |
| 4118 | azCol = tableColumnList(p, zTable); |
| 4119 | if( azCol==0 ){ |
| 4120 | p->nErr++; |
| 4121 | return 0; |
| 4122 | } |
| 4123 | |
| 4124 | /* Always quote the table name, even if it appears to be pure ascii, |
| 4125 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 4126 | initText(&sTable); |
| 4127 | appendText(&sTable, zTable, quoteChar(zTable)); |
| 4128 | /* If preserving the rowid, add a column list after the table name. |
| 4129 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 4130 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 4131 | */ |
| 4132 | if( azCol[0] ){ |
| 4133 | appendText(&sTable, "(", 0); |
| 4134 | appendText(&sTable, azCol[0], 0); |
| 4135 | for(i=1; azCol[i]; i++){ |
| 4136 | appendText(&sTable, ",", 0); |
| 4137 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
| 4138 | } |
| 4139 | appendText(&sTable, ")", 0); |
| 4140 | } |
| 4141 | |
| 4142 | /* Build an appropriate SELECT statement */ |
| 4143 | initText(&sSelect); |
| 4144 | appendText(&sSelect, "SELECT ", 0); |
| 4145 | if( azCol[0] ){ |
| 4146 | appendText(&sSelect, azCol[0], 0); |
| 4147 | appendText(&sSelect, ",", 0); |
| 4148 | } |
| 4149 | for(i=1; azCol[i]; i++){ |
| 4150 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
| 4151 | if( azCol[i+1] ){ |
| 4152 | appendText(&sSelect, ",", 0); |
| 4153 | } |
| 4154 | } |
| 4155 | freeColumnList(azCol); |
| 4156 | appendText(&sSelect, " FROM ", 0); |
| 4157 | appendText(&sSelect, zTable, quoteChar(zTable)); |
| 4158 | |
| 4159 | savedDestTable = p->zDestTable; |
| 4160 | savedMode = p->mode; |
| 4161 | p->zDestTable = sTable.z; |
| 4162 | p->mode = p->cMode = MODE_Insert; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 4163 | rc = shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4164 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 4165 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 4166 | toggleSelectOrder(p->db); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 4167 | shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4168 | toggleSelectOrder(p->db); |
| 4169 | } |
| 4170 | p->zDestTable = savedDestTable; |
| 4171 | p->mode = savedMode; |
| 4172 | freeText(&sTable); |
| 4173 | freeText(&sSelect); |
| 4174 | if( rc ) p->nErr++; |
| 4175 | } |
| 4176 | return 0; |
| 4177 | } |
| 4178 | |
| 4179 | /* |
| 4180 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 4181 | ** the contents of the query are output as SQL statements. |
| 4182 | ** |
| 4183 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 4184 | ** "ORDER BY rowid DESC" to the end. |
| 4185 | */ |
| 4186 | static int run_schema_dump_query( |
| 4187 | ShellState *p, |
| 4188 | const char *zQuery |
| 4189 | ){ |
| 4190 | int rc; |
| 4191 | char *zErr = 0; |
| 4192 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
| 4193 | if( rc==SQLITE_CORRUPT ){ |
| 4194 | char *zQ2; |
| 4195 | int len = strlen30(zQuery); |
| 4196 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 4197 | if( zErr ){ |
| 4198 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
| 4199 | sqlite3_free(zErr); |
| 4200 | zErr = 0; |
| 4201 | } |
| 4202 | zQ2 = malloc( len+100 ); |
| 4203 | if( zQ2==0 ) return rc; |
| 4204 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
| 4205 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 4206 | if( rc ){ |
| 4207 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
| 4208 | }else{ |
| 4209 | rc = SQLITE_CORRUPT; |
| 4210 | } |
| 4211 | sqlite3_free(zErr); |
| 4212 | free(zQ2); |
| 4213 | } |
| 4214 | return rc; |
| 4215 | } |
| 4216 | |
| 4217 | /* |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4218 | ** Text of help messages. |
| 4219 | ** |
| 4220 | ** The help text for each individual command begins with a line that starts |
| 4221 | ** with ".". Subsequent lines are supplimental information. |
| 4222 | ** |
| 4223 | ** There must be two or more spaces between the end of the command and the |
| 4224 | ** start of the description of what that command does. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4225 | */ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4226 | static const char *(azHelp[]) = { |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4227 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4228 | ".archive ... Manage SQL archives", |
| 4229 | " Each command must have exactly one of the following options:", |
| 4230 | " -c, --create Create a new archive", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4231 | " -u, --update Add or update files with changed mtime", |
| 4232 | " -i, --insert Like -u but always add even if unchanged", |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 4233 | " -r, --remove Remove files from archive", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4234 | " -t, --list List contents of archive", |
| 4235 | " -x, --extract Extract files from archive", |
| 4236 | " Optional arguments:", |
| 4237 | " -v, --verbose Print each filename as it is processed", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4238 | " -f FILE, --file FILE Use archive FILE (default is current db)", |
| 4239 | " -a FILE, --append FILE Open FILE using the apndvfs VFS", |
| 4240 | " -C DIR, --directory DIR Read/extract files from directory DIR", |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 4241 | " -g, --glob Use glob matching for names in archive", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4242 | " -n, --dryrun Show the SQL that would have occurred", |
| 4243 | " Examples:", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4244 | " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", |
| 4245 | " .ar -tf ARCHIVE # List members of ARCHIVE", |
| 4246 | " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4247 | " See also:", |
larrybr | bd0d62c | 2021-06-13 08:23:28 +0000 | [diff] [blame] | 4248 | " http://sqlite.org/cli.html#sqlite_archive_support", |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4249 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4250 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4251 | ".auth ON|OFF Show authorizer callbacks", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4252 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4253 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE", |
larrybr | a7919ad | 2022-02-19 21:25:48 +0000 | [diff] [blame] | 4254 | " Options:", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4255 | " --append Use the appendvfs", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4256 | " --async Write to FILE without journal and fsync()", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4257 | ".bail on|off Stop after hitting an error. Default OFF", |
| 4258 | ".binary on|off Turn binary output on or off. Default OFF", |
| 4259 | ".cd DIRECTORY Change the working directory to DIRECTORY", |
| 4260 | ".changes on|off Show number of rows changed by SQL", |
| 4261 | ".check GLOB Fail if output since .testcase does not match", |
| 4262 | ".clone NEWDB Clone data into NEWDB from the existing database", |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4263 | ".connection [close] [#] Open or close an auxiliary database connection", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4264 | ".databases List names and files of attached databases", |
| 4265 | ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", |
| 4266 | ".dbinfo ?DB? Show status information about the database", |
larrybr | 7bdbe59 | 2021-03-15 12:56:00 +0000 | [diff] [blame] | 4267 | ".dump ?OBJECTS? Render database content as SQL", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4268 | " Options:", |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4269 | " --data-only Output only INSERT statements", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4270 | " --newlines Allow unescaped newline characters in output", |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4271 | " --nosys Omit system tables (ex: \"sqlite_stat1\")", |
| 4272 | " --preserve-rowids Include ROWID values in the output", |
larrybr | 7bdbe59 | 2021-03-15 12:56:00 +0000 | [diff] [blame] | 4273 | " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump", |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 4274 | " Additional LIKE patterns can be given in subsequent arguments", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4275 | ".echo on|off Turn command echo on or off", |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 4276 | ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", |
| 4277 | " Other Modes:", |
| 4278 | #ifdef SQLITE_DEBUG |
| 4279 | " test Show raw EXPLAIN QUERY PLAN output", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4280 | " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 4281 | #endif |
| 4282 | " trigger Like \"full\" but also show trigger bytecode", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4283 | ".excel Display the output of next command in spreadsheet", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4284 | " --bom Put a UTF8 byte-order mark on intermediate file", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4285 | ".exit ?CODE? Exit this program with return-code CODE", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4286 | ".expert EXPERIMENTAL. Suggest indexes for queries", |
drh | 978256f | 2019-11-02 00:00:14 +0000 | [diff] [blame] | 4287 | ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 4288 | ".filectrl CMD ... Run various sqlite3_file_control() operations", |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 4289 | " --schema SCHEMA Use SCHEMA instead of \"main\"", |
| 4290 | " --help Show CMD details", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4291 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", |
| 4292 | ".headers on|off Turn display of headers on or off", |
| 4293 | ".help ?-all? ?PATTERN? Show help text for PATTERN", |
| 4294 | ".import FILE TABLE Import data from FILE into TABLE", |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 4295 | " Options:", |
| 4296 | " --ascii Use \\037 and \\036 as column and row separators", |
| 4297 | " --csv Use , and \\n as column and row separators", |
| 4298 | " --skip N Skip the first N rows of input", |
larrybr | 738d7b9 | 2022-01-13 21:22:54 +0000 | [diff] [blame] | 4299 | " --schema S Target table to be S.TABLE", |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 4300 | " -v \"Verbose\" - increase auxiliary output", |
| 4301 | " Notes:", |
| 4302 | " * If TABLE does not exist, it is created. The first row of input", |
| 4303 | " determines the column names.", |
| 4304 | " * If neither --csv or --ascii are used, the input mode is derived", |
| 4305 | " from the \".mode\" output mode", |
| 4306 | " * If FILE begins with \"|\" then it is a command that generates the", |
| 4307 | " input text.", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4308 | #ifndef SQLITE_OMIT_TEST_CONTROL |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4309 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4310 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4311 | ".indexes ?TABLE? Show names of indexes", |
| 4312 | " If TABLE is specified, only show indexes for", |
| 4313 | " tables matching TABLE using the LIKE operator.", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4314 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4315 | ".iotrace FILE Enable I/O diagnostic logging to FILE", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4316 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4317 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", |
| 4318 | ".lint OPTIONS Report potential schema issues.", |
| 4319 | " Options:", |
| 4320 | " fkey-indexes Find missing foreign key indexes", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4321 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4322 | ".load FILE ?ENTRY? Load an extension library", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4323 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4324 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 4325 | ".mode MODE ?OPTIONS? Set output mode", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4326 | " MODE is one of:", |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 4327 | " ascii Columns/rows delimited by 0x1F and 0x1E", |
| 4328 | " box Tables using unicode box-drawing characters", |
| 4329 | " csv Comma-separated values", |
| 4330 | " column Output in columns. (See .width)", |
| 4331 | " html HTML <table> code", |
| 4332 | " insert SQL insert statements for TABLE", |
| 4333 | " json Results in a JSON array", |
| 4334 | " line One value per line", |
| 4335 | " list Values delimited by \"|\"", |
| 4336 | " markdown Markdown table format", |
| 4337 | " qbox Shorthand for \"box --width 60 --quote\"", |
| 4338 | " quote Escape answers as for SQL", |
| 4339 | " table ASCII-art table", |
| 4340 | " tabs Tab-separated values", |
| 4341 | " tcl TCL list elements", |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 4342 | " OPTIONS: (for columnar modes or insert mode):", |
| 4343 | " --wrap N Wrap output lines to no longer than N characters", |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 4344 | " --wordwrap B Wrap or not at word boundaries per B (on/off)", |
| 4345 | " --ww Shorthand for \"--wordwrap 1\"", |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 4346 | " --quote Quote output text as SQL literals", |
| 4347 | " --noquote Do not quote output text", |
| 4348 | " TABLE The name of SQL table used for \"insert\" mode", |
| 4349 | ".nonce STRING Suspend safe mode for one command if nonce matches", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4350 | ".nullvalue STRING Use STRING in place of NULL values", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4351 | ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4352 | " If FILE begins with '|' then open as a pipe", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4353 | " --bom Put a UTF8 byte-order mark at the beginning", |
| 4354 | " -e Send output to the system text editor", |
| 4355 | " -x Send output as CSV to a spreadsheet (same as \".excel\")", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4356 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", |
| 4357 | " Options:", |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4358 | " --append Use appendvfs to append database to the end of FILE", |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4359 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | d10c3ca | 2021-05-08 11:57:35 +0000 | [diff] [blame] | 4360 | " --deserialize Load into memory using sqlite3_deserialize()", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4361 | " --hexdb Load the output of \"dbtotxt\" as an in-memory db", |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 4362 | " --maxsize N Maximum size for --hexdb or --deserialized database", |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 4363 | #endif |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4364 | " --new Initialize FILE to an empty database", |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 4365 | " --nofollow Do not follow symbolic links", |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4366 | " --readonly Open FILE readonly", |
| 4367 | " --zip FILE is a ZIP archive", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4368 | ".output ?FILE? Send output to FILE or stdout if FILE is omitted", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4369 | " If FILE begins with '|' then open it as a pipe.", |
| 4370 | " Options:", |
| 4371 | " --bom Prefix output with a UTF8 byte-order mark", |
| 4372 | " -e Send output to the system text editor", |
| 4373 | " -x Send output as CSV to a spreadsheet", |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 4374 | ".parameter CMD ... Manage SQL parameter bindings", |
| 4375 | " clear Erase all bindings", |
| 4376 | " init Initialize the TEMP table that holds bindings", |
| 4377 | " list List the current parameter bindings", |
| 4378 | " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4379 | " PARAMETER should start with one of: $ : @ ?", |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 4380 | " unset PARAMETER Remove PARAMETER from the binding table", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4381 | ".print STRING... Print literal STRING", |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 4382 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 4383 | ".progress N Invoke progress handler after every N opcodes", |
| 4384 | " --limit N Interrupt after N progress callbacks", |
| 4385 | " --once Do no more than one progress interrupt", |
| 4386 | " --quiet|-q No output except at interrupts", |
| 4387 | " --reset Reset the count for each input and interrupt", |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 4388 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4389 | ".prompt MAIN CONTINUE Replace the standard prompts", |
| 4390 | ".quit Exit this program", |
larrybr | a2ba25b | 2021-12-28 05:08:38 +0000 | [diff] [blame] | 4391 | ".read FILE Read input from FILE or command output", |
| 4392 | " If FILE begins with \"|\", it is a command that generates the input.", |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 4393 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 4394 | ".recover Recover as much data as possible from corrupt db.", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4395 | " --freelist-corrupt Assume the freelist is corrupt", |
| 4396 | " --recovery-db NAME Store recovery metadata in database file NAME", |
| 4397 | " --lost-and-found TABLE Alternative name for the lost-and-found table", |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 4398 | " --no-rowids Do not attempt to recover rowid values", |
| 4399 | " that are not also INTEGER PRIMARY KEYs", |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 4400 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4401 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", |
larrybr | a7919ad | 2022-02-19 21:25:48 +0000 | [diff] [blame] | 4402 | ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4403 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", |
| 4404 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN", |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 4405 | " Options:", |
| 4406 | " --indent Try to pretty-print the schema", |
| 4407 | " --nosys Omit objects whose names start with \"sqlite_\"", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4408 | ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", |
| 4409 | " Options:", |
| 4410 | " --init Create a new SELFTEST table", |
| 4411 | " -v Verbose output", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4412 | ".separator COL ?ROW? Change the column and row separators", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4413 | #if defined(SQLITE_ENABLE_SESSION) |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4414 | ".session ?NAME? CMD ... Create or control sessions", |
| 4415 | " Subcommands:", |
| 4416 | " attach TABLE Attach TABLE", |
| 4417 | " changeset FILE Write a changeset into FILE", |
| 4418 | " close Close one session", |
| 4419 | " enable ?BOOLEAN? Set or query the enable bit", |
| 4420 | " filter GLOB... Reject tables matching GLOBs", |
| 4421 | " indirect ?BOOLEAN? Mark or query the indirect status", |
| 4422 | " isempty Query whether the session is empty", |
| 4423 | " list List currently open session names", |
| 4424 | " open DB NAME Open a new session on DB", |
| 4425 | " patchset FILE Write a patchset into FILE", |
| 4426 | " If ?NAME? is omitted, the first defined session is used.", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4427 | #endif |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4428 | ".sha3sum ... Compute a SHA3 hash of database content", |
| 4429 | " Options:", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4430 | " --schema Also hash the sqlite_schema table", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4431 | " --sha3-224 Use the sha3-224 algorithm", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4432 | " --sha3-256 Use the sha3-256 algorithm (default)", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4433 | " --sha3-384 Use the sha3-384 algorithm", |
| 4434 | " --sha3-512 Use the sha3-512 algorithm", |
| 4435 | " Any other argument is a LIKE pattern for tables to hash", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4436 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4437 | ".shell CMD ARGS... Run CMD ARGS... in a system shell", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4438 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4439 | ".show Show the current values for various settings", |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 4440 | ".stats ?ARG? Show stats or turn stats on or off", |
| 4441 | " off Turn off automatic stat display", |
| 4442 | " on Turn on automatic stat display", |
| 4443 | " stmt Show statement stats", |
| 4444 | " vmstep Show the virtual machine step count only", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4445 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4446 | ".system CMD ARGS... Run CMD ARGS... in a system shell", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4447 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4448 | ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", |
| 4449 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'", |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 4450 | ".testctrl CMD ... Run various sqlite3_test_control() operations", |
| 4451 | " Run \".testctrl\" with no arguments for details", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4452 | ".timeout MS Try opening locked tables for MS milliseconds", |
| 4453 | ".timer on|off Turn SQL timer on or off", |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 4454 | #ifndef SQLITE_OMIT_TRACE |
| 4455 | ".trace ?OPTIONS? Output each SQL statement as it is run", |
| 4456 | " FILE Send output to FILE", |
| 4457 | " stdout Send output to stdout", |
| 4458 | " stderr Send output to stderr", |
| 4459 | " off Disable tracing", |
| 4460 | " --expanded Expand query parameters", |
| 4461 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 4462 | " --normalized Normal the SQL statements", |
| 4463 | #endif |
| 4464 | " --plain Show SQL as it is input", |
| 4465 | " --stmt Trace statement execution (SQLITE_TRACE_STMT)", |
| 4466 | " --profile Profile statements (SQLITE_TRACE_PROFILE)", |
| 4467 | " --row Trace each row (SQLITE_TRACE_ROW)", |
| 4468 | " --close Trace connection close (SQLITE_TRACE_CLOSE)", |
| 4469 | #endif /* SQLITE_OMIT_TRACE */ |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 4470 | #ifdef SQLITE_DEBUG |
| 4471 | ".unmodule NAME ... Unregister virtual table modules", |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 4472 | " --allexcept Unregister everything except those named", |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 4473 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4474 | ".vfsinfo ?AUX? Information about the top-level VFS", |
| 4475 | ".vfslist List all available VFSes", |
| 4476 | ".vfsname ?AUX? Print the name of the VFS stack", |
drh | 7da29a3 | 2020-05-29 19:17:20 +0000 | [diff] [blame] | 4477 | ".width NUM1 NUM2 ... Set minimum column widths for columnar output", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4478 | " Negative values right-justify", |
| 4479 | }; |
| 4480 | |
| 4481 | /* |
| 4482 | ** Output help text. |
| 4483 | ** |
| 4484 | ** zPattern describes the set of commands for which help text is provided. |
| 4485 | ** If zPattern is NULL, then show all commands, but only give a one-line |
| 4486 | ** description of each. |
| 4487 | ** |
| 4488 | ** Return the number of matches. |
| 4489 | */ |
| 4490 | static int showHelp(FILE *out, const char *zPattern){ |
drh | e93f826 | 2018-10-11 16:53:37 +0000 | [diff] [blame] | 4491 | int i = 0; |
| 4492 | int j = 0; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4493 | int n = 0; |
| 4494 | char *zPat; |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4495 | if( zPattern==0 |
| 4496 | || zPattern[0]=='0' |
| 4497 | || strcmp(zPattern,"-a")==0 |
| 4498 | || strcmp(zPattern,"-all")==0 |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4499 | || strcmp(zPattern,"--all")==0 |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4500 | ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4501 | /* Show all commands, but only one line per command */ |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4502 | if( zPattern==0 ) zPattern = ""; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4503 | for(i=0; i<ArraySize(azHelp); i++){ |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4504 | if( azHelp[i][0]=='.' || zPattern[0] ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4505 | utf8_printf(out, "%s\n", azHelp[i]); |
| 4506 | n++; |
| 4507 | } |
| 4508 | } |
| 4509 | }else{ |
| 4510 | /* Look for commands that for which zPattern is an exact prefix */ |
| 4511 | zPat = sqlite3_mprintf(".%s*", zPattern); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4512 | shell_check_oom(zPat); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4513 | for(i=0; i<ArraySize(azHelp); i++){ |
| 4514 | if( sqlite3_strglob(zPat, azHelp[i])==0 ){ |
| 4515 | utf8_printf(out, "%s\n", azHelp[i]); |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4516 | j = i+1; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4517 | n++; |
| 4518 | } |
| 4519 | } |
| 4520 | sqlite3_free(zPat); |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4521 | if( n ){ |
| 4522 | if( n==1 ){ |
| 4523 | /* when zPattern is a prefix of exactly one command, then include the |
| 4524 | ** details of that command, which should begin at offset j */ |
| 4525 | while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ |
| 4526 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4527 | j++; |
| 4528 | } |
| 4529 | } |
| 4530 | return n; |
| 4531 | } |
| 4532 | /* Look for commands that contain zPattern anywhere. Show the complete |
| 4533 | ** text of all commands that match. */ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4534 | zPat = sqlite3_mprintf("%%%s%%", zPattern); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4535 | shell_check_oom(zPat); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4536 | for(i=0; i<ArraySize(azHelp); i++){ |
| 4537 | if( azHelp[i][0]=='.' ) j = i; |
| 4538 | if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ |
| 4539 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4540 | while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ |
| 4541 | j++; |
| 4542 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4543 | } |
| 4544 | i = j; |
| 4545 | n++; |
| 4546 | } |
| 4547 | } |
| 4548 | sqlite3_free(zPat); |
| 4549 | } |
| 4550 | return n; |
| 4551 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4552 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4553 | /* Forward reference */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4554 | static int process_input(ShellState *p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4555 | |
| 4556 | /* |
| 4557 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 4558 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 4559 | ** the memory. |
| 4560 | ** |
| 4561 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 4562 | ** read. |
| 4563 | ** |
| 4564 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 4565 | ** from the file before the buffer is returned. This byte is not included in |
| 4566 | ** the final value of (*pnByte), if applicable. |
| 4567 | ** |
| 4568 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 4569 | ** is undefined in this case. |
| 4570 | */ |
| 4571 | static char *readFile(const char *zName, int *pnByte){ |
| 4572 | FILE *in = fopen(zName, "rb"); |
| 4573 | long nIn; |
| 4574 | size_t nRead; |
| 4575 | char *pBuf; |
| 4576 | if( in==0 ) return 0; |
| 4577 | fseek(in, 0, SEEK_END); |
| 4578 | nIn = ftell(in); |
| 4579 | rewind(in); |
| 4580 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 1dbb147 | 2018-10-11 10:37:24 +0000 | [diff] [blame] | 4581 | if( pBuf==0 ){ fclose(in); return 0; } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4582 | nRead = fread(pBuf, nIn, 1, in); |
| 4583 | fclose(in); |
| 4584 | if( nRead!=1 ){ |
| 4585 | sqlite3_free(pBuf); |
| 4586 | return 0; |
| 4587 | } |
| 4588 | pBuf[nIn] = 0; |
| 4589 | if( pnByte ) *pnByte = nIn; |
| 4590 | return pBuf; |
| 4591 | } |
| 4592 | |
| 4593 | #if defined(SQLITE_ENABLE_SESSION) |
| 4594 | /* |
| 4595 | ** Close a single OpenSession object and release all of its associated |
| 4596 | ** resources. |
| 4597 | */ |
| 4598 | static void session_close(OpenSession *pSession){ |
| 4599 | int i; |
| 4600 | sqlite3session_delete(pSession->p); |
| 4601 | sqlite3_free(pSession->zName); |
| 4602 | for(i=0; i<pSession->nFilter; i++){ |
| 4603 | sqlite3_free(pSession->azFilter[i]); |
| 4604 | } |
| 4605 | sqlite3_free(pSession->azFilter); |
| 4606 | memset(pSession, 0, sizeof(OpenSession)); |
| 4607 | } |
| 4608 | #endif |
| 4609 | |
| 4610 | /* |
| 4611 | ** Close all OpenSession objects and release all associated resources. |
| 4612 | */ |
| 4613 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4614 | static void session_close_all(ShellState *p, int i){ |
| 4615 | int j; |
| 4616 | struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; |
| 4617 | for(j=0; j<pAuxDb->nSession; j++){ |
| 4618 | session_close(&pAuxDb->aSession[j]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4619 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4620 | pAuxDb->nSession = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4621 | } |
| 4622 | #else |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4623 | # define session_close_all(X,Y) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4624 | #endif |
| 4625 | |
| 4626 | /* |
| 4627 | ** Implementation of the xFilter function for an open session. Omit |
| 4628 | ** any tables named by ".session filter" but let all other table through. |
| 4629 | */ |
| 4630 | #if defined(SQLITE_ENABLE_SESSION) |
| 4631 | static int session_filter(void *pCtx, const char *zTab){ |
| 4632 | OpenSession *pSession = (OpenSession*)pCtx; |
| 4633 | int i; |
| 4634 | for(i=0; i<pSession->nFilter; i++){ |
| 4635 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 4636 | } |
| 4637 | return 1; |
| 4638 | } |
| 4639 | #endif |
| 4640 | |
| 4641 | /* |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4642 | ** Try to deduce the type of file for zName based on its content. Return |
| 4643 | ** one of the SHELL_OPEN_* constants. |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4644 | ** |
| 4645 | ** If the file does not exist or is empty but its name looks like a ZIP |
| 4646 | ** archive and the dfltZip flag is true, then assume it is a ZIP archive. |
| 4647 | ** Otherwise, assume an ordinary database regardless of the filename if |
| 4648 | ** the type cannot be determined from content. |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4649 | */ |
drh | fc97c1c | 2018-05-14 00:41:12 +0000 | [diff] [blame] | 4650 | int deduceDatabaseType(const char *zName, int dfltZip){ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4651 | FILE *f = fopen(zName, "rb"); |
| 4652 | size_t n; |
| 4653 | int rc = SHELL_OPEN_UNSPEC; |
| 4654 | char zBuf[100]; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4655 | if( f==0 ){ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4656 | if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
| 4657 | return SHELL_OPEN_ZIPFILE; |
| 4658 | }else{ |
| 4659 | return SHELL_OPEN_NORMAL; |
| 4660 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4661 | } |
drh | 2b3c4af | 2018-10-30 14:36:21 +0000 | [diff] [blame] | 4662 | n = fread(zBuf, 16, 1, f); |
| 4663 | if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ |
| 4664 | fclose(f); |
| 4665 | return SHELL_OPEN_NORMAL; |
| 4666 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4667 | fseek(f, -25, SEEK_END); |
| 4668 | n = fread(zBuf, 25, 1, f); |
| 4669 | if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
| 4670 | rc = SHELL_OPEN_APPENDVFS; |
| 4671 | }else{ |
| 4672 | fseek(f, -22, SEEK_END); |
| 4673 | n = fread(zBuf, 22, 1, f); |
| 4674 | if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
| 4675 | && zBuf[3]==0x06 ){ |
| 4676 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4677 | }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
mistachkin | a3926f4 | 2018-05-14 12:23:04 +0000 | [diff] [blame] | 4678 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | fclose(f); |
| 4682 | return rc; |
| 4683 | } |
| 4684 | |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4685 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4686 | /* |
| 4687 | ** Reconstruct an in-memory database using the output from the "dbtotxt" |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4688 | ** program. Read content from the file in p->aAuxDb[].zDbFilename. |
| 4689 | ** If p->aAuxDb[].zDbFilename is 0, then read from standard input. |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4690 | */ |
| 4691 | static unsigned char *readHexDb(ShellState *p, int *pnData){ |
| 4692 | unsigned char *a = 0; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4693 | int nLine; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4694 | int n = 0; |
| 4695 | int pgsz = 0; |
| 4696 | int iOffset = 0; |
| 4697 | int j, k; |
| 4698 | int rc; |
| 4699 | FILE *in; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4700 | const char *zDbFilename = p->pAuxDb->zDbFilename; |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4701 | unsigned int x[16]; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4702 | char zLine[1000]; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4703 | if( zDbFilename ){ |
| 4704 | in = fopen(zDbFilename, "r"); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4705 | if( in==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4706 | utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4707 | return 0; |
| 4708 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4709 | nLine = 0; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4710 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4711 | in = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4712 | nLine = p->lineno; |
drh | 5bf4644 | 2019-05-03 02:41:36 +0000 | [diff] [blame] | 4713 | if( in==0 ) in = stdin; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4714 | } |
| 4715 | *pnData = 0; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4716 | nLine++; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4717 | if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; |
| 4718 | rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); |
| 4719 | if( rc!=2 ) goto readHexDb_error; |
drh | 68feae5 | 2019-05-09 11:18:41 +0000 | [diff] [blame] | 4720 | if( n<0 ) goto readHexDb_error; |
drh | 09ea125 | 2019-07-17 15:05:16 +0000 | [diff] [blame] | 4721 | if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; |
| 4722 | n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ |
drh | 68feae5 | 2019-05-09 11:18:41 +0000 | [diff] [blame] | 4723 | a = sqlite3_malloc( n ? n : 1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4724 | shell_check_oom(a); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4725 | memset(a, 0, n); |
| 4726 | if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ |
| 4727 | utf8_printf(stderr, "invalid pagesize\n"); |
| 4728 | goto readHexDb_error; |
| 4729 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4730 | for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4731 | rc = sscanf(zLine, "| page %d offset %d", &j, &k); |
| 4732 | if( rc==2 ){ |
| 4733 | iOffset = k; |
| 4734 | continue; |
| 4735 | } |
| 4736 | if( strncmp(zLine, "| end ", 6)==0 ){ |
| 4737 | break; |
| 4738 | } |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4739 | rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4740 | &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], |
| 4741 | &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); |
| 4742 | if( rc==17 ){ |
| 4743 | k = iOffset+j; |
drh | 82978ac | 2021-10-01 17:06:44 +0000 | [diff] [blame] | 4744 | if( k+16<=n && k>=0 ){ |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4745 | int ii; |
| 4746 | for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4747 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4748 | } |
| 4749 | } |
| 4750 | *pnData = n; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4751 | if( in!=p->in ){ |
| 4752 | fclose(in); |
| 4753 | }else{ |
| 4754 | p->lineno = nLine; |
| 4755 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4756 | return a; |
| 4757 | |
| 4758 | readHexDb_error: |
drh | 68feae5 | 2019-05-09 11:18:41 +0000 | [diff] [blame] | 4759 | if( in!=p->in ){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4760 | fclose(in); |
| 4761 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4762 | while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4763 | nLine++; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4764 | if(strncmp(zLine, "| end ", 6)==0 ) break; |
| 4765 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4766 | p->lineno = nLine; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4767 | } |
| 4768 | sqlite3_free(a); |
| 4769 | utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); |
| 4770 | return 0; |
| 4771 | } |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4772 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4773 | |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4774 | /* |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 4775 | ** Scalar function "shell_int32". The first argument to this function |
| 4776 | ** must be a blob. The second a non-negative integer. This function |
| 4777 | ** reads and returns a 32-bit big-endian integer from byte |
| 4778 | ** offset (4*<arg2>) of the blob. |
| 4779 | */ |
| 4780 | static void shellInt32( |
| 4781 | sqlite3_context *context, |
| 4782 | int argc, |
| 4783 | sqlite3_value **argv |
| 4784 | ){ |
| 4785 | const unsigned char *pBlob; |
| 4786 | int nBlob; |
| 4787 | int iInt; |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 4788 | |
| 4789 | UNUSED_PARAMETER(argc); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 4790 | nBlob = sqlite3_value_bytes(argv[0]); |
| 4791 | pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); |
| 4792 | iInt = sqlite3_value_int(argv[1]); |
| 4793 | |
| 4794 | if( iInt>=0 && (iInt+1)*4<=nBlob ){ |
| 4795 | const unsigned char *a = &pBlob[iInt*4]; |
| 4796 | sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) |
| 4797 | + ((sqlite3_int64)a[1]<<16) |
| 4798 | + ((sqlite3_int64)a[2]<< 8) |
| 4799 | + ((sqlite3_int64)a[3]<< 0); |
| 4800 | sqlite3_result_int64(context, iVal); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | /* |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 4805 | ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, |
| 4806 | ** using "..." with internal double-quote characters doubled. |
| 4807 | */ |
| 4808 | static void shellIdQuote( |
| 4809 | sqlite3_context *context, |
| 4810 | int argc, |
| 4811 | sqlite3_value **argv |
| 4812 | ){ |
| 4813 | const char *zName = (const char*)sqlite3_value_text(argv[0]); |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 4814 | UNUSED_PARAMETER(argc); |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 4815 | if( zName ){ |
| 4816 | char *z = sqlite3_mprintf("\"%w\"", zName); |
| 4817 | sqlite3_result_text(context, z, -1, sqlite3_free); |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | /* |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4822 | ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X. |
| 4823 | */ |
| 4824 | static void shellUSleepFunc( |
| 4825 | sqlite3_context *context, |
drh | d36f588 | 2020-11-25 16:28:04 +0000 | [diff] [blame] | 4826 | int argcUnused, |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4827 | sqlite3_value **argv |
| 4828 | ){ |
| 4829 | int sleep = sqlite3_value_int(argv[0]); |
drh | d36f588 | 2020-11-25 16:28:04 +0000 | [diff] [blame] | 4830 | (void)argcUnused; |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4831 | sqlite3_sleep(sleep/1000); |
| 4832 | sqlite3_result_int(context, sleep); |
| 4833 | } |
| 4834 | |
| 4835 | /* |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4836 | ** Scalar function "shell_escape_crnl" used by the .recover command. |
| 4837 | ** The argument passed to this function is the output of built-in |
| 4838 | ** function quote(). If the first character of the input is "'", |
| 4839 | ** indicating that the value passed to quote() was a text value, |
| 4840 | ** then this function searches the input for "\n" and "\r" characters |
| 4841 | ** and adds a wrapper similar to the following: |
| 4842 | ** |
| 4843 | ** replace(replace(<input>, '\n', char(10), '\r', char(13)); |
| 4844 | ** |
| 4845 | ** Or, if the first character of the input is not "'", then a copy |
| 4846 | ** of the input is returned. |
| 4847 | */ |
| 4848 | static void shellEscapeCrnl( |
| 4849 | sqlite3_context *context, |
| 4850 | int argc, |
| 4851 | sqlite3_value **argv |
| 4852 | ){ |
| 4853 | const char *zText = (const char*)sqlite3_value_text(argv[0]); |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 4854 | UNUSED_PARAMETER(argc); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 4855 | if( zText && zText[0]=='\'' ){ |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4856 | int nText = sqlite3_value_bytes(argv[0]); |
| 4857 | int i; |
| 4858 | char zBuf1[20]; |
| 4859 | char zBuf2[20]; |
| 4860 | const char *zNL = 0; |
| 4861 | const char *zCR = 0; |
| 4862 | int nCR = 0; |
| 4863 | int nNL = 0; |
| 4864 | |
| 4865 | for(i=0; zText[i]; i++){ |
| 4866 | if( zNL==0 && zText[i]=='\n' ){ |
| 4867 | zNL = unused_string(zText, "\\n", "\\012", zBuf1); |
| 4868 | nNL = (int)strlen(zNL); |
| 4869 | } |
| 4870 | if( zCR==0 && zText[i]=='\r' ){ |
| 4871 | zCR = unused_string(zText, "\\r", "\\015", zBuf2); |
| 4872 | nCR = (int)strlen(zCR); |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | if( zNL || zCR ){ |
| 4877 | int iOut = 0; |
| 4878 | i64 nMax = (nNL > nCR) ? nNL : nCR; |
dan | 51f5ffa | 2019-04-29 11:41:46 +0000 | [diff] [blame] | 4879 | i64 nAlloc = nMax * nText + (nMax+64)*2; |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4880 | char *zOut = (char*)sqlite3_malloc64(nAlloc); |
| 4881 | if( zOut==0 ){ |
| 4882 | sqlite3_result_error_nomem(context); |
| 4883 | return; |
| 4884 | } |
| 4885 | |
| 4886 | if( zNL && zCR ){ |
| 4887 | memcpy(&zOut[iOut], "replace(replace(", 16); |
| 4888 | iOut += 16; |
| 4889 | }else{ |
| 4890 | memcpy(&zOut[iOut], "replace(", 8); |
| 4891 | iOut += 8; |
| 4892 | } |
| 4893 | for(i=0; zText[i]; i++){ |
| 4894 | if( zText[i]=='\n' ){ |
| 4895 | memcpy(&zOut[iOut], zNL, nNL); |
| 4896 | iOut += nNL; |
| 4897 | }else if( zText[i]=='\r' ){ |
| 4898 | memcpy(&zOut[iOut], zCR, nCR); |
| 4899 | iOut += nCR; |
| 4900 | }else{ |
| 4901 | zOut[iOut] = zText[i]; |
| 4902 | iOut++; |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | if( zNL ){ |
| 4907 | memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
| 4908 | memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; |
| 4909 | memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; |
| 4910 | } |
| 4911 | if( zCR ){ |
| 4912 | memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
| 4913 | memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; |
| 4914 | memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; |
| 4915 | } |
| 4916 | |
| 4917 | sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); |
| 4918 | sqlite3_free(zOut); |
| 4919 | return; |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | sqlite3_result_value(context, argv[0]); |
| 4924 | } |
| 4925 | |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4926 | /* Flags for open_db(). |
| 4927 | ** |
| 4928 | ** The default behavior of open_db() is to exit(1) if the database fails to |
| 4929 | ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error |
| 4930 | ** but still returns without calling exit. |
| 4931 | ** |
| 4932 | ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a |
| 4933 | ** ZIP archive if the file does not exist or is empty and its name matches |
| 4934 | ** the *.zip pattern. |
| 4935 | */ |
| 4936 | #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ |
| 4937 | #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ |
| 4938 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4939 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4940 | ** Make sure the database is open. If it is not, then open it. If |
| 4941 | ** the database fails to open, print an error message and exit. |
| 4942 | */ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4943 | static void open_db(ShellState *p, int openFlags){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4944 | if( p->db==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4945 | const char *zDbFilename = p->pAuxDb->zDbFilename; |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 4946 | if( p->openMode==SHELL_OPEN_UNSPEC ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4947 | if( zDbFilename==0 || zDbFilename[0]==0 ){ |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 4948 | p->openMode = SHELL_OPEN_NORMAL; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4949 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4950 | p->openMode = (u8)deduceDatabaseType(zDbFilename, |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4951 | (openFlags & OPEN_DB_ZIPFILE)!=0); |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 4952 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4953 | } |
| 4954 | switch( p->openMode ){ |
| 4955 | case SHELL_OPEN_APPENDVFS: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4956 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 4957 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4958 | break; |
| 4959 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4960 | case SHELL_OPEN_HEXDB: |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4961 | case SHELL_OPEN_DESERIALIZE: { |
| 4962 | sqlite3_open(0, &p->db); |
| 4963 | break; |
| 4964 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4965 | case SHELL_OPEN_ZIPFILE: { |
| 4966 | sqlite3_open(":memory:", &p->db); |
| 4967 | break; |
| 4968 | } |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 4969 | case SHELL_OPEN_READONLY: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4970 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 4971 | SQLITE_OPEN_READONLY|p->openFlags, 0); |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 4972 | break; |
| 4973 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4974 | case SHELL_OPEN_UNSPEC: |
| 4975 | case SHELL_OPEN_NORMAL: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4976 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 4977 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4978 | break; |
| 4979 | } |
| 4980 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4981 | globalDb = p->db; |
| 4982 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 4983 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4984 | zDbFilename, sqlite3_errmsg(p->db)); |
drh | f25cc4f | 2019-01-04 14:29:21 +0000 | [diff] [blame] | 4985 | if( openFlags & OPEN_DB_KEEPALIVE ){ |
| 4986 | sqlite3_open(":memory:", &p->db); |
| 4987 | return; |
| 4988 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4989 | exit(1); |
| 4990 | } |
| 4991 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4992 | sqlite3_enable_load_extension(p->db, 1); |
| 4993 | #endif |
| 4994 | sqlite3_fileio_init(p->db, 0, 0); |
| 4995 | sqlite3_shathree_init(p->db, 0, 0); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 4996 | sqlite3_completion_init(p->db, 0, 0); |
drh | f05dd03 | 2020-04-14 15:53:58 +0000 | [diff] [blame] | 4997 | sqlite3_uint_init(p->db, 0, 0); |
drh | beb9def | 2020-06-22 19:12:23 +0000 | [diff] [blame] | 4998 | sqlite3_decimal_init(p->db, 0, 0); |
drh | 6468990 | 2021-06-03 13:51:31 +0000 | [diff] [blame] | 4999 | sqlite3_regexp_init(p->db, 0, 0); |
drh | 8cda77d | 2020-06-24 15:06:29 +0000 | [diff] [blame] | 5000 | sqlite3_ieee_init(p->db, 0, 0); |
mistachkin | 72c38d8 | 2020-08-28 18:47:39 +0000 | [diff] [blame] | 5001 | sqlite3_series_init(p->db, 0, 0); |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 5002 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 5003 | sqlite3_dbdata_init(p->db, 0, 0); |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 5004 | #endif |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 5005 | #ifdef SQLITE_HAVE_ZLIB |
drh | 9198f5a | 2022-03-19 15:19:35 +0000 | [diff] [blame] | 5006 | if( !p->bSafeModePersist ){ |
| 5007 | sqlite3_zipfile_init(p->db, 0, 0); |
| 5008 | sqlite3_sqlar_init(p->db, 0, 0); |
| 5009 | } |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 5010 | #endif |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 5011 | sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5012 | shellAddSchemaName, 0, 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 5013 | sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, |
| 5014 | shellModuleSchema, 0, 0); |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5015 | sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, |
| 5016 | shellPutsFunc, 0, 0); |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 5017 | sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, |
| 5018 | shellEscapeCrnl, 0, 0); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 5019 | sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, |
| 5020 | shellInt32, 0, 0); |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 5021 | sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, |
| 5022 | shellIdQuote, 0, 0); |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 5023 | sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, |
| 5024 | shellUSleepFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5025 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 5026 | sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, |
| 5027 | editFunc, 0, 0); |
| 5028 | sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, |
| 5029 | editFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5030 | #endif |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5031 | if( p->openMode==SHELL_OPEN_ZIPFILE ){ |
| 5032 | char *zSql = sqlite3_mprintf( |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5033 | "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5034 | shell_check_oom(zSql); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5035 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5036 | sqlite3_free(zSql); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 5037 | } |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 5038 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5039 | else |
| 5040 | if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ |
mistachkin | 9949093 | 2018-12-17 22:19:57 +0000 | [diff] [blame] | 5041 | int rc; |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 5042 | int nData = 0; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5043 | unsigned char *aData; |
| 5044 | if( p->openMode==SHELL_OPEN_DESERIALIZE ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5045 | aData = (unsigned char*)readFile(zDbFilename, &nData); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5046 | }else{ |
| 5047 | aData = readHexDb(p, &nData); |
| 5048 | if( aData==0 ){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5049 | return; |
| 5050 | } |
| 5051 | } |
mistachkin | 9949093 | 2018-12-17 22:19:57 +0000 | [diff] [blame] | 5052 | rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 5053 | SQLITE_DESERIALIZE_RESIZEABLE | |
| 5054 | SQLITE_DESERIALIZE_FREEONCLOSE); |
| 5055 | if( rc ){ |
| 5056 | utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); |
| 5057 | } |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 5058 | if( p->szMax>0 ){ |
| 5059 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); |
| 5060 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5061 | } |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 5062 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5063 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 5064 | if( p->bSafeModePersist && p->db!=0 ){ |
| 5065 | sqlite3_set_authorizer(p->db, safeModeAuth, p); |
| 5066 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5069 | /* |
| 5070 | ** Attempt to close the databaes connection. Report errors. |
| 5071 | */ |
| 5072 | void close_db(sqlite3 *db){ |
| 5073 | int rc = sqlite3_close(db); |
| 5074 | if( rc ){ |
| 5075 | utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", |
| 5076 | rc, sqlite3_errmsg(db)); |
| 5077 | } |
| 5078 | } |
| 5079 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5080 | #if HAVE_READLINE || HAVE_EDITLINE |
| 5081 | /* |
| 5082 | ** Readline completion callbacks |
| 5083 | */ |
| 5084 | static char *readline_completion_generator(const char *text, int state){ |
| 5085 | static sqlite3_stmt *pStmt = 0; |
| 5086 | char *zRet; |
| 5087 | if( state==0 ){ |
| 5088 | char *zSql; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5089 | sqlite3_finalize(pStmt); |
| 5090 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 5091 | " FROM completion(%Q) ORDER BY 1", text); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5092 | shell_check_oom(zSql); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5093 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 5094 | sqlite3_free(zSql); |
| 5095 | } |
| 5096 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5097 | const char *z = (const char*)sqlite3_column_text(pStmt,0); |
| 5098 | zRet = z ? strdup(z) : 0; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5099 | }else{ |
| 5100 | sqlite3_finalize(pStmt); |
| 5101 | pStmt = 0; |
| 5102 | zRet = 0; |
| 5103 | } |
| 5104 | return zRet; |
| 5105 | } |
| 5106 | static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 5107 | rl_attempted_completion_over = 1; |
| 5108 | return rl_completion_matches(zText, readline_completion_generator); |
| 5109 | } |
| 5110 | |
| 5111 | #elif HAVE_LINENOISE |
| 5112 | /* |
| 5113 | ** Linenoise completion callback |
| 5114 | */ |
| 5115 | static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5116 | int nLine = strlen30(zLine); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5117 | int i, iStart; |
| 5118 | sqlite3_stmt *pStmt = 0; |
| 5119 | char *zSql; |
| 5120 | char zBuf[1000]; |
| 5121 | |
| 5122 | if( nLine>sizeof(zBuf)-30 ) return; |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 5123 | if( zLine[0]=='.' || zLine[0]=='#') return; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5124 | for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 5125 | if( i==nLine-1 ) return; |
| 5126 | iStart = i+1; |
| 5127 | memcpy(zBuf, zLine, iStart); |
| 5128 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 5129 | " FROM completion(%Q,%Q) ORDER BY 1", |
| 5130 | &zLine[iStart], zLine); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5131 | shell_check_oom(zSql); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5132 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 5133 | sqlite3_free(zSql); |
| 5134 | sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 5135 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5136 | const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 5137 | int nCompletion = sqlite3_column_bytes(pStmt, 0); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5138 | if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5139 | memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 5140 | linenoiseAddCompletion(lc, zBuf); |
| 5141 | } |
| 5142 | } |
| 5143 | sqlite3_finalize(pStmt); |
| 5144 | } |
| 5145 | #endif |
| 5146 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5147 | /* |
| 5148 | ** Do C-language style dequoting. |
| 5149 | ** |
| 5150 | ** \a -> alarm |
| 5151 | ** \b -> backspace |
| 5152 | ** \t -> tab |
| 5153 | ** \n -> newline |
| 5154 | ** \v -> vertical tab |
| 5155 | ** \f -> form feed |
| 5156 | ** \r -> carriage return |
| 5157 | ** \s -> space |
| 5158 | ** \" -> " |
| 5159 | ** \' -> ' |
| 5160 | ** \\ -> backslash |
| 5161 | ** \NNN -> ascii character NNN in octal |
| 5162 | */ |
| 5163 | static void resolve_backslashes(char *z){ |
| 5164 | int i, j; |
| 5165 | char c; |
| 5166 | while( *z && *z!='\\' ) z++; |
| 5167 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 5168 | if( c=='\\' && z[i+1]!=0 ){ |
| 5169 | c = z[++i]; |
| 5170 | if( c=='a' ){ |
| 5171 | c = '\a'; |
| 5172 | }else if( c=='b' ){ |
| 5173 | c = '\b'; |
| 5174 | }else if( c=='t' ){ |
| 5175 | c = '\t'; |
| 5176 | }else if( c=='n' ){ |
| 5177 | c = '\n'; |
| 5178 | }else if( c=='v' ){ |
| 5179 | c = '\v'; |
| 5180 | }else if( c=='f' ){ |
| 5181 | c = '\f'; |
| 5182 | }else if( c=='r' ){ |
| 5183 | c = '\r'; |
| 5184 | }else if( c=='"' ){ |
| 5185 | c = '"'; |
| 5186 | }else if( c=='\'' ){ |
| 5187 | c = '\''; |
| 5188 | }else if( c=='\\' ){ |
| 5189 | c = '\\'; |
| 5190 | }else if( c>='0' && c<='7' ){ |
| 5191 | c -= '0'; |
| 5192 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 5193 | i++; |
| 5194 | c = (c<<3) + z[i] - '0'; |
| 5195 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 5196 | i++; |
| 5197 | c = (c<<3) + z[i] - '0'; |
| 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | } |
| 5202 | z[j] = c; |
| 5203 | } |
| 5204 | if( j<i ) z[j] = 0; |
| 5205 | } |
| 5206 | |
| 5207 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5208 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 5209 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 5210 | */ |
| 5211 | static int booleanValue(const char *zArg){ |
| 5212 | int i; |
| 5213 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 5214 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 5215 | }else{ |
| 5216 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 5217 | } |
| 5218 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 5219 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 5220 | return 1; |
| 5221 | } |
| 5222 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 5223 | return 0; |
| 5224 | } |
| 5225 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 5226 | zArg); |
| 5227 | return 0; |
| 5228 | } |
| 5229 | |
| 5230 | /* |
| 5231 | ** Set or clear a shell flag according to a boolean value. |
| 5232 | */ |
| 5233 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 5234 | if( booleanValue(zArg) ){ |
| 5235 | ShellSetFlag(p, mFlag); |
| 5236 | }else{ |
| 5237 | ShellClearFlag(p, mFlag); |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | /* |
| 5242 | ** Close an output file, assuming it is not stderr or stdout |
| 5243 | */ |
| 5244 | static void output_file_close(FILE *f){ |
| 5245 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 5246 | } |
| 5247 | |
| 5248 | /* |
| 5249 | ** Try to open an output file. The names "stdout" and "stderr" are |
| 5250 | ** recognized and do the right thing. NULL is returned if the output |
| 5251 | ** filename is "off". |
| 5252 | */ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5253 | static FILE *output_file_open(const char *zFile, int bTextMode){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5254 | FILE *f; |
| 5255 | if( strcmp(zFile,"stdout")==0 ){ |
| 5256 | f = stdout; |
| 5257 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 5258 | f = stderr; |
| 5259 | }else if( strcmp(zFile, "off")==0 ){ |
| 5260 | f = 0; |
| 5261 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5262 | f = fopen(zFile, bTextMode ? "w" : "wb"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5263 | if( f==0 ){ |
| 5264 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 5265 | } |
| 5266 | } |
| 5267 | return f; |
| 5268 | } |
| 5269 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5270 | #ifndef SQLITE_OMIT_TRACE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5271 | /* |
| 5272 | ** A routine for handling output from sqlite3_trace(). |
| 5273 | */ |
| 5274 | static int sql_trace_callback( |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5275 | unsigned mType, /* The trace type */ |
| 5276 | void *pArg, /* The ShellState pointer */ |
| 5277 | void *pP, /* Usually a pointer to sqlite_stmt */ |
| 5278 | void *pX /* Auxiliary output */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5279 | ){ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5280 | ShellState *p = (ShellState*)pArg; |
| 5281 | sqlite3_stmt *pStmt; |
| 5282 | const char *zSql; |
| 5283 | int nSql; |
| 5284 | if( p->traceOut==0 ) return 0; |
| 5285 | if( mType==SQLITE_TRACE_CLOSE ){ |
| 5286 | utf8_printf(p->traceOut, "-- closing database connection\n"); |
| 5287 | return 0; |
| 5288 | } |
| 5289 | if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ |
| 5290 | zSql = (const char*)pX; |
| 5291 | }else{ |
| 5292 | pStmt = (sqlite3_stmt*)pP; |
| 5293 | switch( p->eTraceType ){ |
| 5294 | case SHELL_TRACE_EXPANDED: { |
| 5295 | zSql = sqlite3_expanded_sql(pStmt); |
| 5296 | break; |
| 5297 | } |
| 5298 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 5299 | case SHELL_TRACE_NORMALIZED: { |
| 5300 | zSql = sqlite3_normalized_sql(pStmt); |
| 5301 | break; |
| 5302 | } |
| 5303 | #endif |
| 5304 | default: { |
| 5305 | zSql = sqlite3_sql(pStmt); |
| 5306 | break; |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | if( zSql==0 ) return 0; |
| 5311 | nSql = strlen30(zSql); |
| 5312 | while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } |
| 5313 | switch( mType ){ |
| 5314 | case SQLITE_TRACE_ROW: |
| 5315 | case SQLITE_TRACE_STMT: { |
| 5316 | utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql); |
| 5317 | break; |
| 5318 | } |
| 5319 | case SQLITE_TRACE_PROFILE: { |
| 5320 | sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; |
| 5321 | utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec); |
| 5322 | break; |
| 5323 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5324 | } |
| 5325 | return 0; |
| 5326 | } |
| 5327 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5328 | |
| 5329 | /* |
| 5330 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 5331 | ** a useful spot to set a debugger breakpoint. |
| 5332 | */ |
| 5333 | static void test_breakpoint(void){ |
| 5334 | static int nCall = 0; |
| 5335 | nCall++; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | ** An object used to read a CSV and other files for import. |
| 5340 | */ |
| 5341 | typedef struct ImportCtx ImportCtx; |
| 5342 | struct ImportCtx { |
| 5343 | const char *zFile; /* Name of the input file */ |
| 5344 | FILE *in; /* Read the CSV text from this input stream */ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5345 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5346 | char *z; /* Accumulated text for a field */ |
| 5347 | int n; /* Number of bytes in z */ |
| 5348 | int nAlloc; /* Space allocated for z[] */ |
| 5349 | int nLine; /* Current line number */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 5350 | int nRow; /* Number of rows imported */ |
| 5351 | int nErr; /* Number of errors encountered */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5352 | int bNotFirst; /* True if one or more bytes already read */ |
| 5353 | int cTerm; /* Character that terminated the most recent field */ |
| 5354 | int cColSep; /* The column separator character. (Usually ",") */ |
| 5355 | int cRowSep; /* The row separator character. (Usually "\n") */ |
| 5356 | }; |
| 5357 | |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5358 | /* Clean up resourced used by an ImportCtx */ |
| 5359 | static void import_cleanup(ImportCtx *p){ |
drh | 42c2a04 | 2020-05-29 20:16:19 +0000 | [diff] [blame] | 5360 | if( p->in!=0 && p->xCloser!=0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5361 | p->xCloser(p->in); |
| 5362 | p->in = 0; |
| 5363 | } |
| 5364 | sqlite3_free(p->z); |
| 5365 | p->z = 0; |
| 5366 | } |
| 5367 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5368 | /* Append a single byte to z[] */ |
| 5369 | static void import_append_char(ImportCtx *p, int c){ |
| 5370 | if( p->n+1>=p->nAlloc ){ |
| 5371 | p->nAlloc += p->nAlloc + 100; |
| 5372 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5373 | shell_check_oom(p->z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5374 | } |
| 5375 | p->z[p->n++] = (char)c; |
| 5376 | } |
| 5377 | |
| 5378 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 5379 | ** with the option of having a separator other than ",". |
| 5380 | ** |
| 5381 | ** + Input comes from p->in. |
| 5382 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 5383 | ** from sqlite3_malloc64(). |
| 5384 | ** + Use p->cSep as the column separator. The default is ",". |
| 5385 | ** + Use p->rSep as the row separator. The default is "\n". |
| 5386 | ** + Keep track of the line number in p->nLine. |
| 5387 | ** + Store the character that terminates the field in p->cTerm. Store |
| 5388 | ** EOF on end-of-file. |
| 5389 | ** + Report syntax errors on stderr |
| 5390 | */ |
| 5391 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 5392 | int c; |
| 5393 | int cSep = p->cColSep; |
| 5394 | int rSep = p->cRowSep; |
| 5395 | p->n = 0; |
| 5396 | c = fgetc(p->in); |
| 5397 | if( c==EOF || seenInterrupt ){ |
| 5398 | p->cTerm = EOF; |
| 5399 | return 0; |
| 5400 | } |
| 5401 | if( c=='"' ){ |
| 5402 | int pc, ppc; |
| 5403 | int startLine = p->nLine; |
| 5404 | int cQuote = c; |
| 5405 | pc = ppc = 0; |
| 5406 | while( 1 ){ |
| 5407 | c = fgetc(p->in); |
| 5408 | if( c==rSep ) p->nLine++; |
| 5409 | if( c==cQuote ){ |
| 5410 | if( pc==cQuote ){ |
| 5411 | pc = 0; |
| 5412 | continue; |
| 5413 | } |
| 5414 | } |
| 5415 | if( (c==cSep && pc==cQuote) |
| 5416 | || (c==rSep && pc==cQuote) |
| 5417 | || (c==rSep && pc=='\r' && ppc==cQuote) |
| 5418 | || (c==EOF && pc==cQuote) |
| 5419 | ){ |
| 5420 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
| 5421 | p->cTerm = c; |
| 5422 | break; |
| 5423 | } |
| 5424 | if( pc==cQuote && c!='\r' ){ |
| 5425 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
| 5426 | p->zFile, p->nLine, cQuote); |
| 5427 | } |
| 5428 | if( c==EOF ){ |
| 5429 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
| 5430 | p->zFile, startLine, cQuote); |
| 5431 | p->cTerm = c; |
| 5432 | break; |
| 5433 | } |
| 5434 | import_append_char(p, c); |
| 5435 | ppc = pc; |
| 5436 | pc = c; |
| 5437 | } |
| 5438 | }else{ |
| 5439 | /* If this is the first field being parsed and it begins with the |
| 5440 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 5441 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 5442 | import_append_char(p, c); |
| 5443 | c = fgetc(p->in); |
| 5444 | if( (c&0xff)==0xbb ){ |
| 5445 | import_append_char(p, c); |
| 5446 | c = fgetc(p->in); |
| 5447 | if( (c&0xff)==0xbf ){ |
| 5448 | p->bNotFirst = 1; |
| 5449 | p->n = 0; |
| 5450 | return csv_read_one_field(p); |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 5455 | import_append_char(p, c); |
| 5456 | c = fgetc(p->in); |
| 5457 | } |
| 5458 | if( c==rSep ){ |
| 5459 | p->nLine++; |
| 5460 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 5461 | } |
| 5462 | p->cTerm = c; |
| 5463 | } |
| 5464 | if( p->z ) p->z[p->n] = 0; |
| 5465 | p->bNotFirst = 1; |
| 5466 | return p->z; |
| 5467 | } |
| 5468 | |
| 5469 | /* Read a single field of ASCII delimited text. |
| 5470 | ** |
| 5471 | ** + Input comes from p->in. |
| 5472 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 5473 | ** from sqlite3_malloc64(). |
| 5474 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 5475 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 5476 | ** + Keep track of the row number in p->nLine. |
| 5477 | ** + Store the character that terminates the field in p->cTerm. Store |
| 5478 | ** EOF on end-of-file. |
| 5479 | ** + Report syntax errors on stderr |
| 5480 | */ |
| 5481 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 5482 | int c; |
| 5483 | int cSep = p->cColSep; |
| 5484 | int rSep = p->cRowSep; |
| 5485 | p->n = 0; |
| 5486 | c = fgetc(p->in); |
| 5487 | if( c==EOF || seenInterrupt ){ |
| 5488 | p->cTerm = EOF; |
| 5489 | return 0; |
| 5490 | } |
| 5491 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 5492 | import_append_char(p, c); |
| 5493 | c = fgetc(p->in); |
| 5494 | } |
| 5495 | if( c==rSep ){ |
| 5496 | p->nLine++; |
| 5497 | } |
| 5498 | p->cTerm = c; |
| 5499 | if( p->z ) p->z[p->n] = 0; |
| 5500 | return p->z; |
| 5501 | } |
| 5502 | |
| 5503 | /* |
| 5504 | ** Try to transfer data for table zTable. If an error is seen while |
| 5505 | ** moving forward, try to go backwards. The backwards movement won't |
| 5506 | ** work for WITHOUT ROWID tables. |
| 5507 | */ |
| 5508 | static void tryToCloneData( |
| 5509 | ShellState *p, |
| 5510 | sqlite3 *newDb, |
| 5511 | const char *zTable |
| 5512 | ){ |
| 5513 | sqlite3_stmt *pQuery = 0; |
| 5514 | sqlite3_stmt *pInsert = 0; |
| 5515 | char *zQuery = 0; |
| 5516 | char *zInsert = 0; |
| 5517 | int rc; |
| 5518 | int i, j, n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5519 | int nTable = strlen30(zTable); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5520 | int k = 0; |
| 5521 | int cnt = 0; |
| 5522 | const int spinRate = 10000; |
| 5523 | |
| 5524 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5525 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5526 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5527 | if( rc ){ |
| 5528 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 5529 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5530 | zQuery); |
| 5531 | goto end_data_xfer; |
| 5532 | } |
| 5533 | n = sqlite3_column_count(pQuery); |
| 5534 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5535 | shell_check_oom(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5536 | sqlite3_snprintf(200+nTable,zInsert, |
| 5537 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5538 | i = strlen30(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5539 | for(j=1; j<n; j++){ |
| 5540 | memcpy(zInsert+i, ",?", 2); |
| 5541 | i += 2; |
| 5542 | } |
| 5543 | memcpy(zInsert+i, ");", 3); |
| 5544 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 5545 | if( rc ){ |
| 5546 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 5547 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 5548 | zQuery); |
| 5549 | goto end_data_xfer; |
| 5550 | } |
| 5551 | for(k=0; k<2; k++){ |
| 5552 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 5553 | for(i=0; i<n; i++){ |
| 5554 | switch( sqlite3_column_type(pQuery, i) ){ |
| 5555 | case SQLITE_NULL: { |
| 5556 | sqlite3_bind_null(pInsert, i+1); |
| 5557 | break; |
| 5558 | } |
| 5559 | case SQLITE_INTEGER: { |
| 5560 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 5561 | break; |
| 5562 | } |
| 5563 | case SQLITE_FLOAT: { |
| 5564 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 5565 | break; |
| 5566 | } |
| 5567 | case SQLITE_TEXT: { |
| 5568 | sqlite3_bind_text(pInsert, i+1, |
| 5569 | (const char*)sqlite3_column_text(pQuery,i), |
| 5570 | -1, SQLITE_STATIC); |
| 5571 | break; |
| 5572 | } |
| 5573 | case SQLITE_BLOB: { |
| 5574 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 5575 | sqlite3_column_bytes(pQuery,i), |
| 5576 | SQLITE_STATIC); |
| 5577 | break; |
| 5578 | } |
| 5579 | } |
| 5580 | } /* End for */ |
| 5581 | rc = sqlite3_step(pInsert); |
| 5582 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 5583 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
| 5584 | sqlite3_errmsg(newDb)); |
| 5585 | } |
| 5586 | sqlite3_reset(pInsert); |
| 5587 | cnt++; |
| 5588 | if( (cnt%spinRate)==0 ){ |
| 5589 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 5590 | fflush(stdout); |
| 5591 | } |
| 5592 | } /* End while */ |
| 5593 | if( rc==SQLITE_DONE ) break; |
| 5594 | sqlite3_finalize(pQuery); |
| 5595 | sqlite3_free(zQuery); |
| 5596 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 5597 | zTable); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5598 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5599 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5600 | if( rc ){ |
| 5601 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
| 5602 | break; |
| 5603 | } |
| 5604 | } /* End for(k=0...) */ |
| 5605 | |
| 5606 | end_data_xfer: |
| 5607 | sqlite3_finalize(pQuery); |
| 5608 | sqlite3_finalize(pInsert); |
| 5609 | sqlite3_free(zQuery); |
| 5610 | sqlite3_free(zInsert); |
| 5611 | } |
| 5612 | |
| 5613 | |
| 5614 | /* |
| 5615 | ** Try to transfer all rows of the schema that match zWhere. For |
| 5616 | ** each row, invoke xForEach() on the object defined by that row. |
| 5617 | ** If an error is encountered while moving forward through the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5618 | ** sqlite_schema table, try again moving backwards. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5619 | */ |
| 5620 | static void tryToCloneSchema( |
| 5621 | ShellState *p, |
| 5622 | sqlite3 *newDb, |
| 5623 | const char *zWhere, |
| 5624 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 5625 | ){ |
| 5626 | sqlite3_stmt *pQuery = 0; |
| 5627 | char *zQuery = 0; |
| 5628 | int rc; |
| 5629 | const unsigned char *zName; |
| 5630 | const unsigned char *zSql; |
| 5631 | char *zErrMsg = 0; |
| 5632 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5633 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5634 | " WHERE %s", zWhere); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5635 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5636 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5637 | if( rc ){ |
| 5638 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 5639 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5640 | zQuery); |
| 5641 | goto end_schema_xfer; |
| 5642 | } |
| 5643 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 5644 | zName = sqlite3_column_text(pQuery, 0); |
| 5645 | zSql = sqlite3_column_text(pQuery, 1); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5646 | if( zName==0 || zSql==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5647 | printf("%s... ", zName); fflush(stdout); |
| 5648 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 5649 | if( zErrMsg ){ |
| 5650 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 5651 | sqlite3_free(zErrMsg); |
| 5652 | zErrMsg = 0; |
| 5653 | } |
| 5654 | if( xForEach ){ |
| 5655 | xForEach(p, newDb, (const char*)zName); |
| 5656 | } |
| 5657 | printf("done\n"); |
| 5658 | } |
| 5659 | if( rc!=SQLITE_DONE ){ |
| 5660 | sqlite3_finalize(pQuery); |
| 5661 | sqlite3_free(zQuery); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5662 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5663 | " WHERE %s ORDER BY rowid DESC", zWhere); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5664 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5665 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5666 | if( rc ){ |
| 5667 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 5668 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5669 | zQuery); |
| 5670 | goto end_schema_xfer; |
| 5671 | } |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 5672 | while( sqlite3_step(pQuery)==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5673 | zName = sqlite3_column_text(pQuery, 0); |
| 5674 | zSql = sqlite3_column_text(pQuery, 1); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5675 | if( zName==0 || zSql==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5676 | printf("%s... ", zName); fflush(stdout); |
| 5677 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 5678 | if( zErrMsg ){ |
| 5679 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 5680 | sqlite3_free(zErrMsg); |
| 5681 | zErrMsg = 0; |
| 5682 | } |
| 5683 | if( xForEach ){ |
| 5684 | xForEach(p, newDb, (const char*)zName); |
| 5685 | } |
| 5686 | printf("done\n"); |
| 5687 | } |
| 5688 | } |
| 5689 | end_schema_xfer: |
| 5690 | sqlite3_finalize(pQuery); |
| 5691 | sqlite3_free(zQuery); |
| 5692 | } |
| 5693 | |
| 5694 | /* |
| 5695 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 5696 | ** as possible out of the main database (which might be corrupt) and write it |
| 5697 | ** into zNewDb. |
| 5698 | */ |
| 5699 | static void tryToClone(ShellState *p, const char *zNewDb){ |
| 5700 | int rc; |
| 5701 | sqlite3 *newDb = 0; |
| 5702 | if( access(zNewDb,0)==0 ){ |
| 5703 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
| 5704 | return; |
| 5705 | } |
| 5706 | rc = sqlite3_open(zNewDb, &newDb); |
| 5707 | if( rc ){ |
| 5708 | utf8_printf(stderr, "Cannot create output database: %s\n", |
| 5709 | sqlite3_errmsg(newDb)); |
| 5710 | }else{ |
| 5711 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
| 5712 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
| 5713 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 5714 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
| 5715 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
| 5716 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5717 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5718 | close_db(newDb); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | /* |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5722 | ** Change the output file back to stdout. |
| 5723 | ** |
| 5724 | ** If the p->doXdgOpen flag is set, that means the output was being |
| 5725 | ** redirected to a temporary file named by p->zTempFile. In that case, |
| 5726 | ** launch start/open/xdg-open on that temporary file. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5727 | */ |
| 5728 | static void output_reset(ShellState *p){ |
| 5729 | if( p->outfile[0]=='|' ){ |
| 5730 | #ifndef SQLITE_OMIT_POPEN |
| 5731 | pclose(p->out); |
| 5732 | #endif |
| 5733 | }else{ |
| 5734 | output_file_close(p->out); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5735 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5736 | if( p->doXdgOpen ){ |
| 5737 | const char *zXdgOpenCmd = |
| 5738 | #if defined(_WIN32) |
| 5739 | "start"; |
| 5740 | #elif defined(__APPLE__) |
| 5741 | "open"; |
| 5742 | #else |
| 5743 | "xdg-open"; |
| 5744 | #endif |
| 5745 | char *zCmd; |
| 5746 | zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5747 | if( system(zCmd) ){ |
| 5748 | utf8_printf(stderr, "Failed: [%s]\n", zCmd); |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 5749 | }else{ |
| 5750 | /* Give the start/open/xdg-open command some time to get |
| 5751 | ** going before we continue, and potential delete the |
| 5752 | ** p->zTempFile data file out from under it */ |
| 5753 | sqlite3_sleep(2000); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5754 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5755 | sqlite3_free(zCmd); |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 5756 | outputModePop(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5757 | p->doXdgOpen = 0; |
| 5758 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5759 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5760 | } |
| 5761 | p->outfile[0] = 0; |
| 5762 | p->out = stdout; |
| 5763 | } |
| 5764 | |
| 5765 | /* |
| 5766 | ** Run an SQL command and return the single integer result. |
| 5767 | */ |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 5768 | static int db_int(sqlite3 *db, const char *zSql){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5769 | sqlite3_stmt *pStmt; |
| 5770 | int res = 0; |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 5771 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5772 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5773 | res = sqlite3_column_int(pStmt,0); |
| 5774 | } |
| 5775 | sqlite3_finalize(pStmt); |
| 5776 | return res; |
| 5777 | } |
| 5778 | |
| 5779 | /* |
| 5780 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 5781 | */ |
| 5782 | static unsigned int get2byteInt(unsigned char *a){ |
| 5783 | return (a[0]<<8) + a[1]; |
| 5784 | } |
| 5785 | static unsigned int get4byteInt(unsigned char *a){ |
| 5786 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 5787 | } |
| 5788 | |
| 5789 | /* |
drh | 76c1206 | 2020-01-14 13:13:19 +0000 | [diff] [blame] | 5790 | ** Implementation of the ".dbinfo" command. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5791 | ** |
| 5792 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 5793 | */ |
| 5794 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 5795 | static const struct { const char *zName; int ofst; } aField[] = { |
| 5796 | { "file change counter:", 24 }, |
| 5797 | { "database page count:", 28 }, |
| 5798 | { "freelist page count:", 36 }, |
| 5799 | { "schema cookie:", 40 }, |
| 5800 | { "schema format:", 44 }, |
| 5801 | { "default cache size:", 48 }, |
| 5802 | { "autovacuum top root:", 52 }, |
| 5803 | { "incremental vacuum:", 64 }, |
| 5804 | { "text encoding:", 56 }, |
| 5805 | { "user version:", 60 }, |
| 5806 | { "application id:", 68 }, |
| 5807 | { "software version:", 96 }, |
| 5808 | }; |
| 5809 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 5810 | { "number of tables:", |
| 5811 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 5812 | { "number of indexes:", |
| 5813 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 5814 | { "number of triggers:", |
| 5815 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 5816 | { "number of views:", |
| 5817 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 5818 | { "schema size:", |
| 5819 | "SELECT total(length(sql)) FROM %s" }, |
| 5820 | }; |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5821 | int i, rc; |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 5822 | unsigned iDataVersion; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5823 | char *zSchemaTab; |
| 5824 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5825 | sqlite3_stmt *pStmt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5826 | unsigned char aHdr[100]; |
| 5827 | open_db(p, 0); |
| 5828 | if( p->db==0 ) return 1; |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5829 | rc = sqlite3_prepare_v2(p->db, |
| 5830 | "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
| 5831 | -1, &pStmt, 0); |
| 5832 | if( rc ){ |
drh | 451f89a | 2020-04-28 23:09:56 +0000 | [diff] [blame] | 5833 | utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5834 | sqlite3_finalize(pStmt); |
| 5835 | return 1; |
| 5836 | } |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5837 | sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
| 5838 | if( sqlite3_step(pStmt)==SQLITE_ROW |
| 5839 | && sqlite3_column_bytes(pStmt,0)>100 |
| 5840 | ){ |
| 5841 | memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
| 5842 | sqlite3_finalize(pStmt); |
| 5843 | }else{ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5844 | raw_printf(stderr, "unable to read database header\n"); |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5845 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5846 | return 1; |
| 5847 | } |
| 5848 | i = get2byteInt(aHdr+16); |
| 5849 | if( i==1 ) i = 65536; |
| 5850 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 5851 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 5852 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 5853 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
| 5854 | for(i=0; i<ArraySize(aField); i++){ |
| 5855 | int ofst = aField[i].ofst; |
| 5856 | unsigned int val = get4byteInt(aHdr + ofst); |
| 5857 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
| 5858 | switch( ofst ){ |
| 5859 | case 56: { |
| 5860 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 5861 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 5862 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
| 5863 | } |
| 5864 | } |
| 5865 | raw_printf(p->out, "\n"); |
| 5866 | } |
| 5867 | if( zDb==0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5868 | zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5869 | }else if( strcmp(zDb,"temp")==0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5870 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5871 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5872 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5873 | } |
| 5874 | for(i=0; i<ArraySize(aQuery); i++){ |
| 5875 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 5876 | int val = db_int(p->db, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5877 | sqlite3_free(zSql); |
| 5878 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
| 5879 | } |
| 5880 | sqlite3_free(zSchemaTab); |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 5881 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); |
| 5882 | utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5883 | return 0; |
| 5884 | } |
| 5885 | |
| 5886 | /* |
| 5887 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 5888 | */ |
| 5889 | static int shellDatabaseError(sqlite3 *db){ |
| 5890 | const char *zErr = sqlite3_errmsg(db); |
| 5891 | utf8_printf(stderr, "Error: %s\n", zErr); |
| 5892 | return 1; |
| 5893 | } |
| 5894 | |
| 5895 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5896 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 5897 | ** if they match and FALSE (0) if they do not match. |
| 5898 | ** |
| 5899 | ** Globbing rules: |
| 5900 | ** |
| 5901 | ** '*' Matches any sequence of zero or more characters. |
| 5902 | ** |
| 5903 | ** '?' Matches exactly one character. |
| 5904 | ** |
| 5905 | ** [...] Matches one character from the enclosed list of |
| 5906 | ** characters. |
| 5907 | ** |
| 5908 | ** [^...] Matches one character not in the enclosed list. |
| 5909 | ** |
| 5910 | ** '#' Matches any sequence of one or more digits with an |
| 5911 | ** optional + or - sign in front |
| 5912 | ** |
| 5913 | ** ' ' Any span of whitespace matches any other span of |
| 5914 | ** whitespace. |
| 5915 | ** |
| 5916 | ** Extra whitespace at the end of z[] is ignored. |
| 5917 | */ |
| 5918 | static int testcase_glob(const char *zGlob, const char *z){ |
| 5919 | int c, c2; |
| 5920 | int invert; |
| 5921 | int seen; |
| 5922 | |
| 5923 | while( (c = (*(zGlob++)))!=0 ){ |
| 5924 | if( IsSpace(c) ){ |
| 5925 | if( !IsSpace(*z) ) return 0; |
| 5926 | while( IsSpace(*zGlob) ) zGlob++; |
| 5927 | while( IsSpace(*z) ) z++; |
| 5928 | }else if( c=='*' ){ |
| 5929 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 5930 | if( c=='?' && (*(z++))==0 ) return 0; |
| 5931 | } |
| 5932 | if( c==0 ){ |
| 5933 | return 1; |
| 5934 | }else if( c=='[' ){ |
| 5935 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 5936 | z++; |
| 5937 | } |
| 5938 | return (*z)!=0; |
| 5939 | } |
| 5940 | while( (c2 = (*(z++)))!=0 ){ |
| 5941 | while( c2!=c ){ |
| 5942 | c2 = *(z++); |
| 5943 | if( c2==0 ) return 0; |
| 5944 | } |
| 5945 | if( testcase_glob(zGlob,z) ) return 1; |
| 5946 | } |
| 5947 | return 0; |
| 5948 | }else if( c=='?' ){ |
| 5949 | if( (*(z++))==0 ) return 0; |
| 5950 | }else if( c=='[' ){ |
| 5951 | int prior_c = 0; |
| 5952 | seen = 0; |
| 5953 | invert = 0; |
| 5954 | c = *(z++); |
| 5955 | if( c==0 ) return 0; |
| 5956 | c2 = *(zGlob++); |
| 5957 | if( c2=='^' ){ |
| 5958 | invert = 1; |
| 5959 | c2 = *(zGlob++); |
| 5960 | } |
| 5961 | if( c2==']' ){ |
| 5962 | if( c==']' ) seen = 1; |
| 5963 | c2 = *(zGlob++); |
| 5964 | } |
| 5965 | while( c2 && c2!=']' ){ |
| 5966 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 5967 | c2 = *(zGlob++); |
| 5968 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 5969 | prior_c = 0; |
| 5970 | }else{ |
| 5971 | if( c==c2 ){ |
| 5972 | seen = 1; |
| 5973 | } |
| 5974 | prior_c = c2; |
| 5975 | } |
| 5976 | c2 = *(zGlob++); |
| 5977 | } |
| 5978 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 5979 | }else if( c=='#' ){ |
| 5980 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 5981 | if( !IsDigit(z[0]) ) return 0; |
| 5982 | z++; |
| 5983 | while( IsDigit(z[0]) ){ z++; } |
| 5984 | }else{ |
| 5985 | if( c!=(*(z++)) ) return 0; |
| 5986 | } |
| 5987 | } |
| 5988 | while( IsSpace(*z) ){ z++; } |
| 5989 | return *z==0; |
| 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | /* |
| 5994 | ** Compare the string as a command-line option with either one or two |
| 5995 | ** initial "-" characters. |
| 5996 | */ |
| 5997 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 5998 | if( zStr[0]!='-' ) return 0; |
| 5999 | zStr++; |
| 6000 | if( zStr[0]=='-' ) zStr++; |
| 6001 | return strcmp(zStr, zOpt)==0; |
| 6002 | } |
| 6003 | |
| 6004 | /* |
| 6005 | ** Delete a file. |
| 6006 | */ |
| 6007 | int shellDeleteFile(const char *zFilename){ |
| 6008 | int rc; |
| 6009 | #ifdef _WIN32 |
| 6010 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
| 6011 | rc = _wunlink(z); |
| 6012 | sqlite3_free(z); |
| 6013 | #else |
| 6014 | rc = unlink(zFilename); |
| 6015 | #endif |
| 6016 | return rc; |
| 6017 | } |
| 6018 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6019 | /* |
| 6020 | ** Try to delete the temporary file (if there is one) and free the |
| 6021 | ** memory used to hold the name of the temp file. |
| 6022 | */ |
| 6023 | static void clearTempFile(ShellState *p){ |
| 6024 | if( p->zTempFile==0 ) return; |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 6025 | if( p->doXdgOpen ) return; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6026 | if( shellDeleteFile(p->zTempFile) ) return; |
| 6027 | sqlite3_free(p->zTempFile); |
| 6028 | p->zTempFile = 0; |
| 6029 | } |
| 6030 | |
| 6031 | /* |
| 6032 | ** Create a new temp file name with the given suffix. |
| 6033 | */ |
| 6034 | static void newTempFile(ShellState *p, const char *zSuffix){ |
| 6035 | clearTempFile(p); |
| 6036 | sqlite3_free(p->zTempFile); |
| 6037 | p->zTempFile = 0; |
drh | 7f3bf8a | 2018-01-10 21:50:08 +0000 | [diff] [blame] | 6038 | if( p->db ){ |
| 6039 | sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); |
| 6040 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6041 | if( p->zTempFile==0 ){ |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 6042 | /* If p->db is an in-memory database then the TEMPFILENAME file-control |
| 6043 | ** will not work and we will need to fallback to guessing */ |
| 6044 | char *zTemp; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6045 | sqlite3_uint64 r; |
| 6046 | sqlite3_randomness(sizeof(r), &r); |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 6047 | zTemp = getenv("TEMP"); |
| 6048 | if( zTemp==0 ) zTemp = getenv("TMP"); |
| 6049 | if( zTemp==0 ){ |
| 6050 | #ifdef _WIN32 |
| 6051 | zTemp = "\\tmp"; |
| 6052 | #else |
| 6053 | zTemp = "/tmp"; |
| 6054 | #endif |
| 6055 | } |
| 6056 | p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6057 | }else{ |
| 6058 | p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); |
| 6059 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 6060 | shell_check_oom(p->zTempFile); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6063 | |
| 6064 | /* |
| 6065 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
| 6066 | ** by the ".lint fkey-indexes" command. This scalar function is always |
| 6067 | ** called with four arguments - the parent table name, the parent column name, |
| 6068 | ** the child table name and the child column name. |
| 6069 | ** |
| 6070 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 6071 | ** |
| 6072 | ** If either of the named tables or columns do not exist, this function |
| 6073 | ** returns an empty string. An empty string is also returned if both tables |
| 6074 | ** and columns exist but have the same default collation sequence. Or, |
| 6075 | ** if both exist but the default collation sequences are different, this |
| 6076 | ** function returns the string " COLLATE <parent-collation>", where |
| 6077 | ** <parent-collation> is the default collation sequence of the parent column. |
| 6078 | */ |
| 6079 | static void shellFkeyCollateClause( |
| 6080 | sqlite3_context *pCtx, |
| 6081 | int nVal, |
| 6082 | sqlite3_value **apVal |
| 6083 | ){ |
| 6084 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 6085 | const char *zParent; |
| 6086 | const char *zParentCol; |
| 6087 | const char *zParentSeq; |
| 6088 | const char *zChild; |
| 6089 | const char *zChildCol; |
| 6090 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
| 6091 | int rc; |
| 6092 | |
| 6093 | assert( nVal==4 ); |
| 6094 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 6095 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 6096 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 6097 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 6098 | |
| 6099 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 6100 | rc = sqlite3_table_column_metadata( |
| 6101 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 6102 | ); |
| 6103 | if( rc==SQLITE_OK ){ |
| 6104 | rc = sqlite3_table_column_metadata( |
| 6105 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 6106 | ); |
| 6107 | } |
| 6108 | |
| 6109 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 6110 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 6111 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 6112 | sqlite3_free(z); |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | |
| 6117 | /* |
| 6118 | ** The implementation of dot-command ".lint fkey-indexes". |
| 6119 | */ |
| 6120 | static int lintFkeyIndexes( |
| 6121 | ShellState *pState, /* Current shell tool state */ |
| 6122 | char **azArg, /* Array of arguments passed to dot command */ |
| 6123 | int nArg /* Number of entries in azArg[] */ |
| 6124 | ){ |
| 6125 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 6126 | FILE *out = pState->out; /* Stream to write non-error output to */ |
| 6127 | int bVerbose = 0; /* If -verbose is present */ |
| 6128 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
| 6129 | int i; /* To iterate through azArg[] */ |
| 6130 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 6131 | int rc; /* Return code */ |
| 6132 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
| 6133 | |
| 6134 | /* |
| 6135 | ** This SELECT statement returns one row for each foreign key constraint |
| 6136 | ** in the schema of the main database. The column values are: |
| 6137 | ** |
| 6138 | ** 0. The text of an SQL statement similar to: |
| 6139 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6140 | ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6141 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6142 | ** This SELECT is similar to the one that the foreign keys implementation |
| 6143 | ** 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] | 6144 | ** be used to optimize this query, then it can also be used by the FK |
| 6145 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 6146 | ** table. |
| 6147 | ** |
| 6148 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 6149 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 6150 | ** contains an index that can be used to optimize the query. |
| 6151 | ** |
| 6152 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 6153 | ** |
| 6154 | ** "child_table(child_key1, child_key2)" |
| 6155 | ** |
| 6156 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 6157 | ** |
| 6158 | ** "parent_table(parent_key1, parent_key2)" |
| 6159 | ** |
| 6160 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 6161 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 6162 | ** |
| 6163 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 6164 | ** |
| 6165 | ** 5. The name of the parent table. |
| 6166 | ** |
| 6167 | ** These six values are used by the C logic below to generate the report. |
| 6168 | */ |
| 6169 | const char *zSql = |
| 6170 | "SELECT " |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6171 | " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6172 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 6173 | " || fkey_collate_clause(" |
| 6174 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
| 6175 | ", " |
drh | 8210233 | 2021-03-20 15:11:29 +0000 | [diff] [blame] | 6176 | " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6177 | " || group_concat('*=?', ' AND ') || ')'" |
| 6178 | ", " |
| 6179 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
| 6180 | ", " |
| 6181 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
| 6182 | ", " |
| 6183 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 6184 | " || ' ON ' || quote(s.name) || '('" |
| 6185 | " || group_concat(quote(f.[from]) ||" |
| 6186 | " fkey_collate_clause(" |
| 6187 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
| 6188 | " || ');'" |
| 6189 | ", " |
| 6190 | " f.[table] " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6191 | "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6192 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
| 6193 | "GROUP BY s.name, f.id " |
| 6194 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
| 6195 | ; |
drh | 8210233 | 2021-03-20 15:11:29 +0000 | [diff] [blame] | 6196 | const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6197 | |
| 6198 | for(i=2; i<nArg; i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6199 | int n = strlen30(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6200 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 6201 | bVerbose = 1; |
| 6202 | } |
| 6203 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 6204 | bGroupByParent = 1; |
| 6205 | zIndent = " "; |
| 6206 | } |
| 6207 | else{ |
| 6208 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 6209 | azArg[0], azArg[1] |
| 6210 | ); |
| 6211 | return SQLITE_ERROR; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | /* Register the fkey_collate_clause() SQL function */ |
| 6216 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 6217 | 0, shellFkeyCollateClause, 0, 0 |
| 6218 | ); |
| 6219 | |
| 6220 | |
| 6221 | if( rc==SQLITE_OK ){ |
| 6222 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 6223 | } |
| 6224 | if( rc==SQLITE_OK ){ |
| 6225 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 6226 | } |
| 6227 | |
| 6228 | if( rc==SQLITE_OK ){ |
| 6229 | int rc2; |
| 6230 | char *zPrev = 0; |
| 6231 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 6232 | int res = -1; |
| 6233 | sqlite3_stmt *pExplain = 0; |
| 6234 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 6235 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 6236 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 6237 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 6238 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
| 6239 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
| 6240 | |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 6241 | if( zEQP==0 ) continue; |
| 6242 | if( zGlob==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6243 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 6244 | if( rc!=SQLITE_OK ) break; |
| 6245 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 6246 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 6247 | res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) |
| 6248 | || 0==sqlite3_strglob(zGlobIPK, zPlan)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6249 | } |
| 6250 | rc = sqlite3_finalize(pExplain); |
| 6251 | if( rc!=SQLITE_OK ) break; |
| 6252 | |
| 6253 | if( res<0 ){ |
| 6254 | raw_printf(stderr, "Error: internal error"); |
| 6255 | break; |
| 6256 | }else{ |
| 6257 | if( bGroupByParent |
| 6258 | && (bVerbose || res==0) |
| 6259 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 6260 | ){ |
| 6261 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 6262 | sqlite3_free(zPrev); |
| 6263 | zPrev = sqlite3_mprintf("%s", zParent); |
| 6264 | } |
| 6265 | |
| 6266 | if( res==0 ){ |
| 6267 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 6268 | }else if( bVerbose ){ |
| 6269 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 6270 | zIndent, zFrom, zTarget |
| 6271 | ); |
| 6272 | } |
| 6273 | } |
| 6274 | } |
| 6275 | sqlite3_free(zPrev); |
| 6276 | |
| 6277 | if( rc!=SQLITE_OK ){ |
| 6278 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6279 | } |
| 6280 | |
| 6281 | rc2 = sqlite3_finalize(pSql); |
| 6282 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 6283 | rc = rc2; |
| 6284 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6285 | } |
| 6286 | }else{ |
| 6287 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6288 | } |
| 6289 | |
| 6290 | return rc; |
| 6291 | } |
| 6292 | |
| 6293 | /* |
| 6294 | ** Implementation of ".lint" dot command. |
| 6295 | */ |
| 6296 | static int lintDotCommand( |
| 6297 | ShellState *pState, /* Current shell tool state */ |
| 6298 | char **azArg, /* Array of arguments passed to dot command */ |
| 6299 | int nArg /* Number of entries in azArg[] */ |
| 6300 | ){ |
| 6301 | int n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6302 | n = (nArg>=2 ? strlen30(azArg[1]) : 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6303 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 6304 | return lintFkeyIndexes(pState, azArg, nArg); |
| 6305 | |
| 6306 | usage: |
| 6307 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 6308 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 6309 | raw_printf(stderr, " fkey-indexes\n"); |
| 6310 | return SQLITE_ERROR; |
| 6311 | } |
| 6312 | |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6313 | #if !defined SQLITE_OMIT_VIRTUALTABLE |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6314 | static void shellPrepare( |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6315 | sqlite3 *db, |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6316 | int *pRc, |
| 6317 | const char *zSql, |
| 6318 | sqlite3_stmt **ppStmt |
| 6319 | ){ |
| 6320 | *ppStmt = 0; |
| 6321 | if( *pRc==SQLITE_OK ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6322 | int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6323 | if( rc!=SQLITE_OK ){ |
| 6324 | raw_printf(stderr, "sql error: %s (%d)\n", |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6325 | sqlite3_errmsg(db), sqlite3_errcode(db) |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6326 | ); |
| 6327 | *pRc = rc; |
| 6328 | } |
| 6329 | } |
| 6330 | } |
| 6331 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6332 | /* |
| 6333 | ** Create a prepared statement using printf-style arguments for the SQL. |
| 6334 | ** |
| 6335 | ** This routine is could be marked "static". But it is not always used, |
| 6336 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6337 | ** nuisance compiler warnings about "defined but not used". |
| 6338 | */ |
| 6339 | void shellPreparePrintf( |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6340 | sqlite3 *db, |
| 6341 | int *pRc, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6342 | sqlite3_stmt **ppStmt, |
| 6343 | const char *zFmt, |
| 6344 | ... |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6345 | ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6346 | *ppStmt = 0; |
| 6347 | if( *pRc==SQLITE_OK ){ |
| 6348 | va_list ap; |
| 6349 | char *z; |
| 6350 | va_start(ap, zFmt); |
| 6351 | z = sqlite3_vmprintf(zFmt, ap); |
drh | 1dbb147 | 2018-10-11 10:37:24 +0000 | [diff] [blame] | 6352 | va_end(ap); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6353 | if( z==0 ){ |
| 6354 | *pRc = SQLITE_NOMEM; |
| 6355 | }else{ |
| 6356 | shellPrepare(db, pRc, z, ppStmt); |
| 6357 | sqlite3_free(z); |
| 6358 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6359 | } |
| 6360 | } |
| 6361 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6362 | /* Finalize the prepared statement created using shellPreparePrintf(). |
| 6363 | ** |
| 6364 | ** This routine is could be marked "static". But it is not always used, |
| 6365 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6366 | ** nuisance compiler warnings about "defined but not used". |
| 6367 | */ |
| 6368 | void shellFinalize( |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6369 | int *pRc, |
| 6370 | sqlite3_stmt *pStmt |
| 6371 | ){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6372 | if( pStmt ){ |
| 6373 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 6374 | int rc = sqlite3_finalize(pStmt); |
| 6375 | if( *pRc==SQLITE_OK ){ |
| 6376 | if( rc!=SQLITE_OK ){ |
| 6377 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 6378 | } |
| 6379 | *pRc = rc; |
| 6380 | } |
| 6381 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6384 | /* Reset the prepared statement created using shellPreparePrintf(). |
| 6385 | ** |
| 6386 | ** This routine is could be marked "static". But it is not always used, |
| 6387 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6388 | ** nuisance compiler warnings about "defined but not used". |
| 6389 | */ |
| 6390 | void shellReset( |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6391 | int *pRc, |
| 6392 | sqlite3_stmt *pStmt |
| 6393 | ){ |
| 6394 | int rc = sqlite3_reset(pStmt); |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6395 | if( *pRc==SQLITE_OK ){ |
| 6396 | if( rc!=SQLITE_OK ){ |
| 6397 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 6398 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 6399 | } |
| 6400 | *pRc = rc; |
| 6401 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6402 | } |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6403 | #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ |
| 6404 | |
| 6405 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 6406 | /****************************************************************************** |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6407 | ** The ".archive" or ".ar" command. |
| 6408 | */ |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 6409 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6410 | ** Structure representing a single ".ar" command. |
| 6411 | */ |
| 6412 | typedef struct ArCommand ArCommand; |
| 6413 | struct ArCommand { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6414 | u8 eCmd; /* An AR_CMD_* value */ |
| 6415 | u8 bVerbose; /* True if --verbose */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6416 | u8 bZip; /* True if the archive is a ZIP */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6417 | u8 bDryRun; /* True if --dry-run */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6418 | u8 bAppend; /* True if --append */ |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6419 | u8 bGlob; /* True if --glob */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6420 | u8 fromCmdLine; /* Run from -A instead of .archive */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6421 | int nArg; /* Number of command arguments */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6422 | char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6423 | const char *zFile; /* --file argument, or NULL */ |
| 6424 | const char *zDir; /* --directory argument, or NULL */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6425 | char **azArg; /* Array of command arguments */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6426 | ShellState *p; /* Shell state */ |
| 6427 | sqlite3 *db; /* Database containing the archive */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6428 | }; |
| 6429 | |
| 6430 | /* |
| 6431 | ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. |
| 6432 | */ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6433 | static int arUsage(FILE *f){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 6434 | showHelp(f,"archive"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6435 | return SQLITE_ERROR; |
| 6436 | } |
| 6437 | |
| 6438 | /* |
| 6439 | ** Print an error message for the .ar command to stderr and return |
| 6440 | ** SQLITE_ERROR. |
| 6441 | */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6442 | static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6443 | va_list ap; |
| 6444 | char *z; |
| 6445 | va_start(ap, zFmt); |
| 6446 | z = sqlite3_vmprintf(zFmt, ap); |
| 6447 | va_end(ap); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6448 | utf8_printf(stderr, "Error: %s\n", z); |
| 6449 | if( pAr->fromCmdLine ){ |
| 6450 | utf8_printf(stderr, "Use \"-A\" for more help\n"); |
| 6451 | }else{ |
| 6452 | utf8_printf(stderr, "Use \".archive --help\" for more help\n"); |
| 6453 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6454 | sqlite3_free(z); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6455 | return SQLITE_ERROR; |
| 6456 | } |
| 6457 | |
| 6458 | /* |
| 6459 | ** Values for ArCommand.eCmd. |
| 6460 | */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6461 | #define AR_CMD_CREATE 1 |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6462 | #define AR_CMD_UPDATE 2 |
| 6463 | #define AR_CMD_INSERT 3 |
| 6464 | #define AR_CMD_EXTRACT 4 |
| 6465 | #define AR_CMD_LIST 5 |
| 6466 | #define AR_CMD_HELP 6 |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6467 | #define AR_CMD_REMOVE 7 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6468 | |
| 6469 | /* |
| 6470 | ** Other (non-command) switches. |
| 6471 | */ |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6472 | #define AR_SWITCH_VERBOSE 8 |
| 6473 | #define AR_SWITCH_FILE 9 |
| 6474 | #define AR_SWITCH_DIRECTORY 10 |
| 6475 | #define AR_SWITCH_APPEND 11 |
larrybr | 719506f | 2021-11-01 22:33:20 +0000 | [diff] [blame] | 6476 | #define AR_SWITCH_DRYRUN 12 |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6477 | #define AR_SWITCH_GLOB 13 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6478 | |
| 6479 | static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ |
| 6480 | switch( eSwitch ){ |
| 6481 | case AR_CMD_CREATE: |
| 6482 | case AR_CMD_EXTRACT: |
| 6483 | case AR_CMD_LIST: |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6484 | case AR_CMD_REMOVE: |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6485 | case AR_CMD_UPDATE: |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6486 | case AR_CMD_INSERT: |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6487 | case AR_CMD_HELP: |
| 6488 | if( pAr->eCmd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6489 | return arErrorMsg(pAr, "multiple command options"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6490 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6491 | pAr->eCmd = eSwitch; |
| 6492 | break; |
| 6493 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6494 | case AR_SWITCH_DRYRUN: |
| 6495 | pAr->bDryRun = 1; |
| 6496 | break; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6497 | case AR_SWITCH_GLOB: |
| 6498 | pAr->bGlob = 1; |
| 6499 | break; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6500 | case AR_SWITCH_VERBOSE: |
| 6501 | pAr->bVerbose = 1; |
| 6502 | break; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6503 | case AR_SWITCH_APPEND: |
| 6504 | pAr->bAppend = 1; |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 6505 | /* Fall thru into --file */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6506 | case AR_SWITCH_FILE: |
| 6507 | pAr->zFile = zArg; |
| 6508 | break; |
| 6509 | case AR_SWITCH_DIRECTORY: |
| 6510 | pAr->zDir = zArg; |
| 6511 | break; |
| 6512 | } |
| 6513 | |
| 6514 | return SQLITE_OK; |
| 6515 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6516 | |
| 6517 | /* |
| 6518 | ** Parse the command line for an ".ar" command. The results are written into |
| 6519 | ** structure (*pAr). SQLITE_OK is returned if the command line is parsed |
| 6520 | ** successfully, otherwise an error message is written to stderr and |
| 6521 | ** SQLITE_ERROR returned. |
| 6522 | */ |
| 6523 | static int arParseCommand( |
| 6524 | char **azArg, /* Array of arguments passed to dot command */ |
| 6525 | int nArg, /* Number of entries in azArg[] */ |
| 6526 | ArCommand *pAr /* Populate this object */ |
| 6527 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6528 | struct ArSwitch { |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6529 | const char *zLong; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6530 | char cShort; |
| 6531 | u8 eSwitch; |
| 6532 | u8 bArg; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6533 | } aSwitch[] = { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6534 | { "create", 'c', AR_CMD_CREATE, 0 }, |
| 6535 | { "extract", 'x', AR_CMD_EXTRACT, 0 }, |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6536 | { "insert", 'i', AR_CMD_INSERT, 0 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6537 | { "list", 't', AR_CMD_LIST, 0 }, |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6538 | { "remove", 'r', AR_CMD_REMOVE, 0 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6539 | { "update", 'u', AR_CMD_UPDATE, 0 }, |
| 6540 | { "help", 'h', AR_CMD_HELP, 0 }, |
| 6541 | { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, |
| 6542 | { "file", 'f', AR_SWITCH_FILE, 1 }, |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 6543 | { "append", 'a', AR_SWITCH_APPEND, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6544 | { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6545 | { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6546 | { "glob", 'g', AR_SWITCH_GLOB, 0 }, |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6547 | }; |
| 6548 | int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); |
| 6549 | struct ArSwitch *pEnd = &aSwitch[nSwitch]; |
| 6550 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6551 | if( nArg<=1 ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 6552 | utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6553 | return arUsage(stderr); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6554 | }else{ |
| 6555 | char *z = azArg[1]; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6556 | if( z[0]!='-' ){ |
| 6557 | /* Traditional style [tar] invocation */ |
| 6558 | int i; |
| 6559 | int iArg = 2; |
| 6560 | for(i=0; z[i]; i++){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6561 | const char *zArg = 0; |
| 6562 | struct ArSwitch *pOpt; |
| 6563 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6564 | if( z[i]==pOpt->cShort ) break; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6565 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6566 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6567 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6568 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6569 | if( pOpt->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6570 | if( iArg>=nArg ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6571 | return arErrorMsg(pAr, "option requires an argument: %c",z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6572 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6573 | zArg = azArg[iArg++]; |
| 6574 | } |
| 6575 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6576 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6577 | pAr->nArg = nArg-iArg; |
| 6578 | if( pAr->nArg>0 ){ |
| 6579 | pAr->azArg = &azArg[iArg]; |
| 6580 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6581 | }else{ |
| 6582 | /* Non-traditional invocation */ |
| 6583 | int iArg; |
| 6584 | for(iArg=1; iArg<nArg; iArg++){ |
| 6585 | int n; |
| 6586 | z = azArg[iArg]; |
| 6587 | if( z[0]!='-' ){ |
| 6588 | /* All remaining command line words are command arguments. */ |
| 6589 | pAr->azArg = &azArg[iArg]; |
| 6590 | pAr->nArg = nArg-iArg; |
| 6591 | break; |
| 6592 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6593 | n = strlen30(z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6594 | |
| 6595 | if( z[1]!='-' ){ |
| 6596 | int i; |
| 6597 | /* One or more short options */ |
| 6598 | for(i=1; i<n; i++){ |
| 6599 | const char *zArg = 0; |
| 6600 | struct ArSwitch *pOpt; |
| 6601 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6602 | if( z[i]==pOpt->cShort ) break; |
| 6603 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6604 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6605 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6606 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6607 | if( pOpt->bArg ){ |
| 6608 | if( i<(n-1) ){ |
| 6609 | zArg = &z[i+1]; |
| 6610 | i = n; |
| 6611 | }else{ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6612 | if( iArg>=(nArg-1) ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 6613 | return arErrorMsg(pAr, "option requires an argument: %c", |
| 6614 | z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6615 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6616 | zArg = azArg[++iArg]; |
| 6617 | } |
| 6618 | } |
| 6619 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
| 6620 | } |
| 6621 | }else if( z[2]=='\0' ){ |
| 6622 | /* A -- option, indicating that all remaining command line words |
| 6623 | ** are command arguments. */ |
| 6624 | pAr->azArg = &azArg[iArg+1]; |
| 6625 | pAr->nArg = nArg-iArg-1; |
| 6626 | break; |
| 6627 | }else{ |
| 6628 | /* A long option */ |
| 6629 | const char *zArg = 0; /* Argument for option, if any */ |
| 6630 | struct ArSwitch *pMatch = 0; /* Matching option */ |
| 6631 | struct ArSwitch *pOpt; /* Iterator */ |
| 6632 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6633 | const char *zLong = pOpt->zLong; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6634 | if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6635 | if( pMatch ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6636 | return arErrorMsg(pAr, "ambiguous option: %s",z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6637 | }else{ |
| 6638 | pMatch = pOpt; |
| 6639 | } |
| 6640 | } |
| 6641 | } |
| 6642 | |
| 6643 | if( pMatch==0 ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6644 | return arErrorMsg(pAr, "unrecognized option: %s", z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6645 | } |
| 6646 | if( pMatch->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6647 | if( iArg>=(nArg-1) ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6648 | return arErrorMsg(pAr, "option requires an argument: %s", z); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6649 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6650 | zArg = azArg[++iArg]; |
| 6651 | } |
| 6652 | if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; |
| 6653 | } |
| 6654 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | return SQLITE_OK; |
| 6659 | } |
| 6660 | |
| 6661 | /* |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6662 | ** This function assumes that all arguments within the ArCommand.azArg[] |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6663 | ** array refer to archive members, as for the --extract, --list or --remove |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6664 | ** commands. It checks that each of them are "present". If any specified |
| 6665 | ** file is not present in the archive, an error is printed to stderr and an |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6666 | ** error code returned. Otherwise, if all specified arguments are present |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6667 | ** in the archive, SQLITE_OK is returned. Here, "present" means either an |
| 6668 | ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match |
drh | 0c0fb9c | 2021-11-02 10:54:39 +0000 | [diff] [blame] | 6669 | ** when pAr->bGlob is true. |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6670 | ** |
| 6671 | ** This function strips any trailing '/' characters from each argument. |
| 6672 | ** This is consistent with the way the [tar] command seems to work on |
| 6673 | ** Linux. |
| 6674 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6675 | static int arCheckEntries(ArCommand *pAr){ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6676 | int rc = SQLITE_OK; |
| 6677 | if( pAr->nArg ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6678 | int i, j; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6679 | sqlite3_stmt *pTest = 0; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6680 | const char *zSel = (pAr->bGlob) |
| 6681 | ? "SELECT name FROM %s WHERE glob($name,name)" |
| 6682 | : "SELECT name FROM %s WHERE name=$name"; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6683 | |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6684 | shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6685 | j = sqlite3_bind_parameter_index(pTest, "$name"); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6686 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
| 6687 | char *z = pAr->azArg[i]; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6688 | int n = strlen30(z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6689 | int bOk = 0; |
| 6690 | while( n>0 && z[n-1]=='/' ) n--; |
| 6691 | z[n] = '\0'; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6692 | sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6693 | if( SQLITE_ROW==sqlite3_step(pTest) ){ |
| 6694 | bOk = 1; |
| 6695 | } |
| 6696 | shellReset(&rc, pTest); |
| 6697 | if( rc==SQLITE_OK && bOk==0 ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6698 | utf8_printf(stderr, "not found in archive: %s\n", z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6699 | rc = SQLITE_ERROR; |
| 6700 | } |
| 6701 | } |
| 6702 | shellFinalize(&rc, pTest); |
| 6703 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6704 | return rc; |
| 6705 | } |
| 6706 | |
| 6707 | /* |
| 6708 | ** Format a WHERE clause that can be used against the "sqlar" table to |
| 6709 | ** identify all archive members that match the command arguments held |
| 6710 | ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. |
| 6711 | ** The caller is responsible for eventually calling sqlite3_free() on |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6712 | ** any non-NULL (*pzWhere) value. Here, "match" means strict equality |
| 6713 | ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6714 | */ |
| 6715 | static void arWhereClause( |
| 6716 | int *pRc, |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6717 | ArCommand *pAr, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6718 | char **pzWhere /* OUT: New WHERE clause */ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6719 | ){ |
| 6720 | char *zWhere = 0; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6721 | const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6722 | if( *pRc==SQLITE_OK ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6723 | if( pAr->nArg==0 ){ |
| 6724 | zWhere = sqlite3_mprintf("1"); |
| 6725 | }else{ |
| 6726 | int i; |
| 6727 | const char *zSep = ""; |
| 6728 | for(i=0; i<pAr->nArg; i++){ |
| 6729 | const char *z = pAr->azArg[i]; |
| 6730 | zWhere = sqlite3_mprintf( |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6731 | "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", |
| 6732 | zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6733 | ); |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6734 | if( zWhere==0 ){ |
| 6735 | *pRc = SQLITE_NOMEM; |
| 6736 | break; |
| 6737 | } |
| 6738 | zSep = " OR "; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6739 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6740 | } |
| 6741 | } |
| 6742 | *pzWhere = zWhere; |
| 6743 | } |
| 6744 | |
| 6745 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6746 | ** Implementation of .ar "lisT" command. |
| 6747 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6748 | static int arListCommand(ArCommand *pAr){ |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6749 | const char *zSql = "SELECT %s FROM %s WHERE %s"; |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6750 | const char *azCols[] = { |
| 6751 | "name", |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6752 | "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6753 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6754 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6755 | char *zWhere = 0; |
| 6756 | sqlite3_stmt *pSql = 0; |
| 6757 | int rc; |
| 6758 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6759 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6760 | arWhereClause(&rc, pAr, &zWhere); |
| 6761 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6762 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], |
| 6763 | pAr->zSrcTable, zWhere); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6764 | if( pAr->bDryRun ){ |
| 6765 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 6766 | }else{ |
| 6767 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 6768 | if( pAr->bVerbose ){ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6769 | utf8_printf(pAr->p->out, "%s % 10d %s %s\n", |
| 6770 | sqlite3_column_text(pSql, 0), |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6771 | sqlite3_column_int(pSql, 1), |
| 6772 | sqlite3_column_text(pSql, 2), |
| 6773 | sqlite3_column_text(pSql, 3) |
| 6774 | ); |
| 6775 | }else{ |
| 6776 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 6777 | } |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6778 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6779 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6780 | shellFinalize(&rc, pSql); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6781 | sqlite3_free(zWhere); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6782 | return rc; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6783 | } |
| 6784 | |
| 6785 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6786 | /* |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6787 | ** Implementation of .ar "Remove" command. |
| 6788 | */ |
| 6789 | static int arRemoveCommand(ArCommand *pAr){ |
larrybr | 7774fc0 | 2021-11-01 22:30:24 +0000 | [diff] [blame] | 6790 | int rc = 0; |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6791 | char *zSql = 0; |
| 6792 | char *zWhere = 0; |
| 6793 | |
| 6794 | if( pAr->nArg ){ |
| 6795 | /* Verify that args actually exist within the archive before proceeding. |
| 6796 | ** And formulate a WHERE clause to match them. */ |
| 6797 | rc = arCheckEntries(pAr); |
| 6798 | arWhereClause(&rc, pAr, &zWhere); |
| 6799 | } |
| 6800 | if( rc==SQLITE_OK ){ |
| 6801 | zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", |
| 6802 | pAr->zSrcTable, zWhere); |
| 6803 | if( pAr->bDryRun ){ |
| 6804 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 6805 | }else{ |
| 6806 | char *zErr = 0; |
| 6807 | rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); |
| 6808 | if( rc==SQLITE_OK ){ |
| 6809 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 6810 | if( rc!=SQLITE_OK ){ |
| 6811 | sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); |
| 6812 | }else{ |
| 6813 | rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); |
| 6814 | } |
| 6815 | } |
| 6816 | if( zErr ){ |
| 6817 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 6818 | sqlite3_free(zErr); |
| 6819 | } |
| 6820 | } |
| 6821 | } |
| 6822 | sqlite3_free(zWhere); |
| 6823 | sqlite3_free(zSql); |
| 6824 | return rc; |
| 6825 | } |
| 6826 | |
| 6827 | /* |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6828 | ** Implementation of .ar "eXtract" command. |
| 6829 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6830 | static int arExtractCommand(ArCommand *pAr){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6831 | const char *zSql1 = |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 6832 | "SELECT " |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6833 | " ($dir || name)," |
| 6834 | " writefile(($dir || name), %s, mode, mtime) " |
drh | 0cfd46a | 2018-06-06 01:18:01 +0000 | [diff] [blame] | 6835 | "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" |
| 6836 | " AND name NOT GLOB '*..[/\\]*'"; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6837 | |
| 6838 | const char *azExtraArg[] = { |
| 6839 | "sqlar_uncompress(data, sz)", |
dan | 7c15ac1 | 2018-01-08 19:59:59 +0000 | [diff] [blame] | 6840 | "data" |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6841 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6842 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6843 | sqlite3_stmt *pSql = 0; |
| 6844 | int rc = SQLITE_OK; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6845 | char *zDir = 0; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6846 | char *zWhere = 0; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6847 | int i, j; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6848 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6849 | /* If arguments are specified, check that they actually exist within |
| 6850 | ** the archive before proceeding. And formulate a WHERE clause to |
| 6851 | ** match them. */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6852 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6853 | arWhereClause(&rc, pAr, &zWhere); |
| 6854 | |
| 6855 | if( rc==SQLITE_OK ){ |
| 6856 | if( pAr->zDir ){ |
| 6857 | zDir = sqlite3_mprintf("%s/", pAr->zDir); |
| 6858 | }else{ |
| 6859 | zDir = sqlite3_mprintf(""); |
| 6860 | } |
| 6861 | if( zDir==0 ) rc = SQLITE_NOMEM; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6862 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6863 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6864 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, |
| 6865 | azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6866 | ); |
| 6867 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6868 | if( rc==SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6869 | j = sqlite3_bind_parameter_index(pSql, "$dir"); |
| 6870 | sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6871 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6872 | /* Run the SELECT statement twice. The first time, writefile() is called |
| 6873 | ** for all archive members that should be extracted. The second time, |
| 6874 | ** only for the directories. This is because the timestamps for |
| 6875 | ** extracted directories must be reset after they are populated (as |
| 6876 | ** populating them changes the timestamp). */ |
| 6877 | for(i=0; i<2; i++){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6878 | j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); |
| 6879 | sqlite3_bind_int(pSql, j, i); |
| 6880 | if( pAr->bDryRun ){ |
| 6881 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 6882 | }else{ |
| 6883 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 6884 | if( i==0 && pAr->bVerbose ){ |
| 6885 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 6886 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6887 | } |
| 6888 | } |
| 6889 | shellReset(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6890 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6891 | shellFinalize(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6892 | } |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6893 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6894 | sqlite3_free(zDir); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6895 | sqlite3_free(zWhere); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6896 | return rc; |
| 6897 | } |
| 6898 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6899 | /* |
| 6900 | ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. |
| 6901 | */ |
| 6902 | static int arExecSql(ArCommand *pAr, const char *zSql){ |
| 6903 | int rc; |
| 6904 | if( pAr->bDryRun ){ |
| 6905 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 6906 | rc = SQLITE_OK; |
| 6907 | }else{ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6908 | char *zErr = 0; |
| 6909 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 6910 | if( zErr ){ |
| 6911 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 6912 | sqlite3_free(zErr); |
| 6913 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6914 | } |
| 6915 | return rc; |
| 6916 | } |
| 6917 | |
dan | 1ad3f61 | 2017-12-11 20:22:02 +0000 | [diff] [blame] | 6918 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6919 | /* |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6920 | ** Implementation of .ar "create", "insert", and "update" commands. |
| 6921 | ** |
| 6922 | ** create -> Create a new SQL archive |
| 6923 | ** insert -> Insert or reinsert all files listed |
| 6924 | ** update -> Insert files that have changed or that were not |
| 6925 | ** previously in the archive |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6926 | ** |
| 6927 | ** Create the "sqlar" table in the database if it does not already exist. |
| 6928 | ** Then add each file in the azFile[] array to the archive. Directories |
| 6929 | ** are added recursively. If argument bVerbose is non-zero, a message is |
| 6930 | ** printed on stdout for each file archived. |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 6931 | ** |
| 6932 | ** The create command is the same as update, except that it drops |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6933 | ** any existing "sqlar" table before beginning. The "insert" command |
| 6934 | ** always overwrites every file named on the command-line, where as |
| 6935 | ** "update" only overwrites if the size or mtime or mode has changed. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6936 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6937 | static int arCreateOrUpdateCommand( |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 6938 | ArCommand *pAr, /* Command arguments and options */ |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6939 | int bUpdate, /* true for a --create. */ |
| 6940 | int bOnlyIfChanged /* Only update if file has changed */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6941 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6942 | const char *zCreate = |
drh | afba180 | 2018-01-06 15:49:57 +0000 | [diff] [blame] | 6943 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 6944 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 6945 | " mode INT, -- access permissions\n" |
| 6946 | " mtime INT, -- last modification time\n" |
| 6947 | " sz INT, -- original file size\n" |
| 6948 | " data BLOB -- compressed content\n" |
| 6949 | ")"; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6950 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6951 | const char *zInsertFmt[2] = { |
| 6952 | "REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 6953 | " SELECT\n" |
| 6954 | " %s,\n" |
| 6955 | " mode,\n" |
| 6956 | " mtime,\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6957 | " CASE substr(lsmode(mode),1,1)\n" |
| 6958 | " WHEN '-' THEN length(data)\n" |
| 6959 | " WHEN 'd' THEN 0\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 6960 | " ELSE -1 END,\n" |
drh | 69d2d35 | 2018-03-09 22:18:53 +0000 | [diff] [blame] | 6961 | " sqlar_compress(data)\n" |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6962 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 6963 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 6964 | , |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6965 | "REPLACE INTO %s(name,mode,mtime,data)\n" |
| 6966 | " SELECT\n" |
| 6967 | " %s,\n" |
| 6968 | " mode,\n" |
| 6969 | " mtime,\n" |
| 6970 | " data\n" |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6971 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 6972 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6973 | }; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6974 | int i; /* For iterating through azFile[] */ |
| 6975 | int rc; /* Return code */ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6976 | const char *zTab = 0; /* SQL table into which to insert */ |
| 6977 | char *zSql; |
| 6978 | char zTemp[50]; |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6979 | char *zExists = 0; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6980 | |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6981 | arExecSql(pAr, "PRAGMA page_size=512"); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6982 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6983 | if( rc!=SQLITE_OK ) return rc; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 6984 | zTemp[0] = 0; |
| 6985 | if( pAr->bZip ){ |
| 6986 | /* Initialize the zipfile virtual table, if necessary */ |
| 6987 | if( pAr->zFile ){ |
| 6988 | sqlite3_uint64 r; |
| 6989 | sqlite3_randomness(sizeof(r),&r); |
| 6990 | sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
| 6991 | zTab = zTemp; |
| 6992 | zSql = sqlite3_mprintf( |
| 6993 | "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
| 6994 | zTab, pAr->zFile |
| 6995 | ); |
| 6996 | rc = arExecSql(pAr, zSql); |
| 6997 | sqlite3_free(zSql); |
| 6998 | }else{ |
| 6999 | zTab = "zip"; |
| 7000 | } |
| 7001 | }else{ |
| 7002 | /* Initialize the table for an SQLAR */ |
| 7003 | zTab = "sqlar"; |
| 7004 | if( bUpdate==0 ){ |
| 7005 | rc = arExecSql(pAr, zDrop); |
| 7006 | if( rc!=SQLITE_OK ) goto end_ar_transaction; |
| 7007 | } |
| 7008 | rc = arExecSql(pAr, zCreate); |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 7009 | } |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7010 | if( bOnlyIfChanged ){ |
| 7011 | zExists = sqlite3_mprintf( |
| 7012 | " AND NOT EXISTS(" |
| 7013 | "SELECT 1 FROM %s AS mem" |
| 7014 | " WHERE mem.name=disk.name" |
| 7015 | " AND mem.mtime=disk.mtime" |
| 7016 | " AND mem.mode=disk.mode)", zTab); |
| 7017 | }else{ |
| 7018 | zExists = sqlite3_mprintf(""); |
| 7019 | } |
| 7020 | if( zExists==0 ) rc = SQLITE_NOMEM; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7021 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 7022 | char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7023 | pAr->bVerbose ? "shell_putsnl(name)" : "name", |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7024 | pAr->azArg[i], pAr->zDir, zExists); |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 7025 | rc = arExecSql(pAr, zSql2); |
| 7026 | sqlite3_free(zSql2); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7027 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7028 | end_ar_transaction: |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7029 | if( rc!=SQLITE_OK ){ |
drh | 2bd207f | 2019-01-11 17:19:59 +0000 | [diff] [blame] | 7030 | sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7031 | }else{ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7032 | rc = arExecSql(pAr, "RELEASE ar;"); |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7033 | if( pAr->bZip && pAr->zFile ){ |
| 7034 | zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); |
| 7035 | arExecSql(pAr, zSql); |
| 7036 | sqlite3_free(zSql); |
| 7037 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7038 | } |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7039 | sqlite3_free(zExists); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7040 | return rc; |
| 7041 | } |
| 7042 | |
| 7043 | /* |
| 7044 | ** Implementation of ".ar" dot command. |
| 7045 | */ |
| 7046 | static int arDotCommand( |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7047 | ShellState *pState, /* Current shell tool state */ |
| 7048 | int fromCmdLine, /* True if -A command-line option, not .ar cmd */ |
| 7049 | char **azArg, /* Array of arguments passed to dot command */ |
| 7050 | int nArg /* Number of entries in azArg[] */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7051 | ){ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7052 | ArCommand cmd; |
| 7053 | int rc; |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 7054 | memset(&cmd, 0, sizeof(cmd)); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 7055 | cmd.fromCmdLine = fromCmdLine; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7056 | rc = arParseCommand(azArg, nArg, &cmd); |
| 7057 | if( rc==SQLITE_OK ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7058 | int eDbType = SHELL_OPEN_UNSPEC; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7059 | cmd.p = pState; |
| 7060 | cmd.db = pState->db; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7061 | if( cmd.zFile ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7062 | eDbType = deduceDatabaseType(cmd.zFile, 1); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7063 | }else{ |
| 7064 | eDbType = pState->openMode; |
| 7065 | } |
| 7066 | if( eDbType==SHELL_OPEN_ZIPFILE ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7067 | if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ |
| 7068 | if( cmd.zFile==0 ){ |
| 7069 | cmd.zSrcTable = sqlite3_mprintf("zip"); |
| 7070 | }else{ |
| 7071 | cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); |
| 7072 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 7073 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7074 | cmd.bZip = 1; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 7075 | }else if( cmd.zFile ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7076 | int flags; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7077 | if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7078 | if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 7079 | || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7080 | flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 7081 | }else{ |
| 7082 | flags = SQLITE_OPEN_READONLY; |
| 7083 | } |
drh | a82c95b | 2018-01-10 14:00:00 +0000 | [diff] [blame] | 7084 | cmd.db = 0; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7085 | if( cmd.bDryRun ){ |
| 7086 | utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, |
| 7087 | eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); |
| 7088 | } |
| 7089 | rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, |
| 7090 | eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7091 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7092 | utf8_printf(stderr, "cannot open file: %s (%s)\n", |
| 7093 | cmd.zFile, sqlite3_errmsg(cmd.db) |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7094 | ); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7095 | goto end_ar_command; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7096 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7097 | sqlite3_fileio_init(cmd.db, 0, 0); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7098 | sqlite3_sqlar_init(cmd.db, 0, 0); |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 7099 | sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, |
| 7100 | shellPutsFunc, 0, 0); |
| 7101 | |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7102 | } |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 7103 | if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7104 | if( cmd.eCmd!=AR_CMD_CREATE |
| 7105 | && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) |
| 7106 | ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7107 | utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); |
| 7108 | rc = SQLITE_ERROR; |
| 7109 | goto end_ar_command; |
| 7110 | } |
| 7111 | cmd.zSrcTable = sqlite3_mprintf("sqlar"); |
| 7112 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7113 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7114 | switch( cmd.eCmd ){ |
| 7115 | case AR_CMD_CREATE: |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7116 | rc = arCreateOrUpdateCommand(&cmd, 0, 0); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7117 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7118 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7119 | case AR_CMD_EXTRACT: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7120 | rc = arExtractCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7121 | break; |
| 7122 | |
| 7123 | case AR_CMD_LIST: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7124 | rc = arListCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7125 | break; |
| 7126 | |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 7127 | case AR_CMD_HELP: |
| 7128 | arUsage(pState->out); |
| 7129 | break; |
| 7130 | |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7131 | case AR_CMD_INSERT: |
| 7132 | rc = arCreateOrUpdateCommand(&cmd, 1, 0); |
| 7133 | break; |
| 7134 | |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 7135 | case AR_CMD_REMOVE: |
| 7136 | rc = arRemoveCommand(&cmd); |
| 7137 | break; |
| 7138 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7139 | default: |
| 7140 | assert( cmd.eCmd==AR_CMD_UPDATE ); |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7141 | rc = arCreateOrUpdateCommand(&cmd, 1, 1); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7142 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7143 | } |
| 7144 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7145 | end_ar_command: |
| 7146 | if( cmd.db!=pState->db ){ |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7147 | close_db(cmd.db); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7148 | } |
| 7149 | sqlite3_free(cmd.zSrcTable); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7150 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7151 | return rc; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7152 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 7153 | /* End of the ".archive" or ".ar" command logic |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7154 | *******************************************************************************/ |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 7155 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7156 | |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 7157 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7158 | /* |
| 7159 | ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op. |
| 7160 | ** Otherwise, the SQL statement or statements in zSql are executed using |
| 7161 | ** database connection db and the error code written to *pRc before |
| 7162 | ** this function returns. |
| 7163 | */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7164 | static void shellExec(sqlite3 *db, int *pRc, const char *zSql){ |
| 7165 | int rc = *pRc; |
| 7166 | if( rc==SQLITE_OK ){ |
| 7167 | char *zErr = 0; |
| 7168 | rc = sqlite3_exec(db, zSql, 0, 0, &zErr); |
| 7169 | if( rc!=SQLITE_OK ){ |
| 7170 | raw_printf(stderr, "SQL error: %s\n", zErr); |
| 7171 | } |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 7172 | sqlite3_free(zErr); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7173 | *pRc = rc; |
| 7174 | } |
| 7175 | } |
| 7176 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7177 | /* |
| 7178 | ** Like shellExec(), except that zFmt is a printf() style format string. |
| 7179 | */ |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7180 | static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){ |
| 7181 | char *z = 0; |
| 7182 | if( *pRc==SQLITE_OK ){ |
| 7183 | va_list ap; |
| 7184 | va_start(ap, zFmt); |
| 7185 | z = sqlite3_vmprintf(zFmt, ap); |
| 7186 | va_end(ap); |
| 7187 | if( z==0 ){ |
| 7188 | *pRc = SQLITE_NOMEM; |
| 7189 | }else{ |
| 7190 | shellExec(db, pRc, z); |
| 7191 | } |
| 7192 | sqlite3_free(z); |
| 7193 | } |
| 7194 | } |
| 7195 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7196 | /* |
| 7197 | ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| 7198 | ** Otherwise, an attempt is made to allocate, zero and return a pointer |
| 7199 | ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set |
| 7200 | ** to SQLITE_NOMEM and NULL returned. |
| 7201 | */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7202 | static void *shellMalloc(int *pRc, sqlite3_int64 nByte){ |
| 7203 | void *pRet = 0; |
| 7204 | if( *pRc==SQLITE_OK ){ |
| 7205 | pRet = sqlite3_malloc64(nByte); |
| 7206 | if( pRet==0 ){ |
| 7207 | *pRc = SQLITE_NOMEM; |
| 7208 | }else{ |
| 7209 | memset(pRet, 0, nByte); |
| 7210 | } |
| 7211 | } |
| 7212 | return pRet; |
| 7213 | } |
| 7214 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7215 | /* |
| 7216 | ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| 7217 | ** Otherwise, zFmt is treated as a printf() style string. The result of |
| 7218 | ** formatting it along with any trailing arguments is written into a |
| 7219 | ** buffer obtained from sqlite3_malloc(), and pointer to which is returned. |
| 7220 | ** It is the responsibility of the caller to eventually free this buffer |
| 7221 | ** using a call to sqlite3_free(). |
| 7222 | ** |
| 7223 | ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL |
| 7224 | ** pointer returned. |
| 7225 | */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7226 | static char *shellMPrintf(int *pRc, const char *zFmt, ...){ |
| 7227 | char *z = 0; |
| 7228 | if( *pRc==SQLITE_OK ){ |
| 7229 | va_list ap; |
| 7230 | va_start(ap, zFmt); |
| 7231 | z = sqlite3_vmprintf(zFmt, ap); |
| 7232 | va_end(ap); |
| 7233 | if( z==0 ){ |
| 7234 | *pRc = SQLITE_NOMEM; |
| 7235 | } |
| 7236 | } |
| 7237 | return z; |
| 7238 | } |
| 7239 | |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 7240 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7241 | /* |
| 7242 | ** When running the ".recover" command, each output table, and the special |
| 7243 | ** orphaned row table if it is required, is represented by an instance |
| 7244 | ** of the following struct. |
| 7245 | */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7246 | typedef struct RecoverTable RecoverTable; |
| 7247 | struct RecoverTable { |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7248 | char *zQuoted; /* Quoted version of table name */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7249 | int nCol; /* Number of columns in table */ |
| 7250 | char **azlCol; /* Array of column lists */ |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7251 | int iPk; /* Index of IPK column */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7252 | }; |
| 7253 | |
| 7254 | /* |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7255 | ** Free a RecoverTable object allocated by recoverFindTable() or |
| 7256 | ** recoverOrphanTable(). |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7257 | */ |
| 7258 | static void recoverFreeTable(RecoverTable *pTab){ |
| 7259 | if( pTab ){ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7260 | sqlite3_free(pTab->zQuoted); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7261 | if( pTab->azlCol ){ |
| 7262 | int i; |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7263 | for(i=0; i<=pTab->nCol; i++){ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7264 | sqlite3_free(pTab->azlCol[i]); |
| 7265 | } |
| 7266 | sqlite3_free(pTab->azlCol); |
| 7267 | } |
| 7268 | sqlite3_free(pTab); |
| 7269 | } |
| 7270 | } |
| 7271 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7272 | /* |
| 7273 | ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. |
| 7274 | ** Otherwise, it allocates and returns a RecoverTable object based on the |
| 7275 | ** final four arguments passed to this function. It is the responsibility |
| 7276 | ** of the caller to eventually free the returned object using |
| 7277 | ** recoverFreeTable(). |
| 7278 | */ |
| 7279 | static RecoverTable *recoverNewTable( |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7280 | int *pRc, /* IN/OUT: Error code */ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7281 | const char *zName, /* Name of table */ |
| 7282 | const char *zSql, /* CREATE TABLE statement */ |
| 7283 | int bIntkey, |
| 7284 | int nCol |
| 7285 | ){ |
| 7286 | sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */ |
| 7287 | int rc = *pRc; |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7288 | RecoverTable *pTab = 0; |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7289 | |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7290 | pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable)); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7291 | if( rc==SQLITE_OK ){ |
| 7292 | int nSqlCol = 0; |
| 7293 | int bSqlIntkey = 0; |
| 7294 | sqlite3_stmt *pStmt = 0; |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7295 | |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7296 | rc = sqlite3_open("", &dbtmp); |
| 7297 | if( rc==SQLITE_OK ){ |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7298 | sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0, |
| 7299 | shellIdQuote, 0, 0); |
| 7300 | } |
| 7301 | if( rc==SQLITE_OK ){ |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7302 | rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0); |
| 7303 | } |
| 7304 | if( rc==SQLITE_OK ){ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7305 | rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0); |
| 7306 | if( rc==SQLITE_ERROR ){ |
| 7307 | rc = SQLITE_OK; |
| 7308 | goto finished; |
| 7309 | } |
| 7310 | } |
| 7311 | shellPreparePrintf(dbtmp, &rc, &pStmt, |
| 7312 | "SELECT count(*) FROM pragma_table_info(%Q)", zName |
| 7313 | ); |
| 7314 | if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7315 | nSqlCol = sqlite3_column_int(pStmt, 0); |
| 7316 | } |
| 7317 | shellFinalize(&rc, pStmt); |
| 7318 | |
| 7319 | if( rc!=SQLITE_OK || nSqlCol<nCol ){ |
| 7320 | goto finished; |
| 7321 | } |
| 7322 | |
| 7323 | shellPreparePrintf(dbtmp, &rc, &pStmt, |
| 7324 | "SELECT (" |
| 7325 | " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 7326 | ") FROM sqlite_schema WHERE name = %Q", zName |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7327 | ); |
| 7328 | if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7329 | bSqlIntkey = sqlite3_column_int(pStmt, 0); |
| 7330 | } |
| 7331 | shellFinalize(&rc, pStmt); |
| 7332 | |
| 7333 | if( bIntkey==bSqlIntkey ){ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7334 | int i; |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7335 | const char *zPk = "_rowid_"; |
| 7336 | sqlite3_stmt *pPkFinder = 0; |
| 7337 | |
dan | f57bea3 | 2019-04-27 15:35:45 +0000 | [diff] [blame] | 7338 | /* If this is an intkey table and there is an INTEGER PRIMARY KEY, |
| 7339 | ** set zPk to the name of the PK column, and pTab->iPk to the index |
| 7340 | ** of the column, where columns are 0-numbered from left to right. |
| 7341 | ** Or, if this is a WITHOUT ROWID table or if there is no IPK column, |
| 7342 | ** leave zPk as "_rowid_" and pTab->iPk at -2. */ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7343 | pTab->iPk = -2; |
| 7344 | if( bIntkey ){ |
| 7345 | shellPreparePrintf(dbtmp, &rc, &pPkFinder, |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7346 | "SELECT cid, name FROM pragma_table_info(%Q) " |
| 7347 | " WHERE pk=1 AND type='integer' COLLATE nocase" |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7348 | " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)" |
| 7349 | , zName, zName |
| 7350 | ); |
| 7351 | if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){ |
| 7352 | pTab->iPk = sqlite3_column_int(pPkFinder, 0); |
| 7353 | zPk = (const char*)sqlite3_column_text(pPkFinder, 1); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 7354 | if( zPk==0 ){ zPk = "_"; /* Defensive. Should never happen */ } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7355 | } |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7356 | } |
| 7357 | |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7358 | pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7359 | pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1)); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7360 | pTab->nCol = nSqlCol; |
| 7361 | |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7362 | if( bIntkey ){ |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7363 | pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7364 | }else{ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7365 | pTab->azlCol[0] = shellMPrintf(&rc, ""); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7366 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7367 | i = 1; |
| 7368 | shellPreparePrintf(dbtmp, &rc, &pStmt, |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7369 | "SELECT %Q || group_concat(shell_idquote(name), ', ') " |
dan | f57bea3 | 2019-04-27 15:35:45 +0000 | [diff] [blame] | 7370 | " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) " |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7371 | "FROM pragma_table_info(%Q)", |
dan | f57bea3 | 2019-04-27 15:35:45 +0000 | [diff] [blame] | 7372 | bIntkey ? ", " : "", pTab->iPk, |
| 7373 | bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ", |
| 7374 | zName |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7375 | ); |
| 7376 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7377 | const char *zText = (const char*)sqlite3_column_text(pStmt, 0); |
| 7378 | pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText); |
| 7379 | i++; |
| 7380 | } |
| 7381 | shellFinalize(&rc, pStmt); |
| 7382 | |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7383 | shellFinalize(&rc, pPkFinder); |
| 7384 | } |
| 7385 | } |
| 7386 | |
| 7387 | finished: |
| 7388 | sqlite3_close(dbtmp); |
| 7389 | *pRc = rc; |
dan | 9877965 | 2019-05-09 14:15:19 +0000 | [diff] [blame] | 7390 | if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7391 | recoverFreeTable(pTab); |
| 7392 | pTab = 0; |
| 7393 | } |
| 7394 | return pTab; |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
dan | 0aa01ee | 2019-04-27 19:36:49 +0000 | [diff] [blame] | 7397 | /* |
| 7398 | ** This function is called to search the schema recovered from the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 7399 | ** sqlite_schema table of the (possibly) corrupt database as part |
dan | 0aa01ee | 2019-04-27 19:36:49 +0000 | [diff] [blame] | 7400 | ** of a ".recover" command. Specifically, for a table with root page |
| 7401 | ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the |
| 7402 | ** table must be a WITHOUT ROWID table, or if non-zero, not one of |
| 7403 | ** those. |
| 7404 | ** |
| 7405 | ** If a table is found, a (RecoverTable*) object is returned. Or, if |
| 7406 | ** no such table is found, but bIntkey is false and iRoot is the |
| 7407 | ** root page of an index in the recovered schema, then (*pbNoop) is |
| 7408 | ** set to true and NULL returned. Or, if there is no such table or |
| 7409 | ** index, NULL is returned and (*pbNoop) set to 0, indicating that |
| 7410 | ** the caller should write data to the orphans table. |
| 7411 | */ |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7412 | static RecoverTable *recoverFindTable( |
dan | 0aa01ee | 2019-04-27 19:36:49 +0000 | [diff] [blame] | 7413 | ShellState *pState, /* Shell state object */ |
| 7414 | int *pRc, /* IN/OUT: Error code */ |
| 7415 | int iRoot, /* Root page of table */ |
| 7416 | int bIntkey, /* True for an intkey table */ |
| 7417 | int nCol, /* Number of columns in table */ |
| 7418 | int *pbNoop /* OUT: True if iRoot is root of index */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7419 | ){ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7420 | sqlite3_stmt *pStmt = 0; |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7421 | RecoverTable *pRet = 0; |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7422 | int bNoop = 0; |
| 7423 | const char *zSql = 0; |
| 7424 | const char *zName = 0; |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7425 | |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7426 | /* Search the recovered schema for an object with root page iRoot. */ |
| 7427 | shellPreparePrintf(pState->db, pRc, &pStmt, |
| 7428 | "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot |
| 7429 | ); |
| 7430 | while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7431 | const char *zType = (const char*)sqlite3_column_text(pStmt, 0); |
| 7432 | if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){ |
| 7433 | bNoop = 1; |
| 7434 | break; |
| 7435 | } |
| 7436 | if( sqlite3_stricmp(zType, "table")==0 ){ |
| 7437 | zName = (const char*)sqlite3_column_text(pStmt, 1); |
| 7438 | zSql = (const char*)sqlite3_column_text(pStmt, 2); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 7439 | if( zName!=0 && zSql!=0 ){ |
| 7440 | pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol); |
| 7441 | break; |
| 7442 | } |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7443 | } |
| 7444 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7445 | |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7446 | shellFinalize(pRc, pStmt); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7447 | *pbNoop = bNoop; |
| 7448 | return pRet; |
| 7449 | } |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7450 | |
dan | 0aa01ee | 2019-04-27 19:36:49 +0000 | [diff] [blame] | 7451 | /* |
| 7452 | ** Return a RecoverTable object representing the orphans table. |
| 7453 | */ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7454 | static RecoverTable *recoverOrphanTable( |
dan | 0aa01ee | 2019-04-27 19:36:49 +0000 | [diff] [blame] | 7455 | ShellState *pState, /* Shell state object */ |
| 7456 | int *pRc, /* IN/OUT: Error code */ |
| 7457 | const char *zLostAndFound, /* Base name for orphans table */ |
| 7458 | int nCol /* Number of user data columns */ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7459 | ){ |
| 7460 | RecoverTable *pTab = 0; |
| 7461 | if( nCol>=0 && *pRc==SQLITE_OK ){ |
| 7462 | int i; |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7463 | |
| 7464 | /* This block determines the name of the orphan table. The prefered |
| 7465 | ** name is zLostAndFound. But if that clashes with another name |
| 7466 | ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1 |
| 7467 | ** and so on until a non-clashing name is found. */ |
| 7468 | int iTab = 0; |
| 7469 | char *zTab = shellMPrintf(pRc, "%s", zLostAndFound); |
| 7470 | sqlite3_stmt *pTest = 0; |
| 7471 | shellPrepare(pState->db, pRc, |
| 7472 | "SELECT 1 FROM recovery.schema WHERE name=?", &pTest |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7473 | ); |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7474 | if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); |
| 7475 | while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){ |
| 7476 | shellReset(pRc, pTest); |
| 7477 | sqlite3_free(zTab); |
| 7478 | zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++); |
| 7479 | sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7480 | } |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7481 | shellFinalize(pRc, pTest); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7482 | |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7483 | pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable)); |
| 7484 | if( pTab ){ |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7485 | pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7486 | pTab->nCol = nCol; |
| 7487 | pTab->iPk = -2; |
| 7488 | if( nCol>0 ){ |
| 7489 | pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1)); |
| 7490 | if( pTab->azlCol ){ |
| 7491 | pTab->azlCol[nCol] = shellMPrintf(pRc, ""); |
| 7492 | for(i=nCol-1; i>=0; i--){ |
| 7493 | pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]); |
| 7494 | } |
| 7495 | } |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7496 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7497 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7498 | if( *pRc!=SQLITE_OK ){ |
| 7499 | recoverFreeTable(pTab); |
| 7500 | pTab = 0; |
| 7501 | }else{ |
| 7502 | raw_printf(pState->out, |
| 7503 | "CREATE TABLE %s(rootpgno INTEGER, " |
| 7504 | "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted |
| 7505 | ); |
| 7506 | for(i=0; i<nCol; i++){ |
| 7507 | raw_printf(pState->out, ", c%d", i); |
| 7508 | } |
| 7509 | raw_printf(pState->out, ");\n"); |
| 7510 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7511 | } |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7512 | sqlite3_free(zTab); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7513 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7514 | return pTab; |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
| 7517 | /* |
| 7518 | ** This function is called to recover data from the database. A script |
| 7519 | ** to construct a new database containing all recovered data is output |
| 7520 | ** on stream pState->out. |
| 7521 | */ |
dan | b9b71db | 2019-04-25 16:20:40 +0000 | [diff] [blame] | 7522 | static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7523 | int rc = SQLITE_OK; |
| 7524 | sqlite3_stmt *pLoop = 0; /* Loop through all root pages */ |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7525 | sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */ |
| 7526 | sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */ |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7527 | const char *zRecoveryDb = ""; /* Name of "recovery" database */ |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7528 | const char *zLostAndFound = "lost_and_found"; |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7529 | int i; |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7530 | int nOrphan = -1; |
| 7531 | RecoverTable *pOrphan = 0; |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7532 | |
| 7533 | int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7534 | int bRowids = 1; /* 0 if --no-rowids */ |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7535 | for(i=1; i<nArg; i++){ |
| 7536 | char *z = azArg[i]; |
| 7537 | int n; |
| 7538 | if( z[0]=='-' && z[1]=='-' ) z++; |
drh | 4245e04 | 2019-06-13 13:52:46 +0000 | [diff] [blame] | 7539 | n = strlen30(z); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7540 | if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ |
| 7541 | bFreelist = 0; |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7542 | }else |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7543 | if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ |
| 7544 | i++; |
| 7545 | zRecoveryDb = azArg[i]; |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7546 | }else |
| 7547 | if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ |
| 7548 | i++; |
| 7549 | zLostAndFound = azArg[i]; |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7550 | }else |
| 7551 | if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ |
| 7552 | bRowids = 0; |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7553 | } |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7554 | else{ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7555 | utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); |
| 7556 | showHelp(pState->out, azArg[0]); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7557 | return 1; |
| 7558 | } |
| 7559 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7560 | |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7561 | shellExecPrintf(pState->db, &rc, |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7562 | /* Attach an in-memory database named 'recovery'. Create an indexed |
| 7563 | ** cache of the sqlite_dbptr virtual table. */ |
dan | 01c08bc | 2019-07-24 19:20:30 +0000 | [diff] [blame] | 7564 | "PRAGMA writable_schema = on;" |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7565 | "ATTACH %Q AS recovery;" |
| 7566 | "DROP TABLE IF EXISTS recovery.dbptr;" |
| 7567 | "DROP TABLE IF EXISTS recovery.freelist;" |
| 7568 | "DROP TABLE IF EXISTS recovery.map;" |
| 7569 | "DROP TABLE IF EXISTS recovery.schema;" |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7570 | "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7571 | ); |
| 7572 | |
| 7573 | if( bFreelist ){ |
| 7574 | shellExec(pState->db, &rc, |
| 7575 | "WITH trunk(pgno) AS (" |
| 7576 | " SELECT shell_int32(" |
| 7577 | " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x " |
| 7578 | " WHERE x>0" |
| 7579 | " UNION" |
| 7580 | " SELECT shell_int32(" |
| 7581 | " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x " |
| 7582 | " FROM trunk WHERE x>0" |
| 7583 | ")," |
| 7584 | "freelist(data, n, freepgno) AS (" |
dan | f6099e9 | 2019-05-09 16:57:39 +0000 | [diff] [blame] | 7585 | " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno " |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7586 | " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno" |
| 7587 | " UNION ALL" |
| 7588 | " SELECT data, n-1, shell_int32(data, 2+n) " |
| 7589 | " FROM freelist WHERE n>=0" |
| 7590 | ")" |
| 7591 | "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;" |
| 7592 | ); |
| 7593 | } |
| 7594 | |
dan | 95063c2 | 2019-07-24 08:15:09 +0000 | [diff] [blame] | 7595 | /* If this is an auto-vacuum database, add all pointer-map pages to |
| 7596 | ** the freelist table. Do this regardless of whether or not |
| 7597 | ** --freelist-corrupt was specified. */ |
| 7598 | shellExec(pState->db, &rc, |
| 7599 | "WITH ptrmap(pgno) AS (" |
| 7600 | " SELECT 2 WHERE shell_int32(" |
| 7601 | " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13" |
| 7602 | " )" |
| 7603 | " UNION ALL " |
| 7604 | " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp " |
| 7605 | " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)" |
| 7606 | ")" |
| 7607 | "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap" |
| 7608 | ); |
| 7609 | |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7610 | shellExec(pState->db, &rc, |
dan | ca42438 | 2019-04-26 15:40:27 +0000 | [diff] [blame] | 7611 | "CREATE TABLE recovery.dbptr(" |
| 7612 | " pgno, child, PRIMARY KEY(child, pgno)" |
| 7613 | ") WITHOUT ROWID;" |
| 7614 | "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) " |
| 7615 | " SELECT * FROM sqlite_dbptr" |
| 7616 | " WHERE pgno NOT IN freelist AND child NOT IN freelist;" |
| 7617 | |
| 7618 | /* Delete any pointer to page 1. This ensures that page 1 is considered |
| 7619 | ** a root page, regardless of how corrupt the db is. */ |
| 7620 | "DELETE FROM recovery.dbptr WHERE child = 1;" |
| 7621 | |
| 7622 | /* Delete all pointers to any pages that have more than one pointer |
| 7623 | ** to them. Such pages will be treated as root pages when recovering |
| 7624 | ** data. */ |
| 7625 | "DELETE FROM recovery.dbptr WHERE child IN (" |
| 7626 | " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1" |
| 7627 | ");" |
| 7628 | |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7629 | /* Create the "map" table that will (eventually) contain instructions |
| 7630 | ** for dealing with each page in the db that contains one or more |
| 7631 | ** records. */ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7632 | "CREATE TABLE recovery.map(" |
| 7633 | "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT" |
| 7634 | ");" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7635 | |
| 7636 | /* Populate table [map]. If there are circular loops of pages in the |
| 7637 | ** database, the following adds all pages in such a loop to the map |
| 7638 | ** as individual root pages. This could be handled better. */ |
| 7639 | "WITH pages(i, maxlen) AS (" |
dan | b9b71db | 2019-04-25 16:20:40 +0000 | [diff] [blame] | 7640 | " SELECT page_count, (" |
| 7641 | " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count" |
dan | 13b8767 | 2019-05-09 11:45:21 +0000 | [diff] [blame] | 7642 | " ) FROM pragma_page_count WHERE page_count>0" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7643 | " UNION ALL" |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7644 | " SELECT i-1, (" |
| 7645 | " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1" |
| 7646 | " ) FROM pages WHERE i>=2" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7647 | ")" |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7648 | "INSERT INTO recovery.map(pgno, maxlen, intkey, root) " |
| 7649 | " SELECT i, maxlen, NULL, (" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7650 | " WITH p(orig, pgno, parent) AS (" |
| 7651 | " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)" |
dan | 39e04f8 | 2019-05-09 18:33:32 +0000 | [diff] [blame] | 7652 | " UNION " |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7653 | " SELECT i, p.parent, " |
| 7654 | " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p" |
| 7655 | " )" |
| 7656 | " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)" |
| 7657 | ") " |
dan | d790c9a | 2019-08-26 14:57:58 +0000 | [diff] [blame] | 7658 | "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;" |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7659 | "UPDATE recovery.map AS o SET intkey = (" |
| 7660 | " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno" |
| 7661 | ");" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7662 | |
| 7663 | /* Extract data from page 1 and any linked pages into table |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 7664 | ** recovery.schema. With the same schema as an sqlite_schema table. */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7665 | "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);" |
| 7666 | "INSERT INTO recovery.schema SELECT " |
| 7667 | " max(CASE WHEN field=0 THEN value ELSE NULL END)," |
| 7668 | " max(CASE WHEN field=1 THEN value ELSE NULL END)," |
| 7669 | " max(CASE WHEN field=2 THEN value ELSE NULL END)," |
| 7670 | " max(CASE WHEN field=3 THEN value ELSE NULL END)," |
| 7671 | " max(CASE WHEN field=4 THEN value ELSE NULL END)" |
| 7672 | "FROM sqlite_dbdata WHERE pgno IN (" |
| 7673 | " SELECT pgno FROM recovery.map WHERE root=1" |
| 7674 | ")" |
| 7675 | "GROUP BY pgno, cell;" |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7676 | "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);" |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7677 | ); |
| 7678 | |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7679 | /* Open a transaction, then print out all non-virtual, non-"sqlite_%" |
| 7680 | ** CREATE TABLE statements that extracted from the existing schema. */ |
| 7681 | if( rc==SQLITE_OK ){ |
| 7682 | sqlite3_stmt *pStmt = 0; |
dan | f321057 | 2019-08-06 18:40:36 +0000 | [diff] [blame] | 7683 | /* ".recover" might output content in an order which causes immediate |
| 7684 | ** foreign key constraints to be violated. So disable foreign-key |
| 7685 | ** constraint enforcement to prevent problems when running the output |
| 7686 | ** script. */ |
| 7687 | raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n"); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7688 | raw_printf(pState->out, "BEGIN;\n"); |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7689 | raw_printf(pState->out, "PRAGMA writable_schema = on;\n"); |
| 7690 | shellPrepare(pState->db, &rc, |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7691 | "SELECT sql FROM recovery.schema " |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7692 | "WHERE type='table' AND sql LIKE 'create table%'", &pStmt |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7693 | ); |
| 7694 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7695 | const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0); |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7696 | raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", |
| 7697 | &zCreateTable[12] |
| 7698 | ); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7699 | } |
| 7700 | shellFinalize(&rc, pStmt); |
| 7701 | } |
| 7702 | |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7703 | /* Figure out if an orphan table will be required. And if so, how many |
| 7704 | ** user columns it should contain */ |
| 7705 | shellPrepare(pState->db, &rc, |
dan | 9877965 | 2019-05-09 14:15:19 +0000 | [diff] [blame] | 7706 | "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1" |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7707 | , &pLoop |
| 7708 | ); |
| 7709 | if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ |
| 7710 | nOrphan = sqlite3_column_int(pLoop, 0); |
| 7711 | } |
| 7712 | shellFinalize(&rc, pLoop); |
| 7713 | pLoop = 0; |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7714 | |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7715 | shellPrepare(pState->db, &rc, |
| 7716 | "SELECT pgno FROM recovery.map WHERE root=?", &pPages |
| 7717 | ); |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7718 | |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7719 | shellPrepare(pState->db, &rc, |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7720 | "SELECT max(field), group_concat(shell_escape_crnl(quote" |
| 7721 | "(case when (? AND field<0) then NULL else value end)" |
| 7722 | "), ', ')" |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7723 | ", min(field) " |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7724 | "FROM sqlite_dbdata WHERE pgno = ? AND field != ?" |
| 7725 | "GROUP BY cell", &pCells |
| 7726 | ); |
| 7727 | |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7728 | /* Loop through each root page. */ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7729 | shellPrepare(pState->db, &rc, |
| 7730 | "SELECT root, intkey, max(maxlen) FROM recovery.map" |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7731 | " WHERE root>1 GROUP BY root, intkey ORDER BY root=(" |
| 7732 | " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'" |
| 7733 | ")", &pLoop |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7734 | ); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7735 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ |
| 7736 | int iRoot = sqlite3_column_int(pLoop, 0); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7737 | int bIntkey = sqlite3_column_int(pLoop, 1); |
| 7738 | int nCol = sqlite3_column_int(pLoop, 2); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7739 | int bNoop = 0; |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7740 | RecoverTable *pTab; |
| 7741 | |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7742 | assert( bIntkey==0 || bIntkey==1 ); |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7743 | pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7744 | if( bNoop || rc ) continue; |
dan | 9877965 | 2019-05-09 14:15:19 +0000 | [diff] [blame] | 7745 | if( pTab==0 ){ |
| 7746 | if( pOrphan==0 ){ |
| 7747 | pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); |
| 7748 | } |
| 7749 | pTab = pOrphan; |
| 7750 | if( pTab==0 ) break; |
| 7751 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7752 | |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 7753 | if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7754 | raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); |
| 7755 | } |
| 7756 | sqlite3_bind_int(pPages, 1, iRoot); |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7757 | if( bRowids==0 && pTab->iPk<0 ){ |
| 7758 | sqlite3_bind_int(pCells, 1, 1); |
| 7759 | }else{ |
| 7760 | sqlite3_bind_int(pCells, 1, 0); |
| 7761 | } |
| 7762 | sqlite3_bind_int(pCells, 3, pTab->iPk); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7763 | |
| 7764 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){ |
| 7765 | int iPgno = sqlite3_column_int(pPages, 0); |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7766 | sqlite3_bind_int(pCells, 2, iPgno); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7767 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){ |
| 7768 | int nField = sqlite3_column_int(pCells, 0); |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7769 | int iMin = sqlite3_column_int(pCells, 2); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7770 | const char *zVal = (const char*)sqlite3_column_text(pCells, 1); |
| 7771 | |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7772 | RecoverTable *pTab2 = pTab; |
| 7773 | if( pTab!=pOrphan && (iMin<0)!=bIntkey ){ |
| 7774 | if( pOrphan==0 ){ |
| 7775 | pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); |
| 7776 | } |
| 7777 | pTab2 = pOrphan; |
| 7778 | if( pTab2==0 ) break; |
| 7779 | } |
| 7780 | |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7781 | nField = nField+1; |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7782 | if( pTab2==pOrphan ){ |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7783 | raw_printf(pState->out, |
| 7784 | "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n", |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7785 | pTab2->zQuoted, iRoot, iPgno, nField, |
| 7786 | iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField] |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7787 | ); |
| 7788 | }else{ |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7789 | raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", |
dan | 9443dbc | 2019-07-24 20:10:27 +0000 | [diff] [blame] | 7790 | pTab2->zQuoted, pTab2->azlCol[nField], zVal |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7791 | ); |
| 7792 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7793 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7794 | shellReset(&rc, pCells); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7795 | } |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7796 | shellReset(&rc, pPages); |
| 7797 | if( pTab!=pOrphan ) recoverFreeTable(pTab); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7798 | } |
| 7799 | shellFinalize(&rc, pLoop); |
dan | efa363b | 2019-04-24 20:48:55 +0000 | [diff] [blame] | 7800 | shellFinalize(&rc, pPages); |
| 7801 | shellFinalize(&rc, pCells); |
dan | 98c5ad3 | 2019-04-26 21:11:37 +0000 | [diff] [blame] | 7802 | recoverFreeTable(pOrphan); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7803 | |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7804 | /* The rest of the schema */ |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7805 | if( rc==SQLITE_OK ){ |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7806 | sqlite3_stmt *pStmt = 0; |
| 7807 | shellPrepare(pState->db, &rc, |
| 7808 | "SELECT sql, name FROM recovery.schema " |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 7809 | "WHERE sql NOT LIKE 'create table%'", &pStmt |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7810 | ); |
| 7811 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7812 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 0); |
| 7813 | if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){ |
| 7814 | const char *zName = (const char*)sqlite3_column_text(pStmt, 1); |
| 7815 | char *zPrint = shellMPrintf(&rc, |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 7816 | "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)", |
dan | 38f9c71 | 2019-04-23 18:03:02 +0000 | [diff] [blame] | 7817 | zName, zName, zSql |
| 7818 | ); |
| 7819 | raw_printf(pState->out, "%s;\n", zPrint); |
| 7820 | sqlite3_free(zPrint); |
| 7821 | }else{ |
| 7822 | raw_printf(pState->out, "%s;\n", zSql); |
| 7823 | } |
| 7824 | } |
| 7825 | shellFinalize(&rc, pStmt); |
| 7826 | } |
| 7827 | |
| 7828 | if( rc==SQLITE_OK ){ |
| 7829 | raw_printf(pState->out, "PRAGMA writable_schema = off;\n"); |
dan | b40af49 | 2019-04-22 20:52:12 +0000 | [diff] [blame] | 7830 | raw_printf(pState->out, "COMMIT;\n"); |
| 7831 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7832 | sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0); |
| 7833 | return rc; |
| 7834 | } |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 7835 | #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7836 | |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7837 | |
larrybr | 2d27d36 | 2022-04-16 17:53:25 +0000 | [diff] [blame] | 7838 | /* |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7839 | * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. |
| 7840 | * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, |
| 7841 | * close db and set it to 0, and return the columns spec, to later |
| 7842 | * be sqlite3_free()'ed by the caller. |
| 7843 | * The return is 0 when either: |
| 7844 | * (a) The db was not initialized and zCol==0 (There are no columns.) |
| 7845 | * (b) zCol!=0 (Column was added, db initialized as needed.) |
| 7846 | * The 3rd argument, pRenamed, references an out parameter. If the |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7847 | * pointer is non-zero, its referent will be set to a summary of renames |
| 7848 | * done if renaming was necessary, or set to 0 if none was done. The out |
| 7849 | * string (if any) must be sqlite3_free()'ed by the caller. |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7850 | */ |
| 7851 | #ifdef SHELL_DEBUG |
| 7852 | #define rc_err_oom_die(rc) \ |
| 7853 | if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ |
| 7854 | else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ |
| 7855 | fprintf(stderr,"E:%d\n",rc), assert(0) |
| 7856 | #else |
| 7857 | static void rc_err_oom_die(int rc){ |
| 7858 | if( rc==SQLITE_NOMEM ) shell_check_oom(0); |
| 7859 | assert(rc==SQLITE_OK||rc==SQLITE_DONE); |
| 7860 | } |
| 7861 | #endif |
| 7862 | |
| 7863 | #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ |
| 7864 | static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); |
| 7865 | #else /* Otherwise, memory is faster/better for the transient DB. */ |
| 7866 | static const char *zCOL_DB = ":memory:"; |
| 7867 | #endif |
| 7868 | |
| 7869 | /* Define character (as C string) to separate generated column ordinal |
| 7870 | * from protected part of incoming column names. This defaults to "_" |
| 7871 | * so that incoming column identifiers that did not need not be quoted |
| 7872 | * remain usable without being quoted. It must be one character. |
| 7873 | */ |
| 7874 | #ifndef SHELL_AUTOCOLUMN_SEP |
| 7875 | # define AUTOCOLUMN_SEP "_" |
| 7876 | #else |
| 7877 | # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) |
| 7878 | #endif |
| 7879 | |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7880 | static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7881 | /* Queries and D{D,M}L used here */ |
| 7882 | static const char * const zTabMake = "\ |
| 7883 | CREATE TABLE ColNames(\ |
| 7884 | cpos INTEGER PRIMARY KEY,\ |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7885 | name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ |
| 7886 | CREATE VIEW RepeatedNames AS \ |
| 7887 | SELECT DISTINCT t.name FROM ColNames t \ |
| 7888 | WHERE t.name COLLATE NOCASE IN (\ |
| 7889 | SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ |
| 7890 | );\ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7891 | "; |
| 7892 | static const char * const zTabFill = "\ |
| 7893 | INSERT INTO ColNames(name,nlen,chop,reps,suff)\ |
| 7894 | VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ |
| 7895 | "; |
| 7896 | static const char * const zHasDupes = "\ |
| 7897 | SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ |
| 7898 | <count(name) FROM ColNames\ |
| 7899 | "; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7900 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7901 | static const char * const zDedoctor = "\ |
| 7902 | UPDATE ColNames SET chop=iif(\ |
| 7903 | (substring(name,nlen,1) BETWEEN '0' AND '9')\ |
| 7904 | AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\ |
| 7905 | nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ |
| 7906 | 0\ |
| 7907 | )\ |
| 7908 | "; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7909 | #endif |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7910 | static const char * const zSetReps = "\ |
| 7911 | UPDATE ColNames AS t SET reps=\ |
| 7912 | (SELECT count(*) FROM ColNames d \ |
| 7913 | WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ |
| 7914 | COLLATE NOCASE\ |
| 7915 | )\ |
| 7916 | "; |
| 7917 | #ifdef SQLITE_ENABLE_MATH_FUNCTIONS |
| 7918 | static const char * const zColDigits = "\ |
| 7919 | SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \ |
| 7920 | "; |
larrybr | 2d27d36 | 2022-04-16 17:53:25 +0000 | [diff] [blame] | 7921 | #else |
| 7922 | /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */ |
| 7923 | static const char * const zColDigits = "\ |
| 7924 | SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \ |
| 7925 | WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \ |
| 7926 | ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \ |
| 7927 | "; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7928 | #endif |
| 7929 | static const char * const zRenameRank = |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7930 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7931 | "UPDATE ColNames AS t SET suff=" |
| 7932 | "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7933 | #else /* ...RENAME_MINIMAL_ONE_PASS */ |
| 7934 | "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ |
| 7935 | " SELECT 0 AS nlz" |
| 7936 | " UNION" |
| 7937 | " SELECT nlz+1 AS nlz FROM Lzn" |
| 7938 | " WHERE EXISTS(" |
| 7939 | " SELECT 1" |
| 7940 | " FROM ColNames t, ColNames o" |
| 7941 | " WHERE" |
| 7942 | " iif(t.name IN (SELECT * FROM RepeatedNames)," |
| 7943 | " printf('%s"AUTOCOLUMN_SEP"%s'," |
| 7944 | " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," |
| 7945 | " t.name" |
| 7946 | " )" |
| 7947 | " =" |
| 7948 | " iif(o.name IN (SELECT * FROM RepeatedNames)," |
| 7949 | " printf('%s"AUTOCOLUMN_SEP"%s'," |
| 7950 | " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," |
| 7951 | " o.name" |
| 7952 | " )" |
| 7953 | " COLLATE NOCASE" |
| 7954 | " AND o.cpos<>t.cpos" |
| 7955 | " GROUP BY t.cpos" |
| 7956 | " )" |
| 7957 | ") UPDATE Colnames AS t SET" |
| 7958 | " chop = 0," /* No chopping, never touch incoming names. */ |
| 7959 | " suff = iif(name IN (SELECT * FROM RepeatedNames)," |
| 7960 | " printf('"AUTOCOLUMN_SEP"%s', substring(" |
| 7961 | " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," |
| 7962 | " ''" |
| 7963 | " )" |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7964 | #endif |
| 7965 | ; |
| 7966 | static const char * const zCollectVar = "\ |
| 7967 | SELECT\ |
| 7968 | '('||x'0a'\ |
| 7969 | || group_concat(\ |
| 7970 | cname||' TEXT',\ |
| 7971 | ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ |
| 7972 | ||')' AS ColsSpec \ |
| 7973 | FROM (\ |
larrybr | 039132b | 2022-04-09 18:51:49 +0000 | [diff] [blame] | 7974 | SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7975 | FROM ColNames ORDER BY cpos\ |
| 7976 | )"; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7977 | static const char * const zRenamesDone = |
| 7978 | "SELECT group_concat(" |
larrybr | 039132b | 2022-04-09 18:51:49 +0000 | [diff] [blame] | 7979 | " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7980 | " ','||x'0a')" |
| 7981 | "FROM ColNames WHERE suff<>'' OR chop!=0" |
| 7982 | ; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7983 | int rc; |
| 7984 | sqlite3_stmt *pStmt = 0; |
| 7985 | assert(pDb!=0); |
| 7986 | if( zColNew ){ |
| 7987 | /* Add initial or additional column. Init db if necessary. */ |
| 7988 | if( *pDb==0 ){ |
| 7989 | if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; |
| 7990 | #ifdef SHELL_COLFIX_DB |
| 7991 | if(*zCOL_DB!=':') |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7992 | sqlite3_exec(*pDb,"drop table if exists ColNames;" |
| 7993 | "drop view if exists RepeatedNames;",0,0,0); |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7994 | #endif |
| 7995 | rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); |
| 7996 | rc_err_oom_die(rc); |
| 7997 | } |
| 7998 | assert(*pDb!=0); |
| 7999 | rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); |
| 8000 | rc_err_oom_die(rc); |
| 8001 | rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); |
| 8002 | rc_err_oom_die(rc); |
| 8003 | rc = sqlite3_step(pStmt); |
| 8004 | rc_err_oom_die(rc); |
| 8005 | sqlite3_finalize(pStmt); |
| 8006 | return 0; |
| 8007 | }else if( *pDb==0 ){ |
| 8008 | return 0; |
| 8009 | }else{ |
| 8010 | /* Formulate the columns spec, close the DB, zero *pDb. */ |
| 8011 | char *zColsSpec = 0; |
| 8012 | int hasDupes = db_int(*pDb, zHasDupes); |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 8013 | int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 8014 | if( hasDupes ){ |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8015 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 8016 | rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); |
| 8017 | rc_err_oom_die(rc); |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8018 | #endif |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 8019 | rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); |
| 8020 | rc_err_oom_die(rc); |
| 8021 | rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); |
| 8022 | rc_err_oom_die(rc); |
| 8023 | sqlite3_bind_int(pStmt, 1, nDigits); |
| 8024 | rc = sqlite3_step(pStmt); |
| 8025 | sqlite3_finalize(pStmt); |
| 8026 | assert(rc==SQLITE_DONE); |
| 8027 | } |
| 8028 | assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */ |
| 8029 | rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); |
| 8030 | rc_err_oom_die(rc); |
| 8031 | rc = sqlite3_step(pStmt); |
| 8032 | if( rc==SQLITE_ROW ){ |
| 8033 | zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 8034 | }else{ |
| 8035 | zColsSpec = 0; |
| 8036 | } |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8037 | if( pzRenamed!=0 ){ |
| 8038 | if( !hasDupes ) *pzRenamed = 0; |
| 8039 | else{ |
| 8040 | sqlite3_finalize(pStmt); |
| 8041 | if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) |
| 8042 | && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 8043 | *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 8044 | }else |
| 8045 | *pzRenamed = 0; |
| 8046 | } |
| 8047 | } |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 8048 | sqlite3_finalize(pStmt); |
| 8049 | sqlite3_close(*pDb); |
| 8050 | *pDb = 0; |
| 8051 | return zColsSpec; |
| 8052 | } |
| 8053 | } |
| 8054 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8055 | /* |
| 8056 | ** If an input line begins with "." then invoke this routine to |
| 8057 | ** process that line. |
| 8058 | ** |
| 8059 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 8060 | */ |
| 8061 | static int do_meta_command(char *zLine, ShellState *p){ |
| 8062 | int h = 1; |
| 8063 | int nArg = 0; |
| 8064 | int n, c; |
| 8065 | int rc = 0; |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 8066 | char *azArg[52]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8067 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8068 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8069 | if( p->expert.pExpert ){ |
| 8070 | expertFinish(p, 1, 0); |
| 8071 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8072 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8073 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8074 | /* Parse the input line into tokens. |
| 8075 | */ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 8076 | while( zLine[h] && nArg<ArraySize(azArg)-1 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8077 | while( IsSpace(zLine[h]) ){ h++; } |
| 8078 | if( zLine[h]==0 ) break; |
| 8079 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 8080 | int delim = zLine[h++]; |
| 8081 | azArg[nArg++] = &zLine[h]; |
| 8082 | while( zLine[h] && zLine[h]!=delim ){ |
| 8083 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
| 8084 | h++; |
| 8085 | } |
| 8086 | if( zLine[h]==delim ){ |
| 8087 | zLine[h++] = 0; |
| 8088 | } |
| 8089 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
| 8090 | }else{ |
| 8091 | azArg[nArg++] = &zLine[h]; |
| 8092 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 8093 | if( zLine[h] ) zLine[h++] = 0; |
| 8094 | resolve_backslashes(azArg[nArg-1]); |
| 8095 | } |
| 8096 | } |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 8097 | azArg[nArg] = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8098 | |
| 8099 | /* Process the input line. |
| 8100 | */ |
| 8101 | if( nArg==0 ) return 0; /* no tokens, no error */ |
| 8102 | n = strlen30(azArg[0]); |
| 8103 | c = azArg[0][0]; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 8104 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8105 | |
| 8106 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 8107 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 8108 | if( nArg!=2 ){ |
| 8109 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 8110 | rc = 1; |
| 8111 | goto meta_command_exit; |
| 8112 | } |
| 8113 | open_db(p, 0); |
| 8114 | if( booleanValue(azArg[1]) ){ |
| 8115 | sqlite3_set_authorizer(p->db, shellAuth, p); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8116 | }else if( p->bSafeModePersist ){ |
| 8117 | sqlite3_set_authorizer(p->db, safeModeAuth, p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8118 | }else{ |
| 8119 | sqlite3_set_authorizer(p->db, 0, 0); |
| 8120 | } |
| 8121 | }else |
| 8122 | #endif |
| 8123 | |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 8124 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
| 8125 | if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 8126 | open_db(p, 0); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8127 | failIfSafeMode(p, "cannot run .archive in safe mode"); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 8128 | rc = arDotCommand(p, 0, azArg, nArg); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 8129 | }else |
| 8130 | #endif |
| 8131 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8132 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 8133 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 8134 | ){ |
| 8135 | const char *zDestFile = 0; |
| 8136 | const char *zDb = 0; |
| 8137 | sqlite3 *pDest; |
| 8138 | sqlite3_backup *pBackup; |
| 8139 | int j; |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 8140 | int bAsync = 0; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 8141 | const char *zVfs = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8142 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8143 | for(j=1; j<nArg; j++){ |
| 8144 | const char *z = azArg[j]; |
| 8145 | if( z[0]=='-' ){ |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 8146 | if( z[1]=='-' ) z++; |
| 8147 | if( strcmp(z, "-append")==0 ){ |
| 8148 | zVfs = "apndvfs"; |
| 8149 | }else |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 8150 | if( strcmp(z, "-async")==0 ){ |
| 8151 | bAsync = 1; |
| 8152 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8153 | { |
| 8154 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| 8155 | return 1; |
| 8156 | } |
| 8157 | }else if( zDestFile==0 ){ |
| 8158 | zDestFile = azArg[j]; |
| 8159 | }else if( zDb==0 ){ |
| 8160 | zDb = zDestFile; |
| 8161 | zDestFile = azArg[j]; |
| 8162 | }else{ |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 8163 | raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8164 | return 1; |
| 8165 | } |
| 8166 | } |
| 8167 | if( zDestFile==0 ){ |
| 8168 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
| 8169 | return 1; |
| 8170 | } |
| 8171 | if( zDb==0 ) zDb = "main"; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 8172 | rc = sqlite3_open_v2(zDestFile, &pDest, |
| 8173 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8174 | if( rc!=SQLITE_OK ){ |
| 8175 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 8176 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8177 | return 1; |
| 8178 | } |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 8179 | if( bAsync ){ |
| 8180 | sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", |
| 8181 | 0, 0, 0); |
| 8182 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8183 | open_db(p, 0); |
| 8184 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 8185 | if( pBackup==0 ){ |
| 8186 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 8187 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8188 | return 1; |
| 8189 | } |
| 8190 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 8191 | sqlite3_backup_finish(pBackup); |
| 8192 | if( rc==SQLITE_DONE ){ |
| 8193 | rc = 0; |
| 8194 | }else{ |
| 8195 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 8196 | rc = 1; |
| 8197 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 8198 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8199 | }else |
| 8200 | |
| 8201 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 8202 | if( nArg==2 ){ |
| 8203 | bail_on_error = booleanValue(azArg[1]); |
| 8204 | }else{ |
| 8205 | raw_printf(stderr, "Usage: .bail on|off\n"); |
| 8206 | rc = 1; |
| 8207 | } |
| 8208 | }else |
| 8209 | |
| 8210 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 8211 | if( nArg==2 ){ |
| 8212 | if( booleanValue(azArg[1]) ){ |
| 8213 | setBinaryMode(p->out, 1); |
| 8214 | }else{ |
| 8215 | setTextMode(p->out, 1); |
| 8216 | } |
| 8217 | }else{ |
| 8218 | raw_printf(stderr, "Usage: .binary on|off\n"); |
| 8219 | rc = 1; |
| 8220 | } |
| 8221 | }else |
| 8222 | |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 8223 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 8224 | ** routine named test_breakpoint(). |
| 8225 | */ |
| 8226 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 8227 | test_breakpoint(); |
| 8228 | }else |
| 8229 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8230 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8231 | failIfSafeMode(p, "cannot run .cd in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8232 | if( nArg==2 ){ |
| 8233 | #if defined(_WIN32) || defined(WIN32) |
| 8234 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 8235 | rc = !SetCurrentDirectoryW(z); |
| 8236 | sqlite3_free(z); |
| 8237 | #else |
| 8238 | rc = chdir(azArg[1]); |
| 8239 | #endif |
| 8240 | if( rc ){ |
| 8241 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 8242 | rc = 1; |
| 8243 | } |
| 8244 | }else{ |
| 8245 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 8246 | rc = 1; |
| 8247 | } |
| 8248 | }else |
| 8249 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8250 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 8251 | if( nArg==2 ){ |
| 8252 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 8253 | }else{ |
| 8254 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 8255 | rc = 1; |
| 8256 | } |
| 8257 | }else |
| 8258 | |
| 8259 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 8260 | ** Then read the content of the testcase-out.txt file and compare against |
| 8261 | ** azArg[1]. If there are differences, report an error and exit. |
| 8262 | */ |
| 8263 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 8264 | char *zRes = 0; |
| 8265 | output_reset(p); |
| 8266 | if( nArg!=2 ){ |
| 8267 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
| 8268 | rc = 2; |
| 8269 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
| 8270 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 8271 | rc = 2; |
| 8272 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
| 8273 | utf8_printf(stderr, |
| 8274 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 8275 | p->zTestcase, azArg[1], zRes); |
drh | f30d345 | 2017-10-17 13:44:46 +0000 | [diff] [blame] | 8276 | rc = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8277 | }else{ |
| 8278 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 8279 | p->nCheck++; |
| 8280 | } |
| 8281 | sqlite3_free(zRes); |
| 8282 | }else |
| 8283 | |
| 8284 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8285 | failIfSafeMode(p, "cannot run .clone in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8286 | if( nArg==2 ){ |
| 8287 | tryToClone(p, azArg[1]); |
| 8288 | }else{ |
| 8289 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 8290 | rc = 1; |
| 8291 | } |
| 8292 | }else |
| 8293 | |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 8294 | if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){ |
| 8295 | if( nArg==1 ){ |
| 8296 | /* List available connections */ |
| 8297 | int i; |
| 8298 | for(i=0; i<ArraySize(p->aAuxDb); i++){ |
| 8299 | const char *zFile = p->aAuxDb[i].zDbFilename; |
| 8300 | if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ |
| 8301 | zFile = "(not open)"; |
| 8302 | }else if( zFile==0 ){ |
| 8303 | zFile = "(memory)"; |
| 8304 | }else if( zFile[0]==0 ){ |
| 8305 | zFile = "(temporary-file)"; |
| 8306 | } |
| 8307 | if( p->pAuxDb == &p->aAuxDb[i] ){ |
| 8308 | utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile); |
| 8309 | }else if( p->aAuxDb[i].db!=0 ){ |
| 8310 | utf8_printf(stdout, " %d: %s\n", i, zFile); |
| 8311 | } |
| 8312 | } |
| 8313 | }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ |
| 8314 | int i = azArg[1][0] - '0'; |
| 8315 | if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ |
| 8316 | p->pAuxDb->db = p->db; |
| 8317 | p->pAuxDb = &p->aAuxDb[i]; |
| 8318 | globalDb = p->db = p->pAuxDb->db; |
| 8319 | p->pAuxDb->db = 0; |
| 8320 | } |
| 8321 | }else if( nArg==3 && strcmp(azArg[1], "close")==0 |
| 8322 | && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ |
| 8323 | int i = azArg[2][0] - '0'; |
| 8324 | if( i<0 || i>=ArraySize(p->aAuxDb) ){ |
| 8325 | /* No-op */ |
| 8326 | }else if( p->pAuxDb == &p->aAuxDb[i] ){ |
| 8327 | raw_printf(stderr, "cannot close the active database connection\n"); |
| 8328 | rc = 1; |
| 8329 | }else if( p->aAuxDb[i].db ){ |
| 8330 | session_close_all(p, i); |
| 8331 | close_db(p->aAuxDb[i].db); |
| 8332 | p->aAuxDb[i].db = 0; |
| 8333 | } |
| 8334 | }else{ |
| 8335 | raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); |
| 8336 | rc = 1; |
| 8337 | } |
| 8338 | }else |
| 8339 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8340 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8341 | char **azName = 0; |
| 8342 | int nName = 0; |
| 8343 | sqlite3_stmt *pStmt; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8344 | int i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8345 | open_db(p, 0); |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8346 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
| 8347 | if( rc ){ |
| 8348 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8349 | rc = 1; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8350 | }else{ |
| 8351 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 8352 | const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); |
| 8353 | const char *zFile = (const char*)sqlite3_column_text(pStmt,2); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 8354 | if( zSchema==0 || zFile==0 ) continue; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8355 | azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 8356 | shell_check_oom(azName); |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8357 | azName[nName*2] = strdup(zSchema); |
| 8358 | azName[nName*2+1] = strdup(zFile); |
| 8359 | nName++; |
| 8360 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8361 | } |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 8362 | sqlite3_finalize(pStmt); |
| 8363 | for(i=0; i<nName; i++){ |
| 8364 | int eTxn = sqlite3_txn_state(p->db, azName[i*2]); |
| 8365 | int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); |
| 8366 | const char *z = azName[i*2+1]; |
| 8367 | utf8_printf(p->out, "%s: %s %s%s\n", |
| 8368 | azName[i*2], |
| 8369 | z && z[0] ? z : "\"\"", |
| 8370 | bRdonly ? "r/o" : "r/w", |
| 8371 | eTxn==SQLITE_TXN_NONE ? "" : |
| 8372 | eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); |
| 8373 | free(azName[i*2]); |
| 8374 | free(azName[i*2+1]); |
| 8375 | } |
| 8376 | sqlite3_free(azName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8377 | }else |
| 8378 | |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 8379 | if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 8380 | static const struct DbConfigChoices { |
| 8381 | const char *zName; |
| 8382 | int op; |
| 8383 | } aDbConfig[] = { |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 8384 | { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, |
| 8385 | { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, |
| 8386 | { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 8387 | { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 8388 | { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 8389 | { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, |
drh | 11d88e6 | 2019-08-15 21:27:20 +0000 | [diff] [blame] | 8390 | { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 8391 | { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 8392 | { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, |
| 8393 | { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 8394 | { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, |
| 8395 | { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 8396 | { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 8397 | { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, |
drh | b77da37 | 2020-01-07 16:09:11 +0000 | [diff] [blame] | 8398 | { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, |
dan | 07312a6 | 2019-06-21 14:05:27 +0000 | [diff] [blame] | 8399 | { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 8400 | }; |
| 8401 | int ii, v; |
| 8402 | open_db(p, 0); |
| 8403 | for(ii=0; ii<ArraySize(aDbConfig); ii++){ |
| 8404 | if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; |
| 8405 | if( nArg>=3 ){ |
| 8406 | sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); |
| 8407 | } |
| 8408 | sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 8409 | utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 8410 | if( nArg>1 ) break; |
| 8411 | } |
| 8412 | if( nArg>1 && ii==ArraySize(aDbConfig) ){ |
| 8413 | utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); |
| 8414 | utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); |
| 8415 | } |
| 8416 | }else |
| 8417 | |
| 8418 | if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8419 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 8420 | }else |
| 8421 | |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 8422 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8423 | if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ |
| 8424 | open_db(p, 0); |
dan | b9b71db | 2019-04-25 16:20:40 +0000 | [diff] [blame] | 8425 | rc = recoverDatabaseCmd(p, nArg, azArg); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8426 | }else |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 8427 | #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8428 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8429 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8430 | char *zLike = 0; |
| 8431 | char *zSql; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8432 | int i; |
| 8433 | int savedShowHeader = p->showHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 8434 | int savedShellFlags = p->shellFlgs; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 8435 | ShellClearFlag(p, |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8436 | SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo |
| 8437 | |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8438 | for(i=1; i<nArg; i++){ |
| 8439 | if( azArg[i][0]=='-' ){ |
| 8440 | const char *z = azArg[i]+1; |
| 8441 | if( z[0]=='-' ) z++; |
| 8442 | if( strcmp(z,"preserve-rowids")==0 ){ |
| 8443 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 8444 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 8445 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 8446 | rc = 1; |
drh | 1d29fd8 | 2020-05-29 19:03:03 +0000 | [diff] [blame] | 8447 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8448 | goto meta_command_exit; |
| 8449 | #else |
| 8450 | ShellSetFlag(p, SHFLG_PreserveRowid); |
| 8451 | #endif |
| 8452 | }else |
| 8453 | if( strcmp(z,"newlines")==0 ){ |
| 8454 | ShellSetFlag(p, SHFLG_Newlines); |
| 8455 | }else |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8456 | if( strcmp(z,"data-only")==0 ){ |
| 8457 | ShellSetFlag(p, SHFLG_DumpDataOnly); |
| 8458 | }else |
| 8459 | if( strcmp(z,"nosys")==0 ){ |
| 8460 | ShellSetFlag(p, SHFLG_DumpNoSys); |
| 8461 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8462 | { |
| 8463 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 8464 | rc = 1; |
drh | 1d29fd8 | 2020-05-29 19:03:03 +0000 | [diff] [blame] | 8465 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8466 | goto meta_command_exit; |
| 8467 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8468 | }else{ |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8469 | /* azArg[i] contains a LIKE pattern. This ".dump" request should |
| 8470 | ** only dump data for tables for which either the table name matches |
| 8471 | ** the LIKE pattern, or the table appears to be a shadow table of |
| 8472 | ** a virtual table for which the name matches the LIKE pattern. |
| 8473 | */ |
| 8474 | char *zExpr = sqlite3_mprintf( |
| 8475 | "name LIKE %Q ESCAPE '\\' OR EXISTS (" |
| 8476 | " SELECT 1 FROM sqlite_schema WHERE " |
| 8477 | " name LIKE %Q ESCAPE '\\' AND" |
| 8478 | " sql LIKE 'CREATE VIRTUAL TABLE%%' AND" |
| 8479 | " substr(o.name, 1, length(name)+1) == (name||'_')" |
| 8480 | ")", azArg[i], azArg[i] |
| 8481 | ); |
| 8482 | |
| 8483 | if( zLike ){ |
| 8484 | zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr); |
| 8485 | }else{ |
| 8486 | zLike = zExpr; |
| 8487 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8488 | } |
| 8489 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8490 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8491 | open_db(p, 0); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8492 | |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8493 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8494 | /* When playing back a "dump", the content might appear in an order |
| 8495 | ** which causes immediate foreign key constraints to be violated. |
| 8496 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 8497 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 8498 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 8499 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8500 | p->writableSchema = 0; |
| 8501 | p->showHeader = 0; |
| 8502 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8503 | ** as much of the schema as it can even if the sqlite_schema table is |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8504 | ** corrupt. */ |
| 8505 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 8506 | p->nErr = 0; |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8507 | if( zLike==0 ) zLike = sqlite3_mprintf("true"); |
| 8508 | zSql = sqlite3_mprintf( |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8509 | "SELECT name, type, sql FROM sqlite_schema AS o " |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8510 | "WHERE (%s) AND type=='table'" |
| 8511 | " AND sql NOT NULL" |
| 8512 | " ORDER BY tbl_name='sqlite_sequence', rowid", |
| 8513 | zLike |
| 8514 | ); |
| 8515 | run_schema_dump_query(p,zSql); |
| 8516 | sqlite3_free(zSql); |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8517 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8518 | zSql = sqlite3_mprintf( |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8519 | "SELECT sql FROM sqlite_schema AS o " |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8520 | "WHERE (%s) AND sql NOT NULL" |
| 8521 | " AND type IN ('index','trigger','view')", |
| 8522 | zLike |
| 8523 | ); |
| 8524 | run_table_dump_query(p, zSql); |
| 8525 | sqlite3_free(zSql); |
| 8526 | } |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8527 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8528 | if( p->writableSchema ){ |
| 8529 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 8530 | p->writableSchema = 0; |
| 8531 | } |
| 8532 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 8533 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8534 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8535 | raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); |
| 8536 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8537 | p->showHeader = savedShowHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 8538 | p->shellFlgs = savedShellFlags; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8539 | }else |
| 8540 | |
| 8541 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 8542 | if( nArg==2 ){ |
| 8543 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
| 8544 | }else{ |
| 8545 | raw_printf(stderr, "Usage: .echo on|off\n"); |
| 8546 | rc = 1; |
| 8547 | } |
| 8548 | }else |
| 8549 | |
| 8550 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 8551 | if( nArg==2 ){ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 8552 | p->autoEQPtest = 0; |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8553 | if( p->autoEQPtrace ){ |
| 8554 | if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); |
| 8555 | p->autoEQPtrace = 0; |
| 8556 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8557 | if( strcmp(azArg[1],"full")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8558 | p->autoEQP = AUTOEQP_full; |
| 8559 | }else if( strcmp(azArg[1],"trigger")==0 ){ |
| 8560 | p->autoEQP = AUTOEQP_trigger; |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8561 | #ifdef SQLITE_DEBUG |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 8562 | }else if( strcmp(azArg[1],"test")==0 ){ |
| 8563 | p->autoEQP = AUTOEQP_on; |
| 8564 | p->autoEQPtest = 1; |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8565 | }else if( strcmp(azArg[1],"trace")==0 ){ |
| 8566 | p->autoEQP = AUTOEQP_full; |
| 8567 | p->autoEQPtrace = 1; |
| 8568 | open_db(p, 0); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8569 | sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8570 | sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); |
| 8571 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8572 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 8573 | p->autoEQP = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8574 | } |
| 8575 | }else{ |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8576 | raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8577 | rc = 1; |
| 8578 | } |
| 8579 | }else |
| 8580 | |
| 8581 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 8582 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 8583 | rc = 2; |
| 8584 | }else |
| 8585 | |
| 8586 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 8587 | ** retained purely for backwards compatibility */ |
| 8588 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 8589 | int val = 1; |
| 8590 | if( nArg>=2 ){ |
| 8591 | if( strcmp(azArg[1],"auto")==0 ){ |
| 8592 | val = 99; |
| 8593 | }else{ |
| 8594 | val = booleanValue(azArg[1]); |
| 8595 | } |
| 8596 | } |
| 8597 | if( val==1 && p->mode!=MODE_Explain ){ |
| 8598 | p->normalMode = p->mode; |
| 8599 | p->mode = MODE_Explain; |
| 8600 | p->autoExplain = 0; |
| 8601 | }else if( val==0 ){ |
| 8602 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 8603 | p->autoExplain = 0; |
| 8604 | }else if( val==99 ){ |
| 8605 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 8606 | p->autoExplain = 1; |
| 8607 | } |
| 8608 | }else |
| 8609 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8610 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8611 | if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ |
drh | fe46317 | 2021-12-16 17:57:21 +0000 | [diff] [blame] | 8612 | if( p->bSafeMode ){ |
| 8613 | raw_printf(stderr, |
| 8614 | "Cannot run experimental commands such as \"%s\" in safe mode\n", |
| 8615 | azArg[0]); |
| 8616 | rc = 1; |
| 8617 | }else{ |
| 8618 | open_db(p, 0); |
| 8619 | expertDotCommand(p, azArg, nArg); |
| 8620 | } |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8621 | }else |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8622 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8623 | |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8624 | if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ |
| 8625 | static const struct { |
| 8626 | const char *zCtrlName; /* Name of a test-control option */ |
| 8627 | int ctrlCode; /* Integer code for that option */ |
| 8628 | const char *zUsage; /* Usage notes */ |
| 8629 | } aCtrl[] = { |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8630 | { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8631 | { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8632 | { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, |
| 8633 | { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8634 | { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, |
| 8635 | /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ |
| 8636 | { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8637 | { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8638 | { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, |
| 8639 | { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, |
| 8640 | /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8641 | }; |
| 8642 | int filectrl = -1; |
| 8643 | int iCtrl = -1; |
drh | 4245e04 | 2019-06-13 13:52:46 +0000 | [diff] [blame] | 8644 | sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ |
| 8645 | int isOk = 0; /* 0: usage 1: %lld 2: no-result */ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8646 | int n2, i; |
| 8647 | const char *zCmd = 0; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8648 | const char *zSchema = 0; |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8649 | |
| 8650 | open_db(p, 0); |
| 8651 | zCmd = nArg>=2 ? azArg[1] : "help"; |
| 8652 | |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8653 | if( zCmd[0]=='-' |
| 8654 | && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0) |
| 8655 | && nArg>=4 |
| 8656 | ){ |
| 8657 | zSchema = azArg[2]; |
| 8658 | for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; |
| 8659 | nArg -= 2; |
| 8660 | zCmd = azArg[1]; |
| 8661 | } |
| 8662 | |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8663 | /* The argument can optionally begin with "-" or "--" */ |
| 8664 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 8665 | zCmd++; |
| 8666 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 8667 | } |
| 8668 | |
| 8669 | /* --help lists all file-controls */ |
| 8670 | if( strcmp(zCmd,"help")==0 ){ |
| 8671 | utf8_printf(p->out, "Available file-controls:\n"); |
| 8672 | for(i=0; i<ArraySize(aCtrl); i++){ |
| 8673 | utf8_printf(p->out, " .filectrl %s %s\n", |
| 8674 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
| 8675 | } |
| 8676 | rc = 1; |
| 8677 | goto meta_command_exit; |
| 8678 | } |
| 8679 | |
| 8680 | /* convert filectrl text option to value. allow any unique prefix |
| 8681 | ** of the option name, or a numerical value. */ |
| 8682 | n2 = strlen30(zCmd); |
| 8683 | for(i=0; i<ArraySize(aCtrl); i++){ |
| 8684 | if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
| 8685 | if( filectrl<0 ){ |
| 8686 | filectrl = aCtrl[i].ctrlCode; |
| 8687 | iCtrl = i; |
| 8688 | }else{ |
| 8689 | utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" |
| 8690 | "Use \".filectrl --help\" for help\n", zCmd); |
| 8691 | rc = 1; |
| 8692 | goto meta_command_exit; |
| 8693 | } |
| 8694 | } |
| 8695 | } |
| 8696 | if( filectrl<0 ){ |
| 8697 | utf8_printf(stderr,"Error: unknown file-control: %s\n" |
| 8698 | "Use \".filectrl --help\" for help\n", zCmd); |
| 8699 | }else{ |
| 8700 | switch(filectrl){ |
| 8701 | case SQLITE_FCNTL_SIZE_LIMIT: { |
| 8702 | if( nArg!=2 && nArg!=3 ) break; |
| 8703 | iRes = nArg==3 ? integerValue(azArg[2]) : -1; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8704 | sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8705 | isOk = 1; |
| 8706 | break; |
| 8707 | } |
| 8708 | case SQLITE_FCNTL_LOCK_TIMEOUT: |
| 8709 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 8710 | int x; |
| 8711 | if( nArg!=3 ) break; |
| 8712 | x = (int)integerValue(azArg[2]); |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8713 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8714 | isOk = 2; |
| 8715 | break; |
| 8716 | } |
| 8717 | case SQLITE_FCNTL_PERSIST_WAL: |
| 8718 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 8719 | int x; |
| 8720 | if( nArg!=2 && nArg!=3 ) break; |
| 8721 | x = nArg==3 ? booleanValue(azArg[2]) : -1; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8722 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8723 | iRes = x; |
| 8724 | isOk = 1; |
| 8725 | break; |
| 8726 | } |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8727 | case SQLITE_FCNTL_DATA_VERSION: |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8728 | case SQLITE_FCNTL_HAS_MOVED: { |
| 8729 | int x; |
| 8730 | if( nArg!=2 ) break; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8731 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8732 | iRes = x; |
| 8733 | isOk = 1; |
| 8734 | break; |
| 8735 | } |
| 8736 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 8737 | char *z = 0; |
| 8738 | if( nArg!=2 ) break; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8739 | sqlite3_file_control(p->db, zSchema, filectrl, &z); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8740 | if( z ){ |
| 8741 | utf8_printf(p->out, "%s\n", z); |
| 8742 | sqlite3_free(z); |
| 8743 | } |
| 8744 | isOk = 2; |
| 8745 | break; |
| 8746 | } |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8747 | case SQLITE_FCNTL_RESERVE_BYTES: { |
| 8748 | int x; |
| 8749 | if( nArg>=3 ){ |
| 8750 | x = atoi(azArg[2]); |
| 8751 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
| 8752 | } |
| 8753 | x = -1; |
| 8754 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
| 8755 | utf8_printf(p->out,"%d\n", x); |
| 8756 | isOk = 2; |
| 8757 | break; |
| 8758 | } |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8759 | } |
| 8760 | } |
| 8761 | if( isOk==0 && iCtrl>=0 ){ |
| 8762 | utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
| 8763 | rc = 1; |
| 8764 | }else if( isOk==1 ){ |
drh | e250076 | 2019-06-13 14:07:41 +0000 | [diff] [blame] | 8765 | char zBuf[100]; |
| 8766 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); |
| 8767 | raw_printf(p->out, "%s\n", zBuf); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8768 | } |
| 8769 | }else |
| 8770 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8771 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
| 8772 | ShellState data; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8773 | int doStats = 0; |
| 8774 | memcpy(&data, p, sizeof(data)); |
| 8775 | data.showHeader = 0; |
| 8776 | data.cMode = data.mode = MODE_Semi; |
| 8777 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 8778 | data.cMode = data.mode = MODE_Pretty; |
| 8779 | nArg = 1; |
| 8780 | } |
| 8781 | if( nArg!=1 ){ |
| 8782 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| 8783 | rc = 1; |
| 8784 | goto meta_command_exit; |
| 8785 | } |
| 8786 | open_db(p, 0); |
| 8787 | rc = sqlite3_exec(p->db, |
| 8788 | "SELECT sql FROM" |
| 8789 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8790 | " FROM sqlite_schema UNION ALL" |
| 8791 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8792 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 69935c0 | 2021-06-25 11:14:10 +0000 | [diff] [blame] | 8793 | "ORDER BY x", |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8794 | callback, &data, 0 |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8795 | ); |
| 8796 | if( rc==SQLITE_OK ){ |
| 8797 | sqlite3_stmt *pStmt; |
| 8798 | rc = sqlite3_prepare_v2(p->db, |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8799 | "SELECT rowid FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8800 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 8801 | -1, &pStmt, 0); |
| 8802 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 8803 | sqlite3_finalize(pStmt); |
| 8804 | } |
| 8805 | if( doStats==0 ){ |
| 8806 | raw_printf(p->out, "/* No STAT tables available */\n"); |
| 8807 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8808 | raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8809 | data.cMode = data.mode = MODE_Insert; |
| 8810 | data.zDestTable = "sqlite_stat1"; |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8811 | shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8812 | data.zDestTable = "sqlite_stat4"; |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8813 | shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8814 | raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8815 | } |
| 8816 | }else |
| 8817 | |
| 8818 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 8819 | if( nArg==2 ){ |
| 8820 | p->showHeader = booleanValue(azArg[1]); |
drh | c060508 | 2020-06-05 00:54:27 +0000 | [diff] [blame] | 8821 | p->shellFlgs |= SHFLG_HeaderSet; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8822 | }else{ |
| 8823 | raw_printf(stderr, "Usage: .headers on|off\n"); |
| 8824 | rc = 1; |
| 8825 | } |
| 8826 | }else |
| 8827 | |
| 8828 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 8829 | if( nArg>=2 ){ |
drh | e93f826 | 2018-10-11 16:53:37 +0000 | [diff] [blame] | 8830 | n = showHelp(p->out, azArg[1]); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 8831 | if( n==0 ){ |
| 8832 | utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); |
| 8833 | } |
| 8834 | }else{ |
| 8835 | showHelp(p->out, 0); |
| 8836 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8837 | }else |
| 8838 | |
| 8839 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8840 | char *zTable = 0; /* Insert data into this table */ |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8841 | char *zSchema = 0; /* within this schema (may default to "main") */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8842 | char *zFile = 0; /* Name of file to extra content from */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8843 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 8844 | int nCol; /* Number of columns in the table */ |
| 8845 | int nByte; /* Number of bytes in an SQL string */ |
| 8846 | int i, j; /* Loop counters */ |
| 8847 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 8848 | int nSep; /* Number of bytes in p->colSeparator[] */ |
| 8849 | char *zSql; /* An SQL statement */ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8850 | char *zFullTabName; /* Table name with schema if applicable */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8851 | ImportCtx sCtx; /* Reader context */ |
| 8852 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8853 | int eVerbose = 0; /* Larger for more console output */ |
| 8854 | int nSkip = 0; /* Initial lines to skip */ |
| 8855 | int useOutputMode = 1; /* Use output mode to determine separators */ |
drh | e684ac6 | 2022-03-08 13:59:46 +0000 | [diff] [blame] | 8856 | char *zCreate = 0; /* CREATE TABLE statement text */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8857 | |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8858 | failIfSafeMode(p, "cannot run .import in safe mode"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8859 | memset(&sCtx, 0, sizeof(sCtx)); |
| 8860 | if( p->mode==MODE_Ascii ){ |
| 8861 | xRead = ascii_read_one_field; |
| 8862 | }else{ |
| 8863 | xRead = csv_read_one_field; |
| 8864 | } |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8865 | rc = 1; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8866 | for(i=1; i<nArg; i++){ |
| 8867 | char *z = azArg[i]; |
| 8868 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 8869 | if( z[0]!='-' ){ |
| 8870 | if( zFile==0 ){ |
| 8871 | zFile = z; |
| 8872 | }else if( zTable==0 ){ |
| 8873 | zTable = z; |
| 8874 | }else{ |
| 8875 | utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); |
| 8876 | showHelp(p->out, "import"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8877 | goto meta_command_exit; |
| 8878 | } |
| 8879 | }else if( strcmp(z,"-v")==0 ){ |
| 8880 | eVerbose++; |
larrybr | 738d7b9 | 2022-01-13 21:22:54 +0000 | [diff] [blame] | 8881 | }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){ |
| 8882 | zSchema = azArg[++i]; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8883 | }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){ |
| 8884 | nSkip = integerValue(azArg[++i]); |
| 8885 | }else if( strcmp(z,"-ascii")==0 ){ |
| 8886 | sCtx.cColSep = SEP_Unit[0]; |
| 8887 | sCtx.cRowSep = SEP_Record[0]; |
| 8888 | xRead = ascii_read_one_field; |
| 8889 | useOutputMode = 0; |
| 8890 | }else if( strcmp(z,"-csv")==0 ){ |
| 8891 | sCtx.cColSep = ','; |
| 8892 | sCtx.cRowSep = '\n'; |
| 8893 | xRead = csv_read_one_field; |
| 8894 | useOutputMode = 0; |
| 8895 | }else{ |
| 8896 | utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); |
| 8897 | showHelp(p->out, "import"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8898 | goto meta_command_exit; |
| 8899 | } |
| 8900 | } |
| 8901 | if( zTable==0 ){ |
| 8902 | utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", |
| 8903 | zFile==0 ? "FILE" : "TABLE"); |
| 8904 | showHelp(p->out, "import"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8905 | goto meta_command_exit; |
| 8906 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8907 | seenInterrupt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8908 | open_db(p, 0); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8909 | if( useOutputMode ){ |
| 8910 | /* If neither the --csv or --ascii options are specified, then set |
| 8911 | ** the column and row separator characters from the output mode. */ |
| 8912 | nSep = strlen30(p->colSeparator); |
| 8913 | if( nSep==0 ){ |
| 8914 | raw_printf(stderr, |
| 8915 | "Error: non-null column separator required for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8916 | goto meta_command_exit; |
| 8917 | } |
| 8918 | if( nSep>1 ){ |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8919 | raw_printf(stderr, |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8920 | "Error: multi-character column separators not allowed" |
| 8921 | " for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8922 | goto meta_command_exit; |
| 8923 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8924 | nSep = strlen30(p->rowSeparator); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8925 | if( nSep==0 ){ |
| 8926 | raw_printf(stderr, |
| 8927 | "Error: non-null row separator required for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8928 | goto meta_command_exit; |
| 8929 | } |
| 8930 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){ |
| 8931 | /* When importing CSV (only), if the row separator is set to the |
| 8932 | ** default output row separator, change it to the default input |
| 8933 | ** row separator. This avoids having to maintain different input |
| 8934 | ** and output row separators. */ |
| 8935 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 8936 | nSep = strlen30(p->rowSeparator); |
| 8937 | } |
| 8938 | if( nSep>1 ){ |
| 8939 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
| 8940 | " for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8941 | goto meta_command_exit; |
| 8942 | } |
| 8943 | sCtx.cColSep = p->colSeparator[0]; |
| 8944 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8945 | } |
| 8946 | sCtx.zFile = zFile; |
| 8947 | sCtx.nLine = 1; |
| 8948 | if( sCtx.zFile[0]=='|' ){ |
| 8949 | #ifdef SQLITE_OMIT_POPEN |
| 8950 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8951 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8952 | #else |
| 8953 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 8954 | sCtx.zFile = "<pipe>"; |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8955 | sCtx.xCloser = pclose; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8956 | #endif |
| 8957 | }else{ |
| 8958 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8959 | sCtx.xCloser = fclose; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8960 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8961 | if( sCtx.in==0 ){ |
| 8962 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8963 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8964 | } |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8965 | rc = 0; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8966 | if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ |
| 8967 | char zSep[2]; |
| 8968 | zSep[1] = 0; |
| 8969 | zSep[0] = sCtx.cColSep; |
| 8970 | utf8_printf(p->out, "Column separator "); |
| 8971 | output_c_string(p->out, zSep); |
| 8972 | utf8_printf(p->out, ", row separator "); |
| 8973 | zSep[0] = sCtx.cRowSep; |
| 8974 | output_c_string(p->out, zSep); |
| 8975 | utf8_printf(p->out, "\n"); |
| 8976 | } |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8977 | sCtx.z = sqlite3_malloc64(120); |
| 8978 | if( sCtx.z==0 ){ |
| 8979 | import_cleanup(&sCtx); |
| 8980 | shell_out_of_memory(); |
| 8981 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8982 | /* Below, resources must be freed before exit. */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8983 | while( (nSkip--)>0 ){ |
| 8984 | while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8985 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8986 | if( zSchema!=0 ){ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8987 | zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8988 | }else{ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8989 | zFullTabName = sqlite3_mprintf("\"%w\"", zTable); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8990 | } |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8991 | zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName); |
| 8992 | if( zSql==0 || zFullTabName==0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8993 | import_cleanup(&sCtx); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8994 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8995 | } |
| 8996 | nByte = strlen30(zSql); |
| 8997 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 8998 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| 8999 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 9000 | sqlite3 *dbCols = 0; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 9001 | char *zRenames = 0; |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 9002 | char *zColDefs; |
drh | e684ac6 | 2022-03-08 13:59:46 +0000 | [diff] [blame] | 9003 | zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9004 | while( xRead(&sCtx) ){ |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 9005 | zAutoColumn(sCtx.z, &dbCols, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9006 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
larrybr | 4c5c621 | 2022-02-11 01:21:09 +0000 | [diff] [blame] | 9007 | } |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 9008 | zColDefs = zAutoColumn(0, &dbCols, &zRenames); |
| 9009 | if( zRenames!=0 ){ |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 9010 | utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr, |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 9011 | "Columns renamed during .import %s due to duplicates:\n" |
| 9012 | "%s\n", sCtx.zFile, zRenames); |
| 9013 | sqlite3_free(zRenames); |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 9014 | } |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 9015 | assert(dbCols==0); |
| 9016 | if( zColDefs==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9017 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9018 | import_fail: |
| 9019 | sqlite3_free(zCreate); |
| 9020 | sqlite3_free(zSql); |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 9021 | sqlite3_free(zFullTabName); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9022 | import_cleanup(&sCtx); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 9023 | rc = 1; |
| 9024 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9025 | } |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 9026 | zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 9027 | if( eVerbose>=1 ){ |
| 9028 | utf8_printf(p->out, "%s\n", zCreate); |
| 9029 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9030 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9031 | if( rc ){ |
larrybr | ce0b5e4 | 2022-01-14 16:29:45 +0000 | [diff] [blame] | 9032 | utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9033 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9034 | } |
larrybr | ce0b5e4 | 2022-01-14 16:29:45 +0000 | [diff] [blame] | 9035 | sqlite3_free(zCreate); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9036 | zCreate = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9037 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9038 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9039 | if( rc ){ |
| 9040 | if (pStmt) sqlite3_finalize(pStmt); |
| 9041 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9042 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9043 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9044 | sqlite3_free(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9045 | nCol = sqlite3_column_count(pStmt); |
| 9046 | sqlite3_finalize(pStmt); |
| 9047 | pStmt = 0; |
| 9048 | if( nCol==0 ) return 0; /* no columns, no error */ |
| 9049 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
| 9050 | if( zSql==0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 9051 | import_cleanup(&sCtx); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 9052 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9053 | } |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 9054 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9055 | j = strlen30(zSql); |
| 9056 | for(i=1; i<nCol; i++){ |
| 9057 | zSql[j++] = ','; |
| 9058 | zSql[j++] = '?'; |
| 9059 | } |
| 9060 | zSql[j++] = ')'; |
| 9061 | zSql[j] = 0; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 9062 | if( eVerbose>=2 ){ |
| 9063 | utf8_printf(p->out, "Insert using: %s\n", zSql); |
| 9064 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9065 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9066 | if( rc ){ |
| 9067 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9068 | if (pStmt) sqlite3_finalize(pStmt); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9069 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9070 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 9071 | sqlite3_free(zSql); |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 9072 | sqlite3_free(zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9073 | needCommit = sqlite3_get_autocommit(p->db); |
| 9074 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 9075 | do{ |
| 9076 | int startLine = sCtx.nLine; |
| 9077 | for(i=0; i<nCol; i++){ |
| 9078 | char *z = xRead(&sCtx); |
| 9079 | /* |
| 9080 | ** Did we reach end-of-file before finding any columns? |
| 9081 | ** If so, stop instead of NULL filling the remaining columns. |
| 9082 | */ |
| 9083 | if( z==0 && i==0 ) break; |
| 9084 | /* |
| 9085 | ** Did we reach end-of-file OR end-of-line before finding any |
| 9086 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 9087 | ** the remaining columns. |
| 9088 | */ |
| 9089 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| 9090 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 9091 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 9092 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 9093 | "filling the rest with NULL\n", |
| 9094 | sCtx.zFile, startLine, nCol, i+1); |
| 9095 | i += 2; |
| 9096 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| 9097 | } |
| 9098 | } |
| 9099 | if( sCtx.cTerm==sCtx.cColSep ){ |
| 9100 | do{ |
| 9101 | xRead(&sCtx); |
| 9102 | i++; |
| 9103 | }while( sCtx.cTerm==sCtx.cColSep ); |
| 9104 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 9105 | "extras ignored\n", |
| 9106 | sCtx.zFile, startLine, nCol, i); |
| 9107 | } |
| 9108 | if( i>=nCol ){ |
| 9109 | sqlite3_step(pStmt); |
| 9110 | rc = sqlite3_reset(pStmt); |
| 9111 | if( rc!=SQLITE_OK ){ |
| 9112 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 9113 | startLine, sqlite3_errmsg(p->db)); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 9114 | sCtx.nErr++; |
| 9115 | }else{ |
| 9116 | sCtx.nRow++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9117 | } |
| 9118 | } |
| 9119 | }while( sCtx.cTerm!=EOF ); |
| 9120 | |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 9121 | import_cleanup(&sCtx); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9122 | sqlite3_finalize(pStmt); |
| 9123 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 9124 | if( eVerbose>0 ){ |
| 9125 | utf8_printf(p->out, |
| 9126 | "Added %d rows with %d errors using %d lines of input\n", |
| 9127 | sCtx.nRow, sCtx.nErr, sCtx.nLine-1); |
| 9128 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9129 | }else |
| 9130 | |
| 9131 | #ifndef SQLITE_UNTESTABLE |
| 9132 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 9133 | char *zSql; |
| 9134 | char *zCollist = 0; |
| 9135 | sqlite3_stmt *pStmt; |
| 9136 | int tnum = 0; |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9137 | int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ |
| 9138 | int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9139 | int i; |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 9140 | if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ |
| 9141 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" |
| 9142 | " .imposter off\n"); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9143 | /* Also allowed, but not documented: |
| 9144 | ** |
| 9145 | ** .imposter TABLE IMPOSTER |
| 9146 | ** |
| 9147 | ** where TABLE is a WITHOUT ROWID table. In that case, the |
| 9148 | ** imposter is another WITHOUT ROWID table with the columns in |
| 9149 | ** storage order. */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9150 | rc = 1; |
| 9151 | goto meta_command_exit; |
| 9152 | } |
| 9153 | open_db(p, 0); |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 9154 | if( nArg==2 ){ |
| 9155 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); |
| 9156 | goto meta_command_exit; |
| 9157 | } |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9158 | zSql = sqlite3_mprintf( |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 9159 | "SELECT rootpage, 0 FROM sqlite_schema" |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9160 | " WHERE name='%q' AND type='index'" |
| 9161 | "UNION ALL " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 9162 | "SELECT rootpage, 1 FROM sqlite_schema" |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9163 | " WHERE name='%q' AND type='table'" |
| 9164 | " AND sql LIKE '%%without%%rowid%%'", |
| 9165 | azArg[1], azArg[1] |
| 9166 | ); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9167 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9168 | sqlite3_free(zSql); |
| 9169 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 9170 | tnum = sqlite3_column_int(pStmt, 0); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9171 | isWO = sqlite3_column_int(pStmt, 1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9172 | } |
| 9173 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9174 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 9175 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9176 | sqlite3_free(zSql); |
| 9177 | i = 0; |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 9178 | while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9179 | char zLabel[20]; |
| 9180 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
| 9181 | i++; |
| 9182 | if( zCol==0 ){ |
| 9183 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 9184 | zCol = "_ROWID_"; |
| 9185 | }else{ |
| 9186 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 9187 | zCol = zLabel; |
| 9188 | } |
| 9189 | } |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9190 | if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ |
| 9191 | lenPK = (int)strlen(zCollist); |
| 9192 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9193 | if( zCollist==0 ){ |
| 9194 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 9195 | }else{ |
| 9196 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 9197 | } |
| 9198 | } |
| 9199 | sqlite3_finalize(pStmt); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9200 | if( i==0 || tnum==0 ){ |
| 9201 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 9202 | rc = 1; |
| 9203 | sqlite3_free(zCollist); |
| 9204 | goto meta_command_exit; |
| 9205 | } |
| 9206 | if( lenPK==0 ) lenPK = 100000; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9207 | zSql = sqlite3_mprintf( |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9208 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", |
| 9209 | azArg[2], zCollist, lenPK, zCollist); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9210 | sqlite3_free(zCollist); |
| 9211 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 9212 | if( rc==SQLITE_OK ){ |
| 9213 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 9214 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 9215 | if( rc ){ |
| 9216 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 9217 | }else{ |
| 9218 | utf8_printf(stdout, "%s;\n", zSql); |
| 9219 | raw_printf(stdout, |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 9220 | "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", |
| 9221 | azArg[1], isWO ? "table" : "index" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9222 | ); |
| 9223 | } |
| 9224 | }else{ |
| 9225 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
| 9226 | rc = 1; |
| 9227 | } |
| 9228 | sqlite3_free(zSql); |
| 9229 | }else |
| 9230 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 9231 | |
| 9232 | #ifdef SQLITE_ENABLE_IOTRACE |
| 9233 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
| 9234 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
| 9235 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 9236 | iotrace = 0; |
| 9237 | if( nArg<2 ){ |
| 9238 | sqlite3IoTrace = 0; |
| 9239 | }else if( strcmp(azArg[1], "-")==0 ){ |
| 9240 | sqlite3IoTrace = iotracePrintf; |
| 9241 | iotrace = stdout; |
| 9242 | }else{ |
| 9243 | iotrace = fopen(azArg[1], "w"); |
| 9244 | if( iotrace==0 ){ |
| 9245 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 9246 | sqlite3IoTrace = 0; |
| 9247 | rc = 1; |
| 9248 | }else{ |
| 9249 | sqlite3IoTrace = iotracePrintf; |
| 9250 | } |
| 9251 | } |
| 9252 | }else |
| 9253 | #endif |
| 9254 | |
| 9255 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 9256 | static const struct { |
| 9257 | const char *zLimitName; /* Name of a limit */ |
| 9258 | int limitCode; /* Integer code for that limit */ |
| 9259 | } aLimit[] = { |
| 9260 | { "length", SQLITE_LIMIT_LENGTH }, |
| 9261 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 9262 | { "column", SQLITE_LIMIT_COLUMN }, |
| 9263 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 9264 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 9265 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 9266 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 9267 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 9268 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 9269 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 9270 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 9271 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 9272 | }; |
| 9273 | int i, n2; |
| 9274 | open_db(p, 0); |
| 9275 | if( nArg==1 ){ |
| 9276 | for(i=0; i<ArraySize(aLimit); i++){ |
| 9277 | printf("%20s %d\n", aLimit[i].zLimitName, |
| 9278 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 9279 | } |
| 9280 | }else if( nArg>3 ){ |
| 9281 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
| 9282 | rc = 1; |
| 9283 | goto meta_command_exit; |
| 9284 | }else{ |
| 9285 | int iLimit = -1; |
| 9286 | n2 = strlen30(azArg[1]); |
| 9287 | for(i=0; i<ArraySize(aLimit); i++){ |
| 9288 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 9289 | if( iLimit<0 ){ |
| 9290 | iLimit = i; |
| 9291 | }else{ |
| 9292 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
| 9293 | rc = 1; |
| 9294 | goto meta_command_exit; |
| 9295 | } |
| 9296 | } |
| 9297 | } |
| 9298 | if( iLimit<0 ){ |
| 9299 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
| 9300 | "enter \".limits\" with no arguments for a list.\n", |
| 9301 | azArg[1]); |
| 9302 | rc = 1; |
| 9303 | goto meta_command_exit; |
| 9304 | } |
| 9305 | if( nArg==3 ){ |
| 9306 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 9307 | (int)integerValue(azArg[2])); |
| 9308 | } |
| 9309 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 9310 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 9311 | } |
| 9312 | }else |
| 9313 | |
| 9314 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
| 9315 | open_db(p, 0); |
| 9316 | lintDotCommand(p, azArg, nArg); |
| 9317 | }else |
| 9318 | |
| 9319 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 9320 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
| 9321 | const char *zFile, *zProc; |
| 9322 | char *zErrMsg = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9323 | failIfSafeMode(p, "cannot run .load in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9324 | if( nArg<2 ){ |
| 9325 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
| 9326 | rc = 1; |
| 9327 | goto meta_command_exit; |
| 9328 | } |
| 9329 | zFile = azArg[1]; |
| 9330 | zProc = nArg>=3 ? azArg[2] : 0; |
| 9331 | open_db(p, 0); |
| 9332 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 9333 | if( rc!=SQLITE_OK ){ |
| 9334 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
| 9335 | sqlite3_free(zErrMsg); |
| 9336 | rc = 1; |
| 9337 | } |
| 9338 | }else |
| 9339 | #endif |
| 9340 | |
| 9341 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9342 | failIfSafeMode(p, "cannot run .log in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9343 | if( nArg!=2 ){ |
| 9344 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 9345 | rc = 1; |
| 9346 | }else{ |
| 9347 | const char *zFile = azArg[1]; |
| 9348 | output_file_close(p->pLog); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9349 | p->pLog = output_file_open(zFile, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9350 | } |
| 9351 | }else |
| 9352 | |
| 9353 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9354 | const char *zMode = 0; |
| 9355 | const char *zTabname = 0; |
| 9356 | int i, n2; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9357 | ColModeOpts cmOpts = ColModeOpts_default; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9358 | for(i=1; i<nArg; i++){ |
| 9359 | const char *z = azArg[i]; |
| 9360 | if( optionMatch(z,"wrap") && i+1<nArg ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9361 | cmOpts.iWrap = integerValue(azArg[++i]); |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 9362 | }else if( optionMatch(z,"ww") ){ |
| 9363 | cmOpts.bWordWrap = 1; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9364 | }else if( optionMatch(z,"wordwrap") && i+1<nArg ){ |
| 9365 | cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9366 | }else if( optionMatch(z,"quote") ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9367 | cmOpts.bQuote = 1; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9368 | }else if( optionMatch(z,"noquote") ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9369 | cmOpts.bQuote = 0; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9370 | }else if( zMode==0 ){ |
| 9371 | zMode = z; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 9372 | /* Apply defaults for qbox pseudo-mods. If that |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9373 | * overwrites already-set values, user was informed of this. |
| 9374 | */ |
| 9375 | if( strcmp(z, "qbox")==0 ){ |
| 9376 | ColModeOpts cmo = ColModeOpts_default_qbox; |
| 9377 | zMode = "box"; |
| 9378 | cmOpts = cmo; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9379 | } |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9380 | }else if( zTabname==0 ){ |
| 9381 | zTabname = z; |
| 9382 | }else if( z[0]=='-' ){ |
| 9383 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 9384 | utf8_printf(stderr, "options:\n" |
| 9385 | " --noquote\n" |
| 9386 | " --quote\n" |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9387 | " --wordwrap on/off\n" |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 9388 | " --wrap N\n" |
| 9389 | " --ww\n"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9390 | rc = 1; |
| 9391 | goto meta_command_exit; |
| 9392 | }else{ |
| 9393 | utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
| 9394 | rc = 1; |
| 9395 | goto meta_command_exit; |
| 9396 | } |
| 9397 | } |
| 9398 | if( zMode==0 ){ |
| 9399 | if( p->mode==MODE_Column |
| 9400 | || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) |
| 9401 | ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9402 | raw_printf |
| 9403 | (p->out, |
| 9404 | "current output mode: %s --wrap %d --wordwrap %s --%squote\n", |
| 9405 | modeDescr[p->mode], p->cmOpts.iWrap, |
| 9406 | p->cmOpts.bWordWrap ? "on" : "off", |
| 9407 | p->cmOpts.bQuote ? "" : "no"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9408 | }else{ |
| 9409 | raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 9410 | } |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9411 | zMode = modeDescr[p->mode]; |
| 9412 | } |
| 9413 | n2 = strlen30(zMode); |
| 9414 | if( strncmp(zMode,"lines",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9415 | p->mode = MODE_Line; |
| 9416 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9417 | }else if( strncmp(zMode,"columns",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9418 | p->mode = MODE_Column; |
drh | c060508 | 2020-06-05 00:54:27 +0000 | [diff] [blame] | 9419 | if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ |
| 9420 | p->showHeader = 1; |
| 9421 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9422 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9423 | p->cmOpts = cmOpts; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9424 | }else if( strncmp(zMode,"list",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9425 | p->mode = MODE_List; |
| 9426 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 9427 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9428 | }else if( strncmp(zMode,"html",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9429 | p->mode = MODE_Html; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9430 | }else if( strncmp(zMode,"tcl",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9431 | p->mode = MODE_Tcl; |
| 9432 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
| 9433 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9434 | }else if( strncmp(zMode,"csv",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9435 | p->mode = MODE_Csv; |
| 9436 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 9437 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9438 | }else if( strncmp(zMode,"tabs",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9439 | p->mode = MODE_List; |
| 9440 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9441 | }else if( strncmp(zMode,"insert",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9442 | p->mode = MODE_Insert; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9443 | set_table_name(p, zTabname ? zTabname : "table"); |
| 9444 | }else if( strncmp(zMode,"quote",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9445 | p->mode = MODE_Quote; |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 9446 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 9447 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9448 | }else if( strncmp(zMode,"ascii",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9449 | p->mode = MODE_Ascii; |
| 9450 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 9451 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9452 | }else if( strncmp(zMode,"markdown",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9453 | p->mode = MODE_Markdown; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9454 | p->cmOpts = cmOpts; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9455 | }else if( strncmp(zMode,"table",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9456 | p->mode = MODE_Table; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9457 | p->cmOpts = cmOpts; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9458 | }else if( strncmp(zMode,"box",n2)==0 ){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 9459 | p->mode = MODE_Box; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9460 | p->cmOpts = cmOpts; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9461 | }else if( strncmp(zMode,"count",n2)==0 ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 9462 | p->mode = MODE_Count; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9463 | }else if( strncmp(zMode,"off",n2)==0 ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 9464 | p->mode = MODE_Off; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 9465 | }else if( strncmp(zMode,"json",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9466 | p->mode = MODE_Json; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9467 | }else{ |
| 9468 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 9469 | "ascii box column csv html insert json line list markdown " |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 9470 | "qbox quote table tabs tcl\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9471 | rc = 1; |
| 9472 | } |
| 9473 | p->cMode = p->mode; |
| 9474 | }else |
| 9475 | |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9476 | if( c=='n' && strcmp(azArg[0], "nonce")==0 ){ |
| 9477 | if( nArg!=2 ){ |
| 9478 | raw_printf(stderr, "Usage: .nonce NONCE\n"); |
| 9479 | rc = 1; |
| 9480 | }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){ |
drh | fe46317 | 2021-12-16 17:57:21 +0000 | [diff] [blame] | 9481 | raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", |
| 9482 | p->lineno, azArg[1]); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9483 | exit(1); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 9484 | }else{ |
| 9485 | p->bSafeMode = 0; |
| 9486 | return 0; /* Return immediately to bypass the safe mode reset |
| 9487 | ** at the end of this procedure */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9488 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9489 | }else |
| 9490 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9491 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 9492 | if( nArg==2 ){ |
| 9493 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 9494 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| 9495 | }else{ |
| 9496 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
| 9497 | rc = 1; |
| 9498 | } |
| 9499 | }else |
| 9500 | |
| 9501 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9502 | const char *zFN = 0; /* Pointer to constant filename */ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9503 | char *zNewFilename = 0; /* Name of the database file to open */ |
| 9504 | int iName = 1; /* Index in azArg[] of the filename */ |
| 9505 | int newFlag = 0; /* True to delete file before opening */ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9506 | int openMode = SHELL_OPEN_UNSPEC; |
| 9507 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9508 | /* Check for command-line arguments */ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9509 | for(iName=1; iName<nArg; iName++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9510 | const char *z = azArg[iName]; |
| 9511 | if( optionMatch(z,"new") ){ |
| 9512 | newFlag = 1; |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 9513 | #ifdef SQLITE_HAVE_ZLIB |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 9514 | }else if( optionMatch(z, "zip") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9515 | openMode = SHELL_OPEN_ZIPFILE; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 9516 | #endif |
| 9517 | }else if( optionMatch(z, "append") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9518 | openMode = SHELL_OPEN_APPENDVFS; |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 9519 | }else if( optionMatch(z, "readonly") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9520 | openMode = SHELL_OPEN_READONLY; |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 9521 | }else if( optionMatch(z, "nofollow") ){ |
| 9522 | p->openFlags |= SQLITE_OPEN_NOFOLLOW; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 9523 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 9524 | }else if( optionMatch(z, "deserialize") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9525 | openMode = SHELL_OPEN_DESERIALIZE; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 9526 | }else if( optionMatch(z, "hexdb") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9527 | openMode = SHELL_OPEN_HEXDB; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 9528 | }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ |
| 9529 | p->szMax = integerValue(azArg[++iName]); |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 9530 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9531 | }else if( z[0]=='-' ){ |
| 9532 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 9533 | rc = 1; |
| 9534 | goto meta_command_exit; |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9535 | }else if( zFN ){ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9536 | utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
| 9537 | rc = 1; |
| 9538 | goto meta_command_exit; |
| 9539 | }else{ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9540 | zFN = z; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9541 | } |
| 9542 | } |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9543 | |
| 9544 | /* Close the existing database */ |
| 9545 | session_close_all(p, -1); |
| 9546 | close_db(p->db); |
| 9547 | p->db = 0; |
| 9548 | p->pAuxDb->zDbFilename = 0; |
| 9549 | sqlite3_free(p->pAuxDb->zFreeOnClose); |
| 9550 | p->pAuxDb->zFreeOnClose = 0; |
| 9551 | p->openMode = openMode; |
| 9552 | p->openFlags = 0; |
| 9553 | p->szMax = 0; |
| 9554 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9555 | /* If a filename is specified, try to open it first */ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9556 | if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ |
drh | c320e06 | 2021-12-24 19:44:11 +0000 | [diff] [blame] | 9557 | if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9558 | if( p->bSafeMode |
| 9559 | && p->openMode!=SHELL_OPEN_HEXDB |
drh | c320e06 | 2021-12-24 19:44:11 +0000 | [diff] [blame] | 9560 | && zFN |
| 9561 | && strcmp(zFN,":memory:")!=0 |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9562 | ){ |
| 9563 | failIfSafeMode(p, "cannot open disk-based database files in safe mode"); |
| 9564 | } |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9565 | if( zFN ){ |
| 9566 | zNewFilename = sqlite3_mprintf("%s", zFN); |
| 9567 | shell_check_oom(zNewFilename); |
| 9568 | }else{ |
| 9569 | zNewFilename = 0; |
| 9570 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9571 | p->pAuxDb->zDbFilename = zNewFilename; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 9572 | open_db(p, OPEN_DB_KEEPALIVE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9573 | if( p->db==0 ){ |
| 9574 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 9575 | sqlite3_free(zNewFilename); |
| 9576 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9577 | p->pAuxDb->zFreeOnClose = zNewFilename; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9578 | } |
| 9579 | } |
| 9580 | if( p->db==0 ){ |
| 9581 | /* As a fall-back open a TEMP database */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9582 | p->pAuxDb->zDbFilename = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9583 | open_db(p, 0); |
| 9584 | } |
| 9585 | }else |
| 9586 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9587 | if( (c=='o' |
| 9588 | && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) |
| 9589 | || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9590 | ){ |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9591 | char *zFile = 0; |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9592 | int bTxtMode = 0; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9593 | int i; |
| 9594 | int eMode = 0; |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9595 | int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ |
| 9596 | unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9597 | |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9598 | zBOM[0] = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9599 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9600 | if( c=='e' ){ |
| 9601 | eMode = 'x'; |
| 9602 | bOnce = 2; |
| 9603 | }else if( strncmp(azArg[0],"once",n)==0 ){ |
| 9604 | bOnce = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9605 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9606 | for(i=1; i<nArg; i++){ |
| 9607 | char *z = azArg[i]; |
| 9608 | if( z[0]=='-' ){ |
| 9609 | if( z[1]=='-' ) z++; |
| 9610 | if( strcmp(z,"-bom")==0 ){ |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9611 | zBOM[0] = 0xef; |
| 9612 | zBOM[1] = 0xbb; |
| 9613 | zBOM[2] = 0xbf; |
| 9614 | zBOM[3] = 0; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9615 | }else if( c!='e' && strcmp(z,"-x")==0 ){ |
| 9616 | eMode = 'x'; /* spreadsheet */ |
| 9617 | }else if( c!='e' && strcmp(z,"-e")==0 ){ |
| 9618 | eMode = 'e'; /* text editor */ |
| 9619 | }else{ |
| 9620 | utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", |
| 9621 | azArg[i]); |
| 9622 | showHelp(p->out, azArg[0]); |
| 9623 | rc = 1; |
| 9624 | goto meta_command_exit; |
| 9625 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9626 | }else if( zFile==0 && eMode!='e' && eMode!='x' ){ |
| 9627 | zFile = sqlite3_mprintf("%s", z); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9628 | if( zFile && zFile[0]=='|' ){ |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9629 | while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]); |
| 9630 | break; |
| 9631 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9632 | }else{ |
| 9633 | utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", |
| 9634 | azArg[i]); |
| 9635 | showHelp(p->out, azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9636 | rc = 1; |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9637 | sqlite3_free(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9638 | goto meta_command_exit; |
| 9639 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9640 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9641 | if( zFile==0 ){ |
| 9642 | zFile = sqlite3_mprintf("stdout"); |
| 9643 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9644 | if( bOnce ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9645 | p->outCount = 2; |
| 9646 | }else{ |
| 9647 | p->outCount = 0; |
| 9648 | } |
| 9649 | output_reset(p); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 9650 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9651 | if( eMode=='e' || eMode=='x' ){ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 9652 | p->doXdgOpen = 1; |
| 9653 | outputModePush(p); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9654 | if( eMode=='x' ){ |
| 9655 | /* spreadsheet mode. Output as CSV. */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9656 | newTempFile(p, "csv"); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9657 | ShellClearFlag(p, SHFLG_Echo); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9658 | p->mode = MODE_Csv; |
| 9659 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 9660 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 9661 | }else{ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9662 | /* text editor mode */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9663 | newTempFile(p, "txt"); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9664 | bTxtMode = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9665 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9666 | sqlite3_free(zFile); |
| 9667 | zFile = sqlite3_mprintf("%s", p->zTempFile); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9668 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 9669 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9670 | shell_check_oom(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9671 | if( zFile[0]=='|' ){ |
| 9672 | #ifdef SQLITE_OMIT_POPEN |
| 9673 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 9674 | rc = 1; |
| 9675 | p->out = stdout; |
| 9676 | #else |
| 9677 | p->out = popen(zFile + 1, "w"); |
| 9678 | if( p->out==0 ){ |
| 9679 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
| 9680 | p->out = stdout; |
| 9681 | rc = 1; |
| 9682 | }else{ |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9683 | if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9684 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 9685 | } |
| 9686 | #endif |
| 9687 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9688 | p->out = output_file_open(zFile, bTxtMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9689 | if( p->out==0 ){ |
| 9690 | if( strcmp(zFile,"off")!=0 ){ |
| 9691 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
| 9692 | } |
| 9693 | p->out = stdout; |
| 9694 | rc = 1; |
| 9695 | } else { |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9696 | if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9697 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 9698 | } |
| 9699 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9700 | sqlite3_free(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9701 | }else |
| 9702 | |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9703 | if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ |
| 9704 | open_db(p,0); |
| 9705 | if( nArg<=1 ) goto parameter_syntax_error; |
| 9706 | |
| 9707 | /* .parameter clear |
| 9708 | ** Clear all bind parameters by dropping the TEMP table that holds them. |
| 9709 | */ |
| 9710 | if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9711 | sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9712 | 0, 0, 0); |
| 9713 | }else |
| 9714 | |
| 9715 | /* .parameter list |
| 9716 | ** List all bind parameters. |
| 9717 | */ |
| 9718 | if( nArg==2 && strcmp(azArg[1],"list")==0 ){ |
| 9719 | sqlite3_stmt *pStmt = 0; |
| 9720 | int rx; |
| 9721 | int len = 0; |
| 9722 | rx = sqlite3_prepare_v2(p->db, |
| 9723 | "SELECT max(length(key)) " |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9724 | "FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9725 | if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 9726 | len = sqlite3_column_int(pStmt, 0); |
| 9727 | if( len>40 ) len = 40; |
| 9728 | } |
| 9729 | sqlite3_finalize(pStmt); |
| 9730 | pStmt = 0; |
| 9731 | if( len ){ |
| 9732 | rx = sqlite3_prepare_v2(p->db, |
| 9733 | "SELECT key, quote(value) " |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9734 | "FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 9735 | while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9736 | utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), |
| 9737 | sqlite3_column_text(pStmt,1)); |
| 9738 | } |
| 9739 | sqlite3_finalize(pStmt); |
| 9740 | } |
| 9741 | }else |
| 9742 | |
| 9743 | /* .parameter init |
| 9744 | ** Make sure the TEMP table used to hold bind parameters exists. |
| 9745 | ** Create it if necessary. |
| 9746 | */ |
| 9747 | if( nArg==2 && strcmp(azArg[1],"init")==0 ){ |
| 9748 | bind_table_init(p); |
| 9749 | }else |
| 9750 | |
| 9751 | /* .parameter set NAME VALUE |
| 9752 | ** Set or reset a bind parameter. NAME should be the full parameter |
| 9753 | ** name exactly as it appears in the query. (ex: $abc, @def). The |
| 9754 | ** VALUE can be in either SQL literal notation, or if not it will be |
| 9755 | ** understood to be a text string. |
| 9756 | */ |
| 9757 | if( nArg==4 && strcmp(azArg[1],"set")==0 ){ |
| 9758 | int rx; |
| 9759 | char *zSql; |
| 9760 | sqlite3_stmt *pStmt; |
| 9761 | const char *zKey = azArg[2]; |
| 9762 | const char *zValue = azArg[3]; |
| 9763 | bind_table_init(p); |
| 9764 | zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9765 | "REPLACE INTO temp.sqlite_parameters(key,value)" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9766 | "VALUES(%Q,%s);", zKey, zValue); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9767 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9768 | pStmt = 0; |
| 9769 | rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9770 | sqlite3_free(zSql); |
| 9771 | if( rx!=SQLITE_OK ){ |
| 9772 | sqlite3_finalize(pStmt); |
| 9773 | pStmt = 0; |
| 9774 | zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9775 | "REPLACE INTO temp.sqlite_parameters(key,value)" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9776 | "VALUES(%Q,%Q);", zKey, zValue); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9777 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9778 | rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9779 | sqlite3_free(zSql); |
| 9780 | if( rx!=SQLITE_OK ){ |
| 9781 | utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9782 | sqlite3_finalize(pStmt); |
| 9783 | pStmt = 0; |
| 9784 | rc = 1; |
| 9785 | } |
| 9786 | } |
| 9787 | sqlite3_step(pStmt); |
| 9788 | sqlite3_finalize(pStmt); |
| 9789 | }else |
| 9790 | |
| 9791 | /* .parameter unset NAME |
| 9792 | ** Remove the NAME binding from the parameter binding table, if it |
| 9793 | ** exists. |
| 9794 | */ |
| 9795 | if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ |
| 9796 | char *zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9797 | "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9798 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9799 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 9800 | sqlite3_free(zSql); |
| 9801 | }else |
| 9802 | /* If no command name matches, show a syntax error */ |
| 9803 | parameter_syntax_error: |
| 9804 | showHelp(p->out, "parameter"); |
| 9805 | }else |
| 9806 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9807 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 9808 | int i; |
| 9809 | for(i=1; i<nArg; i++){ |
| 9810 | if( i>1 ) raw_printf(p->out, " "); |
| 9811 | utf8_printf(p->out, "%s", azArg[i]); |
| 9812 | } |
| 9813 | raw_printf(p->out, "\n"); |
| 9814 | }else |
| 9815 | |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 9816 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9817 | if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ |
| 9818 | int i; |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9819 | int nn = 0; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9820 | p->flgProgress = 0; |
| 9821 | p->mxProgress = 0; |
| 9822 | p->nProgress = 0; |
| 9823 | for(i=1; i<nArg; i++){ |
| 9824 | const char *z = azArg[i]; |
| 9825 | if( z[0]=='-' ){ |
| 9826 | z++; |
| 9827 | if( z[0]=='-' ) z++; |
| 9828 | if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9829 | p->flgProgress |= SHELL_PROGRESS_QUIET; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9830 | continue; |
| 9831 | } |
| 9832 | if( strcmp(z,"reset")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9833 | p->flgProgress |= SHELL_PROGRESS_RESET; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9834 | continue; |
| 9835 | } |
| 9836 | if( strcmp(z,"once")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9837 | p->flgProgress |= SHELL_PROGRESS_ONCE; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9838 | continue; |
| 9839 | } |
| 9840 | if( strcmp(z,"limit")==0 ){ |
| 9841 | if( i+1>=nArg ){ |
| 9842 | utf8_printf(stderr, "Error: missing argument on --limit\n"); |
| 9843 | rc = 1; |
| 9844 | goto meta_command_exit; |
| 9845 | }else{ |
| 9846 | p->mxProgress = (int)integerValue(azArg[++i]); |
| 9847 | } |
| 9848 | continue; |
| 9849 | } |
| 9850 | utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); |
| 9851 | rc = 1; |
| 9852 | goto meta_command_exit; |
| 9853 | }else{ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9854 | nn = (int)integerValue(z); |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9855 | } |
| 9856 | } |
| 9857 | open_db(p, 0); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9858 | sqlite3_progress_handler(p->db, nn, progress_handler, p); |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9859 | }else |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 9860 | #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9861 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9862 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
| 9863 | if( nArg >= 2) { |
| 9864 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 9865 | } |
| 9866 | if( nArg >= 3) { |
| 9867 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 9868 | } |
| 9869 | }else |
| 9870 | |
| 9871 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
| 9872 | rc = 2; |
| 9873 | }else |
| 9874 | |
| 9875 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9876 | FILE *inSaved = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 9877 | int savedLineno = p->lineno; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9878 | failIfSafeMode(p, "cannot run .read in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9879 | if( nArg!=2 ){ |
| 9880 | raw_printf(stderr, "Usage: .read FILE\n"); |
| 9881 | rc = 1; |
| 9882 | goto meta_command_exit; |
| 9883 | } |
drh | 30497f4 | 2020-08-26 10:50:48 +0000 | [diff] [blame] | 9884 | if( azArg[1][0]=='|' ){ |
drh | 9d59e3b | 2021-03-12 01:49:08 +0000 | [diff] [blame] | 9885 | #ifdef SQLITE_OMIT_POPEN |
| 9886 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 9887 | rc = 1; |
| 9888 | p->out = stdout; |
| 9889 | #else |
drh | 30497f4 | 2020-08-26 10:50:48 +0000 | [diff] [blame] | 9890 | p->in = popen(azArg[1]+1, "r"); |
| 9891 | if( p->in==0 ){ |
| 9892 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 9893 | rc = 1; |
| 9894 | }else{ |
| 9895 | rc = process_input(p); |
| 9896 | pclose(p->in); |
| 9897 | } |
drh | 9d59e3b | 2021-03-12 01:49:08 +0000 | [diff] [blame] | 9898 | #endif |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 9899 | }else if( (p->in = openChrSource(azArg[1]))==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9900 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 9901 | rc = 1; |
| 9902 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9903 | rc = process_input(p); |
| 9904 | fclose(p->in); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9905 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9906 | p->in = inSaved; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 9907 | p->lineno = savedLineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9908 | }else |
| 9909 | |
| 9910 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
| 9911 | const char *zSrcFile; |
| 9912 | const char *zDb; |
| 9913 | sqlite3 *pSrc; |
| 9914 | sqlite3_backup *pBackup; |
| 9915 | int nTimeout = 0; |
| 9916 | |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9917 | failIfSafeMode(p, "cannot run .restore in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9918 | if( nArg==2 ){ |
| 9919 | zSrcFile = azArg[1]; |
| 9920 | zDb = "main"; |
| 9921 | }else if( nArg==3 ){ |
| 9922 | zSrcFile = azArg[2]; |
| 9923 | zDb = azArg[1]; |
| 9924 | }else{ |
| 9925 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
| 9926 | rc = 1; |
| 9927 | goto meta_command_exit; |
| 9928 | } |
| 9929 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 9930 | if( rc!=SQLITE_OK ){ |
| 9931 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9932 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9933 | return 1; |
| 9934 | } |
| 9935 | open_db(p, 0); |
| 9936 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 9937 | if( pBackup==0 ){ |
| 9938 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9939 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9940 | return 1; |
| 9941 | } |
| 9942 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 9943 | || rc==SQLITE_BUSY ){ |
| 9944 | if( rc==SQLITE_BUSY ){ |
| 9945 | if( nTimeout++ >= 3 ) break; |
| 9946 | sqlite3_sleep(100); |
| 9947 | } |
| 9948 | } |
| 9949 | sqlite3_backup_finish(pBackup); |
| 9950 | if( rc==SQLITE_DONE ){ |
| 9951 | rc = 0; |
| 9952 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 9953 | raw_printf(stderr, "Error: source database is busy\n"); |
| 9954 | rc = 1; |
| 9955 | }else{ |
| 9956 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9957 | rc = 1; |
| 9958 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9959 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9960 | }else |
| 9961 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9962 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 9963 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 9964 | p->scanstatsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9965 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9966 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| 9967 | #endif |
| 9968 | }else{ |
| 9969 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| 9970 | rc = 1; |
| 9971 | } |
| 9972 | }else |
| 9973 | |
| 9974 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 9975 | ShellText sSelect; |
| 9976 | ShellState data; |
| 9977 | char *zErrMsg = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 9978 | const char *zDiv = "("; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9979 | const char *zName = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9980 | int iSchema = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9981 | int bDebug = 0; |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9982 | int bNoSystemTabs = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9983 | int ii; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9984 | |
| 9985 | open_db(p, 0); |
| 9986 | memcpy(&data, p, sizeof(data)); |
| 9987 | data.showHeader = 0; |
| 9988 | data.cMode = data.mode = MODE_Semi; |
| 9989 | initText(&sSelect); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9990 | for(ii=1; ii<nArg; ii++){ |
| 9991 | if( optionMatch(azArg[ii],"indent") ){ |
| 9992 | data.cMode = data.mode = MODE_Pretty; |
| 9993 | }else if( optionMatch(azArg[ii],"debug") ){ |
| 9994 | bDebug = 1; |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9995 | }else if( optionMatch(azArg[ii],"nosys") ){ |
| 9996 | bNoSystemTabs = 1; |
| 9997 | }else if( azArg[ii][0]=='-' ){ |
| 9998 | utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]); |
| 9999 | rc = 1; |
| 10000 | goto meta_command_exit; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10001 | }else if( zName==0 ){ |
| 10002 | zName = azArg[ii]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10003 | }else{ |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 10004 | raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10005 | rc = 1; |
| 10006 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10007 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10008 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10009 | if( zName!=0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10010 | int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 10011 | || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 |
| 10012 | || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 |
| 10013 | || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10014 | if( isSchema ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10015 | char *new_argv[2], *new_colv[2]; |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 10016 | new_argv[0] = sqlite3_mprintf( |
| 10017 | "CREATE TABLE %s (\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10018 | " type text,\n" |
| 10019 | " name text,\n" |
| 10020 | " tbl_name text,\n" |
| 10021 | " rootpage integer,\n" |
| 10022 | " sql text\n" |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 10023 | ")", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10024 | shell_check_oom(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10025 | new_argv[1] = 0; |
| 10026 | new_colv[0] = "sql"; |
| 10027 | new_colv[1] = 0; |
| 10028 | callback(&data, 1, new_argv, new_colv); |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 10029 | sqlite3_free(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10030 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10031 | } |
| 10032 | if( zDiv ){ |
| 10033 | sqlite3_stmt *pStmt = 0; |
| 10034 | rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 10035 | -1, &pStmt, 0); |
| 10036 | if( rc ){ |
| 10037 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 10038 | sqlite3_finalize(pStmt); |
| 10039 | rc = 1; |
| 10040 | goto meta_command_exit; |
| 10041 | } |
| 10042 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 10043 | iSchema = 0; |
| 10044 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 10045 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 10046 | char zScNum[30]; |
| 10047 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 10048 | appendText(&sSelect, zDiv, 0); |
| 10049 | zDiv = " UNION ALL "; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10050 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
| 10051 | if( sqlite3_stricmp(zDb, "main")!=0 ){ |
drh | ea38f4f | 2019-07-13 17:21:47 +0000 | [diff] [blame] | 10052 | appendText(&sSelect, zDb, '\''); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10053 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10054 | appendText(&sSelect, "NULL", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10055 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10056 | appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); |
| 10057 | appendText(&sSelect, zScNum, 0); |
| 10058 | appendText(&sSelect, " AS snum, ", 0); |
| 10059 | appendText(&sSelect, zDb, '\''); |
| 10060 | appendText(&sSelect, " AS sname FROM ", 0); |
drh | ea38f4f | 2019-07-13 17:21:47 +0000 | [diff] [blame] | 10061 | appendText(&sSelect, zDb, quoteChar(zDb)); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10062 | appendText(&sSelect, ".sqlite_schema", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10063 | } |
| 10064 | sqlite3_finalize(pStmt); |
drh | cc3f3d1 | 2019-08-17 15:27:58 +0000 | [diff] [blame] | 10065 | #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 10066 | if( zName ){ |
| 10067 | appendText(&sSelect, |
| 10068 | " UNION ALL SELECT shell_module_schema(name)," |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10069 | " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", |
| 10070 | 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 10071 | } |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 10072 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10073 | appendText(&sSelect, ") WHERE ", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10074 | if( zName ){ |
| 10075 | char *zQarg = sqlite3_mprintf("%Q", zName); |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 10076 | int bGlob; |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10077 | shell_check_oom(zQarg); |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 10078 | bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || |
| 10079 | strchr(zName, '[') != 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10080 | if( strchr(zName, '.') ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10081 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 10082 | }else{ |
| 10083 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 10084 | } |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 10085 | appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10086 | appendText(&sSelect, zQarg, 0); |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 10087 | if( !bGlob ){ |
| 10088 | appendText(&sSelect, " ESCAPE '\\' ", 0); |
| 10089 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10090 | appendText(&sSelect, " AND ", 0); |
| 10091 | sqlite3_free(zQarg); |
| 10092 | } |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 10093 | if( bNoSystemTabs ){ |
| 10094 | appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); |
| 10095 | } |
| 10096 | appendText(&sSelect, "sql IS NOT NULL" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10097 | " ORDER BY snum, rowid", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 10098 | if( bDebug ){ |
| 10099 | utf8_printf(p->out, "SQL: %s;\n", sSelect.z); |
| 10100 | }else{ |
| 10101 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 10102 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10103 | freeText(&sSelect); |
| 10104 | } |
| 10105 | if( zErrMsg ){ |
| 10106 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 10107 | sqlite3_free(zErrMsg); |
| 10108 | rc = 1; |
| 10109 | }else if( rc != SQLITE_OK ){ |
| 10110 | raw_printf(stderr,"Error: querying schema information\n"); |
| 10111 | rc = 1; |
| 10112 | }else{ |
| 10113 | rc = 0; |
| 10114 | } |
| 10115 | }else |
| 10116 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 10117 | if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0) |
| 10118 | || (c=='t' && n==9 && strncmp(azArg[0], "treetrace", n)==0) |
| 10119 | ){ |
drh | fda8e49 | 2020-12-04 16:04:45 +0000 | [diff] [blame] | 10120 | unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 10121 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10122 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10123 | |
| 10124 | #if defined(SQLITE_ENABLE_SESSION) |
| 10125 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10126 | struct AuxDb *pAuxDb = p->pAuxDb; |
| 10127 | OpenSession *pSession = &pAuxDb->aSession[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10128 | char **azCmd = &azArg[1]; |
| 10129 | int iSes = 0; |
| 10130 | int nCmd = nArg - 1; |
| 10131 | int i; |
| 10132 | if( nArg<=1 ) goto session_syntax_error; |
| 10133 | open_db(p, 0); |
| 10134 | if( nArg>=3 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10135 | for(iSes=0; iSes<pAuxDb->nSession; iSes++){ |
| 10136 | if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10137 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10138 | if( iSes<pAuxDb->nSession ){ |
| 10139 | pSession = &pAuxDb->aSession[iSes]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10140 | azCmd++; |
| 10141 | nCmd--; |
| 10142 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10143 | pSession = &pAuxDb->aSession[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10144 | iSes = 0; |
| 10145 | } |
| 10146 | } |
| 10147 | |
| 10148 | /* .session attach TABLE |
| 10149 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 10150 | ** table so that it is never filtered. |
| 10151 | */ |
| 10152 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 10153 | if( nCmd!=2 ) goto session_syntax_error; |
| 10154 | if( pSession->p==0 ){ |
| 10155 | session_not_open: |
| 10156 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
| 10157 | }else{ |
| 10158 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 10159 | if( rc ){ |
| 10160 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| 10161 | rc = 0; |
| 10162 | } |
| 10163 | } |
| 10164 | }else |
| 10165 | |
| 10166 | /* .session changeset FILE |
| 10167 | ** .session patchset FILE |
| 10168 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 10169 | */ |
| 10170 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 10171 | FILE *out = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 10172 | failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10173 | if( nCmd!=2 ) goto session_syntax_error; |
| 10174 | if( pSession->p==0 ) goto session_not_open; |
| 10175 | out = fopen(azCmd[1], "wb"); |
| 10176 | if( out==0 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10177 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", |
| 10178 | azCmd[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10179 | }else{ |
| 10180 | int szChng; |
| 10181 | void *pChng; |
| 10182 | if( azCmd[0][0]=='c' ){ |
| 10183 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| 10184 | }else{ |
| 10185 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 10186 | } |
| 10187 | if( rc ){ |
| 10188 | printf("Error: error code %d\n", rc); |
| 10189 | rc = 0; |
| 10190 | } |
| 10191 | if( pChng |
| 10192 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
| 10193 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| 10194 | szChng); |
| 10195 | } |
| 10196 | sqlite3_free(pChng); |
| 10197 | fclose(out); |
| 10198 | } |
| 10199 | }else |
| 10200 | |
| 10201 | /* .session close |
| 10202 | ** Close the identified session |
| 10203 | */ |
| 10204 | if( strcmp(azCmd[0], "close")==0 ){ |
| 10205 | if( nCmd!=1 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10206 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10207 | session_close(pSession); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10208 | pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10209 | } |
| 10210 | }else |
| 10211 | |
| 10212 | /* .session enable ?BOOLEAN? |
| 10213 | ** Query or set the enable flag |
| 10214 | */ |
| 10215 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 10216 | int ii; |
| 10217 | if( nCmd>2 ) goto session_syntax_error; |
| 10218 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10219 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10220 | ii = sqlite3session_enable(pSession->p, ii); |
| 10221 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 10222 | pSession->zName, ii); |
| 10223 | } |
| 10224 | }else |
| 10225 | |
| 10226 | /* .session filter GLOB .... |
| 10227 | ** Set a list of GLOB patterns of table names to be excluded. |
| 10228 | */ |
| 10229 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 10230 | int ii, nByte; |
| 10231 | if( nCmd<2 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10232 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10233 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 10234 | sqlite3_free(pSession->azFilter[ii]); |
| 10235 | } |
| 10236 | sqlite3_free(pSession->azFilter); |
| 10237 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 10238 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 10239 | if( pSession->azFilter==0 ){ |
| 10240 | raw_printf(stderr, "Error: out or memory\n"); |
| 10241 | exit(1); |
| 10242 | } |
| 10243 | for(ii=1; ii<nCmd; ii++){ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10244 | char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| 10245 | shell_check_oom(x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10246 | } |
| 10247 | pSession->nFilter = ii-1; |
| 10248 | } |
| 10249 | }else |
| 10250 | |
| 10251 | /* .session indirect ?BOOLEAN? |
| 10252 | ** Query or set the indirect flag |
| 10253 | */ |
| 10254 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 10255 | int ii; |
| 10256 | if( nCmd>2 ) goto session_syntax_error; |
| 10257 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10258 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10259 | ii = sqlite3session_indirect(pSession->p, ii); |
| 10260 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 10261 | pSession->zName, ii); |
| 10262 | } |
| 10263 | }else |
| 10264 | |
| 10265 | /* .session isempty |
| 10266 | ** Determine if the session is empty |
| 10267 | */ |
| 10268 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 10269 | int ii; |
| 10270 | if( nCmd!=1 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10271 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10272 | ii = sqlite3session_isempty(pSession->p); |
| 10273 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 10274 | pSession->zName, ii); |
| 10275 | } |
| 10276 | }else |
| 10277 | |
| 10278 | /* .session list |
| 10279 | ** List all currently open sessions |
| 10280 | */ |
| 10281 | if( strcmp(azCmd[0],"list")==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10282 | for(i=0; i<pAuxDb->nSession; i++){ |
| 10283 | utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10284 | } |
| 10285 | }else |
| 10286 | |
| 10287 | /* .session open DB NAME |
| 10288 | ** Open a new session called NAME on the attached database DB. |
| 10289 | ** DB is normally "main". |
| 10290 | */ |
| 10291 | if( strcmp(azCmd[0],"open")==0 ){ |
| 10292 | char *zName; |
| 10293 | if( nCmd!=3 ) goto session_syntax_error; |
| 10294 | zName = azCmd[2]; |
| 10295 | if( zName[0]==0 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10296 | for(i=0; i<pAuxDb->nSession; i++){ |
| 10297 | if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10298 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| 10299 | goto meta_command_exit; |
| 10300 | } |
| 10301 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10302 | if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ |
| 10303 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10304 | goto meta_command_exit; |
| 10305 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10306 | pSession = &pAuxDb->aSession[pAuxDb->nSession]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10307 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 10308 | if( rc ){ |
| 10309 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| 10310 | rc = 0; |
| 10311 | goto meta_command_exit; |
| 10312 | } |
| 10313 | pSession->nFilter = 0; |
| 10314 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10315 | pAuxDb->nSession++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10316 | pSession->zName = sqlite3_mprintf("%s", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10317 | shell_check_oom(pSession->zName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10318 | }else |
| 10319 | /* If no command name matches, show a syntax error */ |
| 10320 | session_syntax_error: |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 10321 | showHelp(p->out, "session"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10322 | }else |
| 10323 | #endif |
| 10324 | |
| 10325 | #ifdef SQLITE_DEBUG |
| 10326 | /* Undocumented commands for internal testing. Subject to change |
| 10327 | ** without notice. */ |
| 10328 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 10329 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 10330 | int i, v; |
| 10331 | for(i=1; i<nArg; i++){ |
| 10332 | v = booleanValue(azArg[i]); |
| 10333 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| 10334 | } |
| 10335 | } |
| 10336 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 10337 | int i; sqlite3_int64 v; |
| 10338 | for(i=1; i<nArg; i++){ |
| 10339 | char zBuf[200]; |
| 10340 | v = integerValue(azArg[i]); |
| 10341 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| 10342 | utf8_printf(p->out, "%s", zBuf); |
| 10343 | } |
| 10344 | } |
| 10345 | }else |
| 10346 | #endif |
| 10347 | |
| 10348 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 10349 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 10350 | int bVerbose = 0; /* Verbose output */ |
| 10351 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 10352 | int i, k; /* Loop counters */ |
| 10353 | int nTest = 0; /* Number of tests runs */ |
| 10354 | int nErr = 0; /* Number of errors seen */ |
| 10355 | ShellText str; /* Answer for a query */ |
| 10356 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
| 10357 | |
| 10358 | open_db(p,0); |
| 10359 | for(i=1; i<nArg; i++){ |
| 10360 | const char *z = azArg[i]; |
| 10361 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 10362 | if( strcmp(z,"-init")==0 ){ |
| 10363 | bIsInit = 1; |
| 10364 | }else |
| 10365 | if( strcmp(z,"-v")==0 ){ |
| 10366 | bVerbose++; |
| 10367 | }else |
| 10368 | { |
| 10369 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 10370 | azArg[i], azArg[0]); |
| 10371 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 10372 | rc = 1; |
| 10373 | goto meta_command_exit; |
| 10374 | } |
| 10375 | } |
| 10376 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 10377 | != SQLITE_OK ){ |
| 10378 | bSelftestExists = 0; |
| 10379 | }else{ |
| 10380 | bSelftestExists = 1; |
| 10381 | } |
| 10382 | if( bIsInit ){ |
| 10383 | createSelftestTable(p); |
| 10384 | bSelftestExists = 1; |
| 10385 | } |
| 10386 | initText(&str); |
| 10387 | appendText(&str, "x", 0); |
| 10388 | for(k=bSelftestExists; k>=0; k--){ |
| 10389 | if( k==1 ){ |
| 10390 | rc = sqlite3_prepare_v2(p->db, |
| 10391 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 10392 | -1, &pStmt, 0); |
| 10393 | }else{ |
| 10394 | rc = sqlite3_prepare_v2(p->db, |
| 10395 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 10396 | " (1,'run','PRAGMA integrity_check','ok')", |
| 10397 | -1, &pStmt, 0); |
| 10398 | } |
| 10399 | if( rc ){ |
| 10400 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 10401 | rc = 1; |
| 10402 | sqlite3_finalize(pStmt); |
| 10403 | goto meta_command_exit; |
| 10404 | } |
| 10405 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 10406 | int tno = sqlite3_column_int(pStmt, 0); |
| 10407 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 10408 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 10409 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
| 10410 | |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 10411 | if( zOp==0 ) continue; |
| 10412 | if( zSql==0 ) continue; |
| 10413 | if( zAns==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10414 | k = 0; |
| 10415 | if( bVerbose>0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10416 | printf("%d: %s %s\n", tno, zOp, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10417 | } |
| 10418 | if( strcmp(zOp,"memo")==0 ){ |
| 10419 | utf8_printf(p->out, "%s\n", zSql); |
| 10420 | }else |
| 10421 | if( strcmp(zOp,"run")==0 ){ |
| 10422 | char *zErrMsg = 0; |
| 10423 | str.n = 0; |
| 10424 | str.z[0] = 0; |
| 10425 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 10426 | nTest++; |
| 10427 | if( bVerbose ){ |
| 10428 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 10429 | } |
| 10430 | if( rc || zErrMsg ){ |
| 10431 | nErr++; |
| 10432 | rc = 1; |
| 10433 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 10434 | sqlite3_free(zErrMsg); |
| 10435 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 10436 | nErr++; |
| 10437 | rc = 1; |
| 10438 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 10439 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 10440 | } |
| 10441 | }else |
| 10442 | { |
| 10443 | utf8_printf(stderr, |
| 10444 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 10445 | rc = 1; |
| 10446 | break; |
| 10447 | } |
| 10448 | } /* End loop over rows of content from SELFTEST */ |
| 10449 | sqlite3_finalize(pStmt); |
| 10450 | } /* End loop over k */ |
| 10451 | freeText(&str); |
| 10452 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 10453 | }else |
| 10454 | |
| 10455 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| 10456 | if( nArg<2 || nArg>3 ){ |
| 10457 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 10458 | rc = 1; |
| 10459 | } |
| 10460 | if( nArg>=2 ){ |
| 10461 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| 10462 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| 10463 | } |
| 10464 | if( nArg>=3 ){ |
| 10465 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 10466 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 10467 | } |
| 10468 | }else |
| 10469 | |
| 10470 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 10471 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 10472 | int i; /* Loop counter */ |
| 10473 | int bSchema = 0; /* Also hash the schema */ |
| 10474 | int bSeparate = 0; /* Hash each table separately */ |
| 10475 | int iSize = 224; /* Hash algorithm to use */ |
| 10476 | int bDebug = 0; /* Only show the query that would have run */ |
| 10477 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 10478 | char *zSql; /* SQL to be run */ |
| 10479 | char *zSep; /* Separator */ |
| 10480 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
| 10481 | ShellText sQuery; /* Set of queries used to read all content */ |
| 10482 | open_db(p, 0); |
| 10483 | for(i=1; i<nArg; i++){ |
| 10484 | const char *z = azArg[i]; |
| 10485 | if( z[0]=='-' ){ |
| 10486 | z++; |
| 10487 | if( z[0]=='-' ) z++; |
| 10488 | if( strcmp(z,"schema")==0 ){ |
| 10489 | bSchema = 1; |
| 10490 | }else |
| 10491 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 10492 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 10493 | ){ |
| 10494 | iSize = atoi(&z[5]); |
| 10495 | }else |
| 10496 | if( strcmp(z,"debug")==0 ){ |
| 10497 | bDebug = 1; |
| 10498 | }else |
| 10499 | { |
| 10500 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 10501 | azArg[i], azArg[0]); |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10502 | showHelp(p->out, azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10503 | rc = 1; |
| 10504 | goto meta_command_exit; |
| 10505 | } |
| 10506 | }else if( zLike ){ |
| 10507 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 10508 | rc = 1; |
| 10509 | goto meta_command_exit; |
| 10510 | }else{ |
| 10511 | zLike = z; |
| 10512 | bSeparate = 1; |
drh | cedfecf | 2018-03-23 12:59:10 +0000 | [diff] [blame] | 10513 | if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10514 | } |
| 10515 | } |
| 10516 | if( bSchema ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10517 | zSql = "SELECT lower(name) FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10518 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10519 | " UNION ALL SELECT 'sqlite_schema'" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10520 | " ORDER BY 1 collate nocase"; |
| 10521 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10522 | zSql = "SELECT lower(name) FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10523 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 10524 | " AND name NOT LIKE 'sqlite_%'" |
| 10525 | " ORDER BY 1 collate nocase"; |
| 10526 | } |
| 10527 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 10528 | initText(&sQuery); |
| 10529 | initText(&sSql); |
| 10530 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 10531 | zSep = "VALUES("; |
| 10532 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 10533 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 10534 | if( zTab==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10535 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 10536 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 10537 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 10538 | appendText(&sQuery,zTab,'"'); |
| 10539 | appendText(&sQuery," NOT INDEXED;", 0); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10540 | }else if( strcmp(zTab, "sqlite_schema")==0 ){ |
| 10541 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10542 | " ORDER BY name;", 0); |
| 10543 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 10544 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 10545 | " ORDER BY name;", 0); |
| 10546 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 10547 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 10548 | " ORDER BY tbl,idx;", 0); |
drh | 175b8f0 | 2019-08-08 15:24:17 +0000 | [diff] [blame] | 10549 | }else if( strcmp(zTab, "sqlite_stat4")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10550 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 10551 | appendText(&sQuery, zTab, 0); |
| 10552 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 10553 | } |
| 10554 | appendText(&sSql, zSep, 0); |
| 10555 | appendText(&sSql, sQuery.z, '\''); |
| 10556 | sQuery.n = 0; |
| 10557 | appendText(&sSql, ",", 0); |
| 10558 | appendText(&sSql, zTab, '\''); |
| 10559 | zSep = "),("; |
| 10560 | } |
| 10561 | sqlite3_finalize(pStmt); |
| 10562 | if( bSeparate ){ |
| 10563 | zSql = sqlite3_mprintf( |
| 10564 | "%s))" |
| 10565 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 10566 | " FROM [sha3sum$query]", |
| 10567 | sSql.z, iSize); |
| 10568 | }else{ |
| 10569 | zSql = sqlite3_mprintf( |
| 10570 | "%s))" |
| 10571 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 10572 | " FROM [sha3sum$query]", |
| 10573 | sSql.z, iSize); |
| 10574 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10575 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10576 | freeText(&sQuery); |
| 10577 | freeText(&sSql); |
| 10578 | if( bDebug ){ |
| 10579 | utf8_printf(p->out, "%s\n", zSql); |
| 10580 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 10581 | shell_exec(p, zSql, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10582 | } |
| 10583 | sqlite3_free(zSql); |
| 10584 | }else |
| 10585 | |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 10586 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10587 | if( c=='s' |
| 10588 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 10589 | ){ |
| 10590 | char *zCmd; |
| 10591 | int i, x; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 10592 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10593 | if( nArg<2 ){ |
| 10594 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
| 10595 | rc = 1; |
| 10596 | goto meta_command_exit; |
| 10597 | } |
| 10598 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10599 | for(i=2; i<nArg && zCmd!=0; i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10600 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 10601 | zCmd, azArg[i]); |
| 10602 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10603 | x = zCmd!=0 ? system(zCmd) : 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10604 | sqlite3_free(zCmd); |
| 10605 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 10606 | }else |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 10607 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10608 | |
| 10609 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 10610 | static const char *azBool[] = { "off", "on", "trigger", "full"}; |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10611 | const char *zOut; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10612 | int i; |
| 10613 | if( nArg!=1 ){ |
| 10614 | raw_printf(stderr, "Usage: .show\n"); |
| 10615 | rc = 1; |
| 10616 | goto meta_command_exit; |
| 10617 | } |
| 10618 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 10619 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10620 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 10621 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 10622 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 10623 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 10624 | if( p->mode==MODE_Column |
| 10625 | || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) |
| 10626 | ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 10627 | utf8_printf |
| 10628 | (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", |
| 10629 | modeDescr[p->mode], p->cmOpts.iWrap, |
| 10630 | p->cmOpts.bWordWrap ? "on" : "off", |
| 10631 | p->cmOpts.bQuote ? "" : "no"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 10632 | }else{ |
| 10633 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 10634 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10635 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| 10636 | output_c_string(p->out, p->nullValue); |
| 10637 | raw_printf(p->out, "\n"); |
| 10638 | utf8_printf(p->out,"%12.12s: %s\n","output", |
| 10639 | strlen30(p->outfile) ? p->outfile : "stdout"); |
| 10640 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
| 10641 | output_c_string(p->out, p->colSeparator); |
| 10642 | raw_printf(p->out, "\n"); |
| 10643 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
| 10644 | output_c_string(p->out, p->rowSeparator); |
| 10645 | raw_printf(p->out, "\n"); |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10646 | switch( p->statsOn ){ |
| 10647 | case 0: zOut = "off"; break; |
| 10648 | default: zOut = "on"; break; |
| 10649 | case 2: zOut = "stmt"; break; |
| 10650 | case 3: zOut = "vmstep"; break; |
| 10651 | } |
| 10652 | utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10653 | utf8_printf(p->out, "%12.12s: ", "width"); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 10654 | for (i=0;i<p->nWidth;i++) { |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10655 | raw_printf(p->out, "%d ", p->colWidth[i]); |
| 10656 | } |
| 10657 | raw_printf(p->out, "\n"); |
| 10658 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10659 | p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10660 | }else |
| 10661 | |
| 10662 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 10663 | if( nArg==2 ){ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10664 | if( strcmp(azArg[1],"stmt")==0 ){ |
| 10665 | p->statsOn = 2; |
| 10666 | }else if( strcmp(azArg[1],"vmstep")==0 ){ |
| 10667 | p->statsOn = 3; |
| 10668 | }else{ |
| 10669 | p->statsOn = (u8)booleanValue(azArg[1]); |
| 10670 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10671 | }else if( nArg==1 ){ |
| 10672 | display_stats(p->db, p, 0); |
| 10673 | }else{ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10674 | raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10675 | rc = 1; |
| 10676 | } |
| 10677 | }else |
| 10678 | |
| 10679 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 10680 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 10681 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 10682 | ){ |
| 10683 | sqlite3_stmt *pStmt; |
| 10684 | char **azResult; |
| 10685 | int nRow, nAlloc; |
| 10686 | int ii; |
| 10687 | ShellText s; |
| 10688 | initText(&s); |
| 10689 | open_db(p, 0); |
| 10690 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 10691 | if( rc ){ |
| 10692 | sqlite3_finalize(pStmt); |
| 10693 | return shellDatabaseError(p->db); |
| 10694 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10695 | |
| 10696 | if( nArg>2 && c=='i' ){ |
| 10697 | /* It is an historical accident that the .indexes command shows an error |
| 10698 | ** when called with the wrong number of arguments whereas the .tables |
| 10699 | ** command does not. */ |
| 10700 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 10701 | rc = 1; |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 10702 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10703 | goto meta_command_exit; |
| 10704 | } |
| 10705 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
| 10706 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 10707 | if( zDbName==0 ) continue; |
| 10708 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 10709 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 10710 | appendText(&s, "SELECT name FROM ", 0); |
| 10711 | }else{ |
| 10712 | appendText(&s, "SELECT ", 0); |
| 10713 | appendText(&s, zDbName, '\''); |
| 10714 | appendText(&s, "||'.'||name FROM ", 0); |
| 10715 | } |
| 10716 | appendText(&s, zDbName, '"'); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10717 | appendText(&s, ".sqlite_schema ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10718 | if( c=='t' ){ |
| 10719 | appendText(&s," WHERE type IN ('table','view')" |
| 10720 | " AND name NOT LIKE 'sqlite_%'" |
| 10721 | " AND name LIKE ?1", 0); |
| 10722 | }else{ |
| 10723 | appendText(&s," WHERE type='index'" |
| 10724 | " AND tbl_name LIKE ?1", 0); |
| 10725 | } |
| 10726 | } |
| 10727 | rc = sqlite3_finalize(pStmt); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 10728 | if( rc==SQLITE_OK ){ |
| 10729 | appendText(&s, " ORDER BY 1", 0); |
| 10730 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 10731 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10732 | freeText(&s); |
| 10733 | if( rc ) return shellDatabaseError(p->db); |
| 10734 | |
| 10735 | /* Run the SQL statement prepared by the above block. Store the results |
| 10736 | ** as an array of nul-terminated strings in azResult[]. */ |
| 10737 | nRow = nAlloc = 0; |
| 10738 | azResult = 0; |
| 10739 | if( nArg>1 ){ |
| 10740 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
| 10741 | }else{ |
| 10742 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 10743 | } |
| 10744 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 10745 | if( nRow>=nAlloc ){ |
| 10746 | char **azNew; |
| 10747 | int n2 = nAlloc*2 + 10; |
| 10748 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10749 | shell_check_oom(azNew); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10750 | nAlloc = n2; |
| 10751 | azResult = azNew; |
| 10752 | } |
| 10753 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10754 | shell_check_oom(azResult[nRow]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10755 | nRow++; |
| 10756 | } |
| 10757 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 10758 | rc = shellDatabaseError(p->db); |
| 10759 | } |
| 10760 | |
| 10761 | /* Pretty-print the contents of array azResult[] to the output */ |
| 10762 | if( rc==0 && nRow>0 ){ |
| 10763 | int len, maxlen = 0; |
| 10764 | int i, j; |
| 10765 | int nPrintCol, nPrintRow; |
| 10766 | for(i=0; i<nRow; i++){ |
| 10767 | len = strlen30(azResult[i]); |
| 10768 | if( len>maxlen ) maxlen = len; |
| 10769 | } |
| 10770 | nPrintCol = 80/(maxlen+2); |
| 10771 | if( nPrintCol<1 ) nPrintCol = 1; |
| 10772 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 10773 | for(i=0; i<nPrintRow; i++){ |
| 10774 | for(j=i; j<nRow; j+=nPrintRow){ |
| 10775 | char *zSp = j<nPrintRow ? "" : " "; |
| 10776 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 10777 | azResult[j] ? azResult[j]:""); |
| 10778 | } |
| 10779 | raw_printf(p->out, "\n"); |
| 10780 | } |
| 10781 | } |
| 10782 | |
| 10783 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 10784 | sqlite3_free(azResult); |
| 10785 | }else |
| 10786 | |
| 10787 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 10788 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 10789 | output_reset(p); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 10790 | p->out = output_file_open("testcase-out.txt", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10791 | if( p->out==0 ){ |
| 10792 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
| 10793 | } |
| 10794 | if( nArg>=2 ){ |
| 10795 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 10796 | }else{ |
| 10797 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 10798 | } |
| 10799 | }else |
| 10800 | |
| 10801 | #ifndef SQLITE_UNTESTABLE |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10802 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10803 | static const struct { |
| 10804 | const char *zCtrlName; /* Name of a test-control option */ |
| 10805 | int ctrlCode; /* Integer code for that option */ |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10806 | int unSafe; /* Not valid for --safe mode */ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10807 | const char *zUsage; /* Usage notes */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10808 | } aCtrl[] = { |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10809 | { "always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, |
| 10810 | { "assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, |
| 10811 | /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ |
| 10812 | /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/ |
| 10813 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, |
| 10814 | { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, |
| 10815 | /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/ |
| 10816 | { "imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, |
| 10817 | { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, |
| 10818 | { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, |
| 10819 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, |
| 10820 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10821 | #ifdef YYCOVERAGE |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10822 | { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10823 | #endif |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10824 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " }, |
| 10825 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, |
| 10826 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, |
| 10827 | { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, |
| 10828 | { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, |
| 10829 | { "sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, |
| 10830 | { "tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10831 | }; |
| 10832 | int testctrl = -1; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10833 | int iCtrl = -1; |
| 10834 | int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ |
| 10835 | int isOk = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10836 | int i, n2; |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 10837 | const char *zCmd = 0; |
| 10838 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10839 | open_db(p, 0); |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 10840 | zCmd = nArg>=2 ? azArg[1] : "help"; |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10841 | |
| 10842 | /* The argument can optionally begin with "-" or "--" */ |
| 10843 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 10844 | zCmd++; |
| 10845 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 10846 | } |
| 10847 | |
| 10848 | /* --help lists all test-controls */ |
| 10849 | if( strcmp(zCmd,"help")==0 ){ |
| 10850 | utf8_printf(p->out, "Available test-controls:\n"); |
| 10851 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10852 | utf8_printf(p->out, " .testctrl %s %s\n", |
| 10853 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10854 | } |
| 10855 | rc = 1; |
| 10856 | goto meta_command_exit; |
| 10857 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10858 | |
| 10859 | /* convert testctrl text option to value. allow any unique prefix |
| 10860 | ** of the option name, or a numerical value. */ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10861 | n2 = strlen30(zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10862 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10863 | if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10864 | if( testctrl<0 ){ |
| 10865 | testctrl = aCtrl[i].ctrlCode; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10866 | iCtrl = i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10867 | }else{ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10868 | utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" |
| 10869 | "Use \".testctrl --help\" for help\n", zCmd); |
| 10870 | rc = 1; |
| 10871 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10872 | } |
| 10873 | } |
| 10874 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10875 | if( testctrl<0 ){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10876 | utf8_printf(stderr,"Error: unknown test-control: %s\n" |
| 10877 | "Use \".testctrl --help\" for help\n", zCmd); |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10878 | }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ |
| 10879 | utf8_printf(stderr, |
| 10880 | "line %d: \".testctrl %s\" may not be used in safe mode\n", |
| 10881 | p->lineno, aCtrl[iCtrl].zCtrlName); |
| 10882 | exit(1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10883 | }else{ |
| 10884 | switch(testctrl){ |
| 10885 | |
| 10886 | /* sqlite3_test_control(int, db, int) */ |
| 10887 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10888 | if( nArg==3 ){ |
drh | af7b765 | 2021-01-13 19:28:17 +0000 | [diff] [blame] | 10889 | unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10890 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10891 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10892 | } |
| 10893 | break; |
| 10894 | |
| 10895 | /* sqlite3_test_control(int) */ |
| 10896 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 10897 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10898 | case SQLITE_TESTCTRL_BYTEORDER: |
| 10899 | if( nArg==2 ){ |
| 10900 | rc2 = sqlite3_test_control(testctrl); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10901 | isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10902 | } |
| 10903 | break; |
| 10904 | |
| 10905 | /* sqlite3_test_control(int, uint) */ |
| 10906 | case SQLITE_TESTCTRL_PENDING_BYTE: |
| 10907 | if( nArg==3 ){ |
| 10908 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
| 10909 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10910 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10911 | } |
| 10912 | break; |
| 10913 | |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10914 | /* sqlite3_test_control(int, int, sqlite3*) */ |
| 10915 | case SQLITE_TESTCTRL_PRNG_SEED: |
| 10916 | if( nArg==3 || nArg==4 ){ |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 10917 | int ii = (int)integerValue(azArg[2]); |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10918 | sqlite3 *db; |
drh | 41428a9 | 2019-08-12 16:25:11 +0000 | [diff] [blame] | 10919 | if( ii==0 && strcmp(azArg[2],"random")==0 ){ |
| 10920 | sqlite3_randomness(sizeof(ii),&ii); |
| 10921 | printf("-- random seed: %d\n", ii); |
| 10922 | } |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10923 | if( nArg==3 ){ |
| 10924 | db = 0; |
| 10925 | }else{ |
| 10926 | db = p->db; |
| 10927 | /* Make sure the schema has been loaded */ |
| 10928 | sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); |
| 10929 | } |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 10930 | rc2 = sqlite3_test_control(testctrl, ii, db); |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10931 | isOk = 3; |
| 10932 | } |
| 10933 | break; |
| 10934 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10935 | /* sqlite3_test_control(int, int) */ |
| 10936 | case SQLITE_TESTCTRL_ASSERT: |
| 10937 | case SQLITE_TESTCTRL_ALWAYS: |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10938 | if( nArg==3 ){ |
| 10939 | int opt = booleanValue(azArg[2]); |
| 10940 | rc2 = sqlite3_test_control(testctrl, opt); |
| 10941 | isOk = 1; |
| 10942 | } |
| 10943 | break; |
| 10944 | |
| 10945 | /* sqlite3_test_control(int, int) */ |
| 10946 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10947 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
| 10948 | if( nArg==3 ){ |
| 10949 | int opt = booleanValue(azArg[2]); |
| 10950 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10951 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10952 | } |
| 10953 | break; |
| 10954 | |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 10955 | /* sqlite3_test_control(sqlite3*) */ |
| 10956 | case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: |
| 10957 | rc2 = sqlite3_test_control(testctrl, p->db); |
drh | 2a83c10 | 2020-01-01 23:02:35 +0000 | [diff] [blame] | 10958 | isOk = 3; |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 10959 | break; |
| 10960 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10961 | case SQLITE_TESTCTRL_IMPOSTER: |
| 10962 | if( nArg==5 ){ |
| 10963 | rc2 = sqlite3_test_control(testctrl, p->db, |
| 10964 | azArg[2], |
| 10965 | integerValue(azArg[3]), |
| 10966 | integerValue(azArg[4])); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10967 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10968 | } |
| 10969 | break; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10970 | |
drh | 37ccfcf | 2020-08-31 18:49:04 +0000 | [diff] [blame] | 10971 | case SQLITE_TESTCTRL_SEEK_COUNT: { |
| 10972 | u64 x = 0; |
| 10973 | rc2 = sqlite3_test_control(testctrl, p->db, &x); |
| 10974 | utf8_printf(p->out, "%llu\n", x); |
| 10975 | isOk = 3; |
| 10976 | break; |
| 10977 | } |
| 10978 | |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10979 | #ifdef YYCOVERAGE |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 10980 | case SQLITE_TESTCTRL_PARSER_COVERAGE: { |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10981 | if( nArg==2 ){ |
| 10982 | sqlite3_test_control(testctrl, p->out); |
| 10983 | isOk = 3; |
| 10984 | } |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 10985 | break; |
| 10986 | } |
| 10987 | #endif |
| 10988 | #ifdef SQLITE_DEBUG |
| 10989 | case SQLITE_TESTCTRL_TUNE: { |
| 10990 | if( nArg==4 ){ |
| 10991 | int id = (int)integerValue(azArg[2]); |
drh | 2d26cfc | 2021-06-04 13:40:26 +0000 | [diff] [blame] | 10992 | int val = (int)integerValue(azArg[3]); |
| 10993 | sqlite3_test_control(testctrl, id, &val); |
| 10994 | isOk = 3; |
| 10995 | }else if( nArg==3 ){ |
| 10996 | int id = (int)integerValue(azArg[2]); |
| 10997 | sqlite3_test_control(testctrl, -id, &rc2); |
| 10998 | isOk = 1; |
| 10999 | }else if( nArg==2 ){ |
| 11000 | int id = 1; |
| 11001 | while(1){ |
| 11002 | int val = 0; |
| 11003 | rc2 = sqlite3_test_control(testctrl, -id, &val); |
| 11004 | if( rc2!=SQLITE_OK ) break; |
| 11005 | if( id>1 ) utf8_printf(p->out, " "); |
| 11006 | utf8_printf(p->out, "%d: %d", id, val); |
| 11007 | id++; |
| 11008 | } |
| 11009 | if( id>1 ) utf8_printf(p->out, "\n"); |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 11010 | isOk = 3; |
| 11011 | } |
| 11012 | break; |
| 11013 | } |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 11014 | #endif |
dan | 779e990 | 2021-07-28 18:13:28 +0000 | [diff] [blame] | 11015 | case SQLITE_TESTCTRL_SORTER_MMAP: |
| 11016 | if( nArg==3 ){ |
| 11017 | int opt = (unsigned int)integerValue(azArg[2]); |
| 11018 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
| 11019 | isOk = 3; |
| 11020 | } |
| 11021 | break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11022 | } |
| 11023 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 11024 | if( isOk==0 && iCtrl>=0 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 11025 | utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 11026 | rc = 1; |
| 11027 | }else if( isOk==1 ){ |
| 11028 | raw_printf(p->out, "%d\n", rc2); |
| 11029 | }else if( isOk==2 ){ |
| 11030 | raw_printf(p->out, "0x%08x\n", rc2); |
| 11031 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11032 | }else |
| 11033 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 11034 | |
| 11035 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
| 11036 | open_db(p, 0); |
| 11037 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
| 11038 | }else |
| 11039 | |
| 11040 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 11041 | if( nArg==2 ){ |
| 11042 | enableTimer = booleanValue(azArg[1]); |
| 11043 | if( enableTimer && !HAS_TIMER ){ |
| 11044 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
| 11045 | enableTimer = 0; |
| 11046 | } |
| 11047 | }else{ |
| 11048 | raw_printf(stderr, "Usage: .timer on|off\n"); |
| 11049 | rc = 1; |
| 11050 | } |
| 11051 | }else |
| 11052 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 11053 | #ifndef SQLITE_OMIT_TRACE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11054 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 11055 | int mType = 0; |
| 11056 | int jj; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11057 | open_db(p, 0); |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 11058 | for(jj=1; jj<nArg; jj++){ |
| 11059 | const char *z = azArg[jj]; |
| 11060 | if( z[0]=='-' ){ |
| 11061 | if( optionMatch(z, "expanded") ){ |
| 11062 | p->eTraceType = SHELL_TRACE_EXPANDED; |
| 11063 | } |
| 11064 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 11065 | else if( optionMatch(z, "normalized") ){ |
| 11066 | p->eTraceType = SHELL_TRACE_NORMALIZED; |
| 11067 | } |
| 11068 | #endif |
| 11069 | else if( optionMatch(z, "plain") ){ |
| 11070 | p->eTraceType = SHELL_TRACE_PLAIN; |
| 11071 | } |
| 11072 | else if( optionMatch(z, "profile") ){ |
| 11073 | mType |= SQLITE_TRACE_PROFILE; |
| 11074 | } |
| 11075 | else if( optionMatch(z, "row") ){ |
| 11076 | mType |= SQLITE_TRACE_ROW; |
| 11077 | } |
| 11078 | else if( optionMatch(z, "stmt") ){ |
| 11079 | mType |= SQLITE_TRACE_STMT; |
| 11080 | } |
| 11081 | else if( optionMatch(z, "close") ){ |
| 11082 | mType |= SQLITE_TRACE_CLOSE; |
| 11083 | } |
| 11084 | else { |
| 11085 | raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); |
| 11086 | rc = 1; |
| 11087 | goto meta_command_exit; |
| 11088 | } |
| 11089 | }else{ |
| 11090 | output_file_close(p->traceOut); |
| 11091 | p->traceOut = output_file_open(azArg[1], 0); |
| 11092 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11093 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11094 | if( p->traceOut==0 ){ |
| 11095 | sqlite3_trace_v2(p->db, 0, 0, 0); |
| 11096 | }else{ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 11097 | if( mType==0 ) mType = SQLITE_TRACE_STMT; |
| 11098 | sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11099 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11100 | }else |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 11101 | #endif /* !defined(SQLITE_OMIT_TRACE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11102 | |
drh | e2b7a76 | 2019-10-02 00:25:08 +0000 | [diff] [blame] | 11103 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 11104 | if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ |
| 11105 | int ii; |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 11106 | int lenOpt; |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 11107 | char *zOpt; |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 11108 | if( nArg<2 ){ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 11109 | raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 11110 | rc = 1; |
| 11111 | goto meta_command_exit; |
| 11112 | } |
| 11113 | open_db(p, 0); |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 11114 | zOpt = azArg[1]; |
| 11115 | if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 11116 | lenOpt = (int)strlen(zOpt); |
| 11117 | if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 11118 | assert( azArg[nArg]==0 ); |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 11119 | sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 11120 | }else{ |
| 11121 | for(ii=1; ii<nArg; ii++){ |
| 11122 | sqlite3_create_module(p->db, azArg[ii], 0, 0); |
| 11123 | } |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 11124 | } |
| 11125 | }else |
| 11126 | #endif |
| 11127 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11128 | #if SQLITE_USER_AUTHENTICATION |
| 11129 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 11130 | if( nArg<2 ){ |
| 11131 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
| 11132 | rc = 1; |
| 11133 | goto meta_command_exit; |
| 11134 | } |
| 11135 | open_db(p, 0); |
| 11136 | if( strcmp(azArg[1],"login")==0 ){ |
| 11137 | if( nArg!=4 ){ |
| 11138 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
| 11139 | rc = 1; |
| 11140 | goto meta_command_exit; |
| 11141 | } |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 11142 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 11143 | strlen30(azArg[3])); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11144 | if( rc ){ |
| 11145 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
| 11146 | rc = 1; |
| 11147 | } |
| 11148 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 11149 | if( nArg!=5 ){ |
| 11150 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
| 11151 | rc = 1; |
| 11152 | goto meta_command_exit; |
| 11153 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 11154 | rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11155 | booleanValue(azArg[4])); |
| 11156 | if( rc ){ |
| 11157 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
| 11158 | rc = 1; |
| 11159 | } |
| 11160 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 11161 | if( nArg!=5 ){ |
| 11162 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
| 11163 | rc = 1; |
| 11164 | goto meta_command_exit; |
| 11165 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 11166 | rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11167 | booleanValue(azArg[4])); |
| 11168 | if( rc ){ |
| 11169 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
| 11170 | rc = 1; |
| 11171 | } |
| 11172 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 11173 | if( nArg!=3 ){ |
| 11174 | raw_printf(stderr, "Usage: .user delete USER\n"); |
| 11175 | rc = 1; |
| 11176 | goto meta_command_exit; |
| 11177 | } |
| 11178 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 11179 | if( rc ){ |
| 11180 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
| 11181 | rc = 1; |
| 11182 | } |
| 11183 | }else{ |
| 11184 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
| 11185 | rc = 1; |
| 11186 | goto meta_command_exit; |
| 11187 | } |
| 11188 | }else |
| 11189 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 11190 | |
| 11191 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
| 11192 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
| 11193 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | 0ed2fd8 | 2018-01-16 20:05:27 +0000 | [diff] [blame] | 11194 | #if SQLITE_HAVE_ZLIB |
| 11195 | utf8_printf(p->out, "zlib version %s\n", zlibVersion()); |
| 11196 | #endif |
| 11197 | #define CTIMEOPT_VAL_(opt) #opt |
| 11198 | #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) |
| 11199 | #if defined(__clang__) && defined(__clang_major__) |
| 11200 | utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." |
| 11201 | CTIMEOPT_VAL(__clang_minor__) "." |
| 11202 | CTIMEOPT_VAL(__clang_patchlevel__) "\n"); |
| 11203 | #elif defined(_MSC_VER) |
| 11204 | utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); |
| 11205 | #elif defined(__GNUC__) && defined(__VERSION__) |
| 11206 | utf8_printf(p->out, "gcc-" __VERSION__ "\n"); |
| 11207 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11208 | }else |
| 11209 | |
| 11210 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 11211 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 11212 | sqlite3_vfs *pVfs = 0; |
| 11213 | if( p->db ){ |
| 11214 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 11215 | if( pVfs ){ |
| 11216 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 11217 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 11218 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 11219 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 11220 | } |
| 11221 | } |
| 11222 | }else |
| 11223 | |
| 11224 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 11225 | sqlite3_vfs *pVfs; |
| 11226 | sqlite3_vfs *pCurrent = 0; |
| 11227 | if( p->db ){ |
| 11228 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 11229 | } |
| 11230 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 11231 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 11232 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 11233 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 11234 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 11235 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 11236 | if( pVfs->pNext ){ |
| 11237 | raw_printf(p->out, "-----------------------------------\n"); |
| 11238 | } |
| 11239 | } |
| 11240 | }else |
| 11241 | |
| 11242 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 11243 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 11244 | char *zVfsName = 0; |
| 11245 | if( p->db ){ |
| 11246 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 11247 | if( zVfsName ){ |
| 11248 | utf8_printf(p->out, "%s\n", zVfsName); |
| 11249 | sqlite3_free(zVfsName); |
| 11250 | } |
| 11251 | } |
| 11252 | }else |
| 11253 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11254 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 11255 | unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
| 11256 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11257 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11258 | |
| 11259 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 11260 | int j; |
| 11261 | assert( nArg<=ArraySize(azArg) ); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 11262 | p->nWidth = nArg-1; |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 11263 | p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 11264 | if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); |
| 11265 | if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; |
| 11266 | for(j=1; j<nArg; j++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11267 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
| 11268 | } |
| 11269 | }else |
| 11270 | |
| 11271 | { |
| 11272 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
| 11273 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
| 11274 | rc = 1; |
| 11275 | } |
| 11276 | |
| 11277 | meta_command_exit: |
| 11278 | if( p->outCount ){ |
| 11279 | p->outCount--; |
| 11280 | if( p->outCount==0 ) output_reset(p); |
| 11281 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11282 | p->bSafeMode = p->bSafeModePersist; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11283 | return rc; |
| 11284 | } |
| 11285 | |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11286 | /* Line scan result and intermediate states (supporting scan resumption) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11287 | */ |
drh | 68911c2 | 2021-09-22 14:26:22 +0000 | [diff] [blame] | 11288 | #ifndef CHAR_BIT |
| 11289 | # define CHAR_BIT 8 |
| 11290 | #endif |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11291 | typedef enum { |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11292 | QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, |
| 11293 | QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11294 | QSS_Start = 0 |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11295 | } QuickScanState; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11296 | #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) |
| 11297 | #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) |
| 11298 | #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11299 | #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11300 | #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11301 | |
| 11302 | /* |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11303 | ** Scan line for classification to guide shell's handling. |
| 11304 | ** The scan is resumable for subsequent lines when prior |
| 11305 | ** return values are passed as the 2nd argument. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11306 | */ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11307 | static QuickScanState quickscan(char *zLine, QuickScanState qss){ |
| 11308 | char cin; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11309 | char cWait = (char)qss; /* intentional narrowing loss */ |
| 11310 | if( cWait==0 ){ |
| 11311 | PlainScan: |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 11312 | assert( cWait==0 ); |
drh | fd7abcd | 2021-09-22 13:43:16 +0000 | [diff] [blame] | 11313 | while( (cin = *zLine++)!=0 ){ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11314 | if( IsSpace(cin) ) |
| 11315 | continue; |
| 11316 | switch (cin){ |
| 11317 | case '-': |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11318 | if( *zLine!='-' ) |
| 11319 | break; |
| 11320 | while((cin = *++zLine)!=0 ) |
| 11321 | if( cin=='\n') |
| 11322 | goto PlainScan; |
| 11323 | return qss; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11324 | case ';': |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11325 | qss |= QSS_EndingSemi; |
| 11326 | continue; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11327 | case '/': |
| 11328 | if( *zLine=='*' ){ |
| 11329 | ++zLine; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11330 | cWait = '*'; |
| 11331 | qss = QSS_SETV(qss, cWait); |
| 11332 | goto TermScan; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11333 | } |
| 11334 | break; |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11335 | case '[': |
| 11336 | cin = ']'; |
| 11337 | /* fall thru */ |
| 11338 | case '`': case '\'': case '"': |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11339 | cWait = cin; |
| 11340 | qss = QSS_HasDark | cWait; |
| 11341 | goto TermScan; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11342 | default: |
| 11343 | break; |
| 11344 | } |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11345 | qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11346 | } |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11347 | }else{ |
| 11348 | TermScan: |
drh | fd7abcd | 2021-09-22 13:43:16 +0000 | [diff] [blame] | 11349 | while( (cin = *zLine++)!=0 ){ |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11350 | if( cin==cWait ){ |
| 11351 | switch( cWait ){ |
| 11352 | case '*': |
| 11353 | if( *zLine != '/' ) |
| 11354 | continue; |
| 11355 | ++zLine; |
| 11356 | cWait = 0; |
| 11357 | qss = QSS_SETV(qss, 0); |
| 11358 | goto PlainScan; |
| 11359 | case '`': case '\'': case '"': |
| 11360 | if(*zLine==cWait){ |
larrybr | 8d463ce | 2021-09-11 02:42:04 +0000 | [diff] [blame] | 11361 | ++zLine; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11362 | continue; |
| 11363 | } |
| 11364 | /* fall thru */ |
| 11365 | case ']': |
| 11366 | cWait = 0; |
| 11367 | qss = QSS_SETV(qss, 0); |
| 11368 | goto PlainScan; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 11369 | default: assert(0); |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11370 | } |
| 11371 | } |
| 11372 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11373 | } |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11374 | return qss; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11375 | } |
| 11376 | |
| 11377 | /* |
| 11378 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 11379 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 11380 | ** as is the Oracle "/". |
| 11381 | */ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11382 | static int line_is_command_terminator(char *zLine){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11383 | while( IsSpace(zLine[0]) ){ zLine++; }; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11384 | if( zLine[0]=='/' ) |
| 11385 | zLine += 1; /* Oracle */ |
| 11386 | else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) |
| 11387 | zLine += 2; /* SQL Server */ |
| 11388 | else |
| 11389 | return 0; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 11390 | return quickscan(zLine, QSS_Start)==QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11391 | } |
| 11392 | |
| 11393 | /* |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 11394 | ** We need a default sqlite3_complete() implementation to use in case |
| 11395 | ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes |
| 11396 | ** any arbitrary text is a complete SQL statement. This is not very |
| 11397 | ** user-friendly, but it does seem to work. |
| 11398 | */ |
| 11399 | #ifdef SQLITE_OMIT_COMPLETE |
dan | c86b23b | 2018-11-16 14:36:42 +0000 | [diff] [blame] | 11400 | #define sqlite3_complete(x) 1 |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 11401 | #endif |
| 11402 | |
| 11403 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11404 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 11405 | ** ends in the middle of a string literal or C-style comment. |
| 11406 | */ |
| 11407 | static int line_is_complete(char *zSql, int nSql){ |
| 11408 | int rc; |
| 11409 | if( zSql==0 ) return 1; |
| 11410 | zSql[nSql] = ';'; |
| 11411 | zSql[nSql+1] = 0; |
| 11412 | rc = sqlite3_complete(zSql); |
| 11413 | zSql[nSql] = 0; |
| 11414 | return rc; |
| 11415 | } |
| 11416 | |
| 11417 | /* |
drh | fc29a86 | 2018-05-11 19:11:18 +0000 | [diff] [blame] | 11418 | ** Run a single line of SQL. Return the number of errors. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11419 | */ |
| 11420 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 11421 | int rc; |
| 11422 | char *zErrMsg = 0; |
| 11423 | |
| 11424 | open_db(p, 0); |
| 11425 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 11426 | if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11427 | BEGIN_TIMER; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 11428 | rc = shell_exec(p, zSql, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11429 | END_TIMER; |
| 11430 | if( rc || zErrMsg ){ |
| 11431 | char zPrefix[100]; |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11432 | const char *zErrorTail; |
| 11433 | const char *zErrorType; |
| 11434 | if( zErrMsg==0 ){ |
| 11435 | zErrorType = "Error"; |
| 11436 | zErrorTail = sqlite3_errmsg(p->db); |
| 11437 | }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){ |
| 11438 | zErrorType = "Parse error"; |
| 11439 | zErrorTail = &zErrMsg[12]; |
| 11440 | }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){ |
| 11441 | zErrorType = "Runtime error"; |
| 11442 | zErrorTail = &zErrMsg[10]; |
| 11443 | }else{ |
| 11444 | zErrorType = "Error"; |
| 11445 | zErrorTail = zErrMsg; |
| 11446 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11447 | if( in!=0 || !stdin_is_interactive ){ |
| 11448 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11449 | "%s near line %d:", zErrorType, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11450 | }else{ |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11451 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11452 | } |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11453 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail); |
| 11454 | sqlite3_free(zErrMsg); |
| 11455 | zErrMsg = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11456 | return 1; |
| 11457 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 6d9f034 | 2021-09-22 10:28:50 +0000 | [diff] [blame] | 11458 | char zLineBuf[2000]; |
| 11459 | sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, |
| 11460 | "changes: %lld total_changes: %lld", |
larrybr | 10496f7 | 2021-06-23 16:07:20 +0000 | [diff] [blame] | 11461 | sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); |
drh | 6d9f034 | 2021-09-22 10:28:50 +0000 | [diff] [blame] | 11462 | raw_printf(p->out, "%s\n", zLineBuf); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11463 | } |
| 11464 | return 0; |
| 11465 | } |
| 11466 | |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11467 | static void echo_group_input(ShellState *p, const char *zDo){ |
| 11468 | if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); |
| 11469 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11470 | |
| 11471 | /* |
| 11472 | ** Read input from *in and process it. If *in==0 then input |
| 11473 | ** is interactive - the user is typing it it. Otherwise, input |
| 11474 | ** is coming from a file or device. A prompt is issued and history |
| 11475 | ** is saved only if input is interactive. An interrupt signal will |
| 11476 | ** cause this routine to exit immediately, unless input is interactive. |
| 11477 | ** |
| 11478 | ** Return the number of errors. |
| 11479 | */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11480 | static int process_input(ShellState *p){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11481 | char *zLine = 0; /* A single input line */ |
| 11482 | char *zSql = 0; /* Accumulated SQL text */ |
| 11483 | int nLine; /* Length of current line */ |
| 11484 | int nSql = 0; /* Bytes of zSql[] used */ |
| 11485 | int nAlloc = 0; /* Allocated zSql[] space */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11486 | int rc; /* Error code */ |
| 11487 | int errCnt = 0; /* Number of errors seen */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11488 | int startline = 0; /* Line number for start of current input */ |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11489 | QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11490 | |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 11491 | if( p->inputNesting==MAX_INPUT_NESTING ){ |
| 11492 | /* This will be more informative in a later version. */ |
| 11493 | utf8_printf(stderr,"Input nesting limit (%d) reached at line %d." |
| 11494 | " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); |
| 11495 | return 1; |
| 11496 | } |
| 11497 | ++p->inputNesting; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11498 | p->lineno = 0; |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11499 | while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11500 | fflush(p->out); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11501 | zLine = one_input_line(p->in, zLine, nSql>0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11502 | if( zLine==0 ){ |
| 11503 | /* End of input */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11504 | if( p->in==0 && stdin_is_interactive ) printf("\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11505 | break; |
| 11506 | } |
| 11507 | if( seenInterrupt ){ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11508 | if( p->in!=0 ) break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11509 | seenInterrupt = 0; |
| 11510 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11511 | p->lineno++; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11512 | if( QSS_INPLAIN(qss) |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11513 | && line_is_command_terminator(zLine) |
| 11514 | && line_is_complete(zSql, nSql) ){ |
| 11515 | memcpy(zLine,";",2); |
| 11516 | } |
| 11517 | qss = quickscan(zLine, qss); |
| 11518 | if( QSS_PLAINWHITE(qss) && nSql==0 ){ |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11519 | /* Just swallow single-line whitespace */ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11520 | echo_group_input(p, zLine); |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11521 | qss = QSS_Start; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11522 | continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11523 | } |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 11524 | if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11525 | echo_group_input(p, zLine); |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 11526 | if( zLine[0]=='.' ){ |
| 11527 | rc = do_meta_command(zLine, p); |
| 11528 | if( rc==2 ){ /* exit requested */ |
| 11529 | break; |
| 11530 | }else if( rc ){ |
| 11531 | errCnt++; |
| 11532 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11533 | } |
larrybr | 8101216 | 2021-10-02 15:34:52 +0000 | [diff] [blame] | 11534 | qss = QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11535 | continue; |
| 11536 | } |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11537 | /* No single-line dispositions remain; accumulate line(s). */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11538 | nLine = strlen30(zLine); |
| 11539 | if( nSql+nLine+2>=nAlloc ){ |
larrybr | 31bffb4 | 2021-09-08 21:49:03 +0000 | [diff] [blame] | 11540 | /* Grow buffer by half-again increments when big. */ |
| 11541 | nAlloc = nSql+(nSql>>1)+nLine+100; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11542 | zSql = realloc(zSql, nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11543 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11544 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11545 | if( nSql==0 ){ |
| 11546 | int i; |
| 11547 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
| 11548 | assert( nAlloc>0 && zSql!=0 ); |
| 11549 | memcpy(zSql, zLine+i, nLine+1-i); |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11550 | startline = p->lineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11551 | nSql = nLine-i; |
| 11552 | }else{ |
| 11553 | zSql[nSql++] = '\n'; |
| 11554 | memcpy(zSql+nSql, zLine, nLine+1); |
| 11555 | nSql += nLine; |
| 11556 | } |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11557 | if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11558 | echo_group_input(p, zSql); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11559 | errCnt += runOneSqlLine(p, zSql, p->in, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11560 | nSql = 0; |
| 11561 | if( p->outCount ){ |
| 11562 | output_reset(p); |
| 11563 | p->outCount = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 11564 | }else{ |
| 11565 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11566 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11567 | p->bSafeMode = p->bSafeModePersist; |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11568 | qss = QSS_Start; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11569 | }else if( nSql && QSS_PLAINWHITE(qss) ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11570 | echo_group_input(p, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11571 | nSql = 0; |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11572 | qss = QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11573 | } |
| 11574 | } |
larrybr | c6e2f2e | 2022-03-15 17:57:42 +0000 | [diff] [blame] | 11575 | if( nSql ){ |
| 11576 | /* This may be incomplete. Let the SQL parser deal with that. */ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11577 | echo_group_input(p, zSql); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11578 | errCnt += runOneSqlLine(p, zSql, p->in, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11579 | } |
| 11580 | free(zSql); |
| 11581 | free(zLine); |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 11582 | --p->inputNesting; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11583 | return errCnt>0; |
| 11584 | } |
| 11585 | |
| 11586 | /* |
| 11587 | ** Return a pathname which is the user's home directory. A |
| 11588 | ** 0 return indicates an error of some kind. |
| 11589 | */ |
| 11590 | static char *find_home_dir(int clearFlag){ |
| 11591 | static char *home_dir = NULL; |
| 11592 | if( clearFlag ){ |
| 11593 | free(home_dir); |
| 11594 | home_dir = 0; |
| 11595 | return 0; |
| 11596 | } |
| 11597 | if( home_dir ) return home_dir; |
| 11598 | |
| 11599 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 11600 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 11601 | { |
| 11602 | struct passwd *pwent; |
| 11603 | uid_t uid = getuid(); |
| 11604 | if( (pwent=getpwuid(uid)) != NULL) { |
| 11605 | home_dir = pwent->pw_dir; |
| 11606 | } |
| 11607 | } |
| 11608 | #endif |
| 11609 | |
| 11610 | #if defined(_WIN32_WCE) |
| 11611 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 11612 | */ |
| 11613 | home_dir = "/"; |
| 11614 | #else |
| 11615 | |
| 11616 | #if defined(_WIN32) || defined(WIN32) |
| 11617 | if (!home_dir) { |
| 11618 | home_dir = getenv("USERPROFILE"); |
| 11619 | } |
| 11620 | #endif |
| 11621 | |
| 11622 | if (!home_dir) { |
| 11623 | home_dir = getenv("HOME"); |
| 11624 | } |
| 11625 | |
| 11626 | #if defined(_WIN32) || defined(WIN32) |
| 11627 | if (!home_dir) { |
| 11628 | char *zDrive, *zPath; |
| 11629 | int n; |
| 11630 | zDrive = getenv("HOMEDRIVE"); |
| 11631 | zPath = getenv("HOMEPATH"); |
| 11632 | if( zDrive && zPath ){ |
| 11633 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
| 11634 | home_dir = malloc( n ); |
| 11635 | if( home_dir==0 ) return 0; |
| 11636 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 11637 | return home_dir; |
| 11638 | } |
| 11639 | home_dir = "c:\\"; |
| 11640 | } |
| 11641 | #endif |
| 11642 | |
| 11643 | #endif /* !_WIN32_WCE */ |
| 11644 | |
| 11645 | if( home_dir ){ |
| 11646 | int n = strlen30(home_dir) + 1; |
| 11647 | char *z = malloc( n ); |
| 11648 | if( z ) memcpy(z, home_dir, n); |
| 11649 | home_dir = z; |
| 11650 | } |
| 11651 | |
| 11652 | return home_dir; |
| 11653 | } |
| 11654 | |
| 11655 | /* |
| 11656 | ** Read input from the file given by sqliterc_override. Or if that |
| 11657 | ** parameter is NULL, take input from ~/.sqliterc |
| 11658 | ** |
| 11659 | ** Returns the number of errors. |
| 11660 | */ |
| 11661 | static void process_sqliterc( |
| 11662 | ShellState *p, /* Configuration data */ |
| 11663 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 11664 | ){ |
| 11665 | char *home_dir = NULL; |
| 11666 | const char *sqliterc = sqliterc_override; |
| 11667 | char *zBuf = 0; |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11668 | FILE *inSaved = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11669 | int savedLineno = p->lineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11670 | |
| 11671 | if (sqliterc == NULL) { |
| 11672 | home_dir = find_home_dir(0); |
| 11673 | if( home_dir==0 ){ |
| 11674 | raw_printf(stderr, "-- warning: cannot find home directory;" |
| 11675 | " cannot read ~/.sqliterc\n"); |
| 11676 | return; |
| 11677 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11678 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11679 | shell_check_oom(zBuf); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11680 | sqliterc = zBuf; |
| 11681 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11682 | p->in = fopen(sqliterc,"rb"); |
| 11683 | if( p->in ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11684 | if( stdin_is_interactive ){ |
| 11685 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
| 11686 | } |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11687 | if( process_input(p) && bail_on_error ) exit(1); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11688 | fclose(p->in); |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11689 | }else if( sqliterc_override!=0 ){ |
| 11690 | utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc); |
| 11691 | if( bail_on_error ) exit(1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11692 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11693 | p->in = inSaved; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11694 | p->lineno = savedLineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11695 | sqlite3_free(zBuf); |
| 11696 | } |
| 11697 | |
| 11698 | /* |
| 11699 | ** Show available command line options |
| 11700 | */ |
| 11701 | static const char zOptions[] = |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11702 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | ad7fd5d | 2018-03-05 20:21:50 +0000 | [diff] [blame] | 11703 | " -A ARGS... run \".archive ARGS\" and exit\n" |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11704 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11705 | " -append append the database to the end of the file\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11706 | " -ascii set output mode to 'ascii'\n" |
| 11707 | " -bail stop after hitting an error\n" |
| 11708 | " -batch force batch I/O\n" |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 11709 | " -box set output mode to 'box'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11710 | " -column set output mode to 'column'\n" |
| 11711 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
| 11712 | " -csv set output mode to 'csv'\n" |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11713 | #if !defined(SQLITE_OMIT_DESERIALIZE) |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11714 | " -deserialize open the database using sqlite3_deserialize()\n" |
| 11715 | #endif |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 11716 | " -echo print inputs before execution\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11717 | " -init FILENAME read/process named file\n" |
| 11718 | " -[no]header turn headers on or off\n" |
| 11719 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 11720 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 11721 | #endif |
| 11722 | " -help show this message\n" |
| 11723 | " -html set output mode to HTML\n" |
| 11724 | " -interactive force interactive I/O\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11725 | " -json set output mode to 'json'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11726 | " -line set output mode to 'line'\n" |
| 11727 | " -list set output mode to 'list'\n" |
| 11728 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11729 | " -markdown set output mode to 'markdown'\n" |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11730 | #if !defined(SQLITE_OMIT_DESERIALIZE) |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11731 | " -maxsize N maximum size for a --deserialize database\n" |
| 11732 | #endif |
drh | af48257 | 2019-02-04 19:52:39 +0000 | [diff] [blame] | 11733 | " -memtrace trace all memory allocations and deallocations\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11734 | " -mmap N default mmap size set to N\n" |
| 11735 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 11736 | " -multiplex enable the multiplexor VFS\n" |
| 11737 | #endif |
| 11738 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 11739 | " -nofollow refuse to open symbolic links to database files\n" |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11740 | " -nonce STRING set the safe-mode escape nonce\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11741 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 11742 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 11743 | " -quote set output mode to 'quote'\n" |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 11744 | " -readonly open the database read-only\n" |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11745 | " -safe enable safe-mode\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11746 | " -separator SEP set output column separator. Default: '|'\n" |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11747 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 11748 | " -sorterref SIZE sorter references threshold size\n" |
| 11749 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11750 | " -stats print memory stats before each finalize\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11751 | " -table set output mode to 'table'\n" |
drh | 2fa7818 | 2020-10-31 18:58:37 +0000 | [diff] [blame] | 11752 | " -tabs set output mode to 'tabs'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11753 | " -version show SQLite version\n" |
| 11754 | " -vfs NAME use NAME as the default VFS\n" |
| 11755 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 11756 | " -vfstrace enable tracing of all VFS calls\n" |
| 11757 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11758 | #ifdef SQLITE_HAVE_ZLIB |
| 11759 | " -zip open the file as a ZIP Archive\n" |
| 11760 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11761 | ; |
| 11762 | static void usage(int showDetail){ |
| 11763 | utf8_printf(stderr, |
| 11764 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 11765 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 11766 | "if the file does not previously exist.\n", Argv0); |
| 11767 | if( showDetail ){ |
| 11768 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
| 11769 | }else{ |
| 11770 | raw_printf(stderr, "Use the -help option for additional information\n"); |
| 11771 | } |
| 11772 | exit(1); |
| 11773 | } |
| 11774 | |
| 11775 | /* |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11776 | ** Internal check: Verify that the SQLite is uninitialized. Print a |
| 11777 | ** error message if it is initialized. |
| 11778 | */ |
| 11779 | static void verify_uninitialized(void){ |
| 11780 | if( sqlite3_config(-1)==SQLITE_MISUSE ){ |
drh | 8e02a18 | 2018-05-30 07:24:41 +0000 | [diff] [blame] | 11781 | utf8_printf(stdout, "WARNING: attempt to configure SQLite after" |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11782 | " initialization.\n"); |
| 11783 | } |
| 11784 | } |
| 11785 | |
| 11786 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11787 | ** Initialize the state information in data |
| 11788 | */ |
| 11789 | static void main_init(ShellState *data) { |
| 11790 | memset(data, 0, sizeof(*data)); |
| 11791 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 11792 | data->autoExplain = 1; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11793 | data->pAuxDb = &data->aAuxDb[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11794 | memcpy(data->colSeparator,SEP_Column, 2); |
| 11795 | memcpy(data->rowSeparator,SEP_Row, 2); |
| 11796 | data->showHeader = 0; |
| 11797 | data->shellFlgs = SHFLG_Lookaside; |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11798 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11799 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
| 11800 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
| 11801 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 11802 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 11803 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
| 11804 | } |
| 11805 | |
| 11806 | /* |
| 11807 | ** Output text to the console in a font that attracts extra attention. |
| 11808 | */ |
| 11809 | #ifdef _WIN32 |
| 11810 | static void printBold(const char *zText){ |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11811 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11812 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 11813 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 11814 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 11815 | SetConsoleTextAttribute(out, |
| 11816 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 11817 | ); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11818 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11819 | printf("%s", zText); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11820 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11821 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11822 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11823 | } |
| 11824 | #else |
| 11825 | static void printBold(const char *zText){ |
| 11826 | printf("\033[1m%s\033[0m", zText); |
| 11827 | } |
| 11828 | #endif |
| 11829 | |
| 11830 | /* |
| 11831 | ** Get the argument to an --option. Throw an error and die if no argument |
| 11832 | ** is available. |
| 11833 | */ |
| 11834 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 11835 | if( i==argc ){ |
| 11836 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
| 11837 | argv[0], argv[argc-1]); |
| 11838 | exit(1); |
| 11839 | } |
| 11840 | return argv[i]; |
| 11841 | } |
| 11842 | |
| 11843 | #ifndef SQLITE_SHELL_IS_UTF8 |
dan | 39b6bd5 | 2021-03-04 18:31:07 +0000 | [diff] [blame] | 11844 | # if (defined(_WIN32) || defined(WIN32)) \ |
| 11845 | && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11846 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 11847 | # else |
| 11848 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 11849 | # endif |
| 11850 | #endif |
| 11851 | |
| 11852 | #if SQLITE_SHELL_IS_UTF8 |
| 11853 | int SQLITE_CDECL main(int argc, char **argv){ |
| 11854 | #else |
| 11855 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 11856 | char **argv; |
| 11857 | #endif |
larrybr | fa5dfa8 | 2022-05-07 03:53:14 +0000 | [diff] [blame] | 11858 | #ifdef SQLITE_DEBUG |
| 11859 | sqlite3_uint64 mem_main_enter = sqlite3_memory_used(); |
| 11860 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11861 | char *zErrMsg = 0; |
| 11862 | ShellState data; |
| 11863 | const char *zInitFile = 0; |
| 11864 | int i; |
| 11865 | int rc = 0; |
| 11866 | int warnInmemoryDb = 0; |
| 11867 | int readStdin = 1; |
| 11868 | int nCmd = 0; |
| 11869 | char **azCmd = 0; |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11870 | const char *zVfs = 0; /* Value of -vfs command-line option */ |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11871 | #if !SQLITE_SHELL_IS_UTF8 |
| 11872 | char **argvToFree = 0; |
| 11873 | int argcToFree = 0; |
| 11874 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11875 | |
| 11876 | setBinaryMode(stdin, 0); |
| 11877 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 11878 | stdin_is_interactive = isatty(0); |
| 11879 | stdout_is_console = isatty(1); |
| 11880 | |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11881 | #if !defined(_WIN32_WCE) |
| 11882 | if( getenv("SQLITE_DEBUG_BREAK") ){ |
| 11883 | if( isatty(0) && isatty(2) ){ |
| 11884 | fprintf(stderr, |
| 11885 | "attach debugger to process %d and press any key to continue.\n", |
| 11886 | GETPID()); |
| 11887 | fgetc(stdin); |
| 11888 | }else{ |
| 11889 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11890 | #if SQLITE_OS_WINRT |
| 11891 | __debugbreak(); |
| 11892 | #else |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11893 | DebugBreak(); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11894 | #endif |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11895 | #elif defined(SIGTRAP) |
| 11896 | raise(SIGTRAP); |
| 11897 | #endif |
| 11898 | } |
| 11899 | } |
| 11900 | #endif |
| 11901 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11902 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 11903 | if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11904 | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
| 11905 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 11906 | exit(1); |
| 11907 | } |
| 11908 | #endif |
| 11909 | main_init(&data); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11910 | |
| 11911 | /* On Windows, we must translate command-line arguments into UTF-8. |
| 11912 | ** The SQLite memory allocator subsystem has to be enabled in order to |
| 11913 | ** do this. But we want to run an sqlite3_shutdown() afterwards so that |
| 11914 | ** subsequent sqlite3_config() calls will work. So copy all results into |
| 11915 | ** memory that does not come from the SQLite memory allocator. |
| 11916 | */ |
drh | 4b18c1d | 2018-02-04 20:33:13 +0000 | [diff] [blame] | 11917 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11918 | sqlite3_initialize(); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11919 | argvToFree = malloc(sizeof(argv[0])*argc*2); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11920 | shell_check_oom(argvToFree); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11921 | argcToFree = argc; |
| 11922 | argv = argvToFree + argc; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11923 | for(i=0; i<argc; i++){ |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11924 | char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 11925 | int n; |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11926 | shell_check_oom(z); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11927 | n = (int)strlen(z); |
| 11928 | argv[i] = malloc( n+1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11929 | shell_check_oom(argv[i]); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11930 | memcpy(argv[i], z, n+1); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11931 | argvToFree[i] = argv[i]; |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11932 | sqlite3_free(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11933 | } |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11934 | sqlite3_shutdown(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11935 | #endif |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11936 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11937 | assert( argc>=1 && argv && argv[0] ); |
| 11938 | Argv0 = argv[0]; |
| 11939 | |
| 11940 | /* Make sure we have a valid signal handler early, before anything |
| 11941 | ** else is done. |
| 11942 | */ |
| 11943 | #ifdef SIGINT |
| 11944 | signal(SIGINT, interrupt_handler); |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 11945 | #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 11946 | SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11947 | #endif |
| 11948 | |
| 11949 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 11950 | { |
| 11951 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 11952 | ** of a C-function that will provide the name of the database file. Use |
| 11953 | ** this compile-time option to embed this shell program in larger |
| 11954 | ** applications. */ |
| 11955 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11956 | SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11957 | warnInmemoryDb = 0; |
| 11958 | } |
| 11959 | #endif |
| 11960 | |
| 11961 | /* Do an initial pass through the command-line argument to locate |
| 11962 | ** the name of the database file, the name of the initialization file, |
| 11963 | ** the size of the alternative malloc heap, |
| 11964 | ** and the first command to execute. |
| 11965 | */ |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11966 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11967 | for(i=1; i<argc; i++){ |
| 11968 | char *z; |
| 11969 | z = argv[i]; |
| 11970 | if( z[0]!='-' ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11971 | if( data.aAuxDb->zDbFilename==0 ){ |
| 11972 | data.aAuxDb->zDbFilename = z; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11973 | }else{ |
| 11974 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 11975 | ** mean that nothing is read from stdin */ |
| 11976 | readStdin = 0; |
| 11977 | nCmd++; |
| 11978 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11979 | shell_check_oom(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11980 | azCmd[nCmd-1] = z; |
| 11981 | } |
| 11982 | } |
| 11983 | if( z[1]=='-' ) z++; |
| 11984 | if( strcmp(z,"-separator")==0 |
| 11985 | || strcmp(z,"-nullvalue")==0 |
| 11986 | || strcmp(z,"-newline")==0 |
| 11987 | || strcmp(z,"-cmd")==0 |
| 11988 | ){ |
| 11989 | (void)cmdline_option_value(argc, argv, ++i); |
| 11990 | }else if( strcmp(z,"-init")==0 ){ |
| 11991 | zInitFile = cmdline_option_value(argc, argv, ++i); |
| 11992 | }else if( strcmp(z,"-batch")==0 ){ |
| 11993 | /* Need to check for batch mode here to so we can avoid printing |
| 11994 | ** informational messages (like from process_sqliterc) before |
| 11995 | ** we do the actual processing of arguments later in a second pass. |
| 11996 | */ |
| 11997 | stdin_is_interactive = 0; |
| 11998 | }else if( strcmp(z,"-heap")==0 ){ |
| 11999 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 12000 | const char *zSize; |
| 12001 | sqlite3_int64 szHeap; |
| 12002 | |
| 12003 | zSize = cmdline_option_value(argc, argv, ++i); |
| 12004 | szHeap = integerValue(zSize); |
| 12005 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
| 12006 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
| 12007 | #else |
| 12008 | (void)cmdline_option_value(argc, argv, ++i); |
| 12009 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12010 | }else if( strcmp(z,"-pagecache")==0 ){ |
drh | f573b4f | 2020-09-28 13:34:05 +0000 | [diff] [blame] | 12011 | sqlite3_int64 n, sz; |
| 12012 | sz = integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12013 | if( sz>70000 ) sz = 70000; |
| 12014 | if( sz<0 ) sz = 0; |
drh | f573b4f | 2020-09-28 13:34:05 +0000 | [diff] [blame] | 12015 | n = integerValue(cmdline_option_value(argc,argv,++i)); |
| 12016 | if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ |
| 12017 | n = 0xffffffffffffLL/sz; |
| 12018 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12019 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 12020 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
| 12021 | data.shellFlgs |= SHFLG_Pagecache; |
| 12022 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 12023 | int n, sz; |
| 12024 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 12025 | if( sz<0 ) sz = 0; |
| 12026 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 12027 | if( n<0 ) n = 0; |
| 12028 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 12029 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | af6d1af | 2021-08-09 17:37:58 +0000 | [diff] [blame] | 12030 | }else if( strcmp(z,"-threadsafe")==0 ){ |
drh | 9d16fb1 | 2021-08-09 17:45:00 +0000 | [diff] [blame] | 12031 | int n; |
| 12032 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 12033 | switch( n ){ |
drh | af6d1af | 2021-08-09 17:37:58 +0000 | [diff] [blame] | 12034 | case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; |
| 12035 | case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; |
| 12036 | default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; |
| 12037 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12038 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 12039 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 12040 | extern int vfstrace_register( |
| 12041 | const char *zTraceName, |
| 12042 | const char *zOldVfsName, |
| 12043 | int (*xOut)(const char*,void*), |
| 12044 | void *pOutArg, |
| 12045 | int makeDefault |
| 12046 | ); |
| 12047 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
| 12048 | #endif |
| 12049 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 12050 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 12051 | extern int sqlite3_multiple_initialize(const char*,int); |
| 12052 | sqlite3_multiplex_initialize(0, 1); |
| 12053 | #endif |
| 12054 | }else if( strcmp(z,"-mmap")==0 ){ |
| 12055 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 12056 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 12057 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 12058 | }else if( strcmp(z,"-sorterref")==0 ){ |
| 12059 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 12060 | sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); |
| 12061 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12062 | }else if( strcmp(z,"-vfs")==0 ){ |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 12063 | zVfs = cmdline_option_value(argc, argv, ++i); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 12064 | #ifdef SQLITE_HAVE_ZLIB |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 12065 | }else if( strcmp(z,"-zip")==0 ){ |
| 12066 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 12067 | #endif |
| 12068 | }else if( strcmp(z,"-append")==0 ){ |
| 12069 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 12070 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 12071 | }else if( strcmp(z,"-deserialize")==0 ){ |
| 12072 | data.openMode = SHELL_OPEN_DESERIALIZE; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 12073 | }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ |
| 12074 | data.szMax = integerValue(argv[++i]); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 12075 | #endif |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 12076 | }else if( strcmp(z,"-readonly")==0 ){ |
| 12077 | data.openMode = SHELL_OPEN_READONLY; |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 12078 | }else if( strcmp(z,"-nofollow")==0 ){ |
| 12079 | data.openFlags = SQLITE_OPEN_NOFOLLOW; |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 12080 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 12081 | }else if( strncmp(z, "-A",2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 12082 | /* All remaining command-line arguments are passed to the ".archive" |
| 12083 | ** command, so ignore them */ |
| 12084 | break; |
| 12085 | #endif |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 12086 | }else if( strcmp(z, "-memtrace")==0 ){ |
| 12087 | sqlite3MemTraceActivate(stderr); |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 12088 | }else if( strcmp(z,"-bail")==0 ){ |
| 12089 | bail_on_error = 1; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 12090 | }else if( strcmp(z,"-nonce")==0 ){ |
| 12091 | free(data.zNonce); |
| 12092 | data.zNonce = strdup(argv[++i]); |
| 12093 | }else if( strcmp(z,"-safe")==0 ){ |
| 12094 | /* no-op - catch this on the second pass */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12095 | } |
| 12096 | } |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 12097 | verify_uninitialized(); |
| 12098 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 12099 | |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 12100 | #ifdef SQLITE_SHELL_INIT_PROC |
| 12101 | { |
| 12102 | /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name |
| 12103 | ** of a C-function that will perform initialization actions on SQLite that |
| 12104 | ** occur just before or after sqlite3_initialize(). Use this compile-time |
| 12105 | ** option to embed this shell program in larger applications. */ |
| 12106 | extern void SQLITE_SHELL_INIT_PROC(void); |
| 12107 | SQLITE_SHELL_INIT_PROC(); |
| 12108 | } |
| 12109 | #else |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 12110 | /* All the sqlite3_config() calls have now been made. So it is safe |
| 12111 | ** to call sqlite3_initialize() and process any command line -vfs option. */ |
| 12112 | sqlite3_initialize(); |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 12113 | #endif |
| 12114 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 12115 | if( zVfs ){ |
| 12116 | sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); |
| 12117 | if( pVfs ){ |
| 12118 | sqlite3_vfs_register(pVfs, 1); |
| 12119 | }else{ |
| 12120 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
| 12121 | exit(1); |
| 12122 | } |
| 12123 | } |
| 12124 | |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12125 | if( data.pAuxDb->zDbFilename==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12126 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12127 | data.pAuxDb->zDbFilename = ":memory:"; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12128 | warnInmemoryDb = argc==1; |
| 12129 | #else |
| 12130 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 12131 | return 1; |
| 12132 | #endif |
| 12133 | } |
| 12134 | data.out = stdout; |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 12135 | sqlite3_appendvfs_init(0,0,0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12136 | |
| 12137 | /* Go ahead and open the database file if it already exists. If the |
| 12138 | ** file does not exist, delay opening it. This prevents empty database |
| 12139 | ** files from being created if a user mistypes the database name argument |
| 12140 | ** to the sqlite command-line tool. |
| 12141 | */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12142 | if( access(data.pAuxDb->zDbFilename, 0)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12143 | open_db(&data, 0); |
| 12144 | } |
| 12145 | |
| 12146 | /* Process the initialization file if there is one. If no -init option |
| 12147 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 12148 | ** try to process it. |
| 12149 | */ |
| 12150 | process_sqliterc(&data,zInitFile); |
| 12151 | |
| 12152 | /* Make a second pass through the command-line argument and set |
| 12153 | ** options. This second pass is delayed until after the initialization |
| 12154 | ** file is processed so that the command-line arguments will override |
| 12155 | ** settings in the initialization file. |
| 12156 | */ |
| 12157 | for(i=1; i<argc; i++){ |
| 12158 | char *z = argv[i]; |
| 12159 | if( z[0]!='-' ) continue; |
| 12160 | if( z[1]=='-' ){ z++; } |
| 12161 | if( strcmp(z,"-init")==0 ){ |
| 12162 | i++; |
| 12163 | }else if( strcmp(z,"-html")==0 ){ |
| 12164 | data.mode = MODE_Html; |
| 12165 | }else if( strcmp(z,"-list")==0 ){ |
| 12166 | data.mode = MODE_List; |
| 12167 | }else if( strcmp(z,"-quote")==0 ){ |
| 12168 | data.mode = MODE_Quote; |
drh | 9191c70 | 2020-08-17 09:11:21 +0000 | [diff] [blame] | 12169 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); |
| 12170 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12171 | }else if( strcmp(z,"-line")==0 ){ |
| 12172 | data.mode = MODE_Line; |
| 12173 | }else if( strcmp(z,"-column")==0 ){ |
| 12174 | data.mode = MODE_Column; |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 12175 | }else if( strcmp(z,"-json")==0 ){ |
| 12176 | data.mode = MODE_Json; |
| 12177 | }else if( strcmp(z,"-markdown")==0 ){ |
| 12178 | data.mode = MODE_Markdown; |
| 12179 | }else if( strcmp(z,"-table")==0 ){ |
| 12180 | data.mode = MODE_Table; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 12181 | }else if( strcmp(z,"-box")==0 ){ |
| 12182 | data.mode = MODE_Box; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12183 | }else if( strcmp(z,"-csv")==0 ){ |
| 12184 | data.mode = MODE_Csv; |
| 12185 | memcpy(data.colSeparator,",",2); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 12186 | #ifdef SQLITE_HAVE_ZLIB |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 12187 | }else if( strcmp(z,"-zip")==0 ){ |
| 12188 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 12189 | #endif |
| 12190 | }else if( strcmp(z,"-append")==0 ){ |
| 12191 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 12192 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 12193 | }else if( strcmp(z,"-deserialize")==0 ){ |
| 12194 | data.openMode = SHELL_OPEN_DESERIALIZE; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 12195 | }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){ |
| 12196 | data.szMax = integerValue(argv[++i]); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 12197 | #endif |
drh | 4aafe59 | 2018-03-23 16:08:30 +0000 | [diff] [blame] | 12198 | }else if( strcmp(z,"-readonly")==0 ){ |
| 12199 | data.openMode = SHELL_OPEN_READONLY; |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 12200 | }else if( strcmp(z,"-nofollow")==0 ){ |
| 12201 | data.openFlags |= SQLITE_OPEN_NOFOLLOW; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12202 | }else if( strcmp(z,"-ascii")==0 ){ |
| 12203 | data.mode = MODE_Ascii; |
drh | 2fa7818 | 2020-10-31 18:58:37 +0000 | [diff] [blame] | 12204 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit); |
| 12205 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record); |
| 12206 | }else if( strcmp(z,"-tabs")==0 ){ |
| 12207 | data.mode = MODE_List; |
| 12208 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab); |
| 12209 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12210 | }else if( strcmp(z,"-separator")==0 ){ |
| 12211 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 12212 | "%s",cmdline_option_value(argc,argv,++i)); |
| 12213 | }else if( strcmp(z,"-newline")==0 ){ |
| 12214 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 12215 | "%s",cmdline_option_value(argc,argv,++i)); |
| 12216 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 12217 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
| 12218 | "%s",cmdline_option_value(argc,argv,++i)); |
| 12219 | }else if( strcmp(z,"-header")==0 ){ |
| 12220 | data.showHeader = 1; |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 12221 | ShellSetFlag(&data, SHFLG_HeaderSet); |
| 12222 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12223 | data.showHeader = 0; |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 12224 | ShellSetFlag(&data, SHFLG_HeaderSet); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12225 | }else if( strcmp(z,"-echo")==0 ){ |
| 12226 | ShellSetFlag(&data, SHFLG_Echo); |
| 12227 | }else if( strcmp(z,"-eqp")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 12228 | data.autoEQP = AUTOEQP_on; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12229 | }else if( strcmp(z,"-eqpfull")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 12230 | data.autoEQP = AUTOEQP_full; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12231 | }else if( strcmp(z,"-stats")==0 ){ |
| 12232 | data.statsOn = 1; |
| 12233 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 12234 | data.scanstatsOn = 1; |
| 12235 | }else if( strcmp(z,"-backslash")==0 ){ |
| 12236 | /* Undocumented command-line option: -backslash |
| 12237 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 12238 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 12239 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 12240 | */ |
| 12241 | ShellSetFlag(&data, SHFLG_Backslash); |
| 12242 | }else if( strcmp(z,"-bail")==0 ){ |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 12243 | /* No-op. The bail_on_error flag should already be set. */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12244 | }else if( strcmp(z,"-version")==0 ){ |
| 12245 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 12246 | return 0; |
| 12247 | }else if( strcmp(z,"-interactive")==0 ){ |
| 12248 | stdin_is_interactive = 1; |
| 12249 | }else if( strcmp(z,"-batch")==0 ){ |
| 12250 | stdin_is_interactive = 0; |
| 12251 | }else if( strcmp(z,"-heap")==0 ){ |
| 12252 | i++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12253 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 12254 | i+=2; |
| 12255 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 12256 | i+=2; |
drh | af6d1af | 2021-08-09 17:37:58 +0000 | [diff] [blame] | 12257 | }else if( strcmp(z,"-threadsafe")==0 ){ |
| 12258 | i+=2; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 12259 | }else if( strcmp(z,"-nonce")==0 ){ |
| 12260 | i += 2; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12261 | }else if( strcmp(z,"-mmap")==0 ){ |
| 12262 | i++; |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 12263 | }else if( strcmp(z,"-memtrace")==0 ){ |
| 12264 | i++; |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 12265 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 12266 | }else if( strcmp(z,"-sorterref")==0 ){ |
| 12267 | i++; |
| 12268 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12269 | }else if( strcmp(z,"-vfs")==0 ){ |
| 12270 | i++; |
| 12271 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 12272 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 12273 | i++; |
| 12274 | #endif |
| 12275 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 12276 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 12277 | i++; |
| 12278 | #endif |
| 12279 | }else if( strcmp(z,"-help")==0 ){ |
| 12280 | usage(1); |
| 12281 | }else if( strcmp(z,"-cmd")==0 ){ |
| 12282 | /* Run commands that follow -cmd first and separately from commands |
| 12283 | ** that simply appear on the command-line. This seems goofy. It would |
| 12284 | ** be better if all commands ran in the order that they appear. But |
| 12285 | ** we retain the goofy behavior for historical compatibility. */ |
| 12286 | if( i==argc-1 ) break; |
| 12287 | z = cmdline_option_value(argc,argv,++i); |
| 12288 | if( z[0]=='.' ){ |
| 12289 | rc = do_meta_command(z, &data); |
| 12290 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
| 12291 | }else{ |
| 12292 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 12293 | rc = shell_exec(&data, z, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12294 | if( zErrMsg!=0 ){ |
| 12295 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 12296 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 12297 | }else if( rc!=0 ){ |
| 12298 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
| 12299 | if( bail_on_error ) return rc; |
| 12300 | } |
| 12301 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 12302 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 12303 | }else if( strncmp(z, "-A", 2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 12304 | if( nCmd>0 ){ |
| 12305 | utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" |
| 12306 | " with \"%s\"\n", z); |
| 12307 | return 1; |
| 12308 | } |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 12309 | open_db(&data, OPEN_DB_ZIPFILE); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 12310 | if( z[2] ){ |
| 12311 | argv[i] = &z[2]; |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 12312 | arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 12313 | }else{ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 12314 | arDotCommand(&data, 1, argv+i, argc-i); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 12315 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 12316 | readStdin = 0; |
| 12317 | break; |
| 12318 | #endif |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 12319 | }else if( strcmp(z,"-safe")==0 ){ |
| 12320 | data.bSafeMode = data.bSafeModePersist = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12321 | }else{ |
| 12322 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 12323 | raw_printf(stderr,"Use -help for a list of options.\n"); |
| 12324 | return 1; |
| 12325 | } |
| 12326 | data.cMode = data.mode; |
| 12327 | } |
| 12328 | |
| 12329 | if( !readStdin ){ |
| 12330 | /* Run all arguments that do not begin with '-' as if they were separate |
| 12331 | ** command-line inputs, except for the argToSkip argument which contains |
| 12332 | ** the database filename. |
| 12333 | */ |
| 12334 | for(i=0; i<nCmd; i++){ |
| 12335 | if( azCmd[i][0]=='.' ){ |
| 12336 | rc = do_meta_command(azCmd[i], &data); |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 12337 | if( rc ){ |
| 12338 | free(azCmd); |
| 12339 | return rc==2 ? 0 : rc; |
| 12340 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12341 | }else{ |
| 12342 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 12343 | rc = shell_exec(&data, azCmd[i], &zErrMsg); |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 12344 | if( zErrMsg || rc ){ |
| 12345 | if( zErrMsg!=0 ){ |
| 12346 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 12347 | }else{ |
| 12348 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| 12349 | } |
| 12350 | sqlite3_free(zErrMsg); |
| 12351 | free(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12352 | return rc!=0 ? rc : 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12353 | } |
| 12354 | } |
| 12355 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12356 | }else{ |
| 12357 | /* Run commands received from standard input |
| 12358 | */ |
| 12359 | if( stdin_is_interactive ){ |
| 12360 | char *zHome; |
drh | a9e4be3 | 2018-10-10 18:56:40 +0000 | [diff] [blame] | 12361 | char *zHistory; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12362 | int nHistory; |
| 12363 | printf( |
| 12364 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
| 12365 | "Enter \".help\" for usage hints.\n", |
| 12366 | sqlite3_libversion(), sqlite3_sourceid() |
| 12367 | ); |
| 12368 | if( warnInmemoryDb ){ |
| 12369 | printf("Connected to a "); |
| 12370 | printBold("transient in-memory database"); |
| 12371 | printf(".\nUse \".open FILENAME\" to reopen on a " |
| 12372 | "persistent database.\n"); |
| 12373 | } |
drh | a9e4be3 | 2018-10-10 18:56:40 +0000 | [diff] [blame] | 12374 | zHistory = getenv("SQLITE_HISTORY"); |
| 12375 | if( zHistory ){ |
| 12376 | zHistory = strdup(zHistory); |
| 12377 | }else if( (zHome = find_home_dir(0))!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12378 | nHistory = strlen30(zHome) + 20; |
| 12379 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 12380 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 12381 | } |
| 12382 | } |
| 12383 | if( zHistory ){ shell_read_history(zHistory); } |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 12384 | #if HAVE_READLINE || HAVE_EDITLINE |
| 12385 | rl_attempted_completion_function = readline_completion; |
| 12386 | #elif HAVE_LINENOISE |
| 12387 | linenoiseSetCompletionCallback(linenoise_completion); |
| 12388 | #endif |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 12389 | data.in = 0; |
| 12390 | rc = process_input(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12391 | if( zHistory ){ |
drh | 5a75dd8 | 2017-07-18 20:59:40 +0000 | [diff] [blame] | 12392 | shell_stifle_history(2000); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12393 | shell_write_history(zHistory); |
| 12394 | free(zHistory); |
| 12395 | } |
| 12396 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 12397 | data.in = stdin; |
| 12398 | rc = process_input(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12399 | } |
| 12400 | } |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 12401 | free(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12402 | set_table_name(&data, 0); |
| 12403 | if( data.db ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12404 | session_close_all(&data, -1); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 12405 | close_db(data.db); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12406 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12407 | for(i=0; i<ArraySize(data.aAuxDb); i++){ |
| 12408 | sqlite3_free(data.aAuxDb[i].zFreeOnClose); |
| 12409 | if( data.aAuxDb[i].db ){ |
| 12410 | session_close_all(&data, i); |
| 12411 | close_db(data.aAuxDb[i].db); |
| 12412 | } |
| 12413 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12414 | find_home_dir(1); |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 12415 | output_reset(&data); |
| 12416 | data.doXdgOpen = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 12417 | clearTempFile(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12418 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 12419 | for(i=0; i<argcToFree; i++) free(argvToFree[i]); |
| 12420 | free(argvToFree); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12421 | #endif |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 12422 | free(data.colWidth); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 12423 | free(data.zNonce); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 12424 | /* Clear the global data structure so that valgrind will detect memory |
| 12425 | ** leaks */ |
| 12426 | memset(&data, 0, sizeof(data)); |
larrybr | fa5dfa8 | 2022-05-07 03:53:14 +0000 | [diff] [blame] | 12427 | #ifdef SQLITE_DEBUG |
| 12428 | if( sqlite3_memory_used()>mem_main_enter ){ |
| 12429 | utf8_printf(stderr, "Memory leaked: %u bytes\n", |
| 12430 | (unsigned int)(sqlite3_memory_used()-mem_main_enter)); |
| 12431 | } |
| 12432 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12433 | return rc; |
| 12434 | } |