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 | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1364 | int echoOn; /* True to echo input commands */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1365 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1366 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1367 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1368 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 1369 | int countChanges; /* True to display change counts */ |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 1370 | int backslashOn; /* Resolve C-style \x escapes in SQL input text */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1371 | int outCount; /* Revert to stdout when reaching zero */ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 1372 | int preserveRowid; /* Preserver ROWID values on a ".dump" command */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1373 | int cnt; /* Number of records displayed so far */ |
| 1374 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 1375 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1376 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1377 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1378 | int cMode; /* temporary output mode for the current query */ |
| 1379 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1380 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1381 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1382 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1383 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1384 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1385 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1386 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1387 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1388 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1389 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1390 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1391 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1392 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1393 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 1394 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1395 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1396 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1397 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1398 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1399 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1400 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1401 | #if defined(SQLITE_ENABLE_SESSION) |
| 1402 | int nSession; /* Number of active sessions */ |
| 1403 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1404 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1405 | }; |
| 1406 | |
| 1407 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1408 | ** These are the allowed shellFlgs values |
| 1409 | */ |
| 1410 | #define SHFLG_Scratch 0x00001 /* The --scratch option is used */ |
| 1411 | #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ |
| 1412 | #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ |
| 1413 | |
| 1414 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1415 | ** These are the allowed modes. |
| 1416 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1417 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1418 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1419 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1420 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1421 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1422 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1423 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1424 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1425 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1426 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1427 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1428 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1429 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1430 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1431 | "line", |
| 1432 | "column", |
| 1433 | "list", |
| 1434 | "semi", |
| 1435 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1436 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1437 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1438 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1439 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1440 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1441 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1442 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1443 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1446 | ** These are the column/row/line separators used by the various |
| 1447 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1448 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1449 | #define SEP_Column "|" |
| 1450 | #define SEP_Row "\n" |
| 1451 | #define SEP_Tab "\t" |
| 1452 | #define SEP_Space " " |
| 1453 | #define SEP_Comma "," |
| 1454 | #define SEP_CrLf "\r\n" |
| 1455 | #define SEP_Unit "\x1F" |
| 1456 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1459 | ** Number of elements in an array |
| 1460 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1461 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1462 | |
| 1463 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1464 | ** A callback for the sqlite3_log() interface. |
| 1465 | */ |
| 1466 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1467 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1468 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1469 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1470 | fflush(p->pLog); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1474 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1475 | */ |
| 1476 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1477 | int i; |
| 1478 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1479 | raw_printf(out,"X'"); |
| 1480 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1481 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1485 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1486 | */ |
| 1487 | static void output_quoted_string(FILE *out, const char *z){ |
| 1488 | int i; |
| 1489 | int nSingle = 0; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1490 | setBinaryMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1491 | for(i=0; z[i]; i++){ |
| 1492 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1493 | } |
| 1494 | if( nSingle==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1495 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1496 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1497 | raw_printf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1498 | while( *z ){ |
| 1499 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 1500 | if( i==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1501 | raw_printf(out,"''"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1502 | z++; |
| 1503 | }else if( z[i]=='\'' ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1504 | utf8_printf(out,"%.*s''",i,z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1505 | z += i+1; |
| 1506 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1507 | utf8_printf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | } |
| 1510 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1511 | raw_printf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1512 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1513 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1517 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1518 | */ |
| 1519 | static void output_c_string(FILE *out, const char *z){ |
| 1520 | unsigned int c; |
| 1521 | fputc('"', out); |
| 1522 | while( (c = *(z++))!=0 ){ |
| 1523 | if( c=='\\' ){ |
| 1524 | fputc(c, out); |
| 1525 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 1526 | }else if( c=='"' ){ |
| 1527 | fputc('\\', out); |
| 1528 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1529 | }else if( c=='\t' ){ |
| 1530 | fputc('\\', out); |
| 1531 | fputc('t', out); |
| 1532 | }else if( c=='\n' ){ |
| 1533 | fputc('\\', out); |
| 1534 | fputc('n', out); |
| 1535 | }else if( c=='\r' ){ |
| 1536 | fputc('\\', out); |
| 1537 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 1538 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1539 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1540 | }else{ |
| 1541 | fputc(c, out); |
| 1542 | } |
| 1543 | } |
| 1544 | fputc('"', out); |
| 1545 | } |
| 1546 | |
| 1547 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1548 | ** Output the given string with characters that are special to |
| 1549 | ** HTML escaped. |
| 1550 | */ |
| 1551 | static void output_html_string(FILE *out, const char *z){ |
| 1552 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 1553 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1554 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1555 | for(i=0; z[i] |
| 1556 | && z[i]!='<' |
| 1557 | && z[i]!='&' |
| 1558 | && z[i]!='>' |
| 1559 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1560 | && z[i]!='\''; |
| 1561 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1562 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1563 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1564 | } |
| 1565 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1566 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1567 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1568 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1569 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1570 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1571 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1572 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1573 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1574 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1575 | }else{ |
| 1576 | break; |
| 1577 | } |
| 1578 | z += i + 1; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1583 | ** If a field contains any character identified by a 1 in the following |
| 1584 | ** array, then the string must be quoted for CSV. |
| 1585 | */ |
| 1586 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1587 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1588 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1589 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1590 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1591 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1592 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1593 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1594 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1595 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1596 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1597 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1598 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1599 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1600 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1601 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1602 | 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] | 1603 | }; |
| 1604 | |
| 1605 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 1606 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1607 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1608 | ** the null value. Strings are quoted if necessary. The separator |
| 1609 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1610 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1611 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1612 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1613 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1614 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1615 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1616 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1617 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1618 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1619 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1620 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1621 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1622 | i = 0; |
| 1623 | break; |
| 1624 | } |
| 1625 | } |
| 1626 | if( i==0 ){ |
| 1627 | putc('"', out); |
| 1628 | for(i=0; z[i]; i++){ |
| 1629 | if( z[i]=='"' ) putc('"', out); |
| 1630 | putc(z[i], out); |
| 1631 | } |
| 1632 | putc('"', out); |
| 1633 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1634 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1635 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1636 | } |
| 1637 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1638 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1642 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1643 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1644 | ** This routine runs when the user presses Ctrl-C |
| 1645 | */ |
| 1646 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1647 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 1648 | seenInterrupt++; |
| 1649 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 1650 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1651 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1652 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1653 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1654 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1655 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1656 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1657 | ** invoked. It always returns SQLITE_OK. |
| 1658 | */ |
| 1659 | static int shellAuth( |
| 1660 | void *pClientData, |
| 1661 | int op, |
| 1662 | const char *zA1, |
| 1663 | const char *zA2, |
| 1664 | const char *zA3, |
| 1665 | const char *zA4 |
| 1666 | ){ |
| 1667 | ShellState *p = (ShellState*)pClientData; |
| 1668 | static const char *azAction[] = { 0, |
| 1669 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1670 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1671 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1672 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1673 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1674 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1675 | "PRAGMA", "READ", "SELECT", |
| 1676 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1677 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1678 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1679 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1680 | }; |
| 1681 | int i; |
| 1682 | const char *az[4]; |
| 1683 | az[0] = zA1; |
| 1684 | az[1] = zA2; |
| 1685 | az[2] = zA3; |
| 1686 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1687 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1688 | for(i=0; i<4; i++){ |
| 1689 | raw_printf(p->out, " "); |
| 1690 | if( az[i] ){ |
| 1691 | output_c_string(p->out, az[i]); |
| 1692 | }else{ |
| 1693 | raw_printf(p->out, "NULL"); |
| 1694 | } |
| 1695 | } |
| 1696 | raw_printf(p->out, "\n"); |
| 1697 | return SQLITE_OK; |
| 1698 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1699 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1700 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1701 | /* |
| 1702 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1703 | ** |
| 1704 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1705 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1706 | */ |
| 1707 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1708 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1709 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1710 | }else{ |
| 1711 | utf8_printf(out, "%s%s", z, zTail); |
| 1712 | } |
| 1713 | } |
| 1714 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1715 | char c = z[n]; |
| 1716 | z[n] = 0; |
| 1717 | printSchemaLine(out, z, zTail); |
| 1718 | z[n] = c; |
| 1719 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1720 | |
| 1721 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1722 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1723 | ** invokes for each row of a query result. |
| 1724 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1725 | static int shell_callback( |
| 1726 | void *pArg, |
| 1727 | int nArg, /* Number of result columns */ |
| 1728 | char **azArg, /* Text of each result column */ |
| 1729 | char **azCol, /* Column names */ |
| 1730 | int *aiType /* Column types */ |
| 1731 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1732 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1733 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1734 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1735 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1736 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1737 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1738 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1739 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1740 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1741 | if( len>w ) w = len; |
| 1742 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1743 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1744 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1745 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1746 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1747 | } |
| 1748 | break; |
| 1749 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1750 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1751 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1752 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1753 | const int *colWidth; |
| 1754 | int showHdr; |
| 1755 | char *rowSep; |
| 1756 | if( p->cMode==MODE_Column ){ |
| 1757 | colWidth = p->colWidth; |
| 1758 | showHdr = p->showHeader; |
| 1759 | rowSep = p->rowSeparator; |
| 1760 | }else{ |
| 1761 | colWidth = aExplainWidths; |
| 1762 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 1763 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1764 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1765 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1766 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1767 | int w, n; |
| 1768 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1769 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1770 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1771 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1772 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1773 | if( w==0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1774 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1775 | if( w<10 ) w = 10; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1776 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1777 | if( w<n ) w = n; |
| 1778 | } |
| 1779 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1780 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1781 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1782 | if( showHdr ){ |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1783 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1784 | utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1785 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1786 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1787 | utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1788 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1789 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1792 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1793 | for(i=0; i<nArg; i++){ |
| 1794 | int w; |
| 1795 | if( i<ArraySize(p->actualWidth) ){ |
| 1796 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1797 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1798 | }else{ |
| 1799 | w = 10; |
| 1800 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1801 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1802 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1803 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1804 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1805 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1808 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1809 | for(i=0; i<nArg; i++){ |
| 1810 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1811 | if( i<ArraySize(p->actualWidth) ){ |
| 1812 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1813 | }else{ |
| 1814 | w = 10; |
| 1815 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1816 | if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1817 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1818 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1819 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1820 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1821 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1822 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1823 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1824 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1825 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1826 | utf8_printf(p->out,"%*.*s%s",-w,-w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1827 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1828 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1829 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1830 | utf8_printf(p->out,"%-*.*s%s",w,w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1831 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1832 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1833 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1834 | } |
| 1835 | break; |
| 1836 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1837 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1838 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1839 | break; |
| 1840 | } |
| 1841 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1842 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1843 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1844 | int nParen = 0; |
| 1845 | char cEnd = 0; |
| 1846 | char c; |
| 1847 | int nLine = 0; |
| 1848 | assert( nArg==1 ); |
| 1849 | if( azArg[0]==0 ) break; |
| 1850 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1851 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1852 | ){ |
| 1853 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1854 | break; |
| 1855 | } |
| 1856 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1857 | j = 0; |
| 1858 | for(i=0; IsSpace(z[i]); i++){} |
| 1859 | for(; (c = z[i])!=0; i++){ |
| 1860 | if( IsSpace(c) ){ |
| 1861 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1862 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1863 | j--; |
| 1864 | } |
| 1865 | z[j++] = c; |
| 1866 | } |
| 1867 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1868 | z[j] = 0; |
| 1869 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1870 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1871 | if( c==cEnd ){ |
| 1872 | cEnd = 0; |
| 1873 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1874 | cEnd = c; |
| 1875 | }else if( c=='[' ){ |
| 1876 | cEnd = ']'; |
| 1877 | }else if( c=='(' ){ |
| 1878 | nParen++; |
| 1879 | }else if( c==')' ){ |
| 1880 | nParen--; |
| 1881 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1882 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1883 | j = 0; |
| 1884 | } |
| 1885 | } |
| 1886 | z[j++] = c; |
| 1887 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 1888 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1889 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1890 | j = 0; |
| 1891 | nLine++; |
| 1892 | while( IsSpace(z[i+1]) ){ i++; } |
| 1893 | } |
| 1894 | } |
| 1895 | z[j] = 0; |
| 1896 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1897 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1898 | sqlite3_free(z); |
| 1899 | break; |
| 1900 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1901 | case MODE_List: { |
| 1902 | if( p->cnt++==0 && p->showHeader ){ |
| 1903 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1904 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1905 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1908 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1909 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1910 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1911 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1912 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1913 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1914 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1915 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1916 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1917 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1918 | } |
| 1919 | break; |
| 1920 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1921 | case MODE_Html: { |
| 1922 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1923 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1924 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1925 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1926 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1927 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1928 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1929 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1930 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1931 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1932 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1933 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1934 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1935 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1936 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1937 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1938 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1941 | case MODE_Tcl: { |
| 1942 | if( p->cnt++==0 && p->showHeader ){ |
| 1943 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1944 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1945 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1946 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1947 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1948 | } |
| 1949 | if( azArg==0 ) break; |
| 1950 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1951 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1952 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1953 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1954 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1955 | break; |
| 1956 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1957 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1958 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1959 | if( p->cnt++==0 && p->showHeader ){ |
| 1960 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1961 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1962 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1963 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1964 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 1965 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1966 | for(i=0; i<nArg; i++){ |
| 1967 | output_csv(p, azArg[i], i<nArg-1); |
| 1968 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1969 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1970 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1971 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1972 | break; |
| 1973 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1974 | case MODE_Quote: |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1975 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1976 | if( azArg==0 ) break; |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1977 | if( p->cMode==MODE_Insert ){ |
| 1978 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 1979 | if( p->showHeader ){ |
| 1980 | raw_printf(p->out,"("); |
| 1981 | for(i=0; i<nArg; i++){ |
| 1982 | char *zSep = i>0 ? ",": ""; |
| 1983 | utf8_printf(p->out, "%s%s", zSep, azCol[i]); |
| 1984 | } |
| 1985 | raw_printf(p->out,")"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 1986 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1987 | raw_printf(p->out," VALUES("); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1988 | }else if( p->cnt==0 && p->showHeader ){ |
| 1989 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 1990 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1991 | output_quoted_string(p->out, azCol[i]); |
| 1992 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 1993 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 1994 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1995 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1996 | for(i=0; i<nArg; i++){ |
| 1997 | char *zSep = i>0 ? ",": ""; |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 1998 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1999 | utf8_printf(p->out,"%sNULL",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2000 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2001 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2002 | output_quoted_string(p->out, azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2003 | }else if( aiType && (aiType[i]==SQLITE_INTEGER |
| 2004 | || aiType[i]==SQLITE_FLOAT) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2005 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2006 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2007 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2008 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2009 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2010 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2011 | }else if( isNumber(azArg[i], 0) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2012 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2013 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2014 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2015 | output_quoted_string(p->out, azArg[i]); |
| 2016 | } |
| 2017 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2018 | raw_printf(p->out,p->cMode==MODE_Quote?"\n":");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2019 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2020 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2021 | case MODE_Ascii: { |
| 2022 | if( p->cnt++==0 && p->showHeader ){ |
| 2023 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2024 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2025 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2026 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2027 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2028 | } |
| 2029 | if( azArg==0 ) break; |
| 2030 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2031 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2032 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2033 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2034 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2035 | break; |
| 2036 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2037 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
| 2040 | |
| 2041 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2042 | ** This is the callback routine that the SQLite library |
| 2043 | ** invokes for each row of a query result. |
| 2044 | */ |
| 2045 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2046 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2047 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2048 | } |
| 2049 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2050 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2051 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2052 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2053 | ** the name of the table given. Escape any quote characters in the |
| 2054 | ** table name. |
| 2055 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2056 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2057 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2058 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2059 | char *z; |
| 2060 | |
| 2061 | if( p->zDestTable ){ |
| 2062 | free(p->zDestTable); |
| 2063 | p->zDestTable = 0; |
| 2064 | } |
| 2065 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2066 | cQuote = quoteChar(zName); |
| 2067 | n = strlen30(zName); |
| 2068 | if( cQuote ) n += 2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2069 | z = p->zDestTable = malloc( n+1 ); |
| 2070 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2071 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2072 | exit(1); |
| 2073 | } |
| 2074 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2075 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2076 | for(i=0; zName[i]; i++){ |
| 2077 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2078 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2079 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2080 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2081 | z[n] = 0; |
| 2082 | } |
| 2083 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2084 | |
| 2085 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2086 | ** Execute a query statement that will generate SQL output. Print |
| 2087 | ** the result columns, comma-separated, on a line and then add a |
| 2088 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2089 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2090 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2091 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2092 | ** "--" comment occurs at the end of the statement, the comment |
| 2093 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2094 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2095 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2096 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2097 | const char *zSelect, /* SELECT statement to extract content */ |
| 2098 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2099 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2100 | sqlite3_stmt *pSelect; |
| 2101 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2102 | int nResult; |
| 2103 | int i; |
| 2104 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2105 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2106 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2107 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2108 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2109 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2110 | return rc; |
| 2111 | } |
| 2112 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2113 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2114 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2115 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2116 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2117 | zFirstRow = 0; |
| 2118 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2119 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2120 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2121 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2122 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2123 | } |
| 2124 | if( z==0 ) z = ""; |
| 2125 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2126 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2127 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2128 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2129 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2130 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2131 | rc = sqlite3_step(pSelect); |
| 2132 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2133 | rc = sqlite3_finalize(pSelect); |
| 2134 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2135 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2136 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2137 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2138 | } |
| 2139 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2142 | /* |
| 2143 | ** Allocate space and save off current error string. |
| 2144 | */ |
| 2145 | static char *save_err_msg( |
| 2146 | sqlite3 *db /* Database to query */ |
| 2147 | ){ |
| 2148 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2149 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2150 | if( zErrMsg ){ |
| 2151 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2152 | } |
| 2153 | return zErrMsg; |
| 2154 | } |
| 2155 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2156 | #ifdef __linux__ |
| 2157 | /* |
| 2158 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2159 | */ |
| 2160 | static void displayLinuxIoStats(FILE *out){ |
| 2161 | FILE *in; |
| 2162 | char z[200]; |
| 2163 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2164 | in = fopen(z, "rb"); |
| 2165 | if( in==0 ) return; |
| 2166 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2167 | static const struct { |
| 2168 | const char *zPattern; |
| 2169 | const char *zDesc; |
| 2170 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 2171 | { "rchar: ", "Bytes received by read():" }, |
| 2172 | { "wchar: ", "Bytes sent to write():" }, |
| 2173 | { "syscr: ", "Read() system calls:" }, |
| 2174 | { "syscw: ", "Write() system calls:" }, |
| 2175 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2176 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2177 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2178 | }; |
| 2179 | int i; |
| 2180 | for(i=0; i<ArraySize(aTrans); i++){ |
| 2181 | int n = (int)strlen(aTrans[i].zPattern); |
| 2182 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2183 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2189 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2190 | #endif |
| 2191 | |
| 2192 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2193 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2194 | ** Display memory stats. |
| 2195 | */ |
| 2196 | static int display_stats( |
| 2197 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2198 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2199 | int bReset /* True to reset the stats */ |
| 2200 | ){ |
| 2201 | int iCur; |
| 2202 | int iHiwtr; |
| 2203 | |
| 2204 | if( pArg && pArg->out ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2205 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2206 | iHiwtr = iCur = -1; |
| 2207 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2208 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2209 | "Memory Used: %d (max %d) bytes\n", |
| 2210 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2211 | iHiwtr = iCur = -1; |
| 2212 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2213 | raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2214 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2215 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 2216 | iHiwtr = iCur = -1; |
| 2217 | sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2218 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2219 | "Number of Pcache Pages Used: %d (max %d) pages\n", |
| 2220 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2221 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2222 | iHiwtr = iCur = -1; |
| 2223 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2224 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2225 | "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", |
| 2226 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2227 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
| 2228 | iHiwtr = iCur = -1; |
| 2229 | sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2230 | raw_printf(pArg->out, |
| 2231 | "Number of Scratch Allocations Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2232 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2233 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2234 | iHiwtr = iCur = -1; |
| 2235 | sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2236 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2237 | "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", |
| 2238 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2239 | iHiwtr = iCur = -1; |
| 2240 | sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2241 | raw_printf(pArg->out, "Largest Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2242 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2243 | iHiwtr = iCur = -1; |
| 2244 | sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2245 | raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2246 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2247 | iHiwtr = iCur = -1; |
| 2248 | sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2249 | raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2250 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2251 | #ifdef YYTRACKMAXSTACKDEPTH |
| 2252 | iHiwtr = iCur = -1; |
| 2253 | sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2254 | raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2255 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2256 | #endif |
| 2257 | } |
| 2258 | |
| 2259 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2260 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2261 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2262 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2263 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2264 | raw_printf(pArg->out, |
| 2265 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2266 | iCur, iHiwtr); |
| 2267 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2268 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2269 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2270 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2271 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2272 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2273 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2274 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2275 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2276 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2277 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2278 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2279 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2280 | iHiwtr = iCur = -1; |
| 2281 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2282 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2283 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2284 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2285 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2286 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2287 | iHiwtr = iCur = -1; |
| 2288 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2289 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2290 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2291 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2292 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2293 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2294 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2295 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2296 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2297 | iHiwtr = iCur = -1; |
| 2298 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2299 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2300 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2304 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2305 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2306 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2307 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2308 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2309 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2310 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2311 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2312 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2315 | #ifdef __linux__ |
| 2316 | displayLinuxIoStats(pArg->out); |
| 2317 | #endif |
| 2318 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 2319 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2320 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2325 | ** Display scan stats. |
| 2326 | */ |
| 2327 | static void display_scanstats( |
| 2328 | sqlite3 *db, /* Database to query */ |
| 2329 | ShellState *pArg /* Pointer to ShellState */ |
| 2330 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2331 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2332 | UNUSED_PARAMETER(db); |
| 2333 | UNUSED_PARAMETER(pArg); |
| 2334 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2335 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2336 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2337 | mx = 0; |
| 2338 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2339 | double rEstLoop = 1.0; |
| 2340 | for(i=n=0; 1; i++){ |
| 2341 | sqlite3_stmt *p = pArg->pStmt; |
| 2342 | sqlite3_int64 nLoop, nVisit; |
| 2343 | double rEst; |
| 2344 | int iSid; |
| 2345 | const char *zExplain; |
| 2346 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2347 | break; |
| 2348 | } |
| 2349 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2350 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2351 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2352 | if( n==0 ){ |
| 2353 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2354 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2355 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2356 | n++; |
| 2357 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2358 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2359 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2360 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2361 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2362 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2363 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 2364 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2365 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2366 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2367 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2368 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2369 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2373 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2374 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2375 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2376 | ** Otherwise, return zero. |
| 2377 | */ |
| 2378 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2379 | int i; |
| 2380 | for(i=0; azArray[i]; i++){ |
| 2381 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2382 | } |
| 2383 | return 0; |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2388 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2389 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2390 | ** |
| 2391 | ** The indenting rules are: |
| 2392 | ** |
| 2393 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2394 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2395 | ** itself by 2 spaces. |
| 2396 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2397 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2398 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 2399 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2400 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2401 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 2402 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2403 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2404 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2405 | const char *zSql; /* The text of the SQL statement */ |
| 2406 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2407 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2408 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2409 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2410 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 2411 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2412 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2413 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2414 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2415 | const char *azGoto[] = { "Goto", 0 }; |
| 2416 | |
| 2417 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2418 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2419 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2420 | p->cMode = p->mode; |
| 2421 | return; |
| 2422 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2423 | zSql = sqlite3_sql(pSql); |
| 2424 | if( zSql==0 ) return; |
| 2425 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2426 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2427 | p->cMode = p->mode; |
| 2428 | return; |
| 2429 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2430 | |
| 2431 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2432 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2433 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2434 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2435 | |
| 2436 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2437 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2438 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2439 | ** the current instruction is part of a sub-program generated by an |
| 2440 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2441 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2442 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2443 | |
| 2444 | /* Grow the p->aiIndent array as required */ |
| 2445 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2446 | if( iOp==0 ){ |
| 2447 | /* Do further verfication that this is explain output. Abort if |
| 2448 | ** it is not */ |
| 2449 | static const char *explainCols[] = { |
| 2450 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2451 | int jj; |
| 2452 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2453 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2454 | p->cMode = p->mode; |
| 2455 | sqlite3_reset(pSql); |
| 2456 | return; |
| 2457 | } |
| 2458 | } |
| 2459 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2460 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2461 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2462 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2463 | } |
| 2464 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2465 | p->aiIndent[iOp] = 0; |
| 2466 | p->nIndent = iOp+1; |
| 2467 | |
| 2468 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2469 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2470 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2471 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2472 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2473 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2474 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2478 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2479 | sqlite3_free(abYield); |
| 2480 | sqlite3_reset(pSql); |
| 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | ** Free the array allocated by explain_data_prepare(). |
| 2485 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2486 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2487 | sqlite3_free(p->aiIndent); |
| 2488 | p->aiIndent = 0; |
| 2489 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2490 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2494 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2495 | */ |
| 2496 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2497 | extern int sqlite3SelectTrace; |
| 2498 | static int savedSelectTrace; |
| 2499 | #endif |
| 2500 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2501 | extern int sqlite3WhereTrace; |
| 2502 | static int savedWhereTrace; |
| 2503 | #endif |
| 2504 | static void disable_debug_trace_modes(void){ |
| 2505 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2506 | savedSelectTrace = sqlite3SelectTrace; |
| 2507 | sqlite3SelectTrace = 0; |
| 2508 | #endif |
| 2509 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2510 | savedWhereTrace = sqlite3WhereTrace; |
| 2511 | sqlite3WhereTrace = 0; |
| 2512 | #endif |
| 2513 | } |
| 2514 | static void restore_debug_trace_modes(void){ |
| 2515 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2516 | sqlite3SelectTrace = savedSelectTrace; |
| 2517 | #endif |
| 2518 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2519 | sqlite3WhereTrace = savedWhereTrace; |
| 2520 | #endif |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | ** Run a prepared statement |
| 2525 | */ |
| 2526 | static void exec_prepared_stmt( |
| 2527 | ShellState *pArg, /* Pointer to ShellState */ |
| 2528 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2529 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2530 | ){ |
| 2531 | int rc; |
| 2532 | |
| 2533 | /* perform the first step. this will tell us if we |
| 2534 | ** have a result set or not and how wide it is. |
| 2535 | */ |
| 2536 | rc = sqlite3_step(pStmt); |
| 2537 | /* if we have a result set... */ |
| 2538 | if( SQLITE_ROW == rc ){ |
| 2539 | /* if we have a callback... */ |
| 2540 | if( xCallback ){ |
| 2541 | /* allocate space for col name ptr, value ptr, and type */ |
| 2542 | int nCol = sqlite3_column_count(pStmt); |
| 2543 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2544 | if( !pData ){ |
| 2545 | rc = SQLITE_NOMEM; |
| 2546 | }else{ |
| 2547 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2548 | char **azVals = &azCols[nCol]; /* Results */ |
| 2549 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2550 | int i, x; |
| 2551 | assert(sizeof(int) <= sizeof(char *)); |
| 2552 | /* save off ptrs to column names */ |
| 2553 | for(i=0; i<nCol; i++){ |
| 2554 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2555 | } |
| 2556 | do{ |
| 2557 | /* extract the data and data types */ |
| 2558 | for(i=0; i<nCol; i++){ |
| 2559 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2560 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2561 | azVals[i] = ""; |
| 2562 | }else{ |
| 2563 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2564 | } |
| 2565 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2566 | rc = SQLITE_NOMEM; |
| 2567 | break; /* from for */ |
| 2568 | } |
| 2569 | } /* end for */ |
| 2570 | |
| 2571 | /* if data and types extracted successfully... */ |
| 2572 | if( SQLITE_ROW == rc ){ |
| 2573 | /* call the supplied callback with the result row data */ |
| 2574 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2575 | rc = SQLITE_ABORT; |
| 2576 | }else{ |
| 2577 | rc = sqlite3_step(pStmt); |
| 2578 | } |
| 2579 | } |
| 2580 | } while( SQLITE_ROW == rc ); |
| 2581 | sqlite3_free(pData); |
| 2582 | } |
| 2583 | }else{ |
| 2584 | do{ |
| 2585 | rc = sqlite3_step(pStmt); |
| 2586 | } while( rc == SQLITE_ROW ); |
| 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2592 | ** Execute a statement or set of statements. Print |
| 2593 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2594 | ** set via the supplied callback. |
| 2595 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2596 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2597 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2598 | ** and callback data argument. |
| 2599 | */ |
| 2600 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2601 | sqlite3 *db, /* An open database */ |
| 2602 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2603 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2604 | /* (not the same as sqlite3_exec) */ |
| 2605 | ShellState *pArg, /* Pointer to ShellState */ |
| 2606 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2607 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2608 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2609 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2610 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2611 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2612 | |
| 2613 | if( pzErrMsg ){ |
| 2614 | *pzErrMsg = NULL; |
| 2615 | } |
| 2616 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2617 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2618 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2619 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2620 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2621 | if( pzErrMsg ){ |
| 2622 | *pzErrMsg = save_err_msg(db); |
| 2623 | } |
| 2624 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2625 | if( !pStmt ){ |
| 2626 | /* this happens for a comment or white-space */ |
| 2627 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2628 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2629 | continue; |
| 2630 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2631 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 2632 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2633 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2634 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2635 | /* save off the prepared statment handle and reset row count */ |
| 2636 | if( pArg ){ |
| 2637 | pArg->pStmt = pStmt; |
| 2638 | pArg->cnt = 0; |
| 2639 | } |
| 2640 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2641 | /* echo the sql statement if echo on */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2642 | if( pArg && pArg->echoOn ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2643 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 2644 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2645 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2646 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2647 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2648 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2649 | char *zEQP; |
| 2650 | disable_debug_trace_modes(); |
| 2651 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2652 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2653 | if( rc==SQLITE_OK ){ |
| 2654 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2655 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2656 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2657 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2658 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | sqlite3_finalize(pExplain); |
| 2662 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2663 | if( pArg->autoEQP>=2 ){ |
| 2664 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2665 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2666 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2667 | if( rc==SQLITE_OK ){ |
| 2668 | pArg->cMode = MODE_Explain; |
| 2669 | explain_data_prepare(pArg, pExplain); |
| 2670 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2671 | explain_data_delete(pArg); |
| 2672 | } |
| 2673 | sqlite3_finalize(pExplain); |
| 2674 | sqlite3_free(zEQP); |
| 2675 | } |
| 2676 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2679 | if( pArg ){ |
| 2680 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2681 | if( pArg->autoExplain |
| 2682 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2683 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2684 | ){ |
| 2685 | pArg->cMode = MODE_Explain; |
| 2686 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2687 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2688 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2689 | ** data required to add indents to the output.*/ |
| 2690 | if( pArg->cMode==MODE_Explain ){ |
| 2691 | explain_data_prepare(pArg, pStmt); |
| 2692 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2695 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2696 | explain_data_delete(pArg); |
| 2697 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2698 | /* print usage stats if stats on */ |
| 2699 | if( pArg && pArg->statsOn ){ |
| 2700 | display_stats(db, pArg, 0); |
| 2701 | } |
| 2702 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2703 | /* print loop-counters if required */ |
| 2704 | if( pArg && pArg->scanstatsOn ){ |
| 2705 | display_scanstats(db, pArg); |
| 2706 | } |
| 2707 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2708 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2709 | ** copy of the error message. Otherwise, set zSql to point to the |
| 2710 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2711 | rc2 = sqlite3_finalize(pStmt); |
| 2712 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2713 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2714 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2715 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2716 | }else if( pzErrMsg ){ |
| 2717 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2718 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2719 | |
| 2720 | /* clear saved stmt handle */ |
| 2721 | if( pArg ){ |
| 2722 | pArg->pStmt = NULL; |
| 2723 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2724 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2725 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2726 | |
| 2727 | return rc; |
| 2728 | } |
| 2729 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2730 | /* |
| 2731 | ** Release memory previously allocated by tableColumnList(). |
| 2732 | */ |
| 2733 | static void freeColumnList(char **azCol){ |
| 2734 | int i; |
| 2735 | for(i=1; azCol[i]; i++){ |
| 2736 | sqlite3_free(azCol[i]); |
| 2737 | } |
| 2738 | /* azCol[0] is a static string */ |
| 2739 | sqlite3_free(azCol); |
| 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | ** Return a list of pointers to strings which are the names of all |
| 2744 | ** columns in table zTab. The memory to hold the names is dynamically |
| 2745 | ** allocated and must be released by the caller using a subsequent call |
| 2746 | ** to freeColumnList(). |
| 2747 | ** |
| 2748 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 2749 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 2750 | ** name of the rowid column. |
| 2751 | ** |
| 2752 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 2753 | ** by an entry with azCol[i]==0. |
| 2754 | */ |
| 2755 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 2756 | char **azCol = 0; |
| 2757 | sqlite3_stmt *pStmt; |
| 2758 | char *zSql; |
| 2759 | int nCol = 0; |
| 2760 | int nAlloc = 0; |
| 2761 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 2762 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 2763 | int preserveRowid = p->preserveRowid; |
| 2764 | int rc; |
| 2765 | |
| 2766 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 2767 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2768 | sqlite3_free(zSql); |
| 2769 | if( rc ) return 0; |
| 2770 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2771 | if( nCol>=nAlloc-2 ){ |
| 2772 | nAlloc = nAlloc*2 + nCol + 10; |
| 2773 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 2774 | if( azCol==0 ){ |
| 2775 | raw_printf(stderr, "Error: out of memory\n"); |
| 2776 | exit(1); |
| 2777 | } |
| 2778 | } |
| 2779 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 2780 | if( sqlite3_column_int(pStmt, 5) ){ |
| 2781 | nPK++; |
| 2782 | if( nPK==1 |
| 2783 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 2784 | "INTEGER")==0 |
| 2785 | ){ |
| 2786 | isIPK = 1; |
| 2787 | }else{ |
| 2788 | isIPK = 0; |
| 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | sqlite3_finalize(pStmt); |
| 2793 | azCol[0] = 0; |
| 2794 | azCol[nCol+1] = 0; |
| 2795 | |
| 2796 | /* The decision of whether or not a rowid really needs to be preserved |
| 2797 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 2798 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 2799 | ** rowids on tables where the rowid is inaccessible because there are other |
| 2800 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 2801 | */ |
| 2802 | if( preserveRowid && isIPK ){ |
| 2803 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 2804 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 2805 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 2806 | ** ROWID aliases. To distinguish these cases, check to see if |
| 2807 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 2808 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 2809 | */ |
| 2810 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 2811 | " WHERE origin='pk'", zTab); |
| 2812 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2813 | sqlite3_free(zSql); |
| 2814 | if( rc ){ |
| 2815 | freeColumnList(azCol); |
| 2816 | return 0; |
| 2817 | } |
| 2818 | rc = sqlite3_step(pStmt); |
| 2819 | sqlite3_finalize(pStmt); |
| 2820 | preserveRowid = rc==SQLITE_ROW; |
| 2821 | } |
| 2822 | if( preserveRowid ){ |
| 2823 | /* Only preserve the rowid if we can find a name to use for the |
| 2824 | ** rowid */ |
| 2825 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 2826 | int i, j; |
| 2827 | for(j=0; j<3; j++){ |
| 2828 | for(i=1; i<=nCol; i++){ |
| 2829 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 2830 | } |
| 2831 | if( i>nCol ){ |
| 2832 | /* At this point, we know that azRowid[j] is not the name of any |
| 2833 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 2834 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 2835 | ** tables will fail this last check */ |
| 2836 | int rc; |
| 2837 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 2838 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 2839 | break; |
| 2840 | } |
| 2841 | } |
| 2842 | } |
| 2843 | return azCol; |
| 2844 | } |
| 2845 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2846 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2847 | ** This is a different callback routine used for dumping the database. |
| 2848 | ** Each row received by this callback consists of a table name, |
| 2849 | ** the table type ("index" or "table") and SQL to create the table. |
| 2850 | ** This routine should print text sufficient to recreate the table. |
| 2851 | */ |
| 2852 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2853 | int rc; |
| 2854 | const char *zTable; |
| 2855 | const char *zType; |
| 2856 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2857 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2858 | |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 2859 | UNUSED_PARAMETER(azCol); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2860 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2861 | zTable = azArg[0]; |
| 2862 | zType = azArg[1]; |
| 2863 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2864 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2865 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2866 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 2867 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2868 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2869 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 2870 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2871 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 2872 | char *zIns; |
| 2873 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2874 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2875 | p->writableSchema = 1; |
| 2876 | } |
| 2877 | zIns = sqlite3_mprintf( |
| 2878 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 2879 | "VALUES('table','%q','%q',0,'%q');", |
| 2880 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2881 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2882 | sqlite3_free(zIns); |
| 2883 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2884 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2885 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 2886 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2887 | |
| 2888 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2889 | ShellText sSelect; |
| 2890 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2891 | char **azCol; |
| 2892 | int i; |
| 2893 | char *savedDestTable; |
| 2894 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2895 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2896 | azCol = tableColumnList(p, zTable); |
| 2897 | if( azCol==0 ){ |
| 2898 | p->nErr++; |
| 2899 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 2902 | /* Always quote the table name, even if it appears to be pure ascii, |
| 2903 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2904 | initText(&sTable); |
| 2905 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2906 | /* If preserving the rowid, add a column list after the table name. |
| 2907 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 2908 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 2909 | */ |
| 2910 | if( azCol[0] ){ |
| 2911 | appendText(&sTable, "(", 0); |
| 2912 | appendText(&sTable, azCol[0], 0); |
| 2913 | for(i=1; azCol[i]; i++){ |
| 2914 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2915 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2916 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2917 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2918 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2919 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2920 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2921 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2922 | appendText(&sSelect, "SELECT ", 0); |
| 2923 | if( azCol[0] ){ |
| 2924 | appendText(&sSelect, azCol[0], 0); |
| 2925 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2926 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2927 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2928 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2929 | if( azCol[i+1] ){ |
| 2930 | appendText(&sSelect, ",", 0); |
| 2931 | } |
| 2932 | } |
| 2933 | freeColumnList(azCol); |
| 2934 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2935 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2936 | |
| 2937 | savedDestTable = p->zDestTable; |
| 2938 | savedMode = p->mode; |
| 2939 | p->zDestTable = sTable.z; |
| 2940 | p->mode = p->cMode = MODE_Insert; |
| 2941 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 2942 | p->zDestTable = savedDestTable; |
| 2943 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2944 | freeText(&sTable); |
| 2945 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2946 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2947 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2948 | return 0; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2952 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 2953 | ** the contents of the query are output as SQL statements. |
| 2954 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2955 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 2956 | ** "ORDER BY rowid DESC" to the end. |
| 2957 | */ |
| 2958 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2959 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2960 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2961 | ){ |
| 2962 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2963 | char *zErr = 0; |
| 2964 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2965 | if( rc==SQLITE_CORRUPT ){ |
| 2966 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2967 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2968 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2969 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2970 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2971 | sqlite3_free(zErr); |
| 2972 | zErr = 0; |
| 2973 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2974 | zQ2 = malloc( len+100 ); |
| 2975 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 2976 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2977 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 2978 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2979 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2980 | }else{ |
| 2981 | rc = SQLITE_CORRUPT; |
| 2982 | } |
| 2983 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2984 | free(zQ2); |
| 2985 | } |
| 2986 | return rc; |
| 2987 | } |
| 2988 | |
| 2989 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2990 | ** Text of a help message |
| 2991 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2992 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2993 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 2994 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2995 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2996 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2997 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2998 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 2999 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3000 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3001 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3002 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3003 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3004 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3005 | " If TABLE specified, only dump tables matching\n" |
| 3006 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3007 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3008 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3009 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3010 | ".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] | 3011 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3012 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3013 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3014 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3015 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3016 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3017 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3018 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3019 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3020 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3021 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3022 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3023 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3024 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 3025 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3026 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3027 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 3028 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3029 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 3030 | ".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] | 3031 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3032 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 3033 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3034 | " column Left-aligned columns. (See .width)\n" |
| 3035 | " html HTML <table> code\n" |
| 3036 | " insert SQL insert statements for TABLE\n" |
| 3037 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 3038 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 3039 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3040 | " tabs Tab-separated values\n" |
| 3041 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3042 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3043 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3044 | ".open ?--new? ?FILE? Close existing database and reopen FILE\n" |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 3045 | " The --new starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3046 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3047 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3048 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3049 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3050 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3051 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3052 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3053 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3054 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3055 | " Add --indent for pretty-printing\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3056 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3057 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3058 | #if defined(SQLITE_ENABLE_SESSION) |
| 3059 | ".session CMD ... Create or control sessions\n" |
| 3060 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3061 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3062 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3063 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3064 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3065 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3066 | ".tables ?TABLE? List names of tables\n" |
| 3067 | " If TABLE specified, only list tables matching\n" |
| 3068 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3069 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 3070 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3071 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3072 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 3073 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 3074 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3075 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 3076 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3077 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3078 | ; |
| 3079 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3080 | #if defined(SQLITE_ENABLE_SESSION) |
| 3081 | /* |
| 3082 | ** Print help information for the ".sessions" command |
| 3083 | */ |
| 3084 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 3085 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3086 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3087 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3088 | "Subcommands:\n" |
| 3089 | " attach TABLE Attach TABLE\n" |
| 3090 | " changeset FILE Write a changeset into FILE\n" |
| 3091 | " close Close one session\n" |
| 3092 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3093 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3094 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3095 | " isempty Query whether the session is empty\n" |
| 3096 | " list List currently open session names\n" |
| 3097 | " open DB NAME Open a new session on DB\n" |
| 3098 | " patchset FILE Write a patchset into FILE\n" |
| 3099 | ); |
| 3100 | } |
| 3101 | #endif |
| 3102 | |
| 3103 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3104 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3105 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3106 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3107 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3108 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 3109 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3110 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3111 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3112 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3113 | ** read. |
| 3114 | ** |
| 3115 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3116 | ** from the file before the buffer is returned. This byte is not included in |
| 3117 | ** the final value of (*pnByte), if applicable. |
| 3118 | ** |
| 3119 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3120 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3121 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3122 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3123 | FILE *in = fopen(zName, "rb"); |
| 3124 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3125 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3126 | char *pBuf; |
| 3127 | if( in==0 ) return 0; |
| 3128 | fseek(in, 0, SEEK_END); |
| 3129 | nIn = ftell(in); |
| 3130 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3131 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3132 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3133 | nRead = fread(pBuf, nIn, 1, in); |
| 3134 | fclose(in); |
| 3135 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3136 | sqlite3_free(pBuf); |
| 3137 | return 0; |
| 3138 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3139 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3140 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3141 | return pBuf; |
| 3142 | } |
| 3143 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3144 | /* |
| 3145 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 3146 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 3147 | ** if the file does not exist or is unreadable. |
| 3148 | */ |
| 3149 | static void readfileFunc( |
| 3150 | sqlite3_context *context, |
| 3151 | int argc, |
| 3152 | sqlite3_value **argv |
| 3153 | ){ |
| 3154 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3155 | void *pBuf; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3156 | int nBuf; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3157 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3158 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3159 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 3160 | if( zName==0 ) return; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3161 | pBuf = readFile(zName, &nBuf); |
| 3162 | if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* |
| 3166 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3167 | ** is written into file X. The number of bytes written is returned. Or |
| 3168 | ** NULL is returned if something goes wrong, such as being unable to open |
| 3169 | ** file X for writing. |
| 3170 | */ |
| 3171 | static void writefileFunc( |
| 3172 | sqlite3_context *context, |
| 3173 | int argc, |
| 3174 | sqlite3_value **argv |
| 3175 | ){ |
| 3176 | FILE *out; |
| 3177 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3178 | sqlite3_int64 rc; |
| 3179 | const char *zFile; |
| 3180 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3181 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3182 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3183 | if( zFile==0 ) return; |
| 3184 | out = fopen(zFile, "wb"); |
| 3185 | if( out==0 ) return; |
| 3186 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 3187 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3188 | rc = 0; |
| 3189 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 3190 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3191 | } |
| 3192 | fclose(out); |
| 3193 | sqlite3_result_int64(context, rc); |
| 3194 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3195 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3196 | #if defined(SQLITE_ENABLE_SESSION) |
| 3197 | /* |
| 3198 | ** Close a single OpenSession object and release all of its associated |
| 3199 | ** resources. |
| 3200 | */ |
| 3201 | static void session_close(OpenSession *pSession){ |
| 3202 | int i; |
| 3203 | sqlite3session_delete(pSession->p); |
| 3204 | sqlite3_free(pSession->zName); |
| 3205 | for(i=0; i<pSession->nFilter; i++){ |
| 3206 | sqlite3_free(pSession->azFilter[i]); |
| 3207 | } |
| 3208 | sqlite3_free(pSession->azFilter); |
| 3209 | memset(pSession, 0, sizeof(OpenSession)); |
| 3210 | } |
| 3211 | #endif |
| 3212 | |
| 3213 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3214 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3215 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3216 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3217 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3218 | int i; |
| 3219 | for(i=0; i<p->nSession; i++){ |
| 3220 | session_close(&p->aSession[i]); |
| 3221 | } |
| 3222 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3223 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3224 | #else |
| 3225 | # define session_close_all(X) |
| 3226 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3227 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3228 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 3229 | ** Implementation of the xFilter function for an open session. Omit |
| 3230 | ** any tables named by ".session filter" but let all other table through. |
| 3231 | */ |
| 3232 | #if defined(SQLITE_ENABLE_SESSION) |
| 3233 | static int session_filter(void *pCtx, const char *zTab){ |
| 3234 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3235 | int i; |
| 3236 | for(i=0; i<pSession->nFilter; i++){ |
| 3237 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3238 | } |
| 3239 | return 1; |
| 3240 | } |
| 3241 | #endif |
| 3242 | |
| 3243 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3244 | ** Make sure the database is open. If it is not, then open it. If |
| 3245 | ** the database fails to open, print an error message and exit. |
| 3246 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3247 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3248 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 3249 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3250 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3251 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3252 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3253 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3254 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3255 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3256 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3257 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 3258 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3259 | sqlite3_enable_load_extension(p->db, 1); |
| 3260 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3261 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3262 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3263 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3264 | writefileFunc, 0, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3265 | sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3266 | sha3Func, 0, 0); |
| 3267 | sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3268 | sha3Func, 0, 0); |
| 3269 | sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3270 | sha3QueryFunc, 0, 0); |
| 3271 | sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3272 | sha3QueryFunc, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3277 | ** Do C-language style dequoting. |
| 3278 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3279 | ** \a -> alarm |
| 3280 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3281 | ** \t -> tab |
| 3282 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3283 | ** \v -> vertical tab |
| 3284 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3285 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3286 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3287 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3288 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3289 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3290 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3291 | */ |
| 3292 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3293 | int i, j; |
| 3294 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3295 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3296 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 3297 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3298 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3299 | if( c=='a' ){ |
| 3300 | c = '\a'; |
| 3301 | }else if( c=='b' ){ |
| 3302 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3303 | }else if( c=='t' ){ |
| 3304 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3305 | }else if( c=='n' ){ |
| 3306 | c = '\n'; |
| 3307 | }else if( c=='v' ){ |
| 3308 | c = '\v'; |
| 3309 | }else if( c=='f' ){ |
| 3310 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3311 | }else if( c=='r' ){ |
| 3312 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3313 | }else if( c=='"' ){ |
| 3314 | c = '"'; |
| 3315 | }else if( c=='\'' ){ |
| 3316 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3317 | }else if( c=='\\' ){ |
| 3318 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3319 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 3320 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3321 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3322 | i++; |
| 3323 | c = (c<<3) + z[i] - '0'; |
| 3324 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3325 | i++; |
| 3326 | c = (c<<3) + z[i] - '0'; |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | z[j] = c; |
| 3332 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3333 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3337 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 3338 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3339 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3340 | static int hexDigitValue(char c){ |
| 3341 | if( c>='0' && c<='9' ) return c - '0'; |
| 3342 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 3343 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 3344 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3348 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 3349 | */ |
| 3350 | static sqlite3_int64 integerValue(const char *zArg){ |
| 3351 | sqlite3_int64 v = 0; |
| 3352 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 3353 | { "KiB", 1024 }, |
| 3354 | { "MiB", 1024*1024 }, |
| 3355 | { "GiB", 1024*1024*1024 }, |
| 3356 | { "KB", 1000 }, |
| 3357 | { "MB", 1000000 }, |
| 3358 | { "GB", 1000000000 }, |
| 3359 | { "K", 1000 }, |
| 3360 | { "M", 1000000 }, |
| 3361 | { "G", 1000000000 }, |
| 3362 | }; |
| 3363 | int i; |
| 3364 | int isNeg = 0; |
| 3365 | if( zArg[0]=='-' ){ |
| 3366 | isNeg = 1; |
| 3367 | zArg++; |
| 3368 | }else if( zArg[0]=='+' ){ |
| 3369 | zArg++; |
| 3370 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3371 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3372 | int x; |
| 3373 | zArg += 2; |
| 3374 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 3375 | v = (v<<4) + x; |
| 3376 | zArg++; |
| 3377 | } |
| 3378 | }else{ |
| 3379 | while( IsDigit(zArg[0]) ){ |
| 3380 | v = v*10 + zArg[0] - '0'; |
| 3381 | zArg++; |
| 3382 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3383 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 3384 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3385 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 3386 | v *= aMult[i].iMult; |
| 3387 | break; |
| 3388 | } |
| 3389 | } |
| 3390 | return isNeg? -v : v; |
| 3391 | } |
| 3392 | |
| 3393 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3394 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3395 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3396 | */ |
| 3397 | static int booleanValue(char *zArg){ |
| 3398 | int i; |
| 3399 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3400 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3401 | }else{ |
| 3402 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3403 | } |
| 3404 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3405 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3406 | return 1; |
| 3407 | } |
| 3408 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3409 | return 0; |
| 3410 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3411 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3412 | zArg); |
| 3413 | return 0; |
| 3414 | } |
| 3415 | |
| 3416 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3417 | ** Close an output file, assuming it is not stderr or stdout |
| 3418 | */ |
| 3419 | static void output_file_close(FILE *f){ |
| 3420 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3425 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3426 | ** filename is "off". |
| 3427 | */ |
| 3428 | static FILE *output_file_open(const char *zFile){ |
| 3429 | FILE *f; |
| 3430 | if( strcmp(zFile,"stdout")==0 ){ |
| 3431 | f = stdout; |
| 3432 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3433 | f = stderr; |
| 3434 | }else if( strcmp(zFile, "off")==0 ){ |
| 3435 | f = 0; |
| 3436 | }else{ |
| 3437 | f = fopen(zFile, "wb"); |
| 3438 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3439 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } |
| 3442 | return f; |
| 3443 | } |
| 3444 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 3445 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3446 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3447 | /* |
| 3448 | ** A routine for handling output from sqlite3_trace(). |
| 3449 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3450 | static int sql_trace_callback( |
| 3451 | unsigned mType, |
| 3452 | void *pArg, |
| 3453 | void *pP, |
| 3454 | void *pX |
| 3455 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3456 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 3457 | UNUSED_PARAMETER(mType); |
| 3458 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3459 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3460 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3461 | int i = (int)strlen(z); |
| 3462 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3463 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3464 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3465 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3466 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3467 | #endif |
| 3468 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3469 | |
| 3470 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3471 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3472 | ** a useful spot to set a debugger breakpoint. |
| 3473 | */ |
| 3474 | static void test_breakpoint(void){ |
| 3475 | static int nCall = 0; |
| 3476 | nCall++; |
| 3477 | } |
| 3478 | |
| 3479 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3480 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3481 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3482 | typedef struct ImportCtx ImportCtx; |
| 3483 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3484 | const char *zFile; /* Name of the input file */ |
| 3485 | FILE *in; /* Read the CSV text from this input stream */ |
| 3486 | char *z; /* Accumulated text for a field */ |
| 3487 | int n; /* Number of bytes in z */ |
| 3488 | int nAlloc; /* Space allocated for z[] */ |
| 3489 | int nLine; /* Current line number */ |
| 3490 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3491 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3492 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3493 | }; |
| 3494 | |
| 3495 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3496 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3497 | if( p->n+1>=p->nAlloc ){ |
| 3498 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3499 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3500 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3501 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3502 | exit(1); |
| 3503 | } |
| 3504 | } |
| 3505 | p->z[p->n++] = (char)c; |
| 3506 | } |
| 3507 | |
| 3508 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3509 | ** with the option of having a separator other than ",". |
| 3510 | ** |
| 3511 | ** + Input comes from p->in. |
| 3512 | ** + 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] | 3513 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3514 | ** + Use p->cSep as the column separator. The default is ",". |
| 3515 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3516 | ** + Keep track of the line number in p->nLine. |
| 3517 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3518 | ** EOF on end-of-file. |
| 3519 | ** + Report syntax errors on stderr |
| 3520 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3521 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3522 | int c; |
| 3523 | int cSep = p->cColSep; |
| 3524 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3525 | p->n = 0; |
| 3526 | c = fgetc(p->in); |
| 3527 | if( c==EOF || seenInterrupt ){ |
| 3528 | p->cTerm = EOF; |
| 3529 | return 0; |
| 3530 | } |
| 3531 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3532 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3533 | int startLine = p->nLine; |
| 3534 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3535 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3536 | while( 1 ){ |
| 3537 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3538 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3539 | if( c==cQuote ){ |
| 3540 | if( pc==cQuote ){ |
| 3541 | pc = 0; |
| 3542 | continue; |
| 3543 | } |
| 3544 | } |
| 3545 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3546 | || (c==rSep && pc==cQuote) |
| 3547 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3548 | || (c==EOF && pc==cQuote) |
| 3549 | ){ |
| 3550 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3551 | p->cTerm = c; |
| 3552 | break; |
| 3553 | } |
| 3554 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3555 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3556 | p->zFile, p->nLine, cQuote); |
| 3557 | } |
| 3558 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3559 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3560 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3561 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3562 | break; |
| 3563 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3564 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3565 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3566 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3567 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3568 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3569 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3570 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3571 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3572 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3573 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3574 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 3575 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3576 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3577 | p->cTerm = c; |
| 3578 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 3579 | if( p->z ) p->z[p->n] = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3580 | return p->z; |
| 3581 | } |
| 3582 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3583 | /* Read a single field of ASCII delimited text. |
| 3584 | ** |
| 3585 | ** + Input comes from p->in. |
| 3586 | ** + 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] | 3587 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3588 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3589 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3590 | ** + Keep track of the row number in p->nLine. |
| 3591 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3592 | ** EOF on end-of-file. |
| 3593 | ** + Report syntax errors on stderr |
| 3594 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3595 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3596 | int c; |
| 3597 | int cSep = p->cColSep; |
| 3598 | int rSep = p->cRowSep; |
| 3599 | p->n = 0; |
| 3600 | c = fgetc(p->in); |
| 3601 | if( c==EOF || seenInterrupt ){ |
| 3602 | p->cTerm = EOF; |
| 3603 | return 0; |
| 3604 | } |
| 3605 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3606 | import_append_char(p, c); |
| 3607 | c = fgetc(p->in); |
| 3608 | } |
| 3609 | if( c==rSep ){ |
| 3610 | p->nLine++; |
| 3611 | } |
| 3612 | p->cTerm = c; |
| 3613 | if( p->z ) p->z[p->n] = 0; |
| 3614 | return p->z; |
| 3615 | } |
| 3616 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3617 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3618 | ** Try to transfer data for table zTable. If an error is seen while |
| 3619 | ** moving forward, try to go backwards. The backwards movement won't |
| 3620 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3621 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3622 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3623 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3624 | sqlite3 *newDb, |
| 3625 | const char *zTable |
| 3626 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3627 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3628 | sqlite3_stmt *pInsert = 0; |
| 3629 | char *zQuery = 0; |
| 3630 | char *zInsert = 0; |
| 3631 | int rc; |
| 3632 | int i, j, n; |
| 3633 | int nTable = (int)strlen(zTable); |
| 3634 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3635 | int cnt = 0; |
| 3636 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3637 | |
| 3638 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3639 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3640 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3641 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3642 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3643 | zQuery); |
| 3644 | goto end_data_xfer; |
| 3645 | } |
| 3646 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3647 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3648 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3649 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3650 | goto end_data_xfer; |
| 3651 | } |
| 3652 | sqlite3_snprintf(200+nTable,zInsert, |
| 3653 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 3654 | i = (int)strlen(zInsert); |
| 3655 | for(j=1; j<n; j++){ |
| 3656 | memcpy(zInsert+i, ",?", 2); |
| 3657 | i += 2; |
| 3658 | } |
| 3659 | memcpy(zInsert+i, ");", 3); |
| 3660 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 3661 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3662 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3663 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 3664 | zQuery); |
| 3665 | goto end_data_xfer; |
| 3666 | } |
| 3667 | for(k=0; k<2; k++){ |
| 3668 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3669 | for(i=0; i<n; i++){ |
| 3670 | switch( sqlite3_column_type(pQuery, i) ){ |
| 3671 | case SQLITE_NULL: { |
| 3672 | sqlite3_bind_null(pInsert, i+1); |
| 3673 | break; |
| 3674 | } |
| 3675 | case SQLITE_INTEGER: { |
| 3676 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 3677 | break; |
| 3678 | } |
| 3679 | case SQLITE_FLOAT: { |
| 3680 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 3681 | break; |
| 3682 | } |
| 3683 | case SQLITE_TEXT: { |
| 3684 | sqlite3_bind_text(pInsert, i+1, |
| 3685 | (const char*)sqlite3_column_text(pQuery,i), |
| 3686 | -1, SQLITE_STATIC); |
| 3687 | break; |
| 3688 | } |
| 3689 | case SQLITE_BLOB: { |
| 3690 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 3691 | sqlite3_column_bytes(pQuery,i), |
| 3692 | SQLITE_STATIC); |
| 3693 | break; |
| 3694 | } |
| 3695 | } |
| 3696 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3697 | rc = sqlite3_step(pInsert); |
| 3698 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3699 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3700 | sqlite3_errmsg(newDb)); |
| 3701 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3702 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3703 | cnt++; |
| 3704 | if( (cnt%spinRate)==0 ){ |
| 3705 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 3706 | fflush(stdout); |
| 3707 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3708 | } /* End while */ |
| 3709 | if( rc==SQLITE_DONE ) break; |
| 3710 | sqlite3_finalize(pQuery); |
| 3711 | sqlite3_free(zQuery); |
| 3712 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 3713 | zTable); |
| 3714 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3715 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3716 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3717 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3718 | } |
| 3719 | } /* End for(k=0...) */ |
| 3720 | |
| 3721 | end_data_xfer: |
| 3722 | sqlite3_finalize(pQuery); |
| 3723 | sqlite3_finalize(pInsert); |
| 3724 | sqlite3_free(zQuery); |
| 3725 | sqlite3_free(zInsert); |
| 3726 | } |
| 3727 | |
| 3728 | |
| 3729 | /* |
| 3730 | ** Try to transfer all rows of the schema that match zWhere. For |
| 3731 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3732 | ** If an error is encountered while moving forward through the |
| 3733 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3734 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3735 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3736 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3737 | sqlite3 *newDb, |
| 3738 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3739 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3740 | ){ |
| 3741 | sqlite3_stmt *pQuery = 0; |
| 3742 | char *zQuery = 0; |
| 3743 | int rc; |
| 3744 | const unsigned char *zName; |
| 3745 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3746 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3747 | |
| 3748 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 3749 | " WHERE %s", zWhere); |
| 3750 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3751 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3752 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3753 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3754 | zQuery); |
| 3755 | goto end_schema_xfer; |
| 3756 | } |
| 3757 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3758 | zName = sqlite3_column_text(pQuery, 0); |
| 3759 | zSql = sqlite3_column_text(pQuery, 1); |
| 3760 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3761 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 3762 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3763 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3764 | sqlite3_free(zErrMsg); |
| 3765 | zErrMsg = 0; |
| 3766 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3767 | if( xForEach ){ |
| 3768 | xForEach(p, newDb, (const char*)zName); |
| 3769 | } |
| 3770 | printf("done\n"); |
| 3771 | } |
| 3772 | if( rc!=SQLITE_DONE ){ |
| 3773 | sqlite3_finalize(pQuery); |
| 3774 | sqlite3_free(zQuery); |
| 3775 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 3776 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 3777 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3778 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3779 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3780 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3781 | zQuery); |
| 3782 | goto end_schema_xfer; |
| 3783 | } |
| 3784 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3785 | zName = sqlite3_column_text(pQuery, 0); |
| 3786 | zSql = sqlite3_column_text(pQuery, 1); |
| 3787 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3788 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 3789 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3790 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3791 | sqlite3_free(zErrMsg); |
| 3792 | zErrMsg = 0; |
| 3793 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3794 | if( xForEach ){ |
| 3795 | xForEach(p, newDb, (const char*)zName); |
| 3796 | } |
| 3797 | printf("done\n"); |
| 3798 | } |
| 3799 | } |
| 3800 | end_schema_xfer: |
| 3801 | sqlite3_finalize(pQuery); |
| 3802 | sqlite3_free(zQuery); |
| 3803 | } |
| 3804 | |
| 3805 | /* |
| 3806 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 3807 | ** as possible out of the main database (which might be corrupt) and write it |
| 3808 | ** into zNewDb. |
| 3809 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3810 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3811 | int rc; |
| 3812 | sqlite3 *newDb = 0; |
| 3813 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3814 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3815 | return; |
| 3816 | } |
| 3817 | rc = sqlite3_open(zNewDb, &newDb); |
| 3818 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3819 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3820 | sqlite3_errmsg(newDb)); |
| 3821 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 3822 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3823 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3824 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 3825 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3826 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 3827 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3828 | } |
| 3829 | sqlite3_close(newDb); |
| 3830 | } |
| 3831 | |
| 3832 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3833 | ** Change the output file back to stdout |
| 3834 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3835 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3836 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3837 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3838 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3839 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3840 | }else{ |
| 3841 | output_file_close(p->out); |
| 3842 | } |
| 3843 | p->outfile[0] = 0; |
| 3844 | p->out = stdout; |
| 3845 | } |
| 3846 | |
| 3847 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3848 | ** Run an SQL command and return the single integer result. |
| 3849 | */ |
| 3850 | static int db_int(ShellState *p, const char *zSql){ |
| 3851 | sqlite3_stmt *pStmt; |
| 3852 | int res = 0; |
| 3853 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3854 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3855 | res = sqlite3_column_int(pStmt,0); |
| 3856 | } |
| 3857 | sqlite3_finalize(pStmt); |
| 3858 | return res; |
| 3859 | } |
| 3860 | |
| 3861 | /* |
| 3862 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 3863 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3864 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3865 | return (a[0]<<8) + a[1]; |
| 3866 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3867 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3868 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 3869 | } |
| 3870 | |
| 3871 | /* |
| 3872 | ** Implementation of the ".info" command. |
| 3873 | ** |
| 3874 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 3875 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3876 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3877 | static const struct { const char *zName; int ofst; } aField[] = { |
| 3878 | { "file change counter:", 24 }, |
| 3879 | { "database page count:", 28 }, |
| 3880 | { "freelist page count:", 36 }, |
| 3881 | { "schema cookie:", 40 }, |
| 3882 | { "schema format:", 44 }, |
| 3883 | { "default cache size:", 48 }, |
| 3884 | { "autovacuum top root:", 52 }, |
| 3885 | { "incremental vacuum:", 64 }, |
| 3886 | { "text encoding:", 56 }, |
| 3887 | { "user version:", 60 }, |
| 3888 | { "application id:", 68 }, |
| 3889 | { "software version:", 96 }, |
| 3890 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3891 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 3892 | { "number of tables:", |
| 3893 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 3894 | { "number of indexes:", |
| 3895 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 3896 | { "number of triggers:", |
| 3897 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 3898 | { "number of views:", |
| 3899 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 3900 | { "schema size:", |
| 3901 | "SELECT total(length(sql)) FROM %s" }, |
| 3902 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 3903 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3904 | int i; |
| 3905 | char *zSchemaTab; |
| 3906 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 3907 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3908 | open_db(p, 0); |
| 3909 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3910 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3911 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 3912 | return 1; |
| 3913 | } |
| 3914 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 3915 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3916 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3917 | return 1; |
| 3918 | } |
| 3919 | i = get2byteInt(aHdr+16); |
| 3920 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3921 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 3922 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 3923 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 3924 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3925 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3926 | int ofst = aField[i].ofst; |
| 3927 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3928 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3929 | switch( ofst ){ |
| 3930 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3931 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 3932 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 3933 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3936 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3937 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3938 | if( zDb==0 ){ |
| 3939 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 3940 | }else if( strcmp(zDb,"temp")==0 ){ |
| 3941 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 3942 | }else{ |
| 3943 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 3944 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3945 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3946 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 3947 | int val = db_int(p, zSql); |
| 3948 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3949 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3950 | } |
| 3951 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3952 | return 0; |
| 3953 | } |
| 3954 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3955 | /* |
| 3956 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 3957 | */ |
| 3958 | static int shellDatabaseError(sqlite3 *db){ |
| 3959 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3960 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3961 | return 1; |
| 3962 | } |
| 3963 | |
| 3964 | /* |
| 3965 | ** Print an out-of-memory message to stderr and return 1. |
| 3966 | */ |
| 3967 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3968 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3969 | return 1; |
| 3970 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3971 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3972 | /* |
| 3973 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 3974 | ** if they match and FALSE (0) if they do not match. |
| 3975 | ** |
| 3976 | ** Globbing rules: |
| 3977 | ** |
| 3978 | ** '*' Matches any sequence of zero or more characters. |
| 3979 | ** |
| 3980 | ** '?' Matches exactly one character. |
| 3981 | ** |
| 3982 | ** [...] Matches one character from the enclosed list of |
| 3983 | ** characters. |
| 3984 | ** |
| 3985 | ** [^...] Matches one character not in the enclosed list. |
| 3986 | ** |
| 3987 | ** '#' Matches any sequence of one or more digits with an |
| 3988 | ** optional + or - sign in front |
| 3989 | ** |
| 3990 | ** ' ' Any span of whitespace matches any other span of |
| 3991 | ** whitespace. |
| 3992 | ** |
| 3993 | ** Extra whitespace at the end of z[] is ignored. |
| 3994 | */ |
| 3995 | static int testcase_glob(const char *zGlob, const char *z){ |
| 3996 | int c, c2; |
| 3997 | int invert; |
| 3998 | int seen; |
| 3999 | |
| 4000 | while( (c = (*(zGlob++)))!=0 ){ |
| 4001 | if( IsSpace(c) ){ |
| 4002 | if( !IsSpace(*z) ) return 0; |
| 4003 | while( IsSpace(*zGlob) ) zGlob++; |
| 4004 | while( IsSpace(*z) ) z++; |
| 4005 | }else if( c=='*' ){ |
| 4006 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4007 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4008 | } |
| 4009 | if( c==0 ){ |
| 4010 | return 1; |
| 4011 | }else if( c=='[' ){ |
| 4012 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4013 | z++; |
| 4014 | } |
| 4015 | return (*z)!=0; |
| 4016 | } |
| 4017 | while( (c2 = (*(z++)))!=0 ){ |
| 4018 | while( c2!=c ){ |
| 4019 | c2 = *(z++); |
| 4020 | if( c2==0 ) return 0; |
| 4021 | } |
| 4022 | if( testcase_glob(zGlob,z) ) return 1; |
| 4023 | } |
| 4024 | return 0; |
| 4025 | }else if( c=='?' ){ |
| 4026 | if( (*(z++))==0 ) return 0; |
| 4027 | }else if( c=='[' ){ |
| 4028 | int prior_c = 0; |
| 4029 | seen = 0; |
| 4030 | invert = 0; |
| 4031 | c = *(z++); |
| 4032 | if( c==0 ) return 0; |
| 4033 | c2 = *(zGlob++); |
| 4034 | if( c2=='^' ){ |
| 4035 | invert = 1; |
| 4036 | c2 = *(zGlob++); |
| 4037 | } |
| 4038 | if( c2==']' ){ |
| 4039 | if( c==']' ) seen = 1; |
| 4040 | c2 = *(zGlob++); |
| 4041 | } |
| 4042 | while( c2 && c2!=']' ){ |
| 4043 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4044 | c2 = *(zGlob++); |
| 4045 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4046 | prior_c = 0; |
| 4047 | }else{ |
| 4048 | if( c==c2 ){ |
| 4049 | seen = 1; |
| 4050 | } |
| 4051 | prior_c = c2; |
| 4052 | } |
| 4053 | c2 = *(zGlob++); |
| 4054 | } |
| 4055 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4056 | }else if( c=='#' ){ |
| 4057 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4058 | if( !IsDigit(z[0]) ) return 0; |
| 4059 | z++; |
| 4060 | while( IsDigit(z[0]) ){ z++; } |
| 4061 | }else{ |
| 4062 | if( c!=(*(z++)) ) return 0; |
| 4063 | } |
| 4064 | } |
| 4065 | while( IsSpace(*z) ){ z++; } |
| 4066 | return *z==0; |
| 4067 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4068 | |
| 4069 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4070 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4071 | ** Compare the string as a command-line option with either one or two |
| 4072 | ** initial "-" characters. |
| 4073 | */ |
| 4074 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4075 | if( zStr[0]!='-' ) return 0; |
| 4076 | zStr++; |
| 4077 | if( zStr[0]=='-' ) zStr++; |
| 4078 | return strcmp(zStr, zOpt)==0; |
| 4079 | } |
| 4080 | |
| 4081 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4082 | ** Delete a file. |
| 4083 | */ |
| 4084 | int shellDeleteFile(const char *zFilename){ |
| 4085 | int rc; |
| 4086 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4087 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4088 | rc = _wunlink(z); |
| 4089 | sqlite3_free(z); |
| 4090 | #else |
| 4091 | rc = unlink(zFilename); |
| 4092 | #endif |
| 4093 | return rc; |
| 4094 | } |
| 4095 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4096 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4097 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4098 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4099 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4100 | ** called with four arguments - the parent table name, the parent column name, |
| 4101 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4102 | ** |
| 4103 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4104 | ** |
| 4105 | ** If either of the named tables or columns do not exist, this function |
| 4106 | ** returns an empty string. An empty string is also returned if both tables |
| 4107 | ** and columns exist but have the same default collation sequence. Or, |
| 4108 | ** if both exist but the default collation sequences are different, this |
| 4109 | ** function returns the string " COLLATE <parent-collation>", where |
| 4110 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4111 | */ |
| 4112 | static void shellFkeyCollateClause( |
| 4113 | sqlite3_context *pCtx, |
| 4114 | int nVal, |
| 4115 | sqlite3_value **apVal |
| 4116 | ){ |
| 4117 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4118 | const char *zParent; |
| 4119 | const char *zParentCol; |
| 4120 | const char *zParentSeq; |
| 4121 | const char *zChild; |
| 4122 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4123 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4124 | int rc; |
| 4125 | |
| 4126 | assert( nVal==4 ); |
| 4127 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4128 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4129 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4130 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4131 | |
| 4132 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4133 | rc = sqlite3_table_column_metadata( |
| 4134 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4135 | ); |
| 4136 | if( rc==SQLITE_OK ){ |
| 4137 | rc = sqlite3_table_column_metadata( |
| 4138 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4139 | ); |
| 4140 | } |
| 4141 | |
| 4142 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4143 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4144 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4145 | sqlite3_free(z); |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | |
| 4150 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4151 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4152 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4153 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4154 | ShellState *pState, /* Current shell tool state */ |
| 4155 | char **azArg, /* Array of arguments passed to dot command */ |
| 4156 | int nArg /* Number of entries in azArg[] */ |
| 4157 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4158 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4159 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4160 | int bVerbose = 0; /* If -verbose is present */ |
| 4161 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4162 | int i; /* To iterate through azArg[] */ |
| 4163 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4164 | int rc; /* Return code */ |
| 4165 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4166 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4167 | /* |
| 4168 | ** This SELECT statement returns one row for each foreign key constraint |
| 4169 | ** in the schema of the main database. The column values are: |
| 4170 | ** |
| 4171 | ** 0. The text of an SQL statement similar to: |
| 4172 | ** |
| 4173 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 4174 | ** |
| 4175 | ** This is the same SELECT that the foreign keys implementation needs |
| 4176 | ** to run internally on child tables. If there is an index that can |
| 4177 | ** be used to optimize this query, then it can also be used by the FK |
| 4178 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4179 | ** table. |
| 4180 | ** |
| 4181 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4182 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4183 | ** contains an index that can be used to optimize the query. |
| 4184 | ** |
| 4185 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4186 | ** |
| 4187 | ** "child_table(child_key1, child_key2)" |
| 4188 | ** |
| 4189 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4190 | ** |
| 4191 | ** "parent_table(parent_key1, parent_key2)" |
| 4192 | ** |
| 4193 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4194 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4195 | ** |
| 4196 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4197 | ** |
| 4198 | ** 5. The name of the parent table. |
| 4199 | ** |
| 4200 | ** These six values are used by the C logic below to generate the report. |
| 4201 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4202 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4203 | "SELECT " |
| 4204 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 4205 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
| 4206 | " || fkey_collate_clause(f.[table], f.[to], s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4207 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4208 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4209 | " || group_concat('*=?', ' AND ') || ')'" |
| 4210 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4211 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4212 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4213 | " f.[table] || '(' || group_concat(COALESCE(f.[to], " |
| 4214 | " (SELECT name FROM pragma_table_info(f.[table]) WHERE pk=seq+1)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4215 | " )) || ')'" |
| 4216 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4217 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4218 | " || ' ON ' || quote(s.name) || '('" |
| 4219 | " || group_concat(quote(f.[from]) ||" |
| 4220 | " fkey_collate_clause(f.[table], f.[to], s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4221 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4222 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4223 | " f.[table] " |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4224 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4225 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
| 4226 | "GROUP BY s.name, f.id " |
| 4227 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4228 | ; |
| 4229 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4230 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 4231 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4232 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4233 | bVerbose = 1; |
| 4234 | } |
| 4235 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4236 | bGroupByParent = 1; |
| 4237 | zIndent = " "; |
| 4238 | } |
| 4239 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4240 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4241 | azArg[0], azArg[1] |
| 4242 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4243 | return SQLITE_ERROR; |
| 4244 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4245 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4246 | |
| 4247 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4248 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4249 | 0, shellFkeyCollateClause, 0, 0 |
| 4250 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4251 | |
| 4252 | |
| 4253 | if( rc==SQLITE_OK ){ |
| 4254 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4255 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4256 | if( rc==SQLITE_OK ){ |
| 4257 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4258 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4259 | |
| 4260 | if( rc==SQLITE_OK ){ |
| 4261 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4262 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4263 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4264 | int res = -1; |
| 4265 | sqlite3_stmt *pExplain = 0; |
| 4266 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4267 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4268 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4269 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4270 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4271 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4272 | |
| 4273 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4274 | if( rc!=SQLITE_OK ) break; |
| 4275 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4276 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
| 4277 | res = (0==sqlite3_strglob(zGlob, zPlan)); |
| 4278 | } |
| 4279 | rc = sqlite3_finalize(pExplain); |
| 4280 | if( rc!=SQLITE_OK ) break; |
| 4281 | |
| 4282 | if( res<0 ){ |
| 4283 | raw_printf(stderr, "Error: internal error"); |
| 4284 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4285 | }else{ |
| 4286 | if( bGroupByParent |
| 4287 | && (bVerbose || res==0) |
| 4288 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 4289 | ){ |
| 4290 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4291 | sqlite3_free(zPrev); |
| 4292 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4293 | } |
| 4294 | |
| 4295 | if( res==0 ){ |
| 4296 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4297 | }else if( bVerbose ){ |
| 4298 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 4299 | zIndent, zFrom, zTarget |
| 4300 | ); |
| 4301 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4302 | } |
| 4303 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4304 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4305 | |
| 4306 | if( rc!=SQLITE_OK ){ |
| 4307 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4308 | } |
| 4309 | |
| 4310 | rc2 = sqlite3_finalize(pSql); |
| 4311 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4312 | rc = rc2; |
| 4313 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4314 | } |
| 4315 | }else{ |
| 4316 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4317 | } |
| 4318 | |
| 4319 | return rc; |
| 4320 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4321 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4322 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4323 | ** Implementation of ".lint" dot command. |
| 4324 | */ |
| 4325 | static int lintDotCommand( |
| 4326 | ShellState *pState, /* Current shell tool state */ |
| 4327 | char **azArg, /* Array of arguments passed to dot command */ |
| 4328 | int nArg /* Number of entries in azArg[] */ |
| 4329 | ){ |
| 4330 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4331 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4332 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4333 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4334 | |
| 4335 | usage: |
| 4336 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4337 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4338 | raw_printf(stderr, " fkey-indexes\n"); |
| 4339 | return SQLITE_ERROR; |
| 4340 | } |
| 4341 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4342 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4343 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4344 | ** If an input line begins with "." then invoke this routine to |
| 4345 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4346 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4347 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4348 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4349 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4350 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4351 | int nArg = 0; |
| 4352 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4353 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4354 | char *azArg[50]; |
| 4355 | |
| 4356 | /* Parse the input line into tokens. |
| 4357 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4358 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 4359 | while( IsSpace(zLine[h]) ){ h++; } |
| 4360 | if( zLine[h]==0 ) break; |
| 4361 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 4362 | int delim = zLine[h++]; |
| 4363 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4364 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4365 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4366 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4367 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4368 | if( zLine[h]==delim ){ |
| 4369 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4370 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4371 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4372 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4373 | azArg[nArg++] = &zLine[h]; |
| 4374 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 4375 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4376 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | /* Process the input line. |
| 4381 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4382 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4383 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4384 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4385 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4386 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4387 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 4388 | if( nArg!=2 ){ |
| 4389 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 4390 | rc = 1; |
| 4391 | goto meta_command_exit; |
| 4392 | } |
| 4393 | open_db(p, 0); |
| 4394 | if( booleanValue(azArg[1]) ){ |
| 4395 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 4396 | }else{ |
| 4397 | sqlite3_set_authorizer(p->db, 0, 0); |
| 4398 | } |
| 4399 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4400 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4401 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4402 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 4403 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 4404 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4405 | const char *zDestFile = 0; |
| 4406 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4407 | sqlite3 *pDest; |
| 4408 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4409 | int j; |
| 4410 | for(j=1; j<nArg; j++){ |
| 4411 | const char *z = azArg[j]; |
| 4412 | if( z[0]=='-' ){ |
| 4413 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4414 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4415 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4416 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4417 | return 1; |
| 4418 | } |
| 4419 | }else if( zDestFile==0 ){ |
| 4420 | zDestFile = azArg[j]; |
| 4421 | }else if( zDb==0 ){ |
| 4422 | zDb = zDestFile; |
| 4423 | zDestFile = azArg[j]; |
| 4424 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4425 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4426 | return 1; |
| 4427 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4428 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4429 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4430 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4431 | return 1; |
| 4432 | } |
| 4433 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4434 | rc = sqlite3_open(zDestFile, &pDest); |
| 4435 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4436 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4437 | sqlite3_close(pDest); |
| 4438 | return 1; |
| 4439 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4440 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4441 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 4442 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4443 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4444 | sqlite3_close(pDest); |
| 4445 | return 1; |
| 4446 | } |
| 4447 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 4448 | sqlite3_backup_finish(pBackup); |
| 4449 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4450 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4451 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4452 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4453 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4454 | } |
| 4455 | sqlite3_close(pDest); |
| 4456 | }else |
| 4457 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4458 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 4459 | if( nArg==2 ){ |
| 4460 | bail_on_error = booleanValue(azArg[1]); |
| 4461 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4462 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4463 | rc = 1; |
| 4464 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 4465 | }else |
| 4466 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4467 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 4468 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4469 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4470 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4471 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4472 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4473 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4474 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4475 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4476 | rc = 1; |
| 4477 | } |
| 4478 | }else |
| 4479 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4480 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 4481 | ** routine named test_breakpoint(). |
| 4482 | */ |
| 4483 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 4484 | test_breakpoint(); |
| 4485 | }else |
| 4486 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4487 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 4488 | if( nArg==2 ){ |
| 4489 | p->countChanges = booleanValue(azArg[1]); |
| 4490 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4491 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4492 | rc = 1; |
| 4493 | } |
| 4494 | }else |
| 4495 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4496 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 4497 | ** Then read the content of the testcase-out.txt file and compare against |
| 4498 | ** azArg[1]. If there are differences, report an error and exit. |
| 4499 | */ |
| 4500 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 4501 | char *zRes = 0; |
| 4502 | output_reset(p); |
| 4503 | if( nArg!=2 ){ |
| 4504 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4505 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4506 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4507 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 4508 | rc = 2; |
| 4509 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4510 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4511 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 4512 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4513 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4514 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4515 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4516 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4517 | } |
| 4518 | sqlite3_free(zRes); |
| 4519 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4520 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4521 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 4522 | if( nArg==2 ){ |
| 4523 | tryToClone(p, azArg[1]); |
| 4524 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4525 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4526 | rc = 1; |
| 4527 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4528 | }else |
| 4529 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4530 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4531 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4532 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4533 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4534 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4535 | data.showHeader = 0; |
| 4536 | data.cMode = data.mode = MODE_List; |
| 4537 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 4538 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4539 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 4540 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4541 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4542 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4543 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4544 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4545 | } |
| 4546 | }else |
| 4547 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4548 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 4549 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 4550 | }else |
| 4551 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4552 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4553 | const char *zLike = 0; |
| 4554 | int i; |
| 4555 | p->preserveRowid = 0; |
| 4556 | for(i=1; i<nArg; i++){ |
| 4557 | if( azArg[i][0]=='-' ){ |
| 4558 | const char *z = azArg[i]+1; |
| 4559 | if( z[0]=='-' ) z++; |
| 4560 | if( strcmp(z,"preserve-rowids")==0 ){ |
| 4561 | p->preserveRowid = 1; |
| 4562 | }else |
| 4563 | { |
| 4564 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4565 | rc = 1; |
| 4566 | goto meta_command_exit; |
| 4567 | } |
| 4568 | }else if( zLike ){ |
| 4569 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 4570 | rc = 1; |
| 4571 | goto meta_command_exit; |
| 4572 | }else{ |
| 4573 | zLike = azArg[i]; |
| 4574 | } |
| 4575 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4576 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 4577 | /* When playing back a "dump", the content might appear in an order |
| 4578 | ** which causes immediate foreign key constraints to be violated. |
| 4579 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4580 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 4581 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4582 | p->writableSchema = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4583 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 4584 | ** as much of the schema as it can even if the sqlite_master table is |
| 4585 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4586 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4587 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4588 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4589 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4590 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4591 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4592 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4593 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4594 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4595 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4596 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4597 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4598 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 4599 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4600 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4601 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4602 | char *zSql; |
| 4603 | zSql = sqlite3_mprintf( |
| 4604 | "SELECT name, type, sql FROM sqlite_master " |
| 4605 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 4606 | " AND sql NOT NULL", zLike); |
| 4607 | run_schema_dump_query(p,zSql); |
| 4608 | sqlite3_free(zSql); |
| 4609 | zSql = sqlite3_mprintf( |
| 4610 | "SELECT sql FROM sqlite_master " |
| 4611 | "WHERE sql NOT NULL" |
| 4612 | " AND type IN ('index','trigger','view')" |
| 4613 | " AND tbl_name LIKE %Q", zLike); |
| 4614 | run_table_dump_query(p, zSql, 0); |
| 4615 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4616 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4617 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4618 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4619 | p->writableSchema = 0; |
| 4620 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4621 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4622 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4623 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4624 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4625 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4626 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 4627 | if( nArg==2 ){ |
| 4628 | p->echoOn = booleanValue(azArg[1]); |
| 4629 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4630 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4631 | rc = 1; |
| 4632 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4633 | }else |
| 4634 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4635 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 4636 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4637 | if( strcmp(azArg[1],"full")==0 ){ |
| 4638 | p->autoEQP = 2; |
| 4639 | }else{ |
| 4640 | p->autoEQP = booleanValue(azArg[1]); |
| 4641 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4642 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4643 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4644 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4645 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 4646 | }else |
| 4647 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 4648 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4649 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4650 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4651 | }else |
| 4652 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4653 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4654 | int val = 1; |
| 4655 | if( nArg>=2 ){ |
| 4656 | if( strcmp(azArg[1],"auto")==0 ){ |
| 4657 | val = 99; |
| 4658 | }else{ |
| 4659 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4660 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4661 | } |
| 4662 | if( val==1 && p->mode!=MODE_Explain ){ |
| 4663 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 4664 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4665 | p->autoExplain = 0; |
| 4666 | }else if( val==0 ){ |
| 4667 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4668 | p->autoExplain = 0; |
| 4669 | }else if( val==99 ){ |
| 4670 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4671 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4672 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4673 | }else |
| 4674 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4675 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4676 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4677 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4678 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4679 | memcpy(&data, p, sizeof(data)); |
| 4680 | data.showHeader = 0; |
| 4681 | data.cMode = data.mode = MODE_Semi; |
| 4682 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 4683 | data.cMode = data.mode = MODE_Pretty; |
| 4684 | nArg = 1; |
| 4685 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4686 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4687 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4688 | rc = 1; |
| 4689 | goto meta_command_exit; |
| 4690 | } |
| 4691 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4692 | rc = sqlite3_exec(p->db, |
| 4693 | "SELECT sql FROM" |
| 4694 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4695 | " FROM sqlite_master UNION ALL" |
| 4696 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4697 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4698 | "ORDER BY rowid", |
| 4699 | callback, &data, &zErrMsg |
| 4700 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4701 | if( rc==SQLITE_OK ){ |
| 4702 | sqlite3_stmt *pStmt; |
| 4703 | rc = sqlite3_prepare_v2(p->db, |
| 4704 | "SELECT rowid FROM sqlite_master" |
| 4705 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 4706 | -1, &pStmt, 0); |
| 4707 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 4708 | sqlite3_finalize(pStmt); |
| 4709 | } |
| 4710 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4711 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4712 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4713 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4714 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 4715 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4716 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4717 | data.zDestTable = "sqlite_stat1"; |
| 4718 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 4719 | shell_callback, &data,&zErrMsg); |
| 4720 | data.zDestTable = "sqlite_stat3"; |
| 4721 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 4722 | shell_callback, &data,&zErrMsg); |
| 4723 | data.zDestTable = "sqlite_stat4"; |
| 4724 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 4725 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4726 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4727 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4728 | }else |
| 4729 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4730 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 4731 | if( nArg==2 ){ |
| 4732 | p->showHeader = booleanValue(azArg[1]); |
| 4733 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4734 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4735 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 4736 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4737 | }else |
| 4738 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4739 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4740 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4741 | }else |
| 4742 | |
| 4743 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 4744 | char *zTable; /* Insert data into this table */ |
| 4745 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4746 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4747 | int nCol; /* Number of columns in the table */ |
| 4748 | int nByte; /* Number of bytes in an SQL string */ |
| 4749 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 4750 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4751 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4752 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4753 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 4754 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 4755 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4756 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4757 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4758 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4759 | goto meta_command_exit; |
| 4760 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 4761 | zFile = azArg[1]; |
| 4762 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4763 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4764 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4765 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4766 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4767 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4768 | raw_printf(stderr, |
| 4769 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4770 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4771 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4772 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4773 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4774 | " for import\n"); |
| 4775 | return 1; |
| 4776 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4777 | nSep = strlen30(p->rowSeparator); |
| 4778 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4779 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4780 | return 1; |
| 4781 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4782 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 4783 | /* When importing CSV (only), if the row separator is set to the |
| 4784 | ** default output row separator, change it to the default input |
| 4785 | ** row separator. This avoids having to maintain different input |
| 4786 | ** and output row separators. */ |
| 4787 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 4788 | nSep = strlen30(p->rowSeparator); |
| 4789 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4790 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4791 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4792 | " for import\n"); |
| 4793 | return 1; |
| 4794 | } |
| 4795 | sCtx.zFile = zFile; |
| 4796 | sCtx.nLine = 1; |
| 4797 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4798 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4799 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4800 | return 1; |
| 4801 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4802 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 4803 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4804 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4805 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4806 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4807 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4808 | xCloser = fclose; |
| 4809 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4810 | if( p->mode==MODE_Ascii ){ |
| 4811 | xRead = ascii_read_one_field; |
| 4812 | }else{ |
| 4813 | xRead = csv_read_one_field; |
| 4814 | } |
| 4815 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4816 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4817 | return 1; |
| 4818 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4819 | sCtx.cColSep = p->colSeparator[0]; |
| 4820 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 4821 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4822 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4823 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4824 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4825 | return 1; |
| 4826 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4827 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 4828 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4829 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4830 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4831 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 4832 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4833 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 4834 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4835 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4836 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4837 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4838 | if( cSep=='(' ){ |
| 4839 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4840 | sqlite3_free(sCtx.z); |
| 4841 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4842 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 4843 | return 1; |
| 4844 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4845 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 4846 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 4847 | sqlite3_free(zCreate); |
| 4848 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4849 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4850 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4851 | sqlite3_free(sCtx.z); |
| 4852 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4853 | return 1; |
| 4854 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 4855 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4856 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4857 | sqlite3_free(zSql); |
| 4858 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4859 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4860 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4861 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4862 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4863 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4864 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4865 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4866 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4867 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4868 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4869 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4870 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4871 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4872 | return 1; |
| 4873 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4874 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4875 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4876 | for(i=1; i<nCol; i++){ |
| 4877 | zSql[j++] = ','; |
| 4878 | zSql[j++] = '?'; |
| 4879 | } |
| 4880 | zSql[j++] = ')'; |
| 4881 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 4882 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4883 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4884 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4885 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 4886 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4887 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4888 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4889 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4890 | needCommit = sqlite3_get_autocommit(p->db); |
| 4891 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4892 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4893 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4894 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4895 | char *z = xRead(&sCtx); |
| 4896 | /* |
| 4897 | ** Did we reach end-of-file before finding any columns? |
| 4898 | ** If so, stop instead of NULL filling the remaining columns. |
| 4899 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4900 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4901 | /* |
| 4902 | ** Did we reach end-of-file OR end-of-line before finding any |
| 4903 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 4904 | ** the remaining columns. |
| 4905 | */ |
| 4906 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4907 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4908 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4909 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4910 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4911 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 4912 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 4913 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 4914 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4915 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4916 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4917 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4918 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4919 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4920 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4921 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4922 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4923 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4924 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4925 | if( i>=nCol ){ |
| 4926 | sqlite3_step(pStmt); |
| 4927 | rc = sqlite3_reset(pStmt); |
| 4928 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4929 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 4930 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4931 | } |
| 4932 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4933 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4934 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4935 | xCloser(sCtx.in); |
| 4936 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4937 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4938 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4939 | }else |
| 4940 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 4941 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4942 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 4943 | char *zSql; |
| 4944 | char *zCollist = 0; |
| 4945 | sqlite3_stmt *pStmt; |
| 4946 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 4947 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4948 | if( nArg!=3 ){ |
| 4949 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 4950 | rc = 1; |
| 4951 | goto meta_command_exit; |
| 4952 | } |
| 4953 | open_db(p, 0); |
| 4954 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 4955 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 4956 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4957 | sqlite3_free(zSql); |
| 4958 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4959 | tnum = sqlite3_column_int(pStmt, 0); |
| 4960 | } |
| 4961 | sqlite3_finalize(pStmt); |
| 4962 | if( tnum==0 ){ |
| 4963 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 4964 | rc = 1; |
| 4965 | goto meta_command_exit; |
| 4966 | } |
| 4967 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 4968 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4969 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 4970 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4971 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 4972 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4973 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 4974 | i++; |
| 4975 | if( zCol==0 ){ |
| 4976 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 4977 | zCol = "_ROWID_"; |
| 4978 | }else{ |
| 4979 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 4980 | zCol = zLabel; |
| 4981 | } |
| 4982 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4983 | if( zCollist==0 ){ |
| 4984 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 4985 | }else{ |
| 4986 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 4987 | } |
| 4988 | } |
| 4989 | sqlite3_finalize(pStmt); |
| 4990 | zSql = sqlite3_mprintf( |
| 4991 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 4992 | azArg[2], zCollist, zCollist); |
| 4993 | sqlite3_free(zCollist); |
| 4994 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 4995 | if( rc==SQLITE_OK ){ |
| 4996 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 4997 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 4998 | if( rc ){ |
| 4999 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 5000 | }else{ |
| 5001 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5002 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5003 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 5004 | ); |
| 5005 | } |
| 5006 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5007 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5008 | rc = 1; |
| 5009 | } |
| 5010 | sqlite3_free(zSql); |
| 5011 | }else |
| 5012 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 5013 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5014 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5015 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 5016 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5017 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 5018 | iotrace = 0; |
| 5019 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5020 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5021 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5022 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5023 | iotrace = stdout; |
| 5024 | }else{ |
| 5025 | iotrace = fopen(azArg[1], "w"); |
| 5026 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5027 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5028 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5029 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5030 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5031 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5035 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5036 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5037 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 5038 | static const struct { |
| 5039 | const char *zLimitName; /* Name of a limit */ |
| 5040 | int limitCode; /* Integer code for that limit */ |
| 5041 | } aLimit[] = { |
| 5042 | { "length", SQLITE_LIMIT_LENGTH }, |
| 5043 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 5044 | { "column", SQLITE_LIMIT_COLUMN }, |
| 5045 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5046 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5047 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 5048 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5049 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 5050 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5051 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 5052 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 5053 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 5054 | }; |
| 5055 | int i, n2; |
| 5056 | open_db(p, 0); |
| 5057 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5058 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5059 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5060 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 5061 | } |
| 5062 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5063 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5064 | rc = 1; |
| 5065 | goto meta_command_exit; |
| 5066 | }else{ |
| 5067 | int iLimit = -1; |
| 5068 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5069 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5070 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 5071 | if( iLimit<0 ){ |
| 5072 | iLimit = i; |
| 5073 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5074 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5075 | rc = 1; |
| 5076 | goto meta_command_exit; |
| 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5081 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5082 | "enter \".limits\" with no arguments for a list.\n", |
| 5083 | azArg[1]); |
| 5084 | rc = 1; |
| 5085 | goto meta_command_exit; |
| 5086 | } |
| 5087 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 5088 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 5089 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5090 | } |
| 5091 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 5092 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 5093 | } |
| 5094 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5095 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5096 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 5097 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5098 | lintDotCommand(p, azArg, nArg); |
| 5099 | }else |
| 5100 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5101 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5102 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5103 | const char *zFile, *zProc; |
| 5104 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5105 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5106 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5107 | rc = 1; |
| 5108 | goto meta_command_exit; |
| 5109 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5110 | zFile = azArg[1]; |
| 5111 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5112 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5113 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 5114 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5115 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5116 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5117 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5118 | } |
| 5119 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5120 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5121 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5122 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 5123 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5124 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5125 | rc = 1; |
| 5126 | }else{ |
| 5127 | const char *zFile = azArg[1]; |
| 5128 | output_file_close(p->pLog); |
| 5129 | p->pLog = output_file_open(zFile); |
| 5130 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5131 | }else |
| 5132 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5133 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 5134 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 5135 | int n2 = (int)strlen(zMode); |
| 5136 | int c2 = zMode[0]; |
| 5137 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5138 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5139 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5140 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5141 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5142 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5143 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5144 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5145 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 5146 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5147 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5148 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5149 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5150 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5151 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5152 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5153 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 5154 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5155 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5156 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5157 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5158 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5159 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5160 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 5161 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5162 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 5163 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 5164 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5165 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5166 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5167 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5168 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5169 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5170 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 5171 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5172 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5173 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5174 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5175 | }else |
| 5176 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5177 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 5178 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5179 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 5180 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5181 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5182 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 5183 | rc = 1; |
| 5184 | } |
| 5185 | }else |
| 5186 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5187 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5188 | char *zNewFilename; /* Name of the database file to open */ |
| 5189 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5190 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5191 | /* Close the existing database */ |
| 5192 | session_close_all(p); |
| 5193 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5194 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 5195 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5196 | sqlite3_free(p->zFreeOnClose); |
| 5197 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5198 | /* Check for command-line arguments */ |
| 5199 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 5200 | const char *z = azArg[iName]; |
| 5201 | if( optionMatch(z,"new") ){ |
| 5202 | newFlag = 1; |
| 5203 | }else if( z[0]=='-' ){ |
| 5204 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 5205 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5206 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5207 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5208 | } |
| 5209 | /* If a filename is specified, try to open it first */ |
| 5210 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 5211 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5212 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5213 | p->zDbFilename = zNewFilename; |
| 5214 | open_db(p, 1); |
| 5215 | if( p->db==0 ){ |
| 5216 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 5217 | sqlite3_free(zNewFilename); |
| 5218 | }else{ |
| 5219 | p->zFreeOnClose = zNewFilename; |
| 5220 | } |
| 5221 | } |
| 5222 | if( p->db==0 ){ |
| 5223 | /* As a fall-back open a TEMP database */ |
| 5224 | p->zDbFilename = 0; |
| 5225 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5226 | } |
| 5227 | }else |
| 5228 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5229 | if( c=='o' |
| 5230 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 5231 | ){ |
| 5232 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 5233 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5234 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5235 | rc = 1; |
| 5236 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5237 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5238 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 5239 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5240 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5241 | rc = 1; |
| 5242 | goto meta_command_exit; |
| 5243 | } |
| 5244 | p->outCount = 2; |
| 5245 | }else{ |
| 5246 | p->outCount = 0; |
| 5247 | } |
| 5248 | output_reset(p); |
| 5249 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5250 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5251 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5252 | rc = 1; |
| 5253 | p->out = stdout; |
| 5254 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5255 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5256 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5257 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5258 | p->out = stdout; |
| 5259 | rc = 1; |
| 5260 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5261 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5262 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5263 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5264 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5265 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5266 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5267 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5268 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 5269 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5270 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5271 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5272 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5273 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | }else |
| 5277 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5278 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 5279 | int i; |
| 5280 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5281 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5282 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5283 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5284 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5285 | }else |
| 5286 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5287 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5288 | if( nArg >= 2) { |
| 5289 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 5290 | } |
| 5291 | if( nArg >= 3) { |
| 5292 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 5293 | } |
| 5294 | }else |
| 5295 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5296 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5297 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5298 | }else |
| 5299 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5300 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 5301 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5302 | if( nArg!=2 ){ |
| 5303 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5304 | rc = 1; |
| 5305 | goto meta_command_exit; |
| 5306 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5307 | alt = fopen(azArg[1], "rb"); |
| 5308 | if( alt==0 ){ |
| 5309 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 5310 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5311 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5312 | rc = process_input(p, alt); |
| 5313 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5314 | } |
| 5315 | }else |
| 5316 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5317 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5318 | const char *zSrcFile; |
| 5319 | const char *zDb; |
| 5320 | sqlite3 *pSrc; |
| 5321 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5322 | int nTimeout = 0; |
| 5323 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5324 | if( nArg==2 ){ |
| 5325 | zSrcFile = azArg[1]; |
| 5326 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5327 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5328 | zSrcFile = azArg[2]; |
| 5329 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5330 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5331 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5332 | rc = 1; |
| 5333 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5334 | } |
| 5335 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 5336 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5337 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5338 | sqlite3_close(pSrc); |
| 5339 | return 1; |
| 5340 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5341 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5342 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 5343 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5344 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5345 | sqlite3_close(pSrc); |
| 5346 | return 1; |
| 5347 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5348 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 5349 | || rc==SQLITE_BUSY ){ |
| 5350 | if( rc==SQLITE_BUSY ){ |
| 5351 | if( nTimeout++ >= 3 ) break; |
| 5352 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5353 | } |
| 5354 | } |
| 5355 | sqlite3_backup_finish(pBackup); |
| 5356 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5357 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5358 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5359 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5360 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5361 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5362 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5363 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5364 | } |
| 5365 | sqlite3_close(pSrc); |
| 5366 | }else |
| 5367 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5368 | |
| 5369 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 5370 | if( nArg==2 ){ |
| 5371 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5372 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5373 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5374 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5375 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5376 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5377 | rc = 1; |
| 5378 | } |
| 5379 | }else |
| 5380 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5381 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5382 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5383 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5384 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5385 | memcpy(&data, p, sizeof(data)); |
| 5386 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5387 | data.cMode = data.mode = MODE_Semi; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5388 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 5389 | data.cMode = data.mode = MODE_Pretty; |
| 5390 | nArg--; |
| 5391 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 5392 | } |
| 5393 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5394 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5395 | 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] | 5396 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5397 | char *new_argv[2], *new_colv[2]; |
| 5398 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 5399 | " type text,\n" |
| 5400 | " name text,\n" |
| 5401 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 5402 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5403 | " sql text\n" |
| 5404 | ")"; |
| 5405 | new_argv[1] = 0; |
| 5406 | new_colv[0] = "sql"; |
| 5407 | new_colv[1] = 0; |
| 5408 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5409 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5410 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5411 | char *new_argv[2], *new_colv[2]; |
| 5412 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 5413 | " type text,\n" |
| 5414 | " name text,\n" |
| 5415 | " tbl_name text,\n" |
| 5416 | " rootpage integer,\n" |
| 5417 | " sql text\n" |
| 5418 | ")"; |
| 5419 | new_argv[1] = 0; |
| 5420 | new_colv[0] = "sql"; |
| 5421 | new_colv[1] = 0; |
| 5422 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5423 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5424 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5425 | char *zSql; |
| 5426 | zSql = sqlite3_mprintf( |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5427 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5428 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5429 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5430 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5431 | "WHERE lower(tbl_name) LIKE %Q" |
drh | 6ac7a58 | 2011-11-04 00:35:56 +0000 | [diff] [blame] | 5432 | " AND type!='meta' AND sql NOTNULL " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5433 | "ORDER BY rowid", azArg[1]); |
| 5434 | rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg); |
| 5435 | sqlite3_free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5436 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5437 | }else if( nArg==1 ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5438 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5439 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5440 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5441 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5442 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5443 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 5444 | "ORDER BY rowid", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5445 | callback, &data, &zErrMsg |
| 5446 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5447 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5448 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5449 | rc = 1; |
| 5450 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5451 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5452 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5453 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5454 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5455 | rc = 1; |
| 5456 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5457 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5458 | rc = 1; |
| 5459 | }else{ |
| 5460 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5461 | } |
| 5462 | }else |
| 5463 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5464 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 5465 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 5466 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5467 | }else |
| 5468 | #endif |
| 5469 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5470 | #if defined(SQLITE_ENABLE_SESSION) |
| 5471 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 5472 | OpenSession *pSession = &p->aSession[0]; |
| 5473 | char **azCmd = &azArg[1]; |
| 5474 | int iSes = 0; |
| 5475 | int nCmd = nArg - 1; |
| 5476 | int i; |
| 5477 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5478 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5479 | if( nArg>=3 ){ |
| 5480 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 5481 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 5482 | } |
| 5483 | if( iSes<p->nSession ){ |
| 5484 | pSession = &p->aSession[iSes]; |
| 5485 | azCmd++; |
| 5486 | nCmd--; |
| 5487 | }else{ |
| 5488 | pSession = &p->aSession[0]; |
| 5489 | iSes = 0; |
| 5490 | } |
| 5491 | } |
| 5492 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5493 | /* .session attach TABLE |
| 5494 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 5495 | ** table so that it is never filtered. |
| 5496 | */ |
| 5497 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 5498 | if( nCmd!=2 ) goto session_syntax_error; |
| 5499 | if( pSession->p==0 ){ |
| 5500 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5501 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5502 | }else{ |
| 5503 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 5504 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5505 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5506 | rc = 0; |
| 5507 | } |
| 5508 | } |
| 5509 | }else |
| 5510 | |
| 5511 | /* .session changeset FILE |
| 5512 | ** .session patchset FILE |
| 5513 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 5514 | */ |
| 5515 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 5516 | FILE *out = 0; |
| 5517 | if( nCmd!=2 ) goto session_syntax_error; |
| 5518 | if( pSession->p==0 ) goto session_not_open; |
| 5519 | out = fopen(azCmd[1], "wb"); |
| 5520 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5521 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5522 | }else{ |
| 5523 | int szChng; |
| 5524 | void *pChng; |
| 5525 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5526 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5527 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5528 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 5529 | } |
| 5530 | if( rc ){ |
| 5531 | printf("Error: error code %d\n", rc); |
| 5532 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5533 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5534 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5535 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5536 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5537 | szChng); |
| 5538 | } |
| 5539 | sqlite3_free(pChng); |
| 5540 | fclose(out); |
| 5541 | } |
| 5542 | }else |
| 5543 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5544 | /* .session close |
| 5545 | ** Close the identified session |
| 5546 | */ |
| 5547 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5548 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5549 | if( p->nSession ){ |
| 5550 | session_close(pSession); |
| 5551 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 5552 | } |
| 5553 | }else |
| 5554 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5555 | /* .session enable ?BOOLEAN? |
| 5556 | ** Query or set the enable flag |
| 5557 | */ |
| 5558 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 5559 | int ii; |
| 5560 | if( nCmd>2 ) goto session_syntax_error; |
| 5561 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5562 | if( p->nSession ){ |
| 5563 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5564 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 5565 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5566 | } |
| 5567 | }else |
| 5568 | |
| 5569 | /* .session filter GLOB .... |
| 5570 | ** Set a list of GLOB patterns of table names to be excluded. |
| 5571 | */ |
| 5572 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 5573 | int ii, nByte; |
| 5574 | if( nCmd<2 ) goto session_syntax_error; |
| 5575 | if( p->nSession ){ |
| 5576 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 5577 | sqlite3_free(pSession->azFilter[ii]); |
| 5578 | } |
| 5579 | sqlite3_free(pSession->azFilter); |
| 5580 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 5581 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 5582 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5583 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5584 | exit(1); |
| 5585 | } |
| 5586 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5587 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5588 | } |
| 5589 | pSession->nFilter = ii-1; |
| 5590 | } |
| 5591 | }else |
| 5592 | |
| 5593 | /* .session indirect ?BOOLEAN? |
| 5594 | ** Query or set the indirect flag |
| 5595 | */ |
| 5596 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 5597 | int ii; |
| 5598 | if( nCmd>2 ) goto session_syntax_error; |
| 5599 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5600 | if( p->nSession ){ |
| 5601 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5602 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 5603 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5604 | } |
| 5605 | }else |
| 5606 | |
| 5607 | /* .session isempty |
| 5608 | ** Determine if the session is empty |
| 5609 | */ |
| 5610 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 5611 | int ii; |
| 5612 | if( nCmd!=1 ) goto session_syntax_error; |
| 5613 | if( p->nSession ){ |
| 5614 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5615 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 5616 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5617 | } |
| 5618 | }else |
| 5619 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5620 | /* .session list |
| 5621 | ** List all currently open sessions |
| 5622 | */ |
| 5623 | if( strcmp(azCmd[0],"list")==0 ){ |
| 5624 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5625 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5626 | } |
| 5627 | }else |
| 5628 | |
| 5629 | /* .session open DB NAME |
| 5630 | ** Open a new session called NAME on the attached database DB. |
| 5631 | ** DB is normally "main". |
| 5632 | */ |
| 5633 | if( strcmp(azCmd[0],"open")==0 ){ |
| 5634 | char *zName; |
| 5635 | if( nCmd!=3 ) goto session_syntax_error; |
| 5636 | zName = azCmd[2]; |
| 5637 | if( zName[0]==0 ) goto session_syntax_error; |
| 5638 | for(i=0; i<p->nSession; i++){ |
| 5639 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5640 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5641 | goto meta_command_exit; |
| 5642 | } |
| 5643 | } |
| 5644 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5645 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5646 | goto meta_command_exit; |
| 5647 | } |
| 5648 | pSession = &p->aSession[p->nSession]; |
| 5649 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 5650 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5651 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5652 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5653 | goto meta_command_exit; |
| 5654 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5655 | pSession->nFilter = 0; |
| 5656 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5657 | p->nSession++; |
| 5658 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 5659 | }else |
| 5660 | /* If no command name matches, show a syntax error */ |
| 5661 | session_syntax_error: |
| 5662 | session_help(p); |
| 5663 | }else |
| 5664 | #endif |
| 5665 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5666 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5667 | /* Undocumented commands for internal testing. Subject to change |
| 5668 | ** without notice. */ |
| 5669 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 5670 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 5671 | int i, v; |
| 5672 | for(i=1; i<nArg; i++){ |
| 5673 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5674 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5675 | } |
| 5676 | } |
| 5677 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 5678 | int i; sqlite3_int64 v; |
| 5679 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5680 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5681 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5682 | 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] | 5683 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5684 | } |
| 5685 | } |
| 5686 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5687 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5688 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5689 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5690 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5691 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5692 | rc = 1; |
| 5693 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5694 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5695 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 5696 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5697 | } |
| 5698 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5699 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 5700 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5701 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5702 | }else |
| 5703 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5704 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 5705 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 5706 | int i; /* Loop counter */ |
| 5707 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5708 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5709 | int iSize = 224; /* Hash algorithm to use */ |
| 5710 | int bDebug = 0; /* Only show the query that would have run */ |
| 5711 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 5712 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5713 | char *zSep; /* Separator */ |
| 5714 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5715 | ShellText sQuery; /* Set of queries used to read all content */ |
| 5716 | for(i=1; i<nArg; i++){ |
| 5717 | const char *z = azArg[i]; |
| 5718 | if( z[0]=='-' ){ |
| 5719 | z++; |
| 5720 | if( z[0]=='-' ) z++; |
| 5721 | if( strcmp(z,"schema")==0 ){ |
| 5722 | bSchema = 1; |
| 5723 | }else |
| 5724 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 5725 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 5726 | ){ |
| 5727 | iSize = atoi(&z[5]); |
| 5728 | }else |
| 5729 | if( strcmp(z,"debug")==0 ){ |
| 5730 | bDebug = 1; |
| 5731 | }else |
| 5732 | { |
| 5733 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5734 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5735 | raw_printf(stderr, "Should be one of: --schema" |
| 5736 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 5737 | rc = 1; |
| 5738 | goto meta_command_exit; |
| 5739 | } |
| 5740 | }else if( zLike ){ |
| 5741 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 5742 | rc = 1; |
| 5743 | goto meta_command_exit; |
| 5744 | }else{ |
| 5745 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5746 | bSeparate = 1; |
| 5747 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5748 | } |
| 5749 | } |
| 5750 | if( bSchema ){ |
| 5751 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 5752 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 5753 | " UNION ALL SELECT 'sqlite_master'" |
| 5754 | " ORDER BY 1 collate nocase"; |
| 5755 | }else{ |
| 5756 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 5757 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 5758 | " AND name NOT LIKE 'sqlite_%'" |
| 5759 | " ORDER BY 1 collate nocase"; |
| 5760 | } |
| 5761 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5762 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5763 | initText(&sSql); |
| 5764 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 5765 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5766 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 5767 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 5768 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 5769 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 5770 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 5771 | appendText(&sQuery,zTab,'"'); |
| 5772 | appendText(&sQuery," NOT INDEXED;", 0); |
| 5773 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 5774 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 5775 | " ORDER BY name;", 0); |
| 5776 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 5777 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 5778 | " ORDER BY name;", 0); |
| 5779 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 5780 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 5781 | " ORDER BY tbl,idx;", 0); |
| 5782 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 5783 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 5784 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 5785 | appendText(&sQuery, zTab, 0); |
| 5786 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 5787 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5788 | appendText(&sSql, zSep, 0); |
| 5789 | appendText(&sSql, sQuery.z, '\''); |
| 5790 | sQuery.n = 0; |
| 5791 | appendText(&sSql, ",", 0); |
| 5792 | appendText(&sSql, zTab, '\''); |
| 5793 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5794 | } |
| 5795 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5796 | if( bSeparate ){ |
| 5797 | zSql = sqlite3_mprintf( |
| 5798 | "%s))" |
| 5799 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 5800 | " FROM [sha3sum$query]", |
| 5801 | sSql.z, iSize); |
| 5802 | }else{ |
| 5803 | zSql = sqlite3_mprintf( |
| 5804 | "%s))" |
| 5805 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 5806 | " FROM [sha3sum$query]", |
| 5807 | sSql.z, iSize); |
| 5808 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5809 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame^] | 5810 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 5811 | if( bDebug ){ |
| 5812 | utf8_printf(p->out, "%s\n", zSql); |
| 5813 | }else{ |
| 5814 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 5815 | } |
| 5816 | sqlite3_free(zSql); |
| 5817 | }else |
| 5818 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5819 | if( c=='s' |
| 5820 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5821 | ){ |
| 5822 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 5823 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5824 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5825 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5826 | rc = 1; |
| 5827 | goto meta_command_exit; |
| 5828 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 5829 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5830 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 5831 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 5832 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5833 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 5834 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5835 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5836 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 5837 | }else |
| 5838 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5839 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5840 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5841 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5842 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5843 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5844 | rc = 1; |
| 5845 | goto meta_command_exit; |
| 5846 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5847 | utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]); |
| 5848 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5849 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 5850 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5851 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5852 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 5853 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5854 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5855 | raw_printf(p->out, "\n"); |
| 5856 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5857 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5858 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5859 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5860 | raw_printf(p->out, "\n"); |
| 5861 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5862 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5863 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5864 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5865 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5866 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5867 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5868 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5869 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5870 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 5871 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5872 | }else |
| 5873 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5874 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 5875 | if( nArg==2 ){ |
| 5876 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 5877 | }else if( nArg==1 ){ |
| 5878 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5879 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 5880 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5881 | rc = 1; |
| 5882 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 5883 | }else |
| 5884 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 5885 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 5886 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 5887 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 5888 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5889 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 5890 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5891 | int nRow, nAlloc; |
| 5892 | char *zSql = 0; |
| 5893 | int ii; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5894 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5895 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5896 | if( rc ) return shellDatabaseError(p->db); |
| 5897 | |
| 5898 | /* Create an SQL statement to query for the list of tables in the |
| 5899 | ** main and all attached databases where the table name matches the |
| 5900 | ** LIKE pattern bound to variable "?1". */ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 5901 | if( c=='t' ){ |
| 5902 | zSql = sqlite3_mprintf( |
| 5903 | "SELECT name FROM sqlite_master" |
| 5904 | " WHERE type IN ('table','view')" |
| 5905 | " AND name NOT LIKE 'sqlite_%%'" |
| 5906 | " AND name LIKE ?1"); |
| 5907 | }else if( nArg>2 ){ |
| 5908 | /* It is an historical accident that the .indexes command shows an error |
| 5909 | ** when called with the wrong number of arguments whereas the .tables |
| 5910 | ** command does not. */ |
| 5911 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 5912 | rc = 1; |
| 5913 | goto meta_command_exit; |
| 5914 | }else{ |
| 5915 | zSql = sqlite3_mprintf( |
| 5916 | "SELECT name FROM sqlite_master" |
| 5917 | " WHERE type='index'" |
| 5918 | " AND tbl_name LIKE ?1"); |
| 5919 | } |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 5920 | for(ii=0; zSql && sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5921 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 5922 | if( zDbName==0 || ii==0 ) continue; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 5923 | if( c=='t' ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5924 | zSql = sqlite3_mprintf( |
| 5925 | "%z UNION ALL " |
| 5926 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 5927 | " WHERE type IN ('table','view')" |
| 5928 | " AND name NOT LIKE 'sqlite_%%'" |
| 5929 | " AND name LIKE ?1", zSql, zDbName, zDbName); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 5930 | }else{ |
| 5931 | zSql = sqlite3_mprintf( |
| 5932 | "%z UNION ALL " |
| 5933 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 5934 | " WHERE type='index'" |
| 5935 | " AND tbl_name LIKE ?1", zSql, zDbName, zDbName); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5936 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 5937 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5938 | rc = sqlite3_finalize(pStmt); |
| 5939 | if( zSql && rc==SQLITE_OK ){ |
| 5940 | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 5941 | if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5942 | } |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5943 | sqlite3_free(zSql); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5944 | if( !zSql ) return shellNomemError(); |
| 5945 | if( rc ) return shellDatabaseError(p->db); |
| 5946 | |
| 5947 | /* Run the SQL statement prepared by the above block. Store the results |
| 5948 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5949 | nRow = nAlloc = 0; |
| 5950 | azResult = 0; |
| 5951 | if( nArg>1 ){ |
| 5952 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5953 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5954 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 5955 | } |
| 5956 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5957 | if( nRow>=nAlloc ){ |
| 5958 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5959 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5960 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5961 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5962 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5963 | break; |
| 5964 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5965 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5966 | azResult = azNew; |
| 5967 | } |
| 5968 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5969 | if( 0==azResult[nRow] ){ |
| 5970 | rc = shellNomemError(); |
| 5971 | break; |
| 5972 | } |
| 5973 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5974 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5975 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 5976 | rc = shellDatabaseError(p->db); |
| 5977 | } |
| 5978 | |
| 5979 | /* Pretty-print the contents of array azResult[] to the output */ |
| 5980 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 5981 | int len, maxlen = 0; |
| 5982 | int i, j; |
| 5983 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5984 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5985 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 5986 | if( len>maxlen ) maxlen = len; |
| 5987 | } |
| 5988 | nPrintCol = 80/(maxlen+2); |
| 5989 | if( nPrintCol<1 ) nPrintCol = 1; |
| 5990 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 5991 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 5992 | for(j=i; j<nRow; j+=nPrintRow){ |
| 5993 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5994 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 5995 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 5996 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5997 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 5998 | } |
| 5999 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6000 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6001 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 6002 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6003 | }else |
| 6004 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6005 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 6006 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 6007 | output_reset(p); |
| 6008 | p->out = output_file_open("testcase-out.txt"); |
| 6009 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6010 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6011 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6012 | if( nArg>=2 ){ |
| 6013 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 6014 | }else{ |
| 6015 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 6016 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6017 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6018 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6019 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6020 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6021 | static const struct { |
| 6022 | const char *zCtrlName; /* Name of a test-control option */ |
| 6023 | int ctrlCode; /* Integer code for that option */ |
| 6024 | } aCtrl[] = { |
| 6025 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 6026 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 6027 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 6028 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 6029 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 6030 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 6031 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 6032 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 6033 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 6034 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 6035 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 6036 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6037 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6038 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 6039 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6040 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6041 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6042 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6043 | int rc2 = 0; |
| 6044 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6045 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6046 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6047 | /* convert testctrl text option to value. allow any unique prefix |
| 6048 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6049 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6050 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6051 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6052 | if( testctrl<0 ){ |
| 6053 | testctrl = aCtrl[i].ctrlCode; |
| 6054 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6055 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6056 | testctrl = -1; |
| 6057 | break; |
| 6058 | } |
| 6059 | } |
| 6060 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6061 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6062 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6063 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6064 | }else{ |
| 6065 | switch(testctrl){ |
| 6066 | |
| 6067 | /* sqlite3_test_control(int, db, int) */ |
| 6068 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6069 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6070 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6071 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6072 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6073 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6074 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6075 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6076 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6077 | } |
| 6078 | break; |
| 6079 | |
| 6080 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6081 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 6082 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6083 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6084 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6085 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6086 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6087 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6088 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6089 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 6090 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6091 | } |
| 6092 | break; |
| 6093 | |
| 6094 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6095 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6096 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 6097 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6098 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6099 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6100 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6101 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6102 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6103 | } |
| 6104 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6105 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6106 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6107 | case SQLITE_TESTCTRL_ASSERT: |
| 6108 | case SQLITE_TESTCTRL_ALWAYS: |
| 6109 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6110 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6111 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6112 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6113 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6114 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6115 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6116 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6117 | } |
| 6118 | break; |
| 6119 | |
| 6120 | /* sqlite3_test_control(int, char *) */ |
| 6121 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6122 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6123 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6124 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6125 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6126 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6127 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6128 | utf8_printf(stderr, |
| 6129 | "Error: testctrl %s takes a single char * option\n", |
| 6130 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6131 | } |
| 6132 | break; |
| 6133 | #endif |
| 6134 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6135 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6136 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6137 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6138 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6139 | integerValue(azArg[3]), |
| 6140 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6141 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6142 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6143 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6144 | } |
| 6145 | break; |
| 6146 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6147 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 6148 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 6149 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 6150 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6151 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6152 | utf8_printf(stderr, |
| 6153 | "Error: CLI support for testctrl %s not implemented\n", |
| 6154 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6155 | break; |
| 6156 | } |
| 6157 | } |
| 6158 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 6159 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6160 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6161 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6162 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6163 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6164 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6165 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6166 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 6167 | if( nArg==2 ){ |
| 6168 | enableTimer = booleanValue(azArg[1]); |
| 6169 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6170 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6171 | enableTimer = 0; |
| 6172 | } |
| 6173 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6174 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6175 | rc = 1; |
| 6176 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6177 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6178 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6179 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6180 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6181 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6182 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6183 | rc = 1; |
| 6184 | goto meta_command_exit; |
| 6185 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 6186 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6187 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 6188 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6189 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6190 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6191 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6192 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6193 | } |
| 6194 | #endif |
| 6195 | }else |
| 6196 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6197 | #if SQLITE_USER_AUTHENTICATION |
| 6198 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 6199 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6200 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6201 | rc = 1; |
| 6202 | goto meta_command_exit; |
| 6203 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 6204 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6205 | if( strcmp(azArg[1],"login")==0 ){ |
| 6206 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6207 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6208 | rc = 1; |
| 6209 | goto meta_command_exit; |
| 6210 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6211 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 6212 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6213 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6214 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6215 | rc = 1; |
| 6216 | } |
| 6217 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 6218 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6219 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6220 | rc = 1; |
| 6221 | goto meta_command_exit; |
| 6222 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6223 | rc = sqlite3_user_add(p->db, azArg[2], |
| 6224 | azArg[3], (int)strlen(azArg[3]), |
| 6225 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6226 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6227 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6228 | rc = 1; |
| 6229 | } |
| 6230 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 6231 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6232 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6233 | rc = 1; |
| 6234 | goto meta_command_exit; |
| 6235 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6236 | rc = sqlite3_user_change(p->db, azArg[2], |
| 6237 | azArg[3], (int)strlen(azArg[3]), |
| 6238 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6239 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6240 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6241 | rc = 1; |
| 6242 | } |
| 6243 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 6244 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6245 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6246 | rc = 1; |
| 6247 | goto meta_command_exit; |
| 6248 | } |
| 6249 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 6250 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6251 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6252 | rc = 1; |
| 6253 | } |
| 6254 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6255 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6256 | rc = 1; |
| 6257 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6258 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6259 | }else |
| 6260 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 6261 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6262 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6263 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6264 | sqlite3_libversion(), sqlite3_sourceid()); |
| 6265 | }else |
| 6266 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6267 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 6268 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 6269 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6270 | if( p->db ){ |
| 6271 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 6272 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6273 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 6274 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6275 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6276 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6277 | } |
| 6278 | } |
| 6279 | }else |
| 6280 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 6281 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 6282 | sqlite3_vfs *pVfs; |
| 6283 | sqlite3_vfs *pCurrent = 0; |
| 6284 | if( p->db ){ |
| 6285 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 6286 | } |
| 6287 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 6288 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 6289 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 6290 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6291 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6292 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 6293 | if( pVfs->pNext ){ |
| 6294 | raw_printf(p->out, "-----------------------------------\n"); |
| 6295 | } |
| 6296 | } |
| 6297 | }else |
| 6298 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6299 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 6300 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 6301 | char *zVfsName = 0; |
| 6302 | if( p->db ){ |
| 6303 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 6304 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6305 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6306 | sqlite3_free(zVfsName); |
| 6307 | } |
| 6308 | } |
| 6309 | }else |
| 6310 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6311 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 6312 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6313 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6314 | }else |
| 6315 | #endif |
| 6316 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6317 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6318 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6319 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6320 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6321 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6322 | } |
| 6323 | }else |
| 6324 | |
| 6325 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6326 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6327 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6328 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6329 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6330 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6331 | meta_command_exit: |
| 6332 | if( p->outCount ){ |
| 6333 | p->outCount--; |
| 6334 | if( p->outCount==0 ) output_reset(p); |
| 6335 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6336 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6339 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6340 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 6341 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6342 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6343 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6344 | int i; |
| 6345 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 6346 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6350 | ** Test to see if a line consists entirely of whitespace. |
| 6351 | */ |
| 6352 | static int _all_whitespace(const char *z){ |
| 6353 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6354 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6355 | if( *z=='/' && z[1]=='*' ){ |
| 6356 | z += 2; |
| 6357 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 6358 | if( *z==0 ) return 0; |
| 6359 | z++; |
| 6360 | continue; |
| 6361 | } |
| 6362 | if( *z=='-' && z[1]=='-' ){ |
| 6363 | z += 2; |
| 6364 | while( *z && *z!='\n' ){ z++; } |
| 6365 | if( *z==0 ) return 1; |
| 6366 | continue; |
| 6367 | } |
| 6368 | return 0; |
| 6369 | } |
| 6370 | return 1; |
| 6371 | } |
| 6372 | |
| 6373 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6374 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 6375 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 6376 | ** as is the Oracle "/". |
| 6377 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6378 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6379 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6380 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 6381 | return 1; /* Oracle */ |
| 6382 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6383 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6384 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6385 | return 1; /* SQL Server */ |
| 6386 | } |
| 6387 | return 0; |
| 6388 | } |
| 6389 | |
| 6390 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6391 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 6392 | ** ends in the middle of a string literal or C-style comment. |
| 6393 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6394 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6395 | int rc; |
| 6396 | if( zSql==0 ) return 1; |
| 6397 | zSql[nSql] = ';'; |
| 6398 | zSql[nSql+1] = 0; |
| 6399 | rc = sqlite3_complete(zSql); |
| 6400 | zSql[nSql] = 0; |
| 6401 | return rc; |
| 6402 | } |
| 6403 | |
| 6404 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6405 | ** Run a single line of SQL |
| 6406 | */ |
| 6407 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 6408 | int rc; |
| 6409 | char *zErrMsg = 0; |
| 6410 | |
| 6411 | open_db(p, 0); |
| 6412 | if( p->backslashOn ) resolve_backslashes(zSql); |
| 6413 | BEGIN_TIMER; |
| 6414 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 6415 | END_TIMER; |
| 6416 | if( rc || zErrMsg ){ |
| 6417 | char zPrefix[100]; |
| 6418 | if( in!=0 || !stdin_is_interactive ){ |
| 6419 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 6420 | "Error: near line %d:", startline); |
| 6421 | }else{ |
| 6422 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 6423 | } |
| 6424 | if( zErrMsg!=0 ){ |
| 6425 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 6426 | sqlite3_free(zErrMsg); |
| 6427 | zErrMsg = 0; |
| 6428 | }else{ |
| 6429 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 6430 | } |
| 6431 | return 1; |
| 6432 | }else if( p->countChanges ){ |
| 6433 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 6434 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 6435 | } |
| 6436 | return 0; |
| 6437 | } |
| 6438 | |
| 6439 | |
| 6440 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6441 | ** Read input from *in and process it. If *in==0 then input |
| 6442 | ** is interactive - the user is typing it it. Otherwise, input |
| 6443 | ** is coming from a file or device. A prompt is issued and history |
| 6444 | ** is saved only if input is interactive. An interrupt signal will |
| 6445 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6446 | ** |
| 6447 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6448 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6449 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6450 | char *zLine = 0; /* A single input line */ |
| 6451 | char *zSql = 0; /* Accumulated SQL text */ |
| 6452 | int nLine; /* Length of current line */ |
| 6453 | int nSql = 0; /* Bytes of zSql[] used */ |
| 6454 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 6455 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6456 | int rc; /* Error code */ |
| 6457 | int errCnt = 0; /* Number of errors seen */ |
| 6458 | int lineno = 0; /* Current line number */ |
| 6459 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6460 | |
| 6461 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 6462 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6463 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6464 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6465 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 6466 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6467 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6468 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6469 | if( seenInterrupt ){ |
| 6470 | if( in!=0 ) break; |
| 6471 | seenInterrupt = 0; |
| 6472 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6473 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6474 | if( nSql==0 && _all_whitespace(zLine) ){ |
| 6475 | if( p->echoOn ) printf("%s\n", zLine); |
| 6476 | continue; |
| 6477 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 6478 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 6479 | if( p->echoOn ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6480 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6481 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6482 | break; |
| 6483 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6484 | errCnt++; |
| 6485 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6486 | continue; |
| 6487 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6488 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6489 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6490 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6491 | nLine = strlen30(zLine); |
| 6492 | if( nSql+nLine+2>=nAlloc ){ |
| 6493 | nAlloc = nSql+nLine+100; |
| 6494 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6495 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6496 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6497 | exit(1); |
| 6498 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6499 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6500 | nSqlPrior = nSql; |
| 6501 | if( nSql==0 ){ |
| 6502 | int i; |
| 6503 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 6504 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6505 | memcpy(zSql, zLine+i, nLine+1-i); |
| 6506 | startline = lineno; |
| 6507 | nSql = nLine-i; |
| 6508 | }else{ |
| 6509 | zSql[nSql++] = '\n'; |
| 6510 | memcpy(zSql+nSql, zLine, nLine+1); |
| 6511 | nSql += nLine; |
| 6512 | } |
| 6513 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6514 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6515 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6516 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6517 | if( p->outCount ){ |
| 6518 | output_reset(p); |
| 6519 | p->outCount = 0; |
| 6520 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6521 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6522 | if( p->echoOn ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 6523 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6524 | } |
| 6525 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6526 | if( nSql && !_all_whitespace(zSql) ){ |
| 6527 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6528 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 6529 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 6530 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 6531 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6534 | /* |
| 6535 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6536 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6537 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6538 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6539 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6540 | if( clearFlag ){ |
| 6541 | free(home_dir); |
| 6542 | home_dir = 0; |
| 6543 | return 0; |
| 6544 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6545 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6546 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 6547 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 6548 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 6549 | { |
| 6550 | struct passwd *pwent; |
| 6551 | uid_t uid = getuid(); |
| 6552 | if( (pwent=getpwuid(uid)) != NULL) { |
| 6553 | home_dir = pwent->pw_dir; |
| 6554 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6555 | } |
| 6556 | #endif |
| 6557 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6558 | #if defined(_WIN32_WCE) |
| 6559 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 6560 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6561 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6562 | #else |
| 6563 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6564 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6565 | if (!home_dir) { |
| 6566 | home_dir = getenv("USERPROFILE"); |
| 6567 | } |
| 6568 | #endif |
| 6569 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6570 | if (!home_dir) { |
| 6571 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6574 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6575 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6576 | char *zDrive, *zPath; |
| 6577 | int n; |
| 6578 | zDrive = getenv("HOMEDRIVE"); |
| 6579 | zPath = getenv("HOMEPATH"); |
| 6580 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6581 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6582 | home_dir = malloc( n ); |
| 6583 | if( home_dir==0 ) return 0; |
| 6584 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 6585 | return home_dir; |
| 6586 | } |
| 6587 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6588 | } |
| 6589 | #endif |
| 6590 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6591 | #endif /* !_WIN32_WCE */ |
| 6592 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6593 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6594 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6595 | char *z = malloc( n ); |
| 6596 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6597 | home_dir = z; |
| 6598 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6599 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6600 | return home_dir; |
| 6601 | } |
| 6602 | |
| 6603 | /* |
| 6604 | ** Read input from the file given by sqliterc_override. Or if that |
| 6605 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6606 | ** |
| 6607 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6608 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6609 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6610 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6611 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 6612 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6613 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6614 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6615 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6616 | FILE *in = NULL; |
| 6617 | |
| 6618 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6619 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6620 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6621 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6622 | " cannot read ~/.sqliterc\n"); |
| 6623 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6624 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 6625 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6626 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 6627 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6628 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 6629 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6630 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6631 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6632 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6633 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6634 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 6635 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6636 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6637 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6640 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6641 | ** Show available command line options |
| 6642 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6643 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6644 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6645 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6646 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6647 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 6648 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6649 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6650 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 6651 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6652 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6653 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 6654 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 6655 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6656 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6657 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6658 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6659 | " -line set output mode to 'line'\n" |
| 6660 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6661 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 6662 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6663 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 6664 | " -multiplex enable the multiplexor VFS\n" |
| 6665 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6666 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6667 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6668 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 6669 | " -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] | 6670 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6671 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6672 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6673 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 6674 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 6675 | " -vfstrace enable tracing of all VFS calls\n" |
| 6676 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6677 | ; |
| 6678 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6679 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6680 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 6681 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 6682 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6683 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6684 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6685 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6686 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 6687 | } |
| 6688 | exit(1); |
| 6689 | } |
| 6690 | |
| 6691 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6692 | ** Initialize the state information in data |
| 6693 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6694 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6695 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6696 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 6697 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6698 | memcpy(data->colSeparator,SEP_Column, 2); |
| 6699 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6700 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6701 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 6702 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 6703 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6704 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6705 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 6706 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6709 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6710 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6711 | */ |
| 6712 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6713 | static void printBold(const char *zText){ |
| 6714 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 6715 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 6716 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 6717 | SetConsoleTextAttribute(out, |
| 6718 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 6719 | ); |
| 6720 | printf("%s", zText); |
| 6721 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6722 | } |
| 6723 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 6724 | static void printBold(const char *zText){ |
| 6725 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6726 | } |
| 6727 | #endif |
| 6728 | |
| 6729 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6730 | ** Get the argument to an --option. Throw an error and die if no argument |
| 6731 | ** is available. |
| 6732 | */ |
| 6733 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 6734 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6735 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6736 | argv[0], argv[argc-1]); |
| 6737 | exit(1); |
| 6738 | } |
| 6739 | return argv[i]; |
| 6740 | } |
| 6741 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6742 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 6743 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 6744 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 6745 | # else |
| 6746 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 6747 | # endif |
| 6748 | #endif |
| 6749 | |
| 6750 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 6751 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6752 | #else |
| 6753 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 6754 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6755 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6756 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6757 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6758 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6759 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6760 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 6761 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 6762 | int readStdin = 1; |
| 6763 | int nCmd = 0; |
| 6764 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6765 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6766 | setBinaryMode(stdin, 0); |
| 6767 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 6768 | stdin_is_interactive = isatty(0); |
| 6769 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6770 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 6771 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 6772 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6773 | 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] | 6774 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 6775 | exit(1); |
| 6776 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 6777 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6778 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6779 | #if !SQLITE_SHELL_IS_UTF8 |
| 6780 | sqlite3_initialize(); |
| 6781 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 6782 | if( argv==0 ){ |
| 6783 | raw_printf(stderr, "out of memory\n"); |
| 6784 | exit(1); |
| 6785 | } |
| 6786 | for(i=0; i<argc; i++){ |
| 6787 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 6788 | if( argv[i]==0 ){ |
| 6789 | raw_printf(stderr, "out of memory\n"); |
| 6790 | exit(1); |
| 6791 | } |
| 6792 | } |
| 6793 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 6794 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6795 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6796 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6797 | /* Make sure we have a valid signal handler early, before anything |
| 6798 | ** else is done. |
| 6799 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 6800 | #ifdef SIGINT |
| 6801 | signal(SIGINT, interrupt_handler); |
| 6802 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6803 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 6804 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 6805 | { |
| 6806 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 6807 | ** of a C-function that will provide the name of the database file. Use |
| 6808 | ** this compile-time option to embed this shell program in larger |
| 6809 | ** applications. */ |
| 6810 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 6811 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 6812 | warnInmemoryDb = 0; |
| 6813 | } |
| 6814 | #endif |
| 6815 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6816 | /* Do an initial pass through the command-line argument to locate |
| 6817 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 6818 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6819 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6820 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6821 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6822 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6823 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6824 | if( z[0]!='-' ){ |
| 6825 | if( data.zDbFilename==0 ){ |
| 6826 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 6827 | }else{ |
| 6828 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 6829 | ** mean that nothing is read from stdin */ |
| 6830 | readStdin = 0; |
| 6831 | nCmd++; |
| 6832 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 6833 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6834 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 6835 | exit(1); |
| 6836 | } |
| 6837 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6838 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6839 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6840 | if( z[1]=='-' ) z++; |
| 6841 | if( strcmp(z,"-separator")==0 |
| 6842 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6843 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6844 | || strcmp(z,"-cmd")==0 |
| 6845 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6846 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6847 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6848 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6849 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6850 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6851 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6852 | ** we do the actual processing of arguments later in a second pass. |
| 6853 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 6854 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6855 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 6856 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 6857 | const char *zSize; |
| 6858 | sqlite3_int64 szHeap; |
| 6859 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6860 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 6861 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 6862 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 6863 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 6864 | #else |
| 6865 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 6866 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6867 | }else if( strcmp(z,"-scratch")==0 ){ |
| 6868 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6869 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6870 | if( sz>400000 ) sz = 400000; |
| 6871 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6872 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6873 | if( n>10 ) n = 10; |
| 6874 | if( n<1 ) n = 1; |
| 6875 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 6876 | data.shellFlgs |= SHFLG_Scratch; |
| 6877 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 6878 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6879 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6880 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 6881 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6882 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 6883 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 6884 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6885 | data.shellFlgs |= SHFLG_Pagecache; |
| 6886 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 6887 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6888 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6889 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 6890 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 6891 | if( n<0 ) n = 0; |
| 6892 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 6893 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 6894 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6895 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 6896 | extern int vfstrace_register( |
| 6897 | const char *zTraceName, |
| 6898 | const char *zOldVfsName, |
| 6899 | int (*xOut)(const char*,void*), |
| 6900 | void *pOutArg, |
| 6901 | int makeDefault |
| 6902 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 6903 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 6904 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 6905 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6906 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 6907 | extern int sqlite3_multiple_initialize(const char*,int); |
| 6908 | sqlite3_multiplex_initialize(0, 1); |
| 6909 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 6910 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 6911 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 6912 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 6913 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6914 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6915 | if( pVfs ){ |
| 6916 | sqlite3_vfs_register(pVfs, 1); |
| 6917 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6918 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6919 | exit(1); |
| 6920 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6921 | } |
| 6922 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6923 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 6924 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6925 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 6926 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 6927 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6928 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 6929 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 6930 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6931 | } |
| 6932 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 6933 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6934 | /* Go ahead and open the database file if it already exists. If the |
| 6935 | ** file does not exist, delay opening it. This prevents empty database |
| 6936 | ** files from being created if a user mistypes the database name argument |
| 6937 | ** to the sqlite command-line tool. |
| 6938 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6939 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6940 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6943 | /* Process the initialization file if there is one. If no -init option |
| 6944 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 6945 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6946 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 6947 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6948 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6949 | /* Make a second pass through the command-line argument and set |
| 6950 | ** options. This second pass is delayed until after the initialization |
| 6951 | ** file is processed so that the command-line arguments will override |
| 6952 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 6953 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6954 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6955 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6956 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6957 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 6958 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6959 | i++; |
| 6960 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6961 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6962 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6963 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6964 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6965 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6966 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 6967 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6968 | }else if( strcmp(z,"-csv")==0 ){ |
| 6969 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6970 | memcpy(data.colSeparator,",",2); |
| 6971 | }else if( strcmp(z,"-ascii")==0 ){ |
| 6972 | data.mode = MODE_Ascii; |
| 6973 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6974 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6975 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6976 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6977 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6978 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6979 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6980 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6981 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6982 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6983 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6984 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 6985 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6986 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6987 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6988 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6989 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 6990 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6991 | data.echoOn = 1; |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 6992 | }else if( strcmp(z,"-eqp")==0 ){ |
| 6993 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6994 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 6995 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6996 | }else if( strcmp(z,"-stats")==0 ){ |
| 6997 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 6998 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 6999 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 7000 | }else if( strcmp(z,"-backslash")==0 ){ |
| 7001 | /* Undocumented command-line option: -backslash |
| 7002 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 7003 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 7004 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 7005 | */ |
| 7006 | data.backslashOn = 1; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7007 | }else if( strcmp(z,"-bail")==0 ){ |
| 7008 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7009 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7010 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 7011 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7012 | }else if( strcmp(z,"-interactive")==0 ){ |
| 7013 | stdin_is_interactive = 1; |
| 7014 | }else if( strcmp(z,"-batch")==0 ){ |
| 7015 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7016 | }else if( strcmp(z,"-heap")==0 ){ |
| 7017 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7018 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7019 | i+=2; |
| 7020 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7021 | i+=2; |
| 7022 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7023 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7024 | }else if( strcmp(z,"-mmap")==0 ){ |
| 7025 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7026 | }else if( strcmp(z,"-vfs")==0 ){ |
| 7027 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7028 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7029 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 7030 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7031 | #endif |
| 7032 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7033 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 7034 | i++; |
| 7035 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7036 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7037 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7038 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7039 | /* Run commands that follow -cmd first and separately from commands |
| 7040 | ** that simply appear on the command-line. This seems goofy. It would |
| 7041 | ** be better if all commands ran in the order that they appear. But |
| 7042 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7043 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7044 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7045 | if( z[0]=='.' ){ |
| 7046 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 7047 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7048 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7049 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7050 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 7051 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7052 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7053 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 7054 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7055 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7056 | if( bail_on_error ) return rc; |
| 7057 | } |
| 7058 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7059 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7060 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 7061 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7062 | return 1; |
| 7063 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7064 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7065 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7066 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7067 | if( !readStdin ){ |
| 7068 | /* Run all arguments that do not begin with '-' as if they were separate |
| 7069 | ** command-line inputs, except for the argToSkip argument which contains |
| 7070 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7071 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7072 | for(i=0; i<nCmd; i++){ |
| 7073 | if( azCmd[i][0]=='.' ){ |
| 7074 | rc = do_meta_command(azCmd[i], &data); |
| 7075 | if( rc ) return rc==2 ? 0 : rc; |
| 7076 | }else{ |
| 7077 | open_db(&data, 0); |
| 7078 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 7079 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7080 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7081 | return rc!=0 ? rc : 1; |
| 7082 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7083 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7084 | return rc; |
| 7085 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 7086 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7087 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7088 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7089 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7090 | /* Run commands received from standard input |
| 7091 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7092 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7093 | char *zHome; |
| 7094 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7095 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7096 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 7097 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7098 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7099 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7100 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7101 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7102 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 7103 | printBold("transient in-memory database"); |
| 7104 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7105 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7106 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7107 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7108 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7109 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7110 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7111 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7112 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7113 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7114 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7115 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7116 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 7117 | shell_stifle_history(100); |
| 7118 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7119 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7120 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7121 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7122 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7123 | } |
| 7124 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 7125 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 7126 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 7127 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 7128 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7129 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7130 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7131 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7132 | #if !SQLITE_SHELL_IS_UTF8 |
| 7133 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7134 | sqlite3_free(argv); |
| 7135 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7136 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7137 | } |