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 | /* |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 21 | ** If requested, include the SQLite compiler options file for MSVC. |
| 22 | */ |
| 23 | #if defined(INCLUDE_MSVC_H) |
| 24 | #include "msvc.h" |
| 25 | #endif |
| 26 | |
| 27 | /* |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 28 | ** No support for loadable extensions in VxWorks. |
| 29 | */ |
drh | ada3f2b | 2015-03-23 21:32:50 +0000 | [diff] [blame] | 30 | #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 31 | # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 32 | #endif |
| 33 | |
| 34 | /* |
drh | 36f7dd3 | 2011-10-13 16:02:17 +0000 | [diff] [blame] | 35 | ** Enable large-file support for fopen() and friends on unix. |
| 36 | */ |
| 37 | #ifndef SQLITE_DISABLE_LFS |
| 38 | # define _LARGE_FILE 1 |
| 39 | # ifndef _FILE_OFFSET_BITS |
| 40 | # define _FILE_OFFSET_BITS 64 |
| 41 | # endif |
| 42 | # define _LARGEFILE_SOURCE 1 |
| 43 | #endif |
| 44 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include <stdio.h> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 48 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 49 | #include "sqlite3.h" |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 50 | #if SQLITE_USER_AUTHENTICATION |
| 51 | # include "sqlite3userauth.h" |
| 52 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 53 | #include <ctype.h> |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 54 | #include <stdarg.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 55 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 56 | #if !defined(_WIN32) && !defined(WIN32) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 57 | # include <signal.h> |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 58 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 59 | # include <pwd.h> |
| 60 | # endif |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 61 | # include <unistd.h> |
| 62 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 63 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 64 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 65 | #if HAVE_READLINE |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 66 | # include <readline/readline.h> |
| 67 | # include <readline/history.h> |
drh | 81d7fd1 | 2010-12-08 00:02:26 +0000 | [diff] [blame] | 68 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 69 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 70 | #if HAVE_EDITLINE |
drh | aaa21b4 | 2014-02-11 14:37:51 +0000 | [diff] [blame] | 71 | # include <editline/readline.h> |
| 72 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 73 | |
| 74 | #if HAVE_EDITLINE || HAVE_READLINE |
| 75 | |
| 76 | # define shell_add_history(X) add_history(X) |
| 77 | # define shell_read_history(X) read_history(X) |
| 78 | # define shell_write_history(X) write_history(X) |
| 79 | # define shell_stifle_history(X) stifle_history(X) |
| 80 | # define shell_readline(X) readline(X) |
| 81 | |
| 82 | #elif HAVE_LINENOISE |
| 83 | |
| 84 | # include "linenoise.h" |
| 85 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 86 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 87 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 88 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 89 | # define shell_readline(X) linenoise(X) |
| 90 | |
| 91 | #else |
| 92 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 93 | # define shell_read_history(X) |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 94 | # define shell_write_history(X) |
| 95 | # define shell_stifle_history(X) |
| 96 | |
| 97 | # define SHELL_USE_LOCAL_GETLINE 1 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 100 | |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 101 | #if defined(_WIN32) || defined(WIN32) |
| 102 | # include <io.h> |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 103 | # include <fcntl.h> |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 104 | # define isatty(h) _isatty(h) |
| 105 | # ifndef access |
| 106 | # define access(f,m) _access((f),(m)) |
| 107 | # endif |
| 108 | # undef popen |
| 109 | # define popen _popen |
| 110 | # undef pclose |
| 111 | # define pclose _pclose |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 112 | #else |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 113 | /* Make sure isatty() has a prototype. */ |
| 114 | extern int isatty(int); |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 115 | |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 116 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 117 | /* popen and pclose are not C89 functions and so are |
| 118 | ** sometimes omitted from the <stdio.h> header */ |
| 119 | extern FILE *popen(const char*,const char*); |
| 120 | extern int pclose(FILE*); |
| 121 | # else |
| 122 | # define SQLITE_OMIT_POPEN 1 |
| 123 | # endif |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 124 | #endif |
drh | 53371f9 | 2013-07-25 17:07:03 +0000 | [diff] [blame] | 125 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 126 | #if defined(_WIN32_WCE) |
| 127 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 128 | * thus we always assume that we have a console. That can be |
| 129 | * overridden with the -batch command line option. |
| 130 | */ |
| 131 | #define isatty(x) 1 |
| 132 | #endif |
| 133 | |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 134 | /* ctype macros that work with signed characters */ |
| 135 | #define IsSpace(X) isspace((unsigned char)X) |
| 136 | #define IsDigit(X) isdigit((unsigned char)X) |
| 137 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 138 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 139 | #if defined(_WIN32) || defined(WIN32) |
| 140 | #include <windows.h> |
| 141 | |
| 142 | /* string conversion routines only needed on Win32 */ |
| 143 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 144 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 145 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 146 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 149 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 150 | ** are automatically translated into \r\n. However, this behavior needs |
| 151 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 152 | ** rendering quoted strings that contain \n characters). The following |
| 153 | ** routines take care of that. |
| 154 | */ |
| 155 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 156 | static void setBinaryMode(FILE *file, int isOutput){ |
| 157 | if( isOutput ) fflush(file); |
| 158 | _setmode(_fileno(file), _O_BINARY); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 159 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 160 | static void setTextMode(FILE *file, int isOutput){ |
| 161 | if( isOutput ) fflush(file); |
| 162 | _setmode(_fileno(file), _O_TEXT); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 163 | } |
| 164 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 165 | # define setBinaryMode(X,Y) |
| 166 | # define setTextMode(X,Y) |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 169 | |
| 170 | /* True if the timer is enabled */ |
| 171 | static int enableTimer = 0; |
| 172 | |
| 173 | /* Return the current wall-clock time */ |
| 174 | static sqlite3_int64 timeOfDay(void){ |
| 175 | static sqlite3_vfs *clockVfs = 0; |
| 176 | sqlite3_int64 t; |
| 177 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
dan | 3fd415b | 2015-11-16 08:54:10 +0000 | [diff] [blame] | 178 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 179 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 180 | }else{ |
| 181 | double r; |
| 182 | clockVfs->xCurrentTime(clockVfs, &r); |
| 183 | t = (sqlite3_int64)(r*86400000.0); |
| 184 | } |
| 185 | return t; |
| 186 | } |
| 187 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 188 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 189 | #include <sys/time.h> |
| 190 | #include <sys/resource.h> |
| 191 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 192 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 193 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 194 | struct rusage { |
| 195 | struct timeval ru_utime; /* user CPU time used */ |
| 196 | struct timeval ru_stime; /* system CPU time used */ |
| 197 | }; |
| 198 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 199 | #endif |
| 200 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 201 | /* Saved resource information for the beginning of an operation */ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 202 | static struct rusage sBegin; /* CPU time at start */ |
| 203 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 204 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 205 | /* |
| 206 | ** Begin timing an operation |
| 207 | */ |
| 208 | static void beginTimer(void){ |
| 209 | if( enableTimer ){ |
| 210 | getrusage(RUSAGE_SELF, &sBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 211 | iBegin = timeOfDay(); |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | /* Return the difference of two time_structs in seconds */ |
| 216 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 217 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 218 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | ** Print the timing results. |
| 223 | */ |
| 224 | static void endTimer(void){ |
| 225 | if( enableTimer ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 226 | sqlite3_int64 iEnd = timeOfDay(); |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 227 | struct rusage sEnd; |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 228 | getrusage(RUSAGE_SELF, &sEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 229 | printf("Run Time: real %.3f user %f sys %f\n", |
| 230 | (iEnd - iBegin)*0.001, |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 231 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 232 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 233 | } |
| 234 | } |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 235 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 236 | #define BEGIN_TIMER beginTimer() |
| 237 | #define END_TIMER endTimer() |
| 238 | #define HAS_TIMER 1 |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 239 | |
| 240 | #elif (defined(_WIN32) || defined(WIN32)) |
| 241 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 242 | /* Saved resource information for the beginning of an operation */ |
| 243 | static HANDLE hProcess; |
| 244 | static FILETIME ftKernelBegin; |
| 245 | static FILETIME ftUserBegin; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 246 | static sqlite3_int64 ftWallBegin; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 247 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 248 | LPFILETIME, LPFILETIME); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 249 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 250 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 251 | /* |
| 252 | ** Check to see if we have timer support. Return 1 if necessary |
| 253 | ** support found (or found previously). |
| 254 | */ |
| 255 | static int hasTimer(void){ |
| 256 | if( getProcessTimesAddr ){ |
| 257 | return 1; |
| 258 | } else { |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 259 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 260 | ** versions. See if the version we are running on has it, and if it |
| 261 | ** does, save off a pointer to it and the current process handle. |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 262 | */ |
| 263 | hProcess = GetCurrentProcess(); |
| 264 | if( hProcess ){ |
| 265 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 266 | if( NULL != hinstLib ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 267 | getProcessTimesAddr = |
| 268 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 269 | if( NULL != getProcessTimesAddr ){ |
| 270 | return 1; |
| 271 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 272 | FreeLibrary(hinstLib); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | ** Begin timing an operation |
| 281 | */ |
| 282 | static void beginTimer(void){ |
| 283 | if( enableTimer && getProcessTimesAddr ){ |
| 284 | FILETIME ftCreation, ftExit; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 285 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 286 | &ftKernelBegin,&ftUserBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 287 | ftWallBegin = timeOfDay(); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | /* Return the difference of two FILETIME structs in seconds */ |
| 292 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 293 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 294 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 295 | return (double) ((i64End - i64Start) / 10000000.0); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | ** Print the timing results. |
| 300 | */ |
| 301 | static void endTimer(void){ |
| 302 | if( enableTimer && getProcessTimesAddr){ |
| 303 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 304 | sqlite3_int64 ftWallEnd = timeOfDay(); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 305 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 306 | printf("Run Time: real %.3f user %f sys %f\n", |
| 307 | (ftWallEnd - ftWallBegin)*0.001, |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 308 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 309 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | #define BEGIN_TIMER beginTimer() |
| 314 | #define END_TIMER endTimer() |
| 315 | #define HAS_TIMER hasTimer() |
| 316 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 317 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 318 | #define BEGIN_TIMER |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 319 | #define END_TIMER |
| 320 | #define HAS_TIMER 0 |
| 321 | #endif |
| 322 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 323 | /* |
| 324 | ** Used to prevent warnings about unused parameters |
| 325 | */ |
| 326 | #define UNUSED_PARAMETER(x) (void)(x) |
| 327 | |
drh | e91d16b | 2008-12-08 18:27:31 +0000 | [diff] [blame] | 328 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 329 | ** If the following flag is set, then command execution stops |
| 330 | ** at an error if we are not interactive. |
| 331 | */ |
| 332 | static int bail_on_error = 0; |
| 333 | |
| 334 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 335 | ** Threat stdin as an interactive input if the following variable |
| 336 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 337 | */ |
| 338 | static int stdin_is_interactive = 1; |
| 339 | |
| 340 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 341 | ** On Windows systems we have to know if standard output is a console |
| 342 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 343 | ** true if translation is required. |
| 344 | */ |
| 345 | static int stdout_is_console = 1; |
| 346 | |
| 347 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 348 | ** The following is the open SQLite database. We make a pointer |
| 349 | ** to this database a static variable so that it can be accessed |
| 350 | ** by the SIGINT handler to interrupt database processing. |
| 351 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 352 | static sqlite3 *globalDb = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 353 | |
| 354 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 355 | ** True if an interrupt (Control-C) has been received. |
| 356 | */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 357 | static volatile int seenInterrupt = 0; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 358 | |
| 359 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 360 | ** This is the name of our program. It is set in main(), used |
| 361 | ** in a number of other places, mostly for error messages. |
| 362 | */ |
| 363 | static char *Argv0; |
| 364 | |
| 365 | /* |
| 366 | ** Prompt strings. Initialized in main. Settable with |
| 367 | ** .prompt main continue |
| 368 | */ |
| 369 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 370 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 371 | |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 372 | /* |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 373 | ** Render output like fprintf(). Except, if the output is going to the |
| 374 | ** console and if this is running on a Windows machine, translate the |
| 375 | ** output from UTF-8 into MBCS. |
| 376 | */ |
| 377 | #if defined(_WIN32) || defined(WIN32) |
| 378 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 379 | va_list ap; |
| 380 | va_start(ap, zFormat); |
| 381 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 382 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 383 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 384 | sqlite3_free(z1); |
| 385 | fputs(z2, out); |
| 386 | sqlite3_free(z2); |
| 387 | }else{ |
| 388 | vfprintf(out, zFormat, ap); |
| 389 | } |
| 390 | va_end(ap); |
| 391 | } |
| 392 | #elif !defined(utf8_printf) |
| 393 | # define utf8_printf fprintf |
| 394 | #endif |
| 395 | |
| 396 | /* |
| 397 | ** Render output like fprintf(). This should not be used on anything that |
| 398 | ** includes string formatting (e.g. "%s"). |
| 399 | */ |
| 400 | #if !defined(raw_printf) |
| 401 | # define raw_printf fprintf |
| 402 | #endif |
| 403 | |
| 404 | /* |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 405 | ** Write I/O traces to the following stream. |
| 406 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 407 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 408 | static FILE *iotrace = 0; |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 409 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | ** This routine works like printf in that its first argument is a |
| 413 | ** format string and subsequent arguments are values to be substituted |
| 414 | ** in place of % fields. The result of formatting this string |
| 415 | ** is written to iotrace. |
| 416 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 417 | #ifdef SQLITE_ENABLE_IOTRACE |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 418 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 419 | va_list ap; |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 420 | char *z; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 421 | if( iotrace==0 ) return; |
| 422 | va_start(ap, zFormat); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 423 | z = sqlite3_vmprintf(zFormat, ap); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 424 | va_end(ap); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 425 | utf8_printf(iotrace, "%s", z); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 426 | sqlite3_free(z); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 427 | } |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 428 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 429 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 430 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 431 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 432 | ** Determines if a string is a number of not. |
| 433 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 434 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 435 | if( *z=='-' || *z=='+' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 436 | if( !IsDigit(*z) ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | z++; |
| 440 | if( realnum ) *realnum = 0; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 441 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 442 | if( *z=='.' ){ |
| 443 | z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 444 | if( !IsDigit(*z) ) return 0; |
| 445 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 446 | if( realnum ) *realnum = 1; |
| 447 | } |
| 448 | if( *z=='e' || *z=='E' ){ |
| 449 | z++; |
| 450 | if( *z=='+' || *z=='-' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 451 | if( !IsDigit(*z) ) return 0; |
| 452 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 453 | if( realnum ) *realnum = 1; |
| 454 | } |
| 455 | return *z==0; |
| 456 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 457 | |
| 458 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 459 | ** Compute a string length that is limited to what can be stored in |
| 460 | ** lower 30 bits of a 32-bit signed integer. |
| 461 | */ |
| 462 | static int strlen30(const char *z){ |
| 463 | const char *z2 = z; |
| 464 | while( *z2 ){ z2++; } |
| 465 | return 0x3fffffff & (int)(z2 - z); |
| 466 | } |
| 467 | |
| 468 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 469 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 470 | ** the text in memory obtained from malloc() and returns a pointer |
| 471 | ** to the text. NULL is returned at end of file, or if malloc() |
| 472 | ** fails. |
| 473 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 474 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 475 | ** a previous call to this routine that may be reused. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 476 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 477 | static char *local_getline(char *zLine, FILE *in){ |
| 478 | int nLine = zLine==0 ? 0 : 100; |
| 479 | int n = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 480 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 481 | while( 1 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 482 | if( n+100>nLine ){ |
| 483 | nLine = nLine*2 + 100; |
| 484 | zLine = realloc(zLine, nLine); |
| 485 | if( zLine==0 ) return 0; |
| 486 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 487 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 488 | if( n==0 ){ |
| 489 | free(zLine); |
| 490 | return 0; |
| 491 | } |
| 492 | zLine[n] = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 493 | break; |
| 494 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 495 | while( zLine[n] ) n++; |
| 496 | if( n>0 && zLine[n-1]=='\n' ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 497 | n--; |
shaneh | 13b3602 | 2009-12-17 21:07:15 +0000 | [diff] [blame] | 498 | if( n>0 && zLine[n-1]=='\r' ) n--; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 499 | zLine[n] = 0; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 500 | break; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 503 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 504 | /* For interactive input on Windows systems, translate the |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 505 | ** multi-byte characterset characters into UTF-8. */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 506 | if( stdin_is_interactive && in==stdin ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 507 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 508 | if( zTrans ){ |
| 509 | int nTrans = strlen30(zTrans)+1; |
| 510 | if( nTrans>nLine ){ |
| 511 | zLine = realloc(zLine, nTrans); |
| 512 | if( zLine==0 ){ |
| 513 | sqlite3_free(zTrans); |
| 514 | return 0; |
| 515 | } |
| 516 | } |
| 517 | memcpy(zLine, zTrans, nTrans); |
| 518 | sqlite3_free(zTrans); |
| 519 | } |
| 520 | } |
| 521 | #endif /* defined(_WIN32) || defined(WIN32) */ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 522 | return zLine; |
| 523 | } |
| 524 | |
| 525 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 526 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 527 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 528 | ** If in==0 then read from standard input and prompt before each line. |
| 529 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 530 | ** If isContinuation is zero, then the main prompt should be used. |
| 531 | ** |
| 532 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 533 | ** routine that can be reused. |
| 534 | ** |
| 535 | ** The result is stored in space obtained from malloc() and must either |
| 536 | ** be freed by the caller or else passed back into this routine via the |
| 537 | ** zPrior argument for reuse. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 538 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 539 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 540 | char *zPrompt; |
| 541 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 542 | if( in!=0 ){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 543 | zResult = local_getline(zPrior, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 544 | }else{ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 545 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 546 | #if SHELL_USE_LOCAL_GETLINE |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 547 | printf("%s", zPrompt); |
| 548 | fflush(stdout); |
| 549 | zResult = local_getline(zPrior, stdin); |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 550 | #else |
| 551 | free(zPrior); |
| 552 | zResult = shell_readline(zPrompt); |
| 553 | if( zResult && *zResult ) shell_add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 554 | #endif |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 555 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 556 | return zResult; |
| 557 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 558 | /* |
| 559 | ** A variable length string to which one can append text. |
| 560 | */ |
| 561 | typedef struct ShellText ShellText; |
| 562 | struct ShellText { |
| 563 | char *z; |
| 564 | int n; |
| 565 | int nAlloc; |
| 566 | }; |
| 567 | |
| 568 | /* |
| 569 | ** Initialize and destroy a ShellText object |
| 570 | */ |
| 571 | static void initText(ShellText *p){ |
| 572 | memset(p, 0, sizeof(*p)); |
| 573 | } |
| 574 | static void freeText(ShellText *p){ |
| 575 | free(p->z); |
| 576 | initText(p); |
| 577 | } |
| 578 | |
| 579 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 580 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 581 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 582 | ** zIn, if it was not NULL, is freed. |
| 583 | ** |
| 584 | ** If the third argument, quote, is not '\0', then it is used as a |
| 585 | ** quote character for zAppend. |
| 586 | */ |
| 587 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 588 | int len; |
| 589 | int i; |
| 590 | int nAppend = strlen30(zAppend); |
| 591 | |
| 592 | len = nAppend+p->n+1; |
| 593 | if( quote ){ |
| 594 | len += 2; |
| 595 | for(i=0; i<nAppend; i++){ |
| 596 | if( zAppend[i]==quote ) len++; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if( p->n+len>=p->nAlloc ){ |
| 601 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 602 | p->z = realloc(p->z, p->nAlloc); |
| 603 | if( p->z==0 ){ |
| 604 | memset(p, 0, sizeof(*p)); |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if( quote ){ |
| 610 | char *zCsr = p->z+p->n; |
| 611 | *zCsr++ = quote; |
| 612 | for(i=0; i<nAppend; i++){ |
| 613 | *zCsr++ = zAppend[i]; |
| 614 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 615 | } |
| 616 | *zCsr++ = quote; |
| 617 | p->n = (int)(zCsr - p->z); |
| 618 | *zCsr = '\0'; |
| 619 | }else{ |
| 620 | memcpy(p->z+p->n, zAppend, nAppend); |
| 621 | p->n += nAppend; |
| 622 | p->z[p->n] = '\0'; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 628 | ** because it contains non-alphanumeric characters, or because it is an |
| 629 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 630 | ** that quoting is required. |
| 631 | ** |
| 632 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 633 | */ |
| 634 | static char quoteChar(const char *zName){ |
| 635 | /* All SQLite keywords, in alphabetical order */ |
| 636 | static const char *azKeywords[] = { |
| 637 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 638 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 639 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 640 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 641 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 642 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 643 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 644 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 645 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 646 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 647 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 648 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 649 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 650 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 651 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 652 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 653 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 654 | "WITH", "WITHOUT", |
| 655 | }; |
| 656 | int i, lwr, upr, mid, c; |
| 657 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 658 | for(i=0; zName[i]; i++){ |
| 659 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 660 | } |
| 661 | lwr = 0; |
| 662 | upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; |
| 663 | while( lwr<=upr ){ |
| 664 | mid = (lwr+upr)/2; |
| 665 | c = sqlite3_stricmp(azKeywords[mid], zName); |
| 666 | if( c==0 ) return '"'; |
| 667 | if( c<0 ){ |
| 668 | lwr = mid+1; |
| 669 | }else{ |
| 670 | upr = mid-1; |
| 671 | } |
| 672 | } |
| 673 | return 0; |
| 674 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 675 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 676 | /****************************************************************************** |
| 677 | ** SHA3 hash implementation copied from ../ext/misc/shathree.c |
| 678 | */ |
| 679 | typedef sqlite3_uint64 u64; |
| 680 | /* |
| 681 | ** Macros to determine whether the machine is big or little endian, |
| 682 | ** and whether or not that determination is run-time or compile-time. |
| 683 | ** |
| 684 | ** For best performance, an attempt is made to guess at the byte-order |
| 685 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 686 | ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined |
| 687 | ** at run-time. |
| 688 | */ |
| 689 | #ifndef SHA3_BYTEORDER |
| 690 | # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 691 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 692 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 693 | defined(__arm__) |
| 694 | # define SHA3_BYTEORDER 1234 |
| 695 | # elif defined(sparc) || defined(__ppc__) |
| 696 | # define SHA3_BYTEORDER 4321 |
| 697 | # else |
| 698 | # define SHA3_BYTEORDER 0 |
| 699 | # endif |
| 700 | #endif |
| 701 | |
| 702 | |
| 703 | /* |
| 704 | ** State structure for a SHA3 hash in progress |
| 705 | */ |
| 706 | typedef struct SHA3Context SHA3Context; |
| 707 | struct SHA3Context { |
| 708 | union { |
| 709 | u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ |
| 710 | unsigned char x[1600]; /* ... or 1600 bytes */ |
| 711 | } u; |
| 712 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 713 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 714 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 715 | }; |
| 716 | |
| 717 | /* |
| 718 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 719 | */ |
| 720 | static void KeccakF1600Step(SHA3Context *p){ |
| 721 | int i; |
| 722 | u64 B0, B1, B2, B3, B4; |
| 723 | u64 C0, C1, C2, C3, C4; |
| 724 | u64 D0, D1, D2, D3, D4; |
| 725 | static const u64 RC[] = { |
| 726 | 0x0000000000000001ULL, 0x0000000000008082ULL, |
| 727 | 0x800000000000808aULL, 0x8000000080008000ULL, |
| 728 | 0x000000000000808bULL, 0x0000000080000001ULL, |
| 729 | 0x8000000080008081ULL, 0x8000000000008009ULL, |
| 730 | 0x000000000000008aULL, 0x0000000000000088ULL, |
| 731 | 0x0000000080008009ULL, 0x000000008000000aULL, |
| 732 | 0x000000008000808bULL, 0x800000000000008bULL, |
| 733 | 0x8000000000008089ULL, 0x8000000000008003ULL, |
| 734 | 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 735 | 0x000000000000800aULL, 0x800000008000000aULL, |
| 736 | 0x8000000080008081ULL, 0x8000000000008080ULL, |
| 737 | 0x0000000080000001ULL, 0x8000000080008008ULL |
| 738 | }; |
| 739 | # define A00 (p->u.s[0]) |
| 740 | # define A01 (p->u.s[1]) |
| 741 | # define A02 (p->u.s[2]) |
| 742 | # define A03 (p->u.s[3]) |
| 743 | # define A04 (p->u.s[4]) |
| 744 | # define A10 (p->u.s[5]) |
| 745 | # define A11 (p->u.s[6]) |
| 746 | # define A12 (p->u.s[7]) |
| 747 | # define A13 (p->u.s[8]) |
| 748 | # define A14 (p->u.s[9]) |
| 749 | # define A20 (p->u.s[10]) |
| 750 | # define A21 (p->u.s[11]) |
| 751 | # define A22 (p->u.s[12]) |
| 752 | # define A23 (p->u.s[13]) |
| 753 | # define A24 (p->u.s[14]) |
| 754 | # define A30 (p->u.s[15]) |
| 755 | # define A31 (p->u.s[16]) |
| 756 | # define A32 (p->u.s[17]) |
| 757 | # define A33 (p->u.s[18]) |
| 758 | # define A34 (p->u.s[19]) |
| 759 | # define A40 (p->u.s[20]) |
| 760 | # define A41 (p->u.s[21]) |
| 761 | # define A42 (p->u.s[22]) |
| 762 | # define A43 (p->u.s[23]) |
| 763 | # define A44 (p->u.s[24]) |
| 764 | # define ROL64(a,x) ((a<<x)|(a>>(64-x))) |
| 765 | |
| 766 | for(i=0; i<24; i+=4){ |
| 767 | C0 = A00^A10^A20^A30^A40; |
| 768 | C1 = A01^A11^A21^A31^A41; |
| 769 | C2 = A02^A12^A22^A32^A42; |
| 770 | C3 = A03^A13^A23^A33^A43; |
| 771 | C4 = A04^A14^A24^A34^A44; |
| 772 | D0 = C4^ROL64(C1, 1); |
| 773 | D1 = C0^ROL64(C2, 1); |
| 774 | D2 = C1^ROL64(C3, 1); |
| 775 | D3 = C2^ROL64(C4, 1); |
| 776 | D4 = C3^ROL64(C0, 1); |
| 777 | |
| 778 | B0 = (A00^D0); |
| 779 | B1 = ROL64((A11^D1), 44); |
| 780 | B2 = ROL64((A22^D2), 43); |
| 781 | B3 = ROL64((A33^D3), 21); |
| 782 | B4 = ROL64((A44^D4), 14); |
| 783 | A00 = B0 ^((~B1)& B2 ); |
| 784 | A00 ^= RC[i]; |
| 785 | A11 = B1 ^((~B2)& B3 ); |
| 786 | A22 = B2 ^((~B3)& B4 ); |
| 787 | A33 = B3 ^((~B4)& B0 ); |
| 788 | A44 = B4 ^((~B0)& B1 ); |
| 789 | |
| 790 | B2 = ROL64((A20^D0), 3); |
| 791 | B3 = ROL64((A31^D1), 45); |
| 792 | B4 = ROL64((A42^D2), 61); |
| 793 | B0 = ROL64((A03^D3), 28); |
| 794 | B1 = ROL64((A14^D4), 20); |
| 795 | A20 = B0 ^((~B1)& B2 ); |
| 796 | A31 = B1 ^((~B2)& B3 ); |
| 797 | A42 = B2 ^((~B3)& B4 ); |
| 798 | A03 = B3 ^((~B4)& B0 ); |
| 799 | A14 = B4 ^((~B0)& B1 ); |
| 800 | |
| 801 | B4 = ROL64((A40^D0), 18); |
| 802 | B0 = ROL64((A01^D1), 1); |
| 803 | B1 = ROL64((A12^D2), 6); |
| 804 | B2 = ROL64((A23^D3), 25); |
| 805 | B3 = ROL64((A34^D4), 8); |
| 806 | A40 = B0 ^((~B1)& B2 ); |
| 807 | A01 = B1 ^((~B2)& B3 ); |
| 808 | A12 = B2 ^((~B3)& B4 ); |
| 809 | A23 = B3 ^((~B4)& B0 ); |
| 810 | A34 = B4 ^((~B0)& B1 ); |
| 811 | |
| 812 | B1 = ROL64((A10^D0), 36); |
| 813 | B2 = ROL64((A21^D1), 10); |
| 814 | B3 = ROL64((A32^D2), 15); |
| 815 | B4 = ROL64((A43^D3), 56); |
| 816 | B0 = ROL64((A04^D4), 27); |
| 817 | A10 = B0 ^((~B1)& B2 ); |
| 818 | A21 = B1 ^((~B2)& B3 ); |
| 819 | A32 = B2 ^((~B3)& B4 ); |
| 820 | A43 = B3 ^((~B4)& B0 ); |
| 821 | A04 = B4 ^((~B0)& B1 ); |
| 822 | |
| 823 | B3 = ROL64((A30^D0), 41); |
| 824 | B4 = ROL64((A41^D1), 2); |
| 825 | B0 = ROL64((A02^D2), 62); |
| 826 | B1 = ROL64((A13^D3), 55); |
| 827 | B2 = ROL64((A24^D4), 39); |
| 828 | A30 = B0 ^((~B1)& B2 ); |
| 829 | A41 = B1 ^((~B2)& B3 ); |
| 830 | A02 = B2 ^((~B3)& B4 ); |
| 831 | A13 = B3 ^((~B4)& B0 ); |
| 832 | A24 = B4 ^((~B0)& B1 ); |
| 833 | |
| 834 | C0 = A00^A20^A40^A10^A30; |
| 835 | C1 = A11^A31^A01^A21^A41; |
| 836 | C2 = A22^A42^A12^A32^A02; |
| 837 | C3 = A33^A03^A23^A43^A13; |
| 838 | C4 = A44^A14^A34^A04^A24; |
| 839 | D0 = C4^ROL64(C1, 1); |
| 840 | D1 = C0^ROL64(C2, 1); |
| 841 | D2 = C1^ROL64(C3, 1); |
| 842 | D3 = C2^ROL64(C4, 1); |
| 843 | D4 = C3^ROL64(C0, 1); |
| 844 | |
| 845 | B0 = (A00^D0); |
| 846 | B1 = ROL64((A31^D1), 44); |
| 847 | B2 = ROL64((A12^D2), 43); |
| 848 | B3 = ROL64((A43^D3), 21); |
| 849 | B4 = ROL64((A24^D4), 14); |
| 850 | A00 = B0 ^((~B1)& B2 ); |
| 851 | A00 ^= RC[i+1]; |
| 852 | A31 = B1 ^((~B2)& B3 ); |
| 853 | A12 = B2 ^((~B3)& B4 ); |
| 854 | A43 = B3 ^((~B4)& B0 ); |
| 855 | A24 = B4 ^((~B0)& B1 ); |
| 856 | |
| 857 | B2 = ROL64((A40^D0), 3); |
| 858 | B3 = ROL64((A21^D1), 45); |
| 859 | B4 = ROL64((A02^D2), 61); |
| 860 | B0 = ROL64((A33^D3), 28); |
| 861 | B1 = ROL64((A14^D4), 20); |
| 862 | A40 = B0 ^((~B1)& B2 ); |
| 863 | A21 = B1 ^((~B2)& B3 ); |
| 864 | A02 = B2 ^((~B3)& B4 ); |
| 865 | A33 = B3 ^((~B4)& B0 ); |
| 866 | A14 = B4 ^((~B0)& B1 ); |
| 867 | |
| 868 | B4 = ROL64((A30^D0), 18); |
| 869 | B0 = ROL64((A11^D1), 1); |
| 870 | B1 = ROL64((A42^D2), 6); |
| 871 | B2 = ROL64((A23^D3), 25); |
| 872 | B3 = ROL64((A04^D4), 8); |
| 873 | A30 = B0 ^((~B1)& B2 ); |
| 874 | A11 = B1 ^((~B2)& B3 ); |
| 875 | A42 = B2 ^((~B3)& B4 ); |
| 876 | A23 = B3 ^((~B4)& B0 ); |
| 877 | A04 = B4 ^((~B0)& B1 ); |
| 878 | |
| 879 | B1 = ROL64((A20^D0), 36); |
| 880 | B2 = ROL64((A01^D1), 10); |
| 881 | B3 = ROL64((A32^D2), 15); |
| 882 | B4 = ROL64((A13^D3), 56); |
| 883 | B0 = ROL64((A44^D4), 27); |
| 884 | A20 = B0 ^((~B1)& B2 ); |
| 885 | A01 = B1 ^((~B2)& B3 ); |
| 886 | A32 = B2 ^((~B3)& B4 ); |
| 887 | A13 = B3 ^((~B4)& B0 ); |
| 888 | A44 = B4 ^((~B0)& B1 ); |
| 889 | |
| 890 | B3 = ROL64((A10^D0), 41); |
| 891 | B4 = ROL64((A41^D1), 2); |
| 892 | B0 = ROL64((A22^D2), 62); |
| 893 | B1 = ROL64((A03^D3), 55); |
| 894 | B2 = ROL64((A34^D4), 39); |
| 895 | A10 = B0 ^((~B1)& B2 ); |
| 896 | A41 = B1 ^((~B2)& B3 ); |
| 897 | A22 = B2 ^((~B3)& B4 ); |
| 898 | A03 = B3 ^((~B4)& B0 ); |
| 899 | A34 = B4 ^((~B0)& B1 ); |
| 900 | |
| 901 | C0 = A00^A40^A30^A20^A10; |
| 902 | C1 = A31^A21^A11^A01^A41; |
| 903 | C2 = A12^A02^A42^A32^A22; |
| 904 | C3 = A43^A33^A23^A13^A03; |
| 905 | C4 = A24^A14^A04^A44^A34; |
| 906 | D0 = C4^ROL64(C1, 1); |
| 907 | D1 = C0^ROL64(C2, 1); |
| 908 | D2 = C1^ROL64(C3, 1); |
| 909 | D3 = C2^ROL64(C4, 1); |
| 910 | D4 = C3^ROL64(C0, 1); |
| 911 | |
| 912 | B0 = (A00^D0); |
| 913 | B1 = ROL64((A21^D1), 44); |
| 914 | B2 = ROL64((A42^D2), 43); |
| 915 | B3 = ROL64((A13^D3), 21); |
| 916 | B4 = ROL64((A34^D4), 14); |
| 917 | A00 = B0 ^((~B1)& B2 ); |
| 918 | A00 ^= RC[i+2]; |
| 919 | A21 = B1 ^((~B2)& B3 ); |
| 920 | A42 = B2 ^((~B3)& B4 ); |
| 921 | A13 = B3 ^((~B4)& B0 ); |
| 922 | A34 = B4 ^((~B0)& B1 ); |
| 923 | |
| 924 | B2 = ROL64((A30^D0), 3); |
| 925 | B3 = ROL64((A01^D1), 45); |
| 926 | B4 = ROL64((A22^D2), 61); |
| 927 | B0 = ROL64((A43^D3), 28); |
| 928 | B1 = ROL64((A14^D4), 20); |
| 929 | A30 = B0 ^((~B1)& B2 ); |
| 930 | A01 = B1 ^((~B2)& B3 ); |
| 931 | A22 = B2 ^((~B3)& B4 ); |
| 932 | A43 = B3 ^((~B4)& B0 ); |
| 933 | A14 = B4 ^((~B0)& B1 ); |
| 934 | |
| 935 | B4 = ROL64((A10^D0), 18); |
| 936 | B0 = ROL64((A31^D1), 1); |
| 937 | B1 = ROL64((A02^D2), 6); |
| 938 | B2 = ROL64((A23^D3), 25); |
| 939 | B3 = ROL64((A44^D4), 8); |
| 940 | A10 = B0 ^((~B1)& B2 ); |
| 941 | A31 = B1 ^((~B2)& B3 ); |
| 942 | A02 = B2 ^((~B3)& B4 ); |
| 943 | A23 = B3 ^((~B4)& B0 ); |
| 944 | A44 = B4 ^((~B0)& B1 ); |
| 945 | |
| 946 | B1 = ROL64((A40^D0), 36); |
| 947 | B2 = ROL64((A11^D1), 10); |
| 948 | B3 = ROL64((A32^D2), 15); |
| 949 | B4 = ROL64((A03^D3), 56); |
| 950 | B0 = ROL64((A24^D4), 27); |
| 951 | A40 = B0 ^((~B1)& B2 ); |
| 952 | A11 = B1 ^((~B2)& B3 ); |
| 953 | A32 = B2 ^((~B3)& B4 ); |
| 954 | A03 = B3 ^((~B4)& B0 ); |
| 955 | A24 = B4 ^((~B0)& B1 ); |
| 956 | |
| 957 | B3 = ROL64((A20^D0), 41); |
| 958 | B4 = ROL64((A41^D1), 2); |
| 959 | B0 = ROL64((A12^D2), 62); |
| 960 | B1 = ROL64((A33^D3), 55); |
| 961 | B2 = ROL64((A04^D4), 39); |
| 962 | A20 = B0 ^((~B1)& B2 ); |
| 963 | A41 = B1 ^((~B2)& B3 ); |
| 964 | A12 = B2 ^((~B3)& B4 ); |
| 965 | A33 = B3 ^((~B4)& B0 ); |
| 966 | A04 = B4 ^((~B0)& B1 ); |
| 967 | |
| 968 | C0 = A00^A30^A10^A40^A20; |
| 969 | C1 = A21^A01^A31^A11^A41; |
| 970 | C2 = A42^A22^A02^A32^A12; |
| 971 | C3 = A13^A43^A23^A03^A33; |
| 972 | C4 = A34^A14^A44^A24^A04; |
| 973 | D0 = C4^ROL64(C1, 1); |
| 974 | D1 = C0^ROL64(C2, 1); |
| 975 | D2 = C1^ROL64(C3, 1); |
| 976 | D3 = C2^ROL64(C4, 1); |
| 977 | D4 = C3^ROL64(C0, 1); |
| 978 | |
| 979 | B0 = (A00^D0); |
| 980 | B1 = ROL64((A01^D1), 44); |
| 981 | B2 = ROL64((A02^D2), 43); |
| 982 | B3 = ROL64((A03^D3), 21); |
| 983 | B4 = ROL64((A04^D4), 14); |
| 984 | A00 = B0 ^((~B1)& B2 ); |
| 985 | A00 ^= RC[i+3]; |
| 986 | A01 = B1 ^((~B2)& B3 ); |
| 987 | A02 = B2 ^((~B3)& B4 ); |
| 988 | A03 = B3 ^((~B4)& B0 ); |
| 989 | A04 = B4 ^((~B0)& B1 ); |
| 990 | |
| 991 | B2 = ROL64((A10^D0), 3); |
| 992 | B3 = ROL64((A11^D1), 45); |
| 993 | B4 = ROL64((A12^D2), 61); |
| 994 | B0 = ROL64((A13^D3), 28); |
| 995 | B1 = ROL64((A14^D4), 20); |
| 996 | A10 = B0 ^((~B1)& B2 ); |
| 997 | A11 = B1 ^((~B2)& B3 ); |
| 998 | A12 = B2 ^((~B3)& B4 ); |
| 999 | A13 = B3 ^((~B4)& B0 ); |
| 1000 | A14 = B4 ^((~B0)& B1 ); |
| 1001 | |
| 1002 | B4 = ROL64((A20^D0), 18); |
| 1003 | B0 = ROL64((A21^D1), 1); |
| 1004 | B1 = ROL64((A22^D2), 6); |
| 1005 | B2 = ROL64((A23^D3), 25); |
| 1006 | B3 = ROL64((A24^D4), 8); |
| 1007 | A20 = B0 ^((~B1)& B2 ); |
| 1008 | A21 = B1 ^((~B2)& B3 ); |
| 1009 | A22 = B2 ^((~B3)& B4 ); |
| 1010 | A23 = B3 ^((~B4)& B0 ); |
| 1011 | A24 = B4 ^((~B0)& B1 ); |
| 1012 | |
| 1013 | B1 = ROL64((A30^D0), 36); |
| 1014 | B2 = ROL64((A31^D1), 10); |
| 1015 | B3 = ROL64((A32^D2), 15); |
| 1016 | B4 = ROL64((A33^D3), 56); |
| 1017 | B0 = ROL64((A34^D4), 27); |
| 1018 | A30 = B0 ^((~B1)& B2 ); |
| 1019 | A31 = B1 ^((~B2)& B3 ); |
| 1020 | A32 = B2 ^((~B3)& B4 ); |
| 1021 | A33 = B3 ^((~B4)& B0 ); |
| 1022 | A34 = B4 ^((~B0)& B1 ); |
| 1023 | |
| 1024 | B3 = ROL64((A40^D0), 41); |
| 1025 | B4 = ROL64((A41^D1), 2); |
| 1026 | B0 = ROL64((A42^D2), 62); |
| 1027 | B1 = ROL64((A43^D3), 55); |
| 1028 | B2 = ROL64((A44^D4), 39); |
| 1029 | A40 = B0 ^((~B1)& B2 ); |
| 1030 | A41 = B1 ^((~B2)& B3 ); |
| 1031 | A42 = B2 ^((~B3)& B4 ); |
| 1032 | A43 = B3 ^((~B4)& B0 ); |
| 1033 | A44 = B4 ^((~B0)& B1 ); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | ** Initialize a new hash. iSize determines the size of the hash |
| 1039 | ** in bits and should be one of 224, 256, 384, or 512. Or iSize |
| 1040 | ** can be zero to use the default hash size of 256 bits. |
| 1041 | */ |
| 1042 | static void SHA3Init(SHA3Context *p, int iSize){ |
| 1043 | memset(p, 0, sizeof(*p)); |
| 1044 | if( iSize>=128 && iSize<=512 ){ |
| 1045 | p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; |
| 1046 | }else{ |
| 1047 | p->nRate = (1600 - 2*256)/8; |
| 1048 | } |
| 1049 | #if SHA3_BYTEORDER==1234 |
| 1050 | /* Known to be little-endian at compile-time. No-op */ |
| 1051 | #elif SHA3_BYTEORDER==4321 |
| 1052 | p->ixMask = 7; /* Big-endian */ |
| 1053 | #else |
| 1054 | { |
| 1055 | static unsigned int one = 1; |
| 1056 | if( 1==*(unsigned char*)&one ){ |
| 1057 | /* Little endian. No byte swapping. */ |
| 1058 | p->ixMask = 0; |
| 1059 | }else{ |
| 1060 | /* Big endian. Byte swap. */ |
| 1061 | p->ixMask = 7; |
| 1062 | } |
| 1063 | } |
| 1064 | #endif |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | ** Make consecutive calls to the SHA3Update function to add new content |
| 1069 | ** to the hash |
| 1070 | */ |
| 1071 | static void SHA3Update( |
| 1072 | SHA3Context *p, |
| 1073 | const unsigned char *aData, |
| 1074 | unsigned int nData |
| 1075 | ){ |
| 1076 | unsigned int i = 0; |
| 1077 | #if SHA3_BYTEORDER==1234 |
| 1078 | if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ |
| 1079 | for(; i+7<nData; i+=8){ |
| 1080 | p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; |
| 1081 | p->nLoaded += 8; |
| 1082 | if( p->nLoaded>=p->nRate ){ |
| 1083 | KeccakF1600Step(p); |
| 1084 | p->nLoaded = 0; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | #endif |
| 1089 | for(; i<nData; i++){ |
| 1090 | #if SHA3_BYTEORDER==1234 |
| 1091 | p->u.x[p->nLoaded] ^= aData[i]; |
| 1092 | #elif SHA3_BYTEORDER==4321 |
| 1093 | p->u.x[p->nLoaded^0x07] ^= aData[i]; |
| 1094 | #else |
| 1095 | p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; |
| 1096 | #endif |
| 1097 | p->nLoaded++; |
| 1098 | if( p->nLoaded==p->nRate ){ |
| 1099 | KeccakF1600Step(p); |
| 1100 | p->nLoaded = 0; |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | ** After all content has been added, invoke SHA3Final() to compute |
| 1107 | ** the final hash. The function returns a pointer to the binary |
| 1108 | ** hash value. |
| 1109 | */ |
| 1110 | static unsigned char *SHA3Final(SHA3Context *p){ |
| 1111 | unsigned int i; |
| 1112 | if( p->nLoaded==p->nRate-1 ){ |
| 1113 | const unsigned char c1 = 0x86; |
| 1114 | SHA3Update(p, &c1, 1); |
| 1115 | }else{ |
| 1116 | const unsigned char c2 = 0x06; |
| 1117 | const unsigned char c3 = 0x80; |
| 1118 | SHA3Update(p, &c2, 1); |
| 1119 | p->nLoaded = p->nRate - 1; |
| 1120 | SHA3Update(p, &c3, 1); |
| 1121 | } |
| 1122 | for(i=0; i<p->nRate; i++){ |
| 1123 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1124 | } |
| 1125 | return &p->u.x[p->nRate]; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | ** Implementation of the sha3(X,SIZE) function. |
| 1130 | ** |
| 1131 | ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default |
| 1132 | ** size is 256. If X is a BLOB, it is hashed as is. |
| 1133 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1134 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1135 | ** of a NULL value is NULL. |
| 1136 | */ |
| 1137 | static void sha3Func( |
| 1138 | sqlite3_context *context, |
| 1139 | int argc, |
| 1140 | sqlite3_value **argv |
| 1141 | ){ |
| 1142 | SHA3Context cx; |
| 1143 | int eType = sqlite3_value_type(argv[0]); |
| 1144 | int nByte = sqlite3_value_bytes(argv[0]); |
| 1145 | int iSize; |
| 1146 | if( argc==1 ){ |
| 1147 | iSize = 256; |
| 1148 | }else{ |
| 1149 | iSize = sqlite3_value_int(argv[1]); |
| 1150 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1151 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1152 | "384 512", -1); |
| 1153 | return; |
| 1154 | } |
| 1155 | } |
| 1156 | if( eType==SQLITE_NULL ) return; |
| 1157 | SHA3Init(&cx, iSize); |
| 1158 | if( eType==SQLITE_BLOB ){ |
| 1159 | SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 1160 | }else{ |
| 1161 | SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); |
| 1162 | } |
| 1163 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1164 | } |
| 1165 | |
| 1166 | /* Compute a string using sqlite3_vsnprintf() with a maximum length |
| 1167 | ** of 50 bytes and add it to the hash. |
| 1168 | */ |
| 1169 | static void hash_step_vformat( |
| 1170 | SHA3Context *p, /* Add content to this context */ |
| 1171 | const char *zFormat, |
| 1172 | ... |
| 1173 | ){ |
| 1174 | va_list ap; |
| 1175 | int n; |
| 1176 | char zBuf[50]; |
| 1177 | va_start(ap, zFormat); |
| 1178 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 1179 | va_end(ap); |
| 1180 | n = (int)strlen(zBuf); |
| 1181 | SHA3Update(p, (unsigned char*)zBuf, n); |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | ** Implementation of the sha3_query(SQL,SIZE) function. |
| 1186 | ** |
| 1187 | ** This function compiles and runs the SQL statement(s) given in the |
| 1188 | ** argument. The results are hashed using a SIZE-bit SHA3. The default |
| 1189 | ** size is 256. |
| 1190 | ** |
| 1191 | ** The format of the byte stream that is hashed is summarized as follows: |
| 1192 | ** |
| 1193 | ** S<n>:<sql> |
| 1194 | ** R |
| 1195 | ** N |
| 1196 | ** I<int> |
| 1197 | ** F<ieee-float> |
| 1198 | ** B<size>:<bytes> |
| 1199 | ** T<size>:<text> |
| 1200 | ** |
| 1201 | ** <sql> is the original SQL text for each statement run and <n> is |
| 1202 | ** the size of that text. The SQL text is UTF-8. A single R character |
| 1203 | ** occurs before the start of each row. N means a NULL value. |
| 1204 | ** I mean an 8-byte little-endian integer <int>. F is a floating point |
| 1205 | ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. |
| 1206 | ** B means blobs of <size> bytes. T means text rendered as <size> |
| 1207 | ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII |
| 1208 | ** text integers. |
| 1209 | ** |
| 1210 | ** For each SQL statement in the X input, there is one S segment. Each |
| 1211 | ** S segment is followed by zero or more R segments, one for each row in the |
| 1212 | ** result set. After each R, there are one or more N, I, F, B, or T segments, |
| 1213 | ** one for each column in the result set. Segments are concatentated directly |
| 1214 | ** with no delimiters of any kind. |
| 1215 | */ |
| 1216 | static void sha3QueryFunc( |
| 1217 | sqlite3_context *context, |
| 1218 | int argc, |
| 1219 | sqlite3_value **argv |
| 1220 | ){ |
| 1221 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1222 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1223 | sqlite3_stmt *pStmt = 0; |
| 1224 | int nCol; /* Number of columns in the result set */ |
| 1225 | int i; /* Loop counter */ |
| 1226 | int rc; |
| 1227 | int n; |
| 1228 | const char *z; |
| 1229 | SHA3Context cx; |
| 1230 | int iSize; |
| 1231 | |
| 1232 | if( argc==1 ){ |
| 1233 | iSize = 256; |
| 1234 | }else{ |
| 1235 | iSize = sqlite3_value_int(argv[1]); |
| 1236 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1237 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1238 | "384 512", -1); |
| 1239 | return; |
| 1240 | } |
| 1241 | } |
| 1242 | if( zSql==0 ) return; |
| 1243 | SHA3Init(&cx, iSize); |
| 1244 | while( zSql[0] ){ |
| 1245 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 1246 | if( rc ){ |
| 1247 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 1248 | zSql, sqlite3_errmsg(db)); |
| 1249 | sqlite3_finalize(pStmt); |
| 1250 | sqlite3_result_error(context, zMsg, -1); |
| 1251 | sqlite3_free(zMsg); |
| 1252 | return; |
| 1253 | } |
| 1254 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 1255 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 1256 | sqlite3_finalize(pStmt); |
| 1257 | sqlite3_result_error(context, zMsg, -1); |
| 1258 | sqlite3_free(zMsg); |
| 1259 | return; |
| 1260 | } |
| 1261 | nCol = sqlite3_column_count(pStmt); |
| 1262 | z = sqlite3_sql(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 1263 | if( z==0 ){ |
| 1264 | sqlite3_finalize(pStmt); |
| 1265 | continue; |
| 1266 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1267 | n = (int)strlen(z); |
| 1268 | hash_step_vformat(&cx,"S%d:",n); |
| 1269 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1270 | |
| 1271 | /* Compute a hash over the result of the query */ |
| 1272 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1273 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 1274 | for(i=0; i<nCol; i++){ |
| 1275 | switch( sqlite3_column_type(pStmt,i) ){ |
| 1276 | case SQLITE_NULL: { |
| 1277 | SHA3Update(&cx, (const unsigned char*)"N",1); |
| 1278 | break; |
| 1279 | } |
| 1280 | case SQLITE_INTEGER: { |
| 1281 | sqlite3_uint64 u; |
| 1282 | int j; |
| 1283 | unsigned char x[9]; |
| 1284 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 1285 | memcpy(&u, &v, 8); |
| 1286 | for(j=8; j>=1; j--){ |
| 1287 | x[j] = u & 0xff; |
| 1288 | u >>= 8; |
| 1289 | } |
| 1290 | x[0] = 'I'; |
| 1291 | SHA3Update(&cx, x, 9); |
| 1292 | break; |
| 1293 | } |
| 1294 | case SQLITE_FLOAT: { |
| 1295 | sqlite3_uint64 u; |
| 1296 | int j; |
| 1297 | unsigned char x[9]; |
| 1298 | double r = sqlite3_column_double(pStmt,i); |
| 1299 | memcpy(&u, &r, 8); |
| 1300 | for(j=8; j>=1; j--){ |
| 1301 | x[j] = u & 0xff; |
| 1302 | u >>= 8; |
| 1303 | } |
| 1304 | x[0] = 'F'; |
| 1305 | SHA3Update(&cx,x,9); |
| 1306 | break; |
| 1307 | } |
| 1308 | case SQLITE_TEXT: { |
| 1309 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1310 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 1311 | hash_step_vformat(&cx,"T%d:",n2); |
| 1312 | SHA3Update(&cx, z2, n2); |
| 1313 | break; |
| 1314 | } |
| 1315 | case SQLITE_BLOB: { |
| 1316 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1317 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 1318 | hash_step_vformat(&cx,"B%d:",n2); |
| 1319 | SHA3Update(&cx, z2, n2); |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | sqlite3_finalize(pStmt); |
| 1326 | } |
| 1327 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1328 | } |
| 1329 | /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c |
| 1330 | ********************************************************************************/ |
| 1331 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1332 | #if defined(SQLITE_ENABLE_SESSION) |
| 1333 | /* |
| 1334 | ** State information for a single open session |
| 1335 | */ |
| 1336 | typedef struct OpenSession OpenSession; |
| 1337 | struct OpenSession { |
| 1338 | char *zName; /* Symbolic name for this session */ |
| 1339 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1340 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1341 | sqlite3_session *p; /* The open session */ |
| 1342 | }; |
| 1343 | #endif |
| 1344 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1345 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1346 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1347 | ** saved so that it can be restored by ".explain off" |
| 1348 | */ |
| 1349 | typedef struct SavedModeInfo SavedModeInfo; |
| 1350 | struct SavedModeInfo { |
| 1351 | int valid; /* Is there legit data in here? */ |
| 1352 | int mode; /* Mode prior to ".explain on" */ |
| 1353 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 1354 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1355 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1356 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1357 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1358 | ** State information about the database connection is contained in an |
| 1359 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1360 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1361 | typedef struct ShellState ShellState; |
| 1362 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1363 | sqlite3 *db; /* The database */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1364 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1365 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1366 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1367 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1368 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1369 | int cnt; /* Number of records displayed so far */ |
| 1370 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 1371 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1372 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1373 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1374 | int cMode; /* temporary output mode for the current query */ |
| 1375 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1376 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1377 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1378 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1379 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1380 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1381 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1382 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1383 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1384 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1385 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1386 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1387 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1388 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1389 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 1390 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1391 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1392 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1393 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1394 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1395 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1396 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1397 | #if defined(SQLITE_ENABLE_SESSION) |
| 1398 | int nSession; /* Number of active sessions */ |
| 1399 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1400 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1401 | }; |
| 1402 | |
| 1403 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1404 | ** These are the allowed shellFlgs values |
| 1405 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 1406 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 1407 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 1408 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 1409 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 1410 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
| 1411 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1412 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
| 1413 | |
| 1414 | /* |
| 1415 | ** Macros for testing and setting shellFlgs |
| 1416 | */ |
| 1417 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1418 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1419 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1422 | ** These are the allowed modes. |
| 1423 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1424 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1425 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1426 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1427 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1428 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1429 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1430 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1431 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1432 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1433 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1434 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1435 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1436 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1437 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1438 | "line", |
| 1439 | "column", |
| 1440 | "list", |
| 1441 | "semi", |
| 1442 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1443 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1444 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1445 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1446 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1447 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1448 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1449 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1450 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1451 | |
| 1452 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1453 | ** These are the column/row/line separators used by the various |
| 1454 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1455 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1456 | #define SEP_Column "|" |
| 1457 | #define SEP_Row "\n" |
| 1458 | #define SEP_Tab "\t" |
| 1459 | #define SEP_Space " " |
| 1460 | #define SEP_Comma "," |
| 1461 | #define SEP_CrLf "\r\n" |
| 1462 | #define SEP_Unit "\x1F" |
| 1463 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1466 | ** Number of elements in an array |
| 1467 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1468 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1469 | |
| 1470 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1471 | ** A callback for the sqlite3_log() interface. |
| 1472 | */ |
| 1473 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1474 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1475 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1476 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1477 | fflush(p->pLog); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1481 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1482 | */ |
| 1483 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1484 | int i; |
| 1485 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1486 | raw_printf(out,"X'"); |
| 1487 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1488 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1492 | ** Output the given string as a quoted string using SQL quoting conventions. |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1493 | ** |
| 1494 | ** The "\n" and "\r" characters are converted to char(10) and char(13) |
| 1495 | ** to prevent them from being transformed by end-of-line translators. |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1496 | */ |
| 1497 | static void output_quoted_string(FILE *out, const char *z){ |
| 1498 | int i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1499 | char c; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1500 | setBinaryMode(out, 1); |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1501 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1502 | if( c==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1503 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1504 | }else{ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1505 | int inQuote = 0; |
| 1506 | int bStarted = 0; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1507 | while( *z ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1508 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1509 | if( c=='\'' ) i++; |
| 1510 | if( i ){ |
| 1511 | if( !inQuote ){ |
| 1512 | if( bStarted ) raw_printf(out, "||"); |
| 1513 | raw_printf(out, "'"); |
| 1514 | inQuote = 1; |
| 1515 | } |
| 1516 | utf8_printf(out, "%.*s", i, z); |
| 1517 | z += i; |
| 1518 | bStarted = 1; |
| 1519 | } |
| 1520 | if( c=='\'' ){ |
| 1521 | raw_printf(out, "'"); |
| 1522 | continue; |
| 1523 | } |
| 1524 | if( inQuote ){ |
| 1525 | raw_printf(out, "'"); |
| 1526 | inQuote = 0; |
| 1527 | } |
| 1528 | if( c==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1529 | break; |
| 1530 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1531 | for(i=0; (c = z[i])=='\r' || c=='\n'; i++){ |
| 1532 | if( bStarted ) raw_printf(out, "||"); |
| 1533 | raw_printf(out, "char(%d)", c); |
| 1534 | bStarted = 1; |
| 1535 | } |
| 1536 | z += i; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1537 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1538 | if( inQuote ) raw_printf(out, "'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1539 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1540 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1544 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1545 | */ |
| 1546 | static void output_c_string(FILE *out, const char *z){ |
| 1547 | unsigned int c; |
| 1548 | fputc('"', out); |
| 1549 | while( (c = *(z++))!=0 ){ |
| 1550 | if( c=='\\' ){ |
| 1551 | fputc(c, out); |
| 1552 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 1553 | }else if( c=='"' ){ |
| 1554 | fputc('\\', out); |
| 1555 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1556 | }else if( c=='\t' ){ |
| 1557 | fputc('\\', out); |
| 1558 | fputc('t', out); |
| 1559 | }else if( c=='\n' ){ |
| 1560 | fputc('\\', out); |
| 1561 | fputc('n', out); |
| 1562 | }else if( c=='\r' ){ |
| 1563 | fputc('\\', out); |
| 1564 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 1565 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1566 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1567 | }else{ |
| 1568 | fputc(c, out); |
| 1569 | } |
| 1570 | } |
| 1571 | fputc('"', out); |
| 1572 | } |
| 1573 | |
| 1574 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1575 | ** Output the given string with characters that are special to |
| 1576 | ** HTML escaped. |
| 1577 | */ |
| 1578 | static void output_html_string(FILE *out, const char *z){ |
| 1579 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 1580 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1581 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1582 | for(i=0; z[i] |
| 1583 | && z[i]!='<' |
| 1584 | && z[i]!='&' |
| 1585 | && z[i]!='>' |
| 1586 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1587 | && z[i]!='\''; |
| 1588 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1589 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1590 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1591 | } |
| 1592 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1593 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1594 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1595 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1596 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1597 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1598 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1599 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1600 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1601 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1602 | }else{ |
| 1603 | break; |
| 1604 | } |
| 1605 | z += i + 1; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1610 | ** If a field contains any character identified by a 1 in the following |
| 1611 | ** array, then the string must be quoted for CSV. |
| 1612 | */ |
| 1613 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1614 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1615 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1616 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1617 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1618 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1619 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1620 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1621 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1622 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1623 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1624 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1625 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1626 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1627 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1628 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1629 | 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] | 1630 | }; |
| 1631 | |
| 1632 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 1633 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1634 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1635 | ** the null value. Strings are quoted if necessary. The separator |
| 1636 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1637 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1638 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1639 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1640 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1641 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1642 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1643 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1644 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1645 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1646 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1647 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1648 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1649 | i = 0; |
| 1650 | break; |
| 1651 | } |
| 1652 | } |
| 1653 | if( i==0 ){ |
| 1654 | putc('"', out); |
| 1655 | for(i=0; z[i]; i++){ |
| 1656 | if( z[i]=='"' ) putc('"', out); |
| 1657 | putc(z[i], out); |
| 1658 | } |
| 1659 | putc('"', out); |
| 1660 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1661 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1662 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1663 | } |
| 1664 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1665 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1669 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1670 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1671 | ** This routine runs when the user presses Ctrl-C |
| 1672 | */ |
| 1673 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1674 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 1675 | seenInterrupt++; |
| 1676 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 1677 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1678 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1679 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1680 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1681 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1682 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1683 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1684 | ** invoked. It always returns SQLITE_OK. |
| 1685 | */ |
| 1686 | static int shellAuth( |
| 1687 | void *pClientData, |
| 1688 | int op, |
| 1689 | const char *zA1, |
| 1690 | const char *zA2, |
| 1691 | const char *zA3, |
| 1692 | const char *zA4 |
| 1693 | ){ |
| 1694 | ShellState *p = (ShellState*)pClientData; |
| 1695 | static const char *azAction[] = { 0, |
| 1696 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1697 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1698 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1699 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1700 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1701 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1702 | "PRAGMA", "READ", "SELECT", |
| 1703 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1704 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1705 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1706 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1707 | }; |
| 1708 | int i; |
| 1709 | const char *az[4]; |
| 1710 | az[0] = zA1; |
| 1711 | az[1] = zA2; |
| 1712 | az[2] = zA3; |
| 1713 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1714 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1715 | for(i=0; i<4; i++){ |
| 1716 | raw_printf(p->out, " "); |
| 1717 | if( az[i] ){ |
| 1718 | output_c_string(p->out, az[i]); |
| 1719 | }else{ |
| 1720 | raw_printf(p->out, "NULL"); |
| 1721 | } |
| 1722 | } |
| 1723 | raw_printf(p->out, "\n"); |
| 1724 | return SQLITE_OK; |
| 1725 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1726 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1727 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1728 | /* |
| 1729 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1730 | ** |
| 1731 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1732 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1733 | */ |
| 1734 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1735 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1736 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1737 | }else{ |
| 1738 | utf8_printf(out, "%s%s", z, zTail); |
| 1739 | } |
| 1740 | } |
| 1741 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1742 | char c = z[n]; |
| 1743 | z[n] = 0; |
| 1744 | printSchemaLine(out, z, zTail); |
| 1745 | z[n] = c; |
| 1746 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1747 | |
| 1748 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1749 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1750 | ** invokes for each row of a query result. |
| 1751 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1752 | static int shell_callback( |
| 1753 | void *pArg, |
| 1754 | int nArg, /* Number of result columns */ |
| 1755 | char **azArg, /* Text of each result column */ |
| 1756 | char **azCol, /* Column names */ |
| 1757 | int *aiType /* Column types */ |
| 1758 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1759 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1760 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1761 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1762 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1763 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1764 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1765 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1766 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1767 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1768 | if( len>w ) w = len; |
| 1769 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1770 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1771 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1772 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1773 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1774 | } |
| 1775 | break; |
| 1776 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1777 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1778 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1779 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1780 | const int *colWidth; |
| 1781 | int showHdr; |
| 1782 | char *rowSep; |
| 1783 | if( p->cMode==MODE_Column ){ |
| 1784 | colWidth = p->colWidth; |
| 1785 | showHdr = p->showHeader; |
| 1786 | rowSep = p->rowSeparator; |
| 1787 | }else{ |
| 1788 | colWidth = aExplainWidths; |
| 1789 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 1790 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1791 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1792 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1793 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1794 | int w, n; |
| 1795 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1796 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1797 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1798 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1799 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1800 | if( w==0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1801 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1802 | if( w<10 ) w = 10; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1803 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1804 | if( w<n ) w = n; |
| 1805 | } |
| 1806 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1807 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1808 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1809 | if( showHdr ){ |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1810 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1811 | utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1812 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1813 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1814 | utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1815 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1816 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1819 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1820 | for(i=0; i<nArg; i++){ |
| 1821 | int w; |
| 1822 | if( i<ArraySize(p->actualWidth) ){ |
| 1823 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1824 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1825 | }else{ |
| 1826 | w = 10; |
| 1827 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1828 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1829 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1830 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1831 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1832 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1835 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1836 | for(i=0; i<nArg; i++){ |
| 1837 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1838 | if( i<ArraySize(p->actualWidth) ){ |
| 1839 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1840 | }else{ |
| 1841 | w = 10; |
| 1842 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1843 | if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1844 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1845 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1846 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1847 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1848 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1849 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1850 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1851 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1852 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1853 | utf8_printf(p->out,"%*.*s%s",-w,-w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1854 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1855 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1856 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1857 | utf8_printf(p->out,"%-*.*s%s",w,w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1858 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1859 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1860 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1861 | } |
| 1862 | break; |
| 1863 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1864 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1865 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1866 | break; |
| 1867 | } |
| 1868 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1869 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1870 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1871 | int nParen = 0; |
| 1872 | char cEnd = 0; |
| 1873 | char c; |
| 1874 | int nLine = 0; |
| 1875 | assert( nArg==1 ); |
| 1876 | if( azArg[0]==0 ) break; |
| 1877 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1878 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1879 | ){ |
| 1880 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1881 | break; |
| 1882 | } |
| 1883 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1884 | j = 0; |
| 1885 | for(i=0; IsSpace(z[i]); i++){} |
| 1886 | for(; (c = z[i])!=0; i++){ |
| 1887 | if( IsSpace(c) ){ |
| 1888 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1889 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1890 | j--; |
| 1891 | } |
| 1892 | z[j++] = c; |
| 1893 | } |
| 1894 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1895 | z[j] = 0; |
| 1896 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1897 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1898 | if( c==cEnd ){ |
| 1899 | cEnd = 0; |
| 1900 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1901 | cEnd = c; |
| 1902 | }else if( c=='[' ){ |
| 1903 | cEnd = ']'; |
| 1904 | }else if( c=='(' ){ |
| 1905 | nParen++; |
| 1906 | }else if( c==')' ){ |
| 1907 | nParen--; |
| 1908 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1909 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1910 | j = 0; |
| 1911 | } |
| 1912 | } |
| 1913 | z[j++] = c; |
| 1914 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 1915 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1916 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1917 | j = 0; |
| 1918 | nLine++; |
| 1919 | while( IsSpace(z[i+1]) ){ i++; } |
| 1920 | } |
| 1921 | } |
| 1922 | z[j] = 0; |
| 1923 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1924 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1925 | sqlite3_free(z); |
| 1926 | break; |
| 1927 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1928 | case MODE_List: { |
| 1929 | if( p->cnt++==0 && p->showHeader ){ |
| 1930 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1931 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1932 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1935 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1936 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1937 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1938 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1939 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1940 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1941 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1942 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1943 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1944 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1945 | } |
| 1946 | break; |
| 1947 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1948 | case MODE_Html: { |
| 1949 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1950 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1951 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1952 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1953 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1954 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1955 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1956 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1957 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1958 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1959 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1960 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1961 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1962 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1963 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1964 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1965 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1966 | break; |
| 1967 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1968 | case MODE_Tcl: { |
| 1969 | if( p->cnt++==0 && p->showHeader ){ |
| 1970 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1971 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1972 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1973 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1974 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1975 | } |
| 1976 | if( azArg==0 ) break; |
| 1977 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1978 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1979 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1980 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1981 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1984 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1985 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1986 | if( p->cnt++==0 && p->showHeader ){ |
| 1987 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1988 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1989 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1990 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1991 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 1992 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1993 | for(i=0; i<nArg; i++){ |
| 1994 | output_csv(p, azArg[i], i<nArg-1); |
| 1995 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1996 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1997 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1998 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1999 | break; |
| 2000 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2001 | case MODE_Quote: |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2002 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2003 | if( azArg==0 ) break; |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2004 | if( p->cMode==MODE_Insert ){ |
| 2005 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2006 | if( p->showHeader ){ |
| 2007 | raw_printf(p->out,"("); |
| 2008 | for(i=0; i<nArg; i++){ |
| 2009 | char *zSep = i>0 ? ",": ""; |
| 2010 | utf8_printf(p->out, "%s%s", zSep, azCol[i]); |
| 2011 | } |
| 2012 | raw_printf(p->out,")"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2013 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2014 | raw_printf(p->out," VALUES("); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2015 | }else if( p->cnt==0 && p->showHeader ){ |
| 2016 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2017 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2018 | output_quoted_string(p->out, azCol[i]); |
| 2019 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2020 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2021 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2022 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2023 | for(i=0; i<nArg; i++){ |
| 2024 | char *zSep = i>0 ? ",": ""; |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2025 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2026 | utf8_printf(p->out,"%sNULL",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2027 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2028 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2029 | output_quoted_string(p->out, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2030 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2031 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2032 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2033 | char z[50]; |
| 2034 | double r = sqlite3_column_double(p->pStmt, i); |
| 2035 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2036 | raw_printf(p->out, "%s%s", zSep, z); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2037 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2038 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2039 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2040 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2041 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2042 | }else if( isNumber(azArg[i], 0) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2043 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2044 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2045 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2046 | output_quoted_string(p->out, azArg[i]); |
| 2047 | } |
| 2048 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2049 | raw_printf(p->out,p->cMode==MODE_Quote?"\n":");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2050 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2051 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2052 | case MODE_Ascii: { |
| 2053 | if( p->cnt++==0 && p->showHeader ){ |
| 2054 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2055 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2056 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2057 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2058 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2059 | } |
| 2060 | if( azArg==0 ) break; |
| 2061 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2062 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2063 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2064 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2065 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2066 | break; |
| 2067 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2068 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2069 | return 0; |
| 2070 | } |
| 2071 | |
| 2072 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2073 | ** This is the callback routine that the SQLite library |
| 2074 | ** invokes for each row of a query result. |
| 2075 | */ |
| 2076 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2077 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2078 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2079 | } |
| 2080 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2081 | /* |
| 2082 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2083 | ** output onto the end of a ShellText object. |
| 2084 | */ |
| 2085 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2086 | ShellText *p = (ShellText*)pArg; |
| 2087 | int i; |
| 2088 | if( p->n ) appendText(p, "|", 0); |
| 2089 | for(i=0; i<nArg; i++){ |
| 2090 | if( i ) appendText(p, ",", 0); |
| 2091 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2092 | } |
| 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | ** Generate an appropriate SELFTEST table in the main database. |
| 2098 | */ |
| 2099 | static void createSelftestTable(ShellState *p){ |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2100 | char *zErrMsg = 0; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2101 | sqlite3_exec(p->db, |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2102 | "SAVEPOINT selftest_init;\n" |
| 2103 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2104 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2105 | " op TEXT,\n" /* Operator: memo run */ |
| 2106 | " cmd TEXT,\n" /* Command text */ |
| 2107 | " ans TEXT\n" /* Desired answer */ |
| 2108 | ");" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2109 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2110 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2111 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2112 | " 'memo','Tests generated by --init');\n" |
| 2113 | "INSERT INTO [_shell$self]\n" |
| 2114 | " SELECT 'run',\n" |
| 2115 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2116 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2117 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2118 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2119 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2120 | " SELECT 'run'," |
| 2121 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2122 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2123 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2124 | " FROM (\n" |
| 2125 | " SELECT name FROM sqlite_master\n" |
| 2126 | " WHERE type='table'\n" |
| 2127 | " AND name<>'selftest'\n" |
| 2128 | " AND coalesce(rootpage,0)>0\n" |
| 2129 | " )\n" |
| 2130 | " ORDER BY name;\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2131 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2132 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2133 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2134 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2135 | "DROP TABLE [_shell$self];" |
| 2136 | ,0,0,&zErrMsg); |
| 2137 | if( zErrMsg ){ |
| 2138 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2139 | sqlite3_free(zErrMsg); |
| 2140 | } |
| 2141 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2144 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2145 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2146 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2147 | ** the name of the table given. Escape any quote characters in the |
| 2148 | ** table name. |
| 2149 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2150 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2151 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2152 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2153 | char *z; |
| 2154 | |
| 2155 | if( p->zDestTable ){ |
| 2156 | free(p->zDestTable); |
| 2157 | p->zDestTable = 0; |
| 2158 | } |
| 2159 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2160 | cQuote = quoteChar(zName); |
| 2161 | n = strlen30(zName); |
| 2162 | if( cQuote ) n += 2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2163 | z = p->zDestTable = malloc( n+1 ); |
| 2164 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2165 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2166 | exit(1); |
| 2167 | } |
| 2168 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2169 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2170 | for(i=0; zName[i]; i++){ |
| 2171 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2172 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2173 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2174 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2175 | z[n] = 0; |
| 2176 | } |
| 2177 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2178 | |
| 2179 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2180 | ** Execute a query statement that will generate SQL output. Print |
| 2181 | ** the result columns, comma-separated, on a line and then add a |
| 2182 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2183 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2184 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2185 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2186 | ** "--" comment occurs at the end of the statement, the comment |
| 2187 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2188 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2189 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2190 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2191 | const char *zSelect, /* SELECT statement to extract content */ |
| 2192 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2193 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2194 | sqlite3_stmt *pSelect; |
| 2195 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2196 | int nResult; |
| 2197 | int i; |
| 2198 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2199 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2200 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2201 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2202 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2203 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2204 | return rc; |
| 2205 | } |
| 2206 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2207 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2208 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2209 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2210 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2211 | zFirstRow = 0; |
| 2212 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2213 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2214 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2215 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2216 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2217 | } |
| 2218 | if( z==0 ) z = ""; |
| 2219 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2220 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2221 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2222 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2223 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2224 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2225 | rc = sqlite3_step(pSelect); |
| 2226 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2227 | rc = sqlite3_finalize(pSelect); |
| 2228 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2229 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2230 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2231 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2232 | } |
| 2233 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2236 | /* |
| 2237 | ** Allocate space and save off current error string. |
| 2238 | */ |
| 2239 | static char *save_err_msg( |
| 2240 | sqlite3 *db /* Database to query */ |
| 2241 | ){ |
| 2242 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2243 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2244 | if( zErrMsg ){ |
| 2245 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2246 | } |
| 2247 | return zErrMsg; |
| 2248 | } |
| 2249 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2250 | #ifdef __linux__ |
| 2251 | /* |
| 2252 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2253 | */ |
| 2254 | static void displayLinuxIoStats(FILE *out){ |
| 2255 | FILE *in; |
| 2256 | char z[200]; |
| 2257 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2258 | in = fopen(z, "rb"); |
| 2259 | if( in==0 ) return; |
| 2260 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2261 | static const struct { |
| 2262 | const char *zPattern; |
| 2263 | const char *zDesc; |
| 2264 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 2265 | { "rchar: ", "Bytes received by read():" }, |
| 2266 | { "wchar: ", "Bytes sent to write():" }, |
| 2267 | { "syscr: ", "Read() system calls:" }, |
| 2268 | { "syscw: ", "Write() system calls:" }, |
| 2269 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2270 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2271 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2272 | }; |
| 2273 | int i; |
| 2274 | for(i=0; i<ArraySize(aTrans); i++){ |
| 2275 | int n = (int)strlen(aTrans[i].zPattern); |
| 2276 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2277 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2278 | break; |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2283 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2284 | #endif |
| 2285 | |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2286 | /* |
| 2287 | ** Display a single line of status using 64-bit values. |
| 2288 | */ |
| 2289 | static void displayStatLine( |
| 2290 | ShellState *p, /* The shell context */ |
| 2291 | char *zLabel, /* Label for this one line */ |
| 2292 | char *zFormat, /* Format for the result */ |
| 2293 | int iStatusCtrl, /* Which status to display */ |
| 2294 | int bReset /* True to reset the stats */ |
| 2295 | ){ |
| 2296 | sqlite3_int64 iCur = -1; |
| 2297 | sqlite3_int64 iHiwtr = -1; |
| 2298 | int i, nPercent; |
| 2299 | char zLine[200]; |
| 2300 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2301 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2302 | if( zFormat[i]=='%' ) nPercent++; |
| 2303 | } |
| 2304 | if( nPercent>1 ){ |
| 2305 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2306 | }else{ |
| 2307 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2308 | } |
| 2309 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2310 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2311 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2312 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2313 | ** Display memory stats. |
| 2314 | */ |
| 2315 | static int display_stats( |
| 2316 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2317 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2318 | int bReset /* True to reset the stats */ |
| 2319 | ){ |
| 2320 | int iCur; |
| 2321 | int iHiwtr; |
| 2322 | |
| 2323 | if( pArg && pArg->out ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2324 | displayStatLine(pArg, "Memory Used:", |
| 2325 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2326 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2327 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2328 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2329 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2330 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2331 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2332 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2333 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2334 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2335 | displayStatLine(pArg, "Number of Scratch Allocations Used:", |
| 2336 | "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2337 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2338 | displayStatLine(pArg, "Number of Scratch Overflow Bytes:", |
| 2339 | "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset); |
| 2340 | displayStatLine(pArg, "Largest Allocation:", |
| 2341 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2342 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2343 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2344 | displayStatLine(pArg, "Largest Scratch Allocation:", |
| 2345 | "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2346 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2347 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2348 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2349 | #endif |
| 2350 | } |
| 2351 | |
| 2352 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2353 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2354 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2355 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2356 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2357 | raw_printf(pArg->out, |
| 2358 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2359 | iCur, iHiwtr); |
| 2360 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2361 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2362 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2363 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2364 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2365 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2366 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2367 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2368 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2369 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2370 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2371 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2372 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2373 | iHiwtr = iCur = -1; |
| 2374 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2375 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2376 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2377 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2378 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2379 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2380 | iHiwtr = iCur = -1; |
| 2381 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2382 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2383 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2384 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2385 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2386 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2387 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2388 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2389 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2390 | iHiwtr = iCur = -1; |
| 2391 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2392 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2393 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2397 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2398 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2399 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2400 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2401 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2402 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2403 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2404 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2405 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2408 | #ifdef __linux__ |
| 2409 | displayLinuxIoStats(pArg->out); |
| 2410 | #endif |
| 2411 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 2412 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2413 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2414 | return 0; |
| 2415 | } |
| 2416 | |
| 2417 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2418 | ** Display scan stats. |
| 2419 | */ |
| 2420 | static void display_scanstats( |
| 2421 | sqlite3 *db, /* Database to query */ |
| 2422 | ShellState *pArg /* Pointer to ShellState */ |
| 2423 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2424 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2425 | UNUSED_PARAMETER(db); |
| 2426 | UNUSED_PARAMETER(pArg); |
| 2427 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2428 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2429 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2430 | mx = 0; |
| 2431 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2432 | double rEstLoop = 1.0; |
| 2433 | for(i=n=0; 1; i++){ |
| 2434 | sqlite3_stmt *p = pArg->pStmt; |
| 2435 | sqlite3_int64 nLoop, nVisit; |
| 2436 | double rEst; |
| 2437 | int iSid; |
| 2438 | const char *zExplain; |
| 2439 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2440 | break; |
| 2441 | } |
| 2442 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2443 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2444 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2445 | if( n==0 ){ |
| 2446 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2447 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2448 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2449 | n++; |
| 2450 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2451 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2452 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2453 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2454 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2455 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2456 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 2457 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2458 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2459 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2460 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2461 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2462 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2466 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2467 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2468 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2469 | ** Otherwise, return zero. |
| 2470 | */ |
| 2471 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2472 | int i; |
| 2473 | for(i=0; azArray[i]; i++){ |
| 2474 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2475 | } |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2481 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2482 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2483 | ** |
| 2484 | ** The indenting rules are: |
| 2485 | ** |
| 2486 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2487 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2488 | ** itself by 2 spaces. |
| 2489 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2490 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2491 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 2492 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2493 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2494 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 2495 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2496 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2497 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2498 | const char *zSql; /* The text of the SQL statement */ |
| 2499 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2500 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2501 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2502 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2503 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 2504 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2505 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2506 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2507 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2508 | const char *azGoto[] = { "Goto", 0 }; |
| 2509 | |
| 2510 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2511 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2512 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2513 | p->cMode = p->mode; |
| 2514 | return; |
| 2515 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2516 | zSql = sqlite3_sql(pSql); |
| 2517 | if( zSql==0 ) return; |
| 2518 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2519 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2520 | p->cMode = p->mode; |
| 2521 | return; |
| 2522 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2523 | |
| 2524 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2525 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2526 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2527 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2530 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2531 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2532 | ** the current instruction is part of a sub-program generated by an |
| 2533 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2534 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2535 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2536 | |
| 2537 | /* Grow the p->aiIndent array as required */ |
| 2538 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2539 | if( iOp==0 ){ |
| 2540 | /* Do further verfication that this is explain output. Abort if |
| 2541 | ** it is not */ |
| 2542 | static const char *explainCols[] = { |
| 2543 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2544 | int jj; |
| 2545 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2546 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2547 | p->cMode = p->mode; |
| 2548 | sqlite3_reset(pSql); |
| 2549 | return; |
| 2550 | } |
| 2551 | } |
| 2552 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2553 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2554 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2555 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2556 | } |
| 2557 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2558 | p->aiIndent[iOp] = 0; |
| 2559 | p->nIndent = iOp+1; |
| 2560 | |
| 2561 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2562 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2563 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2564 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2565 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2566 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2567 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2571 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2572 | sqlite3_free(abYield); |
| 2573 | sqlite3_reset(pSql); |
| 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | ** Free the array allocated by explain_data_prepare(). |
| 2578 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2579 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2580 | sqlite3_free(p->aiIndent); |
| 2581 | p->aiIndent = 0; |
| 2582 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2583 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2587 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2588 | */ |
| 2589 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2590 | extern int sqlite3SelectTrace; |
| 2591 | static int savedSelectTrace; |
| 2592 | #endif |
| 2593 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2594 | extern int sqlite3WhereTrace; |
| 2595 | static int savedWhereTrace; |
| 2596 | #endif |
| 2597 | static void disable_debug_trace_modes(void){ |
| 2598 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2599 | savedSelectTrace = sqlite3SelectTrace; |
| 2600 | sqlite3SelectTrace = 0; |
| 2601 | #endif |
| 2602 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2603 | savedWhereTrace = sqlite3WhereTrace; |
| 2604 | sqlite3WhereTrace = 0; |
| 2605 | #endif |
| 2606 | } |
| 2607 | static void restore_debug_trace_modes(void){ |
| 2608 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2609 | sqlite3SelectTrace = savedSelectTrace; |
| 2610 | #endif |
| 2611 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2612 | sqlite3WhereTrace = savedWhereTrace; |
| 2613 | #endif |
| 2614 | } |
| 2615 | |
| 2616 | /* |
| 2617 | ** Run a prepared statement |
| 2618 | */ |
| 2619 | static void exec_prepared_stmt( |
| 2620 | ShellState *pArg, /* Pointer to ShellState */ |
| 2621 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2622 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2623 | ){ |
| 2624 | int rc; |
| 2625 | |
| 2626 | /* perform the first step. this will tell us if we |
| 2627 | ** have a result set or not and how wide it is. |
| 2628 | */ |
| 2629 | rc = sqlite3_step(pStmt); |
| 2630 | /* if we have a result set... */ |
| 2631 | if( SQLITE_ROW == rc ){ |
| 2632 | /* if we have a callback... */ |
| 2633 | if( xCallback ){ |
| 2634 | /* allocate space for col name ptr, value ptr, and type */ |
| 2635 | int nCol = sqlite3_column_count(pStmt); |
| 2636 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2637 | if( !pData ){ |
| 2638 | rc = SQLITE_NOMEM; |
| 2639 | }else{ |
| 2640 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2641 | char **azVals = &azCols[nCol]; /* Results */ |
| 2642 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2643 | int i, x; |
| 2644 | assert(sizeof(int) <= sizeof(char *)); |
| 2645 | /* save off ptrs to column names */ |
| 2646 | for(i=0; i<nCol; i++){ |
| 2647 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2648 | } |
| 2649 | do{ |
| 2650 | /* extract the data and data types */ |
| 2651 | for(i=0; i<nCol; i++){ |
| 2652 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2653 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2654 | azVals[i] = ""; |
| 2655 | }else{ |
| 2656 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2657 | } |
| 2658 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2659 | rc = SQLITE_NOMEM; |
| 2660 | break; /* from for */ |
| 2661 | } |
| 2662 | } /* end for */ |
| 2663 | |
| 2664 | /* if data and types extracted successfully... */ |
| 2665 | if( SQLITE_ROW == rc ){ |
| 2666 | /* call the supplied callback with the result row data */ |
| 2667 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2668 | rc = SQLITE_ABORT; |
| 2669 | }else{ |
| 2670 | rc = sqlite3_step(pStmt); |
| 2671 | } |
| 2672 | } |
| 2673 | } while( SQLITE_ROW == rc ); |
| 2674 | sqlite3_free(pData); |
| 2675 | } |
| 2676 | }else{ |
| 2677 | do{ |
| 2678 | rc = sqlite3_step(pStmt); |
| 2679 | } while( rc == SQLITE_ROW ); |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2685 | ** Execute a statement or set of statements. Print |
| 2686 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2687 | ** set via the supplied callback. |
| 2688 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2689 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2690 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2691 | ** and callback data argument. |
| 2692 | */ |
| 2693 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2694 | sqlite3 *db, /* An open database */ |
| 2695 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2696 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2697 | /* (not the same as sqlite3_exec) */ |
| 2698 | ShellState *pArg, /* Pointer to ShellState */ |
| 2699 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2700 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2701 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2702 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2703 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2704 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2705 | |
| 2706 | if( pzErrMsg ){ |
| 2707 | *pzErrMsg = NULL; |
| 2708 | } |
| 2709 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2710 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2711 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2712 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2713 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2714 | if( pzErrMsg ){ |
| 2715 | *pzErrMsg = save_err_msg(db); |
| 2716 | } |
| 2717 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2718 | if( !pStmt ){ |
| 2719 | /* this happens for a comment or white-space */ |
| 2720 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2721 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2722 | continue; |
| 2723 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2724 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 2725 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2726 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2727 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2728 | /* save off the prepared statment handle and reset row count */ |
| 2729 | if( pArg ){ |
| 2730 | pArg->pStmt = pStmt; |
| 2731 | pArg->cnt = 0; |
| 2732 | } |
| 2733 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2734 | /* echo the sql statement if echo on */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2735 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2736 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 2737 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2738 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2739 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2740 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2741 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2742 | char *zEQP; |
| 2743 | disable_debug_trace_modes(); |
| 2744 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2745 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2746 | if( rc==SQLITE_OK ){ |
| 2747 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2748 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2749 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2750 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2751 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2752 | } |
| 2753 | } |
| 2754 | sqlite3_finalize(pExplain); |
| 2755 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2756 | if( pArg->autoEQP>=2 ){ |
| 2757 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2758 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2759 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2760 | if( rc==SQLITE_OK ){ |
| 2761 | pArg->cMode = MODE_Explain; |
| 2762 | explain_data_prepare(pArg, pExplain); |
| 2763 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2764 | explain_data_delete(pArg); |
| 2765 | } |
| 2766 | sqlite3_finalize(pExplain); |
| 2767 | sqlite3_free(zEQP); |
| 2768 | } |
| 2769 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2772 | if( pArg ){ |
| 2773 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2774 | if( pArg->autoExplain |
| 2775 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2776 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2777 | ){ |
| 2778 | pArg->cMode = MODE_Explain; |
| 2779 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2780 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2781 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2782 | ** data required to add indents to the output.*/ |
| 2783 | if( pArg->cMode==MODE_Explain ){ |
| 2784 | explain_data_prepare(pArg, pStmt); |
| 2785 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2788 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2789 | explain_data_delete(pArg); |
| 2790 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2791 | /* print usage stats if stats on */ |
| 2792 | if( pArg && pArg->statsOn ){ |
| 2793 | display_stats(db, pArg, 0); |
| 2794 | } |
| 2795 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2796 | /* print loop-counters if required */ |
| 2797 | if( pArg && pArg->scanstatsOn ){ |
| 2798 | display_scanstats(db, pArg); |
| 2799 | } |
| 2800 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2801 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2802 | ** copy of the error message. Otherwise, set zSql to point to the |
| 2803 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2804 | rc2 = sqlite3_finalize(pStmt); |
| 2805 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2806 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2807 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2808 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2809 | }else if( pzErrMsg ){ |
| 2810 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2811 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2812 | |
| 2813 | /* clear saved stmt handle */ |
| 2814 | if( pArg ){ |
| 2815 | pArg->pStmt = NULL; |
| 2816 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2817 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2818 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2819 | |
| 2820 | return rc; |
| 2821 | } |
| 2822 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2823 | /* |
| 2824 | ** Release memory previously allocated by tableColumnList(). |
| 2825 | */ |
| 2826 | static void freeColumnList(char **azCol){ |
| 2827 | int i; |
| 2828 | for(i=1; azCol[i]; i++){ |
| 2829 | sqlite3_free(azCol[i]); |
| 2830 | } |
| 2831 | /* azCol[0] is a static string */ |
| 2832 | sqlite3_free(azCol); |
| 2833 | } |
| 2834 | |
| 2835 | /* |
| 2836 | ** Return a list of pointers to strings which are the names of all |
| 2837 | ** columns in table zTab. The memory to hold the names is dynamically |
| 2838 | ** allocated and must be released by the caller using a subsequent call |
| 2839 | ** to freeColumnList(). |
| 2840 | ** |
| 2841 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 2842 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 2843 | ** name of the rowid column. |
| 2844 | ** |
| 2845 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 2846 | ** by an entry with azCol[i]==0. |
| 2847 | */ |
| 2848 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 2849 | char **azCol = 0; |
| 2850 | sqlite3_stmt *pStmt; |
| 2851 | char *zSql; |
| 2852 | int nCol = 0; |
| 2853 | int nAlloc = 0; |
| 2854 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 2855 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2856 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2857 | int rc; |
| 2858 | |
| 2859 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 2860 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2861 | sqlite3_free(zSql); |
| 2862 | if( rc ) return 0; |
| 2863 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2864 | if( nCol>=nAlloc-2 ){ |
| 2865 | nAlloc = nAlloc*2 + nCol + 10; |
| 2866 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 2867 | if( azCol==0 ){ |
| 2868 | raw_printf(stderr, "Error: out of memory\n"); |
| 2869 | exit(1); |
| 2870 | } |
| 2871 | } |
| 2872 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 2873 | if( sqlite3_column_int(pStmt, 5) ){ |
| 2874 | nPK++; |
| 2875 | if( nPK==1 |
| 2876 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 2877 | "INTEGER")==0 |
| 2878 | ){ |
| 2879 | isIPK = 1; |
| 2880 | }else{ |
| 2881 | isIPK = 0; |
| 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | sqlite3_finalize(pStmt); |
| 2886 | azCol[0] = 0; |
| 2887 | azCol[nCol+1] = 0; |
| 2888 | |
| 2889 | /* The decision of whether or not a rowid really needs to be preserved |
| 2890 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 2891 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 2892 | ** rowids on tables where the rowid is inaccessible because there are other |
| 2893 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 2894 | */ |
| 2895 | if( preserveRowid && isIPK ){ |
| 2896 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 2897 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 2898 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 2899 | ** ROWID aliases. To distinguish these cases, check to see if |
| 2900 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 2901 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 2902 | */ |
| 2903 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 2904 | " WHERE origin='pk'", zTab); |
| 2905 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2906 | sqlite3_free(zSql); |
| 2907 | if( rc ){ |
| 2908 | freeColumnList(azCol); |
| 2909 | return 0; |
| 2910 | } |
| 2911 | rc = sqlite3_step(pStmt); |
| 2912 | sqlite3_finalize(pStmt); |
| 2913 | preserveRowid = rc==SQLITE_ROW; |
| 2914 | } |
| 2915 | if( preserveRowid ){ |
| 2916 | /* Only preserve the rowid if we can find a name to use for the |
| 2917 | ** rowid */ |
| 2918 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 2919 | int i, j; |
| 2920 | for(j=0; j<3; j++){ |
| 2921 | for(i=1; i<=nCol; i++){ |
| 2922 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 2923 | } |
| 2924 | if( i>nCol ){ |
| 2925 | /* At this point, we know that azRowid[j] is not the name of any |
| 2926 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 2927 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 2928 | ** tables will fail this last check */ |
| 2929 | int rc; |
| 2930 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 2931 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 2932 | break; |
| 2933 | } |
| 2934 | } |
| 2935 | } |
| 2936 | return azCol; |
| 2937 | } |
| 2938 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2939 | /* |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 2940 | ** Toggle the reverse_unordered_selects setting. |
| 2941 | */ |
| 2942 | static void toggleSelectOrder(sqlite3 *db){ |
| 2943 | sqlite3_stmt *pStmt = 0; |
| 2944 | int iSetting = 0; |
| 2945 | char zStmt[100]; |
| 2946 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 2947 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2948 | iSetting = sqlite3_column_int(pStmt, 0); |
| 2949 | } |
| 2950 | sqlite3_finalize(pStmt); |
| 2951 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 2952 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 2953 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 2954 | } |
| 2955 | |
| 2956 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2957 | ** This is a different callback routine used for dumping the database. |
| 2958 | ** Each row received by this callback consists of a table name, |
| 2959 | ** the table type ("index" or "table") and SQL to create the table. |
| 2960 | ** This routine should print text sufficient to recreate the table. |
| 2961 | */ |
| 2962 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2963 | int rc; |
| 2964 | const char *zTable; |
| 2965 | const char *zType; |
| 2966 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2967 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2968 | |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 2969 | UNUSED_PARAMETER(azCol); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2970 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2971 | zTable = azArg[0]; |
| 2972 | zType = azArg[1]; |
| 2973 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2974 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2975 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2976 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 2977 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2978 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2979 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 2980 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2981 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 2982 | char *zIns; |
| 2983 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2984 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2985 | p->writableSchema = 1; |
| 2986 | } |
| 2987 | zIns = sqlite3_mprintf( |
| 2988 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 2989 | "VALUES('table','%q','%q',0,'%q');", |
| 2990 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2991 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2992 | sqlite3_free(zIns); |
| 2993 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2994 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2995 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 2996 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2997 | |
| 2998 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2999 | ShellText sSelect; |
| 3000 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3001 | char **azCol; |
| 3002 | int i; |
| 3003 | char *savedDestTable; |
| 3004 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3005 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3006 | azCol = tableColumnList(p, zTable); |
| 3007 | if( azCol==0 ){ |
| 3008 | p->nErr++; |
| 3009 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 3012 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3013 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3014 | initText(&sTable); |
| 3015 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3016 | /* If preserving the rowid, add a column list after the table name. |
| 3017 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3018 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3019 | */ |
| 3020 | if( azCol[0] ){ |
| 3021 | appendText(&sTable, "(", 0); |
| 3022 | appendText(&sTable, azCol[0], 0); |
| 3023 | for(i=1; azCol[i]; i++){ |
| 3024 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3025 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3026 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3027 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3028 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3029 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3030 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3031 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3032 | appendText(&sSelect, "SELECT ", 0); |
| 3033 | if( azCol[0] ){ |
| 3034 | appendText(&sSelect, azCol[0], 0); |
| 3035 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3036 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3037 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3038 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3039 | if( azCol[i+1] ){ |
| 3040 | appendText(&sSelect, ",", 0); |
| 3041 | } |
| 3042 | } |
| 3043 | freeColumnList(azCol); |
| 3044 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3045 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3046 | |
| 3047 | savedDestTable = p->zDestTable; |
| 3048 | savedMode = p->mode; |
| 3049 | p->zDestTable = sTable.z; |
| 3050 | p->mode = p->cMode = MODE_Insert; |
| 3051 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3052 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3053 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3054 | toggleSelectOrder(p->db); |
| 3055 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3056 | toggleSelectOrder(p->db); |
| 3057 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3058 | p->zDestTable = savedDestTable; |
| 3059 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3060 | freeText(&sTable); |
| 3061 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3062 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3063 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3064 | return 0; |
| 3065 | } |
| 3066 | |
| 3067 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3068 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3069 | ** the contents of the query are output as SQL statements. |
| 3070 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3071 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3072 | ** "ORDER BY rowid DESC" to the end. |
| 3073 | */ |
| 3074 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3075 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3076 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3077 | ){ |
| 3078 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3079 | char *zErr = 0; |
| 3080 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3081 | if( rc==SQLITE_CORRUPT ){ |
| 3082 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3083 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3084 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3085 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3086 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3087 | sqlite3_free(zErr); |
| 3088 | zErr = 0; |
| 3089 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3090 | zQ2 = malloc( len+100 ); |
| 3091 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 3092 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3093 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3094 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3095 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3096 | }else{ |
| 3097 | rc = SQLITE_CORRUPT; |
| 3098 | } |
| 3099 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3100 | free(zQ2); |
| 3101 | } |
| 3102 | return rc; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3106 | ** Text of a help message |
| 3107 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 3108 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3109 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3110 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3111 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3112 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3113 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3114 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3115 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3116 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3117 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3118 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3119 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3120 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3121 | " If TABLE specified, only dump tables matching\n" |
| 3122 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3123 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3124 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3125 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3126 | ".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] | 3127 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3128 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3129 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3130 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3131 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3132 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3133 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3134 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3135 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3136 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3137 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3138 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3139 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3140 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 3141 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3142 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3143 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 3144 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3145 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 3146 | ".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] | 3147 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3148 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 3149 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3150 | " column Left-aligned columns. (See .width)\n" |
| 3151 | " html HTML <table> code\n" |
| 3152 | " insert SQL insert statements for TABLE\n" |
| 3153 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 3154 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 3155 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3156 | " tabs Tab-separated values\n" |
| 3157 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3158 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3159 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3160 | ".open ?--new? ?FILE? Close existing database and reopen FILE\n" |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 3161 | " The --new starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3162 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3163 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3164 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3165 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3166 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3167 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3168 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3169 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3170 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3171 | " Add --indent for pretty-printing\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3172 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3173 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3174 | #if defined(SQLITE_ENABLE_SESSION) |
| 3175 | ".session CMD ... Create or control sessions\n" |
| 3176 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3177 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3178 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3179 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3180 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3181 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3182 | ".tables ?TABLE? List names of tables\n" |
| 3183 | " If TABLE specified, only list tables matching\n" |
| 3184 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3185 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 3186 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3187 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3188 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 3189 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 3190 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3191 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 3192 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3193 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3194 | ; |
| 3195 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3196 | #if defined(SQLITE_ENABLE_SESSION) |
| 3197 | /* |
| 3198 | ** Print help information for the ".sessions" command |
| 3199 | */ |
| 3200 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 3201 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3202 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3203 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3204 | "Subcommands:\n" |
| 3205 | " attach TABLE Attach TABLE\n" |
| 3206 | " changeset FILE Write a changeset into FILE\n" |
| 3207 | " close Close one session\n" |
| 3208 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3209 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3210 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3211 | " isempty Query whether the session is empty\n" |
| 3212 | " list List currently open session names\n" |
| 3213 | " open DB NAME Open a new session on DB\n" |
| 3214 | " patchset FILE Write a patchset into FILE\n" |
| 3215 | ); |
| 3216 | } |
| 3217 | #endif |
| 3218 | |
| 3219 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3220 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3221 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3222 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3223 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3224 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 3225 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3226 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3227 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3228 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3229 | ** read. |
| 3230 | ** |
| 3231 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3232 | ** from the file before the buffer is returned. This byte is not included in |
| 3233 | ** the final value of (*pnByte), if applicable. |
| 3234 | ** |
| 3235 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3236 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3237 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3238 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3239 | FILE *in = fopen(zName, "rb"); |
| 3240 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3241 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3242 | char *pBuf; |
| 3243 | if( in==0 ) return 0; |
| 3244 | fseek(in, 0, SEEK_END); |
| 3245 | nIn = ftell(in); |
| 3246 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3247 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3248 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3249 | nRead = fread(pBuf, nIn, 1, in); |
| 3250 | fclose(in); |
| 3251 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3252 | sqlite3_free(pBuf); |
| 3253 | return 0; |
| 3254 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3255 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3256 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3257 | return pBuf; |
| 3258 | } |
| 3259 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3260 | /* |
| 3261 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 3262 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 3263 | ** if the file does not exist or is unreadable. |
| 3264 | */ |
| 3265 | static void readfileFunc( |
| 3266 | sqlite3_context *context, |
| 3267 | int argc, |
| 3268 | sqlite3_value **argv |
| 3269 | ){ |
| 3270 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3271 | void *pBuf; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3272 | int nBuf; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3273 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3274 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3275 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 3276 | if( zName==0 ) return; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3277 | pBuf = readFile(zName, &nBuf); |
| 3278 | if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | /* |
| 3282 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3283 | ** is written into file X. The number of bytes written is returned. Or |
| 3284 | ** NULL is returned if something goes wrong, such as being unable to open |
| 3285 | ** file X for writing. |
| 3286 | */ |
| 3287 | static void writefileFunc( |
| 3288 | sqlite3_context *context, |
| 3289 | int argc, |
| 3290 | sqlite3_value **argv |
| 3291 | ){ |
| 3292 | FILE *out; |
| 3293 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3294 | sqlite3_int64 rc; |
| 3295 | const char *zFile; |
| 3296 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3297 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3298 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3299 | if( zFile==0 ) return; |
| 3300 | out = fopen(zFile, "wb"); |
| 3301 | if( out==0 ) return; |
| 3302 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 3303 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3304 | rc = 0; |
| 3305 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 3306 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3307 | } |
| 3308 | fclose(out); |
| 3309 | sqlite3_result_int64(context, rc); |
| 3310 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3311 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3312 | #if defined(SQLITE_ENABLE_SESSION) |
| 3313 | /* |
| 3314 | ** Close a single OpenSession object and release all of its associated |
| 3315 | ** resources. |
| 3316 | */ |
| 3317 | static void session_close(OpenSession *pSession){ |
| 3318 | int i; |
| 3319 | sqlite3session_delete(pSession->p); |
| 3320 | sqlite3_free(pSession->zName); |
| 3321 | for(i=0; i<pSession->nFilter; i++){ |
| 3322 | sqlite3_free(pSession->azFilter[i]); |
| 3323 | } |
| 3324 | sqlite3_free(pSession->azFilter); |
| 3325 | memset(pSession, 0, sizeof(OpenSession)); |
| 3326 | } |
| 3327 | #endif |
| 3328 | |
| 3329 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3330 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3331 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3332 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3333 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3334 | int i; |
| 3335 | for(i=0; i<p->nSession; i++){ |
| 3336 | session_close(&p->aSession[i]); |
| 3337 | } |
| 3338 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3339 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3340 | #else |
| 3341 | # define session_close_all(X) |
| 3342 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3343 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3344 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 3345 | ** Implementation of the xFilter function for an open session. Omit |
| 3346 | ** any tables named by ".session filter" but let all other table through. |
| 3347 | */ |
| 3348 | #if defined(SQLITE_ENABLE_SESSION) |
| 3349 | static int session_filter(void *pCtx, const char *zTab){ |
| 3350 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3351 | int i; |
| 3352 | for(i=0; i<pSession->nFilter; i++){ |
| 3353 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3354 | } |
| 3355 | return 1; |
| 3356 | } |
| 3357 | #endif |
| 3358 | |
| 3359 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3360 | ** Make sure the database is open. If it is not, then open it. If |
| 3361 | ** the database fails to open, print an error message and exit. |
| 3362 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3363 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3364 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 3365 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3366 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3367 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3368 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3369 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3370 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3371 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3372 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3373 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 3374 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3375 | sqlite3_enable_load_extension(p->db, 1); |
| 3376 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3377 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3378 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3379 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3380 | writefileFunc, 0, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3381 | sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3382 | sha3Func, 0, 0); |
| 3383 | sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3384 | sha3Func, 0, 0); |
| 3385 | sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3386 | sha3QueryFunc, 0, 0); |
| 3387 | sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3388 | sha3QueryFunc, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3393 | ** Do C-language style dequoting. |
| 3394 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3395 | ** \a -> alarm |
| 3396 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3397 | ** \t -> tab |
| 3398 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3399 | ** \v -> vertical tab |
| 3400 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3401 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3402 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3403 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3404 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3405 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3406 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3407 | */ |
| 3408 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3409 | int i, j; |
| 3410 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3411 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3412 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 3413 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3414 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3415 | if( c=='a' ){ |
| 3416 | c = '\a'; |
| 3417 | }else if( c=='b' ){ |
| 3418 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3419 | }else if( c=='t' ){ |
| 3420 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3421 | }else if( c=='n' ){ |
| 3422 | c = '\n'; |
| 3423 | }else if( c=='v' ){ |
| 3424 | c = '\v'; |
| 3425 | }else if( c=='f' ){ |
| 3426 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3427 | }else if( c=='r' ){ |
| 3428 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3429 | }else if( c=='"' ){ |
| 3430 | c = '"'; |
| 3431 | }else if( c=='\'' ){ |
| 3432 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3433 | }else if( c=='\\' ){ |
| 3434 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3435 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 3436 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3437 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3438 | i++; |
| 3439 | c = (c<<3) + z[i] - '0'; |
| 3440 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3441 | i++; |
| 3442 | c = (c<<3) + z[i] - '0'; |
| 3443 | } |
| 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | z[j] = c; |
| 3448 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3449 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3453 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 3454 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3455 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3456 | static int hexDigitValue(char c){ |
| 3457 | if( c>='0' && c<='9' ) return c - '0'; |
| 3458 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 3459 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 3460 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3464 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 3465 | */ |
| 3466 | static sqlite3_int64 integerValue(const char *zArg){ |
| 3467 | sqlite3_int64 v = 0; |
| 3468 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 3469 | { "KiB", 1024 }, |
| 3470 | { "MiB", 1024*1024 }, |
| 3471 | { "GiB", 1024*1024*1024 }, |
| 3472 | { "KB", 1000 }, |
| 3473 | { "MB", 1000000 }, |
| 3474 | { "GB", 1000000000 }, |
| 3475 | { "K", 1000 }, |
| 3476 | { "M", 1000000 }, |
| 3477 | { "G", 1000000000 }, |
| 3478 | }; |
| 3479 | int i; |
| 3480 | int isNeg = 0; |
| 3481 | if( zArg[0]=='-' ){ |
| 3482 | isNeg = 1; |
| 3483 | zArg++; |
| 3484 | }else if( zArg[0]=='+' ){ |
| 3485 | zArg++; |
| 3486 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3487 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3488 | int x; |
| 3489 | zArg += 2; |
| 3490 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 3491 | v = (v<<4) + x; |
| 3492 | zArg++; |
| 3493 | } |
| 3494 | }else{ |
| 3495 | while( IsDigit(zArg[0]) ){ |
| 3496 | v = v*10 + zArg[0] - '0'; |
| 3497 | zArg++; |
| 3498 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3499 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 3500 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3501 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 3502 | v *= aMult[i].iMult; |
| 3503 | break; |
| 3504 | } |
| 3505 | } |
| 3506 | return isNeg? -v : v; |
| 3507 | } |
| 3508 | |
| 3509 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3510 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3511 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3512 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3513 | static int booleanValue(const char *zArg){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3514 | int i; |
| 3515 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3516 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3517 | }else{ |
| 3518 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3519 | } |
| 3520 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3521 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3522 | return 1; |
| 3523 | } |
| 3524 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3525 | return 0; |
| 3526 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3527 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3528 | zArg); |
| 3529 | return 0; |
| 3530 | } |
| 3531 | |
| 3532 | /* |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3533 | ** Set or clear a shell flag according to a boolean value. |
| 3534 | */ |
| 3535 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3536 | if( booleanValue(zArg) ){ |
| 3537 | ShellSetFlag(p, mFlag); |
| 3538 | }else{ |
| 3539 | ShellClearFlag(p, mFlag); |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3544 | ** Close an output file, assuming it is not stderr or stdout |
| 3545 | */ |
| 3546 | static void output_file_close(FILE *f){ |
| 3547 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3552 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3553 | ** filename is "off". |
| 3554 | */ |
| 3555 | static FILE *output_file_open(const char *zFile){ |
| 3556 | FILE *f; |
| 3557 | if( strcmp(zFile,"stdout")==0 ){ |
| 3558 | f = stdout; |
| 3559 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3560 | f = stderr; |
| 3561 | }else if( strcmp(zFile, "off")==0 ){ |
| 3562 | f = 0; |
| 3563 | }else{ |
| 3564 | f = fopen(zFile, "wb"); |
| 3565 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3566 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | return f; |
| 3570 | } |
| 3571 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 3572 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3573 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3574 | /* |
| 3575 | ** A routine for handling output from sqlite3_trace(). |
| 3576 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3577 | static int sql_trace_callback( |
| 3578 | unsigned mType, |
| 3579 | void *pArg, |
| 3580 | void *pP, |
| 3581 | void *pX |
| 3582 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3583 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 3584 | UNUSED_PARAMETER(mType); |
| 3585 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3586 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3587 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3588 | int i = (int)strlen(z); |
| 3589 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3590 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3591 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3592 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3593 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3594 | #endif |
| 3595 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3596 | |
| 3597 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3598 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3599 | ** a useful spot to set a debugger breakpoint. |
| 3600 | */ |
| 3601 | static void test_breakpoint(void){ |
| 3602 | static int nCall = 0; |
| 3603 | nCall++; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3607 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3608 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3609 | typedef struct ImportCtx ImportCtx; |
| 3610 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3611 | const char *zFile; /* Name of the input file */ |
| 3612 | FILE *in; /* Read the CSV text from this input stream */ |
| 3613 | char *z; /* Accumulated text for a field */ |
| 3614 | int n; /* Number of bytes in z */ |
| 3615 | int nAlloc; /* Space allocated for z[] */ |
| 3616 | int nLine; /* Current line number */ |
| 3617 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3618 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3619 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3620 | }; |
| 3621 | |
| 3622 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3623 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3624 | if( p->n+1>=p->nAlloc ){ |
| 3625 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3626 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3627 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3628 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3629 | exit(1); |
| 3630 | } |
| 3631 | } |
| 3632 | p->z[p->n++] = (char)c; |
| 3633 | } |
| 3634 | |
| 3635 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3636 | ** with the option of having a separator other than ",". |
| 3637 | ** |
| 3638 | ** + Input comes from p->in. |
| 3639 | ** + 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] | 3640 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3641 | ** + Use p->cSep as the column separator. The default is ",". |
| 3642 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3643 | ** + Keep track of the line number in p->nLine. |
| 3644 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3645 | ** EOF on end-of-file. |
| 3646 | ** + Report syntax errors on stderr |
| 3647 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3648 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3649 | int c; |
| 3650 | int cSep = p->cColSep; |
| 3651 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3652 | p->n = 0; |
| 3653 | c = fgetc(p->in); |
| 3654 | if( c==EOF || seenInterrupt ){ |
| 3655 | p->cTerm = EOF; |
| 3656 | return 0; |
| 3657 | } |
| 3658 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3659 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3660 | int startLine = p->nLine; |
| 3661 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3662 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3663 | while( 1 ){ |
| 3664 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3665 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3666 | if( c==cQuote ){ |
| 3667 | if( pc==cQuote ){ |
| 3668 | pc = 0; |
| 3669 | continue; |
| 3670 | } |
| 3671 | } |
| 3672 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3673 | || (c==rSep && pc==cQuote) |
| 3674 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3675 | || (c==EOF && pc==cQuote) |
| 3676 | ){ |
| 3677 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3678 | p->cTerm = c; |
| 3679 | break; |
| 3680 | } |
| 3681 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3682 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3683 | p->zFile, p->nLine, cQuote); |
| 3684 | } |
| 3685 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3686 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3687 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3688 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3689 | break; |
| 3690 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3691 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3692 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3693 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3694 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3695 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3696 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3697 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3698 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3699 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3700 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3701 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 3702 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3703 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3704 | p->cTerm = c; |
| 3705 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 3706 | if( p->z ) p->z[p->n] = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3707 | return p->z; |
| 3708 | } |
| 3709 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3710 | /* Read a single field of ASCII delimited text. |
| 3711 | ** |
| 3712 | ** + Input comes from p->in. |
| 3713 | ** + 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] | 3714 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3715 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3716 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3717 | ** + Keep track of the row number in p->nLine. |
| 3718 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3719 | ** EOF on end-of-file. |
| 3720 | ** + Report syntax errors on stderr |
| 3721 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3722 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3723 | int c; |
| 3724 | int cSep = p->cColSep; |
| 3725 | int rSep = p->cRowSep; |
| 3726 | p->n = 0; |
| 3727 | c = fgetc(p->in); |
| 3728 | if( c==EOF || seenInterrupt ){ |
| 3729 | p->cTerm = EOF; |
| 3730 | return 0; |
| 3731 | } |
| 3732 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3733 | import_append_char(p, c); |
| 3734 | c = fgetc(p->in); |
| 3735 | } |
| 3736 | if( c==rSep ){ |
| 3737 | p->nLine++; |
| 3738 | } |
| 3739 | p->cTerm = c; |
| 3740 | if( p->z ) p->z[p->n] = 0; |
| 3741 | return p->z; |
| 3742 | } |
| 3743 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3744 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3745 | ** Try to transfer data for table zTable. If an error is seen while |
| 3746 | ** moving forward, try to go backwards. The backwards movement won't |
| 3747 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3748 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3749 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3750 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3751 | sqlite3 *newDb, |
| 3752 | const char *zTable |
| 3753 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3754 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3755 | sqlite3_stmt *pInsert = 0; |
| 3756 | char *zQuery = 0; |
| 3757 | char *zInsert = 0; |
| 3758 | int rc; |
| 3759 | int i, j, n; |
| 3760 | int nTable = (int)strlen(zTable); |
| 3761 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3762 | int cnt = 0; |
| 3763 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3764 | |
| 3765 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3766 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3767 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3768 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3769 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3770 | zQuery); |
| 3771 | goto end_data_xfer; |
| 3772 | } |
| 3773 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3774 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3775 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3776 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3777 | goto end_data_xfer; |
| 3778 | } |
| 3779 | sqlite3_snprintf(200+nTable,zInsert, |
| 3780 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 3781 | i = (int)strlen(zInsert); |
| 3782 | for(j=1; j<n; j++){ |
| 3783 | memcpy(zInsert+i, ",?", 2); |
| 3784 | i += 2; |
| 3785 | } |
| 3786 | memcpy(zInsert+i, ");", 3); |
| 3787 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 3788 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3789 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3790 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 3791 | zQuery); |
| 3792 | goto end_data_xfer; |
| 3793 | } |
| 3794 | for(k=0; k<2; k++){ |
| 3795 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3796 | for(i=0; i<n; i++){ |
| 3797 | switch( sqlite3_column_type(pQuery, i) ){ |
| 3798 | case SQLITE_NULL: { |
| 3799 | sqlite3_bind_null(pInsert, i+1); |
| 3800 | break; |
| 3801 | } |
| 3802 | case SQLITE_INTEGER: { |
| 3803 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 3804 | break; |
| 3805 | } |
| 3806 | case SQLITE_FLOAT: { |
| 3807 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 3808 | break; |
| 3809 | } |
| 3810 | case SQLITE_TEXT: { |
| 3811 | sqlite3_bind_text(pInsert, i+1, |
| 3812 | (const char*)sqlite3_column_text(pQuery,i), |
| 3813 | -1, SQLITE_STATIC); |
| 3814 | break; |
| 3815 | } |
| 3816 | case SQLITE_BLOB: { |
| 3817 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 3818 | sqlite3_column_bytes(pQuery,i), |
| 3819 | SQLITE_STATIC); |
| 3820 | break; |
| 3821 | } |
| 3822 | } |
| 3823 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3824 | rc = sqlite3_step(pInsert); |
| 3825 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3826 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3827 | sqlite3_errmsg(newDb)); |
| 3828 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3829 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3830 | cnt++; |
| 3831 | if( (cnt%spinRate)==0 ){ |
| 3832 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 3833 | fflush(stdout); |
| 3834 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3835 | } /* End while */ |
| 3836 | if( rc==SQLITE_DONE ) break; |
| 3837 | sqlite3_finalize(pQuery); |
| 3838 | sqlite3_free(zQuery); |
| 3839 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 3840 | zTable); |
| 3841 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3842 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3843 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3844 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3845 | } |
| 3846 | } /* End for(k=0...) */ |
| 3847 | |
| 3848 | end_data_xfer: |
| 3849 | sqlite3_finalize(pQuery); |
| 3850 | sqlite3_finalize(pInsert); |
| 3851 | sqlite3_free(zQuery); |
| 3852 | sqlite3_free(zInsert); |
| 3853 | } |
| 3854 | |
| 3855 | |
| 3856 | /* |
| 3857 | ** Try to transfer all rows of the schema that match zWhere. For |
| 3858 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3859 | ** If an error is encountered while moving forward through the |
| 3860 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3861 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3862 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3863 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3864 | sqlite3 *newDb, |
| 3865 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3866 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3867 | ){ |
| 3868 | sqlite3_stmt *pQuery = 0; |
| 3869 | char *zQuery = 0; |
| 3870 | int rc; |
| 3871 | const unsigned char *zName; |
| 3872 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3873 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3874 | |
| 3875 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 3876 | " WHERE %s", zWhere); |
| 3877 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3878 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3879 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3880 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3881 | zQuery); |
| 3882 | goto end_schema_xfer; |
| 3883 | } |
| 3884 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3885 | zName = sqlite3_column_text(pQuery, 0); |
| 3886 | zSql = sqlite3_column_text(pQuery, 1); |
| 3887 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3888 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 3889 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3890 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3891 | sqlite3_free(zErrMsg); |
| 3892 | zErrMsg = 0; |
| 3893 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3894 | if( xForEach ){ |
| 3895 | xForEach(p, newDb, (const char*)zName); |
| 3896 | } |
| 3897 | printf("done\n"); |
| 3898 | } |
| 3899 | if( rc!=SQLITE_DONE ){ |
| 3900 | sqlite3_finalize(pQuery); |
| 3901 | sqlite3_free(zQuery); |
| 3902 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 3903 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 3904 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3905 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3906 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3907 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3908 | zQuery); |
| 3909 | goto end_schema_xfer; |
| 3910 | } |
| 3911 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3912 | zName = sqlite3_column_text(pQuery, 0); |
| 3913 | zSql = sqlite3_column_text(pQuery, 1); |
| 3914 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3915 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 3916 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3917 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3918 | sqlite3_free(zErrMsg); |
| 3919 | zErrMsg = 0; |
| 3920 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3921 | if( xForEach ){ |
| 3922 | xForEach(p, newDb, (const char*)zName); |
| 3923 | } |
| 3924 | printf("done\n"); |
| 3925 | } |
| 3926 | } |
| 3927 | end_schema_xfer: |
| 3928 | sqlite3_finalize(pQuery); |
| 3929 | sqlite3_free(zQuery); |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 3934 | ** as possible out of the main database (which might be corrupt) and write it |
| 3935 | ** into zNewDb. |
| 3936 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3937 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3938 | int rc; |
| 3939 | sqlite3 *newDb = 0; |
| 3940 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3941 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3942 | return; |
| 3943 | } |
| 3944 | rc = sqlite3_open(zNewDb, &newDb); |
| 3945 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3946 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3947 | sqlite3_errmsg(newDb)); |
| 3948 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 3949 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3950 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3951 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 3952 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3953 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 3954 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3955 | } |
| 3956 | sqlite3_close(newDb); |
| 3957 | } |
| 3958 | |
| 3959 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3960 | ** Change the output file back to stdout |
| 3961 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3962 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3963 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3964 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3965 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3966 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3967 | }else{ |
| 3968 | output_file_close(p->out); |
| 3969 | } |
| 3970 | p->outfile[0] = 0; |
| 3971 | p->out = stdout; |
| 3972 | } |
| 3973 | |
| 3974 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3975 | ** Run an SQL command and return the single integer result. |
| 3976 | */ |
| 3977 | static int db_int(ShellState *p, const char *zSql){ |
| 3978 | sqlite3_stmt *pStmt; |
| 3979 | int res = 0; |
| 3980 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3981 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3982 | res = sqlite3_column_int(pStmt,0); |
| 3983 | } |
| 3984 | sqlite3_finalize(pStmt); |
| 3985 | return res; |
| 3986 | } |
| 3987 | |
| 3988 | /* |
| 3989 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 3990 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3991 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3992 | return (a[0]<<8) + a[1]; |
| 3993 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3994 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3995 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 3996 | } |
| 3997 | |
| 3998 | /* |
| 3999 | ** Implementation of the ".info" command. |
| 4000 | ** |
| 4001 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4002 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4003 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4004 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4005 | { "file change counter:", 24 }, |
| 4006 | { "database page count:", 28 }, |
| 4007 | { "freelist page count:", 36 }, |
| 4008 | { "schema cookie:", 40 }, |
| 4009 | { "schema format:", 44 }, |
| 4010 | { "default cache size:", 48 }, |
| 4011 | { "autovacuum top root:", 52 }, |
| 4012 | { "incremental vacuum:", 64 }, |
| 4013 | { "text encoding:", 56 }, |
| 4014 | { "user version:", 60 }, |
| 4015 | { "application id:", 68 }, |
| 4016 | { "software version:", 96 }, |
| 4017 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4018 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4019 | { "number of tables:", |
| 4020 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4021 | { "number of indexes:", |
| 4022 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4023 | { "number of triggers:", |
| 4024 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4025 | { "number of views:", |
| 4026 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4027 | { "schema size:", |
| 4028 | "SELECT total(length(sql)) FROM %s" }, |
| 4029 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 4030 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4031 | int i; |
| 4032 | char *zSchemaTab; |
| 4033 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 4034 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4035 | open_db(p, 0); |
| 4036 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4037 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4038 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 4039 | return 1; |
| 4040 | } |
| 4041 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 4042 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4043 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4044 | return 1; |
| 4045 | } |
| 4046 | i = get2byteInt(aHdr+16); |
| 4047 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4048 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4049 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4050 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4051 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4052 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4053 | int ofst = aField[i].ofst; |
| 4054 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4055 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4056 | switch( ofst ){ |
| 4057 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4058 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4059 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4060 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4061 | } |
| 4062 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4063 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4064 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4065 | if( zDb==0 ){ |
| 4066 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4067 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4068 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4069 | }else{ |
| 4070 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4071 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4072 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4073 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4074 | int val = db_int(p, zSql); |
| 4075 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4076 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4077 | } |
| 4078 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4082 | /* |
| 4083 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4084 | */ |
| 4085 | static int shellDatabaseError(sqlite3 *db){ |
| 4086 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4087 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4088 | return 1; |
| 4089 | } |
| 4090 | |
| 4091 | /* |
| 4092 | ** Print an out-of-memory message to stderr and return 1. |
| 4093 | */ |
| 4094 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4095 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4096 | return 1; |
| 4097 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4098 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4099 | /* |
| 4100 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4101 | ** if they match and FALSE (0) if they do not match. |
| 4102 | ** |
| 4103 | ** Globbing rules: |
| 4104 | ** |
| 4105 | ** '*' Matches any sequence of zero or more characters. |
| 4106 | ** |
| 4107 | ** '?' Matches exactly one character. |
| 4108 | ** |
| 4109 | ** [...] Matches one character from the enclosed list of |
| 4110 | ** characters. |
| 4111 | ** |
| 4112 | ** [^...] Matches one character not in the enclosed list. |
| 4113 | ** |
| 4114 | ** '#' Matches any sequence of one or more digits with an |
| 4115 | ** optional + or - sign in front |
| 4116 | ** |
| 4117 | ** ' ' Any span of whitespace matches any other span of |
| 4118 | ** whitespace. |
| 4119 | ** |
| 4120 | ** Extra whitespace at the end of z[] is ignored. |
| 4121 | */ |
| 4122 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4123 | int c, c2; |
| 4124 | int invert; |
| 4125 | int seen; |
| 4126 | |
| 4127 | while( (c = (*(zGlob++)))!=0 ){ |
| 4128 | if( IsSpace(c) ){ |
| 4129 | if( !IsSpace(*z) ) return 0; |
| 4130 | while( IsSpace(*zGlob) ) zGlob++; |
| 4131 | while( IsSpace(*z) ) z++; |
| 4132 | }else if( c=='*' ){ |
| 4133 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4134 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4135 | } |
| 4136 | if( c==0 ){ |
| 4137 | return 1; |
| 4138 | }else if( c=='[' ){ |
| 4139 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4140 | z++; |
| 4141 | } |
| 4142 | return (*z)!=0; |
| 4143 | } |
| 4144 | while( (c2 = (*(z++)))!=0 ){ |
| 4145 | while( c2!=c ){ |
| 4146 | c2 = *(z++); |
| 4147 | if( c2==0 ) return 0; |
| 4148 | } |
| 4149 | if( testcase_glob(zGlob,z) ) return 1; |
| 4150 | } |
| 4151 | return 0; |
| 4152 | }else if( c=='?' ){ |
| 4153 | if( (*(z++))==0 ) return 0; |
| 4154 | }else if( c=='[' ){ |
| 4155 | int prior_c = 0; |
| 4156 | seen = 0; |
| 4157 | invert = 0; |
| 4158 | c = *(z++); |
| 4159 | if( c==0 ) return 0; |
| 4160 | c2 = *(zGlob++); |
| 4161 | if( c2=='^' ){ |
| 4162 | invert = 1; |
| 4163 | c2 = *(zGlob++); |
| 4164 | } |
| 4165 | if( c2==']' ){ |
| 4166 | if( c==']' ) seen = 1; |
| 4167 | c2 = *(zGlob++); |
| 4168 | } |
| 4169 | while( c2 && c2!=']' ){ |
| 4170 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4171 | c2 = *(zGlob++); |
| 4172 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4173 | prior_c = 0; |
| 4174 | }else{ |
| 4175 | if( c==c2 ){ |
| 4176 | seen = 1; |
| 4177 | } |
| 4178 | prior_c = c2; |
| 4179 | } |
| 4180 | c2 = *(zGlob++); |
| 4181 | } |
| 4182 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4183 | }else if( c=='#' ){ |
| 4184 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4185 | if( !IsDigit(z[0]) ) return 0; |
| 4186 | z++; |
| 4187 | while( IsDigit(z[0]) ){ z++; } |
| 4188 | }else{ |
| 4189 | if( c!=(*(z++)) ) return 0; |
| 4190 | } |
| 4191 | } |
| 4192 | while( IsSpace(*z) ){ z++; } |
| 4193 | return *z==0; |
| 4194 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4195 | |
| 4196 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4197 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4198 | ** Compare the string as a command-line option with either one or two |
| 4199 | ** initial "-" characters. |
| 4200 | */ |
| 4201 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4202 | if( zStr[0]!='-' ) return 0; |
| 4203 | zStr++; |
| 4204 | if( zStr[0]=='-' ) zStr++; |
| 4205 | return strcmp(zStr, zOpt)==0; |
| 4206 | } |
| 4207 | |
| 4208 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4209 | ** Delete a file. |
| 4210 | */ |
| 4211 | int shellDeleteFile(const char *zFilename){ |
| 4212 | int rc; |
| 4213 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4214 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4215 | rc = _wunlink(z); |
| 4216 | sqlite3_free(z); |
| 4217 | #else |
| 4218 | rc = unlink(zFilename); |
| 4219 | #endif |
| 4220 | return rc; |
| 4221 | } |
| 4222 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4223 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4224 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4225 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4226 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4227 | ** called with four arguments - the parent table name, the parent column name, |
| 4228 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4229 | ** |
| 4230 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4231 | ** |
| 4232 | ** If either of the named tables or columns do not exist, this function |
| 4233 | ** returns an empty string. An empty string is also returned if both tables |
| 4234 | ** and columns exist but have the same default collation sequence. Or, |
| 4235 | ** if both exist but the default collation sequences are different, this |
| 4236 | ** function returns the string " COLLATE <parent-collation>", where |
| 4237 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4238 | */ |
| 4239 | static void shellFkeyCollateClause( |
| 4240 | sqlite3_context *pCtx, |
| 4241 | int nVal, |
| 4242 | sqlite3_value **apVal |
| 4243 | ){ |
| 4244 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4245 | const char *zParent; |
| 4246 | const char *zParentCol; |
| 4247 | const char *zParentSeq; |
| 4248 | const char *zChild; |
| 4249 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4250 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4251 | int rc; |
| 4252 | |
| 4253 | assert( nVal==4 ); |
| 4254 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4255 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4256 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4257 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4258 | |
| 4259 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4260 | rc = sqlite3_table_column_metadata( |
| 4261 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4262 | ); |
| 4263 | if( rc==SQLITE_OK ){ |
| 4264 | rc = sqlite3_table_column_metadata( |
| 4265 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4266 | ); |
| 4267 | } |
| 4268 | |
| 4269 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4270 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4271 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4272 | sqlite3_free(z); |
| 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | |
| 4277 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4278 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4279 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4280 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4281 | ShellState *pState, /* Current shell tool state */ |
| 4282 | char **azArg, /* Array of arguments passed to dot command */ |
| 4283 | int nArg /* Number of entries in azArg[] */ |
| 4284 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4285 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4286 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4287 | int bVerbose = 0; /* If -verbose is present */ |
| 4288 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4289 | int i; /* To iterate through azArg[] */ |
| 4290 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4291 | int rc; /* Return code */ |
| 4292 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4293 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4294 | /* |
| 4295 | ** This SELECT statement returns one row for each foreign key constraint |
| 4296 | ** in the schema of the main database. The column values are: |
| 4297 | ** |
| 4298 | ** 0. The text of an SQL statement similar to: |
| 4299 | ** |
| 4300 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 4301 | ** |
| 4302 | ** This is the same SELECT that the foreign keys implementation needs |
| 4303 | ** to run internally on child tables. If there is an index that can |
| 4304 | ** be used to optimize this query, then it can also be used by the FK |
| 4305 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4306 | ** table. |
| 4307 | ** |
| 4308 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4309 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4310 | ** contains an index that can be used to optimize the query. |
| 4311 | ** |
| 4312 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4313 | ** |
| 4314 | ** "child_table(child_key1, child_key2)" |
| 4315 | ** |
| 4316 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4317 | ** |
| 4318 | ** "parent_table(parent_key1, parent_key2)" |
| 4319 | ** |
| 4320 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4321 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4322 | ** |
| 4323 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4324 | ** |
| 4325 | ** 5. The name of the parent table. |
| 4326 | ** |
| 4327 | ** These six values are used by the C logic below to generate the report. |
| 4328 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4329 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4330 | "SELECT " |
| 4331 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 4332 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 4333 | " || fkey_collate_clause(f.[table], f.[to], s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4334 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4335 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4336 | " || group_concat('*=?', ' AND ') || ')'" |
| 4337 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4338 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4339 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4340 | " f.[table] || '(' || group_concat(COALESCE(f.[to], " |
| 4341 | " (SELECT name FROM pragma_table_info(f.[table]) WHERE pk=seq+1)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4342 | " )) || ')'" |
| 4343 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4344 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4345 | " || ' ON ' || quote(s.name) || '('" |
| 4346 | " || group_concat(quote(f.[from]) ||" |
| 4347 | " fkey_collate_clause(f.[table], f.[to], s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4348 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4349 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4350 | " f.[table] " |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4351 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4352 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
| 4353 | "GROUP BY s.name, f.id " |
| 4354 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4355 | ; |
| 4356 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4357 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 4358 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4359 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4360 | bVerbose = 1; |
| 4361 | } |
| 4362 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4363 | bGroupByParent = 1; |
| 4364 | zIndent = " "; |
| 4365 | } |
| 4366 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4367 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4368 | azArg[0], azArg[1] |
| 4369 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4370 | return SQLITE_ERROR; |
| 4371 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4372 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4373 | |
| 4374 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4375 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4376 | 0, shellFkeyCollateClause, 0, 0 |
| 4377 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4378 | |
| 4379 | |
| 4380 | if( rc==SQLITE_OK ){ |
| 4381 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4382 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4383 | if( rc==SQLITE_OK ){ |
| 4384 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4385 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4386 | |
| 4387 | if( rc==SQLITE_OK ){ |
| 4388 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4389 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4390 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4391 | int res = -1; |
| 4392 | sqlite3_stmt *pExplain = 0; |
| 4393 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4394 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4395 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4396 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4397 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4398 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4399 | |
| 4400 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4401 | if( rc!=SQLITE_OK ) break; |
| 4402 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4403 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
| 4404 | res = (0==sqlite3_strglob(zGlob, zPlan)); |
| 4405 | } |
| 4406 | rc = sqlite3_finalize(pExplain); |
| 4407 | if( rc!=SQLITE_OK ) break; |
| 4408 | |
| 4409 | if( res<0 ){ |
| 4410 | raw_printf(stderr, "Error: internal error"); |
| 4411 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4412 | }else{ |
| 4413 | if( bGroupByParent |
| 4414 | && (bVerbose || res==0) |
| 4415 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 4416 | ){ |
| 4417 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4418 | sqlite3_free(zPrev); |
| 4419 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4420 | } |
| 4421 | |
| 4422 | if( res==0 ){ |
| 4423 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4424 | }else if( bVerbose ){ |
| 4425 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 4426 | zIndent, zFrom, zTarget |
| 4427 | ); |
| 4428 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4429 | } |
| 4430 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4431 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4432 | |
| 4433 | if( rc!=SQLITE_OK ){ |
| 4434 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4435 | } |
| 4436 | |
| 4437 | rc2 = sqlite3_finalize(pSql); |
| 4438 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4439 | rc = rc2; |
| 4440 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4441 | } |
| 4442 | }else{ |
| 4443 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4444 | } |
| 4445 | |
| 4446 | return rc; |
| 4447 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4448 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4449 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4450 | ** Implementation of ".lint" dot command. |
| 4451 | */ |
| 4452 | static int lintDotCommand( |
| 4453 | ShellState *pState, /* Current shell tool state */ |
| 4454 | char **azArg, /* Array of arguments passed to dot command */ |
| 4455 | int nArg /* Number of entries in azArg[] */ |
| 4456 | ){ |
| 4457 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4458 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4459 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4460 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4461 | |
| 4462 | usage: |
| 4463 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4464 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4465 | raw_printf(stderr, " fkey-indexes\n"); |
| 4466 | return SQLITE_ERROR; |
| 4467 | } |
| 4468 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4469 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4470 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4471 | ** If an input line begins with "." then invoke this routine to |
| 4472 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4473 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4474 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4475 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4476 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4477 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4478 | int nArg = 0; |
| 4479 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4480 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4481 | char *azArg[50]; |
| 4482 | |
| 4483 | /* Parse the input line into tokens. |
| 4484 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4485 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 4486 | while( IsSpace(zLine[h]) ){ h++; } |
| 4487 | if( zLine[h]==0 ) break; |
| 4488 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 4489 | int delim = zLine[h++]; |
| 4490 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4491 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4492 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4493 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4494 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4495 | if( zLine[h]==delim ){ |
| 4496 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4497 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4498 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4499 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4500 | azArg[nArg++] = &zLine[h]; |
| 4501 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 4502 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4503 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | /* Process the input line. |
| 4508 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4509 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4510 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4511 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4512 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4513 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4514 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 4515 | if( nArg!=2 ){ |
| 4516 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 4517 | rc = 1; |
| 4518 | goto meta_command_exit; |
| 4519 | } |
| 4520 | open_db(p, 0); |
| 4521 | if( booleanValue(azArg[1]) ){ |
| 4522 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 4523 | }else{ |
| 4524 | sqlite3_set_authorizer(p->db, 0, 0); |
| 4525 | } |
| 4526 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4527 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4528 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4529 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 4530 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 4531 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4532 | const char *zDestFile = 0; |
| 4533 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4534 | sqlite3 *pDest; |
| 4535 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4536 | int j; |
| 4537 | for(j=1; j<nArg; j++){ |
| 4538 | const char *z = azArg[j]; |
| 4539 | if( z[0]=='-' ){ |
| 4540 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4541 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4542 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4543 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4544 | return 1; |
| 4545 | } |
| 4546 | }else if( zDestFile==0 ){ |
| 4547 | zDestFile = azArg[j]; |
| 4548 | }else if( zDb==0 ){ |
| 4549 | zDb = zDestFile; |
| 4550 | zDestFile = azArg[j]; |
| 4551 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4552 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4553 | return 1; |
| 4554 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4555 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4556 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4557 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4558 | return 1; |
| 4559 | } |
| 4560 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4561 | rc = sqlite3_open(zDestFile, &pDest); |
| 4562 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4563 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4564 | sqlite3_close(pDest); |
| 4565 | return 1; |
| 4566 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4567 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4568 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 4569 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4570 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4571 | sqlite3_close(pDest); |
| 4572 | return 1; |
| 4573 | } |
| 4574 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 4575 | sqlite3_backup_finish(pBackup); |
| 4576 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4577 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4578 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4579 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4580 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4581 | } |
| 4582 | sqlite3_close(pDest); |
| 4583 | }else |
| 4584 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4585 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 4586 | if( nArg==2 ){ |
| 4587 | bail_on_error = booleanValue(azArg[1]); |
| 4588 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4589 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4590 | rc = 1; |
| 4591 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 4592 | }else |
| 4593 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4594 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 4595 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4596 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4597 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4598 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4599 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4600 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4601 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4602 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4603 | rc = 1; |
| 4604 | } |
| 4605 | }else |
| 4606 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4607 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 4608 | ** routine named test_breakpoint(). |
| 4609 | */ |
| 4610 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 4611 | test_breakpoint(); |
| 4612 | }else |
| 4613 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4614 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 4615 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4616 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4617 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4618 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4619 | rc = 1; |
| 4620 | } |
| 4621 | }else |
| 4622 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4623 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 4624 | ** Then read the content of the testcase-out.txt file and compare against |
| 4625 | ** azArg[1]. If there are differences, report an error and exit. |
| 4626 | */ |
| 4627 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 4628 | char *zRes = 0; |
| 4629 | output_reset(p); |
| 4630 | if( nArg!=2 ){ |
| 4631 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4632 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4633 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4634 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 4635 | rc = 2; |
| 4636 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4637 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4638 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 4639 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4640 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4641 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4642 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4643 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4644 | } |
| 4645 | sqlite3_free(zRes); |
| 4646 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4647 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4648 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 4649 | if( nArg==2 ){ |
| 4650 | tryToClone(p, azArg[1]); |
| 4651 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4652 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4653 | rc = 1; |
| 4654 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4655 | }else |
| 4656 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4657 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4658 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4659 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4660 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4661 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4662 | data.showHeader = 0; |
| 4663 | data.cMode = data.mode = MODE_List; |
| 4664 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 4665 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4666 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 4667 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4668 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4669 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4670 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4671 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4672 | } |
| 4673 | }else |
| 4674 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4675 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 4676 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 4677 | }else |
| 4678 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4679 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4680 | const char *zLike = 0; |
| 4681 | int i; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4682 | ShellClearFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4683 | for(i=1; i<nArg; i++){ |
| 4684 | if( azArg[i][0]=='-' ){ |
| 4685 | const char *z = azArg[i]+1; |
| 4686 | if( z[0]=='-' ) z++; |
| 4687 | if( strcmp(z,"preserve-rowids")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4688 | ShellSetFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4689 | }else |
| 4690 | { |
| 4691 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4692 | rc = 1; |
| 4693 | goto meta_command_exit; |
| 4694 | } |
| 4695 | }else if( zLike ){ |
| 4696 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 4697 | rc = 1; |
| 4698 | goto meta_command_exit; |
| 4699 | }else{ |
| 4700 | zLike = azArg[i]; |
| 4701 | } |
| 4702 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4703 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 4704 | /* When playing back a "dump", the content might appear in an order |
| 4705 | ** which causes immediate foreign key constraints to be violated. |
| 4706 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4707 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 4708 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4709 | p->writableSchema = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4710 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 4711 | ** as much of the schema as it can even if the sqlite_master table is |
| 4712 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4713 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4714 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4715 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4716 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4717 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4718 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4719 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4720 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4721 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4722 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4723 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4724 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4725 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 4726 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4727 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4728 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4729 | char *zSql; |
| 4730 | zSql = sqlite3_mprintf( |
| 4731 | "SELECT name, type, sql FROM sqlite_master " |
| 4732 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 4733 | " AND sql NOT NULL", zLike); |
| 4734 | run_schema_dump_query(p,zSql); |
| 4735 | sqlite3_free(zSql); |
| 4736 | zSql = sqlite3_mprintf( |
| 4737 | "SELECT sql FROM sqlite_master " |
| 4738 | "WHERE sql NOT NULL" |
| 4739 | " AND type IN ('index','trigger','view')" |
| 4740 | " AND tbl_name LIKE %Q", zLike); |
| 4741 | run_table_dump_query(p, zSql, 0); |
| 4742 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4743 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4744 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4745 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4746 | p->writableSchema = 0; |
| 4747 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4748 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4749 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4750 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4751 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4752 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4753 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 4754 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4755 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4756 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4757 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4758 | rc = 1; |
| 4759 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4760 | }else |
| 4761 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4762 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 4763 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4764 | if( strcmp(azArg[1],"full")==0 ){ |
| 4765 | p->autoEQP = 2; |
| 4766 | }else{ |
| 4767 | p->autoEQP = booleanValue(azArg[1]); |
| 4768 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4769 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4770 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4771 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4772 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 4773 | }else |
| 4774 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 4775 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4776 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4777 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4778 | }else |
| 4779 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4780 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4781 | int val = 1; |
| 4782 | if( nArg>=2 ){ |
| 4783 | if( strcmp(azArg[1],"auto")==0 ){ |
| 4784 | val = 99; |
| 4785 | }else{ |
| 4786 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4787 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4788 | } |
| 4789 | if( val==1 && p->mode!=MODE_Explain ){ |
| 4790 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 4791 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4792 | p->autoExplain = 0; |
| 4793 | }else if( val==0 ){ |
| 4794 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4795 | p->autoExplain = 0; |
| 4796 | }else if( val==99 ){ |
| 4797 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4798 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4799 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4800 | }else |
| 4801 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4802 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4803 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4804 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4805 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4806 | memcpy(&data, p, sizeof(data)); |
| 4807 | data.showHeader = 0; |
| 4808 | data.cMode = data.mode = MODE_Semi; |
| 4809 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 4810 | data.cMode = data.mode = MODE_Pretty; |
| 4811 | nArg = 1; |
| 4812 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4813 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4814 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4815 | rc = 1; |
| 4816 | goto meta_command_exit; |
| 4817 | } |
| 4818 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4819 | rc = sqlite3_exec(p->db, |
| 4820 | "SELECT sql FROM" |
| 4821 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4822 | " FROM sqlite_master UNION ALL" |
| 4823 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4824 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4825 | "ORDER BY rowid", |
| 4826 | callback, &data, &zErrMsg |
| 4827 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4828 | if( rc==SQLITE_OK ){ |
| 4829 | sqlite3_stmt *pStmt; |
| 4830 | rc = sqlite3_prepare_v2(p->db, |
| 4831 | "SELECT rowid FROM sqlite_master" |
| 4832 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 4833 | -1, &pStmt, 0); |
| 4834 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 4835 | sqlite3_finalize(pStmt); |
| 4836 | } |
| 4837 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4838 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4839 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4840 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4841 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 4842 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4843 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4844 | data.zDestTable = "sqlite_stat1"; |
| 4845 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 4846 | shell_callback, &data,&zErrMsg); |
| 4847 | data.zDestTable = "sqlite_stat3"; |
| 4848 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 4849 | shell_callback, &data,&zErrMsg); |
| 4850 | data.zDestTable = "sqlite_stat4"; |
| 4851 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 4852 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4853 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4854 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4855 | }else |
| 4856 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4857 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 4858 | if( nArg==2 ){ |
| 4859 | p->showHeader = booleanValue(azArg[1]); |
| 4860 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4861 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4862 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 4863 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4864 | }else |
| 4865 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4866 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4867 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4868 | }else |
| 4869 | |
| 4870 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 4871 | char *zTable; /* Insert data into this table */ |
| 4872 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4873 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4874 | int nCol; /* Number of columns in the table */ |
| 4875 | int nByte; /* Number of bytes in an SQL string */ |
| 4876 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 4877 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4878 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4879 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4880 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 4881 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 4882 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4883 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4884 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4885 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4886 | goto meta_command_exit; |
| 4887 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 4888 | zFile = azArg[1]; |
| 4889 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4890 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4891 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4892 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4893 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4894 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4895 | raw_printf(stderr, |
| 4896 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4897 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4898 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4899 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4900 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4901 | " for import\n"); |
| 4902 | return 1; |
| 4903 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4904 | nSep = strlen30(p->rowSeparator); |
| 4905 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4906 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4907 | return 1; |
| 4908 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4909 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 4910 | /* When importing CSV (only), if the row separator is set to the |
| 4911 | ** default output row separator, change it to the default input |
| 4912 | ** row separator. This avoids having to maintain different input |
| 4913 | ** and output row separators. */ |
| 4914 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4915 | nSep = strlen30(p->rowSeparator); |
| 4916 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4917 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4918 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4919 | " for import\n"); |
| 4920 | return 1; |
| 4921 | } |
| 4922 | sCtx.zFile = zFile; |
| 4923 | sCtx.nLine = 1; |
| 4924 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4925 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4926 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4927 | return 1; |
| 4928 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4929 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 4930 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4931 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4932 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4933 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4934 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4935 | xCloser = fclose; |
| 4936 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4937 | if( p->mode==MODE_Ascii ){ |
| 4938 | xRead = ascii_read_one_field; |
| 4939 | }else{ |
| 4940 | xRead = csv_read_one_field; |
| 4941 | } |
| 4942 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4943 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4944 | return 1; |
| 4945 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4946 | sCtx.cColSep = p->colSeparator[0]; |
| 4947 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 4948 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4949 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4950 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4951 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4952 | return 1; |
| 4953 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4954 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 4955 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4956 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4957 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4958 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 4959 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4960 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 4961 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4962 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4963 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4964 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4965 | if( cSep=='(' ){ |
| 4966 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4967 | sqlite3_free(sCtx.z); |
| 4968 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4969 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4970 | return 1; |
| 4971 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4972 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 4973 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 4974 | sqlite3_free(zCreate); |
| 4975 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4976 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4977 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4978 | sqlite3_free(sCtx.z); |
| 4979 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4980 | return 1; |
| 4981 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 4982 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4983 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4984 | sqlite3_free(zSql); |
| 4985 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4986 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4987 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4988 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4989 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4990 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4991 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4992 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4993 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4994 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4995 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4996 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4997 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4998 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4999 | return 1; |
| 5000 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5001 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5002 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5003 | for(i=1; i<nCol; i++){ |
| 5004 | zSql[j++] = ','; |
| 5005 | zSql[j++] = '?'; |
| 5006 | } |
| 5007 | zSql[j++] = ')'; |
| 5008 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5009 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5010 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5011 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5012 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5013 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5014 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5015 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5016 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5017 | needCommit = sqlite3_get_autocommit(p->db); |
| 5018 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5019 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5020 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5021 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5022 | char *z = xRead(&sCtx); |
| 5023 | /* |
| 5024 | ** Did we reach end-of-file before finding any columns? |
| 5025 | ** If so, stop instead of NULL filling the remaining columns. |
| 5026 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5027 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5028 | /* |
| 5029 | ** Did we reach end-of-file OR end-of-line before finding any |
| 5030 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 5031 | ** the remaining columns. |
| 5032 | */ |
| 5033 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5034 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5035 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5036 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5037 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5038 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 5039 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 5040 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 5041 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5042 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5043 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5044 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5045 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5046 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5047 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5048 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5049 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5050 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5051 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5052 | if( i>=nCol ){ |
| 5053 | sqlite3_step(pStmt); |
| 5054 | rc = sqlite3_reset(pStmt); |
| 5055 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5056 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 5057 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5058 | } |
| 5059 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5060 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5061 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5062 | xCloser(sCtx.in); |
| 5063 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5064 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5065 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5066 | }else |
| 5067 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 5068 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5069 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 5070 | char *zSql; |
| 5071 | char *zCollist = 0; |
| 5072 | sqlite3_stmt *pStmt; |
| 5073 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5074 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5075 | if( nArg!=3 ){ |
| 5076 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 5077 | rc = 1; |
| 5078 | goto meta_command_exit; |
| 5079 | } |
| 5080 | open_db(p, 0); |
| 5081 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 5082 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 5083 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5084 | sqlite3_free(zSql); |
| 5085 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5086 | tnum = sqlite3_column_int(pStmt, 0); |
| 5087 | } |
| 5088 | sqlite3_finalize(pStmt); |
| 5089 | if( tnum==0 ){ |
| 5090 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 5091 | rc = 1; |
| 5092 | goto meta_command_exit; |
| 5093 | } |
| 5094 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 5095 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5096 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5097 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5098 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5099 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5100 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5101 | i++; |
| 5102 | if( zCol==0 ){ |
| 5103 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 5104 | zCol = "_ROWID_"; |
| 5105 | }else{ |
| 5106 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 5107 | zCol = zLabel; |
| 5108 | } |
| 5109 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5110 | if( zCollist==0 ){ |
| 5111 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 5112 | }else{ |
| 5113 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 5114 | } |
| 5115 | } |
| 5116 | sqlite3_finalize(pStmt); |
| 5117 | zSql = sqlite3_mprintf( |
| 5118 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 5119 | azArg[2], zCollist, zCollist); |
| 5120 | sqlite3_free(zCollist); |
| 5121 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 5122 | if( rc==SQLITE_OK ){ |
| 5123 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5124 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 5125 | if( rc ){ |
| 5126 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 5127 | }else{ |
| 5128 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5129 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5130 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 5131 | ); |
| 5132 | } |
| 5133 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5134 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5135 | rc = 1; |
| 5136 | } |
| 5137 | sqlite3_free(zSql); |
| 5138 | }else |
| 5139 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 5140 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5141 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5142 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 5143 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5144 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 5145 | iotrace = 0; |
| 5146 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5147 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5148 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5149 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5150 | iotrace = stdout; |
| 5151 | }else{ |
| 5152 | iotrace = fopen(azArg[1], "w"); |
| 5153 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5154 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5155 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5156 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5157 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5158 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5159 | } |
| 5160 | } |
| 5161 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5162 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5163 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5164 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 5165 | static const struct { |
| 5166 | const char *zLimitName; /* Name of a limit */ |
| 5167 | int limitCode; /* Integer code for that limit */ |
| 5168 | } aLimit[] = { |
| 5169 | { "length", SQLITE_LIMIT_LENGTH }, |
| 5170 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 5171 | { "column", SQLITE_LIMIT_COLUMN }, |
| 5172 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5173 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5174 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 5175 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5176 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 5177 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5178 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 5179 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 5180 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 5181 | }; |
| 5182 | int i, n2; |
| 5183 | open_db(p, 0); |
| 5184 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5185 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5186 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5187 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 5188 | } |
| 5189 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5190 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5191 | rc = 1; |
| 5192 | goto meta_command_exit; |
| 5193 | }else{ |
| 5194 | int iLimit = -1; |
| 5195 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5196 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5197 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 5198 | if( iLimit<0 ){ |
| 5199 | iLimit = i; |
| 5200 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5201 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5202 | rc = 1; |
| 5203 | goto meta_command_exit; |
| 5204 | } |
| 5205 | } |
| 5206 | } |
| 5207 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5208 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5209 | "enter \".limits\" with no arguments for a list.\n", |
| 5210 | azArg[1]); |
| 5211 | rc = 1; |
| 5212 | goto meta_command_exit; |
| 5213 | } |
| 5214 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 5215 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 5216 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5217 | } |
| 5218 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 5219 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 5220 | } |
| 5221 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5222 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5223 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 5224 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5225 | lintDotCommand(p, azArg, nArg); |
| 5226 | }else |
| 5227 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5228 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5229 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5230 | const char *zFile, *zProc; |
| 5231 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5232 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5233 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5234 | rc = 1; |
| 5235 | goto meta_command_exit; |
| 5236 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5237 | zFile = azArg[1]; |
| 5238 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5239 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5240 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 5241 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5242 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5243 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5244 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5245 | } |
| 5246 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5247 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5248 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5249 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 5250 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5251 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5252 | rc = 1; |
| 5253 | }else{ |
| 5254 | const char *zFile = azArg[1]; |
| 5255 | output_file_close(p->pLog); |
| 5256 | p->pLog = output_file_open(zFile); |
| 5257 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5258 | }else |
| 5259 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5260 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 5261 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 5262 | int n2 = (int)strlen(zMode); |
| 5263 | int c2 = zMode[0]; |
| 5264 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5265 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5266 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5267 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5268 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5269 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5270 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5271 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5272 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 5273 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5274 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5275 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5276 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5277 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5278 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5279 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5280 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 5281 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5282 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5283 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5284 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5285 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5286 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5287 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 5288 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5289 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 5290 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 5291 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5292 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5293 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5294 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5295 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5296 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5297 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 5298 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5299 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5300 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5301 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5302 | }else |
| 5303 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5304 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 5305 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5306 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 5307 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5308 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5309 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 5310 | rc = 1; |
| 5311 | } |
| 5312 | }else |
| 5313 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5314 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5315 | char *zNewFilename; /* Name of the database file to open */ |
| 5316 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5317 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5318 | /* Close the existing database */ |
| 5319 | session_close_all(p); |
| 5320 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5321 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 5322 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5323 | sqlite3_free(p->zFreeOnClose); |
| 5324 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5325 | /* Check for command-line arguments */ |
| 5326 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 5327 | const char *z = azArg[iName]; |
| 5328 | if( optionMatch(z,"new") ){ |
| 5329 | newFlag = 1; |
| 5330 | }else if( z[0]=='-' ){ |
| 5331 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 5332 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5333 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5334 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5335 | } |
| 5336 | /* If a filename is specified, try to open it first */ |
| 5337 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 5338 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5339 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5340 | p->zDbFilename = zNewFilename; |
| 5341 | open_db(p, 1); |
| 5342 | if( p->db==0 ){ |
| 5343 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 5344 | sqlite3_free(zNewFilename); |
| 5345 | }else{ |
| 5346 | p->zFreeOnClose = zNewFilename; |
| 5347 | } |
| 5348 | } |
| 5349 | if( p->db==0 ){ |
| 5350 | /* As a fall-back open a TEMP database */ |
| 5351 | p->zDbFilename = 0; |
| 5352 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5353 | } |
| 5354 | }else |
| 5355 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5356 | if( c=='o' |
| 5357 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 5358 | ){ |
| 5359 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 5360 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5361 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5362 | rc = 1; |
| 5363 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5364 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5365 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 5366 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5367 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5368 | rc = 1; |
| 5369 | goto meta_command_exit; |
| 5370 | } |
| 5371 | p->outCount = 2; |
| 5372 | }else{ |
| 5373 | p->outCount = 0; |
| 5374 | } |
| 5375 | output_reset(p); |
| 5376 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5377 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5378 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5379 | rc = 1; |
| 5380 | p->out = stdout; |
| 5381 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5382 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5383 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5384 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5385 | p->out = stdout; |
| 5386 | rc = 1; |
| 5387 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5388 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5389 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5390 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5391 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5392 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5393 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5394 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5395 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 5396 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5397 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5398 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5399 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5400 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5401 | } |
| 5402 | } |
| 5403 | }else |
| 5404 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5405 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 5406 | int i; |
| 5407 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5408 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5409 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5410 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5411 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5412 | }else |
| 5413 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5414 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5415 | if( nArg >= 2) { |
| 5416 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 5417 | } |
| 5418 | if( nArg >= 3) { |
| 5419 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 5420 | } |
| 5421 | }else |
| 5422 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5423 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5424 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5425 | }else |
| 5426 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5427 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 5428 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5429 | if( nArg!=2 ){ |
| 5430 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5431 | rc = 1; |
| 5432 | goto meta_command_exit; |
| 5433 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5434 | alt = fopen(azArg[1], "rb"); |
| 5435 | if( alt==0 ){ |
| 5436 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 5437 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5438 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5439 | rc = process_input(p, alt); |
| 5440 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5441 | } |
| 5442 | }else |
| 5443 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5444 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5445 | const char *zSrcFile; |
| 5446 | const char *zDb; |
| 5447 | sqlite3 *pSrc; |
| 5448 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5449 | int nTimeout = 0; |
| 5450 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5451 | if( nArg==2 ){ |
| 5452 | zSrcFile = azArg[1]; |
| 5453 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5454 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5455 | zSrcFile = azArg[2]; |
| 5456 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5457 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5458 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5459 | rc = 1; |
| 5460 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5461 | } |
| 5462 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 5463 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5464 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5465 | sqlite3_close(pSrc); |
| 5466 | return 1; |
| 5467 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5468 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5469 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 5470 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5471 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5472 | sqlite3_close(pSrc); |
| 5473 | return 1; |
| 5474 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5475 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 5476 | || rc==SQLITE_BUSY ){ |
| 5477 | if( rc==SQLITE_BUSY ){ |
| 5478 | if( nTimeout++ >= 3 ) break; |
| 5479 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5480 | } |
| 5481 | } |
| 5482 | sqlite3_backup_finish(pBackup); |
| 5483 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5484 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5485 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5486 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5487 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5488 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5489 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5490 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5491 | } |
| 5492 | sqlite3_close(pSrc); |
| 5493 | }else |
| 5494 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5495 | |
| 5496 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 5497 | if( nArg==2 ){ |
| 5498 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5499 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5500 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5501 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5502 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5503 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5504 | rc = 1; |
| 5505 | } |
| 5506 | }else |
| 5507 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5508 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5509 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5510 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5511 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5512 | memcpy(&data, p, sizeof(data)); |
| 5513 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5514 | data.cMode = data.mode = MODE_Semi; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5515 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 5516 | data.cMode = data.mode = MODE_Pretty; |
| 5517 | nArg--; |
| 5518 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 5519 | } |
| 5520 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5521 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5522 | 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] | 5523 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5524 | char *new_argv[2], *new_colv[2]; |
| 5525 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 5526 | " type text,\n" |
| 5527 | " name text,\n" |
| 5528 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 5529 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5530 | " sql text\n" |
| 5531 | ")"; |
| 5532 | new_argv[1] = 0; |
| 5533 | new_colv[0] = "sql"; |
| 5534 | new_colv[1] = 0; |
| 5535 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5536 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5537 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5538 | char *new_argv[2], *new_colv[2]; |
| 5539 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 5540 | " type text,\n" |
| 5541 | " name text,\n" |
| 5542 | " tbl_name text,\n" |
| 5543 | " rootpage integer,\n" |
| 5544 | " sql text\n" |
| 5545 | ")"; |
| 5546 | new_argv[1] = 0; |
| 5547 | new_colv[0] = "sql"; |
| 5548 | new_colv[1] = 0; |
| 5549 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5550 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5551 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5552 | char *zSql; |
| 5553 | zSql = sqlite3_mprintf( |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5554 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5555 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5556 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5557 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5558 | "WHERE lower(tbl_name) LIKE %Q" |
drh | 6ac7a58 | 2011-11-04 00:35:56 +0000 | [diff] [blame] | 5559 | " AND type!='meta' AND sql NOTNULL " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5560 | "ORDER BY rowid", azArg[1]); |
| 5561 | rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg); |
| 5562 | sqlite3_free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5563 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5564 | }else if( nArg==1 ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5565 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5566 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5567 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5568 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5569 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5570 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 5571 | "ORDER BY rowid", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5572 | callback, &data, &zErrMsg |
| 5573 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5574 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5575 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5576 | rc = 1; |
| 5577 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5578 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5579 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5580 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5581 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5582 | rc = 1; |
| 5583 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5584 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5585 | rc = 1; |
| 5586 | }else{ |
| 5587 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5588 | } |
| 5589 | }else |
| 5590 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5591 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 5592 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 5593 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5594 | }else |
| 5595 | #endif |
| 5596 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5597 | #if defined(SQLITE_ENABLE_SESSION) |
| 5598 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 5599 | OpenSession *pSession = &p->aSession[0]; |
| 5600 | char **azCmd = &azArg[1]; |
| 5601 | int iSes = 0; |
| 5602 | int nCmd = nArg - 1; |
| 5603 | int i; |
| 5604 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5605 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5606 | if( nArg>=3 ){ |
| 5607 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 5608 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 5609 | } |
| 5610 | if( iSes<p->nSession ){ |
| 5611 | pSession = &p->aSession[iSes]; |
| 5612 | azCmd++; |
| 5613 | nCmd--; |
| 5614 | }else{ |
| 5615 | pSession = &p->aSession[0]; |
| 5616 | iSes = 0; |
| 5617 | } |
| 5618 | } |
| 5619 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5620 | /* .session attach TABLE |
| 5621 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 5622 | ** table so that it is never filtered. |
| 5623 | */ |
| 5624 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 5625 | if( nCmd!=2 ) goto session_syntax_error; |
| 5626 | if( pSession->p==0 ){ |
| 5627 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5628 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5629 | }else{ |
| 5630 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 5631 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5632 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5633 | rc = 0; |
| 5634 | } |
| 5635 | } |
| 5636 | }else |
| 5637 | |
| 5638 | /* .session changeset FILE |
| 5639 | ** .session patchset FILE |
| 5640 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 5641 | */ |
| 5642 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 5643 | FILE *out = 0; |
| 5644 | if( nCmd!=2 ) goto session_syntax_error; |
| 5645 | if( pSession->p==0 ) goto session_not_open; |
| 5646 | out = fopen(azCmd[1], "wb"); |
| 5647 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5648 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5649 | }else{ |
| 5650 | int szChng; |
| 5651 | void *pChng; |
| 5652 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5653 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5654 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5655 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 5656 | } |
| 5657 | if( rc ){ |
| 5658 | printf("Error: error code %d\n", rc); |
| 5659 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5660 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5661 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5662 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5663 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5664 | szChng); |
| 5665 | } |
| 5666 | sqlite3_free(pChng); |
| 5667 | fclose(out); |
| 5668 | } |
| 5669 | }else |
| 5670 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5671 | /* .session close |
| 5672 | ** Close the identified session |
| 5673 | */ |
| 5674 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5675 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5676 | if( p->nSession ){ |
| 5677 | session_close(pSession); |
| 5678 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 5679 | } |
| 5680 | }else |
| 5681 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5682 | /* .session enable ?BOOLEAN? |
| 5683 | ** Query or set the enable flag |
| 5684 | */ |
| 5685 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 5686 | int ii; |
| 5687 | if( nCmd>2 ) goto session_syntax_error; |
| 5688 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5689 | if( p->nSession ){ |
| 5690 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5691 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 5692 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5693 | } |
| 5694 | }else |
| 5695 | |
| 5696 | /* .session filter GLOB .... |
| 5697 | ** Set a list of GLOB patterns of table names to be excluded. |
| 5698 | */ |
| 5699 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 5700 | int ii, nByte; |
| 5701 | if( nCmd<2 ) goto session_syntax_error; |
| 5702 | if( p->nSession ){ |
| 5703 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 5704 | sqlite3_free(pSession->azFilter[ii]); |
| 5705 | } |
| 5706 | sqlite3_free(pSession->azFilter); |
| 5707 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 5708 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 5709 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5710 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5711 | exit(1); |
| 5712 | } |
| 5713 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5714 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5715 | } |
| 5716 | pSession->nFilter = ii-1; |
| 5717 | } |
| 5718 | }else |
| 5719 | |
| 5720 | /* .session indirect ?BOOLEAN? |
| 5721 | ** Query or set the indirect flag |
| 5722 | */ |
| 5723 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 5724 | int ii; |
| 5725 | if( nCmd>2 ) goto session_syntax_error; |
| 5726 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5727 | if( p->nSession ){ |
| 5728 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5729 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 5730 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5731 | } |
| 5732 | }else |
| 5733 | |
| 5734 | /* .session isempty |
| 5735 | ** Determine if the session is empty |
| 5736 | */ |
| 5737 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 5738 | int ii; |
| 5739 | if( nCmd!=1 ) goto session_syntax_error; |
| 5740 | if( p->nSession ){ |
| 5741 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5742 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 5743 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5744 | } |
| 5745 | }else |
| 5746 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5747 | /* .session list |
| 5748 | ** List all currently open sessions |
| 5749 | */ |
| 5750 | if( strcmp(azCmd[0],"list")==0 ){ |
| 5751 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5752 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5753 | } |
| 5754 | }else |
| 5755 | |
| 5756 | /* .session open DB NAME |
| 5757 | ** Open a new session called NAME on the attached database DB. |
| 5758 | ** DB is normally "main". |
| 5759 | */ |
| 5760 | if( strcmp(azCmd[0],"open")==0 ){ |
| 5761 | char *zName; |
| 5762 | if( nCmd!=3 ) goto session_syntax_error; |
| 5763 | zName = azCmd[2]; |
| 5764 | if( zName[0]==0 ) goto session_syntax_error; |
| 5765 | for(i=0; i<p->nSession; i++){ |
| 5766 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5767 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5768 | goto meta_command_exit; |
| 5769 | } |
| 5770 | } |
| 5771 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5772 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5773 | goto meta_command_exit; |
| 5774 | } |
| 5775 | pSession = &p->aSession[p->nSession]; |
| 5776 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 5777 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5778 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5779 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5780 | goto meta_command_exit; |
| 5781 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5782 | pSession->nFilter = 0; |
| 5783 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5784 | p->nSession++; |
| 5785 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 5786 | }else |
| 5787 | /* If no command name matches, show a syntax error */ |
| 5788 | session_syntax_error: |
| 5789 | session_help(p); |
| 5790 | }else |
| 5791 | #endif |
| 5792 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5793 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5794 | /* Undocumented commands for internal testing. Subject to change |
| 5795 | ** without notice. */ |
| 5796 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 5797 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 5798 | int i, v; |
| 5799 | for(i=1; i<nArg; i++){ |
| 5800 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5801 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5802 | } |
| 5803 | } |
| 5804 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 5805 | int i; sqlite3_int64 v; |
| 5806 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5807 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5808 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5809 | 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] | 5810 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5811 | } |
| 5812 | } |
| 5813 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5814 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5815 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 5816 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 5817 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 5818 | int bVerbose = 0; /* Verbose output */ |
| 5819 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 5820 | char **azTest = 0; /* Content of the SELFTEST table */ |
| 5821 | int nRow = 0; /* Number of rows in the SELFTEST table */ |
| 5822 | int nCol = 4; /* Number of columns in the SELFTEST table */ |
| 5823 | int i; /* Loop counter */ |
| 5824 | int nTest = 0; /* Number of tests runs */ |
| 5825 | int nErr = 0; /* Number of errors seen */ |
| 5826 | ShellText str; /* Answer for a query */ |
| 5827 | static char *azDefaultTest[] = { |
| 5828 | 0, 0, 0, 0, |
| 5829 | "0", "memo", "Missing SELFTEST table - default checks only", "", |
| 5830 | "1", "run", "PRAGMA integrity_check", "ok" |
| 5831 | }; |
| 5832 | static const int nDefaultRow = 2; |
| 5833 | |
| 5834 | open_db(p,0); |
| 5835 | for(i=1; i<nArg; i++){ |
| 5836 | const char *z = azArg[i]; |
| 5837 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 5838 | if( strcmp(z,"-init")==0 ){ |
| 5839 | bIsInit = 1; |
| 5840 | }else |
| 5841 | if( strcmp(z,"-v")==0 ){ |
| 5842 | bVerbose++; |
| 5843 | }else |
| 5844 | { |
| 5845 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 5846 | azArg[i], azArg[0]); |
| 5847 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 5848 | rc = 1; |
| 5849 | goto meta_command_exit; |
| 5850 | } |
| 5851 | } |
| 5852 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 5853 | != SQLITE_OK ){ |
| 5854 | bSelftestExists = 0; |
| 5855 | }else{ |
| 5856 | bSelftestExists = 1; |
| 5857 | } |
| 5858 | if( bIsInit ){ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 5859 | createSelftestTable(p); |
| 5860 | bSelftestExists = 1; |
| 5861 | } |
| 5862 | if( bSelftestExists ){ |
| 5863 | rc = sqlite3_get_table(p->db, |
| 5864 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 5865 | &azTest, &nRow, &nCol, 0); |
| 5866 | if( rc ){ |
| 5867 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 5868 | rc = 1; |
| 5869 | sqlite3_free_table(azTest); |
| 5870 | goto meta_command_exit; |
| 5871 | }else if( nRow==0 ){ |
| 5872 | sqlite3_free_table(azTest); |
| 5873 | azTest = azDefaultTest; |
| 5874 | nRow = nDefaultRow; |
| 5875 | } |
| 5876 | }else{ |
| 5877 | azTest = azDefaultTest; |
| 5878 | nRow = nDefaultRow; |
| 5879 | } |
| 5880 | initText(&str); |
| 5881 | appendText(&str, "x", 0); |
| 5882 | for(i=1; i<=nRow; i++){ |
| 5883 | int tno = atoi(azTest[i*nCol]); |
| 5884 | const char *zOp = azTest[i*nCol+1]; |
| 5885 | const char *zSql = azTest[i*nCol+2]; |
| 5886 | const char *zAns = azTest[i*nCol+3]; |
| 5887 | |
| 5888 | if( bVerbose>0 ){ |
| 5889 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 5890 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 5891 | sqlite3_free(zQuote); |
| 5892 | } |
| 5893 | if( strcmp(zOp,"memo")==0 ){ |
| 5894 | utf8_printf(p->out, "%s\n", zSql); |
| 5895 | }else |
| 5896 | if( strcmp(zOp,"run")==0 ){ |
| 5897 | char *zErrMsg = 0; |
| 5898 | str.n = 0; |
| 5899 | str.z[0] = 0; |
| 5900 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 5901 | nTest++; |
| 5902 | if( bVerbose ){ |
| 5903 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 5904 | } |
| 5905 | if( rc || zErrMsg ){ |
| 5906 | nErr++; |
| 5907 | rc = 1; |
| 5908 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 5909 | sqlite3_free(zErrMsg); |
| 5910 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 5911 | nErr++; |
| 5912 | rc = 1; |
| 5913 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 5914 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 5915 | } |
| 5916 | }else |
| 5917 | { |
| 5918 | utf8_printf(stderr, |
| 5919 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 5920 | rc = 1; |
| 5921 | break; |
| 5922 | } |
| 5923 | } |
| 5924 | freeText(&str); |
| 5925 | if( azTest!=azDefaultTest ) sqlite3_free_table(azTest); |
| 5926 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 5927 | }else |
| 5928 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5929 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5930 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5931 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5932 | rc = 1; |
| 5933 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5934 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5935 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 5936 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5937 | } |
| 5938 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5939 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 5940 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5941 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5942 | }else |
| 5943 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5944 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 5945 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 5946 | int i; /* Loop counter */ |
| 5947 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 5948 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5949 | int iSize = 224; /* Hash algorithm to use */ |
| 5950 | int bDebug = 0; /* Only show the query that would have run */ |
| 5951 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 5952 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 5953 | char *zSep; /* Separator */ |
| 5954 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5955 | ShellText sQuery; /* Set of queries used to read all content */ |
drh | f80d4ff | 2017-03-08 18:06:20 +0000 | [diff] [blame] | 5956 | open_db(p, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5957 | for(i=1; i<nArg; i++){ |
| 5958 | const char *z = azArg[i]; |
| 5959 | if( z[0]=='-' ){ |
| 5960 | z++; |
| 5961 | if( z[0]=='-' ) z++; |
| 5962 | if( strcmp(z,"schema")==0 ){ |
| 5963 | bSchema = 1; |
| 5964 | }else |
| 5965 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 5966 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 5967 | ){ |
| 5968 | iSize = atoi(&z[5]); |
| 5969 | }else |
| 5970 | if( strcmp(z,"debug")==0 ){ |
| 5971 | bDebug = 1; |
| 5972 | }else |
| 5973 | { |
| 5974 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 5975 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5976 | raw_printf(stderr, "Should be one of: --schema" |
| 5977 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 5978 | rc = 1; |
| 5979 | goto meta_command_exit; |
| 5980 | } |
| 5981 | }else if( zLike ){ |
| 5982 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 5983 | rc = 1; |
| 5984 | goto meta_command_exit; |
| 5985 | }else{ |
| 5986 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 5987 | bSeparate = 1; |
| 5988 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5989 | } |
| 5990 | } |
| 5991 | if( bSchema ){ |
| 5992 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 5993 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 5994 | " UNION ALL SELECT 'sqlite_master'" |
| 5995 | " ORDER BY 1 collate nocase"; |
| 5996 | }else{ |
| 5997 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 5998 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 5999 | " AND name NOT LIKE 'sqlite_%'" |
| 6000 | " ORDER BY 1 collate nocase"; |
| 6001 | } |
| 6002 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6003 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6004 | initText(&sSql); |
| 6005 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 6006 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6007 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 6008 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 6009 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 6010 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 6011 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 6012 | appendText(&sQuery,zTab,'"'); |
| 6013 | appendText(&sQuery," NOT INDEXED;", 0); |
| 6014 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 6015 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 6016 | " ORDER BY name;", 0); |
| 6017 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 6018 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 6019 | " ORDER BY name;", 0); |
| 6020 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 6021 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 6022 | " ORDER BY tbl,idx;", 0); |
| 6023 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 6024 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 6025 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 6026 | appendText(&sQuery, zTab, 0); |
| 6027 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 6028 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6029 | appendText(&sSql, zSep, 0); |
| 6030 | appendText(&sSql, sQuery.z, '\''); |
| 6031 | sQuery.n = 0; |
| 6032 | appendText(&sSql, ",", 0); |
| 6033 | appendText(&sSql, zTab, '\''); |
| 6034 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6035 | } |
| 6036 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6037 | if( bSeparate ){ |
| 6038 | zSql = sqlite3_mprintf( |
| 6039 | "%s))" |
| 6040 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 6041 | " FROM [sha3sum$query]", |
| 6042 | sSql.z, iSize); |
| 6043 | }else{ |
| 6044 | zSql = sqlite3_mprintf( |
| 6045 | "%s))" |
| 6046 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 6047 | " FROM [sha3sum$query]", |
| 6048 | sSql.z, iSize); |
| 6049 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6050 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6051 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6052 | if( bDebug ){ |
| 6053 | utf8_printf(p->out, "%s\n", zSql); |
| 6054 | }else{ |
| 6055 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 6056 | } |
| 6057 | sqlite3_free(zSql); |
| 6058 | }else |
| 6059 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6060 | if( c=='s' |
| 6061 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6062 | ){ |
| 6063 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6064 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6065 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6066 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6067 | rc = 1; |
| 6068 | goto meta_command_exit; |
| 6069 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6070 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6071 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6072 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 6073 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6074 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6075 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6076 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6077 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6078 | }else |
| 6079 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6080 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6081 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6082 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6083 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6084 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6085 | rc = 1; |
| 6086 | goto meta_command_exit; |
| 6087 | } |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6088 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 6089 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6090 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6091 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 6092 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6093 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6094 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 6095 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6096 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6097 | raw_printf(p->out, "\n"); |
| 6098 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6099 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6100 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6101 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6102 | raw_printf(p->out, "\n"); |
| 6103 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6104 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6105 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6106 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6107 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6108 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6109 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6110 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6111 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6112 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 6113 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6114 | }else |
| 6115 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6116 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 6117 | if( nArg==2 ){ |
| 6118 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6119 | }else if( nArg==1 ){ |
| 6120 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6121 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6122 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6123 | rc = 1; |
| 6124 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6125 | }else |
| 6126 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6127 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 6128 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 6129 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 6130 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6131 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6132 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6133 | int nRow, nAlloc; |
| 6134 | char *zSql = 0; |
| 6135 | int ii; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6136 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6137 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6138 | if( rc ) return shellDatabaseError(p->db); |
| 6139 | |
| 6140 | /* Create an SQL statement to query for the list of tables in the |
| 6141 | ** main and all attached databases where the table name matches the |
| 6142 | ** LIKE pattern bound to variable "?1". */ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6143 | if( c=='t' ){ |
| 6144 | zSql = sqlite3_mprintf( |
| 6145 | "SELECT name FROM sqlite_master" |
| 6146 | " WHERE type IN ('table','view')" |
| 6147 | " AND name NOT LIKE 'sqlite_%%'" |
| 6148 | " AND name LIKE ?1"); |
| 6149 | }else if( nArg>2 ){ |
| 6150 | /* It is an historical accident that the .indexes command shows an error |
| 6151 | ** when called with the wrong number of arguments whereas the .tables |
| 6152 | ** command does not. */ |
| 6153 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 6154 | rc = 1; |
| 6155 | goto meta_command_exit; |
| 6156 | }else{ |
| 6157 | zSql = sqlite3_mprintf( |
| 6158 | "SELECT name FROM sqlite_master" |
| 6159 | " WHERE type='index'" |
| 6160 | " AND tbl_name LIKE ?1"); |
| 6161 | } |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6162 | for(ii=0; zSql && sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6163 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6164 | if( zDbName==0 || ii==0 ) continue; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6165 | if( c=='t' ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6166 | zSql = sqlite3_mprintf( |
| 6167 | "%z UNION ALL " |
| 6168 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6169 | " WHERE type IN ('table','view')" |
| 6170 | " AND name NOT LIKE 'sqlite_%%'" |
| 6171 | " AND name LIKE ?1", zSql, zDbName, zDbName); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6172 | }else{ |
| 6173 | zSql = sqlite3_mprintf( |
| 6174 | "%z UNION ALL " |
| 6175 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6176 | " WHERE type='index'" |
| 6177 | " AND tbl_name LIKE ?1", zSql, zDbName, zDbName); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6178 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 6179 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6180 | rc = sqlite3_finalize(pStmt); |
| 6181 | if( zSql && rc==SQLITE_OK ){ |
| 6182 | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 6183 | if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6184 | } |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6185 | sqlite3_free(zSql); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6186 | if( !zSql ) return shellNomemError(); |
| 6187 | if( rc ) return shellDatabaseError(p->db); |
| 6188 | |
| 6189 | /* Run the SQL statement prepared by the above block. Store the results |
| 6190 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6191 | nRow = nAlloc = 0; |
| 6192 | azResult = 0; |
| 6193 | if( nArg>1 ){ |
| 6194 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6195 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6196 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 6197 | } |
| 6198 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6199 | if( nRow>=nAlloc ){ |
| 6200 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6201 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6202 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6203 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6204 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6205 | break; |
| 6206 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6207 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6208 | azResult = azNew; |
| 6209 | } |
| 6210 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6211 | if( 0==azResult[nRow] ){ |
| 6212 | rc = shellNomemError(); |
| 6213 | break; |
| 6214 | } |
| 6215 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6216 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6217 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 6218 | rc = shellDatabaseError(p->db); |
| 6219 | } |
| 6220 | |
| 6221 | /* Pretty-print the contents of array azResult[] to the output */ |
| 6222 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6223 | int len, maxlen = 0; |
| 6224 | int i, j; |
| 6225 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6226 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6227 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6228 | if( len>maxlen ) maxlen = len; |
| 6229 | } |
| 6230 | nPrintCol = 80/(maxlen+2); |
| 6231 | if( nPrintCol<1 ) nPrintCol = 1; |
| 6232 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 6233 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6234 | for(j=i; j<nRow; j+=nPrintRow){ |
| 6235 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6236 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 6237 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6238 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6239 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6240 | } |
| 6241 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6242 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6243 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 6244 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6245 | }else |
| 6246 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6247 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 6248 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 6249 | output_reset(p); |
| 6250 | p->out = output_file_open("testcase-out.txt"); |
| 6251 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6252 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6253 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6254 | if( nArg>=2 ){ |
| 6255 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 6256 | }else{ |
| 6257 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 6258 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6259 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6260 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6261 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6262 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6263 | static const struct { |
| 6264 | const char *zCtrlName; /* Name of a test-control option */ |
| 6265 | int ctrlCode; /* Integer code for that option */ |
| 6266 | } aCtrl[] = { |
| 6267 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 6268 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 6269 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 6270 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 6271 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 6272 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 6273 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 6274 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 6275 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 6276 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 6277 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 6278 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6279 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6280 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 6281 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6282 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6283 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6284 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6285 | int rc2 = 0; |
| 6286 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6287 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6288 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6289 | /* convert testctrl text option to value. allow any unique prefix |
| 6290 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6291 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6292 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6293 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6294 | if( testctrl<0 ){ |
| 6295 | testctrl = aCtrl[i].ctrlCode; |
| 6296 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6297 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6298 | testctrl = -1; |
| 6299 | break; |
| 6300 | } |
| 6301 | } |
| 6302 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6303 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6304 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6305 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6306 | }else{ |
| 6307 | switch(testctrl){ |
| 6308 | |
| 6309 | /* sqlite3_test_control(int, db, int) */ |
| 6310 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6311 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6312 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6313 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6314 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6315 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6316 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6317 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6318 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6319 | } |
| 6320 | break; |
| 6321 | |
| 6322 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6323 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 6324 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6325 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6326 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6327 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6328 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6329 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6330 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6331 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 6332 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6333 | } |
| 6334 | break; |
| 6335 | |
| 6336 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6337 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6338 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 6339 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6340 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6341 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6342 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6343 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6344 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6345 | } |
| 6346 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6347 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6348 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6349 | case SQLITE_TESTCTRL_ASSERT: |
| 6350 | case SQLITE_TESTCTRL_ALWAYS: |
| 6351 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6352 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6353 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6354 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6355 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6356 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6357 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6358 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6359 | } |
| 6360 | break; |
| 6361 | |
| 6362 | /* sqlite3_test_control(int, char *) */ |
| 6363 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6364 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6365 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6366 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6367 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6368 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6369 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6370 | utf8_printf(stderr, |
| 6371 | "Error: testctrl %s takes a single char * option\n", |
| 6372 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6373 | } |
| 6374 | break; |
| 6375 | #endif |
| 6376 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6377 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6378 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6379 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6380 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6381 | integerValue(azArg[3]), |
| 6382 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6383 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6384 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6385 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6386 | } |
| 6387 | break; |
| 6388 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6389 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 6390 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 6391 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 6392 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6393 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6394 | utf8_printf(stderr, |
| 6395 | "Error: CLI support for testctrl %s not implemented\n", |
| 6396 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6397 | break; |
| 6398 | } |
| 6399 | } |
| 6400 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 6401 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6402 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6403 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6404 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6405 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6406 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6407 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6408 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 6409 | if( nArg==2 ){ |
| 6410 | enableTimer = booleanValue(azArg[1]); |
| 6411 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6412 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6413 | enableTimer = 0; |
| 6414 | } |
| 6415 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6416 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6417 | rc = 1; |
| 6418 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6419 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6420 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6421 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6422 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6423 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6424 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6425 | rc = 1; |
| 6426 | goto meta_command_exit; |
| 6427 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 6428 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6429 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 6430 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6431 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6432 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6433 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6434 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6435 | } |
| 6436 | #endif |
| 6437 | }else |
| 6438 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6439 | #if SQLITE_USER_AUTHENTICATION |
| 6440 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 6441 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6442 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6443 | rc = 1; |
| 6444 | goto meta_command_exit; |
| 6445 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 6446 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6447 | if( strcmp(azArg[1],"login")==0 ){ |
| 6448 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6449 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6450 | rc = 1; |
| 6451 | goto meta_command_exit; |
| 6452 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6453 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 6454 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6455 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6456 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6457 | rc = 1; |
| 6458 | } |
| 6459 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 6460 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6461 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6462 | rc = 1; |
| 6463 | goto meta_command_exit; |
| 6464 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6465 | rc = sqlite3_user_add(p->db, azArg[2], |
| 6466 | azArg[3], (int)strlen(azArg[3]), |
| 6467 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6468 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6469 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6470 | rc = 1; |
| 6471 | } |
| 6472 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 6473 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6474 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6475 | rc = 1; |
| 6476 | goto meta_command_exit; |
| 6477 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6478 | rc = sqlite3_user_change(p->db, azArg[2], |
| 6479 | azArg[3], (int)strlen(azArg[3]), |
| 6480 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6481 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6482 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6483 | rc = 1; |
| 6484 | } |
| 6485 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 6486 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6487 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6488 | rc = 1; |
| 6489 | goto meta_command_exit; |
| 6490 | } |
| 6491 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 6492 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6493 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6494 | rc = 1; |
| 6495 | } |
| 6496 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6497 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6498 | rc = 1; |
| 6499 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6500 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6501 | }else |
| 6502 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 6503 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6504 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6505 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6506 | sqlite3_libversion(), sqlite3_sourceid()); |
| 6507 | }else |
| 6508 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6509 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 6510 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 6511 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6512 | if( p->db ){ |
| 6513 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 6514 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6515 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 6516 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6517 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6518 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6519 | } |
| 6520 | } |
| 6521 | }else |
| 6522 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 6523 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 6524 | sqlite3_vfs *pVfs; |
| 6525 | sqlite3_vfs *pCurrent = 0; |
| 6526 | if( p->db ){ |
| 6527 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 6528 | } |
| 6529 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 6530 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 6531 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 6532 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6533 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6534 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 6535 | if( pVfs->pNext ){ |
| 6536 | raw_printf(p->out, "-----------------------------------\n"); |
| 6537 | } |
| 6538 | } |
| 6539 | }else |
| 6540 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6541 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 6542 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 6543 | char *zVfsName = 0; |
| 6544 | if( p->db ){ |
| 6545 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 6546 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6547 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6548 | sqlite3_free(zVfsName); |
| 6549 | } |
| 6550 | } |
| 6551 | }else |
| 6552 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6553 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 6554 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6555 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6556 | }else |
| 6557 | #endif |
| 6558 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6559 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6560 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6561 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6562 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6563 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6564 | } |
| 6565 | }else |
| 6566 | |
| 6567 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6568 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6569 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6570 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6571 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6572 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6573 | meta_command_exit: |
| 6574 | if( p->outCount ){ |
| 6575 | p->outCount--; |
| 6576 | if( p->outCount==0 ) output_reset(p); |
| 6577 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6578 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6579 | } |
| 6580 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6581 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6582 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 6583 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6584 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6585 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6586 | int i; |
| 6587 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 6588 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
| 6591 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6592 | ** Test to see if a line consists entirely of whitespace. |
| 6593 | */ |
| 6594 | static int _all_whitespace(const char *z){ |
| 6595 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6596 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6597 | if( *z=='/' && z[1]=='*' ){ |
| 6598 | z += 2; |
| 6599 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 6600 | if( *z==0 ) return 0; |
| 6601 | z++; |
| 6602 | continue; |
| 6603 | } |
| 6604 | if( *z=='-' && z[1]=='-' ){ |
| 6605 | z += 2; |
| 6606 | while( *z && *z!='\n' ){ z++; } |
| 6607 | if( *z==0 ) return 1; |
| 6608 | continue; |
| 6609 | } |
| 6610 | return 0; |
| 6611 | } |
| 6612 | return 1; |
| 6613 | } |
| 6614 | |
| 6615 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6616 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 6617 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 6618 | ** as is the Oracle "/". |
| 6619 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6620 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6621 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6622 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 6623 | return 1; /* Oracle */ |
| 6624 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6625 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6626 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6627 | return 1; /* SQL Server */ |
| 6628 | } |
| 6629 | return 0; |
| 6630 | } |
| 6631 | |
| 6632 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6633 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 6634 | ** ends in the middle of a string literal or C-style comment. |
| 6635 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6636 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6637 | int rc; |
| 6638 | if( zSql==0 ) return 1; |
| 6639 | zSql[nSql] = ';'; |
| 6640 | zSql[nSql+1] = 0; |
| 6641 | rc = sqlite3_complete(zSql); |
| 6642 | zSql[nSql] = 0; |
| 6643 | return rc; |
| 6644 | } |
| 6645 | |
| 6646 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6647 | ** Run a single line of SQL |
| 6648 | */ |
| 6649 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 6650 | int rc; |
| 6651 | char *zErrMsg = 0; |
| 6652 | |
| 6653 | open_db(p, 0); |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6654 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6655 | BEGIN_TIMER; |
| 6656 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 6657 | END_TIMER; |
| 6658 | if( rc || zErrMsg ){ |
| 6659 | char zPrefix[100]; |
| 6660 | if( in!=0 || !stdin_is_interactive ){ |
| 6661 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 6662 | "Error: near line %d:", startline); |
| 6663 | }else{ |
| 6664 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 6665 | } |
| 6666 | if( zErrMsg!=0 ){ |
| 6667 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 6668 | sqlite3_free(zErrMsg); |
| 6669 | zErrMsg = 0; |
| 6670 | }else{ |
| 6671 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 6672 | } |
| 6673 | return 1; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6674 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6675 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 6676 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 6677 | } |
| 6678 | return 0; |
| 6679 | } |
| 6680 | |
| 6681 | |
| 6682 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6683 | ** Read input from *in and process it. If *in==0 then input |
| 6684 | ** is interactive - the user is typing it it. Otherwise, input |
| 6685 | ** is coming from a file or device. A prompt is issued and history |
| 6686 | ** is saved only if input is interactive. An interrupt signal will |
| 6687 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6688 | ** |
| 6689 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6690 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6691 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6692 | char *zLine = 0; /* A single input line */ |
| 6693 | char *zSql = 0; /* Accumulated SQL text */ |
| 6694 | int nLine; /* Length of current line */ |
| 6695 | int nSql = 0; /* Bytes of zSql[] used */ |
| 6696 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 6697 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6698 | int rc; /* Error code */ |
| 6699 | int errCnt = 0; /* Number of errors seen */ |
| 6700 | int lineno = 0; /* Current line number */ |
| 6701 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6702 | |
| 6703 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 6704 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6705 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6706 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6707 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 6708 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6709 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6710 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6711 | if( seenInterrupt ){ |
| 6712 | if( in!=0 ) break; |
| 6713 | seenInterrupt = 0; |
| 6714 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6715 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6716 | if( nSql==0 && _all_whitespace(zLine) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6717 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6718 | continue; |
| 6719 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 6720 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6721 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6722 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6723 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6724 | break; |
| 6725 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6726 | errCnt++; |
| 6727 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6728 | continue; |
| 6729 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6730 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6731 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6732 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6733 | nLine = strlen30(zLine); |
| 6734 | if( nSql+nLine+2>=nAlloc ){ |
| 6735 | nAlloc = nSql+nLine+100; |
| 6736 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6737 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6738 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6739 | exit(1); |
| 6740 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6741 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6742 | nSqlPrior = nSql; |
| 6743 | if( nSql==0 ){ |
| 6744 | int i; |
| 6745 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 6746 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6747 | memcpy(zSql, zLine+i, nLine+1-i); |
| 6748 | startline = lineno; |
| 6749 | nSql = nLine-i; |
| 6750 | }else{ |
| 6751 | zSql[nSql++] = '\n'; |
| 6752 | memcpy(zSql+nSql, zLine, nLine+1); |
| 6753 | nSql += nLine; |
| 6754 | } |
| 6755 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6756 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6757 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6758 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6759 | if( p->outCount ){ |
| 6760 | output_reset(p); |
| 6761 | p->outCount = 0; |
| 6762 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6763 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6764 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 6765 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6766 | } |
| 6767 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6768 | if( nSql && !_all_whitespace(zSql) ){ |
| 6769 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6770 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 6771 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 6772 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 6773 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6776 | /* |
| 6777 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6778 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6779 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6780 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6781 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6782 | if( clearFlag ){ |
| 6783 | free(home_dir); |
| 6784 | home_dir = 0; |
| 6785 | return 0; |
| 6786 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6787 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6788 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 6789 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 6790 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 6791 | { |
| 6792 | struct passwd *pwent; |
| 6793 | uid_t uid = getuid(); |
| 6794 | if( (pwent=getpwuid(uid)) != NULL) { |
| 6795 | home_dir = pwent->pw_dir; |
| 6796 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6797 | } |
| 6798 | #endif |
| 6799 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6800 | #if defined(_WIN32_WCE) |
| 6801 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 6802 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6803 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6804 | #else |
| 6805 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6806 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6807 | if (!home_dir) { |
| 6808 | home_dir = getenv("USERPROFILE"); |
| 6809 | } |
| 6810 | #endif |
| 6811 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6812 | if (!home_dir) { |
| 6813 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6814 | } |
| 6815 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6816 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6817 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6818 | char *zDrive, *zPath; |
| 6819 | int n; |
| 6820 | zDrive = getenv("HOMEDRIVE"); |
| 6821 | zPath = getenv("HOMEPATH"); |
| 6822 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6823 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6824 | home_dir = malloc( n ); |
| 6825 | if( home_dir==0 ) return 0; |
| 6826 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 6827 | return home_dir; |
| 6828 | } |
| 6829 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6830 | } |
| 6831 | #endif |
| 6832 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6833 | #endif /* !_WIN32_WCE */ |
| 6834 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6835 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6836 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6837 | char *z = malloc( n ); |
| 6838 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6839 | home_dir = z; |
| 6840 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6841 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6842 | return home_dir; |
| 6843 | } |
| 6844 | |
| 6845 | /* |
| 6846 | ** Read input from the file given by sqliterc_override. Or if that |
| 6847 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6848 | ** |
| 6849 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6850 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6851 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6852 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6853 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 6854 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6855 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6856 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6857 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6858 | FILE *in = NULL; |
| 6859 | |
| 6860 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6861 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6862 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6863 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6864 | " cannot read ~/.sqliterc\n"); |
| 6865 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6866 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 6867 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6868 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 6869 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6870 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 6871 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6872 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6873 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6874 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6875 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6876 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 6877 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6878 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6879 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6882 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6883 | ** Show available command line options |
| 6884 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6885 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6886 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6887 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6888 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6889 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 6890 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6891 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6892 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 6893 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6894 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6895 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 6896 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 6897 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6898 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6899 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6900 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6901 | " -line set output mode to 'line'\n" |
| 6902 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6903 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 6904 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6905 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 6906 | " -multiplex enable the multiplexor VFS\n" |
| 6907 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6908 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6909 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6910 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 6911 | " -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] | 6912 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6913 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6914 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6915 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 6916 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 6917 | " -vfstrace enable tracing of all VFS calls\n" |
| 6918 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6919 | ; |
| 6920 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6921 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6922 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 6923 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 6924 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6925 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6926 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6927 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6928 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6929 | } |
| 6930 | exit(1); |
| 6931 | } |
| 6932 | |
| 6933 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6934 | ** Initialize the state information in data |
| 6935 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6936 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6937 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6938 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 6939 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6940 | memcpy(data->colSeparator,SEP_Column, 2); |
| 6941 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6942 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6943 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 6944 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 6945 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6946 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6947 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 6948 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6951 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6952 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6953 | */ |
| 6954 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6955 | static void printBold(const char *zText){ |
| 6956 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 6957 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 6958 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 6959 | SetConsoleTextAttribute(out, |
| 6960 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 6961 | ); |
| 6962 | printf("%s", zText); |
| 6963 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6964 | } |
| 6965 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6966 | static void printBold(const char *zText){ |
| 6967 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6968 | } |
| 6969 | #endif |
| 6970 | |
| 6971 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6972 | ** Get the argument to an --option. Throw an error and die if no argument |
| 6973 | ** is available. |
| 6974 | */ |
| 6975 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 6976 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6977 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6978 | argv[0], argv[argc-1]); |
| 6979 | exit(1); |
| 6980 | } |
| 6981 | return argv[i]; |
| 6982 | } |
| 6983 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6984 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 6985 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 6986 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 6987 | # else |
| 6988 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 6989 | # endif |
| 6990 | #endif |
| 6991 | |
| 6992 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 6993 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6994 | #else |
| 6995 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 6996 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6997 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6998 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6999 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7000 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7001 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7002 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7003 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7004 | int readStdin = 1; |
| 7005 | int nCmd = 0; |
| 7006 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7007 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7008 | setBinaryMode(stdin, 0); |
| 7009 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7010 | stdin_is_interactive = isatty(0); |
| 7011 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7012 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 7013 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7014 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7015 | 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] | 7016 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 7017 | exit(1); |
| 7018 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 7019 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7020 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7021 | #if !SQLITE_SHELL_IS_UTF8 |
| 7022 | sqlite3_initialize(); |
| 7023 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 7024 | if( argv==0 ){ |
| 7025 | raw_printf(stderr, "out of memory\n"); |
| 7026 | exit(1); |
| 7027 | } |
| 7028 | for(i=0; i<argc; i++){ |
| 7029 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 7030 | if( argv[i]==0 ){ |
| 7031 | raw_printf(stderr, "out of memory\n"); |
| 7032 | exit(1); |
| 7033 | } |
| 7034 | } |
| 7035 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7036 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7037 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7038 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7039 | /* Make sure we have a valid signal handler early, before anything |
| 7040 | ** else is done. |
| 7041 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 7042 | #ifdef SIGINT |
| 7043 | signal(SIGINT, interrupt_handler); |
| 7044 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7045 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7046 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 7047 | { |
| 7048 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 7049 | ** of a C-function that will provide the name of the database file. Use |
| 7050 | ** this compile-time option to embed this shell program in larger |
| 7051 | ** applications. */ |
| 7052 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 7053 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 7054 | warnInmemoryDb = 0; |
| 7055 | } |
| 7056 | #endif |
| 7057 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7058 | /* Do an initial pass through the command-line argument to locate |
| 7059 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7060 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7061 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7062 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7063 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7064 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7065 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7066 | if( z[0]!='-' ){ |
| 7067 | if( data.zDbFilename==0 ){ |
| 7068 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7069 | }else{ |
| 7070 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 7071 | ** mean that nothing is read from stdin */ |
| 7072 | readStdin = 0; |
| 7073 | nCmd++; |
| 7074 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 7075 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7076 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7077 | exit(1); |
| 7078 | } |
| 7079 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7080 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7081 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7082 | if( z[1]=='-' ) z++; |
| 7083 | if( strcmp(z,"-separator")==0 |
| 7084 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7085 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7086 | || strcmp(z,"-cmd")==0 |
| 7087 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7088 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7089 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7090 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7091 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7092 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7093 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7094 | ** we do the actual processing of arguments later in a second pass. |
| 7095 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 7096 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7097 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 7098 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7099 | const char *zSize; |
| 7100 | sqlite3_int64 szHeap; |
| 7101 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7102 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7103 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7104 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7105 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 7106 | #else |
| 7107 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7108 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7109 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7110 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7111 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7112 | if( sz>400000 ) sz = 400000; |
| 7113 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7114 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7115 | if( n>10 ) n = 10; |
| 7116 | if( n<1 ) n = 1; |
| 7117 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 7118 | data.shellFlgs |= SHFLG_Scratch; |
| 7119 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7120 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7121 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7122 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7123 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7124 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7125 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 7126 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7127 | data.shellFlgs |= SHFLG_Pagecache; |
| 7128 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7129 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7130 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7131 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7132 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7133 | if( n<0 ) n = 0; |
| 7134 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 7135 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7136 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7137 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7138 | extern int vfstrace_register( |
| 7139 | const char *zTraceName, |
| 7140 | const char *zOldVfsName, |
| 7141 | int (*xOut)(const char*,void*), |
| 7142 | void *pOutArg, |
| 7143 | int makeDefault |
| 7144 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7145 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7146 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7147 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7148 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7149 | extern int sqlite3_multiple_initialize(const char*,int); |
| 7150 | sqlite3_multiplex_initialize(0, 1); |
| 7151 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7152 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 7153 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 7154 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7155 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7156 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7157 | if( pVfs ){ |
| 7158 | sqlite3_vfs_register(pVfs, 1); |
| 7159 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7160 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7161 | exit(1); |
| 7162 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7163 | } |
| 7164 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7165 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7166 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7167 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7168 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7169 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7170 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 7171 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7172 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7173 | } |
| 7174 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7175 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7176 | /* Go ahead and open the database file if it already exists. If the |
| 7177 | ** file does not exist, delay opening it. This prevents empty database |
| 7178 | ** files from being created if a user mistypes the database name argument |
| 7179 | ** to the sqlite command-line tool. |
| 7180 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 7181 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7182 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7183 | } |
| 7184 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7185 | /* Process the initialization file if there is one. If no -init option |
| 7186 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 7187 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7188 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7189 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7190 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7191 | /* Make a second pass through the command-line argument and set |
| 7192 | ** options. This second pass is delayed until after the initialization |
| 7193 | ** file is processed so that the command-line arguments will override |
| 7194 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7195 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7196 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7197 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7198 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7199 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 7200 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7201 | i++; |
| 7202 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7203 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7204 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7205 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7206 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7207 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7208 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 7209 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7210 | }else if( strcmp(z,"-csv")==0 ){ |
| 7211 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7212 | memcpy(data.colSeparator,",",2); |
| 7213 | }else if( strcmp(z,"-ascii")==0 ){ |
| 7214 | data.mode = MODE_Ascii; |
| 7215 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7216 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7217 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7218 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7219 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7220 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7221 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7222 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7223 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7224 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7225 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 7226 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7227 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7228 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7229 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7230 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7231 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7232 | }else if( strcmp(z,"-echo")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7233 | ShellSetFlag(&data, SHFLG_Echo); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 7234 | }else if( strcmp(z,"-eqp")==0 ){ |
| 7235 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7236 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 7237 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7238 | }else if( strcmp(z,"-stats")==0 ){ |
| 7239 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 7240 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 7241 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 7242 | }else if( strcmp(z,"-backslash")==0 ){ |
| 7243 | /* Undocumented command-line option: -backslash |
| 7244 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 7245 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 7246 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 7247 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7248 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7249 | }else if( strcmp(z,"-bail")==0 ){ |
| 7250 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7251 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7252 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 7253 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7254 | }else if( strcmp(z,"-interactive")==0 ){ |
| 7255 | stdin_is_interactive = 1; |
| 7256 | }else if( strcmp(z,"-batch")==0 ){ |
| 7257 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7258 | }else if( strcmp(z,"-heap")==0 ){ |
| 7259 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7260 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7261 | i+=2; |
| 7262 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7263 | i+=2; |
| 7264 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7265 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7266 | }else if( strcmp(z,"-mmap")==0 ){ |
| 7267 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7268 | }else if( strcmp(z,"-vfs")==0 ){ |
| 7269 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7270 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7271 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 7272 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7273 | #endif |
| 7274 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7275 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 7276 | i++; |
| 7277 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7278 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7279 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7280 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7281 | /* Run commands that follow -cmd first and separately from commands |
| 7282 | ** that simply appear on the command-line. This seems goofy. It would |
| 7283 | ** be better if all commands ran in the order that they appear. But |
| 7284 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7285 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7286 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7287 | if( z[0]=='.' ){ |
| 7288 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 7289 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7290 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7291 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7292 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 7293 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7294 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7295 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 7296 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7297 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7298 | if( bail_on_error ) return rc; |
| 7299 | } |
| 7300 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7301 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7302 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 7303 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7304 | return 1; |
| 7305 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7306 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7307 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7308 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7309 | if( !readStdin ){ |
| 7310 | /* Run all arguments that do not begin with '-' as if they were separate |
| 7311 | ** command-line inputs, except for the argToSkip argument which contains |
| 7312 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7313 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7314 | for(i=0; i<nCmd; i++){ |
| 7315 | if( azCmd[i][0]=='.' ){ |
| 7316 | rc = do_meta_command(azCmd[i], &data); |
| 7317 | if( rc ) return rc==2 ? 0 : rc; |
| 7318 | }else{ |
| 7319 | open_db(&data, 0); |
| 7320 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 7321 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7322 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7323 | return rc!=0 ? rc : 1; |
| 7324 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7325 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7326 | return rc; |
| 7327 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 7328 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7329 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7330 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7331 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7332 | /* Run commands received from standard input |
| 7333 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7334 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7335 | char *zHome; |
| 7336 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7337 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7338 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 7339 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7340 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7341 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7342 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7343 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7344 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 7345 | printBold("transient in-memory database"); |
| 7346 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7347 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7348 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7349 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7350 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7351 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7352 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7353 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7354 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7355 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7356 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7357 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7358 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 7359 | shell_stifle_history(100); |
| 7360 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7361 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7362 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7363 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7364 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7365 | } |
| 7366 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 7367 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 7368 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 7369 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 7370 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7371 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7372 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7373 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7374 | #if !SQLITE_SHELL_IS_UTF8 |
| 7375 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7376 | sqlite3_free(argv); |
| 7377 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7378 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7379 | } |