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 |
drh | 80fafc2 | 2022-11-07 19:40:20 +0000 | [diff] [blame] | 19 | typedef unsigned int u32; |
| 20 | typedef unsigned short int u16; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 23 | ** Optionally #include a user-defined header, whereby compilation options |
| 24 | ** may be set prior to where they take effect, but after platform setup. |
| 25 | ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include |
| 26 | ** file. Note that this macro has a like effect on sqlite3.c compilation. |
| 27 | */ |
larrybr | 7813839 | 2022-02-12 02:15:37 +0000 | [diff] [blame] | 28 | # define SHELL_STRINGIFY_(f) #f |
| 29 | # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f) |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 30 | #ifdef SQLITE_CUSTOM_INCLUDE |
larrybr | 7813839 | 2022-02-12 02:15:37 +0000 | [diff] [blame] | 31 | # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE) |
larrybr | a13c0c7 | 2021-07-09 00:12:05 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | /* |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 35 | ** Determine if we are dealing with WinRT, which provides only a subset of |
| 36 | ** the full Win32 API. |
| 37 | */ |
| 38 | #if !defined(SQLITE_OS_WINRT) |
| 39 | # define SQLITE_OS_WINRT 0 |
| 40 | #endif |
| 41 | |
| 42 | /* |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 43 | ** If SQLITE_SHELL_FIDDLE is defined then the shell is modified |
| 44 | ** somewhat for use as a WASM module in a web browser. This flag |
| 45 | ** should only be used when building the "fiddle" web application, as |
| 46 | ** the browser-mode build has much different user input requirements |
| 47 | ** and this build mode rewires the user input subsystem to account for |
| 48 | ** that. |
stephan | ea1e3b4 | 2022-07-12 09:40:27 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 52 | ** Warning pragmas copied from msvc.h in the core. |
| 53 | */ |
| 54 | #if defined(_MSC_VER) |
| 55 | #pragma warning(disable : 4054) |
| 56 | #pragma warning(disable : 4055) |
| 57 | #pragma warning(disable : 4100) |
| 58 | #pragma warning(disable : 4127) |
| 59 | #pragma warning(disable : 4130) |
| 60 | #pragma warning(disable : 4152) |
| 61 | #pragma warning(disable : 4189) |
| 62 | #pragma warning(disable : 4206) |
| 63 | #pragma warning(disable : 4210) |
| 64 | #pragma warning(disable : 4232) |
| 65 | #pragma warning(disable : 4244) |
| 66 | #pragma warning(disable : 4305) |
| 67 | #pragma warning(disable : 4306) |
| 68 | #pragma warning(disable : 4702) |
| 69 | #pragma warning(disable : 4706) |
| 70 | #endif /* defined(_MSC_VER) */ |
| 71 | |
| 72 | /* |
| 73 | ** No support for loadable extensions in VxWorks. |
| 74 | */ |
| 75 | #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
| 76 | # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | ** Enable large-file support for fopen() and friends on unix. |
| 81 | */ |
| 82 | #ifndef SQLITE_DISABLE_LFS |
| 83 | # define _LARGE_FILE 1 |
| 84 | # ifndef _FILE_OFFSET_BITS |
| 85 | # define _FILE_OFFSET_BITS 64 |
| 86 | # endif |
| 87 | # define _LARGEFILE_SOURCE 1 |
| 88 | #endif |
| 89 | |
stephan | 6502271 | 2022-09-21 16:21:21 +0000 | [diff] [blame] | 90 | #if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE) |
| 91 | /* |
| 92 | ** emcc requires _POSIX_SOURCE (or one of several similar defines) |
| 93 | ** to expose strdup(). |
| 94 | */ |
| 95 | # define _POSIX_SOURCE |
| 96 | #endif |
| 97 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 98 | #include <stdlib.h> |
| 99 | #include <string.h> |
| 100 | #include <stdio.h> |
| 101 | #include <assert.h> |
| 102 | #include "sqlite3.h" |
drh | 1e506b5 | 2018-01-05 21:01:37 +0000 | [diff] [blame] | 103 | typedef sqlite3_int64 i64; |
| 104 | typedef sqlite3_uint64 u64; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 105 | typedef unsigned char u8; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 106 | #if SQLITE_USER_AUTHENTICATION |
| 107 | # include "sqlite3userauth.h" |
| 108 | #endif |
| 109 | #include <ctype.h> |
| 110 | #include <stdarg.h> |
| 111 | |
| 112 | #if !defined(_WIN32) && !defined(WIN32) |
| 113 | # include <signal.h> |
| 114 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 115 | # include <pwd.h> |
| 116 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 117 | #endif |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 118 | #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 119 | # include <unistd.h> |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 120 | # include <dirent.h> |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 121 | # define GETPID getpid |
mistachkin | 562f0c8 | 2018-01-09 00:28:24 +0000 | [diff] [blame] | 122 | # if defined(__MINGW32__) |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 123 | # define DIRENT dirent |
mistachkin | 2f74b3c | 2018-01-05 20:26:06 +0000 | [diff] [blame] | 124 | # ifndef S_ISLNK |
| 125 | # define S_ISLNK(mode) (0) |
| 126 | # endif |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 127 | # endif |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 128 | #else |
| 129 | # define GETPID (int)GetCurrentProcessId |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 130 | #endif |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 131 | #include <sys/types.h> |
| 132 | #include <sys/stat.h> |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 133 | |
| 134 | #if HAVE_READLINE |
| 135 | # include <readline/readline.h> |
| 136 | # include <readline/history.h> |
| 137 | #endif |
| 138 | |
| 139 | #if HAVE_EDITLINE |
| 140 | # include <editline/readline.h> |
| 141 | #endif |
| 142 | |
| 143 | #if HAVE_EDITLINE || HAVE_READLINE |
| 144 | |
| 145 | # define shell_add_history(X) add_history(X) |
| 146 | # define shell_read_history(X) read_history(X) |
| 147 | # define shell_write_history(X) write_history(X) |
| 148 | # define shell_stifle_history(X) stifle_history(X) |
| 149 | # define shell_readline(X) readline(X) |
| 150 | |
| 151 | #elif HAVE_LINENOISE |
| 152 | |
| 153 | # include "linenoise.h" |
| 154 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 155 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 156 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 157 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 158 | # define shell_readline(X) linenoise(X) |
| 159 | |
| 160 | #else |
| 161 | |
| 162 | # define shell_read_history(X) |
| 163 | # define shell_write_history(X) |
| 164 | # define shell_stifle_history(X) |
| 165 | |
| 166 | # define SHELL_USE_LOCAL_GETLINE 1 |
| 167 | #endif |
| 168 | |
| 169 | |
| 170 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 171 | # if SQLITE_OS_WINRT |
| 172 | # define SQLITE_OMIT_POPEN 1 |
| 173 | # else |
| 174 | # include <io.h> |
| 175 | # include <fcntl.h> |
| 176 | # define isatty(h) _isatty(h) |
| 177 | # ifndef access |
| 178 | # define access(f,m) _access((f),(m)) |
| 179 | # endif |
| 180 | # ifndef unlink |
| 181 | # define unlink _unlink |
| 182 | # endif |
| 183 | # ifndef strdup |
| 184 | # define strdup _strdup |
| 185 | # endif |
| 186 | # undef popen |
| 187 | # define popen _popen |
| 188 | # undef pclose |
| 189 | # define pclose _pclose |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 190 | # endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 191 | #else |
| 192 | /* Make sure isatty() has a prototype. */ |
| 193 | extern int isatty(int); |
| 194 | |
| 195 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 196 | /* popen and pclose are not C89 functions and so are |
| 197 | ** sometimes omitted from the <stdio.h> header */ |
| 198 | extern FILE *popen(const char*,const char*); |
| 199 | extern int pclose(FILE*); |
| 200 | # else |
| 201 | # define SQLITE_OMIT_POPEN 1 |
| 202 | # endif |
| 203 | #endif |
| 204 | |
| 205 | #if defined(_WIN32_WCE) |
| 206 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 207 | * thus we always assume that we have a console. That can be |
| 208 | * overridden with the -batch command line option. |
| 209 | */ |
| 210 | #define isatty(x) 1 |
| 211 | #endif |
| 212 | |
| 213 | /* ctype macros that work with signed characters */ |
| 214 | #define IsSpace(X) isspace((unsigned char)X) |
| 215 | #define IsDigit(X) isdigit((unsigned char)X) |
| 216 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 217 | |
| 218 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 219 | #if SQLITE_OS_WINRT |
| 220 | #include <intrin.h> |
| 221 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 222 | #include <windows.h> |
| 223 | |
| 224 | /* string conversion routines only needed on Win32 */ |
| 225 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 226 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 227 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| 228 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 229 | #endif |
| 230 | |
| 231 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 232 | ** are automatically translated into \r\n. However, this behavior needs |
| 233 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 234 | ** rendering quoted strings that contain \n characters). The following |
| 235 | ** routines take care of that. |
| 236 | */ |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 237 | #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 238 | static void setBinaryMode(FILE *file, int isOutput){ |
| 239 | if( isOutput ) fflush(file); |
| 240 | _setmode(_fileno(file), _O_BINARY); |
| 241 | } |
| 242 | static void setTextMode(FILE *file, int isOutput){ |
| 243 | if( isOutput ) fflush(file); |
| 244 | _setmode(_fileno(file), _O_TEXT); |
| 245 | } |
| 246 | #else |
| 247 | # define setBinaryMode(X,Y) |
| 248 | # define setTextMode(X,Y) |
| 249 | #endif |
| 250 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 251 | /* True if the timer is enabled */ |
| 252 | static int enableTimer = 0; |
| 253 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 254 | /* A version of strcmp() that works with NULL values */ |
| 255 | static int cli_strcmp(const char *a, const char *b){ |
| 256 | if( a==0 ) a = ""; |
| 257 | if( b==0 ) b = ""; |
| 258 | return strcmp(a,b); |
| 259 | } |
| 260 | static int cli_strncmp(const char *a, const char *b, size_t n){ |
| 261 | if( a==0 ) a = ""; |
| 262 | if( b==0 ) b = ""; |
| 263 | return strncmp(a,b,n); |
| 264 | } |
| 265 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 266 | /* Return the current wall-clock time */ |
| 267 | static sqlite3_int64 timeOfDay(void){ |
| 268 | static sqlite3_vfs *clockVfs = 0; |
| 269 | sqlite3_int64 t; |
| 270 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
drh | a959bf5 | 2021-06-15 15:15:40 +0000 | [diff] [blame] | 271 | if( clockVfs==0 ) return 0; /* Never actually happens */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 272 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 273 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 274 | }else{ |
| 275 | double r; |
| 276 | clockVfs->xCurrentTime(clockVfs, &r); |
| 277 | t = (sqlite3_int64)(r*86400000.0); |
| 278 | } |
| 279 | return t; |
| 280 | } |
| 281 | |
| 282 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
| 283 | #include <sys/time.h> |
| 284 | #include <sys/resource.h> |
| 285 | |
| 286 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 287 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 288 | struct rusage { |
| 289 | struct timeval ru_utime; /* user CPU time used */ |
| 290 | struct timeval ru_stime; /* system CPU time used */ |
| 291 | }; |
| 292 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 293 | #endif |
| 294 | |
| 295 | /* Saved resource information for the beginning of an operation */ |
| 296 | static struct rusage sBegin; /* CPU time at start */ |
| 297 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 298 | |
| 299 | /* |
| 300 | ** Begin timing an operation |
| 301 | */ |
| 302 | static void beginTimer(void){ |
| 303 | if( enableTimer ){ |
| 304 | getrusage(RUSAGE_SELF, &sBegin); |
| 305 | iBegin = timeOfDay(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* Return the difference of two time_structs in seconds */ |
| 310 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 311 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 312 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** Print the timing results. |
| 317 | */ |
| 318 | static void endTimer(void){ |
| 319 | if( enableTimer ){ |
| 320 | sqlite3_int64 iEnd = timeOfDay(); |
| 321 | struct rusage sEnd; |
| 322 | getrusage(RUSAGE_SELF, &sEnd); |
| 323 | printf("Run Time: real %.3f user %f sys %f\n", |
| 324 | (iEnd - iBegin)*0.001, |
| 325 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 326 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #define BEGIN_TIMER beginTimer() |
| 331 | #define END_TIMER endTimer() |
| 332 | #define HAS_TIMER 1 |
| 333 | |
| 334 | #elif (defined(_WIN32) || defined(WIN32)) |
| 335 | |
| 336 | /* Saved resource information for the beginning of an operation */ |
| 337 | static HANDLE hProcess; |
| 338 | static FILETIME ftKernelBegin; |
| 339 | static FILETIME ftUserBegin; |
| 340 | static sqlite3_int64 ftWallBegin; |
| 341 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 342 | LPFILETIME, LPFILETIME); |
| 343 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 344 | |
| 345 | /* |
| 346 | ** Check to see if we have timer support. Return 1 if necessary |
| 347 | ** support found (or found previously). |
| 348 | */ |
| 349 | static int hasTimer(void){ |
| 350 | if( getProcessTimesAddr ){ |
| 351 | return 1; |
| 352 | } else { |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 353 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 354 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 355 | ** versions. See if the version we are running on has it, and if it |
| 356 | ** does, save off a pointer to it and the current process handle. |
| 357 | */ |
| 358 | hProcess = GetCurrentProcess(); |
| 359 | if( hProcess ){ |
| 360 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 361 | if( NULL != hinstLib ){ |
| 362 | getProcessTimesAddr = |
| 363 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 364 | if( NULL != getProcessTimesAddr ){ |
| 365 | return 1; |
| 366 | } |
| 367 | FreeLibrary(hinstLib); |
| 368 | } |
| 369 | } |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 370 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 371 | } |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | ** Begin timing an operation |
| 377 | */ |
| 378 | static void beginTimer(void){ |
| 379 | if( enableTimer && getProcessTimesAddr ){ |
| 380 | FILETIME ftCreation, ftExit; |
| 381 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 382 | &ftKernelBegin,&ftUserBegin); |
| 383 | ftWallBegin = timeOfDay(); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Return the difference of two FILETIME structs in seconds */ |
| 388 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 389 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 390 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 391 | return (double) ((i64End - i64Start) / 10000000.0); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | ** Print the timing results. |
| 396 | */ |
| 397 | static void endTimer(void){ |
| 398 | if( enableTimer && getProcessTimesAddr){ |
| 399 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 400 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 401 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 402 | printf("Run Time: real %.3f user %f sys %f\n", |
| 403 | (ftWallEnd - ftWallBegin)*0.001, |
| 404 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 405 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | #define BEGIN_TIMER beginTimer() |
| 410 | #define END_TIMER endTimer() |
| 411 | #define HAS_TIMER hasTimer() |
| 412 | |
| 413 | #else |
| 414 | #define BEGIN_TIMER |
| 415 | #define END_TIMER |
| 416 | #define HAS_TIMER 0 |
| 417 | #endif |
| 418 | |
| 419 | /* |
| 420 | ** Used to prevent warnings about unused parameters |
| 421 | */ |
| 422 | #define UNUSED_PARAMETER(x) (void)(x) |
| 423 | |
| 424 | /* |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 425 | ** Number of elements in an array |
| 426 | */ |
| 427 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
| 428 | |
| 429 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 430 | ** If the following flag is set, then command execution stops |
| 431 | ** at an error if we are not interactive. |
| 432 | */ |
| 433 | static int bail_on_error = 0; |
| 434 | |
| 435 | /* |
| 436 | ** Threat stdin as an interactive input if the following variable |
| 437 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 438 | */ |
| 439 | static int stdin_is_interactive = 1; |
| 440 | |
| 441 | /* |
| 442 | ** On Windows systems we have to know if standard output is a console |
| 443 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 444 | ** true if translation is required. |
| 445 | */ |
| 446 | static int stdout_is_console = 1; |
| 447 | |
| 448 | /* |
| 449 | ** The following is the open SQLite database. We make a pointer |
| 450 | ** to this database a static variable so that it can be accessed |
| 451 | ** by the SIGINT handler to interrupt database processing. |
| 452 | */ |
| 453 | static sqlite3 *globalDb = 0; |
| 454 | |
| 455 | /* |
| 456 | ** True if an interrupt (Control-C) has been received. |
| 457 | */ |
| 458 | static volatile int seenInterrupt = 0; |
| 459 | |
| 460 | /* |
| 461 | ** This is the name of our program. It is set in main(), used |
| 462 | ** in a number of other places, mostly for error messages. |
| 463 | */ |
| 464 | static char *Argv0; |
| 465 | |
| 466 | /* |
| 467 | ** Prompt strings. Initialized in main. Settable with |
| 468 | ** .prompt main continue |
| 469 | */ |
| 470 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 471 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 472 | |
| 473 | /* |
| 474 | ** Render output like fprintf(). Except, if the output is going to the |
| 475 | ** console and if this is running on a Windows machine, translate the |
| 476 | ** output from UTF-8 into MBCS. |
| 477 | */ |
| 478 | #if defined(_WIN32) || defined(WIN32) |
| 479 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 480 | va_list ap; |
| 481 | va_start(ap, zFormat); |
| 482 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
| 483 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
| 484 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
| 485 | sqlite3_free(z1); |
| 486 | fputs(z2, out); |
| 487 | sqlite3_free(z2); |
| 488 | }else{ |
| 489 | vfprintf(out, zFormat, ap); |
| 490 | } |
| 491 | va_end(ap); |
| 492 | } |
| 493 | #elif !defined(utf8_printf) |
| 494 | # define utf8_printf fprintf |
| 495 | #endif |
| 496 | |
| 497 | /* |
| 498 | ** Render output like fprintf(). This should not be used on anything that |
| 499 | ** includes string formatting (e.g. "%s"). |
| 500 | */ |
| 501 | #if !defined(raw_printf) |
| 502 | # define raw_printf fprintf |
| 503 | #endif |
| 504 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 505 | /* Indicate out-of-memory and exit. */ |
| 506 | static void shell_out_of_memory(void){ |
| 507 | raw_printf(stderr,"Error: out of memory\n"); |
| 508 | exit(1); |
| 509 | } |
| 510 | |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 511 | /* Check a pointer to see if it is NULL. If it is NULL, exit with an |
| 512 | ** out-of-memory error. |
| 513 | */ |
| 514 | static void shell_check_oom(void *p){ |
| 515 | if( p==0 ) shell_out_of_memory(); |
| 516 | } |
| 517 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 518 | /* |
| 519 | ** Write I/O traces to the following stream. |
| 520 | */ |
| 521 | #ifdef SQLITE_ENABLE_IOTRACE |
| 522 | static FILE *iotrace = 0; |
| 523 | #endif |
| 524 | |
| 525 | /* |
| 526 | ** This routine works like printf in that its first argument is a |
| 527 | ** format string and subsequent arguments are values to be substituted |
| 528 | ** in place of % fields. The result of formatting this string |
| 529 | ** is written to iotrace. |
| 530 | */ |
| 531 | #ifdef SQLITE_ENABLE_IOTRACE |
| 532 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
| 533 | va_list ap; |
| 534 | char *z; |
| 535 | if( iotrace==0 ) return; |
| 536 | va_start(ap, zFormat); |
| 537 | z = sqlite3_vmprintf(zFormat, ap); |
| 538 | va_end(ap); |
| 539 | utf8_printf(iotrace, "%s", z); |
| 540 | sqlite3_free(z); |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | /* |
| 545 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 546 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 547 | ** in bytes. This is different from the %*.*s specification in printf |
| 548 | ** since with %*.*s the width is measured in bytes, not characters. |
| 549 | */ |
| 550 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 551 | int i; |
| 552 | int n; |
| 553 | int aw = w<0 ? -w : w; |
drh | 4853aa1 | 2022-10-27 18:20:08 +0000 | [diff] [blame] | 554 | if( zUtf==0 ) zUtf = ""; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 555 | for(i=n=0; zUtf[i]; i++){ |
| 556 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 557 | n++; |
| 558 | if( n==aw ){ |
| 559 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | if( n>=aw ){ |
| 565 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 566 | }else if( w<0 ){ |
| 567 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 568 | }else{ |
| 569 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | ** Determines if a string is a number of not. |
| 576 | */ |
| 577 | static int isNumber(const char *z, int *realnum){ |
| 578 | if( *z=='-' || *z=='+' ) z++; |
| 579 | if( !IsDigit(*z) ){ |
| 580 | return 0; |
| 581 | } |
| 582 | z++; |
| 583 | if( realnum ) *realnum = 0; |
| 584 | while( IsDigit(*z) ){ z++; } |
| 585 | if( *z=='.' ){ |
| 586 | z++; |
| 587 | if( !IsDigit(*z) ) return 0; |
| 588 | while( IsDigit(*z) ){ z++; } |
| 589 | if( realnum ) *realnum = 1; |
| 590 | } |
| 591 | if( *z=='e' || *z=='E' ){ |
| 592 | z++; |
| 593 | if( *z=='+' || *z=='-' ) z++; |
| 594 | if( !IsDigit(*z) ) return 0; |
| 595 | while( IsDigit(*z) ){ z++; } |
| 596 | if( realnum ) *realnum = 1; |
| 597 | } |
| 598 | return *z==0; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | ** Compute a string length that is limited to what can be stored in |
| 603 | ** lower 30 bits of a 32-bit signed integer. |
| 604 | */ |
| 605 | static int strlen30(const char *z){ |
| 606 | const char *z2 = z; |
| 607 | while( *z2 ){ z2++; } |
| 608 | return 0x3fffffff & (int)(z2 - z); |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 613 | ** count as a single character. |
| 614 | */ |
| 615 | static int strlenChar(const char *z){ |
| 616 | int n = 0; |
| 617 | while( *z ){ |
| 618 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 619 | } |
| 620 | return n; |
| 621 | } |
| 622 | |
| 623 | /* |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 624 | ** Return open FILE * if zFile exists, can be opened for read |
| 625 | ** and is an ordinary file or a character stream source. |
| 626 | ** Otherwise return 0. |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 627 | */ |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 628 | static FILE * openChrSource(const char *zFile){ |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 629 | #ifdef _WIN32 |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 630 | struct _stat x = {0}; |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 631 | # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 632 | /* On Windows, open first, then check the stream nature. This order |
| 633 | ** is necessary because _stat() and sibs, when checking a named pipe, |
| 634 | ** effectively break the pipe as its supplier sees it. */ |
| 635 | FILE *rv = fopen(zFile, "rb"); |
| 636 | if( rv==0 ) return 0; |
| 637 | if( _fstat(_fileno(rv), &x) != 0 |
| 638 | || !STAT_CHR_SRC(x.st_mode)){ |
| 639 | fclose(rv); |
| 640 | rv = 0; |
| 641 | } |
| 642 | return rv; |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 643 | #else |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 644 | struct stat x = {0}; |
| 645 | int rc = stat(zFile, &x); |
| 646 | # 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] | 647 | if( rc!=0 ) return 0; |
| 648 | if( STAT_CHR_SRC(x.st_mode) ){ |
| 649 | return fopen(zFile, "rb"); |
| 650 | }else{ |
| 651 | return 0; |
| 652 | } |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 653 | #endif |
larrybr | d000785 | 2021-09-13 23:11:46 +0000 | [diff] [blame] | 654 | #undef STAT_CHR_SRC |
| 655 | } |
drh | bbd620e | 2020-07-20 23:33:11 +0000 | [diff] [blame] | 656 | |
| 657 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 658 | ** This routine reads a line of text from FILE in, stores |
| 659 | ** the text in memory obtained from malloc() and returns a pointer |
| 660 | ** to the text. NULL is returned at end of file, or if malloc() |
| 661 | ** fails. |
| 662 | ** |
| 663 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 664 | ** a previous call to this routine that may be reused. |
| 665 | */ |
| 666 | static char *local_getline(char *zLine, FILE *in){ |
| 667 | int nLine = zLine==0 ? 0 : 100; |
| 668 | int n = 0; |
| 669 | |
| 670 | while( 1 ){ |
| 671 | if( n+100>nLine ){ |
| 672 | nLine = nLine*2 + 100; |
| 673 | zLine = realloc(zLine, nLine); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 674 | shell_check_oom(zLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 675 | } |
| 676 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 677 | if( n==0 ){ |
| 678 | free(zLine); |
| 679 | return 0; |
| 680 | } |
| 681 | zLine[n] = 0; |
| 682 | break; |
| 683 | } |
| 684 | while( zLine[n] ) n++; |
| 685 | if( n>0 && zLine[n-1]=='\n' ){ |
| 686 | n--; |
| 687 | if( n>0 && zLine[n-1]=='\r' ) n--; |
| 688 | zLine[n] = 0; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | #if defined(_WIN32) || defined(WIN32) |
| 693 | /* For interactive input on Windows systems, translate the |
| 694 | ** multi-byte characterset characters into UTF-8. */ |
| 695 | if( stdin_is_interactive && in==stdin ){ |
| 696 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| 697 | if( zTrans ){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 698 | i64 nTrans = strlen(zTrans)+1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 699 | if( nTrans>nLine ){ |
| 700 | zLine = realloc(zLine, nTrans); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 701 | shell_check_oom(zLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 702 | } |
| 703 | memcpy(zLine, zTrans, nTrans); |
| 704 | sqlite3_free(zTrans); |
| 705 | } |
| 706 | } |
| 707 | #endif /* defined(_WIN32) || defined(WIN32) */ |
| 708 | return zLine; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | ** Retrieve a single line of input text. |
| 713 | ** |
| 714 | ** If in==0 then read from standard input and prompt before each line. |
| 715 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 716 | ** If isContinuation is zero, then the main prompt should be used. |
| 717 | ** |
| 718 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 719 | ** routine that can be reused. |
| 720 | ** |
| 721 | ** The result is stored in space obtained from malloc() and must either |
| 722 | ** be freed by the caller or else passed back into this routine via the |
| 723 | ** zPrior argument for reuse. |
| 724 | */ |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 725 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 726 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 727 | char *zPrompt; |
| 728 | char *zResult; |
| 729 | if( in!=0 ){ |
| 730 | zResult = local_getline(zPrior, in); |
| 731 | }else{ |
| 732 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
| 733 | #if SHELL_USE_LOCAL_GETLINE |
| 734 | printf("%s", zPrompt); |
| 735 | fflush(stdout); |
| 736 | zResult = local_getline(zPrior, stdin); |
| 737 | #else |
| 738 | free(zPrior); |
| 739 | zResult = shell_readline(zPrompt); |
| 740 | if( zResult && *zResult ) shell_add_history(zResult); |
| 741 | #endif |
| 742 | } |
| 743 | return zResult; |
| 744 | } |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 745 | #endif /* !SQLITE_SHELL_FIDDLE */ |
drh | 5af0698 | 2018-01-10 00:53:55 +0000 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 749 | ** is not a hex digit. |
| 750 | */ |
| 751 | static int hexDigitValue(char c){ |
| 752 | if( c>='0' && c<='9' ) return c - '0'; |
| 753 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 754 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 760 | */ |
| 761 | static sqlite3_int64 integerValue(const char *zArg){ |
| 762 | sqlite3_int64 v = 0; |
| 763 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 764 | { "KiB", 1024 }, |
| 765 | { "MiB", 1024*1024 }, |
| 766 | { "GiB", 1024*1024*1024 }, |
| 767 | { "KB", 1000 }, |
| 768 | { "MB", 1000000 }, |
| 769 | { "GB", 1000000000 }, |
| 770 | { "K", 1000 }, |
| 771 | { "M", 1000000 }, |
| 772 | { "G", 1000000000 }, |
| 773 | }; |
| 774 | int i; |
| 775 | int isNeg = 0; |
| 776 | if( zArg[0]=='-' ){ |
| 777 | isNeg = 1; |
| 778 | zArg++; |
| 779 | }else if( zArg[0]=='+' ){ |
| 780 | zArg++; |
| 781 | } |
| 782 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 783 | int x; |
| 784 | zArg += 2; |
| 785 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 786 | v = (v<<4) + x; |
| 787 | zArg++; |
| 788 | } |
| 789 | }else{ |
| 790 | while( IsDigit(zArg[0]) ){ |
| 791 | v = v*10 + zArg[0] - '0'; |
| 792 | zArg++; |
| 793 | } |
| 794 | } |
| 795 | for(i=0; i<ArraySize(aMult); i++){ |
| 796 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 797 | v *= aMult[i].iMult; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | return isNeg? -v : v; |
| 802 | } |
| 803 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 804 | /* |
| 805 | ** A variable length string to which one can append text. |
| 806 | */ |
| 807 | typedef struct ShellText ShellText; |
| 808 | struct ShellText { |
| 809 | char *z; |
| 810 | int n; |
| 811 | int nAlloc; |
| 812 | }; |
| 813 | |
| 814 | /* |
| 815 | ** Initialize and destroy a ShellText object |
| 816 | */ |
| 817 | static void initText(ShellText *p){ |
| 818 | memset(p, 0, sizeof(*p)); |
| 819 | } |
| 820 | static void freeText(ShellText *p){ |
| 821 | free(p->z); |
| 822 | initText(p); |
| 823 | } |
| 824 | |
| 825 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 826 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 827 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 828 | ** zIn, if it was not NULL, is freed. |
| 829 | ** |
| 830 | ** If the third argument, quote, is not '\0', then it is used as a |
| 831 | ** quote character for zAppend. |
| 832 | */ |
stephan | 0076e49 | 2022-05-18 18:10:17 +0000 | [diff] [blame] | 833 | static void appendText(ShellText *p, const char *zAppend, char quote){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 834 | i64 len; |
| 835 | i64 i; |
| 836 | i64 nAppend = strlen30(zAppend); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 837 | |
| 838 | len = nAppend+p->n+1; |
| 839 | if( quote ){ |
| 840 | len += 2; |
| 841 | for(i=0; i<nAppend; i++){ |
| 842 | if( zAppend[i]==quote ) len++; |
| 843 | } |
| 844 | } |
| 845 | |
drh | 11a9ad5 | 2021-10-04 18:21:14 +0000 | [diff] [blame] | 846 | if( p->z==0 || p->n+len>=p->nAlloc ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 847 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 848 | p->z = realloc(p->z, p->nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 849 | shell_check_oom(p->z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | if( quote ){ |
| 853 | char *zCsr = p->z+p->n; |
| 854 | *zCsr++ = quote; |
| 855 | for(i=0; i<nAppend; i++){ |
| 856 | *zCsr++ = zAppend[i]; |
| 857 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 858 | } |
| 859 | *zCsr++ = quote; |
| 860 | p->n = (int)(zCsr - p->z); |
| 861 | *zCsr = '\0'; |
| 862 | }else{ |
| 863 | memcpy(p->z+p->n, zAppend, nAppend); |
| 864 | p->n += nAppend; |
| 865 | p->z[p->n] = '\0'; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 871 | ** because it contains non-alphanumeric characters, or because it is an |
| 872 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 873 | ** that quoting is required. |
| 874 | ** |
| 875 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 876 | */ |
| 877 | static char quoteChar(const char *zName){ |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 878 | int i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 879 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 880 | for(i=0; zName[i]; i++){ |
| 881 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 882 | } |
drh | fc0ec3e | 2018-04-25 19:02:48 +0000 | [diff] [blame] | 883 | return sqlite3_keyword_check(zName, i) ? '"' : 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 887 | ** Construct a fake object name and column list to describe the structure |
| 888 | ** of the view, virtual table, or table valued function zSchema.zName. |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 889 | */ |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 890 | static char *shellFakeSchema( |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 891 | sqlite3 *db, /* The database connection containing the vtab */ |
| 892 | const char *zSchema, /* Schema of the database holding the vtab */ |
| 893 | const char *zName /* The name of the virtual table */ |
| 894 | ){ |
| 895 | sqlite3_stmt *pStmt = 0; |
| 896 | char *zSql; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 897 | ShellText s; |
| 898 | char cQuote; |
| 899 | char *zDiv = "("; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 900 | int nRow = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 901 | |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 902 | zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", |
| 903 | zSchema ? zSchema : "main", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 904 | shell_check_oom(zSql); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 905 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 906 | sqlite3_free(zSql); |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 907 | initText(&s); |
| 908 | if( zSchema ){ |
| 909 | cQuote = quoteChar(zSchema); |
| 910 | if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; |
| 911 | appendText(&s, zSchema, cQuote); |
| 912 | appendText(&s, ".", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 913 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 914 | cQuote = quoteChar(zName); |
| 915 | appendText(&s, zName, cQuote); |
| 916 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 917 | const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 918 | nRow++; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 919 | appendText(&s, zDiv, 0); |
| 920 | zDiv = ","; |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 921 | if( zCol==0 ) zCol = ""; |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 922 | cQuote = quoteChar(zCol); |
| 923 | appendText(&s, zCol, cQuote); |
| 924 | } |
| 925 | appendText(&s, ")", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 926 | sqlite3_finalize(pStmt); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 927 | if( nRow==0 ){ |
| 928 | freeText(&s); |
| 929 | s.z = 0; |
| 930 | } |
drh | 1d315cf | 2018-01-01 21:49:43 +0000 | [diff] [blame] | 931 | return s.z; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 935 | ** SQL function: shell_module_schema(X) |
| 936 | ** |
| 937 | ** Return a fake schema for the table-valued function or eponymous virtual |
| 938 | ** table X. |
| 939 | */ |
| 940 | static void shellModuleSchema( |
| 941 | sqlite3_context *pCtx, |
| 942 | int nVal, |
| 943 | sqlite3_value **apVal |
| 944 | ){ |
drh | 511b118 | 2021-12-16 13:56:04 +0000 | [diff] [blame] | 945 | const char *zName; |
| 946 | char *zFake; |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 947 | UNUSED_PARAMETER(nVal); |
drh | 511b118 | 2021-12-16 13:56:04 +0000 | [diff] [blame] | 948 | zName = (const char*)sqlite3_value_text(apVal[0]); |
| 949 | zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 950 | if( zFake ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 951 | sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake), |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 952 | -1, sqlite3_free); |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 953 | free(zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 958 | ** SQL function: shell_add_schema(S,X) |
| 959 | ** |
| 960 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 961 | ** Examples: |
| 962 | ** |
| 963 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 964 | ** |
| 965 | ** Also works on |
| 966 | ** |
| 967 | ** CREATE INDEX |
| 968 | ** CREATE UNIQUE INDEX |
| 969 | ** CREATE VIEW |
| 970 | ** CREATE TRIGGER |
| 971 | ** CREATE VIRTUAL TABLE |
| 972 | ** |
| 973 | ** 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] | 974 | ** attached databases into the middle of the sqlite_schema.sql field. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 975 | */ |
| 976 | static void shellAddSchemaName( |
| 977 | sqlite3_context *pCtx, |
| 978 | int nVal, |
| 979 | sqlite3_value **apVal |
| 980 | ){ |
| 981 | static const char *aPrefix[] = { |
| 982 | "TABLE", |
| 983 | "INDEX", |
| 984 | "UNIQUE INDEX", |
| 985 | "VIEW", |
| 986 | "TRIGGER", |
| 987 | "VIRTUAL TABLE" |
| 988 | }; |
| 989 | int i = 0; |
| 990 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 991 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 992 | const char *zName = (const char*)sqlite3_value_text(apVal[2]); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 993 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 994 | UNUSED_PARAMETER(nVal); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 995 | if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 996 | for(i=0; i<ArraySize(aPrefix); i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 997 | int n = strlen30(aPrefix[i]); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 998 | if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 999 | char *z = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 1000 | char *zFake = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 1001 | if( zSchema ){ |
| 1002 | char cQuote = quoteChar(zSchema); |
| 1003 | if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ |
| 1004 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 1005 | }else{ |
| 1006 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 1007 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1008 | } |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 1009 | if( zName |
| 1010 | && aPrefix[i][0]=='V' |
| 1011 | && (zFake = shellFakeSchema(db, zSchema, zName))!=0 |
| 1012 | ){ |
| 1013 | if( z==0 ){ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 1014 | z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 1015 | }else{ |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 1016 | z = sqlite3_mprintf("%z\n/* %s */", z, zFake); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 1017 | } |
dan | dcfbff9 | 2018-01-08 17:05:32 +0000 | [diff] [blame] | 1018 | free(zFake); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 1019 | } |
| 1020 | if( z ){ |
| 1021 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 1022 | return; |
| 1023 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | sqlite3_result_value(pCtx, apVal[0]); |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | ** The source code for several run-time loadable extensions is inserted |
| 1032 | ** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 1033 | ** code, we need to override some macros to make the included program code |
| 1034 | ** work here in the middle of this regular program. |
| 1035 | */ |
| 1036 | #define SQLITE_EXTENSION_INIT1 |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1037 | #define SQLITE_EXTENSION_INIT2(X) (void)(X) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1038 | |
mistachkin | acae8c3 | 2018-01-05 20:08:46 +0000 | [diff] [blame] | 1039 | #if defined(_WIN32) && defined(_MSC_VER) |
drh | 03491a1 | 2018-01-07 21:58:17 +0000 | [diff] [blame] | 1040 | INCLUDE test_windirent.h |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 1041 | INCLUDE test_windirent.c |
| 1042 | #define dirent DIRENT |
mistachkin | dfdfd8c | 2018-01-04 22:46:08 +0000 | [diff] [blame] | 1043 | #endif |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 1044 | INCLUDE ../ext/misc/memtrace.c |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 1045 | INCLUDE ../ext/misc/shathree.c |
drh | f05dd03 | 2020-04-14 15:53:58 +0000 | [diff] [blame] | 1046 | INCLUDE ../ext/misc/uint.c |
drh | beb9def | 2020-06-22 19:12:23 +0000 | [diff] [blame] | 1047 | INCLUDE ../ext/misc/decimal.c |
drh | 8cda77d | 2020-06-24 15:06:29 +0000 | [diff] [blame] | 1048 | INCLUDE ../ext/misc/ieee754.c |
mistachkin | 72c38d8 | 2020-08-28 18:47:39 +0000 | [diff] [blame] | 1049 | INCLUDE ../ext/misc/series.c |
drh | 6468990 | 2021-06-03 13:51:31 +0000 | [diff] [blame] | 1050 | INCLUDE ../ext/misc/regexp.c |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 1051 | #ifndef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 1052 | INCLUDE ../ext/misc/fileio.c |
| 1053 | INCLUDE ../ext/misc/completion.c |
| 1054 | INCLUDE ../ext/misc/appendvfs.c |
| 1055 | #endif |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 1056 | #ifdef SQLITE_HAVE_ZLIB |
dan | 9ebfaad | 2017-12-26 20:39:58 +0000 | [diff] [blame] | 1057 | INCLUDE ../ext/misc/zipfile.c |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 1058 | INCLUDE ../ext/misc/sqlar.c |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 1059 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 1060 | INCLUDE ../ext/expert/sqlite3expert.h |
| 1061 | INCLUDE ../ext/expert/sqlite3expert.c |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1062 | |
stephan | bb5136e | 2022-10-27 11:32:20 +0000 | [diff] [blame] | 1063 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 1064 | #define SQLITE_SHELL_HAVE_RECOVER 1 |
| 1065 | #else |
| 1066 | #define SQLITE_SHELL_HAVE_RECOVER 0 |
| 1067 | #endif |
| 1068 | #if SQLITE_SHELL_HAVE_RECOVER |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 1069 | INCLUDE ../ext/recover/sqlite3recover.h |
dan | 63b74e0 | 2022-11-22 16:12:53 +0000 | [diff] [blame] | 1070 | # ifndef SQLITE_HAVE_SQLITE3R |
| 1071 | INCLUDE ../ext/recover/dbdata.c |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 1072 | INCLUDE ../ext/recover/sqlite3recover.c |
dan | 63b74e0 | 2022-11-22 16:12:53 +0000 | [diff] [blame] | 1073 | # endif |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 1074 | #endif |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 1075 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1076 | #if defined(SQLITE_ENABLE_SESSION) |
| 1077 | /* |
| 1078 | ** State information for a single open session |
| 1079 | */ |
| 1080 | typedef struct OpenSession OpenSession; |
| 1081 | struct OpenSession { |
| 1082 | char *zName; /* Symbolic name for this session */ |
| 1083 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1084 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1085 | sqlite3_session *p; /* The open session */ |
| 1086 | }; |
| 1087 | #endif |
| 1088 | |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 1089 | typedef struct ExpertInfo ExpertInfo; |
| 1090 | struct ExpertInfo { |
| 1091 | sqlite3expert *pExpert; |
| 1092 | int bVerbose; |
| 1093 | }; |
| 1094 | |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1095 | /* A single line in the EQP output */ |
| 1096 | typedef struct EQPGraphRow EQPGraphRow; |
| 1097 | struct EQPGraphRow { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1098 | int iEqpId; /* ID for this row */ |
| 1099 | int iParentId; /* ID of the parent row */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1100 | EQPGraphRow *pNext; /* Next row in sequence */ |
| 1101 | char zText[1]; /* Text to display for this row */ |
| 1102 | }; |
| 1103 | |
| 1104 | /* All EQP output is collected into an instance of the following */ |
| 1105 | typedef struct EQPGraph EQPGraph; |
| 1106 | struct EQPGraph { |
| 1107 | EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ |
| 1108 | EQPGraphRow *pLast; /* Last element of the pRow list */ |
| 1109 | char zPrefix[100]; /* Graph prefix */ |
| 1110 | }; |
| 1111 | |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1112 | /* Parameters affecting columnar mode result display (defaulting together) */ |
| 1113 | typedef struct ColModeOpts { |
| 1114 | int iWrap; /* In columnar modes, wrap lines reaching this limit */ |
| 1115 | u8 bQuote; /* Quote results for .mode box and table */ |
| 1116 | u8 bWordWrap; /* In columnar modes, wrap at word boundaries */ |
| 1117 | } ColModeOpts; |
| 1118 | #define ColModeOpts_default { 60, 0, 0 } |
| 1119 | #define ColModeOpts_default_qbox { 60, 1, 0 } |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1120 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1121 | /* |
| 1122 | ** State information about the database connection is contained in an |
| 1123 | ** instance of the following structure. |
| 1124 | */ |
| 1125 | typedef struct ShellState ShellState; |
| 1126 | struct ShellState { |
| 1127 | sqlite3 *db; /* The database */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1128 | u8 autoExplain; /* Automatically turn on .explain mode */ |
| 1129 | u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1130 | u8 autoEQPtest; /* autoEQP is in test mode */ |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 1131 | u8 autoEQPtrace; /* autoEQP is in trace mode */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1132 | u8 scanstatsOn; /* True to display scan stats before each finalize */ |
| 1133 | u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1134 | u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1135 | u8 nEqpLevel; /* Depth of the EQP output graph */ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 1136 | u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1137 | u8 bSafeMode; /* True to prohibit unsafe operations */ |
| 1138 | u8 bSafeModePersist; /* The long-term value of bSafeMode */ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 1139 | ColModeOpts cmOpts; /* Option values affecting columnar mode output */ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 1140 | unsigned statsOn; /* True to display memory stats before each finalize */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1141 | unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 1142 | int inputNesting; /* Track nesting level of .read and other redirects */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1143 | int outCount; /* Revert to stdout when reaching zero */ |
| 1144 | int cnt; /* Number of records displayed so far */ |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 1145 | int lineno; /* Line number of last line read from in */ |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 1146 | int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 1147 | FILE *in; /* Read commands from this stream */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1148 | FILE *out; /* Write results here */ |
| 1149 | FILE *traceOut; /* Output for sqlite3_trace() */ |
| 1150 | int nErr; /* Number of errors seen */ |
| 1151 | int mode; /* An output mode setting */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1152 | int modePrior; /* Saved mode */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1153 | int cMode; /* temporary output mode for the current query */ |
| 1154 | int normalMode; /* Output mode before ".explain on" */ |
| 1155 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
| 1156 | int showHeader; /* True to show column names in List or Column mode */ |
| 1157 | int nCheck; /* Number of ".check" commands run */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1158 | unsigned nProgress; /* Number of progress callbacks encountered */ |
| 1159 | unsigned mxProgress; /* Maximum progress callbacks before failing */ |
| 1160 | unsigned flgProgress; /* Flags for the progress callback */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1161 | unsigned shellFlgs; /* Various flags */ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1162 | unsigned priorShFlgs; /* Saved copy of flags */ |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 1163 | sqlite3_int64 szMax; /* --maxsize argument to .open */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1164 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 1165 | char *zTempFile; /* Temporary file that might need deleting */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1166 | char zTestcase[30]; /* Name of current test case */ |
| 1167 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1168 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1169 | char colSepPrior[20]; /* Saved column separator */ |
| 1170 | char rowSepPrior[20]; /* Saved row separator */ |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 1171 | int *colWidth; /* Requested width of each column in columnar modes */ |
| 1172 | int *actualWidth; /* Actual width of each column */ |
| 1173 | int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1174 | char nullValue[20]; /* The text to print when a NULL comes back from |
| 1175 | ** the database */ |
| 1176 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1177 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
| 1178 | FILE *pLog; /* Write log output here */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 1179 | struct AuxDb { /* Storage space for auxiliary database connections */ |
| 1180 | sqlite3 *db; /* Connection pointer */ |
| 1181 | const char *zDbFilename; /* Filename used to open the connection */ |
| 1182 | char *zFreeOnClose; /* Free this memory allocation on close */ |
| 1183 | #if defined(SQLITE_ENABLE_SESSION) |
| 1184 | int nSession; /* Number of active sessions */ |
| 1185 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1186 | #endif |
| 1187 | } aAuxDb[5], /* Array of all database connections */ |
| 1188 | *pAuxDb; /* Currently active database connection */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1189 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1190 | int nIndent; /* Size of array aiIndent[] */ |
| 1191 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1192 | char *zNonce; /* Nonce for temporary safe-mode excapes */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1193 | EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1194 | ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 1195 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 1196 | struct { |
| 1197 | const char * zInput; /* Input string from wasm/JS proxy */ |
stephan | 0076e49 | 2022-05-18 18:10:17 +0000 | [diff] [blame] | 1198 | const char * zPos; /* Cursor pos into zInput */ |
stephan | 60d9aa7 | 2022-09-24 07:36:45 +0000 | [diff] [blame] | 1199 | const char * zDefaultDbName; /* Default name for db file */ |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 1200 | } wasm; |
| 1201 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1202 | }; |
| 1203 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 1204 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 1205 | static ShellState shellState; |
| 1206 | #endif |
| 1207 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1208 | |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 1209 | /* Allowed values for ShellState.autoEQP |
| 1210 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1211 | #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ |
| 1212 | #define AUTOEQP_on 1 /* Automatic EQP is on */ |
| 1213 | #define AUTOEQP_trigger 2 /* On and also show plans for triggers */ |
| 1214 | #define AUTOEQP_full 3 /* Show full EXPLAIN */ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 1215 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1216 | /* Allowed values for ShellState.openMode |
| 1217 | */ |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 1218 | #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ |
| 1219 | #define SHELL_OPEN_NORMAL 1 /* Normal database file */ |
| 1220 | #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ |
| 1221 | #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ |
| 1222 | #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ |
| 1223 | #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 1224 | #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 1225 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 1226 | /* Allowed values for ShellState.eTraceType |
| 1227 | */ |
| 1228 | #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ |
| 1229 | #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ |
| 1230 | #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ |
| 1231 | |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1232 | /* Bits in the ShellState.flgProgress variable */ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 1233 | #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ |
| 1234 | #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres |
| 1235 | ** callback limit is reached, and for each |
| 1236 | ** top-level SQL statement */ |
| 1237 | #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 1238 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1239 | /* |
| 1240 | ** These are the allowed shellFlgs values |
| 1241 | */ |
drh | b2a0f75 | 2017-08-28 15:51:35 +0000 | [diff] [blame] | 1242 | #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ |
| 1243 | #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ |
| 1244 | #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ |
| 1245 | #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ |
| 1246 | #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ |
| 1247 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 1248 | #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */ |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 1249 | #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */ |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 1250 | #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ |
| 1251 | #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* |
| 1254 | ** Macros for testing and setting shellFlgs |
| 1255 | */ |
| 1256 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1257 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1258 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
| 1259 | |
| 1260 | /* |
| 1261 | ** These are the allowed modes. |
| 1262 | */ |
| 1263 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
| 1264 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1265 | #define MODE_List 2 /* One record per line with a separator */ |
| 1266 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1267 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1268 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
| 1269 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1270 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1271 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1272 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1273 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1274 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 1275 | #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 1276 | #define MODE_Json 13 /* Output JSON */ |
| 1277 | #define MODE_Markdown 14 /* Markdown formatting */ |
| 1278 | #define MODE_Table 15 /* MySQL-style table formatting */ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 1279 | #define MODE_Box 16 /* Unicode box-drawing characters */ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 1280 | #define MODE_Count 17 /* Output only a count of the rows of output */ |
| 1281 | #define MODE_Off 18 /* No query output shown */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1282 | |
| 1283 | static const char *modeDescr[] = { |
| 1284 | "line", |
| 1285 | "column", |
| 1286 | "list", |
| 1287 | "semi", |
| 1288 | "html", |
| 1289 | "insert", |
| 1290 | "quote", |
| 1291 | "tcl", |
| 1292 | "csv", |
| 1293 | "explain", |
| 1294 | "ascii", |
| 1295 | "prettyprint", |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 1296 | "eqp", |
| 1297 | "json", |
| 1298 | "markdown", |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 1299 | "table", |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 1300 | "box", |
| 1301 | "count", |
| 1302 | "off" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1303 | }; |
| 1304 | |
| 1305 | /* |
| 1306 | ** These are the column/row/line separators used by the various |
| 1307 | ** import/export modes. |
| 1308 | */ |
| 1309 | #define SEP_Column "|" |
| 1310 | #define SEP_Row "\n" |
| 1311 | #define SEP_Tab "\t" |
| 1312 | #define SEP_Space " " |
| 1313 | #define SEP_Comma "," |
| 1314 | #define SEP_CrLf "\r\n" |
| 1315 | #define SEP_Unit "\x1F" |
| 1316 | #define SEP_Record "\x1E" |
| 1317 | |
| 1318 | /* |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 1319 | ** Limit input nesting via .read or any other input redirect. |
| 1320 | ** It's not too expensive, so a generous allowance can be made. |
| 1321 | */ |
| 1322 | #define MAX_INPUT_NESTING 25 |
| 1323 | |
| 1324 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1325 | ** A callback for the sqlite3_log() interface. |
| 1326 | */ |
| 1327 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
| 1328 | ShellState *p = (ShellState*)pArg; |
| 1329 | if( p->pLog==0 ) return; |
| 1330 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
| 1331 | fflush(p->pLog); |
| 1332 | } |
| 1333 | |
| 1334 | /* |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1335 | ** SQL function: shell_putsnl(X) |
| 1336 | ** |
| 1337 | ** Write the text X to the screen (or whatever output is being directed) |
| 1338 | ** adding a newline at the end, and then return X. |
| 1339 | */ |
| 1340 | static void shellPutsFunc( |
| 1341 | sqlite3_context *pCtx, |
| 1342 | int nVal, |
| 1343 | sqlite3_value **apVal |
| 1344 | ){ |
| 1345 | ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
drh | b968518 | 2018-01-17 13:15:23 +0000 | [diff] [blame] | 1346 | (void)nVal; |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 1347 | utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
| 1348 | sqlite3_result_value(pCtx, apVal[0]); |
| 1349 | } |
| 1350 | |
| 1351 | /* |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1352 | ** If in safe mode, print an error message described by the arguments |
| 1353 | ** and exit immediately. |
| 1354 | */ |
| 1355 | static void failIfSafeMode( |
| 1356 | ShellState *p, |
| 1357 | const char *zErrMsg, |
| 1358 | ... |
| 1359 | ){ |
| 1360 | if( p->bSafeMode ){ |
| 1361 | va_list ap; |
| 1362 | char *zMsg; |
| 1363 | va_start(ap, zErrMsg); |
| 1364 | zMsg = sqlite3_vmprintf(zErrMsg, ap); |
| 1365 | va_end(ap); |
| 1366 | raw_printf(stderr, "line %d: ", p->lineno); |
| 1367 | utf8_printf(stderr, "%s\n", zMsg); |
| 1368 | exit(1); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1373 | ** SQL function: edit(VALUE) |
| 1374 | ** edit(VALUE,EDITOR) |
| 1375 | ** |
| 1376 | ** These steps: |
| 1377 | ** |
| 1378 | ** (1) Write VALUE into a temporary file. |
| 1379 | ** (2) Run program EDITOR on that temporary file. |
| 1380 | ** (3) Read the temporary file back and return its content as the result. |
| 1381 | ** (4) Delete the temporary file |
| 1382 | ** |
| 1383 | ** If the EDITOR argument is omitted, use the value in the VISUAL |
| 1384 | ** environment variable. If still there is no EDITOR, through an error. |
| 1385 | ** |
| 1386 | ** Also throw an error if the EDITOR program returns a non-zero exit code. |
| 1387 | */ |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1388 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1389 | static void editFunc( |
| 1390 | sqlite3_context *context, |
| 1391 | int argc, |
| 1392 | sqlite3_value **argv |
| 1393 | ){ |
| 1394 | const char *zEditor; |
| 1395 | char *zTempFile = 0; |
| 1396 | sqlite3 *db; |
| 1397 | char *zCmd = 0; |
| 1398 | int bBin; |
| 1399 | int rc; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1400 | int hasCRNL = 0; |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1401 | FILE *f = 0; |
| 1402 | sqlite3_int64 sz; |
| 1403 | sqlite3_int64 x; |
| 1404 | unsigned char *p = 0; |
| 1405 | |
| 1406 | if( argc==2 ){ |
| 1407 | zEditor = (const char*)sqlite3_value_text(argv[1]); |
| 1408 | }else{ |
| 1409 | zEditor = getenv("VISUAL"); |
| 1410 | } |
| 1411 | if( zEditor==0 ){ |
| 1412 | sqlite3_result_error(context, "no editor for edit()", -1); |
| 1413 | return; |
| 1414 | } |
| 1415 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1416 | sqlite3_result_error(context, "NULL input to edit()", -1); |
| 1417 | return; |
| 1418 | } |
| 1419 | db = sqlite3_context_db_handle(context); |
| 1420 | zTempFile = 0; |
| 1421 | sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); |
| 1422 | if( zTempFile==0 ){ |
| 1423 | sqlite3_uint64 r = 0; |
| 1424 | sqlite3_randomness(sizeof(r), &r); |
| 1425 | zTempFile = sqlite3_mprintf("temp%llx", r); |
| 1426 | if( zTempFile==0 ){ |
| 1427 | sqlite3_result_error_nomem(context); |
| 1428 | return; |
| 1429 | } |
| 1430 | } |
| 1431 | bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1432 | /* When writing the file to be edited, do \n to \r\n conversions on systems |
| 1433 | ** that want \r\n line endings */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1434 | f = fopen(zTempFile, bBin ? "wb" : "w"); |
| 1435 | if( f==0 ){ |
| 1436 | sqlite3_result_error(context, "edit() cannot open temp file", -1); |
| 1437 | goto edit_func_end; |
| 1438 | } |
| 1439 | sz = sqlite3_value_bytes(argv[0]); |
| 1440 | if( bBin ){ |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1441 | x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1442 | }else{ |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1443 | const char *z = (const char*)sqlite3_value_text(argv[0]); |
| 1444 | /* Remember whether or not the value originally contained \r\n */ |
| 1445 | if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1446 | x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1447 | } |
| 1448 | fclose(f); |
| 1449 | f = 0; |
| 1450 | if( x!=sz ){ |
| 1451 | sqlite3_result_error(context, "edit() could not write the whole file", -1); |
| 1452 | goto edit_func_end; |
| 1453 | } |
| 1454 | zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); |
| 1455 | if( zCmd==0 ){ |
| 1456 | sqlite3_result_error_nomem(context); |
| 1457 | goto edit_func_end; |
| 1458 | } |
| 1459 | rc = system(zCmd); |
| 1460 | sqlite3_free(zCmd); |
| 1461 | if( rc ){ |
| 1462 | sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
| 1463 | goto edit_func_end; |
| 1464 | } |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1465 | f = fopen(zTempFile, "rb"); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1466 | if( f==0 ){ |
| 1467 | sqlite3_result_error(context, |
| 1468 | "edit() cannot reopen temp file after edit", -1); |
| 1469 | goto edit_func_end; |
| 1470 | } |
| 1471 | fseek(f, 0, SEEK_END); |
| 1472 | sz = ftell(f); |
| 1473 | rewind(f); |
drh | ee37f8b | 2019-08-23 23:05:32 +0000 | [diff] [blame] | 1474 | p = sqlite3_malloc64( sz+1 ); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1475 | if( p==0 ){ |
| 1476 | sqlite3_result_error_nomem(context); |
| 1477 | goto edit_func_end; |
| 1478 | } |
dan | 4d02b5f | 2019-07-17 07:23:06 +0000 | [diff] [blame] | 1479 | x = fread(p, 1, (size_t)sz, f); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1480 | fclose(f); |
| 1481 | f = 0; |
| 1482 | if( x!=sz ){ |
| 1483 | sqlite3_result_error(context, "could not read back the whole file", -1); |
| 1484 | goto edit_func_end; |
| 1485 | } |
| 1486 | if( bBin ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1487 | sqlite3_result_blob64(context, p, sz, sqlite3_free); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1488 | }else{ |
dan | 60bdcf5 | 2018-10-03 11:13:30 +0000 | [diff] [blame] | 1489 | sqlite3_int64 i, j; |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1490 | if( hasCRNL ){ |
| 1491 | /* If the original contains \r\n then do no conversions back to \n */ |
drh | f018fd5 | 2018-08-06 02:08:53 +0000 | [diff] [blame] | 1492 | }else{ |
| 1493 | /* If the file did not originally contain \r\n then convert any new |
| 1494 | ** \r\n back into \n */ |
| 1495 | for(i=j=0; i<sz; i++){ |
| 1496 | if( p[i]=='\r' && p[i+1]=='\n' ) i++; |
| 1497 | p[j++] = p[i]; |
| 1498 | } |
| 1499 | sz = j; |
| 1500 | p[sz] = 0; |
| 1501 | } |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 1502 | sqlite3_result_text64(context, (const char*)p, sz, |
| 1503 | sqlite3_free, SQLITE_UTF8); |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1504 | } |
| 1505 | p = 0; |
| 1506 | |
| 1507 | edit_func_end: |
| 1508 | if( f ) fclose(f); |
| 1509 | unlink(zTempFile); |
| 1510 | sqlite3_free(zTempFile); |
| 1511 | sqlite3_free(p); |
| 1512 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 1513 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1516 | ** Save or restore the current output mode |
| 1517 | */ |
| 1518 | static void outputModePush(ShellState *p){ |
| 1519 | p->modePrior = p->mode; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1520 | p->priorShFlgs = p->shellFlgs; |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1521 | memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); |
| 1522 | memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); |
| 1523 | } |
| 1524 | static void outputModePop(ShellState *p){ |
| 1525 | p->mode = p->modePrior; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 1526 | p->shellFlgs = p->priorShFlgs; |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 1527 | memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
| 1528 | memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
| 1529 | } |
| 1530 | |
| 1531 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1532 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1533 | */ |
| 1534 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1535 | int i; |
dan | 2ce2b77 | 2022-08-24 11:51:31 +0000 | [diff] [blame] | 1536 | unsigned char *aBlob = (unsigned char*)pBlob; |
| 1537 | |
| 1538 | char *zStr = sqlite3_malloc(nBlob*2 + 1); |
| 1539 | shell_check_oom(zStr); |
| 1540 | |
| 1541 | for(i=0; i<nBlob; i++){ |
| 1542 | static const char aHex[] = { |
| 1543 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 1544 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 1545 | }; |
| 1546 | zStr[i*2] = aHex[ (aBlob[i] >> 4) ]; |
| 1547 | zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; |
| 1548 | } |
| 1549 | zStr[i*2] = '\0'; |
| 1550 | |
| 1551 | raw_printf(out,"X'%s'", zStr); |
| 1552 | sqlite3_free(zStr); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1557 | ** to that string. |
| 1558 | ** |
| 1559 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1560 | ** then make up some string and store it in the buffer zBuf. |
| 1561 | */ |
| 1562 | static const char *unused_string( |
| 1563 | const char *z, /* Result must not appear anywhere in z */ |
| 1564 | const char *zA, const char *zB, /* Try these first */ |
| 1565 | char *zBuf /* Space to store a generated string */ |
| 1566 | ){ |
| 1567 | unsigned i = 0; |
| 1568 | if( strstr(z, zA)==0 ) return zA; |
| 1569 | if( strstr(z, zB)==0 ) return zB; |
| 1570 | do{ |
| 1571 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1572 | }while( strstr(z,zBuf)!=0 ); |
| 1573 | return zBuf; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1578 | ** |
| 1579 | ** See also: output_quoted_escaped_string() |
| 1580 | */ |
| 1581 | static void output_quoted_string(FILE *out, const char *z){ |
| 1582 | int i; |
| 1583 | char c; |
| 1584 | setBinaryMode(out, 1); |
| 1585 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1586 | if( c==0 ){ |
| 1587 | utf8_printf(out,"'%s'",z); |
| 1588 | }else{ |
| 1589 | raw_printf(out, "'"); |
| 1590 | while( *z ){ |
| 1591 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1592 | if( c=='\'' ) i++; |
| 1593 | if( i ){ |
| 1594 | utf8_printf(out, "%.*s", i, z); |
| 1595 | z += i; |
| 1596 | } |
| 1597 | if( c=='\'' ){ |
| 1598 | raw_printf(out, "'"); |
| 1599 | continue; |
| 1600 | } |
| 1601 | if( c==0 ){ |
| 1602 | break; |
| 1603 | } |
| 1604 | z++; |
| 1605 | } |
| 1606 | raw_printf(out, "'"); |
| 1607 | } |
| 1608 | setTextMode(out, 1); |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1613 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1614 | ** get corrupted by end-of-line translation facilities in some operating |
| 1615 | ** systems. |
| 1616 | ** |
| 1617 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1618 | ** escape mechanism. |
| 1619 | */ |
| 1620 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1621 | int i; |
| 1622 | char c; |
| 1623 | setBinaryMode(out, 1); |
| 1624 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1625 | if( c==0 ){ |
| 1626 | utf8_printf(out,"'%s'",z); |
| 1627 | }else{ |
| 1628 | const char *zNL = 0; |
| 1629 | const char *zCR = 0; |
| 1630 | int nNL = 0; |
| 1631 | int nCR = 0; |
| 1632 | char zBuf1[20], zBuf2[20]; |
| 1633 | for(i=0; z[i]; i++){ |
| 1634 | if( z[i]=='\n' ) nNL++; |
| 1635 | if( z[i]=='\r' ) nCR++; |
| 1636 | } |
| 1637 | if( nNL ){ |
| 1638 | raw_printf(out, "replace("); |
| 1639 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1640 | } |
| 1641 | if( nCR ){ |
| 1642 | raw_printf(out, "replace("); |
| 1643 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1644 | } |
| 1645 | raw_printf(out, "'"); |
| 1646 | while( *z ){ |
| 1647 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1648 | if( c=='\'' ) i++; |
| 1649 | if( i ){ |
| 1650 | utf8_printf(out, "%.*s", i, z); |
| 1651 | z += i; |
| 1652 | } |
| 1653 | if( c=='\'' ){ |
| 1654 | raw_printf(out, "'"); |
| 1655 | continue; |
| 1656 | } |
| 1657 | if( c==0 ){ |
| 1658 | break; |
| 1659 | } |
| 1660 | z++; |
| 1661 | if( c=='\n' ){ |
| 1662 | raw_printf(out, "%s", zNL); |
| 1663 | continue; |
| 1664 | } |
| 1665 | raw_printf(out, "%s", zCR); |
| 1666 | } |
| 1667 | raw_printf(out, "'"); |
| 1668 | if( nCR ){ |
| 1669 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1670 | } |
| 1671 | if( nNL ){ |
| 1672 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1673 | } |
| 1674 | } |
| 1675 | setTextMode(out, 1); |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1680 | */ |
| 1681 | static void output_c_string(FILE *out, const char *z){ |
| 1682 | unsigned int c; |
| 1683 | fputc('"', out); |
| 1684 | while( (c = *(z++))!=0 ){ |
| 1685 | if( c=='\\' ){ |
| 1686 | fputc(c, out); |
| 1687 | fputc(c, out); |
| 1688 | }else if( c=='"' ){ |
| 1689 | fputc('\\', out); |
| 1690 | fputc('"', out); |
| 1691 | }else if( c=='\t' ){ |
| 1692 | fputc('\\', out); |
| 1693 | fputc('t', out); |
| 1694 | }else if( c=='\n' ){ |
| 1695 | fputc('\\', out); |
| 1696 | fputc('n', out); |
| 1697 | }else if( c=='\r' ){ |
| 1698 | fputc('\\', out); |
| 1699 | fputc('r', out); |
| 1700 | }else if( !isprint(c&0xff) ){ |
| 1701 | raw_printf(out, "\\%03o", c&0xff); |
| 1702 | }else{ |
| 1703 | fputc(c, out); |
| 1704 | } |
| 1705 | } |
| 1706 | fputc('"', out); |
| 1707 | } |
| 1708 | |
| 1709 | /* |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 1710 | ** Output the given string as a quoted according to JSON quoting rules. |
| 1711 | */ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 1712 | static void output_json_string(FILE *out, const char *z, i64 n){ |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 1713 | unsigned int c; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 1714 | if( n<0 ) n = strlen(z); |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 1715 | fputc('"', out); |
| 1716 | while( n-- ){ |
| 1717 | c = *(z++); |
| 1718 | if( c=='\\' || c=='"' ){ |
| 1719 | fputc('\\', out); |
| 1720 | fputc(c, out); |
| 1721 | }else if( c<=0x1f ){ |
| 1722 | fputc('\\', out); |
| 1723 | if( c=='\b' ){ |
| 1724 | fputc('b', out); |
| 1725 | }else if( c=='\f' ){ |
| 1726 | fputc('f', out); |
| 1727 | }else if( c=='\n' ){ |
| 1728 | fputc('n', out); |
| 1729 | }else if( c=='\r' ){ |
| 1730 | fputc('r', out); |
| 1731 | }else if( c=='\t' ){ |
| 1732 | fputc('t', out); |
| 1733 | }else{ |
| 1734 | raw_printf(out, "u%04x",c); |
| 1735 | } |
| 1736 | }else{ |
| 1737 | fputc(c, out); |
| 1738 | } |
| 1739 | } |
| 1740 | fputc('"', out); |
| 1741 | } |
| 1742 | |
| 1743 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1744 | ** Output the given string with characters that are special to |
| 1745 | ** HTML escaped. |
| 1746 | */ |
| 1747 | static void output_html_string(FILE *out, const char *z){ |
| 1748 | int i; |
| 1749 | if( z==0 ) z = ""; |
| 1750 | while( *z ){ |
| 1751 | for(i=0; z[i] |
| 1752 | && z[i]!='<' |
| 1753 | && z[i]!='&' |
| 1754 | && z[i]!='>' |
| 1755 | && z[i]!='\"' |
| 1756 | && z[i]!='\''; |
| 1757 | i++){} |
| 1758 | if( i>0 ){ |
| 1759 | utf8_printf(out,"%.*s",i,z); |
| 1760 | } |
| 1761 | if( z[i]=='<' ){ |
| 1762 | raw_printf(out,"<"); |
| 1763 | }else if( z[i]=='&' ){ |
| 1764 | raw_printf(out,"&"); |
| 1765 | }else if( z[i]=='>' ){ |
| 1766 | raw_printf(out,">"); |
| 1767 | }else if( z[i]=='\"' ){ |
| 1768 | raw_printf(out,"""); |
| 1769 | }else if( z[i]=='\'' ){ |
| 1770 | raw_printf(out,"'"); |
| 1771 | }else{ |
| 1772 | break; |
| 1773 | } |
| 1774 | z += i + 1; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | ** If a field contains any character identified by a 1 in the following |
| 1780 | ** array, then the string must be quoted for CSV. |
| 1781 | */ |
| 1782 | static const char needCsvQuote[] = { |
| 1783 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1784 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1785 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1786 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1787 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1788 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1789 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1790 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1791 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1792 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1793 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1794 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1795 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1796 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1797 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1798 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1799 | }; |
| 1800 | |
| 1801 | /* |
| 1802 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
| 1803 | ** the separator, which may or may not be a comma. p->nullValue is |
| 1804 | ** the null value. Strings are quoted if necessary. The separator |
| 1805 | ** is only issued if bSep is true. |
| 1806 | */ |
| 1807 | static void output_csv(ShellState *p, const char *z, int bSep){ |
| 1808 | FILE *out = p->out; |
| 1809 | if( z==0 ){ |
| 1810 | utf8_printf(out,"%s",p->nullValue); |
| 1811 | }else{ |
drh | 9cd0c3d | 2021-11-18 15:40:05 +0000 | [diff] [blame] | 1812 | unsigned i; |
| 1813 | for(i=0; z[i]; i++){ |
| 1814 | if( needCsvQuote[((unsigned char*)z)[i]] ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1815 | i = 0; |
| 1816 | break; |
| 1817 | } |
| 1818 | } |
drh | 9cd0c3d | 2021-11-18 15:40:05 +0000 | [diff] [blame] | 1819 | if( i==0 || strstr(z, p->colSeparator)!=0 ){ |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1820 | char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 1821 | shell_check_oom(zQuoted); |
drh | 9b7affc | 2017-11-26 02:14:18 +0000 | [diff] [blame] | 1822 | utf8_printf(out, "%s", zQuoted); |
| 1823 | sqlite3_free(zQuoted); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1824 | }else{ |
| 1825 | utf8_printf(out, "%s", z); |
| 1826 | } |
| 1827 | } |
| 1828 | if( bSep ){ |
| 1829 | utf8_printf(p->out, "%s", p->colSeparator); |
| 1830 | } |
| 1831 | } |
| 1832 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1833 | /* |
| 1834 | ** This routine runs when the user presses Ctrl-C |
| 1835 | */ |
| 1836 | static void interrupt_handler(int NotUsed){ |
| 1837 | UNUSED_PARAMETER(NotUsed); |
| 1838 | seenInterrupt++; |
| 1839 | if( seenInterrupt>2 ) exit(1); |
| 1840 | if( globalDb ) sqlite3_interrupt(globalDb); |
| 1841 | } |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 1842 | |
| 1843 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 1844 | /* |
| 1845 | ** This routine runs for console events (e.g. Ctrl-C) on Win32 |
| 1846 | */ |
| 1847 | static BOOL WINAPI ConsoleCtrlHandler( |
| 1848 | DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ |
| 1849 | ){ |
| 1850 | if( dwCtrlType==CTRL_C_EVENT ){ |
| 1851 | interrupt_handler(0); |
| 1852 | return TRUE; |
| 1853 | } |
| 1854 | return FALSE; |
| 1855 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1856 | #endif |
| 1857 | |
| 1858 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1859 | /* |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1860 | ** This authorizer runs in safe mode. |
| 1861 | */ |
| 1862 | static int safeModeAuth( |
| 1863 | void *pClientData, |
| 1864 | int op, |
| 1865 | const char *zA1, |
| 1866 | const char *zA2, |
| 1867 | const char *zA3, |
| 1868 | const char *zA4 |
| 1869 | ){ |
| 1870 | ShellState *p = (ShellState*)pClientData; |
| 1871 | static const char *azProhibitedFunctions[] = { |
| 1872 | "edit", |
| 1873 | "fts3_tokenizer", |
| 1874 | "load_extension", |
| 1875 | "readfile", |
| 1876 | "writefile", |
| 1877 | "zipfile", |
| 1878 | "zipfile_cds", |
| 1879 | }; |
| 1880 | UNUSED_PARAMETER(zA2); |
| 1881 | UNUSED_PARAMETER(zA3); |
| 1882 | UNUSED_PARAMETER(zA4); |
| 1883 | switch( op ){ |
| 1884 | case SQLITE_ATTACH: { |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 1885 | #ifndef SQLITE_SHELL_FIDDLE |
stephan | 626aa48 | 2022-06-06 04:09:44 +0000 | [diff] [blame] | 1886 | /* In WASM builds the filesystem is a virtual sandbox, so |
| 1887 | ** there's no harm in using ATTACH. */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1888 | failIfSafeMode(p, "cannot run ATTACH in safe mode"); |
stephan | 626aa48 | 2022-06-06 04:09:44 +0000 | [diff] [blame] | 1889 | #endif |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1890 | break; |
| 1891 | } |
| 1892 | case SQLITE_FUNCTION: { |
| 1893 | int i; |
| 1894 | for(i=0; i<ArraySize(azProhibitedFunctions); i++){ |
| 1895 | if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){ |
| 1896 | failIfSafeMode(p, "cannot use the %s() function in safe mode", |
| 1897 | azProhibitedFunctions[i]); |
| 1898 | } |
| 1899 | } |
| 1900 | break; |
| 1901 | } |
| 1902 | } |
| 1903 | return SQLITE_OK; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1907 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1908 | ** invoked. It always returns SQLITE_OK. |
| 1909 | */ |
| 1910 | static int shellAuth( |
| 1911 | void *pClientData, |
| 1912 | int op, |
| 1913 | const char *zA1, |
| 1914 | const char *zA2, |
| 1915 | const char *zA3, |
| 1916 | const char *zA4 |
| 1917 | ){ |
| 1918 | ShellState *p = (ShellState*)pClientData; |
| 1919 | static const char *azAction[] = { 0, |
| 1920 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1921 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1922 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1923 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1924 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1925 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1926 | "PRAGMA", "READ", "SELECT", |
| 1927 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1928 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1929 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1930 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1931 | }; |
| 1932 | int i; |
| 1933 | const char *az[4]; |
| 1934 | az[0] = zA1; |
| 1935 | az[1] = zA2; |
| 1936 | az[2] = zA3; |
| 1937 | az[3] = zA4; |
| 1938 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
| 1939 | for(i=0; i<4; i++){ |
| 1940 | raw_printf(p->out, " "); |
| 1941 | if( az[i] ){ |
| 1942 | output_c_string(p->out, az[i]); |
| 1943 | }else{ |
| 1944 | raw_printf(p->out, "NULL"); |
| 1945 | } |
| 1946 | } |
| 1947 | raw_printf(p->out, "\n"); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 1948 | if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1949 | return SQLITE_OK; |
| 1950 | } |
| 1951 | #endif |
| 1952 | |
| 1953 | /* |
| 1954 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1955 | ** |
| 1956 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1957 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
drh | 78ed74e | 2022-08-17 20:18:34 +0000 | [diff] [blame] | 1958 | ** |
| 1959 | ** If the schema statement in z[] contains a start-of-comment and if |
| 1960 | ** sqlite3_complete() returns false, try to terminate the comment before |
| 1961 | ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1962 | */ |
| 1963 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
drh | 78ed74e | 2022-08-17 20:18:34 +0000 | [diff] [blame] | 1964 | char *zToFree = 0; |
drh | 0a0536a | 2019-05-09 18:13:30 +0000 | [diff] [blame] | 1965 | if( z==0 ) return; |
| 1966 | if( zTail==0 ) return; |
drh | 78ed74e | 2022-08-17 20:18:34 +0000 | [diff] [blame] | 1967 | if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ |
| 1968 | const char *zOrig = z; |
| 1969 | static const char *azTerm[] = { "", "*/", "\n" }; |
| 1970 | int i; |
| 1971 | for(i=0; i<ArraySize(azTerm); i++){ |
| 1972 | char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]); |
| 1973 | if( sqlite3_complete(zNew) ){ |
| 1974 | size_t n = strlen(zNew); |
| 1975 | zNew[n-1] = 0; |
| 1976 | zToFree = zNew; |
| 1977 | z = zNew; |
| 1978 | break; |
| 1979 | } |
| 1980 | sqlite3_free(zNew); |
| 1981 | } |
| 1982 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1983 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1984 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1985 | }else{ |
| 1986 | utf8_printf(out, "%s%s", z, zTail); |
| 1987 | } |
drh | 78ed74e | 2022-08-17 20:18:34 +0000 | [diff] [blame] | 1988 | sqlite3_free(zToFree); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1989 | } |
| 1990 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1991 | char c = z[n]; |
| 1992 | z[n] = 0; |
| 1993 | printSchemaLine(out, z, zTail); |
| 1994 | z[n] = c; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 1998 | ** Return true if string z[] has nothing but whitespace and comments to the |
| 1999 | ** end of the first line. |
| 2000 | */ |
| 2001 | static int wsToEol(const char *z){ |
| 2002 | int i; |
| 2003 | for(i=0; z[i]; i++){ |
| 2004 | if( z[i]=='\n' ) return 1; |
| 2005 | if( IsSpace(z[i]) ) continue; |
| 2006 | if( z[i]=='-' && z[i+1]=='-' ) return 1; |
| 2007 | return 0; |
| 2008 | } |
| 2009 | return 1; |
| 2010 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2011 | |
| 2012 | /* |
| 2013 | ** Add a new entry to the EXPLAIN QUERY PLAN data |
| 2014 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2015 | static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2016 | EQPGraphRow *pNew; |
drh | 8be8924 | 2022-10-17 16:09:33 +0000 | [diff] [blame] | 2017 | i64 nText; |
| 2018 | if( zText==0 ) return; |
| 2019 | nText = strlen(zText); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2020 | if( p->autoEQPtest ){ |
| 2021 | utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); |
| 2022 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2023 | pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2024 | shell_check_oom(pNew); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2025 | pNew->iEqpId = iEqpId; |
| 2026 | pNew->iParentId = p2; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2027 | memcpy(pNew->zText, zText, nText+1); |
| 2028 | pNew->pNext = 0; |
| 2029 | if( p->sGraph.pLast ){ |
| 2030 | p->sGraph.pLast->pNext = pNew; |
| 2031 | }else{ |
| 2032 | p->sGraph.pRow = pNew; |
| 2033 | } |
| 2034 | p->sGraph.pLast = pNew; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
| 2038 | ** Free and reset the EXPLAIN QUERY PLAN data that has been collected |
| 2039 | ** in p->sGraph. |
| 2040 | */ |
| 2041 | static void eqp_reset(ShellState *p){ |
| 2042 | EQPGraphRow *pRow, *pNext; |
| 2043 | for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ |
| 2044 | pNext = pRow->pNext; |
| 2045 | sqlite3_free(pRow); |
| 2046 | } |
| 2047 | memset(&p->sGraph, 0, sizeof(p->sGraph)); |
| 2048 | } |
| 2049 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2050 | /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2051 | ** pOld, or return the first such line if pOld is NULL |
| 2052 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2053 | static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2054 | EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2055 | while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2056 | return pRow; |
| 2057 | } |
| 2058 | |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2059 | /* 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] | 2060 | ** recursively to render sublevels. |
| 2061 | */ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2062 | static void eqp_render_level(ShellState *p, int iEqpId){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2063 | EQPGraphRow *pRow, *pNext; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 2064 | i64 n = strlen(p->sGraph.zPrefix); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2065 | char *z; |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2066 | for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ |
| 2067 | pNext = eqp_next_row(p, iEqpId, pRow); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2068 | z = pRow->zText; |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 2069 | utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, |
| 2070 | pNext ? "|--" : "`--", z); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 2071 | if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2072 | memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2073 | eqp_render_level(p, pRow->iEqpId); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2074 | p->sGraph.zPrefix[n] = 0; |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | ** Display and reset the EXPLAIN QUERY PLAN data |
| 2081 | */ |
| 2082 | static void eqp_render(ShellState *p){ |
| 2083 | EQPGraphRow *pRow = p->sGraph.pRow; |
| 2084 | if( pRow ){ |
| 2085 | if( pRow->zText[0]=='-' ){ |
| 2086 | if( pRow->pNext==0 ){ |
| 2087 | eqp_reset(p); |
| 2088 | return; |
| 2089 | } |
| 2090 | utf8_printf(p->out, "%s\n", pRow->zText+3); |
| 2091 | p->sGraph.pRow = pRow->pNext; |
| 2092 | sqlite3_free(pRow); |
| 2093 | }else{ |
| 2094 | utf8_printf(p->out, "QUERY PLAN\n"); |
| 2095 | } |
| 2096 | p->sGraph.zPrefix[0] = 0; |
| 2097 | eqp_render_level(p, 0); |
| 2098 | eqp_reset(p); |
| 2099 | } |
| 2100 | } |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2101 | |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 2102 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2103 | /* |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2104 | ** Progress handler callback. |
| 2105 | */ |
| 2106 | static int progress_handler(void *pClientData) { |
| 2107 | ShellState *p = (ShellState*)pClientData; |
| 2108 | p->nProgress++; |
| 2109 | if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ |
| 2110 | raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 2111 | if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
| 2112 | if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2113 | return 1; |
| 2114 | } |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 2115 | if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2116 | raw_printf(p->out, "Progress %u\n", p->nProgress); |
| 2117 | } |
| 2118 | return 0; |
| 2119 | } |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 2120 | #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2123 | ** Print N dashes |
| 2124 | */ |
| 2125 | static void print_dashes(FILE *out, int N){ |
| 2126 | const char zDash[] = "--------------------------------------------------"; |
| 2127 | const int nDash = sizeof(zDash) - 1; |
| 2128 | while( N>nDash ){ |
| 2129 | fputs(zDash, out); |
| 2130 | N -= nDash; |
| 2131 | } |
| 2132 | raw_printf(out, "%.*s", N, zDash); |
| 2133 | } |
| 2134 | |
| 2135 | /* |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2136 | ** Print a markdown or table-style row separator using ascii-art |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2137 | */ |
| 2138 | static void print_row_separator( |
| 2139 | ShellState *p, |
| 2140 | int nArg, |
| 2141 | const char *zSep |
| 2142 | ){ |
| 2143 | int i; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2144 | if( nArg>0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2145 | fputs(zSep, p->out); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 2146 | print_dashes(p->out, p->actualWidth[0]+2); |
| 2147 | for(i=1; i<nArg; i++){ |
| 2148 | fputs(zSep, p->out); |
| 2149 | print_dashes(p->out, p->actualWidth[i]+2); |
| 2150 | } |
| 2151 | fputs(zSep, p->out); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2152 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2153 | fputs("\n", p->out); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2157 | ** This is the callback routine that the shell |
| 2158 | ** invokes for each row of a query result. |
| 2159 | */ |
| 2160 | static int shell_callback( |
| 2161 | void *pArg, |
| 2162 | int nArg, /* Number of result columns */ |
| 2163 | char **azArg, /* Text of each result column */ |
| 2164 | char **azCol, /* Column names */ |
drh | d6f2524 | 2020-05-29 12:31:53 +0000 | [diff] [blame] | 2165 | int *aiType /* Column types. Might be NULL */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2166 | ){ |
| 2167 | int i; |
| 2168 | ShellState *p = (ShellState*)pArg; |
| 2169 | |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2170 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2171 | switch( p->cMode ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 2172 | case MODE_Count: |
| 2173 | case MODE_Off: { |
| 2174 | break; |
| 2175 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2176 | case MODE_Line: { |
| 2177 | int w = 5; |
| 2178 | if( azArg==0 ) break; |
| 2179 | for(i=0; i<nArg; i++){ |
| 2180 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
| 2181 | if( len>w ) w = len; |
| 2182 | } |
| 2183 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
| 2184 | for(i=0; i<nArg; i++){ |
| 2185 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
| 2186 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
| 2187 | } |
| 2188 | break; |
| 2189 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2190 | case MODE_Explain: { |
| 2191 | static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 2192 | if( nArg>ArraySize(aExplainWidth) ){ |
| 2193 | nArg = ArraySize(aExplainWidth); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2194 | } |
| 2195 | if( p->cnt++==0 ){ |
| 2196 | for(i=0; i<nArg; i++){ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2197 | int w = aExplainWidth[i]; |
| 2198 | utf8_width_print(p->out, w, azCol[i]); |
| 2199 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2200 | } |
drh | e566ceb | 2020-05-30 15:34:49 +0000 | [diff] [blame] | 2201 | for(i=0; i<nArg; i++){ |
| 2202 | int w = aExplainWidth[i]; |
| 2203 | print_dashes(p->out, w); |
| 2204 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
| 2205 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2206 | } |
| 2207 | if( azArg==0 ) break; |
| 2208 | for(i=0; i<nArg; i++){ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2209 | int w = aExplainWidth[i]; |
drh | aa556b0 | 2021-01-13 12:59:20 +0000 | [diff] [blame] | 2210 | if( i==nArg-1 ) w = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2211 | if( azArg[i] && strlenChar(azArg[i])>w ){ |
| 2212 | w = strlenChar(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2213 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2214 | if( i==1 && p->aiIndent && p->pStmt ){ |
| 2215 | if( p->iIndent<p->nIndent ){ |
| 2216 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2217 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2218 | p->iIndent++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2219 | } |
| 2220 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 2221 | fputs(i==nArg-1 ? "\n" : " ", p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2222 | } |
| 2223 | break; |
| 2224 | } |
| 2225 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 2226 | printSchemaLine(p->out, azArg[0], ";\n"); |
| 2227 | break; |
| 2228 | } |
| 2229 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 2230 | char *z; |
| 2231 | int j; |
| 2232 | int nParen = 0; |
| 2233 | char cEnd = 0; |
| 2234 | char c; |
| 2235 | int nLine = 0; |
| 2236 | assert( nArg==1 ); |
| 2237 | if( azArg[0]==0 ) break; |
| 2238 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 2239 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 2240 | ){ |
| 2241 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 2242 | break; |
| 2243 | } |
| 2244 | z = sqlite3_mprintf("%s", azArg[0]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2245 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2246 | j = 0; |
| 2247 | for(i=0; IsSpace(z[i]); i++){} |
| 2248 | for(; (c = z[i])!=0; i++){ |
| 2249 | if( IsSpace(c) ){ |
drh | c3cbd67 | 2017-10-05 19:12:10 +0000 | [diff] [blame] | 2250 | if( z[j-1]=='\r' ) z[j-1] = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2251 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 2252 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 2253 | j--; |
| 2254 | } |
| 2255 | z[j++] = c; |
| 2256 | } |
| 2257 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 2258 | z[j] = 0; |
| 2259 | if( strlen30(z)>=79 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 2260 | 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] | 2261 | if( c==cEnd ){ |
| 2262 | cEnd = 0; |
| 2263 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 2264 | cEnd = c; |
| 2265 | }else if( c=='[' ){ |
| 2266 | cEnd = ']'; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2267 | }else if( c=='-' && z[i+1]=='-' ){ |
| 2268 | cEnd = '\n'; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2269 | }else if( c=='(' ){ |
| 2270 | nParen++; |
| 2271 | }else if( c==')' ){ |
| 2272 | nParen--; |
| 2273 | if( nLine>0 && nParen==0 && j>0 ){ |
| 2274 | printSchemaLineN(p->out, z, j, "\n"); |
| 2275 | j = 0; |
| 2276 | } |
| 2277 | } |
| 2278 | z[j++] = c; |
drh | 11be81d | 2018-01-06 15:46:20 +0000 | [diff] [blame] | 2279 | if( nParen==1 && cEnd==0 |
| 2280 | && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
| 2281 | ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2282 | if( c=='\n' ) j--; |
| 2283 | printSchemaLineN(p->out, z, j, "\n "); |
| 2284 | j = 0; |
| 2285 | nLine++; |
| 2286 | while( IsSpace(z[i+1]) ){ i++; } |
| 2287 | } |
| 2288 | } |
| 2289 | z[j] = 0; |
| 2290 | } |
| 2291 | printSchemaLine(p->out, z, ";\n"); |
| 2292 | sqlite3_free(z); |
| 2293 | break; |
| 2294 | } |
| 2295 | case MODE_List: { |
| 2296 | if( p->cnt++==0 && p->showHeader ){ |
| 2297 | for(i=0; i<nArg; i++){ |
| 2298 | utf8_printf(p->out,"%s%s",azCol[i], |
| 2299 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
| 2300 | } |
| 2301 | } |
| 2302 | if( azArg==0 ) break; |
| 2303 | for(i=0; i<nArg; i++){ |
| 2304 | char *z = azArg[i]; |
| 2305 | if( z==0 ) z = p->nullValue; |
| 2306 | utf8_printf(p->out, "%s", z); |
| 2307 | if( i<nArg-1 ){ |
| 2308 | utf8_printf(p->out, "%s", p->colSeparator); |
| 2309 | }else{ |
| 2310 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2311 | } |
| 2312 | } |
| 2313 | break; |
| 2314 | } |
| 2315 | case MODE_Html: { |
| 2316 | if( p->cnt++==0 && p->showHeader ){ |
| 2317 | raw_printf(p->out,"<TR>"); |
| 2318 | for(i=0; i<nArg; i++){ |
| 2319 | raw_printf(p->out,"<TH>"); |
| 2320 | output_html_string(p->out, azCol[i]); |
| 2321 | raw_printf(p->out,"</TH>\n"); |
| 2322 | } |
| 2323 | raw_printf(p->out,"</TR>\n"); |
| 2324 | } |
| 2325 | if( azArg==0 ) break; |
| 2326 | raw_printf(p->out,"<TR>"); |
| 2327 | for(i=0; i<nArg; i++){ |
| 2328 | raw_printf(p->out,"<TD>"); |
| 2329 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 2330 | raw_printf(p->out,"</TD>\n"); |
| 2331 | } |
| 2332 | raw_printf(p->out,"</TR>\n"); |
| 2333 | break; |
| 2334 | } |
| 2335 | case MODE_Tcl: { |
| 2336 | if( p->cnt++==0 && p->showHeader ){ |
| 2337 | for(i=0; i<nArg; i++){ |
| 2338 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
| 2339 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 2340 | } |
| 2341 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2342 | } |
| 2343 | if( azArg==0 ) break; |
| 2344 | for(i=0; i<nArg; i++){ |
| 2345 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
| 2346 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
| 2347 | } |
| 2348 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2349 | break; |
| 2350 | } |
| 2351 | case MODE_Csv: { |
| 2352 | setBinaryMode(p->out, 1); |
| 2353 | if( p->cnt++==0 && p->showHeader ){ |
| 2354 | for(i=0; i<nArg; i++){ |
| 2355 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
| 2356 | } |
| 2357 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2358 | } |
| 2359 | if( nArg>0 ){ |
| 2360 | for(i=0; i<nArg; i++){ |
| 2361 | output_csv(p, azArg[i], i<nArg-1); |
| 2362 | } |
| 2363 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2364 | } |
| 2365 | setTextMode(p->out, 1); |
| 2366 | break; |
| 2367 | } |
| 2368 | case MODE_Insert: { |
| 2369 | if( azArg==0 ) break; |
| 2370 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2371 | if( p->showHeader ){ |
| 2372 | raw_printf(p->out,"("); |
| 2373 | for(i=0; i<nArg; i++){ |
| 2374 | if( i>0 ) raw_printf(p->out, ","); |
| 2375 | if( quoteChar(azCol[i]) ){ |
| 2376 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2377 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2378 | utf8_printf(p->out, "%s", z); |
| 2379 | sqlite3_free(z); |
| 2380 | }else{ |
| 2381 | raw_printf(p->out, "%s", azCol[i]); |
| 2382 | } |
| 2383 | } |
| 2384 | raw_printf(p->out,")"); |
| 2385 | } |
| 2386 | p->cnt++; |
| 2387 | for(i=0; i<nArg; i++){ |
| 2388 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2389 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2390 | utf8_printf(p->out,"NULL"); |
| 2391 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2392 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2393 | output_quoted_string(p->out, azArg[i]); |
| 2394 | }else{ |
| 2395 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2396 | } |
| 2397 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2398 | utf8_printf(p->out,"%s", azArg[i]); |
| 2399 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2400 | char z[50]; |
| 2401 | double r = sqlite3_column_double(p->pStmt, i); |
drh | 2f1f880 | 2018-06-13 17:19:20 +0000 | [diff] [blame] | 2402 | sqlite3_uint64 ur; |
| 2403 | memcpy(&ur,&r,sizeof(r)); |
| 2404 | if( ur==0x7ff0000000000000LL ){ |
| 2405 | raw_printf(p->out, "1e999"); |
| 2406 | }else if( ur==0xfff0000000000000LL ){ |
| 2407 | raw_printf(p->out, "-1e999"); |
| 2408 | }else{ |
drh | 537a6bf | 2022-02-15 13:23:09 +0000 | [diff] [blame] | 2409 | sqlite3_int64 ir = (sqlite3_int64)r; |
| 2410 | if( r==(double)ir ){ |
| 2411 | sqlite3_snprintf(50,z,"%lld.0", ir); |
| 2412 | }else{ |
| 2413 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2414 | } |
drh | 2f1f880 | 2018-06-13 17:19:20 +0000 | [diff] [blame] | 2415 | raw_printf(p->out, "%s", z); |
| 2416 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2417 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2418 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2419 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2420 | output_hex_blob(p->out, pBlob, nBlob); |
| 2421 | }else if( isNumber(azArg[i], 0) ){ |
| 2422 | utf8_printf(p->out,"%s", azArg[i]); |
| 2423 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2424 | output_quoted_string(p->out, azArg[i]); |
| 2425 | }else{ |
| 2426 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2427 | } |
| 2428 | } |
| 2429 | raw_printf(p->out,");\n"); |
| 2430 | break; |
| 2431 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2432 | case MODE_Json: { |
| 2433 | if( azArg==0 ) break; |
| 2434 | if( p->cnt==0 ){ |
| 2435 | fputs("[{", p->out); |
| 2436 | }else{ |
| 2437 | fputs(",\n{", p->out); |
| 2438 | } |
| 2439 | p->cnt++; |
| 2440 | for(i=0; i<nArg; i++){ |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2441 | output_json_string(p->out, azCol[i], -1); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2442 | putc(':', p->out); |
| 2443 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2444 | fputs("null",p->out); |
| 2445 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2446 | char z[50]; |
| 2447 | double r = sqlite3_column_double(p->pStmt, i); |
| 2448 | sqlite3_uint64 ur; |
| 2449 | memcpy(&ur,&r,sizeof(r)); |
| 2450 | if( ur==0x7ff0000000000000LL ){ |
| 2451 | raw_printf(p->out, "1e999"); |
| 2452 | }else if( ur==0xfff0000000000000LL ){ |
| 2453 | raw_printf(p->out, "-1e999"); |
| 2454 | }else{ |
| 2455 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2456 | raw_printf(p->out, "%s", z); |
| 2457 | } |
| 2458 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2459 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2460 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2461 | output_json_string(p->out, pBlob, nBlob); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2462 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
drh | 69c093d | 2020-05-29 00:21:43 +0000 | [diff] [blame] | 2463 | output_json_string(p->out, azArg[i], -1); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 2464 | }else{ |
| 2465 | utf8_printf(p->out,"%s", azArg[i]); |
| 2466 | } |
| 2467 | if( i<nArg-1 ){ |
| 2468 | putc(',', p->out); |
| 2469 | } |
| 2470 | } |
| 2471 | putc('}', p->out); |
| 2472 | break; |
| 2473 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2474 | case MODE_Quote: { |
| 2475 | if( azArg==0 ) break; |
| 2476 | if( p->cnt==0 && p->showHeader ){ |
| 2477 | for(i=0; i<nArg; i++){ |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2478 | if( i>0 ) fputs(p->colSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2479 | output_quoted_string(p->out, azCol[i]); |
| 2480 | } |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2481 | fputs(p->rowSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2482 | } |
| 2483 | p->cnt++; |
| 2484 | for(i=0; i<nArg; i++){ |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2485 | if( i>0 ) fputs(p->colSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2486 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2487 | utf8_printf(p->out,"NULL"); |
| 2488 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2489 | output_quoted_string(p->out, azArg[i]); |
| 2490 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2491 | utf8_printf(p->out,"%s", azArg[i]); |
| 2492 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2493 | char z[50]; |
| 2494 | double r = sqlite3_column_double(p->pStmt, i); |
| 2495 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2496 | raw_printf(p->out, "%s", z); |
| 2497 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2498 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2499 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2500 | output_hex_blob(p->out, pBlob, nBlob); |
| 2501 | }else if( isNumber(azArg[i], 0) ){ |
| 2502 | utf8_printf(p->out,"%s", azArg[i]); |
| 2503 | }else{ |
| 2504 | output_quoted_string(p->out, azArg[i]); |
| 2505 | } |
| 2506 | } |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 2507 | fputs(p->rowSeparator, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | } |
| 2510 | case MODE_Ascii: { |
| 2511 | if( p->cnt++==0 && p->showHeader ){ |
| 2512 | for(i=0; i<nArg; i++){ |
| 2513 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2514 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
| 2515 | } |
| 2516 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2517 | } |
| 2518 | if( azArg==0 ) break; |
| 2519 | for(i=0; i<nArg; i++){ |
| 2520 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2521 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
| 2522 | } |
| 2523 | utf8_printf(p->out, "%s", p->rowSeparator); |
| 2524 | break; |
| 2525 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2526 | case MODE_EQP: { |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2527 | eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2530 | } |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | ** This is the callback routine that the SQLite library |
| 2536 | ** invokes for each row of a query result. |
| 2537 | */ |
| 2538 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2539 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2540 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2545 | ** output onto the end of a ShellText object. |
| 2546 | */ |
| 2547 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2548 | ShellText *p = (ShellText*)pArg; |
| 2549 | int i; |
| 2550 | UNUSED_PARAMETER(az); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 2551 | if( azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2552 | if( p->n ) appendText(p, "|", 0); |
| 2553 | for(i=0; i<nArg; i++){ |
| 2554 | if( i ) appendText(p, ",", 0); |
| 2555 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2556 | } |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
| 2560 | /* |
| 2561 | ** Generate an appropriate SELFTEST table in the main database. |
| 2562 | */ |
| 2563 | static void createSelftestTable(ShellState *p){ |
| 2564 | char *zErrMsg = 0; |
| 2565 | sqlite3_exec(p->db, |
| 2566 | "SAVEPOINT selftest_init;\n" |
| 2567 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
| 2568 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2569 | " op TEXT,\n" /* Operator: memo run */ |
| 2570 | " cmd TEXT,\n" /* Command text */ |
| 2571 | " ans TEXT\n" /* Desired answer */ |
| 2572 | ");" |
| 2573 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2574 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2575 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2576 | " 'memo','Tests generated by --init');\n" |
| 2577 | "INSERT INTO [_shell$self]\n" |
| 2578 | " SELECT 'run',\n" |
| 2579 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2580 | "FROM sqlite_schema ORDER BY 2'',224))',\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2581 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2582 | "FROM sqlite_schema ORDER BY 2',224));\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2583 | "INSERT INTO [_shell$self]\n" |
| 2584 | " SELECT 'run'," |
| 2585 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2586 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2587 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2588 | " FROM (\n" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2589 | " SELECT name FROM sqlite_schema\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2590 | " WHERE type='table'\n" |
| 2591 | " AND name<>'selftest'\n" |
| 2592 | " AND coalesce(rootpage,0)>0\n" |
| 2593 | " )\n" |
| 2594 | " ORDER BY name;\n" |
| 2595 | "INSERT INTO [_shell$self]\n" |
| 2596 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
| 2597 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2598 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2599 | "DROP TABLE [_shell$self];" |
| 2600 | ,0,0,&zErrMsg); |
| 2601 | if( zErrMsg ){ |
| 2602 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2603 | sqlite3_free(zErrMsg); |
| 2604 | } |
| 2605 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
| 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | /* |
| 2610 | ** Set the destination table field of the ShellState structure to |
| 2611 | ** the name of the table given. Escape any quote characters in the |
| 2612 | ** table name. |
| 2613 | */ |
| 2614 | static void set_table_name(ShellState *p, const char *zName){ |
| 2615 | int i, n; |
mistachkin | 2158a0c | 2017-09-09 00:51:36 +0000 | [diff] [blame] | 2616 | char cQuote; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2617 | char *z; |
| 2618 | |
| 2619 | if( p->zDestTable ){ |
| 2620 | free(p->zDestTable); |
| 2621 | p->zDestTable = 0; |
| 2622 | } |
| 2623 | if( zName==0 ) return; |
| 2624 | cQuote = quoteChar(zName); |
| 2625 | n = strlen30(zName); |
| 2626 | if( cQuote ) n += n+2; |
| 2627 | z = p->zDestTable = malloc( n+1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2628 | shell_check_oom(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2629 | n = 0; |
| 2630 | if( cQuote ) z[n++] = cQuote; |
| 2631 | for(i=0; zName[i]; i++){ |
| 2632 | z[n++] = zName[i]; |
| 2633 | if( zName[i]==cQuote ) z[n++] = cQuote; |
| 2634 | } |
| 2635 | if( cQuote ) z[n++] = cQuote; |
| 2636 | z[n] = 0; |
| 2637 | } |
| 2638 | |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2639 | /* |
| 2640 | ** Maybe construct two lines of text that point out the position of a |
| 2641 | ** syntax error. Return a pointer to the text, in memory obtained from |
| 2642 | ** sqlite3_malloc(). Or, if the most recent error does not involve a |
| 2643 | ** specific token that we can point to, return an empty string. |
| 2644 | ** |
| 2645 | ** In all cases, the memory returned is obtained from sqlite3_malloc64() |
| 2646 | ** and should be released by the caller invoking sqlite3_free(). |
| 2647 | */ |
| 2648 | static char *shell_error_context(const char *zSql, sqlite3 *db){ |
| 2649 | int iOffset; |
| 2650 | size_t len; |
| 2651 | char *zCode; |
| 2652 | char *zMsg; |
| 2653 | int i; |
| 2654 | if( db==0 |
| 2655 | || zSql==0 |
| 2656 | || (iOffset = sqlite3_error_offset(db))<0 |
| 2657 | ){ |
| 2658 | return sqlite3_mprintf(""); |
| 2659 | } |
| 2660 | while( iOffset>50 ){ |
| 2661 | iOffset--; |
| 2662 | zSql++; |
| 2663 | while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } |
| 2664 | } |
| 2665 | len = strlen(zSql); |
| 2666 | if( len>78 ){ |
| 2667 | len = 78; |
| 2668 | while( (zSql[len]&0xc0)==0x80 ) len--; |
| 2669 | } |
| 2670 | zCode = sqlite3_mprintf("%.*s", len, zSql); |
drh | 1e84e1e | 2022-10-31 01:22:38 +0000 | [diff] [blame] | 2671 | shell_check_oom(zCode); |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2672 | for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } |
| 2673 | if( iOffset<25 ){ |
| 2674 | zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, ""); |
| 2675 | }else{ |
| 2676 | zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, ""); |
| 2677 | } |
| 2678 | return zMsg; |
| 2679 | } |
| 2680 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2681 | |
| 2682 | /* |
| 2683 | ** Execute a query statement that will generate SQL output. Print |
| 2684 | ** the result columns, comma-separated, on a line and then add a |
| 2685 | ** semicolon terminator to the end of that line. |
| 2686 | ** |
| 2687 | ** If the number of columns is 1 and that column contains text "--" |
| 2688 | ** then write the semicolon on a separate line. That way, if a |
| 2689 | ** "--" comment occurs at the end of the statement, the comment |
| 2690 | ** won't consume the semicolon terminator. |
| 2691 | */ |
| 2692 | static int run_table_dump_query( |
| 2693 | ShellState *p, /* Query context */ |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 2694 | const char *zSelect /* SELECT statement to extract content */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2695 | ){ |
| 2696 | sqlite3_stmt *pSelect; |
| 2697 | int rc; |
| 2698 | int nResult; |
| 2699 | int i; |
| 2700 | const char *z; |
| 2701 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
| 2702 | if( rc!=SQLITE_OK || !pSelect ){ |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2703 | char *zContext = shell_error_context(zSelect, p->db); |
| 2704 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc, |
| 2705 | sqlite3_errmsg(p->db), zContext); |
| 2706 | sqlite3_free(zContext); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2707 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2708 | return rc; |
| 2709 | } |
| 2710 | rc = sqlite3_step(pSelect); |
| 2711 | nResult = sqlite3_column_count(pSelect); |
| 2712 | while( rc==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2713 | z = (const char*)sqlite3_column_text(pSelect, 0); |
| 2714 | utf8_printf(p->out, "%s", z); |
| 2715 | for(i=1; i<nResult; i++){ |
| 2716 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
| 2717 | } |
| 2718 | if( z==0 ) z = ""; |
| 2719 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2720 | if( z[0] ){ |
| 2721 | raw_printf(p->out, "\n;\n"); |
| 2722 | }else{ |
| 2723 | raw_printf(p->out, ";\n"); |
| 2724 | } |
| 2725 | rc = sqlite3_step(pSelect); |
| 2726 | } |
| 2727 | rc = sqlite3_finalize(pSelect); |
| 2728 | if( rc!=SQLITE_OK ){ |
| 2729 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2730 | sqlite3_errmsg(p->db)); |
| 2731 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
| 2732 | } |
| 2733 | return rc; |
| 2734 | } |
| 2735 | |
| 2736 | /* |
larrybr | f9a49b0 | 2021-10-26 16:57:09 +0000 | [diff] [blame] | 2737 | ** Allocate space and save off string indicating current error. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2738 | */ |
| 2739 | static char *save_err_msg( |
larrybr | f9a49b0 | 2021-10-26 16:57:09 +0000 | [diff] [blame] | 2740 | sqlite3 *db, /* Database to query */ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2741 | const char *zPhase, /* When the error occcurs */ |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2742 | int rc, /* Error code returned from API */ |
| 2743 | const char *zSql /* SQL string, or NULL */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2744 | ){ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2745 | char *zErr; |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2746 | char *zContext; |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2747 | sqlite3_str *pStr = sqlite3_str_new(0); |
| 2748 | sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); |
| 2749 | if( rc>1 ){ |
| 2750 | sqlite3_str_appendf(pStr, " (%d)", rc); |
| 2751 | } |
drh | f62641e | 2021-12-24 20:22:13 +0000 | [diff] [blame] | 2752 | zContext = shell_error_context(zSql, db); |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 2753 | if( zContext ){ |
| 2754 | sqlite3_str_appendall(pStr, zContext); |
| 2755 | sqlite3_free(zContext); |
| 2756 | } |
| 2757 | zErr = sqlite3_str_finish(pStr); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 2758 | shell_check_oom(zErr); |
| 2759 | return zErr; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | #ifdef __linux__ |
| 2763 | /* |
| 2764 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2765 | */ |
| 2766 | static void displayLinuxIoStats(FILE *out){ |
| 2767 | FILE *in; |
| 2768 | char z[200]; |
| 2769 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2770 | in = fopen(z, "rb"); |
| 2771 | if( in==0 ) return; |
| 2772 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2773 | static const struct { |
| 2774 | const char *zPattern; |
| 2775 | const char *zDesc; |
| 2776 | } aTrans[] = { |
| 2777 | { "rchar: ", "Bytes received by read():" }, |
| 2778 | { "wchar: ", "Bytes sent to write():" }, |
| 2779 | { "syscr: ", "Read() system calls:" }, |
| 2780 | { "syscw: ", "Write() system calls:" }, |
| 2781 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2782 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2783 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 2784 | }; |
| 2785 | int i; |
| 2786 | for(i=0; i<ArraySize(aTrans); i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 2787 | int n = strlen30(aTrans[i].zPattern); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 2788 | if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2789 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
| 2790 | break; |
| 2791 | } |
| 2792 | } |
| 2793 | } |
| 2794 | fclose(in); |
| 2795 | } |
| 2796 | #endif |
| 2797 | |
| 2798 | /* |
| 2799 | ** Display a single line of status using 64-bit values. |
| 2800 | */ |
| 2801 | static void displayStatLine( |
| 2802 | ShellState *p, /* The shell context */ |
| 2803 | char *zLabel, /* Label for this one line */ |
| 2804 | char *zFormat, /* Format for the result */ |
| 2805 | int iStatusCtrl, /* Which status to display */ |
| 2806 | int bReset /* True to reset the stats */ |
| 2807 | ){ |
| 2808 | sqlite3_int64 iCur = -1; |
| 2809 | sqlite3_int64 iHiwtr = -1; |
| 2810 | int i, nPercent; |
| 2811 | char zLine[200]; |
| 2812 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2813 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2814 | if( zFormat[i]=='%' ) nPercent++; |
| 2815 | } |
| 2816 | if( nPercent>1 ){ |
| 2817 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2818 | }else{ |
| 2819 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2820 | } |
| 2821 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2822 | } |
| 2823 | |
| 2824 | /* |
| 2825 | ** Display memory stats. |
| 2826 | */ |
| 2827 | static int display_stats( |
| 2828 | sqlite3 *db, /* Database to query */ |
| 2829 | ShellState *pArg, /* Pointer to ShellState */ |
| 2830 | int bReset /* True to reset the stats */ |
| 2831 | ){ |
| 2832 | int iCur; |
| 2833 | int iHiwtr; |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2834 | FILE *out; |
| 2835 | if( pArg==0 || pArg->out==0 ) return 0; |
| 2836 | out = pArg->out; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2837 | |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 2838 | if( pArg->pStmt && pArg->statsOn==2 ){ |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2839 | int nCol, i, x; |
| 2840 | sqlite3_stmt *pStmt = pArg->pStmt; |
| 2841 | char z[100]; |
| 2842 | nCol = sqlite3_column_count(pStmt); |
| 2843 | raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); |
| 2844 | for(i=0; i<nCol; i++){ |
| 2845 | sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); |
| 2846 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2847 | #ifndef SQLITE_OMIT_DECLTYPE |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2848 | sqlite3_snprintf(30, z+x, "declared type:"); |
| 2849 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2850 | #endif |
| 2851 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2852 | sqlite3_snprintf(30, z+x, "database name:"); |
| 2853 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); |
| 2854 | sqlite3_snprintf(30, z+x, "table name:"); |
| 2855 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); |
| 2856 | sqlite3_snprintf(30, z+x, "origin name:"); |
| 2857 | utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2858 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2859 | } |
drh | 929cce8 | 2018-03-17 16:26:36 +0000 | [diff] [blame] | 2860 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2861 | |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 2862 | if( pArg->statsOn==3 ){ |
| 2863 | if( pArg->pStmt ){ |
| 2864 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2865 | raw_printf(pArg->out, "VM-steps: %d\n", iCur); |
| 2866 | } |
| 2867 | return 0; |
| 2868 | } |
| 2869 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2870 | displayStatLine(pArg, "Memory Used:", |
| 2871 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2872 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2873 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
| 2874 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 2875 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2876 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
| 2877 | } |
| 2878 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2879 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
| 2880 | displayStatLine(pArg, "Largest Allocation:", |
| 2881 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2882 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2883 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2884 | #ifdef YYTRACKMAXSTACKDEPTH |
| 2885 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2886 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
| 2887 | #endif |
| 2888 | |
| 2889 | if( db ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2890 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2891 | iHiwtr = iCur = -1; |
| 2892 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2893 | &iCur, &iHiwtr, bReset); |
| 2894 | raw_printf(pArg->out, |
| 2895 | "Lookaside Slots Used: %d (max %d)\n", |
| 2896 | iCur, iHiwtr); |
| 2897 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2898 | &iCur, &iHiwtr, bReset); |
| 2899 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2900 | iHiwtr); |
| 2901 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2902 | &iCur, &iHiwtr, bReset); |
| 2903 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2904 | iHiwtr); |
| 2905 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2906 | &iCur, &iHiwtr, bReset); |
| 2907 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2908 | iHiwtr); |
| 2909 | } |
| 2910 | iHiwtr = iCur = -1; |
| 2911 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
| 2912 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2913 | iCur); |
| 2914 | iHiwtr = iCur = -1; |
| 2915 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
| 2916 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
| 2917 | iHiwtr = iCur = -1; |
| 2918 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
| 2919 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
| 2920 | iHiwtr = iCur = -1; |
| 2921 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
| 2922 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
| 2923 | iHiwtr = iCur = -1; |
drh | ffc78a4 | 2018-03-14 14:53:50 +0000 | [diff] [blame] | 2924 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); |
| 2925 | raw_printf(pArg->out, "Page cache spills: %d\n", iCur); |
| 2926 | iHiwtr = iCur = -1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2927 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
| 2928 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| 2929 | iCur); |
| 2930 | iHiwtr = iCur = -1; |
| 2931 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
| 2932 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| 2933 | iCur); |
| 2934 | } |
| 2935 | |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2936 | if( pArg->pStmt ){ |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 2937 | int iHit, iMiss; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2938 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2939 | bReset); |
| 2940 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
| 2941 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
| 2942 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
| 2943 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
| 2944 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 2945 | iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset); |
| 2946 | iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset); |
| 2947 | if( iHit || iMiss ){ |
| 2948 | raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n", |
| 2949 | iHit, iHit+iMiss); |
| 2950 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2951 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| 2952 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 2953 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); |
drh | 393344f | 2018-03-09 16:37:05 +0000 | [diff] [blame] | 2954 | raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); |
| 2955 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); |
| 2956 | raw_printf(pArg->out, "Number of times run: %d\n", iCur); |
| 2957 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); |
| 2958 | raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | #ifdef __linux__ |
| 2962 | displayLinuxIoStats(pArg->out); |
| 2963 | #endif |
| 2964 | |
| 2965 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2966 | |
| 2967 | return 0; |
| 2968 | } |
| 2969 | |
| 2970 | /* |
| 2971 | ** Display scan stats. |
| 2972 | */ |
| 2973 | static void display_scanstats( |
| 2974 | sqlite3 *db, /* Database to query */ |
| 2975 | ShellState *pArg /* Pointer to ShellState */ |
| 2976 | ){ |
| 2977 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2978 | UNUSED_PARAMETER(db); |
| 2979 | UNUSED_PARAMETER(pArg); |
| 2980 | #else |
| 2981 | int i, k, n, mx; |
| 2982 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
| 2983 | mx = 0; |
| 2984 | for(k=0; k<=mx; k++){ |
| 2985 | double rEstLoop = 1.0; |
| 2986 | for(i=n=0; 1; i++){ |
| 2987 | sqlite3_stmt *p = pArg->pStmt; |
| 2988 | sqlite3_int64 nLoop, nVisit; |
| 2989 | double rEst; |
| 2990 | int iSid; |
| 2991 | const char *zExplain; |
| 2992 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2993 | break; |
| 2994 | } |
| 2995 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| 2996 | if( iSid>mx ) mx = iSid; |
| 2997 | if( iSid!=k ) continue; |
| 2998 | if( n==0 ){ |
| 2999 | rEstLoop = (double)nLoop; |
| 3000 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| 3001 | } |
| 3002 | n++; |
| 3003 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 3004 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 3005 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| 3006 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| 3007 | rEstLoop *= rEst; |
| 3008 | raw_printf(pArg->out, |
| 3009 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| 3010 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| 3011 | ); |
| 3012 | } |
| 3013 | } |
| 3014 | raw_printf(pArg->out, "---------------------------\n"); |
| 3015 | #endif |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 3020 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 3021 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 3022 | ** Otherwise, return zero. |
| 3023 | */ |
| 3024 | static int str_in_array(const char *zStr, const char **azArray){ |
| 3025 | int i; |
| 3026 | for(i=0; azArray[i]; i++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 3027 | if( 0==cli_strcmp(zStr, azArray[i]) ) return 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3028 | } |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| 3034 | ** and populate the ShellState.aiIndent[] array with the number of |
| 3035 | ** spaces each opcode should be indented before it is output. |
| 3036 | ** |
| 3037 | ** The indenting rules are: |
| 3038 | ** |
| 3039 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 3040 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 3041 | ** itself by 2 spaces. |
| 3042 | ** |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 3043 | ** * Do the previous for "Return" instructions for when P2 is positive. |
| 3044 | ** See tag-20220407a in wherecode.c and vdbe.c. |
| 3045 | ** |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3046 | ** * For each "Goto", if the jump destination is earlier in the program |
| 3047 | ** and ends on one of: |
| 3048 | ** Yield SeekGt SeekLt RowSetRead Rewind |
| 3049 | ** or if the P1 parameter is one instead of zero, |
| 3050 | ** then indent all opcodes between the earlier instruction |
| 3051 | ** and "Goto" by 2 spaces. |
| 3052 | */ |
| 3053 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| 3054 | const char *zSql; /* The text of the SQL statement */ |
| 3055 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 3056 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 3057 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| 3058 | int iOp; /* Index of operation in p->aiIndent[] */ |
| 3059 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 3060 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 3061 | "Return", 0 }; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3062 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 3063 | "Rewind", 0 }; |
| 3064 | const char *azGoto[] = { "Goto", 0 }; |
| 3065 | |
| 3066 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 3067 | ** cannot be verified, return early. */ |
| 3068 | if( sqlite3_column_count(pSql)!=8 ){ |
| 3069 | p->cMode = p->mode; |
| 3070 | return; |
| 3071 | } |
| 3072 | zSql = sqlite3_sql(pSql); |
| 3073 | if( zSql==0 ) return; |
| 3074 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| 3075 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 3076 | p->cMode = p->mode; |
| 3077 | return; |
| 3078 | } |
| 3079 | |
| 3080 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 3081 | int i; |
| 3082 | int iAddr = sqlite3_column_int(pSql, 0); |
| 3083 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| 3084 | |
| 3085 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 3086 | ** p2 is an instruction address, set variable p2op to the index of that |
| 3087 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 3088 | ** the current instruction is part of a sub-program generated by an |
| 3089 | ** SQL trigger or foreign key. */ |
| 3090 | int p2 = sqlite3_column_int(pSql, 3); |
| 3091 | int p2op = (p2 + (iOp-iAddr)); |
| 3092 | |
| 3093 | /* Grow the p->aiIndent array as required */ |
| 3094 | if( iOp>=nAlloc ){ |
| 3095 | if( iOp==0 ){ |
| 3096 | /* Do further verfication that this is explain output. Abort if |
| 3097 | ** it is not */ |
| 3098 | static const char *explainCols[] = { |
| 3099 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 3100 | int jj; |
| 3101 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 3102 | if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3103 | p->cMode = p->mode; |
| 3104 | sqlite3_reset(pSql); |
| 3105 | return; |
| 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | nAlloc += 100; |
| 3110 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3111 | shell_check_oom(p->aiIndent); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3112 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3113 | shell_check_oom(abYield); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3114 | } |
| 3115 | abYield[iOp] = str_in_array(zOp, azYield); |
| 3116 | p->aiIndent[iOp] = 0; |
| 3117 | p->nIndent = iOp+1; |
| 3118 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 3119 | if( str_in_array(zOp, azNext) && p2op>0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3120 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 3121 | } |
| 3122 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 3123 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 3124 | ){ |
| 3125 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | p->iIndent = 0; |
| 3130 | sqlite3_free(abYield); |
| 3131 | sqlite3_reset(pSql); |
| 3132 | } |
| 3133 | |
| 3134 | /* |
| 3135 | ** Free the array allocated by explain_data_prepare(). |
| 3136 | */ |
| 3137 | static void explain_data_delete(ShellState *p){ |
| 3138 | sqlite3_free(p->aiIndent); |
| 3139 | p->aiIndent = 0; |
| 3140 | p->nIndent = 0; |
| 3141 | p->iIndent = 0; |
| 3142 | } |
| 3143 | |
| 3144 | /* |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 3145 | ** Disable and restore .wheretrace and .treetrace/.selecttrace settings. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3146 | */ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3147 | static unsigned int savedSelectTrace; |
| 3148 | static unsigned int savedWhereTrace; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3149 | static void disable_debug_trace_modes(void){ |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3150 | unsigned int zero = 0; |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3151 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3152 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3153 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); |
drh | 0a2fb79 | 2020-12-04 16:58:20 +0000 | [diff] [blame] | 3154 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3155 | } |
| 3156 | static void restore_debug_trace_modes(void){ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 3157 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); |
| 3158 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3161 | /* Create the TEMP table used to store parameter bindings */ |
| 3162 | static void bind_table_init(ShellState *p){ |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3163 | int wrSchema = 0; |
drh | 4b86e20 | 2020-01-19 20:37:26 +0000 | [diff] [blame] | 3164 | int defensiveMode = 0; |
| 3165 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); |
| 3166 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3167 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); |
| 3168 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3169 | sqlite3_exec(p->db, |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3170 | "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3171 | " key TEXT PRIMARY KEY,\n" |
larrybr | dabada6 | 2021-04-04 12:52:58 +0000 | [diff] [blame] | 3172 | " value\n" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3173 | ") WITHOUT ROWID;", |
| 3174 | 0, 0, 0); |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3175 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); |
drh | 4b86e20 | 2020-01-19 20:37:26 +0000 | [diff] [blame] | 3176 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3179 | /* |
| 3180 | ** Bind parameters on a prepared statement. |
| 3181 | ** |
| 3182 | ** Parameter bindings are taken from a TEMP table of the form: |
| 3183 | ** |
drh | 1cb0263 | 2019-03-25 22:05:22 +0000 | [diff] [blame] | 3184 | ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3185 | ** WITHOUT ROWID; |
| 3186 | ** |
drh | 91654b2 | 2020-04-02 13:21:10 +0000 | [diff] [blame] | 3187 | ** No bindings occur if this table does not exist. The name of the table |
| 3188 | ** begins with "sqlite_" so that it will not collide with ordinary application |
| 3189 | ** tables. The table must be in the TEMP schema. |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3190 | */ |
| 3191 | static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ |
| 3192 | int nVar; |
| 3193 | int i; |
| 3194 | int rc; |
| 3195 | sqlite3_stmt *pQ = 0; |
| 3196 | |
| 3197 | nVar = sqlite3_bind_parameter_count(pStmt); |
| 3198 | if( nVar==0 ) return; /* Nothing to do */ |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3199 | if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3200 | "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ |
| 3201 | return; /* Parameter table does not exist */ |
| 3202 | } |
| 3203 | rc = sqlite3_prepare_v2(pArg->db, |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 3204 | "SELECT value FROM temp.sqlite_parameters" |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3205 | " WHERE key=?1", -1, &pQ, 0); |
| 3206 | if( rc || pQ==0 ) return; |
| 3207 | for(i=1; i<=nVar; i++){ |
| 3208 | char zNum[30]; |
| 3209 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
| 3210 | if( zVar==0 ){ |
| 3211 | sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); |
| 3212 | zVar = zNum; |
| 3213 | } |
| 3214 | sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); |
| 3215 | if( sqlite3_step(pQ)==SQLITE_ROW ){ |
| 3216 | sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); |
| 3217 | }else{ |
| 3218 | sqlite3_bind_null(pStmt, i); |
| 3219 | } |
| 3220 | sqlite3_reset(pQ); |
| 3221 | } |
| 3222 | sqlite3_finalize(pQ); |
| 3223 | } |
| 3224 | |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3225 | /* |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3226 | ** UTF8 box-drawing characters. Imagine box lines like this: |
| 3227 | ** |
| 3228 | ** 1 |
| 3229 | ** | |
| 3230 | ** 4 --+-- 2 |
| 3231 | ** | |
| 3232 | ** 3 |
| 3233 | ** |
| 3234 | ** Each box characters has between 2 and 4 of the lines leading from |
| 3235 | ** the center. The characters are here identified by the numbers of |
| 3236 | ** their corresponding lines. |
| 3237 | */ |
| 3238 | #define BOX_24 "\342\224\200" /* U+2500 --- */ |
| 3239 | #define BOX_13 "\342\224\202" /* U+2502 | */ |
| 3240 | #define BOX_23 "\342\224\214" /* U+250c ,- */ |
| 3241 | #define BOX_34 "\342\224\220" /* U+2510 -, */ |
| 3242 | #define BOX_12 "\342\224\224" /* U+2514 '- */ |
| 3243 | #define BOX_14 "\342\224\230" /* U+2518 -' */ |
| 3244 | #define BOX_123 "\342\224\234" /* U+251c |- */ |
| 3245 | #define BOX_134 "\342\224\244" /* U+2524 -| */ |
| 3246 | #define BOX_234 "\342\224\254" /* U+252c -,- */ |
| 3247 | #define BOX_124 "\342\224\264" /* U+2534 -'- */ |
| 3248 | #define BOX_1234 "\342\224\274" /* U+253c -|- */ |
| 3249 | |
| 3250 | /* Draw horizontal line N characters long using unicode box |
| 3251 | ** characters |
| 3252 | */ |
| 3253 | static void print_box_line(FILE *out, int N){ |
| 3254 | const char zDash[] = |
| 3255 | BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 |
| 3256 | BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; |
| 3257 | const int nDash = sizeof(zDash) - 1; |
| 3258 | N *= 3; |
| 3259 | while( N>nDash ){ |
| 3260 | utf8_printf(out, zDash); |
| 3261 | N -= nDash; |
| 3262 | } |
| 3263 | utf8_printf(out, "%.*s", N, zDash); |
| 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | ** Draw a horizontal separator for a MODE_Box table. |
| 3268 | */ |
| 3269 | static void print_box_row_separator( |
| 3270 | ShellState *p, |
| 3271 | int nArg, |
| 3272 | const char *zSep1, |
| 3273 | const char *zSep2, |
| 3274 | const char *zSep3 |
| 3275 | ){ |
| 3276 | int i; |
| 3277 | if( nArg>0 ){ |
| 3278 | utf8_printf(p->out, "%s", zSep1); |
| 3279 | print_box_line(p->out, p->actualWidth[0]+2); |
| 3280 | for(i=1; i<nArg; i++){ |
| 3281 | utf8_printf(p->out, "%s", zSep2); |
| 3282 | print_box_line(p->out, p->actualWidth[i]+2); |
| 3283 | } |
| 3284 | utf8_printf(p->out, "%s", zSep3); |
| 3285 | } |
| 3286 | fputs("\n", p->out); |
| 3287 | } |
| 3288 | |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3289 | /* |
| 3290 | ** z[] is a line of text that is to be displayed the .mode box or table or |
| 3291 | ** similar tabular formats. z[] might contain control characters such |
| 3292 | ** as \n, \t, \f, or \r. |
| 3293 | ** |
| 3294 | ** Compute characters to display on the first line of z[]. Stop at the |
| 3295 | ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3296 | ** from malloc()) of that first line, which caller should free sometime. |
| 3297 | ** Write anything to display on the next line into *pzTail. If this is |
| 3298 | ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3299 | */ |
| 3300 | static char *translateForDisplayAndDup( |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3301 | const unsigned char *z, /* Input text to be transformed */ |
| 3302 | const unsigned char **pzTail, /* OUT: Tail of the input for next line */ |
| 3303 | int mxWidth, /* Max width. 0 means no limit */ |
| 3304 | u8 bWordWrap /* If true, avoid breaking mid-word */ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3305 | ){ |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3306 | int i; /* Input bytes consumed */ |
| 3307 | int j; /* Output bytes generated */ |
| 3308 | int k; /* Input bytes to be displayed */ |
| 3309 | int n; /* Output column number */ |
| 3310 | unsigned char *zOut; /* Output text */ |
| 3311 | |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3312 | if( z==0 ){ |
| 3313 | *pzTail = 0; |
| 3314 | return 0; |
| 3315 | } |
| 3316 | if( mxWidth<0 ) mxWidth = -mxWidth; |
| 3317 | if( mxWidth==0 ) mxWidth = 1000000; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3318 | i = j = n = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3319 | while( n<mxWidth ){ |
| 3320 | if( z[i]>=' ' ){ |
| 3321 | n++; |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3322 | do{ i++; j++; }while( (z[i]&0xc0)==0x80 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3323 | continue; |
| 3324 | } |
| 3325 | if( z[i]=='\t' ){ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3326 | do{ |
| 3327 | n++; |
| 3328 | j++; |
| 3329 | }while( (n&7)!=0 && n<mxWidth ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3330 | i++; |
| 3331 | continue; |
| 3332 | } |
| 3333 | break; |
| 3334 | } |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3335 | if( n>=mxWidth && bWordWrap ){ |
| 3336 | /* Perhaps try to back up to a better place to break the line */ |
| 3337 | for(k=i; k>i/2; k--){ |
| 3338 | if( isspace(z[k-1]) ) break; |
| 3339 | } |
| 3340 | if( k<=i/2 ){ |
| 3341 | for(k=i; k>i/2; k--){ |
drh | e66532a | 2022-02-01 13:17:11 +0000 | [diff] [blame] | 3342 | if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3343 | } |
| 3344 | } |
| 3345 | if( k<=i/2 ){ |
| 3346 | k = i; |
| 3347 | }else{ |
| 3348 | i = k; |
| 3349 | while( z[i]==' ' ) i++; |
| 3350 | } |
| 3351 | }else{ |
| 3352 | k = i; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3353 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3354 | if( n>=mxWidth && z[i]>=' ' ){ |
| 3355 | *pzTail = &z[i]; |
| 3356 | }else if( z[i]=='\r' && z[i+1]=='\n' ){ |
| 3357 | *pzTail = z[i+2] ? &z[i+2] : 0; |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3358 | }else if( z[i]==0 || z[i+1]==0 ){ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3359 | *pzTail = 0; |
| 3360 | }else{ |
| 3361 | *pzTail = &z[i+1]; |
| 3362 | } |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3363 | zOut = malloc( j+1 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3364 | shell_check_oom(zOut); |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3365 | i = j = n = 0; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3366 | while( i<k ){ |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3367 | if( z[i]>=' ' ){ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3368 | n++; |
| 3369 | do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 ); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3370 | continue; |
| 3371 | } |
| 3372 | if( z[i]=='\t' ){ |
| 3373 | do{ |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3374 | n++; |
| 3375 | zOut[j++] = ' '; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3376 | }while( (n&7)!=0 && n<mxWidth ); |
| 3377 | i++; |
| 3378 | continue; |
| 3379 | } |
| 3380 | break; |
| 3381 | } |
drh | b617219 | 2022-01-31 10:55:50 +0000 | [diff] [blame] | 3382 | zOut[j] = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3383 | return (char*)zOut; |
| 3384 | } |
| 3385 | |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3386 | /* Extract the value of the i-th current column for pStmt as an SQL literal |
| 3387 | ** value. Memory is obtained from sqlite3_malloc64() and must be freed by |
| 3388 | ** the caller. |
| 3389 | */ |
| 3390 | static char *quoted_column(sqlite3_stmt *pStmt, int i){ |
| 3391 | switch( sqlite3_column_type(pStmt, i) ){ |
| 3392 | case SQLITE_NULL: { |
| 3393 | return sqlite3_mprintf("NULL"); |
| 3394 | } |
| 3395 | case SQLITE_INTEGER: |
| 3396 | case SQLITE_FLOAT: { |
| 3397 | return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); |
| 3398 | } |
| 3399 | case SQLITE_TEXT: { |
| 3400 | return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i)); |
| 3401 | } |
| 3402 | case SQLITE_BLOB: { |
| 3403 | int j; |
| 3404 | sqlite3_str *pStr = sqlite3_str_new(0); |
| 3405 | const unsigned char *a = sqlite3_column_blob(pStmt,i); |
| 3406 | int n = sqlite3_column_bytes(pStmt,i); |
| 3407 | sqlite3_str_append(pStr, "x'", 2); |
| 3408 | for(j=0; j<n; j++){ |
| 3409 | sqlite3_str_appendf(pStr, "%02x", a[j]); |
| 3410 | } |
| 3411 | sqlite3_str_append(pStr, "'", 1); |
| 3412 | return sqlite3_str_finish(pStr); |
| 3413 | } |
| 3414 | } |
| 3415 | return 0; /* Not reached */ |
| 3416 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3417 | |
| 3418 | /* |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3419 | ** Run a prepared statement and output the result in one of the |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3420 | ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, |
| 3421 | ** or MODE_Box. |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3422 | ** |
| 3423 | ** This is different from ordinary exec_prepared_stmt() in that |
| 3424 | ** it has to run the entire query and gather the results into memory |
| 3425 | ** first, in order to determine column widths, before providing |
| 3426 | ** any output. |
| 3427 | */ |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3428 | static void exec_prepared_stmt_columnar( |
| 3429 | ShellState *p, /* Pointer to ShellState */ |
| 3430 | sqlite3_stmt *pStmt /* Statment to run */ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3431 | ){ |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3432 | sqlite3_int64 nRow = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3433 | int nColumn = 0; |
| 3434 | char **azData = 0; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3435 | sqlite3_int64 nAlloc = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3436 | char *abRowDiv = 0; |
| 3437 | const unsigned char *uz; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3438 | const char *z; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3439 | char **azQuoted = 0; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3440 | int rc; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3441 | sqlite3_int64 i, nData; |
| 3442 | int j, nTotal, w, n; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3443 | const char *colSep = 0; |
| 3444 | const char *rowSep = 0; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3445 | const unsigned char **azNextLine = 0; |
| 3446 | int bNextLine = 0; |
| 3447 | int bMultiLineRowExists = 0; |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 3448 | int bw = p->cmOpts.bWordWrap; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3449 | const char *zEmpty = ""; |
| 3450 | const char *zShowNull = p->nullValue; |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3451 | |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3452 | rc = sqlite3_step(pStmt); |
| 3453 | if( rc!=SQLITE_ROW ) return; |
| 3454 | nColumn = sqlite3_column_count(pStmt); |
| 3455 | nAlloc = nColumn*4; |
drh | 01a8ad2 | 2021-03-20 23:15:52 +0000 | [diff] [blame] | 3456 | if( nAlloc<=0 ) nAlloc = 1; |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3457 | azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3458 | shell_check_oom(azData); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3459 | azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3460 | shell_check_oom((void*)azNextLine); |
| 3461 | memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3462 | if( p->cmOpts.bQuote ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3463 | azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); |
| 3464 | shell_check_oom(azQuoted); |
| 3465 | memset(azQuoted, 0, nColumn*sizeof(char*) ); |
| 3466 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3467 | abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); |
| 3468 | shell_check_oom(abRowDiv); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3469 | if( nColumn>p->nWidth ){ |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 3470 | p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3471 | shell_check_oom(p->colWidth); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3472 | for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; |
| 3473 | p->nWidth = nColumn; |
| 3474 | p->actualWidth = &p->colWidth[nColumn]; |
| 3475 | } |
| 3476 | memset(p->actualWidth, 0, nColumn*sizeof(int)); |
| 3477 | for(i=0; i<nColumn; i++){ |
| 3478 | w = p->colWidth[i]; |
| 3479 | if( w<0 ) w = -w; |
| 3480 | p->actualWidth[i] = w; |
| 3481 | } |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3482 | for(i=0; i<nColumn; i++){ |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3483 | const unsigned char *zNotUsed; |
| 3484 | int wx = p->colWidth[i]; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3485 | if( wx==0 ){ |
| 3486 | wx = p->cmOpts.iWrap; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3487 | } |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3488 | if( wx<0 ) wx = -wx; |
| 3489 | uz = (const unsigned char*)sqlite3_column_name(pStmt,i); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3490 | azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3491 | } |
| 3492 | do{ |
| 3493 | int useNextLine = bNextLine; |
| 3494 | bNextLine = 0; |
| 3495 | if( (nRow+2)*nColumn >= nAlloc ){ |
| 3496 | nAlloc *= 2; |
| 3497 | azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); |
| 3498 | shell_check_oom(azData); |
| 3499 | abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); |
| 3500 | shell_check_oom(abRowDiv); |
| 3501 | } |
| 3502 | abRowDiv[nRow] = 1; |
| 3503 | nRow++; |
| 3504 | for(i=0; i<nColumn; i++){ |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3505 | int wx = p->colWidth[i]; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3506 | if( wx==0 ){ |
| 3507 | wx = p->cmOpts.iWrap; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3508 | } |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 3509 | if( wx<0 ) wx = -wx; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3510 | if( useNextLine ){ |
| 3511 | uz = azNextLine[i]; |
drh | 7e9a56f | 2022-04-21 19:14:23 +0000 | [diff] [blame] | 3512 | if( uz==0 ) uz = (u8*)zEmpty; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3513 | }else if( p->cmOpts.bQuote ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3514 | sqlite3_free(azQuoted[i]); |
| 3515 | azQuoted[i] = quoted_column(pStmt,i); |
| 3516 | uz = (const unsigned char*)azQuoted[i]; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3517 | }else{ |
| 3518 | uz = (const unsigned char*)sqlite3_column_text(pStmt,i); |
drh | 7e9a56f | 2022-04-21 19:14:23 +0000 | [diff] [blame] | 3519 | if( uz==0 ) uz = (u8*)zShowNull; |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3520 | } |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 3521 | azData[nRow*nColumn + i] |
| 3522 | = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3523 | if( azNextLine[i] ){ |
| 3524 | bNextLine = 1; |
| 3525 | abRowDiv[nRow-1] = 0; |
| 3526 | bMultiLineRowExists = 1; |
| 3527 | } |
| 3528 | } |
| 3529 | }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3530 | nTotal = nColumn*(nRow+1); |
| 3531 | for(i=0; i<nTotal; i++){ |
| 3532 | z = azData[i]; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3533 | if( z==0 ) z = (char*)zEmpty; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3534 | n = strlenChar(z); |
| 3535 | j = i%nColumn; |
| 3536 | if( n>p->actualWidth[j] ) p->actualWidth[j] = n; |
| 3537 | } |
drh | 9994298 | 2020-06-15 20:05:37 +0000 | [diff] [blame] | 3538 | if( seenInterrupt ) goto columnar_end; |
drh | 01a8ad2 | 2021-03-20 23:15:52 +0000 | [diff] [blame] | 3539 | if( nColumn==0 ) goto columnar_end; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3540 | switch( p->cMode ){ |
| 3541 | case MODE_Column: { |
| 3542 | colSep = " "; |
| 3543 | rowSep = "\n"; |
| 3544 | if( p->showHeader ){ |
| 3545 | for(i=0; i<nColumn; i++){ |
| 3546 | w = p->actualWidth[i]; |
| 3547 | if( p->colWidth[i]<0 ) w = -w; |
| 3548 | utf8_width_print(p->out, w, azData[i]); |
| 3549 | fputs(i==nColumn-1?"\n":" ", p->out); |
| 3550 | } |
| 3551 | for(i=0; i<nColumn; i++){ |
| 3552 | print_dashes(p->out, p->actualWidth[i]); |
| 3553 | fputs(i==nColumn-1?"\n":" ", p->out); |
| 3554 | } |
| 3555 | } |
| 3556 | break; |
| 3557 | } |
| 3558 | case MODE_Table: { |
| 3559 | colSep = " | "; |
| 3560 | rowSep = " |\n"; |
| 3561 | print_row_separator(p, nColumn, "+"); |
| 3562 | fputs("| ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3563 | for(i=0; i<nColumn; i++){ |
| 3564 | w = p->actualWidth[i]; |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3565 | n = strlenChar(azData[i]); |
| 3566 | utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
| 3567 | fputs(i==nColumn-1?" |\n":" | ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3568 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3569 | print_row_separator(p, nColumn, "+"); |
| 3570 | break; |
| 3571 | } |
| 3572 | case MODE_Markdown: { |
| 3573 | colSep = " | "; |
| 3574 | rowSep = " |\n"; |
| 3575 | fputs("| ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3576 | for(i=0; i<nColumn; i++){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3577 | w = p->actualWidth[i]; |
| 3578 | n = strlenChar(azData[i]); |
| 3579 | utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
| 3580 | fputs(i==nColumn-1?" |\n":" | ", p->out); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3581 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3582 | print_row_separator(p, nColumn, "|"); |
| 3583 | break; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3584 | } |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3585 | case MODE_Box: { |
| 3586 | colSep = " " BOX_13 " "; |
| 3587 | rowSep = " " BOX_13 "\n"; |
| 3588 | print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); |
| 3589 | utf8_printf(p->out, BOX_13 " "); |
| 3590 | for(i=0; i<nColumn; i++){ |
| 3591 | w = p->actualWidth[i]; |
| 3592 | n = strlenChar(azData[i]); |
| 3593 | utf8_printf(p->out, "%*s%s%*s%s", |
| 3594 | (w-n)/2, "", azData[i], (w-n+1)/2, "", |
| 3595 | i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); |
| 3596 | } |
| 3597 | print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); |
| 3598 | break; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3599 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3600 | } |
| 3601 | for(i=nColumn, j=0; i<nTotal; i++, j++){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3602 | if( j==0 && p->cMode!=MODE_Column ){ |
| 3603 | utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); |
| 3604 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3605 | z = azData[i]; |
| 3606 | if( z==0 ) z = p->nullValue; |
| 3607 | w = p->actualWidth[j]; |
| 3608 | if( p->colWidth[j]<0 ) w = -w; |
| 3609 | utf8_width_print(p->out, w, z); |
| 3610 | if( j==nColumn-1 ){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3611 | utf8_printf(p->out, "%s", rowSep); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3612 | if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ |
| 3613 | if( p->cMode==MODE_Table ){ |
| 3614 | print_row_separator(p, nColumn, "+"); |
| 3615 | }else if( p->cMode==MODE_Box ){ |
| 3616 | print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); |
drh | 5aabdae | 2022-02-01 00:00:08 +0000 | [diff] [blame] | 3617 | }else if( p->cMode==MODE_Column ){ |
| 3618 | raw_printf(p->out, "\n"); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3619 | } |
| 3620 | } |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3621 | j = -1; |
drh | dd853c3 | 2020-06-16 17:34:40 +0000 | [diff] [blame] | 3622 | if( seenInterrupt ) goto columnar_end; |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3623 | }else{ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3624 | utf8_printf(p->out, "%s", colSep); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | if( p->cMode==MODE_Table ){ |
| 3628 | print_row_separator(p, nColumn, "+"); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3629 | }else if( p->cMode==MODE_Box ){ |
| 3630 | print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3631 | } |
drh | 9994298 | 2020-06-15 20:05:37 +0000 | [diff] [blame] | 3632 | columnar_end: |
drh | dd853c3 | 2020-06-16 17:34:40 +0000 | [diff] [blame] | 3633 | if( seenInterrupt ){ |
| 3634 | utf8_printf(p->out, "Interrupt\n"); |
| 3635 | } |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3636 | nData = (nRow+1)*nColumn; |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3637 | for(i=0; i<nData; i++){ |
| 3638 | z = azData[i]; |
drh | 744c17c | 2022-05-05 10:02:19 +0000 | [diff] [blame] | 3639 | if( z!=zEmpty && z!=zShowNull ) free(azData[i]); |
larrybr | 6403e77 | 2022-04-20 22:41:10 +0000 | [diff] [blame] | 3640 | } |
drh | f82ce38 | 2020-08-06 16:45:22 +0000 | [diff] [blame] | 3641 | sqlite3_free(azData); |
drh | 5dce6f9 | 2022-01-31 16:29:06 +0000 | [diff] [blame] | 3642 | sqlite3_free((void*)azNextLine); |
drh | 09a39ed | 2022-01-30 21:09:03 +0000 | [diff] [blame] | 3643 | sqlite3_free(abRowDiv); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 3644 | if( azQuoted ){ |
| 3645 | for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]); |
| 3646 | sqlite3_free(azQuoted); |
| 3647 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3648 | } |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3649 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3650 | /* |
| 3651 | ** Run a prepared statement |
| 3652 | */ |
| 3653 | static void exec_prepared_stmt( |
| 3654 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3655 | sqlite3_stmt *pStmt /* Statment to run */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3656 | ){ |
| 3657 | int rc; |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3658 | sqlite3_uint64 nRow = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3659 | |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3660 | if( pArg->cMode==MODE_Column |
| 3661 | || pArg->cMode==MODE_Table |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3662 | || pArg->cMode==MODE_Box |
drh | 8c74863 | 2020-05-29 16:15:58 +0000 | [diff] [blame] | 3663 | || pArg->cMode==MODE_Markdown |
| 3664 | ){ |
| 3665 | exec_prepared_stmt_columnar(pArg, pStmt); |
| 3666 | return; |
| 3667 | } |
| 3668 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3669 | /* perform the first step. this will tell us if we |
| 3670 | ** have a result set or not and how wide it is. |
| 3671 | */ |
| 3672 | rc = sqlite3_step(pStmt); |
| 3673 | /* if we have a result set... */ |
| 3674 | if( SQLITE_ROW == rc ){ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3675 | /* allocate space for col name ptr, value ptr, and type */ |
| 3676 | int nCol = sqlite3_column_count(pStmt); |
| 3677 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 3678 | if( !pData ){ |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 3679 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3680 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3681 | char **azCols = (char **)pData; /* Names of result columns */ |
| 3682 | char **azVals = &azCols[nCol]; /* Results */ |
| 3683 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 3684 | int i, x; |
| 3685 | assert(sizeof(int) <= sizeof(char *)); |
| 3686 | /* save off ptrs to column names */ |
| 3687 | for(i=0; i<nCol; i++){ |
| 3688 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 3689 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3690 | do{ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3691 | nRow++; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3692 | /* extract the data and data types */ |
| 3693 | for(i=0; i<nCol; i++){ |
| 3694 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
drh | 5d1bf4f | 2022-01-02 20:54:33 +0000 | [diff] [blame] | 3695 | if( x==SQLITE_BLOB |
| 3696 | && pArg |
| 3697 | && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) |
| 3698 | ){ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3699 | azVals[i] = ""; |
| 3700 | }else{ |
| 3701 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 3702 | } |
| 3703 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 3704 | rc = SQLITE_NOMEM; |
| 3705 | break; /* from for */ |
| 3706 | } |
| 3707 | } /* end for */ |
| 3708 | |
| 3709 | /* if data and types extracted successfully... */ |
| 3710 | if( SQLITE_ROW == rc ){ |
| 3711 | /* call the supplied callback with the result row data */ |
| 3712 | if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 3713 | rc = SQLITE_ABORT; |
| 3714 | }else{ |
| 3715 | rc = sqlite3_step(pStmt); |
| 3716 | } |
| 3717 | } |
| 3718 | } while( SQLITE_ROW == rc ); |
| 3719 | sqlite3_free(pData); |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 3720 | if( pArg->cMode==MODE_Json ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3721 | fputs("]\n", pArg->out); |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 3722 | }else if( pArg->cMode==MODE_Count ){ |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 3723 | char zBuf[200]; |
| 3724 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n", |
| 3725 | nRow, nRow!=1 ? "s" : ""); |
| 3726 | printf("%s", zBuf); |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 3727 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | } |
| 3731 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3732 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3733 | /* |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3734 | ** This function is called to process SQL if the previous shell command |
| 3735 | ** was ".expert". It passes the SQL in the second argument directly to |
| 3736 | ** the sqlite3expert object. |
| 3737 | ** |
| 3738 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 3739 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 3740 | ** an English language error message. It is the responsibility of the |
| 3741 | ** caller to eventually free this buffer using sqlite3_free(). |
| 3742 | */ |
| 3743 | static int expertHandleSQL( |
| 3744 | ShellState *pState, |
| 3745 | const char *zSql, |
| 3746 | char **pzErr |
| 3747 | ){ |
| 3748 | assert( pState->expert.pExpert ); |
| 3749 | assert( pzErr==0 || *pzErr==0 ); |
| 3750 | return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); |
| 3751 | } |
| 3752 | |
| 3753 | /* |
| 3754 | ** This function is called either to silently clean up the object |
| 3755 | ** created by the ".expert" command (if bCancel==1), or to generate a |
| 3756 | ** report from it and then clean it up (if bCancel==0). |
| 3757 | ** |
| 3758 | ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 3759 | ** code. In this case, (*pzErr) may be set to point to a buffer containing |
| 3760 | ** an English language error message. It is the responsibility of the |
| 3761 | ** caller to eventually free this buffer using sqlite3_free(). |
| 3762 | */ |
| 3763 | static int expertFinish( |
| 3764 | ShellState *pState, |
| 3765 | int bCancel, |
| 3766 | char **pzErr |
| 3767 | ){ |
| 3768 | int rc = SQLITE_OK; |
| 3769 | sqlite3expert *p = pState->expert.pExpert; |
| 3770 | assert( p ); |
| 3771 | assert( bCancel || pzErr==0 || *pzErr==0 ); |
| 3772 | if( bCancel==0 ){ |
| 3773 | FILE *out = pState->out; |
| 3774 | int bVerbose = pState->expert.bVerbose; |
| 3775 | |
| 3776 | rc = sqlite3_expert_analyze(p, pzErr); |
| 3777 | if( rc==SQLITE_OK ){ |
| 3778 | int nQuery = sqlite3_expert_count(p); |
| 3779 | int i; |
| 3780 | |
| 3781 | if( bVerbose ){ |
| 3782 | const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
| 3783 | raw_printf(out, "-- Candidates -----------------------------\n"); |
| 3784 | raw_printf(out, "%s\n", zCand); |
| 3785 | } |
| 3786 | for(i=0; i<nQuery; i++){ |
| 3787 | const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
| 3788 | const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
| 3789 | const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
| 3790 | if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
| 3791 | if( bVerbose ){ |
| 3792 | raw_printf(out, "-- Query %d --------------------------------\n",i+1); |
| 3793 | raw_printf(out, "%s\n\n", zSql); |
| 3794 | } |
| 3795 | raw_printf(out, "%s\n", zIdx); |
| 3796 | raw_printf(out, "%s\n", zEQP); |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | sqlite3_expert_destroy(p); |
| 3801 | pState->expert.pExpert = 0; |
| 3802 | return rc; |
| 3803 | } |
| 3804 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3805 | /* |
| 3806 | ** Implementation of ".expert" dot command. |
| 3807 | */ |
| 3808 | static int expertDotCommand( |
| 3809 | ShellState *pState, /* Current shell tool state */ |
| 3810 | char **azArg, /* Array of arguments passed to dot command */ |
| 3811 | int nArg /* Number of entries in azArg[] */ |
| 3812 | ){ |
| 3813 | int rc = SQLITE_OK; |
| 3814 | char *zErr = 0; |
| 3815 | int i; |
| 3816 | int iSample = 0; |
| 3817 | |
| 3818 | assert( pState->expert.pExpert==0 ); |
| 3819 | memset(&pState->expert, 0, sizeof(ExpertInfo)); |
| 3820 | |
| 3821 | for(i=1; rc==SQLITE_OK && i<nArg; i++){ |
| 3822 | char *z = azArg[i]; |
| 3823 | int n; |
| 3824 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 3825 | n = strlen30(z); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 3826 | if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){ |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3827 | pState->expert.bVerbose = 1; |
| 3828 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 3829 | else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){ |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3830 | if( i==(nArg-1) ){ |
| 3831 | raw_printf(stderr, "option requires an argument: %s\n", z); |
| 3832 | rc = SQLITE_ERROR; |
| 3833 | }else{ |
| 3834 | iSample = (int)integerValue(azArg[++i]); |
| 3835 | if( iSample<0 || iSample>100 ){ |
| 3836 | raw_printf(stderr, "value out of range: %s\n", azArg[i]); |
| 3837 | rc = SQLITE_ERROR; |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | else{ |
| 3842 | raw_printf(stderr, "unknown option: %s\n", z); |
| 3843 | rc = SQLITE_ERROR; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | if( rc==SQLITE_OK ){ |
| 3848 | pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); |
| 3849 | if( pState->expert.pExpert==0 ){ |
drh | e0adf60 | 2021-12-16 14:26:16 +0000 | [diff] [blame] | 3850 | raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory"); |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3851 | rc = SQLITE_ERROR; |
| 3852 | }else{ |
| 3853 | sqlite3_expert_config( |
| 3854 | pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample |
| 3855 | ); |
| 3856 | } |
| 3857 | } |
drh | e0adf60 | 2021-12-16 14:26:16 +0000 | [diff] [blame] | 3858 | sqlite3_free(zErr); |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3859 | |
| 3860 | return rc; |
| 3861 | } |
| 3862 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3863 | |
| 3864 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3865 | ** Execute a statement or set of statements. Print |
| 3866 | ** any result rows/columns depending on the current mode |
| 3867 | ** set via the supplied callback. |
| 3868 | ** |
| 3869 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 3870 | ** function except it takes a slightly different callback |
| 3871 | ** and callback data argument. |
| 3872 | */ |
| 3873 | static int shell_exec( |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3874 | ShellState *pArg, /* Pointer to ShellState */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3875 | const char *zSql, /* SQL to be evaluated */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3876 | char **pzErrMsg /* Error msg written here */ |
| 3877 | ){ |
| 3878 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 3879 | int rc = SQLITE_OK; /* Return Code */ |
| 3880 | int rc2; |
| 3881 | const char *zLeftover; /* Tail of unprocessed SQL */ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3882 | sqlite3 *db = pArg->db; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3883 | |
| 3884 | if( pzErrMsg ){ |
| 3885 | *pzErrMsg = NULL; |
| 3886 | } |
| 3887 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3888 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3889 | if( pArg->expert.pExpert ){ |
| 3890 | rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
| 3891 | return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
| 3892 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 3893 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 3894 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3895 | while( zSql[0] && (SQLITE_OK == rc) ){ |
| 3896 | static const char *zStmtSql; |
| 3897 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 3898 | if( SQLITE_OK != rc ){ |
| 3899 | if( pzErrMsg ){ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 3900 | *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3901 | } |
| 3902 | }else{ |
| 3903 | if( !pStmt ){ |
| 3904 | /* this happens for a comment or white-space */ |
| 3905 | zSql = zLeftover; |
| 3906 | while( IsSpace(zSql[0]) ) zSql++; |
| 3907 | continue; |
| 3908 | } |
| 3909 | zStmtSql = sqlite3_sql(pStmt); |
| 3910 | if( zStmtSql==0 ) zStmtSql = ""; |
| 3911 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 3912 | |
| 3913 | /* save off the prepared statment handle and reset row count */ |
| 3914 | if( pArg ){ |
| 3915 | pArg->pStmt = pStmt; |
| 3916 | pArg->cnt = 0; |
| 3917 | } |
| 3918 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3919 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3920 | if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3921 | sqlite3_stmt *pExplain; |
| 3922 | char *zEQP; |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3923 | int triggerEQP = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3924 | disable_debug_trace_modes(); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3925 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
| 3926 | if( pArg->autoEQP>=AUTOEQP_trigger ){ |
| 3927 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); |
| 3928 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3929 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3930 | shell_check_oom(zEQP); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3931 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3932 | if( rc==SQLITE_OK ){ |
| 3933 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3934 | const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3935 | int iEqpId = sqlite3_column_int(pExplain, 0); |
| 3936 | int iParentId = sqlite3_column_int(pExplain, 1); |
drh | 7e088a6 | 2020-05-02 00:01:39 +0000 | [diff] [blame] | 3937 | if( zEQPLine==0 ) zEQPLine = ""; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3938 | if( zEQPLine[0]=='-' ) eqp_render(pArg); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3939 | eqp_append(pArg, iEqpId, iParentId, zEQPLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3940 | } |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3941 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3942 | } |
| 3943 | sqlite3_finalize(pExplain); |
| 3944 | sqlite3_free(zEQP); |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 3945 | if( pArg->autoEQP>=AUTOEQP_full ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3946 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 3947 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 3948 | shell_check_oom(zEQP); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3949 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3950 | if( rc==SQLITE_OK ){ |
| 3951 | pArg->cMode = MODE_Explain; |
| 3952 | explain_data_prepare(pArg, pExplain); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3953 | exec_prepared_stmt(pArg, pExplain); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3954 | explain_data_delete(pArg); |
| 3955 | } |
| 3956 | sqlite3_finalize(pExplain); |
| 3957 | sqlite3_free(zEQP); |
| 3958 | } |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 3959 | if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ |
| 3960 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); |
| 3961 | /* Reprepare pStmt before reactiving trace modes */ |
| 3962 | sqlite3_finalize(pStmt); |
| 3963 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
drh | 3c49eaf | 2018-06-07 15:23:43 +0000 | [diff] [blame] | 3964 | if( pArg ) pArg->pStmt = pStmt; |
drh | 51efe09 | 2018-03-20 12:04:38 +0000 | [diff] [blame] | 3965 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3966 | restore_debug_trace_modes(); |
| 3967 | } |
| 3968 | |
| 3969 | if( pArg ){ |
| 3970 | pArg->cMode = pArg->mode; |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3971 | if( pArg->autoExplain ){ |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3972 | if( sqlite3_stmt_isexplain(pStmt)==1 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3973 | pArg->cMode = MODE_Explain; |
| 3974 | } |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 3975 | if( sqlite3_stmt_isexplain(pStmt)==2 ){ |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3976 | pArg->cMode = MODE_EQP; |
| 3977 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | /* If the shell is currently in ".explain" mode, gather the extra |
| 3981 | ** data required to add indents to the output.*/ |
| 3982 | if( pArg->cMode==MODE_Explain ){ |
| 3983 | explain_data_prepare(pArg, pStmt); |
| 3984 | } |
| 3985 | } |
| 3986 | |
drh | 8b738d0 | 2019-02-25 18:43:54 +0000 | [diff] [blame] | 3987 | bind_prepared_stmt(pArg, pStmt); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 3988 | exec_prepared_stmt(pArg, pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3989 | explain_data_delete(pArg); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 3990 | eqp_render(pArg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 3991 | |
| 3992 | /* print usage stats if stats on */ |
| 3993 | if( pArg && pArg->statsOn ){ |
| 3994 | display_stats(db, pArg, 0); |
| 3995 | } |
| 3996 | |
| 3997 | /* print loop-counters if required */ |
| 3998 | if( pArg && pArg->scanstatsOn ){ |
| 3999 | display_scanstats(db, pArg); |
| 4000 | } |
| 4001 | |
| 4002 | /* Finalize the statement just executed. If this fails, save a |
| 4003 | ** copy of the error message. Otherwise, set zSql to point to the |
| 4004 | ** next statement to execute. */ |
| 4005 | rc2 = sqlite3_finalize(pStmt); |
| 4006 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
| 4007 | if( rc==SQLITE_OK ){ |
| 4008 | zSql = zLeftover; |
| 4009 | while( IsSpace(zSql[0]) ) zSql++; |
| 4010 | }else if( pzErrMsg ){ |
drh | 633c798 | 2022-02-08 12:13:16 +0000 | [diff] [blame] | 4011 | *pzErrMsg = save_err_msg(db, "stepping", rc, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | /* clear saved stmt handle */ |
| 4015 | if( pArg ){ |
| 4016 | pArg->pStmt = NULL; |
| 4017 | } |
| 4018 | } |
| 4019 | } /* end while */ |
| 4020 | |
| 4021 | return rc; |
| 4022 | } |
| 4023 | |
| 4024 | /* |
| 4025 | ** Release memory previously allocated by tableColumnList(). |
| 4026 | */ |
| 4027 | static void freeColumnList(char **azCol){ |
| 4028 | int i; |
| 4029 | for(i=1; azCol[i]; i++){ |
| 4030 | sqlite3_free(azCol[i]); |
| 4031 | } |
| 4032 | /* azCol[0] is a static string */ |
| 4033 | sqlite3_free(azCol); |
| 4034 | } |
| 4035 | |
| 4036 | /* |
| 4037 | ** Return a list of pointers to strings which are the names of all |
| 4038 | ** columns in table zTab. The memory to hold the names is dynamically |
| 4039 | ** allocated and must be released by the caller using a subsequent call |
| 4040 | ** to freeColumnList(). |
| 4041 | ** |
| 4042 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 4043 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 4044 | ** name of the rowid column. |
| 4045 | ** |
| 4046 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 4047 | ** by an entry with azCol[i]==0. |
| 4048 | */ |
| 4049 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 4050 | char **azCol = 0; |
| 4051 | sqlite3_stmt *pStmt; |
| 4052 | char *zSql; |
| 4053 | int nCol = 0; |
| 4054 | int nAlloc = 0; |
| 4055 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 4056 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 4057 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 4058 | int rc; |
| 4059 | |
| 4060 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4061 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4062 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4063 | sqlite3_free(zSql); |
| 4064 | if( rc ) return 0; |
| 4065 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4066 | if( nCol>=nAlloc-2 ){ |
| 4067 | nAlloc = nAlloc*2 + nCol + 10; |
| 4068 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4069 | shell_check_oom(azCol); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4070 | } |
| 4071 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4072 | shell_check_oom(azCol[nCol]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4073 | if( sqlite3_column_int(pStmt, 5) ){ |
| 4074 | nPK++; |
| 4075 | if( nPK==1 |
| 4076 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 4077 | "INTEGER")==0 |
| 4078 | ){ |
| 4079 | isIPK = 1; |
| 4080 | }else{ |
| 4081 | isIPK = 0; |
| 4082 | } |
| 4083 | } |
| 4084 | } |
| 4085 | sqlite3_finalize(pStmt); |
drh | 4c6cddc | 2017-10-12 10:28:30 +0000 | [diff] [blame] | 4086 | if( azCol==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4087 | azCol[0] = 0; |
| 4088 | azCol[nCol+1] = 0; |
| 4089 | |
| 4090 | /* The decision of whether or not a rowid really needs to be preserved |
| 4091 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 4092 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 4093 | ** rowids on tables where the rowid is inaccessible because there are other |
| 4094 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 4095 | */ |
| 4096 | if( preserveRowid && isIPK ){ |
| 4097 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 4098 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 4099 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 4100 | ** ROWID aliases. To distinguish these cases, check to see if |
| 4101 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 4102 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 4103 | */ |
| 4104 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 4105 | " WHERE origin='pk'", zTab); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4106 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4107 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4108 | sqlite3_free(zSql); |
| 4109 | if( rc ){ |
| 4110 | freeColumnList(azCol); |
| 4111 | return 0; |
| 4112 | } |
| 4113 | rc = sqlite3_step(pStmt); |
| 4114 | sqlite3_finalize(pStmt); |
| 4115 | preserveRowid = rc==SQLITE_ROW; |
| 4116 | } |
| 4117 | if( preserveRowid ){ |
| 4118 | /* Only preserve the rowid if we can find a name to use for the |
| 4119 | ** rowid */ |
| 4120 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 4121 | int i, j; |
| 4122 | for(j=0; j<3; j++){ |
| 4123 | for(i=1; i<=nCol; i++){ |
| 4124 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 4125 | } |
| 4126 | if( i>nCol ){ |
| 4127 | /* At this point, we know that azRowid[j] is not the name of any |
| 4128 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 4129 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 4130 | ** tables will fail this last check */ |
| 4131 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 4132 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 4133 | break; |
| 4134 | } |
| 4135 | } |
| 4136 | } |
| 4137 | return azCol; |
| 4138 | } |
| 4139 | |
| 4140 | /* |
| 4141 | ** Toggle the reverse_unordered_selects setting. |
| 4142 | */ |
| 4143 | static void toggleSelectOrder(sqlite3 *db){ |
| 4144 | sqlite3_stmt *pStmt = 0; |
| 4145 | int iSetting = 0; |
| 4146 | char zStmt[100]; |
| 4147 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 4148 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4149 | iSetting = sqlite3_column_int(pStmt, 0); |
| 4150 | } |
| 4151 | sqlite3_finalize(pStmt); |
| 4152 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 4153 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 4154 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 4155 | } |
| 4156 | |
| 4157 | /* |
| 4158 | ** This is a different callback routine used for dumping the database. |
| 4159 | ** Each row received by this callback consists of a table name, |
| 4160 | ** the table type ("index" or "table") and SQL to create the table. |
| 4161 | ** This routine should print text sufficient to recreate the table. |
| 4162 | */ |
| 4163 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
| 4164 | int rc; |
| 4165 | const char *zTable; |
| 4166 | const char *zType; |
| 4167 | const char *zSql; |
| 4168 | ShellState *p = (ShellState *)pArg; |
mistachkin | a00a016 | 2020-10-18 18:35:34 +0000 | [diff] [blame] | 4169 | int dataOnly; |
| 4170 | int noSys; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4171 | |
| 4172 | UNUSED_PARAMETER(azNotUsed); |
drh | b3c4523 | 2017-08-28 14:33:27 +0000 | [diff] [blame] | 4173 | if( nArg!=3 || azArg==0 ) return 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4174 | zTable = azArg[0]; |
| 4175 | zType = azArg[1]; |
| 4176 | zSql = azArg[2]; |
drh | d5ca2c4 | 2022-10-25 13:42:10 +0000 | [diff] [blame] | 4177 | if( zTable==0 ) return 0; |
| 4178 | if( zType==0 ) return 0; |
mistachkin | a00a016 | 2020-10-18 18:35:34 +0000 | [diff] [blame] | 4179 | dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; |
| 4180 | noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4181 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4182 | if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){ |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4183 | if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
| 4184 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){ |
| 4185 | if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4186 | }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4187 | return 0; |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4188 | }else if( dataOnly ){ |
| 4189 | /* no-op */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4190 | }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4191 | char *zIns; |
| 4192 | if( !p->writableSchema ){ |
| 4193 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 4194 | p->writableSchema = 1; |
| 4195 | } |
| 4196 | zIns = sqlite3_mprintf( |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4197 | "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4198 | "VALUES('table','%q','%q',0,'%q');", |
| 4199 | zTable, zTable, zSql); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4200 | shell_check_oom(zIns); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4201 | utf8_printf(p->out, "%s\n", zIns); |
| 4202 | sqlite3_free(zIns); |
| 4203 | return 0; |
| 4204 | }else{ |
| 4205 | printSchemaLine(p->out, zSql, ";\n"); |
| 4206 | } |
| 4207 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4208 | if( cli_strcmp(zType, "table")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4209 | ShellText sSelect; |
| 4210 | ShellText sTable; |
| 4211 | char **azCol; |
| 4212 | int i; |
| 4213 | char *savedDestTable; |
| 4214 | int savedMode; |
| 4215 | |
| 4216 | azCol = tableColumnList(p, zTable); |
| 4217 | if( azCol==0 ){ |
| 4218 | p->nErr++; |
| 4219 | return 0; |
| 4220 | } |
| 4221 | |
| 4222 | /* Always quote the table name, even if it appears to be pure ascii, |
| 4223 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 4224 | initText(&sTable); |
| 4225 | appendText(&sTable, zTable, quoteChar(zTable)); |
| 4226 | /* If preserving the rowid, add a column list after the table name. |
| 4227 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 4228 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 4229 | */ |
| 4230 | if( azCol[0] ){ |
| 4231 | appendText(&sTable, "(", 0); |
| 4232 | appendText(&sTable, azCol[0], 0); |
| 4233 | for(i=1; azCol[i]; i++){ |
| 4234 | appendText(&sTable, ",", 0); |
| 4235 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
| 4236 | } |
| 4237 | appendText(&sTable, ")", 0); |
| 4238 | } |
| 4239 | |
| 4240 | /* Build an appropriate SELECT statement */ |
| 4241 | initText(&sSelect); |
| 4242 | appendText(&sSelect, "SELECT ", 0); |
| 4243 | if( azCol[0] ){ |
| 4244 | appendText(&sSelect, azCol[0], 0); |
| 4245 | appendText(&sSelect, ",", 0); |
| 4246 | } |
| 4247 | for(i=1; azCol[i]; i++){ |
| 4248 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
| 4249 | if( azCol[i+1] ){ |
| 4250 | appendText(&sSelect, ",", 0); |
| 4251 | } |
| 4252 | } |
| 4253 | freeColumnList(azCol); |
| 4254 | appendText(&sSelect, " FROM ", 0); |
| 4255 | appendText(&sSelect, zTable, quoteChar(zTable)); |
| 4256 | |
| 4257 | savedDestTable = p->zDestTable; |
| 4258 | savedMode = p->mode; |
| 4259 | p->zDestTable = sTable.z; |
| 4260 | p->mode = p->cMode = MODE_Insert; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 4261 | rc = shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4262 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 4263 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 4264 | toggleSelectOrder(p->db); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 4265 | shell_exec(p, sSelect.z, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4266 | toggleSelectOrder(p->db); |
| 4267 | } |
| 4268 | p->zDestTable = savedDestTable; |
| 4269 | p->mode = savedMode; |
| 4270 | freeText(&sTable); |
| 4271 | freeText(&sSelect); |
| 4272 | if( rc ) p->nErr++; |
| 4273 | } |
| 4274 | return 0; |
| 4275 | } |
| 4276 | |
| 4277 | /* |
| 4278 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 4279 | ** the contents of the query are output as SQL statements. |
| 4280 | ** |
| 4281 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 4282 | ** "ORDER BY rowid DESC" to the end. |
| 4283 | */ |
| 4284 | static int run_schema_dump_query( |
| 4285 | ShellState *p, |
| 4286 | const char *zQuery |
| 4287 | ){ |
| 4288 | int rc; |
| 4289 | char *zErr = 0; |
| 4290 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
| 4291 | if( rc==SQLITE_CORRUPT ){ |
| 4292 | char *zQ2; |
| 4293 | int len = strlen30(zQuery); |
| 4294 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 4295 | if( zErr ){ |
| 4296 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
| 4297 | sqlite3_free(zErr); |
| 4298 | zErr = 0; |
| 4299 | } |
| 4300 | zQ2 = malloc( len+100 ); |
| 4301 | if( zQ2==0 ) return rc; |
| 4302 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
| 4303 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 4304 | if( rc ){ |
| 4305 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
| 4306 | }else{ |
| 4307 | rc = SQLITE_CORRUPT; |
| 4308 | } |
| 4309 | sqlite3_free(zErr); |
| 4310 | free(zQ2); |
| 4311 | } |
| 4312 | return rc; |
| 4313 | } |
| 4314 | |
| 4315 | /* |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4316 | ** Text of help messages. |
| 4317 | ** |
| 4318 | ** The help text for each individual command begins with a line that starts |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4319 | ** with ".". Subsequent lines are supplemental information. |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4320 | ** |
| 4321 | ** There must be two or more spaces between the end of the command and the |
| 4322 | ** start of the description of what that command does. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4323 | */ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4324 | static const char *(azHelp[]) = { |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4325 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \ |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4326 | && !defined(SQLITE_SHELL_FIDDLE) |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4327 | ".archive ... Manage SQL archives", |
| 4328 | " Each command must have exactly one of the following options:", |
| 4329 | " -c, --create Create a new archive", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4330 | " -u, --update Add or update files with changed mtime", |
| 4331 | " -i, --insert Like -u but always add even if unchanged", |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 4332 | " -r, --remove Remove files from archive", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4333 | " -t, --list List contents of archive", |
| 4334 | " -x, --extract Extract files from archive", |
| 4335 | " Optional arguments:", |
| 4336 | " -v, --verbose Print each filename as it is processed", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4337 | " -f FILE, --file FILE Use archive FILE (default is current db)", |
| 4338 | " -a FILE, --append FILE Open FILE using the apndvfs VFS", |
| 4339 | " -C DIR, --directory DIR Read/extract files from directory DIR", |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 4340 | " -g, --glob Use glob matching for names in archive", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4341 | " -n, --dryrun Show the SQL that would have occurred", |
| 4342 | " Examples:", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4343 | " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", |
| 4344 | " .ar -tf ARCHIVE # List members of ARCHIVE", |
| 4345 | " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4346 | " See also:", |
larrybr | bd0d62c | 2021-06-13 08:23:28 +0000 | [diff] [blame] | 4347 | " http://sqlite.org/cli.html#sqlite_archive_support", |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 4348 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4349 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4350 | ".auth ON|OFF Show authorizer callbacks", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4351 | #endif |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4352 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4353 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE", |
larrybr | a7919ad | 2022-02-19 21:25:48 +0000 | [diff] [blame] | 4354 | " Options:", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4355 | " --append Use the appendvfs", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4356 | " --async Write to FILE without journal and fsync()", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4357 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4358 | ".bail on|off Stop after hitting an error. Default OFF", |
| 4359 | ".binary on|off Turn binary output on or off. Default OFF", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4360 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4361 | ".cd DIRECTORY Change the working directory to DIRECTORY", |
stephan | 1c0dcec | 2022-05-19 21:56:50 +0000 | [diff] [blame] | 4362 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4363 | ".changes on|off Show number of rows changed by SQL", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4364 | #ifndef SQLITE_SHELL_FIDDLE |
stephan | e26d162 | 2022-05-19 22:04:23 +0000 | [diff] [blame] | 4365 | ".check GLOB Fail if output since .testcase does not match", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4366 | ".clone NEWDB Clone data into NEWDB from the existing database", |
stephan | 1c0dcec | 2022-05-19 21:56:50 +0000 | [diff] [blame] | 4367 | #endif |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4368 | ".connection [close] [#] Open or close an auxiliary database connection", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4369 | ".databases List names and files of attached databases", |
| 4370 | ".dbconfig ?op? ?val? List or change sqlite3_db_config() options", |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 4371 | #if SQLITE_SHELL_HAVE_RECOVER |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4372 | ".dbinfo ?DB? Show status information about the database", |
larrybr | f3d6e8f | 2022-06-05 22:58:40 +0000 | [diff] [blame] | 4373 | #endif |
larrybr | 7bdbe59 | 2021-03-15 12:56:00 +0000 | [diff] [blame] | 4374 | ".dump ?OBJECTS? Render database content as SQL", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4375 | " Options:", |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4376 | " --data-only Output only INSERT statements", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4377 | " --newlines Allow unescaped newline characters in output", |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 4378 | " --nosys Omit system tables (ex: \"sqlite_stat1\")", |
| 4379 | " --preserve-rowids Include ROWID values in the output", |
larrybr | 7bdbe59 | 2021-03-15 12:56:00 +0000 | [diff] [blame] | 4380 | " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump", |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 4381 | " Additional LIKE patterns can be given in subsequent arguments", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4382 | ".echo on|off Turn command echo on or off", |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 4383 | ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", |
| 4384 | " Other Modes:", |
| 4385 | #ifdef SQLITE_DEBUG |
| 4386 | " test Show raw EXPLAIN QUERY PLAN output", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4387 | " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 4388 | #endif |
| 4389 | " trigger Like \"full\" but also show trigger bytecode", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4390 | #ifndef SQLITE_SHELL_FIDDLE |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4391 | ".excel Display the output of next command in spreadsheet", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4392 | " --bom Put a UTF8 byte-order mark on intermediate file", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4393 | #endif |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4394 | #ifndef SQLITE_SHELL_FIDDLE |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4395 | ".exit ?CODE? Exit this program with return-code CODE", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4396 | #endif |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4397 | ".expert EXPERIMENTAL. Suggest indexes for queries", |
drh | 978256f | 2019-11-02 00:00:14 +0000 | [diff] [blame] | 4398 | ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 4399 | ".filectrl CMD ... Run various sqlite3_file_control() operations", |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 4400 | " --schema SCHEMA Use SCHEMA instead of \"main\"", |
| 4401 | " --help Show CMD details", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4402 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables", |
| 4403 | ".headers on|off Turn display of headers on or off", |
| 4404 | ".help ?-all? ?PATTERN? Show help text for PATTERN", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4405 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4406 | ".import FILE TABLE Import data from FILE into TABLE", |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 4407 | " Options:", |
| 4408 | " --ascii Use \\037 and \\036 as column and row separators", |
| 4409 | " --csv Use , and \\n as column and row separators", |
| 4410 | " --skip N Skip the first N rows of input", |
larrybr | 738d7b9 | 2022-01-13 21:22:54 +0000 | [diff] [blame] | 4411 | " --schema S Target table to be S.TABLE", |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 4412 | " -v \"Verbose\" - increase auxiliary output", |
| 4413 | " Notes:", |
| 4414 | " * If TABLE does not exist, it is created. The first row of input", |
| 4415 | " determines the column names.", |
| 4416 | " * If neither --csv or --ascii are used, the input mode is derived", |
| 4417 | " from the \".mode\" output mode", |
| 4418 | " * If FILE begins with \"|\" then it is a command that generates the", |
| 4419 | " input text.", |
stephan | 29f2458 | 2022-05-19 00:38:34 +0000 | [diff] [blame] | 4420 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4421 | #ifndef SQLITE_OMIT_TEST_CONTROL |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4422 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4423 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4424 | ".indexes ?TABLE? Show names of indexes", |
| 4425 | " If TABLE is specified, only show indexes for", |
| 4426 | " tables matching TABLE using the LIKE operator.", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4427 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4428 | ".iotrace FILE Enable I/O diagnostic logging to FILE", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4429 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4430 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", |
| 4431 | ".lint OPTIONS Report potential schema issues.", |
| 4432 | " Options:", |
| 4433 | " fkey-indexes Find missing foreign key indexes", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4434 | #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4435 | ".load FILE ?ENTRY? Load an extension library", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4436 | #endif |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4437 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4438 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout", |
stephan | 618a375 | 2022-05-19 10:24:50 +0000 | [diff] [blame] | 4439 | #endif |
drh | 47741b8 | 2022-01-31 22:14:53 +0000 | [diff] [blame] | 4440 | ".mode MODE ?OPTIONS? Set output mode", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4441 | " MODE is one of:", |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 4442 | " ascii Columns/rows delimited by 0x1F and 0x1E", |
| 4443 | " box Tables using unicode box-drawing characters", |
| 4444 | " csv Comma-separated values", |
| 4445 | " column Output in columns. (See .width)", |
| 4446 | " html HTML <table> code", |
| 4447 | " insert SQL insert statements for TABLE", |
| 4448 | " json Results in a JSON array", |
| 4449 | " line One value per line", |
| 4450 | " list Values delimited by \"|\"", |
| 4451 | " markdown Markdown table format", |
drh | 1f41a8c | 2022-10-24 11:10:40 +0000 | [diff] [blame] | 4452 | " qbox Shorthand for \"box --wrap 60 --quote\"", |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 4453 | " quote Escape answers as for SQL", |
| 4454 | " table ASCII-art table", |
| 4455 | " tabs Tab-separated values", |
| 4456 | " tcl TCL list elements", |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 4457 | " OPTIONS: (for columnar modes or insert mode):", |
| 4458 | " --wrap N Wrap output lines to no longer than N characters", |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 4459 | " --wordwrap B Wrap or not at word boundaries per B (on/off)", |
| 4460 | " --ww Shorthand for \"--wordwrap 1\"", |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 4461 | " --quote Quote output text as SQL literals", |
| 4462 | " --noquote Do not quote output text", |
| 4463 | " TABLE The name of SQL table used for \"insert\" mode", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4464 | #ifndef SQLITE_SHELL_FIDDLE |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 4465 | ".nonce STRING Suspend safe mode for one command if nonce matches", |
stephan | e26d162 | 2022-05-19 22:04:23 +0000 | [diff] [blame] | 4466 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4467 | ".nullvalue STRING Use STRING in place of NULL values", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4468 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4469 | ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4470 | " If FILE begins with '|' then open as a pipe", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4471 | " --bom Put a UTF8 byte-order mark at the beginning", |
| 4472 | " -e Send output to the system text editor", |
| 4473 | " -x Send output as CSV to a spreadsheet (same as \".excel\")", |
stephan | de1e02e | 2022-05-24 19:01:21 +0000 | [diff] [blame] | 4474 | /* Note that .open is (partially) available in WASM builds but is |
stephan | 085c5c6 | 2022-05-25 04:35:22 +0000 | [diff] [blame] | 4475 | ** currently only intended to be used by the fiddle tool, not |
| 4476 | ** end users, so is "undocumented." */ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4477 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", |
| 4478 | " Options:", |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4479 | " --append Use appendvfs to append database to the end of FILE", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4480 | #endif |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4481 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | d10c3ca | 2021-05-08 11:57:35 +0000 | [diff] [blame] | 4482 | " --deserialize Load into memory using sqlite3_deserialize()", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4483 | " --hexdb Load the output of \"dbtotxt\" as an in-memory db", |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 4484 | " --maxsize N Maximum size for --hexdb or --deserialized database", |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 4485 | #endif |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4486 | " --new Initialize FILE to an empty database", |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 4487 | " --nofollow Do not follow symbolic links", |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 4488 | " --readonly Open FILE readonly", |
| 4489 | " --zip FILE is a ZIP archive", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4490 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4491 | ".output ?FILE? Send output to FILE or stdout if FILE is omitted", |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 4492 | " If FILE begins with '|' then open it as a pipe.", |
| 4493 | " Options:", |
| 4494 | " --bom Prefix output with a UTF8 byte-order mark", |
| 4495 | " -e Send output to the system text editor", |
| 4496 | " -x Send output as CSV to a spreadsheet", |
stephan | de1e02e | 2022-05-24 19:01:21 +0000 | [diff] [blame] | 4497 | #endif |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 4498 | ".parameter CMD ... Manage SQL parameter bindings", |
| 4499 | " clear Erase all bindings", |
| 4500 | " init Initialize the TEMP table that holds bindings", |
| 4501 | " list List the current parameter bindings", |
| 4502 | " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4503 | " PARAMETER should start with one of: $ : @ ?", |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 4504 | " unset PARAMETER Remove PARAMETER from the binding table", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4505 | ".print STRING... Print literal STRING", |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 4506 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 4507 | ".progress N Invoke progress handler after every N opcodes", |
| 4508 | " --limit N Interrupt after N progress callbacks", |
| 4509 | " --once Do no more than one progress interrupt", |
| 4510 | " --quiet|-q No output except at interrupts", |
| 4511 | " --reset Reset the count for each input and interrupt", |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 4512 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4513 | ".prompt MAIN CONTINUE Replace the standard prompts", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4514 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4515 | ".quit Exit this program", |
larrybr | a2ba25b | 2021-12-28 05:08:38 +0000 | [diff] [blame] | 4516 | ".read FILE Read input from FILE or command output", |
| 4517 | " If FILE begins with \"|\", it is a command that generates the input.", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4518 | #endif |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 4519 | #if SQLITE_SHELL_HAVE_RECOVER |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 4520 | ".recover Recover as much data as possible from corrupt db.", |
dan | f7fea5b | 2022-10-27 18:19:45 +0000 | [diff] [blame] | 4521 | " --ignore-freelist Ignore pages that appear to be on db freelist", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4522 | " --lost-and-found TABLE Alternative name for the lost-and-found table", |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 4523 | " --no-rowids Do not attempt to recover rowid values", |
| 4524 | " that are not also INTEGER PRIMARY KEYs", |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 4525 | #endif |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4526 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4527 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", |
larrybr | a7919ad | 2022-02-19 21:25:48 +0000 | [diff] [blame] | 4528 | ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4529 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4530 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", |
| 4531 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN", |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 4532 | " Options:", |
| 4533 | " --indent Try to pretty-print the schema", |
| 4534 | " --nosys Omit objects whose names start with \"sqlite_\"", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4535 | ".selftest ?OPTIONS? Run tests defined in the SELFTEST table", |
| 4536 | " Options:", |
| 4537 | " --init Create a new SELFTEST table", |
| 4538 | " -v Verbose output", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4539 | ".separator COL ?ROW? Change the column and row separators", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4540 | #if defined(SQLITE_ENABLE_SESSION) |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4541 | ".session ?NAME? CMD ... Create or control sessions", |
| 4542 | " Subcommands:", |
| 4543 | " attach TABLE Attach TABLE", |
| 4544 | " changeset FILE Write a changeset into FILE", |
| 4545 | " close Close one session", |
| 4546 | " enable ?BOOLEAN? Set or query the enable bit", |
| 4547 | " filter GLOB... Reject tables matching GLOBs", |
| 4548 | " indirect ?BOOLEAN? Mark or query the indirect status", |
| 4549 | " isempty Query whether the session is empty", |
| 4550 | " list List currently open session names", |
| 4551 | " open DB NAME Open a new session on DB", |
| 4552 | " patchset FILE Write a patchset into FILE", |
| 4553 | " If ?NAME? is omitted, the first defined session is used.", |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4554 | #endif |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4555 | ".sha3sum ... Compute a SHA3 hash of database content", |
| 4556 | " Options:", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4557 | " --schema Also hash the sqlite_schema table", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4558 | " --sha3-224 Use the sha3-224 algorithm", |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 4559 | " --sha3-256 Use the sha3-256 algorithm (default)", |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4560 | " --sha3-384 Use the sha3-384 algorithm", |
| 4561 | " --sha3-512 Use the sha3-512 algorithm", |
| 4562 | " Any other argument is a LIKE pattern for tables to hash", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4563 | #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4564 | ".shell CMD ARGS... Run CMD ARGS... in a system shell", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4565 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4566 | ".show Show the current values for various settings", |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 4567 | ".stats ?ARG? Show stats or turn stats on or off", |
| 4568 | " off Turn off automatic stat display", |
| 4569 | " on Turn on automatic stat display", |
| 4570 | " stmt Show statement stats", |
| 4571 | " vmstep Show the virtual machine step count only", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4572 | #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4573 | ".system CMD ARGS... Run CMD ARGS... in a system shell", |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 4574 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4575 | ".tables ?TABLE? List names of tables matching LIKE pattern TABLE", |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 4576 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4577 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'", |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 4578 | #endif |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 4579 | ".testctrl CMD ... Run various sqlite3_test_control() operations", |
| 4580 | " Run \".testctrl\" with no arguments for details", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4581 | ".timeout MS Try opening locked tables for MS milliseconds", |
| 4582 | ".timer on|off Turn SQL timer on or off", |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 4583 | #ifndef SQLITE_OMIT_TRACE |
| 4584 | ".trace ?OPTIONS? Output each SQL statement as it is run", |
| 4585 | " FILE Send output to FILE", |
| 4586 | " stdout Send output to stdout", |
| 4587 | " stderr Send output to stderr", |
| 4588 | " off Disable tracing", |
| 4589 | " --expanded Expand query parameters", |
| 4590 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 4591 | " --normalized Normal the SQL statements", |
| 4592 | #endif |
| 4593 | " --plain Show SQL as it is input", |
| 4594 | " --stmt Trace statement execution (SQLITE_TRACE_STMT)", |
| 4595 | " --profile Profile statements (SQLITE_TRACE_PROFILE)", |
| 4596 | " --row Trace each row (SQLITE_TRACE_ROW)", |
| 4597 | " --close Trace connection close (SQLITE_TRACE_CLOSE)", |
| 4598 | #endif /* SQLITE_OMIT_TRACE */ |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 4599 | #ifdef SQLITE_DEBUG |
| 4600 | ".unmodule NAME ... Unregister virtual table modules", |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 4601 | " --allexcept Unregister everything except those named", |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 4602 | #endif |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4603 | ".vfsinfo ?AUX? Information about the top-level VFS", |
| 4604 | ".vfslist List all available VFSes", |
| 4605 | ".vfsname ?AUX? Print the name of the VFS stack", |
drh | 7da29a3 | 2020-05-29 19:17:20 +0000 | [diff] [blame] | 4606 | ".width NUM1 NUM2 ... Set minimum column widths for columnar output", |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4607 | " Negative values right-justify", |
| 4608 | }; |
| 4609 | |
| 4610 | /* |
| 4611 | ** Output help text. |
| 4612 | ** |
| 4613 | ** zPattern describes the set of commands for which help text is provided. |
| 4614 | ** If zPattern is NULL, then show all commands, but only give a one-line |
| 4615 | ** description of each. |
| 4616 | ** |
| 4617 | ** Return the number of matches. |
| 4618 | */ |
| 4619 | static int showHelp(FILE *out, const char *zPattern){ |
drh | e93f826 | 2018-10-11 16:53:37 +0000 | [diff] [blame] | 4620 | int i = 0; |
| 4621 | int j = 0; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4622 | int n = 0; |
| 4623 | char *zPat; |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4624 | if( zPattern==0 |
| 4625 | || zPattern[0]=='0' |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4626 | || cli_strcmp(zPattern,"-a")==0 |
| 4627 | || cli_strcmp(zPattern,"-all")==0 |
| 4628 | || cli_strcmp(zPattern,"--all")==0 |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4629 | ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4630 | /* Show all commands, but only one line per command */ |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4631 | if( zPattern==0 ) zPattern = ""; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4632 | for(i=0; i<ArraySize(azHelp); i++){ |
drh | 488cddf | 2018-10-06 14:38:17 +0000 | [diff] [blame] | 4633 | if( azHelp[i][0]=='.' || zPattern[0] ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4634 | utf8_printf(out, "%s\n", azHelp[i]); |
| 4635 | n++; |
| 4636 | } |
| 4637 | } |
| 4638 | }else{ |
| 4639 | /* Look for commands that for which zPattern is an exact prefix */ |
| 4640 | zPat = sqlite3_mprintf(".%s*", zPattern); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4641 | shell_check_oom(zPat); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4642 | for(i=0; i<ArraySize(azHelp); i++){ |
| 4643 | if( sqlite3_strglob(zPat, azHelp[i])==0 ){ |
| 4644 | utf8_printf(out, "%s\n", azHelp[i]); |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4645 | j = i+1; |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4646 | n++; |
| 4647 | } |
| 4648 | } |
| 4649 | sqlite3_free(zPat); |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 4650 | if( n ){ |
| 4651 | if( n==1 ){ |
| 4652 | /* when zPattern is a prefix of exactly one command, then include the |
| 4653 | ** details of that command, which should begin at offset j */ |
| 4654 | while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ |
| 4655 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4656 | j++; |
| 4657 | } |
| 4658 | } |
| 4659 | return n; |
| 4660 | } |
| 4661 | /* Look for commands that contain zPattern anywhere. Show the complete |
| 4662 | ** text of all commands that match. */ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4663 | zPat = sqlite3_mprintf("%%%s%%", zPattern); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4664 | shell_check_oom(zPat); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 4665 | for(i=0; i<ArraySize(azHelp); i++){ |
| 4666 | if( azHelp[i][0]=='.' ) j = i; |
| 4667 | if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ |
| 4668 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4669 | while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ |
| 4670 | j++; |
| 4671 | utf8_printf(out, "%s\n", azHelp[j]); |
| 4672 | } |
| 4673 | i = j; |
| 4674 | n++; |
| 4675 | } |
| 4676 | } |
| 4677 | sqlite3_free(zPat); |
| 4678 | } |
| 4679 | return n; |
| 4680 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4681 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4682 | /* Forward reference */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4683 | static int process_input(ShellState *p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4684 | |
| 4685 | /* |
| 4686 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 4687 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 4688 | ** the memory. |
| 4689 | ** |
| 4690 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 4691 | ** read. |
| 4692 | ** |
| 4693 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 4694 | ** from the file before the buffer is returned. This byte is not included in |
| 4695 | ** the final value of (*pnByte), if applicable. |
| 4696 | ** |
| 4697 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 4698 | ** is undefined in this case. |
| 4699 | */ |
| 4700 | static char *readFile(const char *zName, int *pnByte){ |
| 4701 | FILE *in = fopen(zName, "rb"); |
| 4702 | long nIn; |
| 4703 | size_t nRead; |
| 4704 | char *pBuf; |
| 4705 | if( in==0 ) return 0; |
| 4706 | fseek(in, 0, SEEK_END); |
| 4707 | nIn = ftell(in); |
| 4708 | rewind(in); |
| 4709 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 1dbb147 | 2018-10-11 10:37:24 +0000 | [diff] [blame] | 4710 | if( pBuf==0 ){ fclose(in); return 0; } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4711 | nRead = fread(pBuf, nIn, 1, in); |
| 4712 | fclose(in); |
| 4713 | if( nRead!=1 ){ |
| 4714 | sqlite3_free(pBuf); |
| 4715 | return 0; |
| 4716 | } |
| 4717 | pBuf[nIn] = 0; |
| 4718 | if( pnByte ) *pnByte = nIn; |
| 4719 | return pBuf; |
| 4720 | } |
| 4721 | |
| 4722 | #if defined(SQLITE_ENABLE_SESSION) |
| 4723 | /* |
| 4724 | ** Close a single OpenSession object and release all of its associated |
| 4725 | ** resources. |
| 4726 | */ |
| 4727 | static void session_close(OpenSession *pSession){ |
| 4728 | int i; |
| 4729 | sqlite3session_delete(pSession->p); |
| 4730 | sqlite3_free(pSession->zName); |
| 4731 | for(i=0; i<pSession->nFilter; i++){ |
| 4732 | sqlite3_free(pSession->azFilter[i]); |
| 4733 | } |
| 4734 | sqlite3_free(pSession->azFilter); |
| 4735 | memset(pSession, 0, sizeof(OpenSession)); |
| 4736 | } |
| 4737 | #endif |
| 4738 | |
| 4739 | /* |
| 4740 | ** Close all OpenSession objects and release all associated resources. |
| 4741 | */ |
| 4742 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4743 | static void session_close_all(ShellState *p, int i){ |
| 4744 | int j; |
| 4745 | struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; |
| 4746 | for(j=0; j<pAuxDb->nSession; j++){ |
| 4747 | session_close(&pAuxDb->aSession[j]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4748 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4749 | pAuxDb->nSession = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4750 | } |
| 4751 | #else |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4752 | # define session_close_all(X,Y) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4753 | #endif |
| 4754 | |
| 4755 | /* |
| 4756 | ** Implementation of the xFilter function for an open session. Omit |
| 4757 | ** any tables named by ".session filter" but let all other table through. |
| 4758 | */ |
| 4759 | #if defined(SQLITE_ENABLE_SESSION) |
| 4760 | static int session_filter(void *pCtx, const char *zTab){ |
| 4761 | OpenSession *pSession = (OpenSession*)pCtx; |
| 4762 | int i; |
| 4763 | for(i=0; i<pSession->nFilter; i++){ |
| 4764 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 4765 | } |
| 4766 | return 1; |
| 4767 | } |
| 4768 | #endif |
| 4769 | |
| 4770 | /* |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4771 | ** Try to deduce the type of file for zName based on its content. Return |
| 4772 | ** one of the SHELL_OPEN_* constants. |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4773 | ** |
| 4774 | ** If the file does not exist or is empty but its name looks like a ZIP |
| 4775 | ** archive and the dfltZip flag is true, then assume it is a ZIP archive. |
| 4776 | ** Otherwise, assume an ordinary database regardless of the filename if |
| 4777 | ** the type cannot be determined from content. |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4778 | */ |
drh | fc97c1c | 2018-05-14 00:41:12 +0000 | [diff] [blame] | 4779 | int deduceDatabaseType(const char *zName, int dfltZip){ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4780 | FILE *f = fopen(zName, "rb"); |
| 4781 | size_t n; |
| 4782 | int rc = SHELL_OPEN_UNSPEC; |
| 4783 | char zBuf[100]; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4784 | if( f==0 ){ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 4785 | if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
| 4786 | return SHELL_OPEN_ZIPFILE; |
| 4787 | }else{ |
| 4788 | return SHELL_OPEN_NORMAL; |
| 4789 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4790 | } |
drh | 2b3c4af | 2018-10-30 14:36:21 +0000 | [diff] [blame] | 4791 | n = fread(zBuf, 16, 1, f); |
| 4792 | if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ |
| 4793 | fclose(f); |
| 4794 | return SHELL_OPEN_NORMAL; |
| 4795 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4796 | fseek(f, -25, SEEK_END); |
| 4797 | n = fread(zBuf, 25, 1, f); |
| 4798 | if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
| 4799 | rc = SHELL_OPEN_APPENDVFS; |
| 4800 | }else{ |
| 4801 | fseek(f, -22, SEEK_END); |
| 4802 | n = fread(zBuf, 22, 1, f); |
| 4803 | if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
| 4804 | && zBuf[3]==0x06 ){ |
| 4805 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 4806 | }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
mistachkin | a3926f4 | 2018-05-14 12:23:04 +0000 | [diff] [blame] | 4807 | rc = SHELL_OPEN_ZIPFILE; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 4808 | } |
| 4809 | } |
| 4810 | fclose(f); |
| 4811 | return rc; |
| 4812 | } |
| 4813 | |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4814 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4815 | /* |
| 4816 | ** Reconstruct an in-memory database using the output from the "dbtotxt" |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4817 | ** program. Read content from the file in p->aAuxDb[].zDbFilename. |
| 4818 | ** If p->aAuxDb[].zDbFilename is 0, then read from standard input. |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4819 | */ |
| 4820 | static unsigned char *readHexDb(ShellState *p, int *pnData){ |
| 4821 | unsigned char *a = 0; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4822 | int nLine; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4823 | int n = 0; |
| 4824 | int pgsz = 0; |
| 4825 | int iOffset = 0; |
| 4826 | int j, k; |
| 4827 | int rc; |
| 4828 | FILE *in; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4829 | const char *zDbFilename = p->pAuxDb->zDbFilename; |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4830 | unsigned int x[16]; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4831 | char zLine[1000]; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4832 | if( zDbFilename ){ |
| 4833 | in = fopen(zDbFilename, "r"); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4834 | if( in==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 4835 | utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4836 | return 0; |
| 4837 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4838 | nLine = 0; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4839 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4840 | in = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4841 | nLine = p->lineno; |
drh | 5bf4644 | 2019-05-03 02:41:36 +0000 | [diff] [blame] | 4842 | if( in==0 ) in = stdin; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4843 | } |
| 4844 | *pnData = 0; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4845 | nLine++; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4846 | if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; |
| 4847 | rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); |
| 4848 | if( rc!=2 ) goto readHexDb_error; |
drh | 68feae5 | 2019-05-09 11:18:41 +0000 | [diff] [blame] | 4849 | if( n<0 ) goto readHexDb_error; |
drh | 09ea125 | 2019-07-17 15:05:16 +0000 | [diff] [blame] | 4850 | if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; |
| 4851 | 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] | 4852 | a = sqlite3_malloc( n ? n : 1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 4853 | shell_check_oom(a); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4854 | memset(a, 0, n); |
| 4855 | if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ |
| 4856 | utf8_printf(stderr, "invalid pagesize\n"); |
| 4857 | goto readHexDb_error; |
| 4858 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4859 | for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4860 | rc = sscanf(zLine, "| page %d offset %d", &j, &k); |
| 4861 | if( rc==2 ){ |
| 4862 | iOffset = k; |
| 4863 | continue; |
| 4864 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4865 | if( cli_strncmp(zLine, "| end ", 6)==0 ){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4866 | break; |
| 4867 | } |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4868 | 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] | 4869 | &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], |
| 4870 | &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); |
| 4871 | if( rc==17 ){ |
| 4872 | k = iOffset+j; |
drh | 82978ac | 2021-10-01 17:06:44 +0000 | [diff] [blame] | 4873 | if( k+16<=n && k>=0 ){ |
drh | 3ea557e | 2019-04-23 15:30:58 +0000 | [diff] [blame] | 4874 | int ii; |
| 4875 | for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4876 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | *pnData = n; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4880 | if( in!=p->in ){ |
| 4881 | fclose(in); |
| 4882 | }else{ |
| 4883 | p->lineno = nLine; |
| 4884 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4885 | return a; |
| 4886 | |
| 4887 | readHexDb_error: |
drh | 68feae5 | 2019-05-09 11:18:41 +0000 | [diff] [blame] | 4888 | if( in!=p->in ){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4889 | fclose(in); |
| 4890 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 4891 | while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4892 | nLine++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 4893 | if(cli_strncmp(zLine, "| end ", 6)==0 ) break; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4894 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 4895 | p->lineno = nLine; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4896 | } |
| 4897 | sqlite3_free(a); |
| 4898 | utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); |
| 4899 | return 0; |
| 4900 | } |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 4901 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 4902 | |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4903 | /* |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 4904 | ** Scalar function "shell_int32". The first argument to this function |
| 4905 | ** must be a blob. The second a non-negative integer. This function |
| 4906 | ** reads and returns a 32-bit big-endian integer from byte |
| 4907 | ** offset (4*<arg2>) of the blob. |
| 4908 | */ |
| 4909 | static void shellInt32( |
| 4910 | sqlite3_context *context, |
| 4911 | int argc, |
| 4912 | sqlite3_value **argv |
| 4913 | ){ |
| 4914 | const unsigned char *pBlob; |
| 4915 | int nBlob; |
| 4916 | int iInt; |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 4917 | |
| 4918 | UNUSED_PARAMETER(argc); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 4919 | nBlob = sqlite3_value_bytes(argv[0]); |
| 4920 | pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); |
| 4921 | iInt = sqlite3_value_int(argv[1]); |
| 4922 | |
| 4923 | if( iInt>=0 && (iInt+1)*4<=nBlob ){ |
| 4924 | const unsigned char *a = &pBlob[iInt*4]; |
| 4925 | sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) |
| 4926 | + ((sqlite3_int64)a[1]<<16) |
| 4927 | + ((sqlite3_int64)a[2]<< 8) |
| 4928 | + ((sqlite3_int64)a[3]<< 0); |
| 4929 | sqlite3_result_int64(context, iVal); |
| 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | /* |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 4934 | ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, |
| 4935 | ** using "..." with internal double-quote characters doubled. |
| 4936 | */ |
| 4937 | static void shellIdQuote( |
| 4938 | sqlite3_context *context, |
| 4939 | int argc, |
| 4940 | sqlite3_value **argv |
| 4941 | ){ |
| 4942 | const char *zName = (const char*)sqlite3_value_text(argv[0]); |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 4943 | UNUSED_PARAMETER(argc); |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 4944 | if( zName ){ |
| 4945 | char *z = sqlite3_mprintf("\"%w\"", zName); |
| 4946 | sqlite3_result_text(context, z, -1, sqlite3_free); |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | /* |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4951 | ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X. |
| 4952 | */ |
| 4953 | static void shellUSleepFunc( |
| 4954 | sqlite3_context *context, |
drh | d36f588 | 2020-11-25 16:28:04 +0000 | [diff] [blame] | 4955 | int argcUnused, |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4956 | sqlite3_value **argv |
| 4957 | ){ |
| 4958 | int sleep = sqlite3_value_int(argv[0]); |
drh | d36f588 | 2020-11-25 16:28:04 +0000 | [diff] [blame] | 4959 | (void)argcUnused; |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 4960 | sqlite3_sleep(sleep/1000); |
| 4961 | sqlite3_result_int(context, sleep); |
| 4962 | } |
| 4963 | |
| 4964 | /* |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4965 | ** Scalar function "shell_escape_crnl" used by the .recover command. |
| 4966 | ** The argument passed to this function is the output of built-in |
| 4967 | ** function quote(). If the first character of the input is "'", |
| 4968 | ** indicating that the value passed to quote() was a text value, |
| 4969 | ** then this function searches the input for "\n" and "\r" characters |
| 4970 | ** and adds a wrapper similar to the following: |
| 4971 | ** |
| 4972 | ** replace(replace(<input>, '\n', char(10), '\r', char(13)); |
| 4973 | ** |
| 4974 | ** Or, if the first character of the input is not "'", then a copy |
| 4975 | ** of the input is returned. |
| 4976 | */ |
| 4977 | static void shellEscapeCrnl( |
| 4978 | sqlite3_context *context, |
| 4979 | int argc, |
| 4980 | sqlite3_value **argv |
| 4981 | ){ |
| 4982 | const char *zText = (const char*)sqlite3_value_text(argv[0]); |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 4983 | UNUSED_PARAMETER(argc); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 4984 | if( zText && zText[0]=='\'' ){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 4985 | i64 nText = sqlite3_value_bytes(argv[0]); |
| 4986 | i64 i; |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4987 | char zBuf1[20]; |
| 4988 | char zBuf2[20]; |
| 4989 | const char *zNL = 0; |
| 4990 | const char *zCR = 0; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 4991 | i64 nCR = 0; |
| 4992 | i64 nNL = 0; |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4993 | |
| 4994 | for(i=0; zText[i]; i++){ |
| 4995 | if( zNL==0 && zText[i]=='\n' ){ |
| 4996 | zNL = unused_string(zText, "\\n", "\\012", zBuf1); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 4997 | nNL = strlen(zNL); |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 4998 | } |
| 4999 | if( zCR==0 && zText[i]=='\r' ){ |
| 5000 | zCR = unused_string(zText, "\\r", "\\015", zBuf2); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5001 | nCR = strlen(zCR); |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
| 5005 | if( zNL || zCR ){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5006 | i64 iOut = 0; |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 5007 | i64 nMax = (nNL > nCR) ? nNL : nCR; |
dan | 51f5ffa | 2019-04-29 11:41:46 +0000 | [diff] [blame] | 5008 | i64 nAlloc = nMax * nText + (nMax+64)*2; |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 5009 | char *zOut = (char*)sqlite3_malloc64(nAlloc); |
| 5010 | if( zOut==0 ){ |
| 5011 | sqlite3_result_error_nomem(context); |
| 5012 | return; |
| 5013 | } |
| 5014 | |
| 5015 | if( zNL && zCR ){ |
| 5016 | memcpy(&zOut[iOut], "replace(replace(", 16); |
| 5017 | iOut += 16; |
| 5018 | }else{ |
| 5019 | memcpy(&zOut[iOut], "replace(", 8); |
| 5020 | iOut += 8; |
| 5021 | } |
| 5022 | for(i=0; zText[i]; i++){ |
| 5023 | if( zText[i]=='\n' ){ |
| 5024 | memcpy(&zOut[iOut], zNL, nNL); |
| 5025 | iOut += nNL; |
| 5026 | }else if( zText[i]=='\r' ){ |
| 5027 | memcpy(&zOut[iOut], zCR, nCR); |
| 5028 | iOut += nCR; |
| 5029 | }else{ |
| 5030 | zOut[iOut] = zText[i]; |
| 5031 | iOut++; |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | if( zNL ){ |
| 5036 | memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
| 5037 | memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; |
| 5038 | memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; |
| 5039 | } |
| 5040 | if( zCR ){ |
| 5041 | memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
| 5042 | memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; |
| 5043 | memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; |
| 5044 | } |
| 5045 | |
| 5046 | sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); |
| 5047 | sqlite3_free(zOut); |
| 5048 | return; |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | sqlite3_result_value(context, argv[0]); |
| 5053 | } |
| 5054 | |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 5055 | /* Flags for open_db(). |
| 5056 | ** |
| 5057 | ** The default behavior of open_db() is to exit(1) if the database fails to |
| 5058 | ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error |
| 5059 | ** but still returns without calling exit. |
| 5060 | ** |
| 5061 | ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a |
| 5062 | ** ZIP archive if the file does not exist or is empty and its name matches |
| 5063 | ** the *.zip pattern. |
| 5064 | */ |
| 5065 | #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ |
| 5066 | #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ |
| 5067 | |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5068 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5069 | ** Make sure the database is open. If it is not, then open it. If |
| 5070 | ** the database fails to open, print an error message and exit. |
| 5071 | */ |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 5072 | static void open_db(ShellState *p, int openFlags){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5073 | if( p->db==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5074 | const char *zDbFilename = p->pAuxDb->zDbFilename; |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 5075 | if( p->openMode==SHELL_OPEN_UNSPEC ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5076 | if( zDbFilename==0 || zDbFilename[0]==0 ){ |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 5077 | p->openMode = SHELL_OPEN_NORMAL; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 5078 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5079 | p->openMode = (u8)deduceDatabaseType(zDbFilename, |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 5080 | (openFlags & OPEN_DB_ZIPFILE)!=0); |
drh | f2072d1 | 2018-05-11 15:10:11 +0000 | [diff] [blame] | 5081 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5082 | } |
| 5083 | switch( p->openMode ){ |
| 5084 | case SHELL_OPEN_APPENDVFS: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5085 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 5086 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5087 | break; |
| 5088 | } |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5089 | case SHELL_OPEN_HEXDB: |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 5090 | case SHELL_OPEN_DESERIALIZE: { |
| 5091 | sqlite3_open(0, &p->db); |
| 5092 | break; |
| 5093 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5094 | case SHELL_OPEN_ZIPFILE: { |
| 5095 | sqlite3_open(":memory:", &p->db); |
| 5096 | break; |
| 5097 | } |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 5098 | case SHELL_OPEN_READONLY: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5099 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 5100 | SQLITE_OPEN_READONLY|p->openFlags, 0); |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 5101 | break; |
| 5102 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5103 | case SHELL_OPEN_UNSPEC: |
| 5104 | case SHELL_OPEN_NORMAL: { |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5105 | sqlite3_open_v2(zDbFilename, &p->db, |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 5106 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5107 | break; |
| 5108 | } |
| 5109 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5110 | globalDb = p->db; |
| 5111 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 5112 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5113 | zDbFilename, sqlite3_errmsg(p->db)); |
drh | f25cc4f | 2019-01-04 14:29:21 +0000 | [diff] [blame] | 5114 | if( openFlags & OPEN_DB_KEEPALIVE ){ |
| 5115 | sqlite3_open(":memory:", &p->db); |
| 5116 | return; |
| 5117 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5118 | exit(1); |
| 5119 | } |
| 5120 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5121 | sqlite3_enable_load_extension(p->db, 1); |
| 5122 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5123 | sqlite3_shathree_init(p->db, 0, 0); |
drh | f05dd03 | 2020-04-14 15:53:58 +0000 | [diff] [blame] | 5124 | sqlite3_uint_init(p->db, 0, 0); |
drh | beb9def | 2020-06-22 19:12:23 +0000 | [diff] [blame] | 5125 | sqlite3_decimal_init(p->db, 0, 0); |
drh | 6468990 | 2021-06-03 13:51:31 +0000 | [diff] [blame] | 5126 | sqlite3_regexp_init(p->db, 0, 0); |
drh | 8cda77d | 2020-06-24 15:06:29 +0000 | [diff] [blame] | 5127 | sqlite3_ieee_init(p->db, 0, 0); |
mistachkin | 72c38d8 | 2020-08-28 18:47:39 +0000 | [diff] [blame] | 5128 | sqlite3_series_init(p->db, 0, 0); |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 5129 | #ifndef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 5130 | sqlite3_fileio_init(p->db, 0, 0); |
| 5131 | sqlite3_completion_init(p->db, 0, 0); |
| 5132 | #endif |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 5133 | #if SQLITE_SHELL_HAVE_RECOVER |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 5134 | sqlite3_dbdata_init(p->db, 0, 0); |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 5135 | #endif |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 5136 | #ifdef SQLITE_HAVE_ZLIB |
drh | 9198f5a | 2022-03-19 15:19:35 +0000 | [diff] [blame] | 5137 | if( !p->bSafeModePersist ){ |
| 5138 | sqlite3_zipfile_init(p->db, 0, 0); |
| 5139 | sqlite3_sqlar_init(p->db, 0, 0); |
| 5140 | } |
dan | 72afc3c | 2017-12-05 18:32:40 +0000 | [diff] [blame] | 5141 | #endif |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 5142 | sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5143 | shellAddSchemaName, 0, 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 5144 | sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, |
| 5145 | shellModuleSchema, 0, 0); |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 5146 | sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, |
| 5147 | shellPutsFunc, 0, 0); |
dan | b182588 | 2019-04-23 20:48:32 +0000 | [diff] [blame] | 5148 | sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, |
| 5149 | shellEscapeCrnl, 0, 0); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 5150 | sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, |
| 5151 | shellInt32, 0, 0); |
drh | a2de66c | 2019-08-06 20:26:17 +0000 | [diff] [blame] | 5152 | sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, |
| 5153 | shellIdQuote, 0, 0); |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 5154 | sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, |
| 5155 | shellUSleepFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5156 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 9791313 | 2018-01-11 00:04:00 +0000 | [diff] [blame] | 5157 | sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, |
| 5158 | editFunc, 0, 0); |
| 5159 | sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, |
| 5160 | editFunc, 0, 0); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5161 | #endif |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5162 | if( p->openMode==SHELL_OPEN_ZIPFILE ){ |
| 5163 | char *zSql = sqlite3_mprintf( |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5164 | "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5165 | shell_check_oom(zSql); |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5166 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5167 | sqlite3_free(zSql); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 5168 | } |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 5169 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5170 | else |
| 5171 | if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ |
mistachkin | 9949093 | 2018-12-17 22:19:57 +0000 | [diff] [blame] | 5172 | int rc; |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 5173 | int nData = 0; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5174 | unsigned char *aData; |
| 5175 | if( p->openMode==SHELL_OPEN_DESERIALIZE ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 5176 | aData = (unsigned char*)readFile(zDbFilename, &nData); |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5177 | }else{ |
| 5178 | aData = readHexDb(p, &nData); |
| 5179 | if( aData==0 ){ |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 5180 | return; |
| 5181 | } |
| 5182 | } |
mistachkin | 9949093 | 2018-12-17 22:19:57 +0000 | [diff] [blame] | 5183 | rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 5184 | SQLITE_DESERIALIZE_RESIZEABLE | |
| 5185 | SQLITE_DESERIALIZE_FREEONCLOSE); |
| 5186 | if( rc ){ |
| 5187 | utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); |
| 5188 | } |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 5189 | if( p->szMax>0 ){ |
| 5190 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); |
| 5191 | } |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 5192 | } |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 5193 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5194 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 5195 | if( p->bSafeModePersist && p->db!=0 ){ |
| 5196 | sqlite3_set_authorizer(p->db, safeModeAuth, p); |
| 5197 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5200 | /* |
| 5201 | ** Attempt to close the databaes connection. Report errors. |
| 5202 | */ |
| 5203 | void close_db(sqlite3 *db){ |
| 5204 | int rc = sqlite3_close(db); |
| 5205 | if( rc ){ |
| 5206 | utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", |
| 5207 | rc, sqlite3_errmsg(db)); |
| 5208 | } |
| 5209 | } |
| 5210 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5211 | #if HAVE_READLINE || HAVE_EDITLINE |
| 5212 | /* |
| 5213 | ** Readline completion callbacks |
| 5214 | */ |
| 5215 | static char *readline_completion_generator(const char *text, int state){ |
| 5216 | static sqlite3_stmt *pStmt = 0; |
| 5217 | char *zRet; |
| 5218 | if( state==0 ){ |
| 5219 | char *zSql; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5220 | sqlite3_finalize(pStmt); |
| 5221 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 5222 | " FROM completion(%Q) ORDER BY 1", text); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5223 | shell_check_oom(zSql); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5224 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 5225 | sqlite3_free(zSql); |
| 5226 | } |
| 5227 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5228 | const char *z = (const char*)sqlite3_column_text(pStmt,0); |
| 5229 | zRet = z ? strdup(z) : 0; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5230 | }else{ |
| 5231 | sqlite3_finalize(pStmt); |
| 5232 | pStmt = 0; |
| 5233 | zRet = 0; |
| 5234 | } |
| 5235 | return zRet; |
| 5236 | } |
| 5237 | static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 5238 | rl_attempted_completion_over = 1; |
| 5239 | return rl_completion_matches(zText, readline_completion_generator); |
| 5240 | } |
| 5241 | |
| 5242 | #elif HAVE_LINENOISE |
| 5243 | /* |
| 5244 | ** Linenoise completion callback |
| 5245 | */ |
| 5246 | static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5247 | i64 nLine = strlen(zLine); |
| 5248 | i64 i, iStart; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5249 | sqlite3_stmt *pStmt = 0; |
| 5250 | char *zSql; |
| 5251 | char zBuf[1000]; |
| 5252 | |
| 5253 | if( nLine>sizeof(zBuf)-30 ) return; |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 5254 | if( zLine[0]=='.' || zLine[0]=='#') return; |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5255 | for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 5256 | if( i==nLine-1 ) return; |
| 5257 | iStart = i+1; |
| 5258 | memcpy(zBuf, zLine, iStart); |
| 5259 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 5260 | " FROM completion(%Q,%Q) ORDER BY 1", |
| 5261 | &zLine[iStart], zLine); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5262 | shell_check_oom(zSql); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5263 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 5264 | sqlite3_free(zSql); |
| 5265 | sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 5266 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5267 | const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 5268 | int nCompletion = sqlite3_column_bytes(pStmt, 0); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5269 | if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 5270 | memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 5271 | linenoiseAddCompletion(lc, zBuf); |
| 5272 | } |
| 5273 | } |
| 5274 | sqlite3_finalize(pStmt); |
| 5275 | } |
| 5276 | #endif |
| 5277 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5278 | /* |
| 5279 | ** Do C-language style dequoting. |
| 5280 | ** |
| 5281 | ** \a -> alarm |
| 5282 | ** \b -> backspace |
| 5283 | ** \t -> tab |
| 5284 | ** \n -> newline |
| 5285 | ** \v -> vertical tab |
| 5286 | ** \f -> form feed |
| 5287 | ** \r -> carriage return |
| 5288 | ** \s -> space |
| 5289 | ** \" -> " |
| 5290 | ** \' -> ' |
| 5291 | ** \\ -> backslash |
| 5292 | ** \NNN -> ascii character NNN in octal |
| 5293 | */ |
| 5294 | static void resolve_backslashes(char *z){ |
| 5295 | int i, j; |
| 5296 | char c; |
| 5297 | while( *z && *z!='\\' ) z++; |
| 5298 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 5299 | if( c=='\\' && z[i+1]!=0 ){ |
| 5300 | c = z[++i]; |
| 5301 | if( c=='a' ){ |
| 5302 | c = '\a'; |
| 5303 | }else if( c=='b' ){ |
| 5304 | c = '\b'; |
| 5305 | }else if( c=='t' ){ |
| 5306 | c = '\t'; |
| 5307 | }else if( c=='n' ){ |
| 5308 | c = '\n'; |
| 5309 | }else if( c=='v' ){ |
| 5310 | c = '\v'; |
| 5311 | }else if( c=='f' ){ |
| 5312 | c = '\f'; |
| 5313 | }else if( c=='r' ){ |
| 5314 | c = '\r'; |
| 5315 | }else if( c=='"' ){ |
| 5316 | c = '"'; |
| 5317 | }else if( c=='\'' ){ |
| 5318 | c = '\''; |
| 5319 | }else if( c=='\\' ){ |
| 5320 | c = '\\'; |
| 5321 | }else if( c>='0' && c<='7' ){ |
| 5322 | c -= '0'; |
| 5323 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 5324 | i++; |
| 5325 | c = (c<<3) + z[i] - '0'; |
| 5326 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 5327 | i++; |
| 5328 | c = (c<<3) + z[i] - '0'; |
| 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | } |
| 5333 | z[j] = c; |
| 5334 | } |
| 5335 | if( j<i ) z[j] = 0; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5339 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 5340 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 5341 | */ |
| 5342 | static int booleanValue(const char *zArg){ |
| 5343 | int i; |
| 5344 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 5345 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 5346 | }else{ |
| 5347 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 5348 | } |
| 5349 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 5350 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 5351 | return 1; |
| 5352 | } |
| 5353 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 5354 | return 0; |
| 5355 | } |
| 5356 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 5357 | zArg); |
| 5358 | return 0; |
| 5359 | } |
| 5360 | |
| 5361 | /* |
| 5362 | ** Set or clear a shell flag according to a boolean value. |
| 5363 | */ |
| 5364 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 5365 | if( booleanValue(zArg) ){ |
| 5366 | ShellSetFlag(p, mFlag); |
| 5367 | }else{ |
| 5368 | ShellClearFlag(p, mFlag); |
| 5369 | } |
| 5370 | } |
| 5371 | |
| 5372 | /* |
| 5373 | ** Close an output file, assuming it is not stderr or stdout |
| 5374 | */ |
| 5375 | static void output_file_close(FILE *f){ |
| 5376 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 5377 | } |
| 5378 | |
| 5379 | /* |
| 5380 | ** Try to open an output file. The names "stdout" and "stderr" are |
| 5381 | ** recognized and do the right thing. NULL is returned if the output |
| 5382 | ** filename is "off". |
| 5383 | */ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5384 | static FILE *output_file_open(const char *zFile, int bTextMode){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5385 | FILE *f; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 5386 | if( cli_strcmp(zFile,"stdout")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5387 | f = stdout; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 5388 | }else if( cli_strcmp(zFile, "stderr")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5389 | f = stderr; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 5390 | }else if( cli_strcmp(zFile, "off")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5391 | f = 0; |
| 5392 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5393 | f = fopen(zFile, bTextMode ? "w" : "wb"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5394 | if( f==0 ){ |
| 5395 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
| 5396 | } |
| 5397 | } |
| 5398 | return f; |
| 5399 | } |
| 5400 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5401 | #ifndef SQLITE_OMIT_TRACE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5402 | /* |
| 5403 | ** A routine for handling output from sqlite3_trace(). |
| 5404 | */ |
| 5405 | static int sql_trace_callback( |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5406 | unsigned mType, /* The trace type */ |
| 5407 | void *pArg, /* The ShellState pointer */ |
| 5408 | void *pP, /* Usually a pointer to sqlite_stmt */ |
| 5409 | void *pX /* Auxiliary output */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5410 | ){ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5411 | ShellState *p = (ShellState*)pArg; |
| 5412 | sqlite3_stmt *pStmt; |
| 5413 | const char *zSql; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5414 | i64 nSql; |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5415 | if( p->traceOut==0 ) return 0; |
| 5416 | if( mType==SQLITE_TRACE_CLOSE ){ |
| 5417 | utf8_printf(p->traceOut, "-- closing database connection\n"); |
| 5418 | return 0; |
| 5419 | } |
| 5420 | if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ |
| 5421 | zSql = (const char*)pX; |
| 5422 | }else{ |
| 5423 | pStmt = (sqlite3_stmt*)pP; |
| 5424 | switch( p->eTraceType ){ |
| 5425 | case SHELL_TRACE_EXPANDED: { |
| 5426 | zSql = sqlite3_expanded_sql(pStmt); |
| 5427 | break; |
| 5428 | } |
| 5429 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 5430 | case SHELL_TRACE_NORMALIZED: { |
| 5431 | zSql = sqlite3_normalized_sql(pStmt); |
| 5432 | break; |
| 5433 | } |
| 5434 | #endif |
| 5435 | default: { |
| 5436 | zSql = sqlite3_sql(pStmt); |
| 5437 | break; |
| 5438 | } |
| 5439 | } |
| 5440 | } |
| 5441 | if( zSql==0 ) return 0; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5442 | nSql = strlen(zSql); |
| 5443 | if( nSql>1000000000 ) nSql = 1000000000; |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5444 | while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } |
| 5445 | switch( mType ){ |
| 5446 | case SQLITE_TRACE_ROW: |
| 5447 | case SQLITE_TRACE_STMT: { |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5448 | utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql); |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5449 | break; |
| 5450 | } |
| 5451 | case SQLITE_TRACE_PROFILE: { |
| 5452 | sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 5453 | utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec); |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5456 | } |
| 5457 | return 0; |
| 5458 | } |
| 5459 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5460 | |
| 5461 | /* |
| 5462 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 5463 | ** a useful spot to set a debugger breakpoint. |
| 5464 | */ |
| 5465 | static void test_breakpoint(void){ |
| 5466 | static int nCall = 0; |
| 5467 | nCall++; |
| 5468 | } |
| 5469 | |
| 5470 | /* |
| 5471 | ** An object used to read a CSV and other files for import. |
| 5472 | */ |
| 5473 | typedef struct ImportCtx ImportCtx; |
| 5474 | struct ImportCtx { |
| 5475 | const char *zFile; /* Name of the input file */ |
| 5476 | FILE *in; /* Read the CSV text from this input stream */ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5477 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5478 | char *z; /* Accumulated text for a field */ |
| 5479 | int n; /* Number of bytes in z */ |
| 5480 | int nAlloc; /* Space allocated for z[] */ |
| 5481 | int nLine; /* Current line number */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 5482 | int nRow; /* Number of rows imported */ |
| 5483 | int nErr; /* Number of errors encountered */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5484 | int bNotFirst; /* True if one or more bytes already read */ |
| 5485 | int cTerm; /* Character that terminated the most recent field */ |
| 5486 | int cColSep; /* The column separator character. (Usually ",") */ |
| 5487 | int cRowSep; /* The row separator character. (Usually "\n") */ |
| 5488 | }; |
| 5489 | |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5490 | /* Clean up resourced used by an ImportCtx */ |
| 5491 | static void import_cleanup(ImportCtx *p){ |
drh | 42c2a04 | 2020-05-29 20:16:19 +0000 | [diff] [blame] | 5492 | if( p->in!=0 && p->xCloser!=0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 5493 | p->xCloser(p->in); |
| 5494 | p->in = 0; |
| 5495 | } |
| 5496 | sqlite3_free(p->z); |
| 5497 | p->z = 0; |
| 5498 | } |
| 5499 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5500 | /* Append a single byte to z[] */ |
| 5501 | static void import_append_char(ImportCtx *p, int c){ |
| 5502 | if( p->n+1>=p->nAlloc ){ |
| 5503 | p->nAlloc += p->nAlloc + 100; |
| 5504 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5505 | shell_check_oom(p->z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5506 | } |
| 5507 | p->z[p->n++] = (char)c; |
| 5508 | } |
| 5509 | |
| 5510 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 5511 | ** with the option of having a separator other than ",". |
| 5512 | ** |
| 5513 | ** + Input comes from p->in. |
| 5514 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 5515 | ** from sqlite3_malloc64(). |
| 5516 | ** + Use p->cSep as the column separator. The default is ",". |
| 5517 | ** + Use p->rSep as the row separator. The default is "\n". |
| 5518 | ** + Keep track of the line number in p->nLine. |
| 5519 | ** + Store the character that terminates the field in p->cTerm. Store |
| 5520 | ** EOF on end-of-file. |
| 5521 | ** + Report syntax errors on stderr |
| 5522 | */ |
| 5523 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 5524 | int c; |
| 5525 | int cSep = p->cColSep; |
| 5526 | int rSep = p->cRowSep; |
| 5527 | p->n = 0; |
| 5528 | c = fgetc(p->in); |
| 5529 | if( c==EOF || seenInterrupt ){ |
| 5530 | p->cTerm = EOF; |
| 5531 | return 0; |
| 5532 | } |
| 5533 | if( c=='"' ){ |
| 5534 | int pc, ppc; |
| 5535 | int startLine = p->nLine; |
| 5536 | int cQuote = c; |
| 5537 | pc = ppc = 0; |
| 5538 | while( 1 ){ |
| 5539 | c = fgetc(p->in); |
| 5540 | if( c==rSep ) p->nLine++; |
| 5541 | if( c==cQuote ){ |
| 5542 | if( pc==cQuote ){ |
| 5543 | pc = 0; |
| 5544 | continue; |
| 5545 | } |
| 5546 | } |
| 5547 | if( (c==cSep && pc==cQuote) |
| 5548 | || (c==rSep && pc==cQuote) |
| 5549 | || (c==rSep && pc=='\r' && ppc==cQuote) |
| 5550 | || (c==EOF && pc==cQuote) |
| 5551 | ){ |
| 5552 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
| 5553 | p->cTerm = c; |
| 5554 | break; |
| 5555 | } |
| 5556 | if( pc==cQuote && c!='\r' ){ |
| 5557 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
| 5558 | p->zFile, p->nLine, cQuote); |
| 5559 | } |
| 5560 | if( c==EOF ){ |
| 5561 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
| 5562 | p->zFile, startLine, cQuote); |
| 5563 | p->cTerm = c; |
| 5564 | break; |
| 5565 | } |
| 5566 | import_append_char(p, c); |
| 5567 | ppc = pc; |
| 5568 | pc = c; |
| 5569 | } |
| 5570 | }else{ |
| 5571 | /* If this is the first field being parsed and it begins with the |
| 5572 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 5573 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 5574 | import_append_char(p, c); |
| 5575 | c = fgetc(p->in); |
| 5576 | if( (c&0xff)==0xbb ){ |
| 5577 | import_append_char(p, c); |
| 5578 | c = fgetc(p->in); |
| 5579 | if( (c&0xff)==0xbf ){ |
| 5580 | p->bNotFirst = 1; |
| 5581 | p->n = 0; |
| 5582 | return csv_read_one_field(p); |
| 5583 | } |
| 5584 | } |
| 5585 | } |
| 5586 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 5587 | import_append_char(p, c); |
| 5588 | c = fgetc(p->in); |
| 5589 | } |
| 5590 | if( c==rSep ){ |
| 5591 | p->nLine++; |
| 5592 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 5593 | } |
| 5594 | p->cTerm = c; |
| 5595 | } |
| 5596 | if( p->z ) p->z[p->n] = 0; |
| 5597 | p->bNotFirst = 1; |
| 5598 | return p->z; |
| 5599 | } |
| 5600 | |
| 5601 | /* Read a single field of ASCII delimited text. |
| 5602 | ** |
| 5603 | ** + Input comes from p->in. |
| 5604 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 5605 | ** from sqlite3_malloc64(). |
| 5606 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 5607 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 5608 | ** + Keep track of the row number in p->nLine. |
| 5609 | ** + Store the character that terminates the field in p->cTerm. Store |
| 5610 | ** EOF on end-of-file. |
| 5611 | ** + Report syntax errors on stderr |
| 5612 | */ |
| 5613 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 5614 | int c; |
| 5615 | int cSep = p->cColSep; |
| 5616 | int rSep = p->cRowSep; |
| 5617 | p->n = 0; |
| 5618 | c = fgetc(p->in); |
| 5619 | if( c==EOF || seenInterrupt ){ |
| 5620 | p->cTerm = EOF; |
| 5621 | return 0; |
| 5622 | } |
| 5623 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 5624 | import_append_char(p, c); |
| 5625 | c = fgetc(p->in); |
| 5626 | } |
| 5627 | if( c==rSep ){ |
| 5628 | p->nLine++; |
| 5629 | } |
| 5630 | p->cTerm = c; |
| 5631 | if( p->z ) p->z[p->n] = 0; |
| 5632 | return p->z; |
| 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | ** Try to transfer data for table zTable. If an error is seen while |
| 5637 | ** moving forward, try to go backwards. The backwards movement won't |
| 5638 | ** work for WITHOUT ROWID tables. |
| 5639 | */ |
| 5640 | static void tryToCloneData( |
| 5641 | ShellState *p, |
| 5642 | sqlite3 *newDb, |
| 5643 | const char *zTable |
| 5644 | ){ |
| 5645 | sqlite3_stmt *pQuery = 0; |
| 5646 | sqlite3_stmt *pInsert = 0; |
| 5647 | char *zQuery = 0; |
| 5648 | char *zInsert = 0; |
| 5649 | int rc; |
| 5650 | int i, j, n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5651 | int nTable = strlen30(zTable); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5652 | int k = 0; |
| 5653 | int cnt = 0; |
| 5654 | const int spinRate = 10000; |
| 5655 | |
| 5656 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5657 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5658 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5659 | if( rc ){ |
| 5660 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 5661 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5662 | zQuery); |
| 5663 | goto end_data_xfer; |
| 5664 | } |
| 5665 | n = sqlite3_column_count(pQuery); |
| 5666 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5667 | shell_check_oom(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5668 | sqlite3_snprintf(200+nTable,zInsert, |
| 5669 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 5670 | i = strlen30(zInsert); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5671 | for(j=1; j<n; j++){ |
| 5672 | memcpy(zInsert+i, ",?", 2); |
| 5673 | i += 2; |
| 5674 | } |
| 5675 | memcpy(zInsert+i, ");", 3); |
| 5676 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 5677 | if( rc ){ |
| 5678 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
| 5679 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 5680 | zQuery); |
| 5681 | goto end_data_xfer; |
| 5682 | } |
| 5683 | for(k=0; k<2; k++){ |
| 5684 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 5685 | for(i=0; i<n; i++){ |
| 5686 | switch( sqlite3_column_type(pQuery, i) ){ |
| 5687 | case SQLITE_NULL: { |
| 5688 | sqlite3_bind_null(pInsert, i+1); |
| 5689 | break; |
| 5690 | } |
| 5691 | case SQLITE_INTEGER: { |
| 5692 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 5693 | break; |
| 5694 | } |
| 5695 | case SQLITE_FLOAT: { |
| 5696 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 5697 | break; |
| 5698 | } |
| 5699 | case SQLITE_TEXT: { |
| 5700 | sqlite3_bind_text(pInsert, i+1, |
| 5701 | (const char*)sqlite3_column_text(pQuery,i), |
| 5702 | -1, SQLITE_STATIC); |
| 5703 | break; |
| 5704 | } |
| 5705 | case SQLITE_BLOB: { |
| 5706 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 5707 | sqlite3_column_bytes(pQuery,i), |
| 5708 | SQLITE_STATIC); |
| 5709 | break; |
| 5710 | } |
| 5711 | } |
| 5712 | } /* End for */ |
| 5713 | rc = sqlite3_step(pInsert); |
| 5714 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 5715 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
| 5716 | sqlite3_errmsg(newDb)); |
| 5717 | } |
| 5718 | sqlite3_reset(pInsert); |
| 5719 | cnt++; |
| 5720 | if( (cnt%spinRate)==0 ){ |
| 5721 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 5722 | fflush(stdout); |
| 5723 | } |
| 5724 | } /* End while */ |
| 5725 | if( rc==SQLITE_DONE ) break; |
| 5726 | sqlite3_finalize(pQuery); |
| 5727 | sqlite3_free(zQuery); |
| 5728 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 5729 | zTable); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5730 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5731 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5732 | if( rc ){ |
| 5733 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
| 5734 | break; |
| 5735 | } |
| 5736 | } /* End for(k=0...) */ |
| 5737 | |
| 5738 | end_data_xfer: |
| 5739 | sqlite3_finalize(pQuery); |
| 5740 | sqlite3_finalize(pInsert); |
| 5741 | sqlite3_free(zQuery); |
| 5742 | sqlite3_free(zInsert); |
| 5743 | } |
| 5744 | |
| 5745 | |
| 5746 | /* |
| 5747 | ** Try to transfer all rows of the schema that match zWhere. For |
| 5748 | ** each row, invoke xForEach() on the object defined by that row. |
| 5749 | ** If an error is encountered while moving forward through the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5750 | ** sqlite_schema table, try again moving backwards. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5751 | */ |
| 5752 | static void tryToCloneSchema( |
| 5753 | ShellState *p, |
| 5754 | sqlite3 *newDb, |
| 5755 | const char *zWhere, |
| 5756 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 5757 | ){ |
| 5758 | sqlite3_stmt *pQuery = 0; |
| 5759 | char *zQuery = 0; |
| 5760 | int rc; |
| 5761 | const unsigned char *zName; |
| 5762 | const unsigned char *zSql; |
| 5763 | char *zErrMsg = 0; |
| 5764 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5765 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5766 | " WHERE %s", zWhere); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5767 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5768 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5769 | if( rc ){ |
| 5770 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 5771 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5772 | zQuery); |
| 5773 | goto end_schema_xfer; |
| 5774 | } |
| 5775 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 5776 | zName = sqlite3_column_text(pQuery, 0); |
| 5777 | zSql = sqlite3_column_text(pQuery, 1); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5778 | if( zName==0 || zSql==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5779 | printf("%s... ", zName); fflush(stdout); |
| 5780 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 5781 | if( zErrMsg ){ |
| 5782 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 5783 | sqlite3_free(zErrMsg); |
| 5784 | zErrMsg = 0; |
| 5785 | } |
| 5786 | if( xForEach ){ |
| 5787 | xForEach(p, newDb, (const char*)zName); |
| 5788 | } |
| 5789 | printf("done\n"); |
| 5790 | } |
| 5791 | if( rc!=SQLITE_DONE ){ |
| 5792 | sqlite3_finalize(pQuery); |
| 5793 | sqlite3_free(zQuery); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5794 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5795 | " WHERE %s ORDER BY rowid DESC", zWhere); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 5796 | shell_check_oom(zQuery); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5797 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 5798 | if( rc ){ |
| 5799 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 5800 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 5801 | zQuery); |
| 5802 | goto end_schema_xfer; |
| 5803 | } |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 5804 | while( sqlite3_step(pQuery)==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5805 | zName = sqlite3_column_text(pQuery, 0); |
| 5806 | zSql = sqlite3_column_text(pQuery, 1); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 5807 | if( zName==0 || zSql==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5808 | printf("%s... ", zName); fflush(stdout); |
| 5809 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 5810 | if( zErrMsg ){ |
| 5811 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 5812 | sqlite3_free(zErrMsg); |
| 5813 | zErrMsg = 0; |
| 5814 | } |
| 5815 | if( xForEach ){ |
| 5816 | xForEach(p, newDb, (const char*)zName); |
| 5817 | } |
| 5818 | printf("done\n"); |
| 5819 | } |
| 5820 | } |
| 5821 | end_schema_xfer: |
| 5822 | sqlite3_finalize(pQuery); |
| 5823 | sqlite3_free(zQuery); |
| 5824 | } |
| 5825 | |
| 5826 | /* |
| 5827 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 5828 | ** as possible out of the main database (which might be corrupt) and write it |
| 5829 | ** into zNewDb. |
| 5830 | */ |
| 5831 | static void tryToClone(ShellState *p, const char *zNewDb){ |
| 5832 | int rc; |
| 5833 | sqlite3 *newDb = 0; |
| 5834 | if( access(zNewDb,0)==0 ){ |
| 5835 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
| 5836 | return; |
| 5837 | } |
| 5838 | rc = sqlite3_open(zNewDb, &newDb); |
| 5839 | if( rc ){ |
| 5840 | utf8_printf(stderr, "Cannot create output database: %s\n", |
| 5841 | sqlite3_errmsg(newDb)); |
| 5842 | }else{ |
| 5843 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
| 5844 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
| 5845 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 5846 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
| 5847 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
| 5848 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5849 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 5850 | close_db(newDb); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | /* |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5854 | ** Change the output file back to stdout. |
| 5855 | ** |
| 5856 | ** If the p->doXdgOpen flag is set, that means the output was being |
| 5857 | ** redirected to a temporary file named by p->zTempFile. In that case, |
| 5858 | ** launch start/open/xdg-open on that temporary file. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5859 | */ |
| 5860 | static void output_reset(ShellState *p){ |
| 5861 | if( p->outfile[0]=='|' ){ |
| 5862 | #ifndef SQLITE_OMIT_POPEN |
| 5863 | pclose(p->out); |
| 5864 | #endif |
| 5865 | }else{ |
| 5866 | output_file_close(p->out); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5867 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5868 | if( p->doXdgOpen ){ |
| 5869 | const char *zXdgOpenCmd = |
| 5870 | #if defined(_WIN32) |
| 5871 | "start"; |
| 5872 | #elif defined(__APPLE__) |
| 5873 | "open"; |
| 5874 | #else |
| 5875 | "xdg-open"; |
| 5876 | #endif |
| 5877 | char *zCmd; |
| 5878 | zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5879 | if( system(zCmd) ){ |
| 5880 | utf8_printf(stderr, "Failed: [%s]\n", zCmd); |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 5881 | }else{ |
| 5882 | /* Give the start/open/xdg-open command some time to get |
| 5883 | ** going before we continue, and potential delete the |
| 5884 | ** p->zTempFile data file out from under it */ |
| 5885 | sqlite3_sleep(2000); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 5886 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5887 | sqlite3_free(zCmd); |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 5888 | outputModePop(p); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 5889 | p->doXdgOpen = 0; |
| 5890 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 5891 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5892 | } |
| 5893 | p->outfile[0] = 0; |
| 5894 | p->out = stdout; |
| 5895 | } |
| 5896 | |
| 5897 | /* |
| 5898 | ** Run an SQL command and return the single integer result. |
| 5899 | */ |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 5900 | static int db_int(sqlite3 *db, const char *zSql){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5901 | sqlite3_stmt *pStmt; |
| 5902 | int res = 0; |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 5903 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5904 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5905 | res = sqlite3_column_int(pStmt,0); |
| 5906 | } |
| 5907 | sqlite3_finalize(pStmt); |
| 5908 | return res; |
| 5909 | } |
| 5910 | |
drh | 0483668 | 2022-06-08 18:29:23 +0000 | [diff] [blame] | 5911 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5912 | /* |
| 5913 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 5914 | */ |
| 5915 | static unsigned int get2byteInt(unsigned char *a){ |
| 5916 | return (a[0]<<8) + a[1]; |
| 5917 | } |
| 5918 | static unsigned int get4byteInt(unsigned char *a){ |
| 5919 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 5920 | } |
| 5921 | |
| 5922 | /* |
drh | 76c1206 | 2020-01-14 13:13:19 +0000 | [diff] [blame] | 5923 | ** Implementation of the ".dbinfo" command. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5924 | ** |
| 5925 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 5926 | */ |
| 5927 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 5928 | static const struct { const char *zName; int ofst; } aField[] = { |
| 5929 | { "file change counter:", 24 }, |
| 5930 | { "database page count:", 28 }, |
| 5931 | { "freelist page count:", 36 }, |
| 5932 | { "schema cookie:", 40 }, |
| 5933 | { "schema format:", 44 }, |
| 5934 | { "default cache size:", 48 }, |
| 5935 | { "autovacuum top root:", 52 }, |
| 5936 | { "incremental vacuum:", 64 }, |
| 5937 | { "text encoding:", 56 }, |
| 5938 | { "user version:", 60 }, |
| 5939 | { "application id:", 68 }, |
| 5940 | { "software version:", 96 }, |
| 5941 | }; |
| 5942 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 5943 | { "number of tables:", |
| 5944 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 5945 | { "number of indexes:", |
| 5946 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 5947 | { "number of triggers:", |
| 5948 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 5949 | { "number of views:", |
| 5950 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 5951 | { "schema size:", |
| 5952 | "SELECT total(length(sql)) FROM %s" }, |
| 5953 | }; |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5954 | int i, rc; |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 5955 | unsigned iDataVersion; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5956 | char *zSchemaTab; |
| 5957 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5958 | sqlite3_stmt *pStmt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5959 | unsigned char aHdr[100]; |
| 5960 | open_db(p, 0); |
| 5961 | if( p->db==0 ) return 1; |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5962 | rc = sqlite3_prepare_v2(p->db, |
| 5963 | "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
| 5964 | -1, &pStmt, 0); |
| 5965 | if( rc ){ |
drh | 451f89a | 2020-04-28 23:09:56 +0000 | [diff] [blame] | 5966 | utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); |
drh | 87c889c | 2019-03-20 18:22:51 +0000 | [diff] [blame] | 5967 | sqlite3_finalize(pStmt); |
| 5968 | return 1; |
| 5969 | } |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5970 | sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
| 5971 | if( sqlite3_step(pStmt)==SQLITE_ROW |
| 5972 | && sqlite3_column_bytes(pStmt,0)>100 |
| 5973 | ){ |
| 5974 | memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
| 5975 | sqlite3_finalize(pStmt); |
| 5976 | }else{ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5977 | raw_printf(stderr, "unable to read database header\n"); |
drh | 512e6c3 | 2017-10-11 17:51:08 +0000 | [diff] [blame] | 5978 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 5979 | return 1; |
| 5980 | } |
| 5981 | i = get2byteInt(aHdr+16); |
| 5982 | if( i==1 ) i = 65536; |
| 5983 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 5984 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 5985 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 5986 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
| 5987 | for(i=0; i<ArraySize(aField); i++){ |
| 5988 | int ofst = aField[i].ofst; |
| 5989 | unsigned int val = get4byteInt(aHdr + ofst); |
| 5990 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
| 5991 | switch( ofst ){ |
| 5992 | case 56: { |
| 5993 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 5994 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 5995 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
| 5996 | } |
| 5997 | } |
| 5998 | raw_printf(p->out, "\n"); |
| 5999 | } |
| 6000 | if( zDb==0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6001 | zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 6002 | }else if( cli_strcmp(zDb,"temp")==0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6003 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6004 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6005 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6006 | } |
| 6007 | for(i=0; i<ArraySize(aQuery); i++){ |
| 6008 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 6009 | int val = db_int(p->db, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6010 | sqlite3_free(zSql); |
| 6011 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
| 6012 | } |
| 6013 | sqlite3_free(zSchemaTab); |
drh | ea99a31 | 2018-07-18 19:09:07 +0000 | [diff] [blame] | 6014 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); |
| 6015 | utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6016 | return 0; |
| 6017 | } |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 6018 | #endif /* SQLITE_SHELL_HAVE_RECOVER */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6019 | |
| 6020 | /* |
| 6021 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 6022 | */ |
| 6023 | static int shellDatabaseError(sqlite3 *db){ |
| 6024 | const char *zErr = sqlite3_errmsg(db); |
| 6025 | utf8_printf(stderr, "Error: %s\n", zErr); |
| 6026 | return 1; |
| 6027 | } |
| 6028 | |
| 6029 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6030 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 6031 | ** if they match and FALSE (0) if they do not match. |
| 6032 | ** |
| 6033 | ** Globbing rules: |
| 6034 | ** |
| 6035 | ** '*' Matches any sequence of zero or more characters. |
| 6036 | ** |
| 6037 | ** '?' Matches exactly one character. |
| 6038 | ** |
| 6039 | ** [...] Matches one character from the enclosed list of |
| 6040 | ** characters. |
| 6041 | ** |
| 6042 | ** [^...] Matches one character not in the enclosed list. |
| 6043 | ** |
| 6044 | ** '#' Matches any sequence of one or more digits with an |
| 6045 | ** optional + or - sign in front |
| 6046 | ** |
| 6047 | ** ' ' Any span of whitespace matches any other span of |
| 6048 | ** whitespace. |
| 6049 | ** |
| 6050 | ** Extra whitespace at the end of z[] is ignored. |
| 6051 | */ |
| 6052 | static int testcase_glob(const char *zGlob, const char *z){ |
| 6053 | int c, c2; |
| 6054 | int invert; |
| 6055 | int seen; |
| 6056 | |
| 6057 | while( (c = (*(zGlob++)))!=0 ){ |
| 6058 | if( IsSpace(c) ){ |
| 6059 | if( !IsSpace(*z) ) return 0; |
| 6060 | while( IsSpace(*zGlob) ) zGlob++; |
| 6061 | while( IsSpace(*z) ) z++; |
| 6062 | }else if( c=='*' ){ |
| 6063 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 6064 | if( c=='?' && (*(z++))==0 ) return 0; |
| 6065 | } |
| 6066 | if( c==0 ){ |
| 6067 | return 1; |
| 6068 | }else if( c=='[' ){ |
| 6069 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 6070 | z++; |
| 6071 | } |
| 6072 | return (*z)!=0; |
| 6073 | } |
| 6074 | while( (c2 = (*(z++)))!=0 ){ |
| 6075 | while( c2!=c ){ |
| 6076 | c2 = *(z++); |
| 6077 | if( c2==0 ) return 0; |
| 6078 | } |
| 6079 | if( testcase_glob(zGlob,z) ) return 1; |
| 6080 | } |
| 6081 | return 0; |
| 6082 | }else if( c=='?' ){ |
| 6083 | if( (*(z++))==0 ) return 0; |
| 6084 | }else if( c=='[' ){ |
| 6085 | int prior_c = 0; |
| 6086 | seen = 0; |
| 6087 | invert = 0; |
| 6088 | c = *(z++); |
| 6089 | if( c==0 ) return 0; |
| 6090 | c2 = *(zGlob++); |
| 6091 | if( c2=='^' ){ |
| 6092 | invert = 1; |
| 6093 | c2 = *(zGlob++); |
| 6094 | } |
| 6095 | if( c2==']' ){ |
| 6096 | if( c==']' ) seen = 1; |
| 6097 | c2 = *(zGlob++); |
| 6098 | } |
| 6099 | while( c2 && c2!=']' ){ |
| 6100 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 6101 | c2 = *(zGlob++); |
| 6102 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 6103 | prior_c = 0; |
| 6104 | }else{ |
| 6105 | if( c==c2 ){ |
| 6106 | seen = 1; |
| 6107 | } |
| 6108 | prior_c = c2; |
| 6109 | } |
| 6110 | c2 = *(zGlob++); |
| 6111 | } |
| 6112 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 6113 | }else if( c=='#' ){ |
| 6114 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 6115 | if( !IsDigit(z[0]) ) return 0; |
| 6116 | z++; |
| 6117 | while( IsDigit(z[0]) ){ z++; } |
| 6118 | }else{ |
| 6119 | if( c!=(*(z++)) ) return 0; |
| 6120 | } |
| 6121 | } |
| 6122 | while( IsSpace(*z) ){ z++; } |
| 6123 | return *z==0; |
| 6124 | } |
| 6125 | |
| 6126 | |
| 6127 | /* |
| 6128 | ** Compare the string as a command-line option with either one or two |
| 6129 | ** initial "-" characters. |
| 6130 | */ |
| 6131 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 6132 | if( zStr[0]!='-' ) return 0; |
| 6133 | zStr++; |
| 6134 | if( zStr[0]=='-' ) zStr++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 6135 | return cli_strcmp(zStr, zOpt)==0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | /* |
| 6139 | ** Delete a file. |
| 6140 | */ |
| 6141 | int shellDeleteFile(const char *zFilename){ |
| 6142 | int rc; |
| 6143 | #ifdef _WIN32 |
| 6144 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
| 6145 | rc = _wunlink(z); |
| 6146 | sqlite3_free(z); |
| 6147 | #else |
| 6148 | rc = unlink(zFilename); |
| 6149 | #endif |
| 6150 | return rc; |
| 6151 | } |
| 6152 | |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6153 | /* |
| 6154 | ** Try to delete the temporary file (if there is one) and free the |
| 6155 | ** memory used to hold the name of the temp file. |
| 6156 | */ |
| 6157 | static void clearTempFile(ShellState *p){ |
| 6158 | if( p->zTempFile==0 ) return; |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 6159 | if( p->doXdgOpen ) return; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6160 | if( shellDeleteFile(p->zTempFile) ) return; |
| 6161 | sqlite3_free(p->zTempFile); |
| 6162 | p->zTempFile = 0; |
| 6163 | } |
| 6164 | |
| 6165 | /* |
| 6166 | ** Create a new temp file name with the given suffix. |
| 6167 | */ |
| 6168 | static void newTempFile(ShellState *p, const char *zSuffix){ |
| 6169 | clearTempFile(p); |
| 6170 | sqlite3_free(p->zTempFile); |
| 6171 | p->zTempFile = 0; |
drh | 7f3bf8a | 2018-01-10 21:50:08 +0000 | [diff] [blame] | 6172 | if( p->db ){ |
| 6173 | sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); |
| 6174 | } |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6175 | if( p->zTempFile==0 ){ |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 6176 | /* If p->db is an in-memory database then the TEMPFILENAME file-control |
| 6177 | ** will not work and we will need to fallback to guessing */ |
| 6178 | char *zTemp; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6179 | sqlite3_uint64 r; |
| 6180 | sqlite3_randomness(sizeof(r), &r); |
drh | 1d9ea27 | 2020-04-17 23:46:54 +0000 | [diff] [blame] | 6181 | zTemp = getenv("TEMP"); |
| 6182 | if( zTemp==0 ) zTemp = getenv("TMP"); |
| 6183 | if( zTemp==0 ){ |
| 6184 | #ifdef _WIN32 |
| 6185 | zTemp = "\\tmp"; |
| 6186 | #else |
| 6187 | zTemp = "/tmp"; |
| 6188 | #endif |
| 6189 | } |
| 6190 | p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6191 | }else{ |
| 6192 | p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); |
| 6193 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 6194 | shell_check_oom(p->zTempFile); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 6195 | } |
| 6196 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6197 | |
| 6198 | /* |
| 6199 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
| 6200 | ** by the ".lint fkey-indexes" command. This scalar function is always |
| 6201 | ** called with four arguments - the parent table name, the parent column name, |
| 6202 | ** the child table name and the child column name. |
| 6203 | ** |
| 6204 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 6205 | ** |
| 6206 | ** If either of the named tables or columns do not exist, this function |
| 6207 | ** returns an empty string. An empty string is also returned if both tables |
| 6208 | ** and columns exist but have the same default collation sequence. Or, |
| 6209 | ** if both exist but the default collation sequences are different, this |
| 6210 | ** function returns the string " COLLATE <parent-collation>", where |
| 6211 | ** <parent-collation> is the default collation sequence of the parent column. |
| 6212 | */ |
| 6213 | static void shellFkeyCollateClause( |
| 6214 | sqlite3_context *pCtx, |
| 6215 | int nVal, |
| 6216 | sqlite3_value **apVal |
| 6217 | ){ |
| 6218 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 6219 | const char *zParent; |
| 6220 | const char *zParentCol; |
| 6221 | const char *zParentSeq; |
| 6222 | const char *zChild; |
| 6223 | const char *zChildCol; |
| 6224 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
| 6225 | int rc; |
| 6226 | |
| 6227 | assert( nVal==4 ); |
| 6228 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 6229 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 6230 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 6231 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 6232 | |
| 6233 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 6234 | rc = sqlite3_table_column_metadata( |
| 6235 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 6236 | ); |
| 6237 | if( rc==SQLITE_OK ){ |
| 6238 | rc = sqlite3_table_column_metadata( |
| 6239 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 6240 | ); |
| 6241 | } |
| 6242 | |
| 6243 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 6244 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 6245 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 6246 | sqlite3_free(z); |
| 6247 | } |
| 6248 | } |
| 6249 | |
| 6250 | |
| 6251 | /* |
| 6252 | ** The implementation of dot-command ".lint fkey-indexes". |
| 6253 | */ |
| 6254 | static int lintFkeyIndexes( |
| 6255 | ShellState *pState, /* Current shell tool state */ |
| 6256 | char **azArg, /* Array of arguments passed to dot command */ |
| 6257 | int nArg /* Number of entries in azArg[] */ |
| 6258 | ){ |
| 6259 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 6260 | FILE *out = pState->out; /* Stream to write non-error output to */ |
| 6261 | int bVerbose = 0; /* If -verbose is present */ |
| 6262 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
| 6263 | int i; /* To iterate through azArg[] */ |
| 6264 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 6265 | int rc; /* Return code */ |
| 6266 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
| 6267 | |
| 6268 | /* |
| 6269 | ** This SELECT statement returns one row for each foreign key constraint |
| 6270 | ** in the schema of the main database. The column values are: |
| 6271 | ** |
| 6272 | ** 0. The text of an SQL statement similar to: |
| 6273 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6274 | ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6275 | ** |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6276 | ** This SELECT is similar to the one that the foreign keys implementation |
| 6277 | ** 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] | 6278 | ** be used to optimize this query, then it can also be used by the FK |
| 6279 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 6280 | ** table. |
| 6281 | ** |
| 6282 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 6283 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 6284 | ** contains an index that can be used to optimize the query. |
| 6285 | ** |
| 6286 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 6287 | ** |
| 6288 | ** "child_table(child_key1, child_key2)" |
| 6289 | ** |
| 6290 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 6291 | ** |
| 6292 | ** "parent_table(parent_key1, parent_key2)" |
| 6293 | ** |
| 6294 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 6295 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 6296 | ** |
| 6297 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 6298 | ** |
| 6299 | ** 5. The name of the parent table. |
| 6300 | ** |
| 6301 | ** These six values are used by the C logic below to generate the report. |
| 6302 | */ |
| 6303 | const char *zSql = |
| 6304 | "SELECT " |
dan | f967931 | 2017-12-01 18:40:18 +0000 | [diff] [blame] | 6305 | " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6306 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 6307 | " || fkey_collate_clause(" |
| 6308 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
| 6309 | ", " |
drh | 8210233 | 2021-03-20 15:11:29 +0000 | [diff] [blame] | 6310 | " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6311 | " || group_concat('*=?', ' AND ') || ')'" |
| 6312 | ", " |
| 6313 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
| 6314 | ", " |
| 6315 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
| 6316 | ", " |
| 6317 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 6318 | " || ' ON ' || quote(s.name) || '('" |
| 6319 | " || group_concat(quote(f.[from]) ||" |
| 6320 | " fkey_collate_clause(" |
| 6321 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
| 6322 | " || ');'" |
| 6323 | ", " |
| 6324 | " f.[table] " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6325 | "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6326 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
| 6327 | "GROUP BY s.name, f.id " |
| 6328 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
| 6329 | ; |
drh | 8210233 | 2021-03-20 15:11:29 +0000 | [diff] [blame] | 6330 | const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6331 | |
| 6332 | for(i=2; i<nArg; i++){ |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6333 | int n = strlen30(azArg[i]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6334 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 6335 | bVerbose = 1; |
| 6336 | } |
| 6337 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 6338 | bGroupByParent = 1; |
| 6339 | zIndent = " "; |
| 6340 | } |
| 6341 | else{ |
| 6342 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 6343 | azArg[0], azArg[1] |
| 6344 | ); |
| 6345 | return SQLITE_ERROR; |
| 6346 | } |
| 6347 | } |
| 6348 | |
| 6349 | /* Register the fkey_collate_clause() SQL function */ |
| 6350 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 6351 | 0, shellFkeyCollateClause, 0, 0 |
| 6352 | ); |
| 6353 | |
| 6354 | |
| 6355 | if( rc==SQLITE_OK ){ |
| 6356 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 6357 | } |
| 6358 | if( rc==SQLITE_OK ){ |
| 6359 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 6360 | } |
| 6361 | |
| 6362 | if( rc==SQLITE_OK ){ |
| 6363 | int rc2; |
| 6364 | char *zPrev = 0; |
| 6365 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 6366 | int res = -1; |
| 6367 | sqlite3_stmt *pExplain = 0; |
| 6368 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 6369 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 6370 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 6371 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 6372 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
| 6373 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
| 6374 | |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 6375 | if( zEQP==0 ) continue; |
| 6376 | if( zGlob==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6377 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 6378 | if( rc!=SQLITE_OK ) break; |
| 6379 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 6380 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 6381 | res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) |
| 6382 | || 0==sqlite3_strglob(zGlobIPK, zPlan)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6383 | } |
| 6384 | rc = sqlite3_finalize(pExplain); |
| 6385 | if( rc!=SQLITE_OK ) break; |
| 6386 | |
| 6387 | if( res<0 ){ |
| 6388 | raw_printf(stderr, "Error: internal error"); |
| 6389 | break; |
| 6390 | }else{ |
| 6391 | if( bGroupByParent |
| 6392 | && (bVerbose || res==0) |
| 6393 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 6394 | ){ |
| 6395 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 6396 | sqlite3_free(zPrev); |
| 6397 | zPrev = sqlite3_mprintf("%s", zParent); |
| 6398 | } |
| 6399 | |
| 6400 | if( res==0 ){ |
| 6401 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 6402 | }else if( bVerbose ){ |
| 6403 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 6404 | zIndent, zFrom, zTarget |
| 6405 | ); |
| 6406 | } |
| 6407 | } |
| 6408 | } |
| 6409 | sqlite3_free(zPrev); |
| 6410 | |
| 6411 | if( rc!=SQLITE_OK ){ |
| 6412 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6413 | } |
| 6414 | |
| 6415 | rc2 = sqlite3_finalize(pSql); |
| 6416 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 6417 | rc = rc2; |
| 6418 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6419 | } |
| 6420 | }else{ |
| 6421 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 6422 | } |
| 6423 | |
| 6424 | return rc; |
| 6425 | } |
| 6426 | |
| 6427 | /* |
| 6428 | ** Implementation of ".lint" dot command. |
| 6429 | */ |
| 6430 | static int lintDotCommand( |
| 6431 | ShellState *pState, /* Current shell tool state */ |
| 6432 | char **azArg, /* Array of arguments passed to dot command */ |
| 6433 | int nArg /* Number of entries in azArg[] */ |
| 6434 | ){ |
| 6435 | int n; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6436 | n = (nArg>=2 ? strlen30(azArg[1]) : 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 6437 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 6438 | return lintFkeyIndexes(pState, azArg, nArg); |
| 6439 | |
| 6440 | usage: |
| 6441 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 6442 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 6443 | raw_printf(stderr, " fkey-indexes\n"); |
| 6444 | return SQLITE_ERROR; |
| 6445 | } |
| 6446 | |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6447 | #if !defined SQLITE_OMIT_VIRTUALTABLE |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6448 | static void shellPrepare( |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6449 | sqlite3 *db, |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6450 | int *pRc, |
| 6451 | const char *zSql, |
| 6452 | sqlite3_stmt **ppStmt |
| 6453 | ){ |
| 6454 | *ppStmt = 0; |
| 6455 | if( *pRc==SQLITE_OK ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6456 | int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6457 | if( rc!=SQLITE_OK ){ |
| 6458 | raw_printf(stderr, "sql error: %s (%d)\n", |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6459 | sqlite3_errmsg(db), sqlite3_errcode(db) |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6460 | ); |
| 6461 | *pRc = rc; |
| 6462 | } |
| 6463 | } |
| 6464 | } |
| 6465 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6466 | /* |
| 6467 | ** Create a prepared statement using printf-style arguments for the SQL. |
| 6468 | ** |
| 6469 | ** This routine is could be marked "static". But it is not always used, |
| 6470 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6471 | ** nuisance compiler warnings about "defined but not used". |
| 6472 | */ |
| 6473 | void shellPreparePrintf( |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6474 | sqlite3 *db, |
| 6475 | int *pRc, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6476 | sqlite3_stmt **ppStmt, |
| 6477 | const char *zFmt, |
| 6478 | ... |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6479 | ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6480 | *ppStmt = 0; |
| 6481 | if( *pRc==SQLITE_OK ){ |
| 6482 | va_list ap; |
| 6483 | char *z; |
| 6484 | va_start(ap, zFmt); |
| 6485 | z = sqlite3_vmprintf(zFmt, ap); |
drh | 1dbb147 | 2018-10-11 10:37:24 +0000 | [diff] [blame] | 6486 | va_end(ap); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6487 | if( z==0 ){ |
| 6488 | *pRc = SQLITE_NOMEM; |
| 6489 | }else{ |
| 6490 | shellPrepare(db, pRc, z, ppStmt); |
| 6491 | sqlite3_free(z); |
| 6492 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6493 | } |
| 6494 | } |
| 6495 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6496 | /* Finalize the prepared statement created using shellPreparePrintf(). |
| 6497 | ** |
| 6498 | ** This routine is could be marked "static". But it is not always used, |
| 6499 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6500 | ** nuisance compiler warnings about "defined but not used". |
| 6501 | */ |
| 6502 | void shellFinalize( |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6503 | int *pRc, |
| 6504 | sqlite3_stmt *pStmt |
| 6505 | ){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6506 | if( pStmt ){ |
| 6507 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 6508 | int rc = sqlite3_finalize(pStmt); |
| 6509 | if( *pRc==SQLITE_OK ){ |
| 6510 | if( rc!=SQLITE_OK ){ |
| 6511 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 6512 | } |
| 6513 | *pRc = rc; |
| 6514 | } |
| 6515 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
drh | 9546c76 | 2019-05-10 17:50:33 +0000 | [diff] [blame] | 6518 | /* Reset the prepared statement created using shellPreparePrintf(). |
| 6519 | ** |
| 6520 | ** This routine is could be marked "static". But it is not always used, |
| 6521 | ** depending on compile-time options. By omitting the "static", we avoid |
| 6522 | ** nuisance compiler warnings about "defined but not used". |
| 6523 | */ |
| 6524 | void shellReset( |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6525 | int *pRc, |
| 6526 | sqlite3_stmt *pStmt |
| 6527 | ){ |
| 6528 | int rc = sqlite3_reset(pStmt); |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6529 | if( *pRc==SQLITE_OK ){ |
| 6530 | if( rc!=SQLITE_OK ){ |
| 6531 | sqlite3 *db = sqlite3_db_handle(pStmt); |
| 6532 | raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
| 6533 | } |
| 6534 | *pRc = rc; |
| 6535 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6536 | } |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6537 | #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ |
| 6538 | |
| 6539 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 6540 | /****************************************************************************** |
dan | 1b16216 | 2019-04-27 20:15:15 +0000 | [diff] [blame] | 6541 | ** The ".archive" or ".ar" command. |
| 6542 | */ |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 6543 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6544 | ** Structure representing a single ".ar" command. |
| 6545 | */ |
| 6546 | typedef struct ArCommand ArCommand; |
| 6547 | struct ArCommand { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6548 | u8 eCmd; /* An AR_CMD_* value */ |
| 6549 | u8 bVerbose; /* True if --verbose */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6550 | u8 bZip; /* True if the archive is a ZIP */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6551 | u8 bDryRun; /* True if --dry-run */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6552 | u8 bAppend; /* True if --append */ |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6553 | u8 bGlob; /* True if --glob */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6554 | u8 fromCmdLine; /* Run from -A instead of .archive */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6555 | int nArg; /* Number of command arguments */ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6556 | char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6557 | const char *zFile; /* --file argument, or NULL */ |
| 6558 | const char *zDir; /* --directory argument, or NULL */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6559 | char **azArg; /* Array of command arguments */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6560 | ShellState *p; /* Shell state */ |
| 6561 | sqlite3 *db; /* Database containing the archive */ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6562 | }; |
| 6563 | |
| 6564 | /* |
| 6565 | ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. |
| 6566 | */ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6567 | static int arUsage(FILE *f){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 6568 | showHelp(f,"archive"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6569 | return SQLITE_ERROR; |
| 6570 | } |
| 6571 | |
| 6572 | /* |
| 6573 | ** Print an error message for the .ar command to stderr and return |
| 6574 | ** SQLITE_ERROR. |
| 6575 | */ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6576 | static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6577 | va_list ap; |
| 6578 | char *z; |
| 6579 | va_start(ap, zFmt); |
| 6580 | z = sqlite3_vmprintf(zFmt, ap); |
| 6581 | va_end(ap); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6582 | utf8_printf(stderr, "Error: %s\n", z); |
| 6583 | if( pAr->fromCmdLine ){ |
| 6584 | utf8_printf(stderr, "Use \"-A\" for more help\n"); |
| 6585 | }else{ |
| 6586 | utf8_printf(stderr, "Use \".archive --help\" for more help\n"); |
| 6587 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6588 | sqlite3_free(z); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6589 | return SQLITE_ERROR; |
| 6590 | } |
| 6591 | |
| 6592 | /* |
| 6593 | ** Values for ArCommand.eCmd. |
| 6594 | */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6595 | #define AR_CMD_CREATE 1 |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6596 | #define AR_CMD_UPDATE 2 |
| 6597 | #define AR_CMD_INSERT 3 |
| 6598 | #define AR_CMD_EXTRACT 4 |
| 6599 | #define AR_CMD_LIST 5 |
| 6600 | #define AR_CMD_HELP 6 |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6601 | #define AR_CMD_REMOVE 7 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6602 | |
| 6603 | /* |
| 6604 | ** Other (non-command) switches. |
| 6605 | */ |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6606 | #define AR_SWITCH_VERBOSE 8 |
| 6607 | #define AR_SWITCH_FILE 9 |
| 6608 | #define AR_SWITCH_DIRECTORY 10 |
| 6609 | #define AR_SWITCH_APPEND 11 |
larrybr | 719506f | 2021-11-01 22:33:20 +0000 | [diff] [blame] | 6610 | #define AR_SWITCH_DRYRUN 12 |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6611 | #define AR_SWITCH_GLOB 13 |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6612 | |
| 6613 | static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ |
| 6614 | switch( eSwitch ){ |
| 6615 | case AR_CMD_CREATE: |
| 6616 | case AR_CMD_EXTRACT: |
| 6617 | case AR_CMD_LIST: |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6618 | case AR_CMD_REMOVE: |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6619 | case AR_CMD_UPDATE: |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6620 | case AR_CMD_INSERT: |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6621 | case AR_CMD_HELP: |
| 6622 | if( pAr->eCmd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6623 | return arErrorMsg(pAr, "multiple command options"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6624 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6625 | pAr->eCmd = eSwitch; |
| 6626 | break; |
| 6627 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6628 | case AR_SWITCH_DRYRUN: |
| 6629 | pAr->bDryRun = 1; |
| 6630 | break; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6631 | case AR_SWITCH_GLOB: |
| 6632 | pAr->bGlob = 1; |
| 6633 | break; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6634 | case AR_SWITCH_VERBOSE: |
| 6635 | pAr->bVerbose = 1; |
| 6636 | break; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 6637 | case AR_SWITCH_APPEND: |
| 6638 | pAr->bAppend = 1; |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 6639 | /* Fall thru into --file */ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6640 | case AR_SWITCH_FILE: |
| 6641 | pAr->zFile = zArg; |
| 6642 | break; |
| 6643 | case AR_SWITCH_DIRECTORY: |
| 6644 | pAr->zDir = zArg; |
| 6645 | break; |
| 6646 | } |
| 6647 | |
| 6648 | return SQLITE_OK; |
| 6649 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6650 | |
| 6651 | /* |
| 6652 | ** Parse the command line for an ".ar" command. The results are written into |
| 6653 | ** structure (*pAr). SQLITE_OK is returned if the command line is parsed |
| 6654 | ** successfully, otherwise an error message is written to stderr and |
| 6655 | ** SQLITE_ERROR returned. |
| 6656 | */ |
| 6657 | static int arParseCommand( |
| 6658 | char **azArg, /* Array of arguments passed to dot command */ |
| 6659 | int nArg, /* Number of entries in azArg[] */ |
| 6660 | ArCommand *pAr /* Populate this object */ |
| 6661 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6662 | struct ArSwitch { |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6663 | const char *zLong; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6664 | char cShort; |
| 6665 | u8 eSwitch; |
| 6666 | u8 bArg; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6667 | } aSwitch[] = { |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6668 | { "create", 'c', AR_CMD_CREATE, 0 }, |
| 6669 | { "extract", 'x', AR_CMD_EXTRACT, 0 }, |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 6670 | { "insert", 'i', AR_CMD_INSERT, 0 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6671 | { "list", 't', AR_CMD_LIST, 0 }, |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6672 | { "remove", 'r', AR_CMD_REMOVE, 0 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6673 | { "update", 'u', AR_CMD_UPDATE, 0 }, |
| 6674 | { "help", 'h', AR_CMD_HELP, 0 }, |
| 6675 | { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, |
| 6676 | { "file", 'f', AR_SWITCH_FILE, 1 }, |
drh | ca7733b | 2018-01-10 18:09:20 +0000 | [diff] [blame] | 6677 | { "append", 'a', AR_SWITCH_APPEND, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6678 | { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6679 | { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6680 | { "glob", 'g', AR_SWITCH_GLOB, 0 }, |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6681 | }; |
| 6682 | int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); |
| 6683 | struct ArSwitch *pEnd = &aSwitch[nSwitch]; |
| 6684 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6685 | if( nArg<=1 ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 6686 | utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6687 | return arUsage(stderr); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6688 | }else{ |
| 6689 | char *z = azArg[1]; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6690 | if( z[0]!='-' ){ |
| 6691 | /* Traditional style [tar] invocation */ |
| 6692 | int i; |
| 6693 | int iArg = 2; |
| 6694 | for(i=0; z[i]; i++){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6695 | const char *zArg = 0; |
| 6696 | struct ArSwitch *pOpt; |
| 6697 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6698 | if( z[i]==pOpt->cShort ) break; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6699 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6700 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6701 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6702 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6703 | if( pOpt->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6704 | if( iArg>=nArg ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6705 | return arErrorMsg(pAr, "option requires an argument: %c",z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6706 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6707 | zArg = azArg[iArg++]; |
| 6708 | } |
| 6709 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6710 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6711 | pAr->nArg = nArg-iArg; |
| 6712 | if( pAr->nArg>0 ){ |
| 6713 | pAr->azArg = &azArg[iArg]; |
| 6714 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6715 | }else{ |
| 6716 | /* Non-traditional invocation */ |
| 6717 | int iArg; |
| 6718 | for(iArg=1; iArg<nArg; iArg++){ |
| 6719 | int n; |
| 6720 | z = azArg[iArg]; |
| 6721 | if( z[0]!='-' ){ |
| 6722 | /* All remaining command line words are command arguments. */ |
| 6723 | pAr->azArg = &azArg[iArg]; |
| 6724 | pAr->nArg = nArg-iArg; |
| 6725 | break; |
| 6726 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6727 | n = strlen30(z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6728 | |
| 6729 | if( z[1]!='-' ){ |
| 6730 | int i; |
| 6731 | /* One or more short options */ |
| 6732 | for(i=1; i<n; i++){ |
| 6733 | const char *zArg = 0; |
| 6734 | struct ArSwitch *pOpt; |
| 6735 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6736 | if( z[i]==pOpt->cShort ) break; |
| 6737 | } |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6738 | if( pOpt==pEnd ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6739 | return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6740 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6741 | if( pOpt->bArg ){ |
| 6742 | if( i<(n-1) ){ |
| 6743 | zArg = &z[i+1]; |
| 6744 | i = n; |
| 6745 | }else{ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6746 | if( iArg>=(nArg-1) ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 6747 | return arErrorMsg(pAr, "option requires an argument: %c", |
| 6748 | z[i]); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6749 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6750 | zArg = azArg[++iArg]; |
| 6751 | } |
| 6752 | } |
| 6753 | if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
| 6754 | } |
| 6755 | }else if( z[2]=='\0' ){ |
| 6756 | /* A -- option, indicating that all remaining command line words |
| 6757 | ** are command arguments. */ |
| 6758 | pAr->azArg = &azArg[iArg+1]; |
| 6759 | pAr->nArg = nArg-iArg-1; |
| 6760 | break; |
| 6761 | }else{ |
| 6762 | /* A long option */ |
| 6763 | const char *zArg = 0; /* Argument for option, if any */ |
| 6764 | struct ArSwitch *pMatch = 0; /* Matching option */ |
| 6765 | struct ArSwitch *pOpt; /* Iterator */ |
| 6766 | for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){ |
| 6767 | const char *zLong = pOpt->zLong; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6768 | if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6769 | if( pMatch ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6770 | return arErrorMsg(pAr, "ambiguous option: %s",z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6771 | }else{ |
| 6772 | pMatch = pOpt; |
| 6773 | } |
| 6774 | } |
| 6775 | } |
| 6776 | |
| 6777 | if( pMatch==0 ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6778 | return arErrorMsg(pAr, "unrecognized option: %s", z); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6779 | } |
| 6780 | if( pMatch->bArg ){ |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6781 | if( iArg>=(nArg-1) ){ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6782 | return arErrorMsg(pAr, "option requires an argument: %s", z); |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 6783 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 6784 | zArg = azArg[++iArg]; |
| 6785 | } |
| 6786 | if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; |
| 6787 | } |
| 6788 | } |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | return SQLITE_OK; |
| 6793 | } |
| 6794 | |
| 6795 | /* |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6796 | ** This function assumes that all arguments within the ArCommand.azArg[] |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6797 | ** array refer to archive members, as for the --extract, --list or --remove |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6798 | ** commands. It checks that each of them are "present". If any specified |
| 6799 | ** 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] | 6800 | ** error code returned. Otherwise, if all specified arguments are present |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6801 | ** in the archive, SQLITE_OK is returned. Here, "present" means either an |
| 6802 | ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match |
drh | 0c0fb9c | 2021-11-02 10:54:39 +0000 | [diff] [blame] | 6803 | ** when pAr->bGlob is true. |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6804 | ** |
| 6805 | ** This function strips any trailing '/' characters from each argument. |
| 6806 | ** This is consistent with the way the [tar] command seems to work on |
| 6807 | ** Linux. |
| 6808 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6809 | static int arCheckEntries(ArCommand *pAr){ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6810 | int rc = SQLITE_OK; |
| 6811 | if( pAr->nArg ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6812 | int i, j; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6813 | sqlite3_stmt *pTest = 0; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6814 | const char *zSel = (pAr->bGlob) |
| 6815 | ? "SELECT name FROM %s WHERE glob($name,name)" |
| 6816 | : "SELECT name FROM %s WHERE name=$name"; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6817 | |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6818 | shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6819 | j = sqlite3_bind_parameter_index(pTest, "$name"); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6820 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
| 6821 | char *z = pAr->azArg[i]; |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 6822 | int n = strlen30(z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6823 | int bOk = 0; |
| 6824 | while( n>0 && z[n-1]=='/' ) n--; |
| 6825 | z[n] = '\0'; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6826 | sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6827 | if( SQLITE_ROW==sqlite3_step(pTest) ){ |
| 6828 | bOk = 1; |
| 6829 | } |
| 6830 | shellReset(&rc, pTest); |
| 6831 | if( rc==SQLITE_OK && bOk==0 ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6832 | utf8_printf(stderr, "not found in archive: %s\n", z); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6833 | rc = SQLITE_ERROR; |
| 6834 | } |
| 6835 | } |
| 6836 | shellFinalize(&rc, pTest); |
| 6837 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6838 | return rc; |
| 6839 | } |
| 6840 | |
| 6841 | /* |
| 6842 | ** Format a WHERE clause that can be used against the "sqlar" table to |
| 6843 | ** identify all archive members that match the command arguments held |
| 6844 | ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. |
| 6845 | ** The caller is responsible for eventually calling sqlite3_free() on |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6846 | ** any non-NULL (*pzWhere) value. Here, "match" means strict equality |
| 6847 | ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6848 | */ |
| 6849 | static void arWhereClause( |
| 6850 | int *pRc, |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6851 | ArCommand *pAr, |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6852 | char **pzWhere /* OUT: New WHERE clause */ |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6853 | ){ |
| 6854 | char *zWhere = 0; |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6855 | const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6856 | if( *pRc==SQLITE_OK ){ |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6857 | if( pAr->nArg==0 ){ |
| 6858 | zWhere = sqlite3_mprintf("1"); |
| 6859 | }else{ |
| 6860 | int i; |
| 6861 | const char *zSep = ""; |
| 6862 | for(i=0; i<pAr->nArg; i++){ |
| 6863 | const char *z = pAr->azArg[i]; |
| 6864 | zWhere = sqlite3_mprintf( |
larrybr | 8f09f4b | 2021-11-02 00:18:11 +0000 | [diff] [blame] | 6865 | "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", |
| 6866 | zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6867 | ); |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 6868 | if( zWhere==0 ){ |
| 6869 | *pRc = SQLITE_NOMEM; |
| 6870 | break; |
| 6871 | } |
| 6872 | zSep = " OR "; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6873 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6874 | } |
| 6875 | } |
| 6876 | *pzWhere = zWhere; |
| 6877 | } |
| 6878 | |
| 6879 | /* |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6880 | ** Implementation of .ar "lisT" command. |
| 6881 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6882 | static int arListCommand(ArCommand *pAr){ |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6883 | const char *zSql = "SELECT %s FROM %s WHERE %s"; |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6884 | const char *azCols[] = { |
| 6885 | "name", |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6886 | "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6887 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6888 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6889 | char *zWhere = 0; |
| 6890 | sqlite3_stmt *pSql = 0; |
| 6891 | int rc; |
| 6892 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6893 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6894 | arWhereClause(&rc, pAr, &zWhere); |
| 6895 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6896 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], |
| 6897 | pAr->zSrcTable, zWhere); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6898 | if( pAr->bDryRun ){ |
| 6899 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 6900 | }else{ |
| 6901 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 6902 | if( pAr->bVerbose ){ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 6903 | utf8_printf(pAr->p->out, "%s % 10d %s %s\n", |
| 6904 | sqlite3_column_text(pSql, 0), |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6905 | sqlite3_column_int(pSql, 1), |
| 6906 | sqlite3_column_text(pSql, 2), |
| 6907 | sqlite3_column_text(pSql, 3) |
| 6908 | ); |
| 6909 | }else{ |
| 6910 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 6911 | } |
dan | b5090e4 | 2017-12-27 21:13:21 +0000 | [diff] [blame] | 6912 | } |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6913 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6914 | shellFinalize(&rc, pSql); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 6915 | sqlite3_free(zWhere); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6916 | return rc; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
| 6919 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6920 | /* |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6921 | ** Implementation of .ar "Remove" command. |
| 6922 | */ |
| 6923 | static int arRemoveCommand(ArCommand *pAr){ |
larrybr | 7774fc0 | 2021-11-01 22:30:24 +0000 | [diff] [blame] | 6924 | int rc = 0; |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 6925 | char *zSql = 0; |
| 6926 | char *zWhere = 0; |
| 6927 | |
| 6928 | if( pAr->nArg ){ |
| 6929 | /* Verify that args actually exist within the archive before proceeding. |
| 6930 | ** And formulate a WHERE clause to match them. */ |
| 6931 | rc = arCheckEntries(pAr); |
| 6932 | arWhereClause(&rc, pAr, &zWhere); |
| 6933 | } |
| 6934 | if( rc==SQLITE_OK ){ |
| 6935 | zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", |
| 6936 | pAr->zSrcTable, zWhere); |
| 6937 | if( pAr->bDryRun ){ |
| 6938 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 6939 | }else{ |
| 6940 | char *zErr = 0; |
| 6941 | rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); |
| 6942 | if( rc==SQLITE_OK ){ |
| 6943 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 6944 | if( rc!=SQLITE_OK ){ |
| 6945 | sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); |
| 6946 | }else{ |
| 6947 | rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); |
| 6948 | } |
| 6949 | } |
| 6950 | if( zErr ){ |
| 6951 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 6952 | sqlite3_free(zErr); |
| 6953 | } |
| 6954 | } |
| 6955 | } |
| 6956 | sqlite3_free(zWhere); |
| 6957 | sqlite3_free(zSql); |
| 6958 | return rc; |
| 6959 | } |
| 6960 | |
| 6961 | /* |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6962 | ** Implementation of .ar "eXtract" command. |
| 6963 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6964 | static int arExtractCommand(ArCommand *pAr){ |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 6965 | const char *zSql1 = |
dan | d1b51d4 | 2017-12-16 19:11:26 +0000 | [diff] [blame] | 6966 | "SELECT " |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6967 | " ($dir || name)," |
| 6968 | " writefile(($dir || name), %s, mode, mtime) " |
drh | 0cfd46a | 2018-06-06 01:18:01 +0000 | [diff] [blame] | 6969 | "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" |
| 6970 | " AND name NOT GLOB '*..[/\\]*'"; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6971 | |
| 6972 | const char *azExtraArg[] = { |
| 6973 | "sqlar_uncompress(data, sz)", |
dan | 7c15ac1 | 2018-01-08 19:59:59 +0000 | [diff] [blame] | 6974 | "data" |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6975 | }; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 6976 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6977 | sqlite3_stmt *pSql = 0; |
| 6978 | int rc = SQLITE_OK; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6979 | char *zDir = 0; |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6980 | char *zWhere = 0; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6981 | int i, j; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6982 | |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6983 | /* If arguments are specified, check that they actually exist within |
| 6984 | ** the archive before proceeding. And formulate a WHERE clause to |
| 6985 | ** match them. */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6986 | rc = arCheckEntries(pAr); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 6987 | arWhereClause(&rc, pAr, &zWhere); |
| 6988 | |
| 6989 | if( rc==SQLITE_OK ){ |
| 6990 | if( pAr->zDir ){ |
| 6991 | zDir = sqlite3_mprintf("%s/", pAr->zDir); |
| 6992 | }else{ |
| 6993 | zDir = sqlite3_mprintf(""); |
| 6994 | } |
| 6995 | if( zDir==0 ) rc = SQLITE_NOMEM; |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 6996 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 6997 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 6998 | shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, |
| 6999 | azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 7000 | ); |
| 7001 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 7002 | if( rc==SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7003 | j = sqlite3_bind_parameter_index(pSql, "$dir"); |
| 7004 | sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 7005 | |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 7006 | /* Run the SELECT statement twice. The first time, writefile() is called |
| 7007 | ** for all archive members that should be extracted. The second time, |
| 7008 | ** only for the directories. This is because the timestamps for |
| 7009 | ** extracted directories must be reset after they are populated (as |
| 7010 | ** populating them changes the timestamp). */ |
| 7011 | for(i=0; i<2; i++){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7012 | j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); |
| 7013 | sqlite3_bind_int(pSql, j, i); |
| 7014 | if( pAr->bDryRun ){ |
| 7015 | utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
| 7016 | }else{ |
| 7017 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 7018 | if( i==0 && pAr->bVerbose ){ |
| 7019 | utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
| 7020 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | shellReset(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 7024 | } |
dan | ac15e2d | 2017-12-14 19:15:07 +0000 | [diff] [blame] | 7025 | shellFinalize(&rc, pSql); |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 7026 | } |
dan | 25c1218 | 2017-12-07 21:03:33 +0000 | [diff] [blame] | 7027 | |
dan | 2ad0949 | 2017-12-09 18:28:22 +0000 | [diff] [blame] | 7028 | sqlite3_free(zDir); |
dan | 3f67ddf | 2017-12-13 20:04:53 +0000 | [diff] [blame] | 7029 | sqlite3_free(zWhere); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7030 | return rc; |
| 7031 | } |
| 7032 | |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7033 | /* |
| 7034 | ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. |
| 7035 | */ |
| 7036 | static int arExecSql(ArCommand *pAr, const char *zSql){ |
| 7037 | int rc; |
| 7038 | if( pAr->bDryRun ){ |
| 7039 | utf8_printf(pAr->p->out, "%s\n", zSql); |
| 7040 | rc = SQLITE_OK; |
| 7041 | }else{ |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 7042 | char *zErr = 0; |
| 7043 | rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
| 7044 | if( zErr ){ |
| 7045 | utf8_printf(stdout, "ERROR: %s\n", zErr); |
| 7046 | sqlite3_free(zErr); |
| 7047 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7048 | } |
| 7049 | return rc; |
| 7050 | } |
| 7051 | |
dan | 1ad3f61 | 2017-12-11 20:22:02 +0000 | [diff] [blame] | 7052 | |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7053 | /* |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7054 | ** Implementation of .ar "create", "insert", and "update" commands. |
| 7055 | ** |
| 7056 | ** create -> Create a new SQL archive |
| 7057 | ** insert -> Insert or reinsert all files listed |
| 7058 | ** update -> Insert files that have changed or that were not |
| 7059 | ** previously in the archive |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7060 | ** |
| 7061 | ** Create the "sqlar" table in the database if it does not already exist. |
| 7062 | ** Then add each file in the azFile[] array to the archive. Directories |
| 7063 | ** are added recursively. If argument bVerbose is non-zero, a message is |
| 7064 | ** printed on stdout for each file archived. |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 7065 | ** |
| 7066 | ** The create command is the same as update, except that it drops |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7067 | ** any existing "sqlar" table before beginning. The "insert" command |
| 7068 | ** always overwrites every file named on the command-line, where as |
| 7069 | ** "update" only overwrites if the size or mtime or mode has changed. |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7070 | */ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7071 | static int arCreateOrUpdateCommand( |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 7072 | ArCommand *pAr, /* Command arguments and options */ |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7073 | int bUpdate, /* true for a --create. */ |
| 7074 | int bOnlyIfChanged /* Only update if file has changed */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7075 | ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7076 | const char *zCreate = |
drh | afba180 | 2018-01-06 15:49:57 +0000 | [diff] [blame] | 7077 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 7078 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 7079 | " mode INT, -- access permissions\n" |
| 7080 | " mtime INT, -- last modification time\n" |
| 7081 | " sz INT, -- original file size\n" |
| 7082 | " data BLOB -- compressed content\n" |
| 7083 | ")"; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7084 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7085 | const char *zInsertFmt[2] = { |
| 7086 | "REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7087 | " SELECT\n" |
| 7088 | " %s,\n" |
| 7089 | " mode,\n" |
| 7090 | " mtime,\n" |
drh | 410cad9 | 2018-01-10 17:19:16 +0000 | [diff] [blame] | 7091 | " CASE substr(lsmode(mode),1,1)\n" |
| 7092 | " WHEN '-' THEN length(data)\n" |
| 7093 | " WHEN 'd' THEN 0\n" |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7094 | " ELSE -1 END,\n" |
drh | 69d2d35 | 2018-03-09 22:18:53 +0000 | [diff] [blame] | 7095 | " sqlar_compress(data)\n" |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7096 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 7097 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 7098 | , |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7099 | "REPLACE INTO %s(name,mode,mtime,data)\n" |
| 7100 | " SELECT\n" |
| 7101 | " %s,\n" |
| 7102 | " mode,\n" |
| 7103 | " mtime,\n" |
| 7104 | " data\n" |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7105 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 7106 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7107 | }; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7108 | int i; /* For iterating through azFile[] */ |
| 7109 | int rc; /* Return code */ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7110 | const char *zTab = 0; /* SQL table into which to insert */ |
| 7111 | char *zSql; |
| 7112 | char zTemp[50]; |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7113 | char *zExists = 0; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7114 | |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7115 | arExecSql(pAr, "PRAGMA page_size=512"); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7116 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7117 | if( rc!=SQLITE_OK ) return rc; |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7118 | zTemp[0] = 0; |
| 7119 | if( pAr->bZip ){ |
| 7120 | /* Initialize the zipfile virtual table, if necessary */ |
| 7121 | if( pAr->zFile ){ |
| 7122 | sqlite3_uint64 r; |
| 7123 | sqlite3_randomness(sizeof(r),&r); |
| 7124 | sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
| 7125 | zTab = zTemp; |
| 7126 | zSql = sqlite3_mprintf( |
| 7127 | "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
| 7128 | zTab, pAr->zFile |
| 7129 | ); |
| 7130 | rc = arExecSql(pAr, zSql); |
| 7131 | sqlite3_free(zSql); |
| 7132 | }else{ |
| 7133 | zTab = "zip"; |
| 7134 | } |
| 7135 | }else{ |
| 7136 | /* Initialize the table for an SQLAR */ |
| 7137 | zTab = "sqlar"; |
| 7138 | if( bUpdate==0 ){ |
| 7139 | rc = arExecSql(pAr, zDrop); |
| 7140 | if( rc!=SQLITE_OK ) goto end_ar_transaction; |
| 7141 | } |
| 7142 | rc = arExecSql(pAr, zCreate); |
dan | 06741a3 | 2017-12-13 20:17:18 +0000 | [diff] [blame] | 7143 | } |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7144 | if( bOnlyIfChanged ){ |
| 7145 | zExists = sqlite3_mprintf( |
| 7146 | " AND NOT EXISTS(" |
| 7147 | "SELECT 1 FROM %s AS mem" |
| 7148 | " WHERE mem.name=disk.name" |
| 7149 | " AND mem.mtime=disk.mtime" |
| 7150 | " AND mem.mode=disk.mode)", zTab); |
| 7151 | }else{ |
| 7152 | zExists = sqlite3_mprintf(""); |
| 7153 | } |
| 7154 | if( zExists==0 ) rc = SQLITE_NOMEM; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7155 | for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 7156 | char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7157 | pAr->bVerbose ? "shell_putsnl(name)" : "name", |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7158 | pAr->azArg[i], pAr->zDir, zExists); |
mistachkin | ce2052b | 2018-03-23 00:31:53 +0000 | [diff] [blame] | 7159 | rc = arExecSql(pAr, zSql2); |
| 7160 | sqlite3_free(zSql2); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7161 | } |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7162 | end_ar_transaction: |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7163 | if( rc!=SQLITE_OK ){ |
drh | 2bd207f | 2019-01-11 17:19:59 +0000 | [diff] [blame] | 7164 | sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7165 | }else{ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7166 | rc = arExecSql(pAr, "RELEASE ar;"); |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7167 | if( pAr->bZip && pAr->zFile ){ |
| 7168 | zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); |
| 7169 | arExecSql(pAr, zSql); |
| 7170 | sqlite3_free(zSql); |
| 7171 | } |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7172 | } |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7173 | sqlite3_free(zExists); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7174 | return rc; |
| 7175 | } |
| 7176 | |
| 7177 | /* |
| 7178 | ** Implementation of ".ar" dot command. |
| 7179 | */ |
| 7180 | static int arDotCommand( |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7181 | ShellState *pState, /* Current shell tool state */ |
| 7182 | int fromCmdLine, /* True if -A command-line option, not .ar cmd */ |
| 7183 | char **azArg, /* Array of arguments passed to dot command */ |
| 7184 | int nArg /* Number of entries in azArg[] */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7185 | ){ |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7186 | ArCommand cmd; |
| 7187 | int rc; |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 7188 | memset(&cmd, 0, sizeof(cmd)); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 7189 | cmd.fromCmdLine = fromCmdLine; |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7190 | rc = arParseCommand(azArg, nArg, &cmd); |
| 7191 | if( rc==SQLITE_OK ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7192 | int eDbType = SHELL_OPEN_UNSPEC; |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7193 | cmd.p = pState; |
| 7194 | cmd.db = pState->db; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7195 | if( cmd.zFile ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7196 | eDbType = deduceDatabaseType(cmd.zFile, 1); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7197 | }else{ |
| 7198 | eDbType = pState->openMode; |
| 7199 | } |
| 7200 | if( eDbType==SHELL_OPEN_ZIPFILE ){ |
drh | 1bf208c | 2018-03-09 21:54:01 +0000 | [diff] [blame] | 7201 | if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ |
| 7202 | if( cmd.zFile==0 ){ |
| 7203 | cmd.zSrcTable = sqlite3_mprintf("zip"); |
| 7204 | }else{ |
| 7205 | cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); |
| 7206 | } |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 7207 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7208 | cmd.bZip = 1; |
dan | 5a78b81 | 2017-12-27 18:54:11 +0000 | [diff] [blame] | 7209 | }else if( cmd.zFile ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7210 | int flags; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7211 | if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7212 | if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 7213 | || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7214 | flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 7215 | }else{ |
| 7216 | flags = SQLITE_OPEN_READONLY; |
| 7217 | } |
drh | a82c95b | 2018-01-10 14:00:00 +0000 | [diff] [blame] | 7218 | cmd.db = 0; |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7219 | if( cmd.bDryRun ){ |
| 7220 | utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, |
| 7221 | eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); |
| 7222 | } |
| 7223 | rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, |
| 7224 | eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7225 | if( rc!=SQLITE_OK ){ |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7226 | utf8_printf(stderr, "cannot open file: %s (%s)\n", |
| 7227 | cmd.zFile, sqlite3_errmsg(cmd.db) |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7228 | ); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7229 | goto end_ar_command; |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7230 | } |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7231 | sqlite3_fileio_init(cmd.db, 0, 0); |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7232 | sqlite3_sqlar_init(cmd.db, 0, 0); |
drh | 3466064 | 2018-01-10 17:39:54 +0000 | [diff] [blame] | 7233 | sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, |
| 7234 | shellPutsFunc, 0, 0); |
| 7235 | |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7236 | } |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 7237 | if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ |
drh | 634c70f | 2018-01-10 16:50:18 +0000 | [diff] [blame] | 7238 | if( cmd.eCmd!=AR_CMD_CREATE |
| 7239 | && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) |
| 7240 | ){ |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7241 | utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); |
| 7242 | rc = SQLITE_ERROR; |
| 7243 | goto end_ar_command; |
| 7244 | } |
| 7245 | cmd.zSrcTable = sqlite3_mprintf("sqlar"); |
| 7246 | } |
dan | d4b56e5 | 2017-12-12 20:04:59 +0000 | [diff] [blame] | 7247 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7248 | switch( cmd.eCmd ){ |
| 7249 | case AR_CMD_CREATE: |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7250 | rc = arCreateOrUpdateCommand(&cmd, 0, 0); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7251 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7252 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7253 | case AR_CMD_EXTRACT: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7254 | rc = arExtractCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7255 | break; |
| 7256 | |
| 7257 | case AR_CMD_LIST: |
drh | b376b3d | 2018-01-10 13:11:51 +0000 | [diff] [blame] | 7258 | rc = arListCommand(&cmd); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7259 | break; |
| 7260 | |
dan | 0d0547f | 2017-12-14 15:40:42 +0000 | [diff] [blame] | 7261 | case AR_CMD_HELP: |
| 7262 | arUsage(pState->out); |
| 7263 | break; |
| 7264 | |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7265 | case AR_CMD_INSERT: |
| 7266 | rc = arCreateOrUpdateCommand(&cmd, 1, 0); |
| 7267 | break; |
| 7268 | |
larrybr | 47061b9 | 2021-11-01 17:22:52 +0000 | [diff] [blame] | 7269 | case AR_CMD_REMOVE: |
| 7270 | rc = arRemoveCommand(&cmd); |
| 7271 | break; |
| 7272 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7273 | default: |
| 7274 | assert( cmd.eCmd==AR_CMD_UPDATE ); |
drh | b17ea91 | 2019-03-25 14:24:19 +0000 | [diff] [blame] | 7275 | rc = arCreateOrUpdateCommand(&cmd, 1, 1); |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7276 | break; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7277 | } |
| 7278 | } |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7279 | end_ar_command: |
| 7280 | if( cmd.db!=pState->db ){ |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7281 | close_db(cmd.db); |
drh | a5676c4 | 2018-01-10 15:17:34 +0000 | [diff] [blame] | 7282 | } |
| 7283 | sqlite3_free(cmd.zSrcTable); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7284 | |
dan | 88be020 | 2017-12-09 17:58:02 +0000 | [diff] [blame] | 7285 | return rc; |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7286 | } |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 7287 | /* End of the ".archive" or ".ar" command logic |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7288 | *******************************************************************************/ |
drh | e37c0e1 | 2018-01-06 19:19:50 +0000 | [diff] [blame] | 7289 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7290 | |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 7291 | #if SQLITE_SHELL_HAVE_RECOVER |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7292 | |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7293 | /* |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7294 | ** This function is used as a callback by the recover extension. Simply |
| 7295 | ** print the supplied SQL statement to stdout. |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7296 | */ |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7297 | static int recoverSqlCb(void *pCtx, const char *zSql){ |
| 7298 | ShellState *pState = (ShellState*)pCtx; |
dan | 80b1f6f | 2022-09-23 11:40:43 +0000 | [diff] [blame] | 7299 | utf8_printf(pState->out, "%s;\n", zSql); |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7300 | return SQLITE_OK; |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | /* |
| 7304 | ** This function is called to recover data from the database. A script |
| 7305 | ** to construct a new database containing all recovered data is output |
| 7306 | ** on stream pState->out. |
| 7307 | */ |
dan | b9b71db | 2019-04-25 16:20:40 +0000 | [diff] [blame] | 7308 | static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7309 | int rc = SQLITE_OK; |
drh | 2cdcc7f | 2022-11-02 14:08:26 +0000 | [diff] [blame] | 7310 | const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */ |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7311 | const char *zLAF = "lost_and_found"; |
dan | f7fea5b | 2022-10-27 18:19:45 +0000 | [diff] [blame] | 7312 | int bFreelist = 1; /* 0 if --ignore-freelist is specified */ |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7313 | int bRowids = 1; /* 0 if --no-rowids */ |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7314 | sqlite3_recover *p = 0; |
| 7315 | int i = 0; |
| 7316 | |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7317 | for(i=1; i<nArg; i++){ |
| 7318 | char *z = azArg[i]; |
| 7319 | int n; |
| 7320 | if( z[0]=='-' && z[1]=='-' ) z++; |
drh | 4245e04 | 2019-06-13 13:52:46 +0000 | [diff] [blame] | 7321 | n = strlen30(z); |
dan | f7fea5b | 2022-10-27 18:19:45 +0000 | [diff] [blame] | 7322 | if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){ |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7323 | bFreelist = 0; |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7324 | }else |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7325 | if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ |
drh | 2cdcc7f | 2022-11-02 14:08:26 +0000 | [diff] [blame] | 7326 | /* This option determines the name of the ATTACH-ed database used |
| 7327 | ** internally by the recovery extension. The default is "" which |
| 7328 | ** means to use a temporary database that is automatically deleted |
| 7329 | ** when closed. This option is undocumented and might disappear at |
| 7330 | ** any moment. */ |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7331 | i++; |
| 7332 | zRecoveryDb = azArg[i]; |
dan | 42ebb01 | 2019-04-27 18:47:03 +0000 | [diff] [blame] | 7333 | }else |
| 7334 | if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ |
| 7335 | i++; |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7336 | zLAF = azArg[i]; |
dan | 8cce6b8 | 2019-09-14 16:44:51 +0000 | [diff] [blame] | 7337 | }else |
| 7338 | if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ |
| 7339 | bRowids = 0; |
dan | c0b4243 | 2019-04-26 15:14:53 +0000 | [diff] [blame] | 7340 | } |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7341 | else{ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 7342 | utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); |
| 7343 | showHelp(pState->out, azArg[0]); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7344 | return 1; |
| 7345 | } |
| 7346 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7347 | |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7348 | p = sqlite3_recover_init_sql( |
| 7349 | pState->db, "main", recoverSqlCb, (void*)pState |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7350 | ); |
| 7351 | |
drh | 2cdcc7f | 2022-11-02 14:08:26 +0000 | [diff] [blame] | 7352 | sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */ |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7353 | sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); |
| 7354 | sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); |
| 7355 | sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); |
| 7356 | |
dan | 46d4398 | 2022-09-08 21:43:18 +0000 | [diff] [blame] | 7357 | sqlite3_recover_run(p); |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7358 | if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ |
| 7359 | const char *zErr = sqlite3_recover_errmsg(p); |
| 7360 | int errCode = sqlite3_recover_errcode(p); |
| 7361 | raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode); |
dan | 9c014f8 | 2019-04-25 19:23:15 +0000 | [diff] [blame] | 7362 | } |
dan | 9a27d65 | 2022-09-08 19:22:29 +0000 | [diff] [blame] | 7363 | rc = sqlite3_recover_finish(p); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7364 | return rc; |
| 7365 | } |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 7366 | #endif /* SQLITE_SHELL_HAVE_RECOVER */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7367 | |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7368 | |
larrybr | 2d27d36 | 2022-04-16 17:53:25 +0000 | [diff] [blame] | 7369 | /* |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7370 | * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. |
| 7371 | * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, |
| 7372 | * close db and set it to 0, and return the columns spec, to later |
| 7373 | * be sqlite3_free()'ed by the caller. |
| 7374 | * The return is 0 when either: |
| 7375 | * (a) The db was not initialized and zCol==0 (There are no columns.) |
| 7376 | * (b) zCol!=0 (Column was added, db initialized as needed.) |
| 7377 | * The 3rd argument, pRenamed, references an out parameter. If the |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7378 | * pointer is non-zero, its referent will be set to a summary of renames |
| 7379 | * done if renaming was necessary, or set to 0 if none was done. The out |
| 7380 | * string (if any) must be sqlite3_free()'ed by the caller. |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7381 | */ |
| 7382 | #ifdef SHELL_DEBUG |
| 7383 | #define rc_err_oom_die(rc) \ |
| 7384 | if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ |
| 7385 | else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ |
| 7386 | fprintf(stderr,"E:%d\n",rc), assert(0) |
| 7387 | #else |
| 7388 | static void rc_err_oom_die(int rc){ |
| 7389 | if( rc==SQLITE_NOMEM ) shell_check_oom(0); |
| 7390 | assert(rc==SQLITE_OK||rc==SQLITE_DONE); |
| 7391 | } |
| 7392 | #endif |
| 7393 | |
| 7394 | #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ |
| 7395 | static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); |
| 7396 | #else /* Otherwise, memory is faster/better for the transient DB. */ |
| 7397 | static const char *zCOL_DB = ":memory:"; |
| 7398 | #endif |
| 7399 | |
| 7400 | /* Define character (as C string) to separate generated column ordinal |
| 7401 | * from protected part of incoming column names. This defaults to "_" |
| 7402 | * so that incoming column identifiers that did not need not be quoted |
| 7403 | * remain usable without being quoted. It must be one character. |
| 7404 | */ |
| 7405 | #ifndef SHELL_AUTOCOLUMN_SEP |
| 7406 | # define AUTOCOLUMN_SEP "_" |
| 7407 | #else |
| 7408 | # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) |
| 7409 | #endif |
| 7410 | |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7411 | static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7412 | /* Queries and D{D,M}L used here */ |
| 7413 | static const char * const zTabMake = "\ |
| 7414 | CREATE TABLE ColNames(\ |
| 7415 | cpos INTEGER PRIMARY KEY,\ |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7416 | name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ |
| 7417 | CREATE VIEW RepeatedNames AS \ |
| 7418 | SELECT DISTINCT t.name FROM ColNames t \ |
| 7419 | WHERE t.name COLLATE NOCASE IN (\ |
| 7420 | SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ |
| 7421 | );\ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7422 | "; |
| 7423 | static const char * const zTabFill = "\ |
| 7424 | INSERT INTO ColNames(name,nlen,chop,reps,suff)\ |
| 7425 | VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ |
| 7426 | "; |
| 7427 | static const char * const zHasDupes = "\ |
| 7428 | SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ |
| 7429 | <count(name) FROM ColNames\ |
| 7430 | "; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7431 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7432 | static const char * const zDedoctor = "\ |
| 7433 | UPDATE ColNames SET chop=iif(\ |
| 7434 | (substring(name,nlen,1) BETWEEN '0' AND '9')\ |
| 7435 | AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\ |
| 7436 | nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ |
| 7437 | 0\ |
| 7438 | )\ |
| 7439 | "; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7440 | #endif |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7441 | static const char * const zSetReps = "\ |
| 7442 | UPDATE ColNames AS t SET reps=\ |
| 7443 | (SELECT count(*) FROM ColNames d \ |
| 7444 | WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ |
| 7445 | COLLATE NOCASE\ |
| 7446 | )\ |
| 7447 | "; |
| 7448 | #ifdef SQLITE_ENABLE_MATH_FUNCTIONS |
| 7449 | static const char * const zColDigits = "\ |
| 7450 | SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \ |
| 7451 | "; |
larrybr | 2d27d36 | 2022-04-16 17:53:25 +0000 | [diff] [blame] | 7452 | #else |
| 7453 | /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */ |
| 7454 | static const char * const zColDigits = "\ |
| 7455 | SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \ |
| 7456 | WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \ |
| 7457 | ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \ |
| 7458 | "; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7459 | #endif |
| 7460 | static const char * const zRenameRank = |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7461 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7462 | "UPDATE ColNames AS t SET suff=" |
| 7463 | "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7464 | #else /* ...RENAME_MINIMAL_ONE_PASS */ |
| 7465 | "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ |
| 7466 | " SELECT 0 AS nlz" |
| 7467 | " UNION" |
| 7468 | " SELECT nlz+1 AS nlz FROM Lzn" |
| 7469 | " WHERE EXISTS(" |
| 7470 | " SELECT 1" |
| 7471 | " FROM ColNames t, ColNames o" |
| 7472 | " WHERE" |
| 7473 | " iif(t.name IN (SELECT * FROM RepeatedNames)," |
| 7474 | " printf('%s"AUTOCOLUMN_SEP"%s'," |
| 7475 | " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," |
| 7476 | " t.name" |
| 7477 | " )" |
| 7478 | " =" |
| 7479 | " iif(o.name IN (SELECT * FROM RepeatedNames)," |
| 7480 | " printf('%s"AUTOCOLUMN_SEP"%s'," |
| 7481 | " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," |
| 7482 | " o.name" |
| 7483 | " )" |
| 7484 | " COLLATE NOCASE" |
| 7485 | " AND o.cpos<>t.cpos" |
| 7486 | " GROUP BY t.cpos" |
| 7487 | " )" |
| 7488 | ") UPDATE Colnames AS t SET" |
| 7489 | " chop = 0," /* No chopping, never touch incoming names. */ |
| 7490 | " suff = iif(name IN (SELECT * FROM RepeatedNames)," |
| 7491 | " printf('"AUTOCOLUMN_SEP"%s', substring(" |
| 7492 | " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," |
| 7493 | " ''" |
| 7494 | " )" |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7495 | #endif |
| 7496 | ; |
| 7497 | static const char * const zCollectVar = "\ |
| 7498 | SELECT\ |
| 7499 | '('||x'0a'\ |
| 7500 | || group_concat(\ |
| 7501 | cname||' TEXT',\ |
| 7502 | ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ |
| 7503 | ||')' AS ColsSpec \ |
| 7504 | FROM (\ |
larrybr | 039132b | 2022-04-09 18:51:49 +0000 | [diff] [blame] | 7505 | SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7506 | FROM ColNames ORDER BY cpos\ |
| 7507 | )"; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7508 | static const char * const zRenamesDone = |
| 7509 | "SELECT group_concat(" |
larrybr | 039132b | 2022-04-09 18:51:49 +0000 | [diff] [blame] | 7510 | " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7511 | " ','||x'0a')" |
| 7512 | "FROM ColNames WHERE suff<>'' OR chop!=0" |
| 7513 | ; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7514 | int rc; |
| 7515 | sqlite3_stmt *pStmt = 0; |
| 7516 | assert(pDb!=0); |
| 7517 | if( zColNew ){ |
| 7518 | /* Add initial or additional column. Init db if necessary. */ |
| 7519 | if( *pDb==0 ){ |
| 7520 | if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; |
| 7521 | #ifdef SHELL_COLFIX_DB |
| 7522 | if(*zCOL_DB!=':') |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7523 | sqlite3_exec(*pDb,"drop table if exists ColNames;" |
| 7524 | "drop view if exists RepeatedNames;",0,0,0); |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7525 | #endif |
| 7526 | rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); |
| 7527 | rc_err_oom_die(rc); |
| 7528 | } |
| 7529 | assert(*pDb!=0); |
| 7530 | rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); |
| 7531 | rc_err_oom_die(rc); |
| 7532 | rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); |
| 7533 | rc_err_oom_die(rc); |
| 7534 | rc = sqlite3_step(pStmt); |
| 7535 | rc_err_oom_die(rc); |
| 7536 | sqlite3_finalize(pStmt); |
| 7537 | return 0; |
| 7538 | }else if( *pDb==0 ){ |
| 7539 | return 0; |
| 7540 | }else{ |
| 7541 | /* Formulate the columns spec, close the DB, zero *pDb. */ |
| 7542 | char *zColsSpec = 0; |
| 7543 | int hasDupes = db_int(*pDb, zHasDupes); |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7544 | int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0; |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7545 | if( hasDupes ){ |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7546 | #ifdef SHELL_COLUMN_RENAME_CLEAN |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7547 | rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); |
| 7548 | rc_err_oom_die(rc); |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7549 | #endif |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7550 | rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); |
| 7551 | rc_err_oom_die(rc); |
| 7552 | rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); |
| 7553 | rc_err_oom_die(rc); |
| 7554 | sqlite3_bind_int(pStmt, 1, nDigits); |
| 7555 | rc = sqlite3_step(pStmt); |
| 7556 | sqlite3_finalize(pStmt); |
| 7557 | assert(rc==SQLITE_DONE); |
| 7558 | } |
| 7559 | assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */ |
| 7560 | rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); |
| 7561 | rc_err_oom_die(rc); |
| 7562 | rc = sqlite3_step(pStmt); |
| 7563 | if( rc==SQLITE_ROW ){ |
| 7564 | zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 7565 | }else{ |
| 7566 | zColsSpec = 0; |
| 7567 | } |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 7568 | if( pzRenamed!=0 ){ |
| 7569 | if( !hasDupes ) *pzRenamed = 0; |
| 7570 | else{ |
| 7571 | sqlite3_finalize(pStmt); |
| 7572 | if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) |
| 7573 | && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7574 | *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
| 7575 | }else |
| 7576 | *pzRenamed = 0; |
| 7577 | } |
| 7578 | } |
larrybr | 42de1c5 | 2022-02-13 22:18:22 +0000 | [diff] [blame] | 7579 | sqlite3_finalize(pStmt); |
| 7580 | sqlite3_close(*pDb); |
| 7581 | *pDb = 0; |
| 7582 | return zColsSpec; |
| 7583 | } |
| 7584 | } |
| 7585 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7586 | /* |
| 7587 | ** If an input line begins with "." then invoke this routine to |
| 7588 | ** process that line. |
| 7589 | ** |
| 7590 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 7591 | */ |
| 7592 | static int do_meta_command(char *zLine, ShellState *p){ |
| 7593 | int h = 1; |
| 7594 | int nArg = 0; |
| 7595 | int n, c; |
| 7596 | int rc = 0; |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 7597 | char *azArg[52]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7598 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 7599 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 7600 | if( p->expert.pExpert ){ |
| 7601 | expertFinish(p, 1, 0); |
| 7602 | } |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 7603 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 7604 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7605 | /* Parse the input line into tokens. |
| 7606 | */ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 7607 | while( zLine[h] && nArg<ArraySize(azArg)-1 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7608 | while( IsSpace(zLine[h]) ){ h++; } |
| 7609 | if( zLine[h]==0 ) break; |
| 7610 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 7611 | int delim = zLine[h++]; |
| 7612 | azArg[nArg++] = &zLine[h]; |
| 7613 | while( zLine[h] && zLine[h]!=delim ){ |
| 7614 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
| 7615 | h++; |
| 7616 | } |
| 7617 | if( zLine[h]==delim ){ |
| 7618 | zLine[h++] = 0; |
| 7619 | } |
| 7620 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
| 7621 | }else{ |
| 7622 | azArg[nArg++] = &zLine[h]; |
| 7623 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 7624 | if( zLine[h] ) zLine[h++] = 0; |
| 7625 | resolve_backslashes(azArg[nArg-1]); |
| 7626 | } |
| 7627 | } |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 7628 | azArg[nArg] = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7629 | |
| 7630 | /* Process the input line. |
| 7631 | */ |
| 7632 | if( nArg==0 ) return 0; /* no tokens, no error */ |
| 7633 | n = strlen30(azArg[0]); |
| 7634 | c = azArg[0][0]; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 7635 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7636 | |
| 7637 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7638 | if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7639 | if( nArg!=2 ){ |
| 7640 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 7641 | rc = 1; |
| 7642 | goto meta_command_exit; |
| 7643 | } |
| 7644 | open_db(p, 0); |
| 7645 | if( booleanValue(azArg[1]) ){ |
| 7646 | sqlite3_set_authorizer(p->db, shellAuth, p); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 7647 | }else if( p->bSafeModePersist ){ |
| 7648 | sqlite3_set_authorizer(p->db, safeModeAuth, p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7649 | }else{ |
| 7650 | sqlite3_set_authorizer(p->db, 0, 0); |
| 7651 | } |
| 7652 | }else |
| 7653 | #endif |
| 7654 | |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 7655 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7656 | && !defined(SQLITE_SHELL_FIDDLE) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7657 | if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){ |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7658 | open_db(p, 0); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 7659 | failIfSafeMode(p, "cannot run .archive in safe mode"); |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 7660 | rc = arDotCommand(p, 0, azArg, nArg); |
dan | fd0245d | 2017-12-07 15:44:29 +0000 | [diff] [blame] | 7661 | }else |
| 7662 | #endif |
| 7663 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7664 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7665 | if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0) |
| 7666 | || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7667 | ){ |
| 7668 | const char *zDestFile = 0; |
| 7669 | const char *zDb = 0; |
| 7670 | sqlite3 *pDest; |
| 7671 | sqlite3_backup *pBackup; |
| 7672 | int j; |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 7673 | int bAsync = 0; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 7674 | const char *zVfs = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 7675 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7676 | for(j=1; j<nArg; j++){ |
| 7677 | const char *z = azArg[j]; |
| 7678 | if( z[0]=='-' ){ |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 7679 | if( z[1]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7680 | if( cli_strcmp(z, "-append")==0 ){ |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 7681 | zVfs = "apndvfs"; |
| 7682 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7683 | if( cli_strcmp(z, "-async")==0 ){ |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 7684 | bAsync = 1; |
| 7685 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7686 | { |
| 7687 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| 7688 | return 1; |
| 7689 | } |
| 7690 | }else if( zDestFile==0 ){ |
| 7691 | zDestFile = azArg[j]; |
| 7692 | }else if( zDb==0 ){ |
| 7693 | zDb = zDestFile; |
| 7694 | zDestFile = azArg[j]; |
| 7695 | }else{ |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 7696 | raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7697 | return 1; |
| 7698 | } |
| 7699 | } |
| 7700 | if( zDestFile==0 ){ |
| 7701 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
| 7702 | return 1; |
| 7703 | } |
| 7704 | if( zDb==0 ) zDb = "main"; |
drh | 69ed38a | 2018-05-14 00:23:08 +0000 | [diff] [blame] | 7705 | rc = sqlite3_open_v2(zDestFile, &pDest, |
| 7706 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7707 | if( rc!=SQLITE_OK ){ |
| 7708 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7709 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7710 | return 1; |
| 7711 | } |
drh | a50bffb | 2018-12-08 01:09:14 +0000 | [diff] [blame] | 7712 | if( bAsync ){ |
| 7713 | sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", |
| 7714 | 0, 0, 0); |
| 7715 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7716 | open_db(p, 0); |
| 7717 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 7718 | if( pBackup==0 ){ |
| 7719 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7720 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7721 | return 1; |
| 7722 | } |
| 7723 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 7724 | sqlite3_backup_finish(pBackup); |
| 7725 | if( rc==SQLITE_DONE ){ |
| 7726 | rc = 0; |
| 7727 | }else{ |
| 7728 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 7729 | rc = 1; |
| 7730 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 7731 | close_db(pDest); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7732 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7733 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7734 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7735 | if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7736 | if( nArg==2 ){ |
| 7737 | bail_on_error = booleanValue(azArg[1]); |
| 7738 | }else{ |
| 7739 | raw_printf(stderr, "Usage: .bail on|off\n"); |
| 7740 | rc = 1; |
| 7741 | } |
| 7742 | }else |
| 7743 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7744 | if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7745 | if( nArg==2 ){ |
| 7746 | if( booleanValue(azArg[1]) ){ |
| 7747 | setBinaryMode(p->out, 1); |
| 7748 | }else{ |
| 7749 | setTextMode(p->out, 1); |
| 7750 | } |
| 7751 | }else{ |
| 7752 | raw_printf(stderr, "Usage: .binary on|off\n"); |
| 7753 | rc = 1; |
| 7754 | } |
| 7755 | }else |
| 7756 | |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 7757 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 7758 | ** routine named test_breakpoint(). |
| 7759 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7760 | if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 7761 | test_breakpoint(); |
| 7762 | }else |
| 7763 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7764 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7765 | if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 7766 | failIfSafeMode(p, "cannot run .cd in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7767 | if( nArg==2 ){ |
| 7768 | #if defined(_WIN32) || defined(WIN32) |
| 7769 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 7770 | rc = !SetCurrentDirectoryW(z); |
| 7771 | sqlite3_free(z); |
| 7772 | #else |
| 7773 | rc = chdir(azArg[1]); |
| 7774 | #endif |
| 7775 | if( rc ){ |
| 7776 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 7777 | rc = 1; |
| 7778 | } |
| 7779 | }else{ |
| 7780 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 7781 | rc = 1; |
| 7782 | } |
| 7783 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7784 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7785 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7786 | if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7787 | if( nArg==2 ){ |
| 7788 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 7789 | }else{ |
| 7790 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 7791 | rc = 1; |
| 7792 | } |
| 7793 | }else |
| 7794 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7795 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7796 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 7797 | ** Then read the content of the testcase-out.txt file and compare against |
| 7798 | ** azArg[1]. If there are differences, report an error and exit. |
| 7799 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7800 | if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7801 | char *zRes = 0; |
| 7802 | output_reset(p); |
| 7803 | if( nArg!=2 ){ |
| 7804 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
| 7805 | rc = 2; |
| 7806 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
| 7807 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 7808 | rc = 2; |
| 7809 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
| 7810 | utf8_printf(stderr, |
| 7811 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 7812 | p->zTestcase, azArg[1], zRes); |
drh | f30d345 | 2017-10-17 13:44:46 +0000 | [diff] [blame] | 7813 | rc = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7814 | }else{ |
| 7815 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
| 7816 | p->nCheck++; |
| 7817 | } |
| 7818 | sqlite3_free(zRes); |
| 7819 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7820 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7821 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7822 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7823 | if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 7824 | failIfSafeMode(p, "cannot run .clone in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7825 | if( nArg==2 ){ |
| 7826 | tryToClone(p, azArg[1]); |
| 7827 | }else{ |
| 7828 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| 7829 | rc = 1; |
| 7830 | } |
| 7831 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 7832 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7833 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7834 | if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 7835 | if( nArg==1 ){ |
| 7836 | /* List available connections */ |
| 7837 | int i; |
| 7838 | for(i=0; i<ArraySize(p->aAuxDb); i++){ |
| 7839 | const char *zFile = p->aAuxDb[i].zDbFilename; |
| 7840 | if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ |
| 7841 | zFile = "(not open)"; |
| 7842 | }else if( zFile==0 ){ |
| 7843 | zFile = "(memory)"; |
| 7844 | }else if( zFile[0]==0 ){ |
| 7845 | zFile = "(temporary-file)"; |
| 7846 | } |
| 7847 | if( p->pAuxDb == &p->aAuxDb[i] ){ |
| 7848 | utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile); |
| 7849 | }else if( p->aAuxDb[i].db!=0 ){ |
| 7850 | utf8_printf(stdout, " %d: %s\n", i, zFile); |
| 7851 | } |
| 7852 | } |
| 7853 | }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ |
| 7854 | int i = azArg[1][0] - '0'; |
| 7855 | if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ |
| 7856 | p->pAuxDb->db = p->db; |
| 7857 | p->pAuxDb = &p->aAuxDb[i]; |
| 7858 | globalDb = p->db = p->pAuxDb->db; |
| 7859 | p->pAuxDb->db = 0; |
| 7860 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7861 | }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0 |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 7862 | && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ |
| 7863 | int i = azArg[2][0] - '0'; |
| 7864 | if( i<0 || i>=ArraySize(p->aAuxDb) ){ |
| 7865 | /* No-op */ |
| 7866 | }else if( p->pAuxDb == &p->aAuxDb[i] ){ |
| 7867 | raw_printf(stderr, "cannot close the active database connection\n"); |
| 7868 | rc = 1; |
| 7869 | }else if( p->aAuxDb[i].db ){ |
| 7870 | session_close_all(p, i); |
| 7871 | close_db(p->aAuxDb[i].db); |
| 7872 | p->aAuxDb[i].db = 0; |
| 7873 | } |
| 7874 | }else{ |
| 7875 | raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); |
| 7876 | rc = 1; |
| 7877 | } |
| 7878 | }else |
| 7879 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7880 | if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){ |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7881 | char **azName = 0; |
| 7882 | int nName = 0; |
| 7883 | sqlite3_stmt *pStmt; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7884 | int i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7885 | open_db(p, 0); |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7886 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
| 7887 | if( rc ){ |
| 7888 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7889 | rc = 1; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7890 | }else{ |
| 7891 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 7892 | const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); |
| 7893 | const char *zFile = (const char*)sqlite3_column_text(pStmt,2); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 7894 | if( zSchema==0 || zFile==0 ) continue; |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7895 | azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 7896 | shell_check_oom(azName); |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7897 | azName[nName*2] = strdup(zSchema); |
| 7898 | azName[nName*2+1] = strdup(zFile); |
| 7899 | nName++; |
| 7900 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7901 | } |
drh | 60081a0 | 2020-08-26 19:07:18 +0000 | [diff] [blame] | 7902 | sqlite3_finalize(pStmt); |
| 7903 | for(i=0; i<nName; i++){ |
| 7904 | int eTxn = sqlite3_txn_state(p->db, azName[i*2]); |
| 7905 | int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); |
| 7906 | const char *z = azName[i*2+1]; |
| 7907 | utf8_printf(p->out, "%s: %s %s%s\n", |
| 7908 | azName[i*2], |
| 7909 | z && z[0] ? z : "\"\"", |
| 7910 | bRdonly ? "r/o" : "r/w", |
| 7911 | eTxn==SQLITE_TXN_NONE ? "" : |
| 7912 | eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); |
| 7913 | free(azName[i*2]); |
| 7914 | free(azName[i*2+1]); |
| 7915 | } |
| 7916 | sqlite3_free(azName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7917 | }else |
| 7918 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7919 | if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){ |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 7920 | static const struct DbConfigChoices { |
| 7921 | const char *zName; |
| 7922 | int op; |
| 7923 | } aDbConfig[] = { |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 7924 | { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, |
| 7925 | { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, |
| 7926 | { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 7927 | { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 7928 | { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 7929 | { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, |
drh | 11d88e6 | 2019-08-15 21:27:20 +0000 | [diff] [blame] | 7930 | { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 7931 | { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 7932 | { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, |
| 7933 | { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 7934 | { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, |
| 7935 | { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, |
drh | 0a6873b | 2019-06-14 21:25:25 +0000 | [diff] [blame] | 7936 | { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 7937 | { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, |
drh | b77da37 | 2020-01-07 16:09:11 +0000 | [diff] [blame] | 7938 | { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, |
dan | 07312a6 | 2019-06-21 14:05:27 +0000 | [diff] [blame] | 7939 | { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 7940 | }; |
| 7941 | int ii, v; |
| 7942 | open_db(p, 0); |
| 7943 | for(ii=0; ii<ArraySize(aDbConfig); ii++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7944 | if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 7945 | if( nArg>=3 ){ |
| 7946 | sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); |
| 7947 | } |
| 7948 | sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); |
drh | b945bcd | 2019-12-31 22:52:10 +0000 | [diff] [blame] | 7949 | utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); |
drh | 7df0119 | 2018-04-28 12:43:16 +0000 | [diff] [blame] | 7950 | if( nArg>1 ) break; |
| 7951 | } |
| 7952 | if( nArg>1 && ii==ArraySize(aDbConfig) ){ |
| 7953 | utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); |
| 7954 | utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); |
| 7955 | } |
| 7956 | }else |
| 7957 | |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 7958 | #if SQLITE_SHELL_HAVE_RECOVER |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7959 | if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7960 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 7961 | }else |
| 7962 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7963 | if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7964 | open_db(p, 0); |
dan | b9b71db | 2019-04-25 16:20:40 +0000 | [diff] [blame] | 7965 | rc = recoverDatabaseCmd(p, nArg, azArg); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7966 | }else |
stephan | 3d42083 | 2022-10-27 03:56:01 +0000 | [diff] [blame] | 7967 | #endif /* SQLITE_SHELL_HAVE_RECOVER */ |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 7968 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7969 | if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){ |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 7970 | char *zLike = 0; |
| 7971 | char *zSql; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7972 | int i; |
| 7973 | int savedShowHeader = p->showHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 7974 | int savedShellFlags = p->shellFlgs; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 7975 | ShellClearFlag(p, |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 7976 | SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo |
| 7977 | |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7978 | for(i=1; i<nArg; i++){ |
| 7979 | if( azArg[i][0]=='-' ){ |
| 7980 | const char *z = azArg[i]+1; |
| 7981 | if( z[0]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7982 | if( cli_strcmp(z,"preserve-rowids")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7983 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 7984 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 7985 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 7986 | rc = 1; |
drh | 1d29fd8 | 2020-05-29 19:03:03 +0000 | [diff] [blame] | 7987 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7988 | goto meta_command_exit; |
| 7989 | #else |
| 7990 | ShellSetFlag(p, SHFLG_PreserveRowid); |
| 7991 | #endif |
| 7992 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7993 | if( cli_strcmp(z,"newlines")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 7994 | ShellSetFlag(p, SHFLG_Newlines); |
| 7995 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7996 | if( cli_strcmp(z,"data-only")==0 ){ |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 7997 | ShellSetFlag(p, SHFLG_DumpDataOnly); |
| 7998 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 7999 | if( cli_strcmp(z,"nosys")==0 ){ |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8000 | ShellSetFlag(p, SHFLG_DumpNoSys); |
| 8001 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8002 | { |
| 8003 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 8004 | rc = 1; |
drh | 1d29fd8 | 2020-05-29 19:03:03 +0000 | [diff] [blame] | 8005 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8006 | goto meta_command_exit; |
| 8007 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8008 | }else{ |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8009 | /* azArg[i] contains a LIKE pattern. This ".dump" request should |
| 8010 | ** only dump data for tables for which either the table name matches |
| 8011 | ** the LIKE pattern, or the table appears to be a shadow table of |
| 8012 | ** a virtual table for which the name matches the LIKE pattern. |
| 8013 | */ |
| 8014 | char *zExpr = sqlite3_mprintf( |
| 8015 | "name LIKE %Q ESCAPE '\\' OR EXISTS (" |
| 8016 | " SELECT 1 FROM sqlite_schema WHERE " |
| 8017 | " name LIKE %Q ESCAPE '\\' AND" |
| 8018 | " sql LIKE 'CREATE VIRTUAL TABLE%%' AND" |
| 8019 | " substr(o.name, 1, length(name)+1) == (name||'_')" |
| 8020 | ")", azArg[i], azArg[i] |
| 8021 | ); |
| 8022 | |
| 8023 | if( zLike ){ |
| 8024 | zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr); |
| 8025 | }else{ |
| 8026 | zLike = zExpr; |
| 8027 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8028 | } |
| 8029 | } |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8030 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8031 | open_db(p, 0); |
dan | 68cb86e | 2019-04-20 20:57:28 +0000 | [diff] [blame] | 8032 | |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8033 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8034 | /* When playing back a "dump", the content might appear in an order |
| 8035 | ** which causes immediate foreign key constraints to be violated. |
| 8036 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 8037 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 8038 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 8039 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8040 | p->writableSchema = 0; |
| 8041 | p->showHeader = 0; |
| 8042 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8043 | ** 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] | 8044 | ** corrupt. */ |
| 8045 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 8046 | p->nErr = 0; |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8047 | if( zLike==0 ) zLike = sqlite3_mprintf("true"); |
| 8048 | zSql = sqlite3_mprintf( |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8049 | "SELECT name, type, sql FROM sqlite_schema AS o " |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8050 | "WHERE (%s) AND type=='table'" |
| 8051 | " AND sql NOT NULL" |
| 8052 | " ORDER BY tbl_name='sqlite_sequence', rowid", |
| 8053 | zLike |
| 8054 | ); |
| 8055 | run_schema_dump_query(p,zSql); |
| 8056 | sqlite3_free(zSql); |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8057 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8058 | zSql = sqlite3_mprintf( |
dan | 78a9d75 | 2021-05-25 11:39:14 +0000 | [diff] [blame] | 8059 | "SELECT sql FROM sqlite_schema AS o " |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8060 | "WHERE (%s) AND sql NOT NULL" |
| 8061 | " AND type IN ('index','trigger','view')", |
| 8062 | zLike |
| 8063 | ); |
| 8064 | run_table_dump_query(p, zSql); |
| 8065 | sqlite3_free(zSql); |
| 8066 | } |
drh | 8e9297f | 2020-03-25 12:50:13 +0000 | [diff] [blame] | 8067 | sqlite3_free(zLike); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8068 | if( p->writableSchema ){ |
| 8069 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 8070 | p->writableSchema = 0; |
| 8071 | } |
| 8072 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 8073 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
drh | c196219 | 2020-10-12 16:54:28 +0000 | [diff] [blame] | 8074 | if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
| 8075 | raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); |
| 8076 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8077 | p->showHeader = savedShowHeader; |
drh | f213b33 | 2018-07-05 17:35:46 +0000 | [diff] [blame] | 8078 | p->shellFlgs = savedShellFlags; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8079 | }else |
| 8080 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8081 | if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8082 | if( nArg==2 ){ |
| 8083 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
| 8084 | }else{ |
| 8085 | raw_printf(stderr, "Usage: .echo on|off\n"); |
| 8086 | rc = 1; |
| 8087 | } |
| 8088 | }else |
| 8089 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8090 | if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8091 | if( nArg==2 ){ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 8092 | p->autoEQPtest = 0; |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8093 | if( p->autoEQPtrace ){ |
| 8094 | if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); |
| 8095 | p->autoEQPtrace = 0; |
| 8096 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8097 | if( cli_strcmp(azArg[1],"full")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8098 | p->autoEQP = AUTOEQP_full; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8099 | }else if( cli_strcmp(azArg[1],"trigger")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 8100 | p->autoEQP = AUTOEQP_trigger; |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8101 | #ifdef SQLITE_DEBUG |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8102 | }else if( cli_strcmp(azArg[1],"test")==0 ){ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 8103 | p->autoEQP = AUTOEQP_on; |
| 8104 | p->autoEQPtest = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8105 | }else if( cli_strcmp(azArg[1],"trace")==0 ){ |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8106 | p->autoEQP = AUTOEQP_full; |
| 8107 | p->autoEQPtrace = 1; |
| 8108 | open_db(p, 0); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8109 | 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] | 8110 | sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); |
| 8111 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8112 | }else{ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 8113 | p->autoEQP = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8114 | } |
| 8115 | }else{ |
drh | b4e5039 | 2019-01-26 15:40:04 +0000 | [diff] [blame] | 8116 | raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8117 | rc = 1; |
| 8118 | } |
| 8119 | }else |
| 8120 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 8121 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8122 | if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8123 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 8124 | rc = 2; |
| 8125 | }else |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 8126 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8127 | |
| 8128 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 8129 | ** retained purely for backwards compatibility */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8130 | if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8131 | int val = 1; |
| 8132 | if( nArg>=2 ){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8133 | if( cli_strcmp(azArg[1],"auto")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8134 | val = 99; |
| 8135 | }else{ |
| 8136 | val = booleanValue(azArg[1]); |
| 8137 | } |
| 8138 | } |
| 8139 | if( val==1 && p->mode!=MODE_Explain ){ |
| 8140 | p->normalMode = p->mode; |
| 8141 | p->mode = MODE_Explain; |
| 8142 | p->autoExplain = 0; |
| 8143 | }else if( val==0 ){ |
| 8144 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 8145 | p->autoExplain = 0; |
| 8146 | }else if( val==99 ){ |
| 8147 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 8148 | p->autoExplain = 1; |
| 8149 | } |
| 8150 | }else |
| 8151 | |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8152 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8153 | if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){ |
drh | fe46317 | 2021-12-16 17:57:21 +0000 | [diff] [blame] | 8154 | if( p->bSafeMode ){ |
| 8155 | raw_printf(stderr, |
| 8156 | "Cannot run experimental commands such as \"%s\" in safe mode\n", |
| 8157 | azArg[0]); |
| 8158 | rc = 1; |
| 8159 | }else{ |
| 8160 | open_db(p, 0); |
| 8161 | expertDotCommand(p, azArg, nArg); |
| 8162 | } |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8163 | }else |
dan | 6b046be | 2018-01-09 15:25:55 +0000 | [diff] [blame] | 8164 | #endif |
dan | 43efc18 | 2017-12-19 17:42:13 +0000 | [diff] [blame] | 8165 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8166 | if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8167 | static const struct { |
| 8168 | const char *zCtrlName; /* Name of a test-control option */ |
| 8169 | int ctrlCode; /* Integer code for that option */ |
| 8170 | const char *zUsage; /* Usage notes */ |
| 8171 | } aCtrl[] = { |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8172 | { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8173 | { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8174 | { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, |
| 8175 | { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8176 | { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, |
| 8177 | /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ |
| 8178 | { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8179 | { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8180 | { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, |
| 8181 | { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, |
| 8182 | /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8183 | }; |
| 8184 | int filectrl = -1; |
| 8185 | int iCtrl = -1; |
drh | 4245e04 | 2019-06-13 13:52:46 +0000 | [diff] [blame] | 8186 | sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ |
| 8187 | int isOk = 0; /* 0: usage 1: %lld 2: no-result */ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8188 | int n2, i; |
| 8189 | const char *zCmd = 0; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8190 | const char *zSchema = 0; |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8191 | |
| 8192 | open_db(p, 0); |
| 8193 | zCmd = nArg>=2 ? azArg[1] : "help"; |
| 8194 | |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8195 | if( zCmd[0]=='-' |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8196 | && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8197 | && nArg>=4 |
| 8198 | ){ |
| 8199 | zSchema = azArg[2]; |
| 8200 | for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; |
| 8201 | nArg -= 2; |
| 8202 | zCmd = azArg[1]; |
| 8203 | } |
| 8204 | |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8205 | /* The argument can optionally begin with "-" or "--" */ |
| 8206 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 8207 | zCmd++; |
| 8208 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 8209 | } |
| 8210 | |
| 8211 | /* --help lists all file-controls */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8212 | if( cli_strcmp(zCmd,"help")==0 ){ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8213 | utf8_printf(p->out, "Available file-controls:\n"); |
| 8214 | for(i=0; i<ArraySize(aCtrl); i++){ |
| 8215 | utf8_printf(p->out, " .filectrl %s %s\n", |
| 8216 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
| 8217 | } |
| 8218 | rc = 1; |
| 8219 | goto meta_command_exit; |
| 8220 | } |
| 8221 | |
| 8222 | /* convert filectrl text option to value. allow any unique prefix |
| 8223 | ** of the option name, or a numerical value. */ |
| 8224 | n2 = strlen30(zCmd); |
| 8225 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8226 | if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8227 | if( filectrl<0 ){ |
| 8228 | filectrl = aCtrl[i].ctrlCode; |
| 8229 | iCtrl = i; |
| 8230 | }else{ |
| 8231 | utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" |
| 8232 | "Use \".filectrl --help\" for help\n", zCmd); |
| 8233 | rc = 1; |
| 8234 | goto meta_command_exit; |
| 8235 | } |
| 8236 | } |
| 8237 | } |
| 8238 | if( filectrl<0 ){ |
| 8239 | utf8_printf(stderr,"Error: unknown file-control: %s\n" |
| 8240 | "Use \".filectrl --help\" for help\n", zCmd); |
| 8241 | }else{ |
| 8242 | switch(filectrl){ |
| 8243 | case SQLITE_FCNTL_SIZE_LIMIT: { |
| 8244 | if( nArg!=2 && nArg!=3 ) break; |
| 8245 | iRes = nArg==3 ? integerValue(azArg[2]) : -1; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8246 | sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8247 | isOk = 1; |
| 8248 | break; |
| 8249 | } |
| 8250 | case SQLITE_FCNTL_LOCK_TIMEOUT: |
| 8251 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 8252 | int x; |
| 8253 | if( nArg!=3 ) break; |
| 8254 | x = (int)integerValue(azArg[2]); |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8255 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8256 | isOk = 2; |
| 8257 | break; |
| 8258 | } |
| 8259 | case SQLITE_FCNTL_PERSIST_WAL: |
| 8260 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 8261 | int x; |
| 8262 | if( nArg!=2 && nArg!=3 ) break; |
| 8263 | x = nArg==3 ? booleanValue(azArg[2]) : -1; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8264 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8265 | iRes = x; |
| 8266 | isOk = 1; |
| 8267 | break; |
| 8268 | } |
drh | 18a4bbd | 2020-12-17 15:17:42 +0000 | [diff] [blame] | 8269 | case SQLITE_FCNTL_DATA_VERSION: |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8270 | case SQLITE_FCNTL_HAS_MOVED: { |
| 8271 | int x; |
| 8272 | if( nArg!=2 ) break; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8273 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8274 | iRes = x; |
| 8275 | isOk = 1; |
| 8276 | break; |
| 8277 | } |
| 8278 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 8279 | char *z = 0; |
| 8280 | if( nArg!=2 ) break; |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8281 | sqlite3_file_control(p->db, zSchema, filectrl, &z); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8282 | if( z ){ |
| 8283 | utf8_printf(p->out, "%s\n", z); |
| 8284 | sqlite3_free(z); |
| 8285 | } |
| 8286 | isOk = 2; |
| 8287 | break; |
| 8288 | } |
drh | 541ef2c | 2020-04-20 16:21:30 +0000 | [diff] [blame] | 8289 | case SQLITE_FCNTL_RESERVE_BYTES: { |
| 8290 | int x; |
| 8291 | if( nArg>=3 ){ |
| 8292 | x = atoi(azArg[2]); |
| 8293 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
| 8294 | } |
| 8295 | x = -1; |
| 8296 | sqlite3_file_control(p->db, zSchema, filectrl, &x); |
| 8297 | utf8_printf(p->out,"%d\n", x); |
| 8298 | isOk = 2; |
| 8299 | break; |
| 8300 | } |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8301 | } |
| 8302 | } |
| 8303 | if( isOk==0 && iCtrl>=0 ){ |
| 8304 | utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
| 8305 | rc = 1; |
| 8306 | }else if( isOk==1 ){ |
drh | e250076 | 2019-06-13 14:07:41 +0000 | [diff] [blame] | 8307 | char zBuf[100]; |
| 8308 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); |
| 8309 | raw_printf(p->out, "%s\n", zBuf); |
drh | d985f72 | 2019-06-05 14:29:53 +0000 | [diff] [blame] | 8310 | } |
| 8311 | }else |
| 8312 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8313 | if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8314 | ShellState data; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8315 | int doStats = 0; |
| 8316 | memcpy(&data, p, sizeof(data)); |
| 8317 | data.showHeader = 0; |
| 8318 | data.cMode = data.mode = MODE_Semi; |
| 8319 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 8320 | data.cMode = data.mode = MODE_Pretty; |
| 8321 | nArg = 1; |
| 8322 | } |
| 8323 | if( nArg!=1 ){ |
| 8324 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| 8325 | rc = 1; |
| 8326 | goto meta_command_exit; |
| 8327 | } |
| 8328 | open_db(p, 0); |
| 8329 | rc = sqlite3_exec(p->db, |
| 8330 | "SELECT sql FROM" |
| 8331 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8332 | " FROM sqlite_schema UNION ALL" |
| 8333 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8334 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 69935c0 | 2021-06-25 11:14:10 +0000 | [diff] [blame] | 8335 | "ORDER BY x", |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8336 | callback, &data, 0 |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8337 | ); |
| 8338 | if( rc==SQLITE_OK ){ |
| 8339 | sqlite3_stmt *pStmt; |
| 8340 | rc = sqlite3_prepare_v2(p->db, |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8341 | "SELECT rowid FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8342 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 8343 | -1, &pStmt, 0); |
| 8344 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 8345 | sqlite3_finalize(pStmt); |
| 8346 | } |
| 8347 | if( doStats==0 ){ |
| 8348 | raw_printf(p->out, "/* No STAT tables available */\n"); |
| 8349 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8350 | raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8351 | data.cMode = data.mode = MODE_Insert; |
| 8352 | data.zDestTable = "sqlite_stat1"; |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8353 | shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8354 | data.zDestTable = "sqlite_stat4"; |
drh | f83d501 | 2021-05-03 13:35:00 +0000 | [diff] [blame] | 8355 | shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8356 | raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8357 | } |
| 8358 | }else |
| 8359 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8360 | if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8361 | if( nArg==2 ){ |
| 8362 | p->showHeader = booleanValue(azArg[1]); |
drh | c060508 | 2020-06-05 00:54:27 +0000 | [diff] [blame] | 8363 | p->shellFlgs |= SHFLG_HeaderSet; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8364 | }else{ |
| 8365 | raw_printf(stderr, "Usage: .headers on|off\n"); |
| 8366 | rc = 1; |
| 8367 | } |
| 8368 | }else |
| 8369 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8370 | if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){ |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 8371 | if( nArg>=2 ){ |
drh | e93f826 | 2018-10-11 16:53:37 +0000 | [diff] [blame] | 8372 | n = showHelp(p->out, azArg[1]); |
drh | 98aa2ab | 2018-09-26 16:53:51 +0000 | [diff] [blame] | 8373 | if( n==0 ){ |
| 8374 | utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); |
| 8375 | } |
| 8376 | }else{ |
| 8377 | showHelp(p->out, 0); |
| 8378 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8379 | }else |
| 8380 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 8381 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8382 | if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8383 | char *zTable = 0; /* Insert data into this table */ |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8384 | char *zSchema = 0; /* within this schema (may default to "main") */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8385 | char *zFile = 0; /* Name of file to extra content from */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8386 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 8387 | int nCol; /* Number of columns in the table */ |
| 8388 | int nByte; /* Number of bytes in an SQL string */ |
| 8389 | int i, j; /* Loop counters */ |
| 8390 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 8391 | int nSep; /* Number of bytes in p->colSeparator[] */ |
| 8392 | char *zSql; /* An SQL statement */ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8393 | char *zFullTabName; /* Table name with schema if applicable */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8394 | ImportCtx sCtx; /* Reader context */ |
| 8395 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8396 | int eVerbose = 0; /* Larger for more console output */ |
| 8397 | int nSkip = 0; /* Initial lines to skip */ |
| 8398 | int useOutputMode = 1; /* Use output mode to determine separators */ |
drh | e684ac6 | 2022-03-08 13:59:46 +0000 | [diff] [blame] | 8399 | char *zCreate = 0; /* CREATE TABLE statement text */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8400 | |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8401 | failIfSafeMode(p, "cannot run .import in safe mode"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8402 | memset(&sCtx, 0, sizeof(sCtx)); |
| 8403 | if( p->mode==MODE_Ascii ){ |
| 8404 | xRead = ascii_read_one_field; |
| 8405 | }else{ |
| 8406 | xRead = csv_read_one_field; |
| 8407 | } |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8408 | rc = 1; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8409 | for(i=1; i<nArg; i++){ |
| 8410 | char *z = azArg[i]; |
| 8411 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 8412 | if( z[0]!='-' ){ |
| 8413 | if( zFile==0 ){ |
| 8414 | zFile = z; |
| 8415 | }else if( zTable==0 ){ |
| 8416 | zTable = z; |
| 8417 | }else{ |
| 8418 | utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); |
| 8419 | showHelp(p->out, "import"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8420 | goto meta_command_exit; |
| 8421 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8422 | }else if( cli_strcmp(z,"-v")==0 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8423 | eVerbose++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8424 | }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){ |
larrybr | 738d7b9 | 2022-01-13 21:22:54 +0000 | [diff] [blame] | 8425 | zSchema = azArg[++i]; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8426 | }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8427 | nSkip = integerValue(azArg[++i]); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8428 | }else if( cli_strcmp(z,"-ascii")==0 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8429 | sCtx.cColSep = SEP_Unit[0]; |
| 8430 | sCtx.cRowSep = SEP_Record[0]; |
| 8431 | xRead = ascii_read_one_field; |
| 8432 | useOutputMode = 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8433 | }else if( cli_strcmp(z,"-csv")==0 ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8434 | sCtx.cColSep = ','; |
| 8435 | sCtx.cRowSep = '\n'; |
| 8436 | xRead = csv_read_one_field; |
| 8437 | useOutputMode = 0; |
| 8438 | }else{ |
| 8439 | utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); |
| 8440 | showHelp(p->out, "import"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8441 | goto meta_command_exit; |
| 8442 | } |
| 8443 | } |
| 8444 | if( zTable==0 ){ |
| 8445 | utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", |
| 8446 | zFile==0 ? "FILE" : "TABLE"); |
| 8447 | showHelp(p->out, "import"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8448 | goto meta_command_exit; |
| 8449 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8450 | seenInterrupt = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8451 | open_db(p, 0); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8452 | if( useOutputMode ){ |
| 8453 | /* If neither the --csv or --ascii options are specified, then set |
| 8454 | ** the column and row separator characters from the output mode. */ |
| 8455 | nSep = strlen30(p->colSeparator); |
| 8456 | if( nSep==0 ){ |
| 8457 | raw_printf(stderr, |
| 8458 | "Error: non-null column separator required for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8459 | goto meta_command_exit; |
| 8460 | } |
| 8461 | if( nSep>1 ){ |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8462 | raw_printf(stderr, |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8463 | "Error: multi-character column separators not allowed" |
| 8464 | " for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8465 | goto meta_command_exit; |
| 8466 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8467 | nSep = strlen30(p->rowSeparator); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8468 | if( nSep==0 ){ |
| 8469 | raw_printf(stderr, |
| 8470 | "Error: non-null row separator required for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8471 | goto meta_command_exit; |
| 8472 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8473 | if( nSep==2 && p->mode==MODE_Csv |
| 8474 | && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 |
| 8475 | ){ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8476 | /* When importing CSV (only), if the row separator is set to the |
| 8477 | ** default output row separator, change it to the default input |
| 8478 | ** row separator. This avoids having to maintain different input |
| 8479 | ** and output row separators. */ |
| 8480 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 8481 | nSep = strlen30(p->rowSeparator); |
| 8482 | } |
| 8483 | if( nSep>1 ){ |
| 8484 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
| 8485 | " for import\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8486 | goto meta_command_exit; |
| 8487 | } |
| 8488 | sCtx.cColSep = p->colSeparator[0]; |
| 8489 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8490 | } |
| 8491 | sCtx.zFile = zFile; |
| 8492 | sCtx.nLine = 1; |
| 8493 | if( sCtx.zFile[0]=='|' ){ |
| 8494 | #ifdef SQLITE_OMIT_POPEN |
| 8495 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8496 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8497 | #else |
| 8498 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 8499 | sCtx.zFile = "<pipe>"; |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8500 | sCtx.xCloser = pclose; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8501 | #endif |
| 8502 | }else{ |
| 8503 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8504 | sCtx.xCloser = fclose; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8505 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8506 | if( sCtx.in==0 ){ |
| 8507 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8508 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8509 | } |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8510 | if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ |
| 8511 | char zSep[2]; |
| 8512 | zSep[1] = 0; |
| 8513 | zSep[0] = sCtx.cColSep; |
| 8514 | utf8_printf(p->out, "Column separator "); |
| 8515 | output_c_string(p->out, zSep); |
| 8516 | utf8_printf(p->out, ", row separator "); |
| 8517 | zSep[0] = sCtx.cRowSep; |
| 8518 | output_c_string(p->out, zSep); |
| 8519 | utf8_printf(p->out, "\n"); |
| 8520 | } |
larrybr | 2f5f674 | 2022-05-09 12:29:47 +0000 | [diff] [blame] | 8521 | sCtx.z = sqlite3_malloc64(120); |
| 8522 | if( sCtx.z==0 ){ |
| 8523 | import_cleanup(&sCtx); |
| 8524 | shell_out_of_memory(); |
| 8525 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8526 | /* Below, resources must be freed before exit. */ |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8527 | while( (nSkip--)>0 ){ |
| 8528 | while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8529 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8530 | if( zSchema!=0 ){ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8531 | zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8532 | }else{ |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8533 | zFullTabName = sqlite3_mprintf("\"%w\"", zTable); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8534 | } |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8535 | zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName); |
| 8536 | if( zSql==0 || zFullTabName==0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8537 | import_cleanup(&sCtx); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8538 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8539 | } |
| 8540 | nByte = strlen30(zSql); |
| 8541 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 8542 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| 8543 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 8544 | sqlite3 *dbCols = 0; |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8545 | char *zRenames = 0; |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 8546 | char *zColDefs; |
drh | e684ac6 | 2022-03-08 13:59:46 +0000 | [diff] [blame] | 8547 | zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8548 | while( xRead(&sCtx) ){ |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 8549 | zAutoColumn(sCtx.z, &dbCols, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8550 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
larrybr | 4c5c621 | 2022-02-11 01:21:09 +0000 | [diff] [blame] | 8551 | } |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8552 | zColDefs = zAutoColumn(0, &dbCols, &zRenames); |
| 8553 | if( zRenames!=0 ){ |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 8554 | utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr, |
larrybr | 3363386 | 2022-02-14 01:12:46 +0000 | [diff] [blame] | 8555 | "Columns renamed during .import %s due to duplicates:\n" |
| 8556 | "%s\n", sCtx.zFile, zRenames); |
| 8557 | sqlite3_free(zRenames); |
larrybr | a033727 | 2022-02-11 13:40:25 +0000 | [diff] [blame] | 8558 | } |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 8559 | assert(dbCols==0); |
| 8560 | if( zColDefs==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8561 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8562 | import_fail: |
| 8563 | sqlite3_free(zCreate); |
| 8564 | sqlite3_free(zSql); |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8565 | sqlite3_free(zFullTabName); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8566 | import_cleanup(&sCtx); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8567 | rc = 1; |
| 8568 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8569 | } |
larrybr | 58a53d6 | 2022-02-10 03:21:48 +0000 | [diff] [blame] | 8570 | zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8571 | if( eVerbose>=1 ){ |
| 8572 | utf8_printf(p->out, "%s\n", zCreate); |
| 8573 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8574 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8575 | if( rc ){ |
larrybr | ce0b5e4 | 2022-01-14 16:29:45 +0000 | [diff] [blame] | 8576 | utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8577 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8578 | } |
larrybr | ce0b5e4 | 2022-01-14 16:29:45 +0000 | [diff] [blame] | 8579 | sqlite3_free(zCreate); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8580 | zCreate = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8581 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 8582 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8583 | if( rc ){ |
| 8584 | if (pStmt) sqlite3_finalize(pStmt); |
| 8585 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8586 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8587 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8588 | sqlite3_free(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8589 | nCol = sqlite3_column_count(pStmt); |
| 8590 | sqlite3_finalize(pStmt); |
| 8591 | pStmt = 0; |
| 8592 | if( nCol==0 ) return 0; /* no columns, no error */ |
| 8593 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
| 8594 | if( zSql==0 ){ |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8595 | import_cleanup(&sCtx); |
drh | 4b5345c | 2018-04-24 13:07:40 +0000 | [diff] [blame] | 8596 | shell_out_of_memory(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8597 | } |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8598 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8599 | j = strlen30(zSql); |
| 8600 | for(i=1; i<nCol; i++){ |
| 8601 | zSql[j++] = ','; |
| 8602 | zSql[j++] = '?'; |
| 8603 | } |
| 8604 | zSql[j++] = ')'; |
| 8605 | zSql[j] = 0; |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8606 | if( eVerbose>=2 ){ |
| 8607 | utf8_printf(p->out, "Insert using: %s\n", zSql); |
| 8608 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8609 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8610 | if( rc ){ |
| 8611 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 8612 | if (pStmt) sqlite3_finalize(pStmt); |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8613 | goto import_fail; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8614 | } |
larrybr | 53e1186 | 2022-03-06 23:41:21 +0000 | [diff] [blame] | 8615 | sqlite3_free(zSql); |
larrybr | 41f4670 | 2022-03-07 00:14:52 +0000 | [diff] [blame] | 8616 | sqlite3_free(zFullTabName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8617 | needCommit = sqlite3_get_autocommit(p->db); |
| 8618 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 8619 | do{ |
| 8620 | int startLine = sCtx.nLine; |
| 8621 | for(i=0; i<nCol; i++){ |
| 8622 | char *z = xRead(&sCtx); |
| 8623 | /* |
| 8624 | ** Did we reach end-of-file before finding any columns? |
| 8625 | ** If so, stop instead of NULL filling the remaining columns. |
| 8626 | */ |
| 8627 | if( z==0 && i==0 ) break; |
| 8628 | /* |
| 8629 | ** Did we reach end-of-file OR end-of-line before finding any |
| 8630 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 8631 | ** the remaining columns. |
| 8632 | */ |
| 8633 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| 8634 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 8635 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 8636 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 8637 | "filling the rest with NULL\n", |
| 8638 | sCtx.zFile, startLine, nCol, i+1); |
| 8639 | i += 2; |
| 8640 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| 8641 | } |
| 8642 | } |
| 8643 | if( sCtx.cTerm==sCtx.cColSep ){ |
| 8644 | do{ |
| 8645 | xRead(&sCtx); |
| 8646 | i++; |
| 8647 | }while( sCtx.cTerm==sCtx.cColSep ); |
| 8648 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| 8649 | "extras ignored\n", |
| 8650 | sCtx.zFile, startLine, nCol, i); |
| 8651 | } |
| 8652 | if( i>=nCol ){ |
| 8653 | sqlite3_step(pStmt); |
| 8654 | rc = sqlite3_reset(pStmt); |
| 8655 | if( rc!=SQLITE_OK ){ |
| 8656 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 8657 | startLine, sqlite3_errmsg(p->db)); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8658 | sCtx.nErr++; |
| 8659 | }else{ |
| 8660 | sCtx.nRow++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8661 | } |
| 8662 | } |
| 8663 | }while( sCtx.cTerm!=EOF ); |
| 8664 | |
drh | 9776784 | 2020-05-29 19:39:35 +0000 | [diff] [blame] | 8665 | import_cleanup(&sCtx); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8666 | sqlite3_finalize(pStmt); |
| 8667 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | ccb3781 | 2020-03-09 15:39:39 +0000 | [diff] [blame] | 8668 | if( eVerbose>0 ){ |
| 8669 | utf8_printf(p->out, |
| 8670 | "Added %d rows with %d errors using %d lines of input\n", |
| 8671 | sCtx.nRow, sCtx.nErr, sCtx.nLine-1); |
| 8672 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8673 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 8674 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8675 | |
| 8676 | #ifndef SQLITE_UNTESTABLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8677 | if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8678 | char *zSql; |
| 8679 | char *zCollist = 0; |
| 8680 | sqlite3_stmt *pStmt; |
| 8681 | int tnum = 0; |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8682 | int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ |
| 8683 | int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8684 | int i; |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 8685 | if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ |
| 8686 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" |
| 8687 | " .imposter off\n"); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8688 | /* Also allowed, but not documented: |
| 8689 | ** |
| 8690 | ** .imposter TABLE IMPOSTER |
| 8691 | ** |
| 8692 | ** where TABLE is a WITHOUT ROWID table. In that case, the |
| 8693 | ** imposter is another WITHOUT ROWID table with the columns in |
| 8694 | ** storage order. */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8695 | rc = 1; |
| 8696 | goto meta_command_exit; |
| 8697 | } |
| 8698 | open_db(p, 0); |
drh | 48d219a | 2018-04-23 18:38:48 +0000 | [diff] [blame] | 8699 | if( nArg==2 ){ |
| 8700 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); |
| 8701 | goto meta_command_exit; |
| 8702 | } |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8703 | zSql = sqlite3_mprintf( |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8704 | "SELECT rootpage, 0 FROM sqlite_schema" |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8705 | " WHERE name='%q' AND type='index'" |
| 8706 | "UNION ALL " |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 8707 | "SELECT rootpage, 1 FROM sqlite_schema" |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8708 | " WHERE name='%q' AND type='table'" |
| 8709 | " AND sql LIKE '%%without%%rowid%%'", |
| 8710 | azArg[1], azArg[1] |
| 8711 | ); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8712 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 8713 | sqlite3_free(zSql); |
| 8714 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 8715 | tnum = sqlite3_column_int(pStmt, 0); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8716 | isWO = sqlite3_column_int(pStmt, 1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8717 | } |
| 8718 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8719 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 8720 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 8721 | sqlite3_free(zSql); |
| 8722 | i = 0; |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 8723 | while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8724 | char zLabel[20]; |
| 8725 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
| 8726 | i++; |
| 8727 | if( zCol==0 ){ |
| 8728 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 8729 | zCol = "_ROWID_"; |
| 8730 | }else{ |
| 8731 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 8732 | zCol = zLabel; |
| 8733 | } |
| 8734 | } |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8735 | if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ |
| 8736 | lenPK = (int)strlen(zCollist); |
| 8737 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8738 | if( zCollist==0 ){ |
| 8739 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 8740 | }else{ |
| 8741 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 8742 | } |
| 8743 | } |
| 8744 | sqlite3_finalize(pStmt); |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8745 | if( i==0 || tnum==0 ){ |
| 8746 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 8747 | rc = 1; |
| 8748 | sqlite3_free(zCollist); |
| 8749 | goto meta_command_exit; |
| 8750 | } |
| 8751 | if( lenPK==0 ) lenPK = 100000; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8752 | zSql = sqlite3_mprintf( |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8753 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", |
| 8754 | azArg[2], zCollist, lenPK, zCollist); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8755 | sqlite3_free(zCollist); |
| 8756 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 8757 | if( rc==SQLITE_OK ){ |
| 8758 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 8759 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 8760 | if( rc ){ |
| 8761 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 8762 | }else{ |
| 8763 | utf8_printf(stdout, "%s;\n", zSql); |
| 8764 | raw_printf(stdout, |
drh | 491c5be | 2019-10-18 15:58:50 +0000 | [diff] [blame] | 8765 | "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", |
| 8766 | azArg[1], isWO ? "table" : "index" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8767 | ); |
| 8768 | } |
| 8769 | }else{ |
| 8770 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
| 8771 | rc = 1; |
| 8772 | } |
| 8773 | sqlite3_free(zSql); |
| 8774 | }else |
| 8775 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 8776 | |
| 8777 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8778 | if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8779 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
| 8780 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 8781 | iotrace = 0; |
| 8782 | if( nArg<2 ){ |
| 8783 | sqlite3IoTrace = 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8784 | }else if( cli_strcmp(azArg[1], "-")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8785 | sqlite3IoTrace = iotracePrintf; |
| 8786 | iotrace = stdout; |
| 8787 | }else{ |
| 8788 | iotrace = fopen(azArg[1], "w"); |
| 8789 | if( iotrace==0 ){ |
| 8790 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 8791 | sqlite3IoTrace = 0; |
| 8792 | rc = 1; |
| 8793 | }else{ |
| 8794 | sqlite3IoTrace = iotracePrintf; |
| 8795 | } |
| 8796 | } |
| 8797 | }else |
| 8798 | #endif |
| 8799 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8800 | if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8801 | static const struct { |
| 8802 | const char *zLimitName; /* Name of a limit */ |
| 8803 | int limitCode; /* Integer code for that limit */ |
| 8804 | } aLimit[] = { |
| 8805 | { "length", SQLITE_LIMIT_LENGTH }, |
| 8806 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 8807 | { "column", SQLITE_LIMIT_COLUMN }, |
| 8808 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 8809 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 8810 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 8811 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 8812 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 8813 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 8814 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 8815 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 8816 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 8817 | }; |
| 8818 | int i, n2; |
| 8819 | open_db(p, 0); |
| 8820 | if( nArg==1 ){ |
| 8821 | for(i=0; i<ArraySize(aLimit); i++){ |
| 8822 | printf("%20s %d\n", aLimit[i].zLimitName, |
| 8823 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 8824 | } |
| 8825 | }else if( nArg>3 ){ |
| 8826 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
| 8827 | rc = 1; |
| 8828 | goto meta_command_exit; |
| 8829 | }else{ |
| 8830 | int iLimit = -1; |
| 8831 | n2 = strlen30(azArg[1]); |
| 8832 | for(i=0; i<ArraySize(aLimit); i++){ |
| 8833 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 8834 | if( iLimit<0 ){ |
| 8835 | iLimit = i; |
| 8836 | }else{ |
| 8837 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
| 8838 | rc = 1; |
| 8839 | goto meta_command_exit; |
| 8840 | } |
| 8841 | } |
| 8842 | } |
| 8843 | if( iLimit<0 ){ |
| 8844 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
| 8845 | "enter \".limits\" with no arguments for a list.\n", |
| 8846 | azArg[1]); |
| 8847 | rc = 1; |
| 8848 | goto meta_command_exit; |
| 8849 | } |
| 8850 | if( nArg==3 ){ |
| 8851 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 8852 | (int)integerValue(azArg[2])); |
| 8853 | } |
| 8854 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 8855 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 8856 | } |
| 8857 | }else |
| 8858 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8859 | if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8860 | open_db(p, 0); |
| 8861 | lintDotCommand(p, azArg, nArg); |
| 8862 | }else |
| 8863 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 8864 | #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8865 | if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8866 | const char *zFile, *zProc; |
| 8867 | char *zErrMsg = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8868 | failIfSafeMode(p, "cannot run .load in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8869 | if( nArg<2 ){ |
| 8870 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
| 8871 | rc = 1; |
| 8872 | goto meta_command_exit; |
| 8873 | } |
| 8874 | zFile = azArg[1]; |
| 8875 | zProc = nArg>=3 ? azArg[2] : 0; |
| 8876 | open_db(p, 0); |
| 8877 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 8878 | if( rc!=SQLITE_OK ){ |
| 8879 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
| 8880 | sqlite3_free(zErrMsg); |
| 8881 | rc = 1; |
| 8882 | } |
| 8883 | }else |
| 8884 | #endif |
| 8885 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 8886 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8887 | if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 8888 | failIfSafeMode(p, "cannot run .log in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8889 | if( nArg!=2 ){ |
| 8890 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
| 8891 | rc = 1; |
| 8892 | }else{ |
| 8893 | const char *zFile = azArg[1]; |
| 8894 | output_file_close(p->pLog); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 8895 | p->pLog = output_file_open(zFile, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8896 | } |
| 8897 | }else |
stephan | 618a375 | 2022-05-19 10:24:50 +0000 | [diff] [blame] | 8898 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8899 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8900 | if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){ |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8901 | const char *zMode = 0; |
| 8902 | const char *zTabname = 0; |
| 8903 | int i, n2; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8904 | ColModeOpts cmOpts = ColModeOpts_default; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8905 | for(i=1; i<nArg; i++){ |
| 8906 | const char *z = azArg[i]; |
| 8907 | if( optionMatch(z,"wrap") && i+1<nArg ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8908 | cmOpts.iWrap = integerValue(azArg[++i]); |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 8909 | }else if( optionMatch(z,"ww") ){ |
| 8910 | cmOpts.bWordWrap = 1; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8911 | }else if( optionMatch(z,"wordwrap") && i+1<nArg ){ |
| 8912 | cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8913 | }else if( optionMatch(z,"quote") ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8914 | cmOpts.bQuote = 1; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8915 | }else if( optionMatch(z,"noquote") ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8916 | cmOpts.bQuote = 0; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8917 | }else if( zMode==0 ){ |
| 8918 | zMode = z; |
drh | 1f41a8c | 2022-10-24 11:10:40 +0000 | [diff] [blame] | 8919 | /* Apply defaults for qbox pseudo-mode. If that |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8920 | * overwrites already-set values, user was informed of this. |
| 8921 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8922 | if( cli_strcmp(z, "qbox")==0 ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8923 | ColModeOpts cmo = ColModeOpts_default_qbox; |
| 8924 | zMode = "box"; |
| 8925 | cmOpts = cmo; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8926 | } |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8927 | }else if( zTabname==0 ){ |
| 8928 | zTabname = z; |
| 8929 | }else if( z[0]=='-' ){ |
| 8930 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 8931 | utf8_printf(stderr, "options:\n" |
| 8932 | " --noquote\n" |
| 8933 | " --quote\n" |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8934 | " --wordwrap on/off\n" |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 8935 | " --wrap N\n" |
| 8936 | " --ww\n"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8937 | rc = 1; |
| 8938 | goto meta_command_exit; |
| 8939 | }else{ |
| 8940 | utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
| 8941 | rc = 1; |
| 8942 | goto meta_command_exit; |
| 8943 | } |
| 8944 | } |
| 8945 | if( zMode==0 ){ |
| 8946 | if( p->mode==MODE_Column |
| 8947 | || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) |
| 8948 | ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8949 | raw_printf |
| 8950 | (p->out, |
| 8951 | "current output mode: %s --wrap %d --wordwrap %s --%squote\n", |
| 8952 | modeDescr[p->mode], p->cmOpts.iWrap, |
| 8953 | p->cmOpts.bWordWrap ? "on" : "off", |
| 8954 | p->cmOpts.bQuote ? "" : "no"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8955 | }else{ |
| 8956 | raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 8957 | } |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8958 | zMode = modeDescr[p->mode]; |
| 8959 | } |
| 8960 | n2 = strlen30(zMode); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8961 | if( cli_strncmp(zMode,"lines",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8962 | p->mode = MODE_Line; |
| 8963 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8964 | }else if( cli_strncmp(zMode,"columns",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8965 | p->mode = MODE_Column; |
drh | c060508 | 2020-06-05 00:54:27 +0000 | [diff] [blame] | 8966 | if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ |
| 8967 | p->showHeader = 1; |
| 8968 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8969 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 8970 | p->cmOpts = cmOpts; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8971 | }else if( cli_strncmp(zMode,"list",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8972 | p->mode = MODE_List; |
| 8973 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 8974 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8975 | }else if( cli_strncmp(zMode,"html",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8976 | p->mode = MODE_Html; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8977 | }else if( cli_strncmp(zMode,"tcl",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8978 | p->mode = MODE_Tcl; |
| 8979 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
| 8980 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8981 | }else if( cli_strncmp(zMode,"csv",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8982 | p->mode = MODE_Csv; |
| 8983 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 8984 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8985 | }else if( cli_strncmp(zMode,"tabs",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8986 | p->mode = MODE_List; |
| 8987 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8988 | }else if( cli_strncmp(zMode,"insert",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8989 | p->mode = MODE_Insert; |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 8990 | set_table_name(p, zTabname ? zTabname : "table"); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8991 | }else if( cli_strncmp(zMode,"quote",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8992 | p->mode = MODE_Quote; |
drh | c683573 | 2020-05-28 20:37:17 +0000 | [diff] [blame] | 8993 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 8994 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8995 | }else if( cli_strncmp(zMode,"ascii",n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8996 | p->mode = MODE_Ascii; |
| 8997 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 8998 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 8999 | }else if( cli_strncmp(zMode,"markdown",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9000 | p->mode = MODE_Markdown; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9001 | p->cmOpts = cmOpts; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9002 | }else if( cli_strncmp(zMode,"table",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9003 | p->mode = MODE_Table; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9004 | p->cmOpts = cmOpts; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9005 | }else if( cli_strncmp(zMode,"box",n2)==0 ){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 9006 | p->mode = MODE_Box; |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 9007 | p->cmOpts = cmOpts; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9008 | }else if( cli_strncmp(zMode,"count",n2)==0 ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 9009 | p->mode = MODE_Count; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9010 | }else if( cli_strncmp(zMode,"off",n2)==0 ){ |
drh | 5d88be8 | 2021-12-09 16:17:43 +0000 | [diff] [blame] | 9011 | p->mode = MODE_Off; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9012 | }else if( cli_strncmp(zMode,"json",n2)==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 9013 | p->mode = MODE_Json; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9014 | }else{ |
| 9015 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 9016 | "ascii box column csv html insert json line list markdown " |
drh | ca1776b | 2022-02-01 12:28:17 +0000 | [diff] [blame] | 9017 | "qbox quote table tabs tcl\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9018 | rc = 1; |
| 9019 | } |
| 9020 | p->cMode = p->mode; |
| 9021 | }else |
| 9022 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9023 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9024 | if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9025 | if( nArg!=2 ){ |
| 9026 | raw_printf(stderr, "Usage: .nonce NONCE\n"); |
| 9027 | rc = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9028 | }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ |
drh | fe46317 | 2021-12-16 17:57:21 +0000 | [diff] [blame] | 9029 | raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", |
| 9030 | p->lineno, azArg[1]); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9031 | exit(1); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 9032 | }else{ |
| 9033 | p->bSafeMode = 0; |
| 9034 | return 0; /* Return immediately to bypass the safe mode reset |
| 9035 | ** at the end of this procedure */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9036 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9037 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9038 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9039 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9040 | if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9041 | if( nArg==2 ){ |
| 9042 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 9043 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
| 9044 | }else{ |
| 9045 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
| 9046 | rc = 1; |
| 9047 | } |
| 9048 | }else |
| 9049 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9050 | if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9051 | const char *zFN = 0; /* Pointer to constant filename */ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9052 | char *zNewFilename = 0; /* Name of the database file to open */ |
| 9053 | int iName = 1; /* Index in azArg[] of the filename */ |
| 9054 | int newFlag = 0; /* True to delete file before opening */ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9055 | int openMode = SHELL_OPEN_UNSPEC; |
| 9056 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9057 | /* Check for command-line arguments */ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9058 | for(iName=1; iName<nArg; iName++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9059 | const char *z = azArg[iName]; |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9060 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9061 | if( optionMatch(z,"new") ){ |
| 9062 | newFlag = 1; |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 9063 | #ifdef SQLITE_HAVE_ZLIB |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 9064 | }else if( optionMatch(z, "zip") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9065 | openMode = SHELL_OPEN_ZIPFILE; |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | }else if( optionMatch(z, "append") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9068 | openMode = SHELL_OPEN_APPENDVFS; |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 9069 | }else if( optionMatch(z, "readonly") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9070 | openMode = SHELL_OPEN_READONLY; |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 9071 | }else if( optionMatch(z, "nofollow") ){ |
| 9072 | p->openFlags |= SQLITE_OPEN_NOFOLLOW; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 9073 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 9074 | }else if( optionMatch(z, "deserialize") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9075 | openMode = SHELL_OPEN_DESERIALIZE; |
drh | 3374648 | 2018-12-13 15:06:26 +0000 | [diff] [blame] | 9076 | }else if( optionMatch(z, "hexdb") ){ |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9077 | openMode = SHELL_OPEN_HEXDB; |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 9078 | }else if( optionMatch(z, "maxsize") && iName+1<nArg ){ |
| 9079 | p->szMax = integerValue(azArg[++iName]); |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 9080 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
stephan | de1e02e | 2022-05-24 19:01:21 +0000 | [diff] [blame] | 9081 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9082 | #endif /* !SQLITE_SHELL_FIDDLE */ |
stephan | de1e02e | 2022-05-24 19:01:21 +0000 | [diff] [blame] | 9083 | if( z[0]=='-' ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9084 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 9085 | rc = 1; |
| 9086 | goto meta_command_exit; |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9087 | }else if( zFN ){ |
drh | f30bbce | 2020-12-02 18:27:48 +0000 | [diff] [blame] | 9088 | utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
| 9089 | rc = 1; |
| 9090 | goto meta_command_exit; |
| 9091 | }else{ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9092 | zFN = z; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9093 | } |
| 9094 | } |
dan | 1872c5b | 2021-12-02 14:16:30 +0000 | [diff] [blame] | 9095 | |
| 9096 | /* Close the existing database */ |
| 9097 | session_close_all(p, -1); |
| 9098 | close_db(p->db); |
| 9099 | p->db = 0; |
| 9100 | p->pAuxDb->zDbFilename = 0; |
| 9101 | sqlite3_free(p->pAuxDb->zFreeOnClose); |
| 9102 | p->pAuxDb->zFreeOnClose = 0; |
| 9103 | p->openMode = openMode; |
| 9104 | p->openFlags = 0; |
| 9105 | p->szMax = 0; |
| 9106 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9107 | /* If a filename is specified, try to open it first */ |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9108 | if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ |
drh | c320e06 | 2021-12-24 19:44:11 +0000 | [diff] [blame] | 9109 | if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9110 | #ifndef SQLITE_SHELL_FIDDLE |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9111 | if( p->bSafeMode |
| 9112 | && p->openMode!=SHELL_OPEN_HEXDB |
drh | c320e06 | 2021-12-24 19:44:11 +0000 | [diff] [blame] | 9113 | && zFN |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9114 | && cli_strcmp(zFN,":memory:")!=0 |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9115 | ){ |
| 9116 | failIfSafeMode(p, "cannot open disk-based database files in safe mode"); |
| 9117 | } |
stephan | de1e02e | 2022-05-24 19:01:21 +0000 | [diff] [blame] | 9118 | #else |
| 9119 | /* WASM mode has its own sandboxed pseudo-filesystem. */ |
| 9120 | #endif |
drh | 61fd4b9 | 2021-12-16 14:59:25 +0000 | [diff] [blame] | 9121 | if( zFN ){ |
| 9122 | zNewFilename = sqlite3_mprintf("%s", zFN); |
| 9123 | shell_check_oom(zNewFilename); |
| 9124 | }else{ |
| 9125 | zNewFilename = 0; |
| 9126 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9127 | p->pAuxDb->zDbFilename = zNewFilename; |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 9128 | open_db(p, OPEN_DB_KEEPALIVE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9129 | if( p->db==0 ){ |
| 9130 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 9131 | sqlite3_free(zNewFilename); |
| 9132 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9133 | p->pAuxDb->zFreeOnClose = zNewFilename; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9134 | } |
| 9135 | } |
| 9136 | if( p->db==0 ){ |
| 9137 | /* As a fall-back open a TEMP database */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9138 | p->pAuxDb->zDbFilename = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9139 | open_db(p, 0); |
| 9140 | } |
| 9141 | }else |
| 9142 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9143 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9144 | if( (c=='o' |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9145 | && (cli_strncmp(azArg[0], "output", n)==0 |
| 9146 | || cli_strncmp(azArg[0], "once", n)==0)) |
| 9147 | || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9148 | ){ |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9149 | char *zFile = 0; |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9150 | int bTxtMode = 0; |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9151 | int i; |
| 9152 | int eMode = 0; |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9153 | int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ |
| 9154 | unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9155 | |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9156 | zBOM[0] = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9157 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9158 | if( c=='e' ){ |
| 9159 | eMode = 'x'; |
| 9160 | bOnce = 2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9161 | }else if( cli_strncmp(azArg[0],"once",n)==0 ){ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9162 | bOnce = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9163 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9164 | for(i=1; i<nArg; i++){ |
| 9165 | char *z = azArg[i]; |
| 9166 | if( z[0]=='-' ){ |
| 9167 | if( z[1]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9168 | if( cli_strcmp(z,"-bom")==0 ){ |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9169 | zBOM[0] = 0xef; |
| 9170 | zBOM[1] = 0xbb; |
| 9171 | zBOM[2] = 0xbf; |
| 9172 | zBOM[3] = 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9173 | }else if( c!='e' && cli_strcmp(z,"-x")==0 ){ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9174 | eMode = 'x'; /* spreadsheet */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9175 | }else if( c!='e' && cli_strcmp(z,"-e")==0 ){ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9176 | eMode = 'e'; /* text editor */ |
| 9177 | }else{ |
| 9178 | utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", |
| 9179 | azArg[i]); |
| 9180 | showHelp(p->out, azArg[0]); |
| 9181 | rc = 1; |
| 9182 | goto meta_command_exit; |
| 9183 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9184 | }else if( zFile==0 && eMode!='e' && eMode!='x' ){ |
| 9185 | zFile = sqlite3_mprintf("%s", z); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9186 | if( zFile && zFile[0]=='|' ){ |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9187 | while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]); |
| 9188 | break; |
| 9189 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9190 | }else{ |
| 9191 | utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", |
| 9192 | azArg[i]); |
| 9193 | showHelp(p->out, azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9194 | rc = 1; |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9195 | sqlite3_free(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9196 | goto meta_command_exit; |
| 9197 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9198 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9199 | if( zFile==0 ){ |
| 9200 | zFile = sqlite3_mprintf("stdout"); |
| 9201 | } |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9202 | if( bOnce ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9203 | p->outCount = 2; |
| 9204 | }else{ |
| 9205 | p->outCount = 0; |
| 9206 | } |
| 9207 | output_reset(p); |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 9208 | #ifndef SQLITE_NOHAVE_SYSTEM |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9209 | if( eMode=='e' || eMode=='x' ){ |
drh | 3c484e8 | 2018-01-10 22:27:21 +0000 | [diff] [blame] | 9210 | p->doXdgOpen = 1; |
| 9211 | outputModePush(p); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9212 | if( eMode=='x' ){ |
| 9213 | /* spreadsheet mode. Output as CSV. */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9214 | newTempFile(p, "csv"); |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9215 | ShellClearFlag(p, SHFLG_Echo); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9216 | p->mode = MODE_Csv; |
| 9217 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
| 9218 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| 9219 | }else{ |
drh | 7a43100 | 2020-04-18 14:12:00 +0000 | [diff] [blame] | 9220 | /* text editor mode */ |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9221 | newTempFile(p, "txt"); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9222 | bTxtMode = 1; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9223 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9224 | sqlite3_free(zFile); |
| 9225 | zFile = sqlite3_mprintf("%s", p->zTempFile); |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 9226 | } |
drh | 04a28c3 | 2018-01-31 01:38:44 +0000 | [diff] [blame] | 9227 | #endif /* SQLITE_NOHAVE_SYSTEM */ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9228 | shell_check_oom(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9229 | if( zFile[0]=='|' ){ |
| 9230 | #ifdef SQLITE_OMIT_POPEN |
| 9231 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 9232 | rc = 1; |
| 9233 | p->out = stdout; |
| 9234 | #else |
| 9235 | p->out = popen(zFile + 1, "w"); |
| 9236 | if( p->out==0 ){ |
| 9237 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
| 9238 | p->out = stdout; |
| 9239 | rc = 1; |
| 9240 | }else{ |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9241 | if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9242 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 9243 | } |
| 9244 | #endif |
| 9245 | }else{ |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 9246 | p->out = output_file_open(zFile, bTxtMode); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9247 | if( p->out==0 ){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9248 | if( cli_strcmp(zFile,"off")!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9249 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
| 9250 | } |
| 9251 | p->out = stdout; |
| 9252 | rc = 1; |
| 9253 | } else { |
drh | 8ad3c43 | 2022-03-23 10:04:52 +0000 | [diff] [blame] | 9254 | if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9255 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
| 9256 | } |
| 9257 | } |
drh | 4b0229a | 2021-02-17 13:19:22 +0000 | [diff] [blame] | 9258 | sqlite3_free(zFile); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9259 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9260 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9261 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9262 | if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9263 | open_db(p,0); |
| 9264 | if( nArg<=1 ) goto parameter_syntax_error; |
| 9265 | |
| 9266 | /* .parameter clear |
| 9267 | ** Clear all bind parameters by dropping the TEMP table that holds them. |
| 9268 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9269 | if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){ |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9270 | sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9271 | 0, 0, 0); |
| 9272 | }else |
| 9273 | |
| 9274 | /* .parameter list |
| 9275 | ** List all bind parameters. |
| 9276 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9277 | if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9278 | sqlite3_stmt *pStmt = 0; |
| 9279 | int rx; |
| 9280 | int len = 0; |
| 9281 | rx = sqlite3_prepare_v2(p->db, |
| 9282 | "SELECT max(length(key)) " |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9283 | "FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9284 | if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 9285 | len = sqlite3_column_int(pStmt, 0); |
| 9286 | if( len>40 ) len = 40; |
| 9287 | } |
| 9288 | sqlite3_finalize(pStmt); |
| 9289 | pStmt = 0; |
| 9290 | if( len ){ |
| 9291 | rx = sqlite3_prepare_v2(p->db, |
| 9292 | "SELECT key, quote(value) " |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9293 | "FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 9294 | while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9295 | utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), |
| 9296 | sqlite3_column_text(pStmt,1)); |
| 9297 | } |
| 9298 | sqlite3_finalize(pStmt); |
| 9299 | } |
| 9300 | }else |
| 9301 | |
| 9302 | /* .parameter init |
| 9303 | ** Make sure the TEMP table used to hold bind parameters exists. |
| 9304 | ** Create it if necessary. |
| 9305 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9306 | if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9307 | bind_table_init(p); |
| 9308 | }else |
| 9309 | |
| 9310 | /* .parameter set NAME VALUE |
| 9311 | ** Set or reset a bind parameter. NAME should be the full parameter |
| 9312 | ** name exactly as it appears in the query. (ex: $abc, @def). The |
| 9313 | ** VALUE can be in either SQL literal notation, or if not it will be |
| 9314 | ** understood to be a text string. |
| 9315 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9316 | if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9317 | int rx; |
| 9318 | char *zSql; |
| 9319 | sqlite3_stmt *pStmt; |
| 9320 | const char *zKey = azArg[2]; |
| 9321 | const char *zValue = azArg[3]; |
| 9322 | bind_table_init(p); |
| 9323 | zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9324 | "REPLACE INTO temp.sqlite_parameters(key,value)" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9325 | "VALUES(%Q,%s);", zKey, zValue); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9326 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9327 | pStmt = 0; |
| 9328 | rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9329 | sqlite3_free(zSql); |
| 9330 | if( rx!=SQLITE_OK ){ |
| 9331 | sqlite3_finalize(pStmt); |
| 9332 | pStmt = 0; |
| 9333 | zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9334 | "REPLACE INTO temp.sqlite_parameters(key,value)" |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9335 | "VALUES(%Q,%Q);", zKey, zValue); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9336 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9337 | rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 9338 | sqlite3_free(zSql); |
| 9339 | if( rx!=SQLITE_OK ){ |
| 9340 | utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9341 | sqlite3_finalize(pStmt); |
| 9342 | pStmt = 0; |
| 9343 | rc = 1; |
| 9344 | } |
| 9345 | } |
| 9346 | sqlite3_step(pStmt); |
| 9347 | sqlite3_finalize(pStmt); |
| 9348 | }else |
| 9349 | |
| 9350 | /* .parameter unset NAME |
| 9351 | ** Remove the NAME binding from the parameter binding table, if it |
| 9352 | ** exists. |
| 9353 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9354 | if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){ |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9355 | char *zSql = sqlite3_mprintf( |
drh | 65c29fd | 2019-03-25 21:56:26 +0000 | [diff] [blame] | 9356 | "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9357 | shell_check_oom(zSql); |
drh | 9cb0264 | 2019-02-28 20:10:52 +0000 | [diff] [blame] | 9358 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 9359 | sqlite3_free(zSql); |
| 9360 | }else |
| 9361 | /* If no command name matches, show a syntax error */ |
| 9362 | parameter_syntax_error: |
| 9363 | showHelp(p->out, "parameter"); |
| 9364 | }else |
| 9365 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9366 | if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9367 | int i; |
| 9368 | for(i=1; i<nArg; i++){ |
| 9369 | if( i>1 ) raw_printf(p->out, " "); |
| 9370 | utf8_printf(p->out, "%s", azArg[i]); |
| 9371 | } |
| 9372 | raw_printf(p->out, "\n"); |
| 9373 | }else |
| 9374 | |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 9375 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9376 | if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9377 | int i; |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9378 | int nn = 0; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9379 | p->flgProgress = 0; |
| 9380 | p->mxProgress = 0; |
| 9381 | p->nProgress = 0; |
| 9382 | for(i=1; i<nArg; i++){ |
| 9383 | const char *z = azArg[i]; |
| 9384 | if( z[0]=='-' ){ |
| 9385 | z++; |
| 9386 | if( z[0]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9387 | if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9388 | p->flgProgress |= SHELL_PROGRESS_QUIET; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9389 | continue; |
| 9390 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9391 | if( cli_strcmp(z,"reset")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9392 | p->flgProgress |= SHELL_PROGRESS_RESET; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9393 | continue; |
| 9394 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9395 | if( cli_strcmp(z,"once")==0 ){ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9396 | p->flgProgress |= SHELL_PROGRESS_ONCE; |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9397 | continue; |
| 9398 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9399 | if( cli_strcmp(z,"limit")==0 ){ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9400 | if( i+1>=nArg ){ |
| 9401 | utf8_printf(stderr, "Error: missing argument on --limit\n"); |
| 9402 | rc = 1; |
| 9403 | goto meta_command_exit; |
| 9404 | }else{ |
| 9405 | p->mxProgress = (int)integerValue(azArg[++i]); |
| 9406 | } |
| 9407 | continue; |
| 9408 | } |
| 9409 | utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); |
| 9410 | rc = 1; |
| 9411 | goto meta_command_exit; |
| 9412 | }else{ |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9413 | nn = (int)integerValue(z); |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9414 | } |
| 9415 | } |
| 9416 | open_db(p, 0); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 9417 | sqlite3_progress_handler(p->db, nn, progress_handler, p); |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9418 | }else |
drh | 569b1d9 | 2019-02-05 20:51:41 +0000 | [diff] [blame] | 9419 | #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
drh | 3f83f59 | 2019-02-04 14:53:18 +0000 | [diff] [blame] | 9420 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9421 | if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9422 | if( nArg >= 2) { |
| 9423 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 9424 | } |
| 9425 | if( nArg >= 3) { |
| 9426 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 9427 | } |
| 9428 | }else |
| 9429 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9430 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9431 | if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9432 | rc = 2; |
| 9433 | }else |
stephan | 02520cc | 2022-05-18 22:58:34 +0000 | [diff] [blame] | 9434 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9435 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9436 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9437 | if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9438 | FILE *inSaved = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 9439 | int savedLineno = p->lineno; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9440 | failIfSafeMode(p, "cannot run .read in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9441 | if( nArg!=2 ){ |
| 9442 | raw_printf(stderr, "Usage: .read FILE\n"); |
| 9443 | rc = 1; |
| 9444 | goto meta_command_exit; |
| 9445 | } |
drh | 30497f4 | 2020-08-26 10:50:48 +0000 | [diff] [blame] | 9446 | if( azArg[1][0]=='|' ){ |
drh | 9d59e3b | 2021-03-12 01:49:08 +0000 | [diff] [blame] | 9447 | #ifdef SQLITE_OMIT_POPEN |
| 9448 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| 9449 | rc = 1; |
| 9450 | p->out = stdout; |
| 9451 | #else |
drh | 30497f4 | 2020-08-26 10:50:48 +0000 | [diff] [blame] | 9452 | p->in = popen(azArg[1]+1, "r"); |
| 9453 | if( p->in==0 ){ |
| 9454 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
| 9455 | rc = 1; |
| 9456 | }else{ |
| 9457 | rc = process_input(p); |
| 9458 | pclose(p->in); |
| 9459 | } |
drh | 9d59e3b | 2021-03-12 01:49:08 +0000 | [diff] [blame] | 9460 | #endif |
larrybr | d96bcc7 | 2021-09-17 21:12:47 +0000 | [diff] [blame] | 9461 | }else if( (p->in = openChrSource(azArg[1]))==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9462 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 9463 | rc = 1; |
| 9464 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9465 | rc = process_input(p); |
| 9466 | fclose(p->in); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9467 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 9468 | p->in = inSaved; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 9469 | p->lineno = savedLineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9470 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9471 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9472 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9473 | #ifndef SQLITE_SHELL_FIDDLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9474 | if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9475 | const char *zSrcFile; |
| 9476 | const char *zDb; |
| 9477 | sqlite3 *pSrc; |
| 9478 | sqlite3_backup *pBackup; |
| 9479 | int nTimeout = 0; |
| 9480 | |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9481 | failIfSafeMode(p, "cannot run .restore in safe mode"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9482 | if( nArg==2 ){ |
| 9483 | zSrcFile = azArg[1]; |
| 9484 | zDb = "main"; |
| 9485 | }else if( nArg==3 ){ |
| 9486 | zSrcFile = azArg[2]; |
| 9487 | zDb = azArg[1]; |
| 9488 | }else{ |
| 9489 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
| 9490 | rc = 1; |
| 9491 | goto meta_command_exit; |
| 9492 | } |
| 9493 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 9494 | if( rc!=SQLITE_OK ){ |
| 9495 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9496 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9497 | return 1; |
| 9498 | } |
| 9499 | open_db(p, 0); |
| 9500 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 9501 | if( pBackup==0 ){ |
| 9502 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9503 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9504 | return 1; |
| 9505 | } |
| 9506 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 9507 | || rc==SQLITE_BUSY ){ |
| 9508 | if( rc==SQLITE_BUSY ){ |
| 9509 | if( nTimeout++ >= 3 ) break; |
| 9510 | sqlite3_sleep(100); |
| 9511 | } |
| 9512 | } |
| 9513 | sqlite3_backup_finish(pBackup); |
| 9514 | if( rc==SQLITE_DONE ){ |
| 9515 | rc = 0; |
| 9516 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 9517 | raw_printf(stderr, "Error: source database is busy\n"); |
| 9518 | rc = 1; |
| 9519 | }else{ |
| 9520 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9521 | rc = 1; |
| 9522 | } |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 9523 | close_db(pSrc); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9524 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 9525 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9526 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9527 | if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9528 | if( nArg==2 ){ |
mistachkin | b71aa09 | 2018-01-23 00:05:18 +0000 | [diff] [blame] | 9529 | p->scanstatsOn = (u8)booleanValue(azArg[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9530 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9531 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| 9532 | #endif |
| 9533 | }else{ |
| 9534 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| 9535 | rc = 1; |
| 9536 | } |
| 9537 | }else |
| 9538 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9539 | if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9540 | ShellText sSelect; |
| 9541 | ShellState data; |
| 9542 | char *zErrMsg = 0; |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 9543 | const char *zDiv = "("; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9544 | const char *zName = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9545 | int iSchema = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9546 | int bDebug = 0; |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9547 | int bNoSystemTabs = 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9548 | int ii; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9549 | |
| 9550 | open_db(p, 0); |
| 9551 | memcpy(&data, p, sizeof(data)); |
| 9552 | data.showHeader = 0; |
| 9553 | data.cMode = data.mode = MODE_Semi; |
| 9554 | initText(&sSelect); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9555 | for(ii=1; ii<nArg; ii++){ |
| 9556 | if( optionMatch(azArg[ii],"indent") ){ |
| 9557 | data.cMode = data.mode = MODE_Pretty; |
| 9558 | }else if( optionMatch(azArg[ii],"debug") ){ |
| 9559 | bDebug = 1; |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9560 | }else if( optionMatch(azArg[ii],"nosys") ){ |
| 9561 | bNoSystemTabs = 1; |
| 9562 | }else if( azArg[ii][0]=='-' ){ |
| 9563 | utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]); |
| 9564 | rc = 1; |
| 9565 | goto meta_command_exit; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9566 | }else if( zName==0 ){ |
| 9567 | zName = azArg[ii]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9568 | }else{ |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9569 | raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9570 | rc = 1; |
| 9571 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9572 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9573 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9574 | if( zName!=0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 9575 | int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 9576 | || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 |
| 9577 | || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 |
| 9578 | || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 9579 | if( isSchema ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9580 | char *new_argv[2], *new_colv[2]; |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 9581 | new_argv[0] = sqlite3_mprintf( |
| 9582 | "CREATE TABLE %s (\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9583 | " type text,\n" |
| 9584 | " name text,\n" |
| 9585 | " tbl_name text,\n" |
| 9586 | " rootpage integer,\n" |
| 9587 | " sql text\n" |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 9588 | ")", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9589 | shell_check_oom(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9590 | new_argv[1] = 0; |
| 9591 | new_colv[0] = "sql"; |
| 9592 | new_colv[1] = 0; |
| 9593 | callback(&data, 1, new_argv, new_colv); |
drh | c22b716 | 2018-01-01 20:11:23 +0000 | [diff] [blame] | 9594 | sqlite3_free(new_argv[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9595 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9596 | } |
| 9597 | if( zDiv ){ |
| 9598 | sqlite3_stmt *pStmt = 0; |
| 9599 | rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 9600 | -1, &pStmt, 0); |
| 9601 | if( rc ){ |
| 9602 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 9603 | sqlite3_finalize(pStmt); |
| 9604 | rc = 1; |
| 9605 | goto meta_command_exit; |
| 9606 | } |
| 9607 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 9608 | iSchema = 0; |
| 9609 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 9610 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 9611 | char zScNum[30]; |
| 9612 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 9613 | appendText(&sSelect, zDiv, 0); |
| 9614 | zDiv = " UNION ALL "; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9615 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
| 9616 | if( sqlite3_stricmp(zDb, "main")!=0 ){ |
drh | ea38f4f | 2019-07-13 17:21:47 +0000 | [diff] [blame] | 9617 | appendText(&sSelect, zDb, '\''); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9618 | }else{ |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9619 | appendText(&sSelect, "NULL", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9620 | } |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9621 | appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); |
| 9622 | appendText(&sSelect, zScNum, 0); |
| 9623 | appendText(&sSelect, " AS snum, ", 0); |
| 9624 | appendText(&sSelect, zDb, '\''); |
| 9625 | appendText(&sSelect, " AS sname FROM ", 0); |
drh | ea38f4f | 2019-07-13 17:21:47 +0000 | [diff] [blame] | 9626 | appendText(&sSelect, zDb, quoteChar(zDb)); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 9627 | appendText(&sSelect, ".sqlite_schema", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9628 | } |
| 9629 | sqlite3_finalize(pStmt); |
drh | cc3f3d1 | 2019-08-17 15:27:58 +0000 | [diff] [blame] | 9630 | #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 9631 | if( zName ){ |
| 9632 | appendText(&sSelect, |
| 9633 | " UNION ALL SELECT shell_module_schema(name)," |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 9634 | " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", |
| 9635 | 0); |
drh | 667a2a2 | 2018-01-02 00:04:37 +0000 | [diff] [blame] | 9636 | } |
drh | cde7b77 | 2018-01-02 12:50:40 +0000 | [diff] [blame] | 9637 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9638 | appendText(&sSelect, ") WHERE ", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9639 | if( zName ){ |
| 9640 | char *zQarg = sqlite3_mprintf("%Q", zName); |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 9641 | int bGlob; |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9642 | shell_check_oom(zQarg); |
mistachkin | c158c07 | 2021-12-31 19:08:20 +0000 | [diff] [blame] | 9643 | bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || |
| 9644 | strchr(zName, '[') != 0; |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9645 | if( strchr(zName, '.') ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9646 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 9647 | }else{ |
| 9648 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 9649 | } |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 9650 | appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9651 | appendText(&sSelect, zQarg, 0); |
mistachkin | 9d10726 | 2018-03-23 14:24:34 +0000 | [diff] [blame] | 9652 | if( !bGlob ){ |
| 9653 | appendText(&sSelect, " ESCAPE '\\' ", 0); |
| 9654 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9655 | appendText(&sSelect, " AND ", 0); |
| 9656 | sqlite3_free(zQarg); |
| 9657 | } |
drh | bbb29ec | 2020-10-12 14:56:47 +0000 | [diff] [blame] | 9658 | if( bNoSystemTabs ){ |
| 9659 | appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); |
| 9660 | } |
| 9661 | appendText(&sSelect, "sql IS NOT NULL" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9662 | " ORDER BY snum, rowid", 0); |
drh | ceba792 | 2018-01-01 21:28:25 +0000 | [diff] [blame] | 9663 | if( bDebug ){ |
| 9664 | utf8_printf(p->out, "SQL: %s;\n", sSelect.z); |
| 9665 | }else{ |
| 9666 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 9667 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9668 | freeText(&sSelect); |
| 9669 | } |
| 9670 | if( zErrMsg ){ |
| 9671 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 9672 | sqlite3_free(zErrMsg); |
| 9673 | rc = 1; |
| 9674 | }else if( rc != SQLITE_OK ){ |
| 9675 | raw_printf(stderr,"Error: querying schema information\n"); |
| 9676 | rc = 1; |
| 9677 | }else{ |
| 9678 | rc = 0; |
| 9679 | } |
| 9680 | }else |
| 9681 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9682 | if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0) |
| 9683 | || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0) |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 9684 | ){ |
drh | fda8e49 | 2020-12-04 16:04:45 +0000 | [diff] [blame] | 9685 | unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 9686 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9687 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9688 | |
| 9689 | #if defined(SQLITE_ENABLE_SESSION) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9690 | if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9691 | struct AuxDb *pAuxDb = p->pAuxDb; |
| 9692 | OpenSession *pSession = &pAuxDb->aSession[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9693 | char **azCmd = &azArg[1]; |
| 9694 | int iSes = 0; |
| 9695 | int nCmd = nArg - 1; |
| 9696 | int i; |
| 9697 | if( nArg<=1 ) goto session_syntax_error; |
| 9698 | open_db(p, 0); |
| 9699 | if( nArg>=3 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9700 | for(iSes=0; iSes<pAuxDb->nSession; iSes++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9701 | if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9702 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9703 | if( iSes<pAuxDb->nSession ){ |
| 9704 | pSession = &pAuxDb->aSession[iSes]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9705 | azCmd++; |
| 9706 | nCmd--; |
| 9707 | }else{ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9708 | pSession = &pAuxDb->aSession[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9709 | iSes = 0; |
| 9710 | } |
| 9711 | } |
| 9712 | |
| 9713 | /* .session attach TABLE |
| 9714 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 9715 | ** table so that it is never filtered. |
| 9716 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9717 | if( cli_strcmp(azCmd[0],"attach")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9718 | if( nCmd!=2 ) goto session_syntax_error; |
| 9719 | if( pSession->p==0 ){ |
| 9720 | session_not_open: |
| 9721 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
| 9722 | }else{ |
| 9723 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 9724 | if( rc ){ |
| 9725 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| 9726 | rc = 0; |
| 9727 | } |
| 9728 | } |
| 9729 | }else |
| 9730 | |
| 9731 | /* .session changeset FILE |
| 9732 | ** .session patchset FILE |
| 9733 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 9734 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9735 | if( cli_strcmp(azCmd[0],"changeset")==0 |
| 9736 | || cli_strcmp(azCmd[0],"patchset")==0 |
| 9737 | ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9738 | FILE *out = 0; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 9739 | failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9740 | if( nCmd!=2 ) goto session_syntax_error; |
| 9741 | if( pSession->p==0 ) goto session_not_open; |
| 9742 | out = fopen(azCmd[1], "wb"); |
| 9743 | if( out==0 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 9744 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", |
| 9745 | azCmd[1]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9746 | }else{ |
| 9747 | int szChng; |
| 9748 | void *pChng; |
| 9749 | if( azCmd[0][0]=='c' ){ |
| 9750 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| 9751 | }else{ |
| 9752 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 9753 | } |
| 9754 | if( rc ){ |
| 9755 | printf("Error: error code %d\n", rc); |
| 9756 | rc = 0; |
| 9757 | } |
| 9758 | if( pChng |
| 9759 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
| 9760 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| 9761 | szChng); |
| 9762 | } |
| 9763 | sqlite3_free(pChng); |
| 9764 | fclose(out); |
| 9765 | } |
| 9766 | }else |
| 9767 | |
| 9768 | /* .session close |
| 9769 | ** Close the identified session |
| 9770 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9771 | if( cli_strcmp(azCmd[0], "close")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9772 | if( nCmd!=1 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9773 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9774 | session_close(pSession); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9775 | pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9776 | } |
| 9777 | }else |
| 9778 | |
| 9779 | /* .session enable ?BOOLEAN? |
| 9780 | ** Query or set the enable flag |
| 9781 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9782 | if( cli_strcmp(azCmd[0], "enable")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9783 | int ii; |
| 9784 | if( nCmd>2 ) goto session_syntax_error; |
| 9785 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9786 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9787 | ii = sqlite3session_enable(pSession->p, ii); |
| 9788 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 9789 | pSession->zName, ii); |
| 9790 | } |
| 9791 | }else |
| 9792 | |
| 9793 | /* .session filter GLOB .... |
| 9794 | ** Set a list of GLOB patterns of table names to be excluded. |
| 9795 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9796 | if( cli_strcmp(azCmd[0], "filter")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9797 | int ii, nByte; |
| 9798 | if( nCmd<2 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9799 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9800 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 9801 | sqlite3_free(pSession->azFilter[ii]); |
| 9802 | } |
| 9803 | sqlite3_free(pSession->azFilter); |
| 9804 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 9805 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 9806 | if( pSession->azFilter==0 ){ |
| 9807 | raw_printf(stderr, "Error: out or memory\n"); |
| 9808 | exit(1); |
| 9809 | } |
| 9810 | for(ii=1; ii<nCmd; ii++){ |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9811 | char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| 9812 | shell_check_oom(x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9813 | } |
| 9814 | pSession->nFilter = ii-1; |
| 9815 | } |
| 9816 | }else |
| 9817 | |
| 9818 | /* .session indirect ?BOOLEAN? |
| 9819 | ** Query or set the indirect flag |
| 9820 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9821 | if( cli_strcmp(azCmd[0], "indirect")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9822 | int ii; |
| 9823 | if( nCmd>2 ) goto session_syntax_error; |
| 9824 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9825 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9826 | ii = sqlite3session_indirect(pSession->p, ii); |
| 9827 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 9828 | pSession->zName, ii); |
| 9829 | } |
| 9830 | }else |
| 9831 | |
| 9832 | /* .session isempty |
| 9833 | ** Determine if the session is empty |
| 9834 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9835 | if( cli_strcmp(azCmd[0], "isempty")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9836 | int ii; |
| 9837 | if( nCmd!=1 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9838 | if( pAuxDb->nSession ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9839 | ii = sqlite3session_isempty(pSession->p); |
| 9840 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 9841 | pSession->zName, ii); |
| 9842 | } |
| 9843 | }else |
| 9844 | |
| 9845 | /* .session list |
| 9846 | ** List all currently open sessions |
| 9847 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9848 | if( cli_strcmp(azCmd[0],"list")==0 ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9849 | for(i=0; i<pAuxDb->nSession; i++){ |
| 9850 | utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9851 | } |
| 9852 | }else |
| 9853 | |
| 9854 | /* .session open DB NAME |
| 9855 | ** Open a new session called NAME on the attached database DB. |
| 9856 | ** DB is normally "main". |
| 9857 | */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9858 | if( cli_strcmp(azCmd[0],"open")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9859 | char *zName; |
| 9860 | if( nCmd!=3 ) goto session_syntax_error; |
| 9861 | zName = azCmd[2]; |
| 9862 | if( zName[0]==0 ) goto session_syntax_error; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9863 | for(i=0; i<pAuxDb->nSession; i++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9864 | if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9865 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| 9866 | goto meta_command_exit; |
| 9867 | } |
| 9868 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9869 | if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ |
| 9870 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9871 | goto meta_command_exit; |
| 9872 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9873 | pSession = &pAuxDb->aSession[pAuxDb->nSession]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9874 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 9875 | if( rc ){ |
| 9876 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| 9877 | rc = 0; |
| 9878 | goto meta_command_exit; |
| 9879 | } |
| 9880 | pSession->nFilter = 0; |
| 9881 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 9882 | pAuxDb->nSession++; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9883 | pSession->zName = sqlite3_mprintf("%s", zName); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 9884 | shell_check_oom(pSession->zName); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9885 | }else |
| 9886 | /* If no command name matches, show a syntax error */ |
| 9887 | session_syntax_error: |
drh | eb7f2a0 | 2018-09-26 18:02:32 +0000 | [diff] [blame] | 9888 | showHelp(p->out, "session"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9889 | }else |
| 9890 | #endif |
| 9891 | |
| 9892 | #ifdef SQLITE_DEBUG |
| 9893 | /* Undocumented commands for internal testing. Subject to change |
| 9894 | ** without notice. */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9895 | if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 9896 | if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9897 | int i, v; |
| 9898 | for(i=1; i<nArg; i++){ |
| 9899 | v = booleanValue(azArg[i]); |
| 9900 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| 9901 | } |
| 9902 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9903 | if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9904 | int i; sqlite3_int64 v; |
| 9905 | for(i=1; i<nArg; i++){ |
| 9906 | char zBuf[200]; |
| 9907 | v = integerValue(azArg[i]); |
| 9908 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| 9909 | utf8_printf(p->out, "%s", zBuf); |
| 9910 | } |
| 9911 | } |
| 9912 | }else |
| 9913 | #endif |
| 9914 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9915 | if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9916 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 9917 | int bVerbose = 0; /* Verbose output */ |
| 9918 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 9919 | int i, k; /* Loop counters */ |
| 9920 | int nTest = 0; /* Number of tests runs */ |
| 9921 | int nErr = 0; /* Number of errors seen */ |
| 9922 | ShellText str; /* Answer for a query */ |
| 9923 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
| 9924 | |
| 9925 | open_db(p,0); |
| 9926 | for(i=1; i<nArg; i++){ |
| 9927 | const char *z = azArg[i]; |
| 9928 | if( z[0]=='-' && z[1]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9929 | if( cli_strcmp(z,"-init")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9930 | bIsInit = 1; |
| 9931 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9932 | if( cli_strcmp(z,"-v")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9933 | bVerbose++; |
| 9934 | }else |
| 9935 | { |
| 9936 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 9937 | azArg[i], azArg[0]); |
| 9938 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 9939 | rc = 1; |
| 9940 | goto meta_command_exit; |
| 9941 | } |
| 9942 | } |
| 9943 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 9944 | != SQLITE_OK ){ |
| 9945 | bSelftestExists = 0; |
| 9946 | }else{ |
| 9947 | bSelftestExists = 1; |
| 9948 | } |
| 9949 | if( bIsInit ){ |
| 9950 | createSelftestTable(p); |
| 9951 | bSelftestExists = 1; |
| 9952 | } |
| 9953 | initText(&str); |
| 9954 | appendText(&str, "x", 0); |
| 9955 | for(k=bSelftestExists; k>=0; k--){ |
| 9956 | if( k==1 ){ |
| 9957 | rc = sqlite3_prepare_v2(p->db, |
| 9958 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 9959 | -1, &pStmt, 0); |
| 9960 | }else{ |
| 9961 | rc = sqlite3_prepare_v2(p->db, |
| 9962 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 9963 | " (1,'run','PRAGMA integrity_check','ok')", |
| 9964 | -1, &pStmt, 0); |
| 9965 | } |
| 9966 | if( rc ){ |
| 9967 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 9968 | rc = 1; |
| 9969 | sqlite3_finalize(pStmt); |
| 9970 | goto meta_command_exit; |
| 9971 | } |
| 9972 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 9973 | int tno = sqlite3_column_int(pStmt, 0); |
| 9974 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 9975 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 9976 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
| 9977 | |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 9978 | if( zOp==0 ) continue; |
| 9979 | if( zSql==0 ) continue; |
| 9980 | if( zAns==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9981 | k = 0; |
| 9982 | if( bVerbose>0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9983 | printf("%d: %s %s\n", tno, zOp, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9984 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9985 | if( cli_strcmp(zOp,"memo")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9986 | utf8_printf(p->out, "%s\n", zSql); |
| 9987 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 9988 | if( cli_strcmp(zOp,"run")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 9989 | char *zErrMsg = 0; |
| 9990 | str.n = 0; |
| 9991 | str.z[0] = 0; |
| 9992 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 9993 | nTest++; |
| 9994 | if( bVerbose ){ |
| 9995 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 9996 | } |
| 9997 | if( rc || zErrMsg ){ |
| 9998 | nErr++; |
| 9999 | rc = 1; |
| 10000 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 10001 | sqlite3_free(zErrMsg); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10002 | }else if( cli_strcmp(zAns,str.z)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10003 | nErr++; |
| 10004 | rc = 1; |
| 10005 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 10006 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 10007 | } |
| 10008 | }else |
| 10009 | { |
| 10010 | utf8_printf(stderr, |
| 10011 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 10012 | rc = 1; |
| 10013 | break; |
| 10014 | } |
| 10015 | } /* End loop over rows of content from SELFTEST */ |
| 10016 | sqlite3_finalize(pStmt); |
| 10017 | } /* End loop over k */ |
| 10018 | freeText(&str); |
| 10019 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 10020 | }else |
| 10021 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10022 | if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10023 | if( nArg<2 || nArg>3 ){ |
| 10024 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 10025 | rc = 1; |
| 10026 | } |
| 10027 | if( nArg>=2 ){ |
| 10028 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| 10029 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| 10030 | } |
| 10031 | if( nArg>=3 ){ |
| 10032 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 10033 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 10034 | } |
| 10035 | }else |
| 10036 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10037 | if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10038 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 10039 | int i; /* Loop counter */ |
| 10040 | int bSchema = 0; /* Also hash the schema */ |
| 10041 | int bSeparate = 0; /* Hash each table separately */ |
| 10042 | int iSize = 224; /* Hash algorithm to use */ |
| 10043 | int bDebug = 0; /* Only show the query that would have run */ |
| 10044 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 10045 | char *zSql; /* SQL to be run */ |
| 10046 | char *zSep; /* Separator */ |
| 10047 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
| 10048 | ShellText sQuery; /* Set of queries used to read all content */ |
| 10049 | open_db(p, 0); |
| 10050 | for(i=1; i<nArg; i++){ |
| 10051 | const char *z = azArg[i]; |
| 10052 | if( z[0]=='-' ){ |
| 10053 | z++; |
| 10054 | if( z[0]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10055 | if( cli_strcmp(z,"schema")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10056 | bSchema = 1; |
| 10057 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10058 | if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 |
| 10059 | || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10060 | ){ |
| 10061 | iSize = atoi(&z[5]); |
| 10062 | }else |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10063 | if( cli_strcmp(z,"debug")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10064 | bDebug = 1; |
| 10065 | }else |
| 10066 | { |
| 10067 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 10068 | azArg[i], azArg[0]); |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10069 | showHelp(p->out, azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10070 | rc = 1; |
| 10071 | goto meta_command_exit; |
| 10072 | } |
| 10073 | }else if( zLike ){ |
| 10074 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 10075 | rc = 1; |
| 10076 | goto meta_command_exit; |
| 10077 | }else{ |
| 10078 | zLike = z; |
| 10079 | bSeparate = 1; |
drh | cedfecf | 2018-03-23 12:59:10 +0000 | [diff] [blame] | 10080 | if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10081 | } |
| 10082 | } |
| 10083 | if( bSchema ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10084 | zSql = "SELECT lower(name) FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10085 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10086 | " UNION ALL SELECT 'sqlite_schema'" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10087 | " ORDER BY 1 collate nocase"; |
| 10088 | }else{ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10089 | zSql = "SELECT lower(name) FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10090 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 10091 | " AND name NOT LIKE 'sqlite_%'" |
| 10092 | " ORDER BY 1 collate nocase"; |
| 10093 | } |
| 10094 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 10095 | initText(&sQuery); |
| 10096 | initText(&sSql); |
| 10097 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 10098 | zSep = "VALUES("; |
| 10099 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 10100 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
drh | 621a5e0 | 2021-12-16 17:35:27 +0000 | [diff] [blame] | 10101 | if( zTab==0 ) continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10102 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10103 | if( cli_strncmp(zTab, "sqlite_",7)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10104 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 10105 | appendText(&sQuery,zTab,'"'); |
| 10106 | appendText(&sQuery," NOT INDEXED;", 0); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10107 | }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){ |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10108 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10109 | " ORDER BY name;", 0); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10110 | }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10111 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 10112 | " ORDER BY name;", 0); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10113 | }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10114 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 10115 | " ORDER BY tbl,idx;", 0); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10116 | }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10117 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 10118 | appendText(&sQuery, zTab, 0); |
| 10119 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 10120 | } |
| 10121 | appendText(&sSql, zSep, 0); |
| 10122 | appendText(&sSql, sQuery.z, '\''); |
| 10123 | sQuery.n = 0; |
| 10124 | appendText(&sSql, ",", 0); |
| 10125 | appendText(&sSql, zTab, '\''); |
| 10126 | zSep = "),("; |
| 10127 | } |
| 10128 | sqlite3_finalize(pStmt); |
| 10129 | if( bSeparate ){ |
| 10130 | zSql = sqlite3_mprintf( |
| 10131 | "%s))" |
| 10132 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 10133 | " FROM [sha3sum$query]", |
| 10134 | sSql.z, iSize); |
| 10135 | }else{ |
| 10136 | zSql = sqlite3_mprintf( |
| 10137 | "%s))" |
| 10138 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 10139 | " FROM [sha3sum$query]", |
| 10140 | sSql.z, iSize); |
| 10141 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10142 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10143 | freeText(&sQuery); |
| 10144 | freeText(&sSql); |
| 10145 | if( bDebug ){ |
| 10146 | utf8_printf(p->out, "%s\n", zSql); |
| 10147 | }else{ |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 10148 | shell_exec(p, zSql, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10149 | } |
| 10150 | sqlite3_free(zSql); |
| 10151 | }else |
| 10152 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 10153 | #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10154 | if( c=='s' |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10155 | && (cli_strncmp(azArg[0], "shell", n)==0 |
| 10156 | || cli_strncmp(azArg[0],"system",n)==0) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10157 | ){ |
| 10158 | char *zCmd; |
| 10159 | int i, x; |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 10160 | failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10161 | if( nArg<2 ){ |
| 10162 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
| 10163 | rc = 1; |
| 10164 | goto meta_command_exit; |
| 10165 | } |
| 10166 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10167 | for(i=2; i<nArg && zCmd!=0; i++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10168 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 10169 | zCmd, azArg[i]); |
| 10170 | } |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10171 | x = zCmd!=0 ? system(zCmd) : 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10172 | sqlite3_free(zCmd); |
| 10173 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| 10174 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 10175 | #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10176 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10177 | if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 10178 | static const char *azBool[] = { "off", "on", "trigger", "full"}; |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10179 | const char *zOut; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10180 | int i; |
| 10181 | if( nArg!=1 ){ |
| 10182 | raw_printf(stderr, "Usage: .show\n"); |
| 10183 | rc = 1; |
| 10184 | goto meta_command_exit; |
| 10185 | } |
| 10186 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 10187 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10188 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 10189 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 10190 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 10191 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 10192 | if( p->mode==MODE_Column |
| 10193 | || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) |
| 10194 | ){ |
larrybr | cc4d55c | 2022-02-01 02:50:45 +0000 | [diff] [blame] | 10195 | utf8_printf |
| 10196 | (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", |
| 10197 | modeDescr[p->mode], p->cmOpts.iWrap, |
| 10198 | p->cmOpts.bWordWrap ? "on" : "off", |
| 10199 | p->cmOpts.bQuote ? "" : "no"); |
drh | e40f286 | 2022-01-31 14:14:29 +0000 | [diff] [blame] | 10200 | }else{ |
| 10201 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 10202 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10203 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| 10204 | output_c_string(p->out, p->nullValue); |
| 10205 | raw_printf(p->out, "\n"); |
| 10206 | utf8_printf(p->out,"%12.12s: %s\n","output", |
| 10207 | strlen30(p->outfile) ? p->outfile : "stdout"); |
| 10208 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
| 10209 | output_c_string(p->out, p->colSeparator); |
| 10210 | raw_printf(p->out, "\n"); |
| 10211 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
| 10212 | output_c_string(p->out, p->rowSeparator); |
| 10213 | raw_printf(p->out, "\n"); |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10214 | switch( p->statsOn ){ |
| 10215 | case 0: zOut = "off"; break; |
| 10216 | default: zOut = "on"; break; |
| 10217 | case 2: zOut = "stmt"; break; |
| 10218 | case 3: zOut = "vmstep"; break; |
| 10219 | } |
| 10220 | utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10221 | utf8_printf(p->out, "%12.12s: ", "width"); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 10222 | for (i=0;i<p->nWidth;i++) { |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10223 | raw_printf(p->out, "%d ", p->colWidth[i]); |
| 10224 | } |
| 10225 | raw_printf(p->out, "\n"); |
| 10226 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 10227 | p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10228 | }else |
| 10229 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10230 | if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10231 | if( nArg==2 ){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10232 | if( cli_strcmp(azArg[1],"stmt")==0 ){ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10233 | p->statsOn = 2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10234 | }else if( cli_strcmp(azArg[1],"vmstep")==0 ){ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10235 | p->statsOn = 3; |
| 10236 | }else{ |
| 10237 | p->statsOn = (u8)booleanValue(azArg[1]); |
| 10238 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10239 | }else if( nArg==1 ){ |
| 10240 | display_stats(p->db, p, 0); |
| 10241 | }else{ |
drh | a6e6cf2 | 2021-01-09 19:10:04 +0000 | [diff] [blame] | 10242 | raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10243 | rc = 1; |
| 10244 | } |
| 10245 | }else |
| 10246 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10247 | if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) |
| 10248 | || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 |
| 10249 | || cli_strncmp(azArg[0], "indexes", n)==0) ) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10250 | ){ |
| 10251 | sqlite3_stmt *pStmt; |
| 10252 | char **azResult; |
| 10253 | int nRow, nAlloc; |
| 10254 | int ii; |
| 10255 | ShellText s; |
| 10256 | initText(&s); |
| 10257 | open_db(p, 0); |
| 10258 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 10259 | if( rc ){ |
| 10260 | sqlite3_finalize(pStmt); |
| 10261 | return shellDatabaseError(p->db); |
| 10262 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10263 | |
| 10264 | if( nArg>2 && c=='i' ){ |
| 10265 | /* It is an historical accident that the .indexes command shows an error |
| 10266 | ** when called with the wrong number of arguments whereas the .tables |
| 10267 | ** command does not. */ |
| 10268 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 10269 | rc = 1; |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 10270 | sqlite3_finalize(pStmt); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10271 | goto meta_command_exit; |
| 10272 | } |
| 10273 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
| 10274 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 10275 | if( zDbName==0 ) continue; |
| 10276 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 10277 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 10278 | appendText(&s, "SELECT name FROM ", 0); |
| 10279 | }else{ |
| 10280 | appendText(&s, "SELECT ", 0); |
| 10281 | appendText(&s, zDbName, '\''); |
| 10282 | appendText(&s, "||'.'||name FROM ", 0); |
| 10283 | } |
| 10284 | appendText(&s, zDbName, '"'); |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 10285 | appendText(&s, ".sqlite_schema ", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10286 | if( c=='t' ){ |
| 10287 | appendText(&s," WHERE type IN ('table','view')" |
| 10288 | " AND name NOT LIKE 'sqlite_%'" |
| 10289 | " AND name LIKE ?1", 0); |
| 10290 | }else{ |
| 10291 | appendText(&s," WHERE type='index'" |
| 10292 | " AND tbl_name LIKE ?1", 0); |
| 10293 | } |
| 10294 | } |
| 10295 | rc = sqlite3_finalize(pStmt); |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 10296 | if( rc==SQLITE_OK ){ |
| 10297 | appendText(&s, " ORDER BY 1", 0); |
| 10298 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 10299 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10300 | freeText(&s); |
| 10301 | if( rc ) return shellDatabaseError(p->db); |
| 10302 | |
| 10303 | /* Run the SQL statement prepared by the above block. Store the results |
| 10304 | ** as an array of nul-terminated strings in azResult[]. */ |
| 10305 | nRow = nAlloc = 0; |
| 10306 | azResult = 0; |
| 10307 | if( nArg>1 ){ |
| 10308 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
| 10309 | }else{ |
| 10310 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 10311 | } |
| 10312 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 10313 | if( nRow>=nAlloc ){ |
| 10314 | char **azNew; |
| 10315 | int n2 = nAlloc*2 + 10; |
| 10316 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10317 | shell_check_oom(azNew); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10318 | nAlloc = n2; |
| 10319 | azResult = azNew; |
| 10320 | } |
| 10321 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 10322 | shell_check_oom(azResult[nRow]); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10323 | nRow++; |
| 10324 | } |
| 10325 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 10326 | rc = shellDatabaseError(p->db); |
| 10327 | } |
| 10328 | |
| 10329 | /* Pretty-print the contents of array azResult[] to the output */ |
| 10330 | if( rc==0 && nRow>0 ){ |
| 10331 | int len, maxlen = 0; |
| 10332 | int i, j; |
| 10333 | int nPrintCol, nPrintRow; |
| 10334 | for(i=0; i<nRow; i++){ |
| 10335 | len = strlen30(azResult[i]); |
| 10336 | if( len>maxlen ) maxlen = len; |
| 10337 | } |
| 10338 | nPrintCol = 80/(maxlen+2); |
| 10339 | if( nPrintCol<1 ) nPrintCol = 1; |
| 10340 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 10341 | for(i=0; i<nPrintRow; i++){ |
| 10342 | for(j=i; j<nRow; j+=nPrintRow){ |
| 10343 | char *zSp = j<nPrintRow ? "" : " "; |
| 10344 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 10345 | azResult[j] ? azResult[j]:""); |
| 10346 | } |
| 10347 | raw_printf(p->out, "\n"); |
| 10348 | } |
| 10349 | } |
| 10350 | |
| 10351 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 10352 | sqlite3_free(azResult); |
| 10353 | }else |
| 10354 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 10355 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10356 | /* Begin redirecting output to the file "testcase-out.txt" */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10357 | if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10358 | output_reset(p); |
drh | a92a01a | 2018-01-10 22:15:37 +0000 | [diff] [blame] | 10359 | p->out = output_file_open("testcase-out.txt", 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10360 | if( p->out==0 ){ |
| 10361 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
| 10362 | } |
| 10363 | if( nArg>=2 ){ |
| 10364 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 10365 | }else{ |
| 10366 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 10367 | } |
| 10368 | }else |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 10369 | #endif /* !defined(SQLITE_SHELL_FIDDLE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10370 | |
| 10371 | #ifndef SQLITE_UNTESTABLE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10372 | if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10373 | static const struct { |
| 10374 | const char *zCtrlName; /* Name of a test-control option */ |
| 10375 | int ctrlCode; /* Integer code for that option */ |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10376 | int unSafe; /* Not valid for --safe mode */ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10377 | const char *zUsage; /* Usage notes */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10378 | } aCtrl[] = { |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10379 | { "always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, |
| 10380 | { "assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, |
| 10381 | /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ |
| 10382 | /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/ |
| 10383 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, |
| 10384 | { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, |
| 10385 | /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/ |
| 10386 | { "imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, |
| 10387 | { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, |
| 10388 | { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, |
| 10389 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, |
| 10390 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10391 | #ifdef YYCOVERAGE |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10392 | { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10393 | #endif |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10394 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " }, |
| 10395 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, |
| 10396 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, |
| 10397 | { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, |
| 10398 | { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, |
| 10399 | { "sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, |
| 10400 | { "tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10401 | }; |
| 10402 | int testctrl = -1; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10403 | int iCtrl = -1; |
| 10404 | int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ |
| 10405 | int isOk = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10406 | int i, n2; |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 10407 | const char *zCmd = 0; |
| 10408 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10409 | open_db(p, 0); |
mistachkin | c6bc15a | 2017-11-21 21:14:32 +0000 | [diff] [blame] | 10410 | zCmd = nArg>=2 ? azArg[1] : "help"; |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10411 | |
| 10412 | /* The argument can optionally begin with "-" or "--" */ |
| 10413 | if( zCmd[0]=='-' && zCmd[1] ){ |
| 10414 | zCmd++; |
| 10415 | if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
| 10416 | } |
| 10417 | |
| 10418 | /* --help lists all test-controls */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10419 | if( cli_strcmp(zCmd,"help")==0 ){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10420 | utf8_printf(p->out, "Available test-controls:\n"); |
| 10421 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10422 | utf8_printf(p->out, " .testctrl %s %s\n", |
| 10423 | aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10424 | } |
| 10425 | rc = 1; |
| 10426 | goto meta_command_exit; |
| 10427 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10428 | |
| 10429 | /* convert testctrl text option to value. allow any unique prefix |
| 10430 | ** of the option name, or a numerical value. */ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10431 | n2 = strlen30(zCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10432 | for(i=0; i<ArraySize(aCtrl); i++){ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10433 | if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10434 | if( testctrl<0 ){ |
| 10435 | testctrl = aCtrl[i].ctrlCode; |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10436 | iCtrl = i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10437 | }else{ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10438 | utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" |
| 10439 | "Use \".testctrl --help\" for help\n", zCmd); |
| 10440 | rc = 1; |
| 10441 | goto meta_command_exit; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10442 | } |
| 10443 | } |
| 10444 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10445 | if( testctrl<0 ){ |
drh | 35f51a4 | 2017-11-15 17:07:22 +0000 | [diff] [blame] | 10446 | utf8_printf(stderr,"Error: unknown test-control: %s\n" |
| 10447 | "Use \".testctrl --help\" for help\n", zCmd); |
drh | 38ed1ce | 2021-12-06 15:24:36 +0000 | [diff] [blame] | 10448 | }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ |
| 10449 | utf8_printf(stderr, |
| 10450 | "line %d: \".testctrl %s\" may not be used in safe mode\n", |
| 10451 | p->lineno, aCtrl[iCtrl].zCtrlName); |
| 10452 | exit(1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10453 | }else{ |
| 10454 | switch(testctrl){ |
| 10455 | |
| 10456 | /* sqlite3_test_control(int, db, int) */ |
| 10457 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10458 | if( nArg==3 ){ |
drh | af7b765 | 2021-01-13 19:28:17 +0000 | [diff] [blame] | 10459 | unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10460 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10461 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10462 | } |
| 10463 | break; |
| 10464 | |
| 10465 | /* sqlite3_test_control(int) */ |
| 10466 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 10467 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10468 | case SQLITE_TESTCTRL_BYTEORDER: |
| 10469 | if( nArg==2 ){ |
| 10470 | rc2 = sqlite3_test_control(testctrl); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10471 | isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10472 | } |
| 10473 | break; |
| 10474 | |
| 10475 | /* sqlite3_test_control(int, uint) */ |
| 10476 | case SQLITE_TESTCTRL_PENDING_BYTE: |
| 10477 | if( nArg==3 ){ |
| 10478 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
| 10479 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10480 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10481 | } |
| 10482 | break; |
| 10483 | |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10484 | /* sqlite3_test_control(int, int, sqlite3*) */ |
| 10485 | case SQLITE_TESTCTRL_PRNG_SEED: |
| 10486 | if( nArg==3 || nArg==4 ){ |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 10487 | int ii = (int)integerValue(azArg[2]); |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10488 | sqlite3 *db; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10489 | if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){ |
drh | 41428a9 | 2019-08-12 16:25:11 +0000 | [diff] [blame] | 10490 | sqlite3_randomness(sizeof(ii),&ii); |
| 10491 | printf("-- random seed: %d\n", ii); |
| 10492 | } |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10493 | if( nArg==3 ){ |
| 10494 | db = 0; |
| 10495 | }else{ |
| 10496 | db = p->db; |
| 10497 | /* Make sure the schema has been loaded */ |
| 10498 | sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); |
| 10499 | } |
drh | 51755a7 | 2019-08-08 19:40:29 +0000 | [diff] [blame] | 10500 | rc2 = sqlite3_test_control(testctrl, ii, db); |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 10501 | isOk = 3; |
| 10502 | } |
| 10503 | break; |
| 10504 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10505 | /* sqlite3_test_control(int, int) */ |
| 10506 | case SQLITE_TESTCTRL_ASSERT: |
| 10507 | case SQLITE_TESTCTRL_ALWAYS: |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10508 | if( nArg==3 ){ |
| 10509 | int opt = booleanValue(azArg[2]); |
| 10510 | rc2 = sqlite3_test_control(testctrl, opt); |
| 10511 | isOk = 1; |
| 10512 | } |
| 10513 | break; |
| 10514 | |
| 10515 | /* sqlite3_test_control(int, int) */ |
| 10516 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10517 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
| 10518 | if( nArg==3 ){ |
| 10519 | int opt = booleanValue(azArg[2]); |
| 10520 | rc2 = sqlite3_test_control(testctrl, opt); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10521 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10522 | } |
| 10523 | break; |
| 10524 | |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 10525 | /* sqlite3_test_control(sqlite3*) */ |
| 10526 | case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: |
| 10527 | rc2 = sqlite3_test_control(testctrl, p->db); |
drh | 2a83c10 | 2020-01-01 23:02:35 +0000 | [diff] [blame] | 10528 | isOk = 3; |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 10529 | break; |
| 10530 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10531 | case SQLITE_TESTCTRL_IMPOSTER: |
| 10532 | if( nArg==5 ){ |
| 10533 | rc2 = sqlite3_test_control(testctrl, p->db, |
| 10534 | azArg[2], |
| 10535 | integerValue(azArg[3]), |
| 10536 | integerValue(azArg[4])); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10537 | isOk = 3; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10538 | } |
| 10539 | break; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10540 | |
drh | 37ccfcf | 2020-08-31 18:49:04 +0000 | [diff] [blame] | 10541 | case SQLITE_TESTCTRL_SEEK_COUNT: { |
| 10542 | u64 x = 0; |
| 10543 | rc2 = sqlite3_test_control(testctrl, p->db, &x); |
| 10544 | utf8_printf(p->out, "%llu\n", x); |
| 10545 | isOk = 3; |
| 10546 | break; |
| 10547 | } |
| 10548 | |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10549 | #ifdef YYCOVERAGE |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 10550 | case SQLITE_TESTCTRL_PARSER_COVERAGE: { |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10551 | if( nArg==2 ){ |
| 10552 | sqlite3_test_control(testctrl, p->out); |
| 10553 | isOk = 3; |
| 10554 | } |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 10555 | break; |
| 10556 | } |
| 10557 | #endif |
| 10558 | #ifdef SQLITE_DEBUG |
| 10559 | case SQLITE_TESTCTRL_TUNE: { |
| 10560 | if( nArg==4 ){ |
| 10561 | int id = (int)integerValue(azArg[2]); |
drh | 2d26cfc | 2021-06-04 13:40:26 +0000 | [diff] [blame] | 10562 | int val = (int)integerValue(azArg[3]); |
| 10563 | sqlite3_test_control(testctrl, id, &val); |
| 10564 | isOk = 3; |
| 10565 | }else if( nArg==3 ){ |
| 10566 | int id = (int)integerValue(azArg[2]); |
| 10567 | sqlite3_test_control(testctrl, -id, &rc2); |
| 10568 | isOk = 1; |
| 10569 | }else if( nArg==2 ){ |
| 10570 | int id = 1; |
| 10571 | while(1){ |
| 10572 | int val = 0; |
| 10573 | rc2 = sqlite3_test_control(testctrl, -id, &val); |
| 10574 | if( rc2!=SQLITE_OK ) break; |
| 10575 | if( id>1 ) utf8_printf(p->out, " "); |
| 10576 | utf8_printf(p->out, "%d: %d", id, val); |
| 10577 | id++; |
| 10578 | } |
| 10579 | if( id>1 ) utf8_printf(p->out, "\n"); |
drh | f3c1256 | 2021-06-04 13:16:46 +0000 | [diff] [blame] | 10580 | isOk = 3; |
| 10581 | } |
| 10582 | break; |
| 10583 | } |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 10584 | #endif |
dan | 779e990 | 2021-07-28 18:13:28 +0000 | [diff] [blame] | 10585 | case SQLITE_TESTCTRL_SORTER_MMAP: |
| 10586 | if( nArg==3 ){ |
| 10587 | int opt = (unsigned int)integerValue(azArg[2]); |
| 10588 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
| 10589 | isOk = 3; |
| 10590 | } |
| 10591 | break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10592 | } |
| 10593 | } |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10594 | if( isOk==0 && iCtrl>=0 ){ |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10595 | utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
drh | ef302e8 | 2017-11-15 19:14:08 +0000 | [diff] [blame] | 10596 | rc = 1; |
| 10597 | }else if( isOk==1 ){ |
| 10598 | raw_printf(p->out, "%d\n", rc2); |
| 10599 | }else if( isOk==2 ){ |
| 10600 | raw_printf(p->out, "0x%08x\n", rc2); |
| 10601 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10602 | }else |
| 10603 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 10604 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10605 | if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10606 | open_db(p, 0); |
| 10607 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
| 10608 | }else |
| 10609 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10610 | if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10611 | if( nArg==2 ){ |
| 10612 | enableTimer = booleanValue(azArg[1]); |
| 10613 | if( enableTimer && !HAS_TIMER ){ |
| 10614 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
| 10615 | enableTimer = 0; |
| 10616 | } |
| 10617 | }else{ |
| 10618 | raw_printf(stderr, "Usage: .timer on|off\n"); |
| 10619 | rc = 1; |
| 10620 | } |
| 10621 | }else |
| 10622 | |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 10623 | #ifndef SQLITE_OMIT_TRACE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10624 | if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 10625 | int mType = 0; |
| 10626 | int jj; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10627 | open_db(p, 0); |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 10628 | for(jj=1; jj<nArg; jj++){ |
| 10629 | const char *z = azArg[jj]; |
| 10630 | if( z[0]=='-' ){ |
| 10631 | if( optionMatch(z, "expanded") ){ |
| 10632 | p->eTraceType = SHELL_TRACE_EXPANDED; |
| 10633 | } |
| 10634 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 10635 | else if( optionMatch(z, "normalized") ){ |
| 10636 | p->eTraceType = SHELL_TRACE_NORMALIZED; |
| 10637 | } |
| 10638 | #endif |
| 10639 | else if( optionMatch(z, "plain") ){ |
| 10640 | p->eTraceType = SHELL_TRACE_PLAIN; |
| 10641 | } |
| 10642 | else if( optionMatch(z, "profile") ){ |
| 10643 | mType |= SQLITE_TRACE_PROFILE; |
| 10644 | } |
| 10645 | else if( optionMatch(z, "row") ){ |
| 10646 | mType |= SQLITE_TRACE_ROW; |
| 10647 | } |
| 10648 | else if( optionMatch(z, "stmt") ){ |
| 10649 | mType |= SQLITE_TRACE_STMT; |
| 10650 | } |
| 10651 | else if( optionMatch(z, "close") ){ |
| 10652 | mType |= SQLITE_TRACE_CLOSE; |
| 10653 | } |
| 10654 | else { |
| 10655 | raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); |
| 10656 | rc = 1; |
| 10657 | goto meta_command_exit; |
| 10658 | } |
| 10659 | }else{ |
| 10660 | output_file_close(p->traceOut); |
| 10661 | p->traceOut = output_file_open(azArg[1], 0); |
| 10662 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10663 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10664 | if( p->traceOut==0 ){ |
| 10665 | sqlite3_trace_v2(p->db, 0, 0, 0); |
| 10666 | }else{ |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 10667 | if( mType==0 ) mType = SQLITE_TRACE_STMT; |
| 10668 | sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10669 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10670 | }else |
drh | 707821f | 2018-12-05 13:39:06 +0000 | [diff] [blame] | 10671 | #endif /* !defined(SQLITE_OMIT_TRACE) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10672 | |
drh | e2b7a76 | 2019-10-02 00:25:08 +0000 | [diff] [blame] | 10673 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10674 | if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){ |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 10675 | int ii; |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 10676 | int lenOpt; |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 10677 | char *zOpt; |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 10678 | if( nArg<2 ){ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 10679 | raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 10680 | rc = 1; |
| 10681 | goto meta_command_exit; |
| 10682 | } |
| 10683 | open_db(p, 0); |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 10684 | zOpt = azArg[1]; |
| 10685 | if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 10686 | lenOpt = (int)strlen(zOpt); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10687 | if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 10688 | assert( azArg[nArg]==0 ); |
drh | 8c754a3 | 2019-08-19 20:35:30 +0000 | [diff] [blame] | 10689 | sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); |
drh | 5df8428 | 2019-08-17 19:45:25 +0000 | [diff] [blame] | 10690 | }else{ |
| 10691 | for(ii=1; ii<nArg; ii++){ |
| 10692 | sqlite3_create_module(p->db, azArg[ii], 0, 0); |
| 10693 | } |
drh | cc5979d | 2019-08-16 22:58:29 +0000 | [diff] [blame] | 10694 | } |
| 10695 | }else |
| 10696 | #endif |
| 10697 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10698 | #if SQLITE_USER_AUTHENTICATION |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10699 | if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10700 | if( nArg<2 ){ |
| 10701 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
| 10702 | rc = 1; |
| 10703 | goto meta_command_exit; |
| 10704 | } |
| 10705 | open_db(p, 0); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10706 | if( cli_strcmp(azArg[1],"login")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10707 | if( nArg!=4 ){ |
| 10708 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
| 10709 | rc = 1; |
| 10710 | goto meta_command_exit; |
| 10711 | } |
drh | e2754c1 | 2019-08-26 12:50:01 +0000 | [diff] [blame] | 10712 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 10713 | strlen30(azArg[3])); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10714 | if( rc ){ |
| 10715 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
| 10716 | rc = 1; |
| 10717 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10718 | }else if( cli_strcmp(azArg[1],"add")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10719 | if( nArg!=5 ){ |
| 10720 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
| 10721 | rc = 1; |
| 10722 | goto meta_command_exit; |
| 10723 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 10724 | rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10725 | booleanValue(azArg[4])); |
| 10726 | if( rc ){ |
| 10727 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
| 10728 | rc = 1; |
| 10729 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10730 | }else if( cli_strcmp(azArg[1],"edit")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10731 | if( nArg!=5 ){ |
| 10732 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
| 10733 | rc = 1; |
| 10734 | goto meta_command_exit; |
| 10735 | } |
drh | af2770f | 2018-01-05 14:55:43 +0000 | [diff] [blame] | 10736 | rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10737 | booleanValue(azArg[4])); |
| 10738 | if( rc ){ |
| 10739 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
| 10740 | rc = 1; |
| 10741 | } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10742 | }else if( cli_strcmp(azArg[1],"delete")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10743 | if( nArg!=3 ){ |
| 10744 | raw_printf(stderr, "Usage: .user delete USER\n"); |
| 10745 | rc = 1; |
| 10746 | goto meta_command_exit; |
| 10747 | } |
| 10748 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 10749 | if( rc ){ |
| 10750 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
| 10751 | rc = 1; |
| 10752 | } |
| 10753 | }else{ |
| 10754 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
| 10755 | rc = 1; |
| 10756 | goto meta_command_exit; |
| 10757 | } |
| 10758 | }else |
| 10759 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 10760 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10761 | if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10762 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
| 10763 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | 0ed2fd8 | 2018-01-16 20:05:27 +0000 | [diff] [blame] | 10764 | #if SQLITE_HAVE_ZLIB |
| 10765 | utf8_printf(p->out, "zlib version %s\n", zlibVersion()); |
| 10766 | #endif |
| 10767 | #define CTIMEOPT_VAL_(opt) #opt |
| 10768 | #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) |
| 10769 | #if defined(__clang__) && defined(__clang_major__) |
| 10770 | utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." |
| 10771 | CTIMEOPT_VAL(__clang_minor__) "." |
| 10772 | CTIMEOPT_VAL(__clang_patchlevel__) "\n"); |
| 10773 | #elif defined(_MSC_VER) |
| 10774 | utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); |
| 10775 | #elif defined(__GNUC__) && defined(__VERSION__) |
| 10776 | utf8_printf(p->out, "gcc-" __VERSION__ "\n"); |
| 10777 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10778 | }else |
| 10779 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10780 | if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10781 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 10782 | sqlite3_vfs *pVfs = 0; |
| 10783 | if( p->db ){ |
| 10784 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 10785 | if( pVfs ){ |
| 10786 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 10787 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 10788 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 10789 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 10790 | } |
| 10791 | } |
| 10792 | }else |
| 10793 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10794 | if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10795 | sqlite3_vfs *pVfs; |
| 10796 | sqlite3_vfs *pCurrent = 0; |
| 10797 | if( p->db ){ |
| 10798 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 10799 | } |
| 10800 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 10801 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 10802 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 10803 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 10804 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 10805 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 10806 | if( pVfs->pNext ){ |
| 10807 | raw_printf(p->out, "-----------------------------------\n"); |
| 10808 | } |
| 10809 | } |
| 10810 | }else |
| 10811 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10812 | if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10813 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 10814 | char *zVfsName = 0; |
| 10815 | if( p->db ){ |
| 10816 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 10817 | if( zVfsName ){ |
| 10818 | utf8_printf(p->out, "%s\n", zVfsName); |
| 10819 | sqlite3_free(zVfsName); |
| 10820 | } |
| 10821 | } |
| 10822 | }else |
| 10823 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10824 | if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c0622a4 | 2020-12-04 01:17:57 +0000 | [diff] [blame] | 10825 | unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
| 10826 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10827 | }else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10828 | |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 10829 | if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10830 | int j; |
| 10831 | assert( nArg<=ArraySize(azArg) ); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 10832 | p->nWidth = nArg-1; |
drh | 76fc88f | 2021-10-02 16:39:16 +0000 | [diff] [blame] | 10833 | p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 10834 | if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); |
| 10835 | if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; |
| 10836 | for(j=1; j<nArg; j++){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10837 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
| 10838 | } |
| 10839 | }else |
| 10840 | |
| 10841 | { |
| 10842 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
| 10843 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
| 10844 | rc = 1; |
| 10845 | } |
| 10846 | |
| 10847 | meta_command_exit: |
| 10848 | if( p->outCount ){ |
| 10849 | p->outCount--; |
| 10850 | if( p->outCount==0 ) output_reset(p); |
| 10851 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 10852 | p->bSafeMode = p->bSafeModePersist; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10853 | return rc; |
| 10854 | } |
| 10855 | |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10856 | /* Line scan result and intermediate states (supporting scan resumption) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10857 | */ |
drh | 68911c2 | 2021-09-22 14:26:22 +0000 | [diff] [blame] | 10858 | #ifndef CHAR_BIT |
| 10859 | # define CHAR_BIT 8 |
| 10860 | #endif |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10861 | typedef enum { |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10862 | QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, |
| 10863 | QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 10864 | QSS_Start = 0 |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10865 | } QuickScanState; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10866 | #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) |
| 10867 | #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) |
| 10868 | #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 10869 | #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10870 | #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10871 | |
| 10872 | /* |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10873 | ** Scan line for classification to guide shell's handling. |
| 10874 | ** The scan is resumable for subsequent lines when prior |
| 10875 | ** return values are passed as the 2nd argument. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10876 | */ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10877 | static QuickScanState quickscan(char *zLine, QuickScanState qss){ |
| 10878 | char cin; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10879 | char cWait = (char)qss; /* intentional narrowing loss */ |
| 10880 | if( cWait==0 ){ |
| 10881 | PlainScan: |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 10882 | assert( cWait==0 ); |
drh | fd7abcd | 2021-09-22 13:43:16 +0000 | [diff] [blame] | 10883 | while( (cin = *zLine++)!=0 ){ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10884 | if( IsSpace(cin) ) |
| 10885 | continue; |
| 10886 | switch (cin){ |
| 10887 | case '-': |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10888 | if( *zLine!='-' ) |
| 10889 | break; |
| 10890 | while((cin = *++zLine)!=0 ) |
| 10891 | if( cin=='\n') |
| 10892 | goto PlainScan; |
| 10893 | return qss; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10894 | case ';': |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 10895 | qss |= QSS_EndingSemi; |
| 10896 | continue; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10897 | case '/': |
| 10898 | if( *zLine=='*' ){ |
| 10899 | ++zLine; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10900 | cWait = '*'; |
| 10901 | qss = QSS_SETV(qss, cWait); |
| 10902 | goto TermScan; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10903 | } |
| 10904 | break; |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 10905 | case '[': |
| 10906 | cin = ']'; |
| 10907 | /* fall thru */ |
| 10908 | case '`': case '\'': case '"': |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10909 | cWait = cin; |
| 10910 | qss = QSS_HasDark | cWait; |
| 10911 | goto TermScan; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10912 | default: |
| 10913 | break; |
| 10914 | } |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10915 | qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10916 | } |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10917 | }else{ |
| 10918 | TermScan: |
drh | fd7abcd | 2021-09-22 13:43:16 +0000 | [diff] [blame] | 10919 | while( (cin = *zLine++)!=0 ){ |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10920 | if( cin==cWait ){ |
| 10921 | switch( cWait ){ |
| 10922 | case '*': |
| 10923 | if( *zLine != '/' ) |
| 10924 | continue; |
| 10925 | ++zLine; |
| 10926 | cWait = 0; |
| 10927 | qss = QSS_SETV(qss, 0); |
| 10928 | goto PlainScan; |
| 10929 | case '`': case '\'': case '"': |
| 10930 | if(*zLine==cWait){ |
larrybr | 8d463ce | 2021-09-11 02:42:04 +0000 | [diff] [blame] | 10931 | ++zLine; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 10932 | continue; |
| 10933 | } |
| 10934 | /* fall thru */ |
| 10935 | case ']': |
| 10936 | cWait = 0; |
| 10937 | qss = QSS_SETV(qss, 0); |
| 10938 | goto PlainScan; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 10939 | default: assert(0); |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10940 | } |
| 10941 | } |
| 10942 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10943 | } |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10944 | return qss; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10945 | } |
| 10946 | |
| 10947 | /* |
| 10948 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 10949 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 10950 | ** as is the Oracle "/". |
| 10951 | */ |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10952 | static int line_is_command_terminator(char *zLine){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10953 | while( IsSpace(zLine[0]) ){ zLine++; }; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 10954 | if( zLine[0]=='/' ) |
| 10955 | zLine += 1; /* Oracle */ |
| 10956 | else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) |
| 10957 | zLine += 2; /* SQL Server */ |
| 10958 | else |
| 10959 | return 0; |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 10960 | return quickscan(zLine, QSS_Start)==QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10961 | } |
| 10962 | |
| 10963 | /* |
drh | 841c98e | 2022-11-17 01:24:06 +0000 | [diff] [blame] | 10964 | ** The CLI needs a working sqlite3_complete() to work properly. So error |
| 10965 | ** out of the build if compiling with SQLITE_OMIT_COMPLETE. |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 10966 | */ |
| 10967 | #ifdef SQLITE_OMIT_COMPLETE |
drh | 841c98e | 2022-11-17 01:24:06 +0000 | [diff] [blame] | 10968 | # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE. |
drh | 56f1774 | 2018-01-24 01:58:49 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | |
| 10971 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10972 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 10973 | ** ends in the middle of a string literal or C-style comment. |
| 10974 | */ |
| 10975 | static int line_is_complete(char *zSql, int nSql){ |
| 10976 | int rc; |
| 10977 | if( zSql==0 ) return 1; |
| 10978 | zSql[nSql] = ';'; |
| 10979 | zSql[nSql+1] = 0; |
| 10980 | rc = sqlite3_complete(zSql); |
| 10981 | zSql[nSql] = 0; |
| 10982 | return rc; |
| 10983 | } |
| 10984 | |
| 10985 | /* |
drh | fc29a86 | 2018-05-11 19:11:18 +0000 | [diff] [blame] | 10986 | ** Run a single line of SQL. Return the number of errors. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10987 | */ |
| 10988 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 10989 | int rc; |
| 10990 | char *zErrMsg = 0; |
| 10991 | |
| 10992 | open_db(p, 0); |
| 10993 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | fc4eeef | 2019-02-05 19:48:46 +0000 | [diff] [blame] | 10994 | if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10995 | BEGIN_TIMER; |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 10996 | rc = shell_exec(p, zSql, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 10997 | END_TIMER; |
| 10998 | if( rc || zErrMsg ){ |
| 10999 | char zPrefix[100]; |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11000 | const char *zErrorTail; |
| 11001 | const char *zErrorType; |
| 11002 | if( zErrMsg==0 ){ |
| 11003 | zErrorType = "Error"; |
| 11004 | zErrorTail = sqlite3_errmsg(p->db); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11005 | }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){ |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11006 | zErrorType = "Parse error"; |
| 11007 | zErrorTail = &zErrMsg[12]; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11008 | }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){ |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11009 | zErrorType = "Runtime error"; |
| 11010 | zErrorTail = &zErrMsg[10]; |
| 11011 | }else{ |
| 11012 | zErrorType = "Error"; |
| 11013 | zErrorTail = zErrMsg; |
| 11014 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11015 | if( in!=0 || !stdin_is_interactive ){ |
| 11016 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11017 | "%s near line %d:", zErrorType, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11018 | }else{ |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11019 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11020 | } |
drh | 3e46db2 | 2022-02-08 11:52:45 +0000 | [diff] [blame] | 11021 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail); |
| 11022 | sqlite3_free(zErrMsg); |
| 11023 | zErrMsg = 0; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11024 | return 1; |
| 11025 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 6d9f034 | 2021-09-22 10:28:50 +0000 | [diff] [blame] | 11026 | char zLineBuf[2000]; |
| 11027 | sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, |
| 11028 | "changes: %lld total_changes: %lld", |
larrybr | 10496f7 | 2021-06-23 16:07:20 +0000 | [diff] [blame] | 11029 | sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); |
drh | 6d9f034 | 2021-09-22 10:28:50 +0000 | [diff] [blame] | 11030 | raw_printf(p->out, "%s\n", zLineBuf); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11031 | } |
| 11032 | return 0; |
| 11033 | } |
| 11034 | |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11035 | static void echo_group_input(ShellState *p, const char *zDo){ |
| 11036 | if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); |
| 11037 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11038 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11039 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11040 | /* |
| 11041 | ** Alternate one_input_line() impl for wasm mode. This is not in the primary impl |
| 11042 | ** because we need the global shellState and cannot access it from that function |
| 11043 | ** without moving lots of code around (creating a larger/messier diff). |
| 11044 | */ |
| 11045 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
| 11046 | /* Parse the next line from shellState.wasm.zInput. */ |
| 11047 | const char *zBegin = shellState.wasm.zPos; |
| 11048 | const char *z = zBegin; |
| 11049 | char *zLine = 0; |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11050 | i64 nZ = 0; |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11051 | |
| 11052 | UNUSED_PARAMETER(in); |
| 11053 | UNUSED_PARAMETER(isContinuation); |
| 11054 | if(!z || !*z){ |
| 11055 | return 0; |
| 11056 | } |
| 11057 | while(*z && isspace(*z)) ++z; |
| 11058 | zBegin = z; |
| 11059 | for(; *z && '\n'!=*z; ++nZ, ++z){} |
| 11060 | if(nZ>0 && '\r'==zBegin[nZ-1]){ |
| 11061 | --nZ; |
| 11062 | } |
| 11063 | shellState.wasm.zPos = z; |
| 11064 | zLine = realloc(zPrior, nZ+1); |
| 11065 | shell_check_oom(zLine); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11066 | memcpy(zLine, zBegin, nZ); |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11067 | zLine[nZ] = 0; |
| 11068 | return zLine; |
| 11069 | } |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11070 | #endif /* SQLITE_SHELL_FIDDLE */ |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11071 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11072 | /* |
| 11073 | ** Read input from *in and process it. If *in==0 then input |
| 11074 | ** is interactive - the user is typing it it. Otherwise, input |
| 11075 | ** is coming from a file or device. A prompt is issued and history |
| 11076 | ** is saved only if input is interactive. An interrupt signal will |
| 11077 | ** cause this routine to exit immediately, unless input is interactive. |
| 11078 | ** |
| 11079 | ** Return the number of errors. |
| 11080 | */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11081 | static int process_input(ShellState *p){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11082 | char *zLine = 0; /* A single input line */ |
| 11083 | char *zSql = 0; /* Accumulated SQL text */ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11084 | i64 nLine; /* Length of current line */ |
| 11085 | i64 nSql = 0; /* Bytes of zSql[] used */ |
| 11086 | i64 nAlloc = 0; /* Allocated zSql[] space */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11087 | int rc; /* Error code */ |
| 11088 | int errCnt = 0; /* Number of errors seen */ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11089 | i64 startline = 0; /* Line number for start of current input */ |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11090 | QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11091 | |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 11092 | if( p->inputNesting==MAX_INPUT_NESTING ){ |
| 11093 | /* This will be more informative in a later version. */ |
| 11094 | utf8_printf(stderr,"Input nesting limit (%d) reached at line %d." |
| 11095 | " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); |
| 11096 | return 1; |
| 11097 | } |
| 11098 | ++p->inputNesting; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11099 | p->lineno = 0; |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11100 | while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11101 | fflush(p->out); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11102 | zLine = one_input_line(p->in, zLine, nSql>0); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11103 | if( zLine==0 ){ |
| 11104 | /* End of input */ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11105 | if( p->in==0 && stdin_is_interactive ) printf("\n"); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11106 | break; |
| 11107 | } |
| 11108 | if( seenInterrupt ){ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11109 | if( p->in!=0 ) break; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11110 | seenInterrupt = 0; |
| 11111 | } |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11112 | p->lineno++; |
larrybr | 7e00984 | 2021-09-18 21:35:22 +0000 | [diff] [blame] | 11113 | if( QSS_INPLAIN(qss) |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11114 | && line_is_command_terminator(zLine) |
| 11115 | && line_is_complete(zSql, nSql) ){ |
| 11116 | memcpy(zLine,";",2); |
| 11117 | } |
| 11118 | qss = quickscan(zLine, qss); |
| 11119 | if( QSS_PLAINWHITE(qss) && nSql==0 ){ |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11120 | /* Just swallow single-line whitespace */ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11121 | echo_group_input(p, zLine); |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11122 | qss = QSS_Start; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11123 | continue; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11124 | } |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 11125 | if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11126 | echo_group_input(p, zLine); |
drh | 1615c37 | 2018-05-12 23:56:22 +0000 | [diff] [blame] | 11127 | if( zLine[0]=='.' ){ |
| 11128 | rc = do_meta_command(zLine, p); |
| 11129 | if( rc==2 ){ /* exit requested */ |
| 11130 | break; |
| 11131 | }else if( rc ){ |
| 11132 | errCnt++; |
| 11133 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11134 | } |
larrybr | 8101216 | 2021-10-02 15:34:52 +0000 | [diff] [blame] | 11135 | qss = QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11136 | continue; |
| 11137 | } |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11138 | /* No single-line dispositions remain; accumulate line(s). */ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11139 | nLine = strlen(zLine); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11140 | if( nSql+nLine+2>=nAlloc ){ |
larrybr | 31bffb4 | 2021-09-08 21:49:03 +0000 | [diff] [blame] | 11141 | /* Grow buffer by half-again increments when big. */ |
| 11142 | nAlloc = nSql+(nSql>>1)+nLine+100; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11143 | zSql = realloc(zSql, nAlloc); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11144 | shell_check_oom(zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11145 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11146 | if( nSql==0 ){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11147 | i64 i; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11148 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
| 11149 | assert( nAlloc>0 && zSql!=0 ); |
| 11150 | memcpy(zSql, zLine+i, nLine+1-i); |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11151 | startline = p->lineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11152 | nSql = nLine-i; |
| 11153 | }else{ |
| 11154 | zSql[nSql++] = '\n'; |
| 11155 | memcpy(zSql+nSql, zLine, nLine+1); |
| 11156 | nSql += nLine; |
| 11157 | } |
larrybr | a96bbe9 | 2021-09-10 19:45:22 +0000 | [diff] [blame] | 11158 | if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11159 | echo_group_input(p, zSql); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11160 | errCnt += runOneSqlLine(p, zSql, p->in, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11161 | nSql = 0; |
| 11162 | if( p->outCount ){ |
| 11163 | output_reset(p); |
| 11164 | p->outCount = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 11165 | }else{ |
| 11166 | clearTempFile(p); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11167 | } |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11168 | p->bSafeMode = p->bSafeModePersist; |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11169 | qss = QSS_Start; |
larrybr | 8bc4cbc | 2021-09-10 00:58:46 +0000 | [diff] [blame] | 11170 | }else if( nSql && QSS_PLAINWHITE(qss) ){ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11171 | echo_group_input(p, zSql); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11172 | nSql = 0; |
larrybr | d797d6b | 2021-10-03 22:03:59 +0000 | [diff] [blame] | 11173 | qss = QSS_Start; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11174 | } |
| 11175 | } |
larrybr | c6e2f2e | 2022-03-15 17:57:42 +0000 | [diff] [blame] | 11176 | if( nSql ){ |
| 11177 | /* This may be incomplete. Let the SQL parser deal with that. */ |
larrybr | f487481 | 2022-05-11 19:59:31 +0000 | [diff] [blame] | 11178 | echo_group_input(p, zSql); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11179 | errCnt += runOneSqlLine(p, zSql, p->in, startline); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11180 | } |
| 11181 | free(zSql); |
| 11182 | free(zLine); |
larrybr | d48e88e | 2022-01-24 06:36:16 +0000 | [diff] [blame] | 11183 | --p->inputNesting; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11184 | return errCnt>0; |
| 11185 | } |
| 11186 | |
| 11187 | /* |
| 11188 | ** Return a pathname which is the user's home directory. A |
| 11189 | ** 0 return indicates an error of some kind. |
| 11190 | */ |
| 11191 | static char *find_home_dir(int clearFlag){ |
| 11192 | static char *home_dir = NULL; |
| 11193 | if( clearFlag ){ |
| 11194 | free(home_dir); |
| 11195 | home_dir = 0; |
| 11196 | return 0; |
| 11197 | } |
| 11198 | if( home_dir ) return home_dir; |
| 11199 | |
| 11200 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 11201 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 11202 | { |
| 11203 | struct passwd *pwent; |
| 11204 | uid_t uid = getuid(); |
| 11205 | if( (pwent=getpwuid(uid)) != NULL) { |
| 11206 | home_dir = pwent->pw_dir; |
| 11207 | } |
| 11208 | } |
| 11209 | #endif |
| 11210 | |
| 11211 | #if defined(_WIN32_WCE) |
| 11212 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 11213 | */ |
| 11214 | home_dir = "/"; |
| 11215 | #else |
| 11216 | |
| 11217 | #if defined(_WIN32) || defined(WIN32) |
| 11218 | if (!home_dir) { |
| 11219 | home_dir = getenv("USERPROFILE"); |
| 11220 | } |
| 11221 | #endif |
| 11222 | |
| 11223 | if (!home_dir) { |
| 11224 | home_dir = getenv("HOME"); |
| 11225 | } |
| 11226 | |
| 11227 | #if defined(_WIN32) || defined(WIN32) |
| 11228 | if (!home_dir) { |
| 11229 | char *zDrive, *zPath; |
| 11230 | int n; |
| 11231 | zDrive = getenv("HOMEDRIVE"); |
| 11232 | zPath = getenv("HOMEPATH"); |
| 11233 | if( zDrive && zPath ){ |
| 11234 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
| 11235 | home_dir = malloc( n ); |
| 11236 | if( home_dir==0 ) return 0; |
| 11237 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 11238 | return home_dir; |
| 11239 | } |
| 11240 | home_dir = "c:\\"; |
| 11241 | } |
| 11242 | #endif |
| 11243 | |
| 11244 | #endif /* !_WIN32_WCE */ |
| 11245 | |
| 11246 | if( home_dir ){ |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11247 | i64 n = strlen(home_dir) + 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11248 | char *z = malloc( n ); |
| 11249 | if( z ) memcpy(z, home_dir, n); |
| 11250 | home_dir = z; |
| 11251 | } |
| 11252 | |
| 11253 | return home_dir; |
| 11254 | } |
| 11255 | |
| 11256 | /* |
stephan | 6e8a334 | 2022-11-06 13:12:11 +0000 | [diff] [blame] | 11257 | ** On non-Windows platforms, look for $XDG_CONFIG_HOME. |
| 11258 | ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return |
| 11259 | ** the path to it, else return 0. The result is cached for |
| 11260 | ** subsequent calls. |
| 11261 | */ |
| 11262 | static const char *find_xdg_config(void){ |
| 11263 | #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \ |
| 11264 | || defined(__RTP__) || defined(_WRS_KERNEL) |
| 11265 | return 0; |
| 11266 | #else |
| 11267 | static int alreadyTried = 0; |
| 11268 | static char *zConfig = 0; |
| 11269 | const char *zXdgHome; |
| 11270 | |
| 11271 | if( alreadyTried!=0 ){ |
| 11272 | return zConfig; |
| 11273 | } |
| 11274 | alreadyTried = 1; |
| 11275 | zXdgHome = getenv("XDG_CONFIG_HOME"); |
| 11276 | if( zXdgHome==0 ){ |
| 11277 | return 0; |
| 11278 | } |
| 11279 | zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome); |
| 11280 | shell_check_oom(zConfig); |
| 11281 | if( access(zConfig,0)!=0 ){ |
| 11282 | sqlite3_free(zConfig); |
| 11283 | zConfig = 0; |
| 11284 | } |
| 11285 | return zConfig; |
| 11286 | #endif |
| 11287 | } |
| 11288 | |
| 11289 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11290 | ** Read input from the file given by sqliterc_override. Or if that |
stephan | 6e8a334 | 2022-11-06 13:12:11 +0000 | [diff] [blame] | 11291 | ** parameter is NULL, take input from the first of find_xdg_config() |
| 11292 | ** or ~/.sqliterc which is found. |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11293 | ** |
| 11294 | ** Returns the number of errors. |
| 11295 | */ |
| 11296 | static void process_sqliterc( |
| 11297 | ShellState *p, /* Configuration data */ |
| 11298 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 11299 | ){ |
| 11300 | char *home_dir = NULL; |
| 11301 | const char *sqliterc = sqliterc_override; |
| 11302 | char *zBuf = 0; |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11303 | FILE *inSaved = p->in; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11304 | int savedLineno = p->lineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11305 | |
stephan | 6e8a334 | 2022-11-06 13:12:11 +0000 | [diff] [blame] | 11306 | if( sqliterc == NULL ){ |
| 11307 | sqliterc = find_xdg_config(); |
| 11308 | } |
| 11309 | if( sqliterc == NULL ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11310 | home_dir = find_home_dir(0); |
| 11311 | if( home_dir==0 ){ |
| 11312 | raw_printf(stderr, "-- warning: cannot find home directory;" |
| 11313 | " cannot read ~/.sqliterc\n"); |
| 11314 | return; |
| 11315 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11316 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11317 | shell_check_oom(zBuf); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11318 | sqliterc = zBuf; |
| 11319 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11320 | p->in = fopen(sqliterc,"rb"); |
| 11321 | if( p->in ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11322 | if( stdin_is_interactive ){ |
| 11323 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
| 11324 | } |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11325 | if( process_input(p) && bail_on_error ) exit(1); |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11326 | fclose(p->in); |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11327 | }else if( sqliterc_override!=0 ){ |
| 11328 | utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc); |
| 11329 | if( bail_on_error ) exit(1); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11330 | } |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 11331 | p->in = inSaved; |
drh | 2c8ee02 | 2018-12-13 18:59:30 +0000 | [diff] [blame] | 11332 | p->lineno = savedLineno; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11333 | sqlite3_free(zBuf); |
| 11334 | } |
| 11335 | |
| 11336 | /* |
| 11337 | ** Show available command line options |
| 11338 | */ |
| 11339 | static const char zOptions[] = |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11340 | #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | ad7fd5d | 2018-03-05 20:21:50 +0000 | [diff] [blame] | 11341 | " -A ARGS... run \".archive ARGS\" and exit\n" |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11342 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11343 | " -append append the database to the end of the file\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11344 | " -ascii set output mode to 'ascii'\n" |
| 11345 | " -bail stop after hitting an error\n" |
| 11346 | " -batch force batch I/O\n" |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 11347 | " -box set output mode to 'box'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11348 | " -column set output mode to 'column'\n" |
| 11349 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
| 11350 | " -csv set output mode to 'csv'\n" |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11351 | #if !defined(SQLITE_OMIT_DESERIALIZE) |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11352 | " -deserialize open the database using sqlite3_deserialize()\n" |
| 11353 | #endif |
larrybr | 527c39d | 2022-05-10 14:55:45 +0000 | [diff] [blame] | 11354 | " -echo print inputs before execution\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11355 | " -init FILENAME read/process named file\n" |
| 11356 | " -[no]header turn headers on or off\n" |
| 11357 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 11358 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 11359 | #endif |
| 11360 | " -help show this message\n" |
| 11361 | " -html set output mode to HTML\n" |
| 11362 | " -interactive force interactive I/O\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11363 | " -json set output mode to 'json'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11364 | " -line set output mode to 'line'\n" |
| 11365 | " -list set output mode to 'list'\n" |
| 11366 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11367 | " -markdown set output mode to 'markdown'\n" |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11368 | #if !defined(SQLITE_OMIT_DESERIALIZE) |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11369 | " -maxsize N maximum size for a --deserialize database\n" |
| 11370 | #endif |
drh | af48257 | 2019-02-04 19:52:39 +0000 | [diff] [blame] | 11371 | " -memtrace trace all memory allocations and deallocations\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11372 | " -mmap N default mmap size set to N\n" |
| 11373 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 11374 | " -multiplex enable the multiplexor VFS\n" |
| 11375 | #endif |
| 11376 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 11377 | " -nofollow refuse to open symbolic links to database files\n" |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11378 | " -nonce STRING set the safe-mode escape nonce\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11379 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 11380 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 11381 | " -quote set output mode to 'quote'\n" |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 11382 | " -readonly open the database read-only\n" |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11383 | " -safe enable safe-mode\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11384 | " -separator SEP set output column separator. Default: '|'\n" |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11385 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 11386 | " -sorterref SIZE sorter references threshold size\n" |
| 11387 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11388 | " -stats print memory stats before each finalize\n" |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11389 | " -table set output mode to 'table'\n" |
drh | 2fa7818 | 2020-10-31 18:58:37 +0000 | [diff] [blame] | 11390 | " -tabs set output mode to 'tabs'\n" |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11391 | " -version show SQLite version\n" |
| 11392 | " -vfs NAME use NAME as the default VFS\n" |
| 11393 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 11394 | " -vfstrace enable tracing of all VFS calls\n" |
| 11395 | #endif |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11396 | #ifdef SQLITE_HAVE_ZLIB |
| 11397 | " -zip open the file as a ZIP Archive\n" |
| 11398 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11399 | ; |
| 11400 | static void usage(int showDetail){ |
| 11401 | utf8_printf(stderr, |
| 11402 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 11403 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 11404 | "if the file does not previously exist.\n", Argv0); |
| 11405 | if( showDetail ){ |
| 11406 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
| 11407 | }else{ |
| 11408 | raw_printf(stderr, "Use the -help option for additional information\n"); |
| 11409 | } |
| 11410 | exit(1); |
| 11411 | } |
| 11412 | |
| 11413 | /* |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11414 | ** Internal check: Verify that the SQLite is uninitialized. Print a |
| 11415 | ** error message if it is initialized. |
| 11416 | */ |
| 11417 | static void verify_uninitialized(void){ |
| 11418 | if( sqlite3_config(-1)==SQLITE_MISUSE ){ |
drh | 8e02a18 | 2018-05-30 07:24:41 +0000 | [diff] [blame] | 11419 | utf8_printf(stdout, "WARNING: attempt to configure SQLite after" |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11420 | " initialization.\n"); |
| 11421 | } |
| 11422 | } |
| 11423 | |
| 11424 | /* |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11425 | ** Initialize the state information in data |
| 11426 | */ |
| 11427 | static void main_init(ShellState *data) { |
| 11428 | memset(data, 0, sizeof(*data)); |
| 11429 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 11430 | data->autoExplain = 1; |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11431 | data->pAuxDb = &data->aAuxDb[0]; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11432 | memcpy(data->colSeparator,SEP_Column, 2); |
| 11433 | memcpy(data->rowSeparator,SEP_Row, 2); |
| 11434 | data->showHeader = 0; |
| 11435 | data->shellFlgs = SHFLG_Lookaside; |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11436 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11437 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
| 11438 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
| 11439 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 11440 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 11441 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
| 11442 | } |
| 11443 | |
| 11444 | /* |
| 11445 | ** Output text to the console in a font that attracts extra attention. |
| 11446 | */ |
| 11447 | #ifdef _WIN32 |
| 11448 | static void printBold(const char *zText){ |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11449 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11450 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 11451 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 11452 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 11453 | SetConsoleTextAttribute(out, |
| 11454 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 11455 | ); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11456 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11457 | printf("%s", zText); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11458 | #if !SQLITE_OS_WINRT |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11459 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11460 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11461 | } |
| 11462 | #else |
| 11463 | static void printBold(const char *zText){ |
| 11464 | printf("\033[1m%s\033[0m", zText); |
| 11465 | } |
| 11466 | #endif |
| 11467 | |
| 11468 | /* |
| 11469 | ** Get the argument to an --option. Throw an error and die if no argument |
| 11470 | ** is available. |
| 11471 | */ |
| 11472 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 11473 | if( i==argc ){ |
| 11474 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
| 11475 | argv[0], argv[argc-1]); |
| 11476 | exit(1); |
| 11477 | } |
| 11478 | return argv[i]; |
| 11479 | } |
| 11480 | |
| 11481 | #ifndef SQLITE_SHELL_IS_UTF8 |
dan | 39b6bd5 | 2021-03-04 18:31:07 +0000 | [diff] [blame] | 11482 | # if (defined(_WIN32) || defined(WIN32)) \ |
| 11483 | && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11484 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 11485 | # else |
| 11486 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 11487 | # endif |
| 11488 | #endif |
| 11489 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11490 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11491 | # define main fiddle_main |
| 11492 | #endif |
| 11493 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11494 | #if SQLITE_SHELL_IS_UTF8 |
| 11495 | int SQLITE_CDECL main(int argc, char **argv){ |
| 11496 | #else |
| 11497 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 11498 | char **argv; |
| 11499 | #endif |
larrybr | fa5dfa8 | 2022-05-07 03:53:14 +0000 | [diff] [blame] | 11500 | #ifdef SQLITE_DEBUG |
mistachkin | 07fae32 | 2022-07-06 23:50:01 +0000 | [diff] [blame] | 11501 | sqlite3_int64 mem_main_enter = sqlite3_memory_used(); |
larrybr | fa5dfa8 | 2022-05-07 03:53:14 +0000 | [diff] [blame] | 11502 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11503 | char *zErrMsg = 0; |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11504 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11505 | # define data shellState |
| 11506 | #else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11507 | ShellState data; |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11508 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11509 | const char *zInitFile = 0; |
| 11510 | int i; |
| 11511 | int rc = 0; |
| 11512 | int warnInmemoryDb = 0; |
| 11513 | int readStdin = 1; |
| 11514 | int nCmd = 0; |
| 11515 | char **azCmd = 0; |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11516 | const char *zVfs = 0; /* Value of -vfs command-line option */ |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11517 | #if !SQLITE_SHELL_IS_UTF8 |
| 11518 | char **argvToFree = 0; |
| 11519 | int argcToFree = 0; |
| 11520 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11521 | |
| 11522 | setBinaryMode(stdin, 0); |
| 11523 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11524 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11525 | stdin_is_interactive = 0; |
| 11526 | stdout_is_console = 1; |
stephan | 60d9aa7 | 2022-09-24 07:36:45 +0000 | [diff] [blame] | 11527 | data.wasm.zDefaultDbName = "/fiddle.sqlite3"; |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11528 | #else |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11529 | stdin_is_interactive = isatty(0); |
| 11530 | stdout_is_console = isatty(1); |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11531 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11532 | |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11533 | #if !defined(_WIN32_WCE) |
| 11534 | if( getenv("SQLITE_DEBUG_BREAK") ){ |
| 11535 | if( isatty(0) && isatty(2) ){ |
| 11536 | fprintf(stderr, |
| 11537 | "attach debugger to process %d and press any key to continue.\n", |
| 11538 | GETPID()); |
| 11539 | fgetc(stdin); |
| 11540 | }else{ |
| 11541 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11542 | #if SQLITE_OS_WINRT |
| 11543 | __debugbreak(); |
| 11544 | #else |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11545 | DebugBreak(); |
mistachkin | 43e8627 | 2020-04-09 15:31:22 +0000 | [diff] [blame] | 11546 | #endif |
mistachkin | 1e8487d | 2018-07-22 06:25:35 +0000 | [diff] [blame] | 11547 | #elif defined(SIGTRAP) |
| 11548 | raise(SIGTRAP); |
| 11549 | #endif |
| 11550 | } |
| 11551 | } |
| 11552 | #endif |
| 11553 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11554 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11555 | if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11556 | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
| 11557 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 11558 | exit(1); |
| 11559 | } |
| 11560 | #endif |
| 11561 | main_init(&data); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11562 | |
| 11563 | /* On Windows, we must translate command-line arguments into UTF-8. |
| 11564 | ** The SQLite memory allocator subsystem has to be enabled in order to |
| 11565 | ** do this. But we want to run an sqlite3_shutdown() afterwards so that |
| 11566 | ** subsequent sqlite3_config() calls will work. So copy all results into |
| 11567 | ** memory that does not come from the SQLite memory allocator. |
| 11568 | */ |
drh | 4b18c1d | 2018-02-04 20:33:13 +0000 | [diff] [blame] | 11569 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11570 | sqlite3_initialize(); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11571 | argvToFree = malloc(sizeof(argv[0])*argc*2); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11572 | shell_check_oom(argvToFree); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11573 | argcToFree = argc; |
| 11574 | argv = argvToFree + argc; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11575 | for(i=0; i<argc; i++){ |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11576 | char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11577 | i64 n; |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11578 | shell_check_oom(z); |
drh | 7d23d15 | 2022-10-11 12:02:42 +0000 | [diff] [blame] | 11579 | n = strlen(z); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11580 | argv[i] = malloc( n+1 ); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11581 | shell_check_oom(argv[i]); |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11582 | memcpy(argv[i], z, n+1); |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 11583 | argvToFree[i] = argv[i]; |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11584 | sqlite3_free(z); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11585 | } |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11586 | sqlite3_shutdown(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11587 | #endif |
drh | 501ea05 | 2018-02-15 01:03:37 +0000 | [diff] [blame] | 11588 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11589 | assert( argc>=1 && argv && argv[0] ); |
| 11590 | Argv0 = argv[0]; |
| 11591 | |
| 11592 | /* Make sure we have a valid signal handler early, before anything |
| 11593 | ** else is done. |
| 11594 | */ |
| 11595 | #ifdef SIGINT |
| 11596 | signal(SIGINT, interrupt_handler); |
mistachkin | b4bab90 | 2017-10-27 17:09:44 +0000 | [diff] [blame] | 11597 | #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
| 11598 | SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11599 | #endif |
| 11600 | |
| 11601 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 11602 | { |
| 11603 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 11604 | ** of a C-function that will provide the name of the database file. Use |
| 11605 | ** this compile-time option to embed this shell program in larger |
| 11606 | ** applications. */ |
| 11607 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11608 | SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11609 | warnInmemoryDb = 0; |
| 11610 | } |
| 11611 | #endif |
| 11612 | |
| 11613 | /* Do an initial pass through the command-line argument to locate |
| 11614 | ** the name of the database file, the name of the initialization file, |
| 11615 | ** the size of the alternative malloc heap, |
| 11616 | ** and the first command to execute. |
| 11617 | */ |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11618 | verify_uninitialized(); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11619 | for(i=1; i<argc; i++){ |
| 11620 | char *z; |
| 11621 | z = argv[i]; |
| 11622 | if( z[0]!='-' ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11623 | if( data.aAuxDb->zDbFilename==0 ){ |
| 11624 | data.aAuxDb->zDbFilename = z; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11625 | }else{ |
| 11626 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 11627 | ** mean that nothing is read from stdin */ |
| 11628 | readStdin = 0; |
| 11629 | nCmd++; |
| 11630 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
drh | e3e2565 | 2021-12-16 13:29:28 +0000 | [diff] [blame] | 11631 | shell_check_oom(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11632 | azCmd[nCmd-1] = z; |
| 11633 | } |
| 11634 | } |
| 11635 | if( z[1]=='-' ) z++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11636 | if( cli_strcmp(z,"-separator")==0 |
| 11637 | || cli_strcmp(z,"-nullvalue")==0 |
| 11638 | || cli_strcmp(z,"-newline")==0 |
| 11639 | || cli_strcmp(z,"-cmd")==0 |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11640 | ){ |
| 11641 | (void)cmdline_option_value(argc, argv, ++i); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11642 | }else if( cli_strcmp(z,"-init")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11643 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11644 | }else if( cli_strcmp(z,"-batch")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11645 | /* Need to check for batch mode here to so we can avoid printing |
| 11646 | ** informational messages (like from process_sqliterc) before |
| 11647 | ** we do the actual processing of arguments later in a second pass. |
| 11648 | */ |
| 11649 | stdin_is_interactive = 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11650 | }else if( cli_strcmp(z,"-heap")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11651 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 11652 | const char *zSize; |
| 11653 | sqlite3_int64 szHeap; |
| 11654 | |
| 11655 | zSize = cmdline_option_value(argc, argv, ++i); |
| 11656 | szHeap = integerValue(zSize); |
| 11657 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
| 11658 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
| 11659 | #else |
| 11660 | (void)cmdline_option_value(argc, argv, ++i); |
| 11661 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11662 | }else if( cli_strcmp(z,"-pagecache")==0 ){ |
drh | f573b4f | 2020-09-28 13:34:05 +0000 | [diff] [blame] | 11663 | sqlite3_int64 n, sz; |
| 11664 | sz = integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11665 | if( sz>70000 ) sz = 70000; |
| 11666 | if( sz<0 ) sz = 0; |
drh | f573b4f | 2020-09-28 13:34:05 +0000 | [diff] [blame] | 11667 | n = integerValue(cmdline_option_value(argc,argv,++i)); |
| 11668 | if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ |
| 11669 | n = 0xffffffffffffLL/sz; |
| 11670 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11671 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 11672 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
| 11673 | data.shellFlgs |= SHFLG_Pagecache; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11674 | }else if( cli_strcmp(z,"-lookaside")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11675 | int n, sz; |
| 11676 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 11677 | if( sz<0 ) sz = 0; |
| 11678 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 11679 | if( n<0 ) n = 0; |
| 11680 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 11681 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11682 | }else if( cli_strcmp(z,"-threadsafe")==0 ){ |
drh | 9d16fb1 | 2021-08-09 17:45:00 +0000 | [diff] [blame] | 11683 | int n; |
| 11684 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
| 11685 | switch( n ){ |
drh | af6d1af | 2021-08-09 17:37:58 +0000 | [diff] [blame] | 11686 | case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; |
| 11687 | case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; |
| 11688 | default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; |
| 11689 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11690 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11691 | }else if( cli_strcmp(z,"-vfstrace")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11692 | extern int vfstrace_register( |
| 11693 | const char *zTraceName, |
| 11694 | const char *zOldVfsName, |
| 11695 | int (*xOut)(const char*,void*), |
| 11696 | void *pOutArg, |
| 11697 | int makeDefault |
| 11698 | ); |
| 11699 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
| 11700 | #endif |
| 11701 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11702 | }else if( cli_strcmp(z,"-multiplex")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11703 | extern int sqlite3_multiple_initialize(const char*,int); |
| 11704 | sqlite3_multiplex_initialize(0, 1); |
| 11705 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11706 | }else if( cli_strcmp(z,"-mmap")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11707 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 11708 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11709 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11710 | }else if( cli_strcmp(z,"-sorterref")==0 ){ |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11711 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 11712 | sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); |
| 11713 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11714 | }else if( cli_strcmp(z,"-vfs")==0 ){ |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11715 | zVfs = cmdline_option_value(argc, argv, ++i); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11716 | #ifdef SQLITE_HAVE_ZLIB |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11717 | }else if( cli_strcmp(z,"-zip")==0 ){ |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 11718 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 11719 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11720 | }else if( cli_strcmp(z,"-append")==0 ){ |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 11721 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11722 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11723 | }else if( cli_strcmp(z,"-deserialize")==0 ){ |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 11724 | data.openMode = SHELL_OPEN_DESERIALIZE; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11725 | }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11726 | data.szMax = integerValue(argv[++i]); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 11727 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11728 | }else if( cli_strcmp(z,"-readonly")==0 ){ |
drh | ee269a6 | 2018-02-14 23:27:43 +0000 | [diff] [blame] | 11729 | data.openMode = SHELL_OPEN_READONLY; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11730 | }else if( cli_strcmp(z,"-nofollow")==0 ){ |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 11731 | data.openFlags = SQLITE_OPEN_NOFOLLOW; |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11732 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11733 | }else if( cli_strncmp(z, "-A",2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11734 | /* All remaining command-line arguments are passed to the ".archive" |
| 11735 | ** command, so ignore them */ |
| 11736 | break; |
| 11737 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11738 | }else if( cli_strcmp(z, "-memtrace")==0 ){ |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 11739 | sqlite3MemTraceActivate(stderr); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11740 | }else if( cli_strcmp(z,"-bail")==0 ){ |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11741 | bail_on_error = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11742 | }else if( cli_strcmp(z,"-nonce")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11743 | free(data.zNonce); |
| 11744 | data.zNonce = strdup(argv[++i]); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11745 | }else if( cli_strcmp(z,"-safe")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11746 | /* no-op - catch this on the second pass */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11747 | } |
| 11748 | } |
drh | e7df892 | 2018-04-18 10:44:58 +0000 | [diff] [blame] | 11749 | verify_uninitialized(); |
| 11750 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11751 | |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 11752 | #ifdef SQLITE_SHELL_INIT_PROC |
| 11753 | { |
| 11754 | /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name |
| 11755 | ** of a C-function that will perform initialization actions on SQLite that |
| 11756 | ** occur just before or after sqlite3_initialize(). Use this compile-time |
| 11757 | ** option to embed this shell program in larger applications. */ |
| 11758 | extern void SQLITE_SHELL_INIT_PROC(void); |
| 11759 | SQLITE_SHELL_INIT_PROC(); |
| 11760 | } |
| 11761 | #else |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11762 | /* All the sqlite3_config() calls have now been made. So it is safe |
| 11763 | ** to call sqlite3_initialize() and process any command line -vfs option. */ |
| 11764 | sqlite3_initialize(); |
drh | d11b8f6 | 2018-04-25 13:27:07 +0000 | [diff] [blame] | 11765 | #endif |
| 11766 | |
dan | 16a4742 | 2018-04-18 09:16:11 +0000 | [diff] [blame] | 11767 | if( zVfs ){ |
| 11768 | sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); |
| 11769 | if( pVfs ){ |
| 11770 | sqlite3_vfs_register(pVfs, 1); |
| 11771 | }else{ |
| 11772 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
| 11773 | exit(1); |
| 11774 | } |
| 11775 | } |
| 11776 | |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11777 | if( data.pAuxDb->zDbFilename==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11778 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11779 | data.pAuxDb->zDbFilename = ":memory:"; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11780 | warnInmemoryDb = argc==1; |
| 11781 | #else |
| 11782 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
| 11783 | return 1; |
| 11784 | #endif |
| 11785 | } |
| 11786 | data.out = stdout; |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 11787 | #ifndef SQLITE_SHELL_FIDDLE |
drh | 8682e12 | 2018-01-07 20:38:10 +0000 | [diff] [blame] | 11788 | sqlite3_appendvfs_init(0,0,0); |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 11789 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11790 | |
| 11791 | /* Go ahead and open the database file if it already exists. If the |
| 11792 | ** file does not exist, delay opening it. This prevents empty database |
| 11793 | ** files from being created if a user mistypes the database name argument |
| 11794 | ** to the sqlite command-line tool. |
| 11795 | */ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 11796 | if( access(data.pAuxDb->zDbFilename, 0)==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11797 | open_db(&data, 0); |
| 11798 | } |
| 11799 | |
| 11800 | /* Process the initialization file if there is one. If no -init option |
| 11801 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 11802 | ** try to process it. |
| 11803 | */ |
| 11804 | process_sqliterc(&data,zInitFile); |
| 11805 | |
| 11806 | /* Make a second pass through the command-line argument and set |
| 11807 | ** options. This second pass is delayed until after the initialization |
| 11808 | ** file is processed so that the command-line arguments will override |
| 11809 | ** settings in the initialization file. |
| 11810 | */ |
| 11811 | for(i=1; i<argc; i++){ |
| 11812 | char *z = argv[i]; |
| 11813 | if( z[0]!='-' ) continue; |
| 11814 | if( z[1]=='-' ){ z++; } |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11815 | if( cli_strcmp(z,"-init")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11816 | i++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11817 | }else if( cli_strcmp(z,"-html")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11818 | data.mode = MODE_Html; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11819 | }else if( cli_strcmp(z,"-list")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11820 | data.mode = MODE_List; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11821 | }else if( cli_strcmp(z,"-quote")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11822 | data.mode = MODE_Quote; |
drh | 9191c70 | 2020-08-17 09:11:21 +0000 | [diff] [blame] | 11823 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); |
| 11824 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11825 | }else if( cli_strcmp(z,"-line")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11826 | data.mode = MODE_Line; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11827 | }else if( cli_strcmp(z,"-column")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11828 | data.mode = MODE_Column; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11829 | }else if( cli_strcmp(z,"-json")==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11830 | data.mode = MODE_Json; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11831 | }else if( cli_strcmp(z,"-markdown")==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11832 | data.mode = MODE_Markdown; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11833 | }else if( cli_strcmp(z,"-table")==0 ){ |
drh | 30c54a0 | 2020-05-28 23:49:50 +0000 | [diff] [blame] | 11834 | data.mode = MODE_Table; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11835 | }else if( cli_strcmp(z,"-box")==0 ){ |
drh | 0908e38 | 2020-06-04 18:05:39 +0000 | [diff] [blame] | 11836 | data.mode = MODE_Box; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11837 | }else if( cli_strcmp(z,"-csv")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11838 | data.mode = MODE_Csv; |
| 11839 | memcpy(data.colSeparator,",",2); |
drh | 3baed31 | 2018-03-08 18:14:41 +0000 | [diff] [blame] | 11840 | #ifdef SQLITE_HAVE_ZLIB |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11841 | }else if( cli_strcmp(z,"-zip")==0 ){ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 11842 | data.openMode = SHELL_OPEN_ZIPFILE; |
| 11843 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11844 | }else if( cli_strcmp(z,"-append")==0 ){ |
drh | 1fa6d9f | 2018-01-06 21:46:01 +0000 | [diff] [blame] | 11845 | data.openMode = SHELL_OPEN_APPENDVFS; |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 11846 | #ifndef SQLITE_OMIT_DESERIALIZE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11847 | }else if( cli_strcmp(z,"-deserialize")==0 ){ |
drh | 60f34ae | 2018-10-30 13:19:49 +0000 | [diff] [blame] | 11848 | data.openMode = SHELL_OPEN_DESERIALIZE; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11849 | }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ |
drh | 6ca6448 | 2019-01-22 16:06:20 +0000 | [diff] [blame] | 11850 | data.szMax = integerValue(argv[++i]); |
drh | a751f39 | 2018-10-30 15:31:22 +0000 | [diff] [blame] | 11851 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11852 | }else if( cli_strcmp(z,"-readonly")==0 ){ |
drh | 4aafe59 | 2018-03-23 16:08:30 +0000 | [diff] [blame] | 11853 | data.openMode = SHELL_OPEN_READONLY; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11854 | }else if( cli_strcmp(z,"-nofollow")==0 ){ |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 11855 | data.openFlags |= SQLITE_OPEN_NOFOLLOW; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11856 | }else if( cli_strcmp(z,"-ascii")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11857 | data.mode = MODE_Ascii; |
drh | 2fa7818 | 2020-10-31 18:58:37 +0000 | [diff] [blame] | 11858 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit); |
| 11859 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11860 | }else if( cli_strcmp(z,"-tabs")==0 ){ |
drh | 2fa7818 | 2020-10-31 18:58:37 +0000 | [diff] [blame] | 11861 | data.mode = MODE_List; |
| 11862 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab); |
| 11863 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11864 | }else if( cli_strcmp(z,"-separator")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11865 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| 11866 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11867 | }else if( cli_strcmp(z,"-newline")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11868 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| 11869 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11870 | }else if( cli_strcmp(z,"-nullvalue")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11871 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
| 11872 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11873 | }else if( cli_strcmp(z,"-header")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11874 | data.showHeader = 1; |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 11875 | ShellSetFlag(&data, SHFLG_HeaderSet); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11876 | }else if( cli_strcmp(z,"-noheader")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11877 | data.showHeader = 0; |
larrybr | ae50912 | 2021-09-10 01:45:20 +0000 | [diff] [blame] | 11878 | ShellSetFlag(&data, SHFLG_HeaderSet); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11879 | }else if( cli_strcmp(z,"-echo")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11880 | ShellSetFlag(&data, SHFLG_Echo); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11881 | }else if( cli_strcmp(z,"-eqp")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 11882 | data.autoEQP = AUTOEQP_on; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11883 | }else if( cli_strcmp(z,"-eqpfull")==0 ){ |
drh | ada7045 | 2017-12-21 21:02:27 +0000 | [diff] [blame] | 11884 | data.autoEQP = AUTOEQP_full; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11885 | }else if( cli_strcmp(z,"-stats")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11886 | data.statsOn = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11887 | }else if( cli_strcmp(z,"-scanstats")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11888 | data.scanstatsOn = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11889 | }else if( cli_strcmp(z,"-backslash")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11890 | /* Undocumented command-line option: -backslash |
| 11891 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 11892 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 11893 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 11894 | */ |
| 11895 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11896 | }else if( cli_strcmp(z,"-bail")==0 ){ |
drh | b7c46aa | 2020-11-25 13:59:47 +0000 | [diff] [blame] | 11897 | /* No-op. The bail_on_error flag should already be set. */ |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11898 | }else if( cli_strcmp(z,"-version")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11899 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 11900 | return 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11901 | }else if( cli_strcmp(z,"-interactive")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11902 | stdin_is_interactive = 1; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11903 | }else if( cli_strcmp(z,"-batch")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11904 | stdin_is_interactive = 0; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11905 | }else if( cli_strcmp(z,"-heap")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11906 | i++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11907 | }else if( cli_strcmp(z,"-pagecache")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11908 | i+=2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11909 | }else if( cli_strcmp(z,"-lookaside")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11910 | i+=2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11911 | }else if( cli_strcmp(z,"-threadsafe")==0 ){ |
drh | af6d1af | 2021-08-09 17:37:58 +0000 | [diff] [blame] | 11912 | i+=2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11913 | }else if( cli_strcmp(z,"-nonce")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11914 | i += 2; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11915 | }else if( cli_strcmp(z,"-mmap")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11916 | i++; |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11917 | }else if( cli_strcmp(z,"-memtrace")==0 ){ |
drh | 50b910a | 2019-01-21 14:55:03 +0000 | [diff] [blame] | 11918 | i++; |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11919 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11920 | }else if( cli_strcmp(z,"-sorterref")==0 ){ |
drh | a90d84f | 2018-04-18 15:21:13 +0000 | [diff] [blame] | 11921 | i++; |
| 11922 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11923 | }else if( cli_strcmp(z,"-vfs")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11924 | i++; |
| 11925 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11926 | }else if( cli_strcmp(z,"-vfstrace")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11927 | i++; |
| 11928 | #endif |
| 11929 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11930 | }else if( cli_strcmp(z,"-multiplex")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11931 | i++; |
| 11932 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11933 | }else if( cli_strcmp(z,"-help")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11934 | usage(1); |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11935 | }else if( cli_strcmp(z,"-cmd")==0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11936 | /* Run commands that follow -cmd first and separately from commands |
| 11937 | ** that simply appear on the command-line. This seems goofy. It would |
| 11938 | ** be better if all commands ran in the order that they appear. But |
| 11939 | ** we retain the goofy behavior for historical compatibility. */ |
| 11940 | if( i==argc-1 ) break; |
| 11941 | z = cmdline_option_value(argc,argv,++i); |
| 11942 | if( z[0]=='.' ){ |
| 11943 | rc = do_meta_command(z, &data); |
| 11944 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
| 11945 | }else{ |
| 11946 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 11947 | rc = shell_exec(&data, z, &zErrMsg); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11948 | if( zErrMsg!=0 ){ |
| 11949 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 11950 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 11951 | }else if( rc!=0 ){ |
| 11952 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
| 11953 | if( bail_on_error ) return rc; |
| 11954 | } |
| 11955 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11956 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11957 | }else if( cli_strncmp(z, "-A", 2)==0 ){ |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11958 | if( nCmd>0 ){ |
| 11959 | utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" |
| 11960 | " with \"%s\"\n", z); |
| 11961 | return 1; |
| 11962 | } |
drh | be4ccb2 | 2018-05-17 20:04:24 +0000 | [diff] [blame] | 11963 | open_db(&data, OPEN_DB_ZIPFILE); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 11964 | if( z[2] ){ |
| 11965 | argv[i] = &z[2]; |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 11966 | arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 11967 | }else{ |
drh | d0f9cdc | 2018-05-17 14:09:06 +0000 | [diff] [blame] | 11968 | arDotCommand(&data, 1, argv+i, argc-i); |
drh | 93b7731 | 2018-03-05 20:20:22 +0000 | [diff] [blame] | 11969 | } |
drh | da57d96 | 2018-03-05 19:34:05 +0000 | [diff] [blame] | 11970 | readStdin = 0; |
| 11971 | break; |
| 11972 | #endif |
drh | bf70f1b | 2022-10-19 18:04:42 +0000 | [diff] [blame] | 11973 | }else if( cli_strcmp(z,"-safe")==0 ){ |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 11974 | data.bSafeMode = data.bSafeModePersist = 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11975 | }else{ |
| 11976 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 11977 | raw_printf(stderr,"Use -help for a list of options.\n"); |
| 11978 | return 1; |
| 11979 | } |
| 11980 | data.cMode = data.mode; |
| 11981 | } |
| 11982 | |
| 11983 | if( !readStdin ){ |
| 11984 | /* Run all arguments that do not begin with '-' as if they were separate |
| 11985 | ** command-line inputs, except for the argToSkip argument which contains |
| 11986 | ** the database filename. |
| 11987 | */ |
| 11988 | for(i=0; i<nCmd; i++){ |
| 11989 | if( azCmd[i][0]=='.' ){ |
| 11990 | rc = do_meta_command(azCmd[i], &data); |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 11991 | if( rc ){ |
| 11992 | free(azCmd); |
| 11993 | return rc==2 ? 0 : rc; |
| 11994 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 11995 | }else{ |
| 11996 | open_db(&data, 0); |
drh | a10b999 | 2018-03-09 15:24:33 +0000 | [diff] [blame] | 11997 | rc = shell_exec(&data, azCmd[i], &zErrMsg); |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 11998 | if( zErrMsg || rc ){ |
| 11999 | if( zErrMsg!=0 ){ |
| 12000 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
| 12001 | }else{ |
| 12002 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| 12003 | } |
| 12004 | sqlite3_free(zErrMsg); |
| 12005 | free(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12006 | return rc!=0 ? rc : 1; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12007 | } |
| 12008 | } |
| 12009 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12010 | }else{ |
| 12011 | /* Run commands received from standard input |
| 12012 | */ |
| 12013 | if( stdin_is_interactive ){ |
| 12014 | char *zHome; |
drh | a9e4be3 | 2018-10-10 18:56:40 +0000 | [diff] [blame] | 12015 | char *zHistory; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12016 | int nHistory; |
| 12017 | printf( |
| 12018 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
| 12019 | "Enter \".help\" for usage hints.\n", |
| 12020 | sqlite3_libversion(), sqlite3_sourceid() |
| 12021 | ); |
| 12022 | if( warnInmemoryDb ){ |
| 12023 | printf("Connected to a "); |
| 12024 | printBold("transient in-memory database"); |
| 12025 | printf(".\nUse \".open FILENAME\" to reopen on a " |
| 12026 | "persistent database.\n"); |
| 12027 | } |
drh | a9e4be3 | 2018-10-10 18:56:40 +0000 | [diff] [blame] | 12028 | zHistory = getenv("SQLITE_HISTORY"); |
| 12029 | if( zHistory ){ |
| 12030 | zHistory = strdup(zHistory); |
| 12031 | }else if( (zHome = find_home_dir(0))!=0 ){ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12032 | nHistory = strlen30(zHome) + 20; |
| 12033 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 12034 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 12035 | } |
| 12036 | } |
| 12037 | if( zHistory ){ shell_read_history(zHistory); } |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 12038 | #if HAVE_READLINE || HAVE_EDITLINE |
| 12039 | rl_attempted_completion_function = readline_completion; |
| 12040 | #elif HAVE_LINENOISE |
| 12041 | linenoiseSetCompletionCallback(linenoise_completion); |
| 12042 | #endif |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 12043 | data.in = 0; |
| 12044 | rc = process_input(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12045 | if( zHistory ){ |
drh | 5a75dd8 | 2017-07-18 20:59:40 +0000 | [diff] [blame] | 12046 | shell_stifle_history(2000); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12047 | shell_write_history(zHistory); |
| 12048 | free(zHistory); |
| 12049 | } |
| 12050 | }else{ |
drh | 60379d4 | 2018-12-13 18:30:01 +0000 | [diff] [blame] | 12051 | data.in = stdin; |
| 12052 | rc = process_input(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12053 | } |
| 12054 | } |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 12055 | #ifndef SQLITE_SHELL_FIDDLE |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12056 | /* In WASM mode we have to leave the db state in place so that |
| 12057 | ** client code can "push" SQL into it after this call returns. */ |
dan | aff1a57 | 2020-11-17 21:09:56 +0000 | [diff] [blame] | 12058 | free(azCmd); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12059 | set_table_name(&data, 0); |
| 12060 | if( data.db ){ |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12061 | session_close_all(&data, -1); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 12062 | close_db(data.db); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12063 | } |
drh | 3740712 | 2021-07-23 18:43:58 +0000 | [diff] [blame] | 12064 | for(i=0; i<ArraySize(data.aAuxDb); i++){ |
| 12065 | sqlite3_free(data.aAuxDb[i].zFreeOnClose); |
| 12066 | if( data.aAuxDb[i].db ){ |
| 12067 | session_close_all(&data, i); |
| 12068 | close_db(data.aAuxDb[i].db); |
| 12069 | } |
| 12070 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12071 | find_home_dir(1); |
drh | 536c345 | 2018-01-11 00:38:39 +0000 | [diff] [blame] | 12072 | output_reset(&data); |
| 12073 | data.doXdgOpen = 0; |
drh | 13c2093 | 2018-01-10 21:41:55 +0000 | [diff] [blame] | 12074 | clearTempFile(&data); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12075 | #if !SQLITE_SHELL_IS_UTF8 |
drh | 1f22f62 | 2018-05-17 13:29:14 +0000 | [diff] [blame] | 12076 | for(i=0; i<argcToFree; i++) free(argvToFree[i]); |
| 12077 | free(argvToFree); |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12078 | #endif |
drh | 0285d98 | 2020-05-29 14:38:43 +0000 | [diff] [blame] | 12079 | free(data.colWidth); |
drh | b97e2ad | 2021-08-26 18:31:39 +0000 | [diff] [blame] | 12080 | free(data.zNonce); |
drh | 9e80403 | 2018-05-18 17:11:50 +0000 | [diff] [blame] | 12081 | /* Clear the global data structure so that valgrind will detect memory |
| 12082 | ** leaks */ |
| 12083 | memset(&data, 0, sizeof(data)); |
larrybr | fa5dfa8 | 2022-05-07 03:53:14 +0000 | [diff] [blame] | 12084 | #ifdef SQLITE_DEBUG |
| 12085 | if( sqlite3_memory_used()>mem_main_enter ){ |
| 12086 | utf8_printf(stderr, "Memory leaked: %u bytes\n", |
| 12087 | (unsigned int)(sqlite3_memory_used()-mem_main_enter)); |
| 12088 | } |
| 12089 | #endif |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 12090 | #endif /* !SQLITE_SHELL_FIDDLE */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 12091 | return rc; |
| 12092 | } |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12093 | |
| 12094 | |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 12095 | #ifdef SQLITE_SHELL_FIDDLE |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12096 | /* Only for emcc experimentation purposes. */ |
| 12097 | int fiddle_experiment(int a,int b){ |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12098 | return a + b; |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12099 | } |
| 12100 | |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12101 | /* |
| 12102 | ** Returns a pointer to the current DB handle. |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12103 | */ |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12104 | sqlite3 * fiddle_db_handle(){ |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12105 | return globalDb; |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12106 | } |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12107 | |
| 12108 | /* |
| 12109 | ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then |
| 12110 | ** "main" is assumed. Returns 0 if no db with the given name is |
| 12111 | ** open. |
| 12112 | */ |
| 12113 | sqlite3_vfs * fiddle_db_vfs(const char *zDbName){ |
| 12114 | sqlite3_vfs * pVfs = 0; |
| 12115 | if(globalDb){ |
| 12116 | sqlite3_file_control(globalDb, zDbName ? zDbName : "main", |
| 12117 | SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 12118 | } |
| 12119 | return pVfs; |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12120 | } |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12121 | |
stephan | 2eb4541 | 2022-05-21 00:01:45 +0000 | [diff] [blame] | 12122 | /* Only for emcc experimentation purposes. */ |
| 12123 | sqlite3 * fiddle_db_arg(sqlite3 *arg){ |
| 12124 | printf("fiddle_db_arg(%p)\n", (const void*)arg); |
| 12125 | return arg; |
| 12126 | } |
| 12127 | |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12128 | /* |
stephan | 4257373 | 2022-05-21 14:19:05 +0000 | [diff] [blame] | 12129 | ** Intended to be called via a SharedWorker() while a separate |
| 12130 | ** SharedWorker() (which manages the wasm module) is performing work |
stephan | 085c5c6 | 2022-05-25 04:35:22 +0000 | [diff] [blame] | 12131 | ** which should be interrupted. Unfortunately, SharedWorker is not |
| 12132 | ** portable enough to make real use of. |
stephan | 4257373 | 2022-05-21 14:19:05 +0000 | [diff] [blame] | 12133 | */ |
| 12134 | void fiddle_interrupt(void){ |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12135 | if( globalDb ) sqlite3_interrupt(globalDb); |
stephan | 4257373 | 2022-05-21 14:19:05 +0000 | [diff] [blame] | 12136 | } |
| 12137 | |
| 12138 | /* |
stephan | 085c5c6 | 2022-05-25 04:35:22 +0000 | [diff] [blame] | 12139 | ** Returns the filename of the given db name, assuming "main" if |
| 12140 | ** zDbName is NULL. Returns NULL if globalDb is not opened. |
stephan | 80bf869 | 2022-05-24 22:16:12 +0000 | [diff] [blame] | 12141 | */ |
| 12142 | const char * fiddle_db_filename(const char * zDbName){ |
| 12143 | return globalDb |
| 12144 | ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") |
| 12145 | : NULL; |
| 12146 | } |
| 12147 | |
| 12148 | /* |
stephan | a4c357f | 2022-10-03 11:42:45 +0000 | [diff] [blame] | 12149 | ** Completely wipes out the contents of the currently-opened database |
| 12150 | ** but leaves its storage intact for reuse. |
stephan | cd69784 | 2022-05-27 03:27:10 +0000 | [diff] [blame] | 12151 | */ |
| 12152 | void fiddle_reset_db(void){ |
stephan | a4c357f | 2022-10-03 11:42:45 +0000 | [diff] [blame] | 12153 | if( globalDb ){ |
| 12154 | int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); |
| 12155 | if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0); |
| 12156 | sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); |
stephan | cd69784 | 2022-05-27 03:27:10 +0000 | [diff] [blame] | 12157 | } |
stephan | cd69784 | 2022-05-27 03:27:10 +0000 | [diff] [blame] | 12158 | } |
| 12159 | |
| 12160 | /* |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12161 | ** Uses the current database's VFS xRead to stream the db file's |
| 12162 | ** contents out to the given callback. The callback gets a single |
| 12163 | ** chunk of size n (its 2nd argument) on each call and must return 0 |
| 12164 | ** on success, non-0 on error. This function returns 0 on success, |
| 12165 | ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 |
| 12166 | ** code from the callback. Note that this is not thread-friendly: it |
| 12167 | ** expects that it will be the only thread reading the db file and |
| 12168 | ** takes no measures to ensure that is the case. |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12169 | */ |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12170 | int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){ |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12171 | sqlite3_int64 nSize = 0; |
| 12172 | sqlite3_int64 nPos = 0; |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12173 | sqlite3_file * pFile = 0; |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12174 | unsigned char buf[1024 * 8]; |
| 12175 | int nBuf = (int)sizeof(buf); |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12176 | int rc = shellState.db |
| 12177 | ? sqlite3_file_control(shellState.db, "main", |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12178 | SQLITE_FCNTL_FILE_POINTER, &pFile) |
| 12179 | : SQLITE_NOTFOUND; |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12180 | if( rc ) return rc; |
| 12181 | rc = pFile->pMethods->xFileSize(pFile, &nSize); |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12182 | if( rc ) return rc; |
| 12183 | if(nSize % nBuf){ |
| 12184 | /* DB size is not an even multiple of the buffer size. Reduce |
| 12185 | ** buffer size so that we do not unduly inflate the db size when |
| 12186 | ** exporting. */ |
| 12187 | if(0 == nSize % 4096) nBuf = 4096; |
| 12188 | else if(0 == nSize % 2048) nBuf = 2048; |
| 12189 | else if(0 == nSize % 1024) nBuf = 1024; |
| 12190 | else nBuf = 512; |
| 12191 | } |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12192 | for( ; 0==rc && nPos<nSize; nPos += nBuf ){ |
| 12193 | rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); |
| 12194 | if(SQLITE_IOERR_SHORT_READ == rc){ |
| 12195 | rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; |
| 12196 | } |
stephan | 278d3fa | 2022-09-26 13:55:10 +0000 | [diff] [blame] | 12197 | if( 0==rc ) rc = xCallback(buf, nBuf); |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12198 | } |
| 12199 | return rc; |
| 12200 | } |
stephan | 1f095d4 | 2022-09-26 11:38:58 +0000 | [diff] [blame] | 12201 | |
| 12202 | /* |
stephan | 60d9aa7 | 2022-09-24 07:36:45 +0000 | [diff] [blame] | 12203 | ** Trivial exportable function for emscripten. It processes zSql as if |
| 12204 | ** it were input to the sqlite3 shell and redirects all output to the |
stephan | 842c5ee | 2022-10-20 05:14:37 +0000 | [diff] [blame] | 12205 | ** wasm binding. fiddle_main() must have been called before this |
| 12206 | ** is called, or results are undefined. |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12207 | */ |
stephan | 0076e49 | 2022-05-18 18:10:17 +0000 | [diff] [blame] | 12208 | void fiddle_exec(const char * zSql){ |
stephan | 60d9aa7 | 2022-09-24 07:36:45 +0000 | [diff] [blame] | 12209 | if(zSql && *zSql){ |
| 12210 | if('.'==*zSql) puts(zSql); |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12211 | shellState.wasm.zInput = zSql; |
| 12212 | shellState.wasm.zPos = zSql; |
| 12213 | process_input(&shellState); |
stephan | 60d9aa7 | 2022-09-24 07:36:45 +0000 | [diff] [blame] | 12214 | shellState.wasm.zInput = shellState.wasm.zPos = 0; |
stephan | f8cd3d2 | 2022-05-18 17:14:24 +0000 | [diff] [blame] | 12215 | } |
| 12216 | } |
stephan | 4413ec7 | 2022-07-12 15:53:02 +0000 | [diff] [blame] | 12217 | #endif /* SQLITE_SHELL_FIDDLE */ |