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 | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 430 | /* |
| 431 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 432 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 433 | ** in bytes. This is different from the %*.*s specification in printf |
| 434 | ** since with %*.*s the width is measured in bytes, not characters. |
| 435 | */ |
| 436 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 437 | int i; |
| 438 | int n; |
| 439 | int aw = w<0 ? -w : w; |
| 440 | char zBuf[1000]; |
drh | f8a2e8c | 2017-05-06 17:12:52 +0000 | [diff] [blame] | 441 | if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 442 | for(i=n=0; zUtf[i]; i++){ |
| 443 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 444 | n++; |
| 445 | if( n==aw ){ |
| 446 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | if( n>=aw ){ |
| 452 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 453 | }else if( w<0 ){ |
| 454 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 455 | }else{ |
| 456 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 457 | } |
| 458 | } |
| 459 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 460 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 461 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 462 | ** Determines if a string is a number of not. |
| 463 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 464 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 465 | if( *z=='-' || *z=='+' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 466 | if( !IsDigit(*z) ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | z++; |
| 470 | if( realnum ) *realnum = 0; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 471 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 472 | if( *z=='.' ){ |
| 473 | z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 474 | if( !IsDigit(*z) ) return 0; |
| 475 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 476 | if( realnum ) *realnum = 1; |
| 477 | } |
| 478 | if( *z=='e' || *z=='E' ){ |
| 479 | z++; |
| 480 | if( *z=='+' || *z=='-' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 481 | if( !IsDigit(*z) ) return 0; |
| 482 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 483 | if( realnum ) *realnum = 1; |
| 484 | } |
| 485 | return *z==0; |
| 486 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 487 | |
| 488 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 489 | ** Compute a string length that is limited to what can be stored in |
| 490 | ** lower 30 bits of a 32-bit signed integer. |
| 491 | */ |
| 492 | static int strlen30(const char *z){ |
| 493 | const char *z2 = z; |
| 494 | while( *z2 ){ z2++; } |
| 495 | return 0x3fffffff & (int)(z2 - z); |
| 496 | } |
| 497 | |
| 498 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 499 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 500 | ** the text in memory obtained from malloc() and returns a pointer |
| 501 | ** to the text. NULL is returned at end of file, or if malloc() |
| 502 | ** fails. |
| 503 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 504 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 505 | ** a previous call to this routine that may be reused. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 506 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 507 | static char *local_getline(char *zLine, FILE *in){ |
| 508 | int nLine = zLine==0 ? 0 : 100; |
| 509 | int n = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 510 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 511 | while( 1 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 512 | if( n+100>nLine ){ |
| 513 | nLine = nLine*2 + 100; |
| 514 | zLine = realloc(zLine, nLine); |
| 515 | if( zLine==0 ) return 0; |
| 516 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 517 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 518 | if( n==0 ){ |
| 519 | free(zLine); |
| 520 | return 0; |
| 521 | } |
| 522 | zLine[n] = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 523 | break; |
| 524 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 525 | while( zLine[n] ) n++; |
| 526 | if( n>0 && zLine[n-1]=='\n' ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 527 | n--; |
shaneh | 13b3602 | 2009-12-17 21:07:15 +0000 | [diff] [blame] | 528 | if( n>0 && zLine[n-1]=='\r' ) n--; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 529 | zLine[n] = 0; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 530 | break; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 533 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 534 | /* For interactive input on Windows systems, translate the |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 535 | ** multi-byte characterset characters into UTF-8. */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 536 | if( stdin_is_interactive && in==stdin ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 537 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 538 | if( zTrans ){ |
| 539 | int nTrans = strlen30(zTrans)+1; |
| 540 | if( nTrans>nLine ){ |
| 541 | zLine = realloc(zLine, nTrans); |
| 542 | if( zLine==0 ){ |
| 543 | sqlite3_free(zTrans); |
| 544 | return 0; |
| 545 | } |
| 546 | } |
| 547 | memcpy(zLine, zTrans, nTrans); |
| 548 | sqlite3_free(zTrans); |
| 549 | } |
| 550 | } |
| 551 | #endif /* defined(_WIN32) || defined(WIN32) */ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 552 | return zLine; |
| 553 | } |
| 554 | |
| 555 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 556 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 557 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 558 | ** If in==0 then read from standard input and prompt before each line. |
| 559 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 560 | ** If isContinuation is zero, then the main prompt should be used. |
| 561 | ** |
| 562 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 563 | ** routine that can be reused. |
| 564 | ** |
| 565 | ** The result is stored in space obtained from malloc() and must either |
| 566 | ** be freed by the caller or else passed back into this routine via the |
| 567 | ** zPrior argument for reuse. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 568 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 569 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 570 | char *zPrompt; |
| 571 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 572 | if( in!=0 ){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 573 | zResult = local_getline(zPrior, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 574 | }else{ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 575 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 576 | #if SHELL_USE_LOCAL_GETLINE |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 577 | printf("%s", zPrompt); |
| 578 | fflush(stdout); |
| 579 | zResult = local_getline(zPrior, stdin); |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 580 | #else |
| 581 | free(zPrior); |
| 582 | zResult = shell_readline(zPrompt); |
| 583 | if( zResult && *zResult ) shell_add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 584 | #endif |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 585 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 586 | return zResult; |
| 587 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 588 | /* |
| 589 | ** A variable length string to which one can append text. |
| 590 | */ |
| 591 | typedef struct ShellText ShellText; |
| 592 | struct ShellText { |
| 593 | char *z; |
| 594 | int n; |
| 595 | int nAlloc; |
| 596 | }; |
| 597 | |
| 598 | /* |
| 599 | ** Initialize and destroy a ShellText object |
| 600 | */ |
| 601 | static void initText(ShellText *p){ |
| 602 | memset(p, 0, sizeof(*p)); |
| 603 | } |
| 604 | static void freeText(ShellText *p){ |
| 605 | free(p->z); |
| 606 | initText(p); |
| 607 | } |
| 608 | |
| 609 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 610 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 611 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 612 | ** zIn, if it was not NULL, is freed. |
| 613 | ** |
| 614 | ** If the third argument, quote, is not '\0', then it is used as a |
| 615 | ** quote character for zAppend. |
| 616 | */ |
| 617 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 618 | int len; |
| 619 | int i; |
| 620 | int nAppend = strlen30(zAppend); |
| 621 | |
| 622 | len = nAppend+p->n+1; |
| 623 | if( quote ){ |
| 624 | len += 2; |
| 625 | for(i=0; i<nAppend; i++){ |
| 626 | if( zAppend[i]==quote ) len++; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if( p->n+len>=p->nAlloc ){ |
| 631 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 632 | p->z = realloc(p->z, p->nAlloc); |
| 633 | if( p->z==0 ){ |
| 634 | memset(p, 0, sizeof(*p)); |
| 635 | return; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if( quote ){ |
| 640 | char *zCsr = p->z+p->n; |
| 641 | *zCsr++ = quote; |
| 642 | for(i=0; i<nAppend; i++){ |
| 643 | *zCsr++ = zAppend[i]; |
| 644 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 645 | } |
| 646 | *zCsr++ = quote; |
| 647 | p->n = (int)(zCsr - p->z); |
| 648 | *zCsr = '\0'; |
| 649 | }else{ |
| 650 | memcpy(p->z+p->n, zAppend, nAppend); |
| 651 | p->n += nAppend; |
| 652 | p->z[p->n] = '\0'; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 658 | ** because it contains non-alphanumeric characters, or because it is an |
| 659 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 660 | ** that quoting is required. |
| 661 | ** |
| 662 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 663 | */ |
| 664 | static char quoteChar(const char *zName){ |
| 665 | /* All SQLite keywords, in alphabetical order */ |
| 666 | static const char *azKeywords[] = { |
| 667 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 668 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 669 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 670 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 671 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 672 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 673 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 674 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 675 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 676 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 677 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 678 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 679 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 680 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 681 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 682 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 683 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 684 | "WITH", "WITHOUT", |
| 685 | }; |
| 686 | int i, lwr, upr, mid, c; |
| 687 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 688 | for(i=0; zName[i]; i++){ |
| 689 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 690 | } |
| 691 | lwr = 0; |
| 692 | upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; |
| 693 | while( lwr<=upr ){ |
| 694 | mid = (lwr+upr)/2; |
| 695 | c = sqlite3_stricmp(azKeywords[mid], zName); |
| 696 | if( c==0 ) return '"'; |
| 697 | if( c<0 ){ |
| 698 | lwr = mid+1; |
| 699 | }else{ |
| 700 | upr = mid-1; |
| 701 | } |
| 702 | } |
| 703 | return 0; |
| 704 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 705 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 706 | /****************************************************************************** |
| 707 | ** SHA3 hash implementation copied from ../ext/misc/shathree.c |
| 708 | */ |
| 709 | typedef sqlite3_uint64 u64; |
| 710 | /* |
| 711 | ** Macros to determine whether the machine is big or little endian, |
| 712 | ** and whether or not that determination is run-time or compile-time. |
| 713 | ** |
| 714 | ** For best performance, an attempt is made to guess at the byte-order |
| 715 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 716 | ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined |
| 717 | ** at run-time. |
| 718 | */ |
| 719 | #ifndef SHA3_BYTEORDER |
| 720 | # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 721 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 722 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 723 | defined(__arm__) |
| 724 | # define SHA3_BYTEORDER 1234 |
| 725 | # elif defined(sparc) || defined(__ppc__) |
| 726 | # define SHA3_BYTEORDER 4321 |
| 727 | # else |
| 728 | # define SHA3_BYTEORDER 0 |
| 729 | # endif |
| 730 | #endif |
| 731 | |
| 732 | |
| 733 | /* |
| 734 | ** State structure for a SHA3 hash in progress |
| 735 | */ |
| 736 | typedef struct SHA3Context SHA3Context; |
| 737 | struct SHA3Context { |
| 738 | union { |
| 739 | u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ |
| 740 | unsigned char x[1600]; /* ... or 1600 bytes */ |
| 741 | } u; |
| 742 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 743 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 744 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 745 | }; |
| 746 | |
drh | 050b124 | 2017-05-04 11:13:50 +0000 | [diff] [blame] | 747 | /* Allow the following routine to use the B0 variable, which is also |
| 748 | ** a macro in the termios.h header file */ |
| 749 | #undef B0 |
| 750 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 751 | /* |
| 752 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 753 | */ |
| 754 | static void KeccakF1600Step(SHA3Context *p){ |
| 755 | int i; |
| 756 | u64 B0, B1, B2, B3, B4; |
| 757 | u64 C0, C1, C2, C3, C4; |
| 758 | u64 D0, D1, D2, D3, D4; |
| 759 | static const u64 RC[] = { |
| 760 | 0x0000000000000001ULL, 0x0000000000008082ULL, |
| 761 | 0x800000000000808aULL, 0x8000000080008000ULL, |
| 762 | 0x000000000000808bULL, 0x0000000080000001ULL, |
| 763 | 0x8000000080008081ULL, 0x8000000000008009ULL, |
| 764 | 0x000000000000008aULL, 0x0000000000000088ULL, |
| 765 | 0x0000000080008009ULL, 0x000000008000000aULL, |
| 766 | 0x000000008000808bULL, 0x800000000000008bULL, |
| 767 | 0x8000000000008089ULL, 0x8000000000008003ULL, |
| 768 | 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 769 | 0x000000000000800aULL, 0x800000008000000aULL, |
| 770 | 0x8000000080008081ULL, 0x8000000000008080ULL, |
| 771 | 0x0000000080000001ULL, 0x8000000080008008ULL |
| 772 | }; |
| 773 | # define A00 (p->u.s[0]) |
| 774 | # define A01 (p->u.s[1]) |
| 775 | # define A02 (p->u.s[2]) |
| 776 | # define A03 (p->u.s[3]) |
| 777 | # define A04 (p->u.s[4]) |
| 778 | # define A10 (p->u.s[5]) |
| 779 | # define A11 (p->u.s[6]) |
| 780 | # define A12 (p->u.s[7]) |
| 781 | # define A13 (p->u.s[8]) |
| 782 | # define A14 (p->u.s[9]) |
| 783 | # define A20 (p->u.s[10]) |
| 784 | # define A21 (p->u.s[11]) |
| 785 | # define A22 (p->u.s[12]) |
| 786 | # define A23 (p->u.s[13]) |
| 787 | # define A24 (p->u.s[14]) |
| 788 | # define A30 (p->u.s[15]) |
| 789 | # define A31 (p->u.s[16]) |
| 790 | # define A32 (p->u.s[17]) |
| 791 | # define A33 (p->u.s[18]) |
| 792 | # define A34 (p->u.s[19]) |
| 793 | # define A40 (p->u.s[20]) |
| 794 | # define A41 (p->u.s[21]) |
| 795 | # define A42 (p->u.s[22]) |
| 796 | # define A43 (p->u.s[23]) |
| 797 | # define A44 (p->u.s[24]) |
| 798 | # define ROL64(a,x) ((a<<x)|(a>>(64-x))) |
| 799 | |
| 800 | for(i=0; i<24; i+=4){ |
| 801 | C0 = A00^A10^A20^A30^A40; |
| 802 | C1 = A01^A11^A21^A31^A41; |
| 803 | C2 = A02^A12^A22^A32^A42; |
| 804 | C3 = A03^A13^A23^A33^A43; |
| 805 | C4 = A04^A14^A24^A34^A44; |
| 806 | D0 = C4^ROL64(C1, 1); |
| 807 | D1 = C0^ROL64(C2, 1); |
| 808 | D2 = C1^ROL64(C3, 1); |
| 809 | D3 = C2^ROL64(C4, 1); |
| 810 | D4 = C3^ROL64(C0, 1); |
| 811 | |
| 812 | B0 = (A00^D0); |
| 813 | B1 = ROL64((A11^D1), 44); |
| 814 | B2 = ROL64((A22^D2), 43); |
| 815 | B3 = ROL64((A33^D3), 21); |
| 816 | B4 = ROL64((A44^D4), 14); |
| 817 | A00 = B0 ^((~B1)& B2 ); |
| 818 | A00 ^= RC[i]; |
| 819 | A11 = B1 ^((~B2)& B3 ); |
| 820 | A22 = B2 ^((~B3)& B4 ); |
| 821 | A33 = B3 ^((~B4)& B0 ); |
| 822 | A44 = B4 ^((~B0)& B1 ); |
| 823 | |
| 824 | B2 = ROL64((A20^D0), 3); |
| 825 | B3 = ROL64((A31^D1), 45); |
| 826 | B4 = ROL64((A42^D2), 61); |
| 827 | B0 = ROL64((A03^D3), 28); |
| 828 | B1 = ROL64((A14^D4), 20); |
| 829 | A20 = B0 ^((~B1)& B2 ); |
| 830 | A31 = B1 ^((~B2)& B3 ); |
| 831 | A42 = B2 ^((~B3)& B4 ); |
| 832 | A03 = B3 ^((~B4)& B0 ); |
| 833 | A14 = B4 ^((~B0)& B1 ); |
| 834 | |
| 835 | B4 = ROL64((A40^D0), 18); |
| 836 | B0 = ROL64((A01^D1), 1); |
| 837 | B1 = ROL64((A12^D2), 6); |
| 838 | B2 = ROL64((A23^D3), 25); |
| 839 | B3 = ROL64((A34^D4), 8); |
| 840 | A40 = B0 ^((~B1)& B2 ); |
| 841 | A01 = B1 ^((~B2)& B3 ); |
| 842 | A12 = B2 ^((~B3)& B4 ); |
| 843 | A23 = B3 ^((~B4)& B0 ); |
| 844 | A34 = B4 ^((~B0)& B1 ); |
| 845 | |
| 846 | B1 = ROL64((A10^D0), 36); |
| 847 | B2 = ROL64((A21^D1), 10); |
| 848 | B3 = ROL64((A32^D2), 15); |
| 849 | B4 = ROL64((A43^D3), 56); |
| 850 | B0 = ROL64((A04^D4), 27); |
| 851 | A10 = B0 ^((~B1)& B2 ); |
| 852 | A21 = B1 ^((~B2)& B3 ); |
| 853 | A32 = B2 ^((~B3)& B4 ); |
| 854 | A43 = B3 ^((~B4)& B0 ); |
| 855 | A04 = B4 ^((~B0)& B1 ); |
| 856 | |
| 857 | B3 = ROL64((A30^D0), 41); |
| 858 | B4 = ROL64((A41^D1), 2); |
| 859 | B0 = ROL64((A02^D2), 62); |
| 860 | B1 = ROL64((A13^D3), 55); |
| 861 | B2 = ROL64((A24^D4), 39); |
| 862 | A30 = B0 ^((~B1)& B2 ); |
| 863 | A41 = B1 ^((~B2)& B3 ); |
| 864 | A02 = B2 ^((~B3)& B4 ); |
| 865 | A13 = B3 ^((~B4)& B0 ); |
| 866 | A24 = B4 ^((~B0)& B1 ); |
| 867 | |
| 868 | C0 = A00^A20^A40^A10^A30; |
| 869 | C1 = A11^A31^A01^A21^A41; |
| 870 | C2 = A22^A42^A12^A32^A02; |
| 871 | C3 = A33^A03^A23^A43^A13; |
| 872 | C4 = A44^A14^A34^A04^A24; |
| 873 | D0 = C4^ROL64(C1, 1); |
| 874 | D1 = C0^ROL64(C2, 1); |
| 875 | D2 = C1^ROL64(C3, 1); |
| 876 | D3 = C2^ROL64(C4, 1); |
| 877 | D4 = C3^ROL64(C0, 1); |
| 878 | |
| 879 | B0 = (A00^D0); |
| 880 | B1 = ROL64((A31^D1), 44); |
| 881 | B2 = ROL64((A12^D2), 43); |
| 882 | B3 = ROL64((A43^D3), 21); |
| 883 | B4 = ROL64((A24^D4), 14); |
| 884 | A00 = B0 ^((~B1)& B2 ); |
| 885 | A00 ^= RC[i+1]; |
| 886 | A31 = B1 ^((~B2)& B3 ); |
| 887 | A12 = B2 ^((~B3)& B4 ); |
| 888 | A43 = B3 ^((~B4)& B0 ); |
| 889 | A24 = B4 ^((~B0)& B1 ); |
| 890 | |
| 891 | B2 = ROL64((A40^D0), 3); |
| 892 | B3 = ROL64((A21^D1), 45); |
| 893 | B4 = ROL64((A02^D2), 61); |
| 894 | B0 = ROL64((A33^D3), 28); |
| 895 | B1 = ROL64((A14^D4), 20); |
| 896 | A40 = B0 ^((~B1)& B2 ); |
| 897 | A21 = B1 ^((~B2)& B3 ); |
| 898 | A02 = B2 ^((~B3)& B4 ); |
| 899 | A33 = B3 ^((~B4)& B0 ); |
| 900 | A14 = B4 ^((~B0)& B1 ); |
| 901 | |
| 902 | B4 = ROL64((A30^D0), 18); |
| 903 | B0 = ROL64((A11^D1), 1); |
| 904 | B1 = ROL64((A42^D2), 6); |
| 905 | B2 = ROL64((A23^D3), 25); |
| 906 | B3 = ROL64((A04^D4), 8); |
| 907 | A30 = B0 ^((~B1)& B2 ); |
| 908 | A11 = B1 ^((~B2)& B3 ); |
| 909 | A42 = B2 ^((~B3)& B4 ); |
| 910 | A23 = B3 ^((~B4)& B0 ); |
| 911 | A04 = B4 ^((~B0)& B1 ); |
| 912 | |
| 913 | B1 = ROL64((A20^D0), 36); |
| 914 | B2 = ROL64((A01^D1), 10); |
| 915 | B3 = ROL64((A32^D2), 15); |
| 916 | B4 = ROL64((A13^D3), 56); |
| 917 | B0 = ROL64((A44^D4), 27); |
| 918 | A20 = B0 ^((~B1)& B2 ); |
| 919 | A01 = B1 ^((~B2)& B3 ); |
| 920 | A32 = B2 ^((~B3)& B4 ); |
| 921 | A13 = B3 ^((~B4)& B0 ); |
| 922 | A44 = B4 ^((~B0)& B1 ); |
| 923 | |
| 924 | B3 = ROL64((A10^D0), 41); |
| 925 | B4 = ROL64((A41^D1), 2); |
| 926 | B0 = ROL64((A22^D2), 62); |
| 927 | B1 = ROL64((A03^D3), 55); |
| 928 | B2 = ROL64((A34^D4), 39); |
| 929 | A10 = B0 ^((~B1)& B2 ); |
| 930 | A41 = B1 ^((~B2)& B3 ); |
| 931 | A22 = B2 ^((~B3)& B4 ); |
| 932 | A03 = B3 ^((~B4)& B0 ); |
| 933 | A34 = B4 ^((~B0)& B1 ); |
| 934 | |
| 935 | C0 = A00^A40^A30^A20^A10; |
| 936 | C1 = A31^A21^A11^A01^A41; |
| 937 | C2 = A12^A02^A42^A32^A22; |
| 938 | C3 = A43^A33^A23^A13^A03; |
| 939 | C4 = A24^A14^A04^A44^A34; |
| 940 | D0 = C4^ROL64(C1, 1); |
| 941 | D1 = C0^ROL64(C2, 1); |
| 942 | D2 = C1^ROL64(C3, 1); |
| 943 | D3 = C2^ROL64(C4, 1); |
| 944 | D4 = C3^ROL64(C0, 1); |
| 945 | |
| 946 | B0 = (A00^D0); |
| 947 | B1 = ROL64((A21^D1), 44); |
| 948 | B2 = ROL64((A42^D2), 43); |
| 949 | B3 = ROL64((A13^D3), 21); |
| 950 | B4 = ROL64((A34^D4), 14); |
| 951 | A00 = B0 ^((~B1)& B2 ); |
| 952 | A00 ^= RC[i+2]; |
| 953 | A21 = B1 ^((~B2)& B3 ); |
| 954 | A42 = B2 ^((~B3)& B4 ); |
| 955 | A13 = B3 ^((~B4)& B0 ); |
| 956 | A34 = B4 ^((~B0)& B1 ); |
| 957 | |
| 958 | B2 = ROL64((A30^D0), 3); |
| 959 | B3 = ROL64((A01^D1), 45); |
| 960 | B4 = ROL64((A22^D2), 61); |
| 961 | B0 = ROL64((A43^D3), 28); |
| 962 | B1 = ROL64((A14^D4), 20); |
| 963 | A30 = B0 ^((~B1)& B2 ); |
| 964 | A01 = B1 ^((~B2)& B3 ); |
| 965 | A22 = B2 ^((~B3)& B4 ); |
| 966 | A43 = B3 ^((~B4)& B0 ); |
| 967 | A14 = B4 ^((~B0)& B1 ); |
| 968 | |
| 969 | B4 = ROL64((A10^D0), 18); |
| 970 | B0 = ROL64((A31^D1), 1); |
| 971 | B1 = ROL64((A02^D2), 6); |
| 972 | B2 = ROL64((A23^D3), 25); |
| 973 | B3 = ROL64((A44^D4), 8); |
| 974 | A10 = B0 ^((~B1)& B2 ); |
| 975 | A31 = B1 ^((~B2)& B3 ); |
| 976 | A02 = B2 ^((~B3)& B4 ); |
| 977 | A23 = B3 ^((~B4)& B0 ); |
| 978 | A44 = B4 ^((~B0)& B1 ); |
| 979 | |
| 980 | B1 = ROL64((A40^D0), 36); |
| 981 | B2 = ROL64((A11^D1), 10); |
| 982 | B3 = ROL64((A32^D2), 15); |
| 983 | B4 = ROL64((A03^D3), 56); |
| 984 | B0 = ROL64((A24^D4), 27); |
| 985 | A40 = B0 ^((~B1)& B2 ); |
| 986 | A11 = B1 ^((~B2)& B3 ); |
| 987 | A32 = B2 ^((~B3)& B4 ); |
| 988 | A03 = B3 ^((~B4)& B0 ); |
| 989 | A24 = B4 ^((~B0)& B1 ); |
| 990 | |
| 991 | B3 = ROL64((A20^D0), 41); |
| 992 | B4 = ROL64((A41^D1), 2); |
| 993 | B0 = ROL64((A12^D2), 62); |
| 994 | B1 = ROL64((A33^D3), 55); |
| 995 | B2 = ROL64((A04^D4), 39); |
| 996 | A20 = B0 ^((~B1)& B2 ); |
| 997 | A41 = B1 ^((~B2)& B3 ); |
| 998 | A12 = B2 ^((~B3)& B4 ); |
| 999 | A33 = B3 ^((~B4)& B0 ); |
| 1000 | A04 = B4 ^((~B0)& B1 ); |
| 1001 | |
| 1002 | C0 = A00^A30^A10^A40^A20; |
| 1003 | C1 = A21^A01^A31^A11^A41; |
| 1004 | C2 = A42^A22^A02^A32^A12; |
| 1005 | C3 = A13^A43^A23^A03^A33; |
| 1006 | C4 = A34^A14^A44^A24^A04; |
| 1007 | D0 = C4^ROL64(C1, 1); |
| 1008 | D1 = C0^ROL64(C2, 1); |
| 1009 | D2 = C1^ROL64(C3, 1); |
| 1010 | D3 = C2^ROL64(C4, 1); |
| 1011 | D4 = C3^ROL64(C0, 1); |
| 1012 | |
| 1013 | B0 = (A00^D0); |
| 1014 | B1 = ROL64((A01^D1), 44); |
| 1015 | B2 = ROL64((A02^D2), 43); |
| 1016 | B3 = ROL64((A03^D3), 21); |
| 1017 | B4 = ROL64((A04^D4), 14); |
| 1018 | A00 = B0 ^((~B1)& B2 ); |
| 1019 | A00 ^= RC[i+3]; |
| 1020 | A01 = B1 ^((~B2)& B3 ); |
| 1021 | A02 = B2 ^((~B3)& B4 ); |
| 1022 | A03 = B3 ^((~B4)& B0 ); |
| 1023 | A04 = B4 ^((~B0)& B1 ); |
| 1024 | |
| 1025 | B2 = ROL64((A10^D0), 3); |
| 1026 | B3 = ROL64((A11^D1), 45); |
| 1027 | B4 = ROL64((A12^D2), 61); |
| 1028 | B0 = ROL64((A13^D3), 28); |
| 1029 | B1 = ROL64((A14^D4), 20); |
| 1030 | A10 = B0 ^((~B1)& B2 ); |
| 1031 | A11 = B1 ^((~B2)& B3 ); |
| 1032 | A12 = B2 ^((~B3)& B4 ); |
| 1033 | A13 = B3 ^((~B4)& B0 ); |
| 1034 | A14 = B4 ^((~B0)& B1 ); |
| 1035 | |
| 1036 | B4 = ROL64((A20^D0), 18); |
| 1037 | B0 = ROL64((A21^D1), 1); |
| 1038 | B1 = ROL64((A22^D2), 6); |
| 1039 | B2 = ROL64((A23^D3), 25); |
| 1040 | B3 = ROL64((A24^D4), 8); |
| 1041 | A20 = B0 ^((~B1)& B2 ); |
| 1042 | A21 = B1 ^((~B2)& B3 ); |
| 1043 | A22 = B2 ^((~B3)& B4 ); |
| 1044 | A23 = B3 ^((~B4)& B0 ); |
| 1045 | A24 = B4 ^((~B0)& B1 ); |
| 1046 | |
| 1047 | B1 = ROL64((A30^D0), 36); |
| 1048 | B2 = ROL64((A31^D1), 10); |
| 1049 | B3 = ROL64((A32^D2), 15); |
| 1050 | B4 = ROL64((A33^D3), 56); |
| 1051 | B0 = ROL64((A34^D4), 27); |
| 1052 | A30 = B0 ^((~B1)& B2 ); |
| 1053 | A31 = B1 ^((~B2)& B3 ); |
| 1054 | A32 = B2 ^((~B3)& B4 ); |
| 1055 | A33 = B3 ^((~B4)& B0 ); |
| 1056 | A34 = B4 ^((~B0)& B1 ); |
| 1057 | |
| 1058 | B3 = ROL64((A40^D0), 41); |
| 1059 | B4 = ROL64((A41^D1), 2); |
| 1060 | B0 = ROL64((A42^D2), 62); |
| 1061 | B1 = ROL64((A43^D3), 55); |
| 1062 | B2 = ROL64((A44^D4), 39); |
| 1063 | A40 = B0 ^((~B1)& B2 ); |
| 1064 | A41 = B1 ^((~B2)& B3 ); |
| 1065 | A42 = B2 ^((~B3)& B4 ); |
| 1066 | A43 = B3 ^((~B4)& B0 ); |
| 1067 | A44 = B4 ^((~B0)& B1 ); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | ** Initialize a new hash. iSize determines the size of the hash |
| 1073 | ** in bits and should be one of 224, 256, 384, or 512. Or iSize |
| 1074 | ** can be zero to use the default hash size of 256 bits. |
| 1075 | */ |
| 1076 | static void SHA3Init(SHA3Context *p, int iSize){ |
| 1077 | memset(p, 0, sizeof(*p)); |
| 1078 | if( iSize>=128 && iSize<=512 ){ |
| 1079 | p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; |
| 1080 | }else{ |
| 1081 | p->nRate = (1600 - 2*256)/8; |
| 1082 | } |
| 1083 | #if SHA3_BYTEORDER==1234 |
| 1084 | /* Known to be little-endian at compile-time. No-op */ |
| 1085 | #elif SHA3_BYTEORDER==4321 |
| 1086 | p->ixMask = 7; /* Big-endian */ |
| 1087 | #else |
| 1088 | { |
| 1089 | static unsigned int one = 1; |
| 1090 | if( 1==*(unsigned char*)&one ){ |
| 1091 | /* Little endian. No byte swapping. */ |
| 1092 | p->ixMask = 0; |
| 1093 | }else{ |
| 1094 | /* Big endian. Byte swap. */ |
| 1095 | p->ixMask = 7; |
| 1096 | } |
| 1097 | } |
| 1098 | #endif |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | ** Make consecutive calls to the SHA3Update function to add new content |
| 1103 | ** to the hash |
| 1104 | */ |
| 1105 | static void SHA3Update( |
| 1106 | SHA3Context *p, |
| 1107 | const unsigned char *aData, |
| 1108 | unsigned int nData |
| 1109 | ){ |
| 1110 | unsigned int i = 0; |
| 1111 | #if SHA3_BYTEORDER==1234 |
| 1112 | if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ |
| 1113 | for(; i+7<nData; i+=8){ |
| 1114 | p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; |
| 1115 | p->nLoaded += 8; |
| 1116 | if( p->nLoaded>=p->nRate ){ |
| 1117 | KeccakF1600Step(p); |
| 1118 | p->nLoaded = 0; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | #endif |
| 1123 | for(; i<nData; i++){ |
| 1124 | #if SHA3_BYTEORDER==1234 |
| 1125 | p->u.x[p->nLoaded] ^= aData[i]; |
| 1126 | #elif SHA3_BYTEORDER==4321 |
| 1127 | p->u.x[p->nLoaded^0x07] ^= aData[i]; |
| 1128 | #else |
| 1129 | p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; |
| 1130 | #endif |
| 1131 | p->nLoaded++; |
| 1132 | if( p->nLoaded==p->nRate ){ |
| 1133 | KeccakF1600Step(p); |
| 1134 | p->nLoaded = 0; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | ** After all content has been added, invoke SHA3Final() to compute |
| 1141 | ** the final hash. The function returns a pointer to the binary |
| 1142 | ** hash value. |
| 1143 | */ |
| 1144 | static unsigned char *SHA3Final(SHA3Context *p){ |
| 1145 | unsigned int i; |
| 1146 | if( p->nLoaded==p->nRate-1 ){ |
| 1147 | const unsigned char c1 = 0x86; |
| 1148 | SHA3Update(p, &c1, 1); |
| 1149 | }else{ |
| 1150 | const unsigned char c2 = 0x06; |
| 1151 | const unsigned char c3 = 0x80; |
| 1152 | SHA3Update(p, &c2, 1); |
| 1153 | p->nLoaded = p->nRate - 1; |
| 1154 | SHA3Update(p, &c3, 1); |
| 1155 | } |
| 1156 | for(i=0; i<p->nRate; i++){ |
| 1157 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1158 | } |
| 1159 | return &p->u.x[p->nRate]; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | ** Implementation of the sha3(X,SIZE) function. |
| 1164 | ** |
| 1165 | ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default |
| 1166 | ** size is 256. If X is a BLOB, it is hashed as is. |
| 1167 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1168 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1169 | ** of a NULL value is NULL. |
| 1170 | */ |
| 1171 | static void sha3Func( |
| 1172 | sqlite3_context *context, |
| 1173 | int argc, |
| 1174 | sqlite3_value **argv |
| 1175 | ){ |
| 1176 | SHA3Context cx; |
| 1177 | int eType = sqlite3_value_type(argv[0]); |
| 1178 | int nByte = sqlite3_value_bytes(argv[0]); |
| 1179 | int iSize; |
| 1180 | if( argc==1 ){ |
| 1181 | iSize = 256; |
| 1182 | }else{ |
| 1183 | iSize = sqlite3_value_int(argv[1]); |
| 1184 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1185 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1186 | "384 512", -1); |
| 1187 | return; |
| 1188 | } |
| 1189 | } |
| 1190 | if( eType==SQLITE_NULL ) return; |
| 1191 | SHA3Init(&cx, iSize); |
| 1192 | if( eType==SQLITE_BLOB ){ |
| 1193 | SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 1194 | }else{ |
| 1195 | SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); |
| 1196 | } |
| 1197 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1198 | } |
| 1199 | |
| 1200 | /* Compute a string using sqlite3_vsnprintf() with a maximum length |
| 1201 | ** of 50 bytes and add it to the hash. |
| 1202 | */ |
| 1203 | static void hash_step_vformat( |
| 1204 | SHA3Context *p, /* Add content to this context */ |
| 1205 | const char *zFormat, |
| 1206 | ... |
| 1207 | ){ |
| 1208 | va_list ap; |
| 1209 | int n; |
| 1210 | char zBuf[50]; |
| 1211 | va_start(ap, zFormat); |
| 1212 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 1213 | va_end(ap); |
| 1214 | n = (int)strlen(zBuf); |
| 1215 | SHA3Update(p, (unsigned char*)zBuf, n); |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | ** Implementation of the sha3_query(SQL,SIZE) function. |
| 1220 | ** |
| 1221 | ** This function compiles and runs the SQL statement(s) given in the |
| 1222 | ** argument. The results are hashed using a SIZE-bit SHA3. The default |
| 1223 | ** size is 256. |
| 1224 | ** |
| 1225 | ** The format of the byte stream that is hashed is summarized as follows: |
| 1226 | ** |
| 1227 | ** S<n>:<sql> |
| 1228 | ** R |
| 1229 | ** N |
| 1230 | ** I<int> |
| 1231 | ** F<ieee-float> |
| 1232 | ** B<size>:<bytes> |
| 1233 | ** T<size>:<text> |
| 1234 | ** |
| 1235 | ** <sql> is the original SQL text for each statement run and <n> is |
| 1236 | ** the size of that text. The SQL text is UTF-8. A single R character |
| 1237 | ** occurs before the start of each row. N means a NULL value. |
| 1238 | ** I mean an 8-byte little-endian integer <int>. F is a floating point |
| 1239 | ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. |
| 1240 | ** B means blobs of <size> bytes. T means text rendered as <size> |
| 1241 | ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII |
| 1242 | ** text integers. |
| 1243 | ** |
| 1244 | ** For each SQL statement in the X input, there is one S segment. Each |
| 1245 | ** S segment is followed by zero or more R segments, one for each row in the |
| 1246 | ** result set. After each R, there are one or more N, I, F, B, or T segments, |
| 1247 | ** one for each column in the result set. Segments are concatentated directly |
| 1248 | ** with no delimiters of any kind. |
| 1249 | */ |
| 1250 | static void sha3QueryFunc( |
| 1251 | sqlite3_context *context, |
| 1252 | int argc, |
| 1253 | sqlite3_value **argv |
| 1254 | ){ |
| 1255 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1256 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1257 | sqlite3_stmt *pStmt = 0; |
| 1258 | int nCol; /* Number of columns in the result set */ |
| 1259 | int i; /* Loop counter */ |
| 1260 | int rc; |
| 1261 | int n; |
| 1262 | const char *z; |
| 1263 | SHA3Context cx; |
| 1264 | int iSize; |
| 1265 | |
| 1266 | if( argc==1 ){ |
| 1267 | iSize = 256; |
| 1268 | }else{ |
| 1269 | iSize = sqlite3_value_int(argv[1]); |
| 1270 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1271 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1272 | "384 512", -1); |
| 1273 | return; |
| 1274 | } |
| 1275 | } |
| 1276 | if( zSql==0 ) return; |
| 1277 | SHA3Init(&cx, iSize); |
| 1278 | while( zSql[0] ){ |
| 1279 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 1280 | if( rc ){ |
| 1281 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 1282 | zSql, sqlite3_errmsg(db)); |
| 1283 | sqlite3_finalize(pStmt); |
| 1284 | sqlite3_result_error(context, zMsg, -1); |
| 1285 | sqlite3_free(zMsg); |
| 1286 | return; |
| 1287 | } |
| 1288 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 1289 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 1290 | sqlite3_finalize(pStmt); |
| 1291 | sqlite3_result_error(context, zMsg, -1); |
| 1292 | sqlite3_free(zMsg); |
| 1293 | return; |
| 1294 | } |
| 1295 | nCol = sqlite3_column_count(pStmt); |
| 1296 | z = sqlite3_sql(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 1297 | if( z==0 ){ |
| 1298 | sqlite3_finalize(pStmt); |
| 1299 | continue; |
| 1300 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1301 | n = (int)strlen(z); |
| 1302 | hash_step_vformat(&cx,"S%d:",n); |
| 1303 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1304 | |
| 1305 | /* Compute a hash over the result of the query */ |
| 1306 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1307 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 1308 | for(i=0; i<nCol; i++){ |
| 1309 | switch( sqlite3_column_type(pStmt,i) ){ |
| 1310 | case SQLITE_NULL: { |
| 1311 | SHA3Update(&cx, (const unsigned char*)"N",1); |
| 1312 | break; |
| 1313 | } |
| 1314 | case SQLITE_INTEGER: { |
| 1315 | sqlite3_uint64 u; |
| 1316 | int j; |
| 1317 | unsigned char x[9]; |
| 1318 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 1319 | memcpy(&u, &v, 8); |
| 1320 | for(j=8; j>=1; j--){ |
| 1321 | x[j] = u & 0xff; |
| 1322 | u >>= 8; |
| 1323 | } |
| 1324 | x[0] = 'I'; |
| 1325 | SHA3Update(&cx, x, 9); |
| 1326 | break; |
| 1327 | } |
| 1328 | case SQLITE_FLOAT: { |
| 1329 | sqlite3_uint64 u; |
| 1330 | int j; |
| 1331 | unsigned char x[9]; |
| 1332 | double r = sqlite3_column_double(pStmt,i); |
| 1333 | memcpy(&u, &r, 8); |
| 1334 | for(j=8; j>=1; j--){ |
| 1335 | x[j] = u & 0xff; |
| 1336 | u >>= 8; |
| 1337 | } |
| 1338 | x[0] = 'F'; |
| 1339 | SHA3Update(&cx,x,9); |
| 1340 | break; |
| 1341 | } |
| 1342 | case SQLITE_TEXT: { |
| 1343 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1344 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 1345 | hash_step_vformat(&cx,"T%d:",n2); |
| 1346 | SHA3Update(&cx, z2, n2); |
| 1347 | break; |
| 1348 | } |
| 1349 | case SQLITE_BLOB: { |
| 1350 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1351 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 1352 | hash_step_vformat(&cx,"B%d:",n2); |
| 1353 | SHA3Update(&cx, z2, n2); |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | sqlite3_finalize(pStmt); |
| 1360 | } |
| 1361 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1362 | } |
| 1363 | /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c |
| 1364 | ********************************************************************************/ |
| 1365 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1366 | #if defined(SQLITE_ENABLE_SESSION) |
| 1367 | /* |
| 1368 | ** State information for a single open session |
| 1369 | */ |
| 1370 | typedef struct OpenSession OpenSession; |
| 1371 | struct OpenSession { |
| 1372 | char *zName; /* Symbolic name for this session */ |
| 1373 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 1374 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 1375 | sqlite3_session *p; /* The open session */ |
| 1376 | }; |
| 1377 | #endif |
| 1378 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1379 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1380 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1381 | ** saved so that it can be restored by ".explain off" |
| 1382 | */ |
| 1383 | typedef struct SavedModeInfo SavedModeInfo; |
| 1384 | struct SavedModeInfo { |
| 1385 | int valid; /* Is there legit data in here? */ |
| 1386 | int mode; /* Mode prior to ".explain on" */ |
| 1387 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 1388 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1389 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1390 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1391 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1392 | ** State information about the database connection is contained in an |
| 1393 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1394 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1395 | typedef struct ShellState ShellState; |
| 1396 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1397 | sqlite3 *db; /* The database */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1398 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1399 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 1400 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 1401 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 1402 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1403 | int cnt; /* Number of records displayed so far */ |
| 1404 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 1405 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 1406 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1407 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1408 | int cMode; /* temporary output mode for the current query */ |
| 1409 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1410 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1411 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1412 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1413 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1414 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 1415 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1416 | char colSeparator[20]; /* Column separator character for several modes */ |
| 1417 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1418 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1419 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1420 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1421 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1422 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1423 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 1424 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1425 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1426 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1427 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1428 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 1429 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1430 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 1431 | #if defined(SQLITE_ENABLE_SESSION) |
| 1432 | int nSession; /* Number of active sessions */ |
| 1433 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 1434 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1435 | }; |
| 1436 | |
| 1437 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1438 | ** These are the allowed shellFlgs values |
| 1439 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 1440 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 1441 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 1442 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 1443 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 1444 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
| 1445 | #define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1446 | #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
| 1447 | |
| 1448 | /* |
| 1449 | ** Macros for testing and setting shellFlgs |
| 1450 | */ |
| 1451 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 1452 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 1453 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 1454 | |
| 1455 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1456 | ** These are the allowed modes. |
| 1457 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1458 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1459 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1460 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1461 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1462 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1463 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1464 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 1465 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 1466 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 1467 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 1468 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 1469 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1470 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1471 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1472 | "line", |
| 1473 | "column", |
| 1474 | "list", |
| 1475 | "semi", |
| 1476 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1477 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 1478 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1479 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1480 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1481 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1482 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1483 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1484 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1487 | ** These are the column/row/line separators used by the various |
| 1488 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1489 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 1490 | #define SEP_Column "|" |
| 1491 | #define SEP_Row "\n" |
| 1492 | #define SEP_Tab "\t" |
| 1493 | #define SEP_Space " " |
| 1494 | #define SEP_Comma "," |
| 1495 | #define SEP_CrLf "\r\n" |
| 1496 | #define SEP_Unit "\x1F" |
| 1497 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1500 | ** Number of elements in an array |
| 1501 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1502 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1503 | |
| 1504 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1505 | ** A callback for the sqlite3_log() interface. |
| 1506 | */ |
| 1507 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1508 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1509 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1510 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 1511 | fflush(p->pLog); |
| 1512 | } |
| 1513 | |
| 1514 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1515 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 1516 | */ |
| 1517 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 1518 | int i; |
| 1519 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1520 | raw_printf(out,"X'"); |
| 1521 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 1522 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1526 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 1527 | ** to that string. |
| 1528 | ** |
| 1529 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 1530 | ** then make up some string and store it in the buffer zBuf. |
| 1531 | */ |
| 1532 | static const char *unused_string( |
| 1533 | const char *z, /* Result must not appear anywhere in z */ |
| 1534 | const char *zA, const char *zB, /* Try these first */ |
| 1535 | char *zBuf /* Space to store a generated string */ |
| 1536 | ){ |
| 1537 | unsigned i = 0; |
| 1538 | if( strstr(z, zA)==0 ) return zA; |
| 1539 | if( strstr(z, zB)==0 ) return zB; |
| 1540 | do{ |
| 1541 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 1542 | }while( strstr(z,zBuf)!=0 ); |
| 1543 | return zBuf; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1547 | ** Output the given string as a quoted string using SQL quoting conventions. |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1548 | ** |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1549 | ** See also: output_quoted_escaped_string() |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1550 | */ |
| 1551 | static void output_quoted_string(FILE *out, const char *z){ |
| 1552 | int i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1553 | char c; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1554 | setBinaryMode(out, 1); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 1555 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1556 | if( c==0 ){ |
| 1557 | utf8_printf(out,"'%s'",z); |
| 1558 | }else{ |
| 1559 | raw_printf(out, "'"); |
| 1560 | while( *z ){ |
| 1561 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 1562 | if( c=='\'' ) i++; |
| 1563 | if( i ){ |
| 1564 | utf8_printf(out, "%.*s", i, z); |
| 1565 | z += i; |
| 1566 | } |
| 1567 | if( c=='\'' ){ |
| 1568 | raw_printf(out, "'"); |
| 1569 | continue; |
| 1570 | } |
| 1571 | if( c==0 ){ |
| 1572 | break; |
| 1573 | } |
| 1574 | z++; |
| 1575 | } |
| 1576 | raw_printf(out, "'"); |
| 1577 | } |
| 1578 | setTextMode(out, 1); |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1583 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 1584 | ** get corrupted by end-of-line translation facilities in some operating |
| 1585 | ** systems. |
| 1586 | ** |
| 1587 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 1588 | ** escape mechanism. |
| 1589 | */ |
| 1590 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 1591 | int i; |
| 1592 | char c; |
| 1593 | setBinaryMode(out, 1); |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1594 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 1595 | if( c==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1596 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1597 | }else{ |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1598 | const char *zNL = 0; |
| 1599 | const char *zCR = 0; |
| 1600 | int nNL = 0; |
| 1601 | int nCR = 0; |
| 1602 | char zBuf1[20], zBuf2[20]; |
| 1603 | for(i=0; z[i]; i++){ |
| 1604 | if( z[i]=='\n' ) nNL++; |
| 1605 | if( z[i]=='\r' ) nCR++; |
| 1606 | } |
| 1607 | if( nNL ){ |
| 1608 | raw_printf(out, "replace("); |
| 1609 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 1610 | } |
| 1611 | if( nCR ){ |
| 1612 | raw_printf(out, "replace("); |
| 1613 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 1614 | } |
| 1615 | raw_printf(out, "'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1616 | while( *z ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1617 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 1618 | if( c=='\'' ) i++; |
| 1619 | if( i ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1620 | utf8_printf(out, "%.*s", i, z); |
| 1621 | z += i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1622 | } |
| 1623 | if( c=='\'' ){ |
| 1624 | raw_printf(out, "'"); |
| 1625 | continue; |
| 1626 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1627 | if( c==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1630 | z++; |
| 1631 | if( c=='\n' ){ |
| 1632 | raw_printf(out, "%s", zNL); |
| 1633 | continue; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 1634 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1635 | raw_printf(out, "%s", zCR); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1636 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 1637 | raw_printf(out, "'"); |
| 1638 | if( nCR ){ |
| 1639 | raw_printf(out, ",'%s',char(13))", zCR); |
| 1640 | } |
| 1641 | if( nNL ){ |
| 1642 | raw_printf(out, ",'%s',char(10))", zNL); |
| 1643 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1644 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1645 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1649 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1650 | */ |
| 1651 | static void output_c_string(FILE *out, const char *z){ |
| 1652 | unsigned int c; |
| 1653 | fputc('"', out); |
| 1654 | while( (c = *(z++))!=0 ){ |
| 1655 | if( c=='\\' ){ |
| 1656 | fputc(c, out); |
| 1657 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 1658 | }else if( c=='"' ){ |
| 1659 | fputc('\\', out); |
| 1660 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1661 | }else if( c=='\t' ){ |
| 1662 | fputc('\\', out); |
| 1663 | fputc('t', out); |
| 1664 | }else if( c=='\n' ){ |
| 1665 | fputc('\\', out); |
| 1666 | fputc('n', out); |
| 1667 | }else if( c=='\r' ){ |
| 1668 | fputc('\\', out); |
| 1669 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 1670 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1671 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1672 | }else{ |
| 1673 | fputc(c, out); |
| 1674 | } |
| 1675 | } |
| 1676 | fputc('"', out); |
| 1677 | } |
| 1678 | |
| 1679 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1680 | ** Output the given string with characters that are special to |
| 1681 | ** HTML escaped. |
| 1682 | */ |
| 1683 | static void output_html_string(FILE *out, const char *z){ |
| 1684 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 1685 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1686 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1687 | for(i=0; z[i] |
| 1688 | && z[i]!='<' |
| 1689 | && z[i]!='&' |
| 1690 | && z[i]!='>' |
| 1691 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1692 | && z[i]!='\''; |
| 1693 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1694 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1695 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1696 | } |
| 1697 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1698 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1699 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1700 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1701 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1702 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1703 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1704 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 1705 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1706 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1707 | }else{ |
| 1708 | break; |
| 1709 | } |
| 1710 | z += i + 1; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1715 | ** If a field contains any character identified by a 1 in the following |
| 1716 | ** array, then the string must be quoted for CSV. |
| 1717 | */ |
| 1718 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1719 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1720 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1721 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1722 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1723 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1724 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1725 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1726 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1727 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1728 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1729 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1730 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1731 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1732 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1733 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1734 | 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] | 1735 | }; |
| 1736 | |
| 1737 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 1738 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1739 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 1740 | ** the null value. Strings are quoted if necessary. The separator |
| 1741 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1742 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1743 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1744 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1745 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1746 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1747 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1748 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1749 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1750 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 1751 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1752 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 1753 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1754 | i = 0; |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | if( i==0 ){ |
| 1759 | putc('"', out); |
| 1760 | for(i=0; z[i]; i++){ |
| 1761 | if( z[i]=='"' ) putc('"', out); |
| 1762 | putc(z[i], out); |
| 1763 | } |
| 1764 | putc('"', out); |
| 1765 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1766 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1767 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1768 | } |
| 1769 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1770 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1774 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1775 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1776 | ** This routine runs when the user presses Ctrl-C |
| 1777 | */ |
| 1778 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1779 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 1780 | seenInterrupt++; |
| 1781 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 1782 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1783 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1784 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1785 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1786 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1787 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1788 | ** When the ".auth ON" is set, the following authorizer callback is |
| 1789 | ** invoked. It always returns SQLITE_OK. |
| 1790 | */ |
| 1791 | static int shellAuth( |
| 1792 | void *pClientData, |
| 1793 | int op, |
| 1794 | const char *zA1, |
| 1795 | const char *zA2, |
| 1796 | const char *zA3, |
| 1797 | const char *zA4 |
| 1798 | ){ |
| 1799 | ShellState *p = (ShellState*)pClientData; |
| 1800 | static const char *azAction[] = { 0, |
| 1801 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 1802 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 1803 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 1804 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 1805 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 1806 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 1807 | "PRAGMA", "READ", "SELECT", |
| 1808 | "TRANSACTION", "UPDATE", "ATTACH", |
| 1809 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 1810 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 1811 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 1812 | }; |
| 1813 | int i; |
| 1814 | const char *az[4]; |
| 1815 | az[0] = zA1; |
| 1816 | az[1] = zA2; |
| 1817 | az[2] = zA3; |
| 1818 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1819 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1820 | for(i=0; i<4; i++){ |
| 1821 | raw_printf(p->out, " "); |
| 1822 | if( az[i] ){ |
| 1823 | output_c_string(p->out, az[i]); |
| 1824 | }else{ |
| 1825 | raw_printf(p->out, "NULL"); |
| 1826 | } |
| 1827 | } |
| 1828 | raw_printf(p->out, "\n"); |
| 1829 | return SQLITE_OK; |
| 1830 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 1831 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 1832 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1833 | /* |
| 1834 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 1835 | ** |
| 1836 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 1837 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 1838 | */ |
| 1839 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 1840 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 1841 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 1842 | }else{ |
| 1843 | utf8_printf(out, "%s%s", z, zTail); |
| 1844 | } |
| 1845 | } |
| 1846 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 1847 | char c = z[n]; |
| 1848 | z[n] = 0; |
| 1849 | printSchemaLine(out, z, zTail); |
| 1850 | z[n] = c; |
| 1851 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 1852 | |
| 1853 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 1854 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1855 | ** invokes for each row of a query result. |
| 1856 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 1857 | static int shell_callback( |
| 1858 | void *pArg, |
| 1859 | int nArg, /* Number of result columns */ |
| 1860 | char **azArg, /* Text of each result column */ |
| 1861 | char **azCol, /* Column names */ |
| 1862 | int *aiType /* Column types */ |
| 1863 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1864 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 1865 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 1866 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1867 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1868 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1869 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1870 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1871 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1872 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1873 | if( len>w ) w = len; |
| 1874 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1875 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1876 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1877 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1878 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1879 | } |
| 1880 | break; |
| 1881 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1882 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1883 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1884 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 1885 | const int *colWidth; |
| 1886 | int showHdr; |
| 1887 | char *rowSep; |
| 1888 | if( p->cMode==MODE_Column ){ |
| 1889 | colWidth = p->colWidth; |
| 1890 | showHdr = p->showHeader; |
| 1891 | rowSep = p->rowSeparator; |
| 1892 | }else{ |
| 1893 | colWidth = aExplainWidths; |
| 1894 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 1895 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1896 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1897 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1898 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1899 | int w, n; |
| 1900 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1901 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1902 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1903 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1904 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1905 | if( w==0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1906 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1907 | if( w<10 ) w = 10; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 1908 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1909 | if( w<n ) w = n; |
| 1910 | } |
| 1911 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1912 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1913 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1914 | if( showHdr ){ |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 1915 | utf8_width_print(p->out, w, azCol[i]); |
| 1916 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1919 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1920 | for(i=0; i<nArg; i++){ |
| 1921 | int w; |
| 1922 | if( i<ArraySize(p->actualWidth) ){ |
| 1923 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 1924 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1925 | }else{ |
| 1926 | w = 10; |
| 1927 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1928 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 1929 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1930 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1931 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1932 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1935 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1936 | for(i=0; i<nArg; i++){ |
| 1937 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1938 | if( i<ArraySize(p->actualWidth) ){ |
| 1939 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1940 | }else{ |
| 1941 | w = 10; |
| 1942 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 1943 | if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1944 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1945 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1946 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1947 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 1948 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1949 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 1950 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 1951 | } |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 1952 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 1953 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1954 | } |
| 1955 | break; |
| 1956 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1957 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 1958 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
| 1961 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 1962 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1963 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1964 | int nParen = 0; |
| 1965 | char cEnd = 0; |
| 1966 | char c; |
| 1967 | int nLine = 0; |
| 1968 | assert( nArg==1 ); |
| 1969 | if( azArg[0]==0 ) break; |
| 1970 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 1971 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 1972 | ){ |
| 1973 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 1974 | break; |
| 1975 | } |
| 1976 | z = sqlite3_mprintf("%s", azArg[0]); |
| 1977 | j = 0; |
| 1978 | for(i=0; IsSpace(z[i]); i++){} |
| 1979 | for(; (c = z[i])!=0; i++){ |
| 1980 | if( IsSpace(c) ){ |
| 1981 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 1982 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 1983 | j--; |
| 1984 | } |
| 1985 | z[j++] = c; |
| 1986 | } |
| 1987 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 1988 | z[j] = 0; |
| 1989 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 1990 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 1991 | if( c==cEnd ){ |
| 1992 | cEnd = 0; |
| 1993 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 1994 | cEnd = c; |
| 1995 | }else if( c=='[' ){ |
| 1996 | cEnd = ']'; |
| 1997 | }else if( c=='(' ){ |
| 1998 | nParen++; |
| 1999 | }else if( c==')' ){ |
| 2000 | nParen--; |
| 2001 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2002 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2003 | j = 0; |
| 2004 | } |
| 2005 | } |
| 2006 | z[j++] = c; |
| 2007 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 2008 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2009 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2010 | j = 0; |
| 2011 | nLine++; |
| 2012 | while( IsSpace(z[i+1]) ){ i++; } |
| 2013 | } |
| 2014 | } |
| 2015 | z[j] = 0; |
| 2016 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2017 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2018 | sqlite3_free(z); |
| 2019 | break; |
| 2020 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2021 | case MODE_List: { |
| 2022 | if( p->cnt++==0 && p->showHeader ){ |
| 2023 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2024 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2025 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2026 | } |
| 2027 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2028 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2029 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2030 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2031 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2032 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2033 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2034 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2035 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2036 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2037 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2038 | } |
| 2039 | break; |
| 2040 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2041 | case MODE_Html: { |
| 2042 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2043 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2044 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2045 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2046 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2047 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2048 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2049 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2050 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2051 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2052 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2053 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2054 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2055 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2056 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2057 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2058 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2059 | break; |
| 2060 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2061 | case MODE_Tcl: { |
| 2062 | if( p->cnt++==0 && p->showHeader ){ |
| 2063 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2064 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2065 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2066 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2067 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2068 | } |
| 2069 | if( azArg==0 ) break; |
| 2070 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2071 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2072 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2073 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2074 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2077 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2078 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2079 | if( p->cnt++==0 && p->showHeader ){ |
| 2080 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2081 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2082 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2083 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2084 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 2085 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 2086 | for(i=0; i<nArg; i++){ |
| 2087 | output_csv(p, azArg[i], i<nArg-1); |
| 2088 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2089 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2090 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2091 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2092 | break; |
| 2093 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2094 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2095 | if( azArg==0 ) break; |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2096 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2097 | if( p->showHeader ){ |
| 2098 | raw_printf(p->out,"("); |
| 2099 | for(i=0; i<nArg; i++){ |
| 2100 | if( i>0 ) raw_printf(p->out, ","); |
| 2101 | if( quoteChar(azCol[i]) ){ |
| 2102 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 2103 | utf8_printf(p->out, "%s", z); |
| 2104 | sqlite3_free(z); |
| 2105 | }else{ |
| 2106 | raw_printf(p->out, "%s", azCol[i]); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2107 | } |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2108 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2109 | raw_printf(p->out,")"); |
| 2110 | } |
| 2111 | p->cnt++; |
| 2112 | for(i=0; i<nArg; i++){ |
| 2113 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2114 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2115 | utf8_printf(p->out,"NULL"); |
| 2116 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2117 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2118 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2119 | utf8_printf(p->out,"%s", azArg[i]); |
| 2120 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2121 | char z[50]; |
| 2122 | double r = sqlite3_column_double(p->pStmt, i); |
| 2123 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2124 | raw_printf(p->out, "%s", z); |
| 2125 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2126 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2127 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2128 | output_hex_blob(p->out, pBlob, nBlob); |
| 2129 | }else if( isNumber(azArg[i], 0) ){ |
| 2130 | utf8_printf(p->out,"%s", azArg[i]); |
| 2131 | }else{ |
| 2132 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2133 | } |
| 2134 | } |
| 2135 | raw_printf(p->out,");\n"); |
| 2136 | break; |
| 2137 | } |
| 2138 | case MODE_Quote: { |
| 2139 | if( azArg==0 ) break; |
| 2140 | if( p->cnt==0 && p->showHeader ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2141 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2142 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2143 | output_quoted_string(p->out, azCol[i]); |
| 2144 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2145 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2146 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2147 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2148 | for(i=0; i<nArg; i++){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2149 | if( i>0 ) raw_printf(p->out, ","); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2150 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2151 | utf8_printf(p->out,"NULL"); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2152 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2153 | output_quoted_string(p->out, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2154 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2155 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2156 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2157 | char z[50]; |
| 2158 | double r = sqlite3_column_double(p->pStmt, i); |
| 2159 | sqlite3_snprintf(50,z,"%!.20g", r); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2160 | raw_printf(p->out, "%s", z); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2161 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2162 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2163 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2164 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2165 | }else if( isNumber(azArg[i], 0) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2166 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2167 | }else{ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2168 | output_quoted_string(p->out, azArg[i]); |
| 2169 | } |
| 2170 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2171 | raw_printf(p->out,"\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2172 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2173 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2174 | case MODE_Ascii: { |
| 2175 | if( p->cnt++==0 && p->showHeader ){ |
| 2176 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2177 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2178 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2179 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2180 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2181 | } |
| 2182 | if( azArg==0 ) break; |
| 2183 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2184 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2185 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2186 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2187 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2188 | break; |
| 2189 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2190 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2195 | ** This is the callback routine that the SQLite library |
| 2196 | ** invokes for each row of a query result. |
| 2197 | */ |
| 2198 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2199 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 2200 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 2201 | } |
| 2202 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2203 | /* |
| 2204 | ** This is the callback routine from sqlite3_exec() that appends all |
| 2205 | ** output onto the end of a ShellText object. |
| 2206 | */ |
| 2207 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 2208 | ShellText *p = (ShellText*)pArg; |
| 2209 | int i; |
drh | 2fb79e9 | 2017-03-25 12:08:11 +0000 | [diff] [blame] | 2210 | UNUSED_PARAMETER(az); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2211 | if( p->n ) appendText(p, "|", 0); |
| 2212 | for(i=0; i<nArg; i++){ |
| 2213 | if( i ) appendText(p, ",", 0); |
| 2214 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 2215 | } |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | /* |
| 2220 | ** Generate an appropriate SELFTEST table in the main database. |
| 2221 | */ |
| 2222 | static void createSelftestTable(ShellState *p){ |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2223 | char *zErrMsg = 0; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2224 | sqlite3_exec(p->db, |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2225 | "SAVEPOINT selftest_init;\n" |
| 2226 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2227 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 2228 | " op TEXT,\n" /* Operator: memo run */ |
| 2229 | " cmd TEXT,\n" /* Command text */ |
| 2230 | " ans TEXT\n" /* Desired answer */ |
| 2231 | ");" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2232 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 2233 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 2234 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 2235 | " 'memo','Tests generated by --init');\n" |
| 2236 | "INSERT INTO [_shell$self]\n" |
| 2237 | " SELECT 'run',\n" |
| 2238 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 2239 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 2240 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 2241 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 2242 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2243 | " SELECT 'run'," |
| 2244 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 2245 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 2246 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 2247 | " FROM (\n" |
| 2248 | " SELECT name FROM sqlite_master\n" |
| 2249 | " WHERE type='table'\n" |
| 2250 | " AND name<>'selftest'\n" |
| 2251 | " AND coalesce(rootpage,0)>0\n" |
| 2252 | " )\n" |
| 2253 | " ORDER BY name;\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2254 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2255 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 2256 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 2257 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 2258 | "DROP TABLE [_shell$self];" |
| 2259 | ,0,0,&zErrMsg); |
| 2260 | if( zErrMsg ){ |
| 2261 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 2262 | sqlite3_free(zErrMsg); |
| 2263 | } |
| 2264 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2267 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2268 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2269 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2270 | ** the name of the table given. Escape any quote characters in the |
| 2271 | ** table name. |
| 2272 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2273 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2274 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2275 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2276 | char *z; |
| 2277 | |
| 2278 | if( p->zDestTable ){ |
| 2279 | free(p->zDestTable); |
| 2280 | p->zDestTable = 0; |
| 2281 | } |
| 2282 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2283 | cQuote = quoteChar(zName); |
| 2284 | n = strlen30(zName); |
| 2285 | if( cQuote ) n += 2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2286 | z = p->zDestTable = malloc( n+1 ); |
| 2287 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2288 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2289 | exit(1); |
| 2290 | } |
| 2291 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2292 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2293 | for(i=0; zName[i]; i++){ |
| 2294 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2295 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2296 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 2297 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2298 | z[n] = 0; |
| 2299 | } |
| 2300 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2303 | ** Execute a query statement that will generate SQL output. Print |
| 2304 | ** the result columns, comma-separated, on a line and then add a |
| 2305 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2306 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2307 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2308 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2309 | ** "--" comment occurs at the end of the statement, the comment |
| 2310 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2311 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2312 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2313 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2314 | const char *zSelect, /* SELECT statement to extract content */ |
| 2315 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2316 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2317 | sqlite3_stmt *pSelect; |
| 2318 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2319 | int nResult; |
| 2320 | int i; |
| 2321 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 2322 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2323 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2324 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2325 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2326 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2327 | return rc; |
| 2328 | } |
| 2329 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2330 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2331 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2332 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2333 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2334 | zFirstRow = 0; |
| 2335 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2336 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2337 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2338 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2339 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2340 | } |
| 2341 | if( z==0 ) z = ""; |
| 2342 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 2343 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2344 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 2345 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2346 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2347 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2348 | rc = sqlite3_step(pSelect); |
| 2349 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2350 | rc = sqlite3_finalize(pSelect); |
| 2351 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2352 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 2353 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 2354 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2355 | } |
| 2356 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2359 | /* |
| 2360 | ** Allocate space and save off current error string. |
| 2361 | */ |
| 2362 | static char *save_err_msg( |
| 2363 | sqlite3 *db /* Database to query */ |
| 2364 | ){ |
| 2365 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2366 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2367 | if( zErrMsg ){ |
| 2368 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 2369 | } |
| 2370 | return zErrMsg; |
| 2371 | } |
| 2372 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2373 | #ifdef __linux__ |
| 2374 | /* |
| 2375 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 2376 | */ |
| 2377 | static void displayLinuxIoStats(FILE *out){ |
| 2378 | FILE *in; |
| 2379 | char z[200]; |
| 2380 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 2381 | in = fopen(z, "rb"); |
| 2382 | if( in==0 ) return; |
| 2383 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 2384 | static const struct { |
| 2385 | const char *zPattern; |
| 2386 | const char *zDesc; |
| 2387 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 2388 | { "rchar: ", "Bytes received by read():" }, |
| 2389 | { "wchar: ", "Bytes sent to write():" }, |
| 2390 | { "syscr: ", "Read() system calls:" }, |
| 2391 | { "syscw: ", "Write() system calls:" }, |
| 2392 | { "read_bytes: ", "Bytes read from storage:" }, |
| 2393 | { "write_bytes: ", "Bytes written to storage:" }, |
| 2394 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2395 | }; |
| 2396 | int i; |
| 2397 | for(i=0; i<ArraySize(aTrans); i++){ |
| 2398 | int n = (int)strlen(aTrans[i].zPattern); |
| 2399 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2400 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2406 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2407 | #endif |
| 2408 | |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2409 | /* |
| 2410 | ** Display a single line of status using 64-bit values. |
| 2411 | */ |
| 2412 | static void displayStatLine( |
| 2413 | ShellState *p, /* The shell context */ |
| 2414 | char *zLabel, /* Label for this one line */ |
| 2415 | char *zFormat, /* Format for the result */ |
| 2416 | int iStatusCtrl, /* Which status to display */ |
| 2417 | int bReset /* True to reset the stats */ |
| 2418 | ){ |
| 2419 | sqlite3_int64 iCur = -1; |
| 2420 | sqlite3_int64 iHiwtr = -1; |
| 2421 | int i, nPercent; |
| 2422 | char zLine[200]; |
| 2423 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 2424 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 2425 | if( zFormat[i]=='%' ) nPercent++; |
| 2426 | } |
| 2427 | if( nPercent>1 ){ |
| 2428 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 2429 | }else{ |
| 2430 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 2431 | } |
| 2432 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 2433 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2434 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2435 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2436 | ** Display memory stats. |
| 2437 | */ |
| 2438 | static int display_stats( |
| 2439 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2440 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2441 | int bReset /* True to reset the stats */ |
| 2442 | ){ |
| 2443 | int iCur; |
| 2444 | int iHiwtr; |
| 2445 | |
| 2446 | if( pArg && pArg->out ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2447 | displayStatLine(pArg, "Memory Used:", |
| 2448 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 2449 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 2450 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2451 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2452 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 2453 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2454 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2455 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 2456 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2457 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2458 | displayStatLine(pArg, "Number of Scratch Allocations Used:", |
| 2459 | "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2460 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2461 | displayStatLine(pArg, "Number of Scratch Overflow Bytes:", |
| 2462 | "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset); |
| 2463 | displayStatLine(pArg, "Largest Allocation:", |
| 2464 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 2465 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 2466 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 2467 | displayStatLine(pArg, "Largest Scratch Allocation:", |
| 2468 | "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2469 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 2470 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 2471 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2472 | #endif |
| 2473 | } |
| 2474 | |
| 2475 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2476 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 2477 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2478 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 2479 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2480 | raw_printf(pArg->out, |
| 2481 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2482 | iCur, iHiwtr); |
| 2483 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 2484 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2485 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 2486 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2487 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 2488 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2489 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 2490 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2491 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 2492 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2493 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 2494 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2495 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2496 | iHiwtr = iCur = -1; |
| 2497 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2498 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 2499 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2500 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2501 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2502 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 2503 | iHiwtr = iCur = -1; |
| 2504 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2505 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2506 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2507 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2508 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 2509 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2510 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2511 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2512 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2513 | iHiwtr = iCur = -1; |
| 2514 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2515 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2516 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2520 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 2521 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2522 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2523 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2524 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2525 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2526 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2527 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2528 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 2531 | #ifdef __linux__ |
| 2532 | displayLinuxIoStats(pArg->out); |
| 2533 | #endif |
| 2534 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 2535 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 2536 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2537 | return 0; |
| 2538 | } |
| 2539 | |
| 2540 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2541 | ** Display scan stats. |
| 2542 | */ |
| 2543 | static void display_scanstats( |
| 2544 | sqlite3 *db, /* Database to query */ |
| 2545 | ShellState *pArg /* Pointer to ShellState */ |
| 2546 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 2547 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2548 | UNUSED_PARAMETER(db); |
| 2549 | UNUSED_PARAMETER(pArg); |
| 2550 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2551 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2552 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2553 | mx = 0; |
| 2554 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2555 | double rEstLoop = 1.0; |
| 2556 | for(i=n=0; 1; i++){ |
| 2557 | sqlite3_stmt *p = pArg->pStmt; |
| 2558 | sqlite3_int64 nLoop, nVisit; |
| 2559 | double rEst; |
| 2560 | int iSid; |
| 2561 | const char *zExplain; |
| 2562 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 2563 | break; |
| 2564 | } |
| 2565 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2566 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2567 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2568 | if( n==0 ){ |
| 2569 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2570 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 2571 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2572 | n++; |
| 2573 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 2574 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 2575 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2576 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2577 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2578 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2579 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 2580 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 2581 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2582 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2583 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2584 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 2585 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2589 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2590 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 2591 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 2592 | ** Otherwise, return zero. |
| 2593 | */ |
| 2594 | static int str_in_array(const char *zStr, const char **azArray){ |
| 2595 | int i; |
| 2596 | for(i=0; azArray[i]; i++){ |
| 2597 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2598 | } |
| 2599 | return 0; |
| 2600 | } |
| 2601 | |
| 2602 | /* |
| 2603 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2604 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2605 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2606 | ** |
| 2607 | ** The indenting rules are: |
| 2608 | ** |
| 2609 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2610 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 2611 | ** itself by 2 spaces. |
| 2612 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2613 | ** * For each "Goto", if the jump destination is earlier in the program |
| 2614 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 2615 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2616 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 2617 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 2618 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2619 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2620 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2621 | const char *zSql; /* The text of the SQL statement */ |
| 2622 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 2623 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 2624 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2625 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2626 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 2627 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2628 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2629 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2630 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2631 | const char *azGoto[] = { "Goto", 0 }; |
| 2632 | |
| 2633 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 2634 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2635 | if( sqlite3_column_count(pSql)!=8 ){ |
| 2636 | p->cMode = p->mode; |
| 2637 | return; |
| 2638 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2639 | zSql = sqlite3_sql(pSql); |
| 2640 | if( zSql==0 ) return; |
| 2641 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2642 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 2643 | p->cMode = p->mode; |
| 2644 | return; |
| 2645 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2646 | |
| 2647 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 2648 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2649 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2650 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2651 | |
| 2652 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 2653 | ** p2 is an instruction address, set variable p2op to the index of that |
| 2654 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 2655 | ** the current instruction is part of a sub-program generated by an |
| 2656 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2657 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2658 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2659 | |
| 2660 | /* Grow the p->aiIndent array as required */ |
| 2661 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2662 | if( iOp==0 ){ |
| 2663 | /* Do further verfication that this is explain output. Abort if |
| 2664 | ** it is not */ |
| 2665 | static const char *explainCols[] = { |
| 2666 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 2667 | int jj; |
| 2668 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 2669 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 2670 | p->cMode = p->mode; |
| 2671 | sqlite3_reset(pSql); |
| 2672 | return; |
| 2673 | } |
| 2674 | } |
| 2675 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2676 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 2677 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 2678 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2679 | } |
| 2680 | abYield[iOp] = str_in_array(zOp, azYield); |
| 2681 | p->aiIndent[iOp] = 0; |
| 2682 | p->nIndent = iOp+1; |
| 2683 | |
| 2684 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2685 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2686 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 2687 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 2688 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 2689 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2690 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2694 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2695 | sqlite3_free(abYield); |
| 2696 | sqlite3_reset(pSql); |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | ** Free the array allocated by explain_data_prepare(). |
| 2701 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2702 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2703 | sqlite3_free(p->aiIndent); |
| 2704 | p->aiIndent = 0; |
| 2705 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2706 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2710 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 2711 | */ |
| 2712 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2713 | extern int sqlite3SelectTrace; |
| 2714 | static int savedSelectTrace; |
| 2715 | #endif |
| 2716 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2717 | extern int sqlite3WhereTrace; |
| 2718 | static int savedWhereTrace; |
| 2719 | #endif |
| 2720 | static void disable_debug_trace_modes(void){ |
| 2721 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2722 | savedSelectTrace = sqlite3SelectTrace; |
| 2723 | sqlite3SelectTrace = 0; |
| 2724 | #endif |
| 2725 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2726 | savedWhereTrace = sqlite3WhereTrace; |
| 2727 | sqlite3WhereTrace = 0; |
| 2728 | #endif |
| 2729 | } |
| 2730 | static void restore_debug_trace_modes(void){ |
| 2731 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 2732 | sqlite3SelectTrace = savedSelectTrace; |
| 2733 | #endif |
| 2734 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 2735 | sqlite3WhereTrace = savedWhereTrace; |
| 2736 | #endif |
| 2737 | } |
| 2738 | |
| 2739 | /* |
| 2740 | ** Run a prepared statement |
| 2741 | */ |
| 2742 | static void exec_prepared_stmt( |
| 2743 | ShellState *pArg, /* Pointer to ShellState */ |
| 2744 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 2745 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 2746 | ){ |
| 2747 | int rc; |
| 2748 | |
| 2749 | /* perform the first step. this will tell us if we |
| 2750 | ** have a result set or not and how wide it is. |
| 2751 | */ |
| 2752 | rc = sqlite3_step(pStmt); |
| 2753 | /* if we have a result set... */ |
| 2754 | if( SQLITE_ROW == rc ){ |
| 2755 | /* if we have a callback... */ |
| 2756 | if( xCallback ){ |
| 2757 | /* allocate space for col name ptr, value ptr, and type */ |
| 2758 | int nCol = sqlite3_column_count(pStmt); |
| 2759 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 2760 | if( !pData ){ |
| 2761 | rc = SQLITE_NOMEM; |
| 2762 | }else{ |
| 2763 | char **azCols = (char **)pData; /* Names of result columns */ |
| 2764 | char **azVals = &azCols[nCol]; /* Results */ |
| 2765 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 2766 | int i, x; |
| 2767 | assert(sizeof(int) <= sizeof(char *)); |
| 2768 | /* save off ptrs to column names */ |
| 2769 | for(i=0; i<nCol; i++){ |
| 2770 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 2771 | } |
| 2772 | do{ |
| 2773 | /* extract the data and data types */ |
| 2774 | for(i=0; i<nCol; i++){ |
| 2775 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 2776 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 2777 | azVals[i] = ""; |
| 2778 | }else{ |
| 2779 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 2780 | } |
| 2781 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 2782 | rc = SQLITE_NOMEM; |
| 2783 | break; /* from for */ |
| 2784 | } |
| 2785 | } /* end for */ |
| 2786 | |
| 2787 | /* if data and types extracted successfully... */ |
| 2788 | if( SQLITE_ROW == rc ){ |
| 2789 | /* call the supplied callback with the result row data */ |
| 2790 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 2791 | rc = SQLITE_ABORT; |
| 2792 | }else{ |
| 2793 | rc = sqlite3_step(pStmt); |
| 2794 | } |
| 2795 | } |
| 2796 | } while( SQLITE_ROW == rc ); |
| 2797 | sqlite3_free(pData); |
| 2798 | } |
| 2799 | }else{ |
| 2800 | do{ |
| 2801 | rc = sqlite3_step(pStmt); |
| 2802 | } while( rc == SQLITE_ROW ); |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2808 | ** Execute a statement or set of statements. Print |
| 2809 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2810 | ** set via the supplied callback. |
| 2811 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2812 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 2813 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2814 | ** and callback data argument. |
| 2815 | */ |
| 2816 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2817 | sqlite3 *db, /* An open database */ |
| 2818 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2819 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2820 | /* (not the same as sqlite3_exec) */ |
| 2821 | ShellState *pArg, /* Pointer to ShellState */ |
| 2822 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2823 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2824 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 2825 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2826 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2827 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2828 | |
| 2829 | if( pzErrMsg ){ |
| 2830 | *pzErrMsg = NULL; |
| 2831 | } |
| 2832 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2833 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2834 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2835 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 2836 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2837 | if( pzErrMsg ){ |
| 2838 | *pzErrMsg = save_err_msg(db); |
| 2839 | } |
| 2840 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2841 | if( !pStmt ){ |
| 2842 | /* this happens for a comment or white-space */ |
| 2843 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2844 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2845 | continue; |
| 2846 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2847 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 2848 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2849 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2850 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2851 | /* save off the prepared statment handle and reset row count */ |
| 2852 | if( pArg ){ |
| 2853 | pArg->pStmt = pStmt; |
| 2854 | pArg->cnt = 0; |
| 2855 | } |
| 2856 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2857 | /* echo the sql statement if echo on */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2858 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2859 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 2860 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 2861 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2862 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2863 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2864 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2865 | char *zEQP; |
| 2866 | disable_debug_trace_modes(); |
| 2867 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2868 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2869 | if( rc==SQLITE_OK ){ |
| 2870 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2871 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 2872 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 2873 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2874 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2875 | } |
| 2876 | } |
| 2877 | sqlite3_finalize(pExplain); |
| 2878 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2879 | if( pArg->autoEQP>=2 ){ |
| 2880 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 2881 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 2882 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 2883 | if( rc==SQLITE_OK ){ |
| 2884 | pArg->cMode = MODE_Explain; |
| 2885 | explain_data_prepare(pArg, pExplain); |
| 2886 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 2887 | explain_data_delete(pArg); |
| 2888 | } |
| 2889 | sqlite3_finalize(pExplain); |
| 2890 | sqlite3_free(zEQP); |
| 2891 | } |
| 2892 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2895 | if( pArg ){ |
| 2896 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 2897 | if( pArg->autoExplain |
| 2898 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2899 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2900 | ){ |
| 2901 | pArg->cMode = MODE_Explain; |
| 2902 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2903 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2904 | /* If the shell is currently in ".explain" mode, gather the extra |
| 2905 | ** data required to add indents to the output.*/ |
| 2906 | if( pArg->cMode==MODE_Explain ){ |
| 2907 | explain_data_prepare(pArg, pStmt); |
| 2908 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 2911 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2912 | explain_data_delete(pArg); |
| 2913 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2914 | /* print usage stats if stats on */ |
| 2915 | if( pArg && pArg->statsOn ){ |
| 2916 | display_stats(db, pArg, 0); |
| 2917 | } |
| 2918 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2919 | /* print loop-counters if required */ |
| 2920 | if( pArg && pArg->scanstatsOn ){ |
| 2921 | display_scanstats(db, pArg); |
| 2922 | } |
| 2923 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2924 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2925 | ** copy of the error message. Otherwise, set zSql to point to the |
| 2926 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2927 | rc2 = sqlite3_finalize(pStmt); |
| 2928 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2929 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2930 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 2931 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 2932 | }else if( pzErrMsg ){ |
| 2933 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2934 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2935 | |
| 2936 | /* clear saved stmt handle */ |
| 2937 | if( pArg ){ |
| 2938 | pArg->pStmt = NULL; |
| 2939 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2940 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2941 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2942 | |
| 2943 | return rc; |
| 2944 | } |
| 2945 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2946 | /* |
| 2947 | ** Release memory previously allocated by tableColumnList(). |
| 2948 | */ |
| 2949 | static void freeColumnList(char **azCol){ |
| 2950 | int i; |
| 2951 | for(i=1; azCol[i]; i++){ |
| 2952 | sqlite3_free(azCol[i]); |
| 2953 | } |
| 2954 | /* azCol[0] is a static string */ |
| 2955 | sqlite3_free(azCol); |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | ** Return a list of pointers to strings which are the names of all |
| 2960 | ** columns in table zTab. The memory to hold the names is dynamically |
| 2961 | ** allocated and must be released by the caller using a subsequent call |
| 2962 | ** to freeColumnList(). |
| 2963 | ** |
| 2964 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 2965 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 2966 | ** name of the rowid column. |
| 2967 | ** |
| 2968 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 2969 | ** by an entry with azCol[i]==0. |
| 2970 | */ |
| 2971 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 2972 | char **azCol = 0; |
| 2973 | sqlite3_stmt *pStmt; |
| 2974 | char *zSql; |
| 2975 | int nCol = 0; |
| 2976 | int nAlloc = 0; |
| 2977 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 2978 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2979 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 2980 | int rc; |
| 2981 | |
| 2982 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 2983 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 2984 | sqlite3_free(zSql); |
| 2985 | if( rc ) return 0; |
| 2986 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2987 | if( nCol>=nAlloc-2 ){ |
| 2988 | nAlloc = nAlloc*2 + nCol + 10; |
| 2989 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 2990 | if( azCol==0 ){ |
| 2991 | raw_printf(stderr, "Error: out of memory\n"); |
| 2992 | exit(1); |
| 2993 | } |
| 2994 | } |
| 2995 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 2996 | if( sqlite3_column_int(pStmt, 5) ){ |
| 2997 | nPK++; |
| 2998 | if( nPK==1 |
| 2999 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 3000 | "INTEGER")==0 |
| 3001 | ){ |
| 3002 | isIPK = 1; |
| 3003 | }else{ |
| 3004 | isIPK = 0; |
| 3005 | } |
| 3006 | } |
| 3007 | } |
| 3008 | sqlite3_finalize(pStmt); |
| 3009 | azCol[0] = 0; |
| 3010 | azCol[nCol+1] = 0; |
| 3011 | |
| 3012 | /* The decision of whether or not a rowid really needs to be preserved |
| 3013 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3014 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3015 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3016 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3017 | */ |
| 3018 | if( preserveRowid && isIPK ){ |
| 3019 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 3020 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 3021 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 3022 | ** ROWID aliases. To distinguish these cases, check to see if |
| 3023 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 3024 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 3025 | */ |
| 3026 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 3027 | " WHERE origin='pk'", zTab); |
| 3028 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3029 | sqlite3_free(zSql); |
| 3030 | if( rc ){ |
| 3031 | freeColumnList(azCol); |
| 3032 | return 0; |
| 3033 | } |
| 3034 | rc = sqlite3_step(pStmt); |
| 3035 | sqlite3_finalize(pStmt); |
| 3036 | preserveRowid = rc==SQLITE_ROW; |
| 3037 | } |
| 3038 | if( preserveRowid ){ |
| 3039 | /* Only preserve the rowid if we can find a name to use for the |
| 3040 | ** rowid */ |
| 3041 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3042 | int i, j; |
| 3043 | for(j=0; j<3; j++){ |
| 3044 | for(i=1; i<=nCol; i++){ |
| 3045 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3046 | } |
| 3047 | if( i>nCol ){ |
| 3048 | /* At this point, we know that azRowid[j] is not the name of any |
| 3049 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3050 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3051 | ** tables will fail this last check */ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3052 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3053 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3054 | break; |
| 3055 | } |
| 3056 | } |
| 3057 | } |
| 3058 | return azCol; |
| 3059 | } |
| 3060 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3061 | /* |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3062 | ** Toggle the reverse_unordered_selects setting. |
| 3063 | */ |
| 3064 | static void toggleSelectOrder(sqlite3 *db){ |
| 3065 | sqlite3_stmt *pStmt = 0; |
| 3066 | int iSetting = 0; |
| 3067 | char zStmt[100]; |
| 3068 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3069 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3070 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3071 | } |
| 3072 | sqlite3_finalize(pStmt); |
| 3073 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3074 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3075 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3076 | } |
| 3077 | |
| 3078 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3079 | ** This is a different callback routine used for dumping the database. |
| 3080 | ** Each row received by this callback consists of a table name, |
| 3081 | ** the table type ("index" or "table") and SQL to create the table. |
| 3082 | ** This routine should print text sufficient to recreate the table. |
| 3083 | */ |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3084 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3085 | int rc; |
| 3086 | const char *zTable; |
| 3087 | const char *zType; |
| 3088 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3089 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3090 | |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3091 | UNUSED_PARAMETER(azNotUsed); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3092 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3093 | zTable = azArg[0]; |
| 3094 | zType = azArg[1]; |
| 3095 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3096 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3097 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3098 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 3099 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3100 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3101 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3102 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3103 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3104 | char *zIns; |
| 3105 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3106 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3107 | p->writableSchema = 1; |
| 3108 | } |
| 3109 | zIns = sqlite3_mprintf( |
| 3110 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3111 | "VALUES('table','%q','%q',0,'%q');", |
| 3112 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3113 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3114 | sqlite3_free(zIns); |
| 3115 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3116 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 3117 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 3118 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3119 | |
| 3120 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3121 | ShellText sSelect; |
| 3122 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3123 | char **azCol; |
| 3124 | int i; |
| 3125 | char *savedDestTable; |
| 3126 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3127 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3128 | azCol = tableColumnList(p, zTable); |
| 3129 | if( azCol==0 ){ |
| 3130 | p->nErr++; |
| 3131 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 3134 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3135 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3136 | initText(&sTable); |
| 3137 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3138 | /* If preserving the rowid, add a column list after the table name. |
| 3139 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3140 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3141 | */ |
| 3142 | if( azCol[0] ){ |
| 3143 | appendText(&sTable, "(", 0); |
| 3144 | appendText(&sTable, azCol[0], 0); |
| 3145 | for(i=1; azCol[i]; i++){ |
| 3146 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3147 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3148 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3149 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3150 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3151 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3152 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3153 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3154 | appendText(&sSelect, "SELECT ", 0); |
| 3155 | if( azCol[0] ){ |
| 3156 | appendText(&sSelect, azCol[0], 0); |
| 3157 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3158 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3159 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3160 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3161 | if( azCol[i+1] ){ |
| 3162 | appendText(&sSelect, ",", 0); |
| 3163 | } |
| 3164 | } |
| 3165 | freeColumnList(azCol); |
| 3166 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3167 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3168 | |
| 3169 | savedDestTable = p->zDestTable; |
| 3170 | savedMode = p->mode; |
| 3171 | p->zDestTable = sTable.z; |
| 3172 | p->mode = p->cMode = MODE_Insert; |
| 3173 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3174 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3175 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3176 | toggleSelectOrder(p->db); |
| 3177 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3178 | toggleSelectOrder(p->db); |
| 3179 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3180 | p->zDestTable = savedDestTable; |
| 3181 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3182 | freeText(&sTable); |
| 3183 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3184 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3185 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3190 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3191 | ** the contents of the query are output as SQL statements. |
| 3192 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3193 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3194 | ** "ORDER BY rowid DESC" to the end. |
| 3195 | */ |
| 3196 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3197 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3198 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3199 | ){ |
| 3200 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3201 | char *zErr = 0; |
| 3202 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3203 | if( rc==SQLITE_CORRUPT ){ |
| 3204 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3205 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3206 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3207 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3208 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3209 | sqlite3_free(zErr); |
| 3210 | zErr = 0; |
| 3211 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3212 | zQ2 = malloc( len+100 ); |
| 3213 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 3214 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3215 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 3216 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3217 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3218 | }else{ |
| 3219 | rc = SQLITE_CORRUPT; |
| 3220 | } |
| 3221 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3222 | free(zQ2); |
| 3223 | } |
| 3224 | return rc; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3228 | ** Text of a help message |
| 3229 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 3230 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3231 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 3232 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3233 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3234 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3235 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3236 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame^] | 3237 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 3238 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3239 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3240 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 3241 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3242 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3243 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3244 | " If TABLE specified, only dump tables matching\n" |
| 3245 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3246 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3247 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3248 | ".exit Exit this program\n" |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3249 | ".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] | 3250 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3251 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3252 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3253 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 3254 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 3255 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 3256 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 3257 | ".indexes ?TABLE? Show names of all indexes\n" |
| 3258 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3259 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 3260 | #ifdef SQLITE_ENABLE_IOTRACE |
| 3261 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 3262 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 3263 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 3264 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 3265 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3266 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 3267 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 3268 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 3269 | ".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] | 3270 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3271 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 3272 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3273 | " column Left-aligned columns. (See .width)\n" |
| 3274 | " html HTML <table> code\n" |
| 3275 | " insert SQL insert statements for TABLE\n" |
| 3276 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 3277 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 3278 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 3279 | " tabs Tab-separated values\n" |
| 3280 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3281 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3282 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3283 | ".open ?--new? ?FILE? Close existing database and reopen FILE\n" |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 3284 | " The --new starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3285 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 3286 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3287 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3288 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3289 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 3290 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 3291 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3292 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 3293 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 3294 | " Add --indent for pretty-printing\n" |
drh | a5d75ba | 2017-03-15 14:20:34 +0000 | [diff] [blame] | 3295 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 3296 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 3297 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3298 | #if defined(SQLITE_ENABLE_SESSION) |
| 3299 | ".session CMD ... Create or control sessions\n" |
| 3300 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3301 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3302 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3303 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3304 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3305 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 3306 | ".tables ?TABLE? List names of tables\n" |
| 3307 | " If TABLE specified, only list tables matching\n" |
| 3308 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 3309 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 3310 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3311 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3312 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 3313 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 3314 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3315 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 3316 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 3317 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3318 | ; |
| 3319 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3320 | #if defined(SQLITE_ENABLE_SESSION) |
| 3321 | /* |
| 3322 | ** Print help information for the ".sessions" command |
| 3323 | */ |
| 3324 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 3325 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3326 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 3327 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 3328 | "Subcommands:\n" |
| 3329 | " attach TABLE Attach TABLE\n" |
| 3330 | " changeset FILE Write a changeset into FILE\n" |
| 3331 | " close Close one session\n" |
| 3332 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3333 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3334 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 3335 | " isempty Query whether the session is empty\n" |
| 3336 | " list List currently open session names\n" |
| 3337 | " open DB NAME Open a new session on DB\n" |
| 3338 | " patchset FILE Write a patchset into FILE\n" |
| 3339 | ); |
| 3340 | } |
| 3341 | #endif |
| 3342 | |
| 3343 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3344 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3345 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3346 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3347 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3348 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
| 3349 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 3350 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3351 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3352 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 3353 | ** read. |
| 3354 | ** |
| 3355 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 3356 | ** from the file before the buffer is returned. This byte is not included in |
| 3357 | ** the final value of (*pnByte), if applicable. |
| 3358 | ** |
| 3359 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 3360 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3361 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3362 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3363 | FILE *in = fopen(zName, "rb"); |
| 3364 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3365 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3366 | char *pBuf; |
| 3367 | if( in==0 ) return 0; |
| 3368 | fseek(in, 0, SEEK_END); |
| 3369 | nIn = ftell(in); |
| 3370 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3371 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3372 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3373 | nRead = fread(pBuf, nIn, 1, in); |
| 3374 | fclose(in); |
| 3375 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3376 | sqlite3_free(pBuf); |
| 3377 | return 0; |
| 3378 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 3379 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3380 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 3381 | return pBuf; |
| 3382 | } |
| 3383 | |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3384 | /* |
| 3385 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 3386 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 3387 | ** if the file does not exist or is unreadable. |
| 3388 | */ |
| 3389 | static void readfileFunc( |
| 3390 | sqlite3_context *context, |
| 3391 | int argc, |
| 3392 | sqlite3_value **argv |
| 3393 | ){ |
| 3394 | const char *zName; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3395 | void *pBuf; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3396 | int nBuf; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3397 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3398 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3399 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 3400 | if( zName==0 ) return; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 3401 | pBuf = readFile(zName, &nBuf); |
| 3402 | if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | /* |
| 3406 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3407 | ** is written into file X. The number of bytes written is returned. Or |
| 3408 | ** NULL is returned if something goes wrong, such as being unable to open |
| 3409 | ** file X for writing. |
| 3410 | */ |
| 3411 | static void writefileFunc( |
| 3412 | sqlite3_context *context, |
| 3413 | int argc, |
| 3414 | sqlite3_value **argv |
| 3415 | ){ |
| 3416 | FILE *out; |
| 3417 | const char *z; |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3418 | sqlite3_int64 rc; |
| 3419 | const char *zFile; |
| 3420 | |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3421 | UNUSED_PARAMETER(argc); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3422 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3423 | if( zFile==0 ) return; |
| 3424 | out = fopen(zFile, "wb"); |
| 3425 | if( out==0 ) return; |
| 3426 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 3427 | if( z==0 ){ |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3428 | rc = 0; |
| 3429 | }else{ |
drh | 490fe86 | 2014-08-11 14:21:32 +0000 | [diff] [blame] | 3430 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3431 | } |
| 3432 | fclose(out); |
| 3433 | sqlite3_result_int64(context, rc); |
| 3434 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3435 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3436 | #if defined(SQLITE_ENABLE_SESSION) |
| 3437 | /* |
| 3438 | ** Close a single OpenSession object and release all of its associated |
| 3439 | ** resources. |
| 3440 | */ |
| 3441 | static void session_close(OpenSession *pSession){ |
| 3442 | int i; |
| 3443 | sqlite3session_delete(pSession->p); |
| 3444 | sqlite3_free(pSession->zName); |
| 3445 | for(i=0; i<pSession->nFilter; i++){ |
| 3446 | sqlite3_free(pSession->azFilter[i]); |
| 3447 | } |
| 3448 | sqlite3_free(pSession->azFilter); |
| 3449 | memset(pSession, 0, sizeof(OpenSession)); |
| 3450 | } |
| 3451 | #endif |
| 3452 | |
| 3453 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3454 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3455 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3456 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3457 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3458 | int i; |
| 3459 | for(i=0; i<p->nSession; i++){ |
| 3460 | session_close(&p->aSession[i]); |
| 3461 | } |
| 3462 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3463 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 3464 | #else |
| 3465 | # define session_close_all(X) |
| 3466 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 3467 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3468 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 3469 | ** Implementation of the xFilter function for an open session. Omit |
| 3470 | ** any tables named by ".session filter" but let all other table through. |
| 3471 | */ |
| 3472 | #if defined(SQLITE_ENABLE_SESSION) |
| 3473 | static int session_filter(void *pCtx, const char *zTab){ |
| 3474 | OpenSession *pSession = (OpenSession*)pCtx; |
| 3475 | int i; |
| 3476 | for(i=0; i<pSession->nFilter; i++){ |
| 3477 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 3478 | } |
| 3479 | return 1; |
| 3480 | } |
| 3481 | #endif |
| 3482 | |
| 3483 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3484 | ** Make sure the database is open. If it is not, then open it. If |
| 3485 | ** the database fails to open, print an error message and exit. |
| 3486 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3487 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3488 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 3489 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3490 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3491 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3492 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3493 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3494 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 3495 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3496 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3497 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 3498 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3499 | sqlite3_enable_load_extension(p->db, 1); |
| 3500 | #endif |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3501 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3502 | readfileFunc, 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 3503 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
drh | ba5b093 | 2014-07-24 12:39:59 +0000 | [diff] [blame] | 3504 | writefileFunc, 0, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 3505 | sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3506 | sha3Func, 0, 0); |
| 3507 | sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3508 | sha3Func, 0, 0); |
| 3509 | sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3510 | sha3QueryFunc, 0, 0); |
| 3511 | sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3512 | sha3QueryFunc, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3517 | ** Do C-language style dequoting. |
| 3518 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3519 | ** \a -> alarm |
| 3520 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3521 | ** \t -> tab |
| 3522 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3523 | ** \v -> vertical tab |
| 3524 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3525 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3526 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3527 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3528 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3529 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3530 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3531 | */ |
| 3532 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3533 | int i, j; |
| 3534 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3535 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3536 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 3537 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3538 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3539 | if( c=='a' ){ |
| 3540 | c = '\a'; |
| 3541 | }else if( c=='b' ){ |
| 3542 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3543 | }else if( c=='t' ){ |
| 3544 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3545 | }else if( c=='n' ){ |
| 3546 | c = '\n'; |
| 3547 | }else if( c=='v' ){ |
| 3548 | c = '\v'; |
| 3549 | }else if( c=='f' ){ |
| 3550 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3551 | }else if( c=='r' ){ |
| 3552 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 3553 | }else if( c=='"' ){ |
| 3554 | c = '"'; |
| 3555 | }else if( c=='\'' ){ |
| 3556 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 3557 | }else if( c=='\\' ){ |
| 3558 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3559 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 3560 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3561 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3562 | i++; |
| 3563 | c = (c<<3) + z[i] - '0'; |
| 3564 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 3565 | i++; |
| 3566 | c = (c<<3) + z[i] - '0'; |
| 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | } |
| 3571 | z[j] = c; |
| 3572 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 3573 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3577 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 3578 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3579 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3580 | static int hexDigitValue(char c){ |
| 3581 | if( c>='0' && c<='9' ) return c - '0'; |
| 3582 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 3583 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 3584 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3588 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 3589 | */ |
| 3590 | static sqlite3_int64 integerValue(const char *zArg){ |
| 3591 | sqlite3_int64 v = 0; |
| 3592 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 3593 | { "KiB", 1024 }, |
| 3594 | { "MiB", 1024*1024 }, |
| 3595 | { "GiB", 1024*1024*1024 }, |
| 3596 | { "KB", 1000 }, |
| 3597 | { "MB", 1000000 }, |
| 3598 | { "GB", 1000000000 }, |
| 3599 | { "K", 1000 }, |
| 3600 | { "M", 1000000 }, |
| 3601 | { "G", 1000000000 }, |
| 3602 | }; |
| 3603 | int i; |
| 3604 | int isNeg = 0; |
| 3605 | if( zArg[0]=='-' ){ |
| 3606 | isNeg = 1; |
| 3607 | zArg++; |
| 3608 | }else if( zArg[0]=='+' ){ |
| 3609 | zArg++; |
| 3610 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3611 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3612 | int x; |
| 3613 | zArg += 2; |
| 3614 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 3615 | v = (v<<4) + x; |
| 3616 | zArg++; |
| 3617 | } |
| 3618 | }else{ |
| 3619 | while( IsDigit(zArg[0]) ){ |
| 3620 | v = v*10 + zArg[0] - '0'; |
| 3621 | zArg++; |
| 3622 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3623 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 3624 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 3625 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 3626 | v *= aMult[i].iMult; |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
| 3630 | return isNeg? -v : v; |
| 3631 | } |
| 3632 | |
| 3633 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3634 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 3635 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 3636 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3637 | static int booleanValue(const char *zArg){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3638 | int i; |
| 3639 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 3640 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 3641 | }else{ |
| 3642 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 3643 | } |
| 3644 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 3645 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 3646 | return 1; |
| 3647 | } |
| 3648 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 3649 | return 0; |
| 3650 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3651 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 3652 | zArg); |
| 3653 | return 0; |
| 3654 | } |
| 3655 | |
| 3656 | /* |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3657 | ** Set or clear a shell flag according to a boolean value. |
| 3658 | */ |
| 3659 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 3660 | if( booleanValue(zArg) ){ |
| 3661 | ShellSetFlag(p, mFlag); |
| 3662 | }else{ |
| 3663 | ShellClearFlag(p, mFlag); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3668 | ** Close an output file, assuming it is not stderr or stdout |
| 3669 | */ |
| 3670 | static void output_file_close(FILE *f){ |
| 3671 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3676 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3677 | ** filename is "off". |
| 3678 | */ |
| 3679 | static FILE *output_file_open(const char *zFile){ |
| 3680 | FILE *f; |
| 3681 | if( strcmp(zFile,"stdout")==0 ){ |
| 3682 | f = stdout; |
| 3683 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 3684 | f = stderr; |
| 3685 | }else if( strcmp(zFile, "off")==0 ){ |
| 3686 | f = 0; |
| 3687 | }else{ |
| 3688 | f = fopen(zFile, "wb"); |
| 3689 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3690 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3691 | } |
| 3692 | } |
| 3693 | return f; |
| 3694 | } |
| 3695 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 3696 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3697 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3698 | /* |
| 3699 | ** A routine for handling output from sqlite3_trace(). |
| 3700 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3701 | static int sql_trace_callback( |
| 3702 | unsigned mType, |
| 3703 | void *pArg, |
| 3704 | void *pP, |
| 3705 | void *pX |
| 3706 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3707 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 3708 | UNUSED_PARAMETER(mType); |
| 3709 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3710 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3711 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3712 | int i = (int)strlen(z); |
| 3713 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3714 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 3715 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 3716 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3717 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 3718 | #endif |
| 3719 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 3720 | |
| 3721 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 3722 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 3723 | ** a useful spot to set a debugger breakpoint. |
| 3724 | */ |
| 3725 | static void test_breakpoint(void){ |
| 3726 | static int nCall = 0; |
| 3727 | nCall++; |
| 3728 | } |
| 3729 | |
| 3730 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3731 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3732 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3733 | typedef struct ImportCtx ImportCtx; |
| 3734 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3735 | const char *zFile; /* Name of the input file */ |
| 3736 | FILE *in; /* Read the CSV text from this input stream */ |
| 3737 | char *z; /* Accumulated text for a field */ |
| 3738 | int n; /* Number of bytes in z */ |
| 3739 | int nAlloc; /* Space allocated for z[] */ |
| 3740 | int nLine; /* Current line number */ |
| 3741 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3742 | int cColSep; /* The column separator character. (Usually ",") */ |
| 3743 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3744 | }; |
| 3745 | |
| 3746 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3747 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3748 | if( p->n+1>=p->nAlloc ){ |
| 3749 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3750 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3751 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3752 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3753 | exit(1); |
| 3754 | } |
| 3755 | } |
| 3756 | p->z[p->n++] = (char)c; |
| 3757 | } |
| 3758 | |
| 3759 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 3760 | ** with the option of having a separator other than ",". |
| 3761 | ** |
| 3762 | ** + Input comes from p->in. |
| 3763 | ** + 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] | 3764 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3765 | ** + Use p->cSep as the column separator. The default is ",". |
| 3766 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3767 | ** + Keep track of the line number in p->nLine. |
| 3768 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3769 | ** EOF on end-of-file. |
| 3770 | ** + Report syntax errors on stderr |
| 3771 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3772 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3773 | int c; |
| 3774 | int cSep = p->cColSep; |
| 3775 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3776 | p->n = 0; |
| 3777 | c = fgetc(p->in); |
| 3778 | if( c==EOF || seenInterrupt ){ |
| 3779 | p->cTerm = EOF; |
| 3780 | return 0; |
| 3781 | } |
| 3782 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3783 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3784 | int startLine = p->nLine; |
| 3785 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3786 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3787 | while( 1 ){ |
| 3788 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3789 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3790 | if( c==cQuote ){ |
| 3791 | if( pc==cQuote ){ |
| 3792 | pc = 0; |
| 3793 | continue; |
| 3794 | } |
| 3795 | } |
| 3796 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3797 | || (c==rSep && pc==cQuote) |
| 3798 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3799 | || (c==EOF && pc==cQuote) |
| 3800 | ){ |
| 3801 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3802 | p->cTerm = c; |
| 3803 | break; |
| 3804 | } |
| 3805 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3806 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3807 | p->zFile, p->nLine, cQuote); |
| 3808 | } |
| 3809 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3810 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3811 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3812 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3813 | break; |
| 3814 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3815 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 3816 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3817 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3818 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3819 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3820 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3821 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 3822 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3823 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3824 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3825 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 3826 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3827 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3828 | p->cTerm = c; |
| 3829 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 3830 | if( p->z ) p->z[p->n] = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3831 | return p->z; |
| 3832 | } |
| 3833 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3834 | /* Read a single field of ASCII delimited text. |
| 3835 | ** |
| 3836 | ** + Input comes from p->in. |
| 3837 | ** + 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] | 3838 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3839 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 3840 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 3841 | ** + Keep track of the row number in p->nLine. |
| 3842 | ** + Store the character that terminates the field in p->cTerm. Store |
| 3843 | ** EOF on end-of-file. |
| 3844 | ** + Report syntax errors on stderr |
| 3845 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 3846 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 3847 | int c; |
| 3848 | int cSep = p->cColSep; |
| 3849 | int rSep = p->cRowSep; |
| 3850 | p->n = 0; |
| 3851 | c = fgetc(p->in); |
| 3852 | if( c==EOF || seenInterrupt ){ |
| 3853 | p->cTerm = EOF; |
| 3854 | return 0; |
| 3855 | } |
| 3856 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 3857 | import_append_char(p, c); |
| 3858 | c = fgetc(p->in); |
| 3859 | } |
| 3860 | if( c==rSep ){ |
| 3861 | p->nLine++; |
| 3862 | } |
| 3863 | p->cTerm = c; |
| 3864 | if( p->z ) p->z[p->n] = 0; |
| 3865 | return p->z; |
| 3866 | } |
| 3867 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 3868 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3869 | ** Try to transfer data for table zTable. If an error is seen while |
| 3870 | ** moving forward, try to go backwards. The backwards movement won't |
| 3871 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3872 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3873 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3874 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3875 | sqlite3 *newDb, |
| 3876 | const char *zTable |
| 3877 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3878 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3879 | sqlite3_stmt *pInsert = 0; |
| 3880 | char *zQuery = 0; |
| 3881 | char *zInsert = 0; |
| 3882 | int rc; |
| 3883 | int i, j, n; |
| 3884 | int nTable = (int)strlen(zTable); |
| 3885 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3886 | int cnt = 0; |
| 3887 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3888 | |
| 3889 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 3890 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3891 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3892 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3893 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 3894 | zQuery); |
| 3895 | goto end_data_xfer; |
| 3896 | } |
| 3897 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3898 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3899 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3900 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3901 | goto end_data_xfer; |
| 3902 | } |
| 3903 | sqlite3_snprintf(200+nTable,zInsert, |
| 3904 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 3905 | i = (int)strlen(zInsert); |
| 3906 | for(j=1; j<n; j++){ |
| 3907 | memcpy(zInsert+i, ",?", 2); |
| 3908 | i += 2; |
| 3909 | } |
| 3910 | memcpy(zInsert+i, ");", 3); |
| 3911 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 3912 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3913 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3914 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 3915 | zQuery); |
| 3916 | goto end_data_xfer; |
| 3917 | } |
| 3918 | for(k=0; k<2; k++){ |
| 3919 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 3920 | for(i=0; i<n; i++){ |
| 3921 | switch( sqlite3_column_type(pQuery, i) ){ |
| 3922 | case SQLITE_NULL: { |
| 3923 | sqlite3_bind_null(pInsert, i+1); |
| 3924 | break; |
| 3925 | } |
| 3926 | case SQLITE_INTEGER: { |
| 3927 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 3928 | break; |
| 3929 | } |
| 3930 | case SQLITE_FLOAT: { |
| 3931 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 3932 | break; |
| 3933 | } |
| 3934 | case SQLITE_TEXT: { |
| 3935 | sqlite3_bind_text(pInsert, i+1, |
| 3936 | (const char*)sqlite3_column_text(pQuery,i), |
| 3937 | -1, SQLITE_STATIC); |
| 3938 | break; |
| 3939 | } |
| 3940 | case SQLITE_BLOB: { |
| 3941 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 3942 | sqlite3_column_bytes(pQuery,i), |
| 3943 | SQLITE_STATIC); |
| 3944 | break; |
| 3945 | } |
| 3946 | } |
| 3947 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3948 | rc = sqlite3_step(pInsert); |
| 3949 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3950 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3951 | sqlite3_errmsg(newDb)); |
| 3952 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3953 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3954 | cnt++; |
| 3955 | if( (cnt%spinRate)==0 ){ |
| 3956 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 3957 | fflush(stdout); |
| 3958 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3959 | } /* End while */ |
| 3960 | if( rc==SQLITE_DONE ) break; |
| 3961 | sqlite3_finalize(pQuery); |
| 3962 | sqlite3_free(zQuery); |
| 3963 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 3964 | zTable); |
| 3965 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 3966 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3967 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3968 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3969 | } |
| 3970 | } /* End for(k=0...) */ |
| 3971 | |
| 3972 | end_data_xfer: |
| 3973 | sqlite3_finalize(pQuery); |
| 3974 | sqlite3_finalize(pInsert); |
| 3975 | sqlite3_free(zQuery); |
| 3976 | sqlite3_free(zInsert); |
| 3977 | } |
| 3978 | |
| 3979 | |
| 3980 | /* |
| 3981 | ** Try to transfer all rows of the schema that match zWhere. For |
| 3982 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3983 | ** If an error is encountered while moving forward through the |
| 3984 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3985 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 3986 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3987 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3988 | sqlite3 *newDb, |
| 3989 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3990 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3991 | ){ |
| 3992 | sqlite3_stmt *pQuery = 0; |
| 3993 | char *zQuery = 0; |
| 3994 | int rc; |
| 3995 | const unsigned char *zName; |
| 3996 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 3997 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 3998 | |
| 3999 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4000 | " WHERE %s", zWhere); |
| 4001 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4002 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4003 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4004 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4005 | zQuery); |
| 4006 | goto end_schema_xfer; |
| 4007 | } |
| 4008 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4009 | zName = sqlite3_column_text(pQuery, 0); |
| 4010 | zSql = sqlite3_column_text(pQuery, 1); |
| 4011 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4012 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4013 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4014 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4015 | sqlite3_free(zErrMsg); |
| 4016 | zErrMsg = 0; |
| 4017 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4018 | if( xForEach ){ |
| 4019 | xForEach(p, newDb, (const char*)zName); |
| 4020 | } |
| 4021 | printf("done\n"); |
| 4022 | } |
| 4023 | if( rc!=SQLITE_DONE ){ |
| 4024 | sqlite3_finalize(pQuery); |
| 4025 | sqlite3_free(zQuery); |
| 4026 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4027 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4028 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4029 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4030 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4031 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4032 | zQuery); |
| 4033 | goto end_schema_xfer; |
| 4034 | } |
| 4035 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4036 | zName = sqlite3_column_text(pQuery, 0); |
| 4037 | zSql = sqlite3_column_text(pQuery, 1); |
| 4038 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4039 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4040 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4041 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4042 | sqlite3_free(zErrMsg); |
| 4043 | zErrMsg = 0; |
| 4044 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4045 | if( xForEach ){ |
| 4046 | xForEach(p, newDb, (const char*)zName); |
| 4047 | } |
| 4048 | printf("done\n"); |
| 4049 | } |
| 4050 | } |
| 4051 | end_schema_xfer: |
| 4052 | sqlite3_finalize(pQuery); |
| 4053 | sqlite3_free(zQuery); |
| 4054 | } |
| 4055 | |
| 4056 | /* |
| 4057 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4058 | ** as possible out of the main database (which might be corrupt) and write it |
| 4059 | ** into zNewDb. |
| 4060 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4061 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4062 | int rc; |
| 4063 | sqlite3 *newDb = 0; |
| 4064 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4065 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4066 | return; |
| 4067 | } |
| 4068 | rc = sqlite3_open(zNewDb, &newDb); |
| 4069 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4070 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4071 | sqlite3_errmsg(newDb)); |
| 4072 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4073 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4074 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4075 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4076 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4077 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4078 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4079 | } |
| 4080 | sqlite3_close(newDb); |
| 4081 | } |
| 4082 | |
| 4083 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4084 | ** Change the output file back to stdout |
| 4085 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4086 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4087 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4088 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4089 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4090 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4091 | }else{ |
| 4092 | output_file_close(p->out); |
| 4093 | } |
| 4094 | p->outfile[0] = 0; |
| 4095 | p->out = stdout; |
| 4096 | } |
| 4097 | |
| 4098 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4099 | ** Run an SQL command and return the single integer result. |
| 4100 | */ |
| 4101 | static int db_int(ShellState *p, const char *zSql){ |
| 4102 | sqlite3_stmt *pStmt; |
| 4103 | int res = 0; |
| 4104 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4105 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4106 | res = sqlite3_column_int(pStmt,0); |
| 4107 | } |
| 4108 | sqlite3_finalize(pStmt); |
| 4109 | return res; |
| 4110 | } |
| 4111 | |
| 4112 | /* |
| 4113 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4114 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4115 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4116 | return (a[0]<<8) + a[1]; |
| 4117 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4118 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4119 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4120 | } |
| 4121 | |
| 4122 | /* |
| 4123 | ** Implementation of the ".info" command. |
| 4124 | ** |
| 4125 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4126 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4127 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4128 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4129 | { "file change counter:", 24 }, |
| 4130 | { "database page count:", 28 }, |
| 4131 | { "freelist page count:", 36 }, |
| 4132 | { "schema cookie:", 40 }, |
| 4133 | { "schema format:", 44 }, |
| 4134 | { "default cache size:", 48 }, |
| 4135 | { "autovacuum top root:", 52 }, |
| 4136 | { "incremental vacuum:", 64 }, |
| 4137 | { "text encoding:", 56 }, |
| 4138 | { "user version:", 60 }, |
| 4139 | { "application id:", 68 }, |
| 4140 | { "software version:", 96 }, |
| 4141 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4142 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4143 | { "number of tables:", |
| 4144 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4145 | { "number of indexes:", |
| 4146 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4147 | { "number of triggers:", |
| 4148 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4149 | { "number of views:", |
| 4150 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4151 | { "schema size:", |
| 4152 | "SELECT total(length(sql)) FROM %s" }, |
| 4153 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 4154 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4155 | int i; |
| 4156 | char *zSchemaTab; |
| 4157 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 4158 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4159 | open_db(p, 0); |
| 4160 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4161 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4162 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 4163 | return 1; |
| 4164 | } |
| 4165 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 4166 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4167 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4168 | return 1; |
| 4169 | } |
| 4170 | i = get2byteInt(aHdr+16); |
| 4171 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4172 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4173 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 4174 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 4175 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4176 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4177 | int ofst = aField[i].ofst; |
| 4178 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4179 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4180 | switch( ofst ){ |
| 4181 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4182 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 4183 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 4184 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4185 | } |
| 4186 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4187 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4188 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4189 | if( zDb==0 ){ |
| 4190 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 4191 | }else if( strcmp(zDb,"temp")==0 ){ |
| 4192 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 4193 | }else{ |
| 4194 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 4195 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 4196 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4197 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 4198 | int val = db_int(p, zSql); |
| 4199 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4200 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4201 | } |
| 4202 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4203 | return 0; |
| 4204 | } |
| 4205 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4206 | /* |
| 4207 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 4208 | */ |
| 4209 | static int shellDatabaseError(sqlite3 *db){ |
| 4210 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4211 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4212 | return 1; |
| 4213 | } |
| 4214 | |
| 4215 | /* |
| 4216 | ** Print an out-of-memory message to stderr and return 1. |
| 4217 | */ |
| 4218 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4219 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 4220 | return 1; |
| 4221 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4222 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4223 | /* |
| 4224 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 4225 | ** if they match and FALSE (0) if they do not match. |
| 4226 | ** |
| 4227 | ** Globbing rules: |
| 4228 | ** |
| 4229 | ** '*' Matches any sequence of zero or more characters. |
| 4230 | ** |
| 4231 | ** '?' Matches exactly one character. |
| 4232 | ** |
| 4233 | ** [...] Matches one character from the enclosed list of |
| 4234 | ** characters. |
| 4235 | ** |
| 4236 | ** [^...] Matches one character not in the enclosed list. |
| 4237 | ** |
| 4238 | ** '#' Matches any sequence of one or more digits with an |
| 4239 | ** optional + or - sign in front |
| 4240 | ** |
| 4241 | ** ' ' Any span of whitespace matches any other span of |
| 4242 | ** whitespace. |
| 4243 | ** |
| 4244 | ** Extra whitespace at the end of z[] is ignored. |
| 4245 | */ |
| 4246 | static int testcase_glob(const char *zGlob, const char *z){ |
| 4247 | int c, c2; |
| 4248 | int invert; |
| 4249 | int seen; |
| 4250 | |
| 4251 | while( (c = (*(zGlob++)))!=0 ){ |
| 4252 | if( IsSpace(c) ){ |
| 4253 | if( !IsSpace(*z) ) return 0; |
| 4254 | while( IsSpace(*zGlob) ) zGlob++; |
| 4255 | while( IsSpace(*z) ) z++; |
| 4256 | }else if( c=='*' ){ |
| 4257 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 4258 | if( c=='?' && (*(z++))==0 ) return 0; |
| 4259 | } |
| 4260 | if( c==0 ){ |
| 4261 | return 1; |
| 4262 | }else if( c=='[' ){ |
| 4263 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 4264 | z++; |
| 4265 | } |
| 4266 | return (*z)!=0; |
| 4267 | } |
| 4268 | while( (c2 = (*(z++)))!=0 ){ |
| 4269 | while( c2!=c ){ |
| 4270 | c2 = *(z++); |
| 4271 | if( c2==0 ) return 0; |
| 4272 | } |
| 4273 | if( testcase_glob(zGlob,z) ) return 1; |
| 4274 | } |
| 4275 | return 0; |
| 4276 | }else if( c=='?' ){ |
| 4277 | if( (*(z++))==0 ) return 0; |
| 4278 | }else if( c=='[' ){ |
| 4279 | int prior_c = 0; |
| 4280 | seen = 0; |
| 4281 | invert = 0; |
| 4282 | c = *(z++); |
| 4283 | if( c==0 ) return 0; |
| 4284 | c2 = *(zGlob++); |
| 4285 | if( c2=='^' ){ |
| 4286 | invert = 1; |
| 4287 | c2 = *(zGlob++); |
| 4288 | } |
| 4289 | if( c2==']' ){ |
| 4290 | if( c==']' ) seen = 1; |
| 4291 | c2 = *(zGlob++); |
| 4292 | } |
| 4293 | while( c2 && c2!=']' ){ |
| 4294 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 4295 | c2 = *(zGlob++); |
| 4296 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 4297 | prior_c = 0; |
| 4298 | }else{ |
| 4299 | if( c==c2 ){ |
| 4300 | seen = 1; |
| 4301 | } |
| 4302 | prior_c = c2; |
| 4303 | } |
| 4304 | c2 = *(zGlob++); |
| 4305 | } |
| 4306 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 4307 | }else if( c=='#' ){ |
| 4308 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 4309 | if( !IsDigit(z[0]) ) return 0; |
| 4310 | z++; |
| 4311 | while( IsDigit(z[0]) ){ z++; } |
| 4312 | }else{ |
| 4313 | if( c!=(*(z++)) ) return 0; |
| 4314 | } |
| 4315 | } |
| 4316 | while( IsSpace(*z) ){ z++; } |
| 4317 | return *z==0; |
| 4318 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4319 | |
| 4320 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4321 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4322 | ** Compare the string as a command-line option with either one or two |
| 4323 | ** initial "-" characters. |
| 4324 | */ |
| 4325 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 4326 | if( zStr[0]!='-' ) return 0; |
| 4327 | zStr++; |
| 4328 | if( zStr[0]=='-' ) zStr++; |
| 4329 | return strcmp(zStr, zOpt)==0; |
| 4330 | } |
| 4331 | |
| 4332 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4333 | ** Delete a file. |
| 4334 | */ |
| 4335 | int shellDeleteFile(const char *zFilename){ |
| 4336 | int rc; |
| 4337 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4338 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4339 | rc = _wunlink(z); |
| 4340 | sqlite3_free(z); |
| 4341 | #else |
| 4342 | rc = unlink(zFilename); |
| 4343 | #endif |
| 4344 | return rc; |
| 4345 | } |
| 4346 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4347 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4348 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4349 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4350 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4351 | ** called with four arguments - the parent table name, the parent column name, |
| 4352 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4353 | ** |
| 4354 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 4355 | ** |
| 4356 | ** If either of the named tables or columns do not exist, this function |
| 4357 | ** returns an empty string. An empty string is also returned if both tables |
| 4358 | ** and columns exist but have the same default collation sequence. Or, |
| 4359 | ** if both exist but the default collation sequences are different, this |
| 4360 | ** function returns the string " COLLATE <parent-collation>", where |
| 4361 | ** <parent-collation> is the default collation sequence of the parent column. |
| 4362 | */ |
| 4363 | static void shellFkeyCollateClause( |
| 4364 | sqlite3_context *pCtx, |
| 4365 | int nVal, |
| 4366 | sqlite3_value **apVal |
| 4367 | ){ |
| 4368 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 4369 | const char *zParent; |
| 4370 | const char *zParentCol; |
| 4371 | const char *zParentSeq; |
| 4372 | const char *zChild; |
| 4373 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4374 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4375 | int rc; |
| 4376 | |
| 4377 | assert( nVal==4 ); |
| 4378 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 4379 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 4380 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 4381 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 4382 | |
| 4383 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 4384 | rc = sqlite3_table_column_metadata( |
| 4385 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 4386 | ); |
| 4387 | if( rc==SQLITE_OK ){ |
| 4388 | rc = sqlite3_table_column_metadata( |
| 4389 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 4390 | ); |
| 4391 | } |
| 4392 | |
| 4393 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 4394 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 4395 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 4396 | sqlite3_free(z); |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | |
| 4401 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4402 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4403 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4404 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4405 | ShellState *pState, /* Current shell tool state */ |
| 4406 | char **azArg, /* Array of arguments passed to dot command */ |
| 4407 | int nArg /* Number of entries in azArg[] */ |
| 4408 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4409 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 4410 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4411 | int bVerbose = 0; /* If -verbose is present */ |
| 4412 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4413 | int i; /* To iterate through azArg[] */ |
| 4414 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 4415 | int rc; /* Return code */ |
| 4416 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4417 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4418 | /* |
| 4419 | ** This SELECT statement returns one row for each foreign key constraint |
| 4420 | ** in the schema of the main database. The column values are: |
| 4421 | ** |
| 4422 | ** 0. The text of an SQL statement similar to: |
| 4423 | ** |
| 4424 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 4425 | ** |
| 4426 | ** This is the same SELECT that the foreign keys implementation needs |
| 4427 | ** to run internally on child tables. If there is an index that can |
| 4428 | ** be used to optimize this query, then it can also be used by the FK |
| 4429 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 4430 | ** table. |
| 4431 | ** |
| 4432 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 4433 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 4434 | ** contains an index that can be used to optimize the query. |
| 4435 | ** |
| 4436 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 4437 | ** |
| 4438 | ** "child_table(child_key1, child_key2)" |
| 4439 | ** |
| 4440 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 4441 | ** |
| 4442 | ** "parent_table(parent_key1, parent_key2)" |
| 4443 | ** |
| 4444 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 4445 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 4446 | ** |
| 4447 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 4448 | ** |
| 4449 | ** 5. The name of the parent table. |
| 4450 | ** |
| 4451 | ** These six values are used by the C logic below to generate the report. |
| 4452 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4453 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4454 | "SELECT " |
| 4455 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 4456 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4457 | " || fkey_collate_clause(" |
| 4458 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4459 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4460 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4461 | " || group_concat('*=?', ' AND ') || ')'" |
| 4462 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4463 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4464 | ", " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4465 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4466 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4467 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 4468 | " || ' ON ' || quote(s.name) || '('" |
| 4469 | " || group_concat(quote(f.[from]) ||" |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4470 | " fkey_collate_clause(" |
| 4471 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4472 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4473 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4474 | " f.[table] " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4475 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 4476 | "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4477 | "GROUP BY s.name, f.id " |
| 4478 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4479 | ; |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4480 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4481 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4482 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 4483 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4484 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 4485 | bVerbose = 1; |
| 4486 | } |
| 4487 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 4488 | bGroupByParent = 1; |
| 4489 | zIndent = " "; |
| 4490 | } |
| 4491 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4492 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 4493 | azArg[0], azArg[1] |
| 4494 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4495 | return SQLITE_ERROR; |
| 4496 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4497 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4498 | |
| 4499 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 4500 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 4501 | 0, shellFkeyCollateClause, 0, 0 |
| 4502 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4503 | |
| 4504 | |
| 4505 | if( rc==SQLITE_OK ){ |
| 4506 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 4507 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4508 | if( rc==SQLITE_OK ){ |
| 4509 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 4510 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4511 | |
| 4512 | if( rc==SQLITE_OK ){ |
| 4513 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4514 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4515 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 4516 | int res = -1; |
| 4517 | sqlite3_stmt *pExplain = 0; |
| 4518 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 4519 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 4520 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 4521 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 4522 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4523 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4524 | |
| 4525 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 4526 | if( rc!=SQLITE_OK ) break; |
| 4527 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 4528 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 4529 | res = ( |
| 4530 | 0==sqlite3_strglob(zGlob, zPlan) |
| 4531 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 4532 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4533 | } |
| 4534 | rc = sqlite3_finalize(pExplain); |
| 4535 | if( rc!=SQLITE_OK ) break; |
| 4536 | |
| 4537 | if( res<0 ){ |
| 4538 | raw_printf(stderr, "Error: internal error"); |
| 4539 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4540 | }else{ |
| 4541 | if( bGroupByParent |
| 4542 | && (bVerbose || res==0) |
| 4543 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
| 4544 | ){ |
| 4545 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 4546 | sqlite3_free(zPrev); |
| 4547 | zPrev = sqlite3_mprintf("%s", zParent); |
| 4548 | } |
| 4549 | |
| 4550 | if( res==0 ){ |
| 4551 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 4552 | }else if( bVerbose ){ |
| 4553 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
| 4554 | zIndent, zFrom, zTarget |
| 4555 | ); |
| 4556 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4557 | } |
| 4558 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 4559 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4560 | |
| 4561 | if( rc!=SQLITE_OK ){ |
| 4562 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4563 | } |
| 4564 | |
| 4565 | rc2 = sqlite3_finalize(pSql); |
| 4566 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 4567 | rc = rc2; |
| 4568 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4569 | } |
| 4570 | }else{ |
| 4571 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 4572 | } |
| 4573 | |
| 4574 | return rc; |
| 4575 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4576 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4577 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4578 | ** Implementation of ".lint" dot command. |
| 4579 | */ |
| 4580 | static int lintDotCommand( |
| 4581 | ShellState *pState, /* Current shell tool state */ |
| 4582 | char **azArg, /* Array of arguments passed to dot command */ |
| 4583 | int nArg /* Number of entries in azArg[] */ |
| 4584 | ){ |
| 4585 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 4586 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 4587 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 4588 | return lintFkeyIndexes(pState, azArg, nArg); |
| 4589 | |
| 4590 | usage: |
| 4591 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 4592 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 4593 | raw_printf(stderr, " fkey-indexes\n"); |
| 4594 | return SQLITE_ERROR; |
| 4595 | } |
| 4596 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 4597 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 4598 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4599 | ** If an input line begins with "." then invoke this routine to |
| 4600 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4601 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4602 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4603 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4604 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4605 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4606 | int nArg = 0; |
| 4607 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 4608 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4609 | char *azArg[50]; |
| 4610 | |
| 4611 | /* Parse the input line into tokens. |
| 4612 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4613 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 4614 | while( IsSpace(zLine[h]) ){ h++; } |
| 4615 | if( zLine[h]==0 ) break; |
| 4616 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 4617 | int delim = zLine[h++]; |
| 4618 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4619 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4620 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4621 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4622 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4623 | if( zLine[h]==delim ){ |
| 4624 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4625 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4626 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4627 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4628 | azArg[nArg++] = &zLine[h]; |
| 4629 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 4630 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4631 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | /* Process the input line. |
| 4636 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4637 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4638 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4639 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4640 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4641 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4642 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 4643 | if( nArg!=2 ){ |
| 4644 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 4645 | rc = 1; |
| 4646 | goto meta_command_exit; |
| 4647 | } |
| 4648 | open_db(p, 0); |
| 4649 | if( booleanValue(azArg[1]) ){ |
| 4650 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 4651 | }else{ |
| 4652 | sqlite3_set_authorizer(p->db, 0, 0); |
| 4653 | } |
| 4654 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4655 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4656 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4657 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 4658 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 4659 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4660 | const char *zDestFile = 0; |
| 4661 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4662 | sqlite3 *pDest; |
| 4663 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4664 | int j; |
| 4665 | for(j=1; j<nArg; j++){ |
| 4666 | const char *z = azArg[j]; |
| 4667 | if( z[0]=='-' ){ |
| 4668 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 4669 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4670 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4671 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4672 | return 1; |
| 4673 | } |
| 4674 | }else if( zDestFile==0 ){ |
| 4675 | zDestFile = azArg[j]; |
| 4676 | }else if( zDb==0 ){ |
| 4677 | zDb = zDestFile; |
| 4678 | zDestFile = azArg[j]; |
| 4679 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4680 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4681 | return 1; |
| 4682 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4683 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4684 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4685 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 4686 | return 1; |
| 4687 | } |
| 4688 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4689 | rc = sqlite3_open(zDestFile, &pDest); |
| 4690 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4691 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4692 | sqlite3_close(pDest); |
| 4693 | return 1; |
| 4694 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4695 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4696 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 4697 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4698 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4699 | sqlite3_close(pDest); |
| 4700 | return 1; |
| 4701 | } |
| 4702 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 4703 | sqlite3_backup_finish(pBackup); |
| 4704 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4705 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4706 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4707 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4708 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4709 | } |
| 4710 | sqlite3_close(pDest); |
| 4711 | }else |
| 4712 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4713 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 4714 | if( nArg==2 ){ |
| 4715 | bail_on_error = booleanValue(azArg[1]); |
| 4716 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4717 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4718 | rc = 1; |
| 4719 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 4720 | }else |
| 4721 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4722 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 4723 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4724 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4725 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4726 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4727 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 4728 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4729 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4730 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4731 | rc = 1; |
| 4732 | } |
| 4733 | }else |
| 4734 | |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame^] | 4735 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 4736 | if( nArg==2 ){ |
| 4737 | #if defined(_WIN32) || defined(WIN32) |
| 4738 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 4739 | rc = !SetCurrentDirectoryW(z); |
| 4740 | sqlite3_free(z); |
| 4741 | #else |
| 4742 | rc = chdir(azArg[1]); |
| 4743 | #endif |
| 4744 | if( rc ){ |
| 4745 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 4746 | rc = 1; |
| 4747 | } |
| 4748 | }else{ |
| 4749 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 4750 | rc = 1; |
| 4751 | } |
| 4752 | }else |
| 4753 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4754 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 4755 | ** routine named test_breakpoint(). |
| 4756 | */ |
| 4757 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 4758 | test_breakpoint(); |
| 4759 | }else |
| 4760 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4761 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 4762 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4763 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4764 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4765 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4766 | rc = 1; |
| 4767 | } |
| 4768 | }else |
| 4769 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4770 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 4771 | ** Then read the content of the testcase-out.txt file and compare against |
| 4772 | ** azArg[1]. If there are differences, report an error and exit. |
| 4773 | */ |
| 4774 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 4775 | char *zRes = 0; |
| 4776 | output_reset(p); |
| 4777 | if( nArg!=2 ){ |
| 4778 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4779 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4780 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4781 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 4782 | rc = 2; |
| 4783 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4784 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4785 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 4786 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4787 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4788 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 4789 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4790 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4791 | } |
| 4792 | sqlite3_free(zRes); |
| 4793 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4794 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4795 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 4796 | if( nArg==2 ){ |
| 4797 | tryToClone(p, azArg[1]); |
| 4798 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4799 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4800 | rc = 1; |
| 4801 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4802 | }else |
| 4803 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4804 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4805 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4806 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4807 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4808 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4809 | data.showHeader = 0; |
| 4810 | data.cMode = data.mode = MODE_List; |
| 4811 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 4812 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 4813 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 4814 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 4815 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4816 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 4817 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 4818 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4819 | } |
| 4820 | }else |
| 4821 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4822 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 4823 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 4824 | }else |
| 4825 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4826 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4827 | const char *zLike = 0; |
| 4828 | int i; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4829 | int savedShowHeader = p->showHeader; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4830 | ShellClearFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4831 | for(i=1; i<nArg; i++){ |
| 4832 | if( azArg[i][0]=='-' ){ |
| 4833 | const char *z = azArg[i]+1; |
| 4834 | if( z[0]=='-' ) z++; |
| 4835 | if( strcmp(z,"preserve-rowids")==0 ){ |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4836 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 4837 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 4838 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 4839 | rc = 1; |
| 4840 | goto meta_command_exit; |
| 4841 | #else |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4842 | ShellSetFlag(p, SHFLG_PreserveRowid); |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 4843 | #endif |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4844 | }else |
| 4845 | { |
| 4846 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4847 | rc = 1; |
| 4848 | goto meta_command_exit; |
| 4849 | } |
| 4850 | }else if( zLike ){ |
| 4851 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 4852 | rc = 1; |
| 4853 | goto meta_command_exit; |
| 4854 | }else{ |
| 4855 | zLike = azArg[i]; |
| 4856 | } |
| 4857 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4858 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 4859 | /* When playing back a "dump", the content might appear in an order |
| 4860 | ** which causes immediate foreign key constraints to be violated. |
| 4861 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4862 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 4863 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4864 | p->writableSchema = 0; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4865 | p->showHeader = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4866 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 4867 | ** as much of the schema as it can even if the sqlite_master table is |
| 4868 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4869 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4870 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4871 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4872 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4873 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4874 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4875 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4876 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 4877 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4878 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4879 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4880 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 4881 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 4882 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 4883 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4884 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 4885 | char *zSql; |
| 4886 | zSql = sqlite3_mprintf( |
| 4887 | "SELECT name, type, sql FROM sqlite_master " |
| 4888 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 4889 | " AND sql NOT NULL", zLike); |
| 4890 | run_schema_dump_query(p,zSql); |
| 4891 | sqlite3_free(zSql); |
| 4892 | zSql = sqlite3_mprintf( |
| 4893 | "SELECT sql FROM sqlite_master " |
| 4894 | "WHERE sql NOT NULL" |
| 4895 | " AND type IN ('index','trigger','view')" |
| 4896 | " AND tbl_name LIKE %Q", zLike); |
| 4897 | run_table_dump_query(p, zSql, 0); |
| 4898 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4899 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4900 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4901 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 4902 | p->writableSchema = 0; |
| 4903 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 4904 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 4905 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4906 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 4907 | p->showHeader = savedShowHeader; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 4908 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4909 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4910 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 4911 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4912 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4913 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4914 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4915 | rc = 1; |
| 4916 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4917 | }else |
| 4918 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4919 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 4920 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4921 | if( strcmp(azArg[1],"full")==0 ){ |
| 4922 | p->autoEQP = 2; |
| 4923 | }else{ |
| 4924 | p->autoEQP = booleanValue(azArg[1]); |
| 4925 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4926 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4927 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4928 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4929 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 4930 | }else |
| 4931 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 4932 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4933 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 4934 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4935 | }else |
| 4936 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4937 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4938 | int val = 1; |
| 4939 | if( nArg>=2 ){ |
| 4940 | if( strcmp(azArg[1],"auto")==0 ){ |
| 4941 | val = 99; |
| 4942 | }else{ |
| 4943 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4944 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4945 | } |
| 4946 | if( val==1 && p->mode!=MODE_Explain ){ |
| 4947 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 4948 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 4949 | p->autoExplain = 0; |
| 4950 | }else if( val==0 ){ |
| 4951 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4952 | p->autoExplain = 0; |
| 4953 | }else if( val==99 ){ |
| 4954 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 4955 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4956 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4957 | }else |
| 4958 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4959 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4960 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4961 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4962 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4963 | memcpy(&data, p, sizeof(data)); |
| 4964 | data.showHeader = 0; |
| 4965 | data.cMode = data.mode = MODE_Semi; |
| 4966 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 4967 | data.cMode = data.mode = MODE_Pretty; |
| 4968 | nArg = 1; |
| 4969 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4970 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4971 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4972 | rc = 1; |
| 4973 | goto meta_command_exit; |
| 4974 | } |
| 4975 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4976 | rc = sqlite3_exec(p->db, |
| 4977 | "SELECT sql FROM" |
| 4978 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4979 | " FROM sqlite_master UNION ALL" |
| 4980 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4981 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 4982 | "ORDER BY rowid", |
| 4983 | callback, &data, &zErrMsg |
| 4984 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4985 | if( rc==SQLITE_OK ){ |
| 4986 | sqlite3_stmt *pStmt; |
| 4987 | rc = sqlite3_prepare_v2(p->db, |
| 4988 | "SELECT rowid FROM sqlite_master" |
| 4989 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 4990 | -1, &pStmt, 0); |
| 4991 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 4992 | sqlite3_finalize(pStmt); |
| 4993 | } |
| 4994 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4995 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4996 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4997 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 4998 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 4999 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5000 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5001 | data.zDestTable = "sqlite_stat1"; |
| 5002 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 5003 | shell_callback, &data,&zErrMsg); |
| 5004 | data.zDestTable = "sqlite_stat3"; |
| 5005 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 5006 | shell_callback, &data,&zErrMsg); |
| 5007 | data.zDestTable = "sqlite_stat4"; |
| 5008 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 5009 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5010 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5011 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5012 | }else |
| 5013 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5014 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 5015 | if( nArg==2 ){ |
| 5016 | p->showHeader = booleanValue(azArg[1]); |
| 5017 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5018 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5019 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 5020 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5021 | }else |
| 5022 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5023 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5024 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5025 | }else |
| 5026 | |
| 5027 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5028 | char *zTable; /* Insert data into this table */ |
| 5029 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5030 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5031 | int nCol; /* Number of columns in the table */ |
| 5032 | int nByte; /* Number of bytes in an SQL string */ |
| 5033 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 5034 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5035 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5036 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5037 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 5038 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 5039 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5040 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5041 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5042 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5043 | goto meta_command_exit; |
| 5044 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5045 | zFile = azArg[1]; |
| 5046 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5047 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5048 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5049 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5050 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5051 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5052 | raw_printf(stderr, |
| 5053 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5054 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5055 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5056 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5057 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5058 | " for import\n"); |
| 5059 | return 1; |
| 5060 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5061 | nSep = strlen30(p->rowSeparator); |
| 5062 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5063 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5064 | return 1; |
| 5065 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5066 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 5067 | /* When importing CSV (only), if the row separator is set to the |
| 5068 | ** default output row separator, change it to the default input |
| 5069 | ** row separator. This avoids having to maintain different input |
| 5070 | ** and output row separators. */ |
| 5071 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 5072 | nSep = strlen30(p->rowSeparator); |
| 5073 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5074 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5075 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5076 | " for import\n"); |
| 5077 | return 1; |
| 5078 | } |
| 5079 | sCtx.zFile = zFile; |
| 5080 | sCtx.nLine = 1; |
| 5081 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5082 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5083 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5084 | return 1; |
| 5085 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5086 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 5087 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5088 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5089 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5090 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5091 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5092 | xCloser = fclose; |
| 5093 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5094 | if( p->mode==MODE_Ascii ){ |
| 5095 | xRead = ascii_read_one_field; |
| 5096 | }else{ |
| 5097 | xRead = csv_read_one_field; |
| 5098 | } |
| 5099 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5100 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5101 | return 1; |
| 5102 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5103 | sCtx.cColSep = p->colSeparator[0]; |
| 5104 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 5105 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5106 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5107 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5108 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5109 | return 1; |
| 5110 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5111 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5112 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5113 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5114 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5115 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 5116 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5117 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 5118 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5119 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5120 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5121 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5122 | if( cSep=='(' ){ |
| 5123 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5124 | sqlite3_free(sCtx.z); |
| 5125 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5126 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5127 | return 1; |
| 5128 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5129 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 5130 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 5131 | sqlite3_free(zCreate); |
| 5132 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5133 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5134 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5135 | sqlite3_free(sCtx.z); |
| 5136 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5137 | return 1; |
| 5138 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5139 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5140 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5141 | sqlite3_free(zSql); |
| 5142 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5143 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5144 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5145 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5146 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5147 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5148 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5149 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5150 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5151 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5152 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5153 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5154 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5155 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5156 | return 1; |
| 5157 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5158 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5159 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5160 | for(i=1; i<nCol; i++){ |
| 5161 | zSql[j++] = ','; |
| 5162 | zSql[j++] = '?'; |
| 5163 | } |
| 5164 | zSql[j++] = ')'; |
| 5165 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5166 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5167 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5168 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5169 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5170 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5171 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5172 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5173 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5174 | needCommit = sqlite3_get_autocommit(p->db); |
| 5175 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5176 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5177 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5178 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5179 | char *z = xRead(&sCtx); |
| 5180 | /* |
| 5181 | ** Did we reach end-of-file before finding any columns? |
| 5182 | ** If so, stop instead of NULL filling the remaining columns. |
| 5183 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5184 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5185 | /* |
| 5186 | ** Did we reach end-of-file OR end-of-line before finding any |
| 5187 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 5188 | ** the remaining columns. |
| 5189 | */ |
| 5190 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5191 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5192 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5193 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5194 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5195 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 5196 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 5197 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 5198 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5199 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5200 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5201 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5202 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5203 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5204 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5205 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5206 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5207 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5208 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5209 | if( i>=nCol ){ |
| 5210 | sqlite3_step(pStmt); |
| 5211 | rc = sqlite3_reset(pStmt); |
| 5212 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5213 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 5214 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5215 | } |
| 5216 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5217 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5218 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5219 | xCloser(sCtx.in); |
| 5220 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5221 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5222 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5223 | }else |
| 5224 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 5225 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5226 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 5227 | char *zSql; |
| 5228 | char *zCollist = 0; |
| 5229 | sqlite3_stmt *pStmt; |
| 5230 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5231 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5232 | if( nArg!=3 ){ |
| 5233 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 5234 | rc = 1; |
| 5235 | goto meta_command_exit; |
| 5236 | } |
| 5237 | open_db(p, 0); |
| 5238 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 5239 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 5240 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5241 | sqlite3_free(zSql); |
| 5242 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5243 | tnum = sqlite3_column_int(pStmt, 0); |
| 5244 | } |
| 5245 | sqlite3_finalize(pStmt); |
| 5246 | if( tnum==0 ){ |
| 5247 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 5248 | rc = 1; |
| 5249 | goto meta_command_exit; |
| 5250 | } |
| 5251 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 5252 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 5253 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5254 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5255 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5256 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5257 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 5258 | i++; |
| 5259 | if( zCol==0 ){ |
| 5260 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 5261 | zCol = "_ROWID_"; |
| 5262 | }else{ |
| 5263 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 5264 | zCol = zLabel; |
| 5265 | } |
| 5266 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5267 | if( zCollist==0 ){ |
| 5268 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 5269 | }else{ |
| 5270 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 5271 | } |
| 5272 | } |
| 5273 | sqlite3_finalize(pStmt); |
| 5274 | zSql = sqlite3_mprintf( |
| 5275 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 5276 | azArg[2], zCollist, zCollist); |
| 5277 | sqlite3_free(zCollist); |
| 5278 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 5279 | if( rc==SQLITE_OK ){ |
| 5280 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 5281 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 5282 | if( rc ){ |
| 5283 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 5284 | }else{ |
| 5285 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5286 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5287 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 5288 | ); |
| 5289 | } |
| 5290 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 5291 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5292 | rc = 1; |
| 5293 | } |
| 5294 | sqlite3_free(zSql); |
| 5295 | }else |
| 5296 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 5297 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5298 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5299 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 5300 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5301 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 5302 | iotrace = 0; |
| 5303 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5304 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5305 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5306 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5307 | iotrace = stdout; |
| 5308 | }else{ |
| 5309 | iotrace = fopen(azArg[1], "w"); |
| 5310 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5311 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5312 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5313 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5314 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5315 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 5319 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 5320 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5321 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 5322 | static const struct { |
| 5323 | const char *zLimitName; /* Name of a limit */ |
| 5324 | int limitCode; /* Integer code for that limit */ |
| 5325 | } aLimit[] = { |
| 5326 | { "length", SQLITE_LIMIT_LENGTH }, |
| 5327 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 5328 | { "column", SQLITE_LIMIT_COLUMN }, |
| 5329 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5330 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5331 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 5332 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5333 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 5334 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5335 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 5336 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 5337 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 5338 | }; |
| 5339 | int i, n2; |
| 5340 | open_db(p, 0); |
| 5341 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5342 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5343 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5344 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 5345 | } |
| 5346 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5347 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5348 | rc = 1; |
| 5349 | goto meta_command_exit; |
| 5350 | }else{ |
| 5351 | int iLimit = -1; |
| 5352 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5353 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5354 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 5355 | if( iLimit<0 ){ |
| 5356 | iLimit = i; |
| 5357 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5358 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5359 | rc = 1; |
| 5360 | goto meta_command_exit; |
| 5361 | } |
| 5362 | } |
| 5363 | } |
| 5364 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5365 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5366 | "enter \".limits\" with no arguments for a list.\n", |
| 5367 | azArg[1]); |
| 5368 | rc = 1; |
| 5369 | goto meta_command_exit; |
| 5370 | } |
| 5371 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 5372 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 5373 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 5374 | } |
| 5375 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 5376 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 5377 | } |
| 5378 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 5379 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5380 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 5381 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5382 | lintDotCommand(p, azArg, nArg); |
| 5383 | }else |
| 5384 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5385 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5386 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5387 | const char *zFile, *zProc; |
| 5388 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5389 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5390 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5391 | rc = 1; |
| 5392 | goto meta_command_exit; |
| 5393 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5394 | zFile = azArg[1]; |
| 5395 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5396 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5397 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 5398 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5399 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5400 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5401 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5402 | } |
| 5403 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 5404 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 5405 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5406 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 5407 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5408 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5409 | rc = 1; |
| 5410 | }else{ |
| 5411 | const char *zFile = azArg[1]; |
| 5412 | output_file_close(p->pLog); |
| 5413 | p->pLog = output_file_open(zFile); |
| 5414 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 5415 | }else |
| 5416 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5417 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 5418 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 5419 | int n2 = (int)strlen(zMode); |
| 5420 | int c2 = zMode[0]; |
| 5421 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5422 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5423 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5424 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5425 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5426 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5427 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5428 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5429 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 5430 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5431 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 5432 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5433 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5434 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5435 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 5436 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5437 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 5438 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5439 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5440 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5441 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5442 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5443 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5444 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 5445 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5446 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 5447 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 5448 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5449 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5450 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 5451 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5452 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5453 | }else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5454 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 5455 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5456 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5457 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5458 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5459 | }else |
| 5460 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5461 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 5462 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 5463 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 5464 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5465 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5466 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 5467 | rc = 1; |
| 5468 | } |
| 5469 | }else |
| 5470 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5471 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5472 | char *zNewFilename; /* Name of the database file to open */ |
| 5473 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5474 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5475 | /* Close the existing database */ |
| 5476 | session_close_all(p); |
| 5477 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5478 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 5479 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5480 | sqlite3_free(p->zFreeOnClose); |
| 5481 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5482 | /* Check for command-line arguments */ |
| 5483 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 5484 | const char *z = azArg[iName]; |
| 5485 | if( optionMatch(z,"new") ){ |
| 5486 | newFlag = 1; |
| 5487 | }else if( z[0]=='-' ){ |
| 5488 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 5489 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5490 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5491 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5492 | } |
| 5493 | /* If a filename is specified, try to open it first */ |
| 5494 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 5495 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5496 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5497 | p->zDbFilename = zNewFilename; |
| 5498 | open_db(p, 1); |
| 5499 | if( p->db==0 ){ |
| 5500 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 5501 | sqlite3_free(zNewFilename); |
| 5502 | }else{ |
| 5503 | p->zFreeOnClose = zNewFilename; |
| 5504 | } |
| 5505 | } |
| 5506 | if( p->db==0 ){ |
| 5507 | /* As a fall-back open a TEMP database */ |
| 5508 | p->zDbFilename = 0; |
| 5509 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5510 | } |
| 5511 | }else |
| 5512 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5513 | if( c=='o' |
| 5514 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 5515 | ){ |
| 5516 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 5517 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5518 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5519 | rc = 1; |
| 5520 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5521 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5522 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 5523 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5524 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5525 | rc = 1; |
| 5526 | goto meta_command_exit; |
| 5527 | } |
| 5528 | p->outCount = 2; |
| 5529 | }else{ |
| 5530 | p->outCount = 0; |
| 5531 | } |
| 5532 | output_reset(p); |
| 5533 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5534 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5535 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5536 | rc = 1; |
| 5537 | p->out = stdout; |
| 5538 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5539 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5540 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5541 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5542 | p->out = stdout; |
| 5543 | rc = 1; |
| 5544 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5545 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 5546 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5547 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5548 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5549 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5550 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5551 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5552 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 5553 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5554 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5555 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5556 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5557 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5558 | } |
| 5559 | } |
| 5560 | }else |
| 5561 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5562 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 5563 | int i; |
| 5564 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5565 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5566 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5567 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5568 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 5569 | }else |
| 5570 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5571 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5572 | if( nArg >= 2) { |
| 5573 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 5574 | } |
| 5575 | if( nArg >= 3) { |
| 5576 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 5577 | } |
| 5578 | }else |
| 5579 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5580 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5581 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5582 | }else |
| 5583 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5584 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 5585 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5586 | if( nArg!=2 ){ |
| 5587 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5588 | rc = 1; |
| 5589 | goto meta_command_exit; |
| 5590 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5591 | alt = fopen(azArg[1], "rb"); |
| 5592 | if( alt==0 ){ |
| 5593 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 5594 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5595 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 5596 | rc = process_input(p, alt); |
| 5597 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5598 | } |
| 5599 | }else |
| 5600 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5601 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5602 | const char *zSrcFile; |
| 5603 | const char *zDb; |
| 5604 | sqlite3 *pSrc; |
| 5605 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5606 | int nTimeout = 0; |
| 5607 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5608 | if( nArg==2 ){ |
| 5609 | zSrcFile = azArg[1]; |
| 5610 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5611 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5612 | zSrcFile = azArg[2]; |
| 5613 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5614 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5615 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5616 | rc = 1; |
| 5617 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5618 | } |
| 5619 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 5620 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5621 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5622 | sqlite3_close(pSrc); |
| 5623 | return 1; |
| 5624 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5625 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5626 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 5627 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5628 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5629 | sqlite3_close(pSrc); |
| 5630 | return 1; |
| 5631 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5632 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 5633 | || rc==SQLITE_BUSY ){ |
| 5634 | if( rc==SQLITE_BUSY ){ |
| 5635 | if( nTimeout++ >= 3 ) break; |
| 5636 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5637 | } |
| 5638 | } |
| 5639 | sqlite3_backup_finish(pBackup); |
| 5640 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5641 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 5642 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5643 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5644 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5645 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5646 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5647 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5648 | } |
| 5649 | sqlite3_close(pSrc); |
| 5650 | }else |
| 5651 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5652 | |
| 5653 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 5654 | if( nArg==2 ){ |
| 5655 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5656 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5657 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 5658 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5659 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5660 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 5661 | rc = 1; |
| 5662 | } |
| 5663 | }else |
| 5664 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5665 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5666 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5667 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5668 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5669 | memcpy(&data, p, sizeof(data)); |
| 5670 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5671 | data.cMode = data.mode = MODE_Semi; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5672 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 5673 | data.cMode = data.mode = MODE_Pretty; |
| 5674 | nArg--; |
| 5675 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 5676 | } |
| 5677 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5678 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 5679 | 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] | 5680 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5681 | char *new_argv[2], *new_colv[2]; |
| 5682 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 5683 | " type text,\n" |
| 5684 | " name text,\n" |
| 5685 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 5686 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5687 | " sql text\n" |
| 5688 | ")"; |
| 5689 | new_argv[1] = 0; |
| 5690 | new_colv[0] = "sql"; |
| 5691 | new_colv[1] = 0; |
| 5692 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5693 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 5694 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5695 | char *new_argv[2], *new_colv[2]; |
| 5696 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 5697 | " type text,\n" |
| 5698 | " name text,\n" |
| 5699 | " tbl_name text,\n" |
| 5700 | " rootpage integer,\n" |
| 5701 | " sql text\n" |
| 5702 | ")"; |
| 5703 | new_argv[1] = 0; |
| 5704 | new_colv[0] = "sql"; |
| 5705 | new_colv[1] = 0; |
| 5706 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5707 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5708 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5709 | char *zSql; |
| 5710 | zSql = sqlite3_mprintf( |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5711 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5712 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5713 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5714 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5715 | "WHERE lower(tbl_name) LIKE %Q" |
drh | 6ac7a58 | 2011-11-04 00:35:56 +0000 | [diff] [blame] | 5716 | " AND type!='meta' AND sql NOTNULL " |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5717 | "ORDER BY rowid", azArg[1]); |
| 5718 | rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg); |
| 5719 | sqlite3_free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5720 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5721 | }else if( nArg==1 ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5722 | rc = sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 5723 | "SELECT sql FROM " |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5724 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 5725 | " FROM sqlite_master UNION ALL" |
drh | ac43e98 | 2012-05-21 03:15:06 +0000 | [diff] [blame] | 5726 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5727 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | 1ba0029 | 2013-05-06 21:01:06 +0000 | [diff] [blame] | 5728 | "ORDER BY rowid", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5729 | callback, &data, &zErrMsg |
| 5730 | ); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5731 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5732 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5733 | rc = 1; |
| 5734 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5735 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5736 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5737 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5738 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5739 | rc = 1; |
| 5740 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5741 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5742 | rc = 1; |
| 5743 | }else{ |
| 5744 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5745 | } |
| 5746 | }else |
| 5747 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5748 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 5749 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 5750 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 5751 | }else |
| 5752 | #endif |
| 5753 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5754 | #if defined(SQLITE_ENABLE_SESSION) |
| 5755 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 5756 | OpenSession *pSession = &p->aSession[0]; |
| 5757 | char **azCmd = &azArg[1]; |
| 5758 | int iSes = 0; |
| 5759 | int nCmd = nArg - 1; |
| 5760 | int i; |
| 5761 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5762 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5763 | if( nArg>=3 ){ |
| 5764 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 5765 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 5766 | } |
| 5767 | if( iSes<p->nSession ){ |
| 5768 | pSession = &p->aSession[iSes]; |
| 5769 | azCmd++; |
| 5770 | nCmd--; |
| 5771 | }else{ |
| 5772 | pSession = &p->aSession[0]; |
| 5773 | iSes = 0; |
| 5774 | } |
| 5775 | } |
| 5776 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5777 | /* .session attach TABLE |
| 5778 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 5779 | ** table so that it is never filtered. |
| 5780 | */ |
| 5781 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 5782 | if( nCmd!=2 ) goto session_syntax_error; |
| 5783 | if( pSession->p==0 ){ |
| 5784 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5785 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5786 | }else{ |
| 5787 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 5788 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5789 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5790 | rc = 0; |
| 5791 | } |
| 5792 | } |
| 5793 | }else |
| 5794 | |
| 5795 | /* .session changeset FILE |
| 5796 | ** .session patchset FILE |
| 5797 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 5798 | */ |
| 5799 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 5800 | FILE *out = 0; |
| 5801 | if( nCmd!=2 ) goto session_syntax_error; |
| 5802 | if( pSession->p==0 ) goto session_not_open; |
| 5803 | out = fopen(azCmd[1], "wb"); |
| 5804 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5805 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5806 | }else{ |
| 5807 | int szChng; |
| 5808 | void *pChng; |
| 5809 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5810 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5811 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 5812 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 5813 | } |
| 5814 | if( rc ){ |
| 5815 | printf("Error: error code %d\n", rc); |
| 5816 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5817 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5818 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5819 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5820 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5821 | szChng); |
| 5822 | } |
| 5823 | sqlite3_free(pChng); |
| 5824 | fclose(out); |
| 5825 | } |
| 5826 | }else |
| 5827 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5828 | /* .session close |
| 5829 | ** Close the identified session |
| 5830 | */ |
| 5831 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5832 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5833 | if( p->nSession ){ |
| 5834 | session_close(pSession); |
| 5835 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 5836 | } |
| 5837 | }else |
| 5838 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5839 | /* .session enable ?BOOLEAN? |
| 5840 | ** Query or set the enable flag |
| 5841 | */ |
| 5842 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 5843 | int ii; |
| 5844 | if( nCmd>2 ) goto session_syntax_error; |
| 5845 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5846 | if( p->nSession ){ |
| 5847 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5848 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 5849 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5850 | } |
| 5851 | }else |
| 5852 | |
| 5853 | /* .session filter GLOB .... |
| 5854 | ** Set a list of GLOB patterns of table names to be excluded. |
| 5855 | */ |
| 5856 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 5857 | int ii, nByte; |
| 5858 | if( nCmd<2 ) goto session_syntax_error; |
| 5859 | if( p->nSession ){ |
| 5860 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 5861 | sqlite3_free(pSession->azFilter[ii]); |
| 5862 | } |
| 5863 | sqlite3_free(pSession->azFilter); |
| 5864 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 5865 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 5866 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5867 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5868 | exit(1); |
| 5869 | } |
| 5870 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5871 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5872 | } |
| 5873 | pSession->nFilter = ii-1; |
| 5874 | } |
| 5875 | }else |
| 5876 | |
| 5877 | /* .session indirect ?BOOLEAN? |
| 5878 | ** Query or set the indirect flag |
| 5879 | */ |
| 5880 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 5881 | int ii; |
| 5882 | if( nCmd>2 ) goto session_syntax_error; |
| 5883 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 5884 | if( p->nSession ){ |
| 5885 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5886 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 5887 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5888 | } |
| 5889 | }else |
| 5890 | |
| 5891 | /* .session isempty |
| 5892 | ** Determine if the session is empty |
| 5893 | */ |
| 5894 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 5895 | int ii; |
| 5896 | if( nCmd!=1 ) goto session_syntax_error; |
| 5897 | if( p->nSession ){ |
| 5898 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5899 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 5900 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5901 | } |
| 5902 | }else |
| 5903 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5904 | /* .session list |
| 5905 | ** List all currently open sessions |
| 5906 | */ |
| 5907 | if( strcmp(azCmd[0],"list")==0 ){ |
| 5908 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5909 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5910 | } |
| 5911 | }else |
| 5912 | |
| 5913 | /* .session open DB NAME |
| 5914 | ** Open a new session called NAME on the attached database DB. |
| 5915 | ** DB is normally "main". |
| 5916 | */ |
| 5917 | if( strcmp(azCmd[0],"open")==0 ){ |
| 5918 | char *zName; |
| 5919 | if( nCmd!=3 ) goto session_syntax_error; |
| 5920 | zName = azCmd[2]; |
| 5921 | if( zName[0]==0 ) goto session_syntax_error; |
| 5922 | for(i=0; i<p->nSession; i++){ |
| 5923 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5924 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5925 | goto meta_command_exit; |
| 5926 | } |
| 5927 | } |
| 5928 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5929 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5930 | goto meta_command_exit; |
| 5931 | } |
| 5932 | pSession = &p->aSession[p->nSession]; |
| 5933 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 5934 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 5935 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 5936 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5937 | goto meta_command_exit; |
| 5938 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 5939 | pSession->nFilter = 0; |
| 5940 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 5941 | p->nSession++; |
| 5942 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 5943 | }else |
| 5944 | /* If no command name matches, show a syntax error */ |
| 5945 | session_syntax_error: |
| 5946 | session_help(p); |
| 5947 | }else |
| 5948 | #endif |
| 5949 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5950 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5951 | /* Undocumented commands for internal testing. Subject to change |
| 5952 | ** without notice. */ |
| 5953 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 5954 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 5955 | int i, v; |
| 5956 | for(i=1; i<nArg; i++){ |
| 5957 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5958 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5959 | } |
| 5960 | } |
| 5961 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 5962 | int i; sqlite3_int64 v; |
| 5963 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5964 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5965 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5966 | 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] | 5967 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5968 | } |
| 5969 | } |
| 5970 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 5971 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5972 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 5973 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 5974 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 5975 | int bVerbose = 0; /* Verbose output */ |
| 5976 | int bSelftestExists; /* True if SELFTEST already exists */ |
| 5977 | char **azTest = 0; /* Content of the SELFTEST table */ |
| 5978 | int nRow = 0; /* Number of rows in the SELFTEST table */ |
| 5979 | int nCol = 4; /* Number of columns in the SELFTEST table */ |
| 5980 | int i; /* Loop counter */ |
| 5981 | int nTest = 0; /* Number of tests runs */ |
| 5982 | int nErr = 0; /* Number of errors seen */ |
| 5983 | ShellText str; /* Answer for a query */ |
| 5984 | static char *azDefaultTest[] = { |
| 5985 | 0, 0, 0, 0, |
| 5986 | "0", "memo", "Missing SELFTEST table - default checks only", "", |
| 5987 | "1", "run", "PRAGMA integrity_check", "ok" |
| 5988 | }; |
| 5989 | static const int nDefaultRow = 2; |
| 5990 | |
| 5991 | open_db(p,0); |
| 5992 | for(i=1; i<nArg; i++){ |
| 5993 | const char *z = azArg[i]; |
| 5994 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 5995 | if( strcmp(z,"-init")==0 ){ |
| 5996 | bIsInit = 1; |
| 5997 | }else |
| 5998 | if( strcmp(z,"-v")==0 ){ |
| 5999 | bVerbose++; |
| 6000 | }else |
| 6001 | { |
| 6002 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 6003 | azArg[i], azArg[0]); |
| 6004 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 6005 | rc = 1; |
| 6006 | goto meta_command_exit; |
| 6007 | } |
| 6008 | } |
| 6009 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 6010 | != SQLITE_OK ){ |
| 6011 | bSelftestExists = 0; |
| 6012 | }else{ |
| 6013 | bSelftestExists = 1; |
| 6014 | } |
| 6015 | if( bIsInit ){ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6016 | createSelftestTable(p); |
| 6017 | bSelftestExists = 1; |
| 6018 | } |
| 6019 | if( bSelftestExists ){ |
| 6020 | rc = sqlite3_get_table(p->db, |
| 6021 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 6022 | &azTest, &nRow, &nCol, 0); |
| 6023 | if( rc ){ |
| 6024 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 6025 | rc = 1; |
| 6026 | sqlite3_free_table(azTest); |
| 6027 | goto meta_command_exit; |
| 6028 | }else if( nRow==0 ){ |
| 6029 | sqlite3_free_table(azTest); |
| 6030 | azTest = azDefaultTest; |
| 6031 | nRow = nDefaultRow; |
| 6032 | } |
| 6033 | }else{ |
| 6034 | azTest = azDefaultTest; |
| 6035 | nRow = nDefaultRow; |
| 6036 | } |
| 6037 | initText(&str); |
| 6038 | appendText(&str, "x", 0); |
| 6039 | for(i=1; i<=nRow; i++){ |
| 6040 | int tno = atoi(azTest[i*nCol]); |
| 6041 | const char *zOp = azTest[i*nCol+1]; |
| 6042 | const char *zSql = azTest[i*nCol+2]; |
| 6043 | const char *zAns = azTest[i*nCol+3]; |
| 6044 | |
| 6045 | if( bVerbose>0 ){ |
| 6046 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 6047 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 6048 | sqlite3_free(zQuote); |
| 6049 | } |
| 6050 | if( strcmp(zOp,"memo")==0 ){ |
| 6051 | utf8_printf(p->out, "%s\n", zSql); |
| 6052 | }else |
| 6053 | if( strcmp(zOp,"run")==0 ){ |
| 6054 | char *zErrMsg = 0; |
| 6055 | str.n = 0; |
| 6056 | str.z[0] = 0; |
| 6057 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 6058 | nTest++; |
| 6059 | if( bVerbose ){ |
| 6060 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 6061 | } |
| 6062 | if( rc || zErrMsg ){ |
| 6063 | nErr++; |
| 6064 | rc = 1; |
| 6065 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 6066 | sqlite3_free(zErrMsg); |
| 6067 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 6068 | nErr++; |
| 6069 | rc = 1; |
| 6070 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 6071 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 6072 | } |
| 6073 | }else |
| 6074 | { |
| 6075 | utf8_printf(stderr, |
| 6076 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 6077 | rc = 1; |
| 6078 | break; |
| 6079 | } |
| 6080 | } |
| 6081 | freeText(&str); |
| 6082 | if( azTest!=azDefaultTest ) sqlite3_free_table(azTest); |
| 6083 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 6084 | }else |
| 6085 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6086 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6087 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6088 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6089 | rc = 1; |
| 6090 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6091 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6092 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 6093 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6094 | } |
| 6095 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6096 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 6097 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6098 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6099 | }else |
| 6100 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6101 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 6102 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 6103 | int i; /* Loop counter */ |
| 6104 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6105 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6106 | int iSize = 224; /* Hash algorithm to use */ |
| 6107 | int bDebug = 0; /* Only show the query that would have run */ |
| 6108 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 6109 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6110 | char *zSep; /* Separator */ |
| 6111 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6112 | ShellText sQuery; /* Set of queries used to read all content */ |
drh | f80d4ff | 2017-03-08 18:06:20 +0000 | [diff] [blame] | 6113 | open_db(p, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6114 | for(i=1; i<nArg; i++){ |
| 6115 | const char *z = azArg[i]; |
| 6116 | if( z[0]=='-' ){ |
| 6117 | z++; |
| 6118 | if( z[0]=='-' ) z++; |
| 6119 | if( strcmp(z,"schema")==0 ){ |
| 6120 | bSchema = 1; |
| 6121 | }else |
| 6122 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 6123 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
| 6124 | ){ |
| 6125 | iSize = atoi(&z[5]); |
| 6126 | }else |
| 6127 | if( strcmp(z,"debug")==0 ){ |
| 6128 | bDebug = 1; |
| 6129 | }else |
| 6130 | { |
| 6131 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6132 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6133 | raw_printf(stderr, "Should be one of: --schema" |
| 6134 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 6135 | rc = 1; |
| 6136 | goto meta_command_exit; |
| 6137 | } |
| 6138 | }else if( zLike ){ |
| 6139 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 6140 | rc = 1; |
| 6141 | goto meta_command_exit; |
| 6142 | }else{ |
| 6143 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6144 | bSeparate = 1; |
| 6145 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6146 | } |
| 6147 | } |
| 6148 | if( bSchema ){ |
| 6149 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6150 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6151 | " UNION ALL SELECT 'sqlite_master'" |
| 6152 | " ORDER BY 1 collate nocase"; |
| 6153 | }else{ |
| 6154 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 6155 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 6156 | " AND name NOT LIKE 'sqlite_%'" |
| 6157 | " ORDER BY 1 collate nocase"; |
| 6158 | } |
| 6159 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6160 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6161 | initText(&sSql); |
| 6162 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 6163 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6164 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 6165 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 6166 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 6167 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 6168 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 6169 | appendText(&sQuery,zTab,'"'); |
| 6170 | appendText(&sQuery," NOT INDEXED;", 0); |
| 6171 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 6172 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 6173 | " ORDER BY name;", 0); |
| 6174 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 6175 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 6176 | " ORDER BY name;", 0); |
| 6177 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 6178 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 6179 | " ORDER BY tbl,idx;", 0); |
| 6180 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 6181 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 6182 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 6183 | appendText(&sQuery, zTab, 0); |
| 6184 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 6185 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6186 | appendText(&sSql, zSep, 0); |
| 6187 | appendText(&sSql, sQuery.z, '\''); |
| 6188 | sQuery.n = 0; |
| 6189 | appendText(&sSql, ",", 0); |
| 6190 | appendText(&sSql, zTab, '\''); |
| 6191 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6192 | } |
| 6193 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6194 | if( bSeparate ){ |
| 6195 | zSql = sqlite3_mprintf( |
| 6196 | "%s))" |
| 6197 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 6198 | " FROM [sha3sum$query]", |
| 6199 | sSql.z, iSize); |
| 6200 | }else{ |
| 6201 | zSql = sqlite3_mprintf( |
| 6202 | "%s))" |
| 6203 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 6204 | " FROM [sha3sum$query]", |
| 6205 | sSql.z, iSize); |
| 6206 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6207 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6208 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6209 | if( bDebug ){ |
| 6210 | utf8_printf(p->out, "%s\n", zSql); |
| 6211 | }else{ |
| 6212 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 6213 | } |
| 6214 | sqlite3_free(zSql); |
| 6215 | }else |
| 6216 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6217 | if( c=='s' |
| 6218 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6219 | ){ |
| 6220 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6221 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6222 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6223 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6224 | rc = 1; |
| 6225 | goto meta_command_exit; |
| 6226 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6227 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6228 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 6229 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 6230 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6231 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 6232 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6233 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6234 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 6235 | }else |
| 6236 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6237 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6238 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6239 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6240 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6241 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6242 | rc = 1; |
| 6243 | goto meta_command_exit; |
| 6244 | } |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6245 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 6246 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6247 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6248 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 6249 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6250 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6251 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 6252 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6253 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6254 | raw_printf(p->out, "\n"); |
| 6255 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6256 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6257 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6258 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6259 | raw_printf(p->out, "\n"); |
| 6260 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6261 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6262 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 6263 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6264 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6265 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6266 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6267 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6268 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6269 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 6270 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6271 | }else |
| 6272 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6273 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 6274 | if( nArg==2 ){ |
| 6275 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6276 | }else if( nArg==1 ){ |
| 6277 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6278 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 6279 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6280 | rc = 1; |
| 6281 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 6282 | }else |
| 6283 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6284 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 6285 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 6286 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 6287 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6288 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6289 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6290 | int nRow, nAlloc; |
| 6291 | char *zSql = 0; |
| 6292 | int ii; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6293 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6294 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6295 | if( rc ) return shellDatabaseError(p->db); |
| 6296 | |
| 6297 | /* Create an SQL statement to query for the list of tables in the |
| 6298 | ** main and all attached databases where the table name matches the |
| 6299 | ** LIKE pattern bound to variable "?1". */ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6300 | if( c=='t' ){ |
| 6301 | zSql = sqlite3_mprintf( |
| 6302 | "SELECT name FROM sqlite_master" |
| 6303 | " WHERE type IN ('table','view')" |
| 6304 | " AND name NOT LIKE 'sqlite_%%'" |
| 6305 | " AND name LIKE ?1"); |
| 6306 | }else if( nArg>2 ){ |
| 6307 | /* It is an historical accident that the .indexes command shows an error |
| 6308 | ** when called with the wrong number of arguments whereas the .tables |
| 6309 | ** command does not. */ |
| 6310 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 6311 | rc = 1; |
| 6312 | goto meta_command_exit; |
| 6313 | }else{ |
| 6314 | zSql = sqlite3_mprintf( |
| 6315 | "SELECT name FROM sqlite_master" |
| 6316 | " WHERE type='index'" |
| 6317 | " AND tbl_name LIKE ?1"); |
| 6318 | } |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6319 | for(ii=0; zSql && sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6320 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | a4b81d2 | 2016-12-24 18:04:28 +0000 | [diff] [blame] | 6321 | if( zDbName==0 || ii==0 ) continue; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6322 | if( c=='t' ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6323 | zSql = sqlite3_mprintf( |
| 6324 | "%z UNION ALL " |
| 6325 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6326 | " WHERE type IN ('table','view')" |
| 6327 | " AND name NOT LIKE 'sqlite_%%'" |
| 6328 | " AND name LIKE ?1", zSql, zDbName, zDbName); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 6329 | }else{ |
| 6330 | zSql = sqlite3_mprintf( |
| 6331 | "%z UNION ALL " |
| 6332 | "SELECT '%q.' || name FROM \"%w\".sqlite_master" |
| 6333 | " WHERE type='index'" |
| 6334 | " AND tbl_name LIKE ?1", zSql, zDbName, zDbName); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6335 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 6336 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6337 | rc = sqlite3_finalize(pStmt); |
| 6338 | if( zSql && rc==SQLITE_OK ){ |
| 6339 | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| 6340 | if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6341 | } |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6342 | sqlite3_free(zSql); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6343 | if( !zSql ) return shellNomemError(); |
| 6344 | if( rc ) return shellDatabaseError(p->db); |
| 6345 | |
| 6346 | /* Run the SQL statement prepared by the above block. Store the results |
| 6347 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6348 | nRow = nAlloc = 0; |
| 6349 | azResult = 0; |
| 6350 | if( nArg>1 ){ |
| 6351 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6352 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6353 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 6354 | } |
| 6355 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6356 | if( nRow>=nAlloc ){ |
| 6357 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6358 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6359 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6360 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6361 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6362 | break; |
| 6363 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6364 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6365 | azResult = azNew; |
| 6366 | } |
| 6367 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6368 | if( 0==azResult[nRow] ){ |
| 6369 | rc = shellNomemError(); |
| 6370 | break; |
| 6371 | } |
| 6372 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6373 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6374 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 6375 | rc = shellDatabaseError(p->db); |
| 6376 | } |
| 6377 | |
| 6378 | /* Pretty-print the contents of array azResult[] to the output */ |
| 6379 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6380 | int len, maxlen = 0; |
| 6381 | int i, j; |
| 6382 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6383 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6384 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6385 | if( len>maxlen ) maxlen = len; |
| 6386 | } |
| 6387 | nPrintCol = 80/(maxlen+2); |
| 6388 | if( nPrintCol<1 ) nPrintCol = 1; |
| 6389 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 6390 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6391 | for(j=i; j<nRow; j+=nPrintRow){ |
| 6392 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6393 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 6394 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6395 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6396 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 6397 | } |
| 6398 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 6399 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 6400 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 6401 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6402 | }else |
| 6403 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6404 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 6405 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 6406 | output_reset(p); |
| 6407 | p->out = output_file_open("testcase-out.txt"); |
| 6408 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6409 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6410 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6411 | if( nArg>=2 ){ |
| 6412 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 6413 | }else{ |
| 6414 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 6415 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6416 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 6417 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6418 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6419 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6420 | static const struct { |
| 6421 | const char *zCtrlName; /* Name of a test-control option */ |
| 6422 | int ctrlCode; /* Integer code for that option */ |
| 6423 | } aCtrl[] = { |
| 6424 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 6425 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 6426 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 6427 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 6428 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 6429 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 6430 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 6431 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 6432 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 6433 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 6434 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 6435 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6436 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6437 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 6438 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6439 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6440 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6441 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6442 | int rc2 = 0; |
| 6443 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6444 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6445 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6446 | /* convert testctrl text option to value. allow any unique prefix |
| 6447 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6448 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6449 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6450 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6451 | if( testctrl<0 ){ |
| 6452 | testctrl = aCtrl[i].ctrlCode; |
| 6453 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6454 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6455 | testctrl = -1; |
| 6456 | break; |
| 6457 | } |
| 6458 | } |
| 6459 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6460 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6461 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6462 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6463 | }else{ |
| 6464 | switch(testctrl){ |
| 6465 | |
| 6466 | /* sqlite3_test_control(int, db, int) */ |
| 6467 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6468 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6469 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6470 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6471 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6472 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6473 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6474 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6475 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6476 | } |
| 6477 | break; |
| 6478 | |
| 6479 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6480 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 6481 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6482 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 6483 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6484 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6485 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6486 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6487 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6488 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 6489 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6490 | } |
| 6491 | break; |
| 6492 | |
| 6493 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6494 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6495 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 6496 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6497 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6498 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6499 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6500 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6501 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6502 | } |
| 6503 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6504 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6505 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6506 | case SQLITE_TESTCTRL_ASSERT: |
| 6507 | case SQLITE_TESTCTRL_ALWAYS: |
| 6508 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6509 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6510 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6511 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6512 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6513 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6514 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 6515 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6516 | } |
| 6517 | break; |
| 6518 | |
| 6519 | /* sqlite3_test_control(int, char *) */ |
| 6520 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6521 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6522 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6523 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6524 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6525 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6526 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6527 | utf8_printf(stderr, |
| 6528 | "Error: testctrl %s takes a single char * option\n", |
| 6529 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6530 | } |
| 6531 | break; |
| 6532 | #endif |
| 6533 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6534 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6535 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6536 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 6537 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6538 | integerValue(azArg[3]), |
| 6539 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6540 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6541 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6542 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 6543 | } |
| 6544 | break; |
| 6545 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6546 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 6547 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 6548 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 6549 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6550 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6551 | utf8_printf(stderr, |
| 6552 | "Error: CLI support for testctrl %s not implemented\n", |
| 6553 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6554 | break; |
| 6555 | } |
| 6556 | } |
| 6557 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 6558 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 6559 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6560 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6561 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6562 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6563 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6564 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6565 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 6566 | if( nArg==2 ){ |
| 6567 | enableTimer = booleanValue(azArg[1]); |
| 6568 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6569 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6570 | enableTimer = 0; |
| 6571 | } |
| 6572 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6573 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6574 | rc = 1; |
| 6575 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6576 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6577 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6578 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6579 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6580 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6581 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6582 | rc = 1; |
| 6583 | goto meta_command_exit; |
| 6584 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 6585 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6586 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 6587 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6588 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6589 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6590 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 6591 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6592 | } |
| 6593 | #endif |
| 6594 | }else |
| 6595 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6596 | #if SQLITE_USER_AUTHENTICATION |
| 6597 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 6598 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6599 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6600 | rc = 1; |
| 6601 | goto meta_command_exit; |
| 6602 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 6603 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6604 | if( strcmp(azArg[1],"login")==0 ){ |
| 6605 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6606 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6607 | rc = 1; |
| 6608 | goto meta_command_exit; |
| 6609 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6610 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 6611 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6612 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6613 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6614 | rc = 1; |
| 6615 | } |
| 6616 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 6617 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6618 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6619 | rc = 1; |
| 6620 | goto meta_command_exit; |
| 6621 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6622 | rc = sqlite3_user_add(p->db, azArg[2], |
| 6623 | azArg[3], (int)strlen(azArg[3]), |
| 6624 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6625 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6626 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6627 | rc = 1; |
| 6628 | } |
| 6629 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 6630 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6631 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6632 | rc = 1; |
| 6633 | goto meta_command_exit; |
| 6634 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 6635 | rc = sqlite3_user_change(p->db, azArg[2], |
| 6636 | azArg[3], (int)strlen(azArg[3]), |
| 6637 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6638 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6639 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6640 | rc = 1; |
| 6641 | } |
| 6642 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 6643 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6644 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6645 | rc = 1; |
| 6646 | goto meta_command_exit; |
| 6647 | } |
| 6648 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 6649 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6650 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6651 | rc = 1; |
| 6652 | } |
| 6653 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6654 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6655 | rc = 1; |
| 6656 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6657 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 6658 | }else |
| 6659 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 6660 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6661 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6662 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 6663 | sqlite3_libversion(), sqlite3_sourceid()); |
| 6664 | }else |
| 6665 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6666 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 6667 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 6668 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6669 | if( p->db ){ |
| 6670 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 6671 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6672 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 6673 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6674 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6675 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | }else |
| 6679 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 6680 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 6681 | sqlite3_vfs *pVfs; |
| 6682 | sqlite3_vfs *pCurrent = 0; |
| 6683 | if( p->db ){ |
| 6684 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 6685 | } |
| 6686 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 6687 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 6688 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 6689 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 6690 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 6691 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 6692 | if( pVfs->pNext ){ |
| 6693 | raw_printf(p->out, "-----------------------------------\n"); |
| 6694 | } |
| 6695 | } |
| 6696 | }else |
| 6697 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6698 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 6699 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 6700 | char *zVfsName = 0; |
| 6701 | if( p->db ){ |
| 6702 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 6703 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6704 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6705 | sqlite3_free(zVfsName); |
| 6706 | } |
| 6707 | } |
| 6708 | }else |
| 6709 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6710 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 6711 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6712 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 6713 | }else |
| 6714 | #endif |
| 6715 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6716 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6717 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6718 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6719 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6720 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6721 | } |
| 6722 | }else |
| 6723 | |
| 6724 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6725 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6726 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6727 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6728 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6729 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6730 | meta_command_exit: |
| 6731 | if( p->outCount ){ |
| 6732 | p->outCount--; |
| 6733 | if( p->outCount==0 ) output_reset(p); |
| 6734 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6735 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6738 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6739 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 6740 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6741 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6742 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6743 | int i; |
| 6744 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 6745 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6749 | ** Test to see if a line consists entirely of whitespace. |
| 6750 | */ |
| 6751 | static int _all_whitespace(const char *z){ |
| 6752 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6753 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 6754 | if( *z=='/' && z[1]=='*' ){ |
| 6755 | z += 2; |
| 6756 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 6757 | if( *z==0 ) return 0; |
| 6758 | z++; |
| 6759 | continue; |
| 6760 | } |
| 6761 | if( *z=='-' && z[1]=='-' ){ |
| 6762 | z += 2; |
| 6763 | while( *z && *z!='\n' ){ z++; } |
| 6764 | if( *z==0 ) return 1; |
| 6765 | continue; |
| 6766 | } |
| 6767 | return 0; |
| 6768 | } |
| 6769 | return 1; |
| 6770 | } |
| 6771 | |
| 6772 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6773 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 6774 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 6775 | ** as is the Oracle "/". |
| 6776 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6777 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6778 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6779 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 6780 | return 1; /* Oracle */ |
| 6781 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6782 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6783 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6784 | return 1; /* SQL Server */ |
| 6785 | } |
| 6786 | return 0; |
| 6787 | } |
| 6788 | |
| 6789 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6790 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 6791 | ** ends in the middle of a string literal or C-style comment. |
| 6792 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6793 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 6794 | int rc; |
| 6795 | if( zSql==0 ) return 1; |
| 6796 | zSql[nSql] = ';'; |
| 6797 | zSql[nSql+1] = 0; |
| 6798 | rc = sqlite3_complete(zSql); |
| 6799 | zSql[nSql] = 0; |
| 6800 | return rc; |
| 6801 | } |
| 6802 | |
| 6803 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6804 | ** Run a single line of SQL |
| 6805 | */ |
| 6806 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 6807 | int rc; |
| 6808 | char *zErrMsg = 0; |
| 6809 | |
| 6810 | open_db(p, 0); |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6811 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6812 | BEGIN_TIMER; |
| 6813 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 6814 | END_TIMER; |
| 6815 | if( rc || zErrMsg ){ |
| 6816 | char zPrefix[100]; |
| 6817 | if( in!=0 || !stdin_is_interactive ){ |
| 6818 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 6819 | "Error: near line %d:", startline); |
| 6820 | }else{ |
| 6821 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 6822 | } |
| 6823 | if( zErrMsg!=0 ){ |
| 6824 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 6825 | sqlite3_free(zErrMsg); |
| 6826 | zErrMsg = 0; |
| 6827 | }else{ |
| 6828 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 6829 | } |
| 6830 | return 1; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6831 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6832 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 6833 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 6834 | } |
| 6835 | return 0; |
| 6836 | } |
| 6837 | |
| 6838 | |
| 6839 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6840 | ** Read input from *in and process it. If *in==0 then input |
| 6841 | ** is interactive - the user is typing it it. Otherwise, input |
| 6842 | ** is coming from a file or device. A prompt is issued and history |
| 6843 | ** is saved only if input is interactive. An interrupt signal will |
| 6844 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6845 | ** |
| 6846 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6847 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6848 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6849 | char *zLine = 0; /* A single input line */ |
| 6850 | char *zSql = 0; /* Accumulated SQL text */ |
| 6851 | int nLine; /* Length of current line */ |
| 6852 | int nSql = 0; /* Bytes of zSql[] used */ |
| 6853 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 6854 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6855 | int rc; /* Error code */ |
| 6856 | int errCnt = 0; /* Number of errors seen */ |
| 6857 | int lineno = 0; /* Current line number */ |
| 6858 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6859 | |
| 6860 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 6861 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6862 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6863 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6864 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 6865 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 6866 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6867 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6868 | if( seenInterrupt ){ |
| 6869 | if( in!=0 ) break; |
| 6870 | seenInterrupt = 0; |
| 6871 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 6872 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6873 | if( nSql==0 && _all_whitespace(zLine) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6874 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 6875 | continue; |
| 6876 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 6877 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6878 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6879 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6880 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6881 | break; |
| 6882 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 6883 | errCnt++; |
| 6884 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6885 | continue; |
| 6886 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6887 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6888 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 6889 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6890 | nLine = strlen30(zLine); |
| 6891 | if( nSql+nLine+2>=nAlloc ){ |
| 6892 | nAlloc = nSql+nLine+100; |
| 6893 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6894 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6895 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6896 | exit(1); |
| 6897 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6898 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6899 | nSqlPrior = nSql; |
| 6900 | if( nSql==0 ){ |
| 6901 | int i; |
| 6902 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 6903 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6904 | memcpy(zSql, zLine+i, nLine+1-i); |
| 6905 | startline = lineno; |
| 6906 | nSql = nLine-i; |
| 6907 | }else{ |
| 6908 | zSql[nSql++] = '\n'; |
| 6909 | memcpy(zSql+nSql, zLine, nLine+1); |
| 6910 | nSql += nLine; |
| 6911 | } |
| 6912 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 6913 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6914 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6915 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6916 | if( p->outCount ){ |
| 6917 | output_reset(p); |
| 6918 | p->outCount = 0; |
| 6919 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 6920 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 6921 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 6922 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6923 | } |
| 6924 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6925 | if( nSql && !_all_whitespace(zSql) ){ |
| 6926 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6927 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 6928 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 6929 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 6930 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6933 | /* |
| 6934 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6935 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6936 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6937 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6938 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 6939 | if( clearFlag ){ |
| 6940 | free(home_dir); |
| 6941 | home_dir = 0; |
| 6942 | return 0; |
| 6943 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6944 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6945 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 6946 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 6947 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 6948 | { |
| 6949 | struct passwd *pwent; |
| 6950 | uid_t uid = getuid(); |
| 6951 | if( (pwent=getpwuid(uid)) != NULL) { |
| 6952 | home_dir = pwent->pw_dir; |
| 6953 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6954 | } |
| 6955 | #endif |
| 6956 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6957 | #if defined(_WIN32_WCE) |
| 6958 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 6959 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 6960 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6961 | #else |
| 6962 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6963 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6964 | if (!home_dir) { |
| 6965 | home_dir = getenv("USERPROFILE"); |
| 6966 | } |
| 6967 | #endif |
| 6968 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6969 | if (!home_dir) { |
| 6970 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 6973 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6974 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6975 | char *zDrive, *zPath; |
| 6976 | int n; |
| 6977 | zDrive = getenv("HOMEDRIVE"); |
| 6978 | zPath = getenv("HOMEPATH"); |
| 6979 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6980 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 6981 | home_dir = malloc( n ); |
| 6982 | if( home_dir==0 ) return 0; |
| 6983 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 6984 | return home_dir; |
| 6985 | } |
| 6986 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6987 | } |
| 6988 | #endif |
| 6989 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 6990 | #endif /* !_WIN32_WCE */ |
| 6991 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6992 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 6993 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6994 | char *z = malloc( n ); |
| 6995 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6996 | home_dir = z; |
| 6997 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 6998 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 6999 | return home_dir; |
| 7000 | } |
| 7001 | |
| 7002 | /* |
| 7003 | ** Read input from the file given by sqliterc_override. Or if that |
| 7004 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7005 | ** |
| 7006 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7007 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7008 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7009 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7010 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 7011 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7012 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7013 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 7014 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7015 | FILE *in = NULL; |
| 7016 | |
| 7017 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7018 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7019 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7020 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7021 | " cannot read ~/.sqliterc\n"); |
| 7022 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7023 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 7024 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7025 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 7026 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7027 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 7028 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7029 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7030 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7031 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7032 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7033 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 7034 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7035 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7036 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7039 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7040 | ** Show available command line options |
| 7041 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7042 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7043 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7044 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7045 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7046 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7047 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7048 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7049 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7050 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7051 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7052 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 7053 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 7054 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7055 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7056 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7057 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7058 | " -line set output mode to 'line'\n" |
| 7059 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7060 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7061 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7062 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7063 | " -multiplex enable the multiplexor VFS\n" |
| 7064 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7065 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7066 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7067 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 7068 | " -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] | 7069 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7070 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7071 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7072 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7073 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 7074 | " -vfstrace enable tracing of all VFS calls\n" |
| 7075 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7076 | ; |
| 7077 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7078 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7079 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 7080 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 7081 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7082 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7083 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7084 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7085 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7086 | } |
| 7087 | exit(1); |
| 7088 | } |
| 7089 | |
| 7090 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7091 | ** Initialize the state information in data |
| 7092 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7093 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7094 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7095 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 7096 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7097 | memcpy(data->colSeparator,SEP_Column, 2); |
| 7098 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7099 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7100 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7101 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 7102 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7103 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7104 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 7105 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7108 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7109 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7110 | */ |
| 7111 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7112 | static void printBold(const char *zText){ |
| 7113 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 7114 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 7115 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 7116 | SetConsoleTextAttribute(out, |
| 7117 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 7118 | ); |
| 7119 | printf("%s", zText); |
| 7120 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7121 | } |
| 7122 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7123 | static void printBold(const char *zText){ |
| 7124 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7125 | } |
| 7126 | #endif |
| 7127 | |
| 7128 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7129 | ** Get the argument to an --option. Throw an error and die if no argument |
| 7130 | ** is available. |
| 7131 | */ |
| 7132 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 7133 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7134 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7135 | argv[0], argv[argc-1]); |
| 7136 | exit(1); |
| 7137 | } |
| 7138 | return argv[i]; |
| 7139 | } |
| 7140 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7141 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 7142 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 7143 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 7144 | # else |
| 7145 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 7146 | # endif |
| 7147 | #endif |
| 7148 | |
| 7149 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 7150 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7151 | #else |
| 7152 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7153 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7154 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7155 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7156 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7157 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7158 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7159 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7160 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7161 | int readStdin = 1; |
| 7162 | int nCmd = 0; |
| 7163 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7164 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7165 | setBinaryMode(stdin, 0); |
| 7166 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7167 | stdin_is_interactive = isatty(0); |
| 7168 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7169 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 7170 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7171 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7172 | 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] | 7173 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 7174 | exit(1); |
| 7175 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 7176 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7177 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7178 | #if !SQLITE_SHELL_IS_UTF8 |
| 7179 | sqlite3_initialize(); |
| 7180 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 7181 | if( argv==0 ){ |
| 7182 | raw_printf(stderr, "out of memory\n"); |
| 7183 | exit(1); |
| 7184 | } |
| 7185 | for(i=0; i<argc; i++){ |
| 7186 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 7187 | if( argv[i]==0 ){ |
| 7188 | raw_printf(stderr, "out of memory\n"); |
| 7189 | exit(1); |
| 7190 | } |
| 7191 | } |
| 7192 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 7193 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7194 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7195 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7196 | /* Make sure we have a valid signal handler early, before anything |
| 7197 | ** else is done. |
| 7198 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 7199 | #ifdef SIGINT |
| 7200 | signal(SIGINT, interrupt_handler); |
| 7201 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7202 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7203 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 7204 | { |
| 7205 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 7206 | ** of a C-function that will provide the name of the database file. Use |
| 7207 | ** this compile-time option to embed this shell program in larger |
| 7208 | ** applications. */ |
| 7209 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 7210 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 7211 | warnInmemoryDb = 0; |
| 7212 | } |
| 7213 | #endif |
| 7214 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7215 | /* Do an initial pass through the command-line argument to locate |
| 7216 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7217 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7218 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7219 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7220 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7221 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7222 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7223 | if( z[0]!='-' ){ |
| 7224 | if( data.zDbFilename==0 ){ |
| 7225 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7226 | }else{ |
| 7227 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 7228 | ** mean that nothing is read from stdin */ |
| 7229 | readStdin = 0; |
| 7230 | nCmd++; |
| 7231 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 7232 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7233 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7234 | exit(1); |
| 7235 | } |
| 7236 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7237 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7238 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7239 | if( z[1]=='-' ) z++; |
| 7240 | if( strcmp(z,"-separator")==0 |
| 7241 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7242 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7243 | || strcmp(z,"-cmd")==0 |
| 7244 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7245 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7246 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7247 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7248 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7249 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7250 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7251 | ** we do the actual processing of arguments later in a second pass. |
| 7252 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 7253 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7254 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 7255 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7256 | const char *zSize; |
| 7257 | sqlite3_int64 szHeap; |
| 7258 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7259 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7260 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7261 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7262 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 7263 | #else |
| 7264 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7265 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7266 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7267 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7268 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7269 | if( sz>400000 ) sz = 400000; |
| 7270 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7271 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7272 | if( n>10 ) n = 10; |
| 7273 | if( n<1 ) n = 1; |
| 7274 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 7275 | data.shellFlgs |= SHFLG_Scratch; |
| 7276 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7277 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7278 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7279 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7280 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7281 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 7282 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 7283 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7284 | data.shellFlgs |= SHFLG_Pagecache; |
| 7285 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7286 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7287 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7288 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 7289 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7290 | if( n<0 ) n = 0; |
| 7291 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 7292 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7293 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7294 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7295 | extern int vfstrace_register( |
| 7296 | const char *zTraceName, |
| 7297 | const char *zOldVfsName, |
| 7298 | int (*xOut)(const char*,void*), |
| 7299 | void *pOutArg, |
| 7300 | int makeDefault |
| 7301 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7302 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7303 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7304 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7305 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7306 | extern int sqlite3_multiple_initialize(const char*,int); |
| 7307 | sqlite3_multiplex_initialize(0, 1); |
| 7308 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7309 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 7310 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 7311 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7312 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7313 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7314 | if( pVfs ){ |
| 7315 | sqlite3_vfs_register(pVfs, 1); |
| 7316 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7317 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7318 | exit(1); |
| 7319 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7320 | } |
| 7321 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7322 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7323 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7324 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7325 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 7326 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7327 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 7328 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7329 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7330 | } |
| 7331 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 7332 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7333 | /* Go ahead and open the database file if it already exists. If the |
| 7334 | ** file does not exist, delay opening it. This prevents empty database |
| 7335 | ** files from being created if a user mistypes the database name argument |
| 7336 | ** to the sqlite command-line tool. |
| 7337 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 7338 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7339 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7342 | /* Process the initialization file if there is one. If no -init option |
| 7343 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 7344 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7345 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7346 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7347 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7348 | /* Make a second pass through the command-line argument and set |
| 7349 | ** options. This second pass is delayed until after the initialization |
| 7350 | ** file is processed so that the command-line arguments will override |
| 7351 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7352 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7353 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7354 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7355 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7356 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 7357 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7358 | i++; |
| 7359 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7360 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7361 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7362 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7363 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7364 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7365 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 7366 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7367 | }else if( strcmp(z,"-csv")==0 ){ |
| 7368 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7369 | memcpy(data.colSeparator,",",2); |
| 7370 | }else if( strcmp(z,"-ascii")==0 ){ |
| 7371 | data.mode = MODE_Ascii; |
| 7372 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7373 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7374 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7375 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7376 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7377 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7378 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7379 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7380 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 7381 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7382 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 7383 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7384 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7385 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7386 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7387 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7388 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7389 | }else if( strcmp(z,"-echo")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7390 | ShellSetFlag(&data, SHFLG_Echo); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 7391 | }else if( strcmp(z,"-eqp")==0 ){ |
| 7392 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7393 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 7394 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7395 | }else if( strcmp(z,"-stats")==0 ){ |
| 7396 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 7397 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 7398 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 7399 | }else if( strcmp(z,"-backslash")==0 ){ |
| 7400 | /* Undocumented command-line option: -backslash |
| 7401 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 7402 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 7403 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 7404 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7405 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7406 | }else if( strcmp(z,"-bail")==0 ){ |
| 7407 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7408 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7409 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 7410 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7411 | }else if( strcmp(z,"-interactive")==0 ){ |
| 7412 | stdin_is_interactive = 1; |
| 7413 | }else if( strcmp(z,"-batch")==0 ){ |
| 7414 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 7415 | }else if( strcmp(z,"-heap")==0 ){ |
| 7416 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7417 | }else if( strcmp(z,"-scratch")==0 ){ |
| 7418 | i+=2; |
| 7419 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 7420 | i+=2; |
| 7421 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 7422 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7423 | }else if( strcmp(z,"-mmap")==0 ){ |
| 7424 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7425 | }else if( strcmp(z,"-vfs")==0 ){ |
| 7426 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7427 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 7428 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 7429 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 7430 | #endif |
| 7431 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7432 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 7433 | i++; |
| 7434 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7435 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7436 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7437 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7438 | /* Run commands that follow -cmd first and separately from commands |
| 7439 | ** that simply appear on the command-line. This seems goofy. It would |
| 7440 | ** be better if all commands ran in the order that they appear. But |
| 7441 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7442 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7443 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7444 | if( z[0]=='.' ){ |
| 7445 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 7446 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7447 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7448 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7449 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 7450 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7451 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7452 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 7453 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7454 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7455 | if( bail_on_error ) return rc; |
| 7456 | } |
| 7457 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7458 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7459 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 7460 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7461 | return 1; |
| 7462 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7463 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 7464 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7465 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7466 | if( !readStdin ){ |
| 7467 | /* Run all arguments that do not begin with '-' as if they were separate |
| 7468 | ** command-line inputs, except for the argToSkip argument which contains |
| 7469 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7470 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7471 | for(i=0; i<nCmd; i++){ |
| 7472 | if( azCmd[i][0]=='.' ){ |
| 7473 | rc = do_meta_command(azCmd[i], &data); |
| 7474 | if( rc ) return rc==2 ? 0 : rc; |
| 7475 | }else{ |
| 7476 | open_db(&data, 0); |
| 7477 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 7478 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7479 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7480 | return rc!=0 ? rc : 1; |
| 7481 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7482 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7483 | return rc; |
| 7484 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 7485 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7486 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 7487 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7488 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 7489 | /* Run commands received from standard input |
| 7490 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7491 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7492 | char *zHome; |
| 7493 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7494 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7495 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 7496 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7497 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7498 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7499 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7500 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7501 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 7502 | printBold("transient in-memory database"); |
| 7503 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7504 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 7505 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7506 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7507 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7508 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7509 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7510 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7511 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7512 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7513 | if( zHistory ){ shell_read_history(zHistory); } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7514 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7515 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 7516 | shell_stifle_history(100); |
| 7517 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7518 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7519 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7520 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7521 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7522 | } |
| 7523 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 7524 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 7525 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 7526 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 7527 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 7528 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7529 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7530 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7531 | #if !SQLITE_SHELL_IS_UTF8 |
| 7532 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7533 | sqlite3_free(argv); |
| 7534 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7535 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7536 | } |