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 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 459 | ** A global char* and an SQL function to access its current value |
| 460 | ** from within an SQL statement. This program used to use the |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 461 | ** sqlite_exec_printf() API to substitue a string into an SQL statement. |
| 462 | ** The correct way to do this with sqlite3 is to use the bind API, but |
| 463 | ** since the shell is built around the callback paradigm it would be a lot |
| 464 | ** of work. Instead just use this hack, which is quite harmless. |
| 465 | */ |
| 466 | static const char *zShellStatic = 0; |
| 467 | static void shellstaticFunc( |
| 468 | sqlite3_context *context, |
| 469 | int argc, |
| 470 | sqlite3_value **argv |
| 471 | ){ |
| 472 | assert( 0==argc ); |
| 473 | assert( zShellStatic ); |
shane | d87897d | 2009-01-30 05:40:27 +0000 | [diff] [blame] | 474 | UNUSED_PARAMETER(argc); |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 475 | UNUSED_PARAMETER(argv); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 476 | sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 481 | ** Compute a string length that is limited to what can be stored in |
| 482 | ** lower 30 bits of a 32-bit signed integer. |
| 483 | */ |
| 484 | static int strlen30(const char *z){ |
| 485 | const char *z2 = z; |
| 486 | while( *z2 ){ z2++; } |
| 487 | return 0x3fffffff & (int)(z2 - z); |
| 488 | } |
| 489 | |
| 490 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 491 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 492 | ** the text in memory obtained from malloc() and returns a pointer |
| 493 | ** to the text. NULL is returned at end of file, or if malloc() |
| 494 | ** fails. |
| 495 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 496 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 497 | ** a previous call to this routine that may be reused. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 498 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 499 | static char *local_getline(char *zLine, FILE *in){ |
| 500 | int nLine = zLine==0 ? 0 : 100; |
| 501 | int n = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 502 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 503 | while( 1 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 504 | if( n+100>nLine ){ |
| 505 | nLine = nLine*2 + 100; |
| 506 | zLine = realloc(zLine, nLine); |
| 507 | if( zLine==0 ) return 0; |
| 508 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 509 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 510 | if( n==0 ){ |
| 511 | free(zLine); |
| 512 | return 0; |
| 513 | } |
| 514 | zLine[n] = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 517 | while( zLine[n] ) n++; |
| 518 | if( n>0 && zLine[n-1]=='\n' ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 519 | n--; |
shaneh | 13b3602 | 2009-12-17 21:07:15 +0000 | [diff] [blame] | 520 | if( n>0 && zLine[n-1]=='\r' ) n--; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 521 | zLine[n] = 0; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 522 | break; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 525 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 526 | /* For interactive input on Windows systems, translate the |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 527 | ** multi-byte characterset characters into UTF-8. */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 528 | if( stdin_is_interactive && in==stdin ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 529 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 530 | if( zTrans ){ |
| 531 | int nTrans = strlen30(zTrans)+1; |
| 532 | if( nTrans>nLine ){ |
| 533 | zLine = realloc(zLine, nTrans); |
| 534 | if( zLine==0 ){ |
| 535 | sqlite3_free(zTrans); |
| 536 | return 0; |
| 537 | } |
| 538 | } |
| 539 | memcpy(zLine, zTrans, nTrans); |
| 540 | sqlite3_free(zTrans); |
| 541 | } |
| 542 | } |
| 543 | #endif /* defined(_WIN32) || defined(WIN32) */ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 544 | return zLine; |
| 545 | } |
| 546 | |
| 547 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 548 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 549 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 550 | ** If in==0 then read from standard input and prompt before each line. |
| 551 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 552 | ** If isContinuation is zero, then the main prompt should be used. |
| 553 | ** |
| 554 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 555 | ** routine that can be reused. |
| 556 | ** |
| 557 | ** The result is stored in space obtained from malloc() and must either |
| 558 | ** be freed by the caller or else passed back into this routine via the |
| 559 | ** zPrior argument for reuse. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 560 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 561 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 562 | char *zPrompt; |
| 563 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 564 | if( in!=0 ){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 565 | zResult = local_getline(zPrior, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 566 | }else{ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 567 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 568 | #if SHELL_USE_LOCAL_GETLINE |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 569 | printf("%s", zPrompt); |
| 570 | fflush(stdout); |
| 571 | zResult = local_getline(zPrior, stdin); |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 572 | #else |
| 573 | free(zPrior); |
| 574 | zResult = shell_readline(zPrompt); |
| 575 | if( zResult && *zResult ) shell_add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 576 | #endif |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 577 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 578 | return zResult; |
| 579 | } |
| 580 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 581 | #if defined(SQLITE_ENABLE_SESSION) |
| 582 | /* |
| 583 | ** State information for a single open session |
| 584 | */ |
| 585 | typedef struct OpenSession OpenSession; |
| 586 | struct OpenSession { |
| 587 | char *zName; /* Symbolic name for this session */ |
| 588 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 589 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 590 | sqlite3_session *p; /* The open session */ |
| 591 | }; |
| 592 | #endif |
| 593 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 594 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 595 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 596 | ** saved so that it can be restored by ".explain off" |
| 597 | */ |
| 598 | typedef struct SavedModeInfo SavedModeInfo; |
| 599 | struct SavedModeInfo { |
| 600 | int valid; /* Is there legit data in here? */ |
| 601 | int mode; /* Mode prior to ".explain on" */ |
| 602 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 603 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 604 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 605 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 606 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 607 | ** State information about the database connection is contained in an |
| 608 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 610 | typedef struct ShellState ShellState; |
| 611 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 612 | sqlite3 *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 613 | int echoOn; /* True to echo input commands */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 614 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 615 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 616 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 617 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 618 | int countChanges; /* True to display change counts */ |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 619 | int backslashOn; /* Resolve C-style \x escapes in SQL input text */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 620 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 621 | int cnt; /* Number of records displayed so far */ |
| 622 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 623 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 624 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 625 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 626 | int cMode; /* temporary output mode for the current query */ |
| 627 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 628 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 629 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 630 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 631 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 632 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 633 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 634 | char colSeparator[20]; /* Column separator character for several modes */ |
| 635 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 636 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 637 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 638 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 639 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 640 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 641 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 642 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 643 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 644 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 645 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 646 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 647 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 648 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 649 | #if defined(SQLITE_ENABLE_SESSION) |
| 650 | int nSession; /* Number of active sessions */ |
| 651 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 652 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 656 | ** These are the allowed shellFlgs values |
| 657 | */ |
| 658 | #define SHFLG_Scratch 0x00001 /* The --scratch option is used */ |
| 659 | #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ |
| 660 | #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ |
| 661 | |
| 662 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 663 | ** These are the allowed modes. |
| 664 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 665 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 666 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 667 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 668 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 669 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 670 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 671 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 672 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 673 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 674 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 675 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 676 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 677 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 678 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 679 | "line", |
| 680 | "column", |
| 681 | "list", |
| 682 | "semi", |
| 683 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 684 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 685 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 686 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 687 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 688 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 689 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 690 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 691 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 692 | |
| 693 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 694 | ** These are the column/row/line separators used by the various |
| 695 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 696 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 697 | #define SEP_Column "|" |
| 698 | #define SEP_Row "\n" |
| 699 | #define SEP_Tab "\t" |
| 700 | #define SEP_Space " " |
| 701 | #define SEP_Comma "," |
| 702 | #define SEP_CrLf "\r\n" |
| 703 | #define SEP_Unit "\x1F" |
| 704 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 705 | |
| 706 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 707 | ** Number of elements in an array |
| 708 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 709 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 710 | |
| 711 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 712 | ** A callback for the sqlite3_log() interface. |
| 713 | */ |
| 714 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 715 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 716 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 717 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 718 | fflush(p->pLog); |
| 719 | } |
| 720 | |
| 721 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 722 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 723 | */ |
| 724 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 725 | int i; |
| 726 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 727 | raw_printf(out,"X'"); |
| 728 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 729 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 733 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 734 | */ |
| 735 | static void output_quoted_string(FILE *out, const char *z){ |
| 736 | int i; |
| 737 | int nSingle = 0; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 738 | setBinaryMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 739 | for(i=0; z[i]; i++){ |
| 740 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 741 | } |
| 742 | if( nSingle==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 743 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 744 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 745 | raw_printf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 746 | while( *z ){ |
| 747 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 748 | if( i==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 749 | raw_printf(out,"''"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 750 | z++; |
| 751 | }else if( z[i]=='\'' ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 752 | utf8_printf(out,"%.*s''",i,z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 753 | z += i+1; |
| 754 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 755 | utf8_printf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 756 | break; |
| 757 | } |
| 758 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 759 | raw_printf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 760 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 761 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 765 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 766 | */ |
| 767 | static void output_c_string(FILE *out, const char *z){ |
| 768 | unsigned int c; |
| 769 | fputc('"', out); |
| 770 | while( (c = *(z++))!=0 ){ |
| 771 | if( c=='\\' ){ |
| 772 | fputc(c, out); |
| 773 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 774 | }else if( c=='"' ){ |
| 775 | fputc('\\', out); |
| 776 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 777 | }else if( c=='\t' ){ |
| 778 | fputc('\\', out); |
| 779 | fputc('t', out); |
| 780 | }else if( c=='\n' ){ |
| 781 | fputc('\\', out); |
| 782 | fputc('n', out); |
| 783 | }else if( c=='\r' ){ |
| 784 | fputc('\\', out); |
| 785 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 786 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 787 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 788 | }else{ |
| 789 | fputc(c, out); |
| 790 | } |
| 791 | } |
| 792 | fputc('"', out); |
| 793 | } |
| 794 | |
| 795 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 796 | ** Output the given string with characters that are special to |
| 797 | ** HTML escaped. |
| 798 | */ |
| 799 | static void output_html_string(FILE *out, const char *z){ |
| 800 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 801 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 802 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 803 | for(i=0; z[i] |
| 804 | && z[i]!='<' |
| 805 | && z[i]!='&' |
| 806 | && z[i]!='>' |
| 807 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 808 | && z[i]!='\''; |
| 809 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 810 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 811 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 812 | } |
| 813 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 814 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 815 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 816 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 817 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 818 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 819 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 820 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 821 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 822 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 823 | }else{ |
| 824 | break; |
| 825 | } |
| 826 | z += i + 1; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 831 | ** If a field contains any character identified by a 1 in the following |
| 832 | ** array, then the string must be quoted for CSV. |
| 833 | */ |
| 834 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 835 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 836 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 837 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 838 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 839 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 840 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 841 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 842 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 843 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 844 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 845 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 846 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 847 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 848 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 849 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 850 | 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] | 851 | }; |
| 852 | |
| 853 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 854 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 855 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 856 | ** the null value. Strings are quoted if necessary. The separator |
| 857 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 858 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 859 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 860 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 861 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 862 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 863 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 864 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 865 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 866 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 867 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 868 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 869 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 870 | i = 0; |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | if( i==0 ){ |
| 875 | putc('"', out); |
| 876 | for(i=0; z[i]; i++){ |
| 877 | if( z[i]=='"' ) putc('"', out); |
| 878 | putc(z[i], out); |
| 879 | } |
| 880 | putc('"', out); |
| 881 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 882 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 883 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 884 | } |
| 885 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 886 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 890 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 891 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 892 | ** This routine runs when the user presses Ctrl-C |
| 893 | */ |
| 894 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 895 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 896 | seenInterrupt++; |
| 897 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 898 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 899 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 900 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 901 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 902 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 903 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 904 | ** When the ".auth ON" is set, the following authorizer callback is |
| 905 | ** invoked. It always returns SQLITE_OK. |
| 906 | */ |
| 907 | static int shellAuth( |
| 908 | void *pClientData, |
| 909 | int op, |
| 910 | const char *zA1, |
| 911 | const char *zA2, |
| 912 | const char *zA3, |
| 913 | const char *zA4 |
| 914 | ){ |
| 915 | ShellState *p = (ShellState*)pClientData; |
| 916 | static const char *azAction[] = { 0, |
| 917 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 918 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 919 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 920 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 921 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 922 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 923 | "PRAGMA", "READ", "SELECT", |
| 924 | "TRANSACTION", "UPDATE", "ATTACH", |
| 925 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 926 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 927 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 928 | }; |
| 929 | int i; |
| 930 | const char *az[4]; |
| 931 | az[0] = zA1; |
| 932 | az[1] = zA2; |
| 933 | az[2] = zA3; |
| 934 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 935 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 936 | for(i=0; i<4; i++){ |
| 937 | raw_printf(p->out, " "); |
| 938 | if( az[i] ){ |
| 939 | output_c_string(p->out, az[i]); |
| 940 | }else{ |
| 941 | raw_printf(p->out, "NULL"); |
| 942 | } |
| 943 | } |
| 944 | raw_printf(p->out, "\n"); |
| 945 | return SQLITE_OK; |
| 946 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 947 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 948 | |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 949 | |
| 950 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 951 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 952 | ** invokes for each row of a query result. |
| 953 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 954 | static int shell_callback( |
| 955 | void *pArg, |
| 956 | int nArg, /* Number of result columns */ |
| 957 | char **azArg, /* Text of each result column */ |
| 958 | char **azCol, /* Column names */ |
| 959 | int *aiType /* Column types */ |
| 960 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 961 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 962 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 963 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 964 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 965 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 966 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 967 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 968 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 969 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 970 | if( len>w ) w = len; |
| 971 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 972 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 973 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 974 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 975 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 976 | } |
| 977 | break; |
| 978 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 979 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 980 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 981 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 982 | const int *colWidth; |
| 983 | int showHdr; |
| 984 | char *rowSep; |
| 985 | if( p->cMode==MODE_Column ){ |
| 986 | colWidth = p->colWidth; |
| 987 | showHdr = p->showHeader; |
| 988 | rowSep = p->rowSeparator; |
| 989 | }else{ |
| 990 | colWidth = aExplainWidths; |
| 991 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 992 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 993 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 994 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 995 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 996 | int w, n; |
| 997 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 998 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 999 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1000 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1001 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1002 | if( w==0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1003 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1004 | if( w<10 ) w = 10; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1005 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1006 | if( w<n ) w = n; |
| 1007 | } |
| 1008 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1009 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1010 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1011 | if( showHdr ){ |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1012 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1013 | utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1014 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1015 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1016 | utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1017 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1018 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1021 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1022 | for(i=0; i<nArg; i++){ |
| 1023 | int w; |
| 1024 | if( i<ArraySize(p->actualWidth) ){ |
| 1025 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1026 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1027 | }else{ |
| 1028 | w = 10; |
| 1029 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1030 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1031 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1032 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1033 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1034 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1037 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1038 | for(i=0; i<nArg; i++){ |
| 1039 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1040 | if( i<ArraySize(p->actualWidth) ){ |
| 1041 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1042 | }else{ |
| 1043 | w = 10; |
| 1044 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1045 | if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1046 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1047 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1048 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1049 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1050 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1051 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1052 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1053 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1054 | if( w<0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1055 | utf8_printf(p->out,"%*.*s%s",-w,-w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1056 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1057 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1058 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1059 | utf8_printf(p->out,"%-*.*s%s",w,w, |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1060 | azArg[i] ? azArg[i] : p->nullValue, |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1061 | i==nArg-1 ? rowSep : " "); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1062 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1063 | } |
| 1064 | break; |
| 1065 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1066 | case MODE_Semi: { /* .schema and .fullschema output */ |
| 1067 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1068 | break; |
| 1069 | } |
| 1070 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1071 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1072 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1073 | int nParen = 0; |
| 1074 | char cEnd = 0; |
| 1075 | char c; |
| 1076 | int nLine = 0; |
| 1077 | assert( nArg==1 ); |
| 1078 | if( azArg[0]==0 ) break; |
| 1079 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1080 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1081 | ){ |
| 1082 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1083 | break; |
| 1084 | } |
| 1085 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1086 | j = 0; |
| 1087 | for(i=0; IsSpace(z[i]); i++){} |
| 1088 | for(; (c = z[i])!=0; i++){ |
| 1089 | if( IsSpace(c) ){ |
| 1090 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1091 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1092 | j--; |
| 1093 | } |
| 1094 | z[j++] = c; |
| 1095 | } |
| 1096 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1097 | z[j] = 0; |
| 1098 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1099 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1100 | if( c==cEnd ){ |
| 1101 | cEnd = 0; |
| 1102 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1103 | cEnd = c; |
| 1104 | }else if( c=='[' ){ |
| 1105 | cEnd = ']'; |
| 1106 | }else if( c=='(' ){ |
| 1107 | nParen++; |
| 1108 | }else if( c==')' ){ |
| 1109 | nParen--; |
| 1110 | if( nLine>0 && nParen==0 && j>0 ){ |
| 1111 | utf8_printf(p->out, "%.*s\n", j, z); |
| 1112 | j = 0; |
| 1113 | } |
| 1114 | } |
| 1115 | z[j++] = c; |
| 1116 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 1117 | if( c=='\n' ) j--; |
| 1118 | utf8_printf(p->out, "%.*s\n ", j, z); |
| 1119 | j = 0; |
| 1120 | nLine++; |
| 1121 | while( IsSpace(z[i+1]) ){ i++; } |
| 1122 | } |
| 1123 | } |
| 1124 | z[j] = 0; |
| 1125 | } |
| 1126 | utf8_printf(p->out, "%s;\n", z); |
| 1127 | sqlite3_free(z); |
| 1128 | break; |
| 1129 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1130 | case MODE_List: { |
| 1131 | if( p->cnt++==0 && p->showHeader ){ |
| 1132 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1133 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1134 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1137 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1138 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1139 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1140 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1141 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1142 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1143 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1144 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1145 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1146 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1147 | } |
| 1148 | break; |
| 1149 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1150 | case MODE_Html: { |
| 1151 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1152 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1153 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1154 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1155 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1156 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1157 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1158 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1159 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1160 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1161 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1162 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1163 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1164 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1165 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1166 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1167 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1168 | break; |
| 1169 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1170 | case MODE_Tcl: { |
| 1171 | if( p->cnt++==0 && p->showHeader ){ |
| 1172 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1173 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1174 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1175 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1176 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1177 | } |
| 1178 | if( azArg==0 ) break; |
| 1179 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1180 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1181 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1182 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1183 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1186 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1187 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1188 | if( p->cnt++==0 && p->showHeader ){ |
| 1189 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1190 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1191 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1192 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1193 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 1194 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1195 | for(i=0; i<nArg; i++){ |
| 1196 | output_csv(p, azArg[i], i<nArg-1); |
| 1197 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1198 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1199 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1200 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1203 | case MODE_Quote: |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1204 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1205 | if( azArg==0 ) break; |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1206 | if( p->cMode==MODE_Insert ){ |
| 1207 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 1208 | if( p->showHeader ){ |
| 1209 | raw_printf(p->out,"("); |
| 1210 | for(i=0; i<nArg; i++){ |
| 1211 | char *zSep = i>0 ? ",": ""; |
| 1212 | utf8_printf(p->out, "%s%s", zSep, azCol[i]); |
| 1213 | } |
| 1214 | raw_printf(p->out,")"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 1215 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1216 | raw_printf(p->out," VALUES("); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1217 | }else if( p->cnt==0 && p->showHeader ){ |
| 1218 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 1219 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1220 | output_quoted_string(p->out, azCol[i]); |
| 1221 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 1222 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 1223 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 1224 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1225 | for(i=0; i<nArg; i++){ |
| 1226 | char *zSep = i>0 ? ",": ""; |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 1227 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1228 | utf8_printf(p->out,"%sNULL",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 1229 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1230 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 1231 | output_quoted_string(p->out, azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1232 | }else if( aiType && (aiType[i]==SQLITE_INTEGER |
| 1233 | || aiType[i]==SQLITE_FLOAT) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1234 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1235 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 1236 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 1237 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1238 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1239 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1240 | }else if( isNumber(azArg[i], 0) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1241 | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1242 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1243 | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1244 | output_quoted_string(p->out, azArg[i]); |
| 1245 | } |
| 1246 | } |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1247 | raw_printf(p->out,p->cMode==MODE_Quote?"\n":");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1248 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1249 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1250 | case MODE_Ascii: { |
| 1251 | if( p->cnt++==0 && p->showHeader ){ |
| 1252 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1253 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 1254 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1255 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1256 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1257 | } |
| 1258 | if( azArg==0 ) break; |
| 1259 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1260 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 1261 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1262 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1263 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1266 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1271 | ** This is the callback routine that the SQLite library |
| 1272 | ** invokes for each row of a query result. |
| 1273 | */ |
| 1274 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 1275 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 1276 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 1277 | } |
| 1278 | |
| 1279 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1280 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1281 | ** the name of the table given. Escape any quote characters in the |
| 1282 | ** table name. |
| 1283 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1284 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1285 | int i, n; |
| 1286 | int needQuote; |
| 1287 | char *z; |
| 1288 | |
| 1289 | if( p->zDestTable ){ |
| 1290 | free(p->zDestTable); |
| 1291 | p->zDestTable = 0; |
| 1292 | } |
| 1293 | if( zName==0 ) return; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1294 | needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1295 | for(i=n=0; zName[i]; i++, n++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1296 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1297 | needQuote = 1; |
| 1298 | if( zName[i]=='\'' ) n++; |
| 1299 | } |
| 1300 | } |
| 1301 | if( needQuote ) n += 2; |
| 1302 | z = p->zDestTable = malloc( n+1 ); |
| 1303 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1304 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1305 | exit(1); |
| 1306 | } |
| 1307 | n = 0; |
| 1308 | if( needQuote ) z[n++] = '\''; |
| 1309 | for(i=0; zName[i]; i++){ |
| 1310 | z[n++] = zName[i]; |
| 1311 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 1312 | } |
| 1313 | if( needQuote ) z[n++] = '\''; |
| 1314 | z[n] = 0; |
| 1315 | } |
| 1316 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1317 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 1318 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 1319 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 1320 | ** zIn, if it was not NULL, is freed. |
| 1321 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1322 | ** If the third argument, quote, is not '\0', then it is used as a |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1323 | ** quote character for zAppend. |
| 1324 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1325 | static char *appendText(char *zIn, char const *zAppend, char quote){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1326 | int len; |
| 1327 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1328 | int nAppend = strlen30(zAppend); |
| 1329 | int nIn = (zIn?strlen30(zIn):0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1330 | |
| 1331 | len = nAppend+nIn+1; |
| 1332 | if( quote ){ |
| 1333 | len += 2; |
| 1334 | for(i=0; i<nAppend; i++){ |
| 1335 | if( zAppend[i]==quote ) len++; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | zIn = (char *)realloc(zIn, len); |
| 1340 | if( !zIn ){ |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | if( quote ){ |
| 1345 | char *zCsr = &zIn[nIn]; |
| 1346 | *zCsr++ = quote; |
| 1347 | for(i=0; i<nAppend; i++){ |
| 1348 | *zCsr++ = zAppend[i]; |
| 1349 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 1350 | } |
| 1351 | *zCsr++ = quote; |
| 1352 | *zCsr++ = '\0'; |
| 1353 | assert( (zCsr-zIn)==len ); |
| 1354 | }else{ |
| 1355 | memcpy(&zIn[nIn], zAppend, nAppend); |
| 1356 | zIn[len-1] = '\0'; |
| 1357 | } |
| 1358 | |
| 1359 | return zIn; |
| 1360 | } |
| 1361 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1364 | ** Execute a query statement that will generate SQL output. Print |
| 1365 | ** the result columns, comma-separated, on a line and then add a |
| 1366 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1367 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1368 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1369 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1370 | ** "--" comment occurs at the end of the statement, the comment |
| 1371 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1372 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1373 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1374 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1375 | const char *zSelect, /* SELECT statement to extract content */ |
| 1376 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1377 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1378 | sqlite3_stmt *pSelect; |
| 1379 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1380 | int nResult; |
| 1381 | int i; |
| 1382 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 1383 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1384 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1385 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 1386 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 1387 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1388 | return rc; |
| 1389 | } |
| 1390 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1391 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1392 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1393 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1394 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1395 | zFirstRow = 0; |
| 1396 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1397 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1398 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1399 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1400 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1401 | } |
| 1402 | if( z==0 ) z = ""; |
| 1403 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 1404 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1405 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 1406 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1407 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1408 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1409 | rc = sqlite3_step(pSelect); |
| 1410 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1411 | rc = sqlite3_finalize(pSelect); |
| 1412 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1413 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 1414 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 1415 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1416 | } |
| 1417 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1420 | /* |
| 1421 | ** Allocate space and save off current error string. |
| 1422 | */ |
| 1423 | static char *save_err_msg( |
| 1424 | sqlite3 *db /* Database to query */ |
| 1425 | ){ |
| 1426 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1427 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1428 | if( zErrMsg ){ |
| 1429 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 1430 | } |
| 1431 | return zErrMsg; |
| 1432 | } |
| 1433 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 1434 | #ifdef __linux__ |
| 1435 | /* |
| 1436 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 1437 | */ |
| 1438 | static void displayLinuxIoStats(FILE *out){ |
| 1439 | FILE *in; |
| 1440 | char z[200]; |
| 1441 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 1442 | in = fopen(z, "rb"); |
| 1443 | if( in==0 ) return; |
| 1444 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 1445 | static const struct { |
| 1446 | const char *zPattern; |
| 1447 | const char *zDesc; |
| 1448 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 1449 | { "rchar: ", "Bytes received by read():" }, |
| 1450 | { "wchar: ", "Bytes sent to write():" }, |
| 1451 | { "syscr: ", "Read() system calls:" }, |
| 1452 | { "syscw: ", "Write() system calls:" }, |
| 1453 | { "read_bytes: ", "Bytes read from storage:" }, |
| 1454 | { "write_bytes: ", "Bytes written to storage:" }, |
| 1455 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 1456 | }; |
| 1457 | int i; |
| 1458 | for(i=0; i<ArraySize(aTrans); i++){ |
| 1459 | int n = (int)strlen(aTrans[i].zPattern); |
| 1460 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1461 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1467 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 1468 | #endif |
| 1469 | |
| 1470 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1471 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1472 | ** Display memory stats. |
| 1473 | */ |
| 1474 | static int display_stats( |
| 1475 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1476 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1477 | int bReset /* True to reset the stats */ |
| 1478 | ){ |
| 1479 | int iCur; |
| 1480 | int iHiwtr; |
| 1481 | |
| 1482 | if( pArg && pArg->out ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1483 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1484 | iHiwtr = iCur = -1; |
| 1485 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1486 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1487 | "Memory Used: %d (max %d) bytes\n", |
| 1488 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1489 | iHiwtr = iCur = -1; |
| 1490 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1491 | raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1492 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1493 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
| 1494 | iHiwtr = iCur = -1; |
| 1495 | sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1496 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1497 | "Number of Pcache Pages Used: %d (max %d) pages\n", |
| 1498 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1499 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1500 | iHiwtr = iCur = -1; |
| 1501 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1502 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1503 | "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", |
| 1504 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1505 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
| 1506 | iHiwtr = iCur = -1; |
| 1507 | sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1508 | raw_printf(pArg->out, |
| 1509 | "Number of Scratch Allocations Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1510 | iCur, iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1511 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1512 | iHiwtr = iCur = -1; |
| 1513 | sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1514 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1515 | "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", |
| 1516 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1517 | iHiwtr = iCur = -1; |
| 1518 | sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1519 | raw_printf(pArg->out, "Largest Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1520 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1521 | iHiwtr = iCur = -1; |
| 1522 | sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1523 | raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1524 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1525 | iHiwtr = iCur = -1; |
| 1526 | sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1527 | raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1528 | iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1529 | #ifdef YYTRACKMAXSTACKDEPTH |
| 1530 | iHiwtr = iCur = -1; |
| 1531 | sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1532 | raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1533 | iCur, iHiwtr); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1534 | #endif |
| 1535 | } |
| 1536 | |
| 1537 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1538 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 1539 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1540 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 1541 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1542 | raw_printf(pArg->out, |
| 1543 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1544 | iCur, iHiwtr); |
| 1545 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 1546 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1547 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 1548 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1549 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 1550 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1551 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 1552 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1553 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 1554 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1555 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 1556 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1557 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1558 | iHiwtr = iCur = -1; |
| 1559 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1560 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 1561 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1562 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 1563 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1564 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 1565 | iHiwtr = iCur = -1; |
| 1566 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1567 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1568 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 1569 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1570 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 1571 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1572 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1573 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1574 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1575 | iHiwtr = iCur = -1; |
| 1576 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1577 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1578 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1582 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 1583 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1584 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1585 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1586 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1587 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1588 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 1589 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1590 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 1593 | #ifdef __linux__ |
| 1594 | displayLinuxIoStats(pArg->out); |
| 1595 | #endif |
| 1596 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 1597 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 1598 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1603 | ** Display scan stats. |
| 1604 | */ |
| 1605 | static void display_scanstats( |
| 1606 | sqlite3 *db, /* Database to query */ |
| 1607 | ShellState *pArg /* Pointer to ShellState */ |
| 1608 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 1609 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1610 | UNUSED_PARAMETER(db); |
| 1611 | UNUSED_PARAMETER(pArg); |
| 1612 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 1613 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1614 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 1615 | mx = 0; |
| 1616 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 1617 | double rEstLoop = 1.0; |
| 1618 | for(i=n=0; 1; i++){ |
| 1619 | sqlite3_stmt *p = pArg->pStmt; |
| 1620 | sqlite3_int64 nLoop, nVisit; |
| 1621 | double rEst; |
| 1622 | int iSid; |
| 1623 | const char *zExplain; |
| 1624 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 1625 | break; |
| 1626 | } |
| 1627 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 1628 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 1629 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 1630 | if( n==0 ){ |
| 1631 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1632 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 1633 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 1634 | n++; |
| 1635 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 1636 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 1637 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1638 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 1639 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1640 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1641 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 1642 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 1643 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1644 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1645 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1646 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 1647 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1651 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 1652 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 1653 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 1654 | ** Otherwise, return zero. |
| 1655 | */ |
| 1656 | static int str_in_array(const char *zStr, const char **azArray){ |
| 1657 | int i; |
| 1658 | for(i=0; azArray[i]; i++){ |
| 1659 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 1660 | } |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1666 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1667 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1668 | ** |
| 1669 | ** The indenting rules are: |
| 1670 | ** |
| 1671 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 1672 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 1673 | ** itself by 2 spaces. |
| 1674 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 1675 | ** * For each "Goto", if the jump destination is earlier in the program |
| 1676 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 1677 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 1678 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 1679 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 1680 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1681 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1682 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1683 | const char *zSql; /* The text of the SQL statement */ |
| 1684 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 1685 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 1686 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1687 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1688 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 1689 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 1690 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1691 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 1692 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1693 | const char *azGoto[] = { "Goto", 0 }; |
| 1694 | |
| 1695 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 1696 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 1697 | if( sqlite3_column_count(pSql)!=8 ){ |
| 1698 | p->cMode = p->mode; |
| 1699 | return; |
| 1700 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1701 | zSql = sqlite3_sql(pSql); |
| 1702 | if( zSql==0 ) return; |
| 1703 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 1704 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 1705 | p->cMode = p->mode; |
| 1706 | return; |
| 1707 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1708 | |
| 1709 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 1710 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1711 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1712 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1713 | |
| 1714 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 1715 | ** p2 is an instruction address, set variable p2op to the index of that |
| 1716 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 1717 | ** the current instruction is part of a sub-program generated by an |
| 1718 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1719 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1720 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1721 | |
| 1722 | /* Grow the p->aiIndent array as required */ |
| 1723 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 1724 | if( iOp==0 ){ |
| 1725 | /* Do further verfication that this is explain output. Abort if |
| 1726 | ** it is not */ |
| 1727 | static const char *explainCols[] = { |
| 1728 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 1729 | int jj; |
| 1730 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 1731 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 1732 | p->cMode = p->mode; |
| 1733 | sqlite3_reset(pSql); |
| 1734 | return; |
| 1735 | } |
| 1736 | } |
| 1737 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1738 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1739 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 1740 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1741 | } |
| 1742 | abYield[iOp] = str_in_array(zOp, azYield); |
| 1743 | p->aiIndent[iOp] = 0; |
| 1744 | p->nIndent = iOp+1; |
| 1745 | |
| 1746 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1747 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1748 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 1749 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 1750 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 1751 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1752 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1756 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1757 | sqlite3_free(abYield); |
| 1758 | sqlite3_reset(pSql); |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | ** Free the array allocated by explain_data_prepare(). |
| 1763 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1764 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1765 | sqlite3_free(p->aiIndent); |
| 1766 | p->aiIndent = 0; |
| 1767 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1768 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1772 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 1773 | */ |
| 1774 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1775 | extern int sqlite3SelectTrace; |
| 1776 | static int savedSelectTrace; |
| 1777 | #endif |
| 1778 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1779 | extern int sqlite3WhereTrace; |
| 1780 | static int savedWhereTrace; |
| 1781 | #endif |
| 1782 | static void disable_debug_trace_modes(void){ |
| 1783 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1784 | savedSelectTrace = sqlite3SelectTrace; |
| 1785 | sqlite3SelectTrace = 0; |
| 1786 | #endif |
| 1787 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1788 | savedWhereTrace = sqlite3WhereTrace; |
| 1789 | sqlite3WhereTrace = 0; |
| 1790 | #endif |
| 1791 | } |
| 1792 | static void restore_debug_trace_modes(void){ |
| 1793 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 1794 | sqlite3SelectTrace = savedSelectTrace; |
| 1795 | #endif |
| 1796 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 1797 | sqlite3WhereTrace = savedWhereTrace; |
| 1798 | #endif |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | ** Run a prepared statement |
| 1803 | */ |
| 1804 | static void exec_prepared_stmt( |
| 1805 | ShellState *pArg, /* Pointer to ShellState */ |
| 1806 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 1807 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 1808 | ){ |
| 1809 | int rc; |
| 1810 | |
| 1811 | /* perform the first step. this will tell us if we |
| 1812 | ** have a result set or not and how wide it is. |
| 1813 | */ |
| 1814 | rc = sqlite3_step(pStmt); |
| 1815 | /* if we have a result set... */ |
| 1816 | if( SQLITE_ROW == rc ){ |
| 1817 | /* if we have a callback... */ |
| 1818 | if( xCallback ){ |
| 1819 | /* allocate space for col name ptr, value ptr, and type */ |
| 1820 | int nCol = sqlite3_column_count(pStmt); |
| 1821 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 1822 | if( !pData ){ |
| 1823 | rc = SQLITE_NOMEM; |
| 1824 | }else{ |
| 1825 | char **azCols = (char **)pData; /* Names of result columns */ |
| 1826 | char **azVals = &azCols[nCol]; /* Results */ |
| 1827 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 1828 | int i, x; |
| 1829 | assert(sizeof(int) <= sizeof(char *)); |
| 1830 | /* save off ptrs to column names */ |
| 1831 | for(i=0; i<nCol; i++){ |
| 1832 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 1833 | } |
| 1834 | do{ |
| 1835 | /* extract the data and data types */ |
| 1836 | for(i=0; i<nCol; i++){ |
| 1837 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 1838 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 1839 | azVals[i] = ""; |
| 1840 | }else{ |
| 1841 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 1842 | } |
| 1843 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 1844 | rc = SQLITE_NOMEM; |
| 1845 | break; /* from for */ |
| 1846 | } |
| 1847 | } /* end for */ |
| 1848 | |
| 1849 | /* if data and types extracted successfully... */ |
| 1850 | if( SQLITE_ROW == rc ){ |
| 1851 | /* call the supplied callback with the result row data */ |
| 1852 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 1853 | rc = SQLITE_ABORT; |
| 1854 | }else{ |
| 1855 | rc = sqlite3_step(pStmt); |
| 1856 | } |
| 1857 | } |
| 1858 | } while( SQLITE_ROW == rc ); |
| 1859 | sqlite3_free(pData); |
| 1860 | } |
| 1861 | }else{ |
| 1862 | do{ |
| 1863 | rc = sqlite3_step(pStmt); |
| 1864 | } while( rc == SQLITE_ROW ); |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1870 | ** Execute a statement or set of statements. Print |
| 1871 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1872 | ** set via the supplied callback. |
| 1873 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1874 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 1875 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1876 | ** and callback data argument. |
| 1877 | */ |
| 1878 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1879 | sqlite3 *db, /* An open database */ |
| 1880 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1881 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1882 | /* (not the same as sqlite3_exec) */ |
| 1883 | ShellState *pArg, /* Pointer to ShellState */ |
| 1884 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1885 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 1886 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 1887 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1888 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 1889 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1890 | |
| 1891 | if( pzErrMsg ){ |
| 1892 | *pzErrMsg = NULL; |
| 1893 | } |
| 1894 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1895 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1896 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1897 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 1898 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1899 | if( pzErrMsg ){ |
| 1900 | *pzErrMsg = save_err_msg(db); |
| 1901 | } |
| 1902 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1903 | if( !pStmt ){ |
| 1904 | /* this happens for a comment or white-space */ |
| 1905 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 1906 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1907 | continue; |
| 1908 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1909 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 1910 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1911 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1912 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1913 | /* save off the prepared statment handle and reset row count */ |
| 1914 | if( pArg ){ |
| 1915 | pArg->pStmt = pStmt; |
| 1916 | pArg->cnt = 0; |
| 1917 | } |
| 1918 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 1919 | /* echo the sql statement if echo on */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1920 | if( pArg && pArg->echoOn ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1921 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 1922 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 1923 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 1924 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1925 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 1926 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1927 | char *zEQP; |
| 1928 | disable_debug_trace_modes(); |
| 1929 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 1930 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 1931 | if( rc==SQLITE_OK ){ |
| 1932 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1933 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 1934 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 1935 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1936 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | sqlite3_finalize(pExplain); |
| 1940 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1941 | if( pArg->autoEQP>=2 ){ |
| 1942 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 1943 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 1944 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 1945 | if( rc==SQLITE_OK ){ |
| 1946 | pArg->cMode = MODE_Explain; |
| 1947 | explain_data_prepare(pArg, pExplain); |
| 1948 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 1949 | explain_data_delete(pArg); |
| 1950 | } |
| 1951 | sqlite3_finalize(pExplain); |
| 1952 | sqlite3_free(zEQP); |
| 1953 | } |
| 1954 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1957 | if( pArg ){ |
| 1958 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 1959 | if( pArg->autoExplain |
| 1960 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1961 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1962 | ){ |
| 1963 | pArg->cMode = MODE_Explain; |
| 1964 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1965 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1966 | /* If the shell is currently in ".explain" mode, gather the extra |
| 1967 | ** data required to add indents to the output.*/ |
| 1968 | if( pArg->cMode==MODE_Explain ){ |
| 1969 | explain_data_prepare(pArg, pStmt); |
| 1970 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 1973 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1974 | explain_data_delete(pArg); |
| 1975 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1976 | /* print usage stats if stats on */ |
| 1977 | if( pArg && pArg->statsOn ){ |
| 1978 | display_stats(db, pArg, 0); |
| 1979 | } |
| 1980 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1981 | /* print loop-counters if required */ |
| 1982 | if( pArg && pArg->scanstatsOn ){ |
| 1983 | display_scanstats(db, pArg); |
| 1984 | } |
| 1985 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1986 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 1987 | ** copy of the error message. Otherwise, set zSql to point to the |
| 1988 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1989 | rc2 = sqlite3_finalize(pStmt); |
| 1990 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 1991 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1992 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 1993 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 1994 | }else if( pzErrMsg ){ |
| 1995 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1996 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1997 | |
| 1998 | /* clear saved stmt handle */ |
| 1999 | if( pArg ){ |
| 2000 | pArg->pStmt = NULL; |
| 2001 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2002 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2003 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2004 | |
| 2005 | return rc; |
| 2006 | } |
| 2007 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2008 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2009 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2010 | ** This is a different callback routine used for dumping the database. |
| 2011 | ** Each row received by this callback consists of a table name, |
| 2012 | ** the table type ("index" or "table") and SQL to create the table. |
| 2013 | ** This routine should print text sufficient to recreate the table. |
| 2014 | */ |
| 2015 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2016 | int rc; |
| 2017 | const char *zTable; |
| 2018 | const char *zType; |
| 2019 | const char *zSql; |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2020 | const char *zPrepStmt = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2021 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2022 | |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 2023 | UNUSED_PARAMETER(azCol); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2024 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2025 | zTable = azArg[0]; |
| 2026 | zType = azArg[1]; |
| 2027 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2028 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2029 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2030 | zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 2031 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2032 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2033 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 2034 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2035 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 2036 | char *zIns; |
| 2037 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2038 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2039 | p->writableSchema = 1; |
| 2040 | } |
| 2041 | zIns = sqlite3_mprintf( |
| 2042 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 2043 | "VALUES('table','%q','%q',0,'%q');", |
| 2044 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2045 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2046 | sqlite3_free(zIns); |
| 2047 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 2048 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2049 | utf8_printf(p->out, "%s;\n", zSql); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 2050 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2051 | |
| 2052 | if( strcmp(zType, "table")==0 ){ |
| 2053 | sqlite3_stmt *pTableInfo = 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2054 | char *zSelect = 0; |
| 2055 | char *zTableInfo = 0; |
| 2056 | char *zTmp = 0; |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2057 | int nRow = 0; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2058 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2059 | zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 2060 | zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 2061 | zTableInfo = appendText(zTableInfo, ");", 0); |
| 2062 | |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2063 | rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2064 | free(zTableInfo); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2065 | if( rc!=SQLITE_OK || !pTableInfo ){ |
| 2066 | return 1; |
| 2067 | } |
| 2068 | |
| 2069 | zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 2070 | /* Always quote the table name, even if it appears to be pure ascii, |
| 2071 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
| 2072 | zTmp = appendText(zTmp, zTable, '"'); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2073 | if( zTmp ){ |
| 2074 | zSelect = appendText(zSelect, zTmp, '\''); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 2075 | free(zTmp); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2076 | } |
| 2077 | zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 2078 | rc = sqlite3_step(pTableInfo); |
| 2079 | while( rc==SQLITE_ROW ){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 2080 | const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 2081 | zSelect = appendText(zSelect, "quote(", 0); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 2082 | zSelect = appendText(zSelect, zText, '"'); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2083 | rc = sqlite3_step(pTableInfo); |
| 2084 | if( rc==SQLITE_ROW ){ |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2085 | zSelect = appendText(zSelect, "), ", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2086 | }else{ |
| 2087 | zSelect = appendText(zSelect, ") ", 0); |
| 2088 | } |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2089 | nRow++; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2090 | } |
| 2091 | rc = sqlite3_finalize(pTableInfo); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2092 | if( rc!=SQLITE_OK || nRow==0 ){ |
| 2093 | free(zSelect); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 2094 | return 1; |
| 2095 | } |
| 2096 | zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 2097 | zSelect = appendText(zSelect, zTable, '"'); |
| 2098 | |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2099 | rc = run_table_dump_query(p, zSelect, zPrepStmt); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2100 | if( rc==SQLITE_CORRUPT ){ |
| 2101 | zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2102 | run_table_dump_query(p, zSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2103 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 2104 | free(zSelect); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2105 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2110 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 2111 | ** the contents of the query are output as SQL statements. |
| 2112 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2113 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 2114 | ** "ORDER BY rowid DESC" to the end. |
| 2115 | */ |
| 2116 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2117 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2118 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2119 | ){ |
| 2120 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2121 | char *zErr = 0; |
| 2122 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2123 | if( rc==SQLITE_CORRUPT ){ |
| 2124 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2125 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2126 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2127 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2128 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2129 | sqlite3_free(zErr); |
| 2130 | zErr = 0; |
| 2131 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2132 | zQ2 = malloc( len+100 ); |
| 2133 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 2134 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2135 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 2136 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2137 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2138 | }else{ |
| 2139 | rc = SQLITE_CORRUPT; |
| 2140 | } |
| 2141 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2142 | free(zQ2); |
| 2143 | } |
| 2144 | return rc; |
| 2145 | } |
| 2146 | |
| 2147 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2148 | ** Text of a help message |
| 2149 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2150 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2151 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 2152 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2153 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2154 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2155 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2156 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 2157 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2158 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2159 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 2160 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 2161 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2162 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 2163 | " If TABLE specified, only dump tables matching\n" |
| 2164 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2165 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2166 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2167 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2168 | ".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] | 2169 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2170 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2171 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2172 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 2173 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 2174 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 2175 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 2176 | ".indexes ?TABLE? Show names of all indexes\n" |
| 2177 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 2178 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 2179 | #ifdef SQLITE_ENABLE_IOTRACE |
| 2180 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 2181 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 2182 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 2183 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 2184 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 2185 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 2186 | ".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] | 2187 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 2188 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 2189 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2190 | " column Left-aligned columns. (See .width)\n" |
| 2191 | " html HTML <table> code\n" |
| 2192 | " insert SQL insert statements for TABLE\n" |
| 2193 | " line One value per line\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 2194 | " list Values delimited by .separator strings\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2195 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2196 | " tabs Tab-separated values\n" |
| 2197 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 2198 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2199 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2200 | ".open ?--new? ?FILE? Close existing database and reopen FILE\n" |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 2201 | " The --new starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2202 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 2203 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2204 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2205 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2206 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2207 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 2208 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2209 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2210 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 2211 | " Add --indent for pretty-printing\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 2212 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 2213 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2214 | #if defined(SQLITE_ENABLE_SESSION) |
| 2215 | ".session CMD ... Create or control sessions\n" |
| 2216 | #endif |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 2217 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 2218 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2219 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 2220 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 2221 | ".tables ?TABLE? List names of tables\n" |
| 2222 | " If TABLE specified, only list tables matching\n" |
| 2223 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 2224 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 2225 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2226 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2227 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 2228 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 2229 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 2230 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 2231 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 2232 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2233 | ; |
| 2234 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2235 | #if defined(SQLITE_ENABLE_SESSION) |
| 2236 | /* |
| 2237 | ** Print help information for the ".sessions" command |
| 2238 | */ |
| 2239 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 2240 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2241 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 2242 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 2243 | "Subcommands:\n" |
| 2244 | " attach TABLE Attach TABLE\n" |
| 2245 | " changeset FILE Write a changeset into FILE\n" |
| 2246 | " close Close one session\n" |
| 2247 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2248 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2249 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 2250 | " isempty Query whether the session is empty\n" |
| 2251 | " list List currently open session names\n" |
| 2252 | " open DB NAME Open a new session on DB\n" |
| 2253 | " patchset FILE Write a patchset into FILE\n" |
| 2254 | ); |
| 2255 | } |
| 2256 | #endif |
| 2257 | |
| 2258 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2259 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2260 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2261 | |
| 2262 | |
| 2263 | /* |
| 2264 | ** Read the content of a file into memory obtained from sqlite3_malloc64(). |
| 2265 | ** The caller is responsible for freeing the memory. |
| 2266 | ** |
| 2267 | ** NULL is returned if any error is encountered. |
| 2268 | */ |
| 2269 | static char *readFile(const char *zName){ |
| 2270 | FILE *in = fopen(zName, "rb"); |
| 2271 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 2272 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2273 | char *pBuf; |
| 2274 | if( in==0 ) return 0; |
| 2275 | fseek(in, 0, SEEK_END); |
| 2276 | nIn = ftell(in); |
| 2277 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 2278 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2279 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 2280 | nRead = fread(pBuf, nIn, 1, in); |
| 2281 | fclose(in); |
| 2282 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2283 | sqlite3_free(pBuf); |
| 2284 | return 0; |
| 2285 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 2286 | pBuf[nIn] = 0; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2287 | return pBuf; |
| 2288 | } |
| 2289 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2290 | /* |
| 2291 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 2292 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 2293 | ** if the file does not exist or is unreadable. |
| 2294 | */ |
| 2295 | static void readfileFunc( |
| 2296 | sqlite3_context *context, |
| 2297 | int argc, |
| 2298 | sqlite3_value **argv |
| 2299 | ){ |
| 2300 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2301 | void *pBuf; |
| 2302 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2303 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2304 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 2305 | if( zName==0 ) return; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 2306 | pBuf = readFile(zName); |
| 2307 | if( pBuf ) sqlite3_result_blob(context, pBuf, -1, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | /* |
| 2311 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 2312 | ** is written into file X. The number of bytes written is returned. Or |
| 2313 | ** NULL is returned if something goes wrong, such as being unable to open |
| 2314 | ** file X for writing. |
| 2315 | */ |
| 2316 | static void writefileFunc( |
| 2317 | sqlite3_context *context, |
| 2318 | int argc, |
| 2319 | sqlite3_value **argv |
| 2320 | ){ |
| 2321 | FILE *out; |
| 2322 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2323 | sqlite3_int64 rc; |
| 2324 | const char *zFile; |
| 2325 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2326 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2327 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 2328 | if( zFile==0 ) return; |
| 2329 | out = fopen(zFile, "wb"); |
| 2330 | if( out==0 ) return; |
| 2331 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 2332 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2333 | rc = 0; |
| 2334 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 2335 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2336 | } |
| 2337 | fclose(out); |
| 2338 | sqlite3_result_int64(context, rc); |
| 2339 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2340 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2341 | #if defined(SQLITE_ENABLE_SESSION) |
| 2342 | /* |
| 2343 | ** Close a single OpenSession object and release all of its associated |
| 2344 | ** resources. |
| 2345 | */ |
| 2346 | static void session_close(OpenSession *pSession){ |
| 2347 | int i; |
| 2348 | sqlite3session_delete(pSession->p); |
| 2349 | sqlite3_free(pSession->zName); |
| 2350 | for(i=0; i<pSession->nFilter; i++){ |
| 2351 | sqlite3_free(pSession->azFilter[i]); |
| 2352 | } |
| 2353 | sqlite3_free(pSession->azFilter); |
| 2354 | memset(pSession, 0, sizeof(OpenSession)); |
| 2355 | } |
| 2356 | #endif |
| 2357 | |
| 2358 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 2359 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2360 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2361 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 2362 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2363 | int i; |
| 2364 | for(i=0; i<p->nSession; i++){ |
| 2365 | session_close(&p->aSession[i]); |
| 2366 | } |
| 2367 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2368 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 2369 | #else |
| 2370 | # define session_close_all(X) |
| 2371 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2372 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2373 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 2374 | ** Implementation of the xFilter function for an open session. Omit |
| 2375 | ** any tables named by ".session filter" but let all other table through. |
| 2376 | */ |
| 2377 | #if defined(SQLITE_ENABLE_SESSION) |
| 2378 | static int session_filter(void *pCtx, const char *zTab){ |
| 2379 | OpenSession *pSession = (OpenSession*)pCtx; |
| 2380 | int i; |
| 2381 | for(i=0; i<pSession->nFilter; i++){ |
| 2382 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 2383 | } |
| 2384 | return 1; |
| 2385 | } |
| 2386 | #endif |
| 2387 | |
| 2388 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2389 | ** Make sure the database is open. If it is not, then open it. If |
| 2390 | ** the database fails to open, print an error message and exit. |
| 2391 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2392 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2393 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 2394 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2395 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2396 | globalDb = p->db; |
| 2397 | if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){ |
| 2398 | sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0, |
drh | 4cea5ba | 2008-05-05 16:27:24 +0000 | [diff] [blame] | 2399 | shellstaticFunc, 0, 0); |
| 2400 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2401 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2402 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2403 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 2404 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2405 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2406 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2407 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2408 | sqlite3_enable_load_extension(p->db, 1); |
| 2409 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2410 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2411 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2412 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 2413 | writefileFunc, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2418 | ** Do C-language style dequoting. |
| 2419 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2420 | ** \a -> alarm |
| 2421 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2422 | ** \t -> tab |
| 2423 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2424 | ** \v -> vertical tab |
| 2425 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2426 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2427 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 2428 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2429 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2430 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2431 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2432 | */ |
| 2433 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2434 | int i, j; |
| 2435 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2436 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2437 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 2438 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2439 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2440 | if( c=='a' ){ |
| 2441 | c = '\a'; |
| 2442 | }else if( c=='b' ){ |
| 2443 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2444 | }else if( c=='t' ){ |
| 2445 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2446 | }else if( c=='n' ){ |
| 2447 | c = '\n'; |
| 2448 | }else if( c=='v' ){ |
| 2449 | c = '\v'; |
| 2450 | }else if( c=='f' ){ |
| 2451 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2452 | }else if( c=='r' ){ |
| 2453 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 2454 | }else if( c=='"' ){ |
| 2455 | c = '"'; |
| 2456 | }else if( c=='\'' ){ |
| 2457 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 2458 | }else if( c=='\\' ){ |
| 2459 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2460 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 2461 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2462 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2463 | i++; |
| 2464 | c = (c<<3) + z[i] - '0'; |
| 2465 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2466 | i++; |
| 2467 | c = (c<<3) + z[i] - '0'; |
| 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | z[j] = c; |
| 2473 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2474 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 2478 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 2479 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2480 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 2481 | static int hexDigitValue(char c){ |
| 2482 | if( c>='0' && c<='9' ) return c - '0'; |
| 2483 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 2484 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 2485 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 2489 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 2490 | */ |
| 2491 | static sqlite3_int64 integerValue(const char *zArg){ |
| 2492 | sqlite3_int64 v = 0; |
| 2493 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 2494 | { "KiB", 1024 }, |
| 2495 | { "MiB", 1024*1024 }, |
| 2496 | { "GiB", 1024*1024*1024 }, |
| 2497 | { "KB", 1000 }, |
| 2498 | { "MB", 1000000 }, |
| 2499 | { "GB", 1000000000 }, |
| 2500 | { "K", 1000 }, |
| 2501 | { "M", 1000000 }, |
| 2502 | { "G", 1000000000 }, |
| 2503 | }; |
| 2504 | int i; |
| 2505 | int isNeg = 0; |
| 2506 | if( zArg[0]=='-' ){ |
| 2507 | isNeg = 1; |
| 2508 | zArg++; |
| 2509 | }else if( zArg[0]=='+' ){ |
| 2510 | zArg++; |
| 2511 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 2512 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 2513 | int x; |
| 2514 | zArg += 2; |
| 2515 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 2516 | v = (v<<4) + x; |
| 2517 | zArg++; |
| 2518 | } |
| 2519 | }else{ |
| 2520 | while( IsDigit(zArg[0]) ){ |
| 2521 | v = v*10 + zArg[0] - '0'; |
| 2522 | zArg++; |
| 2523 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 2524 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 2525 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 2526 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 2527 | v *= aMult[i].iMult; |
| 2528 | break; |
| 2529 | } |
| 2530 | } |
| 2531 | return isNeg? -v : v; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 2535 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 2536 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 2537 | */ |
| 2538 | static int booleanValue(char *zArg){ |
| 2539 | int i; |
| 2540 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 2541 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 2542 | }else{ |
| 2543 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 2544 | } |
| 2545 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 2546 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 2547 | return 1; |
| 2548 | } |
| 2549 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 2550 | return 0; |
| 2551 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2552 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 2553 | zArg); |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
| 2557 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2558 | ** Close an output file, assuming it is not stderr or stdout |
| 2559 | */ |
| 2560 | static void output_file_close(FILE *f){ |
| 2561 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 2562 | } |
| 2563 | |
| 2564 | /* |
| 2565 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2566 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2567 | ** filename is "off". |
| 2568 | */ |
| 2569 | static FILE *output_file_open(const char *zFile){ |
| 2570 | FILE *f; |
| 2571 | if( strcmp(zFile,"stdout")==0 ){ |
| 2572 | f = stdout; |
| 2573 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 2574 | f = stderr; |
| 2575 | }else if( strcmp(zFile, "off")==0 ){ |
| 2576 | f = 0; |
| 2577 | }else{ |
| 2578 | f = fopen(zFile, "wb"); |
| 2579 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2580 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | return f; |
| 2584 | } |
| 2585 | |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 2586 | #if !defined(SQLITE_OMIT_BUILTIN_TEST) |
| 2587 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2588 | /* |
| 2589 | ** A routine for handling output from sqlite3_trace(). |
| 2590 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 2591 | static int sql_trace_callback( |
| 2592 | unsigned mType, |
| 2593 | void *pArg, |
| 2594 | void *pP, |
| 2595 | void *pX |
| 2596 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2597 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 2598 | UNUSED_PARAMETER(mType); |
| 2599 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 2600 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 2601 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 2602 | int i = (int)strlen(z); |
| 2603 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2604 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 2605 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 2606 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2607 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 2608 | #endif |
| 2609 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2610 | |
| 2611 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 2612 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 2613 | ** a useful spot to set a debugger breakpoint. |
| 2614 | */ |
| 2615 | static void test_breakpoint(void){ |
| 2616 | static int nCall = 0; |
| 2617 | nCall++; |
| 2618 | } |
| 2619 | |
| 2620 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2621 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2622 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2623 | typedef struct ImportCtx ImportCtx; |
| 2624 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2625 | const char *zFile; /* Name of the input file */ |
| 2626 | FILE *in; /* Read the CSV text from this input stream */ |
| 2627 | char *z; /* Accumulated text for a field */ |
| 2628 | int n; /* Number of bytes in z */ |
| 2629 | int nAlloc; /* Space allocated for z[] */ |
| 2630 | int nLine; /* Current line number */ |
| 2631 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2632 | int cColSep; /* The column separator character. (Usually ",") */ |
| 2633 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2634 | }; |
| 2635 | |
| 2636 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2637 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2638 | if( p->n+1>=p->nAlloc ){ |
| 2639 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2640 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2641 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2642 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2643 | exit(1); |
| 2644 | } |
| 2645 | } |
| 2646 | p->z[p->n++] = (char)c; |
| 2647 | } |
| 2648 | |
| 2649 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 2650 | ** with the option of having a separator other than ",". |
| 2651 | ** |
| 2652 | ** + Input comes from p->in. |
| 2653 | ** + 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] | 2654 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2655 | ** + Use p->cSep as the column separator. The default is ",". |
| 2656 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2657 | ** + Keep track of the line number in p->nLine. |
| 2658 | ** + Store the character that terminates the field in p->cTerm. Store |
| 2659 | ** EOF on end-of-file. |
| 2660 | ** + Report syntax errors on stderr |
| 2661 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 2662 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2663 | int c; |
| 2664 | int cSep = p->cColSep; |
| 2665 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2666 | p->n = 0; |
| 2667 | c = fgetc(p->in); |
| 2668 | if( c==EOF || seenInterrupt ){ |
| 2669 | p->cTerm = EOF; |
| 2670 | return 0; |
| 2671 | } |
| 2672 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2673 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2674 | int startLine = p->nLine; |
| 2675 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 2676 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2677 | while( 1 ){ |
| 2678 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2679 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2680 | if( c==cQuote ){ |
| 2681 | if( pc==cQuote ){ |
| 2682 | pc = 0; |
| 2683 | continue; |
| 2684 | } |
| 2685 | } |
| 2686 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2687 | || (c==rSep && pc==cQuote) |
| 2688 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2689 | || (c==EOF && pc==cQuote) |
| 2690 | ){ |
| 2691 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2692 | p->cTerm = c; |
| 2693 | break; |
| 2694 | } |
| 2695 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2696 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2697 | p->zFile, p->nLine, cQuote); |
| 2698 | } |
| 2699 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2700 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2701 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2702 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2703 | break; |
| 2704 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2705 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 2706 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2707 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 2708 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2709 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2710 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 2711 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 2712 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2713 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2714 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2715 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 2716 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2717 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2718 | p->cTerm = c; |
| 2719 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 2720 | if( p->z ) p->z[p->n] = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2721 | return p->z; |
| 2722 | } |
| 2723 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2724 | /* Read a single field of ASCII delimited text. |
| 2725 | ** |
| 2726 | ** + Input comes from p->in. |
| 2727 | ** + 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] | 2728 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2729 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 2730 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 2731 | ** + Keep track of the row number in p->nLine. |
| 2732 | ** + Store the character that terminates the field in p->cTerm. Store |
| 2733 | ** EOF on end-of-file. |
| 2734 | ** + Report syntax errors on stderr |
| 2735 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 2736 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2737 | int c; |
| 2738 | int cSep = p->cColSep; |
| 2739 | int rSep = p->cRowSep; |
| 2740 | p->n = 0; |
| 2741 | c = fgetc(p->in); |
| 2742 | if( c==EOF || seenInterrupt ){ |
| 2743 | p->cTerm = EOF; |
| 2744 | return 0; |
| 2745 | } |
| 2746 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 2747 | import_append_char(p, c); |
| 2748 | c = fgetc(p->in); |
| 2749 | } |
| 2750 | if( c==rSep ){ |
| 2751 | p->nLine++; |
| 2752 | } |
| 2753 | p->cTerm = c; |
| 2754 | if( p->z ) p->z[p->n] = 0; |
| 2755 | return p->z; |
| 2756 | } |
| 2757 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 2758 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2759 | ** Try to transfer data for table zTable. If an error is seen while |
| 2760 | ** moving forward, try to go backwards. The backwards movement won't |
| 2761 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2762 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 2763 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2764 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2765 | sqlite3 *newDb, |
| 2766 | const char *zTable |
| 2767 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2768 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2769 | sqlite3_stmt *pInsert = 0; |
| 2770 | char *zQuery = 0; |
| 2771 | char *zInsert = 0; |
| 2772 | int rc; |
| 2773 | int i, j, n; |
| 2774 | int nTable = (int)strlen(zTable); |
| 2775 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2776 | int cnt = 0; |
| 2777 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2778 | |
| 2779 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 2780 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2781 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2782 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2783 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2784 | zQuery); |
| 2785 | goto end_data_xfer; |
| 2786 | } |
| 2787 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2788 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2789 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2790 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2791 | goto end_data_xfer; |
| 2792 | } |
| 2793 | sqlite3_snprintf(200+nTable,zInsert, |
| 2794 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 2795 | i = (int)strlen(zInsert); |
| 2796 | for(j=1; j<n; j++){ |
| 2797 | memcpy(zInsert+i, ",?", 2); |
| 2798 | i += 2; |
| 2799 | } |
| 2800 | memcpy(zInsert+i, ");", 3); |
| 2801 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 2802 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2803 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2804 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 2805 | zQuery); |
| 2806 | goto end_data_xfer; |
| 2807 | } |
| 2808 | for(k=0; k<2; k++){ |
| 2809 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2810 | for(i=0; i<n; i++){ |
| 2811 | switch( sqlite3_column_type(pQuery, i) ){ |
| 2812 | case SQLITE_NULL: { |
| 2813 | sqlite3_bind_null(pInsert, i+1); |
| 2814 | break; |
| 2815 | } |
| 2816 | case SQLITE_INTEGER: { |
| 2817 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 2818 | break; |
| 2819 | } |
| 2820 | case SQLITE_FLOAT: { |
| 2821 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 2822 | break; |
| 2823 | } |
| 2824 | case SQLITE_TEXT: { |
| 2825 | sqlite3_bind_text(pInsert, i+1, |
| 2826 | (const char*)sqlite3_column_text(pQuery,i), |
| 2827 | -1, SQLITE_STATIC); |
| 2828 | break; |
| 2829 | } |
| 2830 | case SQLITE_BLOB: { |
| 2831 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 2832 | sqlite3_column_bytes(pQuery,i), |
| 2833 | SQLITE_STATIC); |
| 2834 | break; |
| 2835 | } |
| 2836 | } |
| 2837 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2838 | rc = sqlite3_step(pInsert); |
| 2839 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2840 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2841 | sqlite3_errmsg(newDb)); |
| 2842 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2843 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2844 | cnt++; |
| 2845 | if( (cnt%spinRate)==0 ){ |
| 2846 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 2847 | fflush(stdout); |
| 2848 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2849 | } /* End while */ |
| 2850 | if( rc==SQLITE_DONE ) break; |
| 2851 | sqlite3_finalize(pQuery); |
| 2852 | sqlite3_free(zQuery); |
| 2853 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 2854 | zTable); |
| 2855 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2856 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2857 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2858 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } /* End for(k=0...) */ |
| 2861 | |
| 2862 | end_data_xfer: |
| 2863 | sqlite3_finalize(pQuery); |
| 2864 | sqlite3_finalize(pInsert); |
| 2865 | sqlite3_free(zQuery); |
| 2866 | sqlite3_free(zInsert); |
| 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | /* |
| 2871 | ** Try to transfer all rows of the schema that match zWhere. For |
| 2872 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2873 | ** If an error is encountered while moving forward through the |
| 2874 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2875 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 2876 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2877 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2878 | sqlite3 *newDb, |
| 2879 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2880 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2881 | ){ |
| 2882 | sqlite3_stmt *pQuery = 0; |
| 2883 | char *zQuery = 0; |
| 2884 | int rc; |
| 2885 | const unsigned char *zName; |
| 2886 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2887 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2888 | |
| 2889 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 2890 | " WHERE %s", zWhere); |
| 2891 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2892 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2893 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2894 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2895 | zQuery); |
| 2896 | goto end_schema_xfer; |
| 2897 | } |
| 2898 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2899 | zName = sqlite3_column_text(pQuery, 0); |
| 2900 | zSql = sqlite3_column_text(pQuery, 1); |
| 2901 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2902 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 2903 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2904 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2905 | sqlite3_free(zErrMsg); |
| 2906 | zErrMsg = 0; |
| 2907 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2908 | if( xForEach ){ |
| 2909 | xForEach(p, newDb, (const char*)zName); |
| 2910 | } |
| 2911 | printf("done\n"); |
| 2912 | } |
| 2913 | if( rc!=SQLITE_DONE ){ |
| 2914 | sqlite3_finalize(pQuery); |
| 2915 | sqlite3_free(zQuery); |
| 2916 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 2917 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 2918 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 2919 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2920 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2921 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 2922 | zQuery); |
| 2923 | goto end_schema_xfer; |
| 2924 | } |
| 2925 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 2926 | zName = sqlite3_column_text(pQuery, 0); |
| 2927 | zSql = sqlite3_column_text(pQuery, 1); |
| 2928 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2929 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 2930 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2931 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 2932 | sqlite3_free(zErrMsg); |
| 2933 | zErrMsg = 0; |
| 2934 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2935 | if( xForEach ){ |
| 2936 | xForEach(p, newDb, (const char*)zName); |
| 2937 | } |
| 2938 | printf("done\n"); |
| 2939 | } |
| 2940 | } |
| 2941 | end_schema_xfer: |
| 2942 | sqlite3_finalize(pQuery); |
| 2943 | sqlite3_free(zQuery); |
| 2944 | } |
| 2945 | |
| 2946 | /* |
| 2947 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 2948 | ** as possible out of the main database (which might be corrupt) and write it |
| 2949 | ** into zNewDb. |
| 2950 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2951 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2952 | int rc; |
| 2953 | sqlite3 *newDb = 0; |
| 2954 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2955 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2956 | return; |
| 2957 | } |
| 2958 | rc = sqlite3_open(zNewDb, &newDb); |
| 2959 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2960 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2961 | sqlite3_errmsg(newDb)); |
| 2962 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 2963 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2964 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 2965 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 2966 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2967 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 2968 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 2969 | } |
| 2970 | sqlite3_close(newDb); |
| 2971 | } |
| 2972 | |
| 2973 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2974 | ** Change the output file back to stdout |
| 2975 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2976 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2977 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2978 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2979 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2980 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2981 | }else{ |
| 2982 | output_file_close(p->out); |
| 2983 | } |
| 2984 | p->outfile[0] = 0; |
| 2985 | p->out = stdout; |
| 2986 | } |
| 2987 | |
| 2988 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 2989 | ** Run an SQL command and return the single integer result. |
| 2990 | */ |
| 2991 | static int db_int(ShellState *p, const char *zSql){ |
| 2992 | sqlite3_stmt *pStmt; |
| 2993 | int res = 0; |
| 2994 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2995 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2996 | res = sqlite3_column_int(pStmt,0); |
| 2997 | } |
| 2998 | sqlite3_finalize(pStmt); |
| 2999 | return res; |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 3004 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3005 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3006 | return (a[0]<<8) + a[1]; |
| 3007 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 3008 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3009 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | ** Implementation of the ".info" command. |
| 3014 | ** |
| 3015 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 3016 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3017 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3018 | static const struct { const char *zName; int ofst; } aField[] = { |
| 3019 | { "file change counter:", 24 }, |
| 3020 | { "database page count:", 28 }, |
| 3021 | { "freelist page count:", 36 }, |
| 3022 | { "schema cookie:", 40 }, |
| 3023 | { "schema format:", 44 }, |
| 3024 | { "default cache size:", 48 }, |
| 3025 | { "autovacuum top root:", 52 }, |
| 3026 | { "incremental vacuum:", 64 }, |
| 3027 | { "text encoding:", 56 }, |
| 3028 | { "user version:", 60 }, |
| 3029 | { "application id:", 68 }, |
| 3030 | { "software version:", 96 }, |
| 3031 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3032 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 3033 | { "number of tables:", |
| 3034 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 3035 | { "number of indexes:", |
| 3036 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 3037 | { "number of triggers:", |
| 3038 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 3039 | { "number of views:", |
| 3040 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 3041 | { "schema size:", |
| 3042 | "SELECT total(length(sql)) FROM %s" }, |
| 3043 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 3044 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3045 | int i; |
| 3046 | char *zSchemaTab; |
| 3047 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 3048 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3049 | open_db(p, 0); |
| 3050 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3051 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3052 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 3053 | return 1; |
| 3054 | } |
| 3055 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 3056 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3057 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3058 | return 1; |
| 3059 | } |
| 3060 | i = get2byteInt(aHdr+16); |
| 3061 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3062 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 3063 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 3064 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 3065 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3066 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3067 | int ofst = aField[i].ofst; |
| 3068 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3069 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3070 | switch( ofst ){ |
| 3071 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3072 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 3073 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 3074 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3077 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3078 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3079 | if( zDb==0 ){ |
| 3080 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 3081 | }else if( strcmp(zDb,"temp")==0 ){ |
| 3082 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 3083 | }else{ |
| 3084 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 3085 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3086 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3087 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 3088 | int val = db_int(p, zSql); |
| 3089 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3090 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3091 | } |
| 3092 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3093 | return 0; |
| 3094 | } |
| 3095 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3096 | /* |
| 3097 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 3098 | */ |
| 3099 | static int shellDatabaseError(sqlite3 *db){ |
| 3100 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3101 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3102 | return 1; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
| 3106 | ** Print an out-of-memory message to stderr and return 1. |
| 3107 | */ |
| 3108 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3109 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 3110 | return 1; |
| 3111 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3112 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3113 | /* |
| 3114 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 3115 | ** if they match and FALSE (0) if they do not match. |
| 3116 | ** |
| 3117 | ** Globbing rules: |
| 3118 | ** |
| 3119 | ** '*' Matches any sequence of zero or more characters. |
| 3120 | ** |
| 3121 | ** '?' Matches exactly one character. |
| 3122 | ** |
| 3123 | ** [...] Matches one character from the enclosed list of |
| 3124 | ** characters. |
| 3125 | ** |
| 3126 | ** [^...] Matches one character not in the enclosed list. |
| 3127 | ** |
| 3128 | ** '#' Matches any sequence of one or more digits with an |
| 3129 | ** optional + or - sign in front |
| 3130 | ** |
| 3131 | ** ' ' Any span of whitespace matches any other span of |
| 3132 | ** whitespace. |
| 3133 | ** |
| 3134 | ** Extra whitespace at the end of z[] is ignored. |
| 3135 | */ |
| 3136 | static int testcase_glob(const char *zGlob, const char *z){ |
| 3137 | int c, c2; |
| 3138 | int invert; |
| 3139 | int seen; |
| 3140 | |
| 3141 | while( (c = (*(zGlob++)))!=0 ){ |
| 3142 | if( IsSpace(c) ){ |
| 3143 | if( !IsSpace(*z) ) return 0; |
| 3144 | while( IsSpace(*zGlob) ) zGlob++; |
| 3145 | while( IsSpace(*z) ) z++; |
| 3146 | }else if( c=='*' ){ |
| 3147 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 3148 | if( c=='?' && (*(z++))==0 ) return 0; |
| 3149 | } |
| 3150 | if( c==0 ){ |
| 3151 | return 1; |
| 3152 | }else if( c=='[' ){ |
| 3153 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 3154 | z++; |
| 3155 | } |
| 3156 | return (*z)!=0; |
| 3157 | } |
| 3158 | while( (c2 = (*(z++)))!=0 ){ |
| 3159 | while( c2!=c ){ |
| 3160 | c2 = *(z++); |
| 3161 | if( c2==0 ) return 0; |
| 3162 | } |
| 3163 | if( testcase_glob(zGlob,z) ) return 1; |
| 3164 | } |
| 3165 | return 0; |
| 3166 | }else if( c=='?' ){ |
| 3167 | if( (*(z++))==0 ) return 0; |
| 3168 | }else if( c=='[' ){ |
| 3169 | int prior_c = 0; |
| 3170 | seen = 0; |
| 3171 | invert = 0; |
| 3172 | c = *(z++); |
| 3173 | if( c==0 ) return 0; |
| 3174 | c2 = *(zGlob++); |
| 3175 | if( c2=='^' ){ |
| 3176 | invert = 1; |
| 3177 | c2 = *(zGlob++); |
| 3178 | } |
| 3179 | if( c2==']' ){ |
| 3180 | if( c==']' ) seen = 1; |
| 3181 | c2 = *(zGlob++); |
| 3182 | } |
| 3183 | while( c2 && c2!=']' ){ |
| 3184 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 3185 | c2 = *(zGlob++); |
| 3186 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 3187 | prior_c = 0; |
| 3188 | }else{ |
| 3189 | if( c==c2 ){ |
| 3190 | seen = 1; |
| 3191 | } |
| 3192 | prior_c = c2; |
| 3193 | } |
| 3194 | c2 = *(zGlob++); |
| 3195 | } |
| 3196 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 3197 | }else if( c=='#' ){ |
| 3198 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 3199 | if( !IsDigit(z[0]) ) return 0; |
| 3200 | z++; |
| 3201 | while( IsDigit(z[0]) ){ z++; } |
| 3202 | }else{ |
| 3203 | if( c!=(*(z++)) ) return 0; |
| 3204 | } |
| 3205 | } |
| 3206 | while( IsSpace(*z) ){ z++; } |
| 3207 | return *z==0; |
| 3208 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3209 | |
| 3210 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 3211 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3212 | ** Compare the string as a command-line option with either one or two |
| 3213 | ** initial "-" characters. |
| 3214 | */ |
| 3215 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 3216 | if( zStr[0]!='-' ) return 0; |
| 3217 | zStr++; |
| 3218 | if( zStr[0]=='-' ) zStr++; |
| 3219 | return strcmp(zStr, zOpt)==0; |
| 3220 | } |
| 3221 | |
| 3222 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 3223 | ** Delete a file. |
| 3224 | */ |
| 3225 | int shellDeleteFile(const char *zFilename){ |
| 3226 | int rc; |
| 3227 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3228 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 3229 | rc = _wunlink(z); |
| 3230 | sqlite3_free(z); |
| 3231 | #else |
| 3232 | rc = unlink(zFilename); |
| 3233 | #endif |
| 3234 | return rc; |
| 3235 | } |
| 3236 | |
| 3237 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3238 | ** If an input line begins with "." then invoke this routine to |
| 3239 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3240 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 3241 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3242 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3243 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3244 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3245 | int nArg = 0; |
| 3246 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3247 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3248 | char *azArg[50]; |
| 3249 | |
| 3250 | /* Parse the input line into tokens. |
| 3251 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3252 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 3253 | while( IsSpace(zLine[h]) ){ h++; } |
| 3254 | if( zLine[h]==0 ) break; |
| 3255 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 3256 | int delim = zLine[h++]; |
| 3257 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3258 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3259 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3260 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3261 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3262 | if( zLine[h]==delim ){ |
| 3263 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3264 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3265 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3266 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3267 | azArg[nArg++] = &zLine[h]; |
| 3268 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 3269 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3270 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | /* Process the input line. |
| 3275 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3276 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3277 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3278 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3279 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3280 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3281 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 3282 | if( nArg!=2 ){ |
| 3283 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 3284 | rc = 1; |
| 3285 | goto meta_command_exit; |
| 3286 | } |
| 3287 | open_db(p, 0); |
| 3288 | if( booleanValue(azArg[1]) ){ |
| 3289 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 3290 | }else{ |
| 3291 | sqlite3_set_authorizer(p->db, 0, 0); |
| 3292 | } |
| 3293 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3294 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3295 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3296 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 3297 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 3298 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3299 | const char *zDestFile = 0; |
| 3300 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3301 | sqlite3 *pDest; |
| 3302 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3303 | int j; |
| 3304 | for(j=1; j<nArg; j++){ |
| 3305 | const char *z = azArg[j]; |
| 3306 | if( z[0]=='-' ){ |
| 3307 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 3308 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3309 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3310 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3311 | return 1; |
| 3312 | } |
| 3313 | }else if( zDestFile==0 ){ |
| 3314 | zDestFile = azArg[j]; |
| 3315 | }else if( zDb==0 ){ |
| 3316 | zDb = zDestFile; |
| 3317 | zDestFile = azArg[j]; |
| 3318 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3319 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3320 | return 1; |
| 3321 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3322 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3323 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3324 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 3325 | return 1; |
| 3326 | } |
| 3327 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3328 | rc = sqlite3_open(zDestFile, &pDest); |
| 3329 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3330 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3331 | sqlite3_close(pDest); |
| 3332 | return 1; |
| 3333 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3334 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3335 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 3336 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3337 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3338 | sqlite3_close(pDest); |
| 3339 | return 1; |
| 3340 | } |
| 3341 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 3342 | sqlite3_backup_finish(pBackup); |
| 3343 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3344 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3345 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3346 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3347 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3348 | } |
| 3349 | sqlite3_close(pDest); |
| 3350 | }else |
| 3351 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3352 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 3353 | if( nArg==2 ){ |
| 3354 | bail_on_error = booleanValue(azArg[1]); |
| 3355 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3356 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3357 | rc = 1; |
| 3358 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 3359 | }else |
| 3360 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3361 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 3362 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 3363 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3364 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 3365 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3366 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 3367 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3368 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3369 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3370 | rc = 1; |
| 3371 | } |
| 3372 | }else |
| 3373 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3374 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 3375 | ** routine named test_breakpoint(). |
| 3376 | */ |
| 3377 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 3378 | test_breakpoint(); |
| 3379 | }else |
| 3380 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3381 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 3382 | if( nArg==2 ){ |
| 3383 | p->countChanges = booleanValue(azArg[1]); |
| 3384 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3385 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3386 | rc = 1; |
| 3387 | } |
| 3388 | }else |
| 3389 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3390 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 3391 | ** Then read the content of the testcase-out.txt file and compare against |
| 3392 | ** azArg[1]. If there are differences, report an error and exit. |
| 3393 | */ |
| 3394 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 3395 | char *zRes = 0; |
| 3396 | output_reset(p); |
| 3397 | if( nArg!=2 ){ |
| 3398 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3399 | rc = 2; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3400 | }else if( (zRes = readFile("testcase-out.txt"))==0 ){ |
| 3401 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 3402 | rc = 2; |
| 3403 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3404 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3405 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 3406 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3407 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3408 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3409 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3410 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3411 | } |
| 3412 | sqlite3_free(zRes); |
| 3413 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3414 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3415 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 3416 | if( nArg==2 ){ |
| 3417 | tryToClone(p, azArg[1]); |
| 3418 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3419 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3420 | rc = 1; |
| 3421 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3422 | }else |
| 3423 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3424 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3425 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 3426 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3427 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 3428 | memcpy(&data, p, sizeof(data)); |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 3429 | data.showHeader = 1; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3430 | data.cMode = data.mode = MODE_Column; |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 3431 | data.colWidth[0] = 3; |
| 3432 | data.colWidth[1] = 15; |
| 3433 | data.colWidth[2] = 58; |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 3434 | data.cnt = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3435 | sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 3436 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3437 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 3438 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3439 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3440 | } |
| 3441 | }else |
| 3442 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3443 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 3444 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 3445 | }else |
| 3446 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3447 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3448 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 3449 | /* When playing back a "dump", the content might appear in an order |
| 3450 | ** which causes immediate foreign key constraints to be violated. |
| 3451 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3452 | if( nArg!=1 && nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3453 | raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3454 | rc = 1; |
| 3455 | goto meta_command_exit; |
| 3456 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3457 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 3458 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3459 | p->writableSchema = 0; |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 3460 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3461 | p->nErr = 0; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3462 | if( nArg==1 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3463 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 3464 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3465 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 3466 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3467 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 3468 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3469 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 3470 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3471 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 3472 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3473 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 3474 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3475 | }else{ |
| 3476 | int i; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3477 | for(i=1; i<nArg; i++){ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3478 | zShellStatic = azArg[i]; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3479 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 3480 | "SELECT name, type, sql FROM sqlite_master " |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3481 | "WHERE tbl_name LIKE shellstatic() AND type=='table'" |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3482 | " AND sql NOT NULL"); |
| 3483 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 3484 | "SELECT sql FROM sqlite_master " |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3485 | "WHERE sql NOT NULL" |
| 3486 | " AND type IN ('index','trigger','view')" |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3487 | " AND tbl_name LIKE shellstatic()", 0 |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 3488 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3489 | zShellStatic = 0; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3490 | } |
| 3491 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3492 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3493 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3494 | p->writableSchema = 0; |
| 3495 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 3496 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 3497 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3498 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3499 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3500 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3501 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 3502 | if( nArg==2 ){ |
| 3503 | p->echoOn = booleanValue(azArg[1]); |
| 3504 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3505 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3506 | rc = 1; |
| 3507 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3508 | }else |
| 3509 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3510 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 3511 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3512 | if( strcmp(azArg[1],"full")==0 ){ |
| 3513 | p->autoEQP = 2; |
| 3514 | }else{ |
| 3515 | p->autoEQP = booleanValue(azArg[1]); |
| 3516 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3517 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3518 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3519 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3520 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3521 | }else |
| 3522 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 3523 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3524 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 3525 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3526 | }else |
| 3527 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3528 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3529 | int val = 1; |
| 3530 | if( nArg>=2 ){ |
| 3531 | if( strcmp(azArg[1],"auto")==0 ){ |
| 3532 | val = 99; |
| 3533 | }else{ |
| 3534 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3535 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3536 | } |
| 3537 | if( val==1 && p->mode!=MODE_Explain ){ |
| 3538 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 3539 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3540 | p->autoExplain = 0; |
| 3541 | }else if( val==0 ){ |
| 3542 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 3543 | p->autoExplain = 0; |
| 3544 | }else if( val==99 ){ |
| 3545 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 3546 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3547 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3548 | }else |
| 3549 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3550 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3551 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3552 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3553 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3554 | memcpy(&data, p, sizeof(data)); |
| 3555 | data.showHeader = 0; |
| 3556 | data.cMode = data.mode = MODE_Semi; |
| 3557 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 3558 | data.cMode = data.mode = MODE_Pretty; |
| 3559 | nArg = 1; |
| 3560 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3561 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3562 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3563 | rc = 1; |
| 3564 | goto meta_command_exit; |
| 3565 | } |
| 3566 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3567 | rc = sqlite3_exec(p->db, |
| 3568 | "SELECT sql FROM" |
| 3569 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 3570 | " FROM sqlite_master UNION ALL" |
| 3571 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3572 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3573 | "ORDER BY rowid", |
| 3574 | callback, &data, &zErrMsg |
| 3575 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3576 | if( rc==SQLITE_OK ){ |
| 3577 | sqlite3_stmt *pStmt; |
| 3578 | rc = sqlite3_prepare_v2(p->db, |
| 3579 | "SELECT rowid FROM sqlite_master" |
| 3580 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 3581 | -1, &pStmt, 0); |
| 3582 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 3583 | sqlite3_finalize(pStmt); |
| 3584 | } |
| 3585 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3586 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3587 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3588 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3589 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 3590 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3591 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3592 | data.zDestTable = "sqlite_stat1"; |
| 3593 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 3594 | shell_callback, &data,&zErrMsg); |
| 3595 | data.zDestTable = "sqlite_stat3"; |
| 3596 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 3597 | shell_callback, &data,&zErrMsg); |
| 3598 | data.zDestTable = "sqlite_stat4"; |
| 3599 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 3600 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3601 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 3602 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 3603 | }else |
| 3604 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3605 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 3606 | if( nArg==2 ){ |
| 3607 | p->showHeader = booleanValue(azArg[1]); |
| 3608 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3609 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3610 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 3611 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3612 | }else |
| 3613 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3614 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3615 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3616 | }else |
| 3617 | |
| 3618 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 3619 | char *zTable; /* Insert data into this table */ |
| 3620 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3621 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3622 | int nCol; /* Number of columns in the table */ |
| 3623 | int nByte; /* Number of bytes in an SQL string */ |
| 3624 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 3625 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3626 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3627 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3628 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3629 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 3630 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3631 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3632 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3633 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3634 | goto meta_command_exit; |
| 3635 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 3636 | zFile = azArg[1]; |
| 3637 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3638 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3639 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3640 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3641 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3642 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3643 | raw_printf(stderr, |
| 3644 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3645 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3646 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3647 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3648 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3649 | " for import\n"); |
| 3650 | return 1; |
| 3651 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3652 | nSep = strlen30(p->rowSeparator); |
| 3653 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3654 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3655 | return 1; |
| 3656 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3657 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 3658 | /* When importing CSV (only), if the row separator is set to the |
| 3659 | ** default output row separator, change it to the default input |
| 3660 | ** row separator. This avoids having to maintain different input |
| 3661 | ** and output row separators. */ |
| 3662 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 3663 | nSep = strlen30(p->rowSeparator); |
| 3664 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3665 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3666 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3667 | " for import\n"); |
| 3668 | return 1; |
| 3669 | } |
| 3670 | sCtx.zFile = zFile; |
| 3671 | sCtx.nLine = 1; |
| 3672 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3673 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3674 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3675 | return 1; |
| 3676 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3677 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 3678 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 3679 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3680 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 3681 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3682 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 3683 | xCloser = fclose; |
| 3684 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3685 | if( p->mode==MODE_Ascii ){ |
| 3686 | xRead = ascii_read_one_field; |
| 3687 | }else{ |
| 3688 | xRead = csv_read_one_field; |
| 3689 | } |
| 3690 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3691 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3692 | return 1; |
| 3693 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3694 | sCtx.cColSep = p->colSeparator[0]; |
| 3695 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 3696 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3697 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3698 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3699 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3700 | return 1; |
| 3701 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3702 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 3703 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3704 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3705 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3706 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 3707 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3708 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 3709 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3710 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3711 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3712 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 3713 | if( cSep=='(' ){ |
| 3714 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3715 | sqlite3_free(sCtx.z); |
| 3716 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3717 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 3718 | return 1; |
| 3719 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3720 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 3721 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 3722 | sqlite3_free(zCreate); |
| 3723 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3724 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3725 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3726 | sqlite3_free(sCtx.z); |
| 3727 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3728 | return 1; |
| 3729 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 3730 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3731 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3732 | sqlite3_free(zSql); |
| 3733 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3734 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3735 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3736 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3737 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3738 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3739 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3740 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3741 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3742 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3743 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3744 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3745 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3746 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3747 | return 1; |
| 3748 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3749 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3750 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3751 | for(i=1; i<nCol; i++){ |
| 3752 | zSql[j++] = ','; |
| 3753 | zSql[j++] = '?'; |
| 3754 | } |
| 3755 | zSql[j++] = ')'; |
| 3756 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 3757 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3758 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3759 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3760 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 3761 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3762 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 3763 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3764 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3765 | needCommit = sqlite3_get_autocommit(p->db); |
| 3766 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3767 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3768 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3769 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3770 | char *z = xRead(&sCtx); |
| 3771 | /* |
| 3772 | ** Did we reach end-of-file before finding any columns? |
| 3773 | ** If so, stop instead of NULL filling the remaining columns. |
| 3774 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3775 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3776 | /* |
| 3777 | ** Did we reach end-of-file OR end-of-line before finding any |
| 3778 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 3779 | ** the remaining columns. |
| 3780 | */ |
| 3781 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3782 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3783 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3784 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3785 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3786 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 3787 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 3788 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 3789 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3790 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3791 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3792 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3793 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3794 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3795 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3796 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3797 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3798 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3799 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3800 | if( i>=nCol ){ |
| 3801 | sqlite3_step(pStmt); |
| 3802 | rc = sqlite3_reset(pStmt); |
| 3803 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3804 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 3805 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3806 | } |
| 3807 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3808 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3809 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3810 | xCloser(sCtx.in); |
| 3811 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3812 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3813 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3814 | }else |
| 3815 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3816 | if( c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 3817 | || strncmp(azArg[0], "indexes", n)==0) ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3818 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3819 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3820 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3821 | memcpy(&data, p, sizeof(data)); |
| 3822 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3823 | data.cMode = data.mode = MODE_List; |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3824 | if( nArg==1 ){ |
| 3825 | rc = sqlite3_exec(p->db, |
| 3826 | "SELECT name FROM sqlite_master " |
| 3827 | "WHERE type='index' AND name NOT LIKE 'sqlite_%' " |
| 3828 | "UNION ALL " |
| 3829 | "SELECT name FROM sqlite_temp_master " |
| 3830 | "WHERE type='index' " |
| 3831 | "ORDER BY 1", |
| 3832 | callback, &data, &zErrMsg |
| 3833 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3834 | }else if( nArg==2 ){ |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3835 | zShellStatic = azArg[1]; |
| 3836 | rc = sqlite3_exec(p->db, |
| 3837 | "SELECT name FROM sqlite_master " |
| 3838 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
| 3839 | "UNION ALL " |
| 3840 | "SELECT name FROM sqlite_temp_master " |
| 3841 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
| 3842 | "ORDER BY 1", |
| 3843 | callback, &data, &zErrMsg |
| 3844 | ); |
| 3845 | zShellStatic = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3846 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3847 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3848 | rc = 1; |
| 3849 | goto meta_command_exit; |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3850 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3851 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3852 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 3853 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3854 | rc = 1; |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3855 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3856 | raw_printf(stderr, |
| 3857 | "Error: querying sqlite_master and sqlite_temp_master\n"); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3858 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3859 | } |
| 3860 | }else |
| 3861 | |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3862 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 3863 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 3864 | char *zSql; |
| 3865 | char *zCollist = 0; |
| 3866 | sqlite3_stmt *pStmt; |
| 3867 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 3868 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3869 | if( nArg!=3 ){ |
| 3870 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 3871 | rc = 1; |
| 3872 | goto meta_command_exit; |
| 3873 | } |
| 3874 | open_db(p, 0); |
| 3875 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 3876 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 3877 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3878 | sqlite3_free(zSql); |
| 3879 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3880 | tnum = sqlite3_column_int(pStmt, 0); |
| 3881 | } |
| 3882 | sqlite3_finalize(pStmt); |
| 3883 | if( tnum==0 ){ |
| 3884 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 3885 | rc = 1; |
| 3886 | goto meta_command_exit; |
| 3887 | } |
| 3888 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 3889 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3890 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 3891 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3892 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 3893 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3894 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 3895 | i++; |
| 3896 | if( zCol==0 ){ |
| 3897 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 3898 | zCol = "_ROWID_"; |
| 3899 | }else{ |
| 3900 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 3901 | zCol = zLabel; |
| 3902 | } |
| 3903 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3904 | if( zCollist==0 ){ |
| 3905 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 3906 | }else{ |
| 3907 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 3908 | } |
| 3909 | } |
| 3910 | sqlite3_finalize(pStmt); |
| 3911 | zSql = sqlite3_mprintf( |
| 3912 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 3913 | azArg[2], zCollist, zCollist); |
| 3914 | sqlite3_free(zCollist); |
| 3915 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 3916 | if( rc==SQLITE_OK ){ |
| 3917 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 3918 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 3919 | if( rc ){ |
| 3920 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 3921 | }else{ |
| 3922 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 3923 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3924 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 3925 | ); |
| 3926 | } |
| 3927 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 3928 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3929 | rc = 1; |
| 3930 | } |
| 3931 | sqlite3_free(zSql); |
| 3932 | }else |
| 3933 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 3934 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3935 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3936 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 3937 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3938 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 3939 | iotrace = 0; |
| 3940 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 3941 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3942 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 3943 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3944 | iotrace = stdout; |
| 3945 | }else{ |
| 3946 | iotrace = fopen(azArg[1], "w"); |
| 3947 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3948 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 3949 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 3950 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3951 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 3952 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 3953 | } |
| 3954 | } |
| 3955 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3956 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3957 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3958 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 3959 | static const struct { |
| 3960 | const char *zLimitName; /* Name of a limit */ |
| 3961 | int limitCode; /* Integer code for that limit */ |
| 3962 | } aLimit[] = { |
| 3963 | { "length", SQLITE_LIMIT_LENGTH }, |
| 3964 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 3965 | { "column", SQLITE_LIMIT_COLUMN }, |
| 3966 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 3967 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 3968 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 3969 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 3970 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 3971 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 3972 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 3973 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 3974 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 3975 | }; |
| 3976 | int i, n2; |
| 3977 | open_db(p, 0); |
| 3978 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3979 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3980 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3981 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 3982 | } |
| 3983 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3984 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3985 | rc = 1; |
| 3986 | goto meta_command_exit; |
| 3987 | }else{ |
| 3988 | int iLimit = -1; |
| 3989 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3990 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3991 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 3992 | if( iLimit<0 ){ |
| 3993 | iLimit = i; |
| 3994 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3995 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3996 | rc = 1; |
| 3997 | goto meta_command_exit; |
| 3998 | } |
| 3999 | } |
| 4000 | } |
| 4001 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4002 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 4003 | "enter \".limits\" with no arguments for a list.\n", |
| 4004 | azArg[1]); |
| 4005 | rc = 1; |
| 4006 | goto meta_command_exit; |
| 4007 | } |
| 4008 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 4009 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 4010 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 4011 | } |
| 4012 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 4013 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 4014 | } |
| 4015 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 4016 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 4017 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4018 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4019 | const char *zFile, *zProc; |
| 4020 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4021 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4022 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4023 | rc = 1; |
| 4024 | goto meta_command_exit; |
| 4025 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4026 | zFile = azArg[1]; |
| 4027 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4028 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4029 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 4030 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4031 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4032 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4033 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4034 | } |
| 4035 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 4036 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4037 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4038 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 4039 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4040 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4041 | rc = 1; |
| 4042 | }else{ |
| 4043 | const char *zFile = azArg[1]; |
| 4044 | output_file_close(p->pLog); |
| 4045 | p->pLog = output_file_open(zFile); |
| 4046 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 4047 | }else |
| 4048 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4049 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 4050 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 4051 | int n2 = (int)strlen(zMode); |
| 4052 | int c2 = zMode[0]; |
| 4053 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4054 | p->mode = MODE_Line; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4055 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4056 | p->mode = MODE_Column; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4057 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4058 | p->mode = MODE_List; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4059 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 4060 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4061 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4062 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 4063 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4064 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 4065 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 4066 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4067 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4068 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4069 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 4070 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4071 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 4072 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4073 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 4074 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 4075 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4076 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 4077 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 4078 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 4079 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4080 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4081 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 4082 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4083 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4084 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4085 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4086 | }else |
| 4087 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4088 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 4089 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 4090 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 4091 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4092 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4093 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 4094 | rc = 1; |
| 4095 | } |
| 4096 | }else |
| 4097 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4098 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4099 | char *zNewFilename; /* Name of the database file to open */ |
| 4100 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4101 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4102 | /* Close the existing database */ |
| 4103 | session_close_all(p); |
| 4104 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4105 | p->db = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4106 | sqlite3_free(p->zFreeOnClose); |
| 4107 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4108 | /* Check for command-line arguments */ |
| 4109 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 4110 | const char *z = azArg[iName]; |
| 4111 | if( optionMatch(z,"new") ){ |
| 4112 | newFlag = 1; |
| 4113 | }else if( z[0]=='-' ){ |
| 4114 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 4115 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4116 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4117 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4118 | } |
| 4119 | /* If a filename is specified, try to open it first */ |
| 4120 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 4121 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4122 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4123 | p->zDbFilename = zNewFilename; |
| 4124 | open_db(p, 1); |
| 4125 | if( p->db==0 ){ |
| 4126 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 4127 | sqlite3_free(zNewFilename); |
| 4128 | }else{ |
| 4129 | p->zFreeOnClose = zNewFilename; |
| 4130 | } |
| 4131 | } |
| 4132 | if( p->db==0 ){ |
| 4133 | /* As a fall-back open a TEMP database */ |
| 4134 | p->zDbFilename = 0; |
| 4135 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4136 | } |
| 4137 | }else |
| 4138 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4139 | if( c=='o' |
| 4140 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 4141 | ){ |
| 4142 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 4143 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4144 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4145 | rc = 1; |
| 4146 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4147 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4148 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 4149 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4150 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4151 | rc = 1; |
| 4152 | goto meta_command_exit; |
| 4153 | } |
| 4154 | p->outCount = 2; |
| 4155 | }else{ |
| 4156 | p->outCount = 0; |
| 4157 | } |
| 4158 | output_reset(p); |
| 4159 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4160 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4161 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4162 | rc = 1; |
| 4163 | p->out = stdout; |
| 4164 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4165 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 4166 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4167 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 4168 | p->out = stdout; |
| 4169 | rc = 1; |
| 4170 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4171 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 4172 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4173 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4174 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4175 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4176 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4177 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4178 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4179 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4180 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4181 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4182 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4183 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | }else |
| 4187 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 4188 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 4189 | int i; |
| 4190 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4191 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4192 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 4193 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4194 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 4195 | }else |
| 4196 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4197 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4198 | if( nArg >= 2) { |
| 4199 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 4200 | } |
| 4201 | if( nArg >= 3) { |
| 4202 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 4203 | } |
| 4204 | }else |
| 4205 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4206 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4207 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4208 | }else |
| 4209 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4210 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 4211 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 4212 | if( nArg!=2 ){ |
| 4213 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4214 | rc = 1; |
| 4215 | goto meta_command_exit; |
| 4216 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 4217 | alt = fopen(azArg[1], "rb"); |
| 4218 | if( alt==0 ){ |
| 4219 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 4220 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4221 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 4222 | rc = process_input(p, alt); |
| 4223 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4224 | } |
| 4225 | }else |
| 4226 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4227 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4228 | const char *zSrcFile; |
| 4229 | const char *zDb; |
| 4230 | sqlite3 *pSrc; |
| 4231 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 4232 | int nTimeout = 0; |
| 4233 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4234 | if( nArg==2 ){ |
| 4235 | zSrcFile = azArg[1]; |
| 4236 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4237 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4238 | zSrcFile = azArg[2]; |
| 4239 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4240 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4241 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4242 | rc = 1; |
| 4243 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4244 | } |
| 4245 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 4246 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4247 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4248 | sqlite3_close(pSrc); |
| 4249 | return 1; |
| 4250 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4251 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4252 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 4253 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4254 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4255 | sqlite3_close(pSrc); |
| 4256 | return 1; |
| 4257 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 4258 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 4259 | || rc==SQLITE_BUSY ){ |
| 4260 | if( rc==SQLITE_BUSY ){ |
| 4261 | if( nTimeout++ >= 3 ) break; |
| 4262 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4263 | } |
| 4264 | } |
| 4265 | sqlite3_backup_finish(pBackup); |
| 4266 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4267 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 4268 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4269 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4270 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4271 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4272 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4273 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4274 | } |
| 4275 | sqlite3_close(pSrc); |
| 4276 | }else |
| 4277 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 4278 | |
| 4279 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 4280 | if( nArg==2 ){ |
| 4281 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 4282 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4283 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 4284 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 4285 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4286 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 4287 | rc = 1; |
| 4288 | } |
| 4289 | }else |
| 4290 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4291 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4292 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4293 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4294 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4295 | memcpy(&data, p, sizeof(data)); |
| 4296 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4297 | data.cMode = data.mode = MODE_Semi; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4298 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 4299 | data.cMode = data.mode = MODE_Pretty; |
| 4300 | nArg--; |
| 4301 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 4302 | } |
| 4303 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 4304 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 4305 | 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] | 4306 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4307 | char *new_argv[2], *new_colv[2]; |
| 4308 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 4309 | " type text,\n" |
| 4310 | " name text,\n" |
| 4311 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 4312 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4313 | " sql text\n" |
| 4314 | ")"; |
| 4315 | new_argv[1] = 0; |
| 4316 | new_colv[0] = "sql"; |
| 4317 | new_colv[1] = 0; |
| 4318 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4319 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 4320 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 4321 | char *new_argv[2], *new_colv[2]; |
| 4322 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 4323 | " type text,\n" |
| 4324 | " name text,\n" |
| 4325 | " tbl_name text,\n" |
| 4326 | " rootpage integer,\n" |
| 4327 | " sql text\n" |
| 4328 | ")"; |
| 4329 | new_argv[1] = 0; |
| 4330 | new_colv[0] = "sql"; |
| 4331 | new_colv[1] = 0; |
| 4332 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4333 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4334 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 4335 | zShellStatic = azArg[1]; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4336 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 4337 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 4338 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 4339 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 4340 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 6ac7a58 | 2011-11-04 00:35:56 +0000 | [diff] [blame] | 4341 | "WHERE lower(tbl_name) LIKE shellstatic()" |
| 4342 | " AND type!='meta' AND sql NOTNULL " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 4343 | "ORDER BY rowid", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 4344 | callback, &data, &zErrMsg); |
| 4345 | zShellStatic = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4346 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4347 | }else if( nArg==1 ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4348 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 4349 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 4350 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 4351 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 4352 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4353 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 4354 | "ORDER BY rowid", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4355 | callback, &data, &zErrMsg |
| 4356 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4357 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4358 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4359 | rc = 1; |
| 4360 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4361 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4362 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4363 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4364 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4365 | rc = 1; |
| 4366 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4367 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4368 | rc = 1; |
| 4369 | }else{ |
| 4370 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4371 | } |
| 4372 | }else |
| 4373 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 4374 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 4375 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 1d9be4f | 2015-01-22 11:29:25 +0000 | [diff] [blame] | 4376 | sqlite3SelectTrace = integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 4377 | }else |
| 4378 | #endif |
| 4379 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4380 | #if defined(SQLITE_ENABLE_SESSION) |
| 4381 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 4382 | OpenSession *pSession = &p->aSession[0]; |
| 4383 | char **azCmd = &azArg[1]; |
| 4384 | int iSes = 0; |
| 4385 | int nCmd = nArg - 1; |
| 4386 | int i; |
| 4387 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4388 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4389 | if( nArg>=3 ){ |
| 4390 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 4391 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 4392 | } |
| 4393 | if( iSes<p->nSession ){ |
| 4394 | pSession = &p->aSession[iSes]; |
| 4395 | azCmd++; |
| 4396 | nCmd--; |
| 4397 | }else{ |
| 4398 | pSession = &p->aSession[0]; |
| 4399 | iSes = 0; |
| 4400 | } |
| 4401 | } |
| 4402 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4403 | /* .session attach TABLE |
| 4404 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 4405 | ** table so that it is never filtered. |
| 4406 | */ |
| 4407 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 4408 | if( nCmd!=2 ) goto session_syntax_error; |
| 4409 | if( pSession->p==0 ){ |
| 4410 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4411 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4412 | }else{ |
| 4413 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 4414 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4415 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4416 | rc = 0; |
| 4417 | } |
| 4418 | } |
| 4419 | }else |
| 4420 | |
| 4421 | /* .session changeset FILE |
| 4422 | ** .session patchset FILE |
| 4423 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 4424 | */ |
| 4425 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 4426 | FILE *out = 0; |
| 4427 | if( nCmd!=2 ) goto session_syntax_error; |
| 4428 | if( pSession->p==0 ) goto session_not_open; |
| 4429 | out = fopen(azCmd[1], "wb"); |
| 4430 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4431 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4432 | }else{ |
| 4433 | int szChng; |
| 4434 | void *pChng; |
| 4435 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 4436 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4437 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 4438 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 4439 | } |
| 4440 | if( rc ){ |
| 4441 | printf("Error: error code %d\n", rc); |
| 4442 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4443 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4444 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4445 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4446 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4447 | szChng); |
| 4448 | } |
| 4449 | sqlite3_free(pChng); |
| 4450 | fclose(out); |
| 4451 | } |
| 4452 | }else |
| 4453 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4454 | /* .session close |
| 4455 | ** Close the identified session |
| 4456 | */ |
| 4457 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4458 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4459 | if( p->nSession ){ |
| 4460 | session_close(pSession); |
| 4461 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 4462 | } |
| 4463 | }else |
| 4464 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4465 | /* .session enable ?BOOLEAN? |
| 4466 | ** Query or set the enable flag |
| 4467 | */ |
| 4468 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 4469 | int ii; |
| 4470 | if( nCmd>2 ) goto session_syntax_error; |
| 4471 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 4472 | if( p->nSession ){ |
| 4473 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4474 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 4475 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4476 | } |
| 4477 | }else |
| 4478 | |
| 4479 | /* .session filter GLOB .... |
| 4480 | ** Set a list of GLOB patterns of table names to be excluded. |
| 4481 | */ |
| 4482 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 4483 | int ii, nByte; |
| 4484 | if( nCmd<2 ) goto session_syntax_error; |
| 4485 | if( p->nSession ){ |
| 4486 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 4487 | sqlite3_free(pSession->azFilter[ii]); |
| 4488 | } |
| 4489 | sqlite3_free(pSession->azFilter); |
| 4490 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 4491 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 4492 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4493 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4494 | exit(1); |
| 4495 | } |
| 4496 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4497 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4498 | } |
| 4499 | pSession->nFilter = ii-1; |
| 4500 | } |
| 4501 | }else |
| 4502 | |
| 4503 | /* .session indirect ?BOOLEAN? |
| 4504 | ** Query or set the indirect flag |
| 4505 | */ |
| 4506 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 4507 | int ii; |
| 4508 | if( nCmd>2 ) goto session_syntax_error; |
| 4509 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 4510 | if( p->nSession ){ |
| 4511 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4512 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 4513 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4514 | } |
| 4515 | }else |
| 4516 | |
| 4517 | /* .session isempty |
| 4518 | ** Determine if the session is empty |
| 4519 | */ |
| 4520 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 4521 | int ii; |
| 4522 | if( nCmd!=1 ) goto session_syntax_error; |
| 4523 | if( p->nSession ){ |
| 4524 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4525 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 4526 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4527 | } |
| 4528 | }else |
| 4529 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4530 | /* .session list |
| 4531 | ** List all currently open sessions |
| 4532 | */ |
| 4533 | if( strcmp(azCmd[0],"list")==0 ){ |
| 4534 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4535 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4536 | } |
| 4537 | }else |
| 4538 | |
| 4539 | /* .session open DB NAME |
| 4540 | ** Open a new session called NAME on the attached database DB. |
| 4541 | ** DB is normally "main". |
| 4542 | */ |
| 4543 | if( strcmp(azCmd[0],"open")==0 ){ |
| 4544 | char *zName; |
| 4545 | if( nCmd!=3 ) goto session_syntax_error; |
| 4546 | zName = azCmd[2]; |
| 4547 | if( zName[0]==0 ) goto session_syntax_error; |
| 4548 | for(i=0; i<p->nSession; i++){ |
| 4549 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4550 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4551 | goto meta_command_exit; |
| 4552 | } |
| 4553 | } |
| 4554 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4555 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4556 | goto meta_command_exit; |
| 4557 | } |
| 4558 | pSession = &p->aSession[p->nSession]; |
| 4559 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 4560 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4561 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 4562 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4563 | goto meta_command_exit; |
| 4564 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4565 | pSession->nFilter = 0; |
| 4566 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4567 | p->nSession++; |
| 4568 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 4569 | }else |
| 4570 | /* If no command name matches, show a syntax error */ |
| 4571 | session_syntax_error: |
| 4572 | session_help(p); |
| 4573 | }else |
| 4574 | #endif |
| 4575 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 4576 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4577 | /* Undocumented commands for internal testing. Subject to change |
| 4578 | ** without notice. */ |
| 4579 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 4580 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 4581 | int i, v; |
| 4582 | for(i=1; i<nArg; i++){ |
| 4583 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4584 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4585 | } |
| 4586 | } |
| 4587 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 4588 | int i; sqlite3_int64 v; |
| 4589 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 4590 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4591 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4592 | 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] | 4593 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4594 | } |
| 4595 | } |
| 4596 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 4597 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4598 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4599 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 4600 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4601 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4602 | rc = 1; |
| 4603 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 4604 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4605 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 4606 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 4607 | } |
| 4608 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4609 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 4610 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 4611 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4612 | }else |
| 4613 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4614 | if( c=='s' |
| 4615 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4616 | ){ |
| 4617 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 4618 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4619 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4620 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4621 | rc = 1; |
| 4622 | goto meta_command_exit; |
| 4623 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 4624 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4625 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 4626 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 4627 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4628 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 4629 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4630 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4631 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4632 | }else |
| 4633 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4634 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4635 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4636 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4637 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4638 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4639 | rc = 1; |
| 4640 | goto meta_command_exit; |
| 4641 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4642 | utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]); |
| 4643 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4644 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 4645 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4646 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4647 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 4648 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 4649 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4650 | raw_printf(p->out, "\n"); |
| 4651 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4652 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4653 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4654 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4655 | raw_printf(p->out, "\n"); |
| 4656 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4657 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4658 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4659 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4660 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4661 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4662 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4663 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4664 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4665 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 4666 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4667 | }else |
| 4668 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4669 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 4670 | if( nArg==2 ){ |
| 4671 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 4672 | }else if( nArg==1 ){ |
| 4673 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4674 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 4675 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4676 | rc = 1; |
| 4677 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 4678 | }else |
| 4679 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4680 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4681 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 4682 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4683 | int nRow, nAlloc; |
| 4684 | char *zSql = 0; |
| 4685 | int ii; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4686 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4687 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4688 | if( rc ) return shellDatabaseError(p->db); |
| 4689 | |
| 4690 | /* Create an SQL statement to query for the list of tables in the |
| 4691 | ** main and all attached databases where the table name matches the |
| 4692 | ** LIKE pattern bound to variable "?1". */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4693 | zSql = sqlite3_mprintf( |
| 4694 | "SELECT name FROM sqlite_master" |
| 4695 | " WHERE type IN ('table','view')" |
| 4696 | " AND name NOT LIKE 'sqlite_%%'" |
| 4697 | " AND name LIKE ?1"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4698 | while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4699 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
| 4700 | if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue; |
| 4701 | if( strcmp(zDbName,"temp")==0 ){ |
| 4702 | zSql = sqlite3_mprintf( |
| 4703 | "%z UNION ALL " |
| 4704 | "SELECT 'temp.' || name FROM sqlite_temp_master" |
| 4705 | " WHERE type IN ('table','view')" |
| 4706 | " AND name NOT LIKE 'sqlite_%%'" |
| 4707 | " AND name LIKE ?1", zSql); |
| 4708 | }else{ |
| 4709 | zSql = sqlite3_mprintf( |
| 4710 | "%z UNION ALL " |
| 4711 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 4712 | " WHERE type IN ('table','view')" |
| 4713 | " AND name NOT LIKE 'sqlite_%%'" |
| 4714 | " AND name LIKE ?1", zSql, zDbName, zDbName); |
| 4715 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 4716 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4717 | rc = sqlite3_finalize(pStmt); |
| 4718 | if( zSql && rc==SQLITE_OK ){ |
| 4719 | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 4720 | if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4721 | } |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4722 | sqlite3_free(zSql); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4723 | if( !zSql ) return shellNomemError(); |
| 4724 | if( rc ) return shellDatabaseError(p->db); |
| 4725 | |
| 4726 | /* Run the SQL statement prepared by the above block. Store the results |
| 4727 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4728 | nRow = nAlloc = 0; |
| 4729 | azResult = 0; |
| 4730 | if( nArg>1 ){ |
| 4731 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4732 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4733 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 4734 | } |
| 4735 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4736 | if( nRow>=nAlloc ){ |
| 4737 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4738 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4739 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4740 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4741 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4742 | break; |
| 4743 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4744 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4745 | azResult = azNew; |
| 4746 | } |
| 4747 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4748 | if( 0==azResult[nRow] ){ |
| 4749 | rc = shellNomemError(); |
| 4750 | break; |
| 4751 | } |
| 4752 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4753 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4754 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 4755 | rc = shellDatabaseError(p->db); |
| 4756 | } |
| 4757 | |
| 4758 | /* Pretty-print the contents of array azResult[] to the output */ |
| 4759 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 4760 | int len, maxlen = 0; |
| 4761 | int i, j; |
| 4762 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4763 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4764 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 4765 | if( len>maxlen ) maxlen = len; |
| 4766 | } |
| 4767 | nPrintCol = 80/(maxlen+2); |
| 4768 | if( nPrintCol<1 ) nPrintCol = 1; |
| 4769 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 4770 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4771 | for(j=i; j<nRow; j+=nPrintRow){ |
| 4772 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4773 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 4774 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 4775 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4776 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 4777 | } |
| 4778 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4779 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 4780 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 4781 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4782 | }else |
| 4783 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4784 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 4785 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 4786 | output_reset(p); |
| 4787 | p->out = output_file_open("testcase-out.txt"); |
| 4788 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 4789 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4790 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4791 | if( nArg>=2 ){ |
| 4792 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 4793 | }else{ |
| 4794 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 4795 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4796 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4797 | |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4798 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4799 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4800 | static const struct { |
| 4801 | const char *zCtrlName; /* Name of a test-control option */ |
| 4802 | int ctrlCode; /* Integer code for that option */ |
| 4803 | } aCtrl[] = { |
| 4804 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 4805 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 4806 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 4807 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 4808 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 4809 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 4810 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 4811 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 4812 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 4813 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 4814 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 4815 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4816 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 4817 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 4818 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 4819 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4820 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4821 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4822 | int rc2 = 0; |
| 4823 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4824 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4825 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4826 | /* convert testctrl text option to value. allow any unique prefix |
| 4827 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4828 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4829 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4830 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4831 | if( testctrl<0 ){ |
| 4832 | testctrl = aCtrl[i].ctrlCode; |
| 4833 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4834 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4835 | testctrl = -1; |
| 4836 | break; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4840 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4841 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4842 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4843 | }else{ |
| 4844 | switch(testctrl){ |
| 4845 | |
| 4846 | /* sqlite3_test_control(int, db, int) */ |
| 4847 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4848 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4849 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4850 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4851 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4852 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4853 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4854 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4855 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4856 | } |
| 4857 | break; |
| 4858 | |
| 4859 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 4860 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 4861 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4862 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 4863 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4864 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4865 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4866 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4867 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4868 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 4869 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4870 | } |
| 4871 | break; |
| 4872 | |
| 4873 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4874 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4875 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4876 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4877 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4878 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4879 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4880 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4881 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4882 | } |
| 4883 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4884 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4885 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4886 | case SQLITE_TESTCTRL_ASSERT: |
| 4887 | case SQLITE_TESTCTRL_ALWAYS: |
| 4888 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4889 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4890 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4891 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4892 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4893 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4894 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 4895 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4896 | } |
| 4897 | break; |
| 4898 | |
| 4899 | /* sqlite3_test_control(int, char *) */ |
| 4900 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4901 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4902 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4903 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4904 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4905 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4906 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4907 | utf8_printf(stderr, |
| 4908 | "Error: testctrl %s takes a single char * option\n", |
| 4909 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4910 | } |
| 4911 | break; |
| 4912 | #endif |
| 4913 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 4914 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 4915 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4916 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 4917 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 4918 | integerValue(azArg[3]), |
| 4919 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4920 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 4921 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4922 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 4923 | } |
| 4924 | break; |
| 4925 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4926 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 4927 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 4928 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 4929 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4930 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4931 | utf8_printf(stderr, |
| 4932 | "Error: CLI support for testctrl %s not implemented\n", |
| 4933 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 4934 | break; |
| 4935 | } |
| 4936 | } |
| 4937 | }else |
| 4938 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4939 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4940 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4941 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 4942 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4943 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4944 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 4945 | if( nArg==2 ){ |
| 4946 | enableTimer = booleanValue(azArg[1]); |
| 4947 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4948 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4949 | enableTimer = 0; |
| 4950 | } |
| 4951 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4952 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4953 | rc = 1; |
| 4954 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 4955 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4956 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4957 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4958 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4959 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4960 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4961 | rc = 1; |
| 4962 | goto meta_command_exit; |
| 4963 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 4964 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4965 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 4966 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4967 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 4968 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4969 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 4970 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4971 | } |
| 4972 | #endif |
| 4973 | }else |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4974 | #endif /* !defined(SQLITE_OMIT_BUILTIN_TEST) */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4975 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4976 | #if SQLITE_USER_AUTHENTICATION |
| 4977 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 4978 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4979 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4980 | rc = 1; |
| 4981 | goto meta_command_exit; |
| 4982 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 4983 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4984 | if( strcmp(azArg[1],"login")==0 ){ |
| 4985 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4986 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4987 | rc = 1; |
| 4988 | goto meta_command_exit; |
| 4989 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 4990 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 4991 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4992 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4993 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4994 | rc = 1; |
| 4995 | } |
| 4996 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 4997 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4998 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 4999 | rc = 1; |
| 5000 | goto meta_command_exit; |
| 5001 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 5002 | rc = sqlite3_user_add(p->db, azArg[2], |
| 5003 | azArg[3], (int)strlen(azArg[3]), |
| 5004 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5005 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5006 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5007 | rc = 1; |
| 5008 | } |
| 5009 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 5010 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5011 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5012 | rc = 1; |
| 5013 | goto meta_command_exit; |
| 5014 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 5015 | rc = sqlite3_user_change(p->db, azArg[2], |
| 5016 | azArg[3], (int)strlen(azArg[3]), |
| 5017 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5018 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5019 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5020 | rc = 1; |
| 5021 | } |
| 5022 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 5023 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5024 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5025 | rc = 1; |
| 5026 | goto meta_command_exit; |
| 5027 | } |
| 5028 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 5029 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5030 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5031 | rc = 1; |
| 5032 | } |
| 5033 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5034 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5035 | rc = 1; |
| 5036 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5037 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 5038 | }else |
| 5039 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 5040 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 5041 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5042 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 5043 | sqlite3_libversion(), sqlite3_sourceid()); |
| 5044 | }else |
| 5045 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 5046 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 5047 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 5048 | sqlite3_vfs *pVfs; |
| 5049 | if( p->db ){ |
| 5050 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 5051 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5052 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 5053 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 5054 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 5055 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 5056 | } |
| 5057 | } |
| 5058 | }else |
| 5059 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 5060 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 5061 | sqlite3_vfs *pVfs; |
| 5062 | sqlite3_vfs *pCurrent = 0; |
| 5063 | if( p->db ){ |
| 5064 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 5065 | } |
| 5066 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 5067 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 5068 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 5069 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 5070 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 5071 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 5072 | if( pVfs->pNext ){ |
| 5073 | raw_printf(p->out, "-----------------------------------\n"); |
| 5074 | } |
| 5075 | } |
| 5076 | }else |
| 5077 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5078 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 5079 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 5080 | char *zVfsName = 0; |
| 5081 | if( p->db ){ |
| 5082 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 5083 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5084 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5085 | sqlite3_free(zVfsName); |
| 5086 | } |
| 5087 | } |
| 5088 | }else |
| 5089 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 5090 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 5091 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5092 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 5093 | }else |
| 5094 | #endif |
| 5095 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5096 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5097 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 5098 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5099 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5100 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5101 | } |
| 5102 | }else |
| 5103 | |
| 5104 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5105 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5106 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5107 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5108 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5109 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5110 | meta_command_exit: |
| 5111 | if( p->outCount ){ |
| 5112 | p->outCount--; |
| 5113 | if( p->outCount==0 ) output_reset(p); |
| 5114 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5115 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5118 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 5119 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 5120 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 5121 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5122 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 5123 | int i; |
| 5124 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 5125 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 5129 | ** Test to see if a line consists entirely of whitespace. |
| 5130 | */ |
| 5131 | static int _all_whitespace(const char *z){ |
| 5132 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5133 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 5134 | if( *z=='/' && z[1]=='*' ){ |
| 5135 | z += 2; |
| 5136 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 5137 | if( *z==0 ) return 0; |
| 5138 | z++; |
| 5139 | continue; |
| 5140 | } |
| 5141 | if( *z=='-' && z[1]=='-' ){ |
| 5142 | z += 2; |
| 5143 | while( *z && *z!='\n' ){ z++; } |
| 5144 | if( *z==0 ) return 1; |
| 5145 | continue; |
| 5146 | } |
| 5147 | return 0; |
| 5148 | } |
| 5149 | return 1; |
| 5150 | } |
| 5151 | |
| 5152 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 5153 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 5154 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 5155 | ** as is the Oracle "/". |
| 5156 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5157 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5158 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 5159 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 5160 | return 1; /* Oracle */ |
| 5161 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5162 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5163 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 5164 | return 1; /* SQL Server */ |
| 5165 | } |
| 5166 | return 0; |
| 5167 | } |
| 5168 | |
| 5169 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 5170 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 5171 | ** ends in the middle of a string literal or C-style comment. |
| 5172 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5173 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 5174 | int rc; |
| 5175 | if( zSql==0 ) return 1; |
| 5176 | zSql[nSql] = ';'; |
| 5177 | zSql[nSql+1] = 0; |
| 5178 | rc = sqlite3_complete(zSql); |
| 5179 | zSql[nSql] = 0; |
| 5180 | return rc; |
| 5181 | } |
| 5182 | |
| 5183 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5184 | ** Run a single line of SQL |
| 5185 | */ |
| 5186 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 5187 | int rc; |
| 5188 | char *zErrMsg = 0; |
| 5189 | |
| 5190 | open_db(p, 0); |
| 5191 | if( p->backslashOn ) resolve_backslashes(zSql); |
| 5192 | BEGIN_TIMER; |
| 5193 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 5194 | END_TIMER; |
| 5195 | if( rc || zErrMsg ){ |
| 5196 | char zPrefix[100]; |
| 5197 | if( in!=0 || !stdin_is_interactive ){ |
| 5198 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 5199 | "Error: near line %d:", startline); |
| 5200 | }else{ |
| 5201 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 5202 | } |
| 5203 | if( zErrMsg!=0 ){ |
| 5204 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 5205 | sqlite3_free(zErrMsg); |
| 5206 | zErrMsg = 0; |
| 5207 | }else{ |
| 5208 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 5209 | } |
| 5210 | return 1; |
| 5211 | }else if( p->countChanges ){ |
| 5212 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 5213 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 5214 | } |
| 5215 | return 0; |
| 5216 | } |
| 5217 | |
| 5218 | |
| 5219 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5220 | ** Read input from *in and process it. If *in==0 then input |
| 5221 | ** is interactive - the user is typing it it. Otherwise, input |
| 5222 | ** is coming from a file or device. A prompt is issued and history |
| 5223 | ** is saved only if input is interactive. An interrupt signal will |
| 5224 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5225 | ** |
| 5226 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5227 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5228 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5229 | char *zLine = 0; /* A single input line */ |
| 5230 | char *zSql = 0; /* Accumulated SQL text */ |
| 5231 | int nLine; /* Length of current line */ |
| 5232 | int nSql = 0; /* Bytes of zSql[] used */ |
| 5233 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 5234 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5235 | int rc; /* Error code */ |
| 5236 | int errCnt = 0; /* Number of errors seen */ |
| 5237 | int lineno = 0; /* Current line number */ |
| 5238 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5239 | |
| 5240 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 5241 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5242 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5243 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 5244 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 5245 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 5246 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5247 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5248 | if( seenInterrupt ){ |
| 5249 | if( in!=0 ) break; |
| 5250 | seenInterrupt = 0; |
| 5251 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5252 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 5253 | if( nSql==0 && _all_whitespace(zLine) ){ |
| 5254 | if( p->echoOn ) printf("%s\n", zLine); |
| 5255 | continue; |
| 5256 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 5257 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 5258 | if( p->echoOn ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5259 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5260 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5261 | break; |
| 5262 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5263 | errCnt++; |
| 5264 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5265 | continue; |
| 5266 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5267 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5268 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 5269 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5270 | nLine = strlen30(zLine); |
| 5271 | if( nSql+nLine+2>=nAlloc ){ |
| 5272 | nAlloc = nSql+nLine+100; |
| 5273 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5274 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5275 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5276 | exit(1); |
| 5277 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5278 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5279 | nSqlPrior = nSql; |
| 5280 | if( nSql==0 ){ |
| 5281 | int i; |
| 5282 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 5283 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5284 | memcpy(zSql, zLine+i, nLine+1-i); |
| 5285 | startline = lineno; |
| 5286 | nSql = nLine-i; |
| 5287 | }else{ |
| 5288 | zSql[nSql++] = '\n'; |
| 5289 | memcpy(zSql+nSql, zLine, nLine+1); |
| 5290 | nSql += nLine; |
| 5291 | } |
| 5292 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 5293 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5294 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5295 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5296 | if( p->outCount ){ |
| 5297 | output_reset(p); |
| 5298 | p->outCount = 0; |
| 5299 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 5300 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 5301 | if( p->echoOn ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 5302 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5303 | } |
| 5304 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5305 | if( nSql && !_all_whitespace(zSql) ){ |
| 5306 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5307 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 5308 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 5309 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 5310 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5313 | /* |
| 5314 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5315 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5316 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 5317 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5318 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 5319 | if( clearFlag ){ |
| 5320 | free(home_dir); |
| 5321 | home_dir = 0; |
| 5322 | return 0; |
| 5323 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5324 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5325 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 5326 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 5327 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 5328 | { |
| 5329 | struct passwd *pwent; |
| 5330 | uid_t uid = getuid(); |
| 5331 | if( (pwent=getpwuid(uid)) != NULL) { |
| 5332 | home_dir = pwent->pw_dir; |
| 5333 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5334 | } |
| 5335 | #endif |
| 5336 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 5337 | #if defined(_WIN32_WCE) |
| 5338 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 5339 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5340 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 5341 | #else |
| 5342 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 5343 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 5344 | if (!home_dir) { |
| 5345 | home_dir = getenv("USERPROFILE"); |
| 5346 | } |
| 5347 | #endif |
| 5348 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5349 | if (!home_dir) { |
| 5350 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5351 | } |
| 5352 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 5353 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 5354 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 5355 | char *zDrive, *zPath; |
| 5356 | int n; |
| 5357 | zDrive = getenv("HOMEDRIVE"); |
| 5358 | zPath = getenv("HOMEPATH"); |
| 5359 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5360 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 5361 | home_dir = malloc( n ); |
| 5362 | if( home_dir==0 ) return 0; |
| 5363 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 5364 | return home_dir; |
| 5365 | } |
| 5366 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 5367 | } |
| 5368 | #endif |
| 5369 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 5370 | #endif /* !_WIN32_WCE */ |
| 5371 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5372 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5373 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5374 | char *z = malloc( n ); |
| 5375 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5376 | home_dir = z; |
| 5377 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 5378 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5379 | return home_dir; |
| 5380 | } |
| 5381 | |
| 5382 | /* |
| 5383 | ** Read input from the file given by sqliterc_override. Or if that |
| 5384 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5385 | ** |
| 5386 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5387 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 5388 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5389 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5390 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 5391 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5392 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5393 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 5394 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5395 | FILE *in = NULL; |
| 5396 | |
| 5397 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 5398 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 5399 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5400 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 5401 | " cannot read ~/.sqliterc\n"); |
| 5402 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 5403 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 5404 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5405 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 5406 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5407 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 5408 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5409 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5410 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5411 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5412 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 5413 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 5414 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5415 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 5416 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5419 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5420 | ** Show available command line options |
| 5421 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5422 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5423 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5424 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5425 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5426 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 5427 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5428 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5429 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 5430 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5431 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5432 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 5433 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 5434 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5435 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5436 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5437 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5438 | " -line set output mode to 'line'\n" |
| 5439 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5440 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 5441 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5442 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 5443 | " -multiplex enable the multiplexor VFS\n" |
| 5444 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5445 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5446 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5447 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 5448 | " -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] | 5449 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 5450 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5451 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5452 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 5453 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 5454 | " -vfstrace enable tracing of all VFS calls\n" |
| 5455 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5456 | ; |
| 5457 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5458 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5459 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 5460 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 5461 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5462 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5463 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5464 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5465 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5466 | } |
| 5467 | exit(1); |
| 5468 | } |
| 5469 | |
| 5470 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5471 | ** Initialize the state information in data |
| 5472 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5473 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5474 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5475 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 5476 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5477 | memcpy(data->colSeparator,SEP_Column, 2); |
| 5478 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5479 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5480 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 5481 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5482 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5483 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5484 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 5485 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5488 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 5489 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5490 | */ |
| 5491 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 5492 | static void printBold(const char *zText){ |
| 5493 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 5494 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 5495 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 5496 | SetConsoleTextAttribute(out, |
| 5497 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 5498 | ); |
| 5499 | printf("%s", zText); |
| 5500 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5501 | } |
| 5502 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 5503 | static void printBold(const char *zText){ |
| 5504 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5505 | } |
| 5506 | #endif |
| 5507 | |
| 5508 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5509 | ** Get the argument to an --option. Throw an error and die if no argument |
| 5510 | ** is available. |
| 5511 | */ |
| 5512 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 5513 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5514 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5515 | argv[0], argv[argc-1]); |
| 5516 | exit(1); |
| 5517 | } |
| 5518 | return argv[i]; |
| 5519 | } |
| 5520 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5521 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 5522 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 5523 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 5524 | # else |
| 5525 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 5526 | # endif |
| 5527 | #endif |
| 5528 | |
| 5529 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 5530 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5531 | #else |
| 5532 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 5533 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5534 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5535 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5536 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5537 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5538 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5539 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 5540 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5541 | int readStdin = 1; |
| 5542 | int nCmd = 0; |
| 5543 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5544 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5545 | setBinaryMode(stdin, 0); |
| 5546 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 5547 | stdin_is_interactive = isatty(0); |
| 5548 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5549 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 5550 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 5551 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5552 | 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] | 5553 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 5554 | exit(1); |
| 5555 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5556 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5557 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5558 | #if !SQLITE_SHELL_IS_UTF8 |
| 5559 | sqlite3_initialize(); |
| 5560 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 5561 | if( argv==0 ){ |
| 5562 | raw_printf(stderr, "out of memory\n"); |
| 5563 | exit(1); |
| 5564 | } |
| 5565 | for(i=0; i<argc; i++){ |
| 5566 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 5567 | if( argv[i]==0 ){ |
| 5568 | raw_printf(stderr, "out of memory\n"); |
| 5569 | exit(1); |
| 5570 | } |
| 5571 | } |
| 5572 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 5573 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5574 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5575 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5576 | /* Make sure we have a valid signal handler early, before anything |
| 5577 | ** else is done. |
| 5578 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 5579 | #ifdef SIGINT |
| 5580 | signal(SIGINT, interrupt_handler); |
| 5581 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5582 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5583 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 5584 | { |
| 5585 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 5586 | ** of a C-function that will provide the name of the database file. Use |
| 5587 | ** this compile-time option to embed this shell program in larger |
| 5588 | ** applications. */ |
| 5589 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 5590 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 5591 | warnInmemoryDb = 0; |
| 5592 | } |
| 5593 | #endif |
| 5594 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5595 | /* Do an initial pass through the command-line argument to locate |
| 5596 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5597 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5598 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5599 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5600 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5601 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5602 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5603 | if( z[0]!='-' ){ |
| 5604 | if( data.zDbFilename==0 ){ |
| 5605 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5606 | }else{ |
| 5607 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 5608 | ** mean that nothing is read from stdin */ |
| 5609 | readStdin = 0; |
| 5610 | nCmd++; |
| 5611 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 5612 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5613 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5614 | exit(1); |
| 5615 | } |
| 5616 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5617 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5618 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5619 | if( z[1]=='-' ) z++; |
| 5620 | if( strcmp(z,"-separator")==0 |
| 5621 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5622 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5623 | || strcmp(z,"-cmd")==0 |
| 5624 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5625 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5626 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5627 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5628 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5629 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5630 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5631 | ** we do the actual processing of arguments later in a second pass. |
| 5632 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 5633 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5634 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5635 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5636 | const char *zSize; |
| 5637 | sqlite3_int64 szHeap; |
| 5638 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5639 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 5640 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5641 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5642 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 5643 | #else |
| 5644 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5645 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5646 | }else if( strcmp(z,"-scratch")==0 ){ |
| 5647 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5648 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5649 | if( sz>400000 ) sz = 400000; |
| 5650 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5651 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5652 | if( n>10 ) n = 10; |
| 5653 | if( n<1 ) n = 1; |
| 5654 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 5655 | data.shellFlgs |= SHFLG_Scratch; |
| 5656 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 5657 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5658 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5659 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 5660 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5661 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 5662 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 5663 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5664 | data.shellFlgs |= SHFLG_Pagecache; |
| 5665 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 5666 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5667 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5668 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 5669 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5670 | if( n<0 ) n = 0; |
| 5671 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 5672 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 5673 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5674 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 5675 | extern int vfstrace_register( |
| 5676 | const char *zTraceName, |
| 5677 | const char *zOldVfsName, |
| 5678 | int (*xOut)(const char*,void*), |
| 5679 | void *pOutArg, |
| 5680 | int makeDefault |
| 5681 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 5682 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 5683 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 5684 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5685 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 5686 | extern int sqlite3_multiple_initialize(const char*,int); |
| 5687 | sqlite3_multiplex_initialize(0, 1); |
| 5688 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 5689 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5690 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 5691 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5692 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5693 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5694 | if( pVfs ){ |
| 5695 | sqlite3_vfs_register(pVfs, 1); |
| 5696 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5697 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5698 | exit(1); |
| 5699 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5700 | } |
| 5701 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5702 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 5703 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5704 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5705 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 5706 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5707 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 5708 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 5709 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5710 | } |
| 5711 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 5712 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5713 | /* Go ahead and open the database file if it already exists. If the |
| 5714 | ** file does not exist, delay opening it. This prevents empty database |
| 5715 | ** files from being created if a user mistypes the database name argument |
| 5716 | ** to the sqlite command-line tool. |
| 5717 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5718 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5719 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5722 | /* Process the initialization file if there is one. If no -init option |
| 5723 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 5724 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5725 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 5726 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5727 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5728 | /* Make a second pass through the command-line argument and set |
| 5729 | ** options. This second pass is delayed until after the initialization |
| 5730 | ** file is processed so that the command-line arguments will override |
| 5731 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5732 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5733 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5734 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5735 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5736 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 5737 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5738 | i++; |
| 5739 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5740 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5741 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5742 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5743 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5744 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5745 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 5746 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5747 | }else if( strcmp(z,"-csv")==0 ){ |
| 5748 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5749 | memcpy(data.colSeparator,",",2); |
| 5750 | }else if( strcmp(z,"-ascii")==0 ){ |
| 5751 | data.mode = MODE_Ascii; |
| 5752 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5753 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5754 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5755 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5756 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5757 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5758 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5759 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5760 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 5761 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5762 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5763 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5764 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5765 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5766 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5767 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5768 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5769 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5770 | data.echoOn = 1; |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 5771 | }else if( strcmp(z,"-eqp")==0 ){ |
| 5772 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5773 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 5774 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 5775 | }else if( strcmp(z,"-stats")==0 ){ |
| 5776 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5777 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 5778 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 5779 | }else if( strcmp(z,"-backslash")==0 ){ |
| 5780 | /* Undocumented command-line option: -backslash |
| 5781 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 5782 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 5783 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 5784 | */ |
| 5785 | data.backslashOn = 1; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5786 | }else if( strcmp(z,"-bail")==0 ){ |
| 5787 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 5788 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 5789 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 5790 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5791 | }else if( strcmp(z,"-interactive")==0 ){ |
| 5792 | stdin_is_interactive = 1; |
| 5793 | }else if( strcmp(z,"-batch")==0 ){ |
| 5794 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 5795 | }else if( strcmp(z,"-heap")==0 ){ |
| 5796 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 5797 | }else if( strcmp(z,"-scratch")==0 ){ |
| 5798 | i+=2; |
| 5799 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 5800 | i+=2; |
| 5801 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 5802 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 5803 | }else if( strcmp(z,"-mmap")==0 ){ |
| 5804 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5805 | }else if( strcmp(z,"-vfs")==0 ){ |
| 5806 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 5807 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 5808 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 5809 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 5810 | #endif |
| 5811 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 5812 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 5813 | i++; |
| 5814 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5815 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 5816 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5817 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5818 | /* Run commands that follow -cmd first and separately from commands |
| 5819 | ** that simply appear on the command-line. This seems goofy. It would |
| 5820 | ** be better if all commands ran in the order that they appear. But |
| 5821 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5822 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 5823 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5824 | if( z[0]=='.' ){ |
| 5825 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 5826 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5827 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5828 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5829 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 5830 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5831 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5832 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 5833 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5834 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 5835 | if( bail_on_error ) return rc; |
| 5836 | } |
| 5837 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5838 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5839 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 5840 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5841 | return 1; |
| 5842 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5843 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5844 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5845 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5846 | if( !readStdin ){ |
| 5847 | /* Run all arguments that do not begin with '-' as if they were separate |
| 5848 | ** command-line inputs, except for the argToSkip argument which contains |
| 5849 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5850 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5851 | for(i=0; i<nCmd; i++){ |
| 5852 | if( azCmd[i][0]=='.' ){ |
| 5853 | rc = do_meta_command(azCmd[i], &data); |
| 5854 | if( rc ) return rc==2 ? 0 : rc; |
| 5855 | }else{ |
| 5856 | open_db(&data, 0); |
| 5857 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 5858 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5859 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5860 | return rc!=0 ? rc : 1; |
| 5861 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5862 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5863 | return rc; |
| 5864 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 5865 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5866 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 5867 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5868 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 5869 | /* Run commands received from standard input |
| 5870 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5871 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5872 | char *zHome; |
| 5873 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5874 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5875 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 5876 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5877 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 5878 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5879 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 5880 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5881 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 5882 | printBold("transient in-memory database"); |
| 5883 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 5884 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 5885 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 5886 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5887 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5888 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5889 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 5890 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 5891 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5892 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5893 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5894 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5895 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 5896 | shell_stifle_history(100); |
| 5897 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 5898 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5899 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5900 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5901 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5902 | } |
| 5903 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 5904 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 5905 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5906 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 5907 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 5908 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5909 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 5910 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5911 | #if !SQLITE_SHELL_IS_UTF8 |
| 5912 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 5913 | sqlite3_free(argv); |
| 5914 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 5915 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5916 | } |