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 | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 732 | /****************************************************************************** |
| 733 | ** SHA3 hash implementation copied from ../ext/misc/shathree.c |
| 734 | */ |
| 735 | typedef sqlite3_uint64 u64; |
| 736 | /* |
| 737 | ** Macros to determine whether the machine is big or little endian, |
| 738 | ** and whether or not that determination is run-time or compile-time. |
| 739 | ** |
| 740 | ** For best performance, an attempt is made to guess at the byte-order |
| 741 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 742 | ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined |
| 743 | ** at run-time. |
| 744 | */ |
| 745 | #ifndef SHA3_BYTEORDER |
| 746 | # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 747 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 748 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 749 | defined(__arm__) |
| 750 | # define SHA3_BYTEORDER 1234 |
| 751 | # elif defined(sparc) || defined(__ppc__) |
| 752 | # define SHA3_BYTEORDER 4321 |
| 753 | # else |
| 754 | # define SHA3_BYTEORDER 0 |
| 755 | # endif |
| 756 | #endif |
| 757 | |
| 758 | |
| 759 | /* |
| 760 | ** State structure for a SHA3 hash in progress |
| 761 | */ |
| 762 | typedef struct SHA3Context SHA3Context; |
| 763 | struct SHA3Context { |
| 764 | union { |
| 765 | u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ |
| 766 | unsigned char x[1600]; /* ... or 1600 bytes */ |
| 767 | } u; |
| 768 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 769 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 770 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 771 | }; |
| 772 | |
drh | 050b124 | 2017-05-04 11:13:50 +0000 | [diff] [blame] | 773 | /* Allow the following routine to use the B0 variable, which is also |
| 774 | ** a macro in the termios.h header file */ |
| 775 | #undef B0 |
| 776 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 777 | /* |
| 778 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 779 | */ |
| 780 | static void KeccakF1600Step(SHA3Context *p){ |
| 781 | int i; |
| 782 | u64 B0, B1, B2, B3, B4; |
| 783 | u64 C0, C1, C2, C3, C4; |
| 784 | u64 D0, D1, D2, D3, D4; |
| 785 | static const u64 RC[] = { |
| 786 | 0x0000000000000001ULL, 0x0000000000008082ULL, |
| 787 | 0x800000000000808aULL, 0x8000000080008000ULL, |
| 788 | 0x000000000000808bULL, 0x0000000080000001ULL, |
| 789 | 0x8000000080008081ULL, 0x8000000000008009ULL, |
| 790 | 0x000000000000008aULL, 0x0000000000000088ULL, |
| 791 | 0x0000000080008009ULL, 0x000000008000000aULL, |
| 792 | 0x000000008000808bULL, 0x800000000000008bULL, |
| 793 | 0x8000000000008089ULL, 0x8000000000008003ULL, |
| 794 | 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 795 | 0x000000000000800aULL, 0x800000008000000aULL, |
| 796 | 0x8000000080008081ULL, 0x8000000000008080ULL, |
| 797 | 0x0000000080000001ULL, 0x8000000080008008ULL |
| 798 | }; |
| 799 | # define A00 (p->u.s[0]) |
| 800 | # define A01 (p->u.s[1]) |
| 801 | # define A02 (p->u.s[2]) |
| 802 | # define A03 (p->u.s[3]) |
| 803 | # define A04 (p->u.s[4]) |
| 804 | # define A10 (p->u.s[5]) |
| 805 | # define A11 (p->u.s[6]) |
| 806 | # define A12 (p->u.s[7]) |
| 807 | # define A13 (p->u.s[8]) |
| 808 | # define A14 (p->u.s[9]) |
| 809 | # define A20 (p->u.s[10]) |
| 810 | # define A21 (p->u.s[11]) |
| 811 | # define A22 (p->u.s[12]) |
| 812 | # define A23 (p->u.s[13]) |
| 813 | # define A24 (p->u.s[14]) |
| 814 | # define A30 (p->u.s[15]) |
| 815 | # define A31 (p->u.s[16]) |
| 816 | # define A32 (p->u.s[17]) |
| 817 | # define A33 (p->u.s[18]) |
| 818 | # define A34 (p->u.s[19]) |
| 819 | # define A40 (p->u.s[20]) |
| 820 | # define A41 (p->u.s[21]) |
| 821 | # define A42 (p->u.s[22]) |
| 822 | # define A43 (p->u.s[23]) |
| 823 | # define A44 (p->u.s[24]) |
| 824 | # define ROL64(a,x) ((a<<x)|(a>>(64-x))) |
| 825 | |
| 826 | for(i=0; i<24; i+=4){ |
| 827 | C0 = A00^A10^A20^A30^A40; |
| 828 | C1 = A01^A11^A21^A31^A41; |
| 829 | C2 = A02^A12^A22^A32^A42; |
| 830 | C3 = A03^A13^A23^A33^A43; |
| 831 | C4 = A04^A14^A24^A34^A44; |
| 832 | D0 = C4^ROL64(C1, 1); |
| 833 | D1 = C0^ROL64(C2, 1); |
| 834 | D2 = C1^ROL64(C3, 1); |
| 835 | D3 = C2^ROL64(C4, 1); |
| 836 | D4 = C3^ROL64(C0, 1); |
| 837 | |
| 838 | B0 = (A00^D0); |
| 839 | B1 = ROL64((A11^D1), 44); |
| 840 | B2 = ROL64((A22^D2), 43); |
| 841 | B3 = ROL64((A33^D3), 21); |
| 842 | B4 = ROL64((A44^D4), 14); |
| 843 | A00 = B0 ^((~B1)& B2 ); |
| 844 | A00 ^= RC[i]; |
| 845 | A11 = B1 ^((~B2)& B3 ); |
| 846 | A22 = B2 ^((~B3)& B4 ); |
| 847 | A33 = B3 ^((~B4)& B0 ); |
| 848 | A44 = B4 ^((~B0)& B1 ); |
| 849 | |
| 850 | B2 = ROL64((A20^D0), 3); |
| 851 | B3 = ROL64((A31^D1), 45); |
| 852 | B4 = ROL64((A42^D2), 61); |
| 853 | B0 = ROL64((A03^D3), 28); |
| 854 | B1 = ROL64((A14^D4), 20); |
| 855 | A20 = B0 ^((~B1)& B2 ); |
| 856 | A31 = B1 ^((~B2)& B3 ); |
| 857 | A42 = B2 ^((~B3)& B4 ); |
| 858 | A03 = B3 ^((~B4)& B0 ); |
| 859 | A14 = B4 ^((~B0)& B1 ); |
| 860 | |
| 861 | B4 = ROL64((A40^D0), 18); |
| 862 | B0 = ROL64((A01^D1), 1); |
| 863 | B1 = ROL64((A12^D2), 6); |
| 864 | B2 = ROL64((A23^D3), 25); |
| 865 | B3 = ROL64((A34^D4), 8); |
| 866 | A40 = B0 ^((~B1)& B2 ); |
| 867 | A01 = B1 ^((~B2)& B3 ); |
| 868 | A12 = B2 ^((~B3)& B4 ); |
| 869 | A23 = B3 ^((~B4)& B0 ); |
| 870 | A34 = B4 ^((~B0)& B1 ); |
| 871 | |
| 872 | B1 = ROL64((A10^D0), 36); |
| 873 | B2 = ROL64((A21^D1), 10); |
| 874 | B3 = ROL64((A32^D2), 15); |
| 875 | B4 = ROL64((A43^D3), 56); |
| 876 | B0 = ROL64((A04^D4), 27); |
| 877 | A10 = B0 ^((~B1)& B2 ); |
| 878 | A21 = B1 ^((~B2)& B3 ); |
| 879 | A32 = B2 ^((~B3)& B4 ); |
| 880 | A43 = B3 ^((~B4)& B0 ); |
| 881 | A04 = B4 ^((~B0)& B1 ); |
| 882 | |
| 883 | B3 = ROL64((A30^D0), 41); |
| 884 | B4 = ROL64((A41^D1), 2); |
| 885 | B0 = ROL64((A02^D2), 62); |
| 886 | B1 = ROL64((A13^D3), 55); |
| 887 | B2 = ROL64((A24^D4), 39); |
| 888 | A30 = B0 ^((~B1)& B2 ); |
| 889 | A41 = B1 ^((~B2)& B3 ); |
| 890 | A02 = B2 ^((~B3)& B4 ); |
| 891 | A13 = B3 ^((~B4)& B0 ); |
| 892 | A24 = B4 ^((~B0)& B1 ); |
| 893 | |
| 894 | C0 = A00^A20^A40^A10^A30; |
| 895 | C1 = A11^A31^A01^A21^A41; |
| 896 | C2 = A22^A42^A12^A32^A02; |
| 897 | C3 = A33^A03^A23^A43^A13; |
| 898 | C4 = A44^A14^A34^A04^A24; |
| 899 | D0 = C4^ROL64(C1, 1); |
| 900 | D1 = C0^ROL64(C2, 1); |
| 901 | D2 = C1^ROL64(C3, 1); |
| 902 | D3 = C2^ROL64(C4, 1); |
| 903 | D4 = C3^ROL64(C0, 1); |
| 904 | |
| 905 | B0 = (A00^D0); |
| 906 | B1 = ROL64((A31^D1), 44); |
| 907 | B2 = ROL64((A12^D2), 43); |
| 908 | B3 = ROL64((A43^D3), 21); |
| 909 | B4 = ROL64((A24^D4), 14); |
| 910 | A00 = B0 ^((~B1)& B2 ); |
| 911 | A00 ^= RC[i+1]; |
| 912 | A31 = B1 ^((~B2)& B3 ); |
| 913 | A12 = B2 ^((~B3)& B4 ); |
| 914 | A43 = B3 ^((~B4)& B0 ); |
| 915 | A24 = B4 ^((~B0)& B1 ); |
| 916 | |
| 917 | B2 = ROL64((A40^D0), 3); |
| 918 | B3 = ROL64((A21^D1), 45); |
| 919 | B4 = ROL64((A02^D2), 61); |
| 920 | B0 = ROL64((A33^D3), 28); |
| 921 | B1 = ROL64((A14^D4), 20); |
| 922 | A40 = B0 ^((~B1)& B2 ); |
| 923 | A21 = B1 ^((~B2)& B3 ); |
| 924 | A02 = B2 ^((~B3)& B4 ); |
| 925 | A33 = B3 ^((~B4)& B0 ); |
| 926 | A14 = B4 ^((~B0)& B1 ); |
| 927 | |
| 928 | B4 = ROL64((A30^D0), 18); |
| 929 | B0 = ROL64((A11^D1), 1); |
| 930 | B1 = ROL64((A42^D2), 6); |
| 931 | B2 = ROL64((A23^D3), 25); |
| 932 | B3 = ROL64((A04^D4), 8); |
| 933 | A30 = B0 ^((~B1)& B2 ); |
| 934 | A11 = B1 ^((~B2)& B3 ); |
| 935 | A42 = B2 ^((~B3)& B4 ); |
| 936 | A23 = B3 ^((~B4)& B0 ); |
| 937 | A04 = B4 ^((~B0)& B1 ); |
| 938 | |
| 939 | B1 = ROL64((A20^D0), 36); |
| 940 | B2 = ROL64((A01^D1), 10); |
| 941 | B3 = ROL64((A32^D2), 15); |
| 942 | B4 = ROL64((A13^D3), 56); |
| 943 | B0 = ROL64((A44^D4), 27); |
| 944 | A20 = B0 ^((~B1)& B2 ); |
| 945 | A01 = B1 ^((~B2)& B3 ); |
| 946 | A32 = B2 ^((~B3)& B4 ); |
| 947 | A13 = B3 ^((~B4)& B0 ); |
| 948 | A44 = B4 ^((~B0)& B1 ); |
| 949 | |
| 950 | B3 = ROL64((A10^D0), 41); |
| 951 | B4 = ROL64((A41^D1), 2); |
| 952 | B0 = ROL64((A22^D2), 62); |
| 953 | B1 = ROL64((A03^D3), 55); |
| 954 | B2 = ROL64((A34^D4), 39); |
| 955 | A10 = B0 ^((~B1)& B2 ); |
| 956 | A41 = B1 ^((~B2)& B3 ); |
| 957 | A22 = B2 ^((~B3)& B4 ); |
| 958 | A03 = B3 ^((~B4)& B0 ); |
| 959 | A34 = B4 ^((~B0)& B1 ); |
| 960 | |
| 961 | C0 = A00^A40^A30^A20^A10; |
| 962 | C1 = A31^A21^A11^A01^A41; |
| 963 | C2 = A12^A02^A42^A32^A22; |
| 964 | C3 = A43^A33^A23^A13^A03; |
| 965 | C4 = A24^A14^A04^A44^A34; |
| 966 | D0 = C4^ROL64(C1, 1); |
| 967 | D1 = C0^ROL64(C2, 1); |
| 968 | D2 = C1^ROL64(C3, 1); |
| 969 | D3 = C2^ROL64(C4, 1); |
| 970 | D4 = C3^ROL64(C0, 1); |
| 971 | |
| 972 | B0 = (A00^D0); |
| 973 | B1 = ROL64((A21^D1), 44); |
| 974 | B2 = ROL64((A42^D2), 43); |
| 975 | B3 = ROL64((A13^D3), 21); |
| 976 | B4 = ROL64((A34^D4), 14); |
| 977 | A00 = B0 ^((~B1)& B2 ); |
| 978 | A00 ^= RC[i+2]; |
| 979 | A21 = B1 ^((~B2)& B3 ); |
| 980 | A42 = B2 ^((~B3)& B4 ); |
| 981 | A13 = B3 ^((~B4)& B0 ); |
| 982 | A34 = B4 ^((~B0)& B1 ); |
| 983 | |
| 984 | B2 = ROL64((A30^D0), 3); |
| 985 | B3 = ROL64((A01^D1), 45); |
| 986 | B4 = ROL64((A22^D2), 61); |
| 987 | B0 = ROL64((A43^D3), 28); |
| 988 | B1 = ROL64((A14^D4), 20); |
| 989 | A30 = B0 ^((~B1)& B2 ); |
| 990 | A01 = B1 ^((~B2)& B3 ); |
| 991 | A22 = B2 ^((~B3)& B4 ); |
| 992 | A43 = B3 ^((~B4)& B0 ); |
| 993 | A14 = B4 ^((~B0)& B1 ); |
| 994 | |
| 995 | B4 = ROL64((A10^D0), 18); |
| 996 | B0 = ROL64((A31^D1), 1); |
| 997 | B1 = ROL64((A02^D2), 6); |
| 998 | B2 = ROL64((A23^D3), 25); |
| 999 | B3 = ROL64((A44^D4), 8); |
| 1000 | A10 = B0 ^((~B1)& B2 ); |
| 1001 | A31 = B1 ^((~B2)& B3 ); |
| 1002 | A02 = B2 ^((~B3)& B4 ); |
| 1003 | A23 = B3 ^((~B4)& B0 ); |
| 1004 | A44 = B4 ^((~B0)& B1 ); |
| 1005 | |
| 1006 | B1 = ROL64((A40^D0), 36); |
| 1007 | B2 = ROL64((A11^D1), 10); |
| 1008 | B3 = ROL64((A32^D2), 15); |
| 1009 | B4 = ROL64((A03^D3), 56); |
| 1010 | B0 = ROL64((A24^D4), 27); |
| 1011 | A40 = B0 ^((~B1)& B2 ); |
| 1012 | A11 = B1 ^((~B2)& B3 ); |
| 1013 | A32 = B2 ^((~B3)& B4 ); |
| 1014 | A03 = B3 ^((~B4)& B0 ); |
| 1015 | A24 = B4 ^((~B0)& B1 ); |
| 1016 | |
| 1017 | B3 = ROL64((A20^D0), 41); |
| 1018 | B4 = ROL64((A41^D1), 2); |
| 1019 | B0 = ROL64((A12^D2), 62); |
| 1020 | B1 = ROL64((A33^D3), 55); |
| 1021 | B2 = ROL64((A04^D4), 39); |
| 1022 | A20 = B0 ^((~B1)& B2 ); |
| 1023 | A41 = B1 ^((~B2)& B3 ); |
| 1024 | A12 = B2 ^((~B3)& B4 ); |
| 1025 | A33 = B3 ^((~B4)& B0 ); |
| 1026 | A04 = B4 ^((~B0)& B1 ); |
| 1027 | |
| 1028 | C0 = A00^A30^A10^A40^A20; |
| 1029 | C1 = A21^A01^A31^A11^A41; |
| 1030 | C2 = A42^A22^A02^A32^A12; |
| 1031 | C3 = A13^A43^A23^A03^A33; |
| 1032 | C4 = A34^A14^A44^A24^A04; |
| 1033 | D0 = C4^ROL64(C1, 1); |
| 1034 | D1 = C0^ROL64(C2, 1); |
| 1035 | D2 = C1^ROL64(C3, 1); |
| 1036 | D3 = C2^ROL64(C4, 1); |
| 1037 | D4 = C3^ROL64(C0, 1); |
| 1038 | |
| 1039 | B0 = (A00^D0); |
| 1040 | B1 = ROL64((A01^D1), 44); |
| 1041 | B2 = ROL64((A02^D2), 43); |
| 1042 | B3 = ROL64((A03^D3), 21); |
| 1043 | B4 = ROL64((A04^D4), 14); |
| 1044 | A00 = B0 ^((~B1)& B2 ); |
| 1045 | A00 ^= RC[i+3]; |
| 1046 | A01 = B1 ^((~B2)& B3 ); |
| 1047 | A02 = B2 ^((~B3)& B4 ); |
| 1048 | A03 = B3 ^((~B4)& B0 ); |
| 1049 | A04 = B4 ^((~B0)& B1 ); |
| 1050 | |
| 1051 | B2 = ROL64((A10^D0), 3); |
| 1052 | B3 = ROL64((A11^D1), 45); |
| 1053 | B4 = ROL64((A12^D2), 61); |
| 1054 | B0 = ROL64((A13^D3), 28); |
| 1055 | B1 = ROL64((A14^D4), 20); |
| 1056 | A10 = B0 ^((~B1)& B2 ); |
| 1057 | A11 = B1 ^((~B2)& B3 ); |
| 1058 | A12 = B2 ^((~B3)& B4 ); |
| 1059 | A13 = B3 ^((~B4)& B0 ); |
| 1060 | A14 = B4 ^((~B0)& B1 ); |
| 1061 | |
| 1062 | B4 = ROL64((A20^D0), 18); |
| 1063 | B0 = ROL64((A21^D1), 1); |
| 1064 | B1 = ROL64((A22^D2), 6); |
| 1065 | B2 = ROL64((A23^D3), 25); |
| 1066 | B3 = ROL64((A24^D4), 8); |
| 1067 | A20 = B0 ^((~B1)& B2 ); |
| 1068 | A21 = B1 ^((~B2)& B3 ); |
| 1069 | A22 = B2 ^((~B3)& B4 ); |
| 1070 | A23 = B3 ^((~B4)& B0 ); |
| 1071 | A24 = B4 ^((~B0)& B1 ); |
| 1072 | |
| 1073 | B1 = ROL64((A30^D0), 36); |
| 1074 | B2 = ROL64((A31^D1), 10); |
| 1075 | B3 = ROL64((A32^D2), 15); |
| 1076 | B4 = ROL64((A33^D3), 56); |
| 1077 | B0 = ROL64((A34^D4), 27); |
| 1078 | A30 = B0 ^((~B1)& B2 ); |
| 1079 | A31 = B1 ^((~B2)& B3 ); |
| 1080 | A32 = B2 ^((~B3)& B4 ); |
| 1081 | A33 = B3 ^((~B4)& B0 ); |
| 1082 | A34 = B4 ^((~B0)& B1 ); |
| 1083 | |
| 1084 | B3 = ROL64((A40^D0), 41); |
| 1085 | B4 = ROL64((A41^D1), 2); |
| 1086 | B0 = ROL64((A42^D2), 62); |
| 1087 | B1 = ROL64((A43^D3), 55); |
| 1088 | B2 = ROL64((A44^D4), 39); |
| 1089 | A40 = B0 ^((~B1)& B2 ); |
| 1090 | A41 = B1 ^((~B2)& B3 ); |
| 1091 | A42 = B2 ^((~B3)& B4 ); |
| 1092 | A43 = B3 ^((~B4)& B0 ); |
| 1093 | A44 = B4 ^((~B0)& B1 ); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | ** Initialize a new hash. iSize determines the size of the hash |
| 1099 | ** in bits and should be one of 224, 256, 384, or 512. Or iSize |
| 1100 | ** can be zero to use the default hash size of 256 bits. |
| 1101 | */ |
| 1102 | static void SHA3Init(SHA3Context *p, int iSize){ |
| 1103 | memset(p, 0, sizeof(*p)); |
| 1104 | if( iSize>=128 && iSize<=512 ){ |
| 1105 | p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; |
| 1106 | }else{ |
| 1107 | p->nRate = (1600 - 2*256)/8; |
| 1108 | } |
| 1109 | #if SHA3_BYTEORDER==1234 |
| 1110 | /* Known to be little-endian at compile-time. No-op */ |
| 1111 | #elif SHA3_BYTEORDER==4321 |
| 1112 | p->ixMask = 7; /* Big-endian */ |
| 1113 | #else |
| 1114 | { |
| 1115 | static unsigned int one = 1; |
| 1116 | if( 1==*(unsigned char*)&one ){ |
| 1117 | /* Little endian. No byte swapping. */ |
| 1118 | p->ixMask = 0; |
| 1119 | }else{ |
| 1120 | /* Big endian. Byte swap. */ |
| 1121 | p->ixMask = 7; |
| 1122 | } |
| 1123 | } |
| 1124 | #endif |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | ** Make consecutive calls to the SHA3Update function to add new content |
| 1129 | ** to the hash |
| 1130 | */ |
| 1131 | static void SHA3Update( |
| 1132 | SHA3Context *p, |
| 1133 | const unsigned char *aData, |
| 1134 | unsigned int nData |
| 1135 | ){ |
| 1136 | unsigned int i = 0; |
| 1137 | #if SHA3_BYTEORDER==1234 |
| 1138 | if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ |
| 1139 | for(; i+7<nData; i+=8){ |
| 1140 | p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; |
| 1141 | p->nLoaded += 8; |
| 1142 | if( p->nLoaded>=p->nRate ){ |
| 1143 | KeccakF1600Step(p); |
| 1144 | p->nLoaded = 0; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | #endif |
| 1149 | for(; i<nData; i++){ |
| 1150 | #if SHA3_BYTEORDER==1234 |
| 1151 | p->u.x[p->nLoaded] ^= aData[i]; |
| 1152 | #elif SHA3_BYTEORDER==4321 |
| 1153 | p->u.x[p->nLoaded^0x07] ^= aData[i]; |
| 1154 | #else |
| 1155 | p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; |
| 1156 | #endif |
| 1157 | p->nLoaded++; |
| 1158 | if( p->nLoaded==p->nRate ){ |
| 1159 | KeccakF1600Step(p); |
| 1160 | p->nLoaded = 0; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | ** After all content has been added, invoke SHA3Final() to compute |
| 1167 | ** the final hash. The function returns a pointer to the binary |
| 1168 | ** hash value. |
| 1169 | */ |
| 1170 | static unsigned char *SHA3Final(SHA3Context *p){ |
| 1171 | unsigned int i; |
| 1172 | if( p->nLoaded==p->nRate-1 ){ |
| 1173 | const unsigned char c1 = 0x86; |
| 1174 | SHA3Update(p, &c1, 1); |
| 1175 | }else{ |
| 1176 | const unsigned char c2 = 0x06; |
| 1177 | const unsigned char c3 = 0x80; |
| 1178 | SHA3Update(p, &c2, 1); |
| 1179 | p->nLoaded = p->nRate - 1; |
| 1180 | SHA3Update(p, &c3, 1); |
| 1181 | } |
| 1182 | for(i=0; i<p->nRate; i++){ |
| 1183 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1184 | } |
| 1185 | return &p->u.x[p->nRate]; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | ** Implementation of the sha3(X,SIZE) function. |
| 1190 | ** |
| 1191 | ** 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] | 1192 | ** size is 256. If X is a BLOB, it is hashed as is. |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1193 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1194 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1195 | ** of a NULL value is NULL. |
| 1196 | */ |
| 1197 | static void sha3Func( |
| 1198 | sqlite3_context *context, |
| 1199 | int argc, |
| 1200 | sqlite3_value **argv |
| 1201 | ){ |
| 1202 | SHA3Context cx; |
| 1203 | int eType = sqlite3_value_type(argv[0]); |
| 1204 | int nByte = sqlite3_value_bytes(argv[0]); |
| 1205 | int iSize; |
| 1206 | if( argc==1 ){ |
| 1207 | iSize = 256; |
| 1208 | }else{ |
| 1209 | iSize = sqlite3_value_int(argv[1]); |
| 1210 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1211 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1212 | "384 512", -1); |
| 1213 | return; |
| 1214 | } |
| 1215 | } |
| 1216 | if( eType==SQLITE_NULL ) return; |
| 1217 | SHA3Init(&cx, iSize); |
| 1218 | if( eType==SQLITE_BLOB ){ |
| 1219 | SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 1220 | }else{ |
| 1221 | SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); |
| 1222 | } |
| 1223 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1224 | } |
| 1225 | |
| 1226 | /* Compute a string using sqlite3_vsnprintf() with a maximum length |
| 1227 | ** of 50 bytes and add it to the hash. |
| 1228 | */ |
| 1229 | static void hash_step_vformat( |
| 1230 | SHA3Context *p, /* Add content to this context */ |
| 1231 | const char *zFormat, |
| 1232 | ... |
| 1233 | ){ |
| 1234 | va_list ap; |
| 1235 | int n; |
| 1236 | char zBuf[50]; |
| 1237 | va_start(ap, zFormat); |
| 1238 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 1239 | va_end(ap); |
| 1240 | n = (int)strlen(zBuf); |
| 1241 | SHA3Update(p, (unsigned char*)zBuf, n); |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | ** Implementation of the sha3_query(SQL,SIZE) function. |
| 1246 | ** |
| 1247 | ** This function compiles and runs the SQL statement(s) given in the |
| 1248 | ** argument. The results are hashed using a SIZE-bit SHA3. The default |
| 1249 | ** size is 256. |
| 1250 | ** |
| 1251 | ** The format of the byte stream that is hashed is summarized as follows: |
| 1252 | ** |
| 1253 | ** S<n>:<sql> |
| 1254 | ** R |
| 1255 | ** N |
| 1256 | ** I<int> |
| 1257 | ** F<ieee-float> |
| 1258 | ** B<size>:<bytes> |
| 1259 | ** T<size>:<text> |
| 1260 | ** |
| 1261 | ** <sql> is the original SQL text for each statement run and <n> is |
| 1262 | ** the size of that text. The SQL text is UTF-8. A single R character |
| 1263 | ** occurs before the start of each row. N means a NULL value. |
| 1264 | ** I mean an 8-byte little-endian integer <int>. F is a floating point |
| 1265 | ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. |
| 1266 | ** B means blobs of <size> bytes. T means text rendered as <size> |
| 1267 | ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII |
| 1268 | ** text integers. |
| 1269 | ** |
| 1270 | ** For each SQL statement in the X input, there is one S segment. Each |
| 1271 | ** S segment is followed by zero or more R segments, one for each row in the |
| 1272 | ** result set. After each R, there are one or more N, I, F, B, or T segments, |
| 1273 | ** one for each column in the result set. Segments are concatentated directly |
| 1274 | ** with no delimiters of any kind. |
| 1275 | */ |
| 1276 | static void sha3QueryFunc( |
| 1277 | sqlite3_context *context, |
| 1278 | int argc, |
| 1279 | sqlite3_value **argv |
| 1280 | ){ |
| 1281 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1282 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1283 | sqlite3_stmt *pStmt = 0; |
| 1284 | int nCol; /* Number of columns in the result set */ |
| 1285 | int i; /* Loop counter */ |
| 1286 | int rc; |
| 1287 | int n; |
| 1288 | const char *z; |
| 1289 | SHA3Context cx; |
| 1290 | int iSize; |
| 1291 | |
| 1292 | if( argc==1 ){ |
| 1293 | iSize = 256; |
| 1294 | }else{ |
| 1295 | iSize = sqlite3_value_int(argv[1]); |
| 1296 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1297 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1298 | "384 512", -1); |
| 1299 | return; |
| 1300 | } |
| 1301 | } |
| 1302 | if( zSql==0 ) return; |
| 1303 | SHA3Init(&cx, iSize); |
| 1304 | while( zSql[0] ){ |
| 1305 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 1306 | if( rc ){ |
| 1307 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 1308 | zSql, sqlite3_errmsg(db)); |
| 1309 | sqlite3_finalize(pStmt); |
| 1310 | sqlite3_result_error(context, zMsg, -1); |
| 1311 | sqlite3_free(zMsg); |
| 1312 | return; |
| 1313 | } |
| 1314 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 1315 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 1316 | sqlite3_finalize(pStmt); |
| 1317 | sqlite3_result_error(context, zMsg, -1); |
| 1318 | sqlite3_free(zMsg); |
| 1319 | return; |
| 1320 | } |
| 1321 | nCol = sqlite3_column_count(pStmt); |
| 1322 | z = sqlite3_sql(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 1323 | if( z==0 ){ |
| 1324 | sqlite3_finalize(pStmt); |
| 1325 | continue; |
| 1326 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1327 | n = (int)strlen(z); |
| 1328 | hash_step_vformat(&cx,"S%d:",n); |
| 1329 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1330 | |
| 1331 | /* Compute a hash over the result of the query */ |
| 1332 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1333 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 1334 | for(i=0; i<nCol; i++){ |
| 1335 | switch( sqlite3_column_type(pStmt,i) ){ |
| 1336 | case SQLITE_NULL: { |
| 1337 | SHA3Update(&cx, (const unsigned char*)"N",1); |
| 1338 | break; |
| 1339 | } |
| 1340 | case SQLITE_INTEGER: { |
| 1341 | sqlite3_uint64 u; |
| 1342 | int j; |
| 1343 | unsigned char x[9]; |
| 1344 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 1345 | memcpy(&u, &v, 8); |
| 1346 | for(j=8; j>=1; j--){ |
| 1347 | x[j] = u & 0xff; |
| 1348 | u >>= 8; |
| 1349 | } |
| 1350 | x[0] = 'I'; |
| 1351 | SHA3Update(&cx, x, 9); |
| 1352 | break; |
| 1353 | } |
| 1354 | case SQLITE_FLOAT: { |
| 1355 | sqlite3_uint64 u; |
| 1356 | int j; |
| 1357 | unsigned char x[9]; |
| 1358 | double r = sqlite3_column_double(pStmt,i); |
| 1359 | memcpy(&u, &r, 8); |
| 1360 | for(j=8; j>=1; j--){ |
| 1361 | x[j] = u & 0xff; |
| 1362 | u >>= 8; |
| 1363 | } |
| 1364 | x[0] = 'F'; |
| 1365 | SHA3Update(&cx,x,9); |
| 1366 | break; |
| 1367 | } |
| 1368 | case SQLITE_TEXT: { |
| 1369 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1370 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 1371 | hash_step_vformat(&cx,"T%d:",n2); |
| 1372 | SHA3Update(&cx, z2, n2); |
| 1373 | break; |
| 1374 | } |
| 1375 | case SQLITE_BLOB: { |
| 1376 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1377 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 1378 | hash_step_vformat(&cx,"B%d:",n2); |
| 1379 | SHA3Update(&cx, z2, n2); |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | sqlite3_finalize(pStmt); |
| 1386 | } |
| 1387 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1388 | } |
| 1389 | /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c |
| 1390 | ********************************************************************************/ |
| 1391 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1392 | #if defined(SQLITE_ENABLE_SESSION) |
| 1393 | /* |
| 1394 | ** State information for a single open session |
| 1395 | */ |
| 1396 | typedef struct OpenSession OpenSession; |
| 1397 | struct OpenSession { |
| 1398 | char *zName; /* Symbolic name for this session */ |
| 1399 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1400 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1401 | sqlite3_session *p; /* The open session */ |
| 1402 | }; |
| 1403 | #endif |
| 1404 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1405 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1406 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1407 | ** saved so that it can be restored by ".explain off" |
| 1408 | */ |
| 1409 | typedef struct SavedModeInfo SavedModeInfo; |
| 1410 | struct SavedModeInfo { |
| 1411 | int valid; /* Is there legit data in here? */ |
| 1412 | int mode; /* Mode prior to ".explain on" */ |
| 1413 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 1414 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1415 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1416 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1417 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1418 | ** State information about the database connection is contained in an |
| 1419 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1420 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1421 | typedef struct ShellState ShellState; |
| 1422 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1423 | sqlite3 *db; /* The database */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1424 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1425 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1426 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1427 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1428 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1429 | int cnt; /* Number of records displayed so far */ |
| 1430 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 1431 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1432 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1433 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1434 | int cMode; /* temporary output mode for the current query */ |
| 1435 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1436 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1437 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1438 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1439 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1440 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1441 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1442 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1443 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1444 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1445 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1446 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1447 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1448 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1449 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 1450 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1451 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1452 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1453 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1454 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1455 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1456 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1457 | #if defined(SQLITE_ENABLE_SESSION) |
| 1458 | int nSession; /* Number of active sessions */ |
| 1459 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1460 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1461 | }; |
| 1462 | |
| 1463 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1464 | ** These are the allowed shellFlgs values |
| 1465 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 1466 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 1467 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 1468 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 1469 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 1470 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
| 1471 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1472 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
| 1473 | |
| 1474 | /* |
| 1475 | ** Macros for testing and setting shellFlgs |
| 1476 | */ |
| 1477 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1478 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1479 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1480 | |
| 1481 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1482 | ** These are the allowed modes. |
| 1483 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1484 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1485 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1486 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1487 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1488 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1489 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1490 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1491 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1492 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1493 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1494 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1495 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1496 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1497 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1498 | "line", |
| 1499 | "column", |
| 1500 | "list", |
| 1501 | "semi", |
| 1502 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1503 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1504 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1505 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1506 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1507 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1508 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1509 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1510 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1511 | |
| 1512 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1513 | ** These are the column/row/line separators used by the various |
| 1514 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1515 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1516 | #define SEP_Column "|" |
| 1517 | #define SEP_Row "\n" |
| 1518 | #define SEP_Tab "\t" |
| 1519 | #define SEP_Space " " |
| 1520 | #define SEP_Comma "," |
| 1521 | #define SEP_CrLf "\r\n" |
| 1522 | #define SEP_Unit "\x1F" |
| 1523 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1524 | |
| 1525 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1526 | ** Number of elements in an array |
| 1527 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1528 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1531 | ** A callback for the sqlite3_log() interface. |
| 1532 | */ |
| 1533 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1534 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1535 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1536 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1537 | fflush(p->pLog); |
| 1538 | } |
| 1539 | |
| 1540 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1541 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1542 | */ |
| 1543 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1544 | int i; |
| 1545 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1546 | raw_printf(out,"X'"); |
| 1547 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1548 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | /* |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1552 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1553 | ** to that string. |
| 1554 | ** |
| 1555 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1556 | ** then make up some string and store it in the buffer zBuf. |
| 1557 | */ |
| 1558 | static const char *unused_string( |
| 1559 | const char *z, /* Result must not appear anywhere in z */ |
| 1560 | const char *zA, const char *zB, /* Try these first */ |
| 1561 | char *zBuf /* Space to store a generated string */ |
| 1562 | ){ |
| 1563 | unsigned i = 0; |
| 1564 | if( strstr(z, zA)==0 ) return zA; |
| 1565 | if( strstr(z, zB)==0 ) return zB; |
| 1566 | do{ |
| 1567 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1568 | }while( strstr(z,zBuf)!=0 ); |
| 1569 | return zBuf; |
| 1570 | } |
| 1571 | |
| 1572 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1573 | ** Output the given string as a quoted string using SQL quoting conventions. |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1574 | ** |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1575 | ** See also: output_quoted_escaped_string() |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1576 | */ |
| 1577 | static void output_quoted_string(FILE *out, const char *z){ |
| 1578 | int i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1579 | char c; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1580 | setBinaryMode(out, 1); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1581 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1582 | if( c==0 ){ |
| 1583 | utf8_printf(out,"'%s'",z); |
| 1584 | }else{ |
| 1585 | raw_printf(out, "'"); |
| 1586 | while( *z ){ |
| 1587 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1588 | if( c=='\'' ) i++; |
| 1589 | if( i ){ |
| 1590 | utf8_printf(out, "%.*s", i, z); |
| 1591 | z += i; |
| 1592 | } |
| 1593 | if( c=='\'' ){ |
| 1594 | raw_printf(out, "'"); |
| 1595 | continue; |
| 1596 | } |
| 1597 | if( c==0 ){ |
| 1598 | break; |
| 1599 | } |
| 1600 | z++; |
| 1601 | } |
| 1602 | raw_printf(out, "'"); |
| 1603 | } |
| 1604 | setTextMode(out, 1); |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1609 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1610 | ** get corrupted by end-of-line translation facilities in some operating |
| 1611 | ** systems. |
| 1612 | ** |
| 1613 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1614 | ** escape mechanism. |
| 1615 | */ |
| 1616 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1617 | int i; |
| 1618 | char c; |
| 1619 | setBinaryMode(out, 1); |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1620 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1621 | if( c==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1622 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1623 | }else{ |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1624 | const char *zNL = 0; |
| 1625 | const char *zCR = 0; |
| 1626 | int nNL = 0; |
| 1627 | int nCR = 0; |
| 1628 | char zBuf1[20], zBuf2[20]; |
| 1629 | for(i=0; z[i]; i++){ |
| 1630 | if( z[i]=='\n' ) nNL++; |
| 1631 | if( z[i]=='\r' ) nCR++; |
| 1632 | } |
| 1633 | if( nNL ){ |
| 1634 | raw_printf(out, "replace("); |
| 1635 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1636 | } |
| 1637 | if( nCR ){ |
| 1638 | raw_printf(out, "replace("); |
| 1639 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1640 | } |
| 1641 | raw_printf(out, "'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1642 | while( *z ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1643 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1644 | if( c=='\'' ) i++; |
| 1645 | if( i ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1646 | utf8_printf(out, "%.*s", i, z); |
| 1647 | z += i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1648 | } |
| 1649 | if( c=='\'' ){ |
| 1650 | raw_printf(out, "'"); |
| 1651 | continue; |
| 1652 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1653 | if( c==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1656 | z++; |
| 1657 | if( c=='\n' ){ |
| 1658 | raw_printf(out, "%s", zNL); |
| 1659 | continue; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1660 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1661 | raw_printf(out, "%s", zCR); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1662 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1663 | raw_printf(out, "'"); |
| 1664 | if( nCR ){ |
| 1665 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1666 | } |
| 1667 | if( nNL ){ |
| 1668 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1669 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1670 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1671 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1675 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1676 | */ |
| 1677 | static void output_c_string(FILE *out, const char *z){ |
| 1678 | unsigned int c; |
| 1679 | fputc('"', out); |
| 1680 | while( (c = *(z++))!=0 ){ |
| 1681 | if( c=='\\' ){ |
| 1682 | fputc(c, out); |
| 1683 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 1684 | }else if( c=='"' ){ |
| 1685 | fputc('\\', out); |
| 1686 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1687 | }else if( c=='\t' ){ |
| 1688 | fputc('\\', out); |
| 1689 | fputc('t', out); |
| 1690 | }else if( c=='\n' ){ |
| 1691 | fputc('\\', out); |
| 1692 | fputc('n', out); |
| 1693 | }else if( c=='\r' ){ |
| 1694 | fputc('\\', out); |
| 1695 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 1696 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1697 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1698 | }else{ |
| 1699 | fputc(c, out); |
| 1700 | } |
| 1701 | } |
| 1702 | fputc('"', out); |
| 1703 | } |
| 1704 | |
| 1705 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1706 | ** Output the given string with characters that are special to |
| 1707 | ** HTML escaped. |
| 1708 | */ |
| 1709 | static void output_html_string(FILE *out, const char *z){ |
| 1710 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 1711 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1712 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1713 | for(i=0; z[i] |
| 1714 | && z[i]!='<' |
| 1715 | && z[i]!='&' |
| 1716 | && z[i]!='>' |
| 1717 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1718 | && z[i]!='\''; |
| 1719 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1720 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1721 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1722 | } |
| 1723 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1724 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1725 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1726 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1727 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1728 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1729 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1730 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1731 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1732 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1733 | }else{ |
| 1734 | break; |
| 1735 | } |
| 1736 | z += i + 1; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1741 | ** If a field contains any character identified by a 1 in the following |
| 1742 | ** array, then the string must be quoted for CSV. |
| 1743 | */ |
| 1744 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1745 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1746 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1747 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1748 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1749 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1750 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1751 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1752 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1753 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1754 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1755 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1756 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1757 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1758 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1759 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1760 | 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] | 1761 | }; |
| 1762 | |
| 1763 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 1764 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1765 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1766 | ** the null value. Strings are quoted if necessary. The separator |
| 1767 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1768 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1769 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1770 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1771 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1772 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1773 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1774 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1775 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1776 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1777 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1778 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1779 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1780 | i = 0; |
| 1781 | break; |
| 1782 | } |
| 1783 | } |
| 1784 | if( i==0 ){ |
| 1785 | putc('"', out); |
| 1786 | for(i=0; z[i]; i++){ |
| 1787 | if( z[i]=='"' ) putc('"', out); |
| 1788 | putc(z[i], out); |
| 1789 | } |
| 1790 | putc('"', out); |
| 1791 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1792 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1793 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1794 | } |
| 1795 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1796 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1800 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1801 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1802 | ** This routine runs when the user presses Ctrl-C |
| 1803 | */ |
| 1804 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1805 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 1806 | seenInterrupt++; |
| 1807 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 1808 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1809 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1810 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1811 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1812 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1813 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1814 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1815 | ** invoked. It always returns SQLITE_OK. |
| 1816 | */ |
| 1817 | static int shellAuth( |
| 1818 | void *pClientData, |
| 1819 | int op, |
| 1820 | const char *zA1, |
| 1821 | const char *zA2, |
| 1822 | const char *zA3, |
| 1823 | const char *zA4 |
| 1824 | ){ |
| 1825 | ShellState *p = (ShellState*)pClientData; |
| 1826 | static const char *azAction[] = { 0, |
| 1827 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1828 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1829 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1830 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1831 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1832 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1833 | "PRAGMA", "READ", "SELECT", |
| 1834 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1835 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1836 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1837 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1838 | }; |
| 1839 | int i; |
| 1840 | const char *az[4]; |
| 1841 | az[0] = zA1; |
| 1842 | az[1] = zA2; |
| 1843 | az[2] = zA3; |
| 1844 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1845 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1846 | for(i=0; i<4; i++){ |
| 1847 | raw_printf(p->out, " "); |
| 1848 | if( az[i] ){ |
| 1849 | output_c_string(p->out, az[i]); |
| 1850 | }else{ |
| 1851 | raw_printf(p->out, "NULL"); |
| 1852 | } |
| 1853 | } |
| 1854 | raw_printf(p->out, "\n"); |
| 1855 | return SQLITE_OK; |
| 1856 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1857 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1858 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1859 | /* |
| 1860 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1861 | ** |
| 1862 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1863 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1864 | */ |
| 1865 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1866 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1867 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1868 | }else{ |
| 1869 | utf8_printf(out, "%s%s", z, zTail); |
| 1870 | } |
| 1871 | } |
| 1872 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1873 | char c = z[n]; |
| 1874 | z[n] = 0; |
| 1875 | printSchemaLine(out, z, zTail); |
| 1876 | z[n] = c; |
| 1877 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1878 | |
| 1879 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1880 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1881 | ** invokes for each row of a query result. |
| 1882 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1883 | static int shell_callback( |
| 1884 | void *pArg, |
| 1885 | int nArg, /* Number of result columns */ |
| 1886 | char **azArg, /* Text of each result column */ |
| 1887 | char **azCol, /* Column names */ |
| 1888 | int *aiType /* Column types */ |
| 1889 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1890 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1891 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1892 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1893 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1894 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1895 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1896 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1897 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1898 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1899 | if( len>w ) w = len; |
| 1900 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1901 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1902 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1903 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1904 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1905 | } |
| 1906 | break; |
| 1907 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1908 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1909 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1910 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1911 | const int *colWidth; |
| 1912 | int showHdr; |
| 1913 | char *rowSep; |
| 1914 | if( p->cMode==MODE_Column ){ |
| 1915 | colWidth = p->colWidth; |
| 1916 | showHdr = p->showHeader; |
| 1917 | rowSep = p->rowSeparator; |
| 1918 | }else{ |
| 1919 | colWidth = aExplainWidths; |
| 1920 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 1921 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1922 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1923 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1924 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1925 | int w, n; |
| 1926 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1927 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1928 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1929 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1930 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1931 | if( w==0 ){ |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 1932 | w = strlenChar(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1933 | if( w<10 ) w = 10; |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 1934 | n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1935 | if( w<n ) w = n; |
| 1936 | } |
| 1937 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1938 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1939 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1940 | if( showHdr ){ |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 1941 | utf8_width_print(p->out, w, azCol[i]); |
| 1942 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1945 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1946 | for(i=0; i<nArg; i++){ |
| 1947 | int w; |
| 1948 | if( i<ArraySize(p->actualWidth) ){ |
| 1949 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1950 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1951 | }else{ |
| 1952 | w = 10; |
| 1953 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1954 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1955 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1956 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1957 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1958 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1961 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1962 | for(i=0; i<nArg; i++){ |
| 1963 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1964 | if( i<ArraySize(p->actualWidth) ){ |
| 1965 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1966 | }else{ |
| 1967 | w = 10; |
| 1968 | } |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 1969 | if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ |
| 1970 | w = strlenChar(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1971 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1972 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1973 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1974 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1975 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1976 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1977 | } |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 1978 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 1979 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1980 | } |
| 1981 | break; |
| 1982 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1983 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1984 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1985 | break; |
| 1986 | } |
| 1987 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1988 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1989 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1990 | int nParen = 0; |
| 1991 | char cEnd = 0; |
| 1992 | char c; |
| 1993 | int nLine = 0; |
| 1994 | assert( nArg==1 ); |
| 1995 | if( azArg[0]==0 ) break; |
| 1996 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1997 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1998 | ){ |
| 1999 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 2000 | break; |
| 2001 | } |
| 2002 | z = sqlite3_mprintf("%s", azArg[0]); |
| 2003 | j = 0; |
| 2004 | for(i=0; IsSpace(z[i]); i++){} |
| 2005 | for(; (c = z[i])!=0; i++){ |
| 2006 | if( IsSpace(c) ){ |
| 2007 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 2008 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 2009 | j--; |
| 2010 | } |
| 2011 | z[j++] = c; |
| 2012 | } |
| 2013 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 2014 | z[j] = 0; |
| 2015 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 2016 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2017 | if( c==cEnd ){ |
| 2018 | cEnd = 0; |
| 2019 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 2020 | cEnd = c; |
| 2021 | }else if( c=='[' ){ |
| 2022 | cEnd = ']'; |
| 2023 | }else if( c=='(' ){ |
| 2024 | nParen++; |
| 2025 | }else if( c==')' ){ |
| 2026 | nParen--; |
| 2027 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2028 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2029 | j = 0; |
| 2030 | } |
| 2031 | } |
| 2032 | z[j++] = c; |
| 2033 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 2034 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2035 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2036 | j = 0; |
| 2037 | nLine++; |
| 2038 | while( IsSpace(z[i+1]) ){ i++; } |
| 2039 | } |
| 2040 | } |
| 2041 | z[j] = 0; |
| 2042 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2043 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2044 | sqlite3_free(z); |
| 2045 | break; |
| 2046 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2047 | case MODE_List: { |
| 2048 | if( p->cnt++==0 && p->showHeader ){ |
| 2049 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2050 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2051 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2054 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2055 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2056 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2057 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2058 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2059 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2060 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2061 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2062 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2063 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2064 | } |
| 2065 | break; |
| 2066 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2067 | case MODE_Html: { |
| 2068 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2069 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2070 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2071 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2072 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2073 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2074 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2075 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2076 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2077 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2078 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2079 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2080 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2081 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2082 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2083 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2084 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2087 | case MODE_Tcl: { |
| 2088 | if( p->cnt++==0 && p->showHeader ){ |
| 2089 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2090 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2091 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2092 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2093 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2094 | } |
| 2095 | if( azArg==0 ) break; |
| 2096 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2097 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2098 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2099 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2100 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2101 | break; |
| 2102 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2103 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2104 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2105 | if( p->cnt++==0 && p->showHeader ){ |
| 2106 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2107 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2108 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2109 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2110 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 2111 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 2112 | for(i=0; i<nArg; i++){ |
| 2113 | output_csv(p, azArg[i], i<nArg-1); |
| 2114 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2115 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2116 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2117 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2118 | break; |
| 2119 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2120 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2121 | if( azArg==0 ) break; |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2122 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2123 | if( p->showHeader ){ |
| 2124 | raw_printf(p->out,"("); |
| 2125 | for(i=0; i<nArg; i++){ |
| 2126 | if( i>0 ) raw_printf(p->out, ","); |
| 2127 | if( quoteChar(azCol[i]) ){ |
| 2128 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 2129 | utf8_printf(p->out, "%s", z); |
| 2130 | sqlite3_free(z); |
| 2131 | }else{ |
| 2132 | raw_printf(p->out, "%s", azCol[i]); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2133 | } |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2134 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2135 | raw_printf(p->out,")"); |
| 2136 | } |
| 2137 | p->cnt++; |
| 2138 | for(i=0; i<nArg; i++){ |
| 2139 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2140 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2141 | utf8_printf(p->out,"NULL"); |
| 2142 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2143 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2144 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2145 | utf8_printf(p->out,"%s", azArg[i]); |
| 2146 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2147 | char z[50]; |
| 2148 | double r = sqlite3_column_double(p->pStmt, i); |
| 2149 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2150 | raw_printf(p->out, "%s", z); |
| 2151 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2152 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2153 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2154 | output_hex_blob(p->out, pBlob, nBlob); |
| 2155 | }else if( isNumber(azArg[i], 0) ){ |
| 2156 | utf8_printf(p->out,"%s", azArg[i]); |
| 2157 | }else{ |
| 2158 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2159 | } |
| 2160 | } |
| 2161 | raw_printf(p->out,");\n"); |
| 2162 | break; |
| 2163 | } |
| 2164 | case MODE_Quote: { |
| 2165 | if( azArg==0 ) break; |
| 2166 | if( p->cnt==0 && p->showHeader ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2167 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2168 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2169 | output_quoted_string(p->out, azCol[i]); |
| 2170 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2171 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2172 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2173 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2174 | for(i=0; i<nArg; i++){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2175 | if( i>0 ) raw_printf(p->out, ","); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2176 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2177 | utf8_printf(p->out,"NULL"); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2178 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2179 | output_quoted_string(p->out, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2180 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2181 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2182 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2183 | char z[50]; |
| 2184 | double r = sqlite3_column_double(p->pStmt, i); |
| 2185 | sqlite3_snprintf(50,z,"%!.20g", r); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2186 | raw_printf(p->out, "%s", z); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2187 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2188 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2189 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2190 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2191 | }else if( isNumber(azArg[i], 0) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2192 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2193 | }else{ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2194 | output_quoted_string(p->out, azArg[i]); |
| 2195 | } |
| 2196 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2197 | raw_printf(p->out,"\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2198 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2199 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2200 | case MODE_Ascii: { |
| 2201 | if( p->cnt++==0 && p->showHeader ){ |
| 2202 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2203 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2204 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2205 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2206 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2207 | } |
| 2208 | if( azArg==0 ) break; |
| 2209 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2210 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2211 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2212 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2213 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2216 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2221 | ** This is the callback routine that the SQLite library |
| 2222 | ** invokes for each row of a query result. |
| 2223 | */ |
| 2224 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2225 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2226 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2227 | } |
| 2228 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2229 | /* |
| 2230 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2231 | ** output onto the end of a ShellText object. |
| 2232 | */ |
| 2233 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2234 | ShellText *p = (ShellText*)pArg; |
| 2235 | int i; |
drh | 2fb79e9 | 2017-03-25 12:08:11 +0000 | [diff] [blame] | 2236 | UNUSED_PARAMETER(az); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2237 | if( p->n ) appendText(p, "|", 0); |
| 2238 | for(i=0; i<nArg; i++){ |
| 2239 | if( i ) appendText(p, ",", 0); |
| 2240 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2241 | } |
| 2242 | return 0; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | ** Generate an appropriate SELFTEST table in the main database. |
| 2247 | */ |
| 2248 | static void createSelftestTable(ShellState *p){ |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2249 | char *zErrMsg = 0; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2250 | sqlite3_exec(p->db, |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2251 | "SAVEPOINT selftest_init;\n" |
| 2252 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2253 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2254 | " op TEXT,\n" /* Operator: memo run */ |
| 2255 | " cmd TEXT,\n" /* Command text */ |
| 2256 | " ans TEXT\n" /* Desired answer */ |
| 2257 | ");" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2258 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2259 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2260 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2261 | " 'memo','Tests generated by --init');\n" |
| 2262 | "INSERT INTO [_shell$self]\n" |
| 2263 | " SELECT 'run',\n" |
| 2264 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2265 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2266 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2267 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2268 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2269 | " SELECT 'run'," |
| 2270 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2271 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2272 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2273 | " FROM (\n" |
| 2274 | " SELECT name FROM sqlite_master\n" |
| 2275 | " WHERE type='table'\n" |
| 2276 | " AND name<>'selftest'\n" |
| 2277 | " AND coalesce(rootpage,0)>0\n" |
| 2278 | " )\n" |
| 2279 | " ORDER BY name;\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2280 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2281 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2282 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2283 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2284 | "DROP TABLE [_shell$self];" |
| 2285 | ,0,0,&zErrMsg); |
| 2286 | if( zErrMsg ){ |
| 2287 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2288 | sqlite3_free(zErrMsg); |
| 2289 | } |
| 2290 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2293 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2294 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2295 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2296 | ** the name of the table given. Escape any quote characters in the |
| 2297 | ** table name. |
| 2298 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2299 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2300 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2301 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2302 | char *z; |
| 2303 | |
| 2304 | if( p->zDestTable ){ |
| 2305 | free(p->zDestTable); |
| 2306 | p->zDestTable = 0; |
| 2307 | } |
| 2308 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2309 | cQuote = quoteChar(zName); |
| 2310 | n = strlen30(zName); |
| 2311 | if( cQuote ) n += 2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2312 | z = p->zDestTable = malloc( n+1 ); |
| 2313 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2314 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2315 | exit(1); |
| 2316 | } |
| 2317 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2318 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2319 | for(i=0; zName[i]; i++){ |
| 2320 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2321 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2322 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2323 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2324 | z[n] = 0; |
| 2325 | } |
| 2326 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2327 | |
| 2328 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2329 | ** Execute a query statement that will generate SQL output. Print |
| 2330 | ** the result columns, comma-separated, on a line and then add a |
| 2331 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2332 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2333 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2334 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2335 | ** "--" comment occurs at the end of the statement, the comment |
| 2336 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2337 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2338 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2339 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2340 | const char *zSelect, /* SELECT statement to extract content */ |
| 2341 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2342 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2343 | sqlite3_stmt *pSelect; |
| 2344 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2345 | int nResult; |
| 2346 | int i; |
| 2347 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2348 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2349 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2350 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2351 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2352 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2353 | return rc; |
| 2354 | } |
| 2355 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2356 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2357 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2358 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2359 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2360 | zFirstRow = 0; |
| 2361 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2362 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2363 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2364 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2365 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2366 | } |
| 2367 | if( z==0 ) z = ""; |
| 2368 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2369 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2370 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2371 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2372 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2373 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2374 | rc = sqlite3_step(pSelect); |
| 2375 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2376 | rc = sqlite3_finalize(pSelect); |
| 2377 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2378 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2379 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2380 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2381 | } |
| 2382 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2385 | /* |
| 2386 | ** Allocate space and save off current error string. |
| 2387 | */ |
| 2388 | static char *save_err_msg( |
| 2389 | sqlite3 *db /* Database to query */ |
| 2390 | ){ |
| 2391 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2392 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2393 | if( zErrMsg ){ |
| 2394 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2395 | } |
| 2396 | return zErrMsg; |
| 2397 | } |
| 2398 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2399 | #ifdef __linux__ |
| 2400 | /* |
| 2401 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2402 | */ |
| 2403 | static void displayLinuxIoStats(FILE *out){ |
| 2404 | FILE *in; |
| 2405 | char z[200]; |
| 2406 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2407 | in = fopen(z, "rb"); |
| 2408 | if( in==0 ) return; |
| 2409 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2410 | static const struct { |
| 2411 | const char *zPattern; |
| 2412 | const char *zDesc; |
| 2413 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 2414 | { "rchar: ", "Bytes received by read():" }, |
| 2415 | { "wchar: ", "Bytes sent to write():" }, |
| 2416 | { "syscr: ", "Read() system calls:" }, |
| 2417 | { "syscw: ", "Write() system calls:" }, |
| 2418 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2419 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2420 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2421 | }; |
| 2422 | int i; |
| 2423 | for(i=0; i<ArraySize(aTrans); i++){ |
| 2424 | int n = (int)strlen(aTrans[i].zPattern); |
| 2425 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2426 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2432 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2433 | #endif |
| 2434 | |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2435 | /* |
| 2436 | ** Display a single line of status using 64-bit values. |
| 2437 | */ |
| 2438 | static void displayStatLine( |
| 2439 | ShellState *p, /* The shell context */ |
| 2440 | char *zLabel, /* Label for this one line */ |
| 2441 | char *zFormat, /* Format for the result */ |
| 2442 | int iStatusCtrl, /* Which status to display */ |
| 2443 | int bReset /* True to reset the stats */ |
| 2444 | ){ |
| 2445 | sqlite3_int64 iCur = -1; |
| 2446 | sqlite3_int64 iHiwtr = -1; |
| 2447 | int i, nPercent; |
| 2448 | char zLine[200]; |
| 2449 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2450 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2451 | if( zFormat[i]=='%' ) nPercent++; |
| 2452 | } |
| 2453 | if( nPercent>1 ){ |
| 2454 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2455 | }else{ |
| 2456 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2457 | } |
| 2458 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2459 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2460 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2461 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2462 | ** Display memory stats. |
| 2463 | */ |
| 2464 | static int display_stats( |
| 2465 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2466 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2467 | int bReset /* True to reset the stats */ |
| 2468 | ){ |
| 2469 | int iCur; |
| 2470 | int iHiwtr; |
| 2471 | |
| 2472 | if( pArg && pArg->out ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2473 | displayStatLine(pArg, "Memory Used:", |
| 2474 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2475 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2476 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2477 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2478 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2479 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2480 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2481 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2482 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2483 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2484 | displayStatLine(pArg, "Number of Scratch Allocations Used:", |
| 2485 | "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2486 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2487 | displayStatLine(pArg, "Number of Scratch Overflow Bytes:", |
| 2488 | "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset); |
| 2489 | displayStatLine(pArg, "Largest Allocation:", |
| 2490 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2491 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2492 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2493 | displayStatLine(pArg, "Largest Scratch Allocation:", |
| 2494 | "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2495 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2496 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2497 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2498 | #endif |
| 2499 | } |
| 2500 | |
| 2501 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2502 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2503 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2504 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2505 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2506 | raw_printf(pArg->out, |
| 2507 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2508 | iCur, iHiwtr); |
| 2509 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2510 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2511 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2512 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2513 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2514 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2515 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2516 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2517 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2518 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2519 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2520 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2521 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2522 | iHiwtr = iCur = -1; |
| 2523 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2524 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2525 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2526 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2527 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2528 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2529 | iHiwtr = iCur = -1; |
| 2530 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2531 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2532 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2533 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2534 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2535 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2536 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2537 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2538 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2539 | iHiwtr = iCur = -1; |
| 2540 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2541 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2542 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2546 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2547 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2548 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2549 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2550 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2551 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2552 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2553 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2554 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2557 | #ifdef __linux__ |
| 2558 | displayLinuxIoStats(pArg->out); |
| 2559 | #endif |
| 2560 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 2561 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2562 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2563 | return 0; |
| 2564 | } |
| 2565 | |
| 2566 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2567 | ** Display scan stats. |
| 2568 | */ |
| 2569 | static void display_scanstats( |
| 2570 | sqlite3 *db, /* Database to query */ |
| 2571 | ShellState *pArg /* Pointer to ShellState */ |
| 2572 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2573 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2574 | UNUSED_PARAMETER(db); |
| 2575 | UNUSED_PARAMETER(pArg); |
| 2576 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2577 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2578 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2579 | mx = 0; |
| 2580 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2581 | double rEstLoop = 1.0; |
| 2582 | for(i=n=0; 1; i++){ |
| 2583 | sqlite3_stmt *p = pArg->pStmt; |
| 2584 | sqlite3_int64 nLoop, nVisit; |
| 2585 | double rEst; |
| 2586 | int iSid; |
| 2587 | const char *zExplain; |
| 2588 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2589 | break; |
| 2590 | } |
| 2591 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2592 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2593 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2594 | if( n==0 ){ |
| 2595 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2596 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2597 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2598 | n++; |
| 2599 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2600 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2601 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2602 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2603 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2604 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2605 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 2606 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2607 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2608 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2609 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2610 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2611 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2615 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2616 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2617 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2618 | ** Otherwise, return zero. |
| 2619 | */ |
| 2620 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2621 | int i; |
| 2622 | for(i=0; azArray[i]; i++){ |
| 2623 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2624 | } |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
| 2628 | /* |
| 2629 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2630 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2631 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2632 | ** |
| 2633 | ** The indenting rules are: |
| 2634 | ** |
| 2635 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2636 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2637 | ** itself by 2 spaces. |
| 2638 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2639 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2640 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 2641 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2642 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2643 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 2644 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2645 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2646 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2647 | const char *zSql; /* The text of the SQL statement */ |
| 2648 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2649 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2650 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2651 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2652 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 2653 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2654 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2655 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2656 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2657 | const char *azGoto[] = { "Goto", 0 }; |
| 2658 | |
| 2659 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2660 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2661 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2662 | p->cMode = p->mode; |
| 2663 | return; |
| 2664 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2665 | zSql = sqlite3_sql(pSql); |
| 2666 | if( zSql==0 ) return; |
| 2667 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2668 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2669 | p->cMode = p->mode; |
| 2670 | return; |
| 2671 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2672 | |
| 2673 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2674 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2675 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2676 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2677 | |
| 2678 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2679 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2680 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2681 | ** the current instruction is part of a sub-program generated by an |
| 2682 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2683 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2684 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2685 | |
| 2686 | /* Grow the p->aiIndent array as required */ |
| 2687 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2688 | if( iOp==0 ){ |
| 2689 | /* Do further verfication that this is explain output. Abort if |
| 2690 | ** it is not */ |
| 2691 | static const char *explainCols[] = { |
| 2692 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2693 | int jj; |
| 2694 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2695 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2696 | p->cMode = p->mode; |
| 2697 | sqlite3_reset(pSql); |
| 2698 | return; |
| 2699 | } |
| 2700 | } |
| 2701 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2702 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2703 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2704 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2705 | } |
| 2706 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2707 | p->aiIndent[iOp] = 0; |
| 2708 | p->nIndent = iOp+1; |
| 2709 | |
| 2710 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2711 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2712 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2713 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2714 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2715 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2716 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2720 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2721 | sqlite3_free(abYield); |
| 2722 | sqlite3_reset(pSql); |
| 2723 | } |
| 2724 | |
| 2725 | /* |
| 2726 | ** Free the array allocated by explain_data_prepare(). |
| 2727 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2728 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2729 | sqlite3_free(p->aiIndent); |
| 2730 | p->aiIndent = 0; |
| 2731 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2732 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2736 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2737 | */ |
| 2738 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2739 | extern int sqlite3SelectTrace; |
| 2740 | static int savedSelectTrace; |
| 2741 | #endif |
| 2742 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2743 | extern int sqlite3WhereTrace; |
| 2744 | static int savedWhereTrace; |
| 2745 | #endif |
| 2746 | static void disable_debug_trace_modes(void){ |
| 2747 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2748 | savedSelectTrace = sqlite3SelectTrace; |
| 2749 | sqlite3SelectTrace = 0; |
| 2750 | #endif |
| 2751 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2752 | savedWhereTrace = sqlite3WhereTrace; |
| 2753 | sqlite3WhereTrace = 0; |
| 2754 | #endif |
| 2755 | } |
| 2756 | static void restore_debug_trace_modes(void){ |
| 2757 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2758 | sqlite3SelectTrace = savedSelectTrace; |
| 2759 | #endif |
| 2760 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2761 | sqlite3WhereTrace = savedWhereTrace; |
| 2762 | #endif |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | ** Run a prepared statement |
| 2767 | */ |
| 2768 | static void exec_prepared_stmt( |
| 2769 | ShellState *pArg, /* Pointer to ShellState */ |
| 2770 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2771 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2772 | ){ |
| 2773 | int rc; |
| 2774 | |
| 2775 | /* perform the first step. this will tell us if we |
| 2776 | ** have a result set or not and how wide it is. |
| 2777 | */ |
| 2778 | rc = sqlite3_step(pStmt); |
| 2779 | /* if we have a result set... */ |
| 2780 | if( SQLITE_ROW == rc ){ |
| 2781 | /* if we have a callback... */ |
| 2782 | if( xCallback ){ |
| 2783 | /* allocate space for col name ptr, value ptr, and type */ |
| 2784 | int nCol = sqlite3_column_count(pStmt); |
| 2785 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2786 | if( !pData ){ |
| 2787 | rc = SQLITE_NOMEM; |
| 2788 | }else{ |
| 2789 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2790 | char **azVals = &azCols[nCol]; /* Results */ |
| 2791 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2792 | int i, x; |
| 2793 | assert(sizeof(int) <= sizeof(char *)); |
| 2794 | /* save off ptrs to column names */ |
| 2795 | for(i=0; i<nCol; i++){ |
| 2796 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2797 | } |
| 2798 | do{ |
| 2799 | /* extract the data and data types */ |
| 2800 | for(i=0; i<nCol; i++){ |
| 2801 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2802 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2803 | azVals[i] = ""; |
| 2804 | }else{ |
| 2805 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2806 | } |
| 2807 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2808 | rc = SQLITE_NOMEM; |
| 2809 | break; /* from for */ |
| 2810 | } |
| 2811 | } /* end for */ |
| 2812 | |
| 2813 | /* if data and types extracted successfully... */ |
| 2814 | if( SQLITE_ROW == rc ){ |
| 2815 | /* call the supplied callback with the result row data */ |
| 2816 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2817 | rc = SQLITE_ABORT; |
| 2818 | }else{ |
| 2819 | rc = sqlite3_step(pStmt); |
| 2820 | } |
| 2821 | } |
| 2822 | } while( SQLITE_ROW == rc ); |
| 2823 | sqlite3_free(pData); |
| 2824 | } |
| 2825 | }else{ |
| 2826 | do{ |
| 2827 | rc = sqlite3_step(pStmt); |
| 2828 | } while( rc == SQLITE_ROW ); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2834 | ** Execute a statement or set of statements. Print |
| 2835 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2836 | ** set via the supplied callback. |
| 2837 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2838 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2839 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2840 | ** and callback data argument. |
| 2841 | */ |
| 2842 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2843 | sqlite3 *db, /* An open database */ |
| 2844 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2845 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2846 | /* (not the same as sqlite3_exec) */ |
| 2847 | ShellState *pArg, /* Pointer to ShellState */ |
| 2848 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2849 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2850 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2851 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2852 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2853 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2854 | |
| 2855 | if( pzErrMsg ){ |
| 2856 | *pzErrMsg = NULL; |
| 2857 | } |
| 2858 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2859 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2860 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2861 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2862 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2863 | if( pzErrMsg ){ |
| 2864 | *pzErrMsg = save_err_msg(db); |
| 2865 | } |
| 2866 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2867 | if( !pStmt ){ |
| 2868 | /* this happens for a comment or white-space */ |
| 2869 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2870 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2871 | continue; |
| 2872 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2873 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 2874 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2875 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2876 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2877 | /* save off the prepared statment handle and reset row count */ |
| 2878 | if( pArg ){ |
| 2879 | pArg->pStmt = pStmt; |
| 2880 | pArg->cnt = 0; |
| 2881 | } |
| 2882 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2883 | /* echo the sql statement if echo on */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2884 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2885 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 2886 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2887 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2888 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2889 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2890 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2891 | char *zEQP; |
| 2892 | disable_debug_trace_modes(); |
| 2893 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2894 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2895 | if( rc==SQLITE_OK ){ |
| 2896 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2897 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2898 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2899 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2900 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | sqlite3_finalize(pExplain); |
| 2904 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2905 | if( pArg->autoEQP>=2 ){ |
| 2906 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2907 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2908 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2909 | if( rc==SQLITE_OK ){ |
| 2910 | pArg->cMode = MODE_Explain; |
| 2911 | explain_data_prepare(pArg, pExplain); |
| 2912 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2913 | explain_data_delete(pArg); |
| 2914 | } |
| 2915 | sqlite3_finalize(pExplain); |
| 2916 | sqlite3_free(zEQP); |
| 2917 | } |
| 2918 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2921 | if( pArg ){ |
| 2922 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2923 | if( pArg->autoExplain |
| 2924 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2925 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2926 | ){ |
| 2927 | pArg->cMode = MODE_Explain; |
| 2928 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2929 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2930 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2931 | ** data required to add indents to the output.*/ |
| 2932 | if( pArg->cMode==MODE_Explain ){ |
| 2933 | explain_data_prepare(pArg, pStmt); |
| 2934 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2937 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2938 | explain_data_delete(pArg); |
| 2939 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2940 | /* print usage stats if stats on */ |
| 2941 | if( pArg && pArg->statsOn ){ |
| 2942 | display_stats(db, pArg, 0); |
| 2943 | } |
| 2944 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2945 | /* print loop-counters if required */ |
| 2946 | if( pArg && pArg->scanstatsOn ){ |
| 2947 | display_scanstats(db, pArg); |
| 2948 | } |
| 2949 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2950 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2951 | ** copy of the error message. Otherwise, set zSql to point to the |
| 2952 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2953 | rc2 = sqlite3_finalize(pStmt); |
| 2954 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2955 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2956 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2957 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2958 | }else if( pzErrMsg ){ |
| 2959 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2960 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2961 | |
| 2962 | /* clear saved stmt handle */ |
| 2963 | if( pArg ){ |
| 2964 | pArg->pStmt = NULL; |
| 2965 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2966 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2967 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2968 | |
| 2969 | return rc; |
| 2970 | } |
| 2971 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2972 | /* |
| 2973 | ** Release memory previously allocated by tableColumnList(). |
| 2974 | */ |
| 2975 | static void freeColumnList(char **azCol){ |
| 2976 | int i; |
| 2977 | for(i=1; azCol[i]; i++){ |
| 2978 | sqlite3_free(azCol[i]); |
| 2979 | } |
| 2980 | /* azCol[0] is a static string */ |
| 2981 | sqlite3_free(azCol); |
| 2982 | } |
| 2983 | |
| 2984 | /* |
| 2985 | ** Return a list of pointers to strings which are the names of all |
| 2986 | ** columns in table zTab. The memory to hold the names is dynamically |
| 2987 | ** allocated and must be released by the caller using a subsequent call |
| 2988 | ** to freeColumnList(). |
| 2989 | ** |
| 2990 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 2991 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 2992 | ** name of the rowid column. |
| 2993 | ** |
| 2994 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 2995 | ** by an entry with azCol[i]==0. |
| 2996 | */ |
| 2997 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 2998 | char **azCol = 0; |
| 2999 | sqlite3_stmt *pStmt; |
| 3000 | char *zSql; |
| 3001 | int nCol = 0; |
| 3002 | int nAlloc = 0; |
| 3003 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 3004 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3005 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3006 | int rc; |
| 3007 | |
| 3008 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 3009 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3010 | sqlite3_free(zSql); |
| 3011 | if( rc ) return 0; |
| 3012 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3013 | if( nCol>=nAlloc-2 ){ |
| 3014 | nAlloc = nAlloc*2 + nCol + 10; |
| 3015 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 3016 | if( azCol==0 ){ |
| 3017 | raw_printf(stderr, "Error: out of memory\n"); |
| 3018 | exit(1); |
| 3019 | } |
| 3020 | } |
| 3021 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 3022 | if( sqlite3_column_int(pStmt, 5) ){ |
| 3023 | nPK++; |
| 3024 | if( nPK==1 |
| 3025 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3026 | "INTEGER")==0 |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3027 | ){ |
| 3028 | isIPK = 1; |
| 3029 | }else{ |
| 3030 | isIPK = 0; |
| 3031 | } |
| 3032 | } |
| 3033 | } |
| 3034 | sqlite3_finalize(pStmt); |
| 3035 | azCol[0] = 0; |
| 3036 | azCol[nCol+1] = 0; |
| 3037 | |
| 3038 | /* The decision of whether or not a rowid really needs to be preserved |
| 3039 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3040 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3041 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3042 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3043 | */ |
| 3044 | if( preserveRowid && isIPK ){ |
| 3045 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 3046 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 3047 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 3048 | ** ROWID aliases. To distinguish these cases, check to see if |
| 3049 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 3050 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 3051 | */ |
| 3052 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 3053 | " WHERE origin='pk'", zTab); |
| 3054 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3055 | sqlite3_free(zSql); |
| 3056 | if( rc ){ |
| 3057 | freeColumnList(azCol); |
| 3058 | return 0; |
| 3059 | } |
| 3060 | rc = sqlite3_step(pStmt); |
| 3061 | sqlite3_finalize(pStmt); |
| 3062 | preserveRowid = rc==SQLITE_ROW; |
| 3063 | } |
| 3064 | if( preserveRowid ){ |
| 3065 | /* Only preserve the rowid if we can find a name to use for the |
| 3066 | ** rowid */ |
| 3067 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3068 | int i, j; |
| 3069 | for(j=0; j<3; j++){ |
| 3070 | for(i=1; i<=nCol; i++){ |
| 3071 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3072 | } |
| 3073 | if( i>nCol ){ |
| 3074 | /* At this point, we know that azRowid[j] is not the name of any |
| 3075 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3076 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3077 | ** tables will fail this last check */ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3078 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3079 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3080 | break; |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | return azCol; |
| 3085 | } |
| 3086 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3087 | /* |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3088 | ** Toggle the reverse_unordered_selects setting. |
| 3089 | */ |
| 3090 | static void toggleSelectOrder(sqlite3 *db){ |
| 3091 | sqlite3_stmt *pStmt = 0; |
| 3092 | int iSetting = 0; |
| 3093 | char zStmt[100]; |
| 3094 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3095 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3096 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3097 | } |
| 3098 | sqlite3_finalize(pStmt); |
| 3099 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3100 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3101 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3102 | } |
| 3103 | |
| 3104 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3105 | ** This is a different callback routine used for dumping the database. |
| 3106 | ** Each row received by this callback consists of a table name, |
| 3107 | ** the table type ("index" or "table") and SQL to create the table. |
| 3108 | ** This routine should print text sufficient to recreate the table. |
| 3109 | */ |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3110 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3111 | int rc; |
| 3112 | const char *zTable; |
| 3113 | const char *zType; |
| 3114 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3115 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3116 | |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3117 | UNUSED_PARAMETER(azNotUsed); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3118 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3119 | zTable = azArg[0]; |
| 3120 | zType = azArg[1]; |
| 3121 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3122 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3123 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3124 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 3125 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3126 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3127 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3128 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3129 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3130 | char *zIns; |
| 3131 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3132 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3133 | p->writableSchema = 1; |
| 3134 | } |
| 3135 | zIns = sqlite3_mprintf( |
| 3136 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3137 | "VALUES('table','%q','%q',0,'%q');", |
| 3138 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3139 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3140 | sqlite3_free(zIns); |
| 3141 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3142 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 3143 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 3144 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3145 | |
| 3146 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3147 | ShellText sSelect; |
| 3148 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3149 | char **azCol; |
| 3150 | int i; |
| 3151 | char *savedDestTable; |
| 3152 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3153 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3154 | azCol = tableColumnList(p, zTable); |
| 3155 | if( azCol==0 ){ |
| 3156 | p->nErr++; |
| 3157 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 3160 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3161 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3162 | initText(&sTable); |
| 3163 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3164 | /* If preserving the rowid, add a column list after the table name. |
| 3165 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3166 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3167 | */ |
| 3168 | if( azCol[0] ){ |
| 3169 | appendText(&sTable, "(", 0); |
| 3170 | appendText(&sTable, azCol[0], 0); |
| 3171 | for(i=1; azCol[i]; i++){ |
| 3172 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3173 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3174 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3175 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3176 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3177 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3178 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3179 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3180 | appendText(&sSelect, "SELECT ", 0); |
| 3181 | if( azCol[0] ){ |
| 3182 | appendText(&sSelect, azCol[0], 0); |
| 3183 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3184 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3185 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3186 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3187 | if( azCol[i+1] ){ |
| 3188 | appendText(&sSelect, ",", 0); |
| 3189 | } |
| 3190 | } |
| 3191 | freeColumnList(azCol); |
| 3192 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3193 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3194 | |
| 3195 | savedDestTable = p->zDestTable; |
| 3196 | savedMode = p->mode; |
| 3197 | p->zDestTable = sTable.z; |
| 3198 | p->mode = p->cMode = MODE_Insert; |
| 3199 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3200 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3201 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3202 | toggleSelectOrder(p->db); |
| 3203 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3204 | toggleSelectOrder(p->db); |
| 3205 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3206 | p->zDestTable = savedDestTable; |
| 3207 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3208 | freeText(&sTable); |
| 3209 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3210 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3211 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3216 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3217 | ** the contents of the query are output as SQL statements. |
| 3218 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3219 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3220 | ** "ORDER BY rowid DESC" to the end. |
| 3221 | */ |
| 3222 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3223 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3224 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3225 | ){ |
| 3226 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3227 | char *zErr = 0; |
| 3228 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3229 | if( rc==SQLITE_CORRUPT ){ |
| 3230 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3231 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3232 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3233 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3234 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3235 | sqlite3_free(zErr); |
| 3236 | zErr = 0; |
| 3237 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3238 | zQ2 = malloc( len+100 ); |
| 3239 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 3240 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3241 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3242 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3243 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3244 | }else{ |
| 3245 | rc = SQLITE_CORRUPT; |
| 3246 | } |
| 3247 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3248 | free(zQ2); |
| 3249 | } |
| 3250 | return rc; |
| 3251 | } |
| 3252 | |
| 3253 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3254 | ** Text of a help message |
| 3255 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 3256 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3257 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3258 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3259 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3260 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3261 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3262 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 3263 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3264 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3265 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3266 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3267 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3268 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3269 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3270 | " If TABLE specified, only dump tables matching\n" |
| 3271 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3272 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3273 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3274 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3275 | ".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] | 3276 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3277 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3278 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3279 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3280 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3281 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3282 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3283 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3284 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3285 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3286 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3287 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3288 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3289 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 3290 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3291 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3292 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 3293 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3294 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 3295 | ".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] | 3296 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3297 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 3298 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3299 | " column Left-aligned columns. (See .width)\n" |
| 3300 | " html HTML <table> code\n" |
| 3301 | " insert SQL insert statements for TABLE\n" |
| 3302 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 3303 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 3304 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3305 | " tabs Tab-separated values\n" |
| 3306 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3307 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3308 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3309 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n" |
| 3310 | " The --new option starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3311 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3312 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3313 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3314 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3315 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3316 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3317 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3318 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3319 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3320 | " Add --indent for pretty-printing\n" |
drh | a5d75ba | 2017-03-15 14:20:34 +0000 | [diff] [blame] | 3321 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3322 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3323 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3324 | #if defined(SQLITE_ENABLE_SESSION) |
| 3325 | ".session CMD ... Create or control sessions\n" |
| 3326 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3327 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3328 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3329 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3330 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3331 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3332 | ".tables ?TABLE? List names of tables\n" |
| 3333 | " If TABLE specified, only list tables matching\n" |
| 3334 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3335 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 3336 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3337 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3338 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 3339 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 3340 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3341 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 3342 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3343 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3344 | ; |
| 3345 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3346 | #if defined(SQLITE_ENABLE_SESSION) |
| 3347 | /* |
| 3348 | ** Print help information for the ".sessions" command |
| 3349 | */ |
| 3350 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 3351 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3352 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3353 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3354 | "Subcommands:\n" |
| 3355 | " attach TABLE Attach TABLE\n" |
| 3356 | " changeset FILE Write a changeset into FILE\n" |
| 3357 | " close Close one session\n" |
| 3358 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3359 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3360 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3361 | " isempty Query whether the session is empty\n" |
| 3362 | " list List currently open session names\n" |
| 3363 | " open DB NAME Open a new session on DB\n" |
| 3364 | " patchset FILE Write a patchset into FILE\n" |
| 3365 | ); |
| 3366 | } |
| 3367 | #endif |
| 3368 | |
| 3369 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3370 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3371 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3372 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3373 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3374 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3375 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3376 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3377 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3378 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3379 | ** read. |
| 3380 | ** |
| 3381 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3382 | ** from the file before the buffer is returned. This byte is not included in |
| 3383 | ** the final value of (*pnByte), if applicable. |
| 3384 | ** |
| 3385 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3386 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3387 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3388 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3389 | FILE *in = fopen(zName, "rb"); |
| 3390 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3391 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3392 | char *pBuf; |
| 3393 | if( in==0 ) return 0; |
| 3394 | fseek(in, 0, SEEK_END); |
| 3395 | nIn = ftell(in); |
| 3396 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3397 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3398 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3399 | nRead = fread(pBuf, nIn, 1, in); |
| 3400 | fclose(in); |
| 3401 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3402 | sqlite3_free(pBuf); |
| 3403 | return 0; |
| 3404 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3405 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3406 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3407 | return pBuf; |
| 3408 | } |
| 3409 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3410 | /* |
| 3411 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 3412 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 3413 | ** if the file does not exist or is unreadable. |
| 3414 | */ |
| 3415 | static void readfileFunc( |
| 3416 | sqlite3_context *context, |
| 3417 | int argc, |
| 3418 | sqlite3_value **argv |
| 3419 | ){ |
| 3420 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3421 | void *pBuf; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3422 | int nBuf; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3423 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3424 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3425 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 3426 | if( zName==0 ) return; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3427 | pBuf = readFile(zName, &nBuf); |
| 3428 | if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | /* |
| 3432 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3433 | ** is written into file X. The number of bytes written is returned. Or |
| 3434 | ** NULL is returned if something goes wrong, such as being unable to open |
| 3435 | ** file X for writing. |
| 3436 | */ |
| 3437 | static void writefileFunc( |
| 3438 | sqlite3_context *context, |
| 3439 | int argc, |
| 3440 | sqlite3_value **argv |
| 3441 | ){ |
| 3442 | FILE *out; |
| 3443 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3444 | sqlite3_int64 rc; |
| 3445 | const char *zFile; |
| 3446 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3447 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3448 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3449 | if( zFile==0 ) return; |
| 3450 | out = fopen(zFile, "wb"); |
| 3451 | if( out==0 ) return; |
| 3452 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 3453 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3454 | rc = 0; |
| 3455 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 3456 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3457 | } |
| 3458 | fclose(out); |
| 3459 | sqlite3_result_int64(context, rc); |
| 3460 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3461 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3462 | #if defined(SQLITE_ENABLE_SESSION) |
| 3463 | /* |
| 3464 | ** Close a single OpenSession object and release all of its associated |
| 3465 | ** resources. |
| 3466 | */ |
| 3467 | static void session_close(OpenSession *pSession){ |
| 3468 | int i; |
| 3469 | sqlite3session_delete(pSession->p); |
| 3470 | sqlite3_free(pSession->zName); |
| 3471 | for(i=0; i<pSession->nFilter; i++){ |
| 3472 | sqlite3_free(pSession->azFilter[i]); |
| 3473 | } |
| 3474 | sqlite3_free(pSession->azFilter); |
| 3475 | memset(pSession, 0, sizeof(OpenSession)); |
| 3476 | } |
| 3477 | #endif |
| 3478 | |
| 3479 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3480 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3481 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3482 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3483 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3484 | int i; |
| 3485 | for(i=0; i<p->nSession; i++){ |
| 3486 | session_close(&p->aSession[i]); |
| 3487 | } |
| 3488 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3489 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3490 | #else |
| 3491 | # define session_close_all(X) |
| 3492 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3493 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3494 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 3495 | ** Implementation of the xFilter function for an open session. Omit |
| 3496 | ** any tables named by ".session filter" but let all other table through. |
| 3497 | */ |
| 3498 | #if defined(SQLITE_ENABLE_SESSION) |
| 3499 | static int session_filter(void *pCtx, const char *zTab){ |
| 3500 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3501 | int i; |
| 3502 | for(i=0; i<pSession->nFilter; i++){ |
| 3503 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3504 | } |
| 3505 | return 1; |
| 3506 | } |
| 3507 | #endif |
| 3508 | |
| 3509 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3510 | ** Make sure the database is open. If it is not, then open it. If |
| 3511 | ** the database fails to open, print an error message and exit. |
| 3512 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3513 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3514 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 3515 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3516 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3517 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3518 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3519 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3520 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3521 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3522 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3523 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 3524 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3525 | sqlite3_enable_load_extension(p->db, 1); |
| 3526 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3527 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3528 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3529 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3530 | writefileFunc, 0, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3531 | sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3532 | sha3Func, 0, 0); |
| 3533 | sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3534 | sha3Func, 0, 0); |
| 3535 | sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3536 | sha3QueryFunc, 0, 0); |
| 3537 | sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3538 | sha3QueryFunc, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3543 | ** Do C-language style dequoting. |
| 3544 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3545 | ** \a -> alarm |
| 3546 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3547 | ** \t -> tab |
| 3548 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3549 | ** \v -> vertical tab |
| 3550 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3551 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3552 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3553 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3554 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3555 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3556 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3557 | */ |
| 3558 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3559 | int i, j; |
| 3560 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3561 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3562 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 3563 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3564 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3565 | if( c=='a' ){ |
| 3566 | c = '\a'; |
| 3567 | }else if( c=='b' ){ |
| 3568 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3569 | }else if( c=='t' ){ |
| 3570 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3571 | }else if( c=='n' ){ |
| 3572 | c = '\n'; |
| 3573 | }else if( c=='v' ){ |
| 3574 | c = '\v'; |
| 3575 | }else if( c=='f' ){ |
| 3576 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3577 | }else if( c=='r' ){ |
| 3578 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3579 | }else if( c=='"' ){ |
| 3580 | c = '"'; |
| 3581 | }else if( c=='\'' ){ |
| 3582 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3583 | }else if( c=='\\' ){ |
| 3584 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3585 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 3586 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3587 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3588 | i++; |
| 3589 | c = (c<<3) + z[i] - '0'; |
| 3590 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3591 | i++; |
| 3592 | c = (c<<3) + z[i] - '0'; |
| 3593 | } |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | z[j] = c; |
| 3598 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3599 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3603 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 3604 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3605 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3606 | static int hexDigitValue(char c){ |
| 3607 | if( c>='0' && c<='9' ) return c - '0'; |
| 3608 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 3609 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 3610 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3614 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 3615 | */ |
| 3616 | static sqlite3_int64 integerValue(const char *zArg){ |
| 3617 | sqlite3_int64 v = 0; |
| 3618 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 3619 | { "KiB", 1024 }, |
| 3620 | { "MiB", 1024*1024 }, |
| 3621 | { "GiB", 1024*1024*1024 }, |
| 3622 | { "KB", 1000 }, |
| 3623 | { "MB", 1000000 }, |
| 3624 | { "GB", 1000000000 }, |
| 3625 | { "K", 1000 }, |
| 3626 | { "M", 1000000 }, |
| 3627 | { "G", 1000000000 }, |
| 3628 | }; |
| 3629 | int i; |
| 3630 | int isNeg = 0; |
| 3631 | if( zArg[0]=='-' ){ |
| 3632 | isNeg = 1; |
| 3633 | zArg++; |
| 3634 | }else if( zArg[0]=='+' ){ |
| 3635 | zArg++; |
| 3636 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3637 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3638 | int x; |
| 3639 | zArg += 2; |
| 3640 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 3641 | v = (v<<4) + x; |
| 3642 | zArg++; |
| 3643 | } |
| 3644 | }else{ |
| 3645 | while( IsDigit(zArg[0]) ){ |
| 3646 | v = v*10 + zArg[0] - '0'; |
| 3647 | zArg++; |
| 3648 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3649 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 3650 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3651 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 3652 | v *= aMult[i].iMult; |
| 3653 | break; |
| 3654 | } |
| 3655 | } |
| 3656 | return isNeg? -v : v; |
| 3657 | } |
| 3658 | |
| 3659 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3660 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3661 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3662 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3663 | static int booleanValue(const char *zArg){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3664 | int i; |
| 3665 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3666 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3667 | }else{ |
| 3668 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3669 | } |
| 3670 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3671 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3672 | return 1; |
| 3673 | } |
| 3674 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3675 | return 0; |
| 3676 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3677 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3678 | zArg); |
| 3679 | return 0; |
| 3680 | } |
| 3681 | |
| 3682 | /* |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3683 | ** Set or clear a shell flag according to a boolean value. |
| 3684 | */ |
| 3685 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3686 | if( booleanValue(zArg) ){ |
| 3687 | ShellSetFlag(p, mFlag); |
| 3688 | }else{ |
| 3689 | ShellClearFlag(p, mFlag); |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3694 | ** Close an output file, assuming it is not stderr or stdout |
| 3695 | */ |
| 3696 | static void output_file_close(FILE *f){ |
| 3697 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3698 | } |
| 3699 | |
| 3700 | /* |
| 3701 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3702 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3703 | ** filename is "off". |
| 3704 | */ |
| 3705 | static FILE *output_file_open(const char *zFile){ |
| 3706 | FILE *f; |
| 3707 | if( strcmp(zFile,"stdout")==0 ){ |
| 3708 | f = stdout; |
| 3709 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3710 | f = stderr; |
| 3711 | }else if( strcmp(zFile, "off")==0 ){ |
| 3712 | f = 0; |
| 3713 | }else{ |
| 3714 | f = fopen(zFile, "wb"); |
| 3715 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3716 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | return f; |
| 3720 | } |
| 3721 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 3722 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3723 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3724 | /* |
| 3725 | ** A routine for handling output from sqlite3_trace(). |
| 3726 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3727 | static int sql_trace_callback( |
| 3728 | unsigned mType, |
| 3729 | void *pArg, |
| 3730 | void *pP, |
| 3731 | void *pX |
| 3732 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3733 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 3734 | UNUSED_PARAMETER(mType); |
| 3735 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3736 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3737 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3738 | int i = (int)strlen(z); |
| 3739 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3740 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3741 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3742 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3743 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3744 | #endif |
| 3745 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3746 | |
| 3747 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3748 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3749 | ** a useful spot to set a debugger breakpoint. |
| 3750 | */ |
| 3751 | static void test_breakpoint(void){ |
| 3752 | static int nCall = 0; |
| 3753 | nCall++; |
| 3754 | } |
| 3755 | |
| 3756 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3757 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3758 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3759 | typedef struct ImportCtx ImportCtx; |
| 3760 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3761 | const char *zFile; /* Name of the input file */ |
| 3762 | FILE *in; /* Read the CSV text from this input stream */ |
| 3763 | char *z; /* Accumulated text for a field */ |
| 3764 | int n; /* Number of bytes in z */ |
| 3765 | int nAlloc; /* Space allocated for z[] */ |
| 3766 | int nLine; /* Current line number */ |
| 3767 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3768 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3769 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3770 | }; |
| 3771 | |
| 3772 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3773 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3774 | if( p->n+1>=p->nAlloc ){ |
| 3775 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3776 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3777 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3778 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3779 | exit(1); |
| 3780 | } |
| 3781 | } |
| 3782 | p->z[p->n++] = (char)c; |
| 3783 | } |
| 3784 | |
| 3785 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3786 | ** with the option of having a separator other than ",". |
| 3787 | ** |
| 3788 | ** + Input comes from p->in. |
| 3789 | ** + 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] | 3790 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3791 | ** + Use p->cSep as the column separator. The default is ",". |
| 3792 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3793 | ** + Keep track of the line number in p->nLine. |
| 3794 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3795 | ** EOF on end-of-file. |
| 3796 | ** + Report syntax errors on stderr |
| 3797 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3798 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3799 | int c; |
| 3800 | int cSep = p->cColSep; |
| 3801 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3802 | p->n = 0; |
| 3803 | c = fgetc(p->in); |
| 3804 | if( c==EOF || seenInterrupt ){ |
| 3805 | p->cTerm = EOF; |
| 3806 | return 0; |
| 3807 | } |
| 3808 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3809 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3810 | int startLine = p->nLine; |
| 3811 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3812 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3813 | while( 1 ){ |
| 3814 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3815 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3816 | if( c==cQuote ){ |
| 3817 | if( pc==cQuote ){ |
| 3818 | pc = 0; |
| 3819 | continue; |
| 3820 | } |
| 3821 | } |
| 3822 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3823 | || (c==rSep && pc==cQuote) |
| 3824 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3825 | || (c==EOF && pc==cQuote) |
| 3826 | ){ |
| 3827 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3828 | p->cTerm = c; |
| 3829 | break; |
| 3830 | } |
| 3831 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3832 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3833 | p->zFile, p->nLine, cQuote); |
| 3834 | } |
| 3835 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3836 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3837 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3838 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3839 | break; |
| 3840 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3841 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3842 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3843 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3844 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3845 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3846 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3847 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3848 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3849 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3850 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3851 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 3852 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3853 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3854 | p->cTerm = c; |
| 3855 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 3856 | if( p->z ) p->z[p->n] = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3857 | return p->z; |
| 3858 | } |
| 3859 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3860 | /* Read a single field of ASCII delimited text. |
| 3861 | ** |
| 3862 | ** + Input comes from p->in. |
| 3863 | ** + 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] | 3864 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3865 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3866 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3867 | ** + Keep track of the row number in p->nLine. |
| 3868 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3869 | ** EOF on end-of-file. |
| 3870 | ** + Report syntax errors on stderr |
| 3871 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3872 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3873 | int c; |
| 3874 | int cSep = p->cColSep; |
| 3875 | int rSep = p->cRowSep; |
| 3876 | p->n = 0; |
| 3877 | c = fgetc(p->in); |
| 3878 | if( c==EOF || seenInterrupt ){ |
| 3879 | p->cTerm = EOF; |
| 3880 | return 0; |
| 3881 | } |
| 3882 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3883 | import_append_char(p, c); |
| 3884 | c = fgetc(p->in); |
| 3885 | } |
| 3886 | if( c==rSep ){ |
| 3887 | p->nLine++; |
| 3888 | } |
| 3889 | p->cTerm = c; |
| 3890 | if( p->z ) p->z[p->n] = 0; |
| 3891 | return p->z; |
| 3892 | } |
| 3893 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3894 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3895 | ** Try to transfer data for table zTable. If an error is seen while |
| 3896 | ** moving forward, try to go backwards. The backwards movement won't |
| 3897 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3898 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3899 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3900 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3901 | sqlite3 *newDb, |
| 3902 | const char *zTable |
| 3903 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3904 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3905 | sqlite3_stmt *pInsert = 0; |
| 3906 | char *zQuery = 0; |
| 3907 | char *zInsert = 0; |
| 3908 | int rc; |
| 3909 | int i, j, n; |
| 3910 | int nTable = (int)strlen(zTable); |
| 3911 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3912 | int cnt = 0; |
| 3913 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3914 | |
| 3915 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3916 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3917 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3918 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3919 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3920 | zQuery); |
| 3921 | goto end_data_xfer; |
| 3922 | } |
| 3923 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3924 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3925 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3926 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3927 | goto end_data_xfer; |
| 3928 | } |
| 3929 | sqlite3_snprintf(200+nTable,zInsert, |
| 3930 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 3931 | i = (int)strlen(zInsert); |
| 3932 | for(j=1; j<n; j++){ |
| 3933 | memcpy(zInsert+i, ",?", 2); |
| 3934 | i += 2; |
| 3935 | } |
| 3936 | memcpy(zInsert+i, ");", 3); |
| 3937 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 3938 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3939 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3940 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 3941 | zQuery); |
| 3942 | goto end_data_xfer; |
| 3943 | } |
| 3944 | for(k=0; k<2; k++){ |
| 3945 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3946 | for(i=0; i<n; i++){ |
| 3947 | switch( sqlite3_column_type(pQuery, i) ){ |
| 3948 | case SQLITE_NULL: { |
| 3949 | sqlite3_bind_null(pInsert, i+1); |
| 3950 | break; |
| 3951 | } |
| 3952 | case SQLITE_INTEGER: { |
| 3953 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 3954 | break; |
| 3955 | } |
| 3956 | case SQLITE_FLOAT: { |
| 3957 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 3958 | break; |
| 3959 | } |
| 3960 | case SQLITE_TEXT: { |
| 3961 | sqlite3_bind_text(pInsert, i+1, |
| 3962 | (const char*)sqlite3_column_text(pQuery,i), |
| 3963 | -1, SQLITE_STATIC); |
| 3964 | break; |
| 3965 | } |
| 3966 | case SQLITE_BLOB: { |
| 3967 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 3968 | sqlite3_column_bytes(pQuery,i), |
| 3969 | SQLITE_STATIC); |
| 3970 | break; |
| 3971 | } |
| 3972 | } |
| 3973 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3974 | rc = sqlite3_step(pInsert); |
| 3975 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3976 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3977 | sqlite3_errmsg(newDb)); |
| 3978 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3979 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3980 | cnt++; |
| 3981 | if( (cnt%spinRate)==0 ){ |
| 3982 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 3983 | fflush(stdout); |
| 3984 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3985 | } /* End while */ |
| 3986 | if( rc==SQLITE_DONE ) break; |
| 3987 | sqlite3_finalize(pQuery); |
| 3988 | sqlite3_free(zQuery); |
| 3989 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 3990 | 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, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3994 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3995 | } |
| 3996 | } /* End for(k=0...) */ |
| 3997 | |
| 3998 | end_data_xfer: |
| 3999 | sqlite3_finalize(pQuery); |
| 4000 | sqlite3_finalize(pInsert); |
| 4001 | sqlite3_free(zQuery); |
| 4002 | sqlite3_free(zInsert); |
| 4003 | } |
| 4004 | |
| 4005 | |
| 4006 | /* |
| 4007 | ** Try to transfer all rows of the schema that match zWhere. For |
| 4008 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4009 | ** If an error is encountered while moving forward through the |
| 4010 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4011 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4012 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4013 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4014 | sqlite3 *newDb, |
| 4015 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4016 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4017 | ){ |
| 4018 | sqlite3_stmt *pQuery = 0; |
| 4019 | char *zQuery = 0; |
| 4020 | int rc; |
| 4021 | const unsigned char *zName; |
| 4022 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4023 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4024 | |
| 4025 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4026 | " WHERE %s", zWhere); |
| 4027 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4028 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4029 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4030 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4031 | zQuery); |
| 4032 | goto end_schema_xfer; |
| 4033 | } |
| 4034 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4035 | zName = sqlite3_column_text(pQuery, 0); |
| 4036 | zSql = sqlite3_column_text(pQuery, 1); |
| 4037 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4038 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4039 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4040 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4041 | sqlite3_free(zErrMsg); |
| 4042 | zErrMsg = 0; |
| 4043 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4044 | if( xForEach ){ |
| 4045 | xForEach(p, newDb, (const char*)zName); |
| 4046 | } |
| 4047 | printf("done\n"); |
| 4048 | } |
| 4049 | if( rc!=SQLITE_DONE ){ |
| 4050 | sqlite3_finalize(pQuery); |
| 4051 | sqlite3_free(zQuery); |
| 4052 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4053 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4054 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4055 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4056 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4057 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4058 | zQuery); |
| 4059 | goto end_schema_xfer; |
| 4060 | } |
| 4061 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4062 | zName = sqlite3_column_text(pQuery, 0); |
| 4063 | zSql = sqlite3_column_text(pQuery, 1); |
| 4064 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4065 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4066 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4067 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4068 | sqlite3_free(zErrMsg); |
| 4069 | zErrMsg = 0; |
| 4070 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4071 | if( xForEach ){ |
| 4072 | xForEach(p, newDb, (const char*)zName); |
| 4073 | } |
| 4074 | printf("done\n"); |
| 4075 | } |
| 4076 | } |
| 4077 | end_schema_xfer: |
| 4078 | sqlite3_finalize(pQuery); |
| 4079 | sqlite3_free(zQuery); |
| 4080 | } |
| 4081 | |
| 4082 | /* |
| 4083 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4084 | ** as possible out of the main database (which might be corrupt) and write it |
| 4085 | ** into zNewDb. |
| 4086 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4087 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4088 | int rc; |
| 4089 | sqlite3 *newDb = 0; |
| 4090 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4091 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4092 | return; |
| 4093 | } |
| 4094 | rc = sqlite3_open(zNewDb, &newDb); |
| 4095 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4096 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4097 | sqlite3_errmsg(newDb)); |
| 4098 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4099 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4100 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4101 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4102 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4103 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4104 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4105 | } |
| 4106 | sqlite3_close(newDb); |
| 4107 | } |
| 4108 | |
| 4109 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4110 | ** Change the output file back to stdout |
| 4111 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4112 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4113 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4114 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4115 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4116 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4117 | }else{ |
| 4118 | output_file_close(p->out); |
| 4119 | } |
| 4120 | p->outfile[0] = 0; |
| 4121 | p->out = stdout; |
| 4122 | } |
| 4123 | |
| 4124 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4125 | ** Run an SQL command and return the single integer result. |
| 4126 | */ |
| 4127 | static int db_int(ShellState *p, const char *zSql){ |
| 4128 | sqlite3_stmt *pStmt; |
| 4129 | int res = 0; |
| 4130 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4131 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4132 | res = sqlite3_column_int(pStmt,0); |
| 4133 | } |
| 4134 | sqlite3_finalize(pStmt); |
| 4135 | return res; |
| 4136 | } |
| 4137 | |
| 4138 | /* |
| 4139 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4140 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4141 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4142 | return (a[0]<<8) + a[1]; |
| 4143 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4144 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4145 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4146 | } |
| 4147 | |
| 4148 | /* |
| 4149 | ** Implementation of the ".info" command. |
| 4150 | ** |
| 4151 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4152 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4153 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4154 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4155 | { "file change counter:", 24 }, |
| 4156 | { "database page count:", 28 }, |
| 4157 | { "freelist page count:", 36 }, |
| 4158 | { "schema cookie:", 40 }, |
| 4159 | { "schema format:", 44 }, |
| 4160 | { "default cache size:", 48 }, |
| 4161 | { "autovacuum top root:", 52 }, |
| 4162 | { "incremental vacuum:", 64 }, |
| 4163 | { "text encoding:", 56 }, |
| 4164 | { "user version:", 60 }, |
| 4165 | { "application id:", 68 }, |
| 4166 | { "software version:", 96 }, |
| 4167 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4168 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4169 | { "number of tables:", |
| 4170 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4171 | { "number of indexes:", |
| 4172 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4173 | { "number of triggers:", |
| 4174 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4175 | { "number of views:", |
| 4176 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4177 | { "schema size:", |
| 4178 | "SELECT total(length(sql)) FROM %s" }, |
| 4179 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 4180 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4181 | int i; |
| 4182 | char *zSchemaTab; |
| 4183 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 4184 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4185 | open_db(p, 0); |
| 4186 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4187 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4188 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 4189 | return 1; |
| 4190 | } |
| 4191 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 4192 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4193 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4194 | return 1; |
| 4195 | } |
| 4196 | i = get2byteInt(aHdr+16); |
| 4197 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4198 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4199 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4200 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4201 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4202 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4203 | int ofst = aField[i].ofst; |
| 4204 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4205 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4206 | switch( ofst ){ |
| 4207 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4208 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4209 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4210 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4211 | } |
| 4212 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4213 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4214 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4215 | if( zDb==0 ){ |
| 4216 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4217 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4218 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4219 | }else{ |
| 4220 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4221 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4222 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4223 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4224 | int val = db_int(p, zSql); |
| 4225 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4226 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4227 | } |
| 4228 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4229 | return 0; |
| 4230 | } |
| 4231 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4232 | /* |
| 4233 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4234 | */ |
| 4235 | static int shellDatabaseError(sqlite3 *db){ |
| 4236 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4237 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4238 | return 1; |
| 4239 | } |
| 4240 | |
| 4241 | /* |
| 4242 | ** Print an out-of-memory message to stderr and return 1. |
| 4243 | */ |
| 4244 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4245 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4246 | return 1; |
| 4247 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4248 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4249 | /* |
| 4250 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4251 | ** if they match and FALSE (0) if they do not match. |
| 4252 | ** |
| 4253 | ** Globbing rules: |
| 4254 | ** |
| 4255 | ** '*' Matches any sequence of zero or more characters. |
| 4256 | ** |
| 4257 | ** '?' Matches exactly one character. |
| 4258 | ** |
| 4259 | ** [...] Matches one character from the enclosed list of |
| 4260 | ** characters. |
| 4261 | ** |
| 4262 | ** [^...] Matches one character not in the enclosed list. |
| 4263 | ** |
| 4264 | ** '#' Matches any sequence of one or more digits with an |
| 4265 | ** optional + or - sign in front |
| 4266 | ** |
| 4267 | ** ' ' Any span of whitespace matches any other span of |
| 4268 | ** whitespace. |
| 4269 | ** |
| 4270 | ** Extra whitespace at the end of z[] is ignored. |
| 4271 | */ |
| 4272 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4273 | int c, c2; |
| 4274 | int invert; |
| 4275 | int seen; |
| 4276 | |
| 4277 | while( (c = (*(zGlob++)))!=0 ){ |
| 4278 | if( IsSpace(c) ){ |
| 4279 | if( !IsSpace(*z) ) return 0; |
| 4280 | while( IsSpace(*zGlob) ) zGlob++; |
| 4281 | while( IsSpace(*z) ) z++; |
| 4282 | }else if( c=='*' ){ |
| 4283 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4284 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4285 | } |
| 4286 | if( c==0 ){ |
| 4287 | return 1; |
| 4288 | }else if( c=='[' ){ |
| 4289 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4290 | z++; |
| 4291 | } |
| 4292 | return (*z)!=0; |
| 4293 | } |
| 4294 | while( (c2 = (*(z++)))!=0 ){ |
| 4295 | while( c2!=c ){ |
| 4296 | c2 = *(z++); |
| 4297 | if( c2==0 ) return 0; |
| 4298 | } |
| 4299 | if( testcase_glob(zGlob,z) ) return 1; |
| 4300 | } |
| 4301 | return 0; |
| 4302 | }else if( c=='?' ){ |
| 4303 | if( (*(z++))==0 ) return 0; |
| 4304 | }else if( c=='[' ){ |
| 4305 | int prior_c = 0; |
| 4306 | seen = 0; |
| 4307 | invert = 0; |
| 4308 | c = *(z++); |
| 4309 | if( c==0 ) return 0; |
| 4310 | c2 = *(zGlob++); |
| 4311 | if( c2=='^' ){ |
| 4312 | invert = 1; |
| 4313 | c2 = *(zGlob++); |
| 4314 | } |
| 4315 | if( c2==']' ){ |
| 4316 | if( c==']' ) seen = 1; |
| 4317 | c2 = *(zGlob++); |
| 4318 | } |
| 4319 | while( c2 && c2!=']' ){ |
| 4320 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4321 | c2 = *(zGlob++); |
| 4322 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4323 | prior_c = 0; |
| 4324 | }else{ |
| 4325 | if( c==c2 ){ |
| 4326 | seen = 1; |
| 4327 | } |
| 4328 | prior_c = c2; |
| 4329 | } |
| 4330 | c2 = *(zGlob++); |
| 4331 | } |
| 4332 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4333 | }else if( c=='#' ){ |
| 4334 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4335 | if( !IsDigit(z[0]) ) return 0; |
| 4336 | z++; |
| 4337 | while( IsDigit(z[0]) ){ z++; } |
| 4338 | }else{ |
| 4339 | if( c!=(*(z++)) ) return 0; |
| 4340 | } |
| 4341 | } |
| 4342 | while( IsSpace(*z) ){ z++; } |
| 4343 | return *z==0; |
| 4344 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4345 | |
| 4346 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4347 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4348 | ** Compare the string as a command-line option with either one or two |
| 4349 | ** initial "-" characters. |
| 4350 | */ |
| 4351 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4352 | if( zStr[0]!='-' ) return 0; |
| 4353 | zStr++; |
| 4354 | if( zStr[0]=='-' ) zStr++; |
| 4355 | return strcmp(zStr, zOpt)==0; |
| 4356 | } |
| 4357 | |
| 4358 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4359 | ** Delete a file. |
| 4360 | */ |
| 4361 | int shellDeleteFile(const char *zFilename){ |
| 4362 | int rc; |
| 4363 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4364 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4365 | rc = _wunlink(z); |
| 4366 | sqlite3_free(z); |
| 4367 | #else |
| 4368 | rc = unlink(zFilename); |
| 4369 | #endif |
| 4370 | return rc; |
| 4371 | } |
| 4372 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4373 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4374 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4375 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4376 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4377 | ** called with four arguments - the parent table name, the parent column name, |
| 4378 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4379 | ** |
| 4380 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4381 | ** |
| 4382 | ** If either of the named tables or columns do not exist, this function |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4383 | ** returns an empty string. An empty string is also returned if both tables |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4384 | ** and columns exist but have the same default collation sequence. Or, |
| 4385 | ** if both exist but the default collation sequences are different, this |
| 4386 | ** function returns the string " COLLATE <parent-collation>", where |
| 4387 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4388 | */ |
| 4389 | static void shellFkeyCollateClause( |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4390 | sqlite3_context *pCtx, |
| 4391 | int nVal, |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4392 | sqlite3_value **apVal |
| 4393 | ){ |
| 4394 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4395 | const char *zParent; |
| 4396 | const char *zParentCol; |
| 4397 | const char *zParentSeq; |
| 4398 | const char *zChild; |
| 4399 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4400 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4401 | int rc; |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4402 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4403 | assert( nVal==4 ); |
| 4404 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4405 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4406 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4407 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4408 | |
| 4409 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4410 | rc = sqlite3_table_column_metadata( |
| 4411 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4412 | ); |
| 4413 | if( rc==SQLITE_OK ){ |
| 4414 | rc = sqlite3_table_column_metadata( |
| 4415 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4416 | ); |
| 4417 | } |
| 4418 | |
| 4419 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4420 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4421 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4422 | sqlite3_free(z); |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4428 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4429 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4430 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4431 | ShellState *pState, /* Current shell tool state */ |
| 4432 | char **azArg, /* Array of arguments passed to dot command */ |
| 4433 | int nArg /* Number of entries in azArg[] */ |
| 4434 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4435 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4436 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4437 | int bVerbose = 0; /* If -verbose is present */ |
| 4438 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4439 | int i; /* To iterate through azArg[] */ |
| 4440 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4441 | int rc; /* Return code */ |
| 4442 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4443 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4444 | /* |
| 4445 | ** This SELECT statement returns one row for each foreign key constraint |
| 4446 | ** in the schema of the main database. The column values are: |
| 4447 | ** |
| 4448 | ** 0. The text of an SQL statement similar to: |
| 4449 | ** |
| 4450 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 4451 | ** |
| 4452 | ** This is the same SELECT that the foreign keys implementation needs |
| 4453 | ** to run internally on child tables. If there is an index that can |
| 4454 | ** be used to optimize this query, then it can also be used by the FK |
| 4455 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4456 | ** table. |
| 4457 | ** |
| 4458 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4459 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4460 | ** contains an index that can be used to optimize the query. |
| 4461 | ** |
| 4462 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4463 | ** |
| 4464 | ** "child_table(child_key1, child_key2)" |
| 4465 | ** |
| 4466 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4467 | ** |
| 4468 | ** "parent_table(parent_key1, parent_key2)" |
| 4469 | ** |
| 4470 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4471 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4472 | ** |
| 4473 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4474 | ** |
| 4475 | ** 5. The name of the parent table. |
| 4476 | ** |
| 4477 | ** These six values are used by the C logic below to generate the report. |
| 4478 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4479 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4480 | "SELECT " |
| 4481 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 4482 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4483 | " || fkey_collate_clause(" |
| 4484 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4485 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4486 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4487 | " || group_concat('*=?', ' AND ') || ')'" |
| 4488 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4489 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4490 | ", " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4491 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4492 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4493 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4494 | " || ' ON ' || quote(s.name) || '('" |
| 4495 | " || group_concat(quote(f.[from]) ||" |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4496 | " fkey_collate_clause(" |
| 4497 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4498 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4499 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4500 | " f.[table] " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4501 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4502 | "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] | 4503 | "GROUP BY s.name, f.id " |
| 4504 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4505 | ; |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4506 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4507 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4508 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 4509 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4510 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4511 | bVerbose = 1; |
| 4512 | } |
| 4513 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4514 | bGroupByParent = 1; |
| 4515 | zIndent = " "; |
| 4516 | } |
| 4517 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4518 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4519 | azArg[0], azArg[1] |
| 4520 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4521 | return SQLITE_ERROR; |
| 4522 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4523 | } |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4524 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4525 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4526 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4527 | 0, shellFkeyCollateClause, 0, 0 |
| 4528 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4529 | |
| 4530 | |
| 4531 | if( rc==SQLITE_OK ){ |
| 4532 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4533 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4534 | if( rc==SQLITE_OK ){ |
| 4535 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4536 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4537 | |
| 4538 | if( rc==SQLITE_OK ){ |
| 4539 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4540 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4541 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4542 | int res = -1; |
| 4543 | sqlite3_stmt *pExplain = 0; |
| 4544 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4545 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4546 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4547 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4548 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4549 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4550 | |
| 4551 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4552 | if( rc!=SQLITE_OK ) break; |
| 4553 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4554 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4555 | res = ( |
| 4556 | 0==sqlite3_strglob(zGlob, zPlan) |
| 4557 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 4558 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4559 | } |
| 4560 | rc = sqlite3_finalize(pExplain); |
| 4561 | if( rc!=SQLITE_OK ) break; |
| 4562 | |
| 4563 | if( res<0 ){ |
| 4564 | raw_printf(stderr, "Error: internal error"); |
| 4565 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4566 | }else{ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4567 | if( bGroupByParent |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4568 | && (bVerbose || res==0) |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4569 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4570 | ){ |
| 4571 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4572 | sqlite3_free(zPrev); |
| 4573 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4574 | } |
| 4575 | |
| 4576 | if( res==0 ){ |
| 4577 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4578 | }else if( bVerbose ){ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4579 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4580 | zIndent, zFrom, zTarget |
| 4581 | ); |
| 4582 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4583 | } |
| 4584 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4585 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4586 | |
| 4587 | if( rc!=SQLITE_OK ){ |
| 4588 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4589 | } |
| 4590 | |
| 4591 | rc2 = sqlite3_finalize(pSql); |
| 4592 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4593 | rc = rc2; |
| 4594 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4595 | } |
| 4596 | }else{ |
| 4597 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4598 | } |
| 4599 | |
| 4600 | return rc; |
| 4601 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4602 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4603 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4604 | ** Implementation of ".lint" dot command. |
| 4605 | */ |
| 4606 | static int lintDotCommand( |
| 4607 | ShellState *pState, /* Current shell tool state */ |
| 4608 | char **azArg, /* Array of arguments passed to dot command */ |
| 4609 | int nArg /* Number of entries in azArg[] */ |
| 4610 | ){ |
| 4611 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4612 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4613 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4614 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4615 | |
| 4616 | usage: |
| 4617 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4618 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4619 | raw_printf(stderr, " fkey-indexes\n"); |
| 4620 | return SQLITE_ERROR; |
| 4621 | } |
| 4622 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4623 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4624 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4625 | ** If an input line begins with "." then invoke this routine to |
| 4626 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4627 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4628 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4629 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4630 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4631 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4632 | int nArg = 0; |
| 4633 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4634 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4635 | char *azArg[50]; |
| 4636 | |
| 4637 | /* Parse the input line into tokens. |
| 4638 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4639 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 4640 | while( IsSpace(zLine[h]) ){ h++; } |
| 4641 | if( zLine[h]==0 ) break; |
| 4642 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 4643 | int delim = zLine[h++]; |
| 4644 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4645 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4646 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4647 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4648 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4649 | if( zLine[h]==delim ){ |
| 4650 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4651 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4652 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4653 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4654 | azArg[nArg++] = &zLine[h]; |
| 4655 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 4656 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4657 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | /* Process the input line. |
| 4662 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4663 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4664 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4665 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4666 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4667 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4668 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 4669 | if( nArg!=2 ){ |
| 4670 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 4671 | rc = 1; |
| 4672 | goto meta_command_exit; |
| 4673 | } |
| 4674 | open_db(p, 0); |
| 4675 | if( booleanValue(azArg[1]) ){ |
| 4676 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 4677 | }else{ |
| 4678 | sqlite3_set_authorizer(p->db, 0, 0); |
| 4679 | } |
| 4680 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4681 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4682 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4683 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 4684 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 4685 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4686 | const char *zDestFile = 0; |
| 4687 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4688 | sqlite3 *pDest; |
| 4689 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4690 | int j; |
| 4691 | for(j=1; j<nArg; j++){ |
| 4692 | const char *z = azArg[j]; |
| 4693 | if( z[0]=='-' ){ |
| 4694 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4695 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4696 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4697 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4698 | return 1; |
| 4699 | } |
| 4700 | }else if( zDestFile==0 ){ |
| 4701 | zDestFile = azArg[j]; |
| 4702 | }else if( zDb==0 ){ |
| 4703 | zDb = zDestFile; |
| 4704 | zDestFile = azArg[j]; |
| 4705 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4706 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4707 | return 1; |
| 4708 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4709 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4710 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4711 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4712 | return 1; |
| 4713 | } |
| 4714 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4715 | rc = sqlite3_open(zDestFile, &pDest); |
| 4716 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4717 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4718 | sqlite3_close(pDest); |
| 4719 | return 1; |
| 4720 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4721 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4722 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 4723 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4724 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4725 | sqlite3_close(pDest); |
| 4726 | return 1; |
| 4727 | } |
| 4728 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 4729 | sqlite3_backup_finish(pBackup); |
| 4730 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4731 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4732 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4733 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4734 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4735 | } |
| 4736 | sqlite3_close(pDest); |
| 4737 | }else |
| 4738 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4739 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 4740 | if( nArg==2 ){ |
| 4741 | bail_on_error = booleanValue(azArg[1]); |
| 4742 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4743 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4744 | rc = 1; |
| 4745 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 4746 | }else |
| 4747 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4748 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 4749 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4750 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4751 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4752 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4753 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4754 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4755 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4756 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4757 | rc = 1; |
| 4758 | } |
| 4759 | }else |
| 4760 | |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 4761 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 4762 | if( nArg==2 ){ |
| 4763 | #if defined(_WIN32) || defined(WIN32) |
| 4764 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 4765 | rc = !SetCurrentDirectoryW(z); |
| 4766 | sqlite3_free(z); |
| 4767 | #else |
| 4768 | rc = chdir(azArg[1]); |
| 4769 | #endif |
| 4770 | if( rc ){ |
| 4771 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 4772 | rc = 1; |
| 4773 | } |
| 4774 | }else{ |
| 4775 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 4776 | rc = 1; |
| 4777 | } |
| 4778 | }else |
| 4779 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4780 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 4781 | ** routine named test_breakpoint(). |
| 4782 | */ |
| 4783 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 4784 | test_breakpoint(); |
| 4785 | }else |
| 4786 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4787 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 4788 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4789 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4790 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4791 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4792 | rc = 1; |
| 4793 | } |
| 4794 | }else |
| 4795 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4796 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 4797 | ** Then read the content of the testcase-out.txt file and compare against |
| 4798 | ** azArg[1]. If there are differences, report an error and exit. |
| 4799 | */ |
| 4800 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 4801 | char *zRes = 0; |
| 4802 | output_reset(p); |
| 4803 | if( nArg!=2 ){ |
| 4804 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4805 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4806 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4807 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 4808 | rc = 2; |
| 4809 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4810 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4811 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 4812 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4813 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4814 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4815 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4816 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4817 | } |
| 4818 | sqlite3_free(zRes); |
| 4819 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4820 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4821 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 4822 | if( nArg==2 ){ |
| 4823 | tryToClone(p, azArg[1]); |
| 4824 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4825 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4826 | rc = 1; |
| 4827 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4828 | }else |
| 4829 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4830 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4831 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4832 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4833 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4834 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4835 | data.showHeader = 0; |
| 4836 | data.cMode = data.mode = MODE_List; |
| 4837 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 4838 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4839 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 4840 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4841 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4842 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4843 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4844 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4845 | } |
| 4846 | }else |
| 4847 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4848 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 4849 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 4850 | }else |
| 4851 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4852 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4853 | const char *zLike = 0; |
| 4854 | int i; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4855 | int savedShowHeader = p->showHeader; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4856 | ShellClearFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4857 | for(i=1; i<nArg; i++){ |
| 4858 | if( azArg[i][0]=='-' ){ |
| 4859 | const char *z = azArg[i]+1; |
| 4860 | if( z[0]=='-' ) z++; |
| 4861 | if( strcmp(z,"preserve-rowids")==0 ){ |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4862 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 4863 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 4864 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 4865 | rc = 1; |
| 4866 | goto meta_command_exit; |
| 4867 | #else |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4868 | ShellSetFlag(p, SHFLG_PreserveRowid); |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4869 | #endif |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4870 | }else |
| 4871 | { |
| 4872 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4873 | rc = 1; |
| 4874 | goto meta_command_exit; |
| 4875 | } |
| 4876 | }else if( zLike ){ |
| 4877 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 4878 | rc = 1; |
| 4879 | goto meta_command_exit; |
| 4880 | }else{ |
| 4881 | zLike = azArg[i]; |
| 4882 | } |
| 4883 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4884 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 4885 | /* When playing back a "dump", the content might appear in an order |
| 4886 | ** which causes immediate foreign key constraints to be violated. |
| 4887 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4888 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 4889 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4890 | p->writableSchema = 0; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4891 | p->showHeader = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4892 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 4893 | ** as much of the schema as it can even if the sqlite_master table is |
| 4894 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4895 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4896 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4897 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4898 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4899 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4900 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4901 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4902 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4903 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4904 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4905 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4906 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4907 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 4908 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4909 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4910 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4911 | char *zSql; |
| 4912 | zSql = sqlite3_mprintf( |
| 4913 | "SELECT name, type, sql FROM sqlite_master " |
| 4914 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 4915 | " AND sql NOT NULL", zLike); |
| 4916 | run_schema_dump_query(p,zSql); |
| 4917 | sqlite3_free(zSql); |
| 4918 | zSql = sqlite3_mprintf( |
| 4919 | "SELECT sql FROM sqlite_master " |
| 4920 | "WHERE sql NOT NULL" |
| 4921 | " AND type IN ('index','trigger','view')" |
| 4922 | " AND tbl_name LIKE %Q", zLike); |
| 4923 | run_table_dump_query(p, zSql, 0); |
| 4924 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4925 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4926 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4927 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4928 | p->writableSchema = 0; |
| 4929 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4930 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4931 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4932 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4933 | p->showHeader = savedShowHeader; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4934 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4935 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4936 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 4937 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4938 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4939 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4940 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4941 | rc = 1; |
| 4942 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4943 | }else |
| 4944 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4945 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 4946 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4947 | if( strcmp(azArg[1],"full")==0 ){ |
| 4948 | p->autoEQP = 2; |
| 4949 | }else{ |
| 4950 | p->autoEQP = booleanValue(azArg[1]); |
| 4951 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4952 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4953 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4954 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4955 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 4956 | }else |
| 4957 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 4958 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4959 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4960 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4961 | }else |
| 4962 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4963 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4964 | int val = 1; |
| 4965 | if( nArg>=2 ){ |
| 4966 | if( strcmp(azArg[1],"auto")==0 ){ |
| 4967 | val = 99; |
| 4968 | }else{ |
| 4969 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4970 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4971 | } |
| 4972 | if( val==1 && p->mode!=MODE_Explain ){ |
| 4973 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 4974 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4975 | p->autoExplain = 0; |
| 4976 | }else if( val==0 ){ |
| 4977 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4978 | p->autoExplain = 0; |
| 4979 | }else if( val==99 ){ |
| 4980 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4981 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4982 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4983 | }else |
| 4984 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4985 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4986 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4987 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4988 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4989 | memcpy(&data, p, sizeof(data)); |
| 4990 | data.showHeader = 0; |
| 4991 | data.cMode = data.mode = MODE_Semi; |
| 4992 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 4993 | data.cMode = data.mode = MODE_Pretty; |
| 4994 | nArg = 1; |
| 4995 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4996 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4997 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4998 | rc = 1; |
| 4999 | goto meta_command_exit; |
| 5000 | } |
| 5001 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5002 | rc = sqlite3_exec(p->db, |
| 5003 | "SELECT sql FROM" |
| 5004 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 5005 | " FROM sqlite_master UNION ALL" |
| 5006 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5007 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5008 | "ORDER BY rowid", |
| 5009 | callback, &data, &zErrMsg |
| 5010 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5011 | if( rc==SQLITE_OK ){ |
| 5012 | sqlite3_stmt *pStmt; |
| 5013 | rc = sqlite3_prepare_v2(p->db, |
| 5014 | "SELECT rowid FROM sqlite_master" |
| 5015 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 5016 | -1, &pStmt, 0); |
| 5017 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 5018 | sqlite3_finalize(pStmt); |
| 5019 | } |
| 5020 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5021 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5022 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5023 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5024 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 5025 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5026 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5027 | data.zDestTable = "sqlite_stat1"; |
| 5028 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 5029 | shell_callback, &data,&zErrMsg); |
| 5030 | data.zDestTable = "sqlite_stat3"; |
| 5031 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 5032 | shell_callback, &data,&zErrMsg); |
| 5033 | data.zDestTable = "sqlite_stat4"; |
| 5034 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 5035 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5036 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5037 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5038 | }else |
| 5039 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5040 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 5041 | if( nArg==2 ){ |
| 5042 | p->showHeader = booleanValue(azArg[1]); |
| 5043 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5044 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5045 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 5046 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5047 | }else |
| 5048 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5049 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5050 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5051 | }else |
| 5052 | |
| 5053 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5054 | char *zTable; /* Insert data into this table */ |
| 5055 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5056 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5057 | int nCol; /* Number of columns in the table */ |
| 5058 | int nByte; /* Number of bytes in an SQL string */ |
| 5059 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 5060 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5061 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5062 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5063 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 5064 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 5065 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5066 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5067 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5068 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5069 | goto meta_command_exit; |
| 5070 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5071 | zFile = azArg[1]; |
| 5072 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5073 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5074 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5075 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5076 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5077 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5078 | raw_printf(stderr, |
| 5079 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5080 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5081 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5082 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5083 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5084 | " for import\n"); |
| 5085 | return 1; |
| 5086 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5087 | nSep = strlen30(p->rowSeparator); |
| 5088 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5089 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5090 | return 1; |
| 5091 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5092 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 5093 | /* When importing CSV (only), if the row separator is set to the |
| 5094 | ** default output row separator, change it to the default input |
| 5095 | ** row separator. This avoids having to maintain different input |
| 5096 | ** and output row separators. */ |
| 5097 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 5098 | nSep = strlen30(p->rowSeparator); |
| 5099 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5100 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5101 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5102 | " for import\n"); |
| 5103 | return 1; |
| 5104 | } |
| 5105 | sCtx.zFile = zFile; |
| 5106 | sCtx.nLine = 1; |
| 5107 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5108 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5109 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5110 | return 1; |
| 5111 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5112 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 5113 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5114 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5115 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5116 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5117 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5118 | xCloser = fclose; |
| 5119 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5120 | if( p->mode==MODE_Ascii ){ |
| 5121 | xRead = ascii_read_one_field; |
| 5122 | }else{ |
| 5123 | xRead = csv_read_one_field; |
| 5124 | } |
| 5125 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5126 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5127 | return 1; |
| 5128 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5129 | sCtx.cColSep = p->colSeparator[0]; |
| 5130 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 5131 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5132 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5133 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5134 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5135 | return 1; |
| 5136 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5137 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5138 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5139 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5140 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5141 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 5142 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5143 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 5144 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5145 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5146 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5147 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5148 | if( cSep=='(' ){ |
| 5149 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5150 | sqlite3_free(sCtx.z); |
| 5151 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5152 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5153 | return 1; |
| 5154 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5155 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 5156 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 5157 | sqlite3_free(zCreate); |
| 5158 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5159 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5160 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5161 | sqlite3_free(sCtx.z); |
| 5162 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5163 | return 1; |
| 5164 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5165 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5166 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5167 | sqlite3_free(zSql); |
| 5168 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5169 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5170 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5171 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5172 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5173 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5174 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5175 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5176 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5177 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5178 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5179 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5180 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5181 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5182 | return 1; |
| 5183 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5184 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5185 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5186 | for(i=1; i<nCol; i++){ |
| 5187 | zSql[j++] = ','; |
| 5188 | zSql[j++] = '?'; |
| 5189 | } |
| 5190 | zSql[j++] = ')'; |
| 5191 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5192 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5193 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5194 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5195 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5196 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5197 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5198 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5199 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5200 | needCommit = sqlite3_get_autocommit(p->db); |
| 5201 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5202 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5203 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5204 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5205 | char *z = xRead(&sCtx); |
| 5206 | /* |
| 5207 | ** Did we reach end-of-file before finding any columns? |
| 5208 | ** If so, stop instead of NULL filling the remaining columns. |
| 5209 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5210 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5211 | /* |
| 5212 | ** Did we reach end-of-file OR end-of-line before finding any |
| 5213 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 5214 | ** the remaining columns. |
| 5215 | */ |
| 5216 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5217 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5218 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5219 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5220 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5221 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 5222 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 5223 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 5224 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5225 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5226 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5227 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5228 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5229 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5230 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5231 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5232 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5233 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5234 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5235 | if( i>=nCol ){ |
| 5236 | sqlite3_step(pStmt); |
| 5237 | rc = sqlite3_reset(pStmt); |
| 5238 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5239 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 5240 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5241 | } |
| 5242 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5243 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5244 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5245 | xCloser(sCtx.in); |
| 5246 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5247 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5248 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5249 | }else |
| 5250 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 5251 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5252 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 5253 | char *zSql; |
| 5254 | char *zCollist = 0; |
| 5255 | sqlite3_stmt *pStmt; |
| 5256 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5257 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5258 | if( nArg!=3 ){ |
| 5259 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 5260 | rc = 1; |
| 5261 | goto meta_command_exit; |
| 5262 | } |
| 5263 | open_db(p, 0); |
| 5264 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 5265 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 5266 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5267 | sqlite3_free(zSql); |
| 5268 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5269 | tnum = sqlite3_column_int(pStmt, 0); |
| 5270 | } |
| 5271 | sqlite3_finalize(pStmt); |
| 5272 | if( tnum==0 ){ |
| 5273 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 5274 | rc = 1; |
| 5275 | goto meta_command_exit; |
| 5276 | } |
| 5277 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 5278 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5279 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5280 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5281 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5282 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5283 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5284 | i++; |
| 5285 | if( zCol==0 ){ |
| 5286 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 5287 | zCol = "_ROWID_"; |
| 5288 | }else{ |
| 5289 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 5290 | zCol = zLabel; |
| 5291 | } |
| 5292 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5293 | if( zCollist==0 ){ |
| 5294 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 5295 | }else{ |
| 5296 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 5297 | } |
| 5298 | } |
| 5299 | sqlite3_finalize(pStmt); |
| 5300 | zSql = sqlite3_mprintf( |
| 5301 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 5302 | azArg[2], zCollist, zCollist); |
| 5303 | sqlite3_free(zCollist); |
| 5304 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 5305 | if( rc==SQLITE_OK ){ |
| 5306 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5307 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 5308 | if( rc ){ |
| 5309 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 5310 | }else{ |
| 5311 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5312 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5313 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 5314 | ); |
| 5315 | } |
| 5316 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5317 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5318 | rc = 1; |
| 5319 | } |
| 5320 | sqlite3_free(zSql); |
| 5321 | }else |
| 5322 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 5323 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5324 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5325 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 5326 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5327 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 5328 | iotrace = 0; |
| 5329 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5330 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5331 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5332 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5333 | iotrace = stdout; |
| 5334 | }else{ |
| 5335 | iotrace = fopen(azArg[1], "w"); |
| 5336 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5337 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5338 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5339 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5340 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5341 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5342 | } |
| 5343 | } |
| 5344 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5345 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5346 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5347 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 5348 | static const struct { |
| 5349 | const char *zLimitName; /* Name of a limit */ |
| 5350 | int limitCode; /* Integer code for that limit */ |
| 5351 | } aLimit[] = { |
| 5352 | { "length", SQLITE_LIMIT_LENGTH }, |
| 5353 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 5354 | { "column", SQLITE_LIMIT_COLUMN }, |
| 5355 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5356 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5357 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 5358 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5359 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 5360 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5361 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 5362 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 5363 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 5364 | }; |
| 5365 | int i, n2; |
| 5366 | open_db(p, 0); |
| 5367 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5368 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5369 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5370 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 5371 | } |
| 5372 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5373 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5374 | rc = 1; |
| 5375 | goto meta_command_exit; |
| 5376 | }else{ |
| 5377 | int iLimit = -1; |
| 5378 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5379 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5380 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 5381 | if( iLimit<0 ){ |
| 5382 | iLimit = i; |
| 5383 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5384 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5385 | rc = 1; |
| 5386 | goto meta_command_exit; |
| 5387 | } |
| 5388 | } |
| 5389 | } |
| 5390 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5391 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5392 | "enter \".limits\" with no arguments for a list.\n", |
| 5393 | azArg[1]); |
| 5394 | rc = 1; |
| 5395 | goto meta_command_exit; |
| 5396 | } |
| 5397 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 5398 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 5399 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5400 | } |
| 5401 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 5402 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 5403 | } |
| 5404 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5405 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5406 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 5407 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5408 | lintDotCommand(p, azArg, nArg); |
| 5409 | }else |
| 5410 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5411 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5412 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5413 | const char *zFile, *zProc; |
| 5414 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5415 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5416 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5417 | rc = 1; |
| 5418 | goto meta_command_exit; |
| 5419 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5420 | zFile = azArg[1]; |
| 5421 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5422 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5423 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 5424 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5425 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5426 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5427 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5428 | } |
| 5429 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5430 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5431 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5432 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 5433 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5434 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5435 | rc = 1; |
| 5436 | }else{ |
| 5437 | const char *zFile = azArg[1]; |
| 5438 | output_file_close(p->pLog); |
| 5439 | p->pLog = output_file_open(zFile); |
| 5440 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5441 | }else |
| 5442 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5443 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 5444 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 5445 | int n2 = (int)strlen(zMode); |
| 5446 | int c2 = zMode[0]; |
| 5447 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5448 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5449 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5450 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5451 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5452 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5453 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5454 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5455 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 5456 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5457 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5458 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5459 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5460 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5461 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5462 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5463 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 5464 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5465 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5466 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5467 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5468 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5469 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5470 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 5471 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5472 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 5473 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 5474 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5475 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5476 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5477 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5478 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5479 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5480 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 5481 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5482 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5483 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5484 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5485 | }else |
| 5486 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5487 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 5488 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5489 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 5490 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5491 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5492 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 5493 | rc = 1; |
| 5494 | } |
| 5495 | }else |
| 5496 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5497 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5498 | char *zNewFilename; /* Name of the database file to open */ |
| 5499 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5500 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5501 | /* Close the existing database */ |
| 5502 | session_close_all(p); |
| 5503 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5504 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 5505 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5506 | sqlite3_free(p->zFreeOnClose); |
| 5507 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5508 | /* Check for command-line arguments */ |
| 5509 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 5510 | const char *z = azArg[iName]; |
| 5511 | if( optionMatch(z,"new") ){ |
| 5512 | newFlag = 1; |
| 5513 | }else if( z[0]=='-' ){ |
| 5514 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 5515 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5516 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5517 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5518 | } |
| 5519 | /* If a filename is specified, try to open it first */ |
| 5520 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 5521 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5522 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5523 | p->zDbFilename = zNewFilename; |
| 5524 | open_db(p, 1); |
| 5525 | if( p->db==0 ){ |
| 5526 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 5527 | sqlite3_free(zNewFilename); |
| 5528 | }else{ |
| 5529 | p->zFreeOnClose = zNewFilename; |
| 5530 | } |
| 5531 | } |
| 5532 | if( p->db==0 ){ |
| 5533 | /* As a fall-back open a TEMP database */ |
| 5534 | p->zDbFilename = 0; |
| 5535 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5536 | } |
| 5537 | }else |
| 5538 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5539 | if( c=='o' |
| 5540 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 5541 | ){ |
| 5542 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 5543 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5544 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5545 | rc = 1; |
| 5546 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5547 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5548 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 5549 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5550 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5551 | rc = 1; |
| 5552 | goto meta_command_exit; |
| 5553 | } |
| 5554 | p->outCount = 2; |
| 5555 | }else{ |
| 5556 | p->outCount = 0; |
| 5557 | } |
| 5558 | output_reset(p); |
| 5559 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5560 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5561 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5562 | rc = 1; |
| 5563 | p->out = stdout; |
| 5564 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5565 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5566 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5567 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5568 | p->out = stdout; |
| 5569 | rc = 1; |
| 5570 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5571 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5572 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5573 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5574 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5575 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5576 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5577 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5578 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 5579 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5580 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5581 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5582 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5583 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5584 | } |
| 5585 | } |
| 5586 | }else |
| 5587 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5588 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 5589 | int i; |
| 5590 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5591 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5592 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5593 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5594 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5595 | }else |
| 5596 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5597 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5598 | if( nArg >= 2) { |
| 5599 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 5600 | } |
| 5601 | if( nArg >= 3) { |
| 5602 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 5603 | } |
| 5604 | }else |
| 5605 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5606 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5607 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5608 | }else |
| 5609 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5610 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 5611 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5612 | if( nArg!=2 ){ |
| 5613 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5614 | rc = 1; |
| 5615 | goto meta_command_exit; |
| 5616 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5617 | alt = fopen(azArg[1], "rb"); |
| 5618 | if( alt==0 ){ |
| 5619 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 5620 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5621 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5622 | rc = process_input(p, alt); |
| 5623 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5624 | } |
| 5625 | }else |
| 5626 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5627 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5628 | const char *zSrcFile; |
| 5629 | const char *zDb; |
| 5630 | sqlite3 *pSrc; |
| 5631 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5632 | int nTimeout = 0; |
| 5633 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5634 | if( nArg==2 ){ |
| 5635 | zSrcFile = azArg[1]; |
| 5636 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5637 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5638 | zSrcFile = azArg[2]; |
| 5639 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5640 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5641 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5642 | rc = 1; |
| 5643 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5644 | } |
| 5645 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 5646 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5647 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5648 | sqlite3_close(pSrc); |
| 5649 | return 1; |
| 5650 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5651 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5652 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 5653 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5654 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5655 | sqlite3_close(pSrc); |
| 5656 | return 1; |
| 5657 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5658 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 5659 | || rc==SQLITE_BUSY ){ |
| 5660 | if( rc==SQLITE_BUSY ){ |
| 5661 | if( nTimeout++ >= 3 ) break; |
| 5662 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | sqlite3_backup_finish(pBackup); |
| 5666 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5667 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5668 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5669 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5670 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5671 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5672 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5673 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5674 | } |
| 5675 | sqlite3_close(pSrc); |
| 5676 | }else |
| 5677 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5678 | |
| 5679 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 5680 | if( nArg==2 ){ |
| 5681 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5682 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5683 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5684 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5685 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5686 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5687 | rc = 1; |
| 5688 | } |
| 5689 | }else |
| 5690 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5691 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5692 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5693 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5694 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5695 | memcpy(&data, p, sizeof(data)); |
| 5696 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5697 | data.cMode = data.mode = MODE_Semi; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5698 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 5699 | data.cMode = data.mode = MODE_Pretty; |
| 5700 | nArg--; |
| 5701 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 5702 | } |
| 5703 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5704 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5705 | 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] | 5706 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5707 | char *new_argv[2], *new_colv[2]; |
| 5708 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 5709 | " type text,\n" |
| 5710 | " name text,\n" |
| 5711 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 5712 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5713 | " sql text\n" |
| 5714 | ")"; |
| 5715 | new_argv[1] = 0; |
| 5716 | new_colv[0] = "sql"; |
| 5717 | new_colv[1] = 0; |
| 5718 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5719 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5720 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5721 | char *new_argv[2], *new_colv[2]; |
| 5722 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 5723 | " type text,\n" |
| 5724 | " name text,\n" |
| 5725 | " tbl_name text,\n" |
| 5726 | " rootpage integer,\n" |
| 5727 | " sql text\n" |
| 5728 | ")"; |
| 5729 | new_argv[1] = 0; |
| 5730 | new_colv[0] = "sql"; |
| 5731 | new_colv[1] = 0; |
| 5732 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5733 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5734 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5735 | char *zSql; |
| 5736 | zSql = sqlite3_mprintf( |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5737 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5738 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5739 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5740 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5741 | "WHERE lower(tbl_name) LIKE %Q" |
drh | 6ac7a58 | 2011-11-04 00:35:56 +0000 | [diff] [blame] | 5742 | " AND type!='meta' AND sql NOTNULL " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5743 | "ORDER BY rowid", azArg[1]); |
| 5744 | rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg); |
| 5745 | sqlite3_free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5746 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5747 | }else if( nArg==1 ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5748 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5749 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5750 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5751 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5752 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5753 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 5754 | "ORDER BY rowid", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5755 | callback, &data, &zErrMsg |
| 5756 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5757 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5758 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5759 | rc = 1; |
| 5760 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5761 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5762 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5763 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5764 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5765 | rc = 1; |
| 5766 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5767 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5768 | rc = 1; |
| 5769 | }else{ |
| 5770 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5771 | } |
| 5772 | }else |
| 5773 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5774 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 5775 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 5776 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5777 | }else |
| 5778 | #endif |
| 5779 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5780 | #if defined(SQLITE_ENABLE_SESSION) |
| 5781 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 5782 | OpenSession *pSession = &p->aSession[0]; |
| 5783 | char **azCmd = &azArg[1]; |
| 5784 | int iSes = 0; |
| 5785 | int nCmd = nArg - 1; |
| 5786 | int i; |
| 5787 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5788 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5789 | if( nArg>=3 ){ |
| 5790 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 5791 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 5792 | } |
| 5793 | if( iSes<p->nSession ){ |
| 5794 | pSession = &p->aSession[iSes]; |
| 5795 | azCmd++; |
| 5796 | nCmd--; |
| 5797 | }else{ |
| 5798 | pSession = &p->aSession[0]; |
| 5799 | iSes = 0; |
| 5800 | } |
| 5801 | } |
| 5802 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5803 | /* .session attach TABLE |
| 5804 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 5805 | ** table so that it is never filtered. |
| 5806 | */ |
| 5807 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 5808 | if( nCmd!=2 ) goto session_syntax_error; |
| 5809 | if( pSession->p==0 ){ |
| 5810 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5811 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5812 | }else{ |
| 5813 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 5814 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5815 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5816 | rc = 0; |
| 5817 | } |
| 5818 | } |
| 5819 | }else |
| 5820 | |
| 5821 | /* .session changeset FILE |
| 5822 | ** .session patchset FILE |
| 5823 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 5824 | */ |
| 5825 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 5826 | FILE *out = 0; |
| 5827 | if( nCmd!=2 ) goto session_syntax_error; |
| 5828 | if( pSession->p==0 ) goto session_not_open; |
| 5829 | out = fopen(azCmd[1], "wb"); |
| 5830 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5831 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5832 | }else{ |
| 5833 | int szChng; |
| 5834 | void *pChng; |
| 5835 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5836 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5837 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5838 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 5839 | } |
| 5840 | if( rc ){ |
| 5841 | printf("Error: error code %d\n", rc); |
| 5842 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5843 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5844 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5845 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5846 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5847 | szChng); |
| 5848 | } |
| 5849 | sqlite3_free(pChng); |
| 5850 | fclose(out); |
| 5851 | } |
| 5852 | }else |
| 5853 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5854 | /* .session close |
| 5855 | ** Close the identified session |
| 5856 | */ |
| 5857 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5858 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5859 | if( p->nSession ){ |
| 5860 | session_close(pSession); |
| 5861 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 5862 | } |
| 5863 | }else |
| 5864 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5865 | /* .session enable ?BOOLEAN? |
| 5866 | ** Query or set the enable flag |
| 5867 | */ |
| 5868 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 5869 | int ii; |
| 5870 | if( nCmd>2 ) goto session_syntax_error; |
| 5871 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5872 | if( p->nSession ){ |
| 5873 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5874 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 5875 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5876 | } |
| 5877 | }else |
| 5878 | |
| 5879 | /* .session filter GLOB .... |
| 5880 | ** Set a list of GLOB patterns of table names to be excluded. |
| 5881 | */ |
| 5882 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 5883 | int ii, nByte; |
| 5884 | if( nCmd<2 ) goto session_syntax_error; |
| 5885 | if( p->nSession ){ |
| 5886 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 5887 | sqlite3_free(pSession->azFilter[ii]); |
| 5888 | } |
| 5889 | sqlite3_free(pSession->azFilter); |
| 5890 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 5891 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 5892 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5893 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5894 | exit(1); |
| 5895 | } |
| 5896 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5897 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5898 | } |
| 5899 | pSession->nFilter = ii-1; |
| 5900 | } |
| 5901 | }else |
| 5902 | |
| 5903 | /* .session indirect ?BOOLEAN? |
| 5904 | ** Query or set the indirect flag |
| 5905 | */ |
| 5906 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 5907 | int ii; |
| 5908 | if( nCmd>2 ) goto session_syntax_error; |
| 5909 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5910 | if( p->nSession ){ |
| 5911 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5912 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 5913 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5914 | } |
| 5915 | }else |
| 5916 | |
| 5917 | /* .session isempty |
| 5918 | ** Determine if the session is empty |
| 5919 | */ |
| 5920 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 5921 | int ii; |
| 5922 | if( nCmd!=1 ) goto session_syntax_error; |
| 5923 | if( p->nSession ){ |
| 5924 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5925 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 5926 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5927 | } |
| 5928 | }else |
| 5929 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5930 | /* .session list |
| 5931 | ** List all currently open sessions |
| 5932 | */ |
| 5933 | if( strcmp(azCmd[0],"list")==0 ){ |
| 5934 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5935 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5936 | } |
| 5937 | }else |
| 5938 | |
| 5939 | /* .session open DB NAME |
| 5940 | ** Open a new session called NAME on the attached database DB. |
| 5941 | ** DB is normally "main". |
| 5942 | */ |
| 5943 | if( strcmp(azCmd[0],"open")==0 ){ |
| 5944 | char *zName; |
| 5945 | if( nCmd!=3 ) goto session_syntax_error; |
| 5946 | zName = azCmd[2]; |
| 5947 | if( zName[0]==0 ) goto session_syntax_error; |
| 5948 | for(i=0; i<p->nSession; i++){ |
| 5949 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5950 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5951 | goto meta_command_exit; |
| 5952 | } |
| 5953 | } |
| 5954 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5955 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5956 | goto meta_command_exit; |
| 5957 | } |
| 5958 | pSession = &p->aSession[p->nSession]; |
| 5959 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 5960 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5961 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5962 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5963 | goto meta_command_exit; |
| 5964 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5965 | pSession->nFilter = 0; |
| 5966 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5967 | p->nSession++; |
| 5968 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 5969 | }else |
| 5970 | /* If no command name matches, show a syntax error */ |
| 5971 | session_syntax_error: |
| 5972 | session_help(p); |
| 5973 | }else |
| 5974 | #endif |
| 5975 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5976 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5977 | /* Undocumented commands for internal testing. Subject to change |
| 5978 | ** without notice. */ |
| 5979 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 5980 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 5981 | int i, v; |
| 5982 | for(i=1; i<nArg; i++){ |
| 5983 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5984 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5985 | } |
| 5986 | } |
| 5987 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 5988 | int i; sqlite3_int64 v; |
| 5989 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5990 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5991 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5992 | 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] | 5993 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5994 | } |
| 5995 | } |
| 5996 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5997 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5998 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 5999 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 6000 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 6001 | int bVerbose = 0; /* Verbose output */ |
| 6002 | int bSelftestExists; /* True if SELFTEST already exists */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6003 | int i, k; /* Loop counters */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6004 | int nTest = 0; /* Number of tests runs */ |
| 6005 | int nErr = 0; /* Number of errors seen */ |
| 6006 | ShellText str; /* Answer for a query */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6007 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6008 | |
| 6009 | open_db(p,0); |
| 6010 | for(i=1; i<nArg; i++){ |
| 6011 | const char *z = azArg[i]; |
| 6012 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 6013 | if( strcmp(z,"-init")==0 ){ |
| 6014 | bIsInit = 1; |
| 6015 | }else |
| 6016 | if( strcmp(z,"-v")==0 ){ |
| 6017 | bVerbose++; |
| 6018 | }else |
| 6019 | { |
| 6020 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 6021 | azArg[i], azArg[0]); |
| 6022 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 6023 | rc = 1; |
| 6024 | goto meta_command_exit; |
| 6025 | } |
| 6026 | } |
| 6027 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 6028 | != SQLITE_OK ){ |
| 6029 | bSelftestExists = 0; |
| 6030 | }else{ |
| 6031 | bSelftestExists = 1; |
| 6032 | } |
| 6033 | if( bIsInit ){ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6034 | createSelftestTable(p); |
| 6035 | bSelftestExists = 1; |
| 6036 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6037 | initText(&str); |
| 6038 | appendText(&str, "x", 0); |
| 6039 | for(k=bSelftestExists; k>=0; k--){ |
| 6040 | if( k==1 ){ |
| 6041 | rc = sqlite3_prepare_v2(p->db, |
| 6042 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 6043 | -1, &pStmt, 0); |
| 6044 | }else{ |
| 6045 | rc = sqlite3_prepare_v2(p->db, |
| 6046 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 6047 | " (1,'run','PRAGMA integrity_check','ok')", |
| 6048 | -1, &pStmt, 0); |
| 6049 | } |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6050 | if( rc ){ |
| 6051 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 6052 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6053 | sqlite3_finalize(pStmt); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6054 | goto meta_command_exit; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6055 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6056 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 6057 | int tno = sqlite3_column_int(pStmt, 0); |
| 6058 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 6059 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 6060 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6061 | |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6062 | k = 0; |
| 6063 | if( bVerbose>0 ){ |
| 6064 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 6065 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 6066 | sqlite3_free(zQuote); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6067 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6068 | if( strcmp(zOp,"memo")==0 ){ |
| 6069 | utf8_printf(p->out, "%s\n", zSql); |
| 6070 | }else |
| 6071 | if( strcmp(zOp,"run")==0 ){ |
| 6072 | char *zErrMsg = 0; |
| 6073 | str.n = 0; |
| 6074 | str.z[0] = 0; |
| 6075 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 6076 | nTest++; |
| 6077 | if( bVerbose ){ |
| 6078 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 6079 | } |
| 6080 | if( rc || zErrMsg ){ |
| 6081 | nErr++; |
| 6082 | rc = 1; |
| 6083 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 6084 | sqlite3_free(zErrMsg); |
| 6085 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 6086 | nErr++; |
| 6087 | rc = 1; |
| 6088 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 6089 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 6090 | } |
| 6091 | }else |
| 6092 | { |
| 6093 | utf8_printf(stderr, |
| 6094 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6095 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6096 | break; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6097 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6098 | } /* End loop over rows of content from SELFTEST */ |
| 6099 | sqlite3_finalize(pStmt); |
| 6100 | } /* End loop over k */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6101 | freeText(&str); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6102 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 6103 | }else |
| 6104 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6105 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6106 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6107 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6108 | rc = 1; |
| 6109 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6110 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6111 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 6112 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6113 | } |
| 6114 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6115 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 6116 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6117 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6118 | }else |
| 6119 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6120 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 6121 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 6122 | int i; /* Loop counter */ |
| 6123 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6124 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6125 | int iSize = 224; /* Hash algorithm to use */ |
| 6126 | int bDebug = 0; /* Only show the query that would have run */ |
| 6127 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 6128 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6129 | char *zSep; /* Separator */ |
| 6130 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6131 | ShellText sQuery; /* Set of queries used to read all content */ |
drh | f80d4ff | 2017-03-08 18:06:20 +0000 | [diff] [blame] | 6132 | open_db(p, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6133 | for(i=1; i<nArg; i++){ |
| 6134 | const char *z = azArg[i]; |
| 6135 | if( z[0]=='-' ){ |
| 6136 | z++; |
| 6137 | if( z[0]=='-' ) z++; |
| 6138 | if( strcmp(z,"schema")==0 ){ |
| 6139 | bSchema = 1; |
| 6140 | }else |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6141 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 6142 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6143 | ){ |
| 6144 | iSize = atoi(&z[5]); |
| 6145 | }else |
| 6146 | if( strcmp(z,"debug")==0 ){ |
| 6147 | bDebug = 1; |
| 6148 | }else |
| 6149 | { |
| 6150 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6151 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6152 | raw_printf(stderr, "Should be one of: --schema" |
| 6153 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 6154 | rc = 1; |
| 6155 | goto meta_command_exit; |
| 6156 | } |
| 6157 | }else if( zLike ){ |
| 6158 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 6159 | rc = 1; |
| 6160 | goto meta_command_exit; |
| 6161 | }else{ |
| 6162 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6163 | bSeparate = 1; |
| 6164 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6165 | } |
| 6166 | } |
| 6167 | if( bSchema ){ |
| 6168 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6169 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6170 | " UNION ALL SELECT 'sqlite_master'" |
| 6171 | " ORDER BY 1 collate nocase"; |
| 6172 | }else{ |
| 6173 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6174 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6175 | " AND name NOT LIKE 'sqlite_%'" |
| 6176 | " ORDER BY 1 collate nocase"; |
| 6177 | } |
| 6178 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6179 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6180 | initText(&sSql); |
| 6181 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 6182 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6183 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 6184 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 6185 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 6186 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 6187 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 6188 | appendText(&sQuery,zTab,'"'); |
| 6189 | appendText(&sQuery," NOT INDEXED;", 0); |
| 6190 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 6191 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 6192 | " ORDER BY name;", 0); |
| 6193 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 6194 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 6195 | " ORDER BY name;", 0); |
| 6196 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 6197 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 6198 | " ORDER BY tbl,idx;", 0); |
| 6199 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 6200 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 6201 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 6202 | appendText(&sQuery, zTab, 0); |
| 6203 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 6204 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6205 | appendText(&sSql, zSep, 0); |
| 6206 | appendText(&sSql, sQuery.z, '\''); |
| 6207 | sQuery.n = 0; |
| 6208 | appendText(&sSql, ",", 0); |
| 6209 | appendText(&sSql, zTab, '\''); |
| 6210 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6211 | } |
| 6212 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6213 | if( bSeparate ){ |
| 6214 | zSql = sqlite3_mprintf( |
| 6215 | "%s))" |
| 6216 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 6217 | " FROM [sha3sum$query]", |
| 6218 | sSql.z, iSize); |
| 6219 | }else{ |
| 6220 | zSql = sqlite3_mprintf( |
| 6221 | "%s))" |
| 6222 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 6223 | " FROM [sha3sum$query]", |
| 6224 | sSql.z, iSize); |
| 6225 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6226 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6227 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6228 | if( bDebug ){ |
| 6229 | utf8_printf(p->out, "%s\n", zSql); |
| 6230 | }else{ |
| 6231 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 6232 | } |
| 6233 | sqlite3_free(zSql); |
| 6234 | }else |
| 6235 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6236 | if( c=='s' |
| 6237 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6238 | ){ |
| 6239 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6240 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6241 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6242 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6243 | rc = 1; |
| 6244 | goto meta_command_exit; |
| 6245 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6246 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6247 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6248 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 6249 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6250 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6251 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6252 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6253 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6254 | }else |
| 6255 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6256 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6257 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6258 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6259 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6260 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6261 | rc = 1; |
| 6262 | goto meta_command_exit; |
| 6263 | } |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6264 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 6265 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6266 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6267 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 6268 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6269 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6270 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 6271 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6272 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6273 | raw_printf(p->out, "\n"); |
| 6274 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6275 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6276 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6277 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6278 | raw_printf(p->out, "\n"); |
| 6279 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6280 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6281 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6282 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6283 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6284 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6285 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6286 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6287 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6288 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 6289 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6290 | }else |
| 6291 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6292 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 6293 | if( nArg==2 ){ |
| 6294 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6295 | }else if( nArg==1 ){ |
| 6296 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6297 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6298 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6299 | rc = 1; |
| 6300 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6301 | }else |
| 6302 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6303 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 6304 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 6305 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 6306 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6307 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6308 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6309 | int nRow, nAlloc; |
| 6310 | char *zSql = 0; |
| 6311 | int ii; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6312 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6313 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6314 | if( rc ) return shellDatabaseError(p->db); |
| 6315 | |
| 6316 | /* Create an SQL statement to query for the list of tables in the |
| 6317 | ** main and all attached databases where the table name matches the |
| 6318 | ** LIKE pattern bound to variable "?1". */ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6319 | if( c=='t' ){ |
| 6320 | zSql = sqlite3_mprintf( |
| 6321 | "SELECT name FROM sqlite_master" |
| 6322 | " WHERE type IN ('table','view')" |
| 6323 | " AND name NOT LIKE 'sqlite_%%'" |
| 6324 | " AND name LIKE ?1"); |
| 6325 | }else if( nArg>2 ){ |
| 6326 | /* It is an historical accident that the .indexes command shows an error |
| 6327 | ** when called with the wrong number of arguments whereas the .tables |
| 6328 | ** command does not. */ |
| 6329 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 6330 | rc = 1; |
| 6331 | goto meta_command_exit; |
| 6332 | }else{ |
| 6333 | zSql = sqlite3_mprintf( |
| 6334 | "SELECT name FROM sqlite_master" |
| 6335 | " WHERE type='index'" |
| 6336 | " AND tbl_name LIKE ?1"); |
| 6337 | } |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6338 | for(ii=0; zSql && sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6339 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6340 | if( zDbName==0 || ii==0 ) continue; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6341 | if( c=='t' ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6342 | zSql = sqlite3_mprintf( |
| 6343 | "%z UNION ALL " |
| 6344 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6345 | " WHERE type IN ('table','view')" |
| 6346 | " AND name NOT LIKE 'sqlite_%%'" |
| 6347 | " AND name LIKE ?1", zSql, zDbName, zDbName); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6348 | }else{ |
| 6349 | zSql = sqlite3_mprintf( |
| 6350 | "%z UNION ALL " |
| 6351 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6352 | " WHERE type='index'" |
| 6353 | " AND tbl_name LIKE ?1", zSql, zDbName, zDbName); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6354 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 6355 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6356 | rc = sqlite3_finalize(pStmt); |
| 6357 | if( zSql && rc==SQLITE_OK ){ |
| 6358 | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 6359 | if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6360 | } |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6361 | sqlite3_free(zSql); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6362 | if( !zSql ) return shellNomemError(); |
| 6363 | if( rc ) return shellDatabaseError(p->db); |
| 6364 | |
| 6365 | /* Run the SQL statement prepared by the above block. Store the results |
| 6366 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6367 | nRow = nAlloc = 0; |
| 6368 | azResult = 0; |
| 6369 | if( nArg>1 ){ |
| 6370 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6371 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6372 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 6373 | } |
| 6374 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6375 | if( nRow>=nAlloc ){ |
| 6376 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6377 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6378 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6379 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6380 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6381 | break; |
| 6382 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6383 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6384 | azResult = azNew; |
| 6385 | } |
| 6386 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6387 | if( 0==azResult[nRow] ){ |
| 6388 | rc = shellNomemError(); |
| 6389 | break; |
| 6390 | } |
| 6391 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6392 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6393 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 6394 | rc = shellDatabaseError(p->db); |
| 6395 | } |
| 6396 | |
| 6397 | /* Pretty-print the contents of array azResult[] to the output */ |
| 6398 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6399 | int len, maxlen = 0; |
| 6400 | int i, j; |
| 6401 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6402 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6403 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6404 | if( len>maxlen ) maxlen = len; |
| 6405 | } |
| 6406 | nPrintCol = 80/(maxlen+2); |
| 6407 | if( nPrintCol<1 ) nPrintCol = 1; |
| 6408 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 6409 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6410 | for(j=i; j<nRow; j+=nPrintRow){ |
| 6411 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6412 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 6413 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6414 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6415 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6416 | } |
| 6417 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6418 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6419 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 6420 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6421 | }else |
| 6422 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6423 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 6424 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 6425 | output_reset(p); |
| 6426 | p->out = output_file_open("testcase-out.txt"); |
| 6427 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6428 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6429 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6430 | if( nArg>=2 ){ |
| 6431 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 6432 | }else{ |
| 6433 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 6434 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6435 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6436 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6437 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6438 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6439 | static const struct { |
| 6440 | const char *zCtrlName; /* Name of a test-control option */ |
| 6441 | int ctrlCode; /* Integer code for that option */ |
| 6442 | } aCtrl[] = { |
| 6443 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 6444 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 6445 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 6446 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 6447 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 6448 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 6449 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 6450 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 6451 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 6452 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 6453 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 6454 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6455 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6456 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 6457 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6458 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6459 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6460 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6461 | int rc2 = 0; |
| 6462 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6463 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6464 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6465 | /* convert testctrl text option to value. allow any unique prefix |
| 6466 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6467 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6468 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6469 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6470 | if( testctrl<0 ){ |
| 6471 | testctrl = aCtrl[i].ctrlCode; |
| 6472 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6473 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6474 | testctrl = -1; |
| 6475 | break; |
| 6476 | } |
| 6477 | } |
| 6478 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6479 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6480 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6481 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6482 | }else{ |
| 6483 | switch(testctrl){ |
| 6484 | |
| 6485 | /* sqlite3_test_control(int, db, int) */ |
| 6486 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6487 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6488 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6489 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6490 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6491 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6492 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6493 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6494 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6495 | } |
| 6496 | break; |
| 6497 | |
| 6498 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6499 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 6500 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6501 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6502 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6503 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6504 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6505 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6506 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6507 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 6508 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6509 | } |
| 6510 | break; |
| 6511 | |
| 6512 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6513 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6514 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 6515 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6516 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6517 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6518 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6519 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6520 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6521 | } |
| 6522 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6523 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6524 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6525 | case SQLITE_TESTCTRL_ASSERT: |
| 6526 | case SQLITE_TESTCTRL_ALWAYS: |
| 6527 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6528 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6529 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6530 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6531 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6532 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6533 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6534 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6535 | } |
| 6536 | break; |
| 6537 | |
| 6538 | /* sqlite3_test_control(int, char *) */ |
| 6539 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6540 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6541 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6542 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6543 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6544 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6545 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6546 | utf8_printf(stderr, |
| 6547 | "Error: testctrl %s takes a single char * option\n", |
| 6548 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6549 | } |
| 6550 | break; |
| 6551 | #endif |
| 6552 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6553 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6554 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6555 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6556 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6557 | integerValue(azArg[3]), |
| 6558 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6559 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6560 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6561 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6562 | } |
| 6563 | break; |
| 6564 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6565 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 6566 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 6567 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 6568 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6569 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6570 | utf8_printf(stderr, |
| 6571 | "Error: CLI support for testctrl %s not implemented\n", |
| 6572 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6573 | break; |
| 6574 | } |
| 6575 | } |
| 6576 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 6577 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6578 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6579 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6580 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6581 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6582 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6583 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6584 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 6585 | if( nArg==2 ){ |
| 6586 | enableTimer = booleanValue(azArg[1]); |
| 6587 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6588 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6589 | enableTimer = 0; |
| 6590 | } |
| 6591 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6592 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6593 | rc = 1; |
| 6594 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6595 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6596 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6597 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6598 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6599 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6600 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6601 | rc = 1; |
| 6602 | goto meta_command_exit; |
| 6603 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 6604 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6605 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 6606 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6607 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6608 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6609 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6610 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6611 | } |
| 6612 | #endif |
| 6613 | }else |
| 6614 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6615 | #if SQLITE_USER_AUTHENTICATION |
| 6616 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 6617 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6618 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6619 | rc = 1; |
| 6620 | goto meta_command_exit; |
| 6621 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 6622 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6623 | if( strcmp(azArg[1],"login")==0 ){ |
| 6624 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6625 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6626 | rc = 1; |
| 6627 | goto meta_command_exit; |
| 6628 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6629 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 6630 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6631 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6632 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6633 | rc = 1; |
| 6634 | } |
| 6635 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 6636 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6637 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6638 | rc = 1; |
| 6639 | goto meta_command_exit; |
| 6640 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6641 | rc = sqlite3_user_add(p->db, azArg[2], |
| 6642 | azArg[3], (int)strlen(azArg[3]), |
| 6643 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6644 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6645 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6646 | rc = 1; |
| 6647 | } |
| 6648 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 6649 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6650 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6651 | rc = 1; |
| 6652 | goto meta_command_exit; |
| 6653 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6654 | rc = sqlite3_user_change(p->db, azArg[2], |
| 6655 | azArg[3], (int)strlen(azArg[3]), |
| 6656 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6657 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6658 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6659 | rc = 1; |
| 6660 | } |
| 6661 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 6662 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6663 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6664 | rc = 1; |
| 6665 | goto meta_command_exit; |
| 6666 | } |
| 6667 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 6668 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6669 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6670 | rc = 1; |
| 6671 | } |
| 6672 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6673 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6674 | rc = 1; |
| 6675 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6676 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6677 | }else |
| 6678 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 6679 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6680 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6681 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6682 | sqlite3_libversion(), sqlite3_sourceid()); |
| 6683 | }else |
| 6684 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6685 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 6686 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 6687 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6688 | if( p->db ){ |
| 6689 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 6690 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6691 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 6692 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6693 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6694 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6695 | } |
| 6696 | } |
| 6697 | }else |
| 6698 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 6699 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 6700 | sqlite3_vfs *pVfs; |
| 6701 | sqlite3_vfs *pCurrent = 0; |
| 6702 | if( p->db ){ |
| 6703 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 6704 | } |
| 6705 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 6706 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 6707 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 6708 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6709 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6710 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 6711 | if( pVfs->pNext ){ |
| 6712 | raw_printf(p->out, "-----------------------------------\n"); |
| 6713 | } |
| 6714 | } |
| 6715 | }else |
| 6716 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6717 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 6718 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 6719 | char *zVfsName = 0; |
| 6720 | if( p->db ){ |
| 6721 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 6722 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6723 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6724 | sqlite3_free(zVfsName); |
| 6725 | } |
| 6726 | } |
| 6727 | }else |
| 6728 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6729 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 6730 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6731 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6732 | }else |
| 6733 | #endif |
| 6734 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6735 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6736 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6737 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6738 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6739 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6740 | } |
| 6741 | }else |
| 6742 | |
| 6743 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6744 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6745 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6746 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6747 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6748 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6749 | meta_command_exit: |
| 6750 | if( p->outCount ){ |
| 6751 | p->outCount--; |
| 6752 | if( p->outCount==0 ) output_reset(p); |
| 6753 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6754 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6757 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6758 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 6759 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6760 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6761 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6762 | int i; |
| 6763 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 6764 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
| 6767 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6768 | ** Test to see if a line consists entirely of whitespace. |
| 6769 | */ |
| 6770 | static int _all_whitespace(const char *z){ |
| 6771 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6772 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6773 | if( *z=='/' && z[1]=='*' ){ |
| 6774 | z += 2; |
| 6775 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 6776 | if( *z==0 ) return 0; |
| 6777 | z++; |
| 6778 | continue; |
| 6779 | } |
| 6780 | if( *z=='-' && z[1]=='-' ){ |
| 6781 | z += 2; |
| 6782 | while( *z && *z!='\n' ){ z++; } |
| 6783 | if( *z==0 ) return 1; |
| 6784 | continue; |
| 6785 | } |
| 6786 | return 0; |
| 6787 | } |
| 6788 | return 1; |
| 6789 | } |
| 6790 | |
| 6791 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6792 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 6793 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 6794 | ** as is the Oracle "/". |
| 6795 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6796 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6797 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6798 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 6799 | return 1; /* Oracle */ |
| 6800 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6801 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6802 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6803 | return 1; /* SQL Server */ |
| 6804 | } |
| 6805 | return 0; |
| 6806 | } |
| 6807 | |
| 6808 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6809 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 6810 | ** ends in the middle of a string literal or C-style comment. |
| 6811 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6812 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6813 | int rc; |
| 6814 | if( zSql==0 ) return 1; |
| 6815 | zSql[nSql] = ';'; |
| 6816 | zSql[nSql+1] = 0; |
| 6817 | rc = sqlite3_complete(zSql); |
| 6818 | zSql[nSql] = 0; |
| 6819 | return rc; |
| 6820 | } |
| 6821 | |
| 6822 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6823 | ** Run a single line of SQL |
| 6824 | */ |
| 6825 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 6826 | int rc; |
| 6827 | char *zErrMsg = 0; |
| 6828 | |
| 6829 | open_db(p, 0); |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6830 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6831 | BEGIN_TIMER; |
| 6832 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 6833 | END_TIMER; |
| 6834 | if( rc || zErrMsg ){ |
| 6835 | char zPrefix[100]; |
| 6836 | if( in!=0 || !stdin_is_interactive ){ |
| 6837 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 6838 | "Error: near line %d:", startline); |
| 6839 | }else{ |
| 6840 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 6841 | } |
| 6842 | if( zErrMsg!=0 ){ |
| 6843 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 6844 | sqlite3_free(zErrMsg); |
| 6845 | zErrMsg = 0; |
| 6846 | }else{ |
| 6847 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 6848 | } |
| 6849 | return 1; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6850 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6851 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 6852 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 6853 | } |
| 6854 | return 0; |
| 6855 | } |
| 6856 | |
| 6857 | |
| 6858 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6859 | ** Read input from *in and process it. If *in==0 then input |
| 6860 | ** is interactive - the user is typing it it. Otherwise, input |
| 6861 | ** is coming from a file or device. A prompt is issued and history |
| 6862 | ** is saved only if input is interactive. An interrupt signal will |
| 6863 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6864 | ** |
| 6865 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6866 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6867 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6868 | char *zLine = 0; /* A single input line */ |
| 6869 | char *zSql = 0; /* Accumulated SQL text */ |
| 6870 | int nLine; /* Length of current line */ |
| 6871 | int nSql = 0; /* Bytes of zSql[] used */ |
| 6872 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 6873 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6874 | int rc; /* Error code */ |
| 6875 | int errCnt = 0; /* Number of errors seen */ |
| 6876 | int lineno = 0; /* Current line number */ |
| 6877 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6878 | |
| 6879 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 6880 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6881 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6882 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6883 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 6884 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6885 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6886 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6887 | if( seenInterrupt ){ |
| 6888 | if( in!=0 ) break; |
| 6889 | seenInterrupt = 0; |
| 6890 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6891 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6892 | if( nSql==0 && _all_whitespace(zLine) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6893 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6894 | continue; |
| 6895 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 6896 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6897 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6898 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6899 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6900 | break; |
| 6901 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6902 | errCnt++; |
| 6903 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6904 | continue; |
| 6905 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6906 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6907 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6908 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6909 | nLine = strlen30(zLine); |
| 6910 | if( nSql+nLine+2>=nAlloc ){ |
| 6911 | nAlloc = nSql+nLine+100; |
| 6912 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6913 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6914 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6915 | exit(1); |
| 6916 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6917 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6918 | nSqlPrior = nSql; |
| 6919 | if( nSql==0 ){ |
| 6920 | int i; |
| 6921 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 6922 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6923 | memcpy(zSql, zLine+i, nLine+1-i); |
| 6924 | startline = lineno; |
| 6925 | nSql = nLine-i; |
| 6926 | }else{ |
| 6927 | zSql[nSql++] = '\n'; |
| 6928 | memcpy(zSql+nSql, zLine, nLine+1); |
| 6929 | nSql += nLine; |
| 6930 | } |
| 6931 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6932 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6933 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6934 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6935 | if( p->outCount ){ |
| 6936 | output_reset(p); |
| 6937 | p->outCount = 0; |
| 6938 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6939 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6940 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 6941 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6942 | } |
| 6943 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6944 | if( nSql && !_all_whitespace(zSql) ){ |
| 6945 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6946 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 6947 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 6948 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 6949 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6950 | } |
| 6951 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6952 | /* |
| 6953 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6954 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6955 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6956 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6957 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6958 | if( clearFlag ){ |
| 6959 | free(home_dir); |
| 6960 | home_dir = 0; |
| 6961 | return 0; |
| 6962 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6963 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6964 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 6965 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 6966 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 6967 | { |
| 6968 | struct passwd *pwent; |
| 6969 | uid_t uid = getuid(); |
| 6970 | if( (pwent=getpwuid(uid)) != NULL) { |
| 6971 | home_dir = pwent->pw_dir; |
| 6972 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6973 | } |
| 6974 | #endif |
| 6975 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6976 | #if defined(_WIN32_WCE) |
| 6977 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 6978 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6979 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6980 | #else |
| 6981 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6982 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6983 | if (!home_dir) { |
| 6984 | home_dir = getenv("USERPROFILE"); |
| 6985 | } |
| 6986 | #endif |
| 6987 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6988 | if (!home_dir) { |
| 6989 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6992 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6993 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6994 | char *zDrive, *zPath; |
| 6995 | int n; |
| 6996 | zDrive = getenv("HOMEDRIVE"); |
| 6997 | zPath = getenv("HOMEPATH"); |
| 6998 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6999 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7000 | home_dir = malloc( n ); |
| 7001 | if( home_dir==0 ) return 0; |
| 7002 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 7003 | return home_dir; |
| 7004 | } |
| 7005 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7006 | } |
| 7007 | #endif |
| 7008 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7009 | #endif /* !_WIN32_WCE */ |
| 7010 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7011 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7012 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7013 | char *z = malloc( n ); |
| 7014 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7015 | home_dir = z; |
| 7016 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7017 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7018 | return home_dir; |
| 7019 | } |
| 7020 | |
| 7021 | /* |
| 7022 | ** Read input from the file given by sqliterc_override. Or if that |
| 7023 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7024 | ** |
| 7025 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7026 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7027 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7028 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7029 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 7030 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7031 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7032 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 7033 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7034 | FILE *in = NULL; |
| 7035 | |
| 7036 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7037 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7038 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7039 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7040 | " cannot read ~/.sqliterc\n"); |
| 7041 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7042 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 7043 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7044 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 7045 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7046 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 7047 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7048 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7049 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7050 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7051 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7052 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 7053 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7054 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7055 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7058 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7059 | ** Show available command line options |
| 7060 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7061 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7062 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7063 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7064 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7065 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7066 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7067 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7068 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7069 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7070 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7071 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 7072 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 7073 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7074 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7075 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7076 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7077 | " -line set output mode to 'line'\n" |
| 7078 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7079 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7080 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7081 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7082 | " -multiplex enable the multiplexor VFS\n" |
| 7083 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7084 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7085 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7086 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 7087 | " -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] | 7088 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7089 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7090 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7091 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7092 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 7093 | " -vfstrace enable tracing of all VFS calls\n" |
| 7094 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7095 | ; |
| 7096 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7097 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7098 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 7099 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 7100 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7101 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7102 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7103 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7104 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7105 | } |
| 7106 | exit(1); |
| 7107 | } |
| 7108 | |
| 7109 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7110 | ** Initialize the state information in data |
| 7111 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7112 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7113 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7114 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 7115 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7116 | memcpy(data->colSeparator,SEP_Column, 2); |
| 7117 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7118 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7119 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7120 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 7121 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7122 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7123 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 7124 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7127 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7128 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7129 | */ |
| 7130 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7131 | static void printBold(const char *zText){ |
| 7132 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 7133 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 7134 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 7135 | SetConsoleTextAttribute(out, |
| 7136 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 7137 | ); |
| 7138 | printf("%s", zText); |
| 7139 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7140 | } |
| 7141 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7142 | static void printBold(const char *zText){ |
| 7143 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7144 | } |
| 7145 | #endif |
| 7146 | |
| 7147 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7148 | ** Get the argument to an --option. Throw an error and die if no argument |
| 7149 | ** is available. |
| 7150 | */ |
| 7151 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 7152 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7153 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7154 | argv[0], argv[argc-1]); |
| 7155 | exit(1); |
| 7156 | } |
| 7157 | return argv[i]; |
| 7158 | } |
| 7159 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7160 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 7161 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 7162 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 7163 | # else |
| 7164 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 7165 | # endif |
| 7166 | #endif |
| 7167 | |
| 7168 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 7169 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7170 | #else |
| 7171 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7172 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7173 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7174 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7175 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7176 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7177 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7178 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7179 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7180 | int readStdin = 1; |
| 7181 | int nCmd = 0; |
| 7182 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7183 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7184 | setBinaryMode(stdin, 0); |
| 7185 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7186 | stdin_is_interactive = isatty(0); |
| 7187 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7188 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 7189 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7190 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7191 | 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] | 7192 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 7193 | exit(1); |
| 7194 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 7195 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7196 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7197 | #if !SQLITE_SHELL_IS_UTF8 |
| 7198 | sqlite3_initialize(); |
| 7199 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 7200 | if( argv==0 ){ |
| 7201 | raw_printf(stderr, "out of memory\n"); |
| 7202 | exit(1); |
| 7203 | } |
| 7204 | for(i=0; i<argc; i++){ |
| 7205 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 7206 | if( argv[i]==0 ){ |
| 7207 | raw_printf(stderr, "out of memory\n"); |
| 7208 | exit(1); |
| 7209 | } |
| 7210 | } |
| 7211 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7212 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7213 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7214 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7215 | /* Make sure we have a valid signal handler early, before anything |
| 7216 | ** else is done. |
| 7217 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 7218 | #ifdef SIGINT |
| 7219 | signal(SIGINT, interrupt_handler); |
| 7220 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7221 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7222 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 7223 | { |
| 7224 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 7225 | ** of a C-function that will provide the name of the database file. Use |
| 7226 | ** this compile-time option to embed this shell program in larger |
| 7227 | ** applications. */ |
| 7228 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 7229 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 7230 | warnInmemoryDb = 0; |
| 7231 | } |
| 7232 | #endif |
| 7233 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7234 | /* Do an initial pass through the command-line argument to locate |
| 7235 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7236 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7237 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7238 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7239 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7240 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7241 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7242 | if( z[0]!='-' ){ |
| 7243 | if( data.zDbFilename==0 ){ |
| 7244 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7245 | }else{ |
| 7246 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 7247 | ** mean that nothing is read from stdin */ |
| 7248 | readStdin = 0; |
| 7249 | nCmd++; |
| 7250 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 7251 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7252 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7253 | exit(1); |
| 7254 | } |
| 7255 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7256 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7257 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7258 | if( z[1]=='-' ) z++; |
| 7259 | if( strcmp(z,"-separator")==0 |
| 7260 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7261 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7262 | || strcmp(z,"-cmd")==0 |
| 7263 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7264 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7265 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7266 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7267 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7268 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7269 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7270 | ** we do the actual processing of arguments later in a second pass. |
| 7271 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 7272 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7273 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 7274 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7275 | const char *zSize; |
| 7276 | sqlite3_int64 szHeap; |
| 7277 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7278 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7279 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7280 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7281 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 7282 | #else |
| 7283 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7284 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7285 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7286 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7287 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7288 | if( sz>400000 ) sz = 400000; |
| 7289 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7290 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7291 | if( n>10 ) n = 10; |
| 7292 | if( n<1 ) n = 1; |
| 7293 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 7294 | data.shellFlgs |= SHFLG_Scratch; |
| 7295 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7296 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7297 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7298 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7299 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7300 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7301 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 7302 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7303 | data.shellFlgs |= SHFLG_Pagecache; |
| 7304 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7305 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7306 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7307 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7308 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7309 | if( n<0 ) n = 0; |
| 7310 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 7311 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7312 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7313 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7314 | extern int vfstrace_register( |
| 7315 | const char *zTraceName, |
| 7316 | const char *zOldVfsName, |
| 7317 | int (*xOut)(const char*,void*), |
| 7318 | void *pOutArg, |
| 7319 | int makeDefault |
| 7320 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7321 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7322 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7323 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7324 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7325 | extern int sqlite3_multiple_initialize(const char*,int); |
| 7326 | sqlite3_multiplex_initialize(0, 1); |
| 7327 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7328 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 7329 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 7330 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7331 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7332 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7333 | if( pVfs ){ |
| 7334 | sqlite3_vfs_register(pVfs, 1); |
| 7335 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7336 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7337 | exit(1); |
| 7338 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7339 | } |
| 7340 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7341 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7342 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7343 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7344 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7345 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7346 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 7347 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7348 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7349 | } |
| 7350 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7351 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7352 | /* Go ahead and open the database file if it already exists. If the |
| 7353 | ** file does not exist, delay opening it. This prevents empty database |
| 7354 | ** files from being created if a user mistypes the database name argument |
| 7355 | ** to the sqlite command-line tool. |
| 7356 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 7357 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7358 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7361 | /* Process the initialization file if there is one. If no -init option |
| 7362 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 7363 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7364 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7365 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7366 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7367 | /* Make a second pass through the command-line argument and set |
| 7368 | ** options. This second pass is delayed until after the initialization |
| 7369 | ** file is processed so that the command-line arguments will override |
| 7370 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7371 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7372 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7373 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7374 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7375 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 7376 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7377 | i++; |
| 7378 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7379 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7380 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7381 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7382 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7383 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7384 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 7385 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7386 | }else if( strcmp(z,"-csv")==0 ){ |
| 7387 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7388 | memcpy(data.colSeparator,",",2); |
| 7389 | }else if( strcmp(z,"-ascii")==0 ){ |
| 7390 | data.mode = MODE_Ascii; |
| 7391 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7392 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7393 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7394 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7395 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7396 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7397 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7398 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7399 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7400 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7401 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 7402 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7403 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7404 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7405 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7406 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7407 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7408 | }else if( strcmp(z,"-echo")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7409 | ShellSetFlag(&data, SHFLG_Echo); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 7410 | }else if( strcmp(z,"-eqp")==0 ){ |
| 7411 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7412 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 7413 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7414 | }else if( strcmp(z,"-stats")==0 ){ |
| 7415 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 7416 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 7417 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 7418 | }else if( strcmp(z,"-backslash")==0 ){ |
| 7419 | /* Undocumented command-line option: -backslash |
| 7420 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 7421 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 7422 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 7423 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7424 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7425 | }else if( strcmp(z,"-bail")==0 ){ |
| 7426 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7427 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7428 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 7429 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7430 | }else if( strcmp(z,"-interactive")==0 ){ |
| 7431 | stdin_is_interactive = 1; |
| 7432 | }else if( strcmp(z,"-batch")==0 ){ |
| 7433 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7434 | }else if( strcmp(z,"-heap")==0 ){ |
| 7435 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7436 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7437 | i+=2; |
| 7438 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7439 | i+=2; |
| 7440 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7441 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7442 | }else if( strcmp(z,"-mmap")==0 ){ |
| 7443 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7444 | }else if( strcmp(z,"-vfs")==0 ){ |
| 7445 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7446 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7447 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 7448 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7449 | #endif |
| 7450 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7451 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 7452 | i++; |
| 7453 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7454 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7455 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7456 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7457 | /* Run commands that follow -cmd first and separately from commands |
| 7458 | ** that simply appear on the command-line. This seems goofy. It would |
| 7459 | ** be better if all commands ran in the order that they appear. But |
| 7460 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7461 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7462 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7463 | if( z[0]=='.' ){ |
| 7464 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 7465 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7466 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7467 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7468 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 7469 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7470 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7471 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 7472 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7473 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7474 | if( bail_on_error ) return rc; |
| 7475 | } |
| 7476 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7477 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7478 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 7479 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7480 | return 1; |
| 7481 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7482 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7483 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7484 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7485 | if( !readStdin ){ |
| 7486 | /* Run all arguments that do not begin with '-' as if they were separate |
| 7487 | ** command-line inputs, except for the argToSkip argument which contains |
| 7488 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7489 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7490 | for(i=0; i<nCmd; i++){ |
| 7491 | if( azCmd[i][0]=='.' ){ |
| 7492 | rc = do_meta_command(azCmd[i], &data); |
| 7493 | if( rc ) return rc==2 ? 0 : rc; |
| 7494 | }else{ |
| 7495 | open_db(&data, 0); |
| 7496 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 7497 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7498 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7499 | return rc!=0 ? rc : 1; |
| 7500 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7501 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7502 | return rc; |
| 7503 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 7504 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7505 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7506 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7507 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7508 | /* Run commands received from standard input |
| 7509 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7510 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7511 | char *zHome; |
| 7512 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7513 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7514 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 7515 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7516 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7517 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7518 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7519 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7520 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 7521 | printBold("transient in-memory database"); |
| 7522 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7523 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7524 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7525 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7526 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7527 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7528 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7529 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7530 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7531 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7532 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7533 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7534 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 7535 | shell_stifle_history(100); |
| 7536 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7537 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7538 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7539 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7540 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7541 | } |
| 7542 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 7543 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 7544 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 7545 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 7546 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7547 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7548 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7549 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7550 | #if !SQLITE_SHELL_IS_UTF8 |
| 7551 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7552 | sqlite3_free(argv); |
| 7553 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7554 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7555 | } |