drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code to implement the "sqlite" command line |
| 13 | ** utility for accessing SQLite databases. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 14 | */ |
mistachkin | a3b2ff5 | 2011-09-16 20:16:36 +0000 | [diff] [blame] | 15 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 16 | /* This needs to come before any includes for MSVC compiler */ |
| 17 | #define _CRT_SECURE_NO_WARNINGS |
| 18 | #endif |
| 19 | |
drh | 36f7dd3 | 2011-10-13 16:02:17 +0000 | [diff] [blame] | 20 | /* |
drh | ce13b99 | 2017-05-23 20:00:00 +0000 | [diff] [blame] | 21 | ** Warning pragmas copied from msvc.h in the core. |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 22 | */ |
drh | ce13b99 | 2017-05-23 20:00:00 +0000 | [diff] [blame] | 23 | #if defined(_MSC_VER) |
| 24 | #pragma warning(disable : 4054) |
| 25 | #pragma warning(disable : 4055) |
| 26 | #pragma warning(disable : 4100) |
| 27 | #pragma warning(disable : 4127) |
| 28 | #pragma warning(disable : 4130) |
| 29 | #pragma warning(disable : 4152) |
| 30 | #pragma warning(disable : 4189) |
| 31 | #pragma warning(disable : 4206) |
| 32 | #pragma warning(disable : 4210) |
| 33 | #pragma warning(disable : 4232) |
| 34 | #pragma warning(disable : 4244) |
| 35 | #pragma warning(disable : 4305) |
| 36 | #pragma warning(disable : 4306) |
| 37 | #pragma warning(disable : 4702) |
| 38 | #pragma warning(disable : 4706) |
| 39 | #endif /* defined(_MSC_VER) */ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 42 | ** No support for loadable extensions in VxWorks. |
| 43 | */ |
drh | ada3f2b | 2015-03-23 21:32:50 +0000 | [diff] [blame] | 44 | #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 45 | # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 46 | #endif |
| 47 | |
| 48 | /* |
drh | 36f7dd3 | 2011-10-13 16:02:17 +0000 | [diff] [blame] | 49 | ** Enable large-file support for fopen() and friends on unix. |
| 50 | */ |
| 51 | #ifndef SQLITE_DISABLE_LFS |
| 52 | # define _LARGE_FILE 1 |
| 53 | # ifndef _FILE_OFFSET_BITS |
| 54 | # define _FILE_OFFSET_BITS 64 |
| 55 | # endif |
| 56 | # define _LARGEFILE_SOURCE 1 |
| 57 | #endif |
| 58 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
| 60 | #include <string.h> |
| 61 | #include <stdio.h> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 62 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 63 | #include "sqlite3.h" |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 64 | #if SQLITE_USER_AUTHENTICATION |
| 65 | # include "sqlite3userauth.h" |
| 66 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 67 | #include <ctype.h> |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 68 | #include <stdarg.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 69 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 70 | #if !defined(_WIN32) && !defined(WIN32) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 71 | # include <signal.h> |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 72 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 73 | # include <pwd.h> |
| 74 | # endif |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 75 | # include <unistd.h> |
| 76 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 77 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 78 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 79 | #if HAVE_READLINE |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 80 | # include <readline/readline.h> |
| 81 | # include <readline/history.h> |
drh | 81d7fd1 | 2010-12-08 00:02:26 +0000 | [diff] [blame] | 82 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 83 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 84 | #if HAVE_EDITLINE |
drh | aaa21b4 | 2014-02-11 14:37:51 +0000 | [diff] [blame] | 85 | # include <editline/readline.h> |
| 86 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 87 | |
| 88 | #if HAVE_EDITLINE || HAVE_READLINE |
| 89 | |
| 90 | # define shell_add_history(X) add_history(X) |
| 91 | # define shell_read_history(X) read_history(X) |
| 92 | # define shell_write_history(X) write_history(X) |
| 93 | # define shell_stifle_history(X) stifle_history(X) |
| 94 | # define shell_readline(X) readline(X) |
| 95 | |
| 96 | #elif HAVE_LINENOISE |
| 97 | |
| 98 | # include "linenoise.h" |
| 99 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 100 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 101 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 102 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 103 | # define shell_readline(X) linenoise(X) |
| 104 | |
| 105 | #else |
| 106 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 107 | # define shell_read_history(X) |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 108 | # define shell_write_history(X) |
| 109 | # define shell_stifle_history(X) |
| 110 | |
| 111 | # define SHELL_USE_LOCAL_GETLINE 1 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 114 | |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 115 | #if defined(_WIN32) || defined(WIN32) |
| 116 | # include <io.h> |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 117 | # include <fcntl.h> |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 118 | # define isatty(h) _isatty(h) |
| 119 | # ifndef access |
| 120 | # define access(f,m) _access((f),(m)) |
| 121 | # endif |
| 122 | # undef popen |
| 123 | # define popen _popen |
| 124 | # undef pclose |
| 125 | # define pclose _pclose |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 126 | #else |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 127 | /* Make sure isatty() has a prototype. */ |
| 128 | extern int isatty(int); |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 129 | |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 130 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 131 | /* popen and pclose are not C89 functions and so are |
| 132 | ** sometimes omitted from the <stdio.h> header */ |
| 133 | extern FILE *popen(const char*,const char*); |
| 134 | extern int pclose(FILE*); |
| 135 | # else |
| 136 | # define SQLITE_OMIT_POPEN 1 |
| 137 | # endif |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 138 | #endif |
drh | 53371f9 | 2013-07-25 17:07:03 +0000 | [diff] [blame] | 139 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 140 | #if defined(_WIN32_WCE) |
| 141 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 142 | * thus we always assume that we have a console. That can be |
| 143 | * overridden with the -batch command line option. |
| 144 | */ |
| 145 | #define isatty(x) 1 |
| 146 | #endif |
| 147 | |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 148 | /* ctype macros that work with signed characters */ |
| 149 | #define IsSpace(X) isspace((unsigned char)X) |
| 150 | #define IsDigit(X) isdigit((unsigned char)X) |
| 151 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 152 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 153 | #if defined(_WIN32) || defined(WIN32) |
| 154 | #include <windows.h> |
| 155 | |
| 156 | /* string conversion routines only needed on Win32 */ |
| 157 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 158 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 159 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 160 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 161 | #endif |
| 162 | |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 163 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 164 | ** are automatically translated into \r\n. However, this behavior needs |
| 165 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 166 | ** rendering quoted strings that contain \n characters). The following |
| 167 | ** routines take care of that. |
| 168 | */ |
| 169 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 170 | static void setBinaryMode(FILE *file, int isOutput){ |
| 171 | if( isOutput ) fflush(file); |
| 172 | _setmode(_fileno(file), _O_BINARY); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 173 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 174 | static void setTextMode(FILE *file, int isOutput){ |
| 175 | if( isOutput ) fflush(file); |
| 176 | _setmode(_fileno(file), _O_TEXT); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 177 | } |
| 178 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 179 | # define setBinaryMode(X,Y) |
| 180 | # define setTextMode(X,Y) |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 183 | |
| 184 | /* True if the timer is enabled */ |
| 185 | static int enableTimer = 0; |
| 186 | |
| 187 | /* Return the current wall-clock time */ |
| 188 | static sqlite3_int64 timeOfDay(void){ |
| 189 | static sqlite3_vfs *clockVfs = 0; |
| 190 | sqlite3_int64 t; |
| 191 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
dan | 3fd415b | 2015-11-16 08:54:10 +0000 | [diff] [blame] | 192 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 193 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 194 | }else{ |
| 195 | double r; |
| 196 | clockVfs->xCurrentTime(clockVfs, &r); |
| 197 | t = (sqlite3_int64)(r*86400000.0); |
| 198 | } |
| 199 | return t; |
| 200 | } |
| 201 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 202 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 203 | #include <sys/time.h> |
| 204 | #include <sys/resource.h> |
| 205 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 206 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 207 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 208 | struct rusage { |
| 209 | struct timeval ru_utime; /* user CPU time used */ |
| 210 | struct timeval ru_stime; /* system CPU time used */ |
| 211 | }; |
| 212 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 213 | #endif |
| 214 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 215 | /* Saved resource information for the beginning of an operation */ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 216 | static struct rusage sBegin; /* CPU time at start */ |
| 217 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 218 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 219 | /* |
| 220 | ** Begin timing an operation |
| 221 | */ |
| 222 | static void beginTimer(void){ |
| 223 | if( enableTimer ){ |
| 224 | getrusage(RUSAGE_SELF, &sBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 225 | iBegin = timeOfDay(); |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /* Return the difference of two time_structs in seconds */ |
| 230 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 231 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 232 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | ** Print the timing results. |
| 237 | */ |
| 238 | static void endTimer(void){ |
| 239 | if( enableTimer ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 240 | sqlite3_int64 iEnd = timeOfDay(); |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 241 | struct rusage sEnd; |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 242 | getrusage(RUSAGE_SELF, &sEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 243 | printf("Run Time: real %.3f user %f sys %f\n", |
| 244 | (iEnd - iBegin)*0.001, |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 245 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 246 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 247 | } |
| 248 | } |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 249 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 250 | #define BEGIN_TIMER beginTimer() |
| 251 | #define END_TIMER endTimer() |
| 252 | #define HAS_TIMER 1 |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 253 | |
| 254 | #elif (defined(_WIN32) || defined(WIN32)) |
| 255 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 256 | /* Saved resource information for the beginning of an operation */ |
| 257 | static HANDLE hProcess; |
| 258 | static FILETIME ftKernelBegin; |
| 259 | static FILETIME ftUserBegin; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 260 | static sqlite3_int64 ftWallBegin; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 261 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 262 | LPFILETIME, LPFILETIME); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 263 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 264 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 265 | /* |
| 266 | ** Check to see if we have timer support. Return 1 if necessary |
| 267 | ** support found (or found previously). |
| 268 | */ |
| 269 | static int hasTimer(void){ |
| 270 | if( getProcessTimesAddr ){ |
| 271 | return 1; |
| 272 | } else { |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 273 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 274 | ** versions. See if the version we are running on has it, and if it |
| 275 | ** does, save off a pointer to it and the current process handle. |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 276 | */ |
| 277 | hProcess = GetCurrentProcess(); |
| 278 | if( hProcess ){ |
| 279 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 280 | if( NULL != hinstLib ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 281 | getProcessTimesAddr = |
| 282 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 283 | if( NULL != getProcessTimesAddr ){ |
| 284 | return 1; |
| 285 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 286 | FreeLibrary(hinstLib); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | ** Begin timing an operation |
| 295 | */ |
| 296 | static void beginTimer(void){ |
| 297 | if( enableTimer && getProcessTimesAddr ){ |
| 298 | FILETIME ftCreation, ftExit; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 299 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 300 | &ftKernelBegin,&ftUserBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 301 | ftWallBegin = timeOfDay(); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | /* Return the difference of two FILETIME structs in seconds */ |
| 306 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 307 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 308 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 309 | return (double) ((i64End - i64Start) / 10000000.0); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | ** Print the timing results. |
| 314 | */ |
| 315 | static void endTimer(void){ |
| 316 | if( enableTimer && getProcessTimesAddr){ |
| 317 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 318 | sqlite3_int64 ftWallEnd = timeOfDay(); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 319 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 320 | printf("Run Time: real %.3f user %f sys %f\n", |
| 321 | (ftWallEnd - ftWallBegin)*0.001, |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 322 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 323 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | #define BEGIN_TIMER beginTimer() |
| 328 | #define END_TIMER endTimer() |
| 329 | #define HAS_TIMER hasTimer() |
| 330 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 331 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 332 | #define BEGIN_TIMER |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 333 | #define END_TIMER |
| 334 | #define HAS_TIMER 0 |
| 335 | #endif |
| 336 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 337 | /* |
| 338 | ** Used to prevent warnings about unused parameters |
| 339 | */ |
| 340 | #define UNUSED_PARAMETER(x) (void)(x) |
| 341 | |
drh | e91d16b | 2008-12-08 18:27:31 +0000 | [diff] [blame] | 342 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 343 | ** If the following flag is set, then command execution stops |
| 344 | ** at an error if we are not interactive. |
| 345 | */ |
| 346 | static int bail_on_error = 0; |
| 347 | |
| 348 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 349 | ** Threat stdin as an interactive input if the following variable |
| 350 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 351 | */ |
| 352 | static int stdin_is_interactive = 1; |
| 353 | |
| 354 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 355 | ** On Windows systems we have to know if standard output is a console |
| 356 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 357 | ** true if translation is required. |
| 358 | */ |
| 359 | static int stdout_is_console = 1; |
| 360 | |
| 361 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 362 | ** The following is the open SQLite database. We make a pointer |
| 363 | ** to this database a static variable so that it can be accessed |
| 364 | ** by the SIGINT handler to interrupt database processing. |
| 365 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 366 | static sqlite3 *globalDb = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 367 | |
| 368 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 369 | ** True if an interrupt (Control-C) has been received. |
| 370 | */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 371 | static volatile int seenInterrupt = 0; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 372 | |
| 373 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 374 | ** This is the name of our program. It is set in main(), used |
| 375 | ** in a number of other places, mostly for error messages. |
| 376 | */ |
| 377 | static char *Argv0; |
| 378 | |
| 379 | /* |
| 380 | ** Prompt strings. Initialized in main. Settable with |
| 381 | ** .prompt main continue |
| 382 | */ |
| 383 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 384 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 385 | |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 386 | /* |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 387 | ** Render output like fprintf(). Except, if the output is going to the |
| 388 | ** console and if this is running on a Windows machine, translate the |
| 389 | ** output from UTF-8 into MBCS. |
| 390 | */ |
| 391 | #if defined(_WIN32) || defined(WIN32) |
| 392 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 393 | va_list ap; |
| 394 | va_start(ap, zFormat); |
| 395 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 396 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 397 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 398 | sqlite3_free(z1); |
| 399 | fputs(z2, out); |
| 400 | sqlite3_free(z2); |
| 401 | }else{ |
| 402 | vfprintf(out, zFormat, ap); |
| 403 | } |
| 404 | va_end(ap); |
| 405 | } |
| 406 | #elif !defined(utf8_printf) |
| 407 | # define utf8_printf fprintf |
| 408 | #endif |
| 409 | |
| 410 | /* |
| 411 | ** Render output like fprintf(). This should not be used on anything that |
| 412 | ** includes string formatting (e.g. "%s"). |
| 413 | */ |
| 414 | #if !defined(raw_printf) |
| 415 | # define raw_printf fprintf |
| 416 | #endif |
| 417 | |
| 418 | /* |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 419 | ** Write I/O traces to the following stream. |
| 420 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 421 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 422 | static FILE *iotrace = 0; |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 423 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | ** This routine works like printf in that its first argument is a |
| 427 | ** format string and subsequent arguments are values to be substituted |
| 428 | ** in place of % fields. The result of formatting this string |
| 429 | ** is written to iotrace. |
| 430 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 431 | #ifdef SQLITE_ENABLE_IOTRACE |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 432 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 433 | va_list ap; |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 434 | char *z; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 435 | if( iotrace==0 ) return; |
| 436 | va_start(ap, zFormat); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 437 | z = sqlite3_vmprintf(zFormat, ap); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 438 | va_end(ap); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 439 | utf8_printf(iotrace, "%s", z); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 440 | sqlite3_free(z); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 441 | } |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 442 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 443 | |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 444 | /* |
| 445 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 446 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 447 | ** in bytes. This is different from the %*.*s specification in printf |
| 448 | ** since with %*.*s the width is measured in bytes, not characters. |
| 449 | */ |
| 450 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 451 | int i; |
| 452 | int n; |
| 453 | int aw = w<0 ? -w : w; |
| 454 | char zBuf[1000]; |
drh | f8a2e8c | 2017-05-06 17:12:52 +0000 | [diff] [blame] | 455 | if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 456 | for(i=n=0; zUtf[i]; i++){ |
| 457 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 458 | n++; |
| 459 | if( n==aw ){ |
| 460 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | if( n>=aw ){ |
| 466 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 467 | }else if( w<0 ){ |
| 468 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 469 | }else{ |
| 470 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 471 | } |
| 472 | } |
| 473 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 474 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 475 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 476 | ** Determines if a string is a number of not. |
| 477 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 478 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 479 | if( *z=='-' || *z=='+' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 480 | if( !IsDigit(*z) ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | z++; |
| 484 | if( realnum ) *realnum = 0; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 485 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 486 | if( *z=='.' ){ |
| 487 | z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 488 | if( !IsDigit(*z) ) return 0; |
| 489 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 490 | if( realnum ) *realnum = 1; |
| 491 | } |
| 492 | if( *z=='e' || *z=='E' ){ |
| 493 | z++; |
| 494 | if( *z=='+' || *z=='-' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 495 | if( !IsDigit(*z) ) return 0; |
| 496 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 497 | if( realnum ) *realnum = 1; |
| 498 | } |
| 499 | return *z==0; |
| 500 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 503 | ** Compute a string length that is limited to what can be stored in |
| 504 | ** lower 30 bits of a 32-bit signed integer. |
| 505 | */ |
| 506 | static int strlen30(const char *z){ |
| 507 | const char *z2 = z; |
| 508 | while( *z2 ){ z2++; } |
| 509 | return 0x3fffffff & (int)(z2 - z); |
| 510 | } |
| 511 | |
| 512 | /* |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 513 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 514 | ** count as a single character. |
| 515 | */ |
| 516 | static int strlenChar(const char *z){ |
| 517 | int n = 0; |
| 518 | while( *z ){ |
| 519 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 520 | } |
| 521 | return n; |
| 522 | } |
| 523 | |
| 524 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 525 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 526 | ** the text in memory obtained from malloc() and returns a pointer |
| 527 | ** to the text. NULL is returned at end of file, or if malloc() |
| 528 | ** fails. |
| 529 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 530 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 531 | ** a previous call to this routine that may be reused. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 532 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 533 | static char *local_getline(char *zLine, FILE *in){ |
| 534 | int nLine = zLine==0 ? 0 : 100; |
| 535 | int n = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 536 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 537 | while( 1 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 538 | if( n+100>nLine ){ |
| 539 | nLine = nLine*2 + 100; |
| 540 | zLine = realloc(zLine, nLine); |
| 541 | if( zLine==0 ) return 0; |
| 542 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 543 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 544 | if( n==0 ){ |
| 545 | free(zLine); |
| 546 | return 0; |
| 547 | } |
| 548 | zLine[n] = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 549 | break; |
| 550 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 551 | while( zLine[n] ) n++; |
| 552 | if( n>0 && zLine[n-1]=='\n' ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 553 | n--; |
shaneh | 13b3602 | 2009-12-17 21:07:15 +0000 | [diff] [blame] | 554 | if( n>0 && zLine[n-1]=='\r' ) n--; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 555 | zLine[n] = 0; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 556 | break; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 559 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 560 | /* For interactive input on Windows systems, translate the |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 561 | ** multi-byte characterset characters into UTF-8. */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 562 | if( stdin_is_interactive && in==stdin ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 563 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 564 | if( zTrans ){ |
| 565 | int nTrans = strlen30(zTrans)+1; |
| 566 | if( nTrans>nLine ){ |
| 567 | zLine = realloc(zLine, nTrans); |
| 568 | if( zLine==0 ){ |
| 569 | sqlite3_free(zTrans); |
| 570 | return 0; |
| 571 | } |
| 572 | } |
| 573 | memcpy(zLine, zTrans, nTrans); |
| 574 | sqlite3_free(zTrans); |
| 575 | } |
| 576 | } |
| 577 | #endif /* defined(_WIN32) || defined(WIN32) */ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 578 | return zLine; |
| 579 | } |
| 580 | |
| 581 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 582 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 583 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 584 | ** If in==0 then read from standard input and prompt before each line. |
| 585 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 586 | ** If isContinuation is zero, then the main prompt should be used. |
| 587 | ** |
| 588 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 589 | ** routine that can be reused. |
| 590 | ** |
| 591 | ** The result is stored in space obtained from malloc() and must either |
| 592 | ** be freed by the caller or else passed back into this routine via the |
| 593 | ** zPrior argument for reuse. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 594 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 595 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 596 | char *zPrompt; |
| 597 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 598 | if( in!=0 ){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 599 | zResult = local_getline(zPrior, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 600 | }else{ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 601 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 602 | #if SHELL_USE_LOCAL_GETLINE |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 603 | printf("%s", zPrompt); |
| 604 | fflush(stdout); |
| 605 | zResult = local_getline(zPrior, stdin); |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 606 | #else |
| 607 | free(zPrior); |
| 608 | zResult = shell_readline(zPrompt); |
| 609 | if( zResult && *zResult ) shell_add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 610 | #endif |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 611 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 612 | return zResult; |
| 613 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 614 | /* |
| 615 | ** A variable length string to which one can append text. |
| 616 | */ |
| 617 | typedef struct ShellText ShellText; |
| 618 | struct ShellText { |
| 619 | char *z; |
| 620 | int n; |
| 621 | int nAlloc; |
| 622 | }; |
| 623 | |
| 624 | /* |
| 625 | ** Initialize and destroy a ShellText object |
| 626 | */ |
| 627 | static void initText(ShellText *p){ |
| 628 | memset(p, 0, sizeof(*p)); |
| 629 | } |
| 630 | static void freeText(ShellText *p){ |
| 631 | free(p->z); |
| 632 | initText(p); |
| 633 | } |
| 634 | |
| 635 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 636 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 637 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 638 | ** zIn, if it was not NULL, is freed. |
| 639 | ** |
| 640 | ** If the third argument, quote, is not '\0', then it is used as a |
| 641 | ** quote character for zAppend. |
| 642 | */ |
| 643 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 644 | int len; |
| 645 | int i; |
| 646 | int nAppend = strlen30(zAppend); |
| 647 | |
| 648 | len = nAppend+p->n+1; |
| 649 | if( quote ){ |
| 650 | len += 2; |
| 651 | for(i=0; i<nAppend; i++){ |
| 652 | if( zAppend[i]==quote ) len++; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if( p->n+len>=p->nAlloc ){ |
| 657 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 658 | p->z = realloc(p->z, p->nAlloc); |
| 659 | if( p->z==0 ){ |
| 660 | memset(p, 0, sizeof(*p)); |
| 661 | return; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if( quote ){ |
| 666 | char *zCsr = p->z+p->n; |
| 667 | *zCsr++ = quote; |
| 668 | for(i=0; i<nAppend; i++){ |
| 669 | *zCsr++ = zAppend[i]; |
| 670 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 671 | } |
| 672 | *zCsr++ = quote; |
| 673 | p->n = (int)(zCsr - p->z); |
| 674 | *zCsr = '\0'; |
| 675 | }else{ |
| 676 | memcpy(p->z+p->n, zAppend, nAppend); |
| 677 | p->n += nAppend; |
| 678 | p->z[p->n] = '\0'; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 684 | ** because it contains non-alphanumeric characters, or because it is an |
| 685 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 686 | ** that quoting is required. |
| 687 | ** |
| 688 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 689 | */ |
| 690 | static char quoteChar(const char *zName){ |
| 691 | /* All SQLite keywords, in alphabetical order */ |
| 692 | static const char *azKeywords[] = { |
| 693 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 694 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 695 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 696 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 697 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 698 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 699 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 700 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 701 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 702 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 703 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 704 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 705 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 706 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 707 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 708 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 709 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 710 | "WITH", "WITHOUT", |
| 711 | }; |
| 712 | int i, lwr, upr, mid, c; |
| 713 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 714 | for(i=0; zName[i]; i++){ |
| 715 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 716 | } |
| 717 | lwr = 0; |
| 718 | upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; |
| 719 | while( lwr<=upr ){ |
| 720 | mid = (lwr+upr)/2; |
| 721 | c = sqlite3_stricmp(azKeywords[mid], zName); |
| 722 | if( c==0 ) return '"'; |
| 723 | if( c<0 ){ |
| 724 | lwr = mid+1; |
| 725 | }else{ |
| 726 | upr = mid-1; |
| 727 | } |
| 728 | } |
| 729 | return 0; |
| 730 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 731 | |
drh | 0942559 | 2017-06-15 16:56:05 +0000 | [diff] [blame] | 732 | /* |
| 733 | ** SQL function: shell_add_schema(S,X) |
| 734 | ** |
| 735 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 736 | ** Examples: |
| 737 | ** |
| 738 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 739 | ** |
| 740 | ** Also works on |
| 741 | ** |
| 742 | ** CREATE INDEX |
| 743 | ** CREATE UNIQUE INDEX |
| 744 | ** CREATE VIEW |
| 745 | ** CREATE TRIGGER |
| 746 | ** CREATE VIRTUAL TABLE |
| 747 | ** |
| 748 | ** This UDF is used by the .schema command to insert the schema name of |
| 749 | ** attached databases into the middle of the sqlite_master.sql field. |
| 750 | */ |
| 751 | static void shellAddSchemaName( |
| 752 | sqlite3_context *pCtx, |
| 753 | int nVal, |
| 754 | sqlite3_value **apVal |
| 755 | ){ |
| 756 | static const char *aPrefix[] = { |
| 757 | "TABLE", |
| 758 | "INDEX", |
| 759 | "UNIQUE INDEX", |
| 760 | "VIEW", |
| 761 | "TRIGGER", |
| 762 | "VIRTUAL TABLE" |
| 763 | }; |
| 764 | int i = 0; |
| 765 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 766 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
| 767 | assert( nVal==2 ); |
| 768 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
| 769 | for(i=0; i<sizeof(aPrefix)/sizeof(aPrefix[0]); i++){ |
| 770 | int n = strlen30(aPrefix[i]); |
| 771 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
| 772 | char cQuote = quoteChar(zSchema); |
| 773 | char *z; |
| 774 | if( cQuote ){ |
| 775 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 776 | }else{ |
| 777 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 778 | } |
| 779 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 780 | return; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | sqlite3_result_value(pCtx, apVal[0]); |
| 785 | } |
| 786 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 787 | /****************************************************************************** |
| 788 | ** SHA3 hash implementation copied from ../ext/misc/shathree.c |
| 789 | */ |
| 790 | typedef sqlite3_uint64 u64; |
| 791 | /* |
| 792 | ** Macros to determine whether the machine is big or little endian, |
| 793 | ** and whether or not that determination is run-time or compile-time. |
| 794 | ** |
| 795 | ** For best performance, an attempt is made to guess at the byte-order |
| 796 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 797 | ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined |
| 798 | ** at run-time. |
| 799 | */ |
| 800 | #ifndef SHA3_BYTEORDER |
| 801 | # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 802 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 803 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 804 | defined(__arm__) |
| 805 | # define SHA3_BYTEORDER 1234 |
| 806 | # elif defined(sparc) || defined(__ppc__) |
| 807 | # define SHA3_BYTEORDER 4321 |
| 808 | # else |
| 809 | # define SHA3_BYTEORDER 0 |
| 810 | # endif |
| 811 | #endif |
| 812 | |
| 813 | |
| 814 | /* |
| 815 | ** State structure for a SHA3 hash in progress |
| 816 | */ |
| 817 | typedef struct SHA3Context SHA3Context; |
| 818 | struct SHA3Context { |
| 819 | union { |
| 820 | u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ |
| 821 | unsigned char x[1600]; /* ... or 1600 bytes */ |
| 822 | } u; |
| 823 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 824 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 825 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 826 | }; |
| 827 | |
drh | 050b124 | 2017-05-04 11:13:50 +0000 | [diff] [blame] | 828 | /* Allow the following routine to use the B0 variable, which is also |
| 829 | ** a macro in the termios.h header file */ |
| 830 | #undef B0 |
| 831 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 832 | /* |
| 833 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 834 | */ |
| 835 | static void KeccakF1600Step(SHA3Context *p){ |
| 836 | int i; |
| 837 | u64 B0, B1, B2, B3, B4; |
| 838 | u64 C0, C1, C2, C3, C4; |
| 839 | u64 D0, D1, D2, D3, D4; |
| 840 | static const u64 RC[] = { |
| 841 | 0x0000000000000001ULL, 0x0000000000008082ULL, |
| 842 | 0x800000000000808aULL, 0x8000000080008000ULL, |
| 843 | 0x000000000000808bULL, 0x0000000080000001ULL, |
| 844 | 0x8000000080008081ULL, 0x8000000000008009ULL, |
| 845 | 0x000000000000008aULL, 0x0000000000000088ULL, |
| 846 | 0x0000000080008009ULL, 0x000000008000000aULL, |
| 847 | 0x000000008000808bULL, 0x800000000000008bULL, |
| 848 | 0x8000000000008089ULL, 0x8000000000008003ULL, |
| 849 | 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 850 | 0x000000000000800aULL, 0x800000008000000aULL, |
| 851 | 0x8000000080008081ULL, 0x8000000000008080ULL, |
| 852 | 0x0000000080000001ULL, 0x8000000080008008ULL |
| 853 | }; |
| 854 | # define A00 (p->u.s[0]) |
| 855 | # define A01 (p->u.s[1]) |
| 856 | # define A02 (p->u.s[2]) |
| 857 | # define A03 (p->u.s[3]) |
| 858 | # define A04 (p->u.s[4]) |
| 859 | # define A10 (p->u.s[5]) |
| 860 | # define A11 (p->u.s[6]) |
| 861 | # define A12 (p->u.s[7]) |
| 862 | # define A13 (p->u.s[8]) |
| 863 | # define A14 (p->u.s[9]) |
| 864 | # define A20 (p->u.s[10]) |
| 865 | # define A21 (p->u.s[11]) |
| 866 | # define A22 (p->u.s[12]) |
| 867 | # define A23 (p->u.s[13]) |
| 868 | # define A24 (p->u.s[14]) |
| 869 | # define A30 (p->u.s[15]) |
| 870 | # define A31 (p->u.s[16]) |
| 871 | # define A32 (p->u.s[17]) |
| 872 | # define A33 (p->u.s[18]) |
| 873 | # define A34 (p->u.s[19]) |
| 874 | # define A40 (p->u.s[20]) |
| 875 | # define A41 (p->u.s[21]) |
| 876 | # define A42 (p->u.s[22]) |
| 877 | # define A43 (p->u.s[23]) |
| 878 | # define A44 (p->u.s[24]) |
| 879 | # define ROL64(a,x) ((a<<x)|(a>>(64-x))) |
| 880 | |
| 881 | for(i=0; i<24; i+=4){ |
| 882 | C0 = A00^A10^A20^A30^A40; |
| 883 | C1 = A01^A11^A21^A31^A41; |
| 884 | C2 = A02^A12^A22^A32^A42; |
| 885 | C3 = A03^A13^A23^A33^A43; |
| 886 | C4 = A04^A14^A24^A34^A44; |
| 887 | D0 = C4^ROL64(C1, 1); |
| 888 | D1 = C0^ROL64(C2, 1); |
| 889 | D2 = C1^ROL64(C3, 1); |
| 890 | D3 = C2^ROL64(C4, 1); |
| 891 | D4 = C3^ROL64(C0, 1); |
| 892 | |
| 893 | B0 = (A00^D0); |
| 894 | B1 = ROL64((A11^D1), 44); |
| 895 | B2 = ROL64((A22^D2), 43); |
| 896 | B3 = ROL64((A33^D3), 21); |
| 897 | B4 = ROL64((A44^D4), 14); |
| 898 | A00 = B0 ^((~B1)& B2 ); |
| 899 | A00 ^= RC[i]; |
| 900 | A11 = B1 ^((~B2)& B3 ); |
| 901 | A22 = B2 ^((~B3)& B4 ); |
| 902 | A33 = B3 ^((~B4)& B0 ); |
| 903 | A44 = B4 ^((~B0)& B1 ); |
| 904 | |
| 905 | B2 = ROL64((A20^D0), 3); |
| 906 | B3 = ROL64((A31^D1), 45); |
| 907 | B4 = ROL64((A42^D2), 61); |
| 908 | B0 = ROL64((A03^D3), 28); |
| 909 | B1 = ROL64((A14^D4), 20); |
| 910 | A20 = B0 ^((~B1)& B2 ); |
| 911 | A31 = B1 ^((~B2)& B3 ); |
| 912 | A42 = B2 ^((~B3)& B4 ); |
| 913 | A03 = B3 ^((~B4)& B0 ); |
| 914 | A14 = B4 ^((~B0)& B1 ); |
| 915 | |
| 916 | B4 = ROL64((A40^D0), 18); |
| 917 | B0 = ROL64((A01^D1), 1); |
| 918 | B1 = ROL64((A12^D2), 6); |
| 919 | B2 = ROL64((A23^D3), 25); |
| 920 | B3 = ROL64((A34^D4), 8); |
| 921 | A40 = B0 ^((~B1)& B2 ); |
| 922 | A01 = B1 ^((~B2)& B3 ); |
| 923 | A12 = B2 ^((~B3)& B4 ); |
| 924 | A23 = B3 ^((~B4)& B0 ); |
| 925 | A34 = B4 ^((~B0)& B1 ); |
| 926 | |
| 927 | B1 = ROL64((A10^D0), 36); |
| 928 | B2 = ROL64((A21^D1), 10); |
| 929 | B3 = ROL64((A32^D2), 15); |
| 930 | B4 = ROL64((A43^D3), 56); |
| 931 | B0 = ROL64((A04^D4), 27); |
| 932 | A10 = B0 ^((~B1)& B2 ); |
| 933 | A21 = B1 ^((~B2)& B3 ); |
| 934 | A32 = B2 ^((~B3)& B4 ); |
| 935 | A43 = B3 ^((~B4)& B0 ); |
| 936 | A04 = B4 ^((~B0)& B1 ); |
| 937 | |
| 938 | B3 = ROL64((A30^D0), 41); |
| 939 | B4 = ROL64((A41^D1), 2); |
| 940 | B0 = ROL64((A02^D2), 62); |
| 941 | B1 = ROL64((A13^D3), 55); |
| 942 | B2 = ROL64((A24^D4), 39); |
| 943 | A30 = B0 ^((~B1)& B2 ); |
| 944 | A41 = B1 ^((~B2)& B3 ); |
| 945 | A02 = B2 ^((~B3)& B4 ); |
| 946 | A13 = B3 ^((~B4)& B0 ); |
| 947 | A24 = B4 ^((~B0)& B1 ); |
| 948 | |
| 949 | C0 = A00^A20^A40^A10^A30; |
| 950 | C1 = A11^A31^A01^A21^A41; |
| 951 | C2 = A22^A42^A12^A32^A02; |
| 952 | C3 = A33^A03^A23^A43^A13; |
| 953 | C4 = A44^A14^A34^A04^A24; |
| 954 | D0 = C4^ROL64(C1, 1); |
| 955 | D1 = C0^ROL64(C2, 1); |
| 956 | D2 = C1^ROL64(C3, 1); |
| 957 | D3 = C2^ROL64(C4, 1); |
| 958 | D4 = C3^ROL64(C0, 1); |
| 959 | |
| 960 | B0 = (A00^D0); |
| 961 | B1 = ROL64((A31^D1), 44); |
| 962 | B2 = ROL64((A12^D2), 43); |
| 963 | B3 = ROL64((A43^D3), 21); |
| 964 | B4 = ROL64((A24^D4), 14); |
| 965 | A00 = B0 ^((~B1)& B2 ); |
| 966 | A00 ^= RC[i+1]; |
| 967 | A31 = B1 ^((~B2)& B3 ); |
| 968 | A12 = B2 ^((~B3)& B4 ); |
| 969 | A43 = B3 ^((~B4)& B0 ); |
| 970 | A24 = B4 ^((~B0)& B1 ); |
| 971 | |
| 972 | B2 = ROL64((A40^D0), 3); |
| 973 | B3 = ROL64((A21^D1), 45); |
| 974 | B4 = ROL64((A02^D2), 61); |
| 975 | B0 = ROL64((A33^D3), 28); |
| 976 | B1 = ROL64((A14^D4), 20); |
| 977 | A40 = B0 ^((~B1)& B2 ); |
| 978 | A21 = B1 ^((~B2)& B3 ); |
| 979 | A02 = B2 ^((~B3)& B4 ); |
| 980 | A33 = B3 ^((~B4)& B0 ); |
| 981 | A14 = B4 ^((~B0)& B1 ); |
| 982 | |
| 983 | B4 = ROL64((A30^D0), 18); |
| 984 | B0 = ROL64((A11^D1), 1); |
| 985 | B1 = ROL64((A42^D2), 6); |
| 986 | B2 = ROL64((A23^D3), 25); |
| 987 | B3 = ROL64((A04^D4), 8); |
| 988 | A30 = B0 ^((~B1)& B2 ); |
| 989 | A11 = B1 ^((~B2)& B3 ); |
| 990 | A42 = B2 ^((~B3)& B4 ); |
| 991 | A23 = B3 ^((~B4)& B0 ); |
| 992 | A04 = B4 ^((~B0)& B1 ); |
| 993 | |
| 994 | B1 = ROL64((A20^D0), 36); |
| 995 | B2 = ROL64((A01^D1), 10); |
| 996 | B3 = ROL64((A32^D2), 15); |
| 997 | B4 = ROL64((A13^D3), 56); |
| 998 | B0 = ROL64((A44^D4), 27); |
| 999 | A20 = B0 ^((~B1)& B2 ); |
| 1000 | A01 = B1 ^((~B2)& B3 ); |
| 1001 | A32 = B2 ^((~B3)& B4 ); |
| 1002 | A13 = B3 ^((~B4)& B0 ); |
| 1003 | A44 = B4 ^((~B0)& B1 ); |
| 1004 | |
| 1005 | B3 = ROL64((A10^D0), 41); |
| 1006 | B4 = ROL64((A41^D1), 2); |
| 1007 | B0 = ROL64((A22^D2), 62); |
| 1008 | B1 = ROL64((A03^D3), 55); |
| 1009 | B2 = ROL64((A34^D4), 39); |
| 1010 | A10 = B0 ^((~B1)& B2 ); |
| 1011 | A41 = B1 ^((~B2)& B3 ); |
| 1012 | A22 = B2 ^((~B3)& B4 ); |
| 1013 | A03 = B3 ^((~B4)& B0 ); |
| 1014 | A34 = B4 ^((~B0)& B1 ); |
| 1015 | |
| 1016 | C0 = A00^A40^A30^A20^A10; |
| 1017 | C1 = A31^A21^A11^A01^A41; |
| 1018 | C2 = A12^A02^A42^A32^A22; |
| 1019 | C3 = A43^A33^A23^A13^A03; |
| 1020 | C4 = A24^A14^A04^A44^A34; |
| 1021 | D0 = C4^ROL64(C1, 1); |
| 1022 | D1 = C0^ROL64(C2, 1); |
| 1023 | D2 = C1^ROL64(C3, 1); |
| 1024 | D3 = C2^ROL64(C4, 1); |
| 1025 | D4 = C3^ROL64(C0, 1); |
| 1026 | |
| 1027 | B0 = (A00^D0); |
| 1028 | B1 = ROL64((A21^D1), 44); |
| 1029 | B2 = ROL64((A42^D2), 43); |
| 1030 | B3 = ROL64((A13^D3), 21); |
| 1031 | B4 = ROL64((A34^D4), 14); |
| 1032 | A00 = B0 ^((~B1)& B2 ); |
| 1033 | A00 ^= RC[i+2]; |
| 1034 | A21 = B1 ^((~B2)& B3 ); |
| 1035 | A42 = B2 ^((~B3)& B4 ); |
| 1036 | A13 = B3 ^((~B4)& B0 ); |
| 1037 | A34 = B4 ^((~B0)& B1 ); |
| 1038 | |
| 1039 | B2 = ROL64((A30^D0), 3); |
| 1040 | B3 = ROL64((A01^D1), 45); |
| 1041 | B4 = ROL64((A22^D2), 61); |
| 1042 | B0 = ROL64((A43^D3), 28); |
| 1043 | B1 = ROL64((A14^D4), 20); |
| 1044 | A30 = B0 ^((~B1)& B2 ); |
| 1045 | A01 = B1 ^((~B2)& B3 ); |
| 1046 | A22 = B2 ^((~B3)& B4 ); |
| 1047 | A43 = B3 ^((~B4)& B0 ); |
| 1048 | A14 = B4 ^((~B0)& B1 ); |
| 1049 | |
| 1050 | B4 = ROL64((A10^D0), 18); |
| 1051 | B0 = ROL64((A31^D1), 1); |
| 1052 | B1 = ROL64((A02^D2), 6); |
| 1053 | B2 = ROL64((A23^D3), 25); |
| 1054 | B3 = ROL64((A44^D4), 8); |
| 1055 | A10 = B0 ^((~B1)& B2 ); |
| 1056 | A31 = B1 ^((~B2)& B3 ); |
| 1057 | A02 = B2 ^((~B3)& B4 ); |
| 1058 | A23 = B3 ^((~B4)& B0 ); |
| 1059 | A44 = B4 ^((~B0)& B1 ); |
| 1060 | |
| 1061 | B1 = ROL64((A40^D0), 36); |
| 1062 | B2 = ROL64((A11^D1), 10); |
| 1063 | B3 = ROL64((A32^D2), 15); |
| 1064 | B4 = ROL64((A03^D3), 56); |
| 1065 | B0 = ROL64((A24^D4), 27); |
| 1066 | A40 = B0 ^((~B1)& B2 ); |
| 1067 | A11 = B1 ^((~B2)& B3 ); |
| 1068 | A32 = B2 ^((~B3)& B4 ); |
| 1069 | A03 = B3 ^((~B4)& B0 ); |
| 1070 | A24 = B4 ^((~B0)& B1 ); |
| 1071 | |
| 1072 | B3 = ROL64((A20^D0), 41); |
| 1073 | B4 = ROL64((A41^D1), 2); |
| 1074 | B0 = ROL64((A12^D2), 62); |
| 1075 | B1 = ROL64((A33^D3), 55); |
| 1076 | B2 = ROL64((A04^D4), 39); |
| 1077 | A20 = B0 ^((~B1)& B2 ); |
| 1078 | A41 = B1 ^((~B2)& B3 ); |
| 1079 | A12 = B2 ^((~B3)& B4 ); |
| 1080 | A33 = B3 ^((~B4)& B0 ); |
| 1081 | A04 = B4 ^((~B0)& B1 ); |
| 1082 | |
| 1083 | C0 = A00^A30^A10^A40^A20; |
| 1084 | C1 = A21^A01^A31^A11^A41; |
| 1085 | C2 = A42^A22^A02^A32^A12; |
| 1086 | C3 = A13^A43^A23^A03^A33; |
| 1087 | C4 = A34^A14^A44^A24^A04; |
| 1088 | D0 = C4^ROL64(C1, 1); |
| 1089 | D1 = C0^ROL64(C2, 1); |
| 1090 | D2 = C1^ROL64(C3, 1); |
| 1091 | D3 = C2^ROL64(C4, 1); |
| 1092 | D4 = C3^ROL64(C0, 1); |
| 1093 | |
| 1094 | B0 = (A00^D0); |
| 1095 | B1 = ROL64((A01^D1), 44); |
| 1096 | B2 = ROL64((A02^D2), 43); |
| 1097 | B3 = ROL64((A03^D3), 21); |
| 1098 | B4 = ROL64((A04^D4), 14); |
| 1099 | A00 = B0 ^((~B1)& B2 ); |
| 1100 | A00 ^= RC[i+3]; |
| 1101 | A01 = B1 ^((~B2)& B3 ); |
| 1102 | A02 = B2 ^((~B3)& B4 ); |
| 1103 | A03 = B3 ^((~B4)& B0 ); |
| 1104 | A04 = B4 ^((~B0)& B1 ); |
| 1105 | |
| 1106 | B2 = ROL64((A10^D0), 3); |
| 1107 | B3 = ROL64((A11^D1), 45); |
| 1108 | B4 = ROL64((A12^D2), 61); |
| 1109 | B0 = ROL64((A13^D3), 28); |
| 1110 | B1 = ROL64((A14^D4), 20); |
| 1111 | A10 = B0 ^((~B1)& B2 ); |
| 1112 | A11 = B1 ^((~B2)& B3 ); |
| 1113 | A12 = B2 ^((~B3)& B4 ); |
| 1114 | A13 = B3 ^((~B4)& B0 ); |
| 1115 | A14 = B4 ^((~B0)& B1 ); |
| 1116 | |
| 1117 | B4 = ROL64((A20^D0), 18); |
| 1118 | B0 = ROL64((A21^D1), 1); |
| 1119 | B1 = ROL64((A22^D2), 6); |
| 1120 | B2 = ROL64((A23^D3), 25); |
| 1121 | B3 = ROL64((A24^D4), 8); |
| 1122 | A20 = B0 ^((~B1)& B2 ); |
| 1123 | A21 = B1 ^((~B2)& B3 ); |
| 1124 | A22 = B2 ^((~B3)& B4 ); |
| 1125 | A23 = B3 ^((~B4)& B0 ); |
| 1126 | A24 = B4 ^((~B0)& B1 ); |
| 1127 | |
| 1128 | B1 = ROL64((A30^D0), 36); |
| 1129 | B2 = ROL64((A31^D1), 10); |
| 1130 | B3 = ROL64((A32^D2), 15); |
| 1131 | B4 = ROL64((A33^D3), 56); |
| 1132 | B0 = ROL64((A34^D4), 27); |
| 1133 | A30 = B0 ^((~B1)& B2 ); |
| 1134 | A31 = B1 ^((~B2)& B3 ); |
| 1135 | A32 = B2 ^((~B3)& B4 ); |
| 1136 | A33 = B3 ^((~B4)& B0 ); |
| 1137 | A34 = B4 ^((~B0)& B1 ); |
| 1138 | |
| 1139 | B3 = ROL64((A40^D0), 41); |
| 1140 | B4 = ROL64((A41^D1), 2); |
| 1141 | B0 = ROL64((A42^D2), 62); |
| 1142 | B1 = ROL64((A43^D3), 55); |
| 1143 | B2 = ROL64((A44^D4), 39); |
| 1144 | A40 = B0 ^((~B1)& B2 ); |
| 1145 | A41 = B1 ^((~B2)& B3 ); |
| 1146 | A42 = B2 ^((~B3)& B4 ); |
| 1147 | A43 = B3 ^((~B4)& B0 ); |
| 1148 | A44 = B4 ^((~B0)& B1 ); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | ** Initialize a new hash. iSize determines the size of the hash |
| 1154 | ** in bits and should be one of 224, 256, 384, or 512. Or iSize |
| 1155 | ** can be zero to use the default hash size of 256 bits. |
| 1156 | */ |
| 1157 | static void SHA3Init(SHA3Context *p, int iSize){ |
| 1158 | memset(p, 0, sizeof(*p)); |
| 1159 | if( iSize>=128 && iSize<=512 ){ |
| 1160 | p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; |
| 1161 | }else{ |
| 1162 | p->nRate = (1600 - 2*256)/8; |
| 1163 | } |
| 1164 | #if SHA3_BYTEORDER==1234 |
| 1165 | /* Known to be little-endian at compile-time. No-op */ |
| 1166 | #elif SHA3_BYTEORDER==4321 |
| 1167 | p->ixMask = 7; /* Big-endian */ |
| 1168 | #else |
| 1169 | { |
| 1170 | static unsigned int one = 1; |
| 1171 | if( 1==*(unsigned char*)&one ){ |
| 1172 | /* Little endian. No byte swapping. */ |
| 1173 | p->ixMask = 0; |
| 1174 | }else{ |
| 1175 | /* Big endian. Byte swap. */ |
| 1176 | p->ixMask = 7; |
| 1177 | } |
| 1178 | } |
| 1179 | #endif |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | ** Make consecutive calls to the SHA3Update function to add new content |
| 1184 | ** to the hash |
| 1185 | */ |
| 1186 | static void SHA3Update( |
| 1187 | SHA3Context *p, |
| 1188 | const unsigned char *aData, |
| 1189 | unsigned int nData |
| 1190 | ){ |
| 1191 | unsigned int i = 0; |
| 1192 | #if SHA3_BYTEORDER==1234 |
| 1193 | if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ |
| 1194 | for(; i+7<nData; i+=8){ |
| 1195 | p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; |
| 1196 | p->nLoaded += 8; |
| 1197 | if( p->nLoaded>=p->nRate ){ |
| 1198 | KeccakF1600Step(p); |
| 1199 | p->nLoaded = 0; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | #endif |
| 1204 | for(; i<nData; i++){ |
| 1205 | #if SHA3_BYTEORDER==1234 |
| 1206 | p->u.x[p->nLoaded] ^= aData[i]; |
| 1207 | #elif SHA3_BYTEORDER==4321 |
| 1208 | p->u.x[p->nLoaded^0x07] ^= aData[i]; |
| 1209 | #else |
| 1210 | p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; |
| 1211 | #endif |
| 1212 | p->nLoaded++; |
| 1213 | if( p->nLoaded==p->nRate ){ |
| 1214 | KeccakF1600Step(p); |
| 1215 | p->nLoaded = 0; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | ** After all content has been added, invoke SHA3Final() to compute |
| 1222 | ** the final hash. The function returns a pointer to the binary |
| 1223 | ** hash value. |
| 1224 | */ |
| 1225 | static unsigned char *SHA3Final(SHA3Context *p){ |
| 1226 | unsigned int i; |
| 1227 | if( p->nLoaded==p->nRate-1 ){ |
| 1228 | const unsigned char c1 = 0x86; |
| 1229 | SHA3Update(p, &c1, 1); |
| 1230 | }else{ |
| 1231 | const unsigned char c2 = 0x06; |
| 1232 | const unsigned char c3 = 0x80; |
| 1233 | SHA3Update(p, &c2, 1); |
| 1234 | p->nLoaded = p->nRate - 1; |
| 1235 | SHA3Update(p, &c3, 1); |
| 1236 | } |
| 1237 | for(i=0; i<p->nRate; i++){ |
| 1238 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1239 | } |
| 1240 | return &p->u.x[p->nRate]; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | ** Implementation of the sha3(X,SIZE) function. |
| 1245 | ** |
| 1246 | ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 1247 | ** size is 256. If X is a BLOB, it is hashed as is. |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1248 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1249 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1250 | ** of a NULL value is NULL. |
| 1251 | */ |
| 1252 | static void sha3Func( |
| 1253 | sqlite3_context *context, |
| 1254 | int argc, |
| 1255 | sqlite3_value **argv |
| 1256 | ){ |
| 1257 | SHA3Context cx; |
| 1258 | int eType = sqlite3_value_type(argv[0]); |
| 1259 | int nByte = sqlite3_value_bytes(argv[0]); |
| 1260 | int iSize; |
| 1261 | if( argc==1 ){ |
| 1262 | iSize = 256; |
| 1263 | }else{ |
| 1264 | iSize = sqlite3_value_int(argv[1]); |
| 1265 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1266 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1267 | "384 512", -1); |
| 1268 | return; |
| 1269 | } |
| 1270 | } |
| 1271 | if( eType==SQLITE_NULL ) return; |
| 1272 | SHA3Init(&cx, iSize); |
| 1273 | if( eType==SQLITE_BLOB ){ |
| 1274 | SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 1275 | }else{ |
| 1276 | SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); |
| 1277 | } |
| 1278 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1279 | } |
| 1280 | |
| 1281 | /* Compute a string using sqlite3_vsnprintf() with a maximum length |
| 1282 | ** of 50 bytes and add it to the hash. |
| 1283 | */ |
| 1284 | static void hash_step_vformat( |
| 1285 | SHA3Context *p, /* Add content to this context */ |
| 1286 | const char *zFormat, |
| 1287 | ... |
| 1288 | ){ |
| 1289 | va_list ap; |
| 1290 | int n; |
| 1291 | char zBuf[50]; |
| 1292 | va_start(ap, zFormat); |
| 1293 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 1294 | va_end(ap); |
| 1295 | n = (int)strlen(zBuf); |
| 1296 | SHA3Update(p, (unsigned char*)zBuf, n); |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | ** Implementation of the sha3_query(SQL,SIZE) function. |
| 1301 | ** |
| 1302 | ** This function compiles and runs the SQL statement(s) given in the |
| 1303 | ** argument. The results are hashed using a SIZE-bit SHA3. The default |
| 1304 | ** size is 256. |
| 1305 | ** |
| 1306 | ** The format of the byte stream that is hashed is summarized as follows: |
| 1307 | ** |
| 1308 | ** S<n>:<sql> |
| 1309 | ** R |
| 1310 | ** N |
| 1311 | ** I<int> |
| 1312 | ** F<ieee-float> |
| 1313 | ** B<size>:<bytes> |
| 1314 | ** T<size>:<text> |
| 1315 | ** |
| 1316 | ** <sql> is the original SQL text for each statement run and <n> is |
| 1317 | ** the size of that text. The SQL text is UTF-8. A single R character |
| 1318 | ** occurs before the start of each row. N means a NULL value. |
| 1319 | ** I mean an 8-byte little-endian integer <int>. F is a floating point |
| 1320 | ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. |
| 1321 | ** B means blobs of <size> bytes. T means text rendered as <size> |
| 1322 | ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII |
| 1323 | ** text integers. |
| 1324 | ** |
| 1325 | ** For each SQL statement in the X input, there is one S segment. Each |
| 1326 | ** S segment is followed by zero or more R segments, one for each row in the |
| 1327 | ** result set. After each R, there are one or more N, I, F, B, or T segments, |
| 1328 | ** one for each column in the result set. Segments are concatentated directly |
| 1329 | ** with no delimiters of any kind. |
| 1330 | */ |
| 1331 | static void sha3QueryFunc( |
| 1332 | sqlite3_context *context, |
| 1333 | int argc, |
| 1334 | sqlite3_value **argv |
| 1335 | ){ |
| 1336 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1337 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1338 | sqlite3_stmt *pStmt = 0; |
| 1339 | int nCol; /* Number of columns in the result set */ |
| 1340 | int i; /* Loop counter */ |
| 1341 | int rc; |
| 1342 | int n; |
| 1343 | const char *z; |
| 1344 | SHA3Context cx; |
| 1345 | int iSize; |
| 1346 | |
| 1347 | if( argc==1 ){ |
| 1348 | iSize = 256; |
| 1349 | }else{ |
| 1350 | iSize = sqlite3_value_int(argv[1]); |
| 1351 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1352 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1353 | "384 512", -1); |
| 1354 | return; |
| 1355 | } |
| 1356 | } |
| 1357 | if( zSql==0 ) return; |
| 1358 | SHA3Init(&cx, iSize); |
| 1359 | while( zSql[0] ){ |
| 1360 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 1361 | if( rc ){ |
| 1362 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 1363 | zSql, sqlite3_errmsg(db)); |
| 1364 | sqlite3_finalize(pStmt); |
| 1365 | sqlite3_result_error(context, zMsg, -1); |
| 1366 | sqlite3_free(zMsg); |
| 1367 | return; |
| 1368 | } |
| 1369 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 1370 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 1371 | sqlite3_finalize(pStmt); |
| 1372 | sqlite3_result_error(context, zMsg, -1); |
| 1373 | sqlite3_free(zMsg); |
| 1374 | return; |
| 1375 | } |
| 1376 | nCol = sqlite3_column_count(pStmt); |
| 1377 | z = sqlite3_sql(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 1378 | if( z==0 ){ |
| 1379 | sqlite3_finalize(pStmt); |
| 1380 | continue; |
| 1381 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1382 | n = (int)strlen(z); |
| 1383 | hash_step_vformat(&cx,"S%d:",n); |
| 1384 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1385 | |
| 1386 | /* Compute a hash over the result of the query */ |
| 1387 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1388 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 1389 | for(i=0; i<nCol; i++){ |
| 1390 | switch( sqlite3_column_type(pStmt,i) ){ |
| 1391 | case SQLITE_NULL: { |
| 1392 | SHA3Update(&cx, (const unsigned char*)"N",1); |
| 1393 | break; |
| 1394 | } |
| 1395 | case SQLITE_INTEGER: { |
| 1396 | sqlite3_uint64 u; |
| 1397 | int j; |
| 1398 | unsigned char x[9]; |
| 1399 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 1400 | memcpy(&u, &v, 8); |
| 1401 | for(j=8; j>=1; j--){ |
| 1402 | x[j] = u & 0xff; |
| 1403 | u >>= 8; |
| 1404 | } |
| 1405 | x[0] = 'I'; |
| 1406 | SHA3Update(&cx, x, 9); |
| 1407 | break; |
| 1408 | } |
| 1409 | case SQLITE_FLOAT: { |
| 1410 | sqlite3_uint64 u; |
| 1411 | int j; |
| 1412 | unsigned char x[9]; |
| 1413 | double r = sqlite3_column_double(pStmt,i); |
| 1414 | memcpy(&u, &r, 8); |
| 1415 | for(j=8; j>=1; j--){ |
| 1416 | x[j] = u & 0xff; |
| 1417 | u >>= 8; |
| 1418 | } |
| 1419 | x[0] = 'F'; |
| 1420 | SHA3Update(&cx,x,9); |
| 1421 | break; |
| 1422 | } |
| 1423 | case SQLITE_TEXT: { |
| 1424 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1425 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 1426 | hash_step_vformat(&cx,"T%d:",n2); |
| 1427 | SHA3Update(&cx, z2, n2); |
| 1428 | break; |
| 1429 | } |
| 1430 | case SQLITE_BLOB: { |
| 1431 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1432 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 1433 | hash_step_vformat(&cx,"B%d:",n2); |
| 1434 | SHA3Update(&cx, z2, n2); |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | sqlite3_finalize(pStmt); |
| 1441 | } |
| 1442 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1443 | } |
| 1444 | /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c |
| 1445 | ********************************************************************************/ |
| 1446 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1447 | #if defined(SQLITE_ENABLE_SESSION) |
| 1448 | /* |
| 1449 | ** State information for a single open session |
| 1450 | */ |
| 1451 | typedef struct OpenSession OpenSession; |
| 1452 | struct OpenSession { |
| 1453 | char *zName; /* Symbolic name for this session */ |
| 1454 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1455 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1456 | sqlite3_session *p; /* The open session */ |
| 1457 | }; |
| 1458 | #endif |
| 1459 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1460 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1461 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1462 | ** saved so that it can be restored by ".explain off" |
| 1463 | */ |
| 1464 | typedef struct SavedModeInfo SavedModeInfo; |
| 1465 | struct SavedModeInfo { |
| 1466 | int valid; /* Is there legit data in here? */ |
| 1467 | int mode; /* Mode prior to ".explain on" */ |
| 1468 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 1469 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1470 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1471 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1472 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1473 | ** State information about the database connection is contained in an |
| 1474 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1475 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1476 | typedef struct ShellState ShellState; |
| 1477 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1478 | sqlite3 *db; /* The database */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1479 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1480 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1481 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1482 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1483 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1484 | int cnt; /* Number of records displayed so far */ |
| 1485 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 1486 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1487 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1488 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1489 | int cMode; /* temporary output mode for the current query */ |
| 1490 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1491 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1492 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1493 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1494 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1495 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1496 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1497 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1498 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1499 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1500 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1501 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1502 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1503 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1504 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 1505 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1506 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1507 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1508 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1509 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1510 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1511 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1512 | #if defined(SQLITE_ENABLE_SESSION) |
| 1513 | int nSession; /* Number of active sessions */ |
| 1514 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1515 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1516 | }; |
| 1517 | |
| 1518 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1519 | ** These are the allowed shellFlgs values |
| 1520 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 1521 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 1522 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 1523 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 1524 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 1525 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
| 1526 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1527 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
| 1528 | |
| 1529 | /* |
| 1530 | ** Macros for testing and setting shellFlgs |
| 1531 | */ |
| 1532 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1533 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1534 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1535 | |
| 1536 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1537 | ** These are the allowed modes. |
| 1538 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1539 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1540 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1541 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1542 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1543 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1544 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1545 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1546 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1547 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1548 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1549 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1550 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1551 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1552 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1553 | "line", |
| 1554 | "column", |
| 1555 | "list", |
| 1556 | "semi", |
| 1557 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1558 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1559 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1560 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1561 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1562 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1563 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1564 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1565 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1568 | ** These are the column/row/line separators used by the various |
| 1569 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1570 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1571 | #define SEP_Column "|" |
| 1572 | #define SEP_Row "\n" |
| 1573 | #define SEP_Tab "\t" |
| 1574 | #define SEP_Space " " |
| 1575 | #define SEP_Comma "," |
| 1576 | #define SEP_CrLf "\r\n" |
| 1577 | #define SEP_Unit "\x1F" |
| 1578 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1579 | |
| 1580 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1581 | ** Number of elements in an array |
| 1582 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1583 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1586 | ** A callback for the sqlite3_log() interface. |
| 1587 | */ |
| 1588 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1589 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1590 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1591 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1592 | fflush(p->pLog); |
| 1593 | } |
| 1594 | |
| 1595 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1596 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1597 | */ |
| 1598 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1599 | int i; |
| 1600 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1601 | raw_printf(out,"X'"); |
| 1602 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1603 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | /* |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1607 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1608 | ** to that string. |
| 1609 | ** |
| 1610 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1611 | ** then make up some string and store it in the buffer zBuf. |
| 1612 | */ |
| 1613 | static const char *unused_string( |
| 1614 | const char *z, /* Result must not appear anywhere in z */ |
| 1615 | const char *zA, const char *zB, /* Try these first */ |
| 1616 | char *zBuf /* Space to store a generated string */ |
| 1617 | ){ |
| 1618 | unsigned i = 0; |
| 1619 | if( strstr(z, zA)==0 ) return zA; |
| 1620 | if( strstr(z, zB)==0 ) return zB; |
| 1621 | do{ |
| 1622 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1623 | }while( strstr(z,zBuf)!=0 ); |
| 1624 | return zBuf; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1628 | ** Output the given string as a quoted string using SQL quoting conventions. |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1629 | ** |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1630 | ** See also: output_quoted_escaped_string() |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1631 | */ |
| 1632 | static void output_quoted_string(FILE *out, const char *z){ |
| 1633 | int i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1634 | char c; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1635 | setBinaryMode(out, 1); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1636 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1637 | if( c==0 ){ |
| 1638 | utf8_printf(out,"'%s'",z); |
| 1639 | }else{ |
| 1640 | raw_printf(out, "'"); |
| 1641 | while( *z ){ |
| 1642 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1643 | if( c=='\'' ) i++; |
| 1644 | if( i ){ |
| 1645 | utf8_printf(out, "%.*s", i, z); |
| 1646 | z += i; |
| 1647 | } |
| 1648 | if( c=='\'' ){ |
| 1649 | raw_printf(out, "'"); |
| 1650 | continue; |
| 1651 | } |
| 1652 | if( c==0 ){ |
| 1653 | break; |
| 1654 | } |
| 1655 | z++; |
| 1656 | } |
| 1657 | raw_printf(out, "'"); |
| 1658 | } |
| 1659 | setTextMode(out, 1); |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1664 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1665 | ** get corrupted by end-of-line translation facilities in some operating |
| 1666 | ** systems. |
| 1667 | ** |
| 1668 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1669 | ** escape mechanism. |
| 1670 | */ |
| 1671 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1672 | int i; |
| 1673 | char c; |
| 1674 | setBinaryMode(out, 1); |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1675 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1676 | if( c==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1677 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1678 | }else{ |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1679 | const char *zNL = 0; |
| 1680 | const char *zCR = 0; |
| 1681 | int nNL = 0; |
| 1682 | int nCR = 0; |
| 1683 | char zBuf1[20], zBuf2[20]; |
| 1684 | for(i=0; z[i]; i++){ |
| 1685 | if( z[i]=='\n' ) nNL++; |
| 1686 | if( z[i]=='\r' ) nCR++; |
| 1687 | } |
| 1688 | if( nNL ){ |
| 1689 | raw_printf(out, "replace("); |
| 1690 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1691 | } |
| 1692 | if( nCR ){ |
| 1693 | raw_printf(out, "replace("); |
| 1694 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1695 | } |
| 1696 | raw_printf(out, "'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1697 | while( *z ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1698 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1699 | if( c=='\'' ) i++; |
| 1700 | if( i ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1701 | utf8_printf(out, "%.*s", i, z); |
| 1702 | z += i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1703 | } |
| 1704 | if( c=='\'' ){ |
| 1705 | raw_printf(out, "'"); |
| 1706 | continue; |
| 1707 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1708 | if( c==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1709 | break; |
| 1710 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1711 | z++; |
| 1712 | if( c=='\n' ){ |
| 1713 | raw_printf(out, "%s", zNL); |
| 1714 | continue; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1715 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1716 | raw_printf(out, "%s", zCR); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1717 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1718 | raw_printf(out, "'"); |
| 1719 | if( nCR ){ |
| 1720 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1721 | } |
| 1722 | if( nNL ){ |
| 1723 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1724 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1725 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1726 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1730 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1731 | */ |
| 1732 | static void output_c_string(FILE *out, const char *z){ |
| 1733 | unsigned int c; |
| 1734 | fputc('"', out); |
| 1735 | while( (c = *(z++))!=0 ){ |
| 1736 | if( c=='\\' ){ |
| 1737 | fputc(c, out); |
| 1738 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 1739 | }else if( c=='"' ){ |
| 1740 | fputc('\\', out); |
| 1741 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1742 | }else if( c=='\t' ){ |
| 1743 | fputc('\\', out); |
| 1744 | fputc('t', out); |
| 1745 | }else if( c=='\n' ){ |
| 1746 | fputc('\\', out); |
| 1747 | fputc('n', out); |
| 1748 | }else if( c=='\r' ){ |
| 1749 | fputc('\\', out); |
| 1750 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 1751 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1752 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1753 | }else{ |
| 1754 | fputc(c, out); |
| 1755 | } |
| 1756 | } |
| 1757 | fputc('"', out); |
| 1758 | } |
| 1759 | |
| 1760 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1761 | ** Output the given string with characters that are special to |
| 1762 | ** HTML escaped. |
| 1763 | */ |
| 1764 | static void output_html_string(FILE *out, const char *z){ |
| 1765 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 1766 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1767 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1768 | for(i=0; z[i] |
| 1769 | && z[i]!='<' |
| 1770 | && z[i]!='&' |
| 1771 | && z[i]!='>' |
| 1772 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1773 | && z[i]!='\''; |
| 1774 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1775 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1776 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1777 | } |
| 1778 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1779 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1780 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1781 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1782 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1783 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1784 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1785 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1786 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1787 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1788 | }else{ |
| 1789 | break; |
| 1790 | } |
| 1791 | z += i + 1; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1796 | ** If a field contains any character identified by a 1 in the following |
| 1797 | ** array, then the string must be quoted for CSV. |
| 1798 | */ |
| 1799 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1800 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1801 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1802 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1803 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1804 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1805 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1806 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1807 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1808 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1809 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1810 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1811 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1812 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1813 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1814 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1815 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1816 | }; |
| 1817 | |
| 1818 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 1819 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1820 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1821 | ** the null value. Strings are quoted if necessary. The separator |
| 1822 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1823 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1824 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1825 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1826 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1827 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1828 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1829 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1830 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1831 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1832 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1833 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1834 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1835 | i = 0; |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
| 1839 | if( i==0 ){ |
| 1840 | putc('"', out); |
| 1841 | for(i=0; z[i]; i++){ |
| 1842 | if( z[i]=='"' ) putc('"', out); |
| 1843 | putc(z[i], out); |
| 1844 | } |
| 1845 | putc('"', out); |
| 1846 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1847 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1848 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1849 | } |
| 1850 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1851 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1855 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1856 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1857 | ** This routine runs when the user presses Ctrl-C |
| 1858 | */ |
| 1859 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1860 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 1861 | seenInterrupt++; |
| 1862 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 1863 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1864 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1865 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1866 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1867 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1868 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1869 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1870 | ** invoked. It always returns SQLITE_OK. |
| 1871 | */ |
| 1872 | static int shellAuth( |
| 1873 | void *pClientData, |
| 1874 | int op, |
| 1875 | const char *zA1, |
| 1876 | const char *zA2, |
| 1877 | const char *zA3, |
| 1878 | const char *zA4 |
| 1879 | ){ |
| 1880 | ShellState *p = (ShellState*)pClientData; |
| 1881 | static const char *azAction[] = { 0, |
| 1882 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1883 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1884 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1885 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1886 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1887 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1888 | "PRAGMA", "READ", "SELECT", |
| 1889 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1890 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1891 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1892 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1893 | }; |
| 1894 | int i; |
| 1895 | const char *az[4]; |
| 1896 | az[0] = zA1; |
| 1897 | az[1] = zA2; |
| 1898 | az[2] = zA3; |
| 1899 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1900 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1901 | for(i=0; i<4; i++){ |
| 1902 | raw_printf(p->out, " "); |
| 1903 | if( az[i] ){ |
| 1904 | output_c_string(p->out, az[i]); |
| 1905 | }else{ |
| 1906 | raw_printf(p->out, "NULL"); |
| 1907 | } |
| 1908 | } |
| 1909 | raw_printf(p->out, "\n"); |
| 1910 | return SQLITE_OK; |
| 1911 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1912 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1913 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1914 | /* |
| 1915 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1916 | ** |
| 1917 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1918 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1919 | */ |
| 1920 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1921 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1922 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1923 | }else{ |
| 1924 | utf8_printf(out, "%s%s", z, zTail); |
| 1925 | } |
| 1926 | } |
| 1927 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1928 | char c = z[n]; |
| 1929 | z[n] = 0; |
| 1930 | printSchemaLine(out, z, zTail); |
| 1931 | z[n] = c; |
| 1932 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1935 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1936 | ** invokes for each row of a query result. |
| 1937 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1938 | static int shell_callback( |
| 1939 | void *pArg, |
| 1940 | int nArg, /* Number of result columns */ |
| 1941 | char **azArg, /* Text of each result column */ |
| 1942 | char **azCol, /* Column names */ |
| 1943 | int *aiType /* Column types */ |
| 1944 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1945 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1946 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1947 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1948 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1949 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1950 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1951 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1952 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1953 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1954 | if( len>w ) w = len; |
| 1955 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1956 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1957 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1958 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1959 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1960 | } |
| 1961 | break; |
| 1962 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1963 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1964 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1965 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1966 | const int *colWidth; |
| 1967 | int showHdr; |
| 1968 | char *rowSep; |
| 1969 | if( p->cMode==MODE_Column ){ |
| 1970 | colWidth = p->colWidth; |
| 1971 | showHdr = p->showHeader; |
| 1972 | rowSep = p->rowSeparator; |
| 1973 | }else{ |
| 1974 | colWidth = aExplainWidths; |
| 1975 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 1976 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1977 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1978 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1979 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1980 | int w, n; |
| 1981 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1982 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1983 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1984 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1985 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1986 | if( w==0 ){ |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 1987 | w = strlenChar(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1988 | if( w<10 ) w = 10; |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 1989 | n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1990 | if( w<n ) w = n; |
| 1991 | } |
| 1992 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1993 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1994 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1995 | if( showHdr ){ |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 1996 | utf8_width_print(p->out, w, azCol[i]); |
| 1997 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2000 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2001 | for(i=0; i<nArg; i++){ |
| 2002 | int w; |
| 2003 | if( i<ArraySize(p->actualWidth) ){ |
| 2004 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 2005 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2006 | }else{ |
| 2007 | w = 10; |
| 2008 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2009 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2010 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2011 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2012 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2013 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2014 | } |
| 2015 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2016 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2017 | for(i=0; i<nArg; i++){ |
| 2018 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2019 | if( i<ArraySize(p->actualWidth) ){ |
| 2020 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2021 | }else{ |
| 2022 | w = 10; |
| 2023 | } |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 2024 | if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ |
| 2025 | w = strlenChar(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2026 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2027 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2028 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2029 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2030 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2031 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2032 | } |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 2033 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 2034 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2035 | } |
| 2036 | break; |
| 2037 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2038 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2039 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2040 | break; |
| 2041 | } |
| 2042 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 2043 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 2044 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2045 | int nParen = 0; |
| 2046 | char cEnd = 0; |
| 2047 | char c; |
| 2048 | int nLine = 0; |
| 2049 | assert( nArg==1 ); |
| 2050 | if( azArg[0]==0 ) break; |
| 2051 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 2052 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 2053 | ){ |
| 2054 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 2055 | break; |
| 2056 | } |
| 2057 | z = sqlite3_mprintf("%s", azArg[0]); |
| 2058 | j = 0; |
| 2059 | for(i=0; IsSpace(z[i]); i++){} |
| 2060 | for(; (c = z[i])!=0; i++){ |
| 2061 | if( IsSpace(c) ){ |
| 2062 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 2063 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 2064 | j--; |
| 2065 | } |
| 2066 | z[j++] = c; |
| 2067 | } |
| 2068 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 2069 | z[j] = 0; |
| 2070 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 2071 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2072 | if( c==cEnd ){ |
| 2073 | cEnd = 0; |
| 2074 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 2075 | cEnd = c; |
| 2076 | }else if( c=='[' ){ |
| 2077 | cEnd = ']'; |
| 2078 | }else if( c=='(' ){ |
| 2079 | nParen++; |
| 2080 | }else if( c==')' ){ |
| 2081 | nParen--; |
| 2082 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2083 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2084 | j = 0; |
| 2085 | } |
| 2086 | } |
| 2087 | z[j++] = c; |
| 2088 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 2089 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2090 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2091 | j = 0; |
| 2092 | nLine++; |
| 2093 | while( IsSpace(z[i+1]) ){ i++; } |
| 2094 | } |
| 2095 | } |
| 2096 | z[j] = 0; |
| 2097 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2098 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2099 | sqlite3_free(z); |
| 2100 | break; |
| 2101 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2102 | case MODE_List: { |
| 2103 | if( p->cnt++==0 && p->showHeader ){ |
| 2104 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2105 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2106 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2107 | } |
| 2108 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2109 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2110 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2111 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2112 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2113 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2114 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2115 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2116 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2117 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2118 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2119 | } |
| 2120 | break; |
| 2121 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2122 | case MODE_Html: { |
| 2123 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2124 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2125 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2126 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2127 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2128 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2129 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2130 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2131 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2132 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2133 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2134 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2135 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2136 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2137 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2138 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2139 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2142 | case MODE_Tcl: { |
| 2143 | if( p->cnt++==0 && p->showHeader ){ |
| 2144 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2145 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2146 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2147 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2148 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2149 | } |
| 2150 | if( azArg==0 ) break; |
| 2151 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2152 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2153 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2154 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2155 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2158 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2159 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2160 | if( p->cnt++==0 && p->showHeader ){ |
| 2161 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2162 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2163 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2164 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2165 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 2166 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 2167 | for(i=0; i<nArg; i++){ |
| 2168 | output_csv(p, azArg[i], i<nArg-1); |
| 2169 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2170 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2171 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2172 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2173 | break; |
| 2174 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2175 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2176 | if( azArg==0 ) break; |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2177 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2178 | if( p->showHeader ){ |
| 2179 | raw_printf(p->out,"("); |
| 2180 | for(i=0; i<nArg; i++){ |
| 2181 | if( i>0 ) raw_printf(p->out, ","); |
| 2182 | if( quoteChar(azCol[i]) ){ |
| 2183 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 2184 | utf8_printf(p->out, "%s", z); |
| 2185 | sqlite3_free(z); |
| 2186 | }else{ |
| 2187 | raw_printf(p->out, "%s", azCol[i]); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2188 | } |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2189 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2190 | raw_printf(p->out,")"); |
| 2191 | } |
| 2192 | p->cnt++; |
| 2193 | for(i=0; i<nArg; i++){ |
| 2194 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2195 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2196 | utf8_printf(p->out,"NULL"); |
| 2197 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2198 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2199 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2200 | utf8_printf(p->out,"%s", azArg[i]); |
| 2201 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2202 | char z[50]; |
| 2203 | double r = sqlite3_column_double(p->pStmt, i); |
| 2204 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2205 | raw_printf(p->out, "%s", z); |
| 2206 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2207 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2208 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2209 | output_hex_blob(p->out, pBlob, nBlob); |
| 2210 | }else if( isNumber(azArg[i], 0) ){ |
| 2211 | utf8_printf(p->out,"%s", azArg[i]); |
| 2212 | }else{ |
| 2213 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2214 | } |
| 2215 | } |
| 2216 | raw_printf(p->out,");\n"); |
| 2217 | break; |
| 2218 | } |
| 2219 | case MODE_Quote: { |
| 2220 | if( azArg==0 ) break; |
| 2221 | if( p->cnt==0 && p->showHeader ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2222 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2223 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2224 | output_quoted_string(p->out, azCol[i]); |
| 2225 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2226 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2227 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2228 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2229 | for(i=0; i<nArg; i++){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2230 | if( i>0 ) raw_printf(p->out, ","); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2231 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2232 | utf8_printf(p->out,"NULL"); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2233 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2234 | output_quoted_string(p->out, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2235 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2236 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2237 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2238 | char z[50]; |
| 2239 | double r = sqlite3_column_double(p->pStmt, i); |
| 2240 | sqlite3_snprintf(50,z,"%!.20g", r); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2241 | raw_printf(p->out, "%s", z); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2242 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2243 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2244 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2245 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2246 | }else if( isNumber(azArg[i], 0) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2247 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2248 | }else{ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2249 | output_quoted_string(p->out, azArg[i]); |
| 2250 | } |
| 2251 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2252 | raw_printf(p->out,"\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2253 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2254 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2255 | case MODE_Ascii: { |
| 2256 | if( p->cnt++==0 && p->showHeader ){ |
| 2257 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2258 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2259 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2260 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2261 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2262 | } |
| 2263 | if( azArg==0 ) break; |
| 2264 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2265 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2266 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2267 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2268 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2269 | break; |
| 2270 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2271 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2276 | ** This is the callback routine that the SQLite library |
| 2277 | ** invokes for each row of a query result. |
| 2278 | */ |
| 2279 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2280 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2281 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2282 | } |
| 2283 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2284 | /* |
| 2285 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2286 | ** output onto the end of a ShellText object. |
| 2287 | */ |
| 2288 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2289 | ShellText *p = (ShellText*)pArg; |
| 2290 | int i; |
drh | 2fb79e9 | 2017-03-25 12:08:11 +0000 | [diff] [blame] | 2291 | UNUSED_PARAMETER(az); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2292 | if( p->n ) appendText(p, "|", 0); |
| 2293 | for(i=0; i<nArg; i++){ |
| 2294 | if( i ) appendText(p, ",", 0); |
| 2295 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2296 | } |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | ** Generate an appropriate SELFTEST table in the main database. |
| 2302 | */ |
| 2303 | static void createSelftestTable(ShellState *p){ |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2304 | char *zErrMsg = 0; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2305 | sqlite3_exec(p->db, |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2306 | "SAVEPOINT selftest_init;\n" |
| 2307 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2308 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2309 | " op TEXT,\n" /* Operator: memo run */ |
| 2310 | " cmd TEXT,\n" /* Command text */ |
| 2311 | " ans TEXT\n" /* Desired answer */ |
| 2312 | ");" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2313 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2314 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2315 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2316 | " 'memo','Tests generated by --init');\n" |
| 2317 | "INSERT INTO [_shell$self]\n" |
| 2318 | " SELECT 'run',\n" |
| 2319 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2320 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2321 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2322 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2323 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2324 | " SELECT 'run'," |
| 2325 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2326 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2327 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2328 | " FROM (\n" |
| 2329 | " SELECT name FROM sqlite_master\n" |
| 2330 | " WHERE type='table'\n" |
| 2331 | " AND name<>'selftest'\n" |
| 2332 | " AND coalesce(rootpage,0)>0\n" |
| 2333 | " )\n" |
| 2334 | " ORDER BY name;\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2335 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2336 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2337 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2338 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2339 | "DROP TABLE [_shell$self];" |
| 2340 | ,0,0,&zErrMsg); |
| 2341 | if( zErrMsg ){ |
| 2342 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2343 | sqlite3_free(zErrMsg); |
| 2344 | } |
| 2345 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2348 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2349 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2350 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2351 | ** the name of the table given. Escape any quote characters in the |
| 2352 | ** table name. |
| 2353 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2354 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2355 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2356 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2357 | char *z; |
| 2358 | |
| 2359 | if( p->zDestTable ){ |
| 2360 | free(p->zDestTable); |
| 2361 | p->zDestTable = 0; |
| 2362 | } |
| 2363 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2364 | cQuote = quoteChar(zName); |
| 2365 | n = strlen30(zName); |
drh | 45e7d7d | 2017-06-24 13:31:40 +0000 | [diff] [blame] | 2366 | if( cQuote ) n += n+2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2367 | z = p->zDestTable = malloc( n+1 ); |
| 2368 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2369 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2370 | exit(1); |
| 2371 | } |
| 2372 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2373 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2374 | for(i=0; zName[i]; i++){ |
| 2375 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2376 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2377 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2378 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2379 | z[n] = 0; |
| 2380 | } |
| 2381 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2382 | |
| 2383 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2384 | ** Execute a query statement that will generate SQL output. Print |
| 2385 | ** the result columns, comma-separated, on a line and then add a |
| 2386 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2387 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2388 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2389 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2390 | ** "--" comment occurs at the end of the statement, the comment |
| 2391 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2392 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2393 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2394 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2395 | const char *zSelect, /* SELECT statement to extract content */ |
| 2396 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2397 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2398 | sqlite3_stmt *pSelect; |
| 2399 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2400 | int nResult; |
| 2401 | int i; |
| 2402 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2403 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2404 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2405 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2406 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2407 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2408 | return rc; |
| 2409 | } |
| 2410 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2411 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2412 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2413 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2414 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2415 | zFirstRow = 0; |
| 2416 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2417 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2418 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2419 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2420 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2421 | } |
| 2422 | if( z==0 ) z = ""; |
| 2423 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2424 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2425 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2426 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2427 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2428 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2429 | rc = sqlite3_step(pSelect); |
| 2430 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2431 | rc = sqlite3_finalize(pSelect); |
| 2432 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2433 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2434 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2435 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2436 | } |
| 2437 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2440 | /* |
| 2441 | ** Allocate space and save off current error string. |
| 2442 | */ |
| 2443 | static char *save_err_msg( |
| 2444 | sqlite3 *db /* Database to query */ |
| 2445 | ){ |
| 2446 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2447 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2448 | if( zErrMsg ){ |
| 2449 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2450 | } |
| 2451 | return zErrMsg; |
| 2452 | } |
| 2453 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2454 | #ifdef __linux__ |
| 2455 | /* |
| 2456 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2457 | */ |
| 2458 | static void displayLinuxIoStats(FILE *out){ |
| 2459 | FILE *in; |
| 2460 | char z[200]; |
| 2461 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2462 | in = fopen(z, "rb"); |
| 2463 | if( in==0 ) return; |
| 2464 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2465 | static const struct { |
| 2466 | const char *zPattern; |
| 2467 | const char *zDesc; |
| 2468 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 2469 | { "rchar: ", "Bytes received by read():" }, |
| 2470 | { "wchar: ", "Bytes sent to write():" }, |
| 2471 | { "syscr: ", "Read() system calls:" }, |
| 2472 | { "syscw: ", "Write() system calls:" }, |
| 2473 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2474 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2475 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2476 | }; |
| 2477 | int i; |
| 2478 | for(i=0; i<ArraySize(aTrans); i++){ |
| 2479 | int n = (int)strlen(aTrans[i].zPattern); |
| 2480 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2481 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2487 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2488 | #endif |
| 2489 | |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2490 | /* |
| 2491 | ** Display a single line of status using 64-bit values. |
| 2492 | */ |
| 2493 | static void displayStatLine( |
| 2494 | ShellState *p, /* The shell context */ |
| 2495 | char *zLabel, /* Label for this one line */ |
| 2496 | char *zFormat, /* Format for the result */ |
| 2497 | int iStatusCtrl, /* Which status to display */ |
| 2498 | int bReset /* True to reset the stats */ |
| 2499 | ){ |
| 2500 | sqlite3_int64 iCur = -1; |
| 2501 | sqlite3_int64 iHiwtr = -1; |
| 2502 | int i, nPercent; |
| 2503 | char zLine[200]; |
| 2504 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2505 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2506 | if( zFormat[i]=='%' ) nPercent++; |
| 2507 | } |
| 2508 | if( nPercent>1 ){ |
| 2509 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2510 | }else{ |
| 2511 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2512 | } |
| 2513 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2514 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2515 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2516 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2517 | ** Display memory stats. |
| 2518 | */ |
| 2519 | static int display_stats( |
| 2520 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2521 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2522 | int bReset /* True to reset the stats */ |
| 2523 | ){ |
| 2524 | int iCur; |
| 2525 | int iHiwtr; |
| 2526 | |
| 2527 | if( pArg && pArg->out ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2528 | displayStatLine(pArg, "Memory Used:", |
| 2529 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2530 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2531 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2532 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2533 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2534 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2535 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2536 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2537 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2538 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2539 | displayStatLine(pArg, "Number of Scratch Allocations Used:", |
| 2540 | "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2541 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2542 | displayStatLine(pArg, "Number of Scratch Overflow Bytes:", |
| 2543 | "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset); |
| 2544 | displayStatLine(pArg, "Largest Allocation:", |
| 2545 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2546 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2547 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2548 | displayStatLine(pArg, "Largest Scratch Allocation:", |
| 2549 | "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2550 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2551 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2552 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2553 | #endif |
| 2554 | } |
| 2555 | |
| 2556 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2557 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2558 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2559 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2560 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2561 | raw_printf(pArg->out, |
| 2562 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2563 | iCur, iHiwtr); |
| 2564 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2565 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2566 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2567 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2568 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2569 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2570 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2571 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2572 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2573 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2574 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2575 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2576 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2577 | iHiwtr = iCur = -1; |
| 2578 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2579 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2580 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2581 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2582 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2583 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2584 | iHiwtr = iCur = -1; |
| 2585 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2586 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2587 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2588 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2589 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2590 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2591 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2592 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2593 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2594 | iHiwtr = iCur = -1; |
| 2595 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2596 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2597 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2601 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2602 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2603 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2604 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2605 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2606 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2607 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2608 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2609 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2612 | #ifdef __linux__ |
| 2613 | displayLinuxIoStats(pArg->out); |
| 2614 | #endif |
| 2615 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 2616 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2617 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2618 | return 0; |
| 2619 | } |
| 2620 | |
| 2621 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2622 | ** Display scan stats. |
| 2623 | */ |
| 2624 | static void display_scanstats( |
| 2625 | sqlite3 *db, /* Database to query */ |
| 2626 | ShellState *pArg /* Pointer to ShellState */ |
| 2627 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2628 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2629 | UNUSED_PARAMETER(db); |
| 2630 | UNUSED_PARAMETER(pArg); |
| 2631 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2632 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2633 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2634 | mx = 0; |
| 2635 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2636 | double rEstLoop = 1.0; |
| 2637 | for(i=n=0; 1; i++){ |
| 2638 | sqlite3_stmt *p = pArg->pStmt; |
| 2639 | sqlite3_int64 nLoop, nVisit; |
| 2640 | double rEst; |
| 2641 | int iSid; |
| 2642 | const char *zExplain; |
| 2643 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2644 | break; |
| 2645 | } |
| 2646 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2647 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2648 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2649 | if( n==0 ){ |
| 2650 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2651 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2652 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2653 | n++; |
| 2654 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2655 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2656 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2657 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2658 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2659 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2660 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 2661 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2662 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2663 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2664 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2665 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2666 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2670 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2671 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2672 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2673 | ** Otherwise, return zero. |
| 2674 | */ |
| 2675 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2676 | int i; |
| 2677 | for(i=0; azArray[i]; i++){ |
| 2678 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2679 | } |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2685 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2686 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2687 | ** |
| 2688 | ** The indenting rules are: |
| 2689 | ** |
| 2690 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2691 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2692 | ** itself by 2 spaces. |
| 2693 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2694 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2695 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 2696 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2697 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2698 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 2699 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2700 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2701 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2702 | const char *zSql; /* The text of the SQL statement */ |
| 2703 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2704 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2705 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2706 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2707 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 2708 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2709 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2710 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2711 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2712 | const char *azGoto[] = { "Goto", 0 }; |
| 2713 | |
| 2714 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2715 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2716 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2717 | p->cMode = p->mode; |
| 2718 | return; |
| 2719 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2720 | zSql = sqlite3_sql(pSql); |
| 2721 | if( zSql==0 ) return; |
| 2722 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2723 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2724 | p->cMode = p->mode; |
| 2725 | return; |
| 2726 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2727 | |
| 2728 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2729 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2730 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2731 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2732 | |
| 2733 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2734 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2735 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2736 | ** the current instruction is part of a sub-program generated by an |
| 2737 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2738 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2739 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2740 | |
| 2741 | /* Grow the p->aiIndent array as required */ |
| 2742 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2743 | if( iOp==0 ){ |
| 2744 | /* Do further verfication that this is explain output. Abort if |
| 2745 | ** it is not */ |
| 2746 | static const char *explainCols[] = { |
| 2747 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2748 | int jj; |
| 2749 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2750 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2751 | p->cMode = p->mode; |
| 2752 | sqlite3_reset(pSql); |
| 2753 | return; |
| 2754 | } |
| 2755 | } |
| 2756 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2757 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2758 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2759 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2760 | } |
| 2761 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2762 | p->aiIndent[iOp] = 0; |
| 2763 | p->nIndent = iOp+1; |
| 2764 | |
| 2765 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2766 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2767 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2768 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2769 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2770 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2771 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2775 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2776 | sqlite3_free(abYield); |
| 2777 | sqlite3_reset(pSql); |
| 2778 | } |
| 2779 | |
| 2780 | /* |
| 2781 | ** Free the array allocated by explain_data_prepare(). |
| 2782 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2783 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2784 | sqlite3_free(p->aiIndent); |
| 2785 | p->aiIndent = 0; |
| 2786 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2787 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2791 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2792 | */ |
| 2793 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2794 | extern int sqlite3SelectTrace; |
| 2795 | static int savedSelectTrace; |
| 2796 | #endif |
| 2797 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2798 | extern int sqlite3WhereTrace; |
| 2799 | static int savedWhereTrace; |
| 2800 | #endif |
| 2801 | static void disable_debug_trace_modes(void){ |
| 2802 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2803 | savedSelectTrace = sqlite3SelectTrace; |
| 2804 | sqlite3SelectTrace = 0; |
| 2805 | #endif |
| 2806 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2807 | savedWhereTrace = sqlite3WhereTrace; |
| 2808 | sqlite3WhereTrace = 0; |
| 2809 | #endif |
| 2810 | } |
| 2811 | static void restore_debug_trace_modes(void){ |
| 2812 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2813 | sqlite3SelectTrace = savedSelectTrace; |
| 2814 | #endif |
| 2815 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2816 | sqlite3WhereTrace = savedWhereTrace; |
| 2817 | #endif |
| 2818 | } |
| 2819 | |
| 2820 | /* |
| 2821 | ** Run a prepared statement |
| 2822 | */ |
| 2823 | static void exec_prepared_stmt( |
| 2824 | ShellState *pArg, /* Pointer to ShellState */ |
| 2825 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2826 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2827 | ){ |
| 2828 | int rc; |
| 2829 | |
| 2830 | /* perform the first step. this will tell us if we |
| 2831 | ** have a result set or not and how wide it is. |
| 2832 | */ |
| 2833 | rc = sqlite3_step(pStmt); |
| 2834 | /* if we have a result set... */ |
| 2835 | if( SQLITE_ROW == rc ){ |
| 2836 | /* if we have a callback... */ |
| 2837 | if( xCallback ){ |
| 2838 | /* allocate space for col name ptr, value ptr, and type */ |
| 2839 | int nCol = sqlite3_column_count(pStmt); |
| 2840 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2841 | if( !pData ){ |
| 2842 | rc = SQLITE_NOMEM; |
| 2843 | }else{ |
| 2844 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2845 | char **azVals = &azCols[nCol]; /* Results */ |
| 2846 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2847 | int i, x; |
| 2848 | assert(sizeof(int) <= sizeof(char *)); |
| 2849 | /* save off ptrs to column names */ |
| 2850 | for(i=0; i<nCol; i++){ |
| 2851 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2852 | } |
| 2853 | do{ |
| 2854 | /* extract the data and data types */ |
| 2855 | for(i=0; i<nCol; i++){ |
| 2856 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2857 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2858 | azVals[i] = ""; |
| 2859 | }else{ |
| 2860 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2861 | } |
| 2862 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2863 | rc = SQLITE_NOMEM; |
| 2864 | break; /* from for */ |
| 2865 | } |
| 2866 | } /* end for */ |
| 2867 | |
| 2868 | /* if data and types extracted successfully... */ |
| 2869 | if( SQLITE_ROW == rc ){ |
| 2870 | /* call the supplied callback with the result row data */ |
| 2871 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2872 | rc = SQLITE_ABORT; |
| 2873 | }else{ |
| 2874 | rc = sqlite3_step(pStmt); |
| 2875 | } |
| 2876 | } |
| 2877 | } while( SQLITE_ROW == rc ); |
| 2878 | sqlite3_free(pData); |
| 2879 | } |
| 2880 | }else{ |
| 2881 | do{ |
| 2882 | rc = sqlite3_step(pStmt); |
| 2883 | } while( rc == SQLITE_ROW ); |
| 2884 | } |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2889 | ** Execute a statement or set of statements. Print |
| 2890 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2891 | ** set via the supplied callback. |
| 2892 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2893 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2894 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2895 | ** and callback data argument. |
| 2896 | */ |
| 2897 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2898 | sqlite3 *db, /* An open database */ |
| 2899 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2900 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2901 | /* (not the same as sqlite3_exec) */ |
| 2902 | ShellState *pArg, /* Pointer to ShellState */ |
| 2903 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2904 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2905 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2906 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2907 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2908 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2909 | |
| 2910 | if( pzErrMsg ){ |
| 2911 | *pzErrMsg = NULL; |
| 2912 | } |
| 2913 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2914 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2915 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2916 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2917 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2918 | if( pzErrMsg ){ |
| 2919 | *pzErrMsg = save_err_msg(db); |
| 2920 | } |
| 2921 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2922 | if( !pStmt ){ |
| 2923 | /* this happens for a comment or white-space */ |
| 2924 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2925 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2926 | continue; |
| 2927 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2928 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 2929 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2930 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2931 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2932 | /* save off the prepared statment handle and reset row count */ |
| 2933 | if( pArg ){ |
| 2934 | pArg->pStmt = pStmt; |
| 2935 | pArg->cnt = 0; |
| 2936 | } |
| 2937 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2938 | /* echo the sql statement if echo on */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2939 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2940 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 2941 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2942 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2943 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2944 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2945 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2946 | char *zEQP; |
| 2947 | disable_debug_trace_modes(); |
| 2948 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2949 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2950 | if( rc==SQLITE_OK ){ |
| 2951 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2952 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2953 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2954 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2955 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | sqlite3_finalize(pExplain); |
| 2959 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2960 | if( pArg->autoEQP>=2 ){ |
| 2961 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2962 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2963 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2964 | if( rc==SQLITE_OK ){ |
| 2965 | pArg->cMode = MODE_Explain; |
| 2966 | explain_data_prepare(pArg, pExplain); |
| 2967 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2968 | explain_data_delete(pArg); |
| 2969 | } |
| 2970 | sqlite3_finalize(pExplain); |
| 2971 | sqlite3_free(zEQP); |
| 2972 | } |
| 2973 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2976 | if( pArg ){ |
| 2977 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2978 | if( pArg->autoExplain |
| 2979 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2980 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2981 | ){ |
| 2982 | pArg->cMode = MODE_Explain; |
| 2983 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2984 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2985 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2986 | ** data required to add indents to the output.*/ |
| 2987 | if( pArg->cMode==MODE_Explain ){ |
| 2988 | explain_data_prepare(pArg, pStmt); |
| 2989 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2992 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2993 | explain_data_delete(pArg); |
| 2994 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2995 | /* print usage stats if stats on */ |
| 2996 | if( pArg && pArg->statsOn ){ |
| 2997 | display_stats(db, pArg, 0); |
| 2998 | } |
| 2999 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3000 | /* print loop-counters if required */ |
| 3001 | if( pArg && pArg->scanstatsOn ){ |
| 3002 | display_scanstats(db, pArg); |
| 3003 | } |
| 3004 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3005 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3006 | ** copy of the error message. Otherwise, set zSql to point to the |
| 3007 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3008 | rc2 = sqlite3_finalize(pStmt); |
| 3009 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3010 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3011 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 3012 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3013 | }else if( pzErrMsg ){ |
| 3014 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3015 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* clear saved stmt handle */ |
| 3018 | if( pArg ){ |
| 3019 | pArg->pStmt = NULL; |
| 3020 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3021 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3022 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3023 | |
| 3024 | return rc; |
| 3025 | } |
| 3026 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3027 | /* |
| 3028 | ** Release memory previously allocated by tableColumnList(). |
| 3029 | */ |
| 3030 | static void freeColumnList(char **azCol){ |
| 3031 | int i; |
| 3032 | for(i=1; azCol[i]; i++){ |
| 3033 | sqlite3_free(azCol[i]); |
| 3034 | } |
| 3035 | /* azCol[0] is a static string */ |
| 3036 | sqlite3_free(azCol); |
| 3037 | } |
| 3038 | |
| 3039 | /* |
| 3040 | ** Return a list of pointers to strings which are the names of all |
| 3041 | ** columns in table zTab. The memory to hold the names is dynamically |
| 3042 | ** allocated and must be released by the caller using a subsequent call |
| 3043 | ** to freeColumnList(). |
| 3044 | ** |
| 3045 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 3046 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 3047 | ** name of the rowid column. |
| 3048 | ** |
| 3049 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 3050 | ** by an entry with azCol[i]==0. |
| 3051 | */ |
| 3052 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 3053 | char **azCol = 0; |
| 3054 | sqlite3_stmt *pStmt; |
| 3055 | char *zSql; |
| 3056 | int nCol = 0; |
| 3057 | int nAlloc = 0; |
| 3058 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 3059 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3060 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3061 | int rc; |
| 3062 | |
| 3063 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 3064 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3065 | sqlite3_free(zSql); |
| 3066 | if( rc ) return 0; |
| 3067 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3068 | if( nCol>=nAlloc-2 ){ |
| 3069 | nAlloc = nAlloc*2 + nCol + 10; |
| 3070 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 3071 | if( azCol==0 ){ |
| 3072 | raw_printf(stderr, "Error: out of memory\n"); |
| 3073 | exit(1); |
| 3074 | } |
| 3075 | } |
| 3076 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 3077 | if( sqlite3_column_int(pStmt, 5) ){ |
| 3078 | nPK++; |
| 3079 | if( nPK==1 |
| 3080 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3081 | "INTEGER")==0 |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3082 | ){ |
| 3083 | isIPK = 1; |
| 3084 | }else{ |
| 3085 | isIPK = 0; |
| 3086 | } |
| 3087 | } |
| 3088 | } |
| 3089 | sqlite3_finalize(pStmt); |
| 3090 | azCol[0] = 0; |
| 3091 | azCol[nCol+1] = 0; |
| 3092 | |
| 3093 | /* The decision of whether or not a rowid really needs to be preserved |
| 3094 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3095 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3096 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3097 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3098 | */ |
| 3099 | if( preserveRowid && isIPK ){ |
| 3100 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 3101 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 3102 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 3103 | ** ROWID aliases. To distinguish these cases, check to see if |
| 3104 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 3105 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 3106 | */ |
| 3107 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 3108 | " WHERE origin='pk'", zTab); |
| 3109 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3110 | sqlite3_free(zSql); |
| 3111 | if( rc ){ |
| 3112 | freeColumnList(azCol); |
| 3113 | return 0; |
| 3114 | } |
| 3115 | rc = sqlite3_step(pStmt); |
| 3116 | sqlite3_finalize(pStmt); |
| 3117 | preserveRowid = rc==SQLITE_ROW; |
| 3118 | } |
| 3119 | if( preserveRowid ){ |
| 3120 | /* Only preserve the rowid if we can find a name to use for the |
| 3121 | ** rowid */ |
| 3122 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3123 | int i, j; |
| 3124 | for(j=0; j<3; j++){ |
| 3125 | for(i=1; i<=nCol; i++){ |
| 3126 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3127 | } |
| 3128 | if( i>nCol ){ |
| 3129 | /* At this point, we know that azRowid[j] is not the name of any |
| 3130 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3131 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3132 | ** tables will fail this last check */ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3133 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3134 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3135 | break; |
| 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | return azCol; |
| 3140 | } |
| 3141 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3142 | /* |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3143 | ** Toggle the reverse_unordered_selects setting. |
| 3144 | */ |
| 3145 | static void toggleSelectOrder(sqlite3 *db){ |
| 3146 | sqlite3_stmt *pStmt = 0; |
| 3147 | int iSetting = 0; |
| 3148 | char zStmt[100]; |
| 3149 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3150 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3151 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3152 | } |
| 3153 | sqlite3_finalize(pStmt); |
| 3154 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3155 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3156 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3157 | } |
| 3158 | |
| 3159 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3160 | ** This is a different callback routine used for dumping the database. |
| 3161 | ** Each row received by this callback consists of a table name, |
| 3162 | ** the table type ("index" or "table") and SQL to create the table. |
| 3163 | ** This routine should print text sufficient to recreate the table. |
| 3164 | */ |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3165 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3166 | int rc; |
| 3167 | const char *zTable; |
| 3168 | const char *zType; |
| 3169 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3170 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3171 | |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3172 | UNUSED_PARAMETER(azNotUsed); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3173 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3174 | zTable = azArg[0]; |
| 3175 | zType = azArg[1]; |
| 3176 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3177 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3178 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3179 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 3180 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3181 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3182 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3183 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3184 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3185 | char *zIns; |
| 3186 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3187 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3188 | p->writableSchema = 1; |
| 3189 | } |
| 3190 | zIns = sqlite3_mprintf( |
| 3191 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3192 | "VALUES('table','%q','%q',0,'%q');", |
| 3193 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3194 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3195 | sqlite3_free(zIns); |
| 3196 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3197 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 3198 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 3199 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3200 | |
| 3201 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3202 | ShellText sSelect; |
| 3203 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3204 | char **azCol; |
| 3205 | int i; |
| 3206 | char *savedDestTable; |
| 3207 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3208 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3209 | azCol = tableColumnList(p, zTable); |
| 3210 | if( azCol==0 ){ |
| 3211 | p->nErr++; |
| 3212 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 3215 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3216 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3217 | initText(&sTable); |
| 3218 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3219 | /* If preserving the rowid, add a column list after the table name. |
| 3220 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3221 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3222 | */ |
| 3223 | if( azCol[0] ){ |
| 3224 | appendText(&sTable, "(", 0); |
| 3225 | appendText(&sTable, azCol[0], 0); |
| 3226 | for(i=1; azCol[i]; i++){ |
| 3227 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3228 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3229 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3230 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3231 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3232 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3233 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3234 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3235 | appendText(&sSelect, "SELECT ", 0); |
| 3236 | if( azCol[0] ){ |
| 3237 | appendText(&sSelect, azCol[0], 0); |
| 3238 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3239 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3240 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3241 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3242 | if( azCol[i+1] ){ |
| 3243 | appendText(&sSelect, ",", 0); |
| 3244 | } |
| 3245 | } |
| 3246 | freeColumnList(azCol); |
| 3247 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3248 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3249 | |
| 3250 | savedDestTable = p->zDestTable; |
| 3251 | savedMode = p->mode; |
| 3252 | p->zDestTable = sTable.z; |
| 3253 | p->mode = p->cMode = MODE_Insert; |
| 3254 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3255 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3256 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3257 | toggleSelectOrder(p->db); |
| 3258 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3259 | toggleSelectOrder(p->db); |
| 3260 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3261 | p->zDestTable = savedDestTable; |
| 3262 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3263 | freeText(&sTable); |
| 3264 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3265 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3266 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3267 | return 0; |
| 3268 | } |
| 3269 | |
| 3270 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3271 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3272 | ** the contents of the query are output as SQL statements. |
| 3273 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3274 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3275 | ** "ORDER BY rowid DESC" to the end. |
| 3276 | */ |
| 3277 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3278 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3279 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3280 | ){ |
| 3281 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3282 | char *zErr = 0; |
| 3283 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3284 | if( rc==SQLITE_CORRUPT ){ |
| 3285 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3286 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3287 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3288 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3289 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3290 | sqlite3_free(zErr); |
| 3291 | zErr = 0; |
| 3292 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3293 | zQ2 = malloc( len+100 ); |
| 3294 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 3295 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3296 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3297 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3298 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3299 | }else{ |
| 3300 | rc = SQLITE_CORRUPT; |
| 3301 | } |
| 3302 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3303 | free(zQ2); |
| 3304 | } |
| 3305 | return rc; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3309 | ** Text of a help message |
| 3310 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 3311 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3312 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3313 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3314 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3315 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3316 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3317 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 3318 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3319 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3320 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3321 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3322 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3323 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3324 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3325 | " If TABLE specified, only dump tables matching\n" |
| 3326 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3327 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3328 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3329 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3330 | ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3331 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3332 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3333 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3334 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3335 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3336 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3337 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3338 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3339 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3340 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3341 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3342 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3343 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3344 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 3345 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3346 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3347 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 3348 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3349 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 3350 | ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
danielk1977 | 6b77a36 | 2005-01-13 11:10:25 +0000 | [diff] [blame] | 3351 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3352 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 3353 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3354 | " column Left-aligned columns. (See .width)\n" |
| 3355 | " html HTML <table> code\n" |
| 3356 | " insert SQL insert statements for TABLE\n" |
| 3357 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 3358 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 3359 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3360 | " tabs Tab-separated values\n" |
| 3361 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3362 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3363 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3364 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n" |
| 3365 | " The --new option starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3366 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3367 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3368 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3369 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3370 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3371 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3372 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3373 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3374 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3375 | " Add --indent for pretty-printing\n" |
drh | a5d75ba | 2017-03-15 14:20:34 +0000 | [diff] [blame] | 3376 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3377 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3378 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3379 | #if defined(SQLITE_ENABLE_SESSION) |
| 3380 | ".session CMD ... Create or control sessions\n" |
| 3381 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3382 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3383 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3384 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3385 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3386 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3387 | ".tables ?TABLE? List names of tables\n" |
| 3388 | " If TABLE specified, only list tables matching\n" |
| 3389 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3390 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 3391 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3392 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3393 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 3394 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 3395 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3396 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 3397 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3398 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3399 | ; |
| 3400 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3401 | #if defined(SQLITE_ENABLE_SESSION) |
| 3402 | /* |
| 3403 | ** Print help information for the ".sessions" command |
| 3404 | */ |
| 3405 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 3406 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3407 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3408 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3409 | "Subcommands:\n" |
| 3410 | " attach TABLE Attach TABLE\n" |
| 3411 | " changeset FILE Write a changeset into FILE\n" |
| 3412 | " close Close one session\n" |
| 3413 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3414 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3415 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3416 | " isempty Query whether the session is empty\n" |
| 3417 | " list List currently open session names\n" |
| 3418 | " open DB NAME Open a new session on DB\n" |
| 3419 | " patchset FILE Write a patchset into FILE\n" |
| 3420 | ); |
| 3421 | } |
| 3422 | #endif |
| 3423 | |
| 3424 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3425 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3426 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3427 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3428 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3429 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3430 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3431 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3432 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3433 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3434 | ** read. |
| 3435 | ** |
| 3436 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3437 | ** from the file before the buffer is returned. This byte is not included in |
| 3438 | ** the final value of (*pnByte), if applicable. |
| 3439 | ** |
| 3440 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3441 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3442 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3443 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3444 | FILE *in = fopen(zName, "rb"); |
| 3445 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3446 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3447 | char *pBuf; |
| 3448 | if( in==0 ) return 0; |
| 3449 | fseek(in, 0, SEEK_END); |
| 3450 | nIn = ftell(in); |
| 3451 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3452 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3453 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3454 | nRead = fread(pBuf, nIn, 1, in); |
| 3455 | fclose(in); |
| 3456 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3457 | sqlite3_free(pBuf); |
| 3458 | return 0; |
| 3459 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3460 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3461 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3462 | return pBuf; |
| 3463 | } |
| 3464 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3465 | /* |
| 3466 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 3467 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 3468 | ** if the file does not exist or is unreadable. |
| 3469 | */ |
| 3470 | static void readfileFunc( |
| 3471 | sqlite3_context *context, |
| 3472 | int argc, |
| 3473 | sqlite3_value **argv |
| 3474 | ){ |
| 3475 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3476 | void *pBuf; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3477 | int nBuf; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3478 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3479 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3480 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 3481 | if( zName==0 ) return; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3482 | pBuf = readFile(zName, &nBuf); |
| 3483 | if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | /* |
| 3487 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3488 | ** is written into file X. The number of bytes written is returned. Or |
| 3489 | ** NULL is returned if something goes wrong, such as being unable to open |
| 3490 | ** file X for writing. |
| 3491 | */ |
| 3492 | static void writefileFunc( |
| 3493 | sqlite3_context *context, |
| 3494 | int argc, |
| 3495 | sqlite3_value **argv |
| 3496 | ){ |
| 3497 | FILE *out; |
| 3498 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3499 | sqlite3_int64 rc; |
| 3500 | const char *zFile; |
| 3501 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3502 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3503 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3504 | if( zFile==0 ) return; |
| 3505 | out = fopen(zFile, "wb"); |
| 3506 | if( out==0 ) return; |
| 3507 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 3508 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3509 | rc = 0; |
| 3510 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 3511 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3512 | } |
| 3513 | fclose(out); |
| 3514 | sqlite3_result_int64(context, rc); |
| 3515 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3516 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3517 | #if defined(SQLITE_ENABLE_SESSION) |
| 3518 | /* |
| 3519 | ** Close a single OpenSession object and release all of its associated |
| 3520 | ** resources. |
| 3521 | */ |
| 3522 | static void session_close(OpenSession *pSession){ |
| 3523 | int i; |
| 3524 | sqlite3session_delete(pSession->p); |
| 3525 | sqlite3_free(pSession->zName); |
| 3526 | for(i=0; i<pSession->nFilter; i++){ |
| 3527 | sqlite3_free(pSession->azFilter[i]); |
| 3528 | } |
| 3529 | sqlite3_free(pSession->azFilter); |
| 3530 | memset(pSession, 0, sizeof(OpenSession)); |
| 3531 | } |
| 3532 | #endif |
| 3533 | |
| 3534 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3535 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3536 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3537 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3538 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3539 | int i; |
| 3540 | for(i=0; i<p->nSession; i++){ |
| 3541 | session_close(&p->aSession[i]); |
| 3542 | } |
| 3543 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3544 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3545 | #else |
| 3546 | # define session_close_all(X) |
| 3547 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3548 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3549 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 3550 | ** Implementation of the xFilter function for an open session. Omit |
| 3551 | ** any tables named by ".session filter" but let all other table through. |
| 3552 | */ |
| 3553 | #if defined(SQLITE_ENABLE_SESSION) |
| 3554 | static int session_filter(void *pCtx, const char *zTab){ |
| 3555 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3556 | int i; |
| 3557 | for(i=0; i<pSession->nFilter; i++){ |
| 3558 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3559 | } |
| 3560 | return 1; |
| 3561 | } |
| 3562 | #endif |
| 3563 | |
| 3564 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3565 | ** Make sure the database is open. If it is not, then open it. If |
| 3566 | ** the database fails to open, print an error message and exit. |
| 3567 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3568 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3569 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 3570 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3571 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3572 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3573 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3574 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3575 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3576 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3577 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3578 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 3579 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3580 | sqlite3_enable_load_extension(p->db, 1); |
| 3581 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3582 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3583 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3584 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3585 | writefileFunc, 0, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3586 | sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3587 | sha3Func, 0, 0); |
| 3588 | sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3589 | sha3Func, 0, 0); |
| 3590 | sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3591 | sha3QueryFunc, 0, 0); |
| 3592 | sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3593 | sha3QueryFunc, 0, 0); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 3594 | sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0, |
| 3595 | shellAddSchemaName, 0, 0); |
| 3596 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3601 | ** Do C-language style dequoting. |
| 3602 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3603 | ** \a -> alarm |
| 3604 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3605 | ** \t -> tab |
| 3606 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3607 | ** \v -> vertical tab |
| 3608 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3609 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3610 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3611 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3612 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3613 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3614 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3615 | */ |
| 3616 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3617 | int i, j; |
| 3618 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3619 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3620 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 3621 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3622 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3623 | if( c=='a' ){ |
| 3624 | c = '\a'; |
| 3625 | }else if( c=='b' ){ |
| 3626 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3627 | }else if( c=='t' ){ |
| 3628 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3629 | }else if( c=='n' ){ |
| 3630 | c = '\n'; |
| 3631 | }else if( c=='v' ){ |
| 3632 | c = '\v'; |
| 3633 | }else if( c=='f' ){ |
| 3634 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3635 | }else if( c=='r' ){ |
| 3636 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3637 | }else if( c=='"' ){ |
| 3638 | c = '"'; |
| 3639 | }else if( c=='\'' ){ |
| 3640 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3641 | }else if( c=='\\' ){ |
| 3642 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3643 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 3644 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3645 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3646 | i++; |
| 3647 | c = (c<<3) + z[i] - '0'; |
| 3648 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3649 | i++; |
| 3650 | c = (c<<3) + z[i] - '0'; |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | } |
| 3655 | z[j] = c; |
| 3656 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3657 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3661 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 3662 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3663 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3664 | static int hexDigitValue(char c){ |
| 3665 | if( c>='0' && c<='9' ) return c - '0'; |
| 3666 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 3667 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 3668 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3672 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 3673 | */ |
| 3674 | static sqlite3_int64 integerValue(const char *zArg){ |
| 3675 | sqlite3_int64 v = 0; |
| 3676 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 3677 | { "KiB", 1024 }, |
| 3678 | { "MiB", 1024*1024 }, |
| 3679 | { "GiB", 1024*1024*1024 }, |
| 3680 | { "KB", 1000 }, |
| 3681 | { "MB", 1000000 }, |
| 3682 | { "GB", 1000000000 }, |
| 3683 | { "K", 1000 }, |
| 3684 | { "M", 1000000 }, |
| 3685 | { "G", 1000000000 }, |
| 3686 | }; |
| 3687 | int i; |
| 3688 | int isNeg = 0; |
| 3689 | if( zArg[0]=='-' ){ |
| 3690 | isNeg = 1; |
| 3691 | zArg++; |
| 3692 | }else if( zArg[0]=='+' ){ |
| 3693 | zArg++; |
| 3694 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3695 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3696 | int x; |
| 3697 | zArg += 2; |
| 3698 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 3699 | v = (v<<4) + x; |
| 3700 | zArg++; |
| 3701 | } |
| 3702 | }else{ |
| 3703 | while( IsDigit(zArg[0]) ){ |
| 3704 | v = v*10 + zArg[0] - '0'; |
| 3705 | zArg++; |
| 3706 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3707 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 3708 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3709 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 3710 | v *= aMult[i].iMult; |
| 3711 | break; |
| 3712 | } |
| 3713 | } |
| 3714 | return isNeg? -v : v; |
| 3715 | } |
| 3716 | |
| 3717 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3718 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3719 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3720 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3721 | static int booleanValue(const char *zArg){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3722 | int i; |
| 3723 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3724 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3725 | }else{ |
| 3726 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3727 | } |
| 3728 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3729 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3730 | return 1; |
| 3731 | } |
| 3732 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3733 | return 0; |
| 3734 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3735 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3736 | zArg); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | /* |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3741 | ** Set or clear a shell flag according to a boolean value. |
| 3742 | */ |
| 3743 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3744 | if( booleanValue(zArg) ){ |
| 3745 | ShellSetFlag(p, mFlag); |
| 3746 | }else{ |
| 3747 | ShellClearFlag(p, mFlag); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3752 | ** Close an output file, assuming it is not stderr or stdout |
| 3753 | */ |
| 3754 | static void output_file_close(FILE *f){ |
| 3755 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3756 | } |
| 3757 | |
| 3758 | /* |
| 3759 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3760 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3761 | ** filename is "off". |
| 3762 | */ |
| 3763 | static FILE *output_file_open(const char *zFile){ |
| 3764 | FILE *f; |
| 3765 | if( strcmp(zFile,"stdout")==0 ){ |
| 3766 | f = stdout; |
| 3767 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3768 | f = stderr; |
| 3769 | }else if( strcmp(zFile, "off")==0 ){ |
| 3770 | f = 0; |
| 3771 | }else{ |
| 3772 | f = fopen(zFile, "wb"); |
| 3773 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3774 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3775 | } |
| 3776 | } |
| 3777 | return f; |
| 3778 | } |
| 3779 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 3780 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3781 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3782 | /* |
| 3783 | ** A routine for handling output from sqlite3_trace(). |
| 3784 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3785 | static int sql_trace_callback( |
| 3786 | unsigned mType, |
| 3787 | void *pArg, |
| 3788 | void *pP, |
| 3789 | void *pX |
| 3790 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3791 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 3792 | UNUSED_PARAMETER(mType); |
| 3793 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3794 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3795 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3796 | int i = (int)strlen(z); |
| 3797 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3798 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3799 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3800 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3801 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3802 | #endif |
| 3803 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3804 | |
| 3805 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3806 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3807 | ** a useful spot to set a debugger breakpoint. |
| 3808 | */ |
| 3809 | static void test_breakpoint(void){ |
| 3810 | static int nCall = 0; |
| 3811 | nCall++; |
| 3812 | } |
| 3813 | |
| 3814 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3815 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3816 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3817 | typedef struct ImportCtx ImportCtx; |
| 3818 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3819 | const char *zFile; /* Name of the input file */ |
| 3820 | FILE *in; /* Read the CSV text from this input stream */ |
| 3821 | char *z; /* Accumulated text for a field */ |
| 3822 | int n; /* Number of bytes in z */ |
| 3823 | int nAlloc; /* Space allocated for z[] */ |
| 3824 | int nLine; /* Current line number */ |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame^] | 3825 | int bNotFirst; /* True if one or more bytes already read */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3826 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3827 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3828 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3829 | }; |
| 3830 | |
| 3831 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3832 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3833 | if( p->n+1>=p->nAlloc ){ |
| 3834 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3835 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3836 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3837 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3838 | exit(1); |
| 3839 | } |
| 3840 | } |
| 3841 | p->z[p->n++] = (char)c; |
| 3842 | } |
| 3843 | |
| 3844 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3845 | ** with the option of having a separator other than ",". |
| 3846 | ** |
| 3847 | ** + Input comes from p->in. |
| 3848 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3849 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3850 | ** + Use p->cSep as the column separator. The default is ",". |
| 3851 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3852 | ** + Keep track of the line number in p->nLine. |
| 3853 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3854 | ** EOF on end-of-file. |
| 3855 | ** + Report syntax errors on stderr |
| 3856 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3857 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3858 | int c; |
| 3859 | int cSep = p->cColSep; |
| 3860 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3861 | p->n = 0; |
| 3862 | c = fgetc(p->in); |
| 3863 | if( c==EOF || seenInterrupt ){ |
| 3864 | p->cTerm = EOF; |
| 3865 | return 0; |
| 3866 | } |
| 3867 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3868 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3869 | int startLine = p->nLine; |
| 3870 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3871 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3872 | while( 1 ){ |
| 3873 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3874 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3875 | if( c==cQuote ){ |
| 3876 | if( pc==cQuote ){ |
| 3877 | pc = 0; |
| 3878 | continue; |
| 3879 | } |
| 3880 | } |
| 3881 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3882 | || (c==rSep && pc==cQuote) |
| 3883 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3884 | || (c==EOF && pc==cQuote) |
| 3885 | ){ |
| 3886 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3887 | p->cTerm = c; |
| 3888 | break; |
| 3889 | } |
| 3890 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3891 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3892 | p->zFile, p->nLine, cQuote); |
| 3893 | } |
| 3894 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3895 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3896 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3897 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3900 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3901 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3902 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3903 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3904 | }else{ |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame^] | 3905 | /* If this is the first field being parsed and it begins with the |
| 3906 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 3907 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 3908 | import_append_char(p, c); |
| 3909 | c = fgetc(p->in); |
| 3910 | if( (c&0xff)==0xbb ){ |
| 3911 | import_append_char(p, c); |
| 3912 | c = fgetc(p->in); |
| 3913 | if( (c&0xff)==0xbf ){ |
| 3914 | p->bNotFirst = 1; |
| 3915 | p->n = 0; |
| 3916 | return csv_read_one_field(p); |
| 3917 | } |
| 3918 | } |
| 3919 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3920 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3921 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3922 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3923 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3924 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3925 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 3926 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3927 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3928 | p->cTerm = c; |
| 3929 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 3930 | if( p->z ) p->z[p->n] = 0; |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame^] | 3931 | p->bNotFirst = 1; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3932 | return p->z; |
| 3933 | } |
| 3934 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3935 | /* Read a single field of ASCII delimited text. |
| 3936 | ** |
| 3937 | ** + Input comes from p->in. |
| 3938 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3939 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3940 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3941 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3942 | ** + Keep track of the row number in p->nLine. |
| 3943 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3944 | ** EOF on end-of-file. |
| 3945 | ** + Report syntax errors on stderr |
| 3946 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3947 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3948 | int c; |
| 3949 | int cSep = p->cColSep; |
| 3950 | int rSep = p->cRowSep; |
| 3951 | p->n = 0; |
| 3952 | c = fgetc(p->in); |
| 3953 | if( c==EOF || seenInterrupt ){ |
| 3954 | p->cTerm = EOF; |
| 3955 | return 0; |
| 3956 | } |
| 3957 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3958 | import_append_char(p, c); |
| 3959 | c = fgetc(p->in); |
| 3960 | } |
| 3961 | if( c==rSep ){ |
| 3962 | p->nLine++; |
| 3963 | } |
| 3964 | p->cTerm = c; |
| 3965 | if( p->z ) p->z[p->n] = 0; |
| 3966 | return p->z; |
| 3967 | } |
| 3968 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3969 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3970 | ** Try to transfer data for table zTable. If an error is seen while |
| 3971 | ** moving forward, try to go backwards. The backwards movement won't |
| 3972 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3973 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3974 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3975 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3976 | sqlite3 *newDb, |
| 3977 | const char *zTable |
| 3978 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3979 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3980 | sqlite3_stmt *pInsert = 0; |
| 3981 | char *zQuery = 0; |
| 3982 | char *zInsert = 0; |
| 3983 | int rc; |
| 3984 | int i, j, n; |
| 3985 | int nTable = (int)strlen(zTable); |
| 3986 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3987 | int cnt = 0; |
| 3988 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3989 | |
| 3990 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3991 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3992 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3993 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3994 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3995 | zQuery); |
| 3996 | goto end_data_xfer; |
| 3997 | } |
| 3998 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3999 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4000 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4001 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4002 | goto end_data_xfer; |
| 4003 | } |
| 4004 | sqlite3_snprintf(200+nTable,zInsert, |
| 4005 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 4006 | i = (int)strlen(zInsert); |
| 4007 | for(j=1; j<n; j++){ |
| 4008 | memcpy(zInsert+i, ",?", 2); |
| 4009 | i += 2; |
| 4010 | } |
| 4011 | memcpy(zInsert+i, ");", 3); |
| 4012 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 4013 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4014 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4015 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 4016 | zQuery); |
| 4017 | goto end_data_xfer; |
| 4018 | } |
| 4019 | for(k=0; k<2; k++){ |
| 4020 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4021 | for(i=0; i<n; i++){ |
| 4022 | switch( sqlite3_column_type(pQuery, i) ){ |
| 4023 | case SQLITE_NULL: { |
| 4024 | sqlite3_bind_null(pInsert, i+1); |
| 4025 | break; |
| 4026 | } |
| 4027 | case SQLITE_INTEGER: { |
| 4028 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 4029 | break; |
| 4030 | } |
| 4031 | case SQLITE_FLOAT: { |
| 4032 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 4033 | break; |
| 4034 | } |
| 4035 | case SQLITE_TEXT: { |
| 4036 | sqlite3_bind_text(pInsert, i+1, |
| 4037 | (const char*)sqlite3_column_text(pQuery,i), |
| 4038 | -1, SQLITE_STATIC); |
| 4039 | break; |
| 4040 | } |
| 4041 | case SQLITE_BLOB: { |
| 4042 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 4043 | sqlite3_column_bytes(pQuery,i), |
| 4044 | SQLITE_STATIC); |
| 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4049 | rc = sqlite3_step(pInsert); |
| 4050 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4051 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4052 | sqlite3_errmsg(newDb)); |
| 4053 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4054 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4055 | cnt++; |
| 4056 | if( (cnt%spinRate)==0 ){ |
| 4057 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 4058 | fflush(stdout); |
| 4059 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4060 | } /* End while */ |
| 4061 | if( rc==SQLITE_DONE ) break; |
| 4062 | sqlite3_finalize(pQuery); |
| 4063 | sqlite3_free(zQuery); |
| 4064 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 4065 | zTable); |
| 4066 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4067 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4068 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4069 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4070 | } |
| 4071 | } /* End for(k=0...) */ |
| 4072 | |
| 4073 | end_data_xfer: |
| 4074 | sqlite3_finalize(pQuery); |
| 4075 | sqlite3_finalize(pInsert); |
| 4076 | sqlite3_free(zQuery); |
| 4077 | sqlite3_free(zInsert); |
| 4078 | } |
| 4079 | |
| 4080 | |
| 4081 | /* |
| 4082 | ** Try to transfer all rows of the schema that match zWhere. For |
| 4083 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4084 | ** If an error is encountered while moving forward through the |
| 4085 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4086 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4087 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4088 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4089 | sqlite3 *newDb, |
| 4090 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4091 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4092 | ){ |
| 4093 | sqlite3_stmt *pQuery = 0; |
| 4094 | char *zQuery = 0; |
| 4095 | int rc; |
| 4096 | const unsigned char *zName; |
| 4097 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4098 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4099 | |
| 4100 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4101 | " WHERE %s", zWhere); |
| 4102 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4103 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4104 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4105 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4106 | zQuery); |
| 4107 | goto end_schema_xfer; |
| 4108 | } |
| 4109 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4110 | zName = sqlite3_column_text(pQuery, 0); |
| 4111 | zSql = sqlite3_column_text(pQuery, 1); |
| 4112 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4113 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4114 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4115 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4116 | sqlite3_free(zErrMsg); |
| 4117 | zErrMsg = 0; |
| 4118 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4119 | if( xForEach ){ |
| 4120 | xForEach(p, newDb, (const char*)zName); |
| 4121 | } |
| 4122 | printf("done\n"); |
| 4123 | } |
| 4124 | if( rc!=SQLITE_DONE ){ |
| 4125 | sqlite3_finalize(pQuery); |
| 4126 | sqlite3_free(zQuery); |
| 4127 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4128 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4129 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4130 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4131 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4132 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4133 | zQuery); |
| 4134 | goto end_schema_xfer; |
| 4135 | } |
| 4136 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4137 | zName = sqlite3_column_text(pQuery, 0); |
| 4138 | zSql = sqlite3_column_text(pQuery, 1); |
| 4139 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4140 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4141 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4142 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4143 | sqlite3_free(zErrMsg); |
| 4144 | zErrMsg = 0; |
| 4145 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4146 | if( xForEach ){ |
| 4147 | xForEach(p, newDb, (const char*)zName); |
| 4148 | } |
| 4149 | printf("done\n"); |
| 4150 | } |
| 4151 | } |
| 4152 | end_schema_xfer: |
| 4153 | sqlite3_finalize(pQuery); |
| 4154 | sqlite3_free(zQuery); |
| 4155 | } |
| 4156 | |
| 4157 | /* |
| 4158 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4159 | ** as possible out of the main database (which might be corrupt) and write it |
| 4160 | ** into zNewDb. |
| 4161 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4162 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4163 | int rc; |
| 4164 | sqlite3 *newDb = 0; |
| 4165 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4166 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4167 | return; |
| 4168 | } |
| 4169 | rc = sqlite3_open(zNewDb, &newDb); |
| 4170 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4171 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4172 | sqlite3_errmsg(newDb)); |
| 4173 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4174 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4175 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4176 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4177 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4178 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4179 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4180 | } |
| 4181 | sqlite3_close(newDb); |
| 4182 | } |
| 4183 | |
| 4184 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4185 | ** Change the output file back to stdout |
| 4186 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4187 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4188 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4189 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4190 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4191 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4192 | }else{ |
| 4193 | output_file_close(p->out); |
| 4194 | } |
| 4195 | p->outfile[0] = 0; |
| 4196 | p->out = stdout; |
| 4197 | } |
| 4198 | |
| 4199 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4200 | ** Run an SQL command and return the single integer result. |
| 4201 | */ |
| 4202 | static int db_int(ShellState *p, const char *zSql){ |
| 4203 | sqlite3_stmt *pStmt; |
| 4204 | int res = 0; |
| 4205 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4206 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4207 | res = sqlite3_column_int(pStmt,0); |
| 4208 | } |
| 4209 | sqlite3_finalize(pStmt); |
| 4210 | return res; |
| 4211 | } |
| 4212 | |
| 4213 | /* |
| 4214 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4215 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4216 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4217 | return (a[0]<<8) + a[1]; |
| 4218 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4219 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4220 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4221 | } |
| 4222 | |
| 4223 | /* |
| 4224 | ** Implementation of the ".info" command. |
| 4225 | ** |
| 4226 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4227 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4228 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4229 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4230 | { "file change counter:", 24 }, |
| 4231 | { "database page count:", 28 }, |
| 4232 | { "freelist page count:", 36 }, |
| 4233 | { "schema cookie:", 40 }, |
| 4234 | { "schema format:", 44 }, |
| 4235 | { "default cache size:", 48 }, |
| 4236 | { "autovacuum top root:", 52 }, |
| 4237 | { "incremental vacuum:", 64 }, |
| 4238 | { "text encoding:", 56 }, |
| 4239 | { "user version:", 60 }, |
| 4240 | { "application id:", 68 }, |
| 4241 | { "software version:", 96 }, |
| 4242 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4243 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4244 | { "number of tables:", |
| 4245 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4246 | { "number of indexes:", |
| 4247 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4248 | { "number of triggers:", |
| 4249 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4250 | { "number of views:", |
| 4251 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4252 | { "schema size:", |
| 4253 | "SELECT total(length(sql)) FROM %s" }, |
| 4254 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 4255 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4256 | int i; |
| 4257 | char *zSchemaTab; |
| 4258 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 4259 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4260 | open_db(p, 0); |
| 4261 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4262 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4263 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 4264 | return 1; |
| 4265 | } |
| 4266 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 4267 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4268 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4269 | return 1; |
| 4270 | } |
| 4271 | i = get2byteInt(aHdr+16); |
| 4272 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4273 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4274 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4275 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4276 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4277 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4278 | int ofst = aField[i].ofst; |
| 4279 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4280 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4281 | switch( ofst ){ |
| 4282 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4283 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4284 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4285 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4286 | } |
| 4287 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4288 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4289 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4290 | if( zDb==0 ){ |
| 4291 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4292 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4293 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4294 | }else{ |
| 4295 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4296 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4297 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4298 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4299 | int val = db_int(p, zSql); |
| 4300 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4301 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4302 | } |
| 4303 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4304 | return 0; |
| 4305 | } |
| 4306 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4307 | /* |
| 4308 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4309 | */ |
| 4310 | static int shellDatabaseError(sqlite3 *db){ |
| 4311 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4312 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4313 | return 1; |
| 4314 | } |
| 4315 | |
| 4316 | /* |
| 4317 | ** Print an out-of-memory message to stderr and return 1. |
| 4318 | */ |
| 4319 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4320 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4321 | return 1; |
| 4322 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4323 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4324 | /* |
| 4325 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4326 | ** if they match and FALSE (0) if they do not match. |
| 4327 | ** |
| 4328 | ** Globbing rules: |
| 4329 | ** |
| 4330 | ** '*' Matches any sequence of zero or more characters. |
| 4331 | ** |
| 4332 | ** '?' Matches exactly one character. |
| 4333 | ** |
| 4334 | ** [...] Matches one character from the enclosed list of |
| 4335 | ** characters. |
| 4336 | ** |
| 4337 | ** [^...] Matches one character not in the enclosed list. |
| 4338 | ** |
| 4339 | ** '#' Matches any sequence of one or more digits with an |
| 4340 | ** optional + or - sign in front |
| 4341 | ** |
| 4342 | ** ' ' Any span of whitespace matches any other span of |
| 4343 | ** whitespace. |
| 4344 | ** |
| 4345 | ** Extra whitespace at the end of z[] is ignored. |
| 4346 | */ |
| 4347 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4348 | int c, c2; |
| 4349 | int invert; |
| 4350 | int seen; |
| 4351 | |
| 4352 | while( (c = (*(zGlob++)))!=0 ){ |
| 4353 | if( IsSpace(c) ){ |
| 4354 | if( !IsSpace(*z) ) return 0; |
| 4355 | while( IsSpace(*zGlob) ) zGlob++; |
| 4356 | while( IsSpace(*z) ) z++; |
| 4357 | }else if( c=='*' ){ |
| 4358 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4359 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4360 | } |
| 4361 | if( c==0 ){ |
| 4362 | return 1; |
| 4363 | }else if( c=='[' ){ |
| 4364 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4365 | z++; |
| 4366 | } |
| 4367 | return (*z)!=0; |
| 4368 | } |
| 4369 | while( (c2 = (*(z++)))!=0 ){ |
| 4370 | while( c2!=c ){ |
| 4371 | c2 = *(z++); |
| 4372 | if( c2==0 ) return 0; |
| 4373 | } |
| 4374 | if( testcase_glob(zGlob,z) ) return 1; |
| 4375 | } |
| 4376 | return 0; |
| 4377 | }else if( c=='?' ){ |
| 4378 | if( (*(z++))==0 ) return 0; |
| 4379 | }else if( c=='[' ){ |
| 4380 | int prior_c = 0; |
| 4381 | seen = 0; |
| 4382 | invert = 0; |
| 4383 | c = *(z++); |
| 4384 | if( c==0 ) return 0; |
| 4385 | c2 = *(zGlob++); |
| 4386 | if( c2=='^' ){ |
| 4387 | invert = 1; |
| 4388 | c2 = *(zGlob++); |
| 4389 | } |
| 4390 | if( c2==']' ){ |
| 4391 | if( c==']' ) seen = 1; |
| 4392 | c2 = *(zGlob++); |
| 4393 | } |
| 4394 | while( c2 && c2!=']' ){ |
| 4395 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4396 | c2 = *(zGlob++); |
| 4397 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4398 | prior_c = 0; |
| 4399 | }else{ |
| 4400 | if( c==c2 ){ |
| 4401 | seen = 1; |
| 4402 | } |
| 4403 | prior_c = c2; |
| 4404 | } |
| 4405 | c2 = *(zGlob++); |
| 4406 | } |
| 4407 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4408 | }else if( c=='#' ){ |
| 4409 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4410 | if( !IsDigit(z[0]) ) return 0; |
| 4411 | z++; |
| 4412 | while( IsDigit(z[0]) ){ z++; } |
| 4413 | }else{ |
| 4414 | if( c!=(*(z++)) ) return 0; |
| 4415 | } |
| 4416 | } |
| 4417 | while( IsSpace(*z) ){ z++; } |
| 4418 | return *z==0; |
| 4419 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4420 | |
| 4421 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4422 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4423 | ** Compare the string as a command-line option with either one or two |
| 4424 | ** initial "-" characters. |
| 4425 | */ |
| 4426 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4427 | if( zStr[0]!='-' ) return 0; |
| 4428 | zStr++; |
| 4429 | if( zStr[0]=='-' ) zStr++; |
| 4430 | return strcmp(zStr, zOpt)==0; |
| 4431 | } |
| 4432 | |
| 4433 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4434 | ** Delete a file. |
| 4435 | */ |
| 4436 | int shellDeleteFile(const char *zFilename){ |
| 4437 | int rc; |
| 4438 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4439 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4440 | rc = _wunlink(z); |
| 4441 | sqlite3_free(z); |
| 4442 | #else |
| 4443 | rc = unlink(zFilename); |
| 4444 | #endif |
| 4445 | return rc; |
| 4446 | } |
| 4447 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4448 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4449 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4450 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4451 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4452 | ** called with four arguments - the parent table name, the parent column name, |
| 4453 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4454 | ** |
| 4455 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4456 | ** |
| 4457 | ** If either of the named tables or columns do not exist, this function |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4458 | ** returns an empty string. An empty string is also returned if both tables |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4459 | ** and columns exist but have the same default collation sequence. Or, |
| 4460 | ** if both exist but the default collation sequences are different, this |
| 4461 | ** function returns the string " COLLATE <parent-collation>", where |
| 4462 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4463 | */ |
| 4464 | static void shellFkeyCollateClause( |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4465 | sqlite3_context *pCtx, |
| 4466 | int nVal, |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4467 | sqlite3_value **apVal |
| 4468 | ){ |
| 4469 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4470 | const char *zParent; |
| 4471 | const char *zParentCol; |
| 4472 | const char *zParentSeq; |
| 4473 | const char *zChild; |
| 4474 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4475 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4476 | int rc; |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4477 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4478 | assert( nVal==4 ); |
| 4479 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4480 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4481 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4482 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4483 | |
| 4484 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4485 | rc = sqlite3_table_column_metadata( |
| 4486 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4487 | ); |
| 4488 | if( rc==SQLITE_OK ){ |
| 4489 | rc = sqlite3_table_column_metadata( |
| 4490 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4491 | ); |
| 4492 | } |
| 4493 | |
| 4494 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4495 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4496 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4497 | sqlite3_free(z); |
| 4498 | } |
| 4499 | } |
| 4500 | |
| 4501 | |
| 4502 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4503 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4504 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4505 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4506 | ShellState *pState, /* Current shell tool state */ |
| 4507 | char **azArg, /* Array of arguments passed to dot command */ |
| 4508 | int nArg /* Number of entries in azArg[] */ |
| 4509 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4510 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4511 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4512 | int bVerbose = 0; /* If -verbose is present */ |
| 4513 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4514 | int i; /* To iterate through azArg[] */ |
| 4515 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4516 | int rc; /* Return code */ |
| 4517 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4518 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4519 | /* |
| 4520 | ** This SELECT statement returns one row for each foreign key constraint |
| 4521 | ** in the schema of the main database. The column values are: |
| 4522 | ** |
| 4523 | ** 0. The text of an SQL statement similar to: |
| 4524 | ** |
| 4525 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 4526 | ** |
| 4527 | ** This is the same SELECT that the foreign keys implementation needs |
| 4528 | ** to run internally on child tables. If there is an index that can |
| 4529 | ** be used to optimize this query, then it can also be used by the FK |
| 4530 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4531 | ** table. |
| 4532 | ** |
| 4533 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4534 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4535 | ** contains an index that can be used to optimize the query. |
| 4536 | ** |
| 4537 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4538 | ** |
| 4539 | ** "child_table(child_key1, child_key2)" |
| 4540 | ** |
| 4541 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4542 | ** |
| 4543 | ** "parent_table(parent_key1, parent_key2)" |
| 4544 | ** |
| 4545 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4546 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4547 | ** |
| 4548 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4549 | ** |
| 4550 | ** 5. The name of the parent table. |
| 4551 | ** |
| 4552 | ** These six values are used by the C logic below to generate the report. |
| 4553 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4554 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4555 | "SELECT " |
| 4556 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 4557 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4558 | " || fkey_collate_clause(" |
| 4559 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4560 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4561 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4562 | " || group_concat('*=?', ' AND ') || ')'" |
| 4563 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4564 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4565 | ", " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4566 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4567 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4568 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4569 | " || ' ON ' || quote(s.name) || '('" |
| 4570 | " || group_concat(quote(f.[from]) ||" |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4571 | " fkey_collate_clause(" |
| 4572 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4573 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4574 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4575 | " f.[table] " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4576 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4577 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4578 | "GROUP BY s.name, f.id " |
| 4579 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4580 | ; |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4581 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4582 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4583 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 4584 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4585 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4586 | bVerbose = 1; |
| 4587 | } |
| 4588 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4589 | bGroupByParent = 1; |
| 4590 | zIndent = " "; |
| 4591 | } |
| 4592 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4593 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4594 | azArg[0], azArg[1] |
| 4595 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4596 | return SQLITE_ERROR; |
| 4597 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4598 | } |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4599 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4600 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4601 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4602 | 0, shellFkeyCollateClause, 0, 0 |
| 4603 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4604 | |
| 4605 | |
| 4606 | if( rc==SQLITE_OK ){ |
| 4607 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4608 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4609 | if( rc==SQLITE_OK ){ |
| 4610 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4611 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4612 | |
| 4613 | if( rc==SQLITE_OK ){ |
| 4614 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4615 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4616 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4617 | int res = -1; |
| 4618 | sqlite3_stmt *pExplain = 0; |
| 4619 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4620 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4621 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4622 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4623 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4624 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4625 | |
| 4626 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4627 | if( rc!=SQLITE_OK ) break; |
| 4628 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4629 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4630 | res = ( |
| 4631 | 0==sqlite3_strglob(zGlob, zPlan) |
| 4632 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 4633 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4634 | } |
| 4635 | rc = sqlite3_finalize(pExplain); |
| 4636 | if( rc!=SQLITE_OK ) break; |
| 4637 | |
| 4638 | if( res<0 ){ |
| 4639 | raw_printf(stderr, "Error: internal error"); |
| 4640 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4641 | }else{ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4642 | if( bGroupByParent |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4643 | && (bVerbose || res==0) |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4644 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4645 | ){ |
| 4646 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4647 | sqlite3_free(zPrev); |
| 4648 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4649 | } |
| 4650 | |
| 4651 | if( res==0 ){ |
| 4652 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4653 | }else if( bVerbose ){ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4654 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4655 | zIndent, zFrom, zTarget |
| 4656 | ); |
| 4657 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4658 | } |
| 4659 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4660 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4661 | |
| 4662 | if( rc!=SQLITE_OK ){ |
| 4663 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4664 | } |
| 4665 | |
| 4666 | rc2 = sqlite3_finalize(pSql); |
| 4667 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4668 | rc = rc2; |
| 4669 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4670 | } |
| 4671 | }else{ |
| 4672 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4673 | } |
| 4674 | |
| 4675 | return rc; |
| 4676 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4677 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4678 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4679 | ** Implementation of ".lint" dot command. |
| 4680 | */ |
| 4681 | static int lintDotCommand( |
| 4682 | ShellState *pState, /* Current shell tool state */ |
| 4683 | char **azArg, /* Array of arguments passed to dot command */ |
| 4684 | int nArg /* Number of entries in azArg[] */ |
| 4685 | ){ |
| 4686 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4687 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4688 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4689 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4690 | |
| 4691 | usage: |
| 4692 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4693 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4694 | raw_printf(stderr, " fkey-indexes\n"); |
| 4695 | return SQLITE_ERROR; |
| 4696 | } |
| 4697 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4698 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4699 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4700 | ** If an input line begins with "." then invoke this routine to |
| 4701 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4702 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4703 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4704 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4705 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4706 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4707 | int nArg = 0; |
| 4708 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4709 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4710 | char *azArg[50]; |
| 4711 | |
| 4712 | /* Parse the input line into tokens. |
| 4713 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4714 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 4715 | while( IsSpace(zLine[h]) ){ h++; } |
| 4716 | if( zLine[h]==0 ) break; |
| 4717 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 4718 | int delim = zLine[h++]; |
| 4719 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4720 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4721 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4722 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4723 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4724 | if( zLine[h]==delim ){ |
| 4725 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4726 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4727 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4728 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4729 | azArg[nArg++] = &zLine[h]; |
| 4730 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 4731 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4732 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | /* Process the input line. |
| 4737 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4738 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4739 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4740 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4741 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4742 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4743 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 4744 | if( nArg!=2 ){ |
| 4745 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 4746 | rc = 1; |
| 4747 | goto meta_command_exit; |
| 4748 | } |
| 4749 | open_db(p, 0); |
| 4750 | if( booleanValue(azArg[1]) ){ |
| 4751 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 4752 | }else{ |
| 4753 | sqlite3_set_authorizer(p->db, 0, 0); |
| 4754 | } |
| 4755 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4756 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4757 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4758 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 4759 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 4760 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4761 | const char *zDestFile = 0; |
| 4762 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4763 | sqlite3 *pDest; |
| 4764 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4765 | int j; |
| 4766 | for(j=1; j<nArg; j++){ |
| 4767 | const char *z = azArg[j]; |
| 4768 | if( z[0]=='-' ){ |
| 4769 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4770 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4771 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4772 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4773 | return 1; |
| 4774 | } |
| 4775 | }else if( zDestFile==0 ){ |
| 4776 | zDestFile = azArg[j]; |
| 4777 | }else if( zDb==0 ){ |
| 4778 | zDb = zDestFile; |
| 4779 | zDestFile = azArg[j]; |
| 4780 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4781 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4782 | return 1; |
| 4783 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4784 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4785 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4786 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4787 | return 1; |
| 4788 | } |
| 4789 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4790 | rc = sqlite3_open(zDestFile, &pDest); |
| 4791 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4792 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4793 | sqlite3_close(pDest); |
| 4794 | return 1; |
| 4795 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4796 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4797 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 4798 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4799 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4800 | sqlite3_close(pDest); |
| 4801 | return 1; |
| 4802 | } |
| 4803 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 4804 | sqlite3_backup_finish(pBackup); |
| 4805 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4806 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4807 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4808 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4809 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4810 | } |
| 4811 | sqlite3_close(pDest); |
| 4812 | }else |
| 4813 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4814 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 4815 | if( nArg==2 ){ |
| 4816 | bail_on_error = booleanValue(azArg[1]); |
| 4817 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4818 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4819 | rc = 1; |
| 4820 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 4821 | }else |
| 4822 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4823 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 4824 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4825 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4826 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4827 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4828 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4829 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4830 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4831 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4832 | rc = 1; |
| 4833 | } |
| 4834 | }else |
| 4835 | |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 4836 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 4837 | if( nArg==2 ){ |
| 4838 | #if defined(_WIN32) || defined(WIN32) |
| 4839 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 4840 | rc = !SetCurrentDirectoryW(z); |
| 4841 | sqlite3_free(z); |
| 4842 | #else |
| 4843 | rc = chdir(azArg[1]); |
| 4844 | #endif |
| 4845 | if( rc ){ |
| 4846 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 4847 | rc = 1; |
| 4848 | } |
| 4849 | }else{ |
| 4850 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 4851 | rc = 1; |
| 4852 | } |
| 4853 | }else |
| 4854 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4855 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 4856 | ** routine named test_breakpoint(). |
| 4857 | */ |
| 4858 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 4859 | test_breakpoint(); |
| 4860 | }else |
| 4861 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4862 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 4863 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4864 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4865 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4866 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4867 | rc = 1; |
| 4868 | } |
| 4869 | }else |
| 4870 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4871 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 4872 | ** Then read the content of the testcase-out.txt file and compare against |
| 4873 | ** azArg[1]. If there are differences, report an error and exit. |
| 4874 | */ |
| 4875 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 4876 | char *zRes = 0; |
| 4877 | output_reset(p); |
| 4878 | if( nArg!=2 ){ |
| 4879 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4880 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4881 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4882 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 4883 | rc = 2; |
| 4884 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4885 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4886 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 4887 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4888 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4889 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4890 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4891 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4892 | } |
| 4893 | sqlite3_free(zRes); |
| 4894 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4895 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4896 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 4897 | if( nArg==2 ){ |
| 4898 | tryToClone(p, azArg[1]); |
| 4899 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4900 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4901 | rc = 1; |
| 4902 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4903 | }else |
| 4904 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4905 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4906 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4907 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4908 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4909 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4910 | data.showHeader = 0; |
| 4911 | data.cMode = data.mode = MODE_List; |
| 4912 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 4913 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4914 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 4915 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4916 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4917 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4918 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4919 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4920 | } |
| 4921 | }else |
| 4922 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4923 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 4924 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 4925 | }else |
| 4926 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4927 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4928 | const char *zLike = 0; |
| 4929 | int i; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4930 | int savedShowHeader = p->showHeader; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4931 | ShellClearFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4932 | for(i=1; i<nArg; i++){ |
| 4933 | if( azArg[i][0]=='-' ){ |
| 4934 | const char *z = azArg[i]+1; |
| 4935 | if( z[0]=='-' ) z++; |
| 4936 | if( strcmp(z,"preserve-rowids")==0 ){ |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4937 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 4938 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 4939 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 4940 | rc = 1; |
| 4941 | goto meta_command_exit; |
| 4942 | #else |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4943 | ShellSetFlag(p, SHFLG_PreserveRowid); |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4944 | #endif |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4945 | }else |
| 4946 | { |
| 4947 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4948 | rc = 1; |
| 4949 | goto meta_command_exit; |
| 4950 | } |
| 4951 | }else if( zLike ){ |
| 4952 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 4953 | rc = 1; |
| 4954 | goto meta_command_exit; |
| 4955 | }else{ |
| 4956 | zLike = azArg[i]; |
| 4957 | } |
| 4958 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4959 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 4960 | /* When playing back a "dump", the content might appear in an order |
| 4961 | ** which causes immediate foreign key constraints to be violated. |
| 4962 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4963 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 4964 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4965 | p->writableSchema = 0; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4966 | p->showHeader = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4967 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 4968 | ** as much of the schema as it can even if the sqlite_master table is |
| 4969 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4970 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4971 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4972 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4973 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4974 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4975 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4976 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4977 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4978 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4979 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4980 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4981 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4982 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 4983 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4984 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4985 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4986 | char *zSql; |
| 4987 | zSql = sqlite3_mprintf( |
| 4988 | "SELECT name, type, sql FROM sqlite_master " |
| 4989 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 4990 | " AND sql NOT NULL", zLike); |
| 4991 | run_schema_dump_query(p,zSql); |
| 4992 | sqlite3_free(zSql); |
| 4993 | zSql = sqlite3_mprintf( |
| 4994 | "SELECT sql FROM sqlite_master " |
| 4995 | "WHERE sql NOT NULL" |
| 4996 | " AND type IN ('index','trigger','view')" |
| 4997 | " AND tbl_name LIKE %Q", zLike); |
| 4998 | run_table_dump_query(p, zSql, 0); |
| 4999 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 5000 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 5001 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5002 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 5003 | p->writableSchema = 0; |
| 5004 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 5005 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5006 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5007 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 5008 | p->showHeader = savedShowHeader; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 5009 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5010 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5011 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 5012 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 5013 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5014 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5015 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5016 | rc = 1; |
| 5017 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5018 | }else |
| 5019 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5020 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 5021 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5022 | if( strcmp(azArg[1],"full")==0 ){ |
| 5023 | p->autoEQP = 2; |
| 5024 | }else{ |
| 5025 | p->autoEQP = booleanValue(azArg[1]); |
| 5026 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5027 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5028 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5029 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5030 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 5031 | }else |
| 5032 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 5033 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5034 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5035 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5036 | }else |
| 5037 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5038 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5039 | int val = 1; |
| 5040 | if( nArg>=2 ){ |
| 5041 | if( strcmp(azArg[1],"auto")==0 ){ |
| 5042 | val = 99; |
| 5043 | }else{ |
| 5044 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5045 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5046 | } |
| 5047 | if( val==1 && p->mode!=MODE_Explain ){ |
| 5048 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 5049 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5050 | p->autoExplain = 0; |
| 5051 | }else if( val==0 ){ |
| 5052 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5053 | p->autoExplain = 0; |
| 5054 | }else if( val==99 ){ |
| 5055 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5056 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5057 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5058 | }else |
| 5059 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5060 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5061 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5062 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5063 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5064 | memcpy(&data, p, sizeof(data)); |
| 5065 | data.showHeader = 0; |
| 5066 | data.cMode = data.mode = MODE_Semi; |
| 5067 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 5068 | data.cMode = data.mode = MODE_Pretty; |
| 5069 | nArg = 1; |
| 5070 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5071 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5072 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5073 | rc = 1; |
| 5074 | goto meta_command_exit; |
| 5075 | } |
| 5076 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5077 | rc = sqlite3_exec(p->db, |
| 5078 | "SELECT sql FROM" |
| 5079 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 5080 | " FROM sqlite_master UNION ALL" |
| 5081 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5082 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5083 | "ORDER BY rowid", |
| 5084 | callback, &data, &zErrMsg |
| 5085 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5086 | if( rc==SQLITE_OK ){ |
| 5087 | sqlite3_stmt *pStmt; |
| 5088 | rc = sqlite3_prepare_v2(p->db, |
| 5089 | "SELECT rowid FROM sqlite_master" |
| 5090 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 5091 | -1, &pStmt, 0); |
| 5092 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 5093 | sqlite3_finalize(pStmt); |
| 5094 | } |
| 5095 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5096 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5097 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5098 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5099 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 5100 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5101 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5102 | data.zDestTable = "sqlite_stat1"; |
| 5103 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 5104 | shell_callback, &data,&zErrMsg); |
| 5105 | data.zDestTable = "sqlite_stat3"; |
| 5106 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 5107 | shell_callback, &data,&zErrMsg); |
| 5108 | data.zDestTable = "sqlite_stat4"; |
| 5109 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 5110 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5111 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5112 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5113 | }else |
| 5114 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5115 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 5116 | if( nArg==2 ){ |
| 5117 | p->showHeader = booleanValue(azArg[1]); |
| 5118 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5119 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5120 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 5121 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5122 | }else |
| 5123 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5124 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5125 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5126 | }else |
| 5127 | |
| 5128 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5129 | char *zTable; /* Insert data into this table */ |
| 5130 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5131 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5132 | int nCol; /* Number of columns in the table */ |
| 5133 | int nByte; /* Number of bytes in an SQL string */ |
| 5134 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 5135 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5136 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5137 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5138 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 5139 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 5140 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5141 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5142 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5143 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5144 | goto meta_command_exit; |
| 5145 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5146 | zFile = azArg[1]; |
| 5147 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5148 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5149 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5150 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5151 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5152 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5153 | raw_printf(stderr, |
| 5154 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5155 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5156 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5157 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5158 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5159 | " for import\n"); |
| 5160 | return 1; |
| 5161 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5162 | nSep = strlen30(p->rowSeparator); |
| 5163 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5164 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5165 | return 1; |
| 5166 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5167 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 5168 | /* When importing CSV (only), if the row separator is set to the |
| 5169 | ** default output row separator, change it to the default input |
| 5170 | ** row separator. This avoids having to maintain different input |
| 5171 | ** and output row separators. */ |
| 5172 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 5173 | nSep = strlen30(p->rowSeparator); |
| 5174 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5175 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5176 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5177 | " for import\n"); |
| 5178 | return 1; |
| 5179 | } |
| 5180 | sCtx.zFile = zFile; |
| 5181 | sCtx.nLine = 1; |
| 5182 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5183 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5184 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5185 | return 1; |
| 5186 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5187 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 5188 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5189 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5190 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5191 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5192 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5193 | xCloser = fclose; |
| 5194 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5195 | if( p->mode==MODE_Ascii ){ |
| 5196 | xRead = ascii_read_one_field; |
| 5197 | }else{ |
| 5198 | xRead = csv_read_one_field; |
| 5199 | } |
| 5200 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5201 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5202 | return 1; |
| 5203 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5204 | sCtx.cColSep = p->colSeparator[0]; |
| 5205 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 5206 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5207 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5208 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5209 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5210 | return 1; |
| 5211 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5212 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5213 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5214 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5215 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5216 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 5217 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5218 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 5219 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5220 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5221 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5222 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5223 | if( cSep=='(' ){ |
| 5224 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5225 | sqlite3_free(sCtx.z); |
| 5226 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5227 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5228 | return 1; |
| 5229 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5230 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 5231 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 5232 | sqlite3_free(zCreate); |
| 5233 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5234 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5235 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5236 | sqlite3_free(sCtx.z); |
| 5237 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5238 | return 1; |
| 5239 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5240 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5241 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5242 | sqlite3_free(zSql); |
| 5243 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5244 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5245 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5246 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5247 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5248 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5249 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5250 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5251 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5252 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5253 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5254 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5255 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5256 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5257 | return 1; |
| 5258 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5259 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5260 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5261 | for(i=1; i<nCol; i++){ |
| 5262 | zSql[j++] = ','; |
| 5263 | zSql[j++] = '?'; |
| 5264 | } |
| 5265 | zSql[j++] = ')'; |
| 5266 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5267 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5268 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5269 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5270 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5271 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5272 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5273 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5274 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5275 | needCommit = sqlite3_get_autocommit(p->db); |
| 5276 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5277 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5278 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5279 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5280 | char *z = xRead(&sCtx); |
| 5281 | /* |
| 5282 | ** Did we reach end-of-file before finding any columns? |
| 5283 | ** If so, stop instead of NULL filling the remaining columns. |
| 5284 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5285 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5286 | /* |
| 5287 | ** Did we reach end-of-file OR end-of-line before finding any |
| 5288 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 5289 | ** the remaining columns. |
| 5290 | */ |
| 5291 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5292 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5293 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5294 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5295 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5296 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 5297 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 5298 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 5299 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5300 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5301 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5302 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5303 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5304 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5305 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5306 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5307 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5308 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5309 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5310 | if( i>=nCol ){ |
| 5311 | sqlite3_step(pStmt); |
| 5312 | rc = sqlite3_reset(pStmt); |
| 5313 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5314 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 5315 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5318 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5319 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5320 | xCloser(sCtx.in); |
| 5321 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5322 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5323 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5324 | }else |
| 5325 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 5326 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5327 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 5328 | char *zSql; |
| 5329 | char *zCollist = 0; |
| 5330 | sqlite3_stmt *pStmt; |
| 5331 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5332 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5333 | if( nArg!=3 ){ |
| 5334 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 5335 | rc = 1; |
| 5336 | goto meta_command_exit; |
| 5337 | } |
| 5338 | open_db(p, 0); |
| 5339 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 5340 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 5341 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5342 | sqlite3_free(zSql); |
| 5343 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5344 | tnum = sqlite3_column_int(pStmt, 0); |
| 5345 | } |
| 5346 | sqlite3_finalize(pStmt); |
| 5347 | if( tnum==0 ){ |
| 5348 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 5349 | rc = 1; |
| 5350 | goto meta_command_exit; |
| 5351 | } |
| 5352 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 5353 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5354 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5355 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5356 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5357 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5358 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5359 | i++; |
| 5360 | if( zCol==0 ){ |
| 5361 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 5362 | zCol = "_ROWID_"; |
| 5363 | }else{ |
| 5364 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 5365 | zCol = zLabel; |
| 5366 | } |
| 5367 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5368 | if( zCollist==0 ){ |
| 5369 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 5370 | }else{ |
| 5371 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 5372 | } |
| 5373 | } |
| 5374 | sqlite3_finalize(pStmt); |
| 5375 | zSql = sqlite3_mprintf( |
| 5376 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 5377 | azArg[2], zCollist, zCollist); |
| 5378 | sqlite3_free(zCollist); |
| 5379 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 5380 | if( rc==SQLITE_OK ){ |
| 5381 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5382 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 5383 | if( rc ){ |
| 5384 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 5385 | }else{ |
| 5386 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5387 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5388 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 5389 | ); |
| 5390 | } |
| 5391 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5392 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5393 | rc = 1; |
| 5394 | } |
| 5395 | sqlite3_free(zSql); |
| 5396 | }else |
| 5397 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 5398 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5399 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5400 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 5401 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5402 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 5403 | iotrace = 0; |
| 5404 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5405 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5406 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5407 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5408 | iotrace = stdout; |
| 5409 | }else{ |
| 5410 | iotrace = fopen(azArg[1], "w"); |
| 5411 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5412 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5413 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5414 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5415 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5416 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5417 | } |
| 5418 | } |
| 5419 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5420 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5421 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5422 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 5423 | static const struct { |
| 5424 | const char *zLimitName; /* Name of a limit */ |
| 5425 | int limitCode; /* Integer code for that limit */ |
| 5426 | } aLimit[] = { |
| 5427 | { "length", SQLITE_LIMIT_LENGTH }, |
| 5428 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 5429 | { "column", SQLITE_LIMIT_COLUMN }, |
| 5430 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5431 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5432 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 5433 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5434 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 5435 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5436 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 5437 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 5438 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 5439 | }; |
| 5440 | int i, n2; |
| 5441 | open_db(p, 0); |
| 5442 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5443 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5444 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5445 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 5446 | } |
| 5447 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5448 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5449 | rc = 1; |
| 5450 | goto meta_command_exit; |
| 5451 | }else{ |
| 5452 | int iLimit = -1; |
| 5453 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5454 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5455 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 5456 | if( iLimit<0 ){ |
| 5457 | iLimit = i; |
| 5458 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5459 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5460 | rc = 1; |
| 5461 | goto meta_command_exit; |
| 5462 | } |
| 5463 | } |
| 5464 | } |
| 5465 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5466 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5467 | "enter \".limits\" with no arguments for a list.\n", |
| 5468 | azArg[1]); |
| 5469 | rc = 1; |
| 5470 | goto meta_command_exit; |
| 5471 | } |
| 5472 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 5473 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 5474 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5475 | } |
| 5476 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 5477 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 5478 | } |
| 5479 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5480 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5481 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 5482 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5483 | lintDotCommand(p, azArg, nArg); |
| 5484 | }else |
| 5485 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5486 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5487 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5488 | const char *zFile, *zProc; |
| 5489 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5490 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5491 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5492 | rc = 1; |
| 5493 | goto meta_command_exit; |
| 5494 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5495 | zFile = azArg[1]; |
| 5496 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5497 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5498 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 5499 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5500 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5501 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5502 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5503 | } |
| 5504 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5505 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5506 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5507 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 5508 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5509 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5510 | rc = 1; |
| 5511 | }else{ |
| 5512 | const char *zFile = azArg[1]; |
| 5513 | output_file_close(p->pLog); |
| 5514 | p->pLog = output_file_open(zFile); |
| 5515 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5516 | }else |
| 5517 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5518 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 5519 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 5520 | int n2 = (int)strlen(zMode); |
| 5521 | int c2 = zMode[0]; |
| 5522 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5523 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5524 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5525 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5526 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5527 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5528 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5529 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5530 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 5531 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5532 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5533 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5534 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5535 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5536 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5537 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5538 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 5539 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5540 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5541 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5542 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5543 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5544 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5545 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 5546 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5547 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 5548 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 5549 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5550 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5551 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5552 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5553 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5554 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5555 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 5556 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5557 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5558 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5559 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5560 | }else |
| 5561 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5562 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 5563 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5564 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 5565 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5566 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5567 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 5568 | rc = 1; |
| 5569 | } |
| 5570 | }else |
| 5571 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5572 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5573 | char *zNewFilename; /* Name of the database file to open */ |
| 5574 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5575 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5576 | /* Close the existing database */ |
| 5577 | session_close_all(p); |
| 5578 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5579 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 5580 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5581 | sqlite3_free(p->zFreeOnClose); |
| 5582 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5583 | /* Check for command-line arguments */ |
| 5584 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 5585 | const char *z = azArg[iName]; |
| 5586 | if( optionMatch(z,"new") ){ |
| 5587 | newFlag = 1; |
| 5588 | }else if( z[0]=='-' ){ |
| 5589 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 5590 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5591 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5592 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5593 | } |
| 5594 | /* If a filename is specified, try to open it first */ |
| 5595 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 5596 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5597 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5598 | p->zDbFilename = zNewFilename; |
| 5599 | open_db(p, 1); |
| 5600 | if( p->db==0 ){ |
| 5601 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 5602 | sqlite3_free(zNewFilename); |
| 5603 | }else{ |
| 5604 | p->zFreeOnClose = zNewFilename; |
| 5605 | } |
| 5606 | } |
| 5607 | if( p->db==0 ){ |
| 5608 | /* As a fall-back open a TEMP database */ |
| 5609 | p->zDbFilename = 0; |
| 5610 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5611 | } |
| 5612 | }else |
| 5613 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5614 | if( c=='o' |
| 5615 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 5616 | ){ |
| 5617 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 5618 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5619 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5620 | rc = 1; |
| 5621 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5622 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5623 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 5624 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5625 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5626 | rc = 1; |
| 5627 | goto meta_command_exit; |
| 5628 | } |
| 5629 | p->outCount = 2; |
| 5630 | }else{ |
| 5631 | p->outCount = 0; |
| 5632 | } |
| 5633 | output_reset(p); |
| 5634 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5635 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5636 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5637 | rc = 1; |
| 5638 | p->out = stdout; |
| 5639 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5640 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5641 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5642 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5643 | p->out = stdout; |
| 5644 | rc = 1; |
| 5645 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5646 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5647 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5648 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5649 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5650 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5651 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5652 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5653 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 5654 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5655 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5656 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5657 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5658 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5659 | } |
| 5660 | } |
| 5661 | }else |
| 5662 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5663 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 5664 | int i; |
| 5665 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5666 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5667 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5668 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5669 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5670 | }else |
| 5671 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5672 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5673 | if( nArg >= 2) { |
| 5674 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 5675 | } |
| 5676 | if( nArg >= 3) { |
| 5677 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 5678 | } |
| 5679 | }else |
| 5680 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5681 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5682 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5683 | }else |
| 5684 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5685 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 5686 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5687 | if( nArg!=2 ){ |
| 5688 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5689 | rc = 1; |
| 5690 | goto meta_command_exit; |
| 5691 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5692 | alt = fopen(azArg[1], "rb"); |
| 5693 | if( alt==0 ){ |
| 5694 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 5695 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5696 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5697 | rc = process_input(p, alt); |
| 5698 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5699 | } |
| 5700 | }else |
| 5701 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5702 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5703 | const char *zSrcFile; |
| 5704 | const char *zDb; |
| 5705 | sqlite3 *pSrc; |
| 5706 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5707 | int nTimeout = 0; |
| 5708 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5709 | if( nArg==2 ){ |
| 5710 | zSrcFile = azArg[1]; |
| 5711 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5712 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5713 | zSrcFile = azArg[2]; |
| 5714 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5715 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5716 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5717 | rc = 1; |
| 5718 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5719 | } |
| 5720 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 5721 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5722 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5723 | sqlite3_close(pSrc); |
| 5724 | return 1; |
| 5725 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5726 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5727 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 5728 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5729 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5730 | sqlite3_close(pSrc); |
| 5731 | return 1; |
| 5732 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5733 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 5734 | || rc==SQLITE_BUSY ){ |
| 5735 | if( rc==SQLITE_BUSY ){ |
| 5736 | if( nTimeout++ >= 3 ) break; |
| 5737 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5738 | } |
| 5739 | } |
| 5740 | sqlite3_backup_finish(pBackup); |
| 5741 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5742 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5743 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5744 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5745 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5746 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5747 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5748 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5749 | } |
| 5750 | sqlite3_close(pSrc); |
| 5751 | }else |
| 5752 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5753 | |
| 5754 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 5755 | if( nArg==2 ){ |
| 5756 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5757 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5758 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5759 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5760 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5761 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5762 | rc = 1; |
| 5763 | } |
| 5764 | }else |
| 5765 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5766 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5767 | ShellText sSelect; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5768 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5769 | char *zErrMsg = 0; |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5770 | const char *zDiv = 0; |
| 5771 | int iSchema = 0; |
| 5772 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5773 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5774 | memcpy(&data, p, sizeof(data)); |
| 5775 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5776 | data.cMode = data.mode = MODE_Semi; |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5777 | initText(&sSelect); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5778 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 5779 | data.cMode = data.mode = MODE_Pretty; |
| 5780 | nArg--; |
| 5781 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 5782 | } |
| 5783 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5784 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5785 | for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5786 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5787 | char *new_argv[2], *new_colv[2]; |
| 5788 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 5789 | " type text,\n" |
| 5790 | " name text,\n" |
| 5791 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 5792 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5793 | " sql text\n" |
| 5794 | ")"; |
| 5795 | new_argv[1] = 0; |
| 5796 | new_colv[0] = "sql"; |
| 5797 | new_colv[1] = 0; |
| 5798 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5799 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5800 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5801 | char *new_argv[2], *new_colv[2]; |
| 5802 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 5803 | " type text,\n" |
| 5804 | " name text,\n" |
| 5805 | " tbl_name text,\n" |
| 5806 | " rootpage integer,\n" |
| 5807 | " sql text\n" |
| 5808 | ")"; |
| 5809 | new_argv[1] = 0; |
| 5810 | new_colv[0] = "sql"; |
| 5811 | new_colv[1] = 0; |
| 5812 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5813 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5814 | }else{ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5815 | zDiv = "("; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5816 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5817 | }else if( nArg==1 ){ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5818 | zDiv = "("; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5819 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5820 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5821 | rc = 1; |
| 5822 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5823 | } |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5824 | if( zDiv ){ |
| 5825 | sqlite3_stmt *pStmt = 0; |
| 5826 | sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 5827 | -1, &pStmt, 0); |
| 5828 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 5829 | iSchema = 0; |
| 5830 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5831 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 5832 | char zScNum[30]; |
| 5833 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 5834 | appendText(&sSelect, zDiv, 0); |
| 5835 | zDiv = " UNION ALL "; |
| 5836 | if( strcmp(zDb, "main")!=0 ){ |
| 5837 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
drh | 90cdec0 | 2017-06-15 13:07:56 +0000 | [diff] [blame] | 5838 | appendText(&sSelect, zDb, '"'); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5839 | appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0); |
| 5840 | appendText(&sSelect, zScNum, 0); |
| 5841 | appendText(&sSelect, " AS snum, ", 0); |
| 5842 | appendText(&sSelect, zDb, '\''); |
| 5843 | appendText(&sSelect, " AS sname FROM ", 0); |
drh | 90cdec0 | 2017-06-15 13:07:56 +0000 | [diff] [blame] | 5844 | appendText(&sSelect, zDb, '"'); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 5845 | appendText(&sSelect, ".sqlite_master", 0); |
| 5846 | }else{ |
| 5847 | appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0); |
| 5848 | appendText(&sSelect, zScNum, 0); |
| 5849 | appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0); |
| 5850 | } |
| 5851 | } |
| 5852 | sqlite3_finalize(pStmt); |
| 5853 | appendText(&sSelect, ") WHERE ", 0); |
| 5854 | if( nArg>1 ){ |
| 5855 | char *zQarg = sqlite3_mprintf("%Q", azArg[1]); |
| 5856 | if( strchr(azArg[1], '.') ){ |
| 5857 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 5858 | }else{ |
| 5859 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 5860 | } |
| 5861 | appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0); |
| 5862 | appendText(&sSelect, zQarg, 0); |
| 5863 | appendText(&sSelect, " AND ", 0); |
| 5864 | sqlite3_free(zQarg); |
| 5865 | } |
| 5866 | appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" |
| 5867 | " ORDER BY snum, rowid", 0); |
| 5868 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 5869 | freeText(&sSelect); |
| 5870 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5871 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5872 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5873 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5874 | rc = 1; |
| 5875 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5876 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5877 | rc = 1; |
| 5878 | }else{ |
| 5879 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5880 | } |
| 5881 | }else |
| 5882 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5883 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 5884 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 5885 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5886 | }else |
| 5887 | #endif |
| 5888 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5889 | #if defined(SQLITE_ENABLE_SESSION) |
| 5890 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 5891 | OpenSession *pSession = &p->aSession[0]; |
| 5892 | char **azCmd = &azArg[1]; |
| 5893 | int iSes = 0; |
| 5894 | int nCmd = nArg - 1; |
| 5895 | int i; |
| 5896 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5897 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5898 | if( nArg>=3 ){ |
| 5899 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 5900 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 5901 | } |
| 5902 | if( iSes<p->nSession ){ |
| 5903 | pSession = &p->aSession[iSes]; |
| 5904 | azCmd++; |
| 5905 | nCmd--; |
| 5906 | }else{ |
| 5907 | pSession = &p->aSession[0]; |
| 5908 | iSes = 0; |
| 5909 | } |
| 5910 | } |
| 5911 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5912 | /* .session attach TABLE |
| 5913 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 5914 | ** table so that it is never filtered. |
| 5915 | */ |
| 5916 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 5917 | if( nCmd!=2 ) goto session_syntax_error; |
| 5918 | if( pSession->p==0 ){ |
| 5919 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5920 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5921 | }else{ |
| 5922 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 5923 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5924 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5925 | rc = 0; |
| 5926 | } |
| 5927 | } |
| 5928 | }else |
| 5929 | |
| 5930 | /* .session changeset FILE |
| 5931 | ** .session patchset FILE |
| 5932 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 5933 | */ |
| 5934 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 5935 | FILE *out = 0; |
| 5936 | if( nCmd!=2 ) goto session_syntax_error; |
| 5937 | if( pSession->p==0 ) goto session_not_open; |
| 5938 | out = fopen(azCmd[1], "wb"); |
| 5939 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5940 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5941 | }else{ |
| 5942 | int szChng; |
| 5943 | void *pChng; |
| 5944 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5945 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5946 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5947 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 5948 | } |
| 5949 | if( rc ){ |
| 5950 | printf("Error: error code %d\n", rc); |
| 5951 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5952 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5953 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5954 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5955 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5956 | szChng); |
| 5957 | } |
| 5958 | sqlite3_free(pChng); |
| 5959 | fclose(out); |
| 5960 | } |
| 5961 | }else |
| 5962 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5963 | /* .session close |
| 5964 | ** Close the identified session |
| 5965 | */ |
| 5966 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5967 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5968 | if( p->nSession ){ |
| 5969 | session_close(pSession); |
| 5970 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 5971 | } |
| 5972 | }else |
| 5973 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5974 | /* .session enable ?BOOLEAN? |
| 5975 | ** Query or set the enable flag |
| 5976 | */ |
| 5977 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 5978 | int ii; |
| 5979 | if( nCmd>2 ) goto session_syntax_error; |
| 5980 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5981 | if( p->nSession ){ |
| 5982 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5983 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 5984 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5985 | } |
| 5986 | }else |
| 5987 | |
| 5988 | /* .session filter GLOB .... |
| 5989 | ** Set a list of GLOB patterns of table names to be excluded. |
| 5990 | */ |
| 5991 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 5992 | int ii, nByte; |
| 5993 | if( nCmd<2 ) goto session_syntax_error; |
| 5994 | if( p->nSession ){ |
| 5995 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 5996 | sqlite3_free(pSession->azFilter[ii]); |
| 5997 | } |
| 5998 | sqlite3_free(pSession->azFilter); |
| 5999 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 6000 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 6001 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6002 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6003 | exit(1); |
| 6004 | } |
| 6005 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6006 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6007 | } |
| 6008 | pSession->nFilter = ii-1; |
| 6009 | } |
| 6010 | }else |
| 6011 | |
| 6012 | /* .session indirect ?BOOLEAN? |
| 6013 | ** Query or set the indirect flag |
| 6014 | */ |
| 6015 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 6016 | int ii; |
| 6017 | if( nCmd>2 ) goto session_syntax_error; |
| 6018 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6019 | if( p->nSession ){ |
| 6020 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6021 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 6022 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6023 | } |
| 6024 | }else |
| 6025 | |
| 6026 | /* .session isempty |
| 6027 | ** Determine if the session is empty |
| 6028 | */ |
| 6029 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 6030 | int ii; |
| 6031 | if( nCmd!=1 ) goto session_syntax_error; |
| 6032 | if( p->nSession ){ |
| 6033 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6034 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 6035 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6036 | } |
| 6037 | }else |
| 6038 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6039 | /* .session list |
| 6040 | ** List all currently open sessions |
| 6041 | */ |
| 6042 | if( strcmp(azCmd[0],"list")==0 ){ |
| 6043 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6044 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6045 | } |
| 6046 | }else |
| 6047 | |
| 6048 | /* .session open DB NAME |
| 6049 | ** Open a new session called NAME on the attached database DB. |
| 6050 | ** DB is normally "main". |
| 6051 | */ |
| 6052 | if( strcmp(azCmd[0],"open")==0 ){ |
| 6053 | char *zName; |
| 6054 | if( nCmd!=3 ) goto session_syntax_error; |
| 6055 | zName = azCmd[2]; |
| 6056 | if( zName[0]==0 ) goto session_syntax_error; |
| 6057 | for(i=0; i<p->nSession; i++){ |
| 6058 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6059 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6060 | goto meta_command_exit; |
| 6061 | } |
| 6062 | } |
| 6063 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6064 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6065 | goto meta_command_exit; |
| 6066 | } |
| 6067 | pSession = &p->aSession[p->nSession]; |
| 6068 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 6069 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6070 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6071 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6072 | goto meta_command_exit; |
| 6073 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6074 | pSession->nFilter = 0; |
| 6075 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6076 | p->nSession++; |
| 6077 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 6078 | }else |
| 6079 | /* If no command name matches, show a syntax error */ |
| 6080 | session_syntax_error: |
| 6081 | session_help(p); |
| 6082 | }else |
| 6083 | #endif |
| 6084 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6085 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6086 | /* Undocumented commands for internal testing. Subject to change |
| 6087 | ** without notice. */ |
| 6088 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 6089 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 6090 | int i, v; |
| 6091 | for(i=1; i<nArg; i++){ |
| 6092 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6093 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6094 | } |
| 6095 | } |
| 6096 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 6097 | int i; sqlite3_int64 v; |
| 6098 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6099 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6100 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6101 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6102 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6103 | } |
| 6104 | } |
| 6105 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6106 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6107 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6108 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 6109 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 6110 | int bVerbose = 0; /* Verbose output */ |
| 6111 | int bSelftestExists; /* True if SELFTEST already exists */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6112 | int i, k; /* Loop counters */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6113 | int nTest = 0; /* Number of tests runs */ |
| 6114 | int nErr = 0; /* Number of errors seen */ |
| 6115 | ShellText str; /* Answer for a query */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6116 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6117 | |
| 6118 | open_db(p,0); |
| 6119 | for(i=1; i<nArg; i++){ |
| 6120 | const char *z = azArg[i]; |
| 6121 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 6122 | if( strcmp(z,"-init")==0 ){ |
| 6123 | bIsInit = 1; |
| 6124 | }else |
| 6125 | if( strcmp(z,"-v")==0 ){ |
| 6126 | bVerbose++; |
| 6127 | }else |
| 6128 | { |
| 6129 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 6130 | azArg[i], azArg[0]); |
| 6131 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 6132 | rc = 1; |
| 6133 | goto meta_command_exit; |
| 6134 | } |
| 6135 | } |
| 6136 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 6137 | != SQLITE_OK ){ |
| 6138 | bSelftestExists = 0; |
| 6139 | }else{ |
| 6140 | bSelftestExists = 1; |
| 6141 | } |
| 6142 | if( bIsInit ){ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6143 | createSelftestTable(p); |
| 6144 | bSelftestExists = 1; |
| 6145 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6146 | initText(&str); |
| 6147 | appendText(&str, "x", 0); |
| 6148 | for(k=bSelftestExists; k>=0; k--){ |
| 6149 | if( k==1 ){ |
| 6150 | rc = sqlite3_prepare_v2(p->db, |
| 6151 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 6152 | -1, &pStmt, 0); |
| 6153 | }else{ |
| 6154 | rc = sqlite3_prepare_v2(p->db, |
| 6155 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 6156 | " (1,'run','PRAGMA integrity_check','ok')", |
| 6157 | -1, &pStmt, 0); |
| 6158 | } |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6159 | if( rc ){ |
| 6160 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 6161 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6162 | sqlite3_finalize(pStmt); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6163 | goto meta_command_exit; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6164 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6165 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 6166 | int tno = sqlite3_column_int(pStmt, 0); |
| 6167 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 6168 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 6169 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6170 | |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6171 | k = 0; |
| 6172 | if( bVerbose>0 ){ |
| 6173 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 6174 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 6175 | sqlite3_free(zQuote); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6176 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6177 | if( strcmp(zOp,"memo")==0 ){ |
| 6178 | utf8_printf(p->out, "%s\n", zSql); |
| 6179 | }else |
| 6180 | if( strcmp(zOp,"run")==0 ){ |
| 6181 | char *zErrMsg = 0; |
| 6182 | str.n = 0; |
| 6183 | str.z[0] = 0; |
| 6184 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 6185 | nTest++; |
| 6186 | if( bVerbose ){ |
| 6187 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 6188 | } |
| 6189 | if( rc || zErrMsg ){ |
| 6190 | nErr++; |
| 6191 | rc = 1; |
| 6192 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 6193 | sqlite3_free(zErrMsg); |
| 6194 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 6195 | nErr++; |
| 6196 | rc = 1; |
| 6197 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 6198 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 6199 | } |
| 6200 | }else |
| 6201 | { |
| 6202 | utf8_printf(stderr, |
| 6203 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6204 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6205 | break; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6206 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6207 | } /* End loop over rows of content from SELFTEST */ |
| 6208 | sqlite3_finalize(pStmt); |
| 6209 | } /* End loop over k */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6210 | freeText(&str); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6211 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 6212 | }else |
| 6213 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6214 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6215 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6216 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6217 | rc = 1; |
| 6218 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6219 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6220 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 6221 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6222 | } |
| 6223 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6224 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 6225 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6226 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6227 | }else |
| 6228 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6229 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 6230 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 6231 | int i; /* Loop counter */ |
| 6232 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6233 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6234 | int iSize = 224; /* Hash algorithm to use */ |
| 6235 | int bDebug = 0; /* Only show the query that would have run */ |
| 6236 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 6237 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6238 | char *zSep; /* Separator */ |
| 6239 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6240 | ShellText sQuery; /* Set of queries used to read all content */ |
drh | f80d4ff | 2017-03-08 18:06:20 +0000 | [diff] [blame] | 6241 | open_db(p, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6242 | for(i=1; i<nArg; i++){ |
| 6243 | const char *z = azArg[i]; |
| 6244 | if( z[0]=='-' ){ |
| 6245 | z++; |
| 6246 | if( z[0]=='-' ) z++; |
| 6247 | if( strcmp(z,"schema")==0 ){ |
| 6248 | bSchema = 1; |
| 6249 | }else |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6250 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 6251 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6252 | ){ |
| 6253 | iSize = atoi(&z[5]); |
| 6254 | }else |
| 6255 | if( strcmp(z,"debug")==0 ){ |
| 6256 | bDebug = 1; |
| 6257 | }else |
| 6258 | { |
| 6259 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6260 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6261 | raw_printf(stderr, "Should be one of: --schema" |
| 6262 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 6263 | rc = 1; |
| 6264 | goto meta_command_exit; |
| 6265 | } |
| 6266 | }else if( zLike ){ |
| 6267 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 6268 | rc = 1; |
| 6269 | goto meta_command_exit; |
| 6270 | }else{ |
| 6271 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6272 | bSeparate = 1; |
| 6273 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6274 | } |
| 6275 | } |
| 6276 | if( bSchema ){ |
| 6277 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6278 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6279 | " UNION ALL SELECT 'sqlite_master'" |
| 6280 | " ORDER BY 1 collate nocase"; |
| 6281 | }else{ |
| 6282 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6283 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6284 | " AND name NOT LIKE 'sqlite_%'" |
| 6285 | " ORDER BY 1 collate nocase"; |
| 6286 | } |
| 6287 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6288 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6289 | initText(&sSql); |
| 6290 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 6291 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6292 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 6293 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 6294 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 6295 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 6296 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 6297 | appendText(&sQuery,zTab,'"'); |
| 6298 | appendText(&sQuery," NOT INDEXED;", 0); |
| 6299 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 6300 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 6301 | " ORDER BY name;", 0); |
| 6302 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 6303 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 6304 | " ORDER BY name;", 0); |
| 6305 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 6306 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 6307 | " ORDER BY tbl,idx;", 0); |
| 6308 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 6309 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 6310 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 6311 | appendText(&sQuery, zTab, 0); |
| 6312 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 6313 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6314 | appendText(&sSql, zSep, 0); |
| 6315 | appendText(&sSql, sQuery.z, '\''); |
| 6316 | sQuery.n = 0; |
| 6317 | appendText(&sSql, ",", 0); |
| 6318 | appendText(&sSql, zTab, '\''); |
| 6319 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6320 | } |
| 6321 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6322 | if( bSeparate ){ |
| 6323 | zSql = sqlite3_mprintf( |
| 6324 | "%s))" |
| 6325 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 6326 | " FROM [sha3sum$query]", |
| 6327 | sSql.z, iSize); |
| 6328 | }else{ |
| 6329 | zSql = sqlite3_mprintf( |
| 6330 | "%s))" |
| 6331 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 6332 | " FROM [sha3sum$query]", |
| 6333 | sSql.z, iSize); |
| 6334 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6335 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6336 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6337 | if( bDebug ){ |
| 6338 | utf8_printf(p->out, "%s\n", zSql); |
| 6339 | }else{ |
| 6340 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 6341 | } |
| 6342 | sqlite3_free(zSql); |
| 6343 | }else |
| 6344 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6345 | if( c=='s' |
| 6346 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6347 | ){ |
| 6348 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6349 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6350 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6351 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6352 | rc = 1; |
| 6353 | goto meta_command_exit; |
| 6354 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6355 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6356 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6357 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 6358 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6359 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6360 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6361 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6362 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6363 | }else |
| 6364 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6365 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6366 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6367 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6368 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6369 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6370 | rc = 1; |
| 6371 | goto meta_command_exit; |
| 6372 | } |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6373 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 6374 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6375 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6376 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 6377 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6378 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6379 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 6380 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6381 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6382 | raw_printf(p->out, "\n"); |
| 6383 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6384 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6385 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6386 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6387 | raw_printf(p->out, "\n"); |
| 6388 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6389 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6390 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6391 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6392 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6393 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6394 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6395 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6396 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6397 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 6398 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6399 | }else |
| 6400 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6401 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 6402 | if( nArg==2 ){ |
| 6403 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6404 | }else if( nArg==1 ){ |
| 6405 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6406 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6407 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6408 | rc = 1; |
| 6409 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6410 | }else |
| 6411 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6412 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 6413 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 6414 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 6415 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6416 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6417 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6418 | int nRow, nAlloc; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6419 | int ii; |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6420 | ShellText s; |
| 6421 | initText(&s); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6422 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6423 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6424 | if( rc ) return shellDatabaseError(p->db); |
| 6425 | |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6426 | if( nArg>2 && c=='i' ){ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6427 | /* It is an historical accident that the .indexes command shows an error |
| 6428 | ** when called with the wrong number of arguments whereas the .tables |
| 6429 | ** command does not. */ |
| 6430 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 6431 | rc = 1; |
| 6432 | goto meta_command_exit; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6433 | } |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6434 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6435 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6436 | if( zDbName==0 ) continue; |
| 6437 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 6438 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 6439 | appendText(&s, "SELECT name FROM ", 0); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6440 | }else{ |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6441 | appendText(&s, "SELECT ", 0); |
| 6442 | appendText(&s, zDbName, '\''); |
| 6443 | appendText(&s, "||'.'||name FROM ", 0); |
| 6444 | } |
| 6445 | appendText(&s, zDbName, '"'); |
| 6446 | appendText(&s, ".sqlite_master ", 0); |
| 6447 | if( c=='t' ){ |
| 6448 | appendText(&s," WHERE type IN ('table','view')" |
| 6449 | " AND name NOT LIKE 'sqlite_%'" |
| 6450 | " AND name LIKE ?1", 0); |
| 6451 | }else{ |
| 6452 | appendText(&s," WHERE type='index'" |
| 6453 | " AND tbl_name LIKE ?1", 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6454 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 6455 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6456 | rc = sqlite3_finalize(pStmt); |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 6457 | appendText(&s, " ORDER BY 1", 0); |
| 6458 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 6459 | freeText(&s); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6460 | if( rc ) return shellDatabaseError(p->db); |
| 6461 | |
| 6462 | /* Run the SQL statement prepared by the above block. Store the results |
| 6463 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6464 | nRow = nAlloc = 0; |
| 6465 | azResult = 0; |
| 6466 | if( nArg>1 ){ |
| 6467 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6468 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6469 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 6470 | } |
| 6471 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6472 | if( nRow>=nAlloc ){ |
| 6473 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6474 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6475 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6476 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6477 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6478 | break; |
| 6479 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6480 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6481 | azResult = azNew; |
| 6482 | } |
| 6483 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6484 | if( 0==azResult[nRow] ){ |
| 6485 | rc = shellNomemError(); |
| 6486 | break; |
| 6487 | } |
| 6488 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6489 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6490 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 6491 | rc = shellDatabaseError(p->db); |
| 6492 | } |
| 6493 | |
| 6494 | /* Pretty-print the contents of array azResult[] to the output */ |
| 6495 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6496 | int len, maxlen = 0; |
| 6497 | int i, j; |
| 6498 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6499 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6500 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6501 | if( len>maxlen ) maxlen = len; |
| 6502 | } |
| 6503 | nPrintCol = 80/(maxlen+2); |
| 6504 | if( nPrintCol<1 ) nPrintCol = 1; |
| 6505 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 6506 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6507 | for(j=i; j<nRow; j+=nPrintRow){ |
| 6508 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6509 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 6510 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6511 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6512 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6513 | } |
| 6514 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6515 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6516 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 6517 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6518 | }else |
| 6519 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6520 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 6521 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 6522 | output_reset(p); |
| 6523 | p->out = output_file_open("testcase-out.txt"); |
| 6524 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6525 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6526 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6527 | if( nArg>=2 ){ |
| 6528 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 6529 | }else{ |
| 6530 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 6531 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6532 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6533 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6534 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6535 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6536 | static const struct { |
| 6537 | const char *zCtrlName; /* Name of a test-control option */ |
| 6538 | int ctrlCode; /* Integer code for that option */ |
| 6539 | } aCtrl[] = { |
| 6540 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 6541 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 6542 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 6543 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 6544 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 6545 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 6546 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 6547 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 6548 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 6549 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 6550 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 6551 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6552 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6553 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 6554 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6555 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6556 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6557 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6558 | int rc2 = 0; |
| 6559 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6560 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6561 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6562 | /* convert testctrl text option to value. allow any unique prefix |
| 6563 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6564 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6565 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6566 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6567 | if( testctrl<0 ){ |
| 6568 | testctrl = aCtrl[i].ctrlCode; |
| 6569 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6570 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6571 | testctrl = -1; |
| 6572 | break; |
| 6573 | } |
| 6574 | } |
| 6575 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6576 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6577 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6578 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6579 | }else{ |
| 6580 | switch(testctrl){ |
| 6581 | |
| 6582 | /* sqlite3_test_control(int, db, int) */ |
| 6583 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6584 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6585 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6586 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6587 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6588 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6589 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6590 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6591 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6592 | } |
| 6593 | break; |
| 6594 | |
| 6595 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6596 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 6597 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6598 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6599 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6600 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6601 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6602 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6603 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6604 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 6605 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6606 | } |
| 6607 | break; |
| 6608 | |
| 6609 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6610 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6611 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 6612 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6613 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6614 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6615 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6616 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6617 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6618 | } |
| 6619 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6620 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6621 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6622 | case SQLITE_TESTCTRL_ASSERT: |
| 6623 | case SQLITE_TESTCTRL_ALWAYS: |
| 6624 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6625 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6626 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6627 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6628 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6629 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6630 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6631 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6632 | } |
| 6633 | break; |
| 6634 | |
| 6635 | /* sqlite3_test_control(int, char *) */ |
| 6636 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6637 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6638 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6639 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6640 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6641 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6642 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6643 | utf8_printf(stderr, |
| 6644 | "Error: testctrl %s takes a single char * option\n", |
| 6645 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6646 | } |
| 6647 | break; |
| 6648 | #endif |
| 6649 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6650 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6651 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6652 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6653 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6654 | integerValue(azArg[3]), |
| 6655 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6656 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6657 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6658 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6659 | } |
| 6660 | break; |
| 6661 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6662 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 6663 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 6664 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 6665 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6666 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6667 | utf8_printf(stderr, |
| 6668 | "Error: CLI support for testctrl %s not implemented\n", |
| 6669 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6670 | break; |
| 6671 | } |
| 6672 | } |
| 6673 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 6674 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6675 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6676 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6677 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6678 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6679 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6680 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6681 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 6682 | if( nArg==2 ){ |
| 6683 | enableTimer = booleanValue(azArg[1]); |
| 6684 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6685 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6686 | enableTimer = 0; |
| 6687 | } |
| 6688 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6689 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6690 | rc = 1; |
| 6691 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6692 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6693 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6694 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6695 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6696 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6697 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6698 | rc = 1; |
| 6699 | goto meta_command_exit; |
| 6700 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 6701 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6702 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 6703 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6704 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6705 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6706 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6707 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6708 | } |
| 6709 | #endif |
| 6710 | }else |
| 6711 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6712 | #if SQLITE_USER_AUTHENTICATION |
| 6713 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 6714 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6715 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6716 | rc = 1; |
| 6717 | goto meta_command_exit; |
| 6718 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 6719 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6720 | if( strcmp(azArg[1],"login")==0 ){ |
| 6721 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6722 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6723 | rc = 1; |
| 6724 | goto meta_command_exit; |
| 6725 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6726 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 6727 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6728 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6729 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6730 | rc = 1; |
| 6731 | } |
| 6732 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 6733 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6734 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6735 | rc = 1; |
| 6736 | goto meta_command_exit; |
| 6737 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6738 | rc = sqlite3_user_add(p->db, azArg[2], |
| 6739 | azArg[3], (int)strlen(azArg[3]), |
| 6740 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6741 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6742 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6743 | rc = 1; |
| 6744 | } |
| 6745 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 6746 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6747 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6748 | rc = 1; |
| 6749 | goto meta_command_exit; |
| 6750 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6751 | rc = sqlite3_user_change(p->db, azArg[2], |
| 6752 | azArg[3], (int)strlen(azArg[3]), |
| 6753 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6754 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6755 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6756 | rc = 1; |
| 6757 | } |
| 6758 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 6759 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6760 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6761 | rc = 1; |
| 6762 | goto meta_command_exit; |
| 6763 | } |
| 6764 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 6765 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6766 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6767 | rc = 1; |
| 6768 | } |
| 6769 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6770 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6771 | rc = 1; |
| 6772 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6773 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6774 | }else |
| 6775 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 6776 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6777 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6778 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6779 | sqlite3_libversion(), sqlite3_sourceid()); |
| 6780 | }else |
| 6781 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6782 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 6783 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 6784 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6785 | if( p->db ){ |
| 6786 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 6787 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6788 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 6789 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6790 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6791 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6792 | } |
| 6793 | } |
| 6794 | }else |
| 6795 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 6796 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 6797 | sqlite3_vfs *pVfs; |
| 6798 | sqlite3_vfs *pCurrent = 0; |
| 6799 | if( p->db ){ |
| 6800 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 6801 | } |
| 6802 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 6803 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 6804 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 6805 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6806 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6807 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 6808 | if( pVfs->pNext ){ |
| 6809 | raw_printf(p->out, "-----------------------------------\n"); |
| 6810 | } |
| 6811 | } |
| 6812 | }else |
| 6813 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6814 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 6815 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 6816 | char *zVfsName = 0; |
| 6817 | if( p->db ){ |
| 6818 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 6819 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6820 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6821 | sqlite3_free(zVfsName); |
| 6822 | } |
| 6823 | } |
| 6824 | }else |
| 6825 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6826 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 6827 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6828 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6829 | }else |
| 6830 | #endif |
| 6831 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6832 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6833 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6834 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6835 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6836 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6837 | } |
| 6838 | }else |
| 6839 | |
| 6840 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6841 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6842 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6843 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6844 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6845 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6846 | meta_command_exit: |
| 6847 | if( p->outCount ){ |
| 6848 | p->outCount--; |
| 6849 | if( p->outCount==0 ) output_reset(p); |
| 6850 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6851 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6854 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6855 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 6856 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6857 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6858 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6859 | int i; |
| 6860 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 6861 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6865 | ** Test to see if a line consists entirely of whitespace. |
| 6866 | */ |
| 6867 | static int _all_whitespace(const char *z){ |
| 6868 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6869 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6870 | if( *z=='/' && z[1]=='*' ){ |
| 6871 | z += 2; |
| 6872 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 6873 | if( *z==0 ) return 0; |
| 6874 | z++; |
| 6875 | continue; |
| 6876 | } |
| 6877 | if( *z=='-' && z[1]=='-' ){ |
| 6878 | z += 2; |
| 6879 | while( *z && *z!='\n' ){ z++; } |
| 6880 | if( *z==0 ) return 1; |
| 6881 | continue; |
| 6882 | } |
| 6883 | return 0; |
| 6884 | } |
| 6885 | return 1; |
| 6886 | } |
| 6887 | |
| 6888 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6889 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 6890 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 6891 | ** as is the Oracle "/". |
| 6892 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6893 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6894 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6895 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 6896 | return 1; /* Oracle */ |
| 6897 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6898 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6899 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6900 | return 1; /* SQL Server */ |
| 6901 | } |
| 6902 | return 0; |
| 6903 | } |
| 6904 | |
| 6905 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6906 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 6907 | ** ends in the middle of a string literal or C-style comment. |
| 6908 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6909 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6910 | int rc; |
| 6911 | if( zSql==0 ) return 1; |
| 6912 | zSql[nSql] = ';'; |
| 6913 | zSql[nSql+1] = 0; |
| 6914 | rc = sqlite3_complete(zSql); |
| 6915 | zSql[nSql] = 0; |
| 6916 | return rc; |
| 6917 | } |
| 6918 | |
| 6919 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6920 | ** Run a single line of SQL |
| 6921 | */ |
| 6922 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 6923 | int rc; |
| 6924 | char *zErrMsg = 0; |
| 6925 | |
| 6926 | open_db(p, 0); |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6927 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6928 | BEGIN_TIMER; |
| 6929 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 6930 | END_TIMER; |
| 6931 | if( rc || zErrMsg ){ |
| 6932 | char zPrefix[100]; |
| 6933 | if( in!=0 || !stdin_is_interactive ){ |
| 6934 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 6935 | "Error: near line %d:", startline); |
| 6936 | }else{ |
| 6937 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 6938 | } |
| 6939 | if( zErrMsg!=0 ){ |
| 6940 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 6941 | sqlite3_free(zErrMsg); |
| 6942 | zErrMsg = 0; |
| 6943 | }else{ |
| 6944 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 6945 | } |
| 6946 | return 1; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6947 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6948 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 6949 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 6950 | } |
| 6951 | return 0; |
| 6952 | } |
| 6953 | |
| 6954 | |
| 6955 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6956 | ** Read input from *in and process it. If *in==0 then input |
| 6957 | ** is interactive - the user is typing it it. Otherwise, input |
| 6958 | ** is coming from a file or device. A prompt is issued and history |
| 6959 | ** is saved only if input is interactive. An interrupt signal will |
| 6960 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6961 | ** |
| 6962 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6963 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6964 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6965 | char *zLine = 0; /* A single input line */ |
| 6966 | char *zSql = 0; /* Accumulated SQL text */ |
| 6967 | int nLine; /* Length of current line */ |
| 6968 | int nSql = 0; /* Bytes of zSql[] used */ |
| 6969 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 6970 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6971 | int rc; /* Error code */ |
| 6972 | int errCnt = 0; /* Number of errors seen */ |
| 6973 | int lineno = 0; /* Current line number */ |
| 6974 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6975 | |
| 6976 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 6977 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6978 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6979 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6980 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 6981 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6982 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6983 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6984 | if( seenInterrupt ){ |
| 6985 | if( in!=0 ) break; |
| 6986 | seenInterrupt = 0; |
| 6987 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6988 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6989 | if( nSql==0 && _all_whitespace(zLine) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6990 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6991 | continue; |
| 6992 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 6993 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6994 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6995 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6996 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6997 | break; |
| 6998 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6999 | errCnt++; |
| 7000 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7001 | continue; |
| 7002 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7003 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7004 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 7005 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7006 | nLine = strlen30(zLine); |
| 7007 | if( nSql+nLine+2>=nAlloc ){ |
| 7008 | nAlloc = nSql+nLine+100; |
| 7009 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7010 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7011 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7012 | exit(1); |
| 7013 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7014 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7015 | nSqlPrior = nSql; |
| 7016 | if( nSql==0 ){ |
| 7017 | int i; |
| 7018 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 7019 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7020 | memcpy(zSql, zLine+i, nLine+1-i); |
| 7021 | startline = lineno; |
| 7022 | nSql = nLine-i; |
| 7023 | }else{ |
| 7024 | zSql[nSql++] = '\n'; |
| 7025 | memcpy(zSql+nSql, zLine, nLine+1); |
| 7026 | nSql += nLine; |
| 7027 | } |
| 7028 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 7029 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7030 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7031 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7032 | if( p->outCount ){ |
| 7033 | output_reset(p); |
| 7034 | p->outCount = 0; |
| 7035 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7036 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7037 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 7038 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7039 | } |
| 7040 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7041 | if( nSql && !_all_whitespace(zSql) ){ |
| 7042 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7043 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 7044 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 7045 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 7046 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7049 | /* |
| 7050 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7051 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7052 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7053 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7054 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7055 | if( clearFlag ){ |
| 7056 | free(home_dir); |
| 7057 | home_dir = 0; |
| 7058 | return 0; |
| 7059 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7060 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7061 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 7062 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 7063 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 7064 | { |
| 7065 | struct passwd *pwent; |
| 7066 | uid_t uid = getuid(); |
| 7067 | if( (pwent=getpwuid(uid)) != NULL) { |
| 7068 | home_dir = pwent->pw_dir; |
| 7069 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7070 | } |
| 7071 | #endif |
| 7072 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7073 | #if defined(_WIN32_WCE) |
| 7074 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 7075 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7076 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7077 | #else |
| 7078 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 7079 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7080 | if (!home_dir) { |
| 7081 | home_dir = getenv("USERPROFILE"); |
| 7082 | } |
| 7083 | #endif |
| 7084 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7085 | if (!home_dir) { |
| 7086 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 7089 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7090 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7091 | char *zDrive, *zPath; |
| 7092 | int n; |
| 7093 | zDrive = getenv("HOMEDRIVE"); |
| 7094 | zPath = getenv("HOMEPATH"); |
| 7095 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7096 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7097 | home_dir = malloc( n ); |
| 7098 | if( home_dir==0 ) return 0; |
| 7099 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 7100 | return home_dir; |
| 7101 | } |
| 7102 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7103 | } |
| 7104 | #endif |
| 7105 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7106 | #endif /* !_WIN32_WCE */ |
| 7107 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7108 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7109 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7110 | char *z = malloc( n ); |
| 7111 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7112 | home_dir = z; |
| 7113 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7114 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7115 | return home_dir; |
| 7116 | } |
| 7117 | |
| 7118 | /* |
| 7119 | ** Read input from the file given by sqliterc_override. Or if that |
| 7120 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7121 | ** |
| 7122 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7123 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7124 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7125 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7126 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 7127 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7128 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7129 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 7130 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7131 | FILE *in = NULL; |
| 7132 | |
| 7133 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7134 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7135 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7136 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7137 | " cannot read ~/.sqliterc\n"); |
| 7138 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7139 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 7140 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7141 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 7142 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7143 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 7144 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7145 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7146 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7147 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7148 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7149 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 7150 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7151 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7152 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7155 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7156 | ** Show available command line options |
| 7157 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7158 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7159 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7160 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7161 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7162 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7163 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7164 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7165 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7166 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7167 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7168 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 7169 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 7170 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7171 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7172 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7173 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7174 | " -line set output mode to 'line'\n" |
| 7175 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7176 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7177 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7178 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7179 | " -multiplex enable the multiplexor VFS\n" |
| 7180 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7181 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7182 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7183 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 7184 | " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7185 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7186 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7187 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7188 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7189 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 7190 | " -vfstrace enable tracing of all VFS calls\n" |
| 7191 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7192 | ; |
| 7193 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7194 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7195 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 7196 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 7197 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7198 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7199 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7200 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7201 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7202 | } |
| 7203 | exit(1); |
| 7204 | } |
| 7205 | |
| 7206 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7207 | ** Initialize the state information in data |
| 7208 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7209 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7210 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7211 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 7212 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7213 | memcpy(data->colSeparator,SEP_Column, 2); |
| 7214 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7215 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7216 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7217 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 7218 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7219 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7220 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 7221 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7224 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7225 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7226 | */ |
| 7227 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7228 | static void printBold(const char *zText){ |
| 7229 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 7230 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 7231 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 7232 | SetConsoleTextAttribute(out, |
| 7233 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 7234 | ); |
| 7235 | printf("%s", zText); |
| 7236 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7237 | } |
| 7238 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7239 | static void printBold(const char *zText){ |
| 7240 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7241 | } |
| 7242 | #endif |
| 7243 | |
| 7244 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7245 | ** Get the argument to an --option. Throw an error and die if no argument |
| 7246 | ** is available. |
| 7247 | */ |
| 7248 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 7249 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7250 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7251 | argv[0], argv[argc-1]); |
| 7252 | exit(1); |
| 7253 | } |
| 7254 | return argv[i]; |
| 7255 | } |
| 7256 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7257 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 7258 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 7259 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 7260 | # else |
| 7261 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 7262 | # endif |
| 7263 | #endif |
| 7264 | |
| 7265 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 7266 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7267 | #else |
| 7268 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7269 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7270 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7271 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7272 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7273 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7274 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7275 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7276 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7277 | int readStdin = 1; |
| 7278 | int nCmd = 0; |
| 7279 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7280 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7281 | setBinaryMode(stdin, 0); |
| 7282 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7283 | stdin_is_interactive = isatty(0); |
| 7284 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7285 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 7286 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7287 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7288 | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7289 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 7290 | exit(1); |
| 7291 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 7292 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7293 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7294 | #if !SQLITE_SHELL_IS_UTF8 |
| 7295 | sqlite3_initialize(); |
| 7296 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 7297 | if( argv==0 ){ |
| 7298 | raw_printf(stderr, "out of memory\n"); |
| 7299 | exit(1); |
| 7300 | } |
| 7301 | for(i=0; i<argc; i++){ |
| 7302 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 7303 | if( argv[i]==0 ){ |
| 7304 | raw_printf(stderr, "out of memory\n"); |
| 7305 | exit(1); |
| 7306 | } |
| 7307 | } |
| 7308 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7309 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7310 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7311 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7312 | /* Make sure we have a valid signal handler early, before anything |
| 7313 | ** else is done. |
| 7314 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 7315 | #ifdef SIGINT |
| 7316 | signal(SIGINT, interrupt_handler); |
| 7317 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7318 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7319 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 7320 | { |
| 7321 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 7322 | ** of a C-function that will provide the name of the database file. Use |
| 7323 | ** this compile-time option to embed this shell program in larger |
| 7324 | ** applications. */ |
| 7325 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 7326 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 7327 | warnInmemoryDb = 0; |
| 7328 | } |
| 7329 | #endif |
| 7330 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7331 | /* Do an initial pass through the command-line argument to locate |
| 7332 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7333 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7334 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7335 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7336 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7337 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7338 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7339 | if( z[0]!='-' ){ |
| 7340 | if( data.zDbFilename==0 ){ |
| 7341 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7342 | }else{ |
| 7343 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 7344 | ** mean that nothing is read from stdin */ |
| 7345 | readStdin = 0; |
| 7346 | nCmd++; |
| 7347 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 7348 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7349 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7350 | exit(1); |
| 7351 | } |
| 7352 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7353 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7354 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7355 | if( z[1]=='-' ) z++; |
| 7356 | if( strcmp(z,"-separator")==0 |
| 7357 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7358 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7359 | || strcmp(z,"-cmd")==0 |
| 7360 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7361 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7362 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7363 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7364 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7365 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7366 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7367 | ** we do the actual processing of arguments later in a second pass. |
| 7368 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 7369 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7370 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 7371 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7372 | const char *zSize; |
| 7373 | sqlite3_int64 szHeap; |
| 7374 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7375 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7376 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7377 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7378 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 7379 | #else |
| 7380 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7381 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7382 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7383 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7384 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7385 | if( sz>400000 ) sz = 400000; |
| 7386 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7387 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7388 | if( n>10 ) n = 10; |
| 7389 | if( n<1 ) n = 1; |
| 7390 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 7391 | data.shellFlgs |= SHFLG_Scratch; |
| 7392 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7393 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7394 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7395 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7396 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7397 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7398 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 7399 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7400 | data.shellFlgs |= SHFLG_Pagecache; |
| 7401 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7402 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7403 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7404 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7405 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7406 | if( n<0 ) n = 0; |
| 7407 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 7408 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7409 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7410 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7411 | extern int vfstrace_register( |
| 7412 | const char *zTraceName, |
| 7413 | const char *zOldVfsName, |
| 7414 | int (*xOut)(const char*,void*), |
| 7415 | void *pOutArg, |
| 7416 | int makeDefault |
| 7417 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7418 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7419 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7420 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7421 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7422 | extern int sqlite3_multiple_initialize(const char*,int); |
| 7423 | sqlite3_multiplex_initialize(0, 1); |
| 7424 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7425 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 7426 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 7427 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7428 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7429 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7430 | if( pVfs ){ |
| 7431 | sqlite3_vfs_register(pVfs, 1); |
| 7432 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7433 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7434 | exit(1); |
| 7435 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7436 | } |
| 7437 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7438 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7439 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7440 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7441 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7442 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7443 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 7444 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7445 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7446 | } |
| 7447 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7448 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7449 | /* Go ahead and open the database file if it already exists. If the |
| 7450 | ** file does not exist, delay opening it. This prevents empty database |
| 7451 | ** files from being created if a user mistypes the database name argument |
| 7452 | ** to the sqlite command-line tool. |
| 7453 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 7454 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7455 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7456 | } |
| 7457 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7458 | /* Process the initialization file if there is one. If no -init option |
| 7459 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 7460 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7461 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7462 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7463 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7464 | /* Make a second pass through the command-line argument and set |
| 7465 | ** options. This second pass is delayed until after the initialization |
| 7466 | ** file is processed so that the command-line arguments will override |
| 7467 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7468 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7469 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7470 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7471 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7472 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 7473 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7474 | i++; |
| 7475 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7476 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7477 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7478 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7479 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7480 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7481 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 7482 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7483 | }else if( strcmp(z,"-csv")==0 ){ |
| 7484 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7485 | memcpy(data.colSeparator,",",2); |
| 7486 | }else if( strcmp(z,"-ascii")==0 ){ |
| 7487 | data.mode = MODE_Ascii; |
| 7488 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7489 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7490 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7491 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7492 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7493 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7494 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7495 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7496 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7497 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7498 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 7499 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7500 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7501 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7502 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7503 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7504 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7505 | }else if( strcmp(z,"-echo")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7506 | ShellSetFlag(&data, SHFLG_Echo); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 7507 | }else if( strcmp(z,"-eqp")==0 ){ |
| 7508 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7509 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 7510 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7511 | }else if( strcmp(z,"-stats")==0 ){ |
| 7512 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 7513 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 7514 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 7515 | }else if( strcmp(z,"-backslash")==0 ){ |
| 7516 | /* Undocumented command-line option: -backslash |
| 7517 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 7518 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 7519 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 7520 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7521 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7522 | }else if( strcmp(z,"-bail")==0 ){ |
| 7523 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7524 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7525 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 7526 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7527 | }else if( strcmp(z,"-interactive")==0 ){ |
| 7528 | stdin_is_interactive = 1; |
| 7529 | }else if( strcmp(z,"-batch")==0 ){ |
| 7530 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7531 | }else if( strcmp(z,"-heap")==0 ){ |
| 7532 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7533 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7534 | i+=2; |
| 7535 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7536 | i+=2; |
| 7537 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7538 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7539 | }else if( strcmp(z,"-mmap")==0 ){ |
| 7540 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7541 | }else if( strcmp(z,"-vfs")==0 ){ |
| 7542 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7543 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7544 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 7545 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7546 | #endif |
| 7547 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7548 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 7549 | i++; |
| 7550 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7551 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7552 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7553 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7554 | /* Run commands that follow -cmd first and separately from commands |
| 7555 | ** that simply appear on the command-line. This seems goofy. It would |
| 7556 | ** be better if all commands ran in the order that they appear. But |
| 7557 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7558 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7559 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7560 | if( z[0]=='.' ){ |
| 7561 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 7562 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7563 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7564 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7565 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 7566 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7567 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7568 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 7569 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7570 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7571 | if( bail_on_error ) return rc; |
| 7572 | } |
| 7573 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7574 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7575 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 7576 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7577 | return 1; |
| 7578 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7579 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7580 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7581 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7582 | if( !readStdin ){ |
| 7583 | /* Run all arguments that do not begin with '-' as if they were separate |
| 7584 | ** command-line inputs, except for the argToSkip argument which contains |
| 7585 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7586 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7587 | for(i=0; i<nCmd; i++){ |
| 7588 | if( azCmd[i][0]=='.' ){ |
| 7589 | rc = do_meta_command(azCmd[i], &data); |
| 7590 | if( rc ) return rc==2 ? 0 : rc; |
| 7591 | }else{ |
| 7592 | open_db(&data, 0); |
| 7593 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 7594 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7595 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7596 | return rc!=0 ? rc : 1; |
| 7597 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7598 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7599 | return rc; |
| 7600 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 7601 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7602 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7603 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7604 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7605 | /* Run commands received from standard input |
| 7606 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7607 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7608 | char *zHome; |
| 7609 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7610 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7611 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 7612 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7613 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7614 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7615 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7616 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7617 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 7618 | printBold("transient in-memory database"); |
| 7619 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7620 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7621 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7622 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7623 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7624 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7625 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7626 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7627 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7628 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7629 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7630 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7631 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 7632 | shell_stifle_history(100); |
| 7633 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7634 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7635 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7636 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7637 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7638 | } |
| 7639 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 7640 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 7641 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 7642 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 7643 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7644 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7645 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7646 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7647 | #if !SQLITE_SHELL_IS_UTF8 |
| 7648 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7649 | sqlite3_free(argv); |
| 7650 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7651 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7652 | } |